Skip to content

Commit 207847c

Browse files
keymgmt: KEYMGMT library <-> provider functions
Start implemententing the keymgmt functions: * parsec_provider_query now returns the function table for OSSL_OP_KEYMGMT * Implement OSSL_FUNC_keymgmt_new, returning a dummy Key Object. Signed-off-by: Tomás González <tomasagustin.gonzalezorlando@arm.com>
1 parent 12edbd7 commit 207847c

File tree

4 files changed

+66
-6
lines changed

4 files changed

+66
-6
lines changed
Lines changed: 53 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,53 @@
1+
// Copyright 2024 Contributors to the Parsec project.
2+
// SPDX-License-Identifier: Apache-2.0
3+
4+
use crate::openssl_binding::{OSSL_ALGORITHM, OSSL_DISPATCH, OSSL_FUNC_KEYMGMT_NEW};
5+
use crate::ParsecProviderContext;
6+
use parsec_openssl2::types::VOID_PTR;
7+
use parsec_openssl2::*;
8+
use std::sync::{Arc, Mutex};
9+
10+
// Parameter names that Providers can define
11+
const PARSEC_PROVIDER_RSA_NAME: &[u8; 4] = b"RSA\0";
12+
const PARSEC_PROVIDER_DESCRIPTION_RSA: &[u8; 11] = b"Parsec RSA\0";
13+
const PARSEC_PROVIDER_DFLT_PROPERTIES: &[u8; 16] = b"provider=parsec\0";
14+
15+
struct ParsecProviderKeyObject {
16+
_provctx: Arc<ParsecProviderContext>,
17+
key_name: Mutex<Option<String>>,
18+
}
19+
20+
fn kmgmt_keyobj_new(provctx: Arc<ParsecProviderContext>) -> Arc<ParsecProviderKeyObject> {
21+
Arc::new(ParsecProviderKeyObject {
22+
_provctx: provctx.clone(),
23+
key_name: None.into(),
24+
})
25+
}
26+
27+
/*
28+
should create a provider side key object. The provider context provctx is passed and may be incorporated
29+
in the key object, but that is not mandatory.
30+
*/
31+
pub unsafe extern "C" fn parsec_provider_kmgmt_new(provctx: VOID_PTR) -> VOID_PTR {
32+
if provctx.is_null() {
33+
return std::ptr::null_mut();
34+
}
35+
let ctx = provctx as *const ParsecProviderContext;
36+
Arc::increment_strong_count(ctx);
37+
let context = Arc::from_raw(ctx);
38+
39+
Arc::into_raw(kmgmt_keyobj_new(context)) as VOID_PTR
40+
}
41+
42+
pub type KeyMgmtNewPtr = unsafe extern "C" fn(VOID_PTR) -> VOID_PTR;
43+
const OSSL_FUNC_KEYMGMT_NEW_PTR: KeyMgmtNewPtr = parsec_provider_kmgmt_new;
44+
45+
const PARSEC_PROVIDER_RSA_KEYMGMT_IMPL: [OSSL_DISPATCH; 1] =
46+
[unsafe { ossl_dispatch!(OSSL_FUNC_KEYMGMT_NEW, OSSL_FUNC_KEYMGMT_NEW_PTR) }];
47+
48+
pub const PARSEC_PROVIDER_KEYMGMT: [OSSL_ALGORITHM; 1] = [ossl_algorithm!(
49+
PARSEC_PROVIDER_RSA_NAME,
50+
PARSEC_PROVIDER_DFLT_PROPERTIES,
51+
PARSEC_PROVIDER_RSA_KEYMGMT_IMPL,
52+
PARSEC_PROVIDER_DESCRIPTION_RSA
53+
)];

parsec-openssl-provider/src/lib.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,6 @@
22
// SPDX-License-Identifier: Apache-2.0
33
#![allow(clippy::missing_safety_doc)]
44

5-
use std::mem;
65
use std::sync::Arc;
76

87
pub use openssl_errors;
@@ -16,6 +15,7 @@ use parsec_openssl2::openssl::error::ErrorStack;
1615
use parsec_openssl2::types::VOID_PTR;
1716
use parsec_openssl2::{openssl_binding, types};
1817

18+
mod keymgmt;
1919
mod provider;
2020
use provider::*;
2121

parsec-openssl-provider/src/provider.rs

Lines changed: 11 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
// Copyright 2023 Contributors to the Parsec project.
22
// SPDX-License-Identifier: Apache-2.0
33

4+
use crate::keymgmt::PARSEC_PROVIDER_KEYMGMT;
45
use parsec_openssl2::{
56
locate_and_set_provider_status_param, locate_and_set_utf8_param, ossl_param, OPENSSL_ERROR,
67
OPENSSL_SUCCESS, OSSL_PROVIDER,
@@ -11,8 +12,9 @@ use parsec_client::BasicClient;
1112
use std::sync::Arc;
1213

1314
use crate::openssl_binding::{
14-
OSSL_ALGORITHM, OSSL_PARAM, OSSL_PARAM_INTEGER, OSSL_PARAM_UTF8_PTR, OSSL_PROV_PARAM_BUILDINFO,
15-
OSSL_PROV_PARAM_NAME, OSSL_PROV_PARAM_STATUS, OSSL_PROV_PARAM_VERSION,
15+
OSSL_ALGORITHM, OSSL_OP_KEYMGMT, OSSL_PARAM, OSSL_PARAM_INTEGER, OSSL_PARAM_UTF8_PTR,
16+
OSSL_PROV_PARAM_BUILDINFO, OSSL_PROV_PARAM_NAME, OSSL_PROV_PARAM_STATUS,
17+
OSSL_PROV_PARAM_VERSION,
1618
};
1719

1820
// Parsec provider parameters
@@ -33,7 +35,7 @@ const PARSEC_PROVIDER_PARAM_TYPES: [OSSL_PARAM; 5] = [
3335
];
3436

3537
pub struct ParsecProviderContext {
36-
client: BasicClient,
38+
pub client: BasicClient,
3739
}
3840

3941
impl ParsecProviderContext {
@@ -101,11 +103,15 @@ pub type ProviderTeardownPtr = unsafe extern "C" fn(provctx: *const OSSL_PROVIDE
101103
// The null provider implementation currently doesn't supply any algorithms to the core
102104
pub unsafe extern "C" fn parsec_provider_query(
103105
_prov: *mut OSSL_PROVIDER,
104-
_operation_id: ::std::os::raw::c_int,
106+
operation_id: ::std::os::raw::c_int,
105107
no_cache: *mut ::std::os::raw::c_int,
106108
) -> *const OSSL_ALGORITHM {
107109
*no_cache = 0;
108-
std::ptr::null_mut()
110+
111+
match operation_id as u32 {
112+
OSSL_OP_KEYMGMT => PARSEC_PROVIDER_KEYMGMT.as_ptr(),
113+
_ => std::ptr::null_mut(),
114+
}
109115
}
110116

111117
// Teardowns the Provider context

parsec-openssl2/src/lib.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@ pub mod types;
88

99
pub use openssl;
1010
pub use openssl2::*;
11+
pub type Openssl2Error = openssl2::Error;
1112

1213
// OpenSSL expects an integer return value of 1 and 0 for success and error
1314
pub const OPENSSL_SUCCESS: std::os::raw::c_int = 1;

0 commit comments

Comments
 (0)