Loading openssl/src/bn/mod.rs +3 −3 Original line number Diff line number Diff line use libc::{c_int, c_ulong, c_void}; use std::ffi::{CStr, CString}; use std::cmp::Ordering; use std::{fmt, ptr}; use std::{fmt, ptr, mem}; use ffi; use ssl::error::SslError; Loading Loading @@ -473,9 +473,9 @@ impl BigNum { n } pub unsafe fn into_raw(self) -> *mut ffi::BIGNUM { pub fn into_raw(self) -> *mut ffi::BIGNUM { let mut me = self; ptr::replace(&mut me.0, ptr::null_mut()) mem::replace(&mut me.0, ptr::null_mut()) } pub fn to_vec(&self) -> Vec<u8> { Loading openssl/src/crypto/pkey.rs +27 −3 Original line number Diff line number Diff line Loading @@ -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() { Loading @@ -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)) } } Loading Loading @@ -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() { Loading Loading @@ -811,6 +813,28 @@ mod tests { assert!(pub_key.windows(10).any(|s| s == b"PUBLIC KEY")); } #[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() { Loading openssl/src/crypto/rsa.rs +4 −16 Original line number Diff line number Diff line Loading @@ -20,14 +20,16 @@ impl Drop for RSA { impl RSA { /// only useful for associating the key material directly with the key, it's safer to use /// the supplied load and save methods for DER formatted keys. pub fn new() -> Result<RSA, SslError> { pub fn from_public_components(n: BigNum, e: BigNum) -> Result<RSA, SslError> { unsafe { let rsa = try_ssl_null!(ffi::RSA_new()); (*rsa).n = n.into_raw(); (*rsa).e = e.into_raw(); Ok(RSA(rsa)) } } pub fn with_raw(rsa: *mut ffi::RSA) -> RSA { pub fn from_raw(rsa: *mut ffi::RSA) -> RSA { RSA(rsa) } Loading Loading @@ -74,13 +76,6 @@ impl RSA { } } /// set the key modulus pub fn set_n(&mut self, n: BigNum) { unsafe { (*self.0).n = n.into_raw(); } } pub fn has_n(&self) -> bool { unsafe { !(*self.0).n.is_null() Loading @@ -99,13 +94,6 @@ impl RSA { } } /// set the exponent pub fn set_e(&mut self, e: BigNum) { unsafe { (*self.0).e = e.into_raw(); } } pub fn has_e(&self) -> bool { unsafe { !(*self.0).e.is_null() Loading Loading
openssl/src/bn/mod.rs +3 −3 Original line number Diff line number Diff line use libc::{c_int, c_ulong, c_void}; use std::ffi::{CStr, CString}; use std::cmp::Ordering; use std::{fmt, ptr}; use std::{fmt, ptr, mem}; use ffi; use ssl::error::SslError; Loading Loading @@ -473,9 +473,9 @@ impl BigNum { n } pub unsafe fn into_raw(self) -> *mut ffi::BIGNUM { pub fn into_raw(self) -> *mut ffi::BIGNUM { let mut me = self; ptr::replace(&mut me.0, ptr::null_mut()) mem::replace(&mut me.0, ptr::null_mut()) } pub fn to_vec(&self) -> Vec<u8> { Loading
openssl/src/crypto/pkey.rs +27 −3 Original line number Diff line number Diff line Loading @@ -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() { Loading @@ -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)) } } Loading Loading @@ -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() { Loading Loading @@ -811,6 +813,28 @@ mod tests { assert!(pub_key.windows(10).any(|s| s == b"PUBLIC KEY")); } #[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() { Loading
openssl/src/crypto/rsa.rs +4 −16 Original line number Diff line number Diff line Loading @@ -20,14 +20,16 @@ impl Drop for RSA { impl RSA { /// only useful for associating the key material directly with the key, it's safer to use /// the supplied load and save methods for DER formatted keys. pub fn new() -> Result<RSA, SslError> { pub fn from_public_components(n: BigNum, e: BigNum) -> Result<RSA, SslError> { unsafe { let rsa = try_ssl_null!(ffi::RSA_new()); (*rsa).n = n.into_raw(); (*rsa).e = e.into_raw(); Ok(RSA(rsa)) } } pub fn with_raw(rsa: *mut ffi::RSA) -> RSA { pub fn from_raw(rsa: *mut ffi::RSA) -> RSA { RSA(rsa) } Loading Loading @@ -74,13 +76,6 @@ impl RSA { } } /// set the key modulus pub fn set_n(&mut self, n: BigNum) { unsafe { (*self.0).n = n.into_raw(); } } pub fn has_n(&self) -> bool { unsafe { !(*self.0).n.is_null() Loading @@ -99,13 +94,6 @@ impl RSA { } } /// set the exponent pub fn set_e(&mut self, e: BigNum) { unsafe { (*self.0).e = e.into_raw(); } } pub fn has_e(&self) -> bool { unsafe { !(*self.0).e.is_null() Loading