xref: /freebsd/crypto/openssl/crypto/evp/evp_fetch.c (revision e0c4386e)
1 /*
2  * Copyright 2019-2024 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              * Note that there is a corner case here, in which, if a user
353              * passes a name of the form name1:name2:..., then the construction
354              * will create a method against all names, but the lookup will fail
355              * as ossl_namemap_name2num treats the name string as a single name
356              * rather than introducing new features where in the EVP_<obj>_fetch
357              * parses the string and querys for each, return an error.
358              */
359             if (name_id == 0)
360                 name_id = ossl_namemap_name2num(namemap, name);
361             if (name_id == 0) {
362                 ERR_raise_data(ERR_LIB_EVP, ERR_R_FETCH_FAILED,
363                                "Algorithm %s cannot be found", name);
364                 free_method(method);
365                 method = NULL;
366             } else {
367                 meth_id = evp_method_id(name_id, operation_id);
368                 if (meth_id != 0)
369                     ossl_method_store_cache_set(store, prov, meth_id, propq,
370                                                 method, up_ref_method, free_method);
371             }
372         }
373 
374         /*
375          * If we never were in the constructor, the algorithm to be fetched
376          * is unsupported.
377          */
378         unsupported = !methdata->flag_construct_error_occurred;
379     }
380 
381     if ((name_id != 0 || name != NULL) && method == NULL) {
382         int code = unsupported ? ERR_R_UNSUPPORTED : ERR_R_FETCH_FAILED;
383 
384         if (name == NULL)
385             name = ossl_namemap_num2name(namemap, name_id, 0);
386         ERR_raise_data(ERR_LIB_EVP, code,
387                        "%s, Algorithm (%s : %d), Properties (%s)",
388                        ossl_lib_ctx_get_descriptor(methdata->libctx),
389                        name == NULL ? "<null>" : name, name_id,
390                        properties == NULL ? "<null>" : properties);
391     }
392 
393     return method;
394 }
395 
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 *))396 void *evp_generic_fetch(OSSL_LIB_CTX *libctx, int operation_id,
397                         const char *name, const char *properties,
398                         void *(*new_method)(int name_id,
399                                             const OSSL_ALGORITHM *algodef,
400                                             OSSL_PROVIDER *prov),
401                         int (*up_ref_method)(void *),
402                         void (*free_method)(void *))
403 {
404     struct evp_method_data_st methdata;
405     void *method;
406 
407     methdata.libctx = libctx;
408     methdata.tmp_store = NULL;
409     method = inner_evp_generic_fetch(&methdata, NULL, operation_id,
410                                      0, name, properties,
411                                      new_method, up_ref_method, free_method);
412     dealloc_tmp_evp_method_store(methdata.tmp_store);
413     return method;
414 }
415 
416 /*
417  * evp_generic_fetch_by_number() is special, and only returns methods for
418  * already known names, i.e. it refuses to work if no name_id can be found
419  * (it's considered an internal programming error).
420  * This is meant to be used when one method needs to fetch an associated
421  * method.
422  */
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 *))423 void *evp_generic_fetch_by_number(OSSL_LIB_CTX *libctx, int operation_id,
424                                   int name_id, const char *properties,
425                                   void *(*new_method)(int name_id,
426                                                       const OSSL_ALGORITHM *algodef,
427                                                       OSSL_PROVIDER *prov),
428                                   int (*up_ref_method)(void *),
429                                   void (*free_method)(void *))
430 {
431     struct evp_method_data_st methdata;
432     void *method;
433 
434     methdata.libctx = libctx;
435     methdata.tmp_store = NULL;
436     method = inner_evp_generic_fetch(&methdata, NULL, operation_id,
437                                      name_id, NULL, properties,
438                                      new_method, up_ref_method, free_method);
439     dealloc_tmp_evp_method_store(methdata.tmp_store);
440     return method;
441 }
442 
443 /*
444  * evp_generic_fetch_from_prov() is special, and only returns methods from
445  * the given provider.
446  * This is meant to be used when one method needs to fetch an associated
447  * method.
448  */
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 *))449 void *evp_generic_fetch_from_prov(OSSL_PROVIDER *prov, int operation_id,
450                                   const char *name, const char *properties,
451                                   void *(*new_method)(int name_id,
452                                                       const OSSL_ALGORITHM *algodef,
453                                                       OSSL_PROVIDER *prov),
454                                   int (*up_ref_method)(void *),
455                                   void (*free_method)(void *))
456 {
457     struct evp_method_data_st methdata;
458     void *method;
459 
460     methdata.libctx = ossl_provider_libctx(prov);
461     methdata.tmp_store = NULL;
462     method = inner_evp_generic_fetch(&methdata, prov, operation_id,
463                                      0, name, properties,
464                                      new_method, up_ref_method, free_method);
465     dealloc_tmp_evp_method_store(methdata.tmp_store);
466     return method;
467 }
468 
evp_method_store_cache_flush(OSSL_LIB_CTX * libctx)469 int evp_method_store_cache_flush(OSSL_LIB_CTX *libctx)
470 {
471     OSSL_METHOD_STORE *store = get_evp_method_store(libctx);
472 
473     if (store != NULL)
474         return ossl_method_store_cache_flush_all(store);
475     return 1;
476 }
477 
evp_method_store_remove_all_provided(const OSSL_PROVIDER * prov)478 int evp_method_store_remove_all_provided(const OSSL_PROVIDER *prov)
479 {
480     OSSL_LIB_CTX *libctx = ossl_provider_libctx(prov);
481     OSSL_METHOD_STORE *store = get_evp_method_store(libctx);
482 
483     if (store != NULL)
484         return ossl_method_store_remove_all_provided(store, prov);
485     return 1;
486 }
487 
evp_set_parsed_default_properties(OSSL_LIB_CTX * libctx,OSSL_PROPERTY_LIST * def_prop,int loadconfig,int mirrored)488 static int evp_set_parsed_default_properties(OSSL_LIB_CTX *libctx,
489                                              OSSL_PROPERTY_LIST *def_prop,
490                                              int loadconfig,
491                                              int mirrored)
492 {
493     OSSL_METHOD_STORE *store = get_evp_method_store(libctx);
494     OSSL_PROPERTY_LIST **plp = ossl_ctx_global_properties(libctx, loadconfig);
495 
496     if (plp != NULL && store != NULL) {
497 #ifndef FIPS_MODULE
498         char *propstr = NULL;
499         size_t strsz;
500 
501         if (mirrored) {
502             if (ossl_global_properties_no_mirrored(libctx))
503                 return 0;
504         } else {
505             /*
506              * These properties have been explicitly set on this libctx, so
507              * don't allow any mirroring from a parent libctx.
508              */
509             ossl_global_properties_stop_mirroring(libctx);
510         }
511 
512         strsz = ossl_property_list_to_string(libctx, def_prop, NULL, 0);
513         if (strsz > 0)
514             propstr = OPENSSL_malloc(strsz);
515         if (propstr == NULL) {
516             ERR_raise(ERR_LIB_EVP, ERR_R_INTERNAL_ERROR);
517             return 0;
518         }
519         if (ossl_property_list_to_string(libctx, def_prop, propstr,
520                                          strsz) == 0) {
521             OPENSSL_free(propstr);
522             ERR_raise(ERR_LIB_EVP, ERR_R_INTERNAL_ERROR);
523             return 0;
524         }
525         ossl_provider_default_props_update(libctx, propstr);
526         OPENSSL_free(propstr);
527 #endif
528         ossl_property_free(*plp);
529         *plp = def_prop;
530         if (store != NULL)
531             return ossl_method_store_cache_flush_all(store);
532     }
533     ERR_raise(ERR_LIB_EVP, ERR_R_INTERNAL_ERROR);
534     return 0;
535 }
536 
evp_set_default_properties_int(OSSL_LIB_CTX * libctx,const char * propq,int loadconfig,int mirrored)537 int evp_set_default_properties_int(OSSL_LIB_CTX *libctx, const char *propq,
538                                    int loadconfig, int mirrored)
539 {
540     OSSL_PROPERTY_LIST *pl = NULL;
541 
542     if (propq != NULL && (pl = ossl_parse_query(libctx, propq, 1)) == NULL) {
543         ERR_raise(ERR_LIB_EVP, EVP_R_DEFAULT_QUERY_PARSE_ERROR);
544         return 0;
545     }
546     if (!evp_set_parsed_default_properties(libctx, pl, loadconfig, mirrored)) {
547         ossl_property_free(pl);
548         return 0;
549     }
550     return 1;
551 }
552 
EVP_set_default_properties(OSSL_LIB_CTX * libctx,const char * propq)553 int EVP_set_default_properties(OSSL_LIB_CTX *libctx, const char *propq)
554 {
555     return evp_set_default_properties_int(libctx, propq, 1, 0);
556 }
557 
evp_default_properties_merge(OSSL_LIB_CTX * libctx,const char * propq,int loadconfig)558 static int evp_default_properties_merge(OSSL_LIB_CTX *libctx, const char *propq,
559                                         int loadconfig)
560 {
561     OSSL_PROPERTY_LIST **plp = ossl_ctx_global_properties(libctx, loadconfig);
562     OSSL_PROPERTY_LIST *pl1, *pl2;
563 
564     if (propq == NULL)
565         return 1;
566     if (plp == NULL || *plp == NULL)
567         return evp_set_default_properties_int(libctx, propq, 0, 0);
568     if ((pl1 = ossl_parse_query(libctx, propq, 1)) == NULL) {
569         ERR_raise(ERR_LIB_EVP, EVP_R_DEFAULT_QUERY_PARSE_ERROR);
570         return 0;
571     }
572     pl2 = ossl_property_merge(pl1, *plp);
573     ossl_property_free(pl1);
574     if (pl2 == NULL) {
575         ERR_raise(ERR_LIB_EVP, ERR_R_MALLOC_FAILURE);
576         return 0;
577     }
578     if (!evp_set_parsed_default_properties(libctx, pl2, 0, 0)) {
579         ossl_property_free(pl2);
580         return 0;
581     }
582     return 1;
583 }
584 
evp_default_property_is_enabled(OSSL_LIB_CTX * libctx,const char * prop_name)585 static int evp_default_property_is_enabled(OSSL_LIB_CTX *libctx,
586                                            const char *prop_name)
587 {
588     OSSL_PROPERTY_LIST **plp = ossl_ctx_global_properties(libctx, 1);
589 
590     return plp != NULL && ossl_property_is_enabled(libctx, prop_name, *plp);
591 }
592 
EVP_default_properties_is_fips_enabled(OSSL_LIB_CTX * libctx)593 int EVP_default_properties_is_fips_enabled(OSSL_LIB_CTX *libctx)
594 {
595     return evp_default_property_is_enabled(libctx, "fips");
596 }
597 
evp_default_properties_enable_fips_int(OSSL_LIB_CTX * libctx,int enable,int loadconfig)598 int evp_default_properties_enable_fips_int(OSSL_LIB_CTX *libctx, int enable,
599                                            int loadconfig)
600 {
601     const char *query = (enable != 0) ? "fips=yes" : "-fips";
602 
603     return evp_default_properties_merge(libctx, query, loadconfig);
604 }
605 
EVP_default_properties_enable_fips(OSSL_LIB_CTX * libctx,int enable)606 int EVP_default_properties_enable_fips(OSSL_LIB_CTX *libctx, int enable)
607 {
608     return evp_default_properties_enable_fips_int(libctx, enable, 1);
609 }
610 
evp_get_global_properties_str(OSSL_LIB_CTX * libctx,int loadconfig)611 char *evp_get_global_properties_str(OSSL_LIB_CTX *libctx, int loadconfig)
612 {
613     OSSL_PROPERTY_LIST **plp = ossl_ctx_global_properties(libctx, loadconfig);
614     char *propstr = NULL;
615     size_t sz;
616 
617     if (plp == NULL)
618         return OPENSSL_strdup("");
619 
620     sz = ossl_property_list_to_string(libctx, *plp, NULL, 0);
621     if (sz == 0) {
622         ERR_raise(ERR_LIB_EVP, ERR_R_INTERNAL_ERROR);
623         return NULL;
624     }
625 
626     propstr = OPENSSL_malloc(sz);
627     if (propstr == NULL) {
628         ERR_raise(ERR_LIB_EVP, ERR_R_MALLOC_FAILURE);
629         return NULL;
630     }
631     if (ossl_property_list_to_string(libctx, *plp, propstr, sz) == 0) {
632         ERR_raise(ERR_LIB_EVP, ERR_R_INTERNAL_ERROR);
633         OPENSSL_free(propstr);
634         return NULL;
635     }
636     return propstr;
637 }
638 
639 struct filter_data_st {
640     int operation_id;
641     void (*user_fn)(void *method, void *arg);
642     void *user_arg;
643 };
644 
filter_on_operation_id(int id,void * method,void * arg)645 static void filter_on_operation_id(int id, void *method, void *arg)
646 {
647     struct filter_data_st *data = arg;
648 
649     if ((id & METHOD_ID_OPERATION_MASK) == data->operation_id)
650         data->user_fn(method, data->user_arg);
651 }
652 
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 *))653 void evp_generic_do_all(OSSL_LIB_CTX *libctx, int operation_id,
654                         void (*user_fn)(void *method, void *arg),
655                         void *user_arg,
656                         void *(*new_method)(int name_id,
657                                             const OSSL_ALGORITHM *algodef,
658                                             OSSL_PROVIDER *prov),
659                         int (*up_ref_method)(void *),
660                         void (*free_method)(void *))
661 {
662     struct evp_method_data_st methdata;
663     struct filter_data_st data;
664 
665     methdata.libctx = libctx;
666     methdata.tmp_store = NULL;
667     (void)inner_evp_generic_fetch(&methdata, NULL, operation_id, 0, NULL, NULL,
668                                   new_method, up_ref_method, free_method);
669 
670     data.operation_id = operation_id;
671     data.user_fn = user_fn;
672     data.user_arg = user_arg;
673     if (methdata.tmp_store != NULL)
674         ossl_method_store_do_all(methdata.tmp_store, &filter_on_operation_id,
675                                  &data);
676     ossl_method_store_do_all(get_evp_method_store(libctx),
677                              &filter_on_operation_id, &data);
678     dealloc_tmp_evp_method_store(methdata.tmp_store);
679 }
680 
evp_is_a(OSSL_PROVIDER * prov,int number,const char * legacy_name,const char * name)681 int evp_is_a(OSSL_PROVIDER *prov, int number,
682              const char *legacy_name, const char *name)
683 {
684     /*
685      * For a |prov| that is NULL, the library context will be NULL
686      */
687     OSSL_LIB_CTX *libctx = ossl_provider_libctx(prov);
688     OSSL_NAMEMAP *namemap = ossl_namemap_stored(libctx);
689 
690     if (prov == NULL)
691         number = ossl_namemap_name2num(namemap, legacy_name);
692     return ossl_namemap_name2num(namemap, name) == number;
693 }
694 
evp_names_do_all(OSSL_PROVIDER * prov,int number,void (* fn)(const char * name,void * data),void * data)695 int evp_names_do_all(OSSL_PROVIDER *prov, int number,
696                      void (*fn)(const char *name, void *data),
697                      void *data)
698 {
699     OSSL_LIB_CTX *libctx = ossl_provider_libctx(prov);
700     OSSL_NAMEMAP *namemap = ossl_namemap_stored(libctx);
701 
702     return ossl_namemap_doall_names(namemap, number, fn, data);
703 }
704