diff options
| author | Steven Fackler <[email protected]> | 2016-11-11 19:49:00 +0000 |
|---|---|---|
| committer | Steven Fackler <[email protected]> | 2016-11-11 20:10:10 +0000 |
| commit | 6b7279eb5224a422860d0adb38becd5bca19763f (patch) | |
| tree | b74aab6be99b603ff763263439733354e92f1b4b /openssl/src/dsa.rs | |
| parent | Add EcKey <-> PKey conversions (diff) | |
| download | rust-openssl-6b7279eb5224a422860d0adb38becd5bca19763f.tar.xz rust-openssl-6b7279eb5224a422860d0adb38becd5bca19763f.zip | |
Consistently support both PEM and DER encodings
Closes #500
Diffstat (limited to 'openssl/src/dsa.rs')
| -rw-r--r-- | openssl/src/dsa.rs | 49 |
1 files changed, 45 insertions, 4 deletions
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 { |