take enum instead of ints from openssl header file

This commit is contained in:
Chris Dawes 2016-05-03 22:17:07 +01:00
parent 6bbb21779b
commit 6f410a25b2
1 changed files with 22 additions and 6 deletions

View File

@ -7,6 +7,24 @@ use std::io::{self, Read};
use bn::BigNum;
use bio::MemBio;
#[derive(Copy, Clone, Debug)]
pub enum PKCSHashType {
SHA256,
SHA384,
SHA512
}
/// https://github.com/openssl/openssl/blob/master/include/openssl/obj_mac.h#L2790
impl Into<i32> for PKCSHashType {
fn into(self) -> i32 {
match self {
PKCSHashType::SHA256 => 672,
PKCSHashType::SHA384 => 673,
PKCSHashType::SHA512 => 674
}
}
}
pub struct RSA(*mut ffi::RSA);
impl Drop for RSA {
@ -91,15 +109,13 @@ impl RSA {
}
}
pub fn sign(&self, hash_id: i32, message: &[u8]) -> Result<Vec<u8>, SslError> {
// RSA_sign(t: c_int, m: *const u8, mlen: c_uint, sig: *mut u8, siglen: *mut c_uint, k: *mut RSA) -> c_int
pub fn sign(&self, hash_id: PKCSHashType, message: &[u8]) -> Result<Vec<u8>, SslError> {
let k_len = try!(self.size());
let mut sig = vec![0;k_len as usize];
let mut sig_len = k_len;
unsafe {
let result = ffi::RSA_sign(hash_id, message.as_ptr(), message.len() as u32, sig.as_mut_ptr(), &mut sig_len, self.0);
let result = ffi::RSA_sign(hash_id.into(), message.as_ptr(), message.len() as u32, sig.as_mut_ptr(), &mut sig_len, self.0);
assert!(sig_len == k_len);
if result == 1 {
@ -110,9 +126,9 @@ impl RSA {
}
}
pub fn verify(&self, hash_id: i32, message: &[u8], sig: &[u8]) -> Result<bool, SslError> {
pub fn verify(&self, hash_id: PKCSHashType, message: &[u8], sig: &[u8]) -> Result<bool, SslError> {
unsafe {
let result = ffi::RSA_verify(hash_id, message.as_ptr(), message.len() as u32, sig.as_ptr(), sig.len() as u32, self.0);
let result = ffi::RSA_verify(hash_id.into(), message.as_ptr(), message.len() as u32, sig.as_ptr(), sig.len() as u32, self.0);
Ok(result == 1)
}