diff options
| author | Steven Fackler <[email protected]> | 2016-08-07 22:35:37 -0700 |
|---|---|---|
| committer | Steven Fackler <[email protected]> | 2016-08-07 22:35:37 -0700 |
| commit | 2a3e9a28564626bea0bf729a0ecee43553697654 (patch) | |
| tree | fbe2674308cf5790c1b60e5ab12097c54dfe14b7 /openssl/src | |
| parent | Move init to crate root (diff) | |
| download | rust-openssl-2a3e9a28564626bea0bf729a0ecee43553697654.tar.xz rust-openssl-2a3e9a28564626bea0bf729a0ecee43553697654.zip | |
Add RSA::generate
Diffstat (limited to 'openssl/src')
| -rw-r--r-- | openssl/src/crypto/rsa.rs | 40 | ||||
| -rw-r--r-- | openssl/src/x509/tests.rs | 10 |
2 files changed, 33 insertions, 17 deletions
diff --git a/openssl/src/crypto/rsa.rs b/openssl/src/crypto/rsa.rs index 3410239f..ba73d215 100644 --- a/openssl/src/crypto/rsa.rs +++ b/openssl/src/crypto/rsa.rs @@ -55,11 +55,25 @@ impl RSA { } } - /// the caller should assert that the rsa pointer is valid. pub unsafe fn from_raw(rsa: *mut ffi::RSA) -> RSA { RSA(rsa) } + /// Generates a public/private key pair with the specified size. + /// + /// The public exponent will be 65537. + pub fn generate(bits: u32) -> Result<RSA, ErrorStack> { + unsafe { + let rsa = try_ssl_null!(ffi::RSA_new()); + let rsa = RSA(rsa); + let e = try!(BigNum::new_from(ffi::RSA_F4 as _)); + + try_ssl!(ffi::RSA_generate_key_ex(rsa.0, bits as c_int, e.raw(), ptr::null_mut())); + + Ok(rsa) + } + } + /// Reads an RSA private key from PEM formatted data. pub fn private_key_from_pem(buf: &[u8]) -> Result<RSA, ErrorStack> { let mem_bio = try!(MemBioSlice::new(buf)); @@ -90,6 +104,18 @@ impl RSA { } } + /// Reads an RSA public key from PEM formatted data. + pub fn public_key_from_pem(buf: &[u8]) -> Result<RSA, ErrorStack> { + let mem_bio = try!(MemBioSlice::new(buf)); + unsafe { + let rsa = try_ssl_null!(ffi::PEM_read_bio_RSA_PUBKEY(mem_bio.handle(), + ptr::null_mut(), + None, + ptr::null_mut())); + Ok(RSA(rsa)) + } + } + /// Writes an RSA private key as unencrypted PEM formatted data pub fn private_key_to_pem(&self) -> Result<Vec<u8>, ErrorStack> { let mem_bio = try!(MemBio::new()); @@ -106,18 +132,6 @@ impl RSA { Ok(mem_bio.get_buf().to_owned()) } - /// Reads an RSA public key from PEM formatted data. - pub fn public_key_from_pem(buf: &[u8]) -> Result<RSA, ErrorStack> { - let mem_bio = try!(MemBioSlice::new(buf)); - unsafe { - let rsa = try_ssl_null!(ffi::PEM_read_bio_RSA_PUBKEY(mem_bio.handle(), - ptr::null_mut(), - None, - ptr::null_mut())); - Ok(RSA(rsa)) - } - } - /// Writes an RSA public key as PEM formatted data pub fn public_key_to_pem(&self) -> Result<Vec<u8>, ErrorStack> { let mem_bio = try!(MemBio::new()); diff --git a/openssl/src/x509/tests.rs b/openssl/src/x509/tests.rs index 86b5f92b..aedcaf55 100644 --- a/openssl/src/x509/tests.rs +++ b/openssl/src/x509/tests.rs @@ -2,6 +2,7 @@ use serialize::hex::FromHex; use crypto::hash::Type::SHA1; use crypto::pkey::PKey; +use crypto::rsa::RSA; use x509::{X509, X509Generator}; use x509::extension::Extension::{KeyUsage, ExtKeyUsage, SubjectAltName, OtherNid, OtherStr}; use x509::extension::AltNameOption as SAN; @@ -61,19 +62,20 @@ fn test_cert_gen_extension_bad_ordering() { assert!(result.is_err()); } +*/ #[test] fn test_req_gen() { - let mut pkey = PKey::new(); - pkey.gen(512); + let rsa = RSA::generate(512).unwrap(); + let mut pkey = PKey::new().unwrap(); + pkey.set_rsa(&rsa).unwrap(); let req = get_generator().request(&pkey).unwrap(); - req.write_pem().unwrap(); + req.to_pem().unwrap(); // FIXME: check data in result to be correct, needs implementation // of X509_REQ getters } -*/ #[test] fn test_cert_loading() { |