aboutsummaryrefslogtreecommitdiff
path: root/openssl/src
diff options
context:
space:
mode:
Diffstat (limited to 'openssl/src')
-rw-r--r--openssl/src/crypto/rsa.rs42
-rw-r--r--openssl/src/x509/mod.rs26
-rw-r--r--openssl/src/x509/tests.rs13
3 files changed, 53 insertions, 28 deletions
diff --git a/openssl/src/crypto/rsa.rs b/openssl/src/crypto/rsa.rs
index 3b420fbc..2b563a7a 100644
--- a/openssl/src/crypto/rsa.rs
+++ b/openssl/src/crypto/rsa.rs
@@ -110,23 +110,17 @@ impl RSA {
{
let mut mem_bio = try!(MemBio::new());
- let result = unsafe {
- ffi::PEM_write_bio_RSAPrivateKey(mem_bio.get_handle(),
+ unsafe {
+ try_ssl!(ffi::PEM_write_bio_RSAPrivateKey(mem_bio.get_handle(),
self.0,
ptr::null(),
ptr::null_mut(),
0,
None,
- ptr::null_mut())
- };
-
- if result == 1 {
- try!(io::copy(&mut mem_bio, writer).map_err(StreamError));
-
- Ok(())
- } else {
- Err(SslError::OpenSslErrors(vec![]))
+ ptr::null_mut()));
}
+ try!(io::copy(&mut mem_bio, writer).map_err(StreamError));
+ Ok(())
}
/// Reads an RSA public key from PEM formatted data.
@@ -151,15 +145,12 @@ impl RSA {
{
let mut mem_bio = try!(MemBio::new());
- let result = unsafe { ffi::PEM_write_bio_RSA_PUBKEY(mem_bio.get_handle(), self.0) };
-
- if result == 1 {
- try!(io::copy(&mut mem_bio, writer).map_err(StreamError));
+ unsafe {
+ try_ssl!(ffi::PEM_write_bio_RSA_PUBKEY(mem_bio.get_handle(), self.0))
+ };
- Ok(())
- } else {
- Err(SslError::OpenSslErrors(vec![]))
- }
+ try!(io::copy(&mut mem_bio, writer).map_err(StreamError));
+ Ok(())
}
pub fn size(&self) -> Result<u32, SslError> {
@@ -176,19 +167,14 @@ impl RSA {
let mut sig_len = k_len;
unsafe {
- let result = ffi::RSA_sign(hash.as_nid() as c_int,
+ try_ssl!(ffi::RSA_sign(hash.as_nid() as c_int,
message.as_ptr(),
message.len() as u32,
sig.as_mut_ptr(),
&mut sig_len,
- self.0);
+ self.0));
assert!(sig_len == k_len);
-
- if result == 1 {
- Ok(sig)
- } else {
- Err(SslError::OpenSslErrors(vec![]))
- }
+ Ok(sig)
}
}
@@ -200,7 +186,7 @@ impl RSA {
sig.as_ptr(),
sig.len() as u32,
self.0);
-
+ try_ssl_if!(result == -1);
Ok(result == 1)
}
}
diff --git a/openssl/src/x509/mod.rs b/openssl/src/x509/mod.rs
index 3150cc6e..c9d1772d 100644
--- a/openssl/src/x509/mod.rs
+++ b/openssl/src/x509/mod.rs
@@ -532,6 +532,17 @@ impl<'ctx> X509<'ctx> {
}
io::copy(&mut mem_bio, writer).map_err(StreamError).map(|_| ())
}
+
+ /// Returns a DER serialized form of the certificate
+ pub fn save_der(&self) -> Result<Vec<u8>, SslError> {
+ let mut mem_bio = try!(MemBio::new());
+ unsafe {
+ ffi::i2d_X509_bio(mem_bio.get_handle(), self.handle);
+ }
+ let mut v = Vec::new();
+ try!(io::copy(&mut mem_bio, &mut v).map_err(StreamError));
+ Ok(v)
+ }
}
extern "C" {
@@ -611,6 +622,10 @@ impl X509Req {
X509Req { handle: handle }
}
+ pub fn get_handle(&self) -> *mut ffi::X509_REQ {
+ self.handle
+ }
+
/// Reads CSR from PEM
pub fn from_pem<R>(reader: &mut R) -> Result<X509Req, SslError>
where R: Read
@@ -637,6 +652,17 @@ impl X509Req {
}
io::copy(&mut mem_bio, writer).map_err(StreamError).map(|_| ())
}
+
+ /// Returns a DER serialized form of the CSR
+ pub fn save_der(&self) -> Result<Vec<u8>, SslError> {
+ let mut mem_bio = try!(MemBio::new());
+ unsafe {
+ ffi::i2d_X509_REQ_bio(mem_bio.get_handle(), self.handle);
+ }
+ let mut v = Vec::new();
+ try!(io::copy(&mut mem_bio, &mut v).map_err(StreamError));
+ Ok(v)
+ }
}
impl Drop for X509Req {
diff --git a/openssl/src/x509/tests.rs b/openssl/src/x509/tests.rs
index f547a982..5d9b30ab 100644
--- a/openssl/src/x509/tests.rs
+++ b/openssl/src/x509/tests.rs
@@ -93,6 +93,19 @@ fn test_cert_loading() {
}
#[test]
+fn test_save_der() {
+ let cert_path = Path::new("test/cert.pem");
+ let mut file = File::open(&cert_path)
+ .ok()
+ .expect("Failed to open `test/cert.pem`");
+
+ let cert = X509::from_pem(&mut file).ok().expect("Failed to load PEM");
+
+ let der = cert.save_der().unwrap();
+ assert!(!der.is_empty());
+}
+
+#[test]
fn test_subject_read_cn() {
let cert_path = Path::new("test/cert.pem");
let mut file = File::open(&cert_path)