Skip to content

Commit c2f4d58

Browse files
committed
Use type-safe wrappers instead of EVP_PKEY_assign
In OpenSSL, these are macros, so they didn't get imported by bindgen, but they're proper functions in BoringSSL and we'd prefer callers use those for safety. For OpenSSL, just add the corresponding functions in openssl-sys, matching how rust-openssl handles EVP_PKEY_CTX_ctrl. Using the type-safe wrappers flags that rust-openssl was trying to convert DH to EVP_PKEY, but BoringSSL doesn't actually support this. (DH is a legacy primitive, so we haven't routed it to EVP_PKEY right now.)
1 parent a71f492 commit c2f4d58

File tree

2 files changed

+22
-20
lines changed

2 files changed

+22
-20
lines changed

openssl-sys/src/evp.rs

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -285,3 +285,19 @@ pub unsafe fn EVP_PKEY_CTX_add1_hkdf_info(
285285
info as *mut c_void,
286286
)
287287
}
288+
289+
pub unsafe fn EVP_PKEY_assign_RSA(pkey: *mut EVP_PKEY, rsa: *mut RSA) -> c_int {
290+
EVP_PKEY_assign(pkey, EVP_PKEY_RSA, rsa as *mut c_void)
291+
}
292+
293+
pub unsafe fn EVP_PKEY_assign_DSA(pkey: *mut EVP_PKEY, dsa: *mut DSA) -> c_int {
294+
EVP_PKEY_assign(pkey, EVP_PKEY_DSA, dsa as *mut c_void)
295+
}
296+
297+
pub unsafe fn EVP_PKEY_assign_DH(pkey: *mut EVP_PKEY, dh: *mut DH) -> c_int {
298+
EVP_PKEY_assign(pkey, EVP_PKEY_DH, dh as *mut c_void)
299+
}
300+
301+
pub unsafe fn EVP_PKEY_assign_EC_KEY(pkey: *mut EVP_PKEY, ec_key: *mut EC_KEY) -> c_int {
302+
EVP_PKEY_assign(pkey, EVP_PKEY_EC, ec_key as *mut c_void)
303+
}

openssl/src/pkey.rs

Lines changed: 6 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -406,11 +406,7 @@ impl<T> PKey<T> {
406406
unsafe {
407407
let evp = cvt_p(ffi::EVP_PKEY_new())?;
408408
let pkey = PKey::from_ptr(evp);
409-
cvt(ffi::EVP_PKEY_assign(
410-
pkey.0,
411-
ffi::EVP_PKEY_RSA,
412-
rsa.as_ptr() as *mut _,
413-
))?;
409+
cvt(ffi::EVP_PKEY_assign_RSA(pkey.0, rsa.as_ptr()))?;
414410
mem::forget(rsa);
415411
Ok(pkey)
416412
}
@@ -422,27 +418,20 @@ impl<T> PKey<T> {
422418
unsafe {
423419
let evp = cvt_p(ffi::EVP_PKEY_new())?;
424420
let pkey = PKey::from_ptr(evp);
425-
cvt(ffi::EVP_PKEY_assign(
426-
pkey.0,
427-
ffi::EVP_PKEY_DSA,
428-
dsa.as_ptr() as *mut _,
429-
))?;
421+
cvt(ffi::EVP_PKEY_assign_DSA(pkey.0, dsa.as_ptr()))?;
430422
mem::forget(dsa);
431423
Ok(pkey)
432424
}
433425
}
434426

435427
/// Creates a new `PKey` containing a Diffie-Hellman key.
436428
#[corresponds(EVP_PKEY_assign_DH)]
429+
#[cfg(not(boringssl))]
437430
pub fn from_dh(dh: Dh<T>) -> Result<PKey<T>, ErrorStack> {
438431
unsafe {
439432
let evp = cvt_p(ffi::EVP_PKEY_new())?;
440433
let pkey = PKey::from_ptr(evp);
441-
cvt(ffi::EVP_PKEY_assign(
442-
pkey.0,
443-
ffi::EVP_PKEY_DH,
444-
dh.as_ptr() as *mut _,
445-
))?;
434+
cvt(ffi::EVP_PKEY_assign_DH(pkey.0, dh.as_ptr()))?;
446435
mem::forget(dh);
447436
Ok(pkey)
448437
}
@@ -454,11 +443,7 @@ impl<T> PKey<T> {
454443
unsafe {
455444
let evp = cvt_p(ffi::EVP_PKEY_new())?;
456445
let pkey = PKey::from_ptr(evp);
457-
cvt(ffi::EVP_PKEY_assign(
458-
pkey.0,
459-
ffi::EVP_PKEY_EC,
460-
ec_key.as_ptr() as *mut _,
461-
))?;
446+
cvt(ffi::EVP_PKEY_assign_EC_KEY(pkey.0, ec_key.as_ptr()))?;
462447
mem::forget(ec_key);
463448
Ok(pkey)
464449
}
@@ -861,6 +846,7 @@ impl<T> TryFrom<PKey<T>> for Dsa<T> {
861846
}
862847
}
863848

849+
#[cfg(not(boringssl))]
864850
impl<T> TryFrom<Dh<T>> for PKey<T> {
865851
type Error = ErrorStack;
866852

0 commit comments

Comments
 (0)