Skip to content

Commit a7d1739

Browse files
committed
New OpenSSL 3.* API for managing EVP_PKEY objects
The OpenSSL 3.* users now do not have a way to use non-deprecated API by using this rust bindings, which is not sustainable in the long term as either distributions will stop building with the deprecated API or it will be eventually removed. This is now mostly PoC on using RSA and ECDSA keys using the new API in tests. It does not expose all possible API that are available as I did not have a good way to test the unused API yet. I do not know if this API is available in some other *SSL libraries right now so for now all of the additions are marked with #[cfg(ossl300)]. This is partially based on sfackler#2051 which was abandoned. Fixes: sfackler#2047
1 parent acd312c commit a7d1739

File tree

14 files changed

+517
-5
lines changed

14 files changed

+517
-5
lines changed

openssl-sys/build/run_bindgen.rs

+1
Original file line numberDiff line numberDiff line change
@@ -53,6 +53,7 @@ const INCLUDES: &str = "
5353
#endif
5454
5555
#if OPENSSL_VERSION_NUMBER >= 0x30000000
56+
#include <openssl/param_build.h>
5657
#include <openssl/provider.h>
5758
#endif
5859

openssl-sys/src/core_dispatch.rs

+11
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
use super::*;
2+
use libc::*;
3+
4+
/* OpenSSL 3.* only */
5+
6+
pub const OSSL_KEYMGMT_SELECT_PRIVATE_KEY: c_int = 0x01;
7+
pub const OSSL_KEYMGMT_SELECT_PUBLIC_KEY: c_int = 0x02;
8+
pub const OSSL_KEYMGMT_SELECT_DOMAIN_PARAMETERS: c_int = 0x04;
9+
pub const OSSL_KEYMGMT_SELECT_OTHER_PARAMETERS: c_int = 0x80;
10+
pub const OSSL_KEYMGMT_SELECT_ALL_PARAMETERS: c_int =
11+
OSSL_KEYMGMT_SELECT_DOMAIN_PARAMETERS | OSSL_KEYMGMT_SELECT_OTHER_PARAMETERS;

openssl-sys/src/evp.rs

+9
Original file line numberDiff line numberDiff line change
@@ -38,6 +38,15 @@ pub const EVP_CTRL_GCM_SET_IVLEN: c_int = 0x9;
3838
pub const EVP_CTRL_GCM_GET_TAG: c_int = 0x10;
3939
pub const EVP_CTRL_GCM_SET_TAG: c_int = 0x11;
4040

41+
#[cfg(ossl300)]
42+
pub const EVP_PKEY_KEY_PARAMETERS: c_int = OSSL_KEYMGMT_SELECT_ALL_PARAMETERS;
43+
#[cfg(ossl300)]
44+
pub const EVP_PKEY_PRIVATE_KEY: c_int = EVP_PKEY_KEY_PARAMETERS | OSSL_KEYMGMT_SELECT_PRIVATE_KEY;
45+
#[cfg(ossl300)]
46+
pub const EVP_PKEY_PUBLIC_KEY: c_int = EVP_PKEY_KEY_PARAMETERS | OSSL_KEYMGMT_SELECT_PUBLIC_KEY;
47+
#[cfg(ossl300)]
48+
pub const EVP_PKEY_KEYPAIR: c_int = EVP_PKEY_PUBLIC_KEY | OSSL_KEYMGMT_SELECT_PRIVATE_KEY;
49+
4150
pub unsafe fn EVP_get_digestbynid(type_: c_int) -> *const EVP_MD {
4251
EVP_get_digestbyname(OBJ_nid2sn(type_))
4352
}

openssl-sys/src/handwritten/evp.rs

