|
1 | 1 | // Copyright 2024 Contributors to the Parsec project. |
2 | 2 | // SPDX-License-Identifier: Apache-2.0 |
3 | 3 |
|
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 | +}; |
5 | 7 | use crate::ParsecProviderContext; |
6 | 8 | use parsec_openssl2::types::VOID_PTR; |
7 | 9 | use parsec_openssl2::*; |
@@ -39,11 +41,29 @@ pub unsafe extern "C" fn parsec_provider_kmgmt_new(provctx: VOID_PTR) -> VOID_PT |
39 | 41 | Arc::into_raw(kmgmt_keyobj_new(context)) as VOID_PTR |
40 | 42 | } |
41 | 43 |
|
| 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 | + |
42 | 57 | pub type KeyMgmtNewPtr = unsafe extern "C" fn(VOID_PTR) -> VOID_PTR; |
| 58 | +pub type KeyMgmtFreePtr = unsafe extern "C" fn(VOID_PTR); |
| 59 | + |
43 | 60 | const OSSL_FUNC_KEYMGMT_NEW_PTR: KeyMgmtNewPtr = parsec_provider_kmgmt_new; |
| 61 | +const OSSL_FUNC_KEYMGMT_FREE_PTR: KeyMgmtFreePtr = parsec_provider_kmgmt_free; |
44 | 62 |
|
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 | +]; |
47 | 67 |
|
48 | 68 | pub const PARSEC_PROVIDER_KEYMGMT: [OSSL_ALGORITHM; 1] = [ossl_algorithm!( |
49 | 69 | PARSEC_PROVIDER_RSA_NAME, |
|
0 commit comments