Macroise from_der

This commit is contained in:
Steven Fackler 2016-11-13 17:06:50 +00:00
parent b0415f466c
commit 48c0009418
8 changed files with 53 additions and 92 deletions

View File

@ -1760,6 +1760,7 @@ extern {
pub fn i2d_ECPrivateKey(ec_key: *mut EC_KEY, pp: *mut *mut c_uchar) -> c_int;
pub fn d2i_X509(a: *mut *mut X509, pp: *mut *const c_uchar, length: c_long) -> *mut X509;
pub fn d2i_X509_REQ(a: *mut *mut X509_REQ, pp: *mut *const c_uchar, length: c_long) -> *mut X509_REQ;
pub fn i2d_X509_bio(b: *mut BIO, x: *mut X509) -> c_int;
pub fn i2d_X509(x: *mut X509, buf: *mut *mut u8) -> c_int;
pub fn i2d_X509_REQ_bio(b: *mut BIO, x: *mut X509_REQ) -> c_int;

View File

@ -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)))]

View File

@ -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 {

View File

@ -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)]

View File

@ -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);
}
}

View File

@ -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,

View File

@ -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 {

View File

@ -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.