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/pkey.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/pkey.rs')
| -rw-r--r-- | openssl/src/pkey.rs | 19 |
1 files changed, 14 insertions, 5 deletions
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)); |