Merge pull request #1163 from sfackler/pkey-clone

Implement Clone for PKey
This commit is contained in:
Steven Fackler 2019-10-01 22:11:52 -04:00 committed by GitHub
commit 55f1fc5a73
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
3 changed files with 37 additions and 0 deletions

View File

@ -5,6 +5,8 @@ use *;
#[cfg(not(ossl110))]
pub const CRYPTO_LOCK_X509: c_int = 3;
#[cfg(not(ossl110))]
pub const CRYPTO_LOCK_EVP_PKEY: c_int = 10;
#[cfg(not(ossl110))]
pub const CRYPTO_LOCK_SSL_CTX: c_int = 12;
#[cfg(not(ossl110))]
pub const CRYPTO_LOCK_SSL_SESSION: c_int = 14;

View File

@ -304,6 +304,8 @@ extern "C" {
pub fn EVP_PKEY_new() -> *mut EVP_PKEY;
pub fn EVP_PKEY_free(k: *mut EVP_PKEY);
#[cfg(any(ossl110, libressl270))]
pub fn EVP_PKEY_up_ref(pkey: *mut EVP_PKEY) -> c_int;
pub fn d2i_AutoPrivateKey(
a: *mut *mut EVP_PKEY,

View File

@ -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;