adding functionality to directly get and set RSA key material
This commit is contained in:
parent
c33e2fede0
commit
ef95223d26
|
|
@ -588,6 +588,7 @@ extern "C" {
|
||||||
|
|
||||||
pub fn RAND_bytes(buf: *mut u8, num: c_int) -> c_int;
|
pub fn RAND_bytes(buf: *mut u8, num: c_int) -> c_int;
|
||||||
|
|
||||||
|
pub fn RSA_new() -> *mut RSA;
|
||||||
pub fn RSA_free(rsa: *mut RSA);
|
pub fn RSA_free(rsa: *mut RSA);
|
||||||
pub fn RSA_generate_key(modsz: c_int, e: c_ulong, cb: *const c_void, cbarg: *const c_void) -> *mut RSA;
|
pub fn RSA_generate_key(modsz: c_int, e: c_ulong, cb: *const c_void, cbarg: *const c_void) -> *mut RSA;
|
||||||
pub fn RSA_generate_key_ex(rsa: *mut RSA, bits: c_int, e: *mut BIGNUM, cb: *const c_void) -> c_int;
|
pub fn RSA_generate_key_ex(rsa: *mut RSA, bits: c_int, e: *mut BIGNUM, cb: *const c_void) -> c_int;
|
||||||
|
|
|
||||||
|
|
@ -473,6 +473,11 @@ impl BigNum {
|
||||||
n
|
n
|
||||||
}
|
}
|
||||||
|
|
||||||
|
pub unsafe fn into_raw(self) -> *mut ffi::BIGNUM {
|
||||||
|
let mut me = self;
|
||||||
|
ptr::replace(&mut me.0, ptr::null_mut())
|
||||||
|
}
|
||||||
|
|
||||||
pub fn to_vec(&self) -> Vec<u8> {
|
pub fn to_vec(&self) -> Vec<u8> {
|
||||||
let size = self.num_bytes() as usize;
|
let size = self.num_bytes() as usize;
|
||||||
let mut v = Vec::with_capacity(size);
|
let mut v = Vec::with_capacity(size);
|
||||||
|
|
|
||||||
|
|
@ -205,6 +205,30 @@ 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;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
/// get a reference to the interal RSA key for direct access to the key components
|
||||||
|
pub fn get_rsa(&self) -> RSA {
|
||||||
|
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))
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Returns a DER serialized form of the public key, suitable for load_pub().
|
* Returns a DER serialized form of the public key, suitable for load_pub().
|
||||||
*/
|
*/
|
||||||
|
|
|
||||||
|
|
@ -7,7 +7,7 @@ use std::io::{self, Read};
|
||||||
use bn::BigNum;
|
use bn::BigNum;
|
||||||
use bio::MemBio;
|
use bio::MemBio;
|
||||||
|
|
||||||
pub struct RSA(*mut ffi::RSA);
|
pub struct RSA(pub *mut ffi::RSA);
|
||||||
|
|
||||||
impl Drop for RSA {
|
impl Drop for RSA {
|
||||||
fn drop(&mut self) {
|
fn drop(&mut self) {
|
||||||
|
|
@ -18,6 +18,15 @@ impl Drop for RSA {
|
||||||
}
|
}
|
||||||
|
|
||||||
impl RSA {
|
impl RSA {
|
||||||
|
/// only useful for associating the key material directly with the key, it's safer to use
|
||||||
|
/// the supplied load and save methods for DER formatted keys.
|
||||||
|
pub fn new() -> Result<RSA, SslError> {
|
||||||
|
unsafe {
|
||||||
|
let rsa = try_ssl_null!(ffi::RSA_new());
|
||||||
|
Ok(RSA(rsa))
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
/// Reads an RSA private key from PEM formatted data.
|
/// Reads an RSA private key from PEM formatted data.
|
||||||
pub fn private_key_from_pem<R>(reader: &mut R) -> Result<RSA, SslError>
|
pub fn private_key_from_pem<R>(reader: &mut R) -> Result<RSA, SslError>
|
||||||
where R: Read
|
where R: Read
|
||||||
|
|
@ -61,6 +70,19 @@ impl RSA {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/// set the key modulus
|
||||||
|
pub fn set_n(&mut self, n: BigNum) {
|
||||||
|
unsafe {
|
||||||
|
(*self.0).n = n.into_raw();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
pub fn has_n(&self) -> bool {
|
||||||
|
unsafe {
|
||||||
|
!(*self.0).n.is_null()
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
pub fn d(&self) -> Result<BigNum, SslError> {
|
pub fn d(&self) -> Result<BigNum, SslError> {
|
||||||
unsafe {
|
unsafe {
|
||||||
BigNum::new_from_ffi((*self.0).d)
|
BigNum::new_from_ffi((*self.0).d)
|
||||||
|
|
@ -73,6 +95,19 @@ impl RSA {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/// set the exponent
|
||||||
|
pub fn set_e(&mut self, e: BigNum) {
|
||||||
|
unsafe {
|
||||||
|
(*self.0).e = e.into_raw();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
pub fn has_e(&self) -> bool {
|
||||||
|
unsafe {
|
||||||
|
!(*self.0).e.is_null()
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
pub fn p(&self) -> Result<BigNum, SslError> {
|
pub fn p(&self) -> Result<BigNum, SslError> {
|
||||||
unsafe {
|
unsafe {
|
||||||
BigNum::new_from_ffi((*self.0).p)
|
BigNum::new_from_ffi((*self.0).p)
|
||||||
|
|
|
||||||
Loading…
Reference in New Issue