aboutsummaryrefslogtreecommitdiff
path: root/openssl/src/crypto/pkey.rs
diff options
context:
space:
mode:
authorSteven Fackler <[email protected]>2016-01-30 13:12:06 -0800
committerSteven Fackler <[email protected]>2016-01-30 13:12:06 -0800
commit4e58fd10de3daa3eca08e906a4b2e4239ac6d37d (patch)
tree1a320ec34f9d567dc5484365d88787edaec37b33 /openssl/src/crypto/pkey.rs
parentMerge pull request #344 from sfackler/revert-336-x509-pky-clone (diff)
downloadrust-openssl-4e58fd10de3daa3eca08e906a4b2e4239ac6d37d.tar.xz
rust-openssl-4e58fd10de3daa3eca08e906a4b2e4239ac6d37d.zip
Fix PKey RSA constructors
`set1` functions bump the object's refcount so we were previously leaking the RSA object. Split the decode from PEM part out to a method on RSA and use that in the PKey constructors. Also make RSA a pointer and actually free it.
Diffstat (limited to 'openssl/src/crypto/pkey.rs')
-rw-r--r--openssl/src/crypto/pkey.rs29
1 files changed, 7 insertions, 22 deletions
diff --git a/openssl/src/crypto/pkey.rs b/openssl/src/crypto/pkey.rs
index 314ee63d..7cfa1288 100644
--- a/openssl/src/crypto/pkey.rs
+++ b/openssl/src/crypto/pkey.rs
@@ -9,6 +9,7 @@ use crypto::hash;
use crypto::hash::Type as HashType;
use ffi;
use ssl::error::{SslError, StreamError};
+use crypto::rsa::RSA;
#[derive(Copy, Clone)]
pub enum Parts {
@@ -125,18 +126,10 @@ impl PKey {
pub fn private_rsa_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));
-
+ let rsa = try!(RSA::private_key_from_pem(reader));
unsafe {
- let rsa = try_ssl_null!(ffi::PEM_read_bio_RSAPrivateKey(mem_bio.get_handle(),
- ptr::null_mut(),
- None,
- ptr::null_mut()));
- let evp = ffi::EVP_PKEY_new();
- if ffi::EVP_PKEY_set1_RSA(evp, rsa) == 0 {
- return Err(SslError::get());
- }
+ let evp = try_ssl_null!(ffi::EVP_PKEY_new());
+ try_ssl!(ffi::EVP_PKEY_set1_RSA(evp, rsa.as_ptr()));
Ok(PKey {
evp: evp,
@@ -149,18 +142,10 @@ impl PKey {
pub fn public_rsa_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));
-
+ let rsa = try!(RSA::public_key_from_pem(reader));
unsafe {
- let rsa = try_ssl_null!(ffi::PEM_read_bio_RSA_PUBKEY(mem_bio.get_handle(),
- ptr::null_mut(),
- None,
- ptr::null_mut()));
- let evp = ffi::EVP_PKEY_new();
- if ffi::EVP_PKEY_set1_RSA(evp, rsa) == 0 {
- return Err(SslError::get());
- }
+ let evp = try_ssl_null!(ffi::EVP_PKEY_new());
+ try_ssl!(ffi::EVP_PKEY_set1_RSA(evp, rsa.as_ptr()));
Ok(PKey {
evp: evp,