diff options
| author | Steven Fackler <[email protected]> | 2016-11-13 17:06:50 +0000 |
|---|---|---|
| committer | Steven Fackler <[email protected]> | 2016-11-13 17:06:50 +0000 |
| commit | 48c0009418cbbf7c69c24b35d56e80edb0c80d45 (patch) | |
| tree | 21ab4cb2b259ea883a36865d0971c3ad65ea0722 /openssl/src | |
| parent | Macroise to_der (diff) | |
| download | rust-openssl-48c0009418cbbf7c69c24b35d56e80edb0c80d45.tar.xz rust-openssl-48c0009418cbbf7c69c24b35d56e80edb0c80d45.zip | |
Macroise from_der
Diffstat (limited to 'openssl/src')
| -rw-r--r-- | openssl/src/dh.rs | 12 | ||||
| -rw-r--r-- | openssl/src/dsa.rs | 25 | ||||
| -rw-r--r-- | openssl/src/ec_key.rs | 12 | ||||
| -rw-r--r-- | openssl/src/macros.rs | 35 | ||||
| -rw-r--r-- | openssl/src/pkcs12.rs | 22 | ||||
| -rw-r--r-- | openssl/src/rsa.rs | 25 | ||||
| -rw-r--r-- | openssl/src/x509/mod.rs | 13 |
7 files changed, 52 insertions, 92 deletions
diff --git a/openssl/src/dh.rs b/openssl/src/dh.rs index 2b0a1508..604d4f5a 100644 --- a/openssl/src/dh.rs +++ b/openssl/src/dh.rs @@ -1,7 +1,5 @@ use error::ErrorStack; use ffi; -use libc::c_long; -use std::cmp; use std::mem; use std::ptr; @@ -49,15 +47,7 @@ impl Dh { } } - /// Reads Diffie-Hellman parameters from DER. - pub fn from_der(buf: &[u8]) -> Result<Dh, ErrorStack> { - unsafe { - init(); - let len = cmp::min(buf.len(), c_long::max_value() as usize) as c_long; - let dh = try!(cvt_p(ffi::d2i_DHparams(ptr::null_mut(), &mut buf.as_ptr(), len))); - Ok(Dh(dh)) - } - } + from_der!(Dh, ffi::d2i_DHparams); /// Requires the `v102` or `v110` features and OpenSSL 1.0.2 or OpenSSL 1.1.0. #[cfg(any(all(feature = "v102", ossl102), all(feature = "v110", ossl110)))] diff --git a/openssl/src/dsa.rs b/openssl/src/dsa.rs index 0444ed9f..478272c8 100644 --- a/openssl/src/dsa.rs +++ b/openssl/src/dsa.rs @@ -1,9 +1,8 @@ use error::ErrorStack; use ffi; -use libc::{c_int, c_char, c_void, c_long}; +use libc::{c_int, c_char, c_void}; use std::fmt; use std::ptr; -use std::cmp; use bio::{MemBio, MemBioSlice}; use bn::BigNumRef; @@ -97,6 +96,8 @@ impl Dsa { } private_key_from_pem!(Dsa, ffi::PEM_read_bio_DSAPrivateKey); + private_key_from_der!(Dsa, ffi::d2i_DSAPrivateKey); + public_key_from_der!(Dsa, ffi::d2i_DSAPublicKey); #[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<Dsa, ErrorStack> @@ -129,26 +130,6 @@ impl Dsa { Ok(Dsa(dsa)) } } - - /// Reads a DSA private key from DER formatted data. - pub fn private_key_from_der(buf: &[u8]) -> Result<Dsa, ErrorStack> { - unsafe { - ffi::init(); - let len = cmp::min(buf.len(), c_long::max_value() as usize) as c_long; - let dsa = try!(cvt_p(ffi::d2i_DSAPrivateKey(ptr::null_mut(), &mut buf.as_ptr(), len))); - Ok(Dsa(dsa)) - } - } - - /// Reads a DSA public key from DER formatted data. - pub fn public_key_from_der(buf: &[u8]) -> Result<Dsa, ErrorStack> { - unsafe { - ffi::init(); - let len = cmp::min(buf.len(), c_long::max_value() as usize) as c_long; - let dsa = try!(cvt_p(ffi::d2i_DSAPublicKey(ptr::null_mut(), &mut buf.as_ptr(), len))); - Ok(Dsa(dsa)) - } - } } impl fmt::Debug for Dsa { diff --git a/openssl/src/ec_key.rs b/openssl/src/ec_key.rs index 706265ef..268a6fd2 100644 --- a/openssl/src/ec_key.rs +++ b/openssl/src/ec_key.rs @@ -1,6 +1,4 @@ use ffi; -use std::cmp; -use libc::c_long; use std::ptr; use {cvt, cvt_p, init}; @@ -23,16 +21,8 @@ impl EcKey { } } - /// Deserializes a DER-encoded private key. - pub fn private_key_from_der(der: &[u8]) -> Result<EcKey, ErrorStack> { - unsafe { - init(); - let len = cmp::min(der.len(), c_long::max_value() as usize) as c_long; - cvt_p(ffi::d2i_ECPrivateKey(ptr::null_mut(), &mut der.as_ptr(), len)).map(EcKey) - } - } - private_key_from_pem!(EcKey, ffi::PEM_read_bio_ECPrivateKey); + private_key_from_der!(EcKey, ffi::d2i_ECPrivateKey); } #[cfg(test)] diff --git a/openssl/src/macros.rs b/openssl/src/macros.rs index 7fa15d1f..39944124 100644 --- a/openssl/src/macros.rs +++ b/openssl/src/macros.rs @@ -170,3 +170,38 @@ macro_rules! public_key_to_der { public_key_to_der, $f); } } + +macro_rules! from_der_inner { + (#[$m:meta] $n:ident, $t:ident, $f:path) => { + #[$m] + pub fn $n(der: &[u8]) -> Result<$t, ::error::ErrorStack> { + unsafe { + ::ffi::init(); + let len = ::std::cmp::min(der.len(), ::libc::c_long::max_value() as usize) as ::libc::c_long; + ::cvt_p($f(::std::ptr::null_mut(), &mut der.as_ptr(), len)) + .map($t) + } + } + } +} + +macro_rules! from_der { + ($t:ident, $f:path) => { + from_der_inner!(/// Deserializes a value from DER-formatted data. + from_der, $t, $f); + } +} + +macro_rules! private_key_from_der { + ($t:ident, $f:path) => { + from_der_inner!(/// Deserializes a private key from DER-formatted data. + private_key_from_der, $t, $f); + } +} + +macro_rules! public_key_from_der { + ($t:ident, $f:path) => { + from_der_inner!(/// Deserializes a public key from DER-formatted data. + public_key_from_der, $t, $f); + } +} diff --git a/openssl/src/pkcs12.rs b/openssl/src/pkcs12.rs index 9c224ccd..ee9ae124 100644 --- a/openssl/src/pkcs12.rs +++ b/openssl/src/pkcs12.rs @@ -1,12 +1,10 @@ //! PKCS #12 archives. use ffi; -use libc::{c_long, c_uchar}; -use std::cmp; use std::ptr; use std::ffi::CString; -use {cvt, cvt_p}; +use cvt; use pkey::PKey; use error::ErrorStack; use x509::X509; @@ -15,21 +13,9 @@ use stack::Stack; type_!(Pkcs12, Pkcs12Ref, ffi::PKCS12, ffi::PKCS12_free); -impl Pkcs12 { - /// Deserializes a `Pkcs12` structure from DER-encoded data. - pub fn from_der(der: &[u8]) -> Result<Pkcs12, ErrorStack> { - unsafe { - ffi::init(); - let mut ptr = der.as_ptr() as *const c_uchar; - let length = cmp::min(der.len(), c_long::max_value() as usize) as c_long; - let p12 = try!(cvt_p(ffi::d2i_PKCS12(ptr::null_mut(), &mut ptr, length))); - Ok(Pkcs12(p12)) - } - } -} - impl Pkcs12Ref { /// Extracts the contents of the `Pkcs12`. + // FIXME should take an &[u8] pub fn parse(&self, pass: &str) -> Result<ParsedPkcs12, ErrorStack> { unsafe { let pass = CString::new(pass).unwrap(); @@ -57,6 +43,10 @@ impl Pkcs12Ref { } } +impl Pkcs12 { + from_der!(Pkcs12, ffi::d2i_PKCS12); +} + pub struct ParsedPkcs12 { pub pkey: PKey, pub cert: X509, diff --git a/openssl/src/rsa.rs b/openssl/src/rsa.rs index 89c0bb85..5090f6ad 100644 --- a/openssl/src/rsa.rs +++ b/openssl/src/rsa.rs @@ -1,9 +1,8 @@ use ffi; -use std::cmp; use std::fmt; use std::ptr; use std::mem; -use libc::{c_int, c_void, c_char, c_long}; +use libc::{c_int, c_void, c_char}; use {cvt, cvt_p, cvt_n}; use bn::{BigNum, BigNumRef}; @@ -251,6 +250,8 @@ impl Rsa { } private_key_from_pem!(Rsa, ffi::PEM_read_bio_RSAPrivateKey); + private_key_from_der!(Rsa, ffi::d2i_RSAPrivateKey); + public_key_from_der!(Rsa, ffi::d2i_RSA_PUBKEY); #[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<Rsa, ErrorStack> @@ -282,26 +283,6 @@ impl Rsa { Ok(Rsa(rsa)) } } - - /// Reads an RSA private key from DER formatted data. - pub fn private_key_from_der(buf: &[u8]) -> Result<Rsa, ErrorStack> { - unsafe { - ffi::init(); - let len = cmp::min(buf.len(), c_long::max_value() as usize) as c_long; - let dsa = try!(cvt_p(ffi::d2i_RSAPrivateKey(ptr::null_mut(), &mut buf.as_ptr(), len))); - Ok(Rsa(dsa)) - } - } - - /// Reads an RSA public key from DER formatted data. - pub fn public_key_from_der(buf: &[u8]) -> Result<Rsa, ErrorStack> { - unsafe { - ffi::init(); - let len = cmp::min(buf.len(), c_long::max_value() as usize) as c_long; - let dsa = try!(cvt_p(ffi::d2i_RSA_PUBKEY(ptr::null_mut(), &mut buf.as_ptr(), len))); - Ok(Rsa(dsa)) - } - } } impl fmt::Debug for Rsa { diff --git a/openssl/src/x509/mod.rs b/openssl/src/x509/mod.rs index 74f586c2..0a5a6c4d 100644 --- a/openssl/src/x509/mod.rs +++ b/openssl/src/x509/mod.rs @@ -1,6 +1,5 @@ use libc::{c_char, c_int, c_long, c_ulong}; use std::borrow::Borrow; -use std::cmp; use std::collections::HashMap; use std::error::Error; use std::ffi::{CStr, CString}; @@ -440,15 +439,7 @@ impl ToOwned for X509Ref { } impl X509 { - /// Reads a certificate from DER. - pub fn from_der(buf: &[u8]) -> Result<X509, ErrorStack> { - unsafe { - let mut ptr = buf.as_ptr(); - let len = cmp::min(buf.len(), c_long::max_value() as usize) as c_long; - let x509 = try!(cvt_p(ffi::d2i_X509(ptr::null_mut(), &mut ptr, len))); - Ok(X509::from_ptr(x509)) - } - } + from_der!(X509, ffi::d2i_X509); /// Reads a certificate from PEM. pub fn from_pem(buf: &[u8]) -> Result<X509, ErrorStack> { @@ -583,6 +574,8 @@ impl X509Req { Ok(X509Req::from_ptr(handle)) } } + + from_der!(X509Req, ffi::d2i_X509_REQ); } /// A collection of X.509 extensions. |