Commit 157e6aa9 authored by Steven Fackler's avatar Steven Fackler
Browse files

Rustfmt

parent edfb318e
Loading
Loading
Loading
Loading
+6 −7
Original line number Diff line number Diff line
use libc::{c_long};
use libc::c_long;
use std::ptr;

use ffi;
use ssl::error::{SslError};
use ssl::error::SslError;


pub struct Asn1Time {
    handle: *mut ffi::ASN1_TIME,
    owned: bool
    owned: bool,
}

impl Asn1Time {
@@ -15,7 +15,7 @@ impl Asn1Time {
    pub fn new(handle: *mut ffi::ASN1_TIME) -> Asn1Time {
        Asn1Time {
            handle: handle,
            owned: true
            owned: true,
        }
    }

@@ -23,8 +23,7 @@ impl Asn1Time {
        ffi::init();

        let handle = unsafe {
            try_ssl_null!(ffi::X509_gmtime_adj(ptr::null_mut(),
                                               period as c_long))
            try_ssl_null!(ffi::X509_gmtime_adj(ptr::null_mut(), period as c_long))
        };
        Ok(Asn1Time::new(handle))
    }
@@ -36,7 +35,7 @@ impl Asn1Time {

    /// Returns raw handle
    pub unsafe fn get_handle(&self) -> *mut ffi::ASN1_TIME {
        return self.handle
        return self.handle;
    }
}

+14 −12
Original line number Diff line number Diff line
@@ -6,11 +6,11 @@ use std::cmp;

use ffi;
use ffi_extras;
use ssl::error::{SslError};
use ssl::error::SslError;

pub struct MemBio {
    bio: *mut ffi::BIO,
    owned: bool
    owned: bool,
}

impl Drop for MemBio {
@@ -33,7 +33,7 @@ impl MemBio {

        Ok(MemBio {
            bio: bio,
            owned: true
            owned: true,
        })
    }

@@ -41,7 +41,7 @@ impl MemBio {
    pub fn borrowed(bio: *mut ffi::BIO) -> MemBio {
        MemBio {
            bio: bio,
            owned: false
            owned: false,
        }
    }

@@ -60,17 +60,21 @@ impl MemBio {

    /// Sets the BIO's EOF state.
    pub fn set_eof(&self, eof: bool) {
        let v = if eof { 0 } else { -1 };
        unsafe { ffi_extras::BIO_set_mem_eof_return(self.bio, v); }
        let v = if eof {
            0
        } else {
            -1
        };
        unsafe {
            ffi_extras::BIO_set_mem_eof_return(self.bio, v);
        }
    }
}

impl Read for MemBio {
    fn read(&mut self, buf: &mut [u8]) -> io::Result<usize> {
        let len = cmp::min(c_int::max_value() as usize, buf.len()) as c_int;
        let ret = unsafe {
            ffi::BIO_read(self.bio, buf.as_ptr() as *mut c_void, len)
        };
        let ret = unsafe { ffi::BIO_read(self.bio, buf.as_ptr() as *mut c_void, len) };

        if ret <= 0 {
            let is_eof = unsafe { ffi_extras::BIO_eof(self.bio) };
@@ -88,9 +92,7 @@ impl Read for MemBio {
impl Write for MemBio {
    fn write(&mut self, buf: &[u8]) -> io::Result<usize> {
        let len = cmp::min(c_int::max_value() as usize, buf.len()) as c_int;
        let ret = unsafe {
            ffi::BIO_write(self.bio, buf.as_ptr() as *const c_void, len)
        };
        let ret = unsafe { ffi::BIO_write(self.bio, buf.as_ptr() as *const c_void, len) };

        if ret < 0 {
            Err(io::Error::new(io::ErrorKind::Other, SslError::get()))
+107 −54
Original line number Diff line number Diff line
@@ -111,55 +111,73 @@ impl BigNum {

    pub fn checked_sqr(&self) -> Result<BigNum, SslError> {
        unsafe {
            with_bn_in_ctx!(r, ctx, { ffi::BN_sqr(r.raw(), self.raw(), ctx) == 1 })
            with_bn_in_ctx!(r, ctx, {
                ffi::BN_sqr(r.raw(), self.raw(), ctx) == 1
            })
        }
    }

    pub fn checked_nnmod(&self, n: &BigNum) -> Result<BigNum, SslError> {
        unsafe {
            with_bn_in_ctx!(r, ctx, { ffi::BN_nnmod(r.raw(), self.raw(), n.raw(), ctx) == 1 })
            with_bn_in_ctx!(r, ctx, {
                ffi::BN_nnmod(r.raw(), self.raw(), n.raw(), ctx) == 1
            })
        }
    }

    pub fn checked_mod_add(&self, a: &BigNum, n: &BigNum) -> Result<BigNum, SslError> {
        unsafe {
            with_bn_in_ctx!(r, ctx, { ffi::BN_mod_add(r.raw(), self.raw(), a.raw(), n.raw(), ctx) == 1 })
            with_bn_in_ctx!(r, ctx, {
                ffi::BN_mod_add(r.raw(), self.raw(), a.raw(), n.raw(), ctx) == 1
            })
        }
    }

    pub fn checked_mod_sub(&self, a: &BigNum, n: &BigNum) -> Result<BigNum, SslError> {
        unsafe {
            with_bn_in_ctx!(r, ctx, { ffi::BN_mod_sub(r.raw(), self.raw(), a.raw(), n.raw(), ctx) == 1 })
            with_bn_in_ctx!(r, ctx, {
                ffi::BN_mod_sub(r.raw(), self.raw(), a.raw(), n.raw(), ctx) == 1
            })
        }
    }

    pub fn checked_mod_mul(&self, a: &BigNum, n: &BigNum) -> Result<BigNum, SslError> {
        unsafe {
            with_bn_in_ctx!(r, ctx, { ffi::BN_mod_mul(r.raw(), self.raw(), a.raw(), n.raw(), ctx) == 1 })
            with_bn_in_ctx!(r, ctx, {
                ffi::BN_mod_mul(r.raw(), self.raw(), a.raw(), n.raw(), ctx) == 1
            })
        }
    }

    pub fn checked_mod_sqr(&self, n: &BigNum) -> Result<BigNum, SslError> {
        unsafe {
            with_bn_in_ctx!(r, ctx, { ffi::BN_mod_sqr(r.raw(), self.raw(), n.raw(), ctx) == 1 })
            with_bn_in_ctx!(r, ctx, {
                ffi::BN_mod_sqr(r.raw(), self.raw(), n.raw(), ctx) == 1
            })
        }
    }

    pub fn checked_exp(&self, p: &BigNum) -> Result<BigNum, SslError> {
        unsafe {
            with_bn_in_ctx!(r, ctx, { ffi::BN_exp(r.raw(), self.raw(), p.raw(), ctx) == 1 })
            with_bn_in_ctx!(r, ctx, {
                ffi::BN_exp(r.raw(), self.raw(), p.raw(), ctx) == 1
            })
        }
    }

    pub fn checked_mod_exp(&self, p: &BigNum, n: &BigNum) -> Result<BigNum, SslError> {
        unsafe {
            with_bn_in_ctx!(r, ctx, { ffi::BN_mod_exp(r.raw(), self.raw(), p.raw(), n.raw(), ctx) == 1 })
            with_bn_in_ctx!(r, ctx, {
                ffi::BN_mod_exp(r.raw(), self.raw(), p.raw(), n.raw(), ctx) == 1
            })
        }
    }

    pub fn checked_mod_inv(&self, n: &BigNum) -> Result<BigNum, SslError> {
        unsafe {
            with_bn_in_ctx!(r, ctx, { !ffi::BN_mod_inverse(r.raw(), self.raw(), n.raw(), ctx).is_null() })
            with_bn_in_ctx!(r, ctx, {
                !ffi::BN_mod_inverse(r.raw(), self.raw(), n.raw(), ctx).is_null()
            })
        }
    }

@@ -217,17 +235,28 @@ impl BigNum {

    pub fn checked_gcd(&self, a: &BigNum) -> Result<BigNum, SslError> {
        unsafe {
            with_bn_in_ctx!(r, ctx, { ffi::BN_gcd(r.raw(), self.raw(), a.raw(), ctx) == 1 })
            with_bn_in_ctx!(r, ctx, {
                ffi::BN_gcd(r.raw(), self.raw(), a.raw(), ctx) == 1
            })
        }
    }

    pub fn checked_generate_prime(bits: i32, safe: bool, add: Option<&BigNum>, rem: Option<&BigNum>) -> Result<BigNum, SslError> {
    pub fn checked_generate_prime(bits: i32,
                                  safe: bool,
                                  add: Option<&BigNum>,
                                  rem: Option<&BigNum>)
                                  -> Result<BigNum, SslError> {
        unsafe {
            with_bn_in_ctx!(r, ctx, {
                let add_arg = add.map(|a| a.raw()).unwrap_or(ptr::null_mut());
                let rem_arg = rem.map(|r| r.raw()).unwrap_or(ptr::null_mut());

                ffi::BN_generate_prime_ex(r.raw(), bits as c_int, safe as c_int, add_arg, rem_arg, ptr::null()) == 1
                ffi::BN_generate_prime_ex(r.raw(),
                                          bits as c_int,
                                          safe as c_int,
                                          add_arg,
                                          rem_arg,
                                          ptr::null()) == 1
            })
        }
    }
@@ -243,32 +272,47 @@ impl BigNum {
    pub fn is_prime_fast(&self, checks: i32, do_trial_division: bool) -> Result<bool, SslError> {
        unsafe {
            with_ctx!(ctx, {
                Ok(ffi::BN_is_prime_fasttest_ex(self.raw(), checks as c_int, ctx, do_trial_division as c_int, ptr::null()) == 1)
                Ok(ffi::BN_is_prime_fasttest_ex(self.raw(),
                                                checks as c_int,
                                                ctx,
                                                do_trial_division as c_int,
                                                ptr::null()) == 1)
            })
        }
    }

    pub fn checked_new_random(bits: i32, prop: RNGProperty, odd: bool) -> Result<BigNum, SslError> {
        unsafe {
            with_bn_in_ctx!(r, ctx, { ffi::BN_rand(r.raw(), bits as c_int, prop as c_int, odd as c_int) == 1 })
            with_bn_in_ctx!(r, ctx, {
                ffi::BN_rand(r.raw(), bits as c_int, prop as c_int, odd as c_int) == 1
            })
        }
    }

    pub fn checked_new_pseudo_random(bits: i32, prop: RNGProperty, odd: bool) -> Result<BigNum, SslError> {
    pub fn checked_new_pseudo_random(bits: i32,
                                     prop: RNGProperty,
                                     odd: bool)
                                     -> Result<BigNum, SslError> {
        unsafe {
            with_bn_in_ctx!(r, ctx, { ffi::BN_pseudo_rand(r.raw(), bits as c_int, prop as c_int, odd as c_int) == 1 })
            with_bn_in_ctx!(r, ctx, {
                ffi::BN_pseudo_rand(r.raw(), bits as c_int, prop as c_int, odd as c_int) == 1
            })
        }
    }

    pub fn checked_rand_in_range(&self) -> Result<BigNum, SslError> {
        unsafe {
            with_bn_in_ctx!(r, ctx, { ffi::BN_rand_range(r.raw(), self.raw()) == 1 })
            with_bn_in_ctx!(r, ctx, {
                ffi::BN_rand_range(r.raw(), self.raw()) == 1
            })
        }
    }

    pub fn checked_pseudo_rand_in_range(&self) -> Result<BigNum, SslError> {
        unsafe {
            with_bn_in_ctx!(r, ctx, { ffi::BN_pseudo_rand_range(r.raw(), self.raw()) == 1 })
            with_bn_in_ctx!(r, ctx, {
                ffi::BN_pseudo_rand_range(r.raw(), self.raw()) == 1
            })
        }
    }

@@ -293,9 +337,7 @@ impl BigNum {
    }

    pub fn is_bit_set(&self, n: i32) -> bool {
        unsafe {
            ffi::BN_is_bit_set(self.raw(), n as c_int) == 1
        }
        unsafe { ffi::BN_is_bit_set(self.raw(), n as c_int) == 1 }
    }

    pub fn mask_bits(&mut self, n: i32) -> Result<(), SslError> {
@@ -310,62 +352,78 @@ impl BigNum {

    pub fn checked_shl1(&self) -> Result<BigNum, SslError> {
        unsafe {
            with_bn!(r, { ffi::BN_lshift1(r.raw(), self.raw()) == 1 })
            with_bn!(r, {
                ffi::BN_lshift1(r.raw(), self.raw()) == 1
            })
        }
    }

    pub fn checked_shr1(&self) -> Result<BigNum, SslError> {
        unsafe {
            with_bn!(r, { ffi::BN_rshift1(r.raw(), self.raw()) == 1 })
            with_bn!(r, {
                ffi::BN_rshift1(r.raw(), self.raw()) == 1
            })
        }
    }

    pub fn checked_add(&self, a: &BigNum) -> Result<BigNum, SslError> {
        unsafe {
            with_bn!(r, { ffi::BN_add(r.raw(), self.raw(), a.raw()) == 1 })
            with_bn!(r, {
                ffi::BN_add(r.raw(), self.raw(), a.raw()) == 1
            })
        }
    }

    pub fn checked_sub(&self, a: &BigNum) -> Result<BigNum, SslError> {
        unsafe {
            with_bn!(r, { ffi::BN_sub(r.raw(), self.raw(), a.raw()) == 1 })
            with_bn!(r, {
                ffi::BN_sub(r.raw(), self.raw(), a.raw()) == 1
            })
        }
    }

    pub fn checked_mul(&self, a: &BigNum) -> Result<BigNum, SslError> {
        unsafe {
            with_bn_in_ctx!(r, ctx, { ffi::BN_mul(r.raw(), self.raw(), a.raw(), ctx) == 1 })
            with_bn_in_ctx!(r, ctx, {
                ffi::BN_mul(r.raw(), self.raw(), a.raw(), ctx) == 1
            })
        }
    }

    pub fn checked_div(&self, a: &BigNum) -> Result<BigNum, SslError> {
        unsafe {
            with_bn_in_ctx!(r, ctx, { ffi::BN_div(r.raw(), ptr::null_mut(), self.raw(), a.raw(), ctx) == 1 })
            with_bn_in_ctx!(r, ctx, {
                ffi::BN_div(r.raw(), ptr::null_mut(), self.raw(), a.raw(), ctx) == 1
            })
        }
    }

    pub fn checked_mod(&self, a: &BigNum) -> Result<BigNum, SslError> {
        unsafe {
            with_bn_in_ctx!(r, ctx, { ffi::BN_div(ptr::null_mut(), r.raw(), self.raw(), a.raw(), ctx) == 1 })
            with_bn_in_ctx!(r, ctx, {
                ffi::BN_div(ptr::null_mut(), r.raw(), self.raw(), a.raw(), ctx) == 1
            })
        }
    }

    pub fn checked_shl(&self, a: &i32) -> Result<BigNum, SslError> {
        unsafe {
            with_bn!(r, { ffi::BN_lshift(r.raw(), self.raw(), *a as c_int) == 1 })
            with_bn!(r, {
                ffi::BN_lshift(r.raw(), self.raw(), *a as c_int) == 1
            })
        }
    }

    pub fn checked_shr(&self, a: &i32) -> Result<BigNum, SslError> {
        unsafe {
            with_bn!(r, { ffi::BN_rshift(r.raw(), self.raw(), *a as c_int) == 1 })
            with_bn!(r, {
                ffi::BN_rshift(r.raw(), self.raw(), *a as c_int) == 1
            })
        }
    }

    pub fn negate(&mut self) {
        unsafe {
            ffi::BN_set_negative(self.raw(), !self.is_negative() as c_int)
        }
        unsafe { ffi::BN_set_negative(self.raw(), !self.is_negative() as c_int) }
    }

    pub fn abs_cmp(&self, oth: BigNum) -> Ordering {
@@ -382,15 +440,11 @@ impl BigNum {
    }

    pub fn is_negative(&self) -> bool {
        unsafe {
            (*self.raw()).neg == 1
        }
        unsafe { (*self.raw()).neg == 1 }
    }

    pub fn num_bits(&self) -> i32 {
        unsafe {
            ffi::BN_num_bits(self.raw()) as i32
        }
        unsafe { ffi::BN_num_bits(self.raw()) as i32 }
    }

    pub fn num_bytes(&self) -> i32 {
@@ -421,7 +475,8 @@ impl BigNum {
        unsafe {
            let buf = ffi::BN_bn2dec(self.raw());
            assert!(!buf.is_null());
            let str = String::from_utf8(CStr::from_ptr(buf as *const _).to_bytes().to_vec()).unwrap();
            let str = String::from_utf8(CStr::from_ptr(buf as *const _).to_bytes().to_vec())
                          .unwrap();
            ffi::CRYPTO_free(buf as *mut c_void);
            str
        }
@@ -431,7 +486,8 @@ impl BigNum {
        unsafe {
            let buf = ffi::BN_bn2hex(self.raw());
            assert!(!buf.is_null());
            let str = String::from_utf8(CStr::from_ptr(buf as *const _).to_bytes().to_vec()).unwrap();
            let str = String::from_utf8(CStr::from_ptr(buf as *const _).to_bytes().to_vec())
                          .unwrap();
            ffi::CRYPTO_free(buf as *mut c_void);
            str
        }
@@ -447,9 +503,7 @@ impl fmt::Debug for BigNum {
impl Eq for BigNum {}
impl PartialEq for BigNum {
    fn eq(&self, oth: &BigNum) -> bool {
        unsafe {
            ffi::BN_cmp(self.raw(), oth.raw()) == 0
        }
        unsafe { ffi::BN_cmp(self.raw(), oth.raw()) == 0 }
    }
}

@@ -463,8 +517,7 @@ impl PartialOrd for BigNum {
    fn partial_cmp(&self, oth: &BigNum) -> Option<Ordering> {
        unsafe {
            let v = ffi::BN_cmp(self.raw(), oth.raw());
            let ret =
                if v == 0 {
            let ret = if v == 0 {
                Ordering::Equal
            } else if v < 0 {
                Ordering::Less
@@ -489,7 +542,7 @@ impl Drop for BigNum {
pub mod unchecked {
    use std::ops::{Add, Div, Mul, Neg, Rem, Shl, Shr, Sub};
    use ffi;
    use super::{BigNum};
    use super::BigNum;

    impl<'a> Add<&'a BigNum> for &'a BigNum {
        type Output = BigNum;
+47 −30
Original line number Diff line number Diff line
@@ -14,7 +14,7 @@ pub enum Type {
    SHA256,
    SHA384,
    SHA512,
    RIPEMD160
    RIPEMD160,
}

impl Type {
@@ -112,7 +112,12 @@ impl Hasher {
        };
        let md = ty.evp_md();

        let mut h = Hasher { ctx: ctx, md: md, type_: ty, state: Finalized };
        let mut h = Hasher {
            ctx: ctx,
            md: md,
            type_: ty,
            state: Finalized,
        };
        h.init();
        h
    }
@@ -121,7 +126,9 @@ impl Hasher {
    fn init(&mut self) {
        match self.state {
            Reset => return,
            Updated => { self.finalize(); },
            Updated => {
                self.finalize();
            }
            Finalized => (),
        }
        unsafe {
@@ -137,8 +144,7 @@ impl Hasher {
            self.init();
        }
        unsafe {
            let r = ffi::EVP_DigestUpdate(self.ctx, data.as_ptr(),
                                          data.len() as c_uint);
            let r = ffi::EVP_DigestUpdate(self.ctx, data.as_ptr(), data.len() as c_uint);
            assert_eq!(r, 1);
        }
        self.state = Updated;
@@ -190,7 +196,12 @@ impl Clone for Hasher {
            assert_eq!(r, 1);
            ctx
        };
        Hasher { ctx: ctx, md: self.md, type_: self.type_, state: self.state }
        Hasher {
            ctx: ctx,
            md: self.md,
            type_: self.type_,
            state: self.state,
        }
    }
}

@@ -233,21 +244,32 @@ mod tests {

    // Test vectors from http://www.nsrl.nist.gov/testdata/
    #[allow(non_upper_case_globals)]
    const md5_tests: [(&'static str, &'static str); 13] = [
        ("", "d41d8cd98f00b204e9800998ecf8427e"),
        ("7F", "83acb6e67e50e31db6ed341dd2de1595"),
        ("EC9C", "0b07f0d4ca797d8ac58874f887cb0b68"),
        ("FEE57A", "e0d583171eb06d56198fc0ef22173907"),
        ("42F497E0", "7c430f178aefdf1487fee7144e9641e2"),
        ("C53B777F1C", "75ef141d64cb37ec423da2d9d440c925"),
        ("89D5B576327B", "ebbaf15eb0ed784c6faa9dc32831bf33"),
        ("5D4CCE781EB190", "ce175c4b08172019f05e6b5279889f2c"),
        ("81901FE94932D7B9", "cd4d2f62b8cdb3a0cf968a735a239281"),
        ("C9FFDEE7788EFB4EC9", "e0841a231ab698db30c6c0f3f246c014"),
        ("66AC4B7EBA95E53DC10B", "a3b3cea71910d9af56742aa0bb2fe329"),
        ("A510CD18F7A56852EB0319", "577e216843dd11573574d3fb209b97d8"),
        ("AAED18DBE8938C19ED734A8D", "6f80fb775f27e0a4ce5c2f42fc72c5f1")
    ];
    const md5_tests: [(&'static str, &'static str); 13] = [("",
                                                            "d41d8cd98f00b204e9800998ecf8427e"),
                                                           ("7F",
                                                            "83acb6e67e50e31db6ed341dd2de1595"),
                                                           ("EC9C",
                                                            "0b07f0d4ca797d8ac58874f887cb0b68"),
                                                           ("FEE57A",
                                                            "e0d583171eb06d56198fc0ef22173907"),
                                                           ("42F497E0",
                                                            "7c430f178aefdf1487fee7144e9641e2"),
                                                           ("C53B777F1C",
                                                            "75ef141d64cb37ec423da2d9d440c925"),
                                                           ("89D5B576327B",
                                                            "ebbaf15eb0ed784c6faa9dc32831bf33"),
                                                           ("5D4CCE781EB190",
                                                            "ce175c4b08172019f05e6b5279889f2c"),
                                                           ("81901FE94932D7B9",
                                                            "cd4d2f62b8cdb3a0cf968a735a239281"),
                                                           ("C9FFDEE7788EFB4EC9",
                                                            "e0841a231ab698db30c6c0f3f246c014"),
                                                           ("66AC4B7EBA95E53DC10B",
                                                            "a3b3cea71910d9af56742aa0bb2fe329"),
                                                           ("A510CD18F7A56852EB0319",
                                                            "577e216843dd11573574d3fb209b97d8"),
                                                           ("AAED18DBE8938C19ED734A8D",
                                                            "6f80fb775f27e0a4ce5c2f42fc72c5f1")];

    #[test]
    fn test_md5() {
@@ -305,9 +327,7 @@ mod tests {

    #[test]
    fn test_sha1() {
        let tests = [
            ("616263", "a9993e364706816aba3e25717850c26c9cd0d89d"),
            ];
        let tests = [("616263", "a9993e364706816aba3e25717850c26c9cd0d89d")];

        for test in tests.iter() {
            hash_test(Type::SHA1, test);
@@ -316,9 +336,8 @@ mod tests {

    #[test]
    fn test_sha256() {
        let tests = [
            ("616263", "ba7816bf8f01cfea414140de5dae2223b00361a396177a9cb410ff61f20015ad")
            ];
        let tests = [("616263",
                      "ba7816bf8f01cfea414140de5dae2223b00361a396177a9cb410ff61f20015ad")];

        for test in tests.iter() {
            hash_test(Type::SHA256, test);
@@ -327,9 +346,7 @@ mod tests {

    #[test]
    fn test_ripemd160() {
        let tests = [
            ("616263", "8eb208f7e05d987a9b044a8e98c6b087f15a0bfc")
            ];
        let tests = [("616263", "8eb208f7e05d987a9b044a8e98c6b087f15a0bfc")];

        for test in tests.iter() {
            hash_test(Type::RIPEMD160, test);
+205 −167

File changed.

Preview size limit exceeded, changes collapsed.

Loading