Skip to content

Commit a5ff12e

Browse files
keymgmt: Add Key Objects checks for filled data
Implement: 1. OSSL_FUNC_KEYMGMT_HAS for key objects as indicated by https://www.openssl.org/docs/man3.0/man7/provider-keymgmt.html This currently only checks whether selection is 'OSSL_KEYMGMT_SELECT_OTHER_PARAMETERS' and the key_name has been filled, as currently we don't support any other selection/data in Key Objects. Signed-off-by: Tomás González <tomasagustin.gonzalezorlando@arm.com>
1 parent 5d0bafb commit a5ff12e

File tree

2 files changed

+64
-5
lines changed
  • parsec-openssl-provider-shared/e2e_tests/tests
  • parsec-openssl-provider/src/keymgmt

2 files changed

+64
-5
lines changed
Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
// Copyright 2024 Contributors to the Parsec project.
2+
// SPDX-License-Identifier: Apache-2.0
3+
4+
use e2e_tests::*;
5+
use parsec_openssl_provider::parsec_openssl2::{openssl_bindings, ossl_param};
6+
use std::ffi::CStr;
7+
8+
// Verifies that the provider is able to return a NULL pointer when queried for
9+
// an unsupported function
10+
#[test]
11+
fn test_provider_query_unsupported() {
12+
let provider_path = String::from("../../target/debug");
13+
let provider_name = String::from("libparsec_openssl_provider_shared");
14+
let lib_ctx: LibCtx = LibCtx::new().unwrap();
15+
let provider: Provider = load_provider(&lib_ctx, &provider_name, provider_path);
16+
17+
let mut no_cache: i32 = 0;
18+
unsafe {
19+
assert_eq!(
20+
OSSL_PROVIDER_query_operation(
21+
provider.as_ptr() as _,
22+
OSSL_OP_RAND.try_into().unwrap(),
23+
&mut no_cache as _,
24+
),
25+
std::ptr::null_mut()
26+
);
27+
}
28+
}

parsec-openssl-provider/src/keymgmt/mod.rs

Lines changed: 36 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -2,10 +2,10 @@
22
// SPDX-License-Identifier: Apache-2.0
33

44
use crate::openssl_bindings::{
5-
OSSL_ALGORITHM, OSSL_DISPATCH, OSSL_FUNC_KEYMGMT_FREE, OSSL_FUNC_KEYMGMT_IMPORT,
6-
OSSL_FUNC_KEYMGMT_IMPORT_TYPES, OSSL_FUNC_KEYMGMT_NEW, OSSL_FUNC_KEYMGMT_SETTABLE_PARAMS,
7-
OSSL_FUNC_KEYMGMT_SET_PARAMS, OSSL_FUNC_KEYMGMT_VALIDATE, OSSL_KEYMGMT_SELECT_OTHER_PARAMETERS,
8-
OSSL_PARAM, OSSL_PARAM_UTF8_PTR,
5+
OSSL_ALGORITHM, OSSL_DISPATCH, OSSL_FUNC_KEYMGMT_FREE, OSSL_FUNC_KEYMGMT_HAS,
6+
OSSL_FUNC_KEYMGMT_IMPORT, OSSL_FUNC_KEYMGMT_IMPORT_TYPES, OSSL_FUNC_KEYMGMT_NEW,
7+
OSSL_FUNC_KEYMGMT_SETTABLE_PARAMS, OSSL_FUNC_KEYMGMT_SET_PARAMS, OSSL_FUNC_KEYMGMT_VALIDATE,
8+
OSSL_KEYMGMT_SELECT_OTHER_PARAMETERS, OSSL_PARAM, OSSL_PARAM_UTF8_PTR,
99
};
1010
use crate::{
1111
ParsecProviderContext, PARSEC_PROVIDER_DESCRIPTION_RSA, PARSEC_PROVIDER_DFLT_PROPERTIES,
@@ -110,6 +110,34 @@ pub unsafe extern "C" fn parsec_provider_kmgmt_set_params(
110110
}
111111
}
112112

