Commit 18e7e245 authored by Steven Fackler's avatar Steven Fackler
Browse files

Merge pull request #330 from esclear/master

Add a interface to RSA structs
parents d25858cb 3ee2bf93
Loading
Loading
Loading
Loading
+41 −2
Original line number Diff line number Diff line
@@ -22,9 +22,7 @@ pub type ENGINE = c_void;
pub type EVP_CIPHER = c_void;
pub type EVP_CIPHER_CTX = c_void;
pub type EVP_MD = c_void;
pub type EVP_PKEY = c_void;
pub type EVP_PKEY_CTX = c_void;
pub type RSA = c_void;
pub type SSL = c_void;
pub type SSL_CTX = c_void;
pub type SSL_METHOD = c_void;
@@ -64,6 +62,47 @@ pub struct BIO_METHOD {
// so we can create static BIO_METHODs
unsafe impl Sync for BIO_METHOD {}

#[repr(C)]
pub struct RSA {
    pub pad: c_int,
    pub version: c_long,
    pub meth: *const c_void,

    pub engine: *mut c_void,
    pub n: *mut BIGNUM,
    pub e: *mut BIGNUM,
    pub d: *mut BIGNUM,
    pub p: *mut BIGNUM,
    pub q: *mut BIGNUM,
    pub dmp1: *mut BIGNUM,
    pub dmq1: *mut BIGNUM,
    pub iqmp: *mut BIGNUM,

    pub ex_data: *mut c_void,
    pub references: c_int,
    pub flags: c_int,

    pub _method_mod_n: *mut c_void,
    pub _method_mod_p: *mut c_void,
    pub _method_mod_q: *mut c_void,

    pub bignum_data: *mut c_char,
    pub blinding: *mut c_void,
    pub mt_blinding: *mut c_void,
}

#[repr(C)]
pub struct EVP_PKEY {
    pub type_: c_int,
    pub save_type: c_int,
    pub references: c_int,
    pub ameth: *const c_void,
    pub engine: *mut ENGINE,
    pub pkey: *mut c_void,
    pub save_parameters: c_int,
    pub attributes: *mut c_void,
}

#[repr(C)]
pub struct BIO {
    pub method: *mut BIO_METHOD,
+12 −0
Original line number Diff line number Diff line
@@ -102,6 +102,18 @@ impl BigNum {
        })
    }

    pub unsafe fn new_from_ffi(orig: *mut ffi::BIGNUM) -> Result<BigNum, SslError> {
        if orig.is_null() {
            panic!("Null Pointer was supplied to BigNum::new_from_ffi");
        }
        let r = ffi::BN_dup(orig);
        if r.is_null() {
            Err(SslError::get())
        } else {
            Ok(BigNum(r))
        }
    }

    pub fn new_from_slice(n: &[u8]) -> Result<BigNum, SslError> {
        BigNum::new().and_then(|v| unsafe {
            try_ssl_null!(ffi::BN_bin2bn(n.as_ptr(), n.len() as c_int, v.raw()));
+1 −0
Original line number Diff line number Diff line
@@ -21,5 +21,6 @@ pub mod pkey;
pub mod rand;
pub mod symm;
pub mod memcmp;
pub mod rsa;

mod symm_internal;
+2 −2
Original line number Diff line number Diff line
@@ -100,7 +100,7 @@ impl PKey {
                                                                 None,
                                                                 ptr::null_mut()));
            Ok(PKey {
                evp: evp,
                evp: evp as *mut ffi::EVP_PKEY,
                parts: Parts::Both,
            })
        }
@@ -119,7 +119,7 @@ impl PKey {
                                                             None,
                                                             ptr::null_mut()));
            Ok(PKey {
                evp: evp,
                evp: evp as *mut ffi::EVP_PKEY,
                parts: Parts::Public,
            })
        }
+46 −0
Original line number Diff line number Diff line
use ffi;
use bn::BigNum;
use std::fmt;

pub struct RSA {
    rsa_obj : ffi::RSA
}

impl RSA {
    // The following getters are unsafe, since BigNum::new_from_ffi fails upon null pointers
    pub fn n(&self) -> BigNum {
        unsafe {
            BigNum::new_from_ffi(self.rsa_obj.n).unwrap()
        }
    }

    pub fn d(&self) -> BigNum {
        unsafe {
            BigNum::new_from_ffi(self.rsa_obj.d).unwrap()
        }
    }

    pub fn e(&self) -> BigNum {
        unsafe {
            BigNum::new_from_ffi(self.rsa_obj.e).unwrap()
        }
    }

    pub fn p(&self) -> BigNum {
        unsafe {
            BigNum::new_from_ffi(self.rsa_obj.p).unwrap()
        }
    }

    pub fn q(&self) -> BigNum {
        unsafe {
            BigNum::new_from_ffi(self.rsa_obj.q).unwrap()
        }
    }
}

impl fmt::Debug for RSA {
    fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
        write!(f, "RSA")
    }
}