From 98b7f2f9352e4d92b44245d0737f9a45adb4ae2b Mon Sep 17 00:00:00 2001 From: Steven Fackler Date: Sat, 22 Oct 2016 09:16:38 -0700 Subject: Flatten crypto module --- openssl/src/dsa.rs | 265 +++++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 265 insertions(+) create mode 100644 openssl/src/dsa.rs (limited to 'openssl/src/dsa.rs') diff --git a/openssl/src/dsa.rs b/openssl/src/dsa.rs new file mode 100644 index 00000000..fc005574 --- /dev/null +++ b/openssl/src/dsa.rs @@ -0,0 +1,265 @@ +use ffi; +use std::fmt; +use error::ErrorStack; +use std::ptr; +use libc::{c_int, c_char, c_void}; + +use {cvt, cvt_p}; +use bn::BigNumRef; +use bio::{MemBio, MemBioSlice}; +use util::{CallbackState, invoke_passwd_cb}; + +/// Builder for upfront DSA parameter generation +pub struct DSAParams(*mut ffi::DSA); + +impl DSAParams { + pub fn with_size(size: u32) -> Result { + unsafe { + let dsa = DSAParams(try!(cvt_p(ffi::DSA_new()))); + try!(cvt(ffi::DSA_generate_parameters_ex(dsa.0, + size as c_int, + ptr::null(), + 0, + ptr::null_mut(), + ptr::null_mut(), + ptr::null_mut()))); + Ok(dsa) + } + } + + /// Generate a key pair from the initialized parameters + pub fn generate(self) -> Result { + unsafe { + try!(cvt(ffi::DSA_generate_key(self.0))); + let dsa = DSA(self.0); + ::std::mem::forget(self); + Ok(dsa) + } + } +} + +impl Drop for DSAParams { + fn drop(&mut self) { + unsafe { + ffi::DSA_free(self.0); + } + } +} + +pub struct DSA(*mut ffi::DSA); + +impl Drop for DSA { + fn drop(&mut self) { + unsafe { + ffi::DSA_free(self.0); + } + } +} + +impl DSA { + pub unsafe fn from_ptr(dsa: *mut ffi::DSA) -> DSA { + DSA(dsa) + } + + /// Generate a DSA key pair + /// For more complicated key generation scenarios see the `DSAParams` type + pub fn generate(size: u32) -> Result { + let params = try!(DSAParams::with_size(size)); + params.generate() + } + + /// Reads a DSA private key from PEM formatted data. + pub fn private_key_from_pem(buf: &[u8]) -> Result { + ffi::init(); + let mem_bio = try!(MemBioSlice::new(buf)); + + unsafe { + let dsa = try!(cvt_p(ffi::PEM_read_bio_DSAPrivateKey(mem_bio.as_ptr(), + ptr::null_mut(), + None, + ptr::null_mut()))); + Ok(DSA(dsa)) + } + } + + /// Read a private key from PEM supplying a password callback to be invoked if the private key + /// is encrypted. + /// + /// The callback will be passed the password buffer and should return the number of characters + /// placed into the buffer. + pub fn private_key_from_pem_cb(buf: &[u8], pass_cb: F) -> Result + where F: FnOnce(&mut [c_char]) -> usize + { + ffi::init(); + let mut cb = CallbackState::new(pass_cb); + let mem_bio = try!(MemBioSlice::new(buf)); + + unsafe { + let cb_ptr = &mut cb as *mut _ as *mut c_void; + let dsa = try!(cvt_p(ffi::PEM_read_bio_DSAPrivateKey(mem_bio.as_ptr(), + ptr::null_mut(), + Some(invoke_passwd_cb::), + cb_ptr))); + Ok(DSA(dsa)) + } + } + + /// Writes an DSA private key as unencrypted PEM formatted data + pub fn private_key_to_pem(&self) -> Result, ErrorStack> + { + assert!(self.has_private_key()); + let mem_bio = try!(MemBio::new()); + + unsafe { + try!(cvt(ffi::PEM_write_bio_DSAPrivateKey(mem_bio.as_ptr(), self.0, + ptr::null(), ptr::null_mut(), 0, + None, ptr::null_mut()))) + }; + + Ok(mem_bio.get_buf().to_owned()) + } + + /// Reads an DSA public key from PEM formatted data. + pub fn public_key_from_pem(buf: &[u8]) -> Result + { + ffi::init(); + + let mem_bio = try!(MemBioSlice::new(buf)); + unsafe { + let dsa = try!(cvt_p(ffi::PEM_read_bio_DSA_PUBKEY(mem_bio.as_ptr(), + ptr::null_mut(), + None, + ptr::null_mut()))); + Ok(DSA(dsa)) + } + } + + /// Writes an DSA public key as PEM formatted data + pub fn public_key_to_pem(&self) -> Result, ErrorStack> { + let mem_bio = try!(MemBio::new()); + unsafe { + try!(cvt(ffi::PEM_write_bio_DSA_PUBKEY(mem_bio.as_ptr(), self.0))); + } + Ok(mem_bio.get_buf().to_owned()) + } + + pub fn size(&self) -> Option { + if self.q().is_some() { + unsafe { Some(ffi::DSA_size(self.0) as u32) } + } else { + None + } + } + + pub fn as_ptr(&self) -> *mut ffi::DSA { + self.0 + } + + pub fn p(&self) -> Option<&BigNumRef> { + unsafe { + let p = compat::pqg(self.0)[0]; + if p.is_null() { + None + } else { + Some(BigNumRef::from_ptr(p as *mut _)) + } + } + } + + pub fn q(&self) -> Option<&BigNumRef> { + unsafe { + let q = compat::pqg(self.0)[1]; + if q.is_null() { + None + } else { + Some(BigNumRef::from_ptr(q as *mut _)) + } + } + } + + pub fn g(&self) -> Option<&BigNumRef> { + unsafe { + let g = compat::pqg(self.0)[2]; + if g.is_null() { + None + } else { + Some(BigNumRef::from_ptr(g as *mut _)) + } + } + } + + pub fn has_public_key(&self) -> bool { + unsafe { !compat::keys(self.0)[0].is_null() } + } + + pub fn has_private_key(&self) -> bool { + unsafe { !compat::keys(self.0)[1].is_null() } + } +} + +#[cfg(ossl110)] +mod compat { + use std::ptr; + use ffi::{self, BIGNUM, DSA}; + + pub unsafe fn pqg(d: *const DSA) -> [*const BIGNUM; 3] { + let (mut p, mut q, mut g) = (ptr::null(), ptr::null(), ptr::null()); + ffi::DSA_get0_pqg(d, &mut p, &mut q, &mut g); + [p, q, g] + } + + pub unsafe fn keys(d: *const DSA) -> [*const BIGNUM; 2] { + let (mut pub_key, mut priv_key) = (ptr::null(), ptr::null()); + ffi::DSA_get0_key(d, &mut pub_key, &mut priv_key); + [pub_key, priv_key] + } +} + +#[cfg(ossl10x)] +mod compat { + use ffi::{BIGNUM, DSA}; + + pub unsafe fn pqg(d: *const DSA) -> [*const BIGNUM; 3] { + [(*d).p, (*d).q, (*d).g] + } + + pub unsafe fn keys(d: *const DSA) -> [*const BIGNUM; 2] { + [(*d).pub_key, (*d).priv_key] + } +} + +impl fmt::Debug for DSA { + fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result { + write!(f, "DSA") + } +} + +#[cfg(test)] +mod test { + use libc::c_char; + + use super::*; + + #[test] + pub fn test_generate() { + DSA::generate(1024).unwrap(); + } + + #[test] + pub fn test_password() { + let mut password_queried = false; + let key = include_bytes!("../test/dsa-encrypted.pem"); + DSA::private_key_from_pem_cb(key, |password| { + password_queried = true; + password[0] = b'm' as c_char; + password[1] = b'y' as c_char; + password[2] = b'p' as c_char; + password[3] = b'a' as c_char; + password[4] = b's' as c_char; + password[5] = b's' as c_char; + 6 + }).unwrap(); + + assert!(password_queried); + } +} -- cgit v1.2.3 From b619c4e88505751e6f57c742a11520e65bcc237d Mon Sep 17 00:00:00 2001 From: Steven Fackler Date: Sat, 22 Oct 2016 10:16:49 -0700 Subject: Camel case Dsa --- openssl/src/dsa.rs | 46 +++++++++++++++++++++++----------------------- 1 file changed, 23 insertions(+), 23 deletions(-) (limited to 'openssl/src/dsa.rs') diff --git a/openssl/src/dsa.rs b/openssl/src/dsa.rs index fc005574..46bdfaff 100644 --- a/openssl/src/dsa.rs +++ b/openssl/src/dsa.rs @@ -10,12 +10,12 @@ use bio::{MemBio, MemBioSlice}; use util::{CallbackState, invoke_passwd_cb}; /// Builder for upfront DSA parameter generation -pub struct DSAParams(*mut ffi::DSA); +pub struct DsaParams(*mut ffi::DSA); -impl DSAParams { - pub fn with_size(size: u32) -> Result { +impl DsaParams { + pub fn with_size(size: u32) -> Result { unsafe { - let dsa = DSAParams(try!(cvt_p(ffi::DSA_new()))); + let dsa = DsaParams(try!(cvt_p(ffi::DSA_new()))); try!(cvt(ffi::DSA_generate_parameters_ex(dsa.0, size as c_int, ptr::null(), @@ -28,17 +28,17 @@ impl DSAParams { } /// Generate a key pair from the initialized parameters - pub fn generate(self) -> Result { + pub fn generate(self) -> Result { unsafe { try!(cvt(ffi::DSA_generate_key(self.0))); - let dsa = DSA(self.0); + let dsa = Dsa(self.0); ::std::mem::forget(self); Ok(dsa) } } } -impl Drop for DSAParams { +impl Drop for DsaParams { fn drop(&mut self) { unsafe { ffi::DSA_free(self.0); @@ -46,9 +46,9 @@ impl Drop for DSAParams { } } -pub struct DSA(*mut ffi::DSA); +pub struct Dsa(*mut ffi::DSA); -impl Drop for DSA { +impl Drop for Dsa { fn drop(&mut self) { unsafe { ffi::DSA_free(self.0); @@ -56,20 +56,20 @@ impl Drop for DSA { } } -impl DSA { - pub unsafe fn from_ptr(dsa: *mut ffi::DSA) -> DSA { - DSA(dsa) +impl Dsa { + pub unsafe fn from_ptr(dsa: *mut ffi::DSA) -> Dsa { + Dsa(dsa) } /// Generate a DSA key pair /// For more complicated key generation scenarios see the `DSAParams` type - pub fn generate(size: u32) -> Result { - let params = try!(DSAParams::with_size(size)); + pub fn generate(size: u32) -> Result { + let params = try!(DsaParams::with_size(size)); params.generate() } /// Reads a DSA private key from PEM formatted data. - pub fn private_key_from_pem(buf: &[u8]) -> Result { + pub fn private_key_from_pem(buf: &[u8]) -> Result { ffi::init(); let mem_bio = try!(MemBioSlice::new(buf)); @@ -78,7 +78,7 @@ impl DSA { ptr::null_mut(), None, ptr::null_mut()))); - Ok(DSA(dsa)) + Ok(Dsa(dsa)) } } @@ -87,7 +87,7 @@ impl DSA { /// /// The callback will be passed the password buffer and should return the number of characters /// placed into the buffer. - pub fn private_key_from_pem_cb(buf: &[u8], pass_cb: F) -> Result + pub fn private_key_from_pem_cb(buf: &[u8], pass_cb: F) -> Result where F: FnOnce(&mut [c_char]) -> usize { ffi::init(); @@ -100,7 +100,7 @@ impl DSA { ptr::null_mut(), Some(invoke_passwd_cb::), cb_ptr))); - Ok(DSA(dsa)) + Ok(Dsa(dsa)) } } @@ -120,7 +120,7 @@ impl DSA { } /// Reads an DSA public key from PEM formatted data. - pub fn public_key_from_pem(buf: &[u8]) -> Result + pub fn public_key_from_pem(buf: &[u8]) -> Result { ffi::init(); @@ -130,7 +130,7 @@ impl DSA { ptr::null_mut(), None, ptr::null_mut()))); - Ok(DSA(dsa)) + Ok(Dsa(dsa)) } } @@ -228,7 +228,7 @@ mod compat { } } -impl fmt::Debug for DSA { +impl fmt::Debug for Dsa { fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result { write!(f, "DSA") } @@ -242,14 +242,14 @@ mod test { #[test] pub fn test_generate() { - DSA::generate(1024).unwrap(); + Dsa::generate(1024).unwrap(); } #[test] pub fn test_password() { let mut password_queried = false; let key = include_bytes!("../test/dsa-encrypted.pem"); - DSA::private_key_from_pem_cb(key, |password| { + Dsa::private_key_from_pem_cb(key, |password| { password_queried = true; password[0] = b'm' as c_char; password[1] = b'y' as c_char; -- cgit v1.2.3 From f75f82e466993848393c7a26ccb51dc31b4547fe Mon Sep 17 00:00:00 2001 From: Steven Fackler Date: Sun, 30 Oct 2016 16:37:45 -0700 Subject: Rustfmt --- openssl/src/dsa.rs | 25 ++++++++++++------------- 1 file changed, 12 insertions(+), 13 deletions(-) (limited to 'openssl/src/dsa.rs') diff --git a/openssl/src/dsa.rs b/openssl/src/dsa.rs index 46bdfaff..9351d852 100644 --- a/openssl/src/dsa.rs +++ b/openssl/src/dsa.rs @@ -105,8 +105,7 @@ impl Dsa { } /// Writes an DSA private key as unencrypted PEM formatted data - pub fn private_key_to_pem(&self) -> Result, ErrorStack> - { + pub fn private_key_to_pem(&self) -> Result, ErrorStack> { assert!(self.has_private_key()); let mem_bio = try!(MemBio::new()); @@ -120,8 +119,7 @@ impl Dsa { } /// Reads an DSA public key from PEM formatted data. - pub fn public_key_from_pem(buf: &[u8]) -> Result - { + pub fn public_key_from_pem(buf: &[u8]) -> Result { ffi::init(); let mem_bio = try!(MemBioSlice::new(buf)); @@ -250,15 +248,16 @@ mod test { let mut password_queried = false; let key = include_bytes!("../test/dsa-encrypted.pem"); Dsa::private_key_from_pem_cb(key, |password| { - password_queried = true; - password[0] = b'm' as c_char; - password[1] = b'y' as c_char; - password[2] = b'p' as c_char; - password[3] = b'a' as c_char; - password[4] = b's' as c_char; - password[5] = b's' as c_char; - 6 - }).unwrap(); + password_queried = true; + password[0] = b'm' as c_char; + password[1] = b'y' as c_char; + password[2] = b'p' as c_char; + password[3] = b'a' as c_char; + password[4] = b's' as c_char; + password[5] = b's' as c_char; + 6 + }) + .unwrap(); assert!(password_queried); } -- cgit v1.2.3 From c3b6eff1910aad742971b8c08fa7cf2c3303a9a9 Mon Sep 17 00:00:00 2001 From: Steven Fackler Date: Sun, 30 Oct 2016 16:44:21 -0700 Subject: Add DsaRef --- openssl/src/dsa.rs | 168 +++++++++++++++++++++++++++++------------------------ 1 file changed, 93 insertions(+), 75 deletions(-) (limited to 'openssl/src/dsa.rs') diff --git a/openssl/src/dsa.rs b/openssl/src/dsa.rs index 9351d852..cec1466d 100644 --- a/openssl/src/dsa.rs +++ b/openssl/src/dsa.rs @@ -1,13 +1,15 @@ +use error::ErrorStack; use ffi; +use libc::{c_int, c_char, c_void}; use std::fmt; -use error::ErrorStack; +use std::ops::Deref; use std::ptr; -use libc::{c_int, c_char, c_void}; use {cvt, cvt_p}; use bn::BigNumRef; use bio::{MemBio, MemBioSlice}; use util::{CallbackState, invoke_passwd_cb}; +use opaque::Opaque; /// Builder for upfront DSA parameter generation pub struct DsaParams(*mut ffi::DSA); @@ -46,6 +48,90 @@ impl Drop for DsaParams { } } +pub struct DsaRef(Opaque); + +impl DsaRef { + pub unsafe fn from_ptr<'a>(ptr: *mut ffi::DSA) -> &'a DsaRef { + &*(ptr as *mut _) + } + + pub fn as_ptr(&self) -> *mut ffi::DSA { + self as *const _ as *mut _ + } + + /// Writes an DSA private key as unencrypted PEM formatted data + pub fn private_key_to_pem(&self) -> Result, ErrorStack> { + assert!(self.has_private_key()); + let mem_bio = try!(MemBio::new()); + + unsafe { + try!(cvt(ffi::PEM_write_bio_DSAPrivateKey(mem_bio.as_ptr(), self.as_ptr(), + ptr::null(), ptr::null_mut(), 0, + None, ptr::null_mut()))) + }; + + Ok(mem_bio.get_buf().to_owned()) + } + + /// Writes an DSA public key as PEM formatted data + pub fn public_key_to_pem(&self) -> Result, ErrorStack> { + let mem_bio = try!(MemBio::new()); + unsafe { + try!(cvt(ffi::PEM_write_bio_DSA_PUBKEY(mem_bio.as_ptr(), self.as_ptr()))); + } + Ok(mem_bio.get_buf().to_owned()) + } + + pub fn size(&self) -> Option { + if self.q().is_some() { + unsafe { Some(ffi::DSA_size(self.as_ptr()) as u32) } + } else { + None + } + } + + pub fn p(&self) -> Option<&BigNumRef> { + unsafe { + let p = compat::pqg(self.as_ptr())[0]; + if p.is_null() { + None + } else { + Some(BigNumRef::from_ptr(p as *mut _)) + } + } + } + + pub fn q(&self) -> Option<&BigNumRef> { + unsafe { + let q = compat::pqg(self.as_ptr())[1]; + if q.is_null() { + None + } else { + Some(BigNumRef::from_ptr(q as *mut _)) + } + } + } + + pub fn g(&self) -> Option<&BigNumRef> { + unsafe { + let g = compat::pqg(self.as_ptr())[2]; + if g.is_null() { + None + } else { + Some(BigNumRef::from_ptr(g as *mut _)) + } + } + } + + pub fn has_public_key(&self) -> bool { + unsafe { !compat::keys(self.as_ptr())[0].is_null() } + } + + pub fn has_private_key(&self) -> bool { + unsafe { !compat::keys(self.as_ptr())[1].is_null() } + } +} + pub struct Dsa(*mut ffi::DSA); impl Drop for Dsa { @@ -104,20 +190,6 @@ impl Dsa { } } - /// Writes an DSA private key as unencrypted PEM formatted data - pub fn private_key_to_pem(&self) -> Result, ErrorStack> { - assert!(self.has_private_key()); - let mem_bio = try!(MemBio::new()); - - unsafe { - try!(cvt(ffi::PEM_write_bio_DSAPrivateKey(mem_bio.as_ptr(), self.0, - ptr::null(), ptr::null_mut(), 0, - None, ptr::null_mut()))) - }; - - Ok(mem_bio.get_buf().to_owned()) - } - /// Reads an DSA public key from PEM formatted data. pub fn public_key_from_pem(buf: &[u8]) -> Result { ffi::init(); @@ -131,67 +203,13 @@ impl Dsa { Ok(Dsa(dsa)) } } +} - /// Writes an DSA public key as PEM formatted data - pub fn public_key_to_pem(&self) -> Result, ErrorStack> { - let mem_bio = try!(MemBio::new()); - unsafe { - try!(cvt(ffi::PEM_write_bio_DSA_PUBKEY(mem_bio.as_ptr(), self.0))); - } - Ok(mem_bio.get_buf().to_owned()) - } - - pub fn size(&self) -> Option { - if self.q().is_some() { - unsafe { Some(ffi::DSA_size(self.0) as u32) } - } else { - None - } - } - - pub fn as_ptr(&self) -> *mut ffi::DSA { - self.0 - } - - pub fn p(&self) -> Option<&BigNumRef> { - unsafe { - let p = compat::pqg(self.0)[0]; - if p.is_null() { - None - } else { - Some(BigNumRef::from_ptr(p as *mut _)) - } - } - } - - pub fn q(&self) -> Option<&BigNumRef> { - unsafe { - let q = compat::pqg(self.0)[1]; - if q.is_null() { - None - } else { - Some(BigNumRef::from_ptr(q as *mut _)) - } - } - } - - pub fn g(&self) -> Option<&BigNumRef> { - unsafe { - let g = compat::pqg(self.0)[2]; - if g.is_null() { - None - } else { - Some(BigNumRef::from_ptr(g as *mut _)) - } - } - } - - pub fn has_public_key(&self) -> bool { - unsafe { !compat::keys(self.0)[0].is_null() } - } +impl Deref for Dsa { + type Target = DsaRef; - pub fn has_private_key(&self) -> bool { - unsafe { !compat::keys(self.0)[1].is_null() } + fn deref(&self) -> &DsaRef { + unsafe { DsaRef::from_ptr(self.0) } } } -- cgit v1.2.3 From 287f6df6c6d76754c713120d02d3d9c5e5624089 Mon Sep 17 00:00:00 2001 From: Steven Fackler Date: Sun, 30 Oct 2016 17:04:55 -0700 Subject: Remove DsaParams --- openssl/src/dsa.rs | 56 ++++++++++++++---------------------------------------- 1 file changed, 14 insertions(+), 42 deletions(-) (limited to 'openssl/src/dsa.rs') diff --git a/openssl/src/dsa.rs b/openssl/src/dsa.rs index cec1466d..e92b8ca3 100644 --- a/openssl/src/dsa.rs +++ b/openssl/src/dsa.rs @@ -11,43 +11,6 @@ use bio::{MemBio, MemBioSlice}; use util::{CallbackState, invoke_passwd_cb}; use opaque::Opaque; -/// Builder for upfront DSA parameter generation -pub struct DsaParams(*mut ffi::DSA); - -impl DsaParams { - pub fn with_size(size: u32) -> Result { - unsafe { - let dsa = DsaParams(try!(cvt_p(ffi::DSA_new()))); - try!(cvt(ffi::DSA_generate_parameters_ex(dsa.0, - size as c_int, - ptr::null(), - 0, - ptr::null_mut(), - ptr::null_mut(), - ptr::null_mut()))); - Ok(dsa) - } - } - - /// Generate a key pair from the initialized parameters - pub fn generate(self) -> Result { - unsafe { - try!(cvt(ffi::DSA_generate_key(self.0))); - let dsa = Dsa(self.0); - ::std::mem::forget(self); - Ok(dsa) - } - } -} - -impl Drop for DsaParams { - fn drop(&mut self) { - unsafe { - ffi::DSA_free(self.0); - } - } -} - pub struct DsaRef(Opaque); impl DsaRef { @@ -147,11 +110,20 @@ impl Dsa { Dsa(dsa) } - /// Generate a DSA key pair - /// For more complicated key generation scenarios see the `DSAParams` type - pub fn generate(size: u32) -> Result { - let params = try!(DsaParams::with_size(size)); - params.generate() + /// Generate a DSA key pair. + pub fn generate(bits: u32) -> Result { + unsafe { + let dsa = Dsa(try!(cvt_p(ffi::DSA_new()))); + try!(cvt(ffi::DSA_generate_parameters_ex(dsa.0, + bits as c_int, + ptr::null(), + 0, + ptr::null_mut(), + ptr::null_mut(), + ptr::null_mut()))); + try!(cvt(ffi::DSA_generate_key(dsa .0))); + Ok(dsa) + } } /// Reads a DSA private key from PEM formatted data. -- cgit v1.2.3 From 3363046c34ca3f968ce8b34e885d25aebac2b1f4 Mon Sep 17 00:00:00 2001 From: Steven Fackler Date: Mon, 31 Oct 2016 19:45:52 -0700 Subject: Update bignum --- openssl/src/dsa.rs | 19 ++++++++++--------- 1 file changed, 10 insertions(+), 9 deletions(-) (limited to 'openssl/src/dsa.rs') diff --git a/openssl/src/dsa.rs b/openssl/src/dsa.rs index e92b8ca3..c98d9c35 100644 --- a/openssl/src/dsa.rs +++ b/openssl/src/dsa.rs @@ -5,11 +5,12 @@ use std::fmt; use std::ops::Deref; use std::ptr; -use {cvt, cvt_p}; -use bn::BigNumRef; use bio::{MemBio, MemBioSlice}; -use util::{CallbackState, invoke_passwd_cb}; +use bn::BigNum; +use {cvt, cvt_p}; use opaque::Opaque; +use types::Ref; +use util::{CallbackState, invoke_passwd_cb}; pub struct DsaRef(Opaque); @@ -53,35 +54,35 @@ impl DsaRef { } } - pub fn p(&self) -> Option<&BigNumRef> { + pub fn p(&self) -> Option<&Ref> { unsafe { let p = compat::pqg(self.as_ptr())[0]; if p.is_null() { None } else { - Some(BigNumRef::from_ptr(p as *mut _)) + Some(Ref::::from_ptr(p as *mut _)) } } } - pub fn q(&self) -> Option<&BigNumRef> { + pub fn q(&self) -> Option<&Ref> { unsafe { let q = compat::pqg(self.as_ptr())[1]; if q.is_null() { None } else { - Some(BigNumRef::from_ptr(q as *mut _)) + Some(Ref::::from_ptr(q as *mut _)) } } } - pub fn g(&self) -> Option<&BigNumRef> { + pub fn g(&self) -> Option<&Ref> { unsafe { let g = compat::pqg(self.as_ptr())[2]; if g.is_null() { None } else { - Some(BigNumRef::from_ptr(g as *mut _)) + Some(Ref::::from_ptr(g as *mut _)) } } } -- cgit v1.2.3 From fe5fb75d45c69a2cfca87982e57ea14876d1b795 Mon Sep 17 00:00:00 2001 From: Steven Fackler Date: Mon, 31 Oct 2016 20:04:55 -0700 Subject: Update Dsa --- openssl/src/dsa.rs | 44 ++++++-------------------------------------- 1 file changed, 6 insertions(+), 38 deletions(-) (limited to 'openssl/src/dsa.rs') diff --git a/openssl/src/dsa.rs b/openssl/src/dsa.rs index c98d9c35..6efa7050 100644 --- a/openssl/src/dsa.rs +++ b/openssl/src/dsa.rs @@ -2,27 +2,17 @@ use error::ErrorStack; use ffi; use libc::{c_int, c_char, c_void}; use std::fmt; -use std::ops::Deref; use std::ptr; use bio::{MemBio, MemBioSlice}; use bn::BigNum; use {cvt, cvt_p}; -use opaque::Opaque; use types::Ref; use util::{CallbackState, invoke_passwd_cb}; -pub struct DsaRef(Opaque); - -impl DsaRef { - pub unsafe fn from_ptr<'a>(ptr: *mut ffi::DSA) -> &'a DsaRef { - &*(ptr as *mut _) - } - - pub fn as_ptr(&self) -> *mut ffi::DSA { - self as *const _ as *mut _ - } +type_!(Dsa, ffi::DSA, ffi::DSA_free); +impl Ref { /// Writes an DSA private key as unencrypted PEM formatted data pub fn private_key_to_pem(&self) -> Result, ErrorStack> { assert!(self.has_private_key()); @@ -96,21 +86,7 @@ impl DsaRef { } } -pub struct Dsa(*mut ffi::DSA); - -impl Drop for Dsa { - fn drop(&mut self) { - unsafe { - ffi::DSA_free(self.0); - } - } -} - impl Dsa { - pub unsafe fn from_ptr(dsa: *mut ffi::DSA) -> Dsa { - Dsa(dsa) - } - /// Generate a DSA key pair. pub fn generate(bits: u32) -> Result { unsafe { @@ -122,7 +98,7 @@ impl Dsa { ptr::null_mut(), ptr::null_mut(), ptr::null_mut()))); - try!(cvt(ffi::DSA_generate_key(dsa .0))); + try!(cvt(ffi::DSA_generate_key(dsa.0))); Ok(dsa) } } @@ -178,11 +154,9 @@ impl Dsa { } } -impl Deref for Dsa { - type Target = DsaRef; - - fn deref(&self) -> &DsaRef { - unsafe { DsaRef::from_ptr(self.0) } +impl fmt::Debug for Dsa { + fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result { + write!(f, "DSA") } } @@ -217,12 +191,6 @@ mod compat { } } -impl fmt::Debug for Dsa { - fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result { - write!(f, "DSA") - } -} - #[cfg(test)] mod test { use libc::c_char; -- cgit v1.2.3 From 01ae978db0dc8620b2cc754c0d5cf94a68c1f549 Mon Sep 17 00:00:00 2001 From: Steven Fackler Date: Fri, 4 Nov 2016 16:32:20 -0700 Subject: Get rid of Ref There's unfortunately a rustdoc bug that causes all methods implemented for any Ref to be inlined in the deref methods section :( --- openssl/src/dsa.rs | 20 ++++++++++---------- 1 file changed, 10 insertions(+), 10 deletions(-) (limited to 'openssl/src/dsa.rs') diff --git a/openssl/src/dsa.rs b/openssl/src/dsa.rs index 6efa7050..9dd5669c 100644 --- a/openssl/src/dsa.rs +++ b/openssl/src/dsa.rs @@ -5,14 +5,14 @@ use std::fmt; use std::ptr; use bio::{MemBio, MemBioSlice}; -use bn::BigNum; +use bn::BigNumRef; use {cvt, cvt_p}; -use types::Ref; +use types::OpenSslTypeRef; use util::{CallbackState, invoke_passwd_cb}; -type_!(Dsa, ffi::DSA, ffi::DSA_free); +type_!(Dsa, DsaRef, ffi::DSA, ffi::DSA_free); -impl Ref { +impl DsaRef { /// Writes an DSA private key as unencrypted PEM formatted data pub fn private_key_to_pem(&self) -> Result, ErrorStack> { assert!(self.has_private_key()); @@ -44,35 +44,35 @@ impl Ref { } } - pub fn p(&self) -> Option<&Ref> { + pub fn p(&self) -> Option<&BigNumRef> { unsafe { let p = compat::pqg(self.as_ptr())[0]; if p.is_null() { None } else { - Some(Ref::::from_ptr(p as *mut _)) + Some(BigNumRef::from_ptr(p as *mut _)) } } } - pub fn q(&self) -> Option<&Ref> { + pub fn q(&self) -> Option<&BigNumRef> { unsafe { let q = compat::pqg(self.as_ptr())[1]; if q.is_null() { None } else { - Some(Ref::::from_ptr(q as *mut _)) + Some(BigNumRef::from_ptr(q as *mut _)) } } } - pub fn g(&self) -> Option<&Ref> { + pub fn g(&self) -> Option<&BigNumRef> { unsafe { let g = compat::pqg(self.as_ptr())[2]; if g.is_null() { None } else { - Some(Ref::::from_ptr(g as *mut _)) + Some(BigNumRef::from_ptr(g as *mut _)) } } } -- cgit v1.2.3