1 | /*
|
---|
2 | * Copyright 2020-2022 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 | /* We need to use some engine deprecated APIs */
|
---|
11 | #define OPENSSL_SUPPRESS_DEPRECATED
|
---|
12 |
|
---|
13 | #include <string.h>
|
---|
14 | #include <openssl/core_dispatch.h>
|
---|
15 | #include <openssl/core_names.h>
|
---|
16 | #include <openssl/params.h>
|
---|
17 | #include <openssl/err.h>
|
---|
18 | #include <openssl/evp.h>
|
---|
19 | #include <openssl/proverr.h>
|
---|
20 | #include <openssl/param_build.h>
|
---|
21 | #ifndef FIPS_MODULE
|
---|
22 | # include <openssl/engine.h>
|
---|
23 | #endif
|
---|
24 | #include "internal/param_build_set.h"
|
---|
25 | #include "prov/implementations.h"
|
---|
26 | #include "prov/providercommon.h"
|
---|
27 | #include "prov/provider_ctx.h"
|
---|
28 | #include "prov/macsignature.h"
|
---|
29 |
|
---|
30 | static OSSL_FUNC_keymgmt_new_fn mac_new;
|
---|
31 | static OSSL_FUNC_keymgmt_free_fn mac_free;
|
---|
32 | static OSSL_FUNC_keymgmt_gen_init_fn mac_gen_init;
|
---|
33 | static OSSL_FUNC_keymgmt_gen_fn mac_gen;
|
---|
34 | static OSSL_FUNC_keymgmt_gen_cleanup_fn mac_gen_cleanup;
|
---|
35 | static OSSL_FUNC_keymgmt_gen_set_params_fn mac_gen_set_params;
|
---|
36 | static OSSL_FUNC_keymgmt_gen_settable_params_fn mac_gen_settable_params;
|
---|
37 | static OSSL_FUNC_keymgmt_get_params_fn mac_get_params;
|
---|
38 | static OSSL_FUNC_keymgmt_gettable_params_fn mac_gettable_params;
|
---|
39 | static OSSL_FUNC_keymgmt_set_params_fn mac_set_params;
|
---|
40 | static OSSL_FUNC_keymgmt_settable_params_fn mac_settable_params;
|
---|
41 | static OSSL_FUNC_keymgmt_has_fn mac_has;
|
---|
42 | static OSSL_FUNC_keymgmt_match_fn mac_match;
|
---|
43 | static OSSL_FUNC_keymgmt_import_fn mac_import;
|
---|
44 | static OSSL_FUNC_keymgmt_import_types_fn mac_imexport_types;
|
---|
45 | static OSSL_FUNC_keymgmt_export_fn mac_export;
|
---|
46 | static OSSL_FUNC_keymgmt_export_types_fn mac_imexport_types;
|
---|
47 |
|
---|
48 | static OSSL_FUNC_keymgmt_new_fn mac_new_cmac;
|
---|
49 | static OSSL_FUNC_keymgmt_gettable_params_fn cmac_gettable_params;
|
---|
50 | static OSSL_FUNC_keymgmt_import_types_fn cmac_imexport_types;
|
---|
51 | static OSSL_FUNC_keymgmt_export_types_fn cmac_imexport_types;
|
---|
52 | static OSSL_FUNC_keymgmt_gen_init_fn cmac_gen_init;
|
---|
53 | static OSSL_FUNC_keymgmt_gen_set_params_fn cmac_gen_set_params;
|
---|
54 | static OSSL_FUNC_keymgmt_gen_settable_params_fn cmac_gen_settable_params;
|
---|
55 |
|
---|
56 | struct mac_gen_ctx {
|
---|
57 | OSSL_LIB_CTX *libctx;
|
---|
58 | int selection;
|
---|
59 | unsigned char *priv_key;
|
---|
60 | size_t priv_key_len;
|
---|
61 | PROV_CIPHER cipher;
|
---|
62 | };
|
---|
63 |
|
---|
64 | MAC_KEY *ossl_mac_key_new(OSSL_LIB_CTX *libctx, int cmac)
|
---|
65 | {
|
---|
66 | MAC_KEY *mackey;
|
---|
67 |
|
---|
68 | if (!ossl_prov_is_running())
|
---|
69 | return NULL;
|
---|
70 |
|
---|
71 | mackey = OPENSSL_zalloc(sizeof(*mackey));
|
---|
72 | if (mackey == NULL)
|
---|
73 | return NULL;
|
---|
74 |
|
---|
75 | mackey->lock = CRYPTO_THREAD_lock_new();
|
---|
76 | if (mackey->lock == NULL) {
|
---|
77 | OPENSSL_free(mackey);
|
---|
78 | return NULL;
|
---|
79 | }
|
---|
80 | mackey->libctx = libctx;
|
---|
81 | mackey->refcnt = 1;
|
---|
82 | mackey->cmac = cmac;
|
---|
83 |
|
---|
84 | return mackey;
|
---|
85 | }
|
---|
86 |
|
---|
87 | void ossl_mac_key_free(MAC_KEY *mackey)
|
---|
88 | {
|
---|
89 | int ref = 0;
|
---|
90 |
|
---|
91 | if (mackey == NULL)
|
---|
92 | return;
|
---|
93 |
|
---|
94 | CRYPTO_DOWN_REF(&mackey->refcnt, &ref, mackey->lock);
|
---|
95 | if (ref > 0)
|
---|
96 | return;
|
---|
97 |
|
---|
98 | OPENSSL_secure_clear_free(mackey->priv_key, mackey->priv_key_len);
|
---|
99 | OPENSSL_free(mackey->properties);
|
---|
100 | ossl_prov_cipher_reset(&mackey->cipher);
|
---|
101 | CRYPTO_THREAD_lock_free(mackey->lock);
|
---|
102 | OPENSSL_free(mackey);
|
---|
103 | }
|
---|
104 |
|
---|
105 | int ossl_mac_key_up_ref(MAC_KEY *mackey)
|
---|
106 | {
|
---|
107 | int ref = 0;
|
---|
108 |
|
---|
109 | /* This is effectively doing a new operation on the MAC_KEY and should be
|
---|
110 | * adequately guarded again modules' error states. However, both current
|
---|
111 | * calls here are guarded properly in signature/mac_legacy.c. Thus, it
|
---|
112 | * could be removed here. The concern is that something in the future
|
---|
113 | * might call this function without adequate guards. It's a cheap call,
|
---|
114 | * it seems best to leave it even though it is currently redundant.
|
---|
115 | */
|
---|
116 | if (!ossl_prov_is_running())
|
---|
117 | return 0;
|
---|
118 |
|
---|
119 | CRYPTO_UP_REF(&mackey->refcnt, &ref, mackey->lock);
|
---|
120 | return 1;
|
---|
121 | }
|
---|
122 |
|
---|
123 | static void *mac_new(void *provctx)
|
---|
124 | {
|
---|
125 | return ossl_mac_key_new(PROV_LIBCTX_OF(provctx), 0);
|
---|
126 | }
|
---|
127 |
|
---|
128 | static void *mac_new_cmac(void *provctx)
|
---|
129 | {
|
---|
130 | return ossl_mac_key_new(PROV_LIBCTX_OF(provctx), 1);
|
---|
131 | }
|
---|
132 |
|
---|
133 | static void mac_free(void *mackey)
|
---|
134 | {
|
---|
135 | ossl_mac_key_free(mackey);
|
---|
136 | }
|
---|
137 |
|
---|
138 | static int mac_has(const void *keydata, int selection)
|
---|
139 | {
|
---|
140 | const MAC_KEY *key = keydata;
|
---|
141 | int ok = 0;
|
---|
142 |
|
---|
143 | if (ossl_prov_is_running() && key != NULL) {
|
---|
144 | /*
|
---|
145 | * MAC keys always have all the parameters they need (i.e. none).
|
---|
146 | * Therefore we always return with 1, if asked about parameters.
|
---|
147 | * Similarly for public keys.
|
---|
148 | */
|
---|
149 | ok = 1;
|
---|
150 |
|
---|
151 | if ((selection & OSSL_KEYMGMT_SELECT_PRIVATE_KEY) != 0)
|
---|
152 | ok = key->priv_key != NULL;
|
---|
153 | }
|
---|
154 | return ok;
|
---|
155 | }
|
---|
156 |
|
---|
157 | static int mac_match(const void *keydata1, const void *keydata2, int selection)
|
---|
158 | {
|
---|
159 | const MAC_KEY *key1 = keydata1;
|
---|
160 | const MAC_KEY *key2 = keydata2;
|
---|
161 | int ok = 1;
|
---|
162 |
|
---|
163 | if (!ossl_prov_is_running())
|
---|
164 | return 0;
|
---|
165 |
|
---|
166 | if ((selection & OSSL_KEYMGMT_SELECT_PRIVATE_KEY) != 0) {
|
---|
167 | if ((key1->priv_key == NULL && key2->priv_key != NULL)
|
---|
168 | || (key1->priv_key != NULL && key2->priv_key == NULL)
|
---|
169 | || key1->priv_key_len != key2->priv_key_len
|
---|
170 | || (key1->cipher.cipher == NULL && key2->cipher.cipher != NULL)
|
---|
171 | || (key1->cipher.cipher != NULL && key2->cipher.cipher == NULL))
|
---|
172 | ok = 0;
|
---|
173 | else
|
---|
174 | ok = ok && (key1->priv_key == NULL /* implies key2->privkey == NULL */
|
---|
175 | || CRYPTO_memcmp(key1->priv_key, key2->priv_key,
|
---|
176 | key1->priv_key_len) == 0);
|
---|
177 | if (key1->cipher.cipher != NULL)
|
---|
178 | ok = ok && EVP_CIPHER_is_a(key1->cipher.cipher,
|
---|
179 | EVP_CIPHER_get0_name(key2->cipher.cipher));
|
---|
180 | }
|
---|
181 | return ok;
|
---|
182 | }
|
---|
183 |
|
---|
184 | static int mac_key_fromdata(MAC_KEY *key, const OSSL_PARAM params[])
|
---|
185 | {
|
---|
186 | const OSSL_PARAM *p;
|
---|
187 |
|
---|
188 | p = OSSL_PARAM_locate_const(params, OSSL_PKEY_PARAM_PRIV_KEY);
|
---|
189 | if (p != NULL) {
|
---|
190 | if (p->data_type != OSSL_PARAM_OCTET_STRING) {
|
---|
191 | ERR_raise(ERR_LIB_PROV, ERR_R_PASSED_INVALID_ARGUMENT);
|
---|
192 | return 0;
|
---|
193 | }
|
---|
194 | OPENSSL_secure_clear_free(key->priv_key, key->priv_key_len);
|
---|
195 | /* allocate at least one byte to distinguish empty key from no key set */
|
---|
196 | key->priv_key = OPENSSL_secure_malloc(p->data_size > 0 ? p->data_size : 1);
|
---|
197 | if (key->priv_key == NULL) {
|
---|
198 | ERR_raise(ERR_LIB_PROV, ERR_R_MALLOC_FAILURE);
|
---|
199 | return 0;
|
---|
200 | }
|
---|
201 | memcpy(key->priv_key, p->data, p->data_size);
|
---|
202 | key->priv_key_len = p->data_size;
|
---|
203 | }
|
---|
204 |
|
---|
205 | p = OSSL_PARAM_locate_const(params, OSSL_PKEY_PARAM_PROPERTIES);
|
---|
206 | if (p != NULL) {
|
---|
207 | if (p->data_type != OSSL_PARAM_UTF8_STRING) {
|
---|
208 | ERR_raise(ERR_LIB_PROV, ERR_R_PASSED_INVALID_ARGUMENT);
|
---|
209 | return 0;
|
---|
210 | }
|
---|
211 | OPENSSL_free(key->properties);
|
---|
212 | key->properties = OPENSSL_strdup(p->data);
|
---|
213 | if (key->properties == NULL) {
|
---|
214 | ERR_raise(ERR_LIB_PROV, ERR_R_MALLOC_FAILURE);
|
---|
215 | return 0;
|
---|
216 | }
|
---|
217 | }
|
---|
218 |
|
---|
219 | if (key->cmac && !ossl_prov_cipher_load_from_params(&key->cipher, params,
|
---|
220 | key->libctx)) {
|
---|
221 | ERR_raise(ERR_LIB_PROV, ERR_R_PASSED_INVALID_ARGUMENT);
|
---|
222 | return 0;
|
---|
223 | }
|
---|
224 |
|
---|
225 | if (key->priv_key != NULL)
|
---|
226 | return 1;
|
---|
227 |
|
---|
228 | return 0;
|
---|
229 | }
|
---|
230 |
|
---|
231 | static int mac_import(void *keydata, int selection, const OSSL_PARAM params[])
|
---|
232 | {
|
---|
233 | MAC_KEY *key = keydata;
|
---|
234 |
|
---|
235 | if (!ossl_prov_is_running() || key == NULL)
|
---|
236 | return 0;
|
---|
237 |
|
---|
238 | if ((selection & OSSL_KEYMGMT_SELECT_PRIVATE_KEY) == 0)
|
---|
239 | return 0;
|
---|
240 |
|
---|
241 | return mac_key_fromdata(key, params);
|
---|
242 | }
|
---|
243 |
|
---|
244 | static int key_to_params(MAC_KEY *key, OSSL_PARAM_BLD *tmpl,
|
---|
245 | OSSL_PARAM params[])
|
---|
246 | {
|
---|
247 | if (key == NULL)
|
---|
248 | return 0;
|
---|
249 |
|
---|
250 | if (key->priv_key != NULL
|
---|
251 | && !ossl_param_build_set_octet_string(tmpl, params,
|
---|
252 | OSSL_PKEY_PARAM_PRIV_KEY,
|
---|
253 | key->priv_key, key->priv_key_len))
|
---|
254 | return 0;
|
---|
255 |
|
---|
256 | if (key->cipher.cipher != NULL
|
---|
257 | && !ossl_param_build_set_utf8_string(tmpl, params,
|
---|
258 | OSSL_PKEY_PARAM_CIPHER,
|
---|
259 | EVP_CIPHER_get0_name(key->cipher.cipher)))
|
---|
260 | return 0;
|
---|
261 |
|
---|
262 | #if !defined(OPENSSL_NO_ENGINE) && !defined(FIPS_MODULE)
|
---|
263 | if (key->cipher.engine != NULL
|
---|
264 | && !ossl_param_build_set_utf8_string(tmpl, params,
|
---|
265 | OSSL_PKEY_PARAM_ENGINE,
|
---|
266 | ENGINE_get_id(key->cipher.engine)))
|
---|
267 | return 0;
|
---|
268 | #endif
|
---|
269 |
|
---|
270 | return 1;
|
---|
271 | }
|
---|
272 |
|
---|
273 | static int mac_export(void *keydata, int selection, OSSL_CALLBACK *param_cb,
|
---|
274 | void *cbarg)
|
---|
275 | {
|
---|
276 | MAC_KEY *key = keydata;
|
---|
277 | OSSL_PARAM_BLD *tmpl;
|
---|
278 | OSSL_PARAM *params = NULL;
|
---|
279 | int ret = 0;
|
---|
280 |
|
---|
281 | if (!ossl_prov_is_running() || key == NULL)
|
---|
282 | return 0;
|
---|
283 |
|
---|
284 | tmpl = OSSL_PARAM_BLD_new();
|
---|
285 | if (tmpl == NULL)
|
---|
286 | return 0;
|
---|
287 |
|
---|
288 | if ((selection & OSSL_KEYMGMT_SELECT_PRIVATE_KEY) != 0
|
---|
289 | && !key_to_params(key, tmpl, NULL))
|
---|
290 | goto err;
|
---|
291 |
|
---|
292 | params = OSSL_PARAM_BLD_to_param(tmpl);
|
---|
293 | if (params == NULL)
|
---|
294 | goto err;
|
---|
295 |
|
---|
296 | ret = param_cb(params, cbarg);
|
---|
297 | OSSL_PARAM_free(params);
|
---|
298 | err:
|
---|
299 | OSSL_PARAM_BLD_free(tmpl);
|
---|
300 | return ret;
|
---|
301 | }
|
---|
302 |
|
---|
303 | static const OSSL_PARAM mac_key_types[] = {
|
---|
304 | OSSL_PARAM_octet_string(OSSL_PKEY_PARAM_PRIV_KEY, NULL, 0),
|
---|
305 | OSSL_PARAM_utf8_string(OSSL_PKEY_PARAM_PROPERTIES, NULL, 0),
|
---|
306 | OSSL_PARAM_END
|
---|
307 | };
|
---|
308 | static const OSSL_PARAM *mac_imexport_types(int selection)
|
---|
309 | {
|
---|
310 | if ((selection & OSSL_KEYMGMT_SELECT_PRIVATE_KEY) != 0)
|
---|
311 | return mac_key_types;
|
---|
312 | return NULL;
|
---|
313 | }
|
---|
314 |
|
---|
315 | static const OSSL_PARAM cmac_key_types[] = {
|
---|
316 | OSSL_PARAM_octet_string(OSSL_PKEY_PARAM_PRIV_KEY, NULL, 0),
|
---|
317 | OSSL_PARAM_utf8_string(OSSL_PKEY_PARAM_CIPHER, NULL, 0),
|
---|
318 | OSSL_PARAM_utf8_string(OSSL_PKEY_PARAM_ENGINE, NULL, 0),
|
---|
319 | OSSL_PARAM_utf8_string(OSSL_PKEY_PARAM_PROPERTIES, NULL, 0),
|
---|
320 | OSSL_PARAM_END
|
---|
321 | };
|
---|
322 | static const OSSL_PARAM *cmac_imexport_types(int selection)
|
---|
323 | {
|
---|
324 | if ((selection & OSSL_KEYMGMT_SELECT_PRIVATE_KEY) != 0)
|
---|
325 | return cmac_key_types;
|
---|
326 | return NULL;
|
---|
327 | }
|
---|
328 |
|
---|
329 | static int mac_get_params(void *key, OSSL_PARAM params[])
|
---|
330 | {
|
---|
331 | return key_to_params(key, NULL, params);
|
---|
332 | }
|
---|
333 |
|
---|
334 | static const OSSL_PARAM *mac_gettable_params(void *provctx)
|
---|
335 | {
|
---|
336 | static const OSSL_PARAM gettable_params[] = {
|
---|
337 | OSSL_PARAM_octet_string(OSSL_PKEY_PARAM_PRIV_KEY, NULL, 0),
|
---|
338 | OSSL_PARAM_END
|
---|
339 | };
|
---|
340 | return gettable_params;
|
---|
341 | }
|
---|
342 |
|
---|
343 | static const OSSL_PARAM *cmac_gettable_params(void *provctx)
|
---|
344 | {
|
---|
345 | static const OSSL_PARAM gettable_params[] = {
|
---|
346 | OSSL_PARAM_octet_string(OSSL_PKEY_PARAM_PRIV_KEY, NULL, 0),
|
---|
347 | OSSL_PARAM_utf8_string(OSSL_PKEY_PARAM_CIPHER, NULL, 0),
|
---|
348 | OSSL_PARAM_utf8_string(OSSL_PKEY_PARAM_ENGINE, NULL, 0),
|
---|
349 | OSSL_PARAM_END
|
---|
350 | };
|
---|
351 | return gettable_params;
|
---|
352 | }
|
---|
353 |
|
---|
354 | static int mac_set_params(void *keydata, const OSSL_PARAM params[])
|
---|
355 | {
|
---|
356 | MAC_KEY *key = keydata;
|
---|
357 | const OSSL_PARAM *p;
|
---|
358 |
|
---|
359 | if (key == NULL)
|
---|
360 | return 0;
|
---|
361 |
|
---|
362 | p = OSSL_PARAM_locate_const(params, OSSL_PKEY_PARAM_PRIV_KEY);
|
---|
363 | if (p != NULL)
|
---|
364 | return mac_key_fromdata(key, params);
|
---|
365 |
|
---|
366 | return 1;
|
---|
367 | }
|
---|
368 |
|
---|
369 | static const OSSL_PARAM *mac_settable_params(void *provctx)
|
---|
370 | {
|
---|
371 | static const OSSL_PARAM settable_params[] = {
|
---|
372 | OSSL_PARAM_octet_string(OSSL_PKEY_PARAM_PRIV_KEY, NULL, 0),
|
---|
373 | OSSL_PARAM_END
|
---|
374 | };
|
---|
375 | return settable_params;
|
---|
376 | }
|
---|
377 |
|
---|
378 | static void *mac_gen_init_common(void *provctx, int selection)
|
---|
379 | {
|
---|
380 | OSSL_LIB_CTX *libctx = PROV_LIBCTX_OF(provctx);
|
---|
381 | struct mac_gen_ctx *gctx = NULL;
|
---|
382 |
|
---|
383 | if (!ossl_prov_is_running())
|
---|
384 | return NULL;
|
---|
385 |
|
---|
386 | if ((gctx = OPENSSL_zalloc(sizeof(*gctx))) != NULL) {
|
---|
387 | gctx->libctx = libctx;
|
---|
388 | gctx->selection = selection;
|
---|
389 | }
|
---|
390 | return gctx;
|
---|
391 | }
|
---|
392 |
|
---|
393 | static void *mac_gen_init(void *provctx, int selection,
|
---|
394 | const OSSL_PARAM params[])
|
---|
395 | {
|
---|
396 | struct mac_gen_ctx *gctx = mac_gen_init_common(provctx, selection);
|
---|
397 |
|
---|
398 | if (gctx != NULL && !mac_gen_set_params(gctx, params)) {
|
---|
399 | OPENSSL_free(gctx);
|
---|
400 | gctx = NULL;
|
---|
401 | }
|
---|
402 | return gctx;
|
---|
403 | }
|
---|
404 |
|
---|
405 | static void *cmac_gen_init(void *provctx, int selection,
|
---|
406 | const OSSL_PARAM params[])
|
---|
407 | {
|
---|
408 | struct mac_gen_ctx *gctx = mac_gen_init_common(provctx, selection);
|
---|
409 |
|
---|
410 | if (gctx != NULL && !cmac_gen_set_params(gctx, params)) {
|
---|
411 | OPENSSL_free(gctx);
|
---|
412 | gctx = NULL;
|
---|
413 | }
|
---|
414 | return gctx;
|
---|
415 | }
|
---|
416 |
|
---|
417 | static int mac_gen_set_params(void *genctx, const OSSL_PARAM params[])
|
---|
418 | {
|
---|
419 | struct mac_gen_ctx *gctx = genctx;
|
---|
420 | const OSSL_PARAM *p;
|
---|
421 |
|
---|
422 | if (gctx == NULL)
|
---|
423 | return 0;
|
---|
424 |
|
---|
425 | p = OSSL_PARAM_locate_const(params, OSSL_PKEY_PARAM_PRIV_KEY);
|
---|
426 | if (p != NULL) {
|
---|
427 | if (p->data_type != OSSL_PARAM_OCTET_STRING) {
|
---|
428 | ERR_raise(ERR_LIB_PROV, ERR_R_PASSED_INVALID_ARGUMENT);
|
---|
429 | return 0;
|
---|
430 | }
|
---|
431 | gctx->priv_key = OPENSSL_secure_malloc(p->data_size);
|
---|
432 | if (gctx->priv_key == NULL) {
|
---|
433 | ERR_raise(ERR_LIB_PROV, ERR_R_MALLOC_FAILURE);
|
---|
434 | return 0;
|
---|
435 | }
|
---|
436 | memcpy(gctx->priv_key, p->data, p->data_size);
|
---|
437 | gctx->priv_key_len = p->data_size;
|
---|
438 | }
|
---|
439 |
|
---|
440 | return 1;
|
---|
441 | }
|
---|
442 |
|
---|
443 | static int cmac_gen_set_params(void *genctx, const OSSL_PARAM params[])
|
---|
444 | {
|
---|
445 | struct mac_gen_ctx *gctx = genctx;
|
---|
446 |
|
---|
447 | if (!mac_gen_set_params(genctx, params))
|
---|
448 | return 0;
|
---|
449 |
|
---|
450 | if (!ossl_prov_cipher_load_from_params(&gctx->cipher, params,
|
---|
451 | gctx->libctx)) {
|
---|
452 | ERR_raise(ERR_LIB_PROV, ERR_R_PASSED_INVALID_ARGUMENT);
|
---|
453 | return 0;
|
---|
454 | }
|
---|
455 |
|
---|
456 | return 1;
|
---|
457 | }
|
---|
458 |
|
---|
459 | static const OSSL_PARAM *mac_gen_settable_params(ossl_unused void *genctx,
|
---|
460 | ossl_unused void *provctx)
|
---|
461 | {
|
---|
462 | static OSSL_PARAM settable[] = {
|
---|
463 | OSSL_PARAM_octet_string(OSSL_PKEY_PARAM_PRIV_KEY, NULL, 0),
|
---|
464 | OSSL_PARAM_END
|
---|
465 | };
|
---|
466 | return settable;
|
---|
467 | }
|
---|
468 |
|
---|
469 | static const OSSL_PARAM *cmac_gen_settable_params(ossl_unused void *genctx,
|
---|
470 | ossl_unused void *provctx)
|
---|
471 | {
|
---|
472 | static OSSL_PARAM settable[] = {
|
---|
473 | OSSL_PARAM_octet_string(OSSL_PKEY_PARAM_PRIV_KEY, NULL, 0),
|
---|
474 | OSSL_PARAM_utf8_string(OSSL_PKEY_PARAM_CIPHER, NULL, 0),
|
---|
475 | OSSL_PARAM_END
|
---|
476 | };
|
---|
477 | return settable;
|
---|
478 | }
|
---|
479 |
|
---|
480 | static void *mac_gen(void *genctx, OSSL_CALLBACK *cb, void *cbarg)
|
---|
481 | {
|
---|
482 | struct mac_gen_ctx *gctx = genctx;
|
---|
483 | MAC_KEY *key;
|
---|
484 |
|
---|
485 | if (!ossl_prov_is_running() || gctx == NULL)
|
---|
486 | return NULL;
|
---|
487 |
|
---|
488 | if ((key = ossl_mac_key_new(gctx->libctx, 0)) == NULL) {
|
---|
489 | ERR_raise(ERR_LIB_PROV, ERR_R_MALLOC_FAILURE);
|
---|
490 | return NULL;
|
---|
491 | }
|
---|
492 |
|
---|
493 | /* If we're doing parameter generation then we just return a blank key */
|
---|
494 | if ((gctx->selection & OSSL_KEYMGMT_SELECT_KEYPAIR) == 0)
|
---|
495 | return key;
|
---|
496 |
|
---|
497 | if (gctx->priv_key == NULL) {
|
---|
498 | ERR_raise(ERR_LIB_PROV, PROV_R_INVALID_KEY);
|
---|
499 | ossl_mac_key_free(key);
|
---|
500 | return NULL;
|
---|
501 | }
|
---|
502 |
|
---|
503 | /*
|
---|
504 | * This is horrible but required for backwards compatibility. We don't
|
---|
505 | * actually do real key generation at all. We simply copy the key that was
|
---|
506 | * previously set in the gctx. Hopefully at some point in the future all
|
---|
507 | * of this can be removed and we will only support the EVP_KDF APIs.
|
---|
508 | */
|
---|
509 | if (!ossl_prov_cipher_copy(&key->cipher, &gctx->cipher)) {
|
---|
510 | ossl_mac_key_free(key);
|
---|
511 | ERR_raise(ERR_LIB_PROV, ERR_R_INTERNAL_ERROR);
|
---|
512 | return NULL;
|
---|
513 | }
|
---|
514 | ossl_prov_cipher_reset(&gctx->cipher);
|
---|
515 | key->priv_key = gctx->priv_key;
|
---|
516 | key->priv_key_len = gctx->priv_key_len;
|
---|
517 | gctx->priv_key_len = 0;
|
---|
518 | gctx->priv_key = NULL;
|
---|
519 |
|
---|
520 | return key;
|
---|
521 | }
|
---|
522 |
|
---|
523 | static void mac_gen_cleanup(void *genctx)
|
---|
524 | {
|
---|
525 | struct mac_gen_ctx *gctx = genctx;
|
---|
526 |
|
---|
527 | OPENSSL_secure_clear_free(gctx->priv_key, gctx->priv_key_len);
|
---|
528 | ossl_prov_cipher_reset(&gctx->cipher);
|
---|
529 | OPENSSL_free(gctx);
|
---|
530 | }
|
---|
531 |
|
---|
532 | const OSSL_DISPATCH ossl_mac_legacy_keymgmt_functions[] = {
|
---|
533 | { OSSL_FUNC_KEYMGMT_NEW, (void (*)(void))mac_new },
|
---|
534 | { OSSL_FUNC_KEYMGMT_FREE, (void (*)(void))mac_free },
|
---|
535 | { OSSL_FUNC_KEYMGMT_GET_PARAMS, (void (*) (void))mac_get_params },
|
---|
536 | { OSSL_FUNC_KEYMGMT_GETTABLE_PARAMS, (void (*) (void))mac_gettable_params },
|
---|
537 | { OSSL_FUNC_KEYMGMT_SET_PARAMS, (void (*) (void))mac_set_params },
|
---|
538 | { OSSL_FUNC_KEYMGMT_SETTABLE_PARAMS, (void (*) (void))mac_settable_params },
|
---|
539 | { OSSL_FUNC_KEYMGMT_HAS, (void (*)(void))mac_has },
|
---|
540 | { OSSL_FUNC_KEYMGMT_MATCH, (void (*)(void))mac_match },
|
---|
541 | { OSSL_FUNC_KEYMGMT_IMPORT, (void (*)(void))mac_import },
|
---|
542 | { OSSL_FUNC_KEYMGMT_IMPORT_TYPES, (void (*)(void))mac_imexport_types },
|
---|
543 | { OSSL_FUNC_KEYMGMT_EXPORT, (void (*)(void))mac_export },
|
---|
544 | { OSSL_FUNC_KEYMGMT_EXPORT_TYPES, (void (*)(void))mac_imexport_types },
|
---|
545 | { OSSL_FUNC_KEYMGMT_GEN_INIT, (void (*)(void))mac_gen_init },
|
---|
546 | { OSSL_FUNC_KEYMGMT_GEN_SET_PARAMS, (void (*)(void))mac_gen_set_params },
|
---|
547 | { OSSL_FUNC_KEYMGMT_GEN_SETTABLE_PARAMS,
|
---|
548 | (void (*)(void))mac_gen_settable_params },
|
---|
549 | { OSSL_FUNC_KEYMGMT_GEN, (void (*)(void))mac_gen },
|
---|
550 | { OSSL_FUNC_KEYMGMT_GEN_CLEANUP, (void (*)(void))mac_gen_cleanup },
|
---|
551 | { 0, NULL }
|
---|
552 | };
|
---|
553 |
|
---|
554 | const OSSL_DISPATCH ossl_cmac_legacy_keymgmt_functions[] = {
|
---|
555 | { OSSL_FUNC_KEYMGMT_NEW, (void (*)(void))mac_new_cmac },
|
---|
556 | { OSSL_FUNC_KEYMGMT_FREE, (void (*)(void))mac_free },
|
---|
557 | { OSSL_FUNC_KEYMGMT_GET_PARAMS, (void (*) (void))mac_get_params },
|
---|
558 | { OSSL_FUNC_KEYMGMT_GETTABLE_PARAMS, (void (*) (void))cmac_gettable_params },
|
---|
559 | { OSSL_FUNC_KEYMGMT_SET_PARAMS, (void (*) (void))mac_set_params },
|
---|
560 | { OSSL_FUNC_KEYMGMT_SETTABLE_PARAMS, (void (*) (void))mac_settable_params },
|
---|
561 | { OSSL_FUNC_KEYMGMT_HAS, (void (*)(void))mac_has },
|
---|
562 | { OSSL_FUNC_KEYMGMT_MATCH, (void (*)(void))mac_match },
|
---|
563 | { OSSL_FUNC_KEYMGMT_IMPORT, (void (*)(void))mac_import },
|
---|
564 | { OSSL_FUNC_KEYMGMT_IMPORT_TYPES, (void (*)(void))cmac_imexport_types },
|
---|
565 | { OSSL_FUNC_KEYMGMT_EXPORT, (void (*)(void))mac_export },
|
---|
566 | { OSSL_FUNC_KEYMGMT_EXPORT_TYPES, (void (*)(void))cmac_imexport_types },
|
---|
567 | { OSSL_FUNC_KEYMGMT_GEN_INIT, (void (*)(void))cmac_gen_init },
|
---|
568 | { OSSL_FUNC_KEYMGMT_GEN_SET_PARAMS, (void (*)(void))cmac_gen_set_params },
|
---|
569 | { OSSL_FUNC_KEYMGMT_GEN_SETTABLE_PARAMS,
|
---|
570 | (void (*)(void))cmac_gen_settable_params },
|
---|
571 | { OSSL_FUNC_KEYMGMT_GEN, (void (*)(void))mac_gen },
|
---|
572 | { OSSL_FUNC_KEYMGMT_GEN_CLEANUP, (void (*)(void))mac_gen_cleanup },
|
---|
573 | { 0, NULL }
|
---|
574 | };
|
---|
575 |
|
---|