Loading openssl-sys/src/lib.rs +3 −0 Original line number Diff line number Diff line Loading @@ -566,6 +566,9 @@ extern "C" { pub fn PEM_read_bio_PUBKEY(bio: *mut BIO, out: *mut *mut EVP_PKEY, callback: Option<PasswordCallback>, user_data: *mut c_void) -> *mut X509; pub fn PEM_read_bio_RSAPrivateKey(bio: *mut BIO, rsa: *mut *mut RSA, callback: Option<PasswordCallback>, user_data: *mut c_void) -> *mut RSA; pub fn PEM_read_bio_RSA_PUBKEY(bio: *mut BIO, rsa: *mut *mut RSA, callback: Option<PasswordCallback>, user_data: *mut c_void) -> *mut RSA; pub fn PEM_write_bio_PrivateKey(bio: *mut BIO, pkey: *mut EVP_PKEY, cipher: *const EVP_CIPHER, kstr: *mut c_char, klen: c_int, callback: Option<PasswordCallback>, Loading openssl/Cargo.toml +2 −0 Original line number Diff line number Diff line Loading @@ -25,6 +25,8 @@ rfc5114 = ["openssl-sys/rfc5114"] ecdh_auto = ["openssl-sys-extras/ecdh_auto"] pkcs5_pbkdf2_hmac = ["openssl-sys/pkcs5_pbkdf2_hmac"] nightly = [] [dependencies] bitflags = ">= 0.2, < 0.4" lazy_static = "0.1" Loading openssl/src/c_helpers.c +8 −0 Original line number Diff line number Diff line Loading @@ -7,3 +7,11 @@ void rust_SSL_clone(SSL *ssl) { void rust_SSL_CTX_clone(SSL_CTX *ctx) { CRYPTO_add(&ctx->references,1,CRYPTO_LOCK_SSL_CTX); } void rust_EVP_PKEY_clone(EVP_PKEY *pkey) { CRYPTO_add(&pkey->references,1,CRYPTO_LOCK_EVP_PKEY); } void rust_X509_clone(X509 *x509) { CRYPTO_add(&x509->references,1,CRYPTO_LOCK_X509); } openssl/src/crypto/pkey.rs +85 −0 Original line number Diff line number Diff line Loading @@ -52,11 +52,18 @@ fn openssl_hash_nid(hash: HashType) -> c_int { } } extern "C" { fn rust_EVP_PKEY_clone(pkey: *mut ffi::EVP_PKEY); } pub struct PKey { evp: *mut ffi::EVP_PKEY, parts: Parts, } unsafe impl Send for PKey {} unsafe impl Sync for PKey {} /// Represents a public key, optionally with a private key attached. impl PKey { pub fn new() -> PKey { Loading Loading @@ -118,6 +125,54 @@ 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> where R: Read { let mut mem_bio = try!(MemBio::new()); try!(io::copy(reader, &mut mem_bio).map_err(StreamError)); unsafe { let rsa = try_ssl_null!(ffi::PEM_read_bio_RSAPrivateKey(mem_bio.get_handle(), ptr::null_mut(), None, ptr::null_mut())); let evp = ffi::EVP_PKEY_new(); if ffi::EVP_PKEY_set1_RSA(evp, rsa) == 0 { return Err(SslError::get()); } Ok(PKey { evp: evp, parts: Parts::Public, }) } } /// 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> where R: Read { let mut mem_bio = try!(MemBio::new()); try!(io::copy(reader, &mut mem_bio).map_err(StreamError)); unsafe { let rsa = try_ssl_null!(ffi::PEM_read_bio_RSA_PUBKEY(mem_bio.get_handle(), ptr::null_mut(), None, ptr::null_mut())); let evp = ffi::EVP_PKEY_new(); if ffi::EVP_PKEY_set1_RSA(evp, rsa) == 0 { return Err(SslError::get()); } Ok(PKey { evp: evp, parts: Parts::Public, }) } } fn _tostr(&self, f: unsafe extern "C" fn(*mut ffi::RSA, *const *mut u8) -> c_int) -> Vec<u8> { unsafe { let rsa = ffi::EVP_PKEY_get1_RSA(self.evp); Loading Loading @@ -549,6 +604,16 @@ impl Drop for PKey { } } impl Clone for PKey { fn clone(&self) -> Self { unsafe { rust_EVP_PKEY_clone(self.evp); } PKey::from_handle(self.evp, self.parts) } } #[cfg(test)] mod tests { use std::path::Path; Loading Loading @@ -613,6 +678,26 @@ mod tests { super::PKey::public_key_from_pem(&mut file).unwrap(); } #[test] fn test_private_rsa_key_from_pem() { let key_path = Path::new("test/key.pem"); let mut file = File::open(&key_path) .ok() .expect("Failed to open `test/key.pem`"); super::PKey::private_rsa_key_from_pem(&mut file).unwrap(); } #[test] fn test_public_rsa_key_from_pem() { let key_path = Path::new("test/key.pem.pub"); let mut file = File::open(&key_path) .ok() .expect("Failed to open `test/key.pem.pub`"); super::PKey::public_rsa_key_from_pem(&mut file).unwrap(); } #[test] fn test_private_encrypt() { let mut k0 = super::PKey::new(); Loading openssl/src/lib.rs +1 −0 Original line number Diff line number Diff line #![doc(html_root_url="https://sfackler.github.io/rust-openssl/doc/v0.7.4")] #![cfg_attr(feature = "nightly", feature(const_fn, recover, panic_propagate))] #[macro_use] extern crate bitflags; Loading Loading
openssl-sys/src/lib.rs +3 −0 Original line number Diff line number Diff line Loading @@ -566,6 +566,9 @@ extern "C" { pub fn PEM_read_bio_PUBKEY(bio: *mut BIO, out: *mut *mut EVP_PKEY, callback: Option<PasswordCallback>, user_data: *mut c_void) -> *mut X509; pub fn PEM_read_bio_RSAPrivateKey(bio: *mut BIO, rsa: *mut *mut RSA, callback: Option<PasswordCallback>, user_data: *mut c_void) -> *mut RSA; pub fn PEM_read_bio_RSA_PUBKEY(bio: *mut BIO, rsa: *mut *mut RSA, callback: Option<PasswordCallback>, user_data: *mut c_void) -> *mut RSA; pub fn PEM_write_bio_PrivateKey(bio: *mut BIO, pkey: *mut EVP_PKEY, cipher: *const EVP_CIPHER, kstr: *mut c_char, klen: c_int, callback: Option<PasswordCallback>, Loading
openssl/Cargo.toml +2 −0 Original line number Diff line number Diff line Loading @@ -25,6 +25,8 @@ rfc5114 = ["openssl-sys/rfc5114"] ecdh_auto = ["openssl-sys-extras/ecdh_auto"] pkcs5_pbkdf2_hmac = ["openssl-sys/pkcs5_pbkdf2_hmac"] nightly = [] [dependencies] bitflags = ">= 0.2, < 0.4" lazy_static = "0.1" Loading
openssl/src/c_helpers.c +8 −0 Original line number Diff line number Diff line Loading @@ -7,3 +7,11 @@ void rust_SSL_clone(SSL *ssl) { void rust_SSL_CTX_clone(SSL_CTX *ctx) { CRYPTO_add(&ctx->references,1,CRYPTO_LOCK_SSL_CTX); } void rust_EVP_PKEY_clone(EVP_PKEY *pkey) { CRYPTO_add(&pkey->references,1,CRYPTO_LOCK_EVP_PKEY); } void rust_X509_clone(X509 *x509) { CRYPTO_add(&x509->references,1,CRYPTO_LOCK_X509); }
openssl/src/crypto/pkey.rs +85 −0 Original line number Diff line number Diff line Loading @@ -52,11 +52,18 @@ fn openssl_hash_nid(hash: HashType) -> c_int { } } extern "C" { fn rust_EVP_PKEY_clone(pkey: *mut ffi::EVP_PKEY); } pub struct PKey { evp: *mut ffi::EVP_PKEY, parts: Parts, } unsafe impl Send for PKey {} unsafe impl Sync for PKey {} /// Represents a public key, optionally with a private key attached. impl PKey { pub fn new() -> PKey { Loading Loading @@ -118,6 +125,54 @@ 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> where R: Read { let mut mem_bio = try!(MemBio::new()); try!(io::copy(reader, &mut mem_bio).map_err(StreamError)); unsafe { let rsa = try_ssl_null!(ffi::PEM_read_bio_RSAPrivateKey(mem_bio.get_handle(), ptr::null_mut(), None, ptr::null_mut())); let evp = ffi::EVP_PKEY_new(); if ffi::EVP_PKEY_set1_RSA(evp, rsa) == 0 { return Err(SslError::get()); } Ok(PKey { evp: evp, parts: Parts::Public, }) } } /// 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> where R: Read { let mut mem_bio = try!(MemBio::new()); try!(io::copy(reader, &mut mem_bio).map_err(StreamError)); unsafe { let rsa = try_ssl_null!(ffi::PEM_read_bio_RSA_PUBKEY(mem_bio.get_handle(), ptr::null_mut(), None, ptr::null_mut())); let evp = ffi::EVP_PKEY_new(); if ffi::EVP_PKEY_set1_RSA(evp, rsa) == 0 { return Err(SslError::get()); } Ok(PKey { evp: evp, parts: Parts::Public, }) } } fn _tostr(&self, f: unsafe extern "C" fn(*mut ffi::RSA, *const *mut u8) -> c_int) -> Vec<u8> { unsafe { let rsa = ffi::EVP_PKEY_get1_RSA(self.evp); Loading Loading @@ -549,6 +604,16 @@ impl Drop for PKey { } } impl Clone for PKey { fn clone(&self) -> Self { unsafe { rust_EVP_PKEY_clone(self.evp); } PKey::from_handle(self.evp, self.parts) } } #[cfg(test)] mod tests { use std::path::Path; Loading Loading @@ -613,6 +678,26 @@ mod tests { super::PKey::public_key_from_pem(&mut file).unwrap(); } #[test] fn test_private_rsa_key_from_pem() { let key_path = Path::new("test/key.pem"); let mut file = File::open(&key_path) .ok() .expect("Failed to open `test/key.pem`"); super::PKey::private_rsa_key_from_pem(&mut file).unwrap(); } #[test] fn test_public_rsa_key_from_pem() { let key_path = Path::new("test/key.pem.pub"); let mut file = File::open(&key_path) .ok() .expect("Failed to open `test/key.pem.pub`"); super::PKey::public_rsa_key_from_pem(&mut file).unwrap(); } #[test] fn test_private_encrypt() { let mut k0 = super::PKey::new(); Loading
openssl/src/lib.rs +1 −0 Original line number Diff line number Diff line #![doc(html_root_url="https://sfackler.github.io/rust-openssl/doc/v0.7.4")] #![cfg_attr(feature = "nightly", feature(const_fn, recover, panic_propagate))] #[macro_use] extern crate bitflags; Loading