| 1 |
/* |
| 2 |
* Copyright 2025 The OpenSSL Project Authors. All Rights Reserved. |
| 3 |
* |
| 4 |
* Licensed under the Apache License 2.0 (the "License"). You may not use |
| 5 |
* this file except in compliance with the License. You can obtain a copy |
| 6 |
* in the file LICENSE in the source distribution or at |
| 7 |
* https://www.openssl.org/source/license.html |
| 8 |
*/ |
| 9 |
/* clang-format off */ |
| 10 |
{- |
| 11 |
use OpenSSL::paramnames qw(produce_param_decoder); |
| 12 |
-} |
| 13 |
/* clang-format on */ |
| 14 |
|
| 15 |
#include <string.h> |
| 16 |
#include <openssl/core_dispatch.h> |
| 17 |
#include <openssl/core_names.h> |
| 18 |
#include <openssl/proverr.h> |
| 19 |
#include "crypto/types.h" |
| 20 |
#include "internal/cryptlib.h" |
| 21 |
#include "internal/skey.h" |
| 22 |
#include "prov/provider_ctx.h" |
| 23 |
#include "prov/providercommon.h" |
| 24 |
#include "prov/implementations.h" |
| 25 |
#include "prov/skeymgmt_lcl.h" |
| 26 |
|
| 27 |
void generic_free(void *keydata) |
| 28 |
{ |
| 29 |
PROV_SKEY *generic = keydata; |
| 30 |
|
| 31 |
if (generic == NULL) |
| 32 |
return; |
| 33 |
|
| 34 |
OPENSSL_clear_free(generic->data, generic->length); |
| 35 |
OPENSSL_free(generic); |
| 36 |
} |
| 37 |
|
| 38 |
/* clang-format off */ |
| 39 |
{- produce_param_decoder('generic_skey_import', |
| 40 |
(['OSSL_SKEY_PARAM_RAW_BYTES', 'raw_bytes', 'octet_string'], |
| 41 |
)); -} |
| 42 |
/* clang-format on */ |
| 43 |
|
| 44 |
void *generic_import(void *provctx, int selection, const OSSL_PARAM params[]) |
| 45 |
{ |
| 46 |
OSSL_LIB_CTX *libctx = PROV_LIBCTX_OF(provctx); |
| 47 |
struct generic_skey_import_st p; |
| 48 |
PROV_SKEY *generic = NULL; |
| 49 |
int ok = 0; |
| 50 |
|
| 51 |
if (!ossl_prov_is_running()) |
| 52 |
return NULL; |
| 53 |
|
| 54 |
if ((selection & OSSL_SKEYMGMT_SELECT_SECRET_KEY) == 0) |
| 55 |
return NULL; |
| 56 |
|
| 57 |
if (!generic_skey_import_decoder(params, &p)) |
| 58 |
return NULL; |
| 59 |
|
| 60 |
if (p.raw_bytes == NULL |
| 61 |
|| p.raw_bytes->data_type != OSSL_PARAM_OCTET_STRING) |
| 62 |
return NULL; |
| 63 |
|
| 64 |
generic = OPENSSL_zalloc(sizeof(PROV_SKEY)); |
| 65 |
if (generic == NULL) |
| 66 |
return NULL; |
| 67 |
|
| 68 |
generic->libctx = libctx; |
| 69 |
|
| 70 |
generic->type = SKEY_TYPE_GENERIC; |
| 71 |
|
| 72 |
if ((generic->data = OPENSSL_memdup(p.raw_bytes->data, |
| 73 |
p.raw_bytes->data_size)) |
| 74 |
== NULL) |
| 75 |
goto end; |
| 76 |
generic->length = p.raw_bytes->data_size; |
| 77 |
ok = 1; |
| 78 |
|
| 79 |
end: |
| 80 |
if (ok == 0) { |
| 81 |
generic_free(generic); |
| 82 |
generic = NULL; |
| 83 |
} |
| 84 |
return generic; |
| 85 |
} |
| 86 |
|
| 87 |
const OSSL_PARAM *generic_imp_settable_params(void *provctx) |
| 88 |
{ |
| 89 |
return generic_skey_import_list; |
| 90 |
} |
| 91 |
|
| 92 |
int generic_export(void *keydata, int selection, |
| 93 |
OSSL_CALLBACK *param_callback, void *cbarg) |
| 94 |
{ |
| 95 |
PROV_SKEY *gen = keydata; |
| 96 |
OSSL_PARAM params[2]; |
| 97 |
|
| 98 |
if (!ossl_prov_is_running() || gen == NULL) |
| 99 |
return 0; |
| 100 |
|
| 101 |
/* If we use generic SKEYMGMT as a "base class", we shouldn't check the type */ |
| 102 |
if ((selection & OSSL_SKEYMGMT_SELECT_SECRET_KEY) == 0) |
| 103 |
return 0; |
| 104 |
|
| 105 |
params[0] = OSSL_PARAM_construct_octet_string(OSSL_SKEY_PARAM_RAW_BYTES, |
| 106 |
gen->data, gen->length); |
| 107 |
params[1] = OSSL_PARAM_construct_end(); |
| 108 |
|
| 109 |
return param_callback(params, cbarg); |
| 110 |
} |
| 111 |
|
| 112 |
const OSSL_DISPATCH ossl_generic_skeymgmt_functions[] = { |
| 113 |
{ OSSL_FUNC_SKEYMGMT_FREE, (void (*)(void))generic_free }, |
| 114 |
{ OSSL_FUNC_SKEYMGMT_IMPORT, (void (*)(void))generic_import }, |
| 115 |
{ OSSL_FUNC_SKEYMGMT_EXPORT, (void (*)(void))generic_export }, |
| 116 |
{ OSSL_FUNC_SKEYMGMT_IMP_SETTABLE_PARAMS, |
| 117 |
(void (*)(void))generic_imp_settable_params }, |
| 118 |
OSSL_DISPATCH_END |
| 119 |
}; |