Macroise from_pem

This commit is contained in:
Steven Fackler 2016-11-13 17:56:48 +00:00
parent df9666c334
commit ccef9e339d
6 changed files with 35 additions and 75 deletions

View File

@ -4,7 +4,6 @@ use std::mem;
use std::ptr; use std::ptr;
use {cvt, cvt_p, init}; use {cvt, cvt_p, init};
use bio::MemBioSlice;
use bn::BigNum; use bn::BigNum;
use types::OpenSslTypeRef; use types::OpenSslTypeRef;
@ -26,19 +25,7 @@ impl Dh {
} }
} }
/// Reads Diffie-Hellman parameters from PEM. from_pem!(Dh, ffi::PEM_read_bio_DHparams);
pub fn from_pem(buf: &[u8]) -> Result<Dh, ErrorStack> {
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_der!(Dh, ffi::d2i_DHparams); from_der!(Dh, ffi::d2i_DHparams);
/// Requires the `v102` or `v110` features and OpenSSL 1.0.2 or OpenSSL 1.1.0. /// Requires the `v102` or `v110` features and OpenSSL 1.0.2 or OpenSSL 1.1.0.

View File

@ -97,6 +97,7 @@ impl Dsa {
private_key_from_pem!(Dsa, ffi::PEM_read_bio_DSAPrivateKey); private_key_from_pem!(Dsa, ffi::PEM_read_bio_DSAPrivateKey);
private_key_from_der!(Dsa, ffi::d2i_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); public_key_from_der!(Dsa, ffi::d2i_DSAPublicKey);
#[deprecated(since = "0.9.2", note = "use private_key_from_pem_callback")] #[deprecated(since = "0.9.2", note = "use private_key_from_pem_callback")]
@ -116,20 +117,6 @@ impl Dsa {
Ok(Dsa(dsa)) Ok(Dsa(dsa))
} }
} }
/// Reads a DSA public key from PEM formatted data.
pub fn public_key_from_pem(buf: &[u8]) -> Result<Dsa, ErrorStack> {
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 { impl fmt::Debug for Dsa {

View File

@ -42,15 +42,8 @@ macro_rules! type_ {
macro_rules! private_key_from_pem { macro_rules! private_key_from_pem {
($t:ident, $f:path) => { ($t:ident, $f:path) => {
/// Deserializes a PEM-formatted private key. from_pem_inner!(/// Deserializes a PEM-formatted private key.
pub fn private_key_from_pem(pem: &[u8]) -> Result<$t, ::error::ErrorStack> { private_key_from_pem, $t, $f);
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)
}
}
/// Deserializes a PEM-formatted private key, using the supplied password if the key is /// Deserializes a PEM-formatted private key, using the supplied password if the key is
/// encrypted. /// encrypted.
@ -232,3 +225,31 @@ macro_rules! public_key_from_der {
public_key_from_der, $t, $f); 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);
}
}

View File

@ -137,6 +137,7 @@ impl PKey {
} }
private_key_from_pem!(PKey, ffi::PEM_read_bio_PrivateKey); 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")] #[deprecated(since = "0.9.2", note = "use private_key_from_pem_callback")]
pub fn private_key_from_pem_cb<F>(buf: &[u8], pass_cb: F) -> Result<PKey, ErrorStack> pub fn private_key_from_pem_cb<F>(buf: &[u8], pass_cb: F) -> Result<PKey, ErrorStack>
@ -153,19 +154,6 @@ impl PKey {
Ok(PKey::from_ptr(evp)) Ok(PKey::from_ptr(evp))
} }
} }
/// Reads a public key from PEM.
pub fn public_key_from_pem(buf: &[u8]) -> Result<PKey, ErrorStack> {
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)] #[cfg(test)]

View File

@ -251,6 +251,7 @@ impl Rsa {
private_key_from_pem!(Rsa, ffi::PEM_read_bio_RSAPrivateKey); private_key_from_pem!(Rsa, ffi::PEM_read_bio_RSAPrivateKey);
private_key_from_der!(Rsa, ffi::d2i_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); public_key_from_der!(Rsa, ffi::d2i_RSA_PUBKEY);
#[deprecated(since = "0.9.2", note = "use private_key_from_pem_callback")] #[deprecated(since = "0.9.2", note = "use private_key_from_pem_callback")]
@ -270,19 +271,6 @@ impl Rsa {
Ok(Rsa(rsa)) Ok(Rsa(rsa))
} }
} }
/// Reads an RSA public key from PEM formatted data.
pub fn public_key_from_pem(buf: &[u8]) -> Result<Rsa, ErrorStack> {
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 { impl fmt::Debug for Rsa {

View File

@ -431,19 +431,8 @@ impl ToOwned for X509Ref {
} }
impl X509 { impl X509 {
from_pem!(X509, ffi::PEM_read_bio_X509);
from_der!(X509, ffi::d2i_X509); from_der!(X509, ffi::d2i_X509);
/// Reads a certificate from PEM.
pub fn from_pem(buf: &[u8]) -> Result<X509, ErrorStack> {
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 { impl Clone for X509 {