Skip to content

Commit 2c6a8f7

Browse files
Keymgmt import: Add a key presence check
The kmgmt_import function loads certain data in the keydata. Before doing this, check if the reffered to key is present in the provider. Signed-off-by: Tomás González <tomasagustin.gonzalezorlando@arm.com>
1 parent 707c01e commit 2c6a8f7

File tree

2 files changed

+40
-6
lines changed

2 files changed

+40
-6
lines changed

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

Lines changed: 38 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -146,18 +146,51 @@ pub unsafe extern "C" fn parsec_provider_kmgmt_has(
146146
}
147147
}
148148

149+
/*
150+
should import data indicated by selection into keydata with values taken from the OSSL_PARAM array params
151+
*/
149152
pub unsafe extern "C" fn parsec_provider_kmgmt_import(
150153
key_data: VOID_PTR,
151154
selection: std::os::raw::c_int,
152155
params: *mut OSSL_PARAM,
153156
) -> std::os::raw::c_int {
154-
//TODO: Query the parsec service and get a list of keys, check if the requested import is for a known key and then
155-
// set the parameter
156157
if selection & OSSL_KEYMGMT_SELECT_OTHER_PARAMETERS as std::os::raw::c_int != 0 {
157-
return parsec_provider_kmgmt_set_params(key_data, params);
158-
}
158+
let result = super::r#catch(Some(|| super::Error::PROVIDER_KEYMGMT_IMPORT), || {
159+
let keydata_ptr = key_data as *const ParsecProviderKeyObject;
160+
Arc::increment_strong_count(keydata_ptr);
161+
let arc_keydata = Arc::from_raw(keydata_ptr);
162+
let param: openssl_bindings::OSSL_PARAM =
163+
*openssl_returns_nonnull(openssl_bindings::OSSL_PARAM_locate(
164+
params,
165+
PARSEC_PROVIDER_KEY_NAME.as_ptr() as *const std::os::raw::c_char,
166+
))?;
167+
168+
let key_name = std::str::from_utf8_unchecked(core::slice::from_raw_parts(
169+
param.data as *mut u8,
170+
param.data_size,
171+
));
159172

160-
OPENSSL_SUCCESS
173+
let keys = arc_keydata
174+
.provctx
175+
.get_client()
176+
.list_keys()
177+
.map_err(|_| "Failed to list Parsec Provider's Keys".to_string())?;
178+
179+
if keys.iter().any(|kinfo| kinfo.name == key_name) {
180+
Ok(OPENSSL_SUCCESS)
181+
} else {
182+
Err("Specified Key not found in the Parsec Provider".into())
183+
}
184+
});
185+
match result {
186+
// Right now, settable params are the same as the import types, so it's ok to use this
187+
// function
188+
Ok(_) => parsec_provider_kmgmt_set_params(key_data, params),
189+
Err(()) => OPENSSL_ERROR,
190+
}
191+
} else {
192+
OPENSSL_SUCCESS
193+
}
161194
}
162195

163196
/*

parsec-openssl-provider/src/lib.rs

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -112,11 +112,12 @@ openssl_errors::openssl_errors! {
112112
PROVIDER_GETTABLE_PARAMS("parsec_provider_gettable_params");
113113
PROVIDER_GET_PARAMS("parsec_provider_get_params");
114114
PROVIDER_KEYMGMT_HAS("parsec_provider_kmgmt_has");
115+
PROVIDER_KEYMGMT_IMPORT("parsec_provider_kmgmt_import");
115116
PROVIDER_KEYMGMT_MATCH("parsec_provider_kmgmt_match");
116117
PROVIDER_KEYMGMT_SET_PARAMS("parsec_provider_kmgmt_set_params");
118+
PROVIDER_KEYMGMT_VALIDATE("parsec_provider_kmgmt_validate");
117119
PROVIDER_QUERY("parsec_provider_query");
118120
PROVIDER_TEARDOWN("parsec_provider_teardown");
119-
PROVIDER_KEYMGMT_VALIDATE("parsec_provider_kmgmt_validate");
120121
}
121122

122123
reasons {

0 commit comments

Comments
 (0)