summaryrefslogtreecommitdiff
path: root/openssl/src/pkey.rs
diff options
context:
space:
mode:
authorSteven Fackler <sfackler@gmail.com>2019-10-01 18:45:10 -0700
committerSteven Fackler <sfackler@gmail.com>2019-10-01 18:45:10 -0700
commit7ce0835b7463016623af8cb98654607512f9680f (patch)
tree787878afe4ae355d83f4ac3e2056bfeb0402fe47 /openssl/src/pkey.rs
parentc295fce45cc66fd475a87ebdf01242e12bf8ba5f (diff)
downloadrust-openssl-7ce0835b7463016623af8cb98654607512f9680f.zip
Implement Clone for PKey
Diffstat (limited to 'openssl/src/pkey.rs')
-rw-r--r--openssl/src/pkey.rs33
1 files changed, 33 insertions, 0 deletions
diff --git a/openssl/src/pkey.rs b/openssl/src/pkey.rs
index f05f5f15..f1ab1e2e 100644
--- a/openssl/src/pkey.rs
+++ b/openssl/src/pkey.rs
@@ -126,6 +126,17 @@ generic_foreign_type_and_impl_send_sync! {
pub struct PKeyRef<T>;
}
+impl<T> ToOwned for PKeyRef<T> {
+ type Owned = PKey<T>;
+
+ fn to_owned(&self) -> PKey<T> {
+ unsafe {
+ EVP_PKEY_up_ref(self.as_ptr());
+ PKey::from_ptr(self.as_ptr())
+ }
+ }
+}
+
impl<T> PKeyRef<T> {
/// Returns a copy of the internal RSA key.
///
@@ -272,6 +283,12 @@ where
}
}
+impl<T> Clone for PKey<T> {
+ fn clone(&self) -> PKey<T> {
+ PKeyRef::to_owned(self)
+ }
+}
+
impl<T> PKey<T> {
/// Creates a new `PKey` containing an RSA key.
///
@@ -584,6 +601,22 @@ impl PKey<Public> {
}
}
+cfg_if! {
+ if #[cfg(any(ossl110, libressl270))] {
+ use ffi::EVP_PKEY_up_ref;
+ } else {
+ unsafe extern "C" fn EVP_PKEY_up_ref(pkey: *mut ffi::EVP_PKEY) {
+ ffi::CRYPTO_add_lock(
+ &mut (*pkey).references,
+ 1,
+ ffi::CRYPTO_LOCK_EVP_PKEY,
+ "pkey.rs\0".as_ptr() as *const _,
+ line!() as c_int,
+ );
+ }
+ }
+}
+
#[cfg(test)]
mod tests {
use dh::Dh;