From 98b7f2f9352e4d92b44245d0737f9a45adb4ae2b Mon Sep 17 00:00:00 2001 From: Steven Fackler Date: Sat, 22 Oct 2016 09:16:38 -0700 Subject: Flatten crypto module --- openssl/src/pkey.rs | 193 ++++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 193 insertions(+) create mode 100644 openssl/src/pkey.rs (limited to 'openssl/src/pkey.rs') diff --git a/openssl/src/pkey.rs b/openssl/src/pkey.rs new file mode 100644 index 00000000..d91acb36 --- /dev/null +++ b/openssl/src/pkey.rs @@ -0,0 +1,193 @@ +use libc::{c_void, c_char, c_int}; +use std::ptr; +use std::mem; +use ffi; + +use {cvt, cvt_p}; +use bio::{MemBio, MemBioSlice}; +use dsa::DSA; +use rsa::RSA; +use error::ErrorStack; +use util::{CallbackState, invoke_passwd_cb}; + +pub struct PKey(*mut ffi::EVP_PKEY); + +unsafe impl Send for PKey {} +unsafe impl Sync for PKey {} + +/// Represents a public key, optionally with a private key attached. +impl PKey { + /// Create a new `PKey` containing an RSA key. + pub fn from_rsa(rsa: RSA) -> Result { + unsafe { + let evp = try!(cvt_p(ffi::EVP_PKEY_new())); + let pkey = PKey(evp); + try!(cvt(ffi::EVP_PKEY_assign(pkey.0, ffi::EVP_PKEY_RSA, rsa.as_ptr() as *mut _))); + mem::forget(rsa); + Ok(pkey) + } + } + + /// Create a new `PKey` containing a DSA key. + pub fn from_dsa(dsa: DSA) -> Result { + unsafe { + let evp = try!(cvt_p(ffi::EVP_PKEY_new())); + let pkey = PKey(evp); + try!(cvt(ffi::EVP_PKEY_assign(pkey.0, ffi::EVP_PKEY_DSA, dsa.as_ptr() as *mut _))); + mem::forget(dsa); + Ok(pkey) + } + } + + /// Create a new `PKey` containing an HMAC key. + pub fn hmac(key: &[u8]) -> Result { + unsafe { + assert!(key.len() <= c_int::max_value() as usize); + let key = try!(cvt_p(ffi::EVP_PKEY_new_mac_key(ffi::EVP_PKEY_HMAC, + ptr::null_mut(), + key.as_ptr() as *const _, + key.len() as c_int))); + Ok(PKey(key)) + } + } + + pub unsafe fn from_ptr(handle: *mut ffi::EVP_PKEY) -> PKey { + PKey(handle) + } + + /// Reads private key from PEM, takes ownership of handle + pub fn private_key_from_pem(buf: &[u8]) -> Result { + ffi::init(); + let mem_bio = try!(MemBioSlice::new(buf)); + unsafe { + let evp = try!(cvt_p(ffi::PEM_read_bio_PrivateKey(mem_bio.as_ptr(), + ptr::null_mut(), + None, + ptr::null_mut()))); + Ok(PKey::from_ptr(evp)) + } + } + + /// Read a private key from PEM, supplying a password callback to be invoked if the private key + /// is encrypted. + /// + /// The callback will be passed the password buffer and should return the number of characters + /// placed into the buffer. + pub fn private_key_from_pem_cb(buf: &[u8], pass_cb: F) -> Result + where F: FnOnce(&mut [c_char]) -> usize + { + ffi::init(); + let mut cb = CallbackState::new(pass_cb); + let mem_bio = try!(MemBioSlice::new(buf)); + unsafe { + let evp = try!(cvt_p(ffi::PEM_read_bio_PrivateKey(mem_bio.as_ptr(), + ptr::null_mut(), + Some(invoke_passwd_cb::), + &mut cb as *mut _ as *mut c_void))); + Ok(PKey::from_ptr(evp)) + } + } + + /// Reads public key from PEM, takes ownership of handle + pub fn public_key_from_pem(buf: &[u8]) -> Result { + ffi::init(); + let mem_bio = try!(MemBioSlice::new(buf)); + unsafe { + let evp = try!(cvt_p(ffi::PEM_read_bio_PUBKEY(mem_bio.as_ptr(), + ptr::null_mut(), + None, + ptr::null_mut()))); + Ok(PKey::from_ptr(evp)) + } + } + + /// assign RSA key to this pkey + pub fn set_rsa(&mut self, rsa: &RSA) -> Result<(), ErrorStack> { + unsafe { + // this needs to be a reference as the set1_RSA ups the reference count + let rsa_ptr = rsa.as_ptr(); + try!(cvt(ffi::EVP_PKEY_set1_RSA(self.0, rsa_ptr))); + Ok(()) + } + } + + /// Get a reference to the interal RSA key for direct access to the key components + pub fn rsa(&self) -> Result { + unsafe { + let rsa = try!(cvt_p(ffi::EVP_PKEY_get1_RSA(self.0))); + // this is safe as the ffi increments a reference counter to the internal key + Ok(RSA::from_ptr(rsa)) + } + } + + /// Stores private key as a PEM + // FIXME: also add password and encryption + pub fn private_key_to_pem(&self) -> Result, ErrorStack> { + let mem_bio = try!(MemBio::new()); + unsafe { + try!(cvt(ffi::PEM_write_bio_PrivateKey(mem_bio.as_ptr(), + self.0, + ptr::null(), + ptr::null_mut(), + -1, + None, + ptr::null_mut()))); + + } + Ok(mem_bio.get_buf().to_owned()) + } + + /// Stores public key as a PEM + pub fn public_key_to_pem(&self) -> Result, ErrorStack> { + let mem_bio = try!(MemBio::new()); + unsafe { + try!(cvt(ffi::PEM_write_bio_PUBKEY(mem_bio.as_ptr(), self.0))); + } + Ok(mem_bio.get_buf().to_owned()) + } + + pub fn as_ptr(&self) -> *mut ffi::EVP_PKEY { + return self.0; + } + + pub fn public_eq(&self, other: &PKey) -> bool { + unsafe { ffi::EVP_PKEY_cmp(self.0, other.0) == 1 } + } +} + +impl Drop for PKey { + fn drop(&mut self) { + unsafe { + ffi::EVP_PKEY_free(self.0); + } + } +} + +#[cfg(test)] +mod tests { + #[test] + fn test_private_key_from_pem() { + let key = include_bytes!("../test/key.pem"); + super::PKey::private_key_from_pem(key).unwrap(); + } + + #[test] + fn test_public_key_from_pem() { + let key = include_bytes!("../test/key.pem.pub"); + super::PKey::public_key_from_pem(key).unwrap(); + } + + #[test] + fn test_pem() { + let key = include_bytes!("../test/key.pem"); + let key = super::PKey::private_key_from_pem(key).unwrap(); + + let priv_key = key.private_key_to_pem().unwrap(); + let pub_key = key.public_key_to_pem().unwrap(); + + // As a super-simple verification, just check that the buffers contain + // the `PRIVATE KEY` or `PUBLIC KEY` strings. + assert!(priv_key.windows(11).any(|s| s == b"PRIVATE KEY")); + assert!(pub_key.windows(10).any(|s| s == b"PUBLIC KEY")); + } +} -- cgit v1.2.3 From b619c4e88505751e6f57c742a11520e65bcc237d Mon Sep 17 00:00:00 2001 From: Steven Fackler Date: Sat, 22 Oct 2016 10:16:49 -0700 Subject: Camel case Dsa --- openssl/src/pkey.rs | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) (limited to 'openssl/src/pkey.rs') diff --git a/openssl/src/pkey.rs b/openssl/src/pkey.rs index d91acb36..c581c980 100644 --- a/openssl/src/pkey.rs +++ b/openssl/src/pkey.rs @@ -5,7 +5,7 @@ use ffi; use {cvt, cvt_p}; use bio::{MemBio, MemBioSlice}; -use dsa::DSA; +use dsa::Dsa; use rsa::RSA; use error::ErrorStack; use util::{CallbackState, invoke_passwd_cb}; @@ -29,7 +29,7 @@ impl PKey { } /// Create a new `PKey` containing a DSA key. - pub fn from_dsa(dsa: DSA) -> Result { + pub fn from_dsa(dsa: Dsa) -> Result { unsafe { let evp = try!(cvt_p(ffi::EVP_PKEY_new())); let pkey = PKey(evp); -- cgit v1.2.3 From 3c50c74444e841c9178758fe18dcef38f56da243 Mon Sep 17 00:00:00 2001 From: Steven Fackler Date: Sat, 22 Oct 2016 10:21:16 -0700 Subject: Camel case Rsa --- openssl/src/pkey.rs | 10 +++++----- 1 file changed, 5 insertions(+), 5 deletions(-) (limited to 'openssl/src/pkey.rs') diff --git a/openssl/src/pkey.rs b/openssl/src/pkey.rs index c581c980..e62bbbd9 100644 --- a/openssl/src/pkey.rs +++ b/openssl/src/pkey.rs @@ -6,7 +6,7 @@ use ffi; use {cvt, cvt_p}; use bio::{MemBio, MemBioSlice}; use dsa::Dsa; -use rsa::RSA; +use rsa::Rsa; use error::ErrorStack; use util::{CallbackState, invoke_passwd_cb}; @@ -18,7 +18,7 @@ unsafe impl Sync for PKey {} /// Represents a public key, optionally with a private key attached. impl PKey { /// Create a new `PKey` containing an RSA key. - pub fn from_rsa(rsa: RSA) -> Result { + pub fn from_rsa(rsa: Rsa) -> Result { unsafe { let evp = try!(cvt_p(ffi::EVP_PKEY_new())); let pkey = PKey(evp); @@ -102,7 +102,7 @@ impl PKey { } /// assign RSA key to this pkey - pub fn set_rsa(&mut self, rsa: &RSA) -> Result<(), ErrorStack> { + pub fn set_rsa(&mut self, rsa: &Rsa) -> Result<(), ErrorStack> { unsafe { // this needs to be a reference as the set1_RSA ups the reference count let rsa_ptr = rsa.as_ptr(); @@ -112,11 +112,11 @@ impl PKey { } /// Get a reference to the interal RSA key for direct access to the key components - pub fn rsa(&self) -> Result { + pub fn rsa(&self) -> Result { unsafe { let rsa = try!(cvt_p(ffi::EVP_PKEY_get1_RSA(self.0))); // this is safe as the ffi increments a reference counter to the internal key - Ok(RSA::from_ptr(rsa)) + Ok(Rsa::from_ptr(rsa)) } } -- cgit v1.2.3 From 57d10ebbc3c04d5089b034b9d88f40c302783c96 Mon Sep 17 00:00:00 2001 From: Steven Fackler Date: Sat, 29 Oct 2016 14:19:09 -0700 Subject: Add PKeyRef --- openssl/src/pkey.rs | 109 +++++++++++++++++++++++++++++++--------------------- 1 file changed, 65 insertions(+), 44 deletions(-) (limited to 'openssl/src/pkey.rs') diff --git a/openssl/src/pkey.rs b/openssl/src/pkey.rs index e62bbbd9..4cbfcce7 100644 --- a/openssl/src/pkey.rs +++ b/openssl/src/pkey.rs @@ -1,6 +1,7 @@ use libc::{c_void, c_char, c_int}; use std::ptr; use std::mem; +use std::ops::Deref; use ffi; use {cvt, cvt_p}; @@ -9,13 +10,66 @@ use dsa::Dsa; use rsa::Rsa; use error::ErrorStack; use util::{CallbackState, invoke_passwd_cb}; +use opaque::Opaque; +/// A borrowed `PKey`. +pub struct PKeyRef(Opaque); + +impl PKeyRef { + pub unsafe fn from_ptr<'a>(ptr: *mut ffi::EVP_PKEY) -> &'a PKeyRef { + &*(ptr as *mut _) + } + + pub fn as_ptr(&self) -> *mut ffi::EVP_PKEY { + self as *const _ as *mut _ + } + + /// Get a reference to the interal RSA key for direct access to the key components + pub fn rsa(&self) -> Result { + unsafe { + let rsa = try!(cvt_p(ffi::EVP_PKEY_get1_RSA(self.as_ptr()))); + // this is safe as the ffi increments a reference counter to the internal key + Ok(Rsa::from_ptr(rsa)) + } + } + + /// Stores private key as a PEM + // FIXME: also add password and encryption + pub fn private_key_to_pem(&self) -> Result, ErrorStack> { + let mem_bio = try!(MemBio::new()); + unsafe { + try!(cvt(ffi::PEM_write_bio_PrivateKey(mem_bio.as_ptr(), + self.as_ptr(), + ptr::null(), + ptr::null_mut(), + -1, + None, + ptr::null_mut()))); + + } + Ok(mem_bio.get_buf().to_owned()) + } + + /// Stores public key as a PEM + pub fn public_key_to_pem(&self) -> Result, ErrorStack> { + let mem_bio = try!(MemBio::new()); + unsafe { + try!(cvt(ffi::PEM_write_bio_PUBKEY(mem_bio.as_ptr(), self.as_ptr()))); + } + Ok(mem_bio.get_buf().to_owned()) + } + + pub fn public_eq(&self, other: &PKeyRef) -> bool { + unsafe { ffi::EVP_PKEY_cmp(self.as_ptr(), other.as_ptr()) == 1 } + } +} + +/// Represents a public key, optionally with a private key attached. pub struct PKey(*mut ffi::EVP_PKEY); unsafe impl Send for PKey {} unsafe impl Sync for PKey {} -/// Represents a public key, optionally with a private key attached. impl PKey { /// Create a new `PKey` containing an RSA key. pub fn from_rsa(rsa: Rsa) -> Result { @@ -101,7 +155,7 @@ impl PKey { } } - /// assign RSA key to this pkey + /// Assign an RSA key to this pkey. pub fn set_rsa(&mut self, rsa: &Rsa) -> Result<(), ErrorStack> { unsafe { // this needs to be a reference as the set1_RSA ups the reference count @@ -110,55 +164,22 @@ impl PKey { Ok(()) } } +} - /// Get a reference to the interal RSA key for direct access to the key components - pub fn rsa(&self) -> Result { - unsafe { - let rsa = try!(cvt_p(ffi::EVP_PKEY_get1_RSA(self.0))); - // this is safe as the ffi increments a reference counter to the internal key - Ok(Rsa::from_ptr(rsa)) - } - } - - /// Stores private key as a PEM - // FIXME: also add password and encryption - pub fn private_key_to_pem(&self) -> Result, ErrorStack> { - let mem_bio = try!(MemBio::new()); - unsafe { - try!(cvt(ffi::PEM_write_bio_PrivateKey(mem_bio.as_ptr(), - self.0, - ptr::null(), - ptr::null_mut(), - -1, - None, - ptr::null_mut()))); - - } - Ok(mem_bio.get_buf().to_owned()) - } - - /// Stores public key as a PEM - pub fn public_key_to_pem(&self) -> Result, ErrorStack> { - let mem_bio = try!(MemBio::new()); +impl Drop for PKey { + fn drop(&mut self) { unsafe { - try!(cvt(ffi::PEM_write_bio_PUBKEY(mem_bio.as_ptr(), self.0))); + ffi::EVP_PKEY_free(self.0); } - Ok(mem_bio.get_buf().to_owned()) - } - - pub fn as_ptr(&self) -> *mut ffi::EVP_PKEY { - return self.0; - } - - pub fn public_eq(&self, other: &PKey) -> bool { - unsafe { ffi::EVP_PKEY_cmp(self.0, other.0) == 1 } } } -impl Drop for PKey { - fn drop(&mut self) { +impl Deref for PKey { + type Target = PKeyRef; + + fn deref(&self) -> &PKeyRef { unsafe { - ffi::EVP_PKEY_free(self.0); + PKeyRef::from_ptr(self.0) } } } -- cgit v1.2.3 From f75f82e466993848393c7a26ccb51dc31b4547fe Mon Sep 17 00:00:00 2001 From: Steven Fackler Date: Sun, 30 Oct 2016 16:37:45 -0700 Subject: Rustfmt --- openssl/src/pkey.rs | 4 +--- 1 file changed, 1 insertion(+), 3 deletions(-) (limited to 'openssl/src/pkey.rs') diff --git a/openssl/src/pkey.rs b/openssl/src/pkey.rs index 4cbfcce7..fc48c8b4 100644 --- a/openssl/src/pkey.rs +++ b/openssl/src/pkey.rs @@ -178,9 +178,7 @@ impl Deref for PKey { type Target = PKeyRef; fn deref(&self) -> &PKeyRef { - unsafe { - PKeyRef::from_ptr(self.0) - } + unsafe { PKeyRef::from_ptr(self.0) } } } -- cgit v1.2.3 From 610403a562450099a3744a60c23678ffc3b0dd5f Mon Sep 17 00:00:00 2001 From: Steven Fackler Date: Sun, 30 Oct 2016 16:59:06 -0700 Subject: Add RsaRef --- openssl/src/pkey.rs | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) (limited to 'openssl/src/pkey.rs') diff --git a/openssl/src/pkey.rs b/openssl/src/pkey.rs index fc48c8b4..8e4041b1 100644 --- a/openssl/src/pkey.rs +++ b/openssl/src/pkey.rs @@ -7,7 +7,7 @@ use ffi; use {cvt, cvt_p}; use bio::{MemBio, MemBioSlice}; use dsa::Dsa; -use rsa::Rsa; +use rsa::{Rsa, RsaRef}; use error::ErrorStack; use util::{CallbackState, invoke_passwd_cb}; use opaque::Opaque; @@ -156,7 +156,7 @@ impl PKey { } /// Assign an RSA key to this pkey. - pub fn set_rsa(&mut self, rsa: &Rsa) -> Result<(), ErrorStack> { + pub fn set_rsa(&mut self, rsa: &RsaRef) -> Result<(), ErrorStack> { unsafe { // this needs to be a reference as the set1_RSA ups the reference count let rsa_ptr = rsa.as_ptr(); -- cgit v1.2.3 From f640613863f0b66bc004f9d9d89f73a31701d396 Mon Sep 17 00:00:00 2001 From: Steven Fackler Date: Mon, 31 Oct 2016 20:12:55 -0700 Subject: Update PKey --- openssl/src/pkey.rs | 41 ++++------------------------------------- 1 file changed, 4 insertions(+), 37 deletions(-) (limited to 'openssl/src/pkey.rs') diff --git a/openssl/src/pkey.rs b/openssl/src/pkey.rs index 8e4041b1..2561ab29 100644 --- a/openssl/src/pkey.rs +++ b/openssl/src/pkey.rs @@ -1,7 +1,6 @@ use libc::{c_void, c_char, c_int}; use std::ptr; use std::mem; -use std::ops::Deref; use ffi; use {cvt, cvt_p}; @@ -10,20 +9,11 @@ use dsa::Dsa; use rsa::{Rsa, RsaRef}; use error::ErrorStack; use util::{CallbackState, invoke_passwd_cb}; -use opaque::Opaque; +use types::{OpenSslType, Ref}; -/// A borrowed `PKey`. -pub struct PKeyRef(Opaque); - -impl PKeyRef { - pub unsafe fn from_ptr<'a>(ptr: *mut ffi::EVP_PKEY) -> &'a PKeyRef { - &*(ptr as *mut _) - } - - pub fn as_ptr(&self) -> *mut ffi::EVP_PKEY { - self as *const _ as *mut _ - } +type_!(PKey, ffi::EVP_PKEY, ffi::EVP_PKEY_free); +impl Ref { /// Get a reference to the interal RSA key for direct access to the key components pub fn rsa(&self) -> Result { unsafe { @@ -59,14 +49,11 @@ impl PKeyRef { Ok(mem_bio.get_buf().to_owned()) } - pub fn public_eq(&self, other: &PKeyRef) -> bool { + pub fn public_eq(&self, other: &Ref) -> bool { unsafe { ffi::EVP_PKEY_cmp(self.as_ptr(), other.as_ptr()) == 1 } } } -/// Represents a public key, optionally with a private key attached. -pub struct PKey(*mut ffi::EVP_PKEY); - unsafe impl Send for PKey {} unsafe impl Sync for PKey {} @@ -105,10 +92,6 @@ impl PKey { } } - pub unsafe fn from_ptr(handle: *mut ffi::EVP_PKEY) -> PKey { - PKey(handle) - } - /// Reads private key from PEM, takes ownership of handle pub fn private_key_from_pem(buf: &[u8]) -> Result { ffi::init(); @@ -166,22 +149,6 @@ impl PKey { } } -impl Drop for PKey { - fn drop(&mut self) { - unsafe { - ffi::EVP_PKEY_free(self.0); - } - } -} - -impl Deref for PKey { - type Target = PKeyRef; - - fn deref(&self) -> &PKeyRef { - unsafe { PKeyRef::from_ptr(self.0) } - } -} - #[cfg(test)] mod tests { #[test] -- cgit v1.2.3 From e9d78181c334bb0398f65b30f6165ec1b54fe623 Mon Sep 17 00:00:00 2001 From: Steven Fackler Date: Mon, 31 Oct 2016 20:15:12 -0700 Subject: Update Rsa --- openssl/src/pkey.rs | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) (limited to 'openssl/src/pkey.rs') diff --git a/openssl/src/pkey.rs b/openssl/src/pkey.rs index 2561ab29..6236b642 100644 --- a/openssl/src/pkey.rs +++ b/openssl/src/pkey.rs @@ -6,7 +6,7 @@ use ffi; use {cvt, cvt_p}; use bio::{MemBio, MemBioSlice}; use dsa::Dsa; -use rsa::{Rsa, RsaRef}; +use rsa::Rsa; use error::ErrorStack; use util::{CallbackState, invoke_passwd_cb}; use types::{OpenSslType, Ref}; @@ -139,7 +139,7 @@ impl PKey { } /// Assign an RSA key to this pkey. - pub fn set_rsa(&mut self, rsa: &RsaRef) -> Result<(), ErrorStack> { + pub fn set_rsa(&mut self, rsa: &Ref) -> Result<(), ErrorStack> { unsafe { // this needs to be a reference as the set1_RSA ups the reference count let rsa_ptr = rsa.as_ptr(); -- cgit v1.2.3 From 9ea27c12b9b5d2d723d8686f5bde4de0223cb160 Mon Sep 17 00:00:00 2001 From: Lionel Flandrin Date: Tue, 1 Nov 2016 11:55:02 +0100 Subject: Add method to encode a public key as a DER blob --- openssl/src/pkey.rs | 11 ++++++++++- 1 file changed, 10 insertions(+), 1 deletion(-) (limited to 'openssl/src/pkey.rs') diff --git a/openssl/src/pkey.rs b/openssl/src/pkey.rs index 6236b642..4885ad3c 100644 --- a/openssl/src/pkey.rs +++ b/openssl/src/pkey.rs @@ -40,7 +40,7 @@ impl Ref { Ok(mem_bio.get_buf().to_owned()) } - /// Stores public key as a PEM + /// Encode public key in PEM format pub fn public_key_to_pem(&self) -> Result, ErrorStack> { let mem_bio = try!(MemBio::new()); unsafe { @@ -49,6 +49,15 @@ impl Ref { Ok(mem_bio.get_buf().to_owned()) } + /// Encode public key in DER format + pub fn public_key_to_der(&self) -> Result, ErrorStack> { + let mem_bio = try!(MemBio::new()); + unsafe { + try!(cvt(ffi::i2d_PUBKEY_bio(mem_bio.as_ptr(), self.as_ptr()))); + } + Ok(mem_bio.get_buf().to_owned()) + } + pub fn public_eq(&self, other: &Ref) -> bool { unsafe { ffi::EVP_PKEY_cmp(self.as_ptr(), other.as_ptr()) == 1 } } -- cgit v1.2.3 From 01ae978db0dc8620b2cc754c0d5cf94a68c1f549 Mon Sep 17 00:00:00 2001 From: Steven Fackler Date: Fri, 4 Nov 2016 16:32:20 -0700 Subject: Get rid of Ref There's unfortunately a rustdoc bug that causes all methods implemented for any Ref to be inlined in the deref methods section :( --- openssl/src/pkey.rs | 12 ++++++------ 1 file changed, 6 insertions(+), 6 deletions(-) (limited to 'openssl/src/pkey.rs') diff --git a/openssl/src/pkey.rs b/openssl/src/pkey.rs index 4885ad3c..a1b90a86 100644 --- a/openssl/src/pkey.rs +++ b/openssl/src/pkey.rs @@ -6,14 +6,14 @@ use ffi; use {cvt, cvt_p}; use bio::{MemBio, MemBioSlice}; use dsa::Dsa; -use rsa::Rsa; +use rsa::{Rsa, RsaRef}; use error::ErrorStack; use util::{CallbackState, invoke_passwd_cb}; -use types::{OpenSslType, Ref}; +use types::{OpenSslType, OpenSslTypeRef}; -type_!(PKey, ffi::EVP_PKEY, ffi::EVP_PKEY_free); +type_!(PKey, PKeyRef, ffi::EVP_PKEY, ffi::EVP_PKEY_free); -impl Ref { +impl PKeyRef { /// Get a reference to the interal RSA key for direct access to the key components pub fn rsa(&self) -> Result { unsafe { @@ -58,7 +58,7 @@ impl Ref { Ok(mem_bio.get_buf().to_owned()) } - pub fn public_eq(&self, other: &Ref) -> bool { + pub fn public_eq(&self, other: &PKeyRef) -> bool { unsafe { ffi::EVP_PKEY_cmp(self.as_ptr(), other.as_ptr()) == 1 } } } @@ -148,7 +148,7 @@ impl PKey { } /// Assign an RSA key to this pkey. - pub fn set_rsa(&mut self, rsa: &Ref) -> Result<(), ErrorStack> { + pub fn set_rsa(&mut self, rsa: &RsaRef) -> Result<(), ErrorStack> { unsafe { // this needs to be a reference as the set1_RSA ups the reference count let rsa_ptr = rsa.as_ptr(); -- cgit v1.2.3 From 8ad1e5565b530832a2cf45f3994393c9a77236f0 Mon Sep 17 00:00:00 2001 From: Steven Fackler Date: Sat, 5 Nov 2016 18:49:09 -0700 Subject: Remove set_rsa PKey is reference counted so allowing mutation is unsound --- openssl/src/pkey.rs | 12 +----------- 1 file changed, 1 insertion(+), 11 deletions(-) (limited to 'openssl/src/pkey.rs') diff --git a/openssl/src/pkey.rs b/openssl/src/pkey.rs index a1b90a86..a1ebd695 100644 --- a/openssl/src/pkey.rs +++ b/openssl/src/pkey.rs @@ -6,7 +6,7 @@ use ffi; use {cvt, cvt_p}; use bio::{MemBio, MemBioSlice}; use dsa::Dsa; -use rsa::{Rsa, RsaRef}; +use rsa::Rsa; use error::ErrorStack; use util::{CallbackState, invoke_passwd_cb}; use types::{OpenSslType, OpenSslTypeRef}; @@ -146,16 +146,6 @@ impl PKey { Ok(PKey::from_ptr(evp)) } } - - /// Assign an RSA key to this pkey. - pub fn set_rsa(&mut self, rsa: &RsaRef) -> Result<(), ErrorStack> { - unsafe { - // this needs to be a reference as the set1_RSA ups the reference count - let rsa_ptr = rsa.as_ptr(); - try!(cvt(ffi::EVP_PKEY_set1_RSA(self.0, rsa_ptr))); - Ok(()) - } - } } #[cfg(test)] -- cgit v1.2.3