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