VirtualBox

source: vbox/trunk/src/libs/openssl-3.1.3/crypto/evp/evp_fetch.c@ 102427

Last change on this file since 102427 was 101211, checked in by vboxsync, 17 months ago

openssl-3.1.3: Applied and adjusted our OpenSSL changes to 3.1.2. bugref:10527

File size: 21.6 KB
Line 
1/*
2 * Copyright 2019-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#include <stddef.h>
11#include <openssl/types.h>
12#include <openssl/evp.h>
13#include <openssl/core.h>
14#include "internal/cryptlib.h"
15#include "internal/thread_once.h"
16#include "internal/property.h"
17#include "internal/core.h"
18#include "internal/provider.h"
19#include "internal/namemap.h"
20#include "crypto/evp.h" /* evp_local.h needs it */
21#include "evp_local.h"
22
23#define NAME_SEPARATOR ':'
24
25/* Data to be passed through ossl_method_construct() */
26struct evp_method_data_st {
27 OSSL_LIB_CTX *libctx;
28 int operation_id; /* For get_evp_method_from_store() */
29 int name_id; /* For get_evp_method_from_store() */
30 const char *names; /* For get_evp_method_from_store() */
31 const char *propquery; /* For get_evp_method_from_store() */
32
33 OSSL_METHOD_STORE *tmp_store; /* For get_tmp_evp_method_store() */
34
35 unsigned int flag_construct_error_occurred : 1;
36
37 void *(*method_from_algorithm)(int name_id, const OSSL_ALGORITHM *,
38 OSSL_PROVIDER *);
39 int (*refcnt_up_method)(void *method);
40 void (*destruct_method)(void *method);
41};
42
43/*
44 * Generic routines to fetch / create EVP methods with ossl_method_construct()
45 */
46static void *get_tmp_evp_method_store(void *data)
47{
48 struct evp_method_data_st *methdata = data;
49
50 if (methdata->tmp_store == NULL)
51 methdata->tmp_store = ossl_method_store_new(methdata->libctx);
52 return methdata->tmp_store;
53}
54
55 static void dealloc_tmp_evp_method_store(void *store)
56{
57 if (store != NULL)
58 ossl_method_store_free(store);
59}
60
61static OSSL_METHOD_STORE *get_evp_method_store(OSSL_LIB_CTX *libctx)
62{
63 return ossl_lib_ctx_get_data(libctx, OSSL_LIB_CTX_EVP_METHOD_STORE_INDEX);
64}
65
66static int reserve_evp_method_store(void *store, void *data)
67{
68 struct evp_method_data_st *methdata = data;
69
70 if (store == NULL
71 && (store = get_evp_method_store(methdata->libctx)) == NULL)
72 return 0;
73
74 return ossl_method_lock_store(store);
75}
76
77static int unreserve_evp_method_store(void *store, void *data)
78{
79 struct evp_method_data_st *methdata = data;
80
81 if (store == NULL
82 && (store = get_evp_method_store(methdata->libctx)) == NULL)
83 return 0;
84
85 return ossl_method_unlock_store(store);
86}
87
88/*
89 * To identify the method in the EVP method store, we mix the name identity
90 * with the operation identity, under the assumption that we don't have more
91 * than 2^23 names or more than 2^8 operation types.
92 *
93 * The resulting identity is a 31-bit integer, composed like this:
94 *
95 * +---------23 bits--------+-8 bits-+
96 * | name identity | op id |
97 * +------------------------+--------+
98 *
99 * We limit this composite number to 31 bits, thus leaving the top uint32_t
100 * bit always zero, to avoid negative sign extension when downshifting after
101 * this number happens to be passed to an int (which happens as soon as it's
102 * passed to ossl_method_store_cache_set(), and it's in that form that it
103 * gets passed along to filter_on_operation_id(), defined further down.
104 */
105#define METHOD_ID_OPERATION_MASK 0x000000FF
106#define METHOD_ID_OPERATION_MAX ((1 << 8) - 1)
107#define METHOD_ID_NAME_MASK 0x7FFFFF00
108#define METHOD_ID_NAME_OFFSET 8
109#define METHOD_ID_NAME_MAX ((1 << 23) - 1)
110static uint32_t evp_method_id(int name_id, unsigned int operation_id)
111{
112 if (!ossl_assert(name_id > 0 && name_id <= METHOD_ID_NAME_MAX)
113 || !ossl_assert(operation_id > 0
114 && operation_id <= METHOD_ID_OPERATION_MAX))
115 return 0;
116 return (((name_id << METHOD_ID_NAME_OFFSET) & METHOD_ID_NAME_MASK)
117 | (operation_id & METHOD_ID_OPERATION_MASK));
118}
119
120static void *get_evp_method_from_store(void *store, const OSSL_PROVIDER **prov,
121 void *data)
122{
123 struct evp_method_data_st *methdata = data;
124 void *method = NULL;
125 int name_id;
126 uint32_t meth_id;
127
128 /*
129 * get_evp_method_from_store() is only called to try and get the method
130 * that evp_generic_fetch() is asking for, and the operation id as well
131 * as the name or name id are passed via methdata.
132 */
133 if ((name_id = methdata->name_id) == 0 && methdata->names != NULL) {
134 OSSL_NAMEMAP *namemap = ossl_namemap_stored(methdata->libctx);
135 const char *names = methdata->names;
136 const char *q = strchr(names, NAME_SEPARATOR);
137 size_t l = (q == NULL ? strlen(names) : (size_t)(q - names));
138
139 if (namemap == 0)
140 return NULL;
141 name_id = ossl_namemap_name2num_n(namemap, names, l);
142 }
143
144 if (name_id == 0
145 || (meth_id = evp_method_id(name_id, methdata->operation_id)) == 0)
146 return NULL;
147
148 if (store == NULL
149 && (store = get_evp_method_store(methdata->libctx)) == NULL)
150 return NULL;
151
152 if (!ossl_method_store_fetch(store, meth_id, methdata->propquery, prov,
153 &method))
154 return NULL;
155 return method;
156}
157
158static int put_evp_method_in_store(void *store, void *method,
159 const OSSL_PROVIDER *prov,
160 const char *names, const char *propdef,
161 void *data)
162{
163 struct evp_method_data_st *methdata = data;
164 OSSL_NAMEMAP *namemap;
165 int name_id;
166 uint32_t meth_id;
167 size_t l = 0;
168
169 /*
170 * put_evp_method_in_store() is only called with an EVP method that was
171 * successfully created by construct_method() below, which means that
172 * all the names should already be stored in the namemap with the same
173 * numeric identity, so just use the first to get that identity.
174 */
175 if (names != NULL) {
176 const char *q = strchr(names, NAME_SEPARATOR);
177
178 l = (q == NULL ? strlen(names) : (size_t)(q - names));
179 }
180
181 if ((namemap = ossl_namemap_stored(methdata->libctx)) == NULL
182 || (name_id = ossl_namemap_name2num_n(namemap, names, l)) == 0
183 || (meth_id = evp_method_id(name_id, methdata->operation_id)) == 0)
184 return 0;
185
186 if (store == NULL
187 && (store = get_evp_method_store(methdata->libctx)) == NULL)
188 return 0;
189
190 return ossl_method_store_add(store, prov, meth_id, propdef, method,
191 methdata->refcnt_up_method,
192 methdata->destruct_method);
193}
194
195/*
196 * The core fetching functionality passes the name of the implementation.
197 * This function is responsible to getting an identity number for it.
198 */
199static void *construct_evp_method(const OSSL_ALGORITHM *algodef,
200 OSSL_PROVIDER *prov, void *data)
201{
202 /*
203 * This function is only called if get_evp_method_from_store() returned
204 * NULL, so it's safe to say that of all the spots to create a new
205 * namemap entry, this is it. Should the name already exist there, we
206 * know that ossl_namemap_add_name() will return its corresponding
207 * number.
208 */
209 struct evp_method_data_st *methdata = data;
210 OSSL_LIB_CTX *libctx = ossl_provider_libctx(prov);
211 OSSL_NAMEMAP *namemap = ossl_namemap_stored(libctx);
212 const char *names = algodef->algorithm_names;
213 int name_id = ossl_namemap_add_names(namemap, 0, names, NAME_SEPARATOR);
214 void *method;
215
216 if (name_id == 0)
217 return NULL;
218
219 method = methdata->method_from_algorithm(name_id, algodef, prov);
220
221 /*
222 * Flag to indicate that there was actual construction errors. This
223 * helps inner_evp_generic_fetch() determine what error it should
224 * record on inaccessible algorithms.
225 */
226 if (method == NULL)
227 methdata->flag_construct_error_occurred = 1;
228
229 return method;
230}
231
232static void destruct_evp_method(void *method, void *data)
233{
234 struct evp_method_data_st *methdata = data;
235
236 methdata->destruct_method(method);
237}
238
239static void *
240inner_evp_generic_fetch(struct evp_method_data_st *methdata,
241 OSSL_PROVIDER *prov, int operation_id,
242 const char *name, const char *properties,
243 void *(*new_method)(int name_id,
244 const OSSL_ALGORITHM *algodef,
245 OSSL_PROVIDER *prov),
246 int (*up_ref_method)(void *),
247 void (*free_method)(void *))
248{
249 OSSL_METHOD_STORE *store = get_evp_method_store(methdata->libctx);
250 OSSL_NAMEMAP *namemap = ossl_namemap_stored(methdata->libctx);
251 const char *const propq = properties != NULL ? properties : "";
252 uint32_t meth_id = 0;
253 void *method = NULL;
254 int unsupported, name_id;
255
256 if (store == NULL || namemap == NULL) {
257 ERR_raise(ERR_LIB_EVP, ERR_R_PASSED_INVALID_ARGUMENT);
258 return NULL;
259 }
260
261 /*
262 * If there's ever an operation_id == 0 passed, we have an internal
263 * programming error.
264 */
265 if (!ossl_assert(operation_id > 0)) {
266 ERR_raise(ERR_LIB_EVP, ERR_R_INTERNAL_ERROR);
267 return NULL;
268 }
269
270 /* If we haven't received a name id yet, try to get one for the name */
271 name_id = name != NULL ? ossl_namemap_name2num(namemap, name) : 0;
272
273 /*
274 * If we have a name id, calculate a method id with evp_method_id().
275 *
276 * evp_method_id returns 0 if we have too many operations (more than
277 * about 2^8) or too many names (more than about 2^24). In that case,
278 * we can't create any new method.
279 * For all intents and purposes, this is an internal error.
280 */
281 if (name_id != 0 && (meth_id = evp_method_id(name_id, operation_id)) == 0) {
282 ERR_raise(ERR_LIB_EVP, ERR_R_INTERNAL_ERROR);
283 return NULL;
284 }
285
286 /*
287 * If we haven't found the name yet, chances are that the algorithm to
288 * be fetched is unsupported.
289 */
290 unsupported = name_id == 0;
291
292 if (meth_id == 0
293 || !ossl_method_store_cache_get(store, prov, meth_id, propq, &method)) {
294 OSSL_METHOD_CONSTRUCT_METHOD mcm = {
295 get_tmp_evp_method_store,
296 reserve_evp_method_store,
297 unreserve_evp_method_store,
298 get_evp_method_from_store,
299 put_evp_method_in_store,
300 construct_evp_method,
301 destruct_evp_method
302 };
303
304 methdata->operation_id = operation_id;
305 methdata->name_id = name_id;
306 methdata->names = name;
307 methdata->propquery = propq;
308 methdata->method_from_algorithm = new_method;
309 methdata->refcnt_up_method = up_ref_method;
310 methdata->destruct_method = free_method;
311 methdata->flag_construct_error_occurred = 0;
312 if ((method = ossl_method_construct(methdata->libctx, operation_id,
313 &prov, 0 /* !force_cache */,
314 &mcm, methdata)) != NULL) {
315 /*
316 * If construction did create a method for us, we know that
317 * there is a correct name_id and meth_id, since those have
318 * already been calculated in get_evp_method_from_store() and
319 * put_evp_method_in_store() above.
320 */
321 if (name_id == 0)
322 name_id = ossl_namemap_name2num(namemap, name);
323 meth_id = evp_method_id(name_id, operation_id);
324 if (name_id != 0)
325 ossl_method_store_cache_set(store, prov, meth_id, propq,
326 method, up_ref_method, free_method);
327 }
328
329 /*
330 * If we never were in the constructor, the algorithm to be fetched
331 * is unsupported.
332 */
333 unsupported = !methdata->flag_construct_error_occurred;
334 }
335
336 if ((name_id != 0 || name != NULL) && method == NULL) {
337 int code = unsupported ? ERR_R_UNSUPPORTED : ERR_R_FETCH_FAILED;
338
339 if (name == NULL)
340 name = ossl_namemap_num2name(namemap, name_id, 0);
341 ERR_raise_data(ERR_LIB_EVP, code,
342 "%s, Algorithm (%s : %d), Properties (%s)",
343 ossl_lib_ctx_get_descriptor(methdata->libctx),
344 name == NULL ? "<null>" : name, name_id,
345 properties == NULL ? "<null>" : properties);
346 }
347
348 return method;
349}
350
351void *evp_generic_fetch(OSSL_LIB_CTX *libctx, int operation_id,
352 const char *name, const char *properties,
353 void *(*new_method)(int name_id,
354 const OSSL_ALGORITHM *algodef,
355 OSSL_PROVIDER *prov),
356 int (*up_ref_method)(void *),
357 void (*free_method)(void *))
358{
359 struct evp_method_data_st methdata;
360 void *method;
361
362 methdata.libctx = libctx;
363 methdata.tmp_store = NULL;
364 method = inner_evp_generic_fetch(&methdata, NULL, operation_id,
365 name, properties,
366 new_method, up_ref_method, free_method);
367 dealloc_tmp_evp_method_store(methdata.tmp_store);
368 return method;
369}
370
371/*
372 * evp_generic_fetch_from_prov() is special, and only returns methods from
373 * the given provider.
374 * This is meant to be used when one method needs to fetch an associated
375 * method.
376 */
377void *evp_generic_fetch_from_prov(OSSL_PROVIDER *prov, int operation_id,
378 const char *name, const char *properties,
379 void *(*new_method)(int name_id,
380 const OSSL_ALGORITHM *algodef,
381 OSSL_PROVIDER *prov),
382 int (*up_ref_method)(void *),
383 void (*free_method)(void *))
384{
385 struct evp_method_data_st methdata;
386 void *method;
387
388 methdata.libctx = ossl_provider_libctx(prov);
389 methdata.tmp_store = NULL;
390 method = inner_evp_generic_fetch(&methdata, prov, operation_id,
391 name, properties,
392 new_method, up_ref_method, free_method);
393 dealloc_tmp_evp_method_store(methdata.tmp_store);
394 return method;
395}
396
397int evp_method_store_cache_flush(OSSL_LIB_CTX *libctx)
398{
399 OSSL_METHOD_STORE *store = get_evp_method_store(libctx);
400
401 if (store != NULL)
402 return ossl_method_store_cache_flush_all(store);
403 return 1;
404}
405
406int evp_method_store_remove_all_provided(const OSSL_PROVIDER *prov)
407{
408 OSSL_LIB_CTX *libctx = ossl_provider_libctx(prov);
409 OSSL_METHOD_STORE *store = get_evp_method_store(libctx);
410
411 if (store != NULL)
412 return ossl_method_store_remove_all_provided(store, prov);
413 return 1;
414}
415
416static int evp_set_parsed_default_properties(OSSL_LIB_CTX *libctx,
417 OSSL_PROPERTY_LIST *def_prop,
418 int loadconfig,
419 int mirrored)
420{
421 OSSL_METHOD_STORE *store = get_evp_method_store(libctx);
422 OSSL_PROPERTY_LIST **plp = ossl_ctx_global_properties(libctx, loadconfig);
423
424 if (plp != NULL && store != NULL) {
425#ifndef FIPS_MODULE
426 char *propstr = NULL;
427 size_t strsz;
428
429 if (mirrored) {
430 if (ossl_global_properties_no_mirrored(libctx))
431 return 0;
432 } else {
433 /*
434 * These properties have been explicitly set on this libctx, so
435 * don't allow any mirroring from a parent libctx.
436 */
437 ossl_global_properties_stop_mirroring(libctx);
438 }
439
440 strsz = ossl_property_list_to_string(libctx, def_prop, NULL, 0);
441 if (strsz > 0)
442 propstr = OPENSSL_malloc(strsz);
443 if (propstr == NULL) {
444 ERR_raise(ERR_LIB_EVP, ERR_R_INTERNAL_ERROR);
445 return 0;
446 }
447 if (ossl_property_list_to_string(libctx, def_prop, propstr,
448 strsz) == 0) {
449 OPENSSL_free(propstr);
450 ERR_raise(ERR_LIB_EVP, ERR_R_INTERNAL_ERROR);
451 return 0;
452 }
453 ossl_provider_default_props_update(libctx, propstr);
454 OPENSSL_free(propstr);
455#endif
456 ossl_property_free(*plp);
457 *plp = def_prop;
458 if (store != NULL)
459 return ossl_method_store_cache_flush_all(store);
460 }
461 ERR_raise(ERR_LIB_EVP, ERR_R_INTERNAL_ERROR);
462 return 0;
463}
464
465int evp_set_default_properties_int(OSSL_LIB_CTX *libctx, const char *propq,
466 int loadconfig, int mirrored)
467{
468 OSSL_PROPERTY_LIST *pl = NULL;
469
470 if (propq != NULL && (pl = ossl_parse_query(libctx, propq, 1)) == NULL) {
471 ERR_raise(ERR_LIB_EVP, EVP_R_DEFAULT_QUERY_PARSE_ERROR);
472 return 0;
473 }
474 if (!evp_set_parsed_default_properties(libctx, pl, loadconfig, mirrored)) {
475 ossl_property_free(pl);
476 return 0;
477 }
478 return 1;
479}
480
481int EVP_set_default_properties(OSSL_LIB_CTX *libctx, const char *propq)
482{
483 return evp_set_default_properties_int(libctx, propq, 1, 0);
484}
485
486static int evp_default_properties_merge(OSSL_LIB_CTX *libctx, const char *propq,
487 int loadconfig)
488{
489 OSSL_PROPERTY_LIST **plp = ossl_ctx_global_properties(libctx, loadconfig);
490 OSSL_PROPERTY_LIST *pl1, *pl2;
491
492 if (propq == NULL)
493 return 1;
494 if (plp == NULL || *plp == NULL)
495 return evp_set_default_properties_int(libctx, propq, 0, 0);
496 if ((pl1 = ossl_parse_query(libctx, propq, 1)) == NULL) {
497 ERR_raise(ERR_LIB_EVP, EVP_R_DEFAULT_QUERY_PARSE_ERROR);
498 return 0;
499 }
500 pl2 = ossl_property_merge(pl1, *plp);
501 ossl_property_free(pl1);
502 if (pl2 == NULL) {
503 ERR_raise(ERR_LIB_EVP, ERR_R_MALLOC_FAILURE);
504 return 0;
505 }
506 if (!evp_set_parsed_default_properties(libctx, pl2, 0, 0)) {
507 ossl_property_free(pl2);
508 return 0;
509 }
510 return 1;
511}
512
513static int evp_default_property_is_enabled(OSSL_LIB_CTX *libctx,
514 const char *prop_name)
515{
516 OSSL_PROPERTY_LIST **plp = ossl_ctx_global_properties(libctx, 1);
517
518 return plp != NULL && ossl_property_is_enabled(libctx, prop_name, *plp);
519}
520
521int EVP_default_properties_is_fips_enabled(OSSL_LIB_CTX *libctx)
522{
523 return evp_default_property_is_enabled(libctx, "fips");
524}
525
526int evp_default_properties_enable_fips_int(OSSL_LIB_CTX *libctx, int enable,
527 int loadconfig)
528{
529 const char *query = (enable != 0) ? "fips=yes" : "-fips";
530
531 return evp_default_properties_merge(libctx, query, loadconfig);
532}
533
534int EVP_default_properties_enable_fips(OSSL_LIB_CTX *libctx, int enable)
535{
536 return evp_default_properties_enable_fips_int(libctx, enable, 1);
537}
538
539char *evp_get_global_properties_str(OSSL_LIB_CTX *libctx, int loadconfig)
540{
541 OSSL_PROPERTY_LIST **plp = ossl_ctx_global_properties(libctx, loadconfig);
542 char *propstr = NULL;
543 size_t sz;
544
545 if (plp == NULL)
546 return OPENSSL_strdup("");
547
548 sz = ossl_property_list_to_string(libctx, *plp, NULL, 0);
549 if (sz == 0) {
550 ERR_raise(ERR_LIB_EVP, ERR_R_INTERNAL_ERROR);
551 return NULL;
552 }
553
554 propstr = OPENSSL_malloc(sz);
555 if (propstr == NULL) {
556 ERR_raise(ERR_LIB_EVP, ERR_R_MALLOC_FAILURE);
557 return NULL;
558 }
559 if (ossl_property_list_to_string(libctx, *plp, propstr, sz) == 0) {
560 ERR_raise(ERR_LIB_EVP, ERR_R_INTERNAL_ERROR);
561 OPENSSL_free(propstr);
562 return NULL;
563 }
564 return propstr;
565}
566
567struct filter_data_st {
568 int operation_id;
569 void (*user_fn)(void *method, void *arg);
570 void *user_arg;
571};
572
573static void filter_on_operation_id(int id, void *method, void *arg)
574{
575 struct filter_data_st *data = arg;
576
577 if ((id & METHOD_ID_OPERATION_MASK) == data->operation_id)
578 data->user_fn(method, data->user_arg);
579}
580
581void evp_generic_do_all(OSSL_LIB_CTX *libctx, int operation_id,
582 void (*user_fn)(void *method, void *arg),
583 void *user_arg,
584 void *(*new_method)(int name_id,
585 const OSSL_ALGORITHM *algodef,
586 OSSL_PROVIDER *prov),
587 int (*up_ref_method)(void *),
588 void (*free_method)(void *))
589{
590 struct evp_method_data_st methdata;
591 struct filter_data_st data;
592
593 methdata.libctx = libctx;
594 methdata.tmp_store = NULL;
595 (void)inner_evp_generic_fetch(&methdata, NULL, operation_id, NULL, NULL,
596 new_method, up_ref_method, free_method);
597
598 data.operation_id = operation_id;
599 data.user_fn = user_fn;
600 data.user_arg = user_arg;
601 if (methdata.tmp_store != NULL)
602 ossl_method_store_do_all(methdata.tmp_store, &filter_on_operation_id,
603 &data);
604 ossl_method_store_do_all(get_evp_method_store(libctx),
605 &filter_on_operation_id, &data);
606 dealloc_tmp_evp_method_store(methdata.tmp_store);
607}
608
609int evp_is_a(OSSL_PROVIDER *prov, int number,
610 const char *legacy_name, const char *name)
611{
612 /*
613 * For a |prov| that is NULL, the library context will be NULL
614 */
615 OSSL_LIB_CTX *libctx = ossl_provider_libctx(prov);
616 OSSL_NAMEMAP *namemap = ossl_namemap_stored(libctx);
617
618 if (prov == NULL)
619 number = ossl_namemap_name2num(namemap, legacy_name);
620 return ossl_namemap_name2num(namemap, name) == number;
621}
622
623int evp_names_do_all(OSSL_PROVIDER *prov, int number,
624 void (*fn)(const char *name, void *data),
625 void *data)
626{
627 OSSL_LIB_CTX *libctx = ossl_provider_libctx(prov);
628 OSSL_NAMEMAP *namemap = ossl_namemap_stored(libctx);
629
630 return ossl_namemap_doall_names(namemap, number, fn, data);
631}
Note: See TracBrowser for help on using the repository browser.

© 2024 Oracle Support Privacy / Do Not Sell My Info Terms of Use Trademark Policy Automated Access Etiquette