From 5813ca371dee64c1b2a8da53924be733c82a9421 Mon Sep 17 00:00:00 2001 From: Daniel Albert Date: Fri, 1 Jan 2016 19:33:49 +0000 Subject: [PATCH 1/8] Add RSA structs --- openssl-sys/src/lib.rs | 43 ++++++++++++++++++++++++++++++++++++-- openssl/src/crypto/mod.rs | 1 + openssl/src/crypto/pkey.rs | 4 ++-- 3 files changed, 44 insertions(+), 4 deletions(-) diff --git a/openssl-sys/src/lib.rs b/openssl-sys/src/lib.rs index f780b6d9..b6d2225b 100644 --- a/openssl-sys/src/lib.rs +++ b/openssl-sys/src/lib.rs @@ -22,9 +22,7 @@ pub type ENGINE = c_void; pub type EVP_CIPHER = c_void; pub type EVP_CIPHER_CTX = c_void; pub type EVP_MD = c_void; -pub type EVP_PKEY = c_void; pub type EVP_PKEY_CTX = c_void; -pub type RSA = c_void; pub type SSL = c_void; pub type SSL_CTX = c_void; pub type SSL_METHOD = c_void; @@ -65,6 +63,47 @@ pub struct BIO_METHOD { // so we can create static BIO_METHODs unsafe impl Sync for BIO_METHOD {} +#[repr(C)] +pub struct RSA { + pad: c_int, + version: c_long, + meth: *const c_void, + + pub engine: *mut c_void, + pub n: *mut BIGNUM, + pub e: *mut BIGNUM, + pub d: *mut BIGNUM, + pub p: *mut BIGNUM, + pub q: *mut BIGNUM, + pub dmp1: *mut BIGNUM, + pub dmq1: *mut BIGNUM, + pub iqmp: *mut BIGNUM, + + ex_data: *mut c_void, + references: c_int, + flags: c_int, + + _method_mod_n: *mut c_void, + _method_mod_p: *mut c_void, + _method_mod_q: *mut c_void, + + bignum_data: *mut c_char, + blinding: *mut c_void, + mt_blinding: *mut c_void, +} + +#[repr(C)] +pub struct EVP_PKEY { + pub type_: c_int, + pub save_type: c_int, + pub references: c_int, + pub ameth: *const c_void, + engine: *mut ENGINE, + pub pkey: *mut c_void, + save_parameters: c_int, + attributes: *mut c_void, +} + #[repr(C)] pub struct BIO { pub method: *mut BIO_METHOD, diff --git a/openssl/src/crypto/mod.rs b/openssl/src/crypto/mod.rs index 0868ee95..bb77453f 100644 --- a/openssl/src/crypto/mod.rs +++ b/openssl/src/crypto/mod.rs @@ -21,5 +21,6 @@ pub mod pkey; pub mod rand; pub mod symm; pub mod memcmp; +pub mod rsa; mod symm_internal; diff --git a/openssl/src/crypto/pkey.rs b/openssl/src/crypto/pkey.rs index 10891224..25ce28e8 100644 --- a/openssl/src/crypto/pkey.rs +++ b/openssl/src/crypto/pkey.rs @@ -93,7 +93,7 @@ impl PKey { None, ptr::null_mut())); Ok(PKey { - evp: evp, + evp: evp as *mut ffi::EVP_PKEY, parts: Parts::Both, }) } @@ -112,7 +112,7 @@ impl PKey { None, ptr::null_mut())); Ok(PKey { - evp: evp, + evp: evp as *mut ffi::EVP_PKEY, parts: Parts::Public, }) } From 5e5d24ee25f58675a6770585fc82978b39165cd3 Mon Sep 17 00:00:00 2001 From: Daniel Albert Date: Fri, 1 Jan 2016 19:36:29 +0000 Subject: [PATCH 2/8] Implement the possibility to create BigNums from their ffi counterpart --- openssl/src/bn/mod.rs | 14 ++++++++++++++ 1 file changed, 14 insertions(+) diff --git a/openssl/src/bn/mod.rs b/openssl/src/bn/mod.rs index 51a49241..cd1229af 100644 --- a/openssl/src/bn/mod.rs +++ b/openssl/src/bn/mod.rs @@ -102,6 +102,20 @@ impl BigNum { }) } + pub fn new_from_ffi(orig: *mut ffi::BIGNUM) -> Result { + if orig.is_null() { + panic!("Null Pointer was supplied to BigNum::new_from_ffi"); + } + unsafe { + let r = ffi::BN_dup(orig); + if r.is_null() { + panic!("Unexpected null pointer from BN_dup(..)") + } else { + Ok(BigNum(r)) + } + } + } + pub fn new_from_slice(n: &[u8]) -> Result { BigNum::new().and_then(|v| unsafe { try_ssl_null!(ffi::BN_bin2bn(n.as_ptr(), n.len() as c_int, v.raw())); From 578fac7e80bf20da802815d890cb6b54e280eb6b Mon Sep 17 00:00:00 2001 From: Daniel Albert Date: Fri, 1 Jan 2016 19:46:03 +0000 Subject: [PATCH 3/8] Add public interface to access BigNums from RSA keys --- openssl/src/crypto/rsa.rs | 39 +++++++++++++++++++++++++++++++++++++++ 1 file changed, 39 insertions(+) create mode 100644 openssl/src/crypto/rsa.rs diff --git a/openssl/src/crypto/rsa.rs b/openssl/src/crypto/rsa.rs new file mode 100644 index 00000000..ef59a07e --- /dev/null +++ b/openssl/src/crypto/rsa.rs @@ -0,0 +1,39 @@ +use ffi; +use bn::BigNum; +use std::fmt; + +pub struct RSA { + pub rsa_obj : ffi::RSA +} + +impl RSA { + pub unsafe fn get_n(&self) -> BigNum { + BigNum::new_from_ffi(self.rsa_obj.n).unwrap() + } + + pub unsafe fn get_d(&self) -> BigNum { + BigNum::new_from_ffi(self.rsa_obj.d).unwrap() + } + + pub unsafe fn get_e(&self) -> BigNum { + BigNum::new_from_ffi(self.rsa_obj.e).unwrap() + } + + pub unsafe fn get_p(&self) -> BigNum { + BigNum::new_from_ffi(self.rsa_obj.p).unwrap() + } + + pub unsafe fn get_q(&self) -> BigNum { + BigNum::new_from_ffi(self.rsa_obj.q).unwrap() + } + + pub fn get_type(&self) -> &str { + "rsa" + } +} + +impl fmt::Debug for RSA { + fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result { + write!(f, "Currently no debug output. Sorry :(") + } +} From 123840563703cd1c129a9702761b2028ab6c7210 Mon Sep 17 00:00:00 2001 From: Daniel Albert Date: Sat, 9 Jan 2016 22:09:38 +0000 Subject: [PATCH 4/8] Make the BigNum generation from a native pointer unsafe --- openssl/src/bn/mod.rs | 14 ++++++-------- 1 file changed, 6 insertions(+), 8 deletions(-) diff --git a/openssl/src/bn/mod.rs b/openssl/src/bn/mod.rs index cd1229af..ba1121dd 100644 --- a/openssl/src/bn/mod.rs +++ b/openssl/src/bn/mod.rs @@ -102,17 +102,15 @@ impl BigNum { }) } - pub fn new_from_ffi(orig: *mut ffi::BIGNUM) -> Result { + pub unsafe fn new_from_ffi(orig: *mut ffi::BIGNUM) -> Result { if orig.is_null() { panic!("Null Pointer was supplied to BigNum::new_from_ffi"); } - unsafe { - let r = ffi::BN_dup(orig); - if r.is_null() { - panic!("Unexpected null pointer from BN_dup(..)") - } else { - Ok(BigNum(r)) - } + let r = ffi::BN_dup(orig); + if r.is_null() { + panic!("Unexpected null pointer from BN_dup(..)") + } else { + Ok(BigNum(r)) } } From 6ae8298f2c2a77eb96bd12a6c795f53696a0fe03 Mon Sep 17 00:00:00 2001 From: Daniel Albert Date: Tue, 12 Jan 2016 17:46:08 +0000 Subject: [PATCH 5/8] Make all ffi structs' fields public --- openssl-sys/src/lib.rs | 30 +++++++++++++++--------------- 1 file changed, 15 insertions(+), 15 deletions(-) diff --git a/openssl-sys/src/lib.rs b/openssl-sys/src/lib.rs index b6d2225b..519b0001 100644 --- a/openssl-sys/src/lib.rs +++ b/openssl-sys/src/lib.rs @@ -65,9 +65,9 @@ unsafe impl Sync for BIO_METHOD {} #[repr(C)] pub struct RSA { - pad: c_int, - version: c_long, - meth: *const c_void, + pub pad: c_int, + pub version: c_long, + pub meth: *const c_void, pub engine: *mut c_void, pub n: *mut BIGNUM, @@ -79,17 +79,17 @@ pub struct RSA { pub dmq1: *mut BIGNUM, pub iqmp: *mut BIGNUM, - ex_data: *mut c_void, - references: c_int, - flags: c_int, + pub ex_data: *mut c_void, + pub references: c_int, + pub flags: c_int, - _method_mod_n: *mut c_void, - _method_mod_p: *mut c_void, - _method_mod_q: *mut c_void, + pub _method_mod_n: *mut c_void, + pub _method_mod_p: *mut c_void, + pub _method_mod_q: *mut c_void, - bignum_data: *mut c_char, - blinding: *mut c_void, - mt_blinding: *mut c_void, + pub bignum_data: *mut c_char, + pub blinding: *mut c_void, + pub mt_blinding: *mut c_void, } #[repr(C)] @@ -98,10 +98,10 @@ pub struct EVP_PKEY { pub save_type: c_int, pub references: c_int, pub ameth: *const c_void, - engine: *mut ENGINE, + pub engine: *mut ENGINE, pub pkey: *mut c_void, - save_parameters: c_int, - attributes: *mut c_void, + pub save_parameters: c_int, + pub attributes: *mut c_void, } #[repr(C)] From 7e8df9febdc45b7c84adbf86e380e3114ad7367f Mon Sep 17 00:00:00 2001 From: Daniel Albert Date: Tue, 12 Jan 2016 18:15:07 +0000 Subject: [PATCH 6/8] Adhere to rust conventions --- openssl/src/crypto/rsa.rs | 16 ++++++---------- 1 file changed, 6 insertions(+), 10 deletions(-) diff --git a/openssl/src/crypto/rsa.rs b/openssl/src/crypto/rsa.rs index ef59a07e..40d61884 100644 --- a/openssl/src/crypto/rsa.rs +++ b/openssl/src/crypto/rsa.rs @@ -3,33 +3,29 @@ use bn::BigNum; use std::fmt; pub struct RSA { - pub rsa_obj : ffi::RSA + rsa_obj : ffi::RSA } impl RSA { - pub unsafe fn get_n(&self) -> BigNum { + pub unsafe fn n(&self) -> BigNum { BigNum::new_from_ffi(self.rsa_obj.n).unwrap() } - pub unsafe fn get_d(&self) -> BigNum { + pub unsafe fn d(&self) -> BigNum { BigNum::new_from_ffi(self.rsa_obj.d).unwrap() } - pub unsafe fn get_e(&self) -> BigNum { + pub unsafe fn e(&self) -> BigNum { BigNum::new_from_ffi(self.rsa_obj.e).unwrap() } - pub unsafe fn get_p(&self) -> BigNum { + pub unsafe fn p(&self) -> BigNum { BigNum::new_from_ffi(self.rsa_obj.p).unwrap() } - pub unsafe fn get_q(&self) -> BigNum { + pub unsafe fn q(&self) -> BigNum { BigNum::new_from_ffi(self.rsa_obj.q).unwrap() } - - pub fn get_type(&self) -> &str { - "rsa" - } } impl fmt::Debug for RSA { From 1f45723b39f93d113536d94b89e24f6f70857eba Mon Sep 17 00:00:00 2001 From: Daniel Albert Date: Tue, 12 Jan 2016 20:57:01 +0000 Subject: [PATCH 7/8] Fix incorrect unsafe declaration --- openssl/src/crypto/rsa.rs | 31 +++++++++++++++++++++---------- 1 file changed, 21 insertions(+), 10 deletions(-) diff --git a/openssl/src/crypto/rsa.rs b/openssl/src/crypto/rsa.rs index 40d61884..85a13609 100644 --- a/openssl/src/crypto/rsa.rs +++ b/openssl/src/crypto/rsa.rs @@ -7,24 +7,35 @@ pub struct RSA { } impl RSA { - pub unsafe fn n(&self) -> BigNum { - BigNum::new_from_ffi(self.rsa_obj.n).unwrap() + // The following getters are unsafe, since BigNum::new_from_ffi fails upon null pointers + pub fn n(&self) -> BigNum { + unsafe { + BigNum::new_from_ffi(self.rsa_obj.n).unwrap() + } } - pub unsafe fn d(&self) -> BigNum { - BigNum::new_from_ffi(self.rsa_obj.d).unwrap() + pub fn d(&self) -> BigNum { + unsafe { + BigNum::new_from_ffi(self.rsa_obj.d).unwrap() + } } - pub unsafe fn e(&self) -> BigNum { - BigNum::new_from_ffi(self.rsa_obj.e).unwrap() + pub fn e(&self) -> BigNum { + unsafe { + BigNum::new_from_ffi(self.rsa_obj.e).unwrap() + } } - pub unsafe fn p(&self) -> BigNum { - BigNum::new_from_ffi(self.rsa_obj.p).unwrap() + pub fn p(&self) -> BigNum { + unsafe { + BigNum::new_from_ffi(self.rsa_obj.p).unwrap() + } } - pub unsafe fn q(&self) -> BigNum { - BigNum::new_from_ffi(self.rsa_obj.q).unwrap() + pub fn q(&self) -> BigNum { + unsafe { + BigNum::new_from_ffi(self.rsa_obj.q).unwrap() + } } } From 3ee2bf9310c75228f16ec228d3cd5aaeee66b61f Mon Sep 17 00:00:00 2001 From: Daniel Albert Date: Wed, 20 Jan 2016 20:29:06 +0000 Subject: [PATCH 8/8] Fix up RSA integration --- openssl/src/bn/mod.rs | 2 +- openssl/src/crypto/rsa.rs | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/openssl/src/bn/mod.rs b/openssl/src/bn/mod.rs index ba1121dd..00a0a0ca 100644 --- a/openssl/src/bn/mod.rs +++ b/openssl/src/bn/mod.rs @@ -108,7 +108,7 @@ impl BigNum { } let r = ffi::BN_dup(orig); if r.is_null() { - panic!("Unexpected null pointer from BN_dup(..)") + Err(SslError::get()) } else { Ok(BigNum(r)) } diff --git a/openssl/src/crypto/rsa.rs b/openssl/src/crypto/rsa.rs index 85a13609..fca94465 100644 --- a/openssl/src/crypto/rsa.rs +++ b/openssl/src/crypto/rsa.rs @@ -41,6 +41,6 @@ impl RSA { impl fmt::Debug for RSA { fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result { - write!(f, "Currently no debug output. Sorry :(") + write!(f, "RSA") } }