diff options
| author | Benjamin Fry <[email protected]> | 2016-02-28 22:05:19 -0800 |
|---|---|---|
| committer | Benjamin Fry <[email protected]> | 2016-02-28 22:05:19 -0800 |
| commit | 3fb2c48c9834acafe8a1dfe33a5500430f332ebc (patch) | |
| tree | ecf306807222f0c305da55e56a1aee0dc9ebc8f5 /openssl/src/crypto/pkey.rs | |
| parent | review fixes, keep raw RSA initiallization private (diff) | |
| download | rust-openssl-3fb2c48c9834acafe8a1dfe33a5500430f332ebc.tar.xz rust-openssl-3fb2c48c9834acafe8a1dfe33a5500430f332ebc.zip | |
added public key material to the constructor
Diffstat (limited to 'openssl/src/crypto/pkey.rs')
| -rw-r--r-- | openssl/src/crypto/pkey.rs | 30 |
1 files changed, 27 insertions, 3 deletions
diff --git a/openssl/src/crypto/pkey.rs b/openssl/src/crypto/pkey.rs index df4ac709..cafd50ad 100644 --- a/openssl/src/crypto/pkey.rs +++ b/openssl/src/crypto/pkey.rs @@ -205,9 +205,10 @@ impl PKey { } } - /// pass ownership of the RSA key to this - pub fn set_rsa(&mut self, rsa: RSA) { + /// assign RSA key to this pkey + pub fn set_rsa(&mut self, rsa: &RSA) { unsafe { + // this needs to be a reference as the set1_RSA ups the reference count let rsa_ptr = rsa.as_ptr(); if ffi::EVP_PKEY_set1_RSA(self.evp, rsa_ptr) == 1 { if rsa.has_e() && rsa.has_n() { @@ -222,7 +223,7 @@ impl PKey { unsafe { let evp_pkey: *mut ffi::EVP_PKEY = self.evp; // this is safe as the ffi increments a reference counter to the internal key - RSA::with_raw(ffi::EVP_PKEY_get1_RSA(evp_pkey)) + RSA::from_raw(ffi::EVP_PKEY_get1_RSA(evp_pkey)) } } @@ -625,6 +626,7 @@ mod tests { use std::path::Path; use std::fs::File; use crypto::hash::Type::{MD5, SHA1}; + use crypto::rsa::RSA; #[test] fn test_gen_pub() { @@ -812,6 +814,28 @@ mod tests { } #[test] + fn test_public_key_from_raw() { + let mut k0 = super::PKey::new(); + let mut k1 = super::PKey::new(); + let msg = vec![0xdeu8, 0xadu8, 0xd0u8, 0x0du8]; + + k0.gen(512); + let sig = k0.sign(&msg); + + let r0 = k0.get_rsa(); + let r1 = RSA::from_public_components(r0.n().expect("n"), r0.e().expect("e")).expect("r1"); + k1.set_rsa(&r1); + + assert!(k1.can(super::Role::Encrypt)); + assert!(!k1.can(super::Role::Decrypt)); + assert!(k1.can(super::Role::Verify)); + assert!(!k1.can(super::Role::Sign)); + + let rv = k1.verify(&msg, &sig); + assert!(rv == true); + } + + #[test] #[should_panic(expected = "Could not get RSA key for encryption")] fn test_nokey_encrypt() { let mut pkey = super::PKey::new(); |