Commit fa622326 authored by Steven Fackler's avatar Steven Fackler
Browse files

Error reform

parent 58654bc4
Loading
Loading
Loading
Loading
+3 −4
Original line number Diff line number Diff line
@@ -2,8 +2,7 @@ use libc::c_long;
use std::ptr;

use ffi;
use ssl::error::SslError;

use error::ErrorStack;

pub struct Asn1Time {
    handle: *mut ffi::ASN1_TIME,
@@ -19,7 +18,7 @@ impl Asn1Time {
        }
    }

    fn new_with_period(period: u64) -> Result<Asn1Time, SslError> {
    fn new_with_period(period: u64) -> Result<Asn1Time, ErrorStack> {
        ffi::init();

        let handle = unsafe {
@@ -29,7 +28,7 @@ impl Asn1Time {
    }

    /// Creates a new time on specified interval in days from now
    pub fn days_from_now(days: u32) -> Result<Asn1Time, SslError> {
    pub fn days_from_now(days: u32) -> Result<Asn1Time, ErrorStack> {
        Asn1Time::new_with_period(days as u64 * 60 * 60 * 24)
    }

+4 −4
Original line number Diff line number Diff line
@@ -6,7 +6,7 @@ use std::cmp;

use ffi;
use ffi_extras;
use ssl::error::SslError;
use error::ErrorStack;

pub struct MemBio {
    bio: *mut ffi::BIO,
@@ -25,7 +25,7 @@ impl Drop for MemBio {

impl MemBio {
    /// Creates a new owned memory based BIO
    pub fn new() -> Result<MemBio, SslError> {
    pub fn new() -> Result<MemBio, ErrorStack> {
        ffi::init();

        let bio = unsafe { ffi::BIO_new(ffi::BIO_s_mem()) };
@@ -81,7 +81,7 @@ impl Read for MemBio {
            if is_eof != 0 {
                Ok(0)
            } else {
                Err(io::Error::new(io::ErrorKind::Other, SslError::get()))
                Err(io::Error::new(io::ErrorKind::Other, ErrorStack::get()))
            }
        } else {
            Ok(ret as usize)
@@ -95,7 +95,7 @@ impl Write for MemBio {
        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()))
            Err(io::Error::new(io::ErrorKind::Other, ErrorStack::get()))
        } else {
            Ok(ret as usize)
        }
+54 −54
Original line number Diff line number Diff line
@@ -4,7 +4,7 @@ use std::cmp::Ordering;
use std::{fmt, ptr, mem};

use ffi;
use ssl::error::SslError;
use error::ErrorStack;

pub struct BigNum(*mut ffi::BIGNUM);

@@ -20,7 +20,7 @@ macro_rules! with_ctx(
    ($name:ident, $action:block) => ({
        let $name = ffi::BN_CTX_new();
        if ($name).is_null() {
            Err(SslError::get())
            Err(ErrorStack::get())
        } else {
            let r = $action;
            ffi::BN_CTX_free($name);
@@ -37,7 +37,7 @@ macro_rules! with_bn(
                if $action {
                    Ok($name)
                } else {
                    Err(SslError::get())
                    Err(ErrorStack::get())
                }
            },
            Err(err) => Err(err),
@@ -52,13 +52,13 @@ macro_rules! with_bn_in_ctx(
            Ok($name) => {
                let $ctx_name = ffi::BN_CTX_new();
                if ($ctx_name).is_null() {
                    Err(SslError::get())
                    Err(ErrorStack::get())
                } else {
                    let r =
                        if $action {
                            Ok($name)
                        } else {
                            Err(SslError::get())
                            Err(ErrorStack::get())
                        };
                    ffi::BN_CTX_free($ctx_name);
                    r
@@ -70,7 +70,7 @@ macro_rules! with_bn_in_ctx(
);

impl BigNum {
    pub fn new() -> Result<BigNum, SslError> {
    pub fn new() -> Result<BigNum, ErrorStack> {
        unsafe {
            ffi::init();

@@ -79,14 +79,14 @@ impl BigNum {
        }
    }

    pub fn new_from(n: u64) -> Result<BigNum, SslError> {
    pub fn new_from(n: u64) -> Result<BigNum, ErrorStack> {
        BigNum::new().and_then(|v| unsafe {
            try_ssl!(ffi::BN_set_word(v.raw(), n as c_ulong));
            Ok(v)
        })
    }

    pub fn from_dec_str(s: &str) -> Result<BigNum, SslError> {
    pub fn from_dec_str(s: &str) -> Result<BigNum, ErrorStack> {
        BigNum::new().and_then(|v| unsafe {
            let c_str = CString::new(s.as_bytes()).unwrap();
            try_ssl!(ffi::BN_dec2bn(v.raw_ptr(), c_str.as_ptr() as *const _));
@@ -94,7 +94,7 @@ impl BigNum {
        })
    }

    pub fn from_hex_str(s: &str) -> Result<BigNum, SslError> {
    pub fn from_hex_str(s: &str) -> Result<BigNum, ErrorStack> {
        BigNum::new().and_then(|v| unsafe {
            let c_str = CString::new(s.as_bytes()).unwrap();
            try_ssl!(ffi::BN_hex2bn(v.raw_ptr(), c_str.as_ptr() as *const _));
@@ -102,26 +102,26 @@ impl BigNum {
        })
    }

    pub unsafe fn new_from_ffi(orig: *mut ffi::BIGNUM) -> Result<BigNum, SslError> {
    pub unsafe fn new_from_ffi(orig: *mut ffi::BIGNUM) -> Result<BigNum, ErrorStack> {
        if orig.is_null() {
            panic!("Null Pointer was supplied to BigNum::new_from_ffi");
        }
        let r = ffi::BN_dup(orig);
        if r.is_null() {
            Err(SslError::get())
            Err(ErrorStack::get())
        } else {
            Ok(BigNum(r))
        }
    }

    pub fn new_from_slice(n: &[u8]) -> Result<BigNum, SslError> {
    pub fn new_from_slice(n: &[u8]) -> Result<BigNum, ErrorStack> {
        BigNum::new().and_then(|v| unsafe {
            try_ssl_null!(ffi::BN_bin2bn(n.as_ptr(), n.len() as c_int, v.raw()));
            Ok(v)
        })
    }

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

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

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

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

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

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

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

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

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

    pub fn add_word(&mut self, w: c_ulong) -> Result<(), SslError> {
    pub fn add_word(&mut self, w: c_ulong) -> Result<(), ErrorStack> {
        unsafe {
            if ffi::BN_add_word(self.raw(), w) == 1 {
                Ok(())
            } else {
                Err(SslError::get())
                Err(ErrorStack::get())
            }
        }
    }

    pub fn sub_word(&mut self, w: c_ulong) -> Result<(), SslError> {
    pub fn sub_word(&mut self, w: c_ulong) -> Result<(), ErrorStack> {
        unsafe {
            if ffi::BN_sub_word(self.raw(), w) == 1 {
                Ok(())
            } else {
                Err(SslError::get())
                Err(ErrorStack::get())
            }
        }
    }

    pub fn mul_word(&mut self, w: c_ulong) -> Result<(), SslError> {
    pub fn mul_word(&mut self, w: c_ulong) -> Result<(), ErrorStack> {
        unsafe {
            if ffi::BN_mul_word(self.raw(), w) == 1 {
                Ok(())
            } else {
                Err(SslError::get())
                Err(ErrorStack::get())
            }
        }
    }

    pub fn div_word(&mut self, w: c_ulong) -> Result<c_ulong, SslError> {
    pub fn div_word(&mut self, w: c_ulong) -> Result<c_ulong, ErrorStack> {
        unsafe {
            let result = ffi::BN_div_word(self.raw(), w);
            if result != !0 as c_ulong {
                Ok(result)
            } else {
                Err(SslError::get())
                Err(ErrorStack::get())
            }
        }
    }

    pub fn mod_word(&self, w: c_ulong) -> Result<c_ulong, SslError> {
    pub fn mod_word(&self, w: c_ulong) -> Result<c_ulong, ErrorStack> {
        unsafe {
            let result = ffi::BN_mod_word(self.raw(), w);
            if result != !0 as c_ulong {
                Ok(result)
            } else {
                Err(SslError::get())
                Err(ErrorStack::get())
            }
        }
    }

    pub fn checked_gcd(&self, a: &BigNum) -> Result<BigNum, SslError> {
    pub fn checked_gcd(&self, a: &BigNum) -> Result<BigNum, ErrorStack> {
        unsafe {
            with_bn_in_ctx!(r, ctx, {
                ffi::BN_gcd(r.raw(), self.raw(), a.raw(), ctx) == 1
@@ -257,7 +257,7 @@ impl BigNum {
                                  safe: bool,
                                  add: Option<&BigNum>,
                                  rem: Option<&BigNum>)
                                  -> Result<BigNum, SslError> {
                                  -> Result<BigNum, ErrorStack> {
        unsafe {
            with_bn_in_ctx!(r, ctx, {
                let add_arg = add.map(|a| a.raw()).unwrap_or(ptr::null_mut());
@@ -273,7 +273,7 @@ impl BigNum {
        }
    }

    pub fn is_prime(&self, checks: i32) -> Result<bool, SslError> {
    pub fn is_prime(&self, checks: i32) -> Result<bool, ErrorStack> {
        unsafe {
            with_ctx!(ctx, {
                Ok(ffi::BN_is_prime_ex(self.raw(), checks as c_int, ctx, ptr::null()) == 1)
@@ -281,7 +281,7 @@ impl BigNum {
        }
    }

    pub fn is_prime_fast(&self, checks: i32, do_trial_division: bool) -> Result<bool, SslError> {
    pub fn is_prime_fast(&self, checks: i32, do_trial_division: bool) -> Result<bool, ErrorStack> {
        unsafe {
            with_ctx!(ctx, {
                Ok(ffi::BN_is_prime_fasttest_ex(self.raw(),
@@ -293,7 +293,7 @@ impl BigNum {
        }
    }

    pub fn checked_new_random(bits: i32, prop: RNGProperty, odd: bool) -> Result<BigNum, SslError> {
    pub fn checked_new_random(bits: i32, prop: RNGProperty, odd: bool) -> Result<BigNum, ErrorStack> {
        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
@@ -304,7 +304,7 @@ impl BigNum {
    pub fn checked_new_pseudo_random(bits: i32,
                                     prop: RNGProperty,
                                     odd: bool)
                                     -> Result<BigNum, SslError> {
                                     -> Result<BigNum, ErrorStack> {
        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
@@ -312,7 +312,7 @@ impl BigNum {
        }
    }

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

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

    pub fn set_bit(&mut self, n: i32) -> Result<(), SslError> {
    pub fn set_bit(&mut self, n: i32) -> Result<(), ErrorStack> {
        unsafe {
            if ffi::BN_set_bit(self.raw(), n as c_int) == 1 {
                Ok(())
            } else {
                Err(SslError::get())
                Err(ErrorStack::get())
            }
        }
    }

    pub fn clear_bit(&mut self, n: i32) -> Result<(), SslError> {
    pub fn clear_bit(&mut self, n: i32) -> Result<(), ErrorStack> {
        unsafe {
            if ffi::BN_clear_bit(self.raw(), n as c_int) == 1 {
                Ok(())
            } else {
                Err(SslError::get())
                Err(ErrorStack::get())
            }
        }
    }
@@ -352,17 +352,17 @@ impl BigNum {
        unsafe { ffi::BN_is_bit_set(self.raw(), n as c_int) == 1 }
    }

    pub fn mask_bits(&mut self, n: i32) -> Result<(), SslError> {
    pub fn mask_bits(&mut self, n: i32) -> Result<(), ErrorStack> {
        unsafe {
            if ffi::BN_mask_bits(self.raw(), n as c_int) == 1 {
                Ok(())
            } else {
                Err(SslError::get())
                Err(ErrorStack::get())
            }
        }
    }

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

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

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

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

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

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

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

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

    pub fn checked_shr(&self, a: &i32) -> Result<BigNum, SslError> {
    pub fn checked_shr(&self, a: &i32) -> Result<BigNum, ErrorStack> {
        unsafe {
            with_bn!(r, {
                ffi::BN_rshift(r.raw(), self.raw(), *a as c_int) == 1
+21 −16
Original line number Diff line number Diff line
@@ -8,8 +8,8 @@ use bio::MemBio;
use crypto::hash;
use crypto::hash::Type as HashType;
use ffi;
use ssl::error::{SslError, StreamError};
use crypto::rsa::RSA;
use error::ErrorStack;

#[derive(Copy, Clone)]
pub enum Parts {
@@ -85,17 +85,18 @@ impl PKey {
    }

    /// Reads private key from PEM, takes ownership of handle
    pub fn private_key_from_pem<R>(reader: &mut R) -> Result<PKey, SslError>
    pub fn private_key_from_pem<R>(reader: &mut R) -> io::Result<PKey>
        where R: Read
    {
        let mut mem_bio = try!(MemBio::new());
        try!(io::copy(reader, &mut mem_bio).map_err(StreamError));
        try!(io::copy(reader, &mut mem_bio));

        unsafe {
            let evp = try_ssl_null!(ffi::PEM_read_bio_PrivateKey(mem_bio.get_handle(),
                                                                 ptr::null_mut(),
                                                                 None,
                                                                 ptr::null_mut()));

            Ok(PKey {
                evp: evp as *mut ffi::EVP_PKEY,
                parts: Parts::Both,
@@ -104,11 +105,11 @@ impl PKey {
    }

    /// Reads public key from PEM, takes ownership of handle
    pub fn public_key_from_pem<R>(reader: &mut R) -> Result<PKey, SslError>
    pub fn public_key_from_pem<R>(reader: &mut R) -> io::Result<PKey>
        where R: Read
    {
        let mut mem_bio = try!(MemBio::new());
        try!(io::copy(reader, &mut mem_bio).map_err(StreamError));
        try!(io::copy(reader, &mut mem_bio));

        unsafe {
            let evp = try_ssl_null!(ffi::PEM_read_bio_PUBKEY(mem_bio.get_handle(),
@@ -123,13 +124,15 @@ impl PKey {
    }

    /// Reads an RSA private key from PEM, takes ownership of handle
    pub fn private_rsa_key_from_pem<R>(reader: &mut R) -> Result<PKey, SslError>
    pub fn private_rsa_key_from_pem<R>(reader: &mut R) -> io::Result<PKey>
    where R: Read
    {
        let rsa = try!(RSA::private_key_from_pem(reader));
        unsafe {
            let evp = try_ssl_null!(ffi::EVP_PKEY_new());
            try_ssl!(ffi::EVP_PKEY_set1_RSA(evp, rsa.as_ptr()));
            if ffi::EVP_PKEY_set1_RSA(evp, rsa.as_ptr()) == 0 {
                return Err(io::Error::new(io::ErrorKind::Other, ErrorStack::get()));
            }

            Ok(PKey {
                evp: evp,
@@ -139,13 +142,15 @@ impl PKey {
    }

    /// Reads an RSA public key from PEM, takes ownership of handle
    pub fn public_rsa_key_from_pem<R>(reader: &mut R) -> Result<PKey, SslError>
    pub fn public_rsa_key_from_pem<R>(reader: &mut R) -> io::Result<PKey>
    where R: Read
    {
        let rsa = try!(RSA::public_key_from_pem(reader));
        unsafe {
            let evp = try_ssl_null!(ffi::EVP_PKEY_new());
            try_ssl!(ffi::EVP_PKEY_set1_RSA(evp, rsa.as_ptr()));
            if ffi::EVP_PKEY_set1_RSA(evp, rsa.as_ptr()) == 0 {
                return Err(io::Error::new(io::ErrorKind::Other, ErrorStack::get()));
            }

            Ok(PKey {
                evp: evp,
@@ -260,7 +265,7 @@ impl PKey {
    // FIXME: also add password and encryption
    pub fn write_pem<W: Write>(&self,
                               writer: &mut W /* , password: Option<String> */)
                               -> Result<(), SslError> {
                               -> io::Result<()> {
        let mut mem_bio = try!(MemBio::new());
        unsafe {
            try_ssl!(ffi::PEM_write_bio_PrivateKey(mem_bio.get_handle(),
@@ -273,19 +278,19 @@ impl PKey {

        }
        let mut buf = vec![];
        try!(mem_bio.read_to_end(&mut buf).map_err(StreamError));
        writer.write_all(&buf).map_err(StreamError)
        try!(mem_bio.read_to_end(&mut buf));
        writer.write_all(&buf)
    }

    /// Stores public key as a PEM
    pub fn write_pub_pem<W: Write>(&self,
                                   writer: &mut W /* , password: Option<String> */)
                                   -> Result<(), SslError> {
                                   -> io::Result<()> {
        let mut mem_bio = try!(MemBio::new());
        unsafe { try_ssl!(ffi::PEM_write_bio_PUBKEY(mem_bio.get_handle(), self.evp)) }
        let mut buf = vec![];
        try!(mem_bio.read_to_end(&mut buf).map_err(StreamError));
        writer.write_all(&buf).map_err(StreamError)
        try!(mem_bio.read_to_end(&mut buf));
        writer.write_all(&buf)
    }

    /**
@@ -370,7 +375,7 @@ impl PKey {
                                              openssl_padding_code(padding));

            if rv < 0 as c_int {
                // println!("{:?}", SslError::get());
                // println!("{:?}", ErrorStack::get());
                vec![]
            } else {
                r.truncate(rv as usize);
+11 −11
Original line number Diff line number Diff line
use ffi;
use std::fmt;
use ssl::error::{SslError, StreamError};
use std::ptr;
use std::io::{self, Read};

use bn::BigNum;
use bio::MemBio;
use error::ErrorStack;

pub struct RSA(*mut ffi::RSA);

@@ -20,7 +20,7 @@ impl Drop for RSA {
impl RSA {
    /// only useful for associating the key material directly with the key, it's safer to use
    /// the supplied load and save methods for DER formatted keys.
    pub fn from_public_components(n: BigNum, e: BigNum) -> Result<RSA, SslError> {
    pub fn from_public_components(n: BigNum, e: BigNum) -> Result<RSA, ErrorStack> {
        unsafe {
            let rsa = try_ssl_null!(ffi::RSA_new());
            (*rsa).n = n.into_raw();
@@ -35,11 +35,11 @@ impl RSA {
    }

    /// Reads an RSA private key from PEM formatted data.
    pub fn private_key_from_pem<R>(reader: &mut R) -> Result<RSA, SslError>
    pub fn private_key_from_pem<R>(reader: &mut R) -> io::Result<RSA>
    where R: Read
    {
        let mut mem_bio = try!(MemBio::new());
        try!(io::copy(reader, &mut mem_bio).map_err(StreamError));
        try!(io::copy(reader, &mut mem_bio));

        unsafe {
            let rsa = try_ssl_null!(ffi::PEM_read_bio_RSAPrivateKey(mem_bio.get_handle(),
@@ -51,11 +51,11 @@ impl RSA {
    }

    /// Reads an RSA public key from PEM formatted data.
    pub fn public_key_from_pem<R>(reader: &mut R) -> Result<RSA, SslError>
    pub fn public_key_from_pem<R>(reader: &mut R) -> io::Result<RSA>
    where R: Read
    {
        let mut mem_bio = try!(MemBio::new());
        try!(io::copy(reader, &mut mem_bio).map_err(StreamError));
        try!(io::copy(reader, &mut mem_bio));

        unsafe {
            let rsa = try_ssl_null!(ffi::PEM_read_bio_RSA_PUBKEY(mem_bio.get_handle(),
@@ -71,7 +71,7 @@ impl RSA {
    }

    // The following getters are unsafe, since BigNum::new_from_ffi fails upon null pointers
    pub fn n(&self) -> Result<BigNum, SslError> {
    pub fn n(&self) -> Result<BigNum, ErrorStack> {
        unsafe {
            BigNum::new_from_ffi((*self.0).n)
        }
@@ -83,13 +83,13 @@ impl RSA {
        }
    }

    pub fn d(&self) -> Result<BigNum, SslError> {
    pub fn d(&self) -> Result<BigNum, ErrorStack> {
        unsafe {
            BigNum::new_from_ffi((*self.0).d)
        }
    }

    pub fn e(&self) -> Result<BigNum, SslError> {
    pub fn e(&self) -> Result<BigNum, ErrorStack> {
        unsafe {
            BigNum::new_from_ffi((*self.0).e)
        }
@@ -101,13 +101,13 @@ impl RSA {
        }
    }

    pub fn p(&self) -> Result<BigNum, SslError> {
    pub fn p(&self) -> Result<BigNum, ErrorStack> {
        unsafe {
            BigNum::new_from_ffi((*self.0).p)
        }
    }

    pub fn q(&self) -> Result<BigNum, SslError> {
    pub fn q(&self) -> Result<BigNum, ErrorStack> {
        unsafe {
            BigNum::new_from_ffi((*self.0).q)
        }
Loading