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