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