diff options
Diffstat (limited to 'openssl')
-rw-r--r-- | openssl/src/dh.rs | 48 | ||||
-rw-r--r-- | openssl/src/dsa.rs | 49 | ||||
-rw-r--r-- | openssl/src/pkey.rs | 19 | ||||
-rw-r--r-- | openssl/src/rsa.rs | 47 |
4 files changed, 144 insertions, 19 deletions
diff --git a/openssl/src/dh.rs b/openssl/src/dh.rs index 5a07e50f..ad1ebf9f 100644 --- a/openssl/src/dh.rs +++ b/openssl/src/dh.rs @@ -1,11 +1,12 @@ -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; @@ -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)); @@ -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, @@ -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> { @@ -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(); + } } diff --git a/openssl/src/dsa.rs b/openssl/src/dsa.rs index 9dd5669c..81e551ab 100644 --- a/openssl/src/dsa.rs +++ b/openssl/src/dsa.rs @@ -1,8 +1,9 @@ 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; @@ -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()); @@ -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 { @@ -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) } @@ -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(); @@ -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 { diff --git a/openssl/src/pkey.rs b/openssl/src/pkey.rs index f424e337..b1c196bd 100644 --- a/openssl/src/pkey.rs +++ b/openssl/src/pkey.rs @@ -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()); @@ -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 { @@ -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 { @@ -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 } } @@ -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)); @@ -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)); diff --git a/openssl/src/rsa.rs b/openssl/src/rsa.rs index bd1d16d3..70d41997 100644 --- a/openssl/src/rsa.rs +++ b/openssl/src/rsa.rs @@ -1,8 +1,9 @@ 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}; @@ -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()); @@ -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)); @@ -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(), @@ -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)); @@ -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(), @@ -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 { |