Commit f6bf022c authored by Steven Fackler's avatar Steven Fackler Committed by GitHub
Browse files

Merge pull request #476 from sfackler/error-handling

Overhaul error handling plus random APIs
parents fdb41310 a938a001
Loading
Loading
Loading
Loading
+4 −3
Original line number Diff line number Diff line
@@ -2,9 +2,10 @@ use libc::c_long;
use std::{ptr, fmt};
use std::marker::PhantomData;
use std::ops::Deref;
use ffi;

use {cvt, cvt_p};
use bio::MemBio;
use ffi;
use error::ErrorStack;

/// Corresponds to the ASN.1 structure Time defined in RFC5280
@@ -20,7 +21,7 @@ impl Asn1Time {
        ffi::init();

        unsafe {
            let handle = try_ssl_null!(ffi::X509_gmtime_adj(ptr::null_mut(), period));
            let handle = try!(cvt_p(ffi::X509_gmtime_adj(ptr::null_mut(), period)));
            Ok(Asn1Time::from_ptr(handle))
        }
    }
@@ -58,7 +59,7 @@ impl<'a> fmt::Display for Asn1TimeRef<'a> {
    fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
        let mem_bio = try!(MemBio::new());
        let as_str = unsafe {
            try_ssl!(ffi::ASN1_TIME_print(mem_bio.as_ptr(), self.0));
            try!(cvt(ffi::ASN1_TIME_print(mem_bio.as_ptr(), self.0)));
            String::from_utf8_unchecked(mem_bio.get_buf().to_owned())
        };
        write!(f, "{}", as_str)
+3 −2
Original line number Diff line number Diff line
@@ -4,6 +4,7 @@ use std::slice;
use libc::c_int;
use ffi;

use cvt_p;
use error::ErrorStack;

pub struct MemBioSlice<'a>(*mut ffi::BIO, PhantomData<&'a [u8]>);
@@ -22,7 +23,7 @@ impl<'a> MemBioSlice<'a> {

        assert!(buf.len() <= c_int::max_value() as usize);
        let bio = unsafe {
            try_ssl_null!(BIO_new_mem_buf(buf.as_ptr() as *const _, buf.len() as c_int))
            try!(cvt_p(BIO_new_mem_buf(buf.as_ptr() as *const _, buf.len() as c_int)))
        };

        Ok(MemBioSlice(bio, PhantomData))
@@ -48,7 +49,7 @@ impl MemBio {
        ffi::init();

        let bio = unsafe {
            try_ssl_null!(ffi::BIO_new(ffi::BIO_s_mem()))
            try!(cvt_p(ffi::BIO_new(ffi::BIO_s_mem())))
        };
        Ok(MemBio(bio))
    }
+908 −0

File changed and moved.

Preview size limit exceeded, changes collapsed.

+25 −30
Original line number Diff line number Diff line
@@ -4,26 +4,25 @@ use error::ErrorStack;
use std::ptr;
use libc::{c_int, c_char, c_void};

use {cvt, cvt_p};
use bn::BigNumRef;
use bio::{MemBio, MemBioSlice};
use crypto::util::{CallbackState, invoke_passwd_cb};


/// Builder for upfront DSA parameter generation
pub struct DSAParams(*mut ffi::DSA);

impl DSAParams {
    pub fn with_size(size: u32) -> Result<DSAParams, ErrorStack> {
        unsafe {
            // Wrap it so that if we panic we'll call the dtor
            let dsa = DSAParams(try_ssl_null!(ffi::DSA_new()));
            try_ssl!(ffi::DSA_generate_parameters_ex(dsa.0,
            let dsa = DSAParams(try!(cvt_p(ffi::DSA_new())));
            try!(cvt(ffi::DSA_generate_parameters_ex(dsa.0,
                                                     size as c_int,
                                                     ptr::null(),
                                                     0,
                                                     ptr::null_mut(),
                                                     ptr::null_mut(),
                                                     ptr::null_mut()));
                                                     ptr::null_mut())));
            Ok(dsa)
        }
    }
@@ -31,7 +30,7 @@ impl DSAParams {
    /// Generate a key pair from the initialized parameters
    pub fn generate(self) -> Result<DSA, ErrorStack> {
        unsafe {
            try_ssl!(ffi::DSA_generate_key(self.0));
            try!(cvt(ffi::DSA_generate_key(self.0)));
            let dsa = DSA(self.0);
            ::std::mem::forget(self);
            Ok(dsa)
@@ -75,13 +74,11 @@ impl DSA {
        let mem_bio = try!(MemBioSlice::new(buf));

        unsafe {
            let dsa = try_ssl_null!(ffi::PEM_read_bio_DSAPrivateKey(mem_bio.as_ptr(),
            let dsa = try!(cvt_p(ffi::PEM_read_bio_DSAPrivateKey(mem_bio.as_ptr(),
                                                                 ptr::null_mut(),
                                                                 None,
                                                                    ptr::null_mut()));
            let dsa = DSA(dsa);
            assert!(dsa.has_private_key());
            Ok(dsa)
                                                                 ptr::null_mut())));
            Ok(DSA(dsa))
        }
    }

@@ -99,13 +96,11 @@ impl DSA {

        unsafe {
            let cb_ptr = &mut cb as *mut _ as *mut c_void;
            let dsa = try_ssl_null!(ffi::PEM_read_bio_DSAPrivateKey(mem_bio.as_ptr(),
            let dsa = try!(cvt_p(ffi::PEM_read_bio_DSAPrivateKey(mem_bio.as_ptr(),
                                                                 ptr::null_mut(),
                                                                 Some(invoke_passwd_cb::<F>),
                                                                    cb_ptr));
            let dsa = DSA(dsa);
            assert!(dsa.has_private_key());
            Ok(dsa)
                                                                 cb_ptr)));
            Ok(DSA(dsa))
        }
    }

@@ -116,9 +111,9 @@ impl DSA {
        let mem_bio = try!(MemBio::new());

        unsafe {
            try_ssl!(ffi::PEM_write_bio_DSAPrivateKey(mem_bio.as_ptr(), self.0,
            try!(cvt(ffi::PEM_write_bio_DSAPrivateKey(mem_bio.as_ptr(), self.0,
                                                      ptr::null(), ptr::null_mut(), 0,
                                              None, ptr::null_mut()))
                                                      None, ptr::null_mut())))
        };

        Ok(mem_bio.get_buf().to_owned())
@@ -131,10 +126,10 @@ impl DSA {

        let mem_bio = try!(MemBioSlice::new(buf));
        unsafe {
            let dsa = try_ssl_null!(ffi::PEM_read_bio_DSA_PUBKEY(mem_bio.as_ptr(),
            let dsa = try!(cvt_p(ffi::PEM_read_bio_DSA_PUBKEY(mem_bio.as_ptr(),
                                                              ptr::null_mut(),
                                                              None,
                                                                 ptr::null_mut()));
                                                              ptr::null_mut())));
            Ok(DSA(dsa))
        }
    }
@@ -142,7 +137,9 @@ impl DSA {
    /// Writes an DSA public key as PEM formatted data
    pub fn public_key_to_pem(&self) -> Result<Vec<u8>, ErrorStack> {
        let mem_bio = try!(MemBio::new());
        unsafe { try_ssl!(ffi::PEM_write_bio_DSA_PUBKEY(mem_bio.as_ptr(), self.0)) };
        unsafe {
            try!(cvt(ffi::PEM_write_bio_DSA_PUBKEY(mem_bio.as_ptr(), self.0)));
        }
        Ok(mem_bio.get_buf().to_owned())
    }

@@ -239,11 +236,9 @@ impl fmt::Debug for DSA {

#[cfg(test)]
mod test {
    use std::io::Write;
    use libc::c_char;

    use super::*;
    use crypto::hash::*;

    #[test]
    pub fn test_generate() {
+6 −6
Original line number Diff line number Diff line
use std::io::prelude::*;
use std::io;
use std::ptr;
use ffi;

#[cfg(ossl110)]
@@ -8,6 +7,7 @@ use ffi::{EVP_MD_CTX_new, EVP_MD_CTX_free};
#[cfg(any(ossl101, ossl102))]
use ffi::{EVP_MD_CTX_create as EVP_MD_CTX_new, EVP_MD_CTX_destroy as EVP_MD_CTX_free};

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

#[derive(Copy, Clone)]
@@ -116,7 +116,7 @@ impl Hasher {
    pub fn new(ty: MessageDigest) -> Result<Hasher, ErrorStack> {
        ffi::init();

        let ctx = unsafe { try_ssl_null!(EVP_MD_CTX_new()) };
        let ctx = unsafe { try!(cvt_p(EVP_MD_CTX_new())) };

        let mut h = Hasher {
            ctx: ctx,
@@ -136,7 +136,7 @@ impl Hasher {
            }
            Finalized => (),
        }
        unsafe { try_ssl!(ffi::EVP_DigestInit_ex(self.ctx, self.md, 0 as *mut _)); }
        unsafe { try!(cvt(ffi::EVP_DigestInit_ex(self.ctx, self.md, 0 as *mut _))); }
        self.state = Reset;
        Ok(())
    }
@@ -147,9 +147,9 @@ impl Hasher {
            try!(self.init());
        }
        unsafe {
            try_ssl!(ffi::EVP_DigestUpdate(self.ctx,
            try!(cvt(ffi::EVP_DigestUpdate(self.ctx,
                                           data.as_ptr() as *mut _,
                                           data.len()));
                                           data.len())));
        }
        self.state = Updated;
        Ok(())
@@ -164,7 +164,7 @@ impl Hasher {
        unsafe {
            let mut len = ffi::EVP_MAX_MD_SIZE;
            let mut res = vec![0; len as usize];
            try_ssl!(ffi::EVP_DigestFinal_ex(self.ctx, res.as_mut_ptr(), &mut len));
            try!(cvt(ffi::EVP_DigestFinal_ex(self.ctx, res.as_mut_ptr(), &mut len)));
            res.truncate(len as usize);
            self.state = Finalized;
            Ok(res)
Loading