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

Macroise from_pem

parent df9666c3
Loading
Loading
Loading
Loading
+1 −14
Original line number Diff line number Diff line
@@ -4,7 +4,6 @@ use std::mem;
use std::ptr;

use {cvt, cvt_p, init};
use bio::MemBioSlice;
use bn::BigNum;
use types::OpenSslTypeRef;

@@ -26,19 +25,7 @@ impl Dh {
        }
    }

    /// Reads Diffie-Hellman parameters from PEM.
    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_pem!(Dh, ffi::PEM_read_bio_DHparams);
    from_der!(Dh, ffi::d2i_DHparams);

    /// Requires the `v102` or `v110` features and OpenSSL 1.0.2 or OpenSSL 1.1.0.
+1 −14
Original line number Diff line number Diff line
@@ -97,6 +97,7 @@ impl Dsa {

    private_key_from_pem!(Dsa, ffi::PEM_read_bio_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);

    #[deprecated(since = "0.9.2", note = "use private_key_from_pem_callback")]
@@ -116,20 +117,6 @@ impl 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 {
+30 −9
Original line number Diff line number Diff line
@@ -42,15 +42,8 @@ macro_rules! type_ {

macro_rules! private_key_from_pem {
    ($t:ident, $f:path) => {
        /// Deserializes a PEM-formatted private key.
        pub fn private_key_from_pem(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)
            }
        }
        from_pem_inner!(/// Deserializes a PEM-formatted private key.
            private_key_from_pem, $t, $f);

        /// Deserializes a PEM-formatted private key, using the supplied password if the key is
        /// encrypted.
@@ -232,3 +225,31 @@ macro_rules! public_key_from_der {
            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);
    }
}
+1 −13
Original line number Diff line number Diff line
@@ -137,6 +137,7 @@ impl PKey {
    }

    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")]
    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))
        }
    }

    /// 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)]
+1 −13
Original line number Diff line number Diff line
@@ -251,6 +251,7 @@ impl Rsa {

    private_key_from_pem!(Rsa, ffi::PEM_read_bio_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);

    #[deprecated(since = "0.9.2", note = "use private_key_from_pem_callback")]
@@ -270,19 +271,6 @@ impl 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 {
Loading