diff --git a/openssl/src/dh.rs b/openssl/src/dh.rs index 37663ac0..64494f95 100644 --- a/openssl/src/dh.rs +++ b/openssl/src/dh.rs @@ -4,7 +4,6 @@ use std::mem; use std::ptr; use {cvt, cvt_p, init}; -use bio::MemBioSlice; use bn::BigNum; use types::OpenSslTypeRef; @@ -26,19 +25,7 @@ impl Dh { } } - /// Reads Diffie-Hellman parameters from PEM. - pub fn from_pem(buf: &[u8]) -> Result { - unsafe { - init(); - let mem_bio = try!(MemBioSlice::new(buf)); - cvt_p(ffi::PEM_read_bio_DHparams(mem_bio.as_ptr(), - ptr::null_mut(), - None, - ptr::null_mut())) - .map(Dh) - } - } - + from_pem!(Dh, ffi::PEM_read_bio_DHparams); from_der!(Dh, ffi::d2i_DHparams); /// Requires the `v102` or `v110` features and OpenSSL 1.0.2 or OpenSSL 1.1.0. diff --git a/openssl/src/dsa.rs b/openssl/src/dsa.rs index 478272c8..fbef2c18 100644 --- a/openssl/src/dsa.rs +++ b/openssl/src/dsa.rs @@ -97,6 +97,7 @@ 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); #[deprecated(since = "0.9.2", note = "use private_key_from_pem_callback")] @@ -116,20 +117,6 @@ impl Dsa { Ok(Dsa(dsa)) } } - - /// Reads a 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)) - } - } } impl fmt::Debug for Dsa { diff --git a/openssl/src/macros.rs b/openssl/src/macros.rs index a57f36eb..b36e8319 100644 --- a/openssl/src/macros.rs +++ b/openssl/src/macros.rs @@ -42,15 +42,8 @@ macro_rules! type_ { macro_rules! private_key_from_pem { ($t:ident, $f:path) => { - /// Deserializes a PEM-formatted private key. - pub fn private_key_from_pem(pem: &[u8]) -> Result<$t, ::error::ErrorStack> { - unsafe { - ::init(); - let bio = try!(::bio::MemBioSlice::new(pem)); - cvt_p($f(bio.as_ptr(), ::std::ptr::null_mut(), None, ::std::ptr::null_mut())) - .map($t) - } - } + from_pem_inner!(/// Deserializes a PEM-formatted private key. + private_key_from_pem, $t, $f); /// Deserializes a PEM-formatted private key, using the supplied password if the key is /// encrypted. @@ -232,3 +225,31 @@ macro_rules! public_key_from_der { public_key_from_der, $t, $f); } } + +macro_rules! from_pem_inner { + (#[$m:meta] $n:ident, $t:ident, $f:path) => { + #[$m] + pub fn $n(pem: &[u8]) -> Result<$t, ::error::ErrorStack> { + unsafe { + ::init(); + let bio = try!(::bio::MemBioSlice::new(pem)); + cvt_p($f(bio.as_ptr(), ::std::ptr::null_mut(), None, ::std::ptr::null_mut())) + .map($t) + } + } + } +} + +macro_rules! public_key_from_pem { + ($t:ident, $f:path) => { + from_pem_inner!(/// Deserializes a public key from PEM-formatted data. + public_key_from_pem, $t, $f); + } +} + +macro_rules! from_pem { + ($t:ident, $f:path) => { + from_pem_inner!(/// Deserializes a value from PEM-formatted data. + from_pem, $t, $f); + } +} diff --git a/openssl/src/pkey.rs b/openssl/src/pkey.rs index 05df2f4b..b5ccd3cc 100644 --- a/openssl/src/pkey.rs +++ b/openssl/src/pkey.rs @@ -137,6 +137,7 @@ impl PKey { } private_key_from_pem!(PKey, ffi::PEM_read_bio_PrivateKey); + public_key_from_pem!(PKey, ffi::PEM_read_bio_PUBKEY); #[deprecated(since = "0.9.2", note = "use private_key_from_pem_callback")] pub fn private_key_from_pem_cb(buf: &[u8], pass_cb: F) -> Result @@ -153,19 +154,6 @@ impl PKey { Ok(PKey::from_ptr(evp)) } } - - /// Reads a public key from PEM. - pub fn public_key_from_pem(buf: &[u8]) -> Result { - ffi::init(); - let mem_bio = try!(MemBioSlice::new(buf)); - unsafe { - let evp = try!(cvt_p(ffi::PEM_read_bio_PUBKEY(mem_bio.as_ptr(), - ptr::null_mut(), - None, - ptr::null_mut()))); - Ok(PKey::from_ptr(evp)) - } - } } #[cfg(test)] diff --git a/openssl/src/rsa.rs b/openssl/src/rsa.rs index 5090f6ad..68fc9584 100644 --- a/openssl/src/rsa.rs +++ b/openssl/src/rsa.rs @@ -251,6 +251,7 @@ impl Rsa { private_key_from_pem!(Rsa, ffi::PEM_read_bio_RSAPrivateKey); private_key_from_der!(Rsa, ffi::d2i_RSAPrivateKey); + public_key_from_pem!(Rsa, ffi::PEM_read_bio_RSA_PUBKEY); public_key_from_der!(Rsa, ffi::d2i_RSA_PUBKEY); #[deprecated(since = "0.9.2", note = "use private_key_from_pem_callback")] @@ -270,19 +271,6 @@ impl Rsa { Ok(Rsa(rsa)) } } - - /// Reads an RSA 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 rsa = try!(cvt_p(ffi::PEM_read_bio_RSA_PUBKEY(mem_bio.as_ptr(), - ptr::null_mut(), - None, - ptr::null_mut()))); - Ok(Rsa(rsa)) - } - } } impl fmt::Debug for Rsa { diff --git a/openssl/src/x509/mod.rs b/openssl/src/x509/mod.rs index 8a739ec6..68652f8e 100644 --- a/openssl/src/x509/mod.rs +++ b/openssl/src/x509/mod.rs @@ -431,19 +431,8 @@ impl ToOwned for X509Ref { } impl X509 { + from_pem!(X509, ffi::PEM_read_bio_X509); from_der!(X509, ffi::d2i_X509); - - /// Reads a certificate from PEM. - pub fn from_pem(buf: &[u8]) -> Result { - let mem_bio = try!(MemBioSlice::new(buf)); - unsafe { - let handle = try!(cvt_p(ffi::PEM_read_bio_X509(mem_bio.as_ptr(), - ptr::null_mut(), - None, - ptr::null_mut()))); - Ok(X509::from_ptr(handle)) - } - } } impl Clone for X509 {