2 * Copyright 2019-2023 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 "crypto/decoder.h"
21 #include "crypto/evp.h" /* evp_local.h needs it */
22 #include "evp_local.h"
24 #define NAME_SEPARATOR ':'
26 /* Data to be passed through ossl_method_construct() */
27 struct evp_method_data_st
{
29 int operation_id
; /* For get_evp_method_from_store() */
30 int name_id
; /* For get_evp_method_from_store() */
31 const char *names
; /* For get_evp_method_from_store() */
32 const char *propquery
; /* For get_evp_method_from_store() */
34 OSSL_METHOD_STORE
*tmp_store
; /* For get_tmp_evp_method_store() */
36 unsigned int flag_construct_error_occurred
: 1;
38 void *(*method_from_algorithm
)(int name_id
, const OSSL_ALGORITHM
*,
40 int (*refcnt_up_method
)(void *method
);
41 void (*destruct_method
)(void *method
);
45 * Generic routines to fetch / create EVP methods with ossl_method_construct()
47 static void *get_tmp_evp_method_store(void *data
)
49 struct evp_method_data_st
*methdata
= data
;
51 if (methdata
->tmp_store
== NULL
)
52 methdata
->tmp_store
= ossl_method_store_new(methdata
->libctx
);
53 return methdata
->tmp_store
;
56 static void dealloc_tmp_evp_method_store(void *store
)
59 ossl_method_store_free(store
);
62 static OSSL_METHOD_STORE
*get_evp_method_store(OSSL_LIB_CTX
*libctx
)
64 return ossl_lib_ctx_get_data(libctx
, OSSL_LIB_CTX_EVP_METHOD_STORE_INDEX
);
67 static int reserve_evp_method_store(void *store
, void *data
)
69 struct evp_method_data_st
*methdata
= data
;
72 && (store
= get_evp_method_store(methdata
->libctx
)) == NULL
)
75 return ossl_method_lock_store(store
);
78 static int unreserve_evp_method_store(void *store
, void *data
)
80 struct evp_method_data_st
*methdata
= data
;
83 && (store
= get_evp_method_store(methdata
->libctx
)) == NULL
)
86 return ossl_method_unlock_store(store
);
90 * To identify the method in the EVP method store, we mix the name identity
91 * with the operation identity, under the assumption that we don't have more
92 * than 2^23 names or more than 2^8 operation types.
94 * The resulting identity is a 31-bit integer, composed like this:
96 * +---------23 bits--------+-8 bits-+
97 * | name identity | op id |
98 * +------------------------+--------+
100 * We limit this composite number to 31 bits, thus leaving the top uint32_t
101 * bit always zero, to avoid negative sign extension when downshifting after
102 * this number happens to be passed to an int (which happens as soon as it's
103 * passed to ossl_method_store_cache_set(), and it's in that form that it
104 * gets passed along to filter_on_operation_id(), defined further down.
106 #define METHOD_ID_OPERATION_MASK 0x000000FF
107 #define METHOD_ID_OPERATION_MAX ((1 << 8) - 1)
108 #define METHOD_ID_NAME_MASK 0x7FFFFF00
109 #define METHOD_ID_NAME_OFFSET 8
110 #define METHOD_ID_NAME_MAX ((1 << 23) - 1)
111 static uint32_t evp_method_id(int name_id
, unsigned int operation_id
)
113 if (!ossl_assert(name_id
> 0 && name_id
<= METHOD_ID_NAME_MAX
)
114 || !ossl_assert(operation_id
> 0
115 && operation_id
<= METHOD_ID_OPERATION_MAX
))
117 return (((name_id
<< METHOD_ID_NAME_OFFSET
) & METHOD_ID_NAME_MASK
)
118 | (operation_id
& METHOD_ID_OPERATION_MASK
));
121 static void *get_evp_method_from_store(void *store
, const OSSL_PROVIDER
**prov
,
124 struct evp_method_data_st
*methdata
= data
;
130 * get_evp_method_from_store() is only called to try and get the method
131 * that evp_generic_fetch() is asking for, and the operation id as well
132 * as the name or name id are passed via methdata.
134 if ((name_id
= methdata
->name_id
) == 0 && methdata
->names
!= NULL
) {
135 OSSL_NAMEMAP
*namemap
= ossl_namemap_stored(methdata
->libctx
);
136 const char *names
= methdata
->names
;
137 const char *q
= strchr(names
, NAME_SEPARATOR
);
138 size_t l
= (q
== NULL
? strlen(names
) : (size_t)(q
- names
));
142 name_id
= ossl_namemap_name2num_n(namemap
, names
, l
);
146 || (meth_id
= evp_method_id(name_id
, methdata
->operation_id
)) == 0)
150 && (store
= get_evp_method_store(methdata
->libctx
)) == NULL
)
153 if (!ossl_method_store_fetch(store
, meth_id
, methdata
->propquery
, prov
,
159 static int put_evp_method_in_store(void *store
, void *method
,
160 const OSSL_PROVIDER
*prov
,
161 const char *names
, const char *propdef
,
164 struct evp_method_data_st
*methdata
= data
;
165 OSSL_NAMEMAP
*namemap
;
171 * put_evp_method_in_store() is only called with an EVP method that was
172 * successfully created by construct_method() below, which means that
173 * all the names should already be stored in the namemap with the same
174 * numeric identity, so just use the first to get that identity.
177 const char *q
= strchr(names
, NAME_SEPARATOR
);
179 l
= (q
== NULL
? strlen(names
) : (size_t)(q
- names
));
182 if ((namemap
= ossl_namemap_stored(methdata
->libctx
)) == NULL
183 || (name_id
= ossl_namemap_name2num_n(namemap
, names
, l
)) == 0
184 || (meth_id
= evp_method_id(name_id
, methdata
->operation_id
)) == 0)
188 && (store
= get_evp_method_store(methdata
->libctx
)) == NULL
)
191 return ossl_method_store_add(store
, prov
, meth_id
, propdef
, method
,
192 methdata
->refcnt_up_method
,
193 methdata
->destruct_method
);
197 * The core fetching functionality passes the name of the implementation.
198 * This function is responsible to getting an identity number for it.
200 static void *construct_evp_method(const OSSL_ALGORITHM
*algodef
,
201 OSSL_PROVIDER
*prov
, void *data
)
204 * This function is only called if get_evp_method_from_store() returned
205 * NULL, so it's safe to say that of all the spots to create a new
206 * namemap entry, this is it. Should the name already exist there, we
207 * know that ossl_namemap_add_name() will return its corresponding
210 struct evp_method_data_st
*methdata
= data
;
211 OSSL_LIB_CTX
*libctx
= ossl_provider_libctx(prov
);
212 OSSL_NAMEMAP
*namemap
= ossl_namemap_stored(libctx
);
213 const char *names
= algodef
->algorithm_names
;
214 int name_id
= ossl_namemap_add_names(namemap
, 0, names
, NAME_SEPARATOR
);
220 method
= methdata
->method_from_algorithm(name_id
, algodef
, prov
);
223 * Flag to indicate that there was actual construction errors. This
224 * helps inner_evp_generic_fetch() determine what error it should
225 * record on inaccessible algorithms.
228 methdata
->flag_construct_error_occurred
= 1;
233 static void destruct_evp_method(void *method
, void *data
)
235 struct evp_method_data_st
*methdata
= data
;
237 methdata
->destruct_method(method
);
241 inner_evp_generic_fetch(struct evp_method_data_st
*methdata
,
242 OSSL_PROVIDER
*prov
, int operation_id
,
243 const char *name
, const char *properties
,
244 void *(*new_method
)(int name_id
,
245 const OSSL_ALGORITHM
*algodef
,
246 OSSL_PROVIDER
*prov
),
247 int (*up_ref_method
)(void *),
248 void (*free_method
)(void *))
250 OSSL_METHOD_STORE
*store
= get_evp_method_store(methdata
->libctx
);
251 OSSL_NAMEMAP
*namemap
= ossl_namemap_stored(methdata
->libctx
);
252 const char *const propq
= properties
!= NULL
? properties
: "";
253 uint32_t meth_id
= 0;
255 int unsupported
, name_id
;
257 if (store
== NULL
|| namemap
== NULL
) {
258 ERR_raise(ERR_LIB_EVP
, ERR_R_PASSED_INVALID_ARGUMENT
);
263 * If there's ever an operation_id == 0 passed, we have an internal
266 if (!ossl_assert(operation_id
> 0)) {
267 ERR_raise(ERR_LIB_EVP
, ERR_R_INTERNAL_ERROR
);
271 /* If we haven't received a name id yet, try to get one for the name */
272 name_id
= name
!= NULL
? ossl_namemap_name2num(namemap
, name
) : 0;
275 * If we have a name id, calculate a method id with evp_method_id().
277 * evp_method_id returns 0 if we have too many operations (more than
278 * about 2^8) or too many names (more than about 2^24). In that case,
279 * we can't create any new method.
280 * For all intents and purposes, this is an internal error.
282 if (name_id
!= 0 && (meth_id
= evp_method_id(name_id
, operation_id
)) == 0) {
283 ERR_raise(ERR_LIB_EVP
, ERR_R_INTERNAL_ERROR
);
288 * If we haven't found the name yet, chances are that the algorithm to
289 * be fetched is unsupported.
291 unsupported
= name_id
== 0;
294 || !ossl_method_store_cache_get(store
, prov
, meth_id
, propq
, &method
)) {
295 OSSL_METHOD_CONSTRUCT_METHOD mcm
= {
296 get_tmp_evp_method_store
,
297 reserve_evp_method_store
,
298 unreserve_evp_method_store
,
299 get_evp_method_from_store
,
300 put_evp_method_in_store
,
301 construct_evp_method
,
305 methdata
->operation_id
= operation_id
;
306 methdata
->name_id
= name_id
;
307 methdata
->names
= name
;
308 methdata
->propquery
= propq
;
309 methdata
->method_from_algorithm
= new_method
;
310 methdata
->refcnt_up_method
= up_ref_method
;
311 methdata
->destruct_method
= free_method
;
312 methdata
->flag_construct_error_occurred
= 0;
313 if ((method
= ossl_method_construct(methdata
->libctx
, operation_id
,
314 &prov
, 0 /* !force_cache */,
315 &mcm
, methdata
)) != NULL
) {
317 * If construction did create a method for us, we know that
318 * there is a correct name_id and meth_id, since those have
319 * already been calculated in get_evp_method_from_store() and
320 * put_evp_method_in_store() above.
323 name_id
= ossl_namemap_name2num(namemap
, name
);
324 meth_id
= evp_method_id(name_id
, operation_id
);
326 ossl_method_store_cache_set(store
, prov
, meth_id
, propq
,
327 method
, up_ref_method
, free_method
);
331 * If we never were in the constructor, the algorithm to be fetched
334 unsupported
= !methdata
->flag_construct_error_occurred
;
337 if ((name_id
!= 0 || name
!= NULL
) && method
== NULL
) {
338 int code
= unsupported
? ERR_R_UNSUPPORTED
: ERR_R_FETCH_FAILED
;
341 name
= ossl_namemap_num2name(namemap
, name_id
, 0);
342 ERR_raise_data(ERR_LIB_EVP
, code
,
343 "%s, Algorithm (%s : %d), Properties (%s)",
344 ossl_lib_ctx_get_descriptor(methdata
->libctx
),
345 name
== NULL
? "<null>" : name
, name_id
,
346 properties
== NULL
? "<null>" : properties
);
352 void *evp_generic_fetch(OSSL_LIB_CTX
*libctx
, int operation_id
,
353 const char *name
, const char *properties
,
354 void *(*new_method
)(int name_id
,
355 const OSSL_ALGORITHM
*algodef
,
356 OSSL_PROVIDER
*prov
),
357 int (*up_ref_method
)(void *),
358 void (*free_method
)(void *))
360 struct evp_method_data_st methdata
;
363 methdata
.libctx
= libctx
;
364 methdata
.tmp_store
= NULL
;
365 method
= inner_evp_generic_fetch(&methdata
, NULL
, operation_id
,
367 new_method
, up_ref_method
, free_method
);
368 dealloc_tmp_evp_method_store(methdata
.tmp_store
);
373 * evp_generic_fetch_from_prov() is special, and only returns methods from
374 * the given provider.
375 * This is meant to be used when one method needs to fetch an associated
378 void *evp_generic_fetch_from_prov(OSSL_PROVIDER
*prov
, int operation_id
,
379 const char *name
, const char *properties
,
380 void *(*new_method
)(int name_id
,
381 const OSSL_ALGORITHM
*algodef
,
382 OSSL_PROVIDER
*prov
),
383 int (*up_ref_method
)(void *),
384 void (*free_method
)(void *))
386 struct evp_method_data_st methdata
;
389 methdata
.libctx
= ossl_provider_libctx(prov
);
390 methdata
.tmp_store
= NULL
;
391 method
= inner_evp_generic_fetch(&methdata
, prov
, operation_id
,
393 new_method
, up_ref_method
, free_method
);
394 dealloc_tmp_evp_method_store(methdata
.tmp_store
);
398 int evp_method_store_cache_flush(OSSL_LIB_CTX
*libctx
)
400 OSSL_METHOD_STORE
*store
= get_evp_method_store(libctx
);
403 return ossl_method_store_cache_flush_all(store
);
407 int evp_method_store_remove_all_provided(const OSSL_PROVIDER
*prov
)
409 OSSL_LIB_CTX
*libctx
= ossl_provider_libctx(prov
);
410 OSSL_METHOD_STORE
*store
= get_evp_method_store(libctx
);
413 return ossl_method_store_remove_all_provided(store
, prov
);
417 static int evp_set_parsed_default_properties(OSSL_LIB_CTX
*libctx
,
418 OSSL_PROPERTY_LIST
*def_prop
,
422 OSSL_METHOD_STORE
*store
= get_evp_method_store(libctx
);
423 OSSL_PROPERTY_LIST
**plp
= ossl_ctx_global_properties(libctx
, loadconfig
);
425 if (plp
!= NULL
&& store
!= NULL
) {
428 char *propstr
= NULL
;
432 if (ossl_global_properties_no_mirrored(libctx
))
436 * These properties have been explicitly set on this libctx, so
437 * don't allow any mirroring from a parent libctx.
439 ossl_global_properties_stop_mirroring(libctx
);
442 strsz
= ossl_property_list_to_string(libctx
, def_prop
, NULL
, 0);
444 propstr
= OPENSSL_malloc(strsz
);
445 if (propstr
== NULL
) {
446 ERR_raise(ERR_LIB_EVP
, ERR_R_INTERNAL_ERROR
);
449 if (ossl_property_list_to_string(libctx
, def_prop
, propstr
,
451 OPENSSL_free(propstr
);
452 ERR_raise(ERR_LIB_EVP
, ERR_R_INTERNAL_ERROR
);
455 ossl_provider_default_props_update(libctx
, propstr
);
456 OPENSSL_free(propstr
);
458 ossl_property_free(*plp
);
461 ret
= ossl_method_store_cache_flush_all(store
);
463 ossl_decoder_cache_flush(libctx
);
467 ERR_raise(ERR_LIB_EVP
, ERR_R_INTERNAL_ERROR
);
471 int evp_set_default_properties_int(OSSL_LIB_CTX
*libctx
, const char *propq
,
472 int loadconfig
, int mirrored
)
474 OSSL_PROPERTY_LIST
*pl
= NULL
;
476 if (propq
!= NULL
&& (pl
= ossl_parse_query(libctx
, propq
, 1)) == NULL
) {
477 ERR_raise(ERR_LIB_EVP
, EVP_R_DEFAULT_QUERY_PARSE_ERROR
);
480 if (!evp_set_parsed_default_properties(libctx
, pl
, loadconfig
, mirrored
)) {
481 ossl_property_free(pl
);
487 int EVP_set_default_properties(OSSL_LIB_CTX
*libctx
, const char *propq
)
489 return evp_set_default_properties_int(libctx
, propq
, 1, 0);
492 static int evp_default_properties_merge(OSSL_LIB_CTX
*libctx
, const char *propq
,
495 OSSL_PROPERTY_LIST
**plp
= ossl_ctx_global_properties(libctx
, loadconfig
);
496 OSSL_PROPERTY_LIST
*pl1
, *pl2
;
500 if (plp
== NULL
|| *plp
== NULL
)
501 return evp_set_default_properties_int(libctx
, propq
, 0, 0);
502 if ((pl1
= ossl_parse_query(libctx
, propq
, 1)) == NULL
) {
503 ERR_raise(ERR_LIB_EVP
, EVP_R_DEFAULT_QUERY_PARSE_ERROR
);
506 pl2
= ossl_property_merge(pl1
, *plp
);
507 ossl_property_free(pl1
);
509 ERR_raise(ERR_LIB_EVP
, ERR_R_CRYPTO_LIB
);
512 if (!evp_set_parsed_default_properties(libctx
, pl2
, 0, 0)) {
513 ossl_property_free(pl2
);
519 static int evp_default_property_is_enabled(OSSL_LIB_CTX
*libctx
,
520 const char *prop_name
)
522 OSSL_PROPERTY_LIST
**plp
= ossl_ctx_global_properties(libctx
, 1);
524 return plp
!= NULL
&& ossl_property_is_enabled(libctx
, prop_name
, *plp
);
527 int EVP_default_properties_is_fips_enabled(OSSL_LIB_CTX
*libctx
)
529 return evp_default_property_is_enabled(libctx
, "fips");
532 int evp_default_properties_enable_fips_int(OSSL_LIB_CTX
*libctx
, int enable
,
535 const char *query
= (enable
!= 0) ? "fips=yes" : "-fips";
537 return evp_default_properties_merge(libctx
, query
, loadconfig
);
540 int EVP_default_properties_enable_fips(OSSL_LIB_CTX
*libctx
, int enable
)
542 return evp_default_properties_enable_fips_int(libctx
, enable
, 1);
545 char *evp_get_global_properties_str(OSSL_LIB_CTX
*libctx
, int loadconfig
)
547 OSSL_PROPERTY_LIST
**plp
= ossl_ctx_global_properties(libctx
, loadconfig
);
548 char *propstr
= NULL
;
552 return OPENSSL_strdup("");
554 sz
= ossl_property_list_to_string(libctx
, *plp
, NULL
, 0);
556 ERR_raise(ERR_LIB_EVP
, ERR_R_INTERNAL_ERROR
);
560 propstr
= OPENSSL_malloc(sz
);
563 if (ossl_property_list_to_string(libctx
, *plp
, propstr
, sz
) == 0) {
564 ERR_raise(ERR_LIB_EVP
, ERR_R_INTERNAL_ERROR
);
565 OPENSSL_free(propstr
);
571 struct filter_data_st
{
573 void (*user_fn
)(void *method
, void *arg
);
577 static void filter_on_operation_id(int id
, void *method
, void *arg
)
579 struct filter_data_st
*data
= arg
;
581 if ((id
& METHOD_ID_OPERATION_MASK
) == data
->operation_id
)
582 data
->user_fn(method
, data
->user_arg
);
585 void evp_generic_do_all(OSSL_LIB_CTX
*libctx
, int operation_id
,
586 void (*user_fn
)(void *method
, void *arg
),
588 void *(*new_method
)(int name_id
,
589 const OSSL_ALGORITHM
*algodef
,
590 OSSL_PROVIDER
*prov
),
591 int (*up_ref_method
)(void *),
592 void (*free_method
)(void *))
594 struct evp_method_data_st methdata
;
595 struct filter_data_st data
;
597 methdata
.libctx
= libctx
;
598 methdata
.tmp_store
= NULL
;
599 (void)inner_evp_generic_fetch(&methdata
, NULL
, operation_id
, NULL
, NULL
,
600 new_method
, up_ref_method
, free_method
);
602 data
.operation_id
= operation_id
;
603 data
.user_fn
= user_fn
;
604 data
.user_arg
= user_arg
;
605 if (methdata
.tmp_store
!= NULL
)
606 ossl_method_store_do_all(methdata
.tmp_store
, &filter_on_operation_id
,
608 ossl_method_store_do_all(get_evp_method_store(libctx
),
609 &filter_on_operation_id
, &data
);
610 dealloc_tmp_evp_method_store(methdata
.tmp_store
);
613 int evp_is_a(OSSL_PROVIDER
*prov
, int number
,
614 const char *legacy_name
, const char *name
)
617 * For a |prov| that is NULL, the library context will be NULL
619 OSSL_LIB_CTX
*libctx
= ossl_provider_libctx(prov
);
620 OSSL_NAMEMAP
*namemap
= ossl_namemap_stored(libctx
);
623 number
= ossl_namemap_name2num(namemap
, legacy_name
);
624 return ossl_namemap_name2num(namemap
, name
) == number
;
627 int evp_names_do_all(OSSL_PROVIDER
*prov
, int number
,
628 void (*fn
)(const char *name
, void *data
),
631 OSSL_LIB_CTX
*libctx
= ossl_provider_libctx(prov
);
632 OSSL_NAMEMAP
*namemap
= ossl_namemap_stored(libctx
);
634 return ossl_namemap_doall_names(namemap
, number
, fn
, data
);