From 6ebe581308af861b440557be5baba2edb354f7b8 Mon Sep 17 00:00:00 2001 From: Benjamin Fry Date: Tue, 23 Feb 2016 20:49:21 -0800 Subject: [PATCH] review fixes, keep raw RSA initiallization private --- openssl/src/crypto/pkey.rs | 11 ++++------- openssl/src/crypto/rsa.rs | 6 +++++- 2 files changed, 9 insertions(+), 8 deletions(-) diff --git a/openssl/src/crypto/pkey.rs b/openssl/src/crypto/pkey.rs index dc613bc7..df4ac709 100644 --- a/openssl/src/crypto/pkey.rs +++ b/openssl/src/crypto/pkey.rs @@ -208,13 +208,10 @@ impl PKey { /// pass ownership of the RSA key to this pub fn set_rsa(&mut self, rsa: RSA) { unsafe { - // TODO: should we do something like panic if null? this will fail silently right now let rsa_ptr = rsa.as_ptr(); - if !rsa_ptr.is_null() { - if ffi::EVP_PKEY_set1_RSA(self.evp, rsa_ptr) == 1 { - if rsa.has_e() && rsa.has_n() { - self.parts = Parts::Public; - } + if ffi::EVP_PKEY_set1_RSA(self.evp, rsa_ptr) == 1 { + if rsa.has_e() && rsa.has_n() { + self.parts = Parts::Public; } } } @@ -225,7 +222,7 @@ impl PKey { unsafe { let evp_pkey: *mut ffi::EVP_PKEY = self.evp; // this is safe as the ffi increments a reference counter to the internal key - RSA(ffi::EVP_PKEY_get1_RSA(evp_pkey)) + RSA::with_raw(ffi::EVP_PKEY_get1_RSA(evp_pkey)) } } diff --git a/openssl/src/crypto/rsa.rs b/openssl/src/crypto/rsa.rs index 034f8828..80eec7da 100644 --- a/openssl/src/crypto/rsa.rs +++ b/openssl/src/crypto/rsa.rs @@ -7,7 +7,7 @@ use std::io::{self, Read}; use bn::BigNum; use bio::MemBio; -pub struct RSA(pub *mut ffi::RSA); +pub struct RSA(*mut ffi::RSA); impl Drop for RSA { fn drop(&mut self) { @@ -27,6 +27,10 @@ impl RSA { } } + pub fn with_raw(rsa: *mut ffi::RSA) -> RSA { + RSA(rsa) + } + /// Reads an RSA private key from PEM formatted data. pub fn private_key_from_pem(reader: &mut R) -> Result where R: Read