Skip to content

Commit

Permalink
Add RSA decryption OpenSSL binding
Browse files Browse the repository at this point in the history
  • Loading branch information
mbestavros committed Apr 11, 2019
1 parent fa01d11 commit 239b6e1
Show file tree
Hide file tree
Showing 2 changed files with 30 additions and 3 deletions.
23 changes: 22 additions & 1 deletion src/crypto.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ use openssl::error::ErrorStack;
use openssl::hash::MessageDigest;
use openssl::pkcs5;
use openssl::pkey::{PKey, Private, Public};
use openssl::rsa::Rsa;
use openssl::rsa::{Padding, Rsa};
use openssl::sign::Signer;
use std::error::Error;
use std::fmt;
Expand Down Expand Up @@ -62,6 +62,27 @@ pub fn rsa_generate(
Ok(Rsa::generate(key_size)?)
}

/*
* Inputs: OpenSSL RSA key
* ciphertext to be decrypted
* Output: decrypted plaintext
*
* Take in an RSA-encrypted ciphertext and an RSA private key and decrypt the
* ciphertext based on PKCS1 OAEP. Parameters match that of Python-Keylime.
*/
pub fn rsa_decrypt(
private_key: Rsa<Private>,
ciphertext: String,
) -> Result<String, KeylimeCryptoError> {
let mut dec_result = vec![0; private_key.size() as usize];
let dec_len = private_key.private_decrypt(
ciphertext.as_bytes(),
&mut dec_result,
Padding::PKCS1,
)?;
Ok(to_hex_string(dec_result[..dec_len].to_vec()))
}

/*
* Inputs: password to derive key
* shared salt
Expand Down
10 changes: 8 additions & 2 deletions src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -483,13 +483,19 @@ fn get_request_handler(
if let Err(e) =
set_response_content(200, "Success", response_map, my_response)
{
return emsg("Failed to edit the response content body.", Some(e));
return emsg(
"Failed to edit the response content body.",
Some(e),
);
}
} else {
if let Err(e) =
set_response_content(400, "Bad Request.", Map::new(), my_response)
{
return emsg("Failed to edit the response content body.", Some(e));
return emsg(
"Failed to edit the response content body.",
Some(e),
);
}
return emsg("Bad Request. Invalid request content.", None::<String>);
}
Expand Down

0 comments on commit 239b6e1

Please sign in to comment.