+31
Original file line numberDiff line numberDiff line change
@@ -489,6 +489,31 @@ extern "C" {
489489
#[cfg(any(ossl110, libressl270))]
490490
pub fn EVP_PKEY_up_ref(pkey: *mut EVP_PKEY) -> c_int;
491491

492+
#[cfg(ossl300)]
493+
pub fn EVP_PKEY_fromdata_init(ctx: *mut EVP_PKEY_CTX) -> c_int;
494+
495+
#[cfg(ossl300)]
496+
pub fn EVP_PKEY_fromdata(
497+
ctx: *mut EVP_PKEY_CTX,
498+
ppkey: *mut *mut EVP_PKEY,
499+
selection: c_int,
500+
param: *mut OSSL_PARAM,
501+
) -> c_int;
502+
503+
#[cfg(ossl300)]
504+
pub fn EVP_PKEY_todata(
505+
ppkey: *const EVP_PKEY,
506+
selection: c_int,
507+
param: *mut *mut OSSL_PARAM,
508+
) -> c_int;
509+
510+
#[cfg(ossl300)]
511+
pub fn EVP_PKEY_set_bn_param(
512+
k: *mut EVP_PKEY,
513+
key_name: *const c_char,
514+
bn: *const BIGNUM,
515+
) -> c_int;
516+
492517
pub fn d2i_AutoPrivateKey(
493518
a: *mut *mut EVP_PKEY,
494519
pp: *mut *const c_uchar,
@@ -535,6 +560,12 @@ extern "C" {
535560

536561
pub fn EVP_PKEY_CTX_new(k: *mut EVP_PKEY, e: *mut ENGINE) -> *mut EVP_PKEY_CTX;
537562
pub fn EVP_PKEY_CTX_new_id(id: c_int, e: *mut ENGINE) -> *mut EVP_PKEY_CTX;
563+
#[cfg(ossl300)]
564+
pub fn EVP_PKEY_CTX_new_from_name(
565+
libctx: *mut OSSL_LIB_CTX,
566+
name: *const c_char,
567+
propquery: *const c_char,
568+
) -> *mut EVP_PKEY_CTX;
538569
pub fn EVP_PKEY_CTX_free(ctx: *mut EVP_PKEY_CTX);
539570

540571
pub fn EVP_PKEY_CTX_ctrl(

openssl-sys/src/handwritten/mod.rs

+6
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,9 @@ pub use self::hmac::*;
1515
pub use self::kdf::*;
1616
pub use self::object::*;
1717
pub use self::ocsp::*;
18+
#[cfg(ossl300)]
19+
pub use self::param_build::*;
20+
#[cfg(ossl300)]
1821
pub use self::params::*;
1922
pub use self::pem::*;
2023
pub use self::pkcs12::*;
@@ -54,6 +57,9 @@ mod hmac;
5457
mod kdf;
5558
mod object;
5659
mod ocsp;
60+
#[cfg(ossl300)]
61+
mod param_build;
62+
#[cfg(ossl300)]
5763
mod params;
5864
mod pem;
5965
mod pkcs12;
+27
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
use super::super::*;
2+
use libc::*;
3+
4+
/* OpenSSL 3.* only */
5+
6+
extern "C" {
7+
pub fn OSSL_PARAM_BLD_new() -> *mut OSSL_PARAM_BLD;
8+
pub fn OSSL_PARAM_BLD_free(bld: *mut OSSL_PARAM_BLD);
9+
pub fn OSSL_PARAM_BLD_push_BN(
10+
bld: *mut OSSL_PARAM_BLD,
11+
key: *const c_char,
12+
bn: *const BIGNUM,
13+
) -> c_int;
14+
pub fn OSSL_PARAM_BLD_push_utf8_string(
15+
bld: *mut OSSL_PARAM_BLD,
16+
key: *const c_char,
17+
buf: *const c_char,
18+
bsize: usize,
19+
) -> c_int;
20+
pub fn OSSL_PARAM_BLD_push_octet_string(
21+
bld: *mut OSSL_PARAM_BLD,
22+
key: *const c_char,
23+
buf: *const c_void,
24+
bsize: usize,
25+
) -> c_int;
26+
pub fn OSSL_PARAM_BLD_to_param(bld: *mut OSSL_PARAM_BLD) -> *mut OSSL_PARAM;
27+
}

openssl-sys/src/handwritten/params.rs

+20-3
Original file line numberDiff line numberDiff line change
@@ -2,15 +2,32 @@ use super::super::*;
22
use libc::*;
33

44
extern "C" {
5-
#[cfg(ossl300)]
5+
pub fn OSSL_PARAM_free(p: *mut OSSL_PARAM);
66
pub fn OSSL_PARAM_construct_uint(key: *const c_char, buf: *mut c_uint) -> OSSL_PARAM;
7-
#[cfg(ossl300)]
87
pub fn OSSL_PARAM_construct_end() -> OSSL_PARAM;
9-
#[cfg(ossl300)]
108
pub fn OSSL_PARAM_construct_octet_string(
119
key: *const c_char,
1210
buf: *mut c_void,
1311
bsize: size_t,
1412
) -> OSSL_PARAM;
1513

14+
pub fn OSSL_PARAM_locate(p: *mut OSSL_PARAM, key: *const c_char) -> *mut OSSL_PARAM;
15+
pub fn OSSL_PARAM_get_BN(p: *const OSSL_PARAM, val: *mut *mut BIGNUM) -> c_int;
16+
pub fn OSSL_PARAM_get_utf8_string(
17+
p: *const OSSL_PARAM,
18+
val: *mut *mut c_char,
19+
max_len: usize,
20+
) -> c_int;
21+
pub fn OSSL_PARAM_get_utf8_string_ptr(p: *const OSSL_PARAM, val: *mut *const c_char) -> c_int;
22+
pub fn OSSL_PARAM_get_octet_string(
23+
p: *const OSSL_PARAM,
24+
val: *mut *mut c_void,
25+
max_len: usize,
26+
used_len: *mut usize,
27+
) -> c_int;
28+
pub fn OSSL_PARAM_get_octet_string_ptr(
29+
p: *const OSSL_PARAM,
30+
val: *mut *const c_void,
31+
used_len: *mut usize,
32+
) -> c_int;
1633
}

openssl-sys/src/handwritten/types.rs

+3
Original file line numberDiff line numberDiff line change
@@ -1140,6 +1140,9 @@ pub struct OSSL_PARAM {
11401140
return_size: size_t,
11411141
}
11421142

1143+
#[cfg(ossl300)]
1144+
pub enum OSSL_PARAM_BLD {}
1145+
11431146
#[cfg(ossl300)]
11441147
pub enum EVP_KDF {}
11451148
#[cfg(ossl300)]

openssl-sys/src/lib.rs

+4
Original file line numberDiff line numberDiff line change
@@ -45,6 +45,8 @@ mod openssl {
4545
pub use self::bio::*;
4646
pub use self::bn::*;
4747
pub use self::cms::*;
48+
#[cfg(ossl300)]
49+
pub use self::core_dispatch::*;
4850
pub use self::crypto::*;
4951
pub use self::dtls1::*;
5052
pub use self::ec::*;
@@ -75,6 +77,8 @@ mod openssl {
7577
mod bio;
7678
mod bn;
7779
mod cms;
80+
#[cfg(ossl300)]
81+
mod core_dispatch;
7882
mod crypto;
7983
mod dtls1;
8084
mod ec;

openssl/src/lib.rs

+2
Original file line numberDiff line numberDiff line change
@@ -177,6 +177,8 @@ pub mod memcmp;
177177
pub mod nid;
178178
#[cfg(not(osslconf = "OPENSSL_NO_OCSP"))]
179179
pub mod ocsp;
180+
#[cfg(ossl300)]
181+
pub mod ossl_param;
180182
pub mod pkcs12;
181183
pub mod pkcs5;
182184
#[cfg(not(boringssl))]

0 commit comments

Comments
 (0)