Commit b0415f46 authored by Steven Fackler's avatar Steven Fackler
Browse files

Macroise to_der

parent ed9f600e
Loading
Loading
Loading
Loading
+4 −0
Original line number Diff line number Diff line
@@ -1761,10 +1761,14 @@ extern {

    pub fn d2i_X509(a: *mut *mut X509, pp: *mut *const c_uchar, length: c_long) -> *mut X509;
    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;
    pub fn i2d_X509_REQ(x: *mut X509_REQ, buf: *mut *mut u8) -> c_int;

    pub fn i2d_PUBKEY_bio(b: *mut BIO, x: *mut EVP_PKEY) -> c_int;
    pub fn i2d_PrivateKey_bio(b: *mut BIO, x: *mut EVP_PKEY) -> c_int;
    pub fn i2d_PUBKEY(k: *mut EVP_PKEY, buf: *mut *mut u8) -> c_int;
    pub fn i2d_PrivateKey(k: *mut EVP_PKEY, buf: *mut *mut u8) -> c_int;

    pub fn i2d_RSA_PUBKEY(k: *mut RSA, buf: *mut *mut u8) -> c_int;
    pub fn d2i_RSA_PUBKEY(k: *mut *mut RSA, buf: *mut *const u8, len: c_long) -> *mut RSA;
+1 −9
Original line number Diff line number Diff line
@@ -22,15 +22,7 @@ impl DhRef {
        Ok(mem_bio.get_buf().to_owned())
    }

    /// Encodes the parameters to DER.
    pub fn to_der(&self) -> Result<Vec<u8>, ErrorStack> {
        unsafe {
            let len = try!(cvt(ffi::i2d_DHparams(self.as_ptr(), ptr::null_mut())));
            let mut buf = vec![0; len as usize];
            try!(cvt(ffi::i2d_DHparams(self.as_ptr(), &mut buf.as_mut_ptr())));
            Ok(buf)
        }
    }
    to_der!(ffi::i2d_DHparams);
}

impl Dh {
+2 −19
Original line number Diff line number Diff line
@@ -25,25 +25,8 @@ impl DsaRef {
        Ok(mem_bio.get_buf().to_owned())
    }

    /// Encodes a DSA private key as unencrypted DER formatted data.
    pub fn private_key_to_der(&self) -> Result<Vec<u8>, ErrorStack> {
        unsafe {
            let len = try!(cvt(ffi::i2d_DSAPrivateKey(self.as_ptr(), ptr::null_mut())));
            let mut buf = vec![0; len as usize];
            try!(cvt(ffi::i2d_DSAPrivateKey(self.as_ptr(), &mut buf.as_mut_ptr())));
            Ok(buf)
        }
    }

    /// Encodes a DSA public key as DER formatted data.
    pub fn public_key_to_der(&self) -> Result<Vec<u8>, ErrorStack> {
        unsafe {
            let len = try!(cvt(ffi::i2d_DSAPublicKey(self.as_ptr(), ptr::null_mut())));
            let mut buf = vec![0; len as usize];
            try!(cvt(ffi::i2d_DSAPublicKey(self.as_ptr(), &mut buf.as_mut_ptr())));
            Ok(buf)
        }
    }
    private_key_to_der!(ffi::i2d_DSAPrivateKey);
    public_key_to_der!(ffi::i2d_DSAPublicKey);

    // FIXME should return u32
    pub fn size(&self) -> Option<u32> {
+2 −10
Original line number Diff line number Diff line
@@ -12,16 +12,7 @@ type_!(EcKey, EcKeyRef, ffi::EC_KEY, ffi::EC_KEY_free);

impl EcKeyRef {
    private_key_to_pem!(ffi::PEM_write_bio_ECPrivateKey);

    /// Serializes the private key components to DER.
    pub fn private_key_to_der(&self) -> Result<Vec<u8>, ErrorStack> {
        unsafe {
            let len = try!(cvt(ffi::i2d_ECPrivateKey(self.as_ptr(), ptr::null_mut())));
            let mut buf = vec![0; len as usize];
            try!(cvt(ffi::i2d_ECPrivateKey(self.as_ptr(), &mut buf.as_mut_ptr())));
            Ok(buf)
        }
    }
    private_key_to_der!(ffi::i2d_ECPrivateKey);
}

impl EcKey {
@@ -31,6 +22,7 @@ impl EcKey {
            cvt_p(ffi::EC_KEY_new_by_curve_name(nid.as_raw())).map(EcKey)
        }
    }

    /// Deserializes a DER-encoded private key.
    pub fn private_key_from_der(der: &[u8]) -> Result<EcKey, ErrorStack> {
        unsafe {
+35 −0
Original line number Diff line number Diff line
@@ -135,3 +135,38 @@ macro_rules! private_key_to_pem {
        }
    }
}

macro_rules! to_der_inner {
    (#[$m:meta] $n:ident, $f:path) => {
        #[$m]
        pub fn $n(&self) -> Result<Vec<u8>, ::error::ErrorStack> {
            unsafe {
                let len = try!(::cvt($f(::types::OpenSslTypeRef::as_ptr(self), ptr::null_mut())));
                let mut buf = vec![0; len as usize];
                try!(::cvt($f(::types::OpenSslTypeRef::as_ptr(self), &mut buf.as_mut_ptr())));
                Ok(buf)
            }
        }
    };
}

macro_rules! to_der {
    ($f:path) => {
        to_der_inner!(/// Serializes this value to DER.
            to_der, $f);
    }
}

macro_rules! private_key_to_der {
    ($f:path) => {
        to_der_inner!(/// Serializes the private key to DER.
            private_key_to_der, $f);
    }
}

macro_rules! public_key_to_der {
    ($f:path) => {
        to_der_inner!(/// Serializes the public key to DER.
            public_key_to_der, $f);
    }
}
Loading