|  | 
| 1 | 1 | // Copyright 2024 Contributors to the Parsec project. | 
| 2 | 2 | // SPDX-License-Identifier: Apache-2.0 | 
| 3 | 3 | 
 | 
| 4 |  | -use crate::openssl_bindings::OSSL_ALGORITHM; | 
| 5 |  | -use crate::OSSL_DISPATCH; | 
|  | 4 | +use crate::keymgmt::{kmgmt_keyobj_new, ParsecProviderKeyObject}; | 
|  | 5 | +use crate::openssl_bindings::{ | 
|  | 6 | +    OSSL_ALGORITHM, OSSL_DISPATCH, OSSL_FUNC_SIGNATURE_FREECTX, OSSL_FUNC_SIGNATURE_NEWCTX, | 
|  | 7 | +}; | 
|  | 8 | +use crate::ParsecProviderContext; | 
| 6 | 9 | use crate::{ | 
| 7 | 10 |     PARSEC_PROVIDER_DESCRIPTION_ECDSA, PARSEC_PROVIDER_DESCRIPTION_RSA, | 
| 8 | 11 |     PARSEC_PROVIDER_DFLT_PROPERTIES, PARSEC_PROVIDER_ECDSA_NAME, PARSEC_PROVIDER_RSA_NAME, | 
| 9 | 12 | }; | 
|  | 13 | +use parsec_openssl2::types::VOID_PTR; | 
| 10 | 14 | use parsec_openssl2::*; | 
| 11 | 15 | 
 | 
| 12 |  | -const PARSEC_PROVIDER_RSA_SIGN_IMPL: [OSSL_DISPATCH; 1] = [ossl_dispatch!()]; | 
|  | 16 | +use std::sync::Arc; | 
|  | 17 | + | 
|  | 18 | +struct ParsecProviderSignatureContext { | 
|  | 19 | +    _keyobj: Arc<ParsecProviderKeyObject>, | 
|  | 20 | +} | 
|  | 21 | + | 
|  | 22 | +fn kmgmt_signaturecontext_new( | 
|  | 23 | +    provctx: Arc<ParsecProviderContext>, | 
|  | 24 | +) -> Arc<ParsecProviderSignatureContext> { | 
|  | 25 | +    Arc::new(ParsecProviderSignatureContext { | 
|  | 26 | +        _keyobj: kmgmt_keyobj_new(provctx), | 
|  | 27 | +    }) | 
|  | 28 | +} | 
|  | 29 | + | 
|  | 30 | +/* | 
|  | 31 | +Should create and return a pointer to a provider side structure for holding context information during a | 
|  | 32 | +signature operation. A pointer to this context will be passed back in a number of the other signature operation | 
|  | 33 | +function calls. | 
|  | 34 | +The parameter provctx is the provider context generated during provider initialisation. | 
|  | 35 | +The propq parameter is a property query string that may be (optionally) used by the provider during any "fetches" that | 
|  | 36 | +it may perform (if it performs any). | 
|  | 37 | +*/ | 
|  | 38 | +pub unsafe extern "C" fn parsec_provider_signature_newctx( | 
|  | 39 | +    provctx: VOID_PTR, | 
|  | 40 | +    propq: *const std::os::raw::c_char, | 
|  | 41 | +) -> VOID_PTR { | 
|  | 42 | +    if provctx.is_null() || propq.is_null() { | 
|  | 43 | +        return std::ptr::null_mut(); | 
|  | 44 | +    } | 
|  | 45 | + | 
|  | 46 | +    let ctx = provctx as *const ParsecProviderContext; | 
|  | 47 | +    Arc::increment_strong_count(ctx); | 
|  | 48 | +    let context = Arc::from_raw(ctx); | 
|  | 49 | + | 
|  | 50 | +    Arc::into_raw(kmgmt_signaturecontext_new(context)) as VOID_PTR | 
|  | 51 | +} | 
|  | 52 | + | 
|  | 53 | +// should free any resources associated with the provider side signature context | 
|  | 54 | +pub unsafe extern "C" fn parsec_provider_signature_freectx(ctx: VOID_PTR) { | 
|  | 55 | +    if ctx.is_null() { | 
|  | 56 | +        return; | 
|  | 57 | +    } | 
|  | 58 | + | 
|  | 59 | +    let ctx_ptr = ctx as *const ParsecProviderSignatureContext; | 
|  | 60 | +    let arc_ctx = Arc::from_raw(ctx_ptr); | 
|  | 61 | +    // A strong_count of 1 should be guaranteed by OPENSSL, as it doesn't make sense to be calling | 
|  | 62 | +    // free when you are still using the ctx. | 
|  | 63 | +    assert_eq!(1, Arc::strong_count(&arc_ctx)); | 
|  | 64 | +    // When arc_ctx is dropped, the reference count is decremented and the memory is freed | 
|  | 65 | +} | 
|  | 66 | + | 
|  | 67 | +pub type SignatureNewCtxPtr = | 
|  | 68 | +    unsafe extern "C" fn(VOID_PTR, *const std::os::raw::c_char) -> VOID_PTR; | 
|  | 69 | +pub type SignatureFreeCtxPtr = unsafe extern "C" fn(VOID_PTR); | 
|  | 70 | + | 
|  | 71 | +const OSSL_FUNC_SIGNATURE_NEWCTX_PTR: SignatureNewCtxPtr = parsec_provider_signature_newctx; | 
|  | 72 | +const OSSL_FUNC_SIGNATURE_FREECTX_PTR: SignatureFreeCtxPtr = parsec_provider_signature_freectx; | 
|  | 73 | + | 
| 13 | 74 | const PARSEC_PROVIDER_ECDSA_SIGN_IMPL: [OSSL_DISPATCH; 1] = [ossl_dispatch!()]; | 
|  | 75 | +const PARSEC_PROVIDER_RSA_SIGN_IMPL: [OSSL_DISPATCH; 3] = [ | 
|  | 76 | +    unsafe { ossl_dispatch!(OSSL_FUNC_SIGNATURE_NEWCTX, OSSL_FUNC_SIGNATURE_NEWCTX_PTR) }, | 
|  | 77 | +    unsafe { ossl_dispatch!(OSSL_FUNC_SIGNATURE_FREECTX, OSSL_FUNC_SIGNATURE_FREECTX_PTR) }, | 
|  | 78 | +    ossl_dispatch!(), | 
|  | 79 | +]; | 
| 14 | 80 | 
 | 
| 15 | 81 | pub const PARSEC_PROVIDER_SIGNATURE: [OSSL_ALGORITHM; 3] = [ | 
| 16 | 82 |     ossl_algorithm!( | 
|  | 
0 commit comments