aboutsummaryrefslogtreecommitdiff
path: root/openssl/src
diff options
context:
space:
mode:
authorSteven Fackler <[email protected]>2016-11-11 19:04:54 +0000
committerSteven Fackler <[email protected]>2016-11-11 19:04:54 +0000
commit32cbed0782d47b036938316805f63ed6cc2ea759 (patch)
treeca49e38aa5ff92bbecb71f8a0079a31b02f45550 /openssl/src
parentAdd PKey::dsa (diff)
downloadrust-openssl-32cbed0782d47b036938316805f63ed6cc2ea759.tar.xz
rust-openssl-32cbed0782d47b036938316805f63ed6cc2ea759.zip
PKey <-> DH conversions
Closes #498
Diffstat (limited to 'openssl/src')
-rw-r--r--openssl/src/pkey.rs38
1 files changed, 34 insertions, 4 deletions
diff --git a/openssl/src/pkey.rs b/openssl/src/pkey.rs
index 24f4b308..72e73017 100644
--- a/openssl/src/pkey.rs
+++ b/openssl/src/pkey.rs
@@ -5,6 +5,7 @@ use ffi;
use {cvt, cvt_p};
use bio::{MemBio, MemBioSlice};
+use dh::Dh;
use dsa::Dsa;
use rsa::Rsa;
use error::ErrorStack;
@@ -30,6 +31,14 @@ impl PKeyRef {
}
}
+ /// Returns a copy of the internal DH key.
+ pub fn dh(&self) -> Result<Dh, ErrorStack> {
+ unsafe {
+ let dh = try!(cvt_p(ffi::EVP_PKEY_get1_DH(self.as_ptr())));
+ Ok(Dh::from_ptr(dh))
+ }
+ }
+
/// Stores private key as a PEM
// FIXME: also add password and encryption
pub fn private_key_to_pem(&self) -> Result<Vec<u8>, ErrorStack> {
@@ -74,7 +83,7 @@ unsafe impl Send for PKey {}
unsafe impl Sync for PKey {}
impl PKey {
- /// Create a new `PKey` containing an RSA key.
+ /// Creates a new `PKey` containing an RSA key.
pub fn from_rsa(rsa: Rsa) -> Result<PKey, ErrorStack> {
unsafe {
let evp = try!(cvt_p(ffi::EVP_PKEY_new()));
@@ -85,7 +94,7 @@ impl PKey {
}
}
- /// Create a new `PKey` containing a DSA key.
+ /// Creates a new `PKey` containing a DSA key.
pub fn from_dsa(dsa: Dsa) -> Result<PKey, ErrorStack> {
unsafe {
let evp = try!(cvt_p(ffi::EVP_PKEY_new()));
@@ -96,7 +105,18 @@ impl PKey {
}
}
- /// Create a new `PKey` containing an HMAC key.
+ /// Creates a new `PKey` containing a DH key.
+ pub fn from_dh(dh: Dh) -> Result<PKey, ErrorStack> {
+ 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_DH, dh.as_ptr() as *mut _)));
+ mem::forget(dh);
+ Ok(pkey)
+ }
+ }
+
+ /// Creates a new `PKey` containing an HMAC key.
pub fn hmac(key: &[u8]) -> Result<PKey, ErrorStack> {
unsafe {
assert!(key.len() <= c_int::max_value() as usize);
@@ -157,8 +177,9 @@ impl PKey {
#[cfg(test)]
mod tests {
- use rsa::Rsa;
+ use dh::Dh;
use dsa::Dsa;
+ use rsa::Rsa;
use super::*;
@@ -203,4 +224,13 @@ mod tests {
pkey.dsa().unwrap();
assert!(pkey.rsa().is_err());
}
+
+ #[test]
+ fn test_dh_accessor() {
+ let dh = include_bytes!("../test/dhparams.pem");
+ let dh = Dh::from_pem(dh).unwrap();
+ let pkey = PKey::from_dh(dh).unwrap();
+ pkey.dh().unwrap();
+ assert!(pkey.rsa().is_err());
+ }
}