From be23ff3dce1b5360674bc3902b56b7ece7a70970 Mon Sep 17 00:00:00 2001 From: Cyberunner23 Date: Tue, 5 Jan 2016 11:23:14 -0500 Subject: [PATCH 1/3] Added PEM_read_bio_RSAPrivateKey and PEM_read_bio_RSA_PUBKEY --- openssl-sys/src/lib.rs | 3 +++ 1 file changed, 3 insertions(+) diff --git a/openssl-sys/src/lib.rs b/openssl-sys/src/lib.rs index f780b6d9..22c40d29 100644 --- a/openssl-sys/src/lib.rs +++ b/openssl-sys/src/lib.rs @@ -527,6 +527,9 @@ extern "C" { pub fn PEM_read_bio_PUBKEY(bio: *mut BIO, out: *mut *mut EVP_PKEY, callback: Option, user_data: *mut c_void) -> *mut X509; + pub fn PEM_read_bio_RSAPrivateKey(bio: *mut BIO, rsa: *mut *mut RSA, callback: Option, user_data: *mut c_void) -> *mut RSA; + pub fn PEM_read_bio_RSA_PUBKEY(bio: *mut BIO, rsa: *mut *mut RSA, callback: Option, user_data: *mut c_void) -> *mut RSA; + pub fn PEM_write_bio_PrivateKey(bio: *mut BIO, pkey: *mut EVP_PKEY, cipher: *const EVP_CIPHER, kstr: *mut c_char, klen: c_int, callback: Option, From 1d3277fbee500fda7974250fe58b49ec7b6ba284 Mon Sep 17 00:00:00 2001 From: Cyberunner23 Date: Tue, 5 Jan 2016 13:22:56 -0500 Subject: [PATCH 2/3] Added private_rsa_key_from_pem and public_rsa_key_from_pem. --- openssl/src/crypto/pkey.rs | 48 ++++++++++++++++++++++++++++++++++++++ 1 file changed, 48 insertions(+) diff --git a/openssl/src/crypto/pkey.rs b/openssl/src/crypto/pkey.rs index 10891224..d6f09931 100644 --- a/openssl/src/crypto/pkey.rs +++ b/openssl/src/crypto/pkey.rs @@ -118,6 +118,54 @@ impl PKey { } } + /// Reads an RSA private key from PEM, takes ownership of handle + pub fn private_rsa_key_from_pem(reader: &mut R) -> Result + where R: Read + { + let mut mem_bio = try!(MemBio::new()); + try!(io::copy(reader, &mut mem_bio).map_err(StreamError)); + + unsafe { + let rsa = try_ssl_null!(ffi::PEM_read_bio_RSAPrivateKey(mem_bio.get_handle(), + ptr::null_mut(), + None, + ptr::null_mut())); + let evp = ffi::EVP_PKEY_new(); + if ffi::EVP_PKEY_set1_RSA(evp, rsa) == 0 { + return Err(SslError::get()); + } + + Ok(PKey { + evp: evp, + parts: Parts::Public, + }) + } + } + + /// Reads an RSA public key from PEM, takes ownership of handle + pub fn public_rsa_key_from_pem(reader: &mut R) -> Result + where R: Read + { + let mut mem_bio = try!(MemBio::new()); + try!(io::copy(reader, &mut mem_bio).map_err(StreamError)); + + unsafe { + let rsa = try_ssl_null!(ffi::PEM_read_bio_RSA_PUBKEY(mem_bio.get_handle(), + ptr::null_mut(), + None, + ptr::null_mut())); + let evp = ffi::EVP_PKEY_new(); + if ffi::EVP_PKEY_set1_RSA(evp, rsa) == 0 { + return Err(SslError::get()); + } + + Ok(PKey { + evp: evp, + parts: Parts::Public, + }) + } + } + fn _tostr(&self, f: unsafe extern "C" fn(*mut ffi::RSA, *const *mut u8) -> c_int) -> Vec { unsafe { let rsa = ffi::EVP_PKEY_get1_RSA(self.evp); From c0b9a4c8ecafc212cd9a0418751b9df89fec9d1f Mon Sep 17 00:00:00 2001 From: Cyberunner23 Date: Sat, 9 Jan 2016 14:36:01 -0500 Subject: [PATCH 3/3] Added tests for private_rsa_key_from_pem() and public_rsa_key_from_pem() --- openssl-sys/src/lib.rs | 2 +- openssl/src/crypto/pkey.rs | 20 ++++++++++++++++++++ 2 files changed, 21 insertions(+), 1 deletion(-) diff --git a/openssl-sys/src/lib.rs b/openssl-sys/src/lib.rs index 22c40d29..5554d478 100644 --- a/openssl-sys/src/lib.rs +++ b/openssl-sys/src/lib.rs @@ -527,7 +527,7 @@ extern "C" { pub fn PEM_read_bio_PUBKEY(bio: *mut BIO, out: *mut *mut EVP_PKEY, callback: Option, user_data: *mut c_void) -> *mut X509; - pub fn PEM_read_bio_RSAPrivateKey(bio: *mut BIO, rsa: *mut *mut RSA, callback: Option, user_data: *mut c_void) -> *mut RSA; + pub fn PEM_read_bio_RSAPrivateKey(bio: *mut BIO, rsa: *mut *mut RSA, callback: Option, user_data: *mut c_void) -> *mut RSA; pub fn PEM_read_bio_RSA_PUBKEY(bio: *mut BIO, rsa: *mut *mut RSA, callback: Option, user_data: *mut c_void) -> *mut RSA; pub fn PEM_write_bio_PrivateKey(bio: *mut BIO, pkey: *mut EVP_PKEY, cipher: *const EVP_CIPHER, diff --git a/openssl/src/crypto/pkey.rs b/openssl/src/crypto/pkey.rs index d6f09931..fafee78b 100644 --- a/openssl/src/crypto/pkey.rs +++ b/openssl/src/crypto/pkey.rs @@ -661,6 +661,26 @@ mod tests { super::PKey::public_key_from_pem(&mut file).unwrap(); } + #[test] + fn test_private_rsa_key_from_pem() { + let key_path = Path::new("test/key.pem"); + let mut file = File::open(&key_path) + .ok() + .expect("Failed to open `test/key.pem`"); + + super::PKey::private_rsa_key_from_pem(&mut file).unwrap(); + } + + #[test] + fn test_public_rsa_key_from_pem() { + let key_path = Path::new("test/key.pem.pub"); + let mut file = File::open(&key_path) + .ok() + .expect("Failed to open `test/key.pem.pub`"); + + super::PKey::public_rsa_key_from_pem(&mut file).unwrap(); + } + #[test] fn test_private_encrypt() { let mut k0 = super::PKey::new();