/src/openssl36/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 | | /* clang-format off */ |
10 | | |
11 | | /* clang-format on */ |
12 | | |
13 | | #include <string.h> |
14 | | #include <openssl/core_dispatch.h> |
15 | | #include <openssl/core_names.h> |
16 | | #include <openssl/proverr.h> |
17 | | #include "crypto/types.h" |
18 | | #include "internal/cryptlib.h" |
19 | | #include "internal/skey.h" |
20 | | #include "prov/provider_ctx.h" |
21 | | #include "prov/providercommon.h" |
22 | | #include "prov/implementations.h" |
23 | | #include "prov/skeymgmt_lcl.h" |
24 | | |
25 | | void generic_free(void *keydata) |
26 | 0 | { |
27 | 0 | PROV_SKEY *generic = keydata; |
28 | |
|
29 | 0 | if (generic == NULL) |
30 | 0 | return; |
31 | | |
32 | 0 | OPENSSL_clear_free(generic->data, generic->length); |
33 | 0 | OPENSSL_free(generic); |
34 | 0 | } |
35 | | |
36 | | /* clang-format off */ |
37 | | /* Machine generated by util/perl/OpenSSL/paramnames.pm */ |
38 | | #ifndef generic_skey_import_list |
39 | | static const OSSL_PARAM generic_skey_import_list[] = { |
40 | | OSSL_PARAM_octet_string(OSSL_SKEY_PARAM_RAW_BYTES, NULL, 0), |
41 | | OSSL_PARAM_END |
42 | | }; |
43 | | #endif |
44 | | |
45 | | #ifndef generic_skey_import_st |
46 | | struct generic_skey_import_st { |
47 | | OSSL_PARAM *raw_bytes; |
48 | | }; |
49 | | #endif |
50 | | |
51 | | #ifndef generic_skey_import_decoder |
52 | | static int generic_skey_import_decoder |
53 | | (const OSSL_PARAM *p, struct generic_skey_import_st *r) |
54 | 0 | { |
55 | 0 | const char *s; |
56 | |
|
57 | 0 | memset(r, 0, sizeof(*r)); |
58 | 0 | if (p != NULL) |
59 | 0 | for (; (s = p->key) != NULL; p++) |
60 | 0 | if (ossl_likely(strcmp("raw-bytes", s + 0) == 0)) { |
61 | | /* OSSL_SKEY_PARAM_RAW_BYTES */ |
62 | 0 | if (ossl_unlikely(r->raw_bytes != NULL)) { |
63 | 0 | ERR_raise_data(ERR_LIB_PROV, PROV_R_REPEATED_PARAMETER, |
64 | 0 | "param %s is repeated", s); |
65 | 0 | return 0; |
66 | 0 | } |
67 | 0 | r->raw_bytes = (OSSL_PARAM *)p; |
68 | 0 | } |
69 | 0 | return 1; |
70 | 0 | } |
71 | | #endif |
72 | | /* End of machine generated */ |
73 | | /* clang-format on */ |
74 | | |
75 | | void *generic_import(void *provctx, int selection, const OSSL_PARAM params[]) |
76 | 0 | { |
77 | 0 | OSSL_LIB_CTX *libctx = PROV_LIBCTX_OF(provctx); |
78 | 0 | struct generic_skey_import_st p; |
79 | 0 | PROV_SKEY *generic = NULL; |
80 | 0 | int ok = 0; |
81 | |
|
82 | 0 | if (!ossl_prov_is_running()) |
83 | 0 | return NULL; |
84 | | |
85 | 0 | if ((selection & OSSL_SKEYMGMT_SELECT_SECRET_KEY) == 0) |
86 | 0 | return NULL; |
87 | | |
88 | 0 | if (!generic_skey_import_decoder(params, &p)) |
89 | 0 | return NULL; |
90 | | |
91 | 0 | if (p.raw_bytes == NULL |
92 | 0 | || p.raw_bytes->data_type != OSSL_PARAM_OCTET_STRING) |
93 | 0 | return NULL; |
94 | | |
95 | 0 | generic = OPENSSL_zalloc(sizeof(PROV_SKEY)); |
96 | 0 | if (generic == NULL) |
97 | 0 | return NULL; |
98 | | |
99 | 0 | generic->libctx = libctx; |
100 | |
|
101 | 0 | generic->type = SKEY_TYPE_GENERIC; |
102 | |
|
103 | 0 | if ((generic->data = OPENSSL_memdup(p.raw_bytes->data, |
104 | 0 | p.raw_bytes->data_size)) |
105 | 0 | == NULL) |
106 | 0 | goto end; |
107 | 0 | generic->length = p.raw_bytes->data_size; |
108 | 0 | ok = 1; |
109 | |
|
110 | 0 | end: |
111 | 0 | if (ok == 0) { |
112 | 0 | generic_free(generic); |
113 | 0 | generic = NULL; |
114 | 0 | } |
115 | 0 | return generic; |
116 | 0 | } |
117 | | |
118 | | const OSSL_PARAM *generic_imp_settable_params(void *provctx) |
119 | 0 | { |
120 | 0 | return generic_skey_import_list; |
121 | 0 | } |
122 | | |
123 | | int generic_export(void *keydata, int selection, |
124 | | OSSL_CALLBACK *param_callback, void *cbarg) |
125 | 0 | { |
126 | 0 | PROV_SKEY *gen = keydata; |
127 | 0 | OSSL_PARAM params[2]; |
128 | |
|
129 | 0 | if (!ossl_prov_is_running() || gen == NULL) |
130 | 0 | return 0; |
131 | | |
132 | | /* If we use generic SKEYMGMT as a "base class", we shouldn't check the type */ |
133 | 0 | if ((selection & OSSL_SKEYMGMT_SELECT_SECRET_KEY) == 0) |
134 | 0 | return 0; |
135 | | |
136 | 0 | params[0] = OSSL_PARAM_construct_octet_string(OSSL_SKEY_PARAM_RAW_BYTES, |
137 | 0 | gen->data, gen->length); |
138 | 0 | params[1] = OSSL_PARAM_construct_end(); |
139 | |
|
140 | 0 | return param_callback(params, cbarg); |
141 | 0 | } |
142 | | |
143 | | const OSSL_DISPATCH ossl_generic_skeymgmt_functions[] = { |
144 | | { OSSL_FUNC_SKEYMGMT_FREE, (void (*)(void))generic_free }, |
145 | | { OSSL_FUNC_SKEYMGMT_IMPORT, (void (*)(void))generic_import }, |
146 | | { OSSL_FUNC_SKEYMGMT_EXPORT, (void (*)(void))generic_export }, |
147 | | { OSSL_FUNC_SKEYMGMT_IMP_SETTABLE_PARAMS, |
148 | | (void (*)(void))generic_imp_settable_params }, |
149 | | OSSL_DISPATCH_END |
150 | | }; |