Skip to content

Commit 8059af2

Browse files
Add OSSL_FUNC_KEYMGMT_FREE
Signed-off-by: Tomás González <tomasagustin.gonzalezorlando@arm.com>
1 parent 207847c commit 8059af2

File tree

1 file changed

+23
-3
lines changed
  • parsec-openssl-provider/src/keymgmt

1 file changed

+23
-3
lines changed

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

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

4-
use crate::openssl_binding::{OSSL_ALGORITHM, OSSL_DISPATCH, OSSL_FUNC_KEYMGMT_NEW};
4+
use crate::openssl_binding::{
5+
OSSL_ALGORITHM, OSSL_DISPATCH, OSSL_FUNC_KEYMGMT_FREE, OSSL_FUNC_KEYMGMT_NEW,
6+
};
57
use crate::ParsecProviderContext;
68
use parsec_openssl2::types::VOID_PTR;
79
use parsec_openssl2::*;
@@ -39,11 +41,29 @@ pub unsafe extern "C" fn parsec_provider_kmgmt_new(provctx: VOID_PTR) -> VOID_PT
3941
Arc::into_raw(kmgmt_keyobj_new(context)) as VOID_PTR
4042
}
4143

44+
// should free the passed keydata
45+
pub unsafe extern "C" fn parsec_provider_kmgmt_free(keydata: VOID_PTR) {
46+
if keydata.is_null() {
47+
return;
48+
}
49+
let keydata_ptr = keydata as *const ParsecProviderKeyObject;
50+
let arc_keydata = Arc::from_raw(keydata_ptr);
51+
// A strong_count of 1 should be guaranteed by OPENSSL, as it doesn't make sense to be calling
52+
// free when you are still using keydata.
53+
assert_eq!(1, Arc::strong_count(&arc_keydata));
54+
// When arc_keydata is dropped, the reference count is decremented and the memory is freed
55+
}
56+
4257
pub type KeyMgmtNewPtr = unsafe extern "C" fn(VOID_PTR) -> VOID_PTR;
58+
pub type KeyMgmtFreePtr = unsafe extern "C" fn(VOID_PTR);
59+
4360
const OSSL_FUNC_KEYMGMT_NEW_PTR: KeyMgmtNewPtr = parsec_provider_kmgmt_new;
61+
const OSSL_FUNC_KEYMGMT_FREE_PTR: KeyMgmtFreePtr = parsec_provider_kmgmt_free;
4462

45-
const PARSEC_PROVIDER_RSA_KEYMGMT_IMPL: [OSSL_DISPATCH; 1] =
46-
[unsafe { ossl_dispatch!(OSSL_FUNC_KEYMGMT_NEW, OSSL_FUNC_KEYMGMT_NEW_PTR) }];
63+
const PARSEC_PROVIDER_RSA_KEYMGMT_IMPL: [OSSL_DISPATCH; 2] = [
64+
unsafe { ossl_dispatch!(OSSL_FUNC_KEYMGMT_NEW, OSSL_FUNC_KEYMGMT_NEW_PTR) },
65+
unsafe { ossl_dispatch!(OSSL_FUNC_KEYMGMT_FREE, OSSL_FUNC_KEYMGMT_FREE_PTR) },
66+
];
4767

4868
pub const PARSEC_PROVIDER_KEYMGMT: [OSSL_ALGORITHM; 1] = [ossl_algorithm!(
4969
PARSEC_PROVIDER_RSA_NAME,

0 commit comments

Comments
 (0)