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