Finish crypto error cleanup
This commit is contained in:
parent
19440c2981
commit
89a366d9f7
|
|
@ -59,6 +59,7 @@ use std::io::{self, Write};
|
||||||
use std::marker::PhantomData;
|
use std::marker::PhantomData;
|
||||||
use std::ptr;
|
use std::ptr;
|
||||||
|
|
||||||
|
use {cvt, cvt_p};
|
||||||
use crypto::hash::MessageDigest;
|
use crypto::hash::MessageDigest;
|
||||||
use crypto::pkey::PKey;
|
use crypto::pkey::PKey;
|
||||||
use error::ErrorStack;
|
use error::ErrorStack;
|
||||||
|
|
@ -83,7 +84,7 @@ impl<'a> Signer<'a> {
|
||||||
unsafe {
|
unsafe {
|
||||||
ffi::init();
|
ffi::init();
|
||||||
|
|
||||||
let ctx = try_ssl_null!(EVP_MD_CTX_new());
|
let ctx = try!(cvt_p(EVP_MD_CTX_new()));
|
||||||
let r = ffi::EVP_DigestSignInit(ctx,
|
let r = ffi::EVP_DigestSignInit(ctx,
|
||||||
ptr::null_mut(),
|
ptr::null_mut(),
|
||||||
type_.as_ptr(),
|
type_.as_ptr(),
|
||||||
|
|
@ -93,25 +94,22 @@ impl<'a> Signer<'a> {
|
||||||
EVP_MD_CTX_free(ctx);
|
EVP_MD_CTX_free(ctx);
|
||||||
return Err(ErrorStack::get());
|
return Err(ErrorStack::get());
|
||||||
}
|
}
|
||||||
|
|
||||||
Ok(Signer(ctx, PhantomData))
|
Ok(Signer(ctx, PhantomData))
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
pub fn update(&mut self, buf: &[u8]) -> Result<(), ErrorStack> {
|
pub fn update(&mut self, buf: &[u8]) -> Result<(), ErrorStack> {
|
||||||
unsafe {
|
unsafe {
|
||||||
try_ssl_if!(ffi::EVP_DigestUpdate(self.0, buf.as_ptr() as *const _, buf.len()) != 1);
|
cvt(ffi::EVP_DigestUpdate(self.0, buf.as_ptr() as *const _, buf.len())).map(|_| ())
|
||||||
Ok(())
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
pub fn finish(&self) -> Result<Vec<u8>, ErrorStack> {
|
pub fn finish(&self) -> Result<Vec<u8>, ErrorStack> {
|
||||||
unsafe {
|
unsafe {
|
||||||
let mut len = 0;
|
let mut len = 0;
|
||||||
try_ssl_if!(ffi::EVP_DigestSignFinal(self.0, ptr::null_mut(), &mut len) != 1);
|
try!(cvt(ffi::EVP_DigestSignFinal(self.0, ptr::null_mut(), &mut len)));
|
||||||
let mut buf = vec![0; len];
|
let mut buf = vec![0; len];
|
||||||
try_ssl_if!(ffi::EVP_DigestSignFinal(self.0, buf.as_mut_ptr() as *mut _, &mut len)
|
try!(cvt(ffi::EVP_DigestSignFinal(self.0, buf.as_mut_ptr() as *mut _, &mut len)));
|
||||||
!= 1);
|
|
||||||
// The advertised length is not always equal to the real length for things like DSA
|
// The advertised length is not always equal to the real length for things like DSA
|
||||||
buf.truncate(len);
|
buf.truncate(len);
|
||||||
Ok(buf)
|
Ok(buf)
|
||||||
|
|
@ -145,7 +143,7 @@ impl<'a> Verifier<'a> {
|
||||||
unsafe {
|
unsafe {
|
||||||
ffi::init();
|
ffi::init();
|
||||||
|
|
||||||
let ctx = try_ssl_null!(EVP_MD_CTX_new());
|
let ctx = try!(cvt_p(EVP_MD_CTX_new()));
|
||||||
let r = ffi::EVP_DigestVerifyInit(ctx,
|
let r = ffi::EVP_DigestVerifyInit(ctx,
|
||||||
ptr::null_mut(),
|
ptr::null_mut(),
|
||||||
type_.as_ptr(),
|
type_.as_ptr(),
|
||||||
|
|
@ -162,8 +160,7 @@ impl<'a> Verifier<'a> {
|
||||||
|
|
||||||
pub fn update(&mut self, buf: &[u8]) -> Result<(), ErrorStack> {
|
pub fn update(&mut self, buf: &[u8]) -> Result<(), ErrorStack> {
|
||||||
unsafe {
|
unsafe {
|
||||||
try_ssl_if!(ffi::EVP_DigestUpdate(self.0, buf.as_ptr() as *const _, buf.len()) != 1);
|
cvt(ffi::EVP_DigestUpdate(self.0, buf.as_ptr() as *const _, buf.len())).map(|_| ())
|
||||||
Ok(())
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
||||||
|
|
@ -3,6 +3,7 @@ use std::ptr;
|
||||||
use libc::c_int;
|
use libc::c_int;
|
||||||
use ffi;
|
use ffi;
|
||||||
|
|
||||||
|
use {cvt, cvt_p};
|
||||||
use error::ErrorStack;
|
use error::ErrorStack;
|
||||||
|
|
||||||
#[derive(Copy, Clone)]
|
#[derive(Copy, Clone)]
|
||||||
|
|
@ -170,7 +171,7 @@ impl Crypter {
|
||||||
ffi::init();
|
ffi::init();
|
||||||
|
|
||||||
unsafe {
|
unsafe {
|
||||||
let ctx = try_ssl_null!(ffi::EVP_CIPHER_CTX_new());
|
let ctx = try!(cvt_p(ffi::EVP_CIPHER_CTX_new()));
|
||||||
let crypter = Crypter {
|
let crypter = Crypter {
|
||||||
ctx: ctx,
|
ctx: ctx,
|
||||||
block_size: t.block_size(),
|
block_size: t.block_size(),
|
||||||
|
|
@ -181,15 +182,15 @@ impl Crypter {
|
||||||
Mode::Decrypt => 0,
|
Mode::Decrypt => 0,
|
||||||
};
|
};
|
||||||
|
|
||||||
try_ssl!(ffi::EVP_CipherInit_ex(crypter.ctx,
|
try!(cvt(ffi::EVP_CipherInit_ex(crypter.ctx,
|
||||||
t.as_ptr(),
|
t.as_ptr(),
|
||||||
ptr::null_mut(),
|
ptr::null_mut(),
|
||||||
ptr::null_mut(),
|
ptr::null_mut(),
|
||||||
ptr::null_mut(),
|
ptr::null_mut(),
|
||||||
mode));
|
mode)));
|
||||||
|
|
||||||
assert!(key.len() <= c_int::max_value() as usize);
|
assert!(key.len() <= c_int::max_value() as usize);
|
||||||
try_ssl!(ffi::EVP_CIPHER_CTX_set_key_length(crypter.ctx, key.len() as c_int));
|
try!(cvt(ffi::EVP_CIPHER_CTX_set_key_length(crypter.ctx, key.len() as c_int)));
|
||||||
|
|
||||||
let key = key.as_ptr() as *mut _;
|
let key = key.as_ptr() as *mut _;
|
||||||
let iv = match (iv, t.iv_len()) {
|
let iv = match (iv, t.iv_len()) {
|
||||||
|
|
@ -200,12 +201,12 @@ impl Crypter {
|
||||||
(Some(_), None) | (None, None) => ptr::null_mut(),
|
(Some(_), None) | (None, None) => ptr::null_mut(),
|
||||||
(None, Some(_)) => panic!("an IV is required for this cipher"),
|
(None, Some(_)) => panic!("an IV is required for this cipher"),
|
||||||
};
|
};
|
||||||
try_ssl!(ffi::EVP_CipherInit_ex(crypter.ctx,
|
try!(cvt(ffi::EVP_CipherInit_ex(crypter.ctx,
|
||||||
ptr::null(),
|
ptr::null(),
|
||||||
ptr::null_mut(),
|
ptr::null_mut(),
|
||||||
key,
|
key,
|
||||||
iv,
|
iv,
|
||||||
mode));
|
mode)));
|
||||||
|
|
||||||
Ok(crypter)
|
Ok(crypter)
|
||||||
}
|
}
|
||||||
|
|
@ -237,11 +238,11 @@ impl Crypter {
|
||||||
let mut outl = output.len() as c_int;
|
let mut outl = output.len() as c_int;
|
||||||
let inl = input.len() as c_int;
|
let inl = input.len() as c_int;
|
||||||
|
|
||||||
try_ssl!(ffi::EVP_CipherUpdate(self.ctx,
|
try!(cvt(ffi::EVP_CipherUpdate(self.ctx,
|
||||||
output.as_mut_ptr(),
|
output.as_mut_ptr(),
|
||||||
&mut outl,
|
&mut outl,
|
||||||
input.as_ptr(),
|
input.as_ptr(),
|
||||||
inl));
|
inl)));
|
||||||
|
|
||||||
Ok(outl as usize)
|
Ok(outl as usize)
|
||||||
}
|
}
|
||||||
|
|
@ -262,7 +263,7 @@ impl Crypter {
|
||||||
assert!(output.len() >= self.block_size);
|
assert!(output.len() >= self.block_size);
|
||||||
let mut outl = cmp::min(output.len(), c_int::max_value() as usize) as c_int;
|
let mut outl = cmp::min(output.len(), c_int::max_value() as usize) as c_int;
|
||||||
|
|
||||||
try_ssl!(ffi::EVP_CipherFinal(self.ctx, output.as_mut_ptr(), &mut outl));
|
try!(cvt(ffi::EVP_CipherFinal(self.ctx, output.as_mut_ptr(), &mut outl)));
|
||||||
|
|
||||||
Ok(outl as usize)
|
Ok(outl as usize)
|
||||||
}
|
}
|
||||||
|
|
|
||||||
Loading…
Reference in New Issue