Skip to content

Use the new OpenSSL 3.* API for managing EVP_PKEY objects (RSA, ECDSA) #2380

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 4 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions openssl-sys/build/run_bindgen.rs
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,7 @@ const INCLUDES: &str = "
#endif

#if OPENSSL_VERSION_NUMBER >= 0x30000000
#include <openssl/param_build.h>
#include <openssl/provider.h>
#endif

Expand Down
11 changes: 11 additions & 0 deletions openssl-sys/src/core_dispatch.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
use super::*;
use libc::*;

/* OpenSSL 3.* only */

pub const OSSL_KEYMGMT_SELECT_PRIVATE_KEY: c_int = 0x01;
pub const OSSL_KEYMGMT_SELECT_PUBLIC_KEY: c_int = 0x02;
pub const OSSL_KEYMGMT_SELECT_DOMAIN_PARAMETERS: c_int = 0x04;
pub const OSSL_KEYMGMT_SELECT_OTHER_PARAMETERS: c_int = 0x80;
pub const OSSL_KEYMGMT_SELECT_ALL_PARAMETERS: c_int =
OSSL_KEYMGMT_SELECT_DOMAIN_PARAMETERS | OSSL_KEYMGMT_SELECT_OTHER_PARAMETERS;
9 changes: 9 additions & 0 deletions openssl-sys/src/evp.rs
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,15 @@ pub const EVP_CTRL_GCM_SET_IVLEN: c_int = 0x9;
pub const EVP_CTRL_GCM_GET_TAG: c_int = 0x10;
pub const EVP_CTRL_GCM_SET_TAG: c_int = 0x11;

#[cfg(ossl300)]
pub const EVP_PKEY_KEY_PARAMETERS: c_int = OSSL_KEYMGMT_SELECT_ALL_PARAMETERS;
#[cfg(ossl300)]
pub const EVP_PKEY_PRIVATE_KEY: c_int = EVP_PKEY_KEY_PARAMETERS | OSSL_KEYMGMT_SELECT_PRIVATE_KEY;
#[cfg(ossl300)]
pub const EVP_PKEY_PUBLIC_KEY: c_int = EVP_PKEY_KEY_PARAMETERS | OSSL_KEYMGMT_SELECT_PUBLIC_KEY;
#[cfg(ossl300)]
pub const EVP_PKEY_KEYPAIR: c_int = EVP_PKEY_PUBLIC_KEY | OSSL_KEYMGMT_SELECT_PRIVATE_KEY;

