Check _fromstr function for success

This commit is contained in:
Mathijs van de Nes 2015-09-10 12:47:26 +02:00
parent 60c0b88eba
commit 02b109bf04
1 changed files with 13 additions and 6 deletions

View File

@ -110,11 +110,16 @@ impl PKey {
} }
} }
fn _fromstr(&mut self, s: &[u8], f: unsafe extern "C" fn(*const *mut ffi::RSA, *const *const u8, c_uint) -> *mut ffi::RSA) { fn _fromstr(&mut self, s: &[u8], f: unsafe extern "C" fn(*const *mut ffi::RSA, *const *const u8, c_uint) -> *mut ffi::RSA) -> bool {
unsafe { unsafe {
let rsa = ptr::null_mut(); let rsa = ptr::null_mut();
f(&rsa, &s.as_ptr(), s.len() as c_uint); f(&rsa, &s.as_ptr(), s.len() as c_uint);
ffi::EVP_PKEY_set1_RSA(self.evp, rsa); if !rsa.is_null() {
ffi::EVP_PKEY_set1_RSA(self.evp, rsa) == 1
}
else {
false
}
} }
} }
@ -148,9 +153,10 @@ impl PKey {
* Loads a serialized form of the public key, as produced by save_pub(). * Loads a serialized form of the public key, as produced by save_pub().
*/ */
pub fn load_pub(&mut self, s: &[u8]) { pub fn load_pub(&mut self, s: &[u8]) {
self._fromstr(s, ffi::d2i_RSA_PUBKEY); if self._fromstr(s, ffi::d2i_RSA_PUBKEY) {
self.parts = Parts::Public; self.parts = Parts::Public;
} }
}
/** /**
* Returns a serialized form of the public and private keys, suitable for * Returns a serialized form of the public and private keys, suitable for
@ -164,9 +170,10 @@ impl PKey {
* save_priv(). * save_priv().
*/ */
pub fn load_priv(&mut self, s: &[u8]) { pub fn load_priv(&mut self, s: &[u8]) {
self._fromstr(s, ffi::d2i_RSAPrivateKey); if self._fromstr(s, ffi::d2i_RSAPrivateKey) {
self.parts = Parts::Both; self.parts = Parts::Both;
} }
}
/// Stores private key as a PEM /// Stores private key as a PEM
// FIXME: also add password and encryption // FIXME: also add password and encryption