diff options
| author | Steven Fackler <[email protected]> | 2015-10-10 21:55:37 -0400 |
|---|---|---|
| committer | Steven Fackler <[email protected]> | 2015-10-10 21:55:37 -0400 |
| commit | 60ee731408facdc8e3dfc000fdee2f1291fad664 (patch) | |
| tree | ddc6820af6e6bfcfdceec9c49be71c6f9608f79d | |
| parent | Merge branch 'release' (diff) | |
| parent | Add public key PEM read function. (diff) | |
| download | rust-openssl-60ee731408facdc8e3dfc000fdee2f1291fad664.tar.xz rust-openssl-60ee731408facdc8e3dfc000fdee2f1291fad664.zip | |
Merge pull request #277 from nixpulvis/read_public_pem
Add public key PEM read function.
| -rw-r--r-- | openssl-sys/src/lib.rs | 2 | ||||
| -rw-r--r-- | openssl/src/crypto/pkey.rs | 26 | ||||
| -rw-r--r-- | openssl/test/key.pem.pub | 9 |
3 files changed, 37 insertions, 0 deletions
diff --git a/openssl-sys/src/lib.rs b/openssl-sys/src/lib.rs index 3bc9e59a..29d87214 100644 --- a/openssl-sys/src/lib.rs +++ b/openssl-sys/src/lib.rs @@ -487,6 +487,8 @@ extern "C" { user_data: *mut c_void) -> *mut X509_REQ; pub fn PEM_read_bio_PrivateKey(bio: *mut BIO, out: *mut *mut EVP_PKEY, callback: Option<PasswordCallback>, user_data: *mut c_void) -> *mut X509; + pub fn PEM_read_bio_PUBKEY(bio: *mut BIO, out: *mut *mut EVP_PKEY, callback: Option<PasswordCallback>, + user_data: *mut c_void) -> *mut X509; pub fn PEM_write_bio_PrivateKey(bio: *mut BIO, pkey: *mut EVP_PKEY, cipher: *const EVP_CIPHER, kstr: *mut c_char, klen: c_int, diff --git a/openssl/src/crypto/pkey.rs b/openssl/src/crypto/pkey.rs index 5a528b1b..695bd8a6 100644 --- a/openssl/src/crypto/pkey.rs +++ b/openssl/src/crypto/pkey.rs @@ -96,6 +96,22 @@ impl PKey { } } + /// Reads public key from PEM, takes ownership of handle + pub fn public_key_from_pem<R>(reader: &mut R) -> Result<PKey, SslError> where R: Read { + let mut mem_bio = try!(MemBio::new()); + try!(io::copy(reader, &mut mem_bio).map_err(StreamError)); + + unsafe { + let evp = try_ssl_null!(ffi::PEM_read_bio_PUBKEY(mem_bio.get_handle(), + ptr::null_mut(), + None, ptr::null_mut())); + Ok(PKey { + evp: evp, + parts: Parts::Public, + }) + } + } + fn _tostr(&self, f: unsafe extern "C" fn(*mut ffi::RSA, *const *mut u8) -> c_int) -> Vec<u8> { unsafe { let rsa = ffi::EVP_PKEY_get1_RSA(self.evp); @@ -467,6 +483,16 @@ mod tests { } #[test] + fn test_public_key_from_pem() { + let key_path = Path::new("test/key.pem.pub"); + let mut file = File::open(&key_path) + .ok() + .expect("Failed to open `test/key.pem.pub`"); + + super::PKey::public_key_from_pem(&mut file).unwrap(); + } + + #[test] fn test_encrypt() { let mut k0 = super::PKey::new(); let mut k1 = super::PKey::new(); diff --git a/openssl/test/key.pem.pub b/openssl/test/key.pem.pub new file mode 100644 index 00000000..2a822569 --- /dev/null +++ b/openssl/test/key.pem.pub @@ -0,0 +1,9 @@ +-----BEGIN PUBLIC KEY----- +MIIBIjANBgkqhkiG9w0BAQEFAAOCAQ8AMIIBCgKCAQEAr1bXMptaIgOL9PVL8a7W +KG/C8+IbxP018eMBQZT0SnPQmXp0Q8Aai/F+AEDE7b5sO5U7WdxU4GRYw0wqkQNF +si78KNfoj2ZMlx6NRfl4UKuzrpGTPgQxuKDYedngPpWcbmW4P3zEL2Y7b18n9NJr +atRUzH1Zh/ReRO525Xadu58aviPw1Mzgse7cKyzb03Gll9noLnYNIIpO8jL+QyrD +8qNmfacmR20U0a6XDTtmsmk7AitGETICbTT0KRf+oAP0yIHoonllPpNLUEPZQjrp +ClS/S/wKdj7gaq9TaMbHULhFMjbCV8cuPu//rUAuWp3riaznZGOVQyn3Dp2CB3ad +yQIDAQAB +-----END PUBLIC KEY----- |