Commit 48c00094 authored by Steven Fackler's avatar Steven Fackler
Browse files

Macroise from_der

parent b0415f46
Loading
Loading
Loading
Loading
+1 −0
Original line number Diff line number Diff line
@@ -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;
+1 −11
Original line number Diff line number Diff line
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)))]
+3 −22
Original line number Diff line number Diff line
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 {
+1 −11
Original line number Diff line number Diff line
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)]
+35 −0
Original line number Diff line number Diff line
@@ -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);
    }
}
Loading