diff options
| author | Steven Fackler <[email protected]> | 2016-01-30 12:55:22 -0800 |
|---|---|---|
| committer | Steven Fackler <[email protected]> | 2016-05-03 20:24:07 -0700 |
| commit | fa622326490e1dd27df4d42b4097ca574deedb3f (patch) | |
| tree | f6b60233b5f71847a3fbd87aa13a74f16fe79ddf /openssl/src/crypto | |
| parent | Remove deprecated methods (diff) | |
| download | rust-openssl-fa622326490e1dd27df4d42b4097ca574deedb3f.tar.xz rust-openssl-fa622326490e1dd27df4d42b4097ca574deedb3f.zip | |
Error reform
Diffstat (limited to 'openssl/src/crypto')
| -rw-r--r-- | openssl/src/crypto/pkey.rs | 37 | ||||
| -rw-r--r-- | openssl/src/crypto/rsa.rs | 22 |
2 files changed, 32 insertions, 27 deletions
diff --git a/openssl/src/crypto/pkey.rs b/openssl/src/crypto/pkey.rs index ba0a16b6..1020a82e 100644 --- a/openssl/src/crypto/pkey.rs +++ b/openssl/src/crypto/pkey.rs @@ -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); diff --git a/openssl/src/crypto/rsa.rs b/openssl/src/crypto/rsa.rs index 6fcb5b07..11970933 100644 --- a/openssl/src/crypto/rsa.rs +++ b/openssl/src/crypto/rsa.rs @@ -1,11 +1,11 @@ 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) } |