Skip to content

Commit

Permalink
keymgmt: Implement Key Object Duplication
Browse files Browse the repository at this point in the history
Implement:

 1. OSSL_FUNC_KEYMGMT_DUP

for key objects as indicated by
https://www.openssl.org/docs/man3.0/man7/provider-keymgmt.html

Signed-off-by: Tomás González <[email protected]>
  • Loading branch information
tgonzalezorlandoarm committed Mar 11, 2024
1 parent 4e75a16 commit 360e526
Show file tree
Hide file tree
Showing 2 changed files with 37 additions and 4 deletions.
38 changes: 34 additions & 4 deletions parsec-openssl-provider/src/keymgmt/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,10 @@
// SPDX-License-Identifier: Apache-2.0

use crate::openssl_binding::{
OSSL_ALGORITHM, OSSL_DISPATCH, OSSL_FUNC_KEYMGMT_FREE, OSSL_FUNC_KEYMGMT_IMPORT,
OSSL_FUNC_KEYMGMT_NEW, OSSL_FUNC_KEYMGMT_SETTABLE_PARAMS, OSSL_FUNC_KEYMGMT_SET_PARAMS,
OSSL_KEYMGMT_SELECT_OTHER_PARAMETERS, OSSL_PARAM, OSSL_PARAM_UTF8_PTR,
OSSL_ALGORITHM, OSSL_DISPATCH, OSSL_FUNC_KEYMGMT_DUP, OSSL_FUNC_KEYMGMT_FREE,
OSSL_FUNC_KEYMGMT_IMPORT, OSSL_FUNC_KEYMGMT_NEW, OSSL_FUNC_KEYMGMT_SETTABLE_PARAMS,
OSSL_FUNC_KEYMGMT_SET_PARAMS, OSSL_KEYMGMT_SELECT_OTHER_PARAMETERS, OSSL_PARAM,
OSSL_PARAM_UTF8_PTR,
};
use crate::ParsecProviderContext;
use parsec_openssl2::types::VOID_PTR;
Expand All @@ -22,6 +23,16 @@ struct ParsecProviderKeyObject {
key_name: Mutex<Option<String>>,
}

impl Clone for ParsecProviderKeyObject {
fn clone(&self) -> Self {
let key_name = self.key_name.lock().unwrap();
ParsecProviderKeyObject {
_provctx: self._provctx.clone(),
key_name: Mutex::new(key_name.clone()),
}
}
}

fn kmgmt_keyobj_new(provctx: Arc<ParsecProviderContext>) -> Arc<ParsecProviderKeyObject> {
Arc::new(ParsecProviderKeyObject {
_provctx: provctx.clone(),
Expand Down Expand Up @@ -111,22 +122,40 @@ pub unsafe extern "C" fn parsec_provider_kmgmt_import(
1
}

pub unsafe extern "C" fn parsec_provider_keymgmt_dup(
keydata_from: VOID_PTR,
selection: std::os::raw::c_int,
) -> VOID_PTR {
if selection & OSSL_KEYMGMT_SELECT_OTHER_PARAMETERS as std::os::raw::c_int != 0 {
let keydata_from_ptr = keydata_from as *const ParsecProviderKeyObject;
Arc::increment_strong_count(keydata_from_ptr);
let arc_keydata_from = Arc::from_raw(keydata_from_ptr);

let duplicate: ParsecProviderKeyObject = (*arc_keydata_from).clone();
Arc::into_raw(Arc::new(duplicate)) as VOID_PTR
} else {
std::ptr::null_mut()
}
}

pub type KeyMgmtNewPtr = unsafe extern "C" fn(VOID_PTR) -> VOID_PTR;
pub type KeyMgmtFreePtr = unsafe extern "C" fn(VOID_PTR);
pub type KeyMgmtImportPtr =
unsafe extern "C" fn(VOID_PTR, std::os::raw::c_int, *mut OSSL_PARAM) -> std::os::raw::c_int;
pub type KeyMgmtSetParamsPtr =
unsafe extern "C" fn(VOID_PTR, *mut OSSL_PARAM) -> std::os::raw::c_int;
pub type KeyMgmtSettableParamsPtr = unsafe extern "C" fn(VOID_PTR) -> *const OSSL_PARAM;
pub type KeyMgmtDupPtr = unsafe extern "C" fn(VOID_PTR, std::os::raw::c_int) -> VOID_PTR;

const OSSL_FUNC_KEYMGMT_NEW_PTR: KeyMgmtNewPtr = parsec_provider_kmgmt_new;
const OSSL_FUNC_KEYMGMT_FREE_PTR: KeyMgmtFreePtr = parsec_provider_kmgmt_free;
const OSSL_FUNC_KEYMGMT_IMPORT_PTR: KeyMgmtImportPtr = parsec_provider_kmgmt_import;
const OSSL_FUNC_KEYMGMT_SET_PARAMS_PTR: KeyMgmtSetParamsPtr = parsec_provider_kmgmt_set_params;
const OSSL_FUNC_KEYMGMT_SETTABLE_PARAMS_PTR: KeyMgmtSettableParamsPtr =
parsec_provider_kmgmt_settable_params;
const OSSL_FUNC_KEYMGMT_DUP_PTR: KeyMgmtDupPtr = parsec_provider_keymgmt_dup;

const PARSEC_PROVIDER_RSA_KEYMGMT_IMPL: [OSSL_DISPATCH; 5] = [
const PARSEC_PROVIDER_RSA_KEYMGMT_IMPL: [OSSL_DISPATCH; 6] = [
unsafe { ossl_dispatch!(OSSL_FUNC_KEYMGMT_NEW, OSSL_FUNC_KEYMGMT_NEW_PTR) },
unsafe { ossl_dispatch!(OSSL_FUNC_KEYMGMT_FREE, OSSL_FUNC_KEYMGMT_FREE_PTR) },
unsafe { ossl_dispatch!(OSSL_FUNC_KEYMGMT_IMPORT, OSSL_FUNC_KEYMGMT_IMPORT_PTR) },
Expand All @@ -142,6 +171,7 @@ const PARSEC_PROVIDER_RSA_KEYMGMT_IMPL: [OSSL_DISPATCH; 5] = [
OSSL_FUNC_KEYMGMT_SETTABLE_PARAMS_PTR
)
},
unsafe { ossl_dispatch!(OSSL_FUNC_KEYMGMT_DUP, OSSL_FUNC_KEYMGMT_DUP_PTR) },
];

pub const PARSEC_PROVIDER_KEYMGMT: [OSSL_ALGORITHM; 1] = [ossl_algorithm!(
Expand Down
3 changes: 3 additions & 0 deletions parsec-openssl-sys2/src/c/openssl.h
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,9 @@
/* Basic key object destruction */
# define OSSL_FUNC_KEYMGMT_FREE 10

/* Dup function, constructor */
# define OSSL_FUNC_KEYMGMT_DUP 44

/* Operations */

# define OSSL_OP_KEYMGMT 10
Expand Down

0 comments on commit 360e526

Please sign in to comment.