Loading openssl-sys/src/lib.rs +9 −0 Original line number Diff line number Diff line Loading @@ -1713,11 +1713,20 @@ extern { name: *const c_char, namelen: size_t) -> c_int; pub fn d2i_DHparams(k: *mut *mut DH, pp: *mut *const c_uchar, length: c_long) -> *mut DH; pub fn i2d_DHparams(dh: *const DH, pp: *mut *mut c_uchar) -> c_int; pub fn d2i_DSAPublicKey(a: *mut *mut DSA, pp: *mut *const c_uchar, length: c_long) -> *mut DSA; pub fn i2d_DSAPublicKey(a: *const DSA, pp: *mut *mut c_uchar) -> c_int; pub fn d2i_DSAPrivateKey(a: *mut *mut DSA, pp: *mut *const c_uchar, length: c_long) -> *mut DSA; pub fn i2d_DSAPrivateKey(a: *const DSA, 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 i2d_X509_bio(b: *mut BIO, x: *mut X509) -> c_int; pub fn i2d_X509_REQ_bio(b: *mut BIO, x: *mut X509_REQ) -> 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_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; Loading openssl/src/dh.rs +39 −9 Original line number Diff line number Diff line use ffi; use error::ErrorStack; use bio::MemBioSlice; use std::ptr; use ffi; use libc::c_long; use std::cmp; use std::mem; use std::ptr; use {cvt, cvt_p}; use bio::MemBio; use {cvt, cvt_p, init}; use bio::{MemBio, MemBioSlice}; use bn::BigNum; use types::OpenSslTypeRef; Loading @@ -15,18 +16,27 @@ impl DhRef { /// Encodes the parameters to PEM. pub fn to_pem(&self) -> Result<Vec<u8>, ErrorStack> { let mem_bio = try!(MemBio::new()); unsafe { try!(cvt(ffi::PEM_write_bio_DHparams(mem_bio.as_ptr(), self.as_ptr()))); } 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) } } } impl Dh { pub fn from_params(p: BigNum, g: BigNum, q: BigNum) -> Result<Dh, ErrorStack> { unsafe { init(); let dh = Dh(try!(cvt_p(ffi::DH_new()))); try!(cvt(compat::DH_set0_pqg(dh.0, p.as_ptr(), q.as_ptr(), g.as_ptr()))); mem::forget((p, g, q)); Loading @@ -34,9 +44,11 @@ impl Dh { } } /// Reads Diffie-Hellman parameters from PEM. pub fn from_pem(buf: &[u8]) -> Result<Dh, ErrorStack> { let mem_bio = try!(MemBioSlice::new(buf)); unsafe { init(); let mem_bio = try!(MemBioSlice::new(buf)); cvt_p(ffi::PEM_read_bio_DHparams(mem_bio.as_ptr(), ptr::null_mut(), None, Loading @@ -45,6 +57,16 @@ 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)) } } /// 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)))] pub fn get_1024_160() -> Result<Dh, ErrorStack> { Loading Loading @@ -139,7 +161,15 @@ mod tests { fn test_dh_from_pem() { let mut ctx = SslContext::builder(SslMethod::tls()).unwrap(); let params = include_bytes!("../test/dhparams.pem"); let dh = Dh::from_pem(params).ok().expect("Failed to load PEM"); let dh = Dh::from_pem(params).unwrap(); ctx.set_tmp_dh(&dh).unwrap(); } #[test] fn test_dh_from_der() { let params = include_bytes!("../test/dhparams.pem"); let dh = Dh::from_pem(params).unwrap(); let der = dh.to_der().unwrap(); Dh::from_der(&der).unwrap(); } } openssl/src/dsa.rs +45 −4 Original line number Diff line number Diff line use error::ErrorStack; use ffi; use libc::{c_int, c_char, c_void}; use libc::{c_int, c_char, c_void, c_long}; use std::fmt; use std::ptr; use std::cmp; use bio::{MemBio, MemBioSlice}; use bn::BigNumRef; Loading @@ -13,7 +14,7 @@ use util::{CallbackState, invoke_passwd_cb}; type_!(Dsa, DsaRef, ffi::DSA, ffi::DSA_free); impl DsaRef { /// Writes an DSA private key as unencrypted PEM formatted data /// Encodes a DSA private key as unencrypted PEM formatted data. pub fn private_key_to_pem(&self) -> Result<Vec<u8>, ErrorStack> { assert!(self.has_private_key()); let mem_bio = try!(MemBio::new()); Loading @@ -27,7 +28,7 @@ impl DsaRef { Ok(mem_bio.get_buf().to_owned()) } /// Writes an DSA public key as PEM formatted data /// Encodes a DSA public key as PEM formatted data. pub fn public_key_to_pem(&self) -> Result<Vec<u8>, ErrorStack> { let mem_bio = try!(MemBio::new()); unsafe { Loading @@ -36,6 +37,26 @@ 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) } } pub fn size(&self) -> Option<u32> { if self.q().is_some() { unsafe { Some(ffi::DSA_size(self.as_ptr()) as u32) } Loading Loading @@ -139,7 +160,7 @@ impl Dsa { } } /// Reads an DSA public key from PEM formatted data. /// Reads a DSA public key from PEM formatted data. pub fn public_key_from_pem(buf: &[u8]) -> Result<Dsa, ErrorStack> { ffi::init(); Loading @@ -152,6 +173,26 @@ 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 { Loading openssl/src/pkey.rs +14 −5 Original line number Diff line number Diff line Loading @@ -48,7 +48,7 @@ impl PKeyRef { } } /// Stores private key as a PEM /// Encodes the private key in the PEM format. // FIXME: also add password and encryption pub fn private_key_to_pem(&self) -> Result<Vec<u8>, ErrorStack> { let mem_bio = try!(MemBio::new()); Loading @@ -65,7 +65,7 @@ impl PKeyRef { Ok(mem_bio.get_buf().to_owned()) } /// Encode public key in PEM format /// Encodes the public key in the PEM format. pub fn public_key_to_pem(&self) -> Result<Vec<u8>, ErrorStack> { let mem_bio = try!(MemBio::new()); unsafe { Loading @@ -74,7 +74,7 @@ impl PKeyRef { Ok(mem_bio.get_buf().to_owned()) } /// Encode public key in DER format /// Encodes the public key in the DER format. pub fn public_key_to_der(&self) -> Result<Vec<u8>, ErrorStack> { let mem_bio = try!(MemBio::new()); unsafe { Loading @@ -83,6 +83,15 @@ impl PKeyRef { Ok(mem_bio.get_buf().to_owned()) } /// Encodes the private key in the DER format pub fn private_key_to_der(&self) -> Result<Vec<u8>, ErrorStack> { let mem_bio = try!(MemBio::new()); unsafe { try!(cvt(ffi::i2d_PrivateKey_bio(mem_bio.as_ptr(), self.as_ptr()))); } Ok(mem_bio.get_buf().to_owned()) } pub fn public_eq(&self, other: &PKeyRef) -> bool { unsafe { ffi::EVP_PKEY_cmp(self.as_ptr(), other.as_ptr()) == 1 } } Loading Loading @@ -148,7 +157,7 @@ impl PKey { } } /// Reads private key from PEM, takes ownership of handle /// Reads a private key from PEM. pub fn private_key_from_pem(buf: &[u8]) -> Result<PKey, ErrorStack> { ffi::init(); let mem_bio = try!(MemBioSlice::new(buf)); Loading Loading @@ -181,7 +190,7 @@ impl PKey { } } /// Reads public key from PEM, takes ownership of handle /// 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)); Loading openssl/src/rsa.rs +46 −1 Original line number Diff line number Diff line use ffi; use std::cmp; use std::fmt; use std::ptr; use std::mem; use libc::{c_int, c_void, c_char}; use libc::{c_int, c_void, c_char, c_long}; use {cvt, cvt_p, cvt_n}; use bn::{BigNum, BigNumRef}; Loading Loading @@ -49,6 +50,26 @@ impl RsaRef { Ok(mem_bio.get_buf().to_owned()) } /// Encodes an RSA 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_RSAPrivateKey(self.as_ptr(), ptr::null_mut()))); let mut buf = vec![0; len as usize]; try!(cvt(ffi::i2d_RSAPrivateKey(self.as_ptr(), &mut buf.as_mut_ptr()))); Ok(buf) } } /// Encodes an RSA public key as DER formatted data. pub fn public_key_to_der(&self) -> Result<Vec<u8>, ErrorStack> { unsafe { let len = try!(cvt(ffi::i2d_RSA_PUBKEY(self.as_ptr(), ptr::null_mut()))); let mut buf = vec![0; len as usize]; try!(cvt(ffi::i2d_RSA_PUBKEY(self.as_ptr(), &mut buf.as_mut_ptr()))); Ok(buf) } } pub fn size(&self) -> usize { unsafe { assert!(self.n().is_some()); Loading Loading @@ -250,6 +271,7 @@ impl Rsa { /// /// The public exponent will be 65537. pub fn generate(bits: u32) -> Result<Rsa, ErrorStack> { ffi::init(); unsafe { let rsa = Rsa(try!(cvt_p(ffi::RSA_new()))); let e = try!(BigNum::from_u32(ffi::RSA_F4 as u32)); Loading @@ -260,6 +282,7 @@ impl Rsa { /// Reads an RSA private key from PEM formatted data. pub fn private_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_RSAPrivateKey(mem_bio.as_ptr(), Loading @@ -274,6 +297,7 @@ impl Rsa { pub fn private_key_from_pem_cb<F>(buf: &[u8], pass_cb: F) -> Result<Rsa, ErrorStack> where F: FnOnce(&mut [c_char]) -> usize { ffi::init(); let mut cb = CallbackState::new(pass_cb); let mem_bio = try!(MemBioSlice::new(buf)); Loading @@ -289,6 +313,7 @@ impl 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(), Loading @@ -298,6 +323,26 @@ 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 { Loading Loading
openssl-sys/src/lib.rs +9 −0 Original line number Diff line number Diff line Loading @@ -1713,11 +1713,20 @@ extern { name: *const c_char, namelen: size_t) -> c_int; pub fn d2i_DHparams(k: *mut *mut DH, pp: *mut *const c_uchar, length: c_long) -> *mut DH; pub fn i2d_DHparams(dh: *const DH, pp: *mut *mut c_uchar) -> c_int; pub fn d2i_DSAPublicKey(a: *mut *mut DSA, pp: *mut *const c_uchar, length: c_long) -> *mut DSA; pub fn i2d_DSAPublicKey(a: *const DSA, pp: *mut *mut c_uchar) -> c_int; pub fn d2i_DSAPrivateKey(a: *mut *mut DSA, pp: *mut *const c_uchar, length: c_long) -> *mut DSA; pub fn i2d_DSAPrivateKey(a: *const DSA, 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 i2d_X509_bio(b: *mut BIO, x: *mut X509) -> c_int; pub fn i2d_X509_REQ_bio(b: *mut BIO, x: *mut X509_REQ) -> 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_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; Loading
openssl/src/dh.rs +39 −9 Original line number Diff line number Diff line use ffi; use error::ErrorStack; use bio::MemBioSlice; use std::ptr; use ffi; use libc::c_long; use std::cmp; use std::mem; use std::ptr; use {cvt, cvt_p}; use bio::MemBio; use {cvt, cvt_p, init}; use bio::{MemBio, MemBioSlice}; use bn::BigNum; use types::OpenSslTypeRef; Loading @@ -15,18 +16,27 @@ impl DhRef { /// Encodes the parameters to PEM. pub fn to_pem(&self) -> Result<Vec<u8>, ErrorStack> { let mem_bio = try!(MemBio::new()); unsafe { try!(cvt(ffi::PEM_write_bio_DHparams(mem_bio.as_ptr(), self.as_ptr()))); } 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) } } } impl Dh { pub fn from_params(p: BigNum, g: BigNum, q: BigNum) -> Result<Dh, ErrorStack> { unsafe { init(); let dh = Dh(try!(cvt_p(ffi::DH_new()))); try!(cvt(compat::DH_set0_pqg(dh.0, p.as_ptr(), q.as_ptr(), g.as_ptr()))); mem::forget((p, g, q)); Loading @@ -34,9 +44,11 @@ impl Dh { } } /// Reads Diffie-Hellman parameters from PEM. pub fn from_pem(buf: &[u8]) -> Result<Dh, ErrorStack> { let mem_bio = try!(MemBioSlice::new(buf)); unsafe { init(); let mem_bio = try!(MemBioSlice::new(buf)); cvt_p(ffi::PEM_read_bio_DHparams(mem_bio.as_ptr(), ptr::null_mut(), None, Loading @@ -45,6 +57,16 @@ 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)) } } /// 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)))] pub fn get_1024_160() -> Result<Dh, ErrorStack> { Loading Loading @@ -139,7 +161,15 @@ mod tests { fn test_dh_from_pem() { let mut ctx = SslContext::builder(SslMethod::tls()).unwrap(); let params = include_bytes!("../test/dhparams.pem"); let dh = Dh::from_pem(params).ok().expect("Failed to load PEM"); let dh = Dh::from_pem(params).unwrap(); ctx.set_tmp_dh(&dh).unwrap(); } #[test] fn test_dh_from_der() { let params = include_bytes!("../test/dhparams.pem"); let dh = Dh::from_pem(params).unwrap(); let der = dh.to_der().unwrap(); Dh::from_der(&der).unwrap(); } }
openssl/src/dsa.rs +45 −4 Original line number Diff line number Diff line use error::ErrorStack; use ffi; use libc::{c_int, c_char, c_void}; use libc::{c_int, c_char, c_void, c_long}; use std::fmt; use std::ptr; use std::cmp; use bio::{MemBio, MemBioSlice}; use bn::BigNumRef; Loading @@ -13,7 +14,7 @@ use util::{CallbackState, invoke_passwd_cb}; type_!(Dsa, DsaRef, ffi::DSA, ffi::DSA_free); impl DsaRef { /// Writes an DSA private key as unencrypted PEM formatted data /// Encodes a DSA private key as unencrypted PEM formatted data. pub fn private_key_to_pem(&self) -> Result<Vec<u8>, ErrorStack> { assert!(self.has_private_key()); let mem_bio = try!(MemBio::new()); Loading @@ -27,7 +28,7 @@ impl DsaRef { Ok(mem_bio.get_buf().to_owned()) } /// Writes an DSA public key as PEM formatted data /// Encodes a DSA public key as PEM formatted data. pub fn public_key_to_pem(&self) -> Result<Vec<u8>, ErrorStack> { let mem_bio = try!(MemBio::new()); unsafe { Loading @@ -36,6 +37,26 @@ 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) } } pub fn size(&self) -> Option<u32> { if self.q().is_some() { unsafe { Some(ffi::DSA_size(self.as_ptr()) as u32) } Loading Loading @@ -139,7 +160,7 @@ impl Dsa { } } /// Reads an DSA public key from PEM formatted data. /// Reads a DSA public key from PEM formatted data. pub fn public_key_from_pem(buf: &[u8]) -> Result<Dsa, ErrorStack> { ffi::init(); Loading @@ -152,6 +173,26 @@ 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 { Loading
openssl/src/pkey.rs +14 −5 Original line number Diff line number Diff line Loading @@ -48,7 +48,7 @@ impl PKeyRef { } } /// Stores private key as a PEM /// Encodes the private key in the PEM format. // FIXME: also add password and encryption pub fn private_key_to_pem(&self) -> Result<Vec<u8>, ErrorStack> { let mem_bio = try!(MemBio::new()); Loading @@ -65,7 +65,7 @@ impl PKeyRef { Ok(mem_bio.get_buf().to_owned()) } /// Encode public key in PEM format /// Encodes the public key in the PEM format. pub fn public_key_to_pem(&self) -> Result<Vec<u8>, ErrorStack> { let mem_bio = try!(MemBio::new()); unsafe { Loading @@ -74,7 +74,7 @@ impl PKeyRef { Ok(mem_bio.get_buf().to_owned()) } /// Encode public key in DER format /// Encodes the public key in the DER format. pub fn public_key_to_der(&self) -> Result<Vec<u8>, ErrorStack> { let mem_bio = try!(MemBio::new()); unsafe { Loading @@ -83,6 +83,15 @@ impl PKeyRef { Ok(mem_bio.get_buf().to_owned()) } /// Encodes the private key in the DER format pub fn private_key_to_der(&self) -> Result<Vec<u8>, ErrorStack> { let mem_bio = try!(MemBio::new()); unsafe { try!(cvt(ffi::i2d_PrivateKey_bio(mem_bio.as_ptr(), self.as_ptr()))); } Ok(mem_bio.get_buf().to_owned()) } pub fn public_eq(&self, other: &PKeyRef) -> bool { unsafe { ffi::EVP_PKEY_cmp(self.as_ptr(), other.as_ptr()) == 1 } } Loading Loading @@ -148,7 +157,7 @@ impl PKey { } } /// Reads private key from PEM, takes ownership of handle /// Reads a private key from PEM. pub fn private_key_from_pem(buf: &[u8]) -> Result<PKey, ErrorStack> { ffi::init(); let mem_bio = try!(MemBioSlice::new(buf)); Loading Loading @@ -181,7 +190,7 @@ impl PKey { } } /// Reads public key from PEM, takes ownership of handle /// 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)); Loading
openssl/src/rsa.rs +46 −1 Original line number Diff line number Diff line use ffi; use std::cmp; use std::fmt; use std::ptr; use std::mem; use libc::{c_int, c_void, c_char}; use libc::{c_int, c_void, c_char, c_long}; use {cvt, cvt_p, cvt_n}; use bn::{BigNum, BigNumRef}; Loading Loading @@ -49,6 +50,26 @@ impl RsaRef { Ok(mem_bio.get_buf().to_owned()) } /// Encodes an RSA 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_RSAPrivateKey(self.as_ptr(), ptr::null_mut()))); let mut buf = vec![0; len as usize]; try!(cvt(ffi::i2d_RSAPrivateKey(self.as_ptr(), &mut buf.as_mut_ptr()))); Ok(buf) } } /// Encodes an RSA public key as DER formatted data. pub fn public_key_to_der(&self) -> Result<Vec<u8>, ErrorStack> { unsafe { let len = try!(cvt(ffi::i2d_RSA_PUBKEY(self.as_ptr(), ptr::null_mut()))); let mut buf = vec![0; len as usize]; try!(cvt(ffi::i2d_RSA_PUBKEY(self.as_ptr(), &mut buf.as_mut_ptr()))); Ok(buf) } } pub fn size(&self) -> usize { unsafe { assert!(self.n().is_some()); Loading Loading @@ -250,6 +271,7 @@ impl Rsa { /// /// The public exponent will be 65537. pub fn generate(bits: u32) -> Result<Rsa, ErrorStack> { ffi::init(); unsafe { let rsa = Rsa(try!(cvt_p(ffi::RSA_new()))); let e = try!(BigNum::from_u32(ffi::RSA_F4 as u32)); Loading @@ -260,6 +282,7 @@ impl Rsa { /// Reads an RSA private key from PEM formatted data. pub fn private_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_RSAPrivateKey(mem_bio.as_ptr(), Loading @@ -274,6 +297,7 @@ impl Rsa { pub fn private_key_from_pem_cb<F>(buf: &[u8], pass_cb: F) -> Result<Rsa, ErrorStack> where F: FnOnce(&mut [c_char]) -> usize { ffi::init(); let mut cb = CallbackState::new(pass_cb); let mem_bio = try!(MemBioSlice::new(buf)); Loading @@ -289,6 +313,7 @@ impl 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(), Loading @@ -298,6 +323,26 @@ 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 { Loading