2 * Copyright 2019-2021 The OpenSSL Project Authors. All Rights Reserved.
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
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 "internal/property.h"
21 #include "crypto/evp.h" /* evp_local.h needs it */
22 #include "evp_local.h"
24 #define NAME_SEPARATOR ':'
26 static void evp_method_store_free(void *vstore)
28 ossl_method_store_free(vstore);
31 static void *evp_method_store_new(OSSL_LIB_CTX *ctx)
33 return ossl_method_store_new(ctx);
37 static const OSSL_LIB_CTX_METHOD evp_method_store_method = {
38 /* We want evp_method_store to be cleaned up before the provider store */
39 OSSL_LIB_CTX_METHOD_PRIORITY_2,
41 evp_method_store_free,
44 /* Data to be passed through ossl_method_construct() */
45 struct evp_method_data_st {
47 int operation_id; /* For get_evp_method_from_store() */
48 int name_id; /* For get_evp_method_from_store() */
49 const char *names; /* For get_evp_method_from_store() */
50 const char *propquery; /* For get_evp_method_from_store() */
52 OSSL_METHOD_STORE *tmp_store; /* For get_tmp_evp_method_store() */
54 unsigned int flag_construct_error_occurred : 1;
56 void *(*method_from_algorithm)(int name_id, const OSSL_ALGORITHM *,
58 int (*refcnt_up_method)(void *method);
59 void (*destruct_method)(void *method);
63 * Generic routines to fetch / create EVP methods with ossl_method_construct()
65 static void *get_tmp_evp_method_store(void *data)
67 struct evp_method_data_st *methdata = data;
69 if (methdata->tmp_store == NULL)
70 methdata->tmp_store = ossl_method_store_new(methdata->libctx);
71 return methdata->tmp_store;
74 static void dealloc_tmp_evp_method_store(void *store)
77 ossl_method_store_free(store);
80 static OSSL_METHOD_STORE *get_evp_method_store(OSSL_LIB_CTX *libctx)
82 return ossl_lib_ctx_get_data(libctx, OSSL_LIB_CTX_EVP_METHOD_STORE_INDEX,
83 &evp_method_store_method);
87 * To identify the method in the EVP method store, we mix the name identity
88 * with the operation identity, under the assumption that we don't have more
89 * than 2^23 names or more than 2^8 operation types.
91 * The resulting identity is a 31-bit integer, composed like this:
93 * +---------23 bits--------+-8 bits-+
94 * | name identity | op id |
95 * +------------------------+--------+
97 * We limit this composite number to 31 bits, thus leaving the top uint32_t
98 * bit always zero, to avoid negative sign extension when downshifting after
99 * this number happens to be passed to an int (which happens as soon as it's
100 * passed to ossl_method_store_cache_set(), and it's in that form that it
101 * gets passed along to filter_on_operation_id(), defined further down.
103 #define METHOD_ID_OPERATION_MASK 0x000000FF
104 #define METHOD_ID_OPERATION_MAX ((1 << 8) - 1)
105 #define METHOD_ID_NAME_MASK 0x7FFFFF00
106 #define METHOD_ID_NAME_OFFSET 8
107 #define METHOD_ID_NAME_MAX ((1 << 23) - 1)
108 static uint32_t evp_method_id(int name_id, unsigned int operation_id)
110 if (!ossl_assert(name_id > 0 && name_id <= METHOD_ID_NAME_MAX)
111 || !ossl_assert(operation_id > 0
112 && operation_id <= METHOD_ID_OPERATION_MAX))
114 return (((name_id << METHOD_ID_NAME_OFFSET) & METHOD_ID_NAME_MASK)
115 | (operation_id & METHOD_ID_OPERATION_MASK));
118 static void *get_evp_method_from_store(void *store, const OSSL_PROVIDER **prov,
121 struct evp_method_data_st *methdata = data;
127 * get_evp_method_from_store() is only called to try and get the method
128 * that evp_generic_fetch() is asking for, and the operation id as well
129 * as the name or name id are passed via methdata.
131 if ((name_id = methdata->name_id) == 0 && methdata->names != NULL) {
132 OSSL_NAMEMAP *namemap = ossl_namemap_stored(methdata->libctx);
133 const char *names = methdata->names;
134 const char *q = strchr(names, NAME_SEPARATOR);
135 size_t l = (q == NULL ? strlen(names) : (size_t)(q - names));
139 name_id = ossl_namemap_name2num_n(namemap, names, l);
143 || (meth_id = evp_method_id(name_id, methdata->operation_id)) == 0)
147 && (store = get_evp_method_store(methdata->libctx)) == NULL)
150 if (!ossl_method_store_fetch(store, meth_id, methdata->propquery, prov,
156 static int put_evp_method_in_store(void *store, void *method,
157 const OSSL_PROVIDER *prov,
158 const char *names, const char *propdef,
161 struct evp_method_data_st *methdata = data;
162 OSSL_NAMEMAP *namemap;
168 * put_evp_method_in_store() is only called with an EVP method that was
169 * successfully created by construct_method() below, which means that
170 * all the names should already be stored in the namemap with the same
171 * numeric identity, so just use the first to get that identity.
174 const char *q = strchr(names, NAME_SEPARATOR);
176 l = (q == NULL ? strlen(names) : (size_t)(q - names));
179 if ((namemap = ossl_namemap_stored(methdata->libctx)) == NULL
180 || (name_id = ossl_namemap_name2num_n(namemap, names, l)) == 0
181 || (meth_id = evp_method_id(name_id, methdata->operation_id)) == 0)
185 && (store = get_evp_method_store(methdata->libctx)) == NULL)
188 return ossl_method_store_add(store, prov, meth_id, propdef, method,
189 methdata->refcnt_up_method,
190 methdata->destruct_method);
194 * The core fetching functionality passes the name of the implementation.
195 * This function is responsible to getting an identity number for it.
197 static void *construct_evp_method(const OSSL_ALGORITHM *algodef,
198 OSSL_PROVIDER *prov, void *data)
201 * This function is only called if get_evp_method_from_store() returned
202 * NULL, so it's safe to say that of all the spots to create a new
203 * namemap entry, this is it. Should the name already exist there, we
204 * know that ossl_namemap_add_name() will return its corresponding
207 struct evp_method_data_st *methdata = data;
208 OSSL_LIB_CTX *libctx = ossl_provider_libctx(prov);
209 OSSL_NAMEMAP *namemap = ossl_namemap_stored(libctx);
210 const char *names = algodef->algorithm_names;
211 int name_id = ossl_namemap_add_names(namemap, 0, names, NAME_SEPARATOR);
217 method = methdata->method_from_algorithm(name_id, algodef, prov);
220 * Flag to indicate that there was actual construction errors. This
221 * helps inner_evp_generic_fetch() determine what error it should
222 * record on inaccessible algorithms.
225 methdata->flag_construct_error_occurred = 1;
230 static void destruct_evp_method(void *method, void *data)
232 struct evp_method_data_st *methdata = data;
234 methdata->destruct_method(method);
238 inner_evp_generic_fetch(struct evp_method_data_st *methdata,
239 OSSL_PROVIDER *prov, int operation_id,
240 int name_id, const char *name,
241 const char *properties,
242 void *(*new_method)(int name_id,
243 const OSSL_ALGORITHM *algodef,
244 OSSL_PROVIDER *prov),
245 int (*up_ref_method)(void *),
246 void (*free_method)(void *))
248 OSSL_METHOD_STORE *store = get_evp_method_store(methdata->libctx);
249 OSSL_NAMEMAP *namemap = ossl_namemap_stored(methdata->libctx);
250 uint32_t meth_id = 0;
254 if (store == NULL || namemap == NULL) {
255 ERR_raise(ERR_LIB_EVP, ERR_R_PASSED_INVALID_ARGUMENT);
260 * If there's ever an operation_id == 0 passed, we have an internal
263 if (!ossl_assert(operation_id > 0)) {
264 ERR_raise(ERR_LIB_EVP, ERR_R_INTERNAL_ERROR);
269 * If we have been passed both a name_id and a name, we have an
270 * internal programming error.
272 if (!ossl_assert(name_id == 0 || name == NULL)) {
273 ERR_raise(ERR_LIB_EVP, ERR_R_INTERNAL_ERROR);
277 /* If we haven't received a name id yet, try to get one for the name */
278 if (name_id == 0 && name != NULL)
279 name_id = ossl_namemap_name2num(namemap, name);
282 * If we have a name id, calculate a method id with evp_method_id().
284 * evp_method_id returns 0 if we have too many operations (more than
285 * about 2^8) or too many names (more than about 2^24). In that case,
286 * we can't create any new method.
287 * For all intents and purposes, this is an internal error.
289 if (name_id != 0 && (meth_id = evp_method_id(name_id, operation_id)) == 0) {
290 ERR_raise(ERR_LIB_EVP, ERR_R_INTERNAL_ERROR);
295 * If we haven't found the name yet, chances are that the algorithm to
296 * be fetched is unsupported.
302 || !ossl_method_store_cache_get(store, prov, meth_id, properties,
304 OSSL_METHOD_CONSTRUCT_METHOD mcm = {
305 get_tmp_evp_method_store,
306 get_evp_method_from_store,
307 put_evp_method_in_store,
308 construct_evp_method,
312 methdata->operation_id = operation_id;
313 methdata->name_id = name_id;
314 methdata->names = name;
315 methdata->propquery = properties;
316 methdata->method_from_algorithm = new_method;
317 methdata->refcnt_up_method = up_ref_method;
318 methdata->destruct_method = free_method;
319 methdata->flag_construct_error_occurred = 0;
320 if ((method = ossl_method_construct(methdata->libctx, operation_id,
321 &prov, 0 /* !force_cache */,
322 &mcm, methdata)) != NULL) {
324 * If construction did create a method for us, we know that
325 * there is a correct name_id and meth_id, since those have
326 * already been calculated in get_evp_method_from_store() and
327 * put_evp_method_in_store() above.
330 name_id = ossl_namemap_name2num(namemap, name);
331 meth_id = evp_method_id(name_id, operation_id);
333 ossl_method_store_cache_set(store, prov, meth_id, properties,
334 method, up_ref_method, free_method);
338 * If we never were in the constructor, the algorithm to be fetched
341 unsupported = !methdata->flag_construct_error_occurred;
344 if ((name_id != 0 || name != NULL) && method == NULL) {
345 int code = unsupported ? ERR_R_UNSUPPORTED : ERR_R_FETCH_FAILED;
348 name = ossl_namemap_num2name(namemap, name_id, 0);
349 ERR_raise_data(ERR_LIB_EVP, code,
350 "%s, Algorithm (%s : %d), Properties (%s)",
351 ossl_lib_ctx_get_descriptor(methdata->libctx),
352 name = NULL ? "<null>" : name, name_id,
353 properties == NULL ? "<null>" : properties);
359 void *evp_generic_fetch(OSSL_LIB_CTX *libctx, int operation_id,
360 const char *name, const char *properties,
361 void *(*new_method)(int name_id,
362 const OSSL_ALGORITHM *algodef,
363 OSSL_PROVIDER *prov),
364 int (*up_ref_method)(void *),
365 void (*free_method)(void *))
367 struct evp_method_data_st methdata;
370 methdata.libctx = libctx;
371 methdata.tmp_store = NULL;
372 method = inner_evp_generic_fetch(&methdata, NULL, operation_id,
374 new_method, up_ref_method, free_method);
375 dealloc_tmp_evp_method_store(methdata.tmp_store);
380 * evp_generic_fetch_by_number() is special, and only returns methods for
381 * already known names, i.e. it refuses to work if no name_id can be found
382 * (it's considered an internal programming error).
383 * This is meant to be used when one method needs to fetch an associated
386 void *evp_generic_fetch_by_number(OSSL_LIB_CTX *libctx, int operation_id,
387 int name_id, const char *properties,
388 void *(*new_method)(int name_id,
389 const OSSL_ALGORITHM *algodef,
390 OSSL_PROVIDER *prov),
391 int (*up_ref_method)(void *),
392 void (*free_method)(void *))
394 struct evp_method_data_st methdata;
397 methdata.libctx = libctx;
398 methdata.tmp_store = NULL;
399 method = inner_evp_generic_fetch(&methdata, NULL, operation_id,
400 name_id, NULL, properties,
401 new_method, up_ref_method, free_method);
402 dealloc_tmp_evp_method_store(methdata.tmp_store);
407 * evp_generic_fetch_from_prov() is special, and only returns methods from
408 * the given provider.
409 * This is meant to be used when one method needs to fetch an associated
412 void *evp_generic_fetch_from_prov(OSSL_PROVIDER *prov, int operation_id,
413 const char *name, const char *properties,
414 void *(*new_method)(int name_id,
415 const OSSL_ALGORITHM *algodef,
416 OSSL_PROVIDER *prov),
417 int (*up_ref_method)(void *),
418 void (*free_method)(void *))
420 struct evp_method_data_st methdata;
423 methdata.libctx = ossl_provider_libctx(prov);
424 methdata.tmp_store = NULL;
425 method = inner_evp_generic_fetch(&methdata, prov, operation_id,
427 new_method, up_ref_method, free_method);
428 dealloc_tmp_evp_method_store(methdata.tmp_store);
432 int evp_method_store_flush(OSSL_LIB_CTX *libctx)
434 OSSL_METHOD_STORE *store = get_evp_method_store(libctx);
437 return ossl_method_store_flush_cache(store, 1);
441 static int evp_set_parsed_default_properties(OSSL_LIB_CTX *libctx,
442 OSSL_PROPERTY_LIST *def_prop,
446 OSSL_METHOD_STORE *store = get_evp_method_store(libctx);
447 OSSL_PROPERTY_LIST **plp = ossl_ctx_global_properties(libctx, loadconfig);
449 if (plp != NULL && store != NULL) {
451 char *propstr = NULL;
455 if (ossl_global_properties_no_mirrored(libctx))
459 * These properties have been explicitly set on this libctx, so
460 * don't allow any mirroring from a parent libctx.
462 ossl_global_properties_stop_mirroring(libctx);
465 strsz = ossl_property_list_to_string(libctx, def_prop, NULL, 0);
467 propstr = OPENSSL_malloc(strsz);
468 if (propstr == NULL) {
469 ERR_raise(ERR_LIB_EVP, ERR_R_INTERNAL_ERROR);
472 if (ossl_property_list_to_string(libctx, def_prop, propstr,
474 OPENSSL_free(propstr);
475 ERR_raise(ERR_LIB_EVP, ERR_R_INTERNAL_ERROR);
478 ossl_provider_default_props_update(libctx, propstr);
479 OPENSSL_free(propstr);
481 ossl_property_free(*plp);
484 return ossl_method_store_flush_cache(store, 0);
486 ERR_raise(ERR_LIB_EVP, ERR_R_INTERNAL_ERROR);
490 int evp_set_default_properties_int(OSSL_LIB_CTX *libctx, const char *propq,
491 int loadconfig, int mirrored)
493 OSSL_PROPERTY_LIST *pl = NULL;
495 if (propq != NULL && (pl = ossl_parse_query(libctx, propq, 1)) == NULL) {
496 ERR_raise(ERR_LIB_EVP, EVP_R_DEFAULT_QUERY_PARSE_ERROR);
499 if (!evp_set_parsed_default_properties(libctx, pl, loadconfig, mirrored)) {
500 ossl_property_free(pl);
506 int EVP_set_default_properties(OSSL_LIB_CTX *libctx, const char *propq)
508 return evp_set_default_properties_int(libctx, propq, 1, 0);
511 static int evp_default_properties_merge(OSSL_LIB_CTX *libctx, const char *propq,
514 OSSL_PROPERTY_LIST **plp = ossl_ctx_global_properties(libctx, loadconfig);
515 OSSL_PROPERTY_LIST *pl1, *pl2;
519 if (plp == NULL || *plp == NULL)
520 return evp_set_default_properties_int(libctx, propq, 0, 0);
521 if ((pl1 = ossl_parse_query(libctx, propq, 1)) == NULL) {
522 ERR_raise(ERR_LIB_EVP, EVP_R_DEFAULT_QUERY_PARSE_ERROR);
525 pl2 = ossl_property_merge(pl1, *plp);
526 ossl_property_free(pl1);
528 ERR_raise(ERR_LIB_EVP, ERR_R_MALLOC_FAILURE);
531 if (!evp_set_parsed_default_properties(libctx, pl2, 0, 0)) {
532 ossl_property_free(pl2);
538 static int evp_default_property_is_enabled(OSSL_LIB_CTX *libctx,
539 const char *prop_name)
541 OSSL_PROPERTY_LIST **plp = ossl_ctx_global_properties(libctx, 1);
543 return plp != NULL && ossl_property_is_enabled(libctx, prop_name, *plp);
546 int EVP_default_properties_is_fips_enabled(OSSL_LIB_CTX *libctx)
548 return evp_default_property_is_enabled(libctx, "fips");
551 int evp_default_properties_enable_fips_int(OSSL_LIB_CTX *libctx, int enable,
554 const char *query = (enable != 0) ? "fips=yes" : "-fips";
556 return evp_default_properties_merge(libctx, query, loadconfig);
559 int EVP_default_properties_enable_fips(OSSL_LIB_CTX *libctx, int enable)
561 return evp_default_properties_enable_fips_int(libctx, enable, 1);
564 char *evp_get_global_properties_str(OSSL_LIB_CTX *libctx, int loadconfig)
566 OSSL_PROPERTY_LIST **plp = ossl_ctx_global_properties(libctx, loadconfig);
567 char *propstr = NULL;
571 return OPENSSL_strdup("");
573 sz = ossl_property_list_to_string(libctx, *plp, NULL, 0);
575 ERR_raise(ERR_LIB_EVP, ERR_R_INTERNAL_ERROR);
579 propstr = OPENSSL_malloc(sz);
580 if (propstr == NULL) {
581 ERR_raise(ERR_LIB_EVP, ERR_R_MALLOC_FAILURE);
584 if (ossl_property_list_to_string(libctx, *plp, propstr, sz) == 0) {
585 ERR_raise(ERR_LIB_EVP, ERR_R_INTERNAL_ERROR);
586 OPENSSL_free(propstr);
592 struct filter_data_st {
594 void (*user_fn)(void *method, void *arg);
598 static void filter_on_operation_id(int id, void *method, void *arg)
600 struct filter_data_st *data = arg;
602 if ((id & METHOD_ID_OPERATION_MASK) == data->operation_id)
603 data->user_fn(method, data->user_arg);
606 void evp_generic_do_all(OSSL_LIB_CTX *libctx, int operation_id,
607 void (*user_fn)(void *method, void *arg),
609 void *(*new_method)(int name_id,
610 const OSSL_ALGORITHM *algodef,
611 OSSL_PROVIDER *prov),
612 int (*up_ref_method)(void *),
613 void (*free_method)(void *))
615 struct evp_method_data_st methdata;
616 struct filter_data_st data;
618 methdata.libctx = libctx;
619 methdata.tmp_store = NULL;
620 (void)inner_evp_generic_fetch(&methdata, NULL, operation_id, 0, NULL, NULL,
621 new_method, up_ref_method, free_method);
623 data.operation_id = operation_id;
624 data.user_fn = user_fn;
625 data.user_arg = user_arg;
626 if (methdata.tmp_store != NULL)
627 ossl_method_store_do_all(methdata.tmp_store, &filter_on_operation_id,
629 ossl_method_store_do_all(get_evp_method_store(libctx),
630 &filter_on_operation_id, &data);
631 dealloc_tmp_evp_method_store(methdata.tmp_store);
634 int evp_is_a(OSSL_PROVIDER *prov, int number,
635 const char *legacy_name, const char *name)
638 * For a |prov| that is NULL, the library context will be NULL
640 OSSL_LIB_CTX *libctx = ossl_provider_libctx(prov);
641 OSSL_NAMEMAP *namemap = ossl_namemap_stored(libctx);
644 number = ossl_namemap_name2num(namemap, legacy_name);
645 return ossl_namemap_name2num(namemap, name) == number;
648 int evp_names_do_all(OSSL_PROVIDER *prov, int number,
649 void (*fn)(const char *name, void *data),
652 OSSL_LIB_CTX *libctx = ossl_provider_libctx(prov);
653 OSSL_NAMEMAP *namemap = ossl_namemap_stored(libctx);
655 return ossl_namemap_doall_names(namemap, number, fn, data);