Commit 89a366d9 authored by Steven Fackler's avatar Steven Fackler
Browse files

Finish crypto error cleanup

parent 19440c29
Loading
Loading
Loading
Loading
+7 −10
Original line number Diff line number Diff line
@@ -59,6 +59,7 @@ use std::io::{self, Write};
use std::marker::PhantomData;
use std::ptr;

use {cvt, cvt_p};
use crypto::hash::MessageDigest;
use crypto::pkey::PKey;
use error::ErrorStack;
@@ -83,7 +84,7 @@ impl<'a> Signer<'a> {
        unsafe {
            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,
                                            ptr::null_mut(),
                                            type_.as_ptr(),
@@ -93,25 +94,22 @@ impl<'a> Signer<'a> {
                EVP_MD_CTX_free(ctx);
                return Err(ErrorStack::get());
            }

            Ok(Signer(ctx, PhantomData))
        }
    }

    pub fn update(&mut self, buf: &[u8]) -> Result<(), ErrorStack> {
        unsafe {
            try_ssl_if!(ffi::EVP_DigestUpdate(self.0, buf.as_ptr() as *const _, buf.len()) != 1);
            Ok(())
            cvt(ffi::EVP_DigestUpdate(self.0, buf.as_ptr() as *const _, buf.len())).map(|_| ())
        }
    }

    pub fn finish(&self) -> Result<Vec<u8>, ErrorStack> {
        unsafe {
            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];
            try_ssl_if!(ffi::EVP_DigestSignFinal(self.0, buf.as_mut_ptr() as *mut _, &mut len)
                    != 1);
            try!(cvt(ffi::EVP_DigestSignFinal(self.0, buf.as_mut_ptr() as *mut _, &mut len)));
            // The advertised length is not always equal to the real length for things like DSA
            buf.truncate(len);
            Ok(buf)
@@ -145,7 +143,7 @@ impl<'a> Verifier<'a> {
        unsafe {
            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,
                                              ptr::null_mut(),
                                              type_.as_ptr(),
@@ -162,8 +160,7 @@ impl<'a> Verifier<'a> {

    pub fn update(&mut self, buf: &[u8]) -> Result<(), ErrorStack> {
        unsafe {
            try_ssl_if!(ffi::EVP_DigestUpdate(self.0, buf.as_ptr() as *const _, buf.len()) != 1);
            Ok(())
            cvt(ffi::EVP_DigestUpdate(self.0, buf.as_ptr() as *const _, buf.len())).map(|_| ())
        }
    }

+14 −13
Original line number Diff line number Diff line
@@ -3,6 +3,7 @@ use std::ptr;
use libc::c_int;
use ffi;

use {cvt, cvt_p};
use error::ErrorStack;

#[derive(Copy, Clone)]
@@ -170,7 +171,7 @@ impl Crypter {
        ffi::init();

        unsafe {
            let ctx = try_ssl_null!(ffi::EVP_CIPHER_CTX_new());
            let ctx = try!(cvt_p(ffi::EVP_CIPHER_CTX_new()));
            let crypter = Crypter {
                ctx: ctx,
                block_size: t.block_size(),
@@ -181,15 +182,15 @@ impl Crypter {
                Mode::Decrypt => 0,
            };

            try_ssl!(ffi::EVP_CipherInit_ex(crypter.ctx,
            try!(cvt(ffi::EVP_CipherInit_ex(crypter.ctx,
                                             t.as_ptr(),
                                             ptr::null_mut(),
                                             ptr::null_mut(),
                                             ptr::null_mut(),
                                            mode));
                                             mode)));

            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 iv = match (iv, t.iv_len()) {
@@ -200,12 +201,12 @@ impl Crypter {
                (Some(_), None) | (None, None) => ptr::null_mut(),
                (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_mut(),
                                            key,
                                            iv,
                                            mode));
                                            mode)));

            Ok(crypter)
        }
@@ -237,11 +238,11 @@ impl Crypter {
            let mut outl = output.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(),
                                           &mut outl,
                                           input.as_ptr(),
                                           inl));
                                           inl)));

            Ok(outl as usize)
        }
@@ -262,7 +263,7 @@ impl Crypter {
            assert!(output.len() >= self.block_size);
            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)
        }