pub unsafe fn EVP_get_digestbynid(type_: c_int) -> *const EVP_MD {
EVP_get_digestbyname(OBJ_nid2sn(type_))
}
Expand Down
27 changes: 27 additions & 0 deletions openssl-sys/src/handwritten/evp.rs
Original file line number Diff line number Diff line change
Expand Up @@ -489,6 +489,27 @@ extern "C" {
#[cfg(any(ossl110, libressl270))]
pub fn EVP_PKEY_up_ref(pkey: *mut EVP_PKEY) -> c_int;

#[cfg(ossl300)]
pub fn EVP_PKEY_fromdata_init(ctx: *mut EVP_PKEY_CTX) -> c_int;

#[cfg(ossl300)]
pub fn EVP_PKEY_fromdata(
ctx: *mut EVP_PKEY_CTX,
ppkey: *mut *mut EVP_PKEY,
selection: c_int,
param: *mut OSSL_PARAM,
) -> c_int;

#[cfg(ossl300)]
pub fn EVP_PKEY_todata(
ppkey: *const EVP_PKEY,
selection: c_int,
param: *mut *mut OSSL_PARAM,
) -> c_int;

#[cfg(ossl300)]
pub fn EVP_PKEY_generate(ctx: *mut EVP_PKEY_CTX, k: *mut *mut EVP_PKEY) -> c_int;

pub fn d2i_AutoPrivateKey(
a: *mut *mut EVP_PKEY,
pp: *mut *const c_uchar,
Expand Down Expand Up @@ -535,6 +556,12 @@ extern "C" {

pub fn EVP_PKEY_CTX_new(k: *mut EVP_PKEY, e: *mut ENGINE) -> *mut EVP_PKEY_CTX;
pub fn EVP_PKEY_CTX_new_id(id: c_int, e: *mut ENGINE) -> *mut EVP_PKEY_CTX;
#[cfg(ossl300)]
pub fn EVP_PKEY_CTX_new_from_name(
libctx: *mut OSSL_LIB_CTX,
name: *const c_char,
propquery: *const c_char,
) -> *mut EVP_PKEY_CTX;
pub fn EVP_PKEY_CTX_free(ctx: *mut EVP_PKEY_CTX);

pub fn EVP_PKEY_CTX_ctrl(
Expand Down
6 changes: 6 additions & 0 deletions openssl-sys/src/handwritten/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,9 @@ pub use self::hmac::*;
pub use self::kdf::*;
pub use self::object::*;
pub use self::ocsp::*;
#[cfg(ossl300)]
pub use self::param_build::*;
#[cfg(ossl300)]
pub use self::params::*;
pub use self::pem::*;
pub use self::pkcs12::*;
Expand Down Expand Up @@ -54,6 +57,9 @@ mod hmac;
mod kdf;
mod object;
mod ocsp;
#[cfg(ossl300)]
mod param_build;
#[cfg(ossl300)]
mod params;
mod pem;
mod pkcs12;
Expand Down
32 changes: 32 additions & 0 deletions openssl-sys/src/handwritten/param_build.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
use super::super::*;
use libc::*;

/* OpenSSL 3.* only */

extern "C" {
pub fn OSSL_PARAM_BLD_new() -> *mut OSSL_PARAM_BLD;
pub fn OSSL_PARAM_BLD_free(bld: *mut OSSL_PARAM_BLD);
pub fn OSSL_PARAM_BLD_push_BN(
bld: *mut OSSL_PARAM_BLD,
key: *const c_char,
bn: *const BIGNUM,
) -> c_int;
pub fn OSSL_PARAM_BLD_push_utf8_string(
bld: *mut OSSL_PARAM_BLD,
key: *const c_char,
buf: *const c_char,
bsize: usize,
) -> c_int;
pub fn OSSL_PARAM_BLD_push_octet_string(
bld: *mut OSSL_PARAM_BLD,
key: *const c_char,
buf: *const c_void,
bsize: usize,
) -> c_int;
pub fn OSSL_PARAM_BLD_push_uint(
bld: *mut OSSL_PARAM_BLD,
key: *const c_char,
buf: c_uint,
) -> c_int;
pub fn OSSL_PARAM_BLD_to_param(bld: *mut OSSL_PARAM_BLD) -> *mut OSSL_PARAM;
}
23 changes: 20 additions & 3 deletions openssl-sys/src/handwritten/params.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,15 +2,32 @@ use super::super::*;
use libc::*;

extern "C" {
#[cfg(ossl300)]
pub fn OSSL_PARAM_free(p: *mut OSSL_PARAM);
pub fn OSSL_PARAM_construct_uint(key: *const c_char, buf: *mut c_uint) -> OSSL_PARAM;
#[cfg(ossl300)]
pub fn OSSL_PARAM_construct_end() -> OSSL_PARAM;
#[cfg(ossl300)]
pub fn OSSL_PARAM_construct_octet_string(
key: *const c_char,
buf: *mut c_void,
bsize: size_t,
) -> OSSL_PARAM;

pub fn OSSL_PARAM_locate(p: *mut OSSL_PARAM, key: *const c_char) -> *mut OSSL_PARAM;
pub fn OSSL_PARAM_get_BN(p: *const OSSL_PARAM, val: *mut *mut BIGNUM) -> c_int;
pub fn OSSL_PARAM_get_utf8_string(
p: *const OSSL_PARAM,
val: *mut *mut c_char,
max_len: usize,
) -> c_int;
pub fn OSSL_PARAM_get_utf8_string_ptr(p: *const OSSL_PARAM, val: *mut *const c_char) -> c_int;
pub fn OSSL_PARAM_get_octet_string(
p: *const OSSL_PARAM,
val: *mut *mut c_void,
max_len: usize,
used_len: *mut usize,
) -> c_int;
pub fn OSSL_PARAM_get_octet_string_ptr(
p: *const OSSL_PARAM,
val: *mut *const c_void,
used_len: *mut usize,
) -> c_int;
}
3 changes: 3 additions & 0 deletions openssl-sys/src/handwritten/types.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1140,6 +1140,9 @@ pub struct OSSL_PARAM {
return_size: size_t,
}

#[cfg(ossl300)]
pub enum OSSL_PARAM_BLD {}

#[cfg(ossl300)]
pub enum EVP_KDF {}
#[cfg(ossl300)]
Expand Down
4 changes: 4 additions & 0 deletions openssl-sys/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,8 @@ mod openssl {
pub use self::bio::*;
pub use self::bn::*;
pub use self::cms::*;
#[cfg(ossl300)]
pub use self::core_dispatch::*;
pub use self::crypto::*;
pub use self::dtls1::*;
pub use self::ec::*;
Expand Down Expand Up @@ -72,6 +74,8 @@ mod openssl {
mod bio;
mod bn;
mod cms;
#[cfg(ossl300)]
mod core_dispatch;
mod crypto;
mod dtls1;
mod ec;
Expand Down
107 changes: 42 additions & 65 deletions openssl/src/kdf.rs
Original file line number Diff line number Diff line change
Expand Up @@ -25,14 +25,23 @@ impl Drop for EvpKdfCtx {
cfg_if::cfg_if! {
if #[cfg(all(ossl320, not(osslconf = "OPENSSL_NO_ARGON2")))] {
use std::cmp;
use std::ffi::c_void;
use std::mem::MaybeUninit;
use std::ptr;
use foreign_types::ForeignTypeRef;
use libc::c_char;
use crate::{cvt, cvt_p};
use crate::lib_ctx::LibCtxRef;
use crate::error::ErrorStack;
use crate::ossl_param::OsslParamBuilder;

const OSSL_KDF_PARAM_PASSWORD: &[u8; 5] = b"pass\0";
const OSSL_KDF_PARAM_SALT: &[u8; 5] = b"salt\0";
const OSSL_KDF_PARAM_SECRET: &[u8; 7] = b"secret\0";
const OSSL_KDF_PARAM_ITER: &[u8; 5] = b"iter\0";
const OSSL_KDF_PARAM_SIZE: &[u8; 5] = b"size\0";
const OSSL_KDF_PARAM_THREADS: &[u8; 8] = b"threads\0";
const OSSL_KDF_PARAM_ARGON2_AD: &[u8; 3] = b"ad\0";
const OSSL_KDF_PARAM_ARGON2_LANES: &[u8; 6] = b"lanes\0";
const OSSL_KDF_PARAM_ARGON2_MEMCOST: &[u8; 8] = b"memcost\0";

/// Derives a key using the argon2id algorithm.
///
Expand All @@ -48,72 +57,40 @@ cfg_if::cfg_if! {
salt: &[u8],
ad: Option<&[u8]>,
secret: Option<&[u8]>,
mut iter: u32,
mut lanes: u32,
mut memcost: u32,
iter: u32,
lanes: u32,
memcost: u32,
out: &mut [u8],
) -> Result<(), ErrorStack> {
unsafe {
let libctx = ctx.map_or(ptr::null_mut(), ForeignTypeRef::as_ptr);
let max_threads = unsafe {
ffi::init();
let libctx = ctx.map_or(ptr::null_mut(), ForeignTypeRef::as_ptr);

let max_threads = ffi::OSSL_get_max_threads(libctx);
let mut threads = 1;
// If max_threads is 0, then this isn't a threaded build.
// If max_threads is > u32::MAX we need to clamp since
// argon2id's threads parameter is a u32.
if max_threads > 0 {
threads = cmp::min(lanes, cmp::min(max_threads, u32::MAX as u64) as u32);
}
let mut params: [ffi::OSSL_PARAM; 10] =
core::array::from_fn(|_| MaybeUninit::<ffi::OSSL_PARAM>::zeroed().assume_init());
let mut idx = 0;
params[idx] = ffi::OSSL_PARAM_construct_octet_string(
b"pass\0".as_ptr() as *const c_char,
pass.as_ptr() as *mut c_void,
pass.len(),
);
idx += 1;
params[idx] = ffi::OSSL_PARAM_construct_octet_string(
b"salt\0".as_ptr() as *const c_char,
salt.as_ptr() as *mut c_void,
salt.len(),
);
idx += 1;
params[idx] =
ffi::OSSL_PARAM_construct_uint(b"threads\0".as_ptr() as *const c_char, &mut threads);
idx += 1;
params[idx] =
ffi::OSSL_PARAM_construct_uint(b"lanes\0".as_ptr() as *const c_char, &mut lanes);
idx += 1;
params[idx] =
ffi::OSSL_PARAM_construct_uint(b"memcost\0".as_ptr() as *const c_char, &mut memcost);
idx += 1;
params[idx] =
ffi::OSSL_PARAM_construct_uint(b"iter\0".as_ptr() as *const c_char, &mut iter);
idx += 1;
let mut size = out.len() as u32;
params[idx] =
ffi::OSSL_PARAM_construct_uint(b"size\0".as_ptr() as *const c_char, &mut size);
idx += 1;
if let Some(ad) = ad {
params[idx] = ffi::OSSL_PARAM_construct_octet_string(
b"ad\0".as_ptr() as *const c_char,
ad.as_ptr() as *mut c_void,
ad.len(),
);
idx += 1;
}
if let Some(secret) = secret {
params[idx] = ffi::OSSL_PARAM_construct_octet_string(
b"secret\0".as_ptr() as *const c_char,
secret.as_ptr() as *mut c_void,
secret.len(),
);
idx += 1;
}
params[idx] = ffi::OSSL_PARAM_construct_end();

ffi::OSSL_get_max_threads(libctx)
};
let mut threads = 1;
// If max_threads is 0, then this isn't a threaded build.
// If max_threads is > u32::MAX we need to clamp since
// argon2id's threads parameter is a u32.
if max_threads > 0 {
threads = cmp::min(lanes, cmp::min(max_threads, u32::MAX as u64) as u32);
}
let bld = OsslParamBuilder::new()?;
bld.add_octet_string(OSSL_KDF_PARAM_PASSWORD, pass)?;
bld.add_octet_string(OSSL_KDF_PARAM_SALT, salt)?;
bld.add_uint(OSSL_KDF_PARAM_THREADS, threads)?;
bld.add_uint(OSSL_KDF_PARAM_ARGON2_LANES, lanes)?;
bld.add_uint(OSSL_KDF_PARAM_ARGON2_MEMCOST, memcost)?;
bld.add_uint(OSSL_KDF_PARAM_ITER, iter)?;
let size = out.len() as u32;
bld.add_uint(OSSL_KDF_PARAM_SIZE, size)?;
if let Some(ad) = ad {
bld.add_octet_string(OSSL_KDF_PARAM_ARGON2_AD, ad)?;
}
if let Some(secret) = secret {
bld.add_octet_string(OSSL_KDF_PARAM_SECRET, secret)?;
}
let params = bld.to_param()?;
unsafe {
let argon2 = EvpKdf(cvt_p(ffi::EVP_KDF_fetch(
libctx,
b"ARGON2ID\0".as_ptr() as *const c_char,
Expand Down
6 changes: 6 additions & 0 deletions openssl/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -177,13 +177,19 @@ pub mod memcmp;
pub mod nid;
#[cfg(not(osslconf = "OPENSSL_NO_OCSP"))]
pub mod ocsp;
#[cfg(ossl300)]
mod ossl_param;
pub mod pkcs12;
pub mod pkcs5;
#[cfg(not(boringssl))]
pub mod pkcs7;
pub mod pkey;
pub mod pkey_ctx;
#[cfg(ossl300)]
pub mod pkey_ecdsa;
#[cfg(ossl300)]
pub mod pkey_rsa;
#[cfg(ossl300)]
pub mod provider;
pub mod rand;
pub mod rsa;
Expand Down
Loading