]> git.ipfire.org Git - thirdparty/openssl.git/blame - crypto/evp/evp_fetch.c
fetch: convert a NULL property query to ""
[thirdparty/openssl.git] / crypto / evp / evp_fetch.c
CommitLineData
c13d2ab4 1/*
4333b89f 2 * Copyright 2019-2021 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"
04cb5ec0 20#include "internal/property.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
181ea366 26static void evp_method_store_free(void *vstore)
c13d2ab4
RL
27{
28 ossl_method_store_free(vstore);
29}
30
b4250010 31static void *evp_method_store_new(OSSL_LIB_CTX *ctx)
c13d2ab4 32{
1aedc35f 33 return ossl_method_store_new(ctx);
c13d2ab4
RL
34}
35
36
b4250010 37static const OSSL_LIB_CTX_METHOD evp_method_store_method = {
a16d2174 38 /* We want evp_method_store to be cleaned up before the provider store */
8c627075 39 OSSL_LIB_CTX_METHOD_PRIORITY_2,
181ea366
RL
40 evp_method_store_new,
41 evp_method_store_free,
c13d2ab4
RL
42};
43
c13d2ab4 44/* Data to be passed through ossl_method_construct() */
181ea366 45struct evp_method_data_st {
b4250010 46 OSSL_LIB_CTX *libctx;
181ea366
RL
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() */
ec0ce188 51
9067cf6c
RL
52 OSSL_METHOD_STORE *tmp_store; /* For get_tmp_evp_method_store() */
53
e3a2ba75 54 unsigned int flag_construct_error_occurred : 1;
ec0ce188 55
309a78aa
RL
56 void *(*method_from_algorithm)(int name_id, const OSSL_ALGORITHM *,
57 OSSL_PROVIDER *);
c13d2ab4
RL
58 int (*refcnt_up_method)(void *method);
59 void (*destruct_method)(void *method);
60};
61
62/*
63 * Generic routines to fetch / create EVP methods with ossl_method_construct()
64 */
9067cf6c 65static void *get_tmp_evp_method_store(void *data)
c13d2ab4 66{
9067cf6c
RL
67 struct evp_method_data_st *methdata = data;
68
69 if (methdata->tmp_store == NULL)
70 methdata->tmp_store = ossl_method_store_new(methdata->libctx);
71 return methdata->tmp_store;
c13d2ab4
RL
72}
73
181ea366 74 static void dealloc_tmp_evp_method_store(void *store)
c13d2ab4
RL
75{
76 if (store != NULL)
77 ossl_method_store_free(store);
78}
79
b4250010 80static OSSL_METHOD_STORE *get_evp_method_store(OSSL_LIB_CTX *libctx)
c13d2ab4 81{
b4250010
DMSP
82 return ossl_lib_ctx_get_data(libctx, OSSL_LIB_CTX_EVP_METHOD_STORE_INDEX,
83 &evp_method_store_method);
c13d2ab4
RL
84}
85
2ccb1b4e 86/*
64f849f4
DMSP
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
793b0586 89 * than 2^23 names or more than 2^8 operation types.
2ccb1b4e 90 *
793b0586 91 * The resulting identity is a 31-bit integer, composed like this:
2ccb1b4e 92 *
793b0586 93 * +---------23 bits--------+-8 bits-+
2ccb1b4e
RL
94 * | name identity | op id |
95 * +------------------------+--------+
793b0586
RL
96 *
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.
2ccb1b4e 102 */
793b0586
RL
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)
b418980c 108static uint32_t evp_method_id(int name_id, unsigned int operation_id)
2ccb1b4e 109{
793b0586
RL
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))
2ccb1b4e 113 return 0;
793b0586
RL
114 return (((name_id << METHOD_ID_NAME_OFFSET) & METHOD_ID_NAME_MASK)
115 | (operation_id & METHOD_ID_OPERATION_MASK));
2ccb1b4e
RL
116}
117
dc010ca6
RL
118static void *get_evp_method_from_store(void *store, const OSSL_PROVIDER **prov,
119 void *data)
c13d2ab4 120{
181ea366 121 struct evp_method_data_st *methdata = data;
c13d2ab4 122 void *method = NULL;
793b0586 123 int name_id = 0;
f7c16d48 124 uint32_t meth_id;
c13d2ab4 125
f7c16d48 126 /*
181ea366
RL
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.
f7c16d48 130 */
793b0586 131 if ((name_id = methdata->name_id) == 0 && methdata->names != NULL) {
6882652e 132 OSSL_NAMEMAP *namemap = ossl_namemap_stored(methdata->libctx);
695d195b
RL
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));
f7c16d48
RL
136
137 if (namemap == 0)
138 return NULL;
695d195b 139 name_id = ossl_namemap_name2num_n(namemap, names, l);
f7c16d48
RL
140 }
141
142 if (name_id == 0
b418980c 143 || (meth_id = evp_method_id(name_id, methdata->operation_id)) == 0)
c13d2ab4
RL
144 return NULL;
145
f7c16d48 146 if (store == NULL
6882652e 147 && (store = get_evp_method_store(methdata->libctx)) == NULL)
2e49c054
RL
148 return NULL;
149
dc010ca6 150 if (!ossl_method_store_fetch(store, meth_id, methdata->propquery, prov,
bdbf2df2
P
151 &method))
152 return NULL;
c13d2ab4
RL
153 return method;
154}
155
6882652e
RL
156static int put_evp_method_in_store(void *store, void *method,
157 const OSSL_PROVIDER *prov,
158 const char *names, const char *propdef,
159 void *data)
c13d2ab4 160{
181ea366 161 struct evp_method_data_st *methdata = data;
2e49c054 162 OSSL_NAMEMAP *namemap;
f7c16d48
RL
163 int name_id;
164 uint32_t meth_id;
695d195b 165 size_t l = 0;
dc46e3dd 166
f7c16d48 167 /*
181ea366
RL
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.
f7c16d48 172 */
695d195b
RL
173 if (names != NULL) {
174 const char *q = strchr(names, NAME_SEPARATOR);
175
176 l = (q == NULL ? strlen(names) : (size_t)(q - names));
177 }
178
6882652e 179 if ((namemap = ossl_namemap_stored(methdata->libctx)) == NULL
695d195b 180 || (name_id = ossl_namemap_name2num_n(namemap, names, l)) == 0
6882652e 181 || (meth_id = evp_method_id(name_id, methdata->operation_id)) == 0)
dc46e3dd 182 return 0;
c13d2ab4
RL
183
184 if (store == NULL
6882652e 185 && (store = get_evp_method_store(methdata->libctx)) == NULL)
c13d2ab4
RL
186 return 0;
187
f7c16d48 188 return ossl_method_store_add(store, prov, meth_id, propdef, method,
b1d40ddf
RL
189 methdata->refcnt_up_method,
190 methdata->destruct_method);
c13d2ab4
RL
191}
192
f7c16d48
RL
193/*
194 * The core fetching functionality passes the name of the implementation.
195 * This function is responsible to getting an identity number for it.
196 */
36fa4d8a 197static void *construct_evp_method(const OSSL_ALGORITHM *algodef,
181ea366 198 OSSL_PROVIDER *prov, void *data)
c13d2ab4 199{
f7c16d48 200 /*
181ea366 201 * This function is only called if get_evp_method_from_store() returned
f7c16d48
RL
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
3d83c735
RL
204 * know that ossl_namemap_add_name() will return its corresponding
205 * number.
f7c16d48 206 */
181ea366 207 struct evp_method_data_st *methdata = data;
a829b735 208 OSSL_LIB_CTX *libctx = ossl_provider_libctx(prov);
f7c16d48 209 OSSL_NAMEMAP *namemap = ossl_namemap_stored(libctx);
36fa4d8a 210 const char *names = algodef->algorithm_names;
3d83c735 211 int name_id = ossl_namemap_add_names(namemap, 0, names, NAME_SEPARATOR);
ec0ce188 212 void *method;
c13d2ab4 213
695d195b
RL
214 if (name_id == 0)
215 return NULL;
ec0ce188 216
309a78aa 217 method = methdata->method_from_algorithm(name_id, algodef, prov);
ec0ce188
RL
218
219 /*
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.
223 */
224 if (method == NULL)
e3a2ba75 225 methdata->flag_construct_error_occurred = 1;
ec0ce188
RL
226
227 return method;
c13d2ab4
RL
228}
229
181ea366 230static void destruct_evp_method(void *method, void *data)
c13d2ab4 231{
181ea366 232 struct evp_method_data_st *methdata = data;
c13d2ab4
RL
233
234 methdata->destruct_method(method);
235}
236
181ea366 237static void *
cfce50f7
RL
238inner_evp_generic_fetch(struct evp_method_data_st *methdata,
239 OSSL_PROVIDER *prov, int operation_id,
181ea366
RL
240 int name_id, const char *name,
241 const char *properties,
242 void *(*new_method)(int name_id,
309a78aa 243 const OSSL_ALGORITHM *algodef,
0ddf74bf 244 OSSL_PROVIDER *prov),
181ea366
RL
245 int (*up_ref_method)(void *),
246 void (*free_method)(void *))
c13d2ab4 247{
9067cf6c
RL
248 OSSL_METHOD_STORE *store = get_evp_method_store(methdata->libctx);
249 OSSL_NAMEMAP *namemap = ossl_namemap_stored(methdata->libctx);
af788ad6 250 const char *const propq = properties != NULL ? properties : "";
f7c16d48 251 uint32_t meth_id = 0;
c13d2ab4 252 void *method = NULL;
ec0ce188 253 int unsupported = 0;
c13d2ab4 254
ec0ce188 255 if (store == NULL || namemap == NULL) {
d6d42cda 256 ERR_raise(ERR_LIB_EVP, ERR_R_PASSED_INVALID_ARGUMENT);
e019da7b 257 return NULL;
ec0ce188 258 }
e019da7b 259
2ccb1b4e
RL
260 /*
261 * If there's ever an operation_id == 0 passed, we have an internal
262 * programming error.
263 */
ec0ce188 264 if (!ossl_assert(operation_id > 0)) {
d6d42cda 265 ERR_raise(ERR_LIB_EVP, ERR_R_INTERNAL_ERROR);
2ccb1b4e 266 return NULL;
ec0ce188 267 }
2ccb1b4e
RL
268
269 /*
793b0586 270 * If we have been passed both a name_id and a name, we have an
f7c16d48
RL
271 * internal programming error.
272 */
793b0586 273 if (!ossl_assert(name_id == 0 || name == NULL)) {
d6d42cda 274 ERR_raise(ERR_LIB_EVP, ERR_R_INTERNAL_ERROR);
f7c16d48 275 return NULL;
ec0ce188 276 }
f7c16d48
RL
277
278 /* If we haven't received a name id yet, try to get one for the name */
793b0586 279 if (name_id == 0 && name != NULL)
f7c16d48
RL
280 name_id = ossl_namemap_name2num(namemap, name);
281
282 /*
181ea366 283 * If we have a name id, calculate a method id with evp_method_id().
f7c16d48 284 *
181ea366
RL
285 * evp_method_id returns 0 if we have too many operations (more than
286 * about 2^8) or too many names (more than about 2^24). In that case,
287 * we can't create any new method.
ec0ce188 288 * For all intents and purposes, this is an internal error.
2ccb1b4e 289 */
ec0ce188 290 if (name_id != 0 && (meth_id = evp_method_id(name_id, operation_id)) == 0) {
d6d42cda 291 ERR_raise(ERR_LIB_EVP, ERR_R_INTERNAL_ERROR);
2ccb1b4e 292 return NULL;
ec0ce188
RL
293 }
294
295 /*
296 * If we haven't found the name yet, chances are that the algorithm to
297 * be fetched is unsupported.
298 */
299 if (name_id == 0)
300 unsupported = 1;
2ccb1b4e 301
f7c16d48 302 if (meth_id == 0
af788ad6 303 || !ossl_method_store_cache_get(store, prov, meth_id, propq, &method)) {
c13d2ab4 304 OSSL_METHOD_CONSTRUCT_METHOD mcm = {
9067cf6c 305 get_tmp_evp_method_store,
181ea366
RL
306 get_evp_method_from_store,
307 put_evp_method_in_store,
308 construct_evp_method,
309 destruct_evp_method
c13d2ab4 310 };
9067cf6c 311
9067cf6c
RL
312 methdata->operation_id = operation_id;
313 methdata->name_id = name_id;
314 methdata->names = name;
af788ad6 315 methdata->propquery = propq;
9067cf6c
RL
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,
dc010ca6 321 &prov, 0 /* !force_cache */,
9067cf6c 322 &mcm, methdata)) != NULL) {
2ccb1b4e
RL
323 /*
324 * If construction did create a method for us, we know that
181ea366
RL
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.
2ccb1b4e 328 */
f7c16d48
RL
329 if (name_id == 0)
330 name_id = ossl_namemap_name2num(namemap, name);
b418980c 331 meth_id = evp_method_id(name_id, operation_id);
793b0586 332 if (name_id != 0)
af788ad6 333 ossl_method_store_cache_set(store, prov, meth_id, propq,
dc010ca6 334 method, up_ref_method, free_method);
2ccb1b4e 335 }
ec0ce188
RL
336
337 /*
338 * If we never were in the constructor, the algorithm to be fetched
339 * is unsupported.
340 */
9067cf6c 341 unsupported = !methdata->flag_construct_error_occurred;
c13d2ab4
RL
342 }
343
793b0586 344 if ((name_id != 0 || name != NULL) && method == NULL) {
d6d42cda 345 int code = unsupported ? ERR_R_UNSUPPORTED : ERR_R_FETCH_FAILED;
cb929645 346
ec0ce188
RL
347 if (name == NULL)
348 name = ossl_namemap_num2name(namemap, name_id, 0);
349 ERR_raise_data(ERR_LIB_EVP, code,
350 "%s, Algorithm (%s : %d), Properties (%s)",
9067cf6c 351 ossl_lib_ctx_get_descriptor(methdata->libctx),
ec0ce188
RL
352 name = NULL ? "<null>" : name, name_id,
353 properties == NULL ? "<null>" : properties);
354 }
355
356 return method;
cfbd76c1 357}
cfbd76c1 358
b4250010 359void *evp_generic_fetch(OSSL_LIB_CTX *libctx, int operation_id,
f7c16d48
RL
360 const char *name, const char *properties,
361 void *(*new_method)(int name_id,
309a78aa 362 const OSSL_ALGORITHM *algodef,
0ddf74bf 363 OSSL_PROVIDER *prov),
f7c16d48
RL
364 int (*up_ref_method)(void *),
365 void (*free_method)(void *))
366{
9067cf6c
RL
367 struct evp_method_data_st methdata;
368 void *method;
369
370 methdata.libctx = libctx;
371 methdata.tmp_store = NULL;
cfce50f7
RL
372 method = inner_evp_generic_fetch(&methdata, NULL, operation_id,
373 0, name, properties,
9067cf6c
RL
374 new_method, up_ref_method, free_method);
375 dealloc_tmp_evp_method_store(methdata.tmp_store);
376 return method;
f7c16d48
RL
377}
378
379/*
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
2fd3392c 384 * method.
f7c16d48 385 */
b4250010 386void *evp_generic_fetch_by_number(OSSL_LIB_CTX *libctx, int operation_id,
f7c16d48
RL
387 int name_id, const char *properties,
388 void *(*new_method)(int name_id,
309a78aa 389 const OSSL_ALGORITHM *algodef,
0ddf74bf 390 OSSL_PROVIDER *prov),
f7c16d48
RL
391 int (*up_ref_method)(void *),
392 void (*free_method)(void *))
393{
9067cf6c
RL
394 struct evp_method_data_st methdata;
395 void *method;
396
397 methdata.libctx = libctx;
398 methdata.tmp_store = NULL;
cfce50f7
RL
399 method = inner_evp_generic_fetch(&methdata, NULL, operation_id,
400 name_id, NULL, properties,
9067cf6c
RL
401 new_method, up_ref_method, free_method);
402 dealloc_tmp_evp_method_store(methdata.tmp_store);
403 return method;
f7c16d48
RL
404}
405
2fd3392c
RL
406/*
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
410 * method.
411 */
412void *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 *))
419{
420 struct evp_method_data_st methdata;
421 void *method;
422
423 methdata.libctx = ossl_provider_libctx(prov);
424 methdata.tmp_store = NULL;
425 method = inner_evp_generic_fetch(&methdata, prov, operation_id,
426 0, name, properties,
427 new_method, up_ref_method, free_method);
428 dealloc_tmp_evp_method_store(methdata.tmp_store);
429 return method;
430}
431
860ecfd7 432int evp_method_store_flush(OSSL_LIB_CTX *libctx)
04cb5ec0
SL
433{
434 OSSL_METHOD_STORE *store = get_evp_method_store(libctx);
435
436 if (store != NULL)
860ecfd7
P
437 return ossl_method_store_flush_cache(store, 1);
438 return 1;
04cb5ec0
SL
439}
440
b4250010 441static int evp_set_parsed_default_properties(OSSL_LIB_CTX *libctx,
e6c54619 442 OSSL_PROPERTY_LIST *def_prop,
b1c053ac
MC
443 int loadconfig,
444 int mirrored)
cb929645 445{
181ea366 446 OSSL_METHOD_STORE *store = get_evp_method_store(libctx);
e6c54619 447 OSSL_PROPERTY_LIST **plp = ossl_ctx_global_properties(libctx, loadconfig);
f9e504e8 448
447588b6 449 if (plp != NULL && store != NULL) {
b1c053ac 450#ifndef FIPS_MODULE
447588b6
MC
451 char *propstr = NULL;
452 size_t strsz;
453
b1c053ac
MC
454 if (mirrored) {
455 if (ossl_global_properties_no_mirrored(libctx))
456 return 0;
457 } else {
458 /*
459 * These properties have been explicitly set on this libctx, so
460 * don't allow any mirroring from a parent libctx.
461 */
462 ossl_global_properties_stop_mirroring(libctx);
463 }
464
447588b6
MC
465 strsz = ossl_property_list_to_string(libctx, def_prop, NULL, 0);
466 if (strsz > 0)
467 propstr = OPENSSL_malloc(strsz);
468 if (propstr == NULL) {
469 ERR_raise(ERR_LIB_EVP, ERR_R_INTERNAL_ERROR);
470 return 0;
471 }
472 if (ossl_property_list_to_string(libctx, def_prop, propstr,
473 strsz) == 0) {
474 OPENSSL_free(propstr);
475 ERR_raise(ERR_LIB_EVP, ERR_R_INTERNAL_ERROR);
476 return 0;
477 }
447588b6
MC
478 ossl_provider_default_props_update(libctx, propstr);
479 OPENSSL_free(propstr);
b1c053ac
MC
480#endif
481 ossl_property_free(*plp);
482 *plp = def_prop;
f9e504e8 483 if (store != NULL)
860ecfd7 484 return ossl_method_store_flush_cache(store, 0);
f9e504e8 485 }
9311d0c4 486 ERR_raise(ERR_LIB_EVP, ERR_R_INTERNAL_ERROR);
cb929645
RL
487 return 0;
488}
3d96a51c 489
b4250010 490int evp_set_default_properties_int(OSSL_LIB_CTX *libctx, const char *propq,
b1c053ac 491 int loadconfig, int mirrored)
f9e504e8
P
492{
493 OSSL_PROPERTY_LIST *pl = NULL;
494
1e08f3ba 495 if (propq != NULL && (pl = ossl_parse_query(libctx, propq, 1)) == NULL) {
9311d0c4 496 ERR_raise(ERR_LIB_EVP, EVP_R_DEFAULT_QUERY_PARSE_ERROR);
f9e504e8
P
497 return 0;
498 }
b1c053ac 499 if (!evp_set_parsed_default_properties(libctx, pl, loadconfig, mirrored)) {
e2ed740e
MC
500 ossl_property_free(pl);
501 return 0;
502 }
503 return 1;
f9e504e8
P
504}
505
b4250010 506int EVP_set_default_properties(OSSL_LIB_CTX *libctx, const char *propq)
e6c54619 507{
b1c053ac 508 return evp_set_default_properties_int(libctx, propq, 1, 0);
e6c54619 509}
e0624f0d 510
589fbc18
MC
511static int evp_default_properties_merge(OSSL_LIB_CTX *libctx, const char *propq,
512 int loadconfig)
e0624f0d 513{
589fbc18 514 OSSL_PROPERTY_LIST **plp = ossl_ctx_global_properties(libctx, loadconfig);
f9e504e8
P
515 OSSL_PROPERTY_LIST *pl1, *pl2;
516
517 if (propq == NULL)
518 return 1;
519 if (plp == NULL || *plp == NULL)
589fbc18 520 return evp_set_default_properties_int(libctx, propq, 0, 0);
1e08f3ba 521 if ((pl1 = ossl_parse_query(libctx, propq, 1)) == NULL) {
9311d0c4 522 ERR_raise(ERR_LIB_EVP, EVP_R_DEFAULT_QUERY_PARSE_ERROR);
f9e504e8
P
523 return 0;
524 }
525 pl2 = ossl_property_merge(pl1, *plp);
526 ossl_property_free(pl1);
527 if (pl2 == NULL) {
9311d0c4 528 ERR_raise(ERR_LIB_EVP, ERR_R_MALLOC_FAILURE);
f9e504e8
P
529 return 0;
530 }
b1c053ac 531 if (!evp_set_parsed_default_properties(libctx, pl2, 0, 0)) {
e2ed740e
MC
532 ossl_property_free(pl2);
533 return 0;
534 }
535 return 1;
e0624f0d
SL
536}
537
b4250010 538static int evp_default_property_is_enabled(OSSL_LIB_CTX *libctx,
e0624f0d
SL
539 const char *prop_name)
540{
e6c54619 541 OSSL_PROPERTY_LIST **plp = ossl_ctx_global_properties(libctx, 1);
e0624f0d 542
f9e504e8 543 return plp != NULL && ossl_property_is_enabled(libctx, prop_name, *plp);
e0624f0d
SL
544}
545
b4250010 546int EVP_default_properties_is_fips_enabled(OSSL_LIB_CTX *libctx)
e0624f0d
SL
547{
548 return evp_default_property_is_enabled(libctx, "fips");
549}
550
589fbc18
MC
551int evp_default_properties_enable_fips_int(OSSL_LIB_CTX *libctx, int enable,
552 int loadconfig)
e0624f0d
SL
553{
554 const char *query = (enable != 0) ? "fips=yes" : "-fips";
555
589fbc18
MC
556 return evp_default_properties_merge(libctx, query, loadconfig);
557}
558
559int EVP_default_properties_enable_fips(OSSL_LIB_CTX *libctx, int enable)
560{
561 return evp_default_properties_enable_fips_int(libctx, enable, 1);
e0624f0d
SL
562}
563
447588b6
MC
564char *evp_get_global_properties_str(OSSL_LIB_CTX *libctx, int loadconfig)
565{
566 OSSL_PROPERTY_LIST **plp = ossl_ctx_global_properties(libctx, loadconfig);
567 char *propstr = NULL;
568 size_t sz;
569
72f62f44
P
570 if (plp == NULL)
571 return OPENSSL_strdup("");
572
447588b6
MC
573 sz = ossl_property_list_to_string(libctx, *plp, NULL, 0);
574 if (sz == 0) {
575 ERR_raise(ERR_LIB_EVP, ERR_R_INTERNAL_ERROR);
576 return NULL;
577 }
578
579 propstr = OPENSSL_malloc(sz);
580 if (propstr == NULL) {
581 ERR_raise(ERR_LIB_EVP, ERR_R_MALLOC_FAILURE);
582 return NULL;
583 }
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);
587 return NULL;
588 }
589 return propstr;
590}
e0624f0d 591
793b0586
RL
592struct filter_data_st {
593 int operation_id;
3d96a51c
RL
594 void (*user_fn)(void *method, void *arg);
595 void *user_arg;
3d96a51c
RL
596};
597
793b0586 598static void filter_on_operation_id(int id, void *method, void *arg)
3d96a51c 599{
793b0586 600 struct filter_data_st *data = arg;
3d96a51c 601
793b0586 602 if ((id & METHOD_ID_OPERATION_MASK) == data->operation_id)
3d96a51c 603 data->user_fn(method, data->user_arg);
3d96a51c
RL
604}
605
b4250010 606void evp_generic_do_all(OSSL_LIB_CTX *libctx, int operation_id,
3d96a51c
RL
607 void (*user_fn)(void *method, void *arg),
608 void *user_arg,
f7c16d48 609 void *(*new_method)(int name_id,
309a78aa 610 const OSSL_ALGORITHM *algodef,
0ddf74bf 611 OSSL_PROVIDER *prov),
793b0586 612 int (*up_ref_method)(void *),
3d96a51c
RL
613 void (*free_method)(void *))
614{
793b0586
RL
615 struct evp_method_data_st methdata;
616 struct filter_data_st data;
617
618 methdata.libctx = libctx;
619 methdata.tmp_store = NULL;
cfce50f7 620 (void)inner_evp_generic_fetch(&methdata, NULL, operation_id, 0, NULL, NULL,
793b0586 621 new_method, up_ref_method, free_method);
3d96a51c 622
793b0586 623 data.operation_id = operation_id;
3d96a51c
RL
624 data.user_fn = user_fn;
625 data.user_arg = user_arg;
793b0586
RL
626 if (methdata.tmp_store != NULL)
627 ossl_method_store_do_all(methdata.tmp_store, &filter_on_operation_id,
628 &data);
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);
3d96a51c 632}
f7c16d48 633
e4a1d023
RL
634int evp_is_a(OSSL_PROVIDER *prov, int number,
635 const char *legacy_name, const char *name)
7cfa1717 636{
e4a1d023
RL
637 /*
638 * For a |prov| that is NULL, the library context will be NULL
639 */
a829b735 640 OSSL_LIB_CTX *libctx = ossl_provider_libctx(prov);
7cfa1717
RL
641 OSSL_NAMEMAP *namemap = ossl_namemap_stored(libctx);
642
e4a1d023
RL
643 if (prov == NULL)
644 number = ossl_namemap_name2num(namemap, legacy_name);
7cfa1717
RL
645 return ossl_namemap_name2num(namemap, name) == number;
646}
32040838 647
d84f5515
MC
648int evp_names_do_all(OSSL_PROVIDER *prov, int number,
649 void (*fn)(const char *name, void *data),
650 void *data)
32040838 651{
a829b735 652 OSSL_LIB_CTX *libctx = ossl_provider_libctx(prov);
32040838
RL
653 OSSL_NAMEMAP *namemap = ossl_namemap_stored(libctx);
654
d84f5515 655 return ossl_namemap_doall_names(namemap, number, fn, data);
32040838 656}