diff options
| author | Manuel Schölling <[email protected]> | 2015-03-19 10:15:02 +0100 |
|---|---|---|
| committer | Manuel Schölling <[email protected]> | 2015-04-03 14:34:24 +0200 |
| commit | 632d8398cfd9a3ab146d3208200cbe69018fc4b1 (patch) | |
| tree | 6fd120ec844a1dcb2ee3c173c89a96542c9213b2 /openssl/src/crypto | |
| parent | Change SslVerifyMode to bitflags and add SSL_VERIFY_FAIL_IF_NO_PEER_CERT (diff) | |
| download | rust-openssl-632d8398cfd9a3ab146d3208200cbe69018fc4b1.tar.xz rust-openssl-632d8398cfd9a3ab146d3208200cbe69018fc4b1.zip | |
Add ability to load private keys from files and use raw keys and certificates for SslContext
Diffstat (limited to 'openssl/src/crypto')
| -rw-r--r-- | openssl/src/crypto/pkey.rs | 30 |
1 files changed, 30 insertions, 0 deletions
diff --git a/openssl/src/crypto/pkey.rs b/openssl/src/crypto/pkey.rs index 9bcc79b6..a4c6fb12 100644 --- a/openssl/src/crypto/pkey.rs +++ b/openssl/src/crypto/pkey.rs @@ -1,4 +1,5 @@ use libc::{c_int, c_uint, c_ulong}; +use std::io; use std::io::prelude::*; use std::iter::repeat; use std::mem; @@ -69,6 +70,22 @@ impl PKey { } } + /// Reads private key from PEM, takes ownership of handle + pub fn private_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_PrivateKey(mem_bio.get_handle(), + ptr::null_mut(), + None, ptr::null_mut())); + Ok(PKey { + evp: evp, + parts: Parts::Both, + }) + } + } + 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); @@ -335,6 +352,9 @@ impl Drop for PKey { #[cfg(test)] mod tests { + use std::io; + use std::path::Path; + use std::fs::File; use crypto::hash::Type::{MD5, SHA1}; #[test] @@ -374,6 +394,16 @@ mod tests { } #[test] + fn test_private_key_from_pem() { + let key_path = Path::new("test/key.pem"); + let mut file = File::open(&key_path) + .ok() + .expect("Failed to open `test/key.pem`"); + + super::PKey::private_key_from_pem(&mut file).unwrap(); + } + + #[test] fn test_encrypt() { let mut k0 = super::PKey::new(); let mut k1 = super::PKey::new(); |