113+
/*
114+
should return 1 if all the selected data subsets are contained in the given keydata or 0 otherwise.
115+
For algorithms where some selection is not meaningful the function should just return 1 as the
116+
selected subset is not really missing in the key.
117+
*/
118+
pub unsafe extern "C" fn parsec_provider_kmgmt_has(
119+
keydata: VOID_PTR,
120+
selection: std::os::raw::c_int,
121+
) -> std::os::raw::c_int {
122+
if keydata.is_null() {
123+
return OPENSSL_ERROR;
124+
}
125+
126+
if selection & OSSL_KEYMGMT_SELECT_OTHER_PARAMETERS as std::os::raw::c_int != 0 {
127+
let keydata_ptr = keydata as *const ParsecProviderKeyObject;
128+
Arc::increment_strong_count(keydata_ptr);
129+
let arc_keydata = Arc::from_raw(keydata_ptr);
130+
let key_name = arc_keydata.key_name.lock().unwrap();
131+
if key_name.is_some() {
132+
OPENSSL_SUCCESS
133+
} else {
134+
OPENSSL_ERROR
135+
}
136+
} else {
137+
OPENSSL_SUCCESS
138+
}
139+
}
140+
113141
pub unsafe extern "C" fn parsec_provider_kmgmt_import(
114142
key_data: VOID_PTR,
115143
selection: std::os::raw::c_int,
@@ -175,6 +203,7 @@ pub unsafe extern "C" fn parsec_provider_kmgmt_validate(
175203

176204
pub type KeyMgmtNewPtr = unsafe extern "C" fn(VOID_PTR) -> VOID_PTR;
177205
pub type KeyMgmtFreePtr = unsafe extern "C" fn(VOID_PTR);
206+
pub type KeyMgmtHasPtr = unsafe extern "C" fn(VOID_PTR, std::os::raw::c_int) -> std::os::raw::c_int;
178207
pub type KeyMgmtImportPtr =
179208
unsafe extern "C" fn(VOID_PTR, std::os::raw::c_int, *mut OSSL_PARAM) -> std::os::raw::c_int;
180209
pub type KeyMgmtImportTypesPtr = unsafe extern "C" fn(std::os::raw::c_int) -> *const OSSL_PARAM;
@@ -186,6 +215,7 @@ pub type KeyMgmtValidatePtr =
186215

187216
const OSSL_FUNC_KEYMGMT_NEW_PTR: KeyMgmtNewPtr = parsec_provider_kmgmt_new;
188217
const OSSL_FUNC_KEYMGMT_FREE_PTR: KeyMgmtFreePtr = parsec_provider_kmgmt_free;
218+
const OSSL_FUNC_KEYMGMT_HAS_PTR: KeyMgmtHasPtr = parsec_provider_kmgmt_has;
189219
const OSSL_FUNC_KEYMGMT_IMPORT_PTR: KeyMgmtImportPtr = parsec_provider_kmgmt_import;
190220
const OSSL_FUNC_KEYMGMT_IMPORT_TYPES_PTR: KeyMgmtImportTypesPtr =
191221
parsec_provider_kmgmt_import_types;
@@ -194,9 +224,10 @@ const OSSL_FUNC_KEYMGMT_SETTABLE_PARAMS_PTR: KeyMgmtSettableParamsPtr =
194224
parsec_provider_kmgmt_settable_params;
195225
const OSSL_FUNC_KEYMGMT_VALIDATE_PTR: KeyMgmtValidatePtr = parsec_provider_kmgmt_validate;
196226

197-
const PARSEC_PROVIDER_RSA_KEYMGMT_IMPL: [OSSL_DISPATCH; 8] = [
227+
const PARSEC_PROVIDER_RSA_KEYMGMT_IMPL: [OSSL_DISPATCH; 9] = [
198228
unsafe { ossl_dispatch!(OSSL_FUNC_KEYMGMT_NEW, OSSL_FUNC_KEYMGMT_NEW_PTR) },
199229
unsafe { ossl_dispatch!(OSSL_FUNC_KEYMGMT_FREE, OSSL_FUNC_KEYMGMT_FREE_PTR) },
230+
unsafe { ossl_dispatch!(OSSL_FUNC_KEYMGMT_HAS, OSSL_FUNC_KEYMGMT_HAS_PTR) },
200231
unsafe { ossl_dispatch!(OSSL_FUNC_KEYMGMT_IMPORT, OSSL_FUNC_KEYMGMT_IMPORT_PTR) },
201232
unsafe {
202233
ossl_dispatch!(

0 commit comments

Comments
 (0)