diff options
| author | Steven Fackler <[email protected]> | 2017-12-30 21:46:17 -0800 |
|---|---|---|
| committer | Steven Fackler <[email protected]> | 2017-12-30 21:53:39 -0800 |
| commit | d207897458f8fbd0b2ff6d879721b2f787a5d72b (patch) | |
| tree | f5646108131916584291141407696ef894f2ff1e /openssl/src/dsa.rs | |
| parent | Merge pull request #809 from sfackler/issuer-name (diff) | |
| download | rust-openssl-d207897458f8fbd0b2ff6d879721b2f787a5d72b.tar.xz rust-openssl-d207897458f8fbd0b2ff6d879721b2f787a5d72b.zip | |
Parameterize keys over what they contain
Closes #790
Diffstat (limited to 'openssl/src/dsa.rs')
| -rw-r--r-- | openssl/src/dsa.rs | 105 |
1 files changed, 43 insertions, 62 deletions
diff --git a/openssl/src/dsa.rs b/openssl/src/dsa.rs index 2c9f7de4..19ad2b6e 100644 --- a/openssl/src/dsa.rs +++ b/openssl/src/dsa.rs @@ -6,7 +6,7 @@ //! without the private key. use ffi; -use foreign_types::ForeignTypeRef; +use foreign_types::{ForeignType, ForeignTypeRef}; use libc::c_int; use std::fmt; use std::ptr; @@ -14,8 +14,9 @@ use std::ptr; use {cvt, cvt_p}; use bn::BigNumRef; use error::ErrorStack; +use pkey::{HasParams, HasPrivate, HasPublic, Private, Public}; -foreign_type_and_impl_send_sync! { +generic_foreign_type_and_impl_send_sync! { type CType = ffi::DSA; fn drop = ffi::DSA_free; @@ -40,7 +41,9 @@ foreign_type_and_impl_send_sync! { /// ``` /// use openssl::dsa::Dsa; /// use openssl::error::ErrorStack; - /// fn create_dsa() -> Result< Dsa, ErrorStack > { + /// use openssl::pkey::Private; + /// + /// fn create_dsa() -> Result<Dsa<Private>, ErrorStack> { /// let sign = Dsa::generate(2048)?; /// Ok(sign) /// } @@ -48,83 +51,68 @@ foreign_type_and_impl_send_sync! { /// # create_dsa(); /// # } /// ``` - pub struct Dsa; + pub struct Dsa<T>; /// Reference to [`Dsa`]. /// /// [`Dsa`]: struct.Dsa.html - pub struct DsaRef; + pub struct DsaRef<T>; } -impl DsaRef { +impl<T> DsaRef<T> +where + T: HasPrivate, +{ private_key_to_pem!(ffi::PEM_write_bio_DSAPrivateKey); - public_key_to_pem!(ffi::PEM_write_bio_DSA_PUBKEY); - private_key_to_der!(ffi::i2d_DSAPrivateKey); +} + +impl<T> DsaRef<T> +where + T: HasPublic, +{ + public_key_to_pem!(ffi::PEM_write_bio_DSA_PUBKEY); public_key_to_der!(ffi::i2d_DSAPublicKey); +} - /// Returns the maximum size of the signature output by `self` in bytes. Returns - /// None if the keys are uninitialized. +impl<T> DsaRef<T> +where + T: HasParams, +{ + /// Returns the maximum size of the signature output by `self` in bytes. /// /// OpenSSL documentation at [`DSA_size`] /// /// [`DSA_size`]: https://www.openssl.org/docs/man1.1.0/crypto/DSA_size.html pub fn size(&self) -> u32 { - unsafe { - assert!(self.q().is_some()); - ffi::DSA_size(self.as_ptr()) as u32 - } + unsafe { ffi::DSA_size(self.as_ptr()) as u32 } } /// Returns the DSA prime parameter of `self`. - pub fn p(&self) -> Option<&BigNumRef> { + pub fn p(&self) -> &BigNumRef { unsafe { let p = compat::pqg(self.as_ptr())[0]; - if p.is_null() { - None - } else { - Some(BigNumRef::from_ptr(p as *mut _)) - } + BigNumRef::from_ptr(p as *mut _) } } /// Returns the DSA sub-prime parameter of `self`. - pub fn q(&self) -> Option<&BigNumRef> { + pub fn q(&self) -> &BigNumRef { unsafe { let q = compat::pqg(self.as_ptr())[1]; - if q.is_null() { - None - } else { - Some(BigNumRef::from_ptr(q as *mut _)) - } + BigNumRef::from_ptr(q as *mut _) } } /// Returns the DSA base parameter of `self`. - pub fn g(&self) -> Option<&BigNumRef> { + pub fn g(&self) -> &BigNumRef { unsafe { let g = compat::pqg(self.as_ptr())[2]; - if g.is_null() { - None - } else { - Some(BigNumRef::from_ptr(g as *mut _)) - } + BigNumRef::from_ptr(g as *mut _) } } - - /// Returns whether the DSA includes a public key, used to confirm the authenticity - /// of the message. - pub fn has_public_key(&self) -> bool { - unsafe { !compat::keys(self.as_ptr())[0].is_null() } - } - - /// Returns whether the DSA includes a private key, used to prove the authenticity - /// of a message. - pub fn has_private_key(&self) -> bool { - unsafe { !compat::keys(self.as_ptr())[1].is_null() } - } } -impl Dsa { +impl Dsa<Private> { /// Generate a DSA key pair. /// /// Calls [`DSA_generate_parameters_ex`] to populate the `p`, `g`, and `q` values. @@ -134,10 +122,10 @@ impl Dsa { /// /// [`DSA_generate_parameters_ex`]: https://www.openssl.org/docs/man1.1.0/crypto/DSA_generate_parameters_ex.html /// [`DSA_generate_key`]: https://www.openssl.org/docs/man1.1.0/crypto/DSA_generate_key.html - pub fn generate(bits: u32) -> Result<Dsa, ErrorStack> { + pub fn generate(bits: u32) -> Result<Dsa<Private>, ErrorStack> { ffi::init(); unsafe { - let dsa = Dsa(cvt_p(ffi::DSA_new())?); + let dsa = Dsa::from_ptr(cvt_p(ffi::DSA_new())?); cvt(ffi::DSA_generate_parameters_ex( dsa.0, bits as c_int, @@ -152,13 +140,16 @@ impl Dsa { } } - private_key_from_pem!(Dsa, ffi::PEM_read_bio_DSAPrivateKey); - private_key_from_der!(Dsa, ffi::d2i_DSAPrivateKey); - public_key_from_pem!(Dsa, ffi::PEM_read_bio_DSA_PUBKEY); - public_key_from_der!(Dsa, ffi::d2i_DSAPublicKey); + private_key_from_pem!(Dsa<Private>, ffi::PEM_read_bio_DSAPrivateKey); + private_key_from_der!(Dsa<Private>, ffi::d2i_DSAPrivateKey); +} + +impl Dsa<Public> { + public_key_from_pem!(Dsa<Public>, ffi::PEM_read_bio_DSA_PUBKEY); + public_key_from_der!(Dsa<Public>, ffi::d2i_DSAPublicKey); } -impl fmt::Debug for Dsa { +impl<T> fmt::Debug for Dsa<T> { fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result { write!(f, "DSA") } @@ -174,12 +165,6 @@ mod compat { 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)] @@ -189,10 +174,6 @@ mod compat { 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] - } } #[cfg(test)] |