Skip to content

Commit 7c7958d

Browse files
authored
Merge pull request #2262 from alex/pkey-api
Switch Pkey::from_ to use set1 functions
2 parents 22ffa9a + d7b12cc commit 7c7958d

File tree

2 files changed

+14
-12
lines changed

2 files changed

+14
-12
lines changed

openssl-sys/src/handwritten/evp.rs

+3
Original file line numberDiff line numberDiff line change
@@ -470,8 +470,11 @@ extern "C" {
470470

471471
pub fn EVP_PKEY_set1_RSA(k: *mut EVP_PKEY, r: *mut RSA) -> c_int;
472472
pub fn EVP_PKEY_get1_RSA(k: *mut EVP_PKEY) -> *mut RSA;
473+
pub fn EVP_PKEY_set1_DSA(k: *mut EVP_PKEY, k: *mut DSA) -> c_int;
473474
pub fn EVP_PKEY_get1_DSA(k: *mut EVP_PKEY) -> *mut DSA;
475+
pub fn EVP_PKEY_set1_DH(k: *mut EVP_PKEY, k: *mut DH) -> c_int;
474476
pub fn EVP_PKEY_get1_DH(k: *mut EVP_PKEY) -> *mut DH;
477+
pub fn EVP_PKEY_set1_EC_KEY(k: *mut EVP_PKEY, k: *mut EC_KEY) -> c_int;
475478
pub fn EVP_PKEY_get1_EC_KEY(k: *mut EVP_PKEY) -> *mut EC_KEY;
476479

477480
pub fn EVP_PKEY_new() -> *mut EVP_PKEY;

openssl/src/pkey.rs

+11-12
Original file line numberDiff line numberDiff line change
@@ -60,6 +60,7 @@ use openssl_macros::corresponds;
6060
use std::convert::{TryFrom, TryInto};
6161
use std::ffi::CString;
6262
use std::fmt;
63+
#[cfg(all(not(boringssl), ossl110))]
6364
use std::mem;
6465
use std::ptr;
6566

@@ -407,38 +408,37 @@ impl<T> Clone for PKey<T> {
407408

408409
impl<T> PKey<T> {
409410
/// Creates a new `PKey` containing an RSA key.
410-
#[corresponds(EVP_PKEY_assign_RSA)]
411+
#[corresponds(EVP_PKEY_set1_RSA)]
411412
pub fn from_rsa(rsa: Rsa<T>) -> Result<PKey<T>, ErrorStack> {
413+
// TODO: Next time we make backwards incompatible changes, this could
414+
// become an `&RsaRef<T>`. Same for all the other `from_*` methods.
412415
unsafe {
413416
let evp = cvt_p(ffi::EVP_PKEY_new())?;
414417
let pkey = PKey::from_ptr(evp);
415-
cvt(ffi::EVP_PKEY_assign_RSA(pkey.0, rsa.as_ptr()))?;
416-
mem::forget(rsa);
418+
cvt(ffi::EVP_PKEY_set1_RSA(pkey.0, rsa.as_ptr()))?;
417419
Ok(pkey)
418420
}
419421
}
420422

421423
/// Creates a new `PKey` containing a DSA key.
422-
#[corresponds(EVP_PKEY_assign_DSA)]
424+
#[corresponds(EVP_PKEY_set1_DSA)]
423425
pub fn from_dsa(dsa: Dsa<T>) -> Result<PKey<T>, ErrorStack> {
424426
unsafe {
425427
let evp = cvt_p(ffi::EVP_PKEY_new())?;
426428
let pkey = PKey::from_ptr(evp);
427-
cvt(ffi::EVP_PKEY_assign_DSA(pkey.0, dsa.as_ptr()))?;
428-
mem::forget(dsa);
429+
cvt(ffi::EVP_PKEY_set1_DSA(pkey.0, dsa.as_ptr()))?;
429430
Ok(pkey)
430431
}
431432
}
432433

433434
/// Creates a new `PKey` containing a Diffie-Hellman key.
434-
#[corresponds(EVP_PKEY_assign_DH)]
435+
#[corresponds(EVP_PKEY_set1_DH)]
435436
#[cfg(not(boringssl))]
436437
pub fn from_dh(dh: Dh<T>) -> Result<PKey<T>, ErrorStack> {
437438
unsafe {
438439
let evp = cvt_p(ffi::EVP_PKEY_new())?;
439440
let pkey = PKey::from_ptr(evp);
440-
cvt(ffi::EVP_PKEY_assign_DH(pkey.0, dh.as_ptr()))?;
441-
mem::forget(dh);
441+
cvt(ffi::EVP_PKEY_set1_DH(pkey.0, dh.as_ptr()))?;
442442
Ok(pkey)
443443
}
444444
}
@@ -460,13 +460,12 @@ impl<T> PKey<T> {
460460
}
461461

462462
/// Creates a new `PKey` containing an elliptic curve key.
463-
#[corresponds(EVP_PKEY_assign_EC_KEY)]
463+
#[corresponds(EVP_PKEY_set1_EC_KEY)]
464464
pub fn from_ec_key(ec_key: EcKey<T>) -> Result<PKey<T>, ErrorStack> {
465465
unsafe {
466466
let evp = cvt_p(ffi::EVP_PKEY_new())?;
467467
let pkey = PKey::from_ptr(evp);
468-
cvt(ffi::EVP_PKEY_assign_EC_KEY(pkey.0, ec_key.as_ptr()))?;
469-
mem::forget(ec_key);
468+
cvt(ffi::EVP_PKEY_set1_EC_KEY(pkey.0, ec_key.as_ptr()))?;
470469
Ok(pkey)
471470
}
472471
}

0 commit comments

Comments
 (0)