aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorSteven Fackler <[email protected]>2016-11-01 11:06:47 -0700
committerGitHub <[email protected]>2016-11-01 11:06:47 -0700
commit7bd4fbb6f1874276de9ebd8eefd8eee6e192e297 (patch)
treebcfa35bd89950c71bde1a013ca475dc24c87e374
parentAvoid extra allocations in Asn1Time Display impl (diff)
parentAdd method to encode a public key as a DER blob (diff)
downloadrust-openssl-7bd4fbb6f1874276de9ebd8eefd8eee6e192e297.tar.xz
rust-openssl-7bd4fbb6f1874276de9ebd8eefd8eee6e192e297.zip
Merge pull request #508 from simias/pkey
Add method to encode a public key as a DER blob
-rw-r--r--openssl-sys/src/lib.rs2
-rw-r--r--openssl/src/pkey.rs11
2 files changed, 12 insertions, 1 deletions
diff --git a/openssl-sys/src/lib.rs b/openssl-sys/src/lib.rs
index f556b19f..1a9a2ac1 100644
--- a/openssl-sys/src/lib.rs
+++ b/openssl-sys/src/lib.rs
@@ -1693,6 +1693,8 @@ extern {
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_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;
pub fn i2d_RSAPrivateKey(k: *const RSA, buf: *mut *mut u8) -> c_int;
diff --git a/openssl/src/pkey.rs b/openssl/src/pkey.rs
index 6236b642..4885ad3c 100644
--- a/openssl/src/pkey.rs
+++ b/openssl/src/pkey.rs
@@ -40,7 +40,7 @@ impl Ref<PKey> {
Ok(mem_bio.get_buf().to_owned())
}
- /// Stores public key as a PEM
+ /// Encode public key in PEM format
pub fn public_key_to_pem(&self) -> Result<Vec<u8>, ErrorStack> {
let mem_bio = try!(MemBio::new());
unsafe {
@@ -49,6 +49,15 @@ impl Ref<PKey> {
Ok(mem_bio.get_buf().to_owned())
}
+ /// Encode public key in DER format
+ pub fn public_key_to_der(&self) -> Result<Vec<u8>, ErrorStack> {
+ let mem_bio = try!(MemBio::new());
+ unsafe {
+ try!(cvt(ffi::i2d_PUBKEY_bio(mem_bio.as_ptr(), self.as_ptr())));
+ }
+ Ok(mem_bio.get_buf().to_owned())
+ }
+
pub fn public_eq(&self, other: &Ref<PKey>) -> bool {
unsafe { ffi::EVP_PKEY_cmp(self.as_ptr(), other.as_ptr()) == 1 }
}