/src/openssl/providers/implementations/skeymgmt/generic.c
Line  | Count  | Source  | 
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  |  |  | 
10  |  | #include <string.h>  | 
11  |  | #include <openssl/core_dispatch.h>  | 
12  |  | #include <openssl/core_names.h>  | 
13  |  | #include <openssl/proverr.h>  | 
14  |  | #include "crypto/types.h"  | 
15  |  | #include "internal/cryptlib.h"  | 
16  |  | #include "internal/skey.h"  | 
17  |  | #include "prov/provider_ctx.h"  | 
18  |  | #include "prov/providercommon.h"  | 
19  |  | #include "prov/implementations.h"  | 
20  |  | #include "prov/skeymgmt_lcl.h"  | 
21  |  |  | 
22  |  | #include "providers/implementations/skeymgmt/generic.inc"  | 
23  |  |  | 
24  |  | void generic_free(void *keydata)  | 
25  | 0  | { | 
26  | 0  |     PROV_SKEY *generic = keydata;  | 
27  |  | 
  | 
28  | 0  |     if (generic == NULL)  | 
29  | 0  |         return;  | 
30  |  |  | 
31  | 0  |     OPENSSL_clear_free(generic->data, generic->length);  | 
32  | 0  |     OPENSSL_free(generic);  | 
33  | 0  | }  | 
34  |  |  | 
35  |  | void *generic_import(void *provctx, int selection, const OSSL_PARAM params[])  | 
36  | 0  | { | 
37  | 0  |     OSSL_LIB_CTX *libctx = PROV_LIBCTX_OF(provctx);  | 
38  | 0  |     struct generic_skey_import_st p;  | 
39  | 0  |     PROV_SKEY *generic = NULL;  | 
40  | 0  |     int ok = 0;  | 
41  |  | 
  | 
42  | 0  |     if (!ossl_prov_is_running())  | 
43  | 0  |         return NULL;  | 
44  |  |  | 
45  | 0  |     if ((selection & OSSL_SKEYMGMT_SELECT_SECRET_KEY) == 0)  | 
46  | 0  |         return NULL;  | 
47  |  |  | 
48  | 0  |     if (!generic_skey_import_decoder(params, &p))  | 
49  | 0  |         return NULL;  | 
50  |  |  | 
51  | 0  |     if (p.raw_bytes == NULL  | 
52  | 0  |             || p.raw_bytes->data_type != OSSL_PARAM_OCTET_STRING)  | 
53  | 0  |         return NULL;  | 
54  |  |  | 
55  | 0  |     generic = OPENSSL_zalloc(sizeof(PROV_SKEY));  | 
56  | 0  |     if (generic == NULL)  | 
57  | 0  |         return NULL;  | 
58  |  |  | 
59  | 0  |     generic->libctx = libctx;  | 
60  |  | 
  | 
61  | 0  |     generic->type = SKEY_TYPE_GENERIC;  | 
62  |  | 
  | 
63  | 0  |     if ((generic->data = OPENSSL_memdup(p.raw_bytes->data,  | 
64  | 0  |                                         p.raw_bytes->data_size)) == NULL)  | 
65  | 0  |         goto end;  | 
66  | 0  |     generic->length = p.raw_bytes->data_size;  | 
67  | 0  |     ok = 1;  | 
68  |  | 
  | 
69  | 0  | end:  | 
70  | 0  |     if (ok == 0) { | 
71  | 0  |         generic_free(generic);  | 
72  | 0  |         generic = NULL;  | 
73  | 0  |     }  | 
74  | 0  |     return generic;  | 
75  | 0  | }  | 
76  |  |  | 
77  |  | const OSSL_PARAM *generic_imp_settable_params(void *provctx)  | 
78  | 0  | { | 
79  | 0  |     return generic_skey_import_list;  | 
80  | 0  | }  | 
81  |  |  | 
82  |  | int generic_export(void *keydata, int selection,  | 
83  |  |                    OSSL_CALLBACK *param_callback, void *cbarg)  | 
84  | 0  | { | 
85  | 0  |     PROV_SKEY *gen = keydata;  | 
86  | 0  |     OSSL_PARAM params[2];  | 
87  |  | 
  | 
88  | 0  |     if (!ossl_prov_is_running() || gen == NULL)  | 
89  | 0  |         return 0;  | 
90  |  |  | 
91  |  |     /* If we use generic SKEYMGMT as a "base class", we shouldn't check the type */  | 
92  | 0  |     if ((selection & OSSL_SKEYMGMT_SELECT_SECRET_KEY) == 0)  | 
93  | 0  |         return 0;  | 
94  |  |  | 
95  | 0  |     params[0] = OSSL_PARAM_construct_octet_string(OSSL_SKEY_PARAM_RAW_BYTES,  | 
96  | 0  |                                                   gen->data, gen->length);  | 
97  | 0  |     params[1] = OSSL_PARAM_construct_end();  | 
98  |  | 
  | 
99  | 0  |     return param_callback(params, cbarg);  | 
100  | 0  | }  | 
101  |  |  | 
102  |  | const OSSL_DISPATCH ossl_generic_skeymgmt_functions[] = { | 
103  |  |     { OSSL_FUNC_SKEYMGMT_FREE, (void (*)(void))generic_free }, | 
104  |  |     { OSSL_FUNC_SKEYMGMT_IMPORT, (void (*)(void))generic_import }, | 
105  |  |     { OSSL_FUNC_SKEYMGMT_EXPORT, (void (*)(void))generic_export }, | 
106  |  |     { OSSL_FUNC_SKEYMGMT_IMP_SETTABLE_PARAMS, | 
107  |  |       (void (*)(void))generic_imp_settable_params },  | 
108  |  |     OSSL_DISPATCH_END  | 
109  |  | };  |