Skip to content

Commit 8b3e457

Browse files
committed
Switch Pkey::from_ to use set1 functions
They're more type safe, because they aren't macros, and they slightly simplify reference counting.
1 parent 22ffa9a commit 8b3e457

File tree

2 files changed

+11
-12
lines changed

2 files changed

+11
-12
lines changed

openssl-sys/src/handwritten/evp.rs

Lines changed: 3 additions & 0 deletions
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

Lines changed: 8 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -407,38 +407,35 @@ impl<T> Clone for PKey<T> {
407407

408408
impl<T> PKey<T> {
409409
/// Creates a new `PKey` containing an RSA key.
410-
#[corresponds(EVP_PKEY_assign_RSA)]
410+
#[corresponds(EVP_PKEY_set1_RSA)]
411411
pub fn from_rsa(rsa: Rsa<T>) -> Result<PKey<T>, ErrorStack> {
412412
unsafe {
413413
let evp = cvt_p(ffi::EVP_PKEY_new())?;
414414
let pkey = PKey::from_ptr(evp);
415-
cvt(ffi::EVP_PKEY_assign_RSA(pkey.0, rsa.as_ptr()))?;
416-
mem::forget(rsa);
415+
cvt(ffi::EVP_PKEY_set1_RSA(pkey.0, rsa.as_ptr()))?;
417416
Ok(pkey)
418417
}
419418
}
420419

421420
/// Creates a new `PKey` containing a DSA key.
422-
#[corresponds(EVP_PKEY_assign_DSA)]
421+
#[corresponds(EVP_PKEY_set1_DSA)]
423422
pub fn from_dsa(dsa: Dsa<T>) -> Result<PKey<T>, ErrorStack> {
424423
unsafe {
425424
let evp = cvt_p(ffi::EVP_PKEY_new())?;
426425
let pkey = PKey::from_ptr(evp);
427-
cvt(ffi::EVP_PKEY_assign_DSA(pkey.0, dsa.as_ptr()))?;
428-
mem::forget(dsa);
426+
cvt(ffi::EVP_PKEY_set1_DSA(pkey.0, dsa.as_ptr()))?;
429427
Ok(pkey)
430428
}
431429
}
432430

433431
/// Creates a new `PKey` containing a Diffie-Hellman key.
434-
#[corresponds(EVP_PKEY_assign_DH)]
432+
#[corresponds(EVP_PKEY_set1_DH)]
435433
#[cfg(not(boringssl))]
436434
pub fn from_dh(dh: Dh<T>) -> Result<PKey<T>, ErrorStack> {
437435
unsafe {
438436
let evp = cvt_p(ffi::EVP_PKEY_new())?;
439437
let pkey = PKey::from_ptr(evp);
440-
cvt(ffi::EVP_PKEY_assign_DH(pkey.0, dh.as_ptr()))?;
441-
mem::forget(dh);
438+
cvt(ffi::EVP_PKEY_set1_DH(pkey.0, dh.as_ptr()))?;
442439
Ok(pkey)
443440
}
444441
}
@@ -460,13 +457,12 @@ impl<T> PKey<T> {
460457
}
461458

462459
/// Creates a new `PKey` containing an elliptic curve key.
463-
#[corresponds(EVP_PKEY_assign_EC_KEY)]
460+
#[corresponds(EVP_PKEY_set1_EC_KEY)]
464461
pub fn from_ec_key(ec_key: EcKey<T>) -> Result<PKey<T>, ErrorStack> {
465462
unsafe {
466463
let evp = cvt_p(ffi::EVP_PKEY_new())?;
467464
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);
465+
cvt(ffi::EVP_PKEY_set1_EC_KEY(pkey.0, ec_key.as_ptr()))?;
470466
Ok(pkey)
471467
}
472468
}

0 commit comments

Comments
 (0)