xref: /openssl/crypto/provider_core.c (revision 0557d6c6)
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 <assert.h>
11 #include <openssl/core.h>
12 #include <openssl/core_dispatch.h>
13 #include <openssl/core_names.h>
14 #include <openssl/provider.h>
15 #include <openssl/params.h>
16 #include <openssl/opensslv.h>
17 #include "crypto/cryptlib.h"
18 #ifndef FIPS_MODULE
19 #include "crypto/decoder.h" /* ossl_decoder_store_cache_flush */
20 #include "crypto/encoder.h" /* ossl_encoder_store_cache_flush */
21 #include "crypto/store.h" /* ossl_store_loader_store_cache_flush */
22 #endif
23 #include "crypto/evp.h" /* evp_method_store_cache_flush */
24 #include "crypto/rand.h"
25 #include "internal/nelem.h"
26 #include "internal/thread_once.h"
27 #include "internal/provider.h"
28 #include "internal/refcount.h"
29 #include "internal/bio.h"
30 #include "internal/core.h"
31 #include "provider_local.h"
32 #include "crypto/context.h"
33 #ifndef FIPS_MODULE
34 # include <openssl/self_test.h>
35 # include <openssl/indicator.h>
36 #endif
37 
38 /*
39  * This file defines and uses a number of different structures:
40  *
41  * OSSL_PROVIDER (provider_st): Used to represent all information related to a
42  * single instance of a provider.
43  *
44  * provider_store_st: Holds information about the collection of providers that
45  * are available within the current library context (OSSL_LIB_CTX). It also
46  * holds configuration information about providers that could be loaded at some
47  * future point.
48  *
49  * OSSL_PROVIDER_CHILD_CB: An instance of this structure holds the callbacks
50  * that have been registered for a child library context and the associated
51  * provider that registered those callbacks.
52  *
53  * Where a child library context exists then it has its own instance of the
54  * provider store. Each provider that exists in the parent provider store, has
55  * an associated child provider in the child library context's provider store.
56  * As providers get activated or deactivated this needs to be mirrored in the
57  * associated child providers.
58  *
59  * LOCKING
60  * =======
61  *
62  * There are a number of different locks used in this file and it is important
63  * to understand how they should be used in order to avoid deadlocks.
64  *
65  * Fields within a structure can often be "write once" on creation, and then
66  * "read many". Creation of a structure is done by a single thread, and
67  * therefore no lock is required for the "write once/read many" fields. It is
68  * safe for multiple threads to read these fields without a lock, because they
69  * will never be changed.
70  *
71  * However some fields may be changed after a structure has been created and
72  * shared between multiple threads. Where this is the case a lock is required.
73  *
74  * The locks available are:
75  *
76  * The provider flag_lock: Used to control updates to the various provider
77  * "flags" (flag_initialized and flag_activated).
78  *
79  * The provider activatecnt_lock: Used to control updates to the provider
80  * activatecnt value.
81  *
82  * The provider optbits_lock: Used to control access to the provider's
83  * operation_bits and operation_bits_sz fields.
84  *
85  * The store default_path_lock: Used to control access to the provider store's
86  * default search path value (default_path)
87  *
88  * The store lock: Used to control the stack of provider's held within the
89  * provider store, as well as the stack of registered child provider callbacks.
90  *
91  * As a general rule-of-thumb it is best to:
92  *  - keep the scope of the code that is protected by a lock to the absolute
93  *    minimum possible;
94  *  - try to keep the scope of the lock to within a single function (i.e. avoid
95  *    making calls to other functions while holding a lock);
96  *  - try to only ever hold one lock at a time.
97  *
98  * Unfortunately, it is not always possible to stick to the above guidelines.
99  * Where they are not adhered to there is always a danger of inadvertently
100  * introducing the possibility of deadlock. The following rules MUST be adhered
101  * to in order to avoid that:
102  *  - Holding multiple locks at the same time is only allowed for the
103  *    provider store lock, the provider activatecnt_lock and the provider flag_lock.
104  *  - When holding multiple locks they must be acquired in the following order of
105  *    precedence:
106  *        1) provider store lock
107  *        2) provider flag_lock
108  *        3) provider activatecnt_lock
109  *  - When releasing locks they must be released in the reverse order to which
110  *    they were acquired
111  *  - No locks may be held when making an upcall. NOTE: Some common functions
112  *    can make upcalls as part of their normal operation. If you need to call
113  *    some other function while holding a lock make sure you know whether it
114  *    will make any upcalls or not. For example ossl_provider_up_ref() can call
115  *    ossl_provider_up_ref_parent() which can call the c_prov_up_ref() upcall.
116  *  - It is permissible to hold the store and flag locks when calling child
117  *    provider callbacks. No other locks may be held during such callbacks.
118  */
119 
120 static OSSL_PROVIDER *provider_new(const char *name,
121                                    OSSL_provider_init_fn *init_function,
122                                    STACK_OF(INFOPAIR) *parameters);
123 
124 /*-
125  * Provider Object structure
126  * =========================
127  */
128 
129 #ifndef FIPS_MODULE
130 typedef struct {
131     OSSL_PROVIDER *prov;
132     int (*create_cb)(const OSSL_CORE_HANDLE *provider, void *cbdata);
133     int (*remove_cb)(const OSSL_CORE_HANDLE *provider, void *cbdata);
134     int (*global_props_cb)(const char *props, void *cbdata);
135     void *cbdata;
136 } OSSL_PROVIDER_CHILD_CB;
137 DEFINE_STACK_OF(OSSL_PROVIDER_CHILD_CB)
138 #endif
139 
140 struct provider_store_st;        /* Forward declaration */
141 
142 struct ossl_provider_st {
143     /* Flag bits */
144     unsigned int flag_initialized:1;
145     unsigned int flag_activated:1;
146 
147     /* Getting and setting the flags require synchronization */
148     CRYPTO_RWLOCK *flag_lock;
149 
150     /* OpenSSL library side data */
151     CRYPTO_REF_COUNT refcnt;
152     CRYPTO_RWLOCK *activatecnt_lock; /* For the activatecnt counter */
153     int activatecnt;
154     char *name;
155     char *path;
156     DSO *module;
157     OSSL_provider_init_fn *init_function;
158     STACK_OF(INFOPAIR) *parameters;
159     OSSL_LIB_CTX *libctx; /* The library context this instance is in */
160     struct provider_store_st *store; /* The store this instance belongs to */
161 #ifndef FIPS_MODULE
162     /*
163      * In the FIPS module inner provider, this isn't needed, since the
164      * error upcalls are always direct calls to the outer provider.
165      */
166     int error_lib;     /* ERR library number, one for each provider */
167 # ifndef OPENSSL_NO_ERR
168     ERR_STRING_DATA *error_strings; /* Copy of what the provider gives us */
169 # endif
170 #endif
171 
172     /* Provider side functions */
173     OSSL_FUNC_provider_teardown_fn *teardown;
174     OSSL_FUNC_provider_gettable_params_fn *gettable_params;
175     OSSL_FUNC_provider_get_params_fn *get_params;
176     OSSL_FUNC_provider_get_capabilities_fn *get_capabilities;
177     OSSL_FUNC_provider_self_test_fn *self_test;
178     OSSL_FUNC_provider_query_operation_fn *query_operation;
179     OSSL_FUNC_provider_unquery_operation_fn *unquery_operation;
180 
181     /*
182      * Cache of bit to indicate of query_operation() has been called on
183      * a specific operation or not.
184      */
185     unsigned char *operation_bits;
186     size_t operation_bits_sz;
187     CRYPTO_RWLOCK *opbits_lock;
188 
189 #ifndef FIPS_MODULE
190     /* Whether this provider is the child of some other provider */
191     const OSSL_CORE_HANDLE *handle;
192     unsigned int ischild:1;
193 #endif
194 
195     /* Provider side data */
196     void *provctx;
197     const OSSL_DISPATCH *dispatch;
198 };
DEFINE_STACK_OF(OSSL_PROVIDER)199 DEFINE_STACK_OF(OSSL_PROVIDER)
200 
201 static int ossl_provider_cmp(const OSSL_PROVIDER * const *a,
202                              const OSSL_PROVIDER * const *b)
203 {
204     return strcmp((*a)->name, (*b)->name);
205 }
206 
207 /*-
208  * Provider Object store
209  * =====================
210  *
211  * The Provider Object store is a library context object, and therefore needs
212  * an index.
213  */
214 
215 struct provider_store_st {
216     OSSL_LIB_CTX *libctx;
217     STACK_OF(OSSL_PROVIDER) *providers;
218     STACK_OF(OSSL_PROVIDER_CHILD_CB) *child_cbs;
219     CRYPTO_RWLOCK *default_path_lock;
220     CRYPTO_RWLOCK *lock;
221     char *default_path;
222     OSSL_PROVIDER_INFO *provinfo;
223     size_t numprovinfo;
224     size_t provinfosz;
225     unsigned int use_fallbacks:1;
226     unsigned int freeing:1;
227 };
228 
229 /*
230  * provider_deactivate_free() is a wrapper around ossl_provider_deactivate()
231  * and ossl_provider_free(), called as needed.
232  * Since this is only called when the provider store is being emptied, we
233  * don't need to care about any lock.
234  */
provider_deactivate_free(OSSL_PROVIDER * prov)235 static void provider_deactivate_free(OSSL_PROVIDER *prov)
236 {
237     if (prov->flag_activated)
238         ossl_provider_deactivate(prov, 1);
239     ossl_provider_free(prov);
240 }
241 
242 #ifndef FIPS_MODULE
ossl_provider_child_cb_free(OSSL_PROVIDER_CHILD_CB * cb)243 static void ossl_provider_child_cb_free(OSSL_PROVIDER_CHILD_CB *cb)
244 {
245     OPENSSL_free(cb);
246 }
247 #endif
248 
infopair_free(INFOPAIR * pair)249 static void infopair_free(INFOPAIR *pair)
250 {
251     OPENSSL_free(pair->name);
252     OPENSSL_free(pair->value);
253     OPENSSL_free(pair);
254 }
255 
infopair_copy(const INFOPAIR * src)256 static INFOPAIR *infopair_copy(const INFOPAIR *src)
257 {
258     INFOPAIR *dest = OPENSSL_zalloc(sizeof(*dest));
259 
260     if (dest == NULL)
261         return NULL;
262     if (src->name != NULL) {
263         dest->name = OPENSSL_strdup(src->name);
264         if (dest->name == NULL)
265             goto err;
266     }
267     if (src->value != NULL) {
268         dest->value = OPENSSL_strdup(src->value);
269         if (dest->value == NULL)
270             goto err;
271     }
272     return dest;
273  err:
274     OPENSSL_free(dest->name);
275     OPENSSL_free(dest);
276     return NULL;
277 }
278 
ossl_provider_info_clear(OSSL_PROVIDER_INFO * info)279 void ossl_provider_info_clear(OSSL_PROVIDER_INFO *info)
280 {
281     OPENSSL_free(info->name);
282     OPENSSL_free(info->path);
283     sk_INFOPAIR_pop_free(info->parameters, infopair_free);
284 }
285 
ossl_provider_store_free(void * vstore)286 void ossl_provider_store_free(void *vstore)
287 {
288     struct provider_store_st *store = vstore;
289     size_t i;
290 
291     if (store == NULL)
292         return;
293     store->freeing = 1;
294     OPENSSL_free(store->default_path);
295     sk_OSSL_PROVIDER_pop_free(store->providers, provider_deactivate_free);
296 #ifndef FIPS_MODULE
297     sk_OSSL_PROVIDER_CHILD_CB_pop_free(store->child_cbs,
298                                        ossl_provider_child_cb_free);
299 #endif
300     CRYPTO_THREAD_lock_free(store->default_path_lock);
301     CRYPTO_THREAD_lock_free(store->lock);
302     for (i = 0; i < store->numprovinfo; i++)
303         ossl_provider_info_clear(&store->provinfo[i]);
304     OPENSSL_free(store->provinfo);
305     OPENSSL_free(store);
306 }
307 
ossl_provider_store_new(OSSL_LIB_CTX * ctx)308 void *ossl_provider_store_new(OSSL_LIB_CTX *ctx)
309 {
310     struct provider_store_st *store = OPENSSL_zalloc(sizeof(*store));
311 
312     if (store == NULL
313         || (store->providers = sk_OSSL_PROVIDER_new(ossl_provider_cmp)) == NULL
314         || (store->default_path_lock = CRYPTO_THREAD_lock_new()) == NULL
315 #ifndef FIPS_MODULE
316         || (store->child_cbs = sk_OSSL_PROVIDER_CHILD_CB_new_null()) == NULL
317 #endif
318         || (store->lock = CRYPTO_THREAD_lock_new()) == NULL) {
319         ossl_provider_store_free(store);
320         return NULL;
321     }
322     store->libctx = ctx;
323     store->use_fallbacks = 1;
324 
325     return store;
326 }
327 
get_provider_store(OSSL_LIB_CTX * libctx)328 static struct provider_store_st *get_provider_store(OSSL_LIB_CTX *libctx)
329 {
330     struct provider_store_st *store = NULL;
331 
332     store = ossl_lib_ctx_get_data(libctx, OSSL_LIB_CTX_PROVIDER_STORE_INDEX);
333     if (store == NULL)
334         ERR_raise(ERR_LIB_CRYPTO, ERR_R_INTERNAL_ERROR);
335     return store;
336 }
337 
ossl_provider_disable_fallback_loading(OSSL_LIB_CTX * libctx)338 int ossl_provider_disable_fallback_loading(OSSL_LIB_CTX *libctx)
339 {
340     struct provider_store_st *store;
341 
342     if ((store = get_provider_store(libctx)) != NULL) {
343         if (!CRYPTO_THREAD_write_lock(store->lock))
344             return 0;
345         store->use_fallbacks = 0;
346         CRYPTO_THREAD_unlock(store->lock);
347         return 1;
348     }
349     return 0;
350 }
351 
352 #define BUILTINS_BLOCK_SIZE     10
353 
ossl_provider_info_add_to_store(OSSL_LIB_CTX * libctx,OSSL_PROVIDER_INFO * entry)354 int ossl_provider_info_add_to_store(OSSL_LIB_CTX *libctx,
355                                     OSSL_PROVIDER_INFO *entry)
356 {
357     struct provider_store_st *store = get_provider_store(libctx);
358     int ret = 0;
359 
360     if (entry->name == NULL) {
361         ERR_raise(ERR_LIB_CRYPTO, ERR_R_PASSED_NULL_PARAMETER);
362         return 0;
363     }
364 
365     if (store == NULL) {
366         ERR_raise(ERR_LIB_CRYPTO, ERR_R_INTERNAL_ERROR);
367         return 0;
368     }
369 
370     if (!CRYPTO_THREAD_write_lock(store->lock))
371         return 0;
372     if (store->provinfosz == 0) {
373         store->provinfo = OPENSSL_zalloc(sizeof(*store->provinfo)
374                                          * BUILTINS_BLOCK_SIZE);
375         if (store->provinfo == NULL)
376             goto err;
377         store->provinfosz = BUILTINS_BLOCK_SIZE;
378     } else if (store->numprovinfo == store->provinfosz) {
379         OSSL_PROVIDER_INFO *tmpbuiltins;
380         size_t newsz = store->provinfosz + BUILTINS_BLOCK_SIZE;
381 
382         tmpbuiltins = OPENSSL_realloc(store->provinfo,
383                                       sizeof(*store->provinfo) * newsz);
384         if (tmpbuiltins == NULL)
385             goto err;
386         store->provinfo = tmpbuiltins;
387         store->provinfosz = newsz;
388     }
389     store->provinfo[store->numprovinfo] = *entry;
390     store->numprovinfo++;
391 
392     ret = 1;
393  err:
394     CRYPTO_THREAD_unlock(store->lock);
395     return ret;
396 }
397 
ossl_provider_find(OSSL_LIB_CTX * libctx,const char * name,ossl_unused int noconfig)398 OSSL_PROVIDER *ossl_provider_find(OSSL_LIB_CTX *libctx, const char *name,
399                                   ossl_unused int noconfig)
400 {
401     struct provider_store_st *store = NULL;
402     OSSL_PROVIDER *prov = NULL;
403 
404     if ((store = get_provider_store(libctx)) != NULL) {
405         OSSL_PROVIDER tmpl = { 0, };
406         int i;
407 
408 #if !defined(FIPS_MODULE) && !defined(OPENSSL_NO_AUTOLOAD_CONFIG)
409         /*
410          * Make sure any providers are loaded from config before we try to find
411          * them.
412          */
413         if (!noconfig) {
414             if (ossl_lib_ctx_is_default(libctx))
415                 OPENSSL_init_crypto(OPENSSL_INIT_LOAD_CONFIG, NULL);
416         }
417 #endif
418 
419         tmpl.name = (char *)name;
420         if (!CRYPTO_THREAD_write_lock(store->lock))
421             return NULL;
422         sk_OSSL_PROVIDER_sort(store->providers);
423         if ((i = sk_OSSL_PROVIDER_find(store->providers, &tmpl)) != -1)
424             prov = sk_OSSL_PROVIDER_value(store->providers, i);
425         CRYPTO_THREAD_unlock(store->lock);
426         if (prov != NULL && !ossl_provider_up_ref(prov))
427             prov = NULL;
428     }
429 
430     return prov;
431 }
432 
433 /*-
434  * Provider Object methods
435  * =======================
436  */
437 
provider_new(const char * name,OSSL_provider_init_fn * init_function,STACK_OF (INFOPAIR)* parameters)438 static OSSL_PROVIDER *provider_new(const char *name,
439                                    OSSL_provider_init_fn *init_function,
440                                    STACK_OF(INFOPAIR) *parameters)
441 {
442     OSSL_PROVIDER *prov = NULL;
443 
444     if ((prov = OPENSSL_zalloc(sizeof(*prov))) == NULL)
445         return NULL;
446     if (!CRYPTO_NEW_REF(&prov->refcnt, 1)) {
447         OPENSSL_free(prov);
448         return NULL;
449     }
450     if ((prov->activatecnt_lock = CRYPTO_THREAD_lock_new()) == NULL) {
451         ossl_provider_free(prov);
452         ERR_raise(ERR_LIB_CRYPTO, ERR_R_CRYPTO_LIB);
453         return NULL;
454     }
455 
456     if ((prov->opbits_lock = CRYPTO_THREAD_lock_new()) == NULL
457         || (prov->flag_lock = CRYPTO_THREAD_lock_new()) == NULL
458         || (prov->parameters = sk_INFOPAIR_deep_copy(parameters,
459                                                      infopair_copy,
460                                                      infopair_free)) == NULL) {
461         ossl_provider_free(prov);
462         ERR_raise(ERR_LIB_CRYPTO, ERR_R_CRYPTO_LIB);
463         return NULL;
464     }
465     if ((prov->name = OPENSSL_strdup(name)) == NULL) {
466         ossl_provider_free(prov);
467         return NULL;
468     }
469 
470     prov->init_function = init_function;
471 
472     return prov;
473 }
474 
ossl_provider_up_ref(OSSL_PROVIDER * prov)475 int ossl_provider_up_ref(OSSL_PROVIDER *prov)
476 {
477     int ref = 0;
478 
479     if (CRYPTO_UP_REF(&prov->refcnt, &ref) <= 0)
480         return 0;
481 
482 #ifndef FIPS_MODULE
483     if (prov->ischild) {
484         if (!ossl_provider_up_ref_parent(prov, 0)) {
485             ossl_provider_free(prov);
486             return 0;
487         }
488     }
489 #endif
490 
491     return ref;
492 }
493 
494 #ifndef FIPS_MODULE
provider_up_ref_intern(OSSL_PROVIDER * prov,int activate)495 static int provider_up_ref_intern(OSSL_PROVIDER *prov, int activate)
496 {
497     if (activate)
498         return ossl_provider_activate(prov, 1, 0);
499 
500     return ossl_provider_up_ref(prov);
501 }
502 
provider_free_intern(OSSL_PROVIDER * prov,int deactivate)503 static int provider_free_intern(OSSL_PROVIDER *prov, int deactivate)
504 {
505     if (deactivate)
506         return ossl_provider_deactivate(prov, 1);
507 
508     ossl_provider_free(prov);
509     return 1;
510 }
511 #endif
512 
513 /*
514  * We assume that the requested provider does not already exist in the store.
515  * The caller should check. If it does exist then adding it to the store later
516  * will fail.
517  */
ossl_provider_new(OSSL_LIB_CTX * libctx,const char * name,OSSL_provider_init_fn * init_function,OSSL_PARAM * params,int noconfig)518 OSSL_PROVIDER *ossl_provider_new(OSSL_LIB_CTX *libctx, const char *name,
519                                  OSSL_provider_init_fn *init_function,
520                                  OSSL_PARAM *params, int noconfig)
521 {
522     struct provider_store_st *store = NULL;
523     OSSL_PROVIDER_INFO template;
524     OSSL_PROVIDER *prov = NULL;
525 
526     if ((store = get_provider_store(libctx)) == NULL)
527         return NULL;
528 
529     memset(&template, 0, sizeof(template));
530     if (init_function == NULL) {
531         const OSSL_PROVIDER_INFO *p;
532         size_t i;
533 
534         /* Check if this is a predefined builtin provider */
535         for (p = ossl_predefined_providers; p->name != NULL; p++) {
536             if (strcmp(p->name, name) == 0) {
537                 template = *p;
538                 break;
539             }
540         }
541         if (p->name == NULL) {
542             /* Check if this is a user added provider */
543             if (!CRYPTO_THREAD_read_lock(store->lock))
544                 return NULL;
545             for (i = 0, p = store->provinfo; i < store->numprovinfo; p++, i++) {
546                 if (strcmp(p->name, name) == 0) {
547                     template = *p;
548                     break;
549                 }
550             }
551             CRYPTO_THREAD_unlock(store->lock);
552         }
553     } else {
554         template.init = init_function;
555     }
556 
557     if (params != NULL) {
558         int i;
559 
560         template.parameters = sk_INFOPAIR_new_null();
561         if (template.parameters == NULL)
562             return NULL;
563 
564         for (i = 0; params[i].key != NULL; i++) {
565             if (params[i].data_type != OSSL_PARAM_UTF8_STRING)
566                 continue;
567             if (ossl_provider_info_add_parameter(&template, params[i].key,
568                                                  (char *)params[i].data) <= 0) {
569                 sk_INFOPAIR_pop_free(template.parameters, infopair_free);
570                 return NULL;
571             }
572         }
573     }
574 
575     /* provider_new() generates an error, so no need here */
576     prov = provider_new(name, template.init, template.parameters);
577 
578     if (params != NULL) /* We copied the parameters, let's free them */
579         sk_INFOPAIR_pop_free(template.parameters, infopair_free);
580 
581     if (prov == NULL)
582         return NULL;
583 
584     if (!ossl_provider_set_module_path(prov, template.path)) {
585         ossl_provider_free(prov);
586         return NULL;
587     }
588 
589     prov->libctx = libctx;
590 #ifndef FIPS_MODULE
591     prov->error_lib = ERR_get_next_error_library();
592 #endif
593 
594     /*
595      * At this point, the provider is only partially "loaded".  To be
596      * fully "loaded", ossl_provider_activate() must also be called and it must
597      * then be added to the provider store.
598      */
599 
600     return prov;
601 }
602 
603 /* Assumes that the store lock is held */
create_provider_children(OSSL_PROVIDER * prov)604 static int create_provider_children(OSSL_PROVIDER *prov)
605 {
606     int ret = 1;
607 #ifndef FIPS_MODULE
608     struct provider_store_st *store = prov->store;
609     OSSL_PROVIDER_CHILD_CB *child_cb;
610     int i, max;
611 
612     max = sk_OSSL_PROVIDER_CHILD_CB_num(store->child_cbs);
613     for (i = 0; i < max; i++) {
614         /*
615          * This is newly activated (activatecnt == 1), so we need to
616          * create child providers as necessary.
617          */
618         child_cb = sk_OSSL_PROVIDER_CHILD_CB_value(store->child_cbs, i);
619         ret &= child_cb->create_cb((OSSL_CORE_HANDLE *)prov, child_cb->cbdata);
620     }
621 #endif
622 
623     return ret;
624 }
625 
ossl_provider_add_to_store(OSSL_PROVIDER * prov,OSSL_PROVIDER ** actualprov,int retain_fallbacks)626 int ossl_provider_add_to_store(OSSL_PROVIDER *prov, OSSL_PROVIDER **actualprov,
627                                int retain_fallbacks)
628 {
629     struct provider_store_st *store;
630     int idx;
631     OSSL_PROVIDER tmpl = { 0, };
632     OSSL_PROVIDER *actualtmp = NULL;
633 
634     if (actualprov != NULL)
635         *actualprov = NULL;
636 
637     if ((store = get_provider_store(prov->libctx)) == NULL)
638         return 0;
639 
640     if (!CRYPTO_THREAD_write_lock(store->lock))
641         return 0;
642 
643     tmpl.name = (char *)prov->name;
644     idx = sk_OSSL_PROVIDER_find(store->providers, &tmpl);
645     if (idx == -1)
646         actualtmp = prov;
647     else
648         actualtmp = sk_OSSL_PROVIDER_value(store->providers, idx);
649 
650     if (idx == -1) {
651         if (sk_OSSL_PROVIDER_push(store->providers, prov) == 0)
652             goto err;
653         prov->store = store;
654         if (!create_provider_children(prov)) {
655             sk_OSSL_PROVIDER_delete_ptr(store->providers, prov);
656             goto err;
657         }
658         if (!retain_fallbacks)
659             store->use_fallbacks = 0;
660     }
661 
662     CRYPTO_THREAD_unlock(store->lock);
663 
664     if (actualprov != NULL) {
665         if (!ossl_provider_up_ref(actualtmp)) {
666             ERR_raise(ERR_LIB_CRYPTO, ERR_R_CRYPTO_LIB);
667             actualtmp = NULL;
668             return 0;
669         }
670         *actualprov = actualtmp;
671     }
672 
673     if (idx >= 0) {
674         /*
675          * The provider is already in the store. Probably two threads
676          * independently initialised their own provider objects with the same
677          * name and raced to put them in the store. This thread lost. We
678          * deactivate the one we just created and use the one that already
679          * exists instead.
680          * If we get here then we know we did not create provider children
681          * above, so we inform ossl_provider_deactivate not to attempt to remove
682          * any.
683          */
684         ossl_provider_deactivate(prov, 0);
685         ossl_provider_free(prov);
686     }
687 #ifndef FIPS_MODULE
688     else {
689         /*
690          * This can be done outside the lock. We tolerate other threads getting
691          * the wrong result briefly when creating OSSL_DECODER_CTXs.
692          */
693         ossl_decoder_cache_flush(prov->libctx);
694     }
695 #endif
696 
697     return 1;
698 
699  err:
700     CRYPTO_THREAD_unlock(store->lock);
701     return 0;
702 }
703 
ossl_provider_free(OSSL_PROVIDER * prov)704 void ossl_provider_free(OSSL_PROVIDER *prov)
705 {
706     if (prov != NULL) {
707         int ref = 0;
708 
709         CRYPTO_DOWN_REF(&prov->refcnt, &ref);
710 
711         /*
712          * When the refcount drops to zero, we clean up the provider.
713          * Note that this also does teardown, which may seem late,
714          * considering that init happens on first activation.  However,
715          * there may be other structures hanging on to the provider after
716          * the last deactivation and may therefore need full access to the
717          * provider's services.  Therefore, we deinit late.
718          */
719         if (ref == 0) {
720             if (prov->flag_initialized) {
721                 ossl_provider_teardown(prov);
722 #ifndef OPENSSL_NO_ERR
723 # ifndef FIPS_MODULE
724                 if (prov->error_strings != NULL) {
725                     ERR_unload_strings(prov->error_lib, prov->error_strings);
726                     OPENSSL_free(prov->error_strings);
727                     prov->error_strings = NULL;
728                 }
729 # endif
730 #endif
731                 OPENSSL_free(prov->operation_bits);
732                 prov->operation_bits = NULL;
733                 prov->operation_bits_sz = 0;
734                 prov->flag_initialized = 0;
735             }
736 
737 #ifndef FIPS_MODULE
738             /*
739              * We deregister thread handling whether or not the provider was
740              * initialized. If init was attempted but was not successful then
741              * the provider may still have registered a thread handler.
742              */
743             ossl_init_thread_deregister(prov);
744             DSO_free(prov->module);
745 #endif
746             OPENSSL_free(prov->name);
747             OPENSSL_free(prov->path);
748             sk_INFOPAIR_pop_free(prov->parameters, infopair_free);
749             CRYPTO_THREAD_lock_free(prov->opbits_lock);
750             CRYPTO_THREAD_lock_free(prov->flag_lock);
751             CRYPTO_THREAD_lock_free(prov->activatecnt_lock);
752             CRYPTO_FREE_REF(&prov->refcnt);
753             OPENSSL_free(prov);
754         }
755 #ifndef FIPS_MODULE
756         else if (prov->ischild) {
757             ossl_provider_free_parent(prov, 0);
758         }
759 #endif
760     }
761 }
762 
763 /* Setters */
ossl_provider_set_module_path(OSSL_PROVIDER * prov,const char * module_path)764 int ossl_provider_set_module_path(OSSL_PROVIDER *prov, const char *module_path)
765 {
766     OPENSSL_free(prov->path);
767     prov->path = NULL;
768     if (module_path == NULL)
769         return 1;
770     if ((prov->path = OPENSSL_strdup(module_path)) != NULL)
771         return 1;
772     return 0;
773 }
774 
infopair_add(STACK_OF (INFOPAIR)** infopairsk,const char * name,const char * value)775 static int infopair_add(STACK_OF(INFOPAIR) **infopairsk, const char *name,
776                         const char *value)
777 {
778     INFOPAIR *pair = NULL;
779 
780     if ((pair = OPENSSL_zalloc(sizeof(*pair))) == NULL
781         || (pair->name = OPENSSL_strdup(name)) == NULL
782         || (pair->value = OPENSSL_strdup(value)) == NULL)
783         goto err;
784 
785     if ((*infopairsk == NULL
786          && (*infopairsk = sk_INFOPAIR_new_null()) == NULL)
787         || sk_INFOPAIR_push(*infopairsk, pair) <= 0) {
788         ERR_raise(ERR_LIB_CRYPTO, ERR_R_CRYPTO_LIB);
789         goto err;
790     }
791 
792     return 1;
793 
794  err:
795     if (pair != NULL) {
796         OPENSSL_free(pair->name);
797         OPENSSL_free(pair->value);
798         OPENSSL_free(pair);
799     }
800     return 0;
801 }
802 
ossl_provider_add_parameter(OSSL_PROVIDER * prov,const char * name,const char * value)803 int ossl_provider_add_parameter(OSSL_PROVIDER *prov,
804                                 const char *name, const char *value)
805 {
806     return infopair_add(&prov->parameters, name, value);
807 }
808 
ossl_provider_info_add_parameter(OSSL_PROVIDER_INFO * provinfo,const char * name,const char * value)809 int ossl_provider_info_add_parameter(OSSL_PROVIDER_INFO *provinfo,
810                                      const char *name,
811                                      const char *value)
812 {
813     return infopair_add(&provinfo->parameters, name, value);
814 }
815 
816 /*
817  * Provider activation.
818  *
819  * What "activation" means depends on the provider form; for built in
820  * providers (in the library or the application alike), the provider
821  * can already be considered to be loaded, all that's needed is to
822  * initialize it.  However, for dynamically loadable provider modules,
823  * we must first load that module.
824  *
825  * Built in modules are distinguished from dynamically loaded modules
826  * with an already assigned init function.
827  */
828 static const OSSL_DISPATCH *core_dispatch; /* Define further down */
829 
OSSL_PROVIDER_set_default_search_path(OSSL_LIB_CTX * libctx,const char * path)830 int OSSL_PROVIDER_set_default_search_path(OSSL_LIB_CTX *libctx,
831                                           const char *path)
832 {
833     struct provider_store_st *store;
834     char *p = NULL;
835 
836     if (path != NULL) {
837         p = OPENSSL_strdup(path);
838         if (p == NULL)
839             return 0;
840     }
841     if ((store = get_provider_store(libctx)) != NULL
842             && CRYPTO_THREAD_write_lock(store->default_path_lock)) {
843         OPENSSL_free(store->default_path);
844         store->default_path = p;
845         CRYPTO_THREAD_unlock(store->default_path_lock);
846         return 1;
847     }
848     OPENSSL_free(p);
849     return 0;
850 }
851 
OSSL_PROVIDER_get0_default_search_path(OSSL_LIB_CTX * libctx)852 const char *OSSL_PROVIDER_get0_default_search_path(OSSL_LIB_CTX *libctx)
853 {
854     struct provider_store_st *store;
855     char *path = NULL;
856 
857     if ((store = get_provider_store(libctx)) != NULL
858             && CRYPTO_THREAD_read_lock(store->default_path_lock)) {
859         path = store->default_path;
860         CRYPTO_THREAD_unlock(store->default_path_lock);
861     }
862     return path;
863 }
864 
865 /*
866  * Internal version that doesn't affect the store flags, and thereby avoid
867  * locking.  Direct callers must remember to set the store flags when
868  * appropriate.
869  */
provider_init(OSSL_PROVIDER * prov)870 static int provider_init(OSSL_PROVIDER *prov)
871 {
872     const OSSL_DISPATCH *provider_dispatch = NULL;
873     void *tmp_provctx = NULL;    /* safety measure */
874 #ifndef OPENSSL_NO_ERR
875 # ifndef FIPS_MODULE
876     OSSL_FUNC_provider_get_reason_strings_fn *p_get_reason_strings = NULL;
877 # endif
878 #endif
879     int ok = 0;
880 
881     if (!ossl_assert(!prov->flag_initialized)) {
882         ERR_raise(ERR_LIB_CRYPTO, ERR_R_INTERNAL_ERROR);
883         goto end;
884     }
885 
886     /*
887      * If the init function isn't set, it indicates that this provider is
888      * a loadable module.
889      */
890     if (prov->init_function == NULL) {
891 #ifdef FIPS_MODULE
892         goto end;
893 #else
894         if (prov->module == NULL) {
895             char *allocated_path = NULL;
896             const char *module_path = NULL;
897             char *merged_path = NULL;
898             const char *load_dir = NULL;
899             char *allocated_load_dir = NULL;
900             struct provider_store_st *store;
901 
902             if ((prov->module = DSO_new()) == NULL) {
903                 /* DSO_new() generates an error already */
904                 goto end;
905             }
906 
907             if ((store = get_provider_store(prov->libctx)) == NULL
908                     || !CRYPTO_THREAD_read_lock(store->default_path_lock))
909                 goto end;
910 
911             if (store->default_path != NULL) {
912                 allocated_load_dir = OPENSSL_strdup(store->default_path);
913                 CRYPTO_THREAD_unlock(store->default_path_lock);
914                 if (allocated_load_dir == NULL)
915                     goto end;
916                 load_dir = allocated_load_dir;
917             } else {
918                 CRYPTO_THREAD_unlock(store->default_path_lock);
919             }
920 
921             if (load_dir == NULL) {
922                 load_dir = ossl_safe_getenv("OPENSSL_MODULES");
923                 if (load_dir == NULL)
924                     load_dir = ossl_get_modulesdir();
925             }
926 
927             DSO_ctrl(prov->module, DSO_CTRL_SET_FLAGS,
928                      DSO_FLAG_NAME_TRANSLATION_EXT_ONLY, NULL);
929 
930             module_path = prov->path;
931             if (module_path == NULL)
932                 module_path = allocated_path =
933                     DSO_convert_filename(prov->module, prov->name);
934             if (module_path != NULL)
935                 merged_path = DSO_merge(prov->module, module_path, load_dir);
936 
937             if (merged_path == NULL
938                 || (DSO_load(prov->module, merged_path, NULL, 0)) == NULL) {
939                 DSO_free(prov->module);
940                 prov->module = NULL;
941             }
942 
943             OPENSSL_free(merged_path);
944             OPENSSL_free(allocated_path);
945             OPENSSL_free(allocated_load_dir);
946         }
947 
948         if (prov->module == NULL) {
949             /* DSO has already recorded errors, this is just a tracepoint */
950             ERR_raise_data(ERR_LIB_CRYPTO, ERR_R_DSO_LIB,
951                            "name=%s", prov->name);
952             goto end;
953         }
954 
955         prov->init_function = (OSSL_provider_init_fn *)
956             DSO_bind_func(prov->module, "OSSL_provider_init");
957 #endif
958     }
959 
960     /* Check for and call the initialise function for the provider. */
961     if (prov->init_function == NULL) {
962         ERR_raise_data(ERR_LIB_CRYPTO, ERR_R_UNSUPPORTED,
963                        "name=%s, provider has no provider init function",
964                        prov->name);
965         goto end;
966     }
967 
968     if (!prov->init_function((OSSL_CORE_HANDLE *)prov, core_dispatch,
969                              &provider_dispatch, &tmp_provctx)) {
970         ERR_raise_data(ERR_LIB_CRYPTO, ERR_R_INIT_FAIL,
971                        "name=%s", prov->name);
972         goto end;
973     }
974     prov->provctx = tmp_provctx;
975     prov->dispatch = provider_dispatch;
976 
977     if (provider_dispatch != NULL) {
978         for (; provider_dispatch->function_id != 0; provider_dispatch++) {
979             switch (provider_dispatch->function_id) {
980             case OSSL_FUNC_PROVIDER_TEARDOWN:
981                 prov->teardown =
982                     OSSL_FUNC_provider_teardown(provider_dispatch);
983                 break;
984             case OSSL_FUNC_PROVIDER_GETTABLE_PARAMS:
985                 prov->gettable_params =
986                     OSSL_FUNC_provider_gettable_params(provider_dispatch);
987                 break;
988             case OSSL_FUNC_PROVIDER_GET_PARAMS:
989                 prov->get_params =
990                     OSSL_FUNC_provider_get_params(provider_dispatch);
991                 break;
992             case OSSL_FUNC_PROVIDER_SELF_TEST:
993                 prov->self_test =
994                     OSSL_FUNC_provider_self_test(provider_dispatch);
995                 break;
996             case OSSL_FUNC_PROVIDER_GET_CAPABILITIES:
997                 prov->get_capabilities =
998                     OSSL_FUNC_provider_get_capabilities(provider_dispatch);
999                 break;
1000             case OSSL_FUNC_PROVIDER_QUERY_OPERATION:
1001                 prov->query_operation =
1002                     OSSL_FUNC_provider_query_operation(provider_dispatch);
1003                 break;
1004             case OSSL_FUNC_PROVIDER_UNQUERY_OPERATION:
1005                 prov->unquery_operation =
1006                     OSSL_FUNC_provider_unquery_operation(provider_dispatch);
1007                 break;
1008 #ifndef OPENSSL_NO_ERR
1009 # ifndef FIPS_MODULE
1010             case OSSL_FUNC_PROVIDER_GET_REASON_STRINGS:
1011                 p_get_reason_strings =
1012                     OSSL_FUNC_provider_get_reason_strings(provider_dispatch);
1013                 break;
1014 # endif
1015 #endif
1016             }
1017         }
1018     }
1019 
1020 #ifndef OPENSSL_NO_ERR
1021 # ifndef FIPS_MODULE
1022     if (p_get_reason_strings != NULL) {
1023         const OSSL_ITEM *reasonstrings = p_get_reason_strings(prov->provctx);
1024         size_t cnt, cnt2;
1025 
1026         /*
1027          * ERR_load_strings() handles ERR_STRING_DATA rather than OSSL_ITEM,
1028          * although they are essentially the same type.
1029          * Furthermore, ERR_load_strings() patches the array's error number
1030          * with the error library number, so we need to make a copy of that
1031          * array either way.
1032          */
1033         cnt = 0;
1034         while (reasonstrings[cnt].id != 0) {
1035             if (ERR_GET_LIB(reasonstrings[cnt].id) != 0)
1036                 goto end;
1037             cnt++;
1038         }
1039         cnt++;                   /* One for the terminating item */
1040 
1041         /* Allocate one extra item for the "library" name */
1042         prov->error_strings =
1043             OPENSSL_zalloc(sizeof(ERR_STRING_DATA) * (cnt + 1));
1044         if (prov->error_strings == NULL)
1045             goto end;
1046 
1047         /*
1048          * Set the "library" name.
1049          */
1050         prov->error_strings[0].error = ERR_PACK(prov->error_lib, 0, 0);
1051         prov->error_strings[0].string = prov->name;
1052         /*
1053          * Copy reasonstrings item 0..cnt-1 to prov->error_trings positions
1054          * 1..cnt.
1055          */
1056         for (cnt2 = 1; cnt2 <= cnt; cnt2++) {
1057             prov->error_strings[cnt2].error = (int)reasonstrings[cnt2-1].id;
1058             prov->error_strings[cnt2].string = reasonstrings[cnt2-1].ptr;
1059         }
1060 
1061         ERR_load_strings(prov->error_lib, prov->error_strings);
1062     }
1063 # endif
1064 #endif
1065 
1066     /* With this flag set, this provider has become fully "loaded". */
1067     prov->flag_initialized = 1;
1068     ok = 1;
1069 
1070  end:
1071     return ok;
1072 }
1073 
1074 /*
1075  * Deactivate a provider. If upcalls is 0 then we suppress any upcalls to a
1076  * parent provider. If removechildren is 0 then we suppress any calls to remove
1077  * child providers.
1078  * Return -1 on failure and the activation count on success
1079  */
provider_deactivate(OSSL_PROVIDER * prov,int upcalls,int removechildren)1080 static int provider_deactivate(OSSL_PROVIDER *prov, int upcalls,
1081                                int removechildren)
1082 {
1083     int count;
1084     struct provider_store_st *store;
1085 #ifndef FIPS_MODULE
1086     int freeparent = 0;
1087 #endif
1088     int lock = 1;
1089 
1090     if (!ossl_assert(prov != NULL))
1091         return -1;
1092 
1093     /*
1094      * No need to lock if we've got no store because we've not been shared with
1095      * other threads.
1096      */
1097     store = get_provider_store(prov->libctx);
1098     if (store == NULL)
1099         lock = 0;
1100 
1101     if (lock && !CRYPTO_THREAD_read_lock(store->lock))
1102         return -1;
1103     if (lock && !CRYPTO_THREAD_write_lock(prov->flag_lock)) {
1104         CRYPTO_THREAD_unlock(store->lock);
1105         return -1;
1106     }
1107 
1108     CRYPTO_atomic_add(&prov->activatecnt, -1, &count, prov->activatecnt_lock);
1109 #ifndef FIPS_MODULE
1110     if (count >= 1 && prov->ischild && upcalls) {
1111         /*
1112          * We have had a direct activation in this child libctx so we need to
1113          * now down the ref count in the parent provider. We do the actual down
1114          * ref outside of the flag_lock, since it could involve getting other
1115          * locks.
1116          */
1117         freeparent = 1;
1118     }
1119 #endif
1120 
1121     if (count < 1)
1122         prov->flag_activated = 0;
1123 #ifndef FIPS_MODULE
1124     else
1125         removechildren = 0;
1126 #endif
1127 
1128 #ifndef FIPS_MODULE
1129     if (removechildren && store != NULL) {
1130         int i, max = sk_OSSL_PROVIDER_CHILD_CB_num(store->child_cbs);
1131         OSSL_PROVIDER_CHILD_CB *child_cb;
1132 
1133         for (i = 0; i < max; i++) {
1134             child_cb = sk_OSSL_PROVIDER_CHILD_CB_value(store->child_cbs, i);
1135             child_cb->remove_cb((OSSL_CORE_HANDLE *)prov, child_cb->cbdata);
1136         }
1137     }
1138 #endif
1139     if (lock) {
1140         CRYPTO_THREAD_unlock(prov->flag_lock);
1141         CRYPTO_THREAD_unlock(store->lock);
1142         /*
1143          * This can be done outside the lock. We tolerate other threads getting
1144          * the wrong result briefly when creating OSSL_DECODER_CTXs.
1145          */
1146 #ifndef FIPS_MODULE
1147         if (count < 1)
1148             ossl_decoder_cache_flush(prov->libctx);
1149 #endif
1150     }
1151 #ifndef FIPS_MODULE
1152     if (freeparent)
1153         ossl_provider_free_parent(prov, 1);
1154 #endif
1155 
1156     /* We don't deinit here, that's done in ossl_provider_free() */
1157     return count;
1158 }
1159 
1160 /*
1161  * Activate a provider.
1162  * Return -1 on failure and the activation count on success
1163  */
provider_activate(OSSL_PROVIDER * prov,int lock,int upcalls)1164 static int provider_activate(OSSL_PROVIDER *prov, int lock, int upcalls)
1165 {
1166     int count = -1;
1167     struct provider_store_st *store;
1168     int ret = 1;
1169 
1170     store = prov->store;
1171     /*
1172     * If the provider hasn't been added to the store, then we don't need
1173     * any locks because we've not shared it with other threads.
1174     */
1175     if (store == NULL) {
1176         lock = 0;
1177         if (!provider_init(prov))
1178             return -1;
1179     }
1180 
1181 #ifndef FIPS_MODULE
1182     if (prov->ischild && upcalls && !ossl_provider_up_ref_parent(prov, 1))
1183         return -1;
1184 #endif
1185 
1186     if (lock && !CRYPTO_THREAD_read_lock(store->lock)) {
1187 #ifndef FIPS_MODULE
1188         if (prov->ischild && upcalls)
1189             ossl_provider_free_parent(prov, 1);
1190 #endif
1191         return -1;
1192     }
1193 
1194     if (lock && !CRYPTO_THREAD_write_lock(prov->flag_lock)) {
1195         CRYPTO_THREAD_unlock(store->lock);
1196 #ifndef FIPS_MODULE
1197         if (prov->ischild && upcalls)
1198             ossl_provider_free_parent(prov, 1);
1199 #endif
1200         return -1;
1201     }
1202     if (CRYPTO_atomic_add(&prov->activatecnt, 1, &count, prov->activatecnt_lock)) {
1203         prov->flag_activated = 1;
1204 
1205         if (count == 1 && store != NULL) {
1206             ret = create_provider_children(prov);
1207         }
1208     }
1209     if (lock) {
1210         CRYPTO_THREAD_unlock(prov->flag_lock);
1211         CRYPTO_THREAD_unlock(store->lock);
1212         /*
1213          * This can be done outside the lock. We tolerate other threads getting
1214          * the wrong result briefly when creating OSSL_DECODER_CTXs.
1215          */
1216 #ifndef FIPS_MODULE
1217         if (count == 1)
1218             ossl_decoder_cache_flush(prov->libctx);
1219 #endif
1220     }
1221 
1222     if (!ret)
1223         return -1;
1224 
1225     return count;
1226 }
1227 
provider_flush_store_cache(const OSSL_PROVIDER * prov)1228 static int provider_flush_store_cache(const OSSL_PROVIDER *prov)
1229 {
1230     struct provider_store_st *store;
1231     int freeing;
1232 
1233     if ((store = get_provider_store(prov->libctx)) == NULL)
1234         return 0;
1235 
1236     if (!CRYPTO_THREAD_read_lock(store->lock))
1237         return 0;
1238     freeing = store->freeing;
1239     CRYPTO_THREAD_unlock(store->lock);
1240 
1241     if (!freeing) {
1242         int acc
1243             = evp_method_store_cache_flush(prov->libctx)
1244 #ifndef FIPS_MODULE
1245             + ossl_encoder_store_cache_flush(prov->libctx)
1246             + ossl_decoder_store_cache_flush(prov->libctx)
1247             + ossl_store_loader_store_cache_flush(prov->libctx)
1248 #endif
1249             ;
1250 
1251 #ifndef FIPS_MODULE
1252         return acc == 4;
1253 #else
1254         return acc == 1;
1255 #endif
1256     }
1257     return 1;
1258 }
1259 
provider_remove_store_methods(OSSL_PROVIDER * prov)1260 static int provider_remove_store_methods(OSSL_PROVIDER *prov)
1261 {
1262     struct provider_store_st *store;
1263     int freeing;
1264 
1265     if ((store = get_provider_store(prov->libctx)) == NULL)
1266         return 0;
1267 
1268     if (!CRYPTO_THREAD_read_lock(store->lock))
1269         return 0;
1270     freeing = store->freeing;
1271     CRYPTO_THREAD_unlock(store->lock);
1272 
1273     if (!freeing) {
1274         int acc;
1275 
1276         if (!CRYPTO_THREAD_write_lock(prov->opbits_lock))
1277             return 0;
1278         OPENSSL_free(prov->operation_bits);
1279         prov->operation_bits = NULL;
1280         prov->operation_bits_sz = 0;
1281         CRYPTO_THREAD_unlock(prov->opbits_lock);
1282 
1283         acc = evp_method_store_remove_all_provided(prov)
1284 #ifndef FIPS_MODULE
1285             + ossl_encoder_store_remove_all_provided(prov)
1286             + ossl_decoder_store_remove_all_provided(prov)
1287             + ossl_store_loader_store_remove_all_provided(prov)
1288 #endif
1289             ;
1290 
1291 #ifndef FIPS_MODULE
1292         return acc == 4;
1293 #else
1294         return acc == 1;
1295 #endif
1296     }
1297     return 1;
1298 }
1299 
ossl_provider_activate(OSSL_PROVIDER * prov,int upcalls,int aschild)1300 int ossl_provider_activate(OSSL_PROVIDER *prov, int upcalls, int aschild)
1301 {
1302     int count;
1303 
1304     if (prov == NULL)
1305         return 0;
1306 #ifndef FIPS_MODULE
1307     /*
1308      * If aschild is true, then we only actually do the activation if the
1309      * provider is a child. If its not, this is still success.
1310      */
1311     if (aschild && !prov->ischild)
1312         return 1;
1313 #endif
1314     if ((count = provider_activate(prov, 1, upcalls)) > 0)
1315         return count == 1 ? provider_flush_store_cache(prov) : 1;
1316 
1317     return 0;
1318 }
1319 
ossl_provider_deactivate(OSSL_PROVIDER * prov,int removechildren)1320 int ossl_provider_deactivate(OSSL_PROVIDER *prov, int removechildren)
1321 {
1322     int count;
1323 
1324     if (prov == NULL
1325             || (count = provider_deactivate(prov, 1, removechildren)) < 0)
1326         return 0;
1327     return count == 0 ? provider_remove_store_methods(prov) : 1;
1328 }
1329 
ossl_provider_ctx(const OSSL_PROVIDER * prov)1330 void *ossl_provider_ctx(const OSSL_PROVIDER *prov)
1331 {
1332     return prov != NULL ? prov->provctx : NULL;
1333 }
1334 
1335 /*
1336  * This function only does something once when store->use_fallbacks == 1,
1337  * and then sets store->use_fallbacks = 0, so the second call and so on is
1338  * effectively a no-op.
1339  */
provider_activate_fallbacks(struct provider_store_st * store)1340 static int provider_activate_fallbacks(struct provider_store_st *store)
1341 {
1342     int use_fallbacks;
1343     int activated_fallback_count = 0;
1344     int ret = 0;
1345     const OSSL_PROVIDER_INFO *p;
1346 
1347     if (!CRYPTO_THREAD_read_lock(store->lock))
1348         return 0;
1349     use_fallbacks = store->use_fallbacks;
1350     CRYPTO_THREAD_unlock(store->lock);
1351     if (!use_fallbacks)
1352         return 1;
1353 
1354     if (!CRYPTO_THREAD_write_lock(store->lock))
1355         return 0;
1356     /* Check again, just in case another thread changed it */
1357     use_fallbacks = store->use_fallbacks;
1358     if (!use_fallbacks) {
1359         CRYPTO_THREAD_unlock(store->lock);
1360         return 1;
1361     }
1362 
1363     for (p = ossl_predefined_providers; p->name != NULL; p++) {
1364         OSSL_PROVIDER *prov = NULL;
1365 
1366         if (!p->is_fallback)
1367             continue;
1368         /*
1369          * We use the internal constructor directly here,
1370          * otherwise we get a call loop
1371          */
1372         prov = provider_new(p->name, p->init, NULL);
1373         if (prov == NULL)
1374             goto err;
1375         prov->libctx = store->libctx;
1376 #ifndef FIPS_MODULE
1377         prov->error_lib = ERR_get_next_error_library();
1378 #endif
1379 
1380         /*
1381          * We are calling provider_activate while holding the store lock. This
1382          * means the init function will be called while holding a lock. Normally
1383          * we try to avoid calling a user callback while holding a lock.
1384          * However, fallbacks are never third party providers so we accept this.
1385          */
1386         if (provider_activate(prov, 0, 0) < 0) {
1387             ossl_provider_free(prov);
1388             goto err;
1389         }
1390         prov->store = store;
1391         if (sk_OSSL_PROVIDER_push(store->providers, prov) == 0) {
1392             ossl_provider_free(prov);
1393             goto err;
1394         }
1395         activated_fallback_count++;
1396     }
1397 
1398     if (activated_fallback_count > 0) {
1399         store->use_fallbacks = 0;
1400         ret = 1;
1401     }
1402  err:
1403     CRYPTO_THREAD_unlock(store->lock);
1404     return ret;
1405 }
1406 
ossl_provider_doall_activated(OSSL_LIB_CTX * ctx,int (* cb)(OSSL_PROVIDER * provider,void * cbdata),void * cbdata)1407 int ossl_provider_doall_activated(OSSL_LIB_CTX *ctx,
1408                                   int (*cb)(OSSL_PROVIDER *provider,
1409                                             void *cbdata),
1410                                   void *cbdata)
1411 {
1412     int ret = 0, curr, max, ref = 0;
1413     struct provider_store_st *store = get_provider_store(ctx);
1414     STACK_OF(OSSL_PROVIDER) *provs = NULL;
1415 
1416 #if !defined(FIPS_MODULE) && !defined(OPENSSL_NO_AUTOLOAD_CONFIG)
1417     /*
1418      * Make sure any providers are loaded from config before we try to use
1419      * them.
1420      */
1421     if (ossl_lib_ctx_is_default(ctx))
1422         OPENSSL_init_crypto(OPENSSL_INIT_LOAD_CONFIG, NULL);
1423 #endif
1424 
1425     if (store == NULL)
1426         return 1;
1427     if (!provider_activate_fallbacks(store))
1428         return 0;
1429 
1430     /*
1431      * Under lock, grab a copy of the provider list and up_ref each
1432      * provider so that they don't disappear underneath us.
1433      */
1434     if (!CRYPTO_THREAD_read_lock(store->lock))
1435         return 0;
1436     provs = sk_OSSL_PROVIDER_dup(store->providers);
1437     if (provs == NULL) {
1438         CRYPTO_THREAD_unlock(store->lock);
1439         return 0;
1440     }
1441     max = sk_OSSL_PROVIDER_num(provs);
1442     /*
1443      * We work backwards through the stack so that we can safely delete items
1444      * as we go.
1445      */
1446     for (curr = max - 1; curr >= 0; curr--) {
1447         OSSL_PROVIDER *prov = sk_OSSL_PROVIDER_value(provs, curr);
1448 
1449         if (!CRYPTO_THREAD_read_lock(prov->flag_lock))
1450             goto err_unlock;
1451         if (prov->flag_activated) {
1452             /*
1453              * We call CRYPTO_UP_REF directly rather than ossl_provider_up_ref
1454              * to avoid upping the ref count on the parent provider, which we
1455              * must not do while holding locks.
1456              */
1457             if (CRYPTO_UP_REF(&prov->refcnt, &ref) <= 0) {
1458                 CRYPTO_THREAD_unlock(prov->flag_lock);
1459                 goto err_unlock;
1460             }
1461             /*
1462              * It's already activated, but we up the activated count to ensure
1463              * it remains activated until after we've called the user callback.
1464              * In theory this could mean the parent provider goes inactive,
1465              * whilst still activated in the child for a short period. That's ok.
1466              */
1467             if (!CRYPTO_atomic_add(&prov->activatecnt, 1, &ref,
1468                                    prov->activatecnt_lock)) {
1469                 CRYPTO_DOWN_REF(&prov->refcnt, &ref);
1470                 CRYPTO_THREAD_unlock(prov->flag_lock);
1471                 goto err_unlock;
1472             }
1473         } else {
1474             sk_OSSL_PROVIDER_delete(provs, curr);
1475             max--;
1476         }
1477         CRYPTO_THREAD_unlock(prov->flag_lock);
1478     }
1479     CRYPTO_THREAD_unlock(store->lock);
1480 
1481     /*
1482      * Now, we sweep through all providers not under lock
1483      */
1484     for (curr = 0; curr < max; curr++) {
1485         OSSL_PROVIDER *prov = sk_OSSL_PROVIDER_value(provs, curr);
1486 
1487         if (!cb(prov, cbdata)) {
1488             curr = -1;
1489             goto finish;
1490         }
1491     }
1492     curr = -1;
1493 
1494     ret = 1;
1495     goto finish;
1496 
1497  err_unlock:
1498     CRYPTO_THREAD_unlock(store->lock);
1499  finish:
1500     /*
1501      * The pop_free call doesn't do what we want on an error condition. We
1502      * either start from the first item in the stack, or part way through if
1503      * we only processed some of the items.
1504      */
1505     for (curr++; curr < max; curr++) {
1506         OSSL_PROVIDER *prov = sk_OSSL_PROVIDER_value(provs, curr);
1507 
1508         if (!CRYPTO_atomic_add(&prov->activatecnt, -1, &ref,
1509                                prov->activatecnt_lock)) {
1510             ret = 0;
1511             continue;
1512         }
1513         if (ref < 1) {
1514             /*
1515              * Looks like we need to deactivate properly. We could just have
1516              * done this originally, but it involves taking a write lock so
1517              * we avoid it. We up the count again and do a full deactivation
1518              */
1519             if (CRYPTO_atomic_add(&prov->activatecnt, 1, &ref,
1520                                   prov->activatecnt_lock))
1521                 provider_deactivate(prov, 0, 1);
1522             else
1523                 ret = 0;
1524         }
1525         /*
1526          * As above where we did the up-ref, we don't call ossl_provider_free
1527          * to avoid making upcalls. There should always be at least one ref
1528          * to the provider in the store, so this should never drop to 0.
1529          */
1530         if (!CRYPTO_DOWN_REF(&prov->refcnt, &ref)) {
1531             ret = 0;
1532             continue;
1533         }
1534         /*
1535          * Not much we can do if this assert ever fails. So we don't use
1536          * ossl_assert here.
1537          */
1538         assert(ref > 0);
1539     }
1540     sk_OSSL_PROVIDER_free(provs);
1541     return ret;
1542 }
1543 
OSSL_PROVIDER_available(OSSL_LIB_CTX * libctx,const char * name)1544 int OSSL_PROVIDER_available(OSSL_LIB_CTX *libctx, const char *name)
1545 {
1546     OSSL_PROVIDER *prov = NULL;
1547     int available = 0;
1548     struct provider_store_st *store = get_provider_store(libctx);
1549 
1550     if (store == NULL || !provider_activate_fallbacks(store))
1551         return 0;
1552 
1553     prov = ossl_provider_find(libctx, name, 0);
1554     if (prov != NULL) {
1555         if (!CRYPTO_THREAD_read_lock(prov->flag_lock))
1556             return 0;
1557         available = prov->flag_activated;
1558         CRYPTO_THREAD_unlock(prov->flag_lock);
1559         ossl_provider_free(prov);
1560     }
1561     return available;
1562 }
1563 
1564 /* Getters of Provider Object data */
ossl_provider_name(const OSSL_PROVIDER * prov)1565 const char *ossl_provider_name(const OSSL_PROVIDER *prov)
1566 {
1567     return prov->name;
1568 }
1569 
ossl_provider_dso(const OSSL_PROVIDER * prov)1570 const DSO *ossl_provider_dso(const OSSL_PROVIDER *prov)
1571 {
1572     return prov->module;
1573 }
1574 
ossl_provider_module_name(const OSSL_PROVIDER * prov)1575 const char *ossl_provider_module_name(const OSSL_PROVIDER *prov)
1576 {
1577 #ifdef FIPS_MODULE
1578     return NULL;
1579 #else
1580     return DSO_get_filename(prov->module);
1581 #endif
1582 }
1583 
ossl_provider_module_path(const OSSL_PROVIDER * prov)1584 const char *ossl_provider_module_path(const OSSL_PROVIDER *prov)
1585 {
1586 #ifdef FIPS_MODULE
1587     return NULL;
1588 #else
1589     /* FIXME: Ensure it's a full path */
1590     return DSO_get_filename(prov->module);
1591 #endif
1592 }
1593 
ossl_provider_prov_ctx(const OSSL_PROVIDER * prov)1594 void *ossl_provider_prov_ctx(const OSSL_PROVIDER *prov)
1595 {
1596     if (prov != NULL)
1597         return prov->provctx;
1598 
1599     return NULL;
1600 }
1601 
ossl_provider_get0_dispatch(const OSSL_PROVIDER * prov)1602 const OSSL_DISPATCH *ossl_provider_get0_dispatch(const OSSL_PROVIDER *prov)
1603 {
1604     if (prov != NULL)
1605         return prov->dispatch;
1606 
1607     return NULL;
1608 }
1609 
ossl_provider_libctx(const OSSL_PROVIDER * prov)1610 OSSL_LIB_CTX *ossl_provider_libctx(const OSSL_PROVIDER *prov)
1611 {
1612     return prov != NULL ? prov->libctx : NULL;
1613 }
1614 
1615 /* Wrappers around calls to the provider */
ossl_provider_teardown(const OSSL_PROVIDER * prov)1616 void ossl_provider_teardown(const OSSL_PROVIDER *prov)
1617 {
1618     if (prov->teardown != NULL
1619 #ifndef FIPS_MODULE
1620             && !prov->ischild
1621 #endif
1622        )
1623         prov->teardown(prov->provctx);
1624 }
1625 
ossl_provider_gettable_params(const OSSL_PROVIDER * prov)1626 const OSSL_PARAM *ossl_provider_gettable_params(const OSSL_PROVIDER *prov)
1627 {
1628     return prov->gettable_params == NULL
1629         ? NULL : prov->gettable_params(prov->provctx);
1630 }
1631 
ossl_provider_get_params(const OSSL_PROVIDER * prov,OSSL_PARAM params[])1632 int ossl_provider_get_params(const OSSL_PROVIDER *prov, OSSL_PARAM params[])
1633 {
1634     return prov->get_params == NULL
1635         ? 0 : prov->get_params(prov->provctx, params);
1636 }
1637 
ossl_provider_self_test(const OSSL_PROVIDER * prov)1638 int ossl_provider_self_test(const OSSL_PROVIDER *prov)
1639 {
1640     int ret;
1641 
1642     if (prov->self_test == NULL)
1643         return 1;
1644     ret = prov->self_test(prov->provctx);
1645     if (ret == 0)
1646         (void)provider_remove_store_methods((OSSL_PROVIDER *)prov);
1647     return ret;
1648 }
1649 
ossl_provider_get_capabilities(const OSSL_PROVIDER * prov,const char * capability,OSSL_CALLBACK * cb,void * arg)1650 int ossl_provider_get_capabilities(const OSSL_PROVIDER *prov,
1651                                    const char *capability,
1652                                    OSSL_CALLBACK *cb,
1653                                    void *arg)
1654 {
1655     return prov->get_capabilities == NULL
1656         ? 1 : prov->get_capabilities(prov->provctx, capability, cb, arg);
1657 }
1658 
ossl_provider_query_operation(const OSSL_PROVIDER * prov,int operation_id,int * no_cache)1659 const OSSL_ALGORITHM *ossl_provider_query_operation(const OSSL_PROVIDER *prov,
1660                                                     int operation_id,
1661                                                     int *no_cache)
1662 {
1663     const OSSL_ALGORITHM *res;
1664 
1665     if (prov->query_operation == NULL)
1666         return NULL;
1667     res = prov->query_operation(prov->provctx, operation_id, no_cache);
1668 #if defined(OPENSSL_NO_CACHED_FETCH)
1669     /* Forcing the non-caching of queries */
1670     if (no_cache != NULL)
1671         *no_cache = 1;
1672 #endif
1673     return res;
1674 }
1675 
ossl_provider_unquery_operation(const OSSL_PROVIDER * prov,int operation_id,const OSSL_ALGORITHM * algs)1676 void ossl_provider_unquery_operation(const OSSL_PROVIDER *prov,
1677                                      int operation_id,
1678                                      const OSSL_ALGORITHM *algs)
1679 {
1680     if (prov->unquery_operation != NULL)
1681         prov->unquery_operation(prov->provctx, operation_id, algs);
1682 }
1683 
ossl_provider_set_operation_bit(OSSL_PROVIDER * provider,size_t bitnum)1684 int ossl_provider_set_operation_bit(OSSL_PROVIDER *provider, size_t bitnum)
1685 {
1686     size_t byte = bitnum / 8;
1687     unsigned char bit = (1 << (bitnum % 8)) & 0xFF;
1688 
1689     if (!CRYPTO_THREAD_write_lock(provider->opbits_lock))
1690         return 0;
1691     if (provider->operation_bits_sz <= byte) {
1692         unsigned char *tmp = OPENSSL_realloc(provider->operation_bits,
1693                                              byte + 1);
1694 
1695         if (tmp == NULL) {
1696             CRYPTO_THREAD_unlock(provider->opbits_lock);
1697             return 0;
1698         }
1699         provider->operation_bits = tmp;
1700         memset(provider->operation_bits + provider->operation_bits_sz,
1701                '\0', byte + 1 - provider->operation_bits_sz);
1702         provider->operation_bits_sz = byte + 1;
1703     }
1704     provider->operation_bits[byte] |= bit;
1705     CRYPTO_THREAD_unlock(provider->opbits_lock);
1706     return 1;
1707 }
1708 
ossl_provider_test_operation_bit(OSSL_PROVIDER * provider,size_t bitnum,int * result)1709 int ossl_provider_test_operation_bit(OSSL_PROVIDER *provider, size_t bitnum,
1710                                      int *result)
1711 {
1712     size_t byte = bitnum / 8;
1713     unsigned char bit = (1 << (bitnum % 8)) & 0xFF;
1714 
1715     if (!ossl_assert(result != NULL)) {
1716         ERR_raise(ERR_LIB_CRYPTO, ERR_R_PASSED_NULL_PARAMETER);
1717         return 0;
1718     }
1719 
1720     *result = 0;
1721     if (!CRYPTO_THREAD_read_lock(provider->opbits_lock))
1722         return 0;
1723     if (provider->operation_bits_sz > byte)
1724         *result = ((provider->operation_bits[byte] & bit) != 0);
1725     CRYPTO_THREAD_unlock(provider->opbits_lock);
1726     return 1;
1727 }
1728 
1729 #ifndef FIPS_MODULE
ossl_provider_get_parent(OSSL_PROVIDER * prov)1730 const OSSL_CORE_HANDLE *ossl_provider_get_parent(OSSL_PROVIDER *prov)
1731 {
1732     return prov->handle;
1733 }
1734 
ossl_provider_is_child(const OSSL_PROVIDER * prov)1735 int ossl_provider_is_child(const OSSL_PROVIDER *prov)
1736 {
1737     return prov->ischild;
1738 }
1739 
ossl_provider_set_child(OSSL_PROVIDER * prov,const OSSL_CORE_HANDLE * handle)1740 int ossl_provider_set_child(OSSL_PROVIDER *prov, const OSSL_CORE_HANDLE *handle)
1741 {
1742     prov->handle = handle;
1743     prov->ischild = 1;
1744 
1745     return 1;
1746 }
1747 
ossl_provider_default_props_update(OSSL_LIB_CTX * libctx,const char * props)1748 int ossl_provider_default_props_update(OSSL_LIB_CTX *libctx, const char *props)
1749 {
1750 #ifndef FIPS_MODULE
1751     struct provider_store_st *store = NULL;
1752     int i, max;
1753     OSSL_PROVIDER_CHILD_CB *child_cb;
1754 
1755     if ((store = get_provider_store(libctx)) == NULL)
1756         return 0;
1757 
1758     if (!CRYPTO_THREAD_read_lock(store->lock))
1759         return 0;
1760 
1761     max = sk_OSSL_PROVIDER_CHILD_CB_num(store->child_cbs);
1762     for (i = 0; i < max; i++) {
1763         child_cb = sk_OSSL_PROVIDER_CHILD_CB_value(store->child_cbs, i);
1764         child_cb->global_props_cb(props, child_cb->cbdata);
1765     }
1766 
1767     CRYPTO_THREAD_unlock(store->lock);
1768 #endif
1769     return 1;
1770 }
1771 
ossl_provider_register_child_cb(const OSSL_CORE_HANDLE * handle,int (* create_cb)(const OSSL_CORE_HANDLE * provider,void * cbdata),int (* remove_cb)(const OSSL_CORE_HANDLE * provider,void * cbdata),int (* global_props_cb)(const char * props,void * cbdata),void * cbdata)1772 static int ossl_provider_register_child_cb(const OSSL_CORE_HANDLE *handle,
1773                                            int (*create_cb)(
1774                                                const OSSL_CORE_HANDLE *provider,
1775                                                void *cbdata),
1776                                            int (*remove_cb)(
1777                                                const OSSL_CORE_HANDLE *provider,
1778                                                void *cbdata),
1779                                            int (*global_props_cb)(
1780                                                const char *props,
1781                                                void *cbdata),
1782                                            void *cbdata)
1783 {
1784     /*
1785      * This is really an OSSL_PROVIDER that we created and cast to
1786      * OSSL_CORE_HANDLE originally. Therefore it is safe to cast it back.
1787      */
1788     OSSL_PROVIDER *thisprov = (OSSL_PROVIDER *)handle;
1789     OSSL_PROVIDER *prov;
1790     OSSL_LIB_CTX *libctx = thisprov->libctx;
1791     struct provider_store_st *store = NULL;
1792     int ret = 0, i, max;
1793     OSSL_PROVIDER_CHILD_CB *child_cb;
1794     char *propsstr = NULL;
1795 
1796     if ((store = get_provider_store(libctx)) == NULL)
1797         return 0;
1798 
1799     child_cb = OPENSSL_malloc(sizeof(*child_cb));
1800     if (child_cb == NULL)
1801         return 0;
1802     child_cb->prov = thisprov;
1803     child_cb->create_cb = create_cb;
1804     child_cb->remove_cb = remove_cb;
1805     child_cb->global_props_cb = global_props_cb;
1806     child_cb->cbdata = cbdata;
1807 
1808     if (!CRYPTO_THREAD_write_lock(store->lock)) {
1809         OPENSSL_free(child_cb);
1810         return 0;
1811     }
1812     propsstr = evp_get_global_properties_str(libctx, 0);
1813 
1814     if (propsstr != NULL) {
1815         global_props_cb(propsstr, cbdata);
1816         OPENSSL_free(propsstr);
1817     }
1818     max = sk_OSSL_PROVIDER_num(store->providers);
1819     for (i = 0; i < max; i++) {
1820         int activated;
1821 
1822         prov = sk_OSSL_PROVIDER_value(store->providers, i);
1823 
1824         if (!CRYPTO_THREAD_read_lock(prov->flag_lock))
1825             break;
1826         activated = prov->flag_activated;
1827         CRYPTO_THREAD_unlock(prov->flag_lock);
1828         /*
1829          * We hold the store lock while calling the user callback. This means
1830          * that the user callback must be short and simple and not do anything
1831          * likely to cause a deadlock. We don't hold the flag_lock during this
1832          * call. In theory this means that another thread could deactivate it
1833          * while we are calling create. This is ok because the other thread
1834          * will also call remove_cb, but won't be able to do so until we release
1835          * the store lock.
1836          */
1837         if (activated && !create_cb((OSSL_CORE_HANDLE *)prov, cbdata))
1838             break;
1839     }
1840     if (i == max) {
1841         /* Success */
1842         ret = sk_OSSL_PROVIDER_CHILD_CB_push(store->child_cbs, child_cb);
1843     }
1844     if (i != max || ret <= 0) {
1845         /* Failed during creation. Remove everything we just added */
1846         for (; i >= 0; i--) {
1847             prov = sk_OSSL_PROVIDER_value(store->providers, i);
1848             remove_cb((OSSL_CORE_HANDLE *)prov, cbdata);
1849         }
1850         OPENSSL_free(child_cb);
1851         ret = 0;
1852     }
1853     CRYPTO_THREAD_unlock(store->lock);
1854 
1855     return ret;
1856 }
1857 
ossl_provider_deregister_child_cb(const OSSL_CORE_HANDLE * handle)1858 static void ossl_provider_deregister_child_cb(const OSSL_CORE_HANDLE *handle)
1859 {
1860     /*
1861      * This is really an OSSL_PROVIDER that we created and cast to
1862      * OSSL_CORE_HANDLE originally. Therefore it is safe to cast it back.
1863      */
1864     OSSL_PROVIDER *thisprov = (OSSL_PROVIDER *)handle;
1865     OSSL_LIB_CTX *libctx = thisprov->libctx;
1866     struct provider_store_st *store = NULL;
1867     int i, max;
1868     OSSL_PROVIDER_CHILD_CB *child_cb;
1869 
1870     if ((store = get_provider_store(libctx)) == NULL)
1871         return;
1872 
1873     if (!CRYPTO_THREAD_write_lock(store->lock))
1874         return;
1875     max = sk_OSSL_PROVIDER_CHILD_CB_num(store->child_cbs);
1876     for (i = 0; i < max; i++) {
1877         child_cb = sk_OSSL_PROVIDER_CHILD_CB_value(store->child_cbs, i);
1878         if (child_cb->prov == thisprov) {
1879             /* Found an entry */
1880             sk_OSSL_PROVIDER_CHILD_CB_delete(store->child_cbs, i);
1881             OPENSSL_free(child_cb);
1882             break;
1883         }
1884     }
1885     CRYPTO_THREAD_unlock(store->lock);
1886 }
1887 #endif
1888 
1889 /*-
1890  * Core functions for the provider
1891  * ===============================
1892  *
1893  * This is the set of functions that the core makes available to the provider
1894  */
1895 
1896 /*
1897  * This returns a list of Provider Object parameters with their types, for
1898  * discovery.  We do not expect that many providers will use this, but one
1899  * never knows.
1900  */
1901 static const OSSL_PARAM param_types[] = {
1902     OSSL_PARAM_DEFN(OSSL_PROV_PARAM_CORE_VERSION, OSSL_PARAM_UTF8_PTR, NULL, 0),
1903     OSSL_PARAM_DEFN(OSSL_PROV_PARAM_CORE_PROV_NAME, OSSL_PARAM_UTF8_PTR,
1904                     NULL, 0),
1905 #ifndef FIPS_MODULE
1906     OSSL_PARAM_DEFN(OSSL_PROV_PARAM_CORE_MODULE_FILENAME, OSSL_PARAM_UTF8_PTR,
1907                     NULL, 0),
1908 #endif
1909     OSSL_PARAM_END
1910 };
1911 
1912 /*
1913  * Forward declare all the functions that are provided aa dispatch.
1914  * This ensures that the compiler will complain if they aren't defined
1915  * with the correct signature.
1916  */
1917 static OSSL_FUNC_core_gettable_params_fn core_gettable_params;
1918 static OSSL_FUNC_core_get_params_fn core_get_params;
1919 static OSSL_FUNC_core_get_libctx_fn core_get_libctx;
1920 static OSSL_FUNC_core_thread_start_fn core_thread_start;
1921 #ifndef FIPS_MODULE
1922 static OSSL_FUNC_core_new_error_fn core_new_error;
1923 static OSSL_FUNC_core_set_error_debug_fn core_set_error_debug;
1924 static OSSL_FUNC_core_vset_error_fn core_vset_error;
1925 static OSSL_FUNC_core_set_error_mark_fn core_set_error_mark;
1926 static OSSL_FUNC_core_clear_last_error_mark_fn core_clear_last_error_mark;
1927 static OSSL_FUNC_core_pop_error_to_mark_fn core_pop_error_to_mark;
1928 OSSL_FUNC_BIO_new_file_fn ossl_core_bio_new_file;
1929 OSSL_FUNC_BIO_new_membuf_fn ossl_core_bio_new_mem_buf;
1930 OSSL_FUNC_BIO_read_ex_fn ossl_core_bio_read_ex;
1931 OSSL_FUNC_BIO_write_ex_fn ossl_core_bio_write_ex;
1932 OSSL_FUNC_BIO_gets_fn ossl_core_bio_gets;
1933 OSSL_FUNC_BIO_puts_fn ossl_core_bio_puts;
1934 OSSL_FUNC_BIO_up_ref_fn ossl_core_bio_up_ref;
1935 OSSL_FUNC_BIO_free_fn ossl_core_bio_free;
1936 OSSL_FUNC_BIO_vprintf_fn ossl_core_bio_vprintf;
1937 OSSL_FUNC_BIO_vsnprintf_fn BIO_vsnprintf;
1938 static OSSL_FUNC_indicator_cb_fn core_indicator_get_callback;
1939 static OSSL_FUNC_self_test_cb_fn core_self_test_get_callback;
1940 static OSSL_FUNC_get_entropy_fn rand_get_entropy;
1941 static OSSL_FUNC_get_user_entropy_fn rand_get_user_entropy;
1942 static OSSL_FUNC_cleanup_entropy_fn rand_cleanup_entropy;
1943 static OSSL_FUNC_cleanup_user_entropy_fn rand_cleanup_user_entropy;
1944 static OSSL_FUNC_get_nonce_fn rand_get_nonce;
1945 static OSSL_FUNC_get_user_nonce_fn rand_get_user_nonce;
1946 static OSSL_FUNC_cleanup_nonce_fn rand_cleanup_nonce;
1947 static OSSL_FUNC_cleanup_user_nonce_fn rand_cleanup_user_nonce;
1948 #endif
1949 OSSL_FUNC_CRYPTO_malloc_fn CRYPTO_malloc;
1950 OSSL_FUNC_CRYPTO_zalloc_fn CRYPTO_zalloc;
1951 OSSL_FUNC_CRYPTO_free_fn CRYPTO_free;
1952 OSSL_FUNC_CRYPTO_clear_free_fn CRYPTO_clear_free;
1953 OSSL_FUNC_CRYPTO_realloc_fn CRYPTO_realloc;
1954 OSSL_FUNC_CRYPTO_clear_realloc_fn CRYPTO_clear_realloc;
1955 OSSL_FUNC_CRYPTO_secure_malloc_fn CRYPTO_secure_malloc;
1956 OSSL_FUNC_CRYPTO_secure_zalloc_fn CRYPTO_secure_zalloc;
1957 OSSL_FUNC_CRYPTO_secure_free_fn CRYPTO_secure_free;
1958 OSSL_FUNC_CRYPTO_secure_clear_free_fn CRYPTO_secure_clear_free;
1959 OSSL_FUNC_CRYPTO_secure_allocated_fn CRYPTO_secure_allocated;
1960 OSSL_FUNC_OPENSSL_cleanse_fn OPENSSL_cleanse;
1961 #ifndef FIPS_MODULE
1962 OSSL_FUNC_provider_register_child_cb_fn ossl_provider_register_child_cb;
1963 OSSL_FUNC_provider_deregister_child_cb_fn ossl_provider_deregister_child_cb;
1964 static OSSL_FUNC_provider_name_fn core_provider_get0_name;
1965 static OSSL_FUNC_provider_get0_provider_ctx_fn core_provider_get0_provider_ctx;
1966 static OSSL_FUNC_provider_get0_dispatch_fn core_provider_get0_dispatch;
1967 static OSSL_FUNC_provider_up_ref_fn core_provider_up_ref_intern;
1968 static OSSL_FUNC_provider_free_fn core_provider_free_intern;
1969 static OSSL_FUNC_core_obj_add_sigid_fn core_obj_add_sigid;
1970 static OSSL_FUNC_core_obj_create_fn core_obj_create;
1971 #endif
1972 
core_gettable_params(const OSSL_CORE_HANDLE * handle)1973 static const OSSL_PARAM *core_gettable_params(const OSSL_CORE_HANDLE *handle)
1974 {
1975     return param_types;
1976 }
1977 
core_get_params(const OSSL_CORE_HANDLE * handle,OSSL_PARAM params[])1978 static int core_get_params(const OSSL_CORE_HANDLE *handle, OSSL_PARAM params[])
1979 {
1980     int i;
1981     OSSL_PARAM *p;
1982     /*
1983      * We created this object originally and we know it is actually an
1984      * OSSL_PROVIDER *, so the cast is safe
1985      */
1986     OSSL_PROVIDER *prov = (OSSL_PROVIDER *)handle;
1987 
1988     if ((p = OSSL_PARAM_locate(params, OSSL_PROV_PARAM_CORE_VERSION)) != NULL)
1989         OSSL_PARAM_set_utf8_ptr(p, OPENSSL_VERSION_STR);
1990     if ((p = OSSL_PARAM_locate(params, OSSL_PROV_PARAM_CORE_PROV_NAME)) != NULL)
1991         OSSL_PARAM_set_utf8_ptr(p, prov->name);
1992 
1993 #ifndef FIPS_MODULE
1994     if ((p = OSSL_PARAM_locate(params,
1995                                OSSL_PROV_PARAM_CORE_MODULE_FILENAME)) != NULL)
1996         OSSL_PARAM_set_utf8_ptr(p, ossl_provider_module_path(prov));
1997 #endif
1998 
1999     if (prov->parameters == NULL)
2000         return 1;
2001 
2002     for (i = 0; i < sk_INFOPAIR_num(prov->parameters); i++) {
2003         INFOPAIR *pair = sk_INFOPAIR_value(prov->parameters, i);
2004 
2005         if ((p = OSSL_PARAM_locate(params, pair->name)) != NULL)
2006             OSSL_PARAM_set_utf8_ptr(p, pair->value);
2007     }
2008     return 1;
2009 }
2010 
core_get_libctx(const OSSL_CORE_HANDLE * handle)2011 static OPENSSL_CORE_CTX *core_get_libctx(const OSSL_CORE_HANDLE *handle)
2012 {
2013     /*
2014      * We created this object originally and we know it is actually an
2015      * OSSL_PROVIDER *, so the cast is safe
2016      */
2017     OSSL_PROVIDER *prov = (OSSL_PROVIDER *)handle;
2018 
2019     /*
2020      * Using ossl_provider_libctx would be wrong as that returns
2021      * NULL for |prov| == NULL and NULL libctx has a special meaning
2022      * that does not apply here. Here |prov| == NULL can happen only in
2023      * case of a coding error.
2024      */
2025     assert(prov != NULL);
2026     return (OPENSSL_CORE_CTX *)prov->libctx;
2027 }
2028 
core_thread_start(const OSSL_CORE_HANDLE * handle,OSSL_thread_stop_handler_fn handfn,void * arg)2029 static int core_thread_start(const OSSL_CORE_HANDLE *handle,
2030                              OSSL_thread_stop_handler_fn handfn,
2031                              void *arg)
2032 {
2033     /*
2034      * We created this object originally and we know it is actually an
2035      * OSSL_PROVIDER *, so the cast is safe
2036      */
2037     OSSL_PROVIDER *prov = (OSSL_PROVIDER *)handle;
2038 
2039     return ossl_init_thread_start(prov, arg, handfn);
2040 }
2041 
2042 /*
2043  * The FIPS module inner provider doesn't implement these.  They aren't
2044  * needed there, since the FIPS module upcalls are always the outer provider
2045  * ones.
2046  */
2047 #ifndef FIPS_MODULE
2048 /*
2049  * These error functions should use |handle| to select the proper
2050  * library context to report in the correct error stack if error
2051  * stacks become tied to the library context.
2052  * We cannot currently do that since there's no support for it in the
2053  * ERR subsystem.
2054  */
core_new_error(const OSSL_CORE_HANDLE * handle)2055 static void core_new_error(const OSSL_CORE_HANDLE *handle)
2056 {
2057     ERR_new();
2058 }
2059 
core_set_error_debug(const OSSL_CORE_HANDLE * handle,const char * file,int line,const char * func)2060 static void core_set_error_debug(const OSSL_CORE_HANDLE *handle,
2061                                  const char *file, int line, const char *func)
2062 {
2063     ERR_set_debug(file, line, func);
2064 }
2065 
core_vset_error(const OSSL_CORE_HANDLE * handle,uint32_t reason,const char * fmt,va_list args)2066 static void core_vset_error(const OSSL_CORE_HANDLE *handle,
2067                             uint32_t reason, const char *fmt, va_list args)
2068 {
2069     /*
2070      * We created this object originally and we know it is actually an
2071      * OSSL_PROVIDER *, so the cast is safe
2072      */
2073     OSSL_PROVIDER *prov = (OSSL_PROVIDER *)handle;
2074 
2075     /*
2076      * If the uppermost 8 bits are non-zero, it's an OpenSSL library
2077      * error and will be treated as such.  Otherwise, it's a new style
2078      * provider error and will be treated as such.
2079      */
2080     if (ERR_GET_LIB(reason) != 0) {
2081         ERR_vset_error(ERR_GET_LIB(reason), ERR_GET_REASON(reason), fmt, args);
2082     } else {
2083         ERR_vset_error(prov->error_lib, (int)reason, fmt, args);
2084     }
2085 }
2086 
core_set_error_mark(const OSSL_CORE_HANDLE * handle)2087 static int core_set_error_mark(const OSSL_CORE_HANDLE *handle)
2088 {
2089     return ERR_set_mark();
2090 }
2091 
core_clear_last_error_mark(const OSSL_CORE_HANDLE * handle)2092 static int core_clear_last_error_mark(const OSSL_CORE_HANDLE *handle)
2093 {
2094     return ERR_clear_last_mark();
2095 }
2096 
core_pop_error_to_mark(const OSSL_CORE_HANDLE * handle)2097 static int core_pop_error_to_mark(const OSSL_CORE_HANDLE *handle)
2098 {
2099     return ERR_pop_to_mark();
2100 }
2101 
core_indicator_get_callback(OPENSSL_CORE_CTX * libctx,OSSL_INDICATOR_CALLBACK ** cb)2102 static void core_indicator_get_callback(OPENSSL_CORE_CTX *libctx,
2103                                         OSSL_INDICATOR_CALLBACK **cb)
2104 {
2105     OSSL_INDICATOR_get_callback((OSSL_LIB_CTX *)libctx, cb);
2106 }
2107 
core_self_test_get_callback(OPENSSL_CORE_CTX * libctx,OSSL_CALLBACK ** cb,void ** cbarg)2108 static void core_self_test_get_callback(OPENSSL_CORE_CTX *libctx,
2109                                         OSSL_CALLBACK **cb, void **cbarg)
2110 {
2111     OSSL_SELF_TEST_get_callback((OSSL_LIB_CTX *)libctx, cb, cbarg);
2112 }
2113 
rand_get_entropy(const OSSL_CORE_HANDLE * handle,unsigned char ** pout,int entropy,size_t min_len,size_t max_len)2114 static size_t rand_get_entropy(const OSSL_CORE_HANDLE *handle,
2115                                unsigned char **pout, int entropy,
2116                                size_t min_len, size_t max_len)
2117 {
2118     return ossl_rand_get_entropy((OSSL_LIB_CTX *)core_get_libctx(handle),
2119                                  pout, entropy, min_len, max_len);
2120 }
2121 
rand_get_user_entropy(const OSSL_CORE_HANDLE * handle,unsigned char ** pout,int entropy,size_t min_len,size_t max_len)2122 static size_t rand_get_user_entropy(const OSSL_CORE_HANDLE *handle,
2123                                     unsigned char **pout, int entropy,
2124                                     size_t min_len, size_t max_len)
2125 {
2126     return ossl_rand_get_user_entropy((OSSL_LIB_CTX *)core_get_libctx(handle),
2127                                       pout, entropy, min_len, max_len);
2128 }
2129 
rand_cleanup_entropy(const OSSL_CORE_HANDLE * handle,unsigned char * buf,size_t len)2130 static void rand_cleanup_entropy(const OSSL_CORE_HANDLE *handle,
2131                                  unsigned char *buf, size_t len)
2132 {
2133     ossl_rand_cleanup_entropy((OSSL_LIB_CTX *)core_get_libctx(handle),
2134                               buf, len);
2135 }
2136 
rand_cleanup_user_entropy(const OSSL_CORE_HANDLE * handle,unsigned char * buf,size_t len)2137 static void rand_cleanup_user_entropy(const OSSL_CORE_HANDLE *handle,
2138                                       unsigned char *buf, size_t len)
2139 {
2140     ossl_rand_cleanup_user_entropy((OSSL_LIB_CTX *)core_get_libctx(handle),
2141                                    buf, len);
2142 }
2143 
rand_get_nonce(const OSSL_CORE_HANDLE * handle,unsigned char ** pout,size_t min_len,size_t max_len,const void * salt,size_t salt_len)2144 static size_t rand_get_nonce(const OSSL_CORE_HANDLE *handle,
2145                              unsigned char **pout,
2146                              size_t min_len, size_t max_len,
2147                              const void *salt, size_t salt_len)
2148 {
2149     return ossl_rand_get_nonce((OSSL_LIB_CTX *)core_get_libctx(handle),
2150                                pout, min_len, max_len, salt, salt_len);
2151 }
2152 
rand_get_user_nonce(const OSSL_CORE_HANDLE * handle,unsigned char ** pout,size_t min_len,size_t max_len,const void * salt,size_t salt_len)2153 static size_t rand_get_user_nonce(const OSSL_CORE_HANDLE *handle,
2154                                   unsigned char **pout,
2155                                   size_t min_len, size_t max_len,
2156                                   const void *salt, size_t salt_len)
2157 {
2158     return ossl_rand_get_user_nonce((OSSL_LIB_CTX *)core_get_libctx(handle),
2159                                     pout, min_len, max_len, salt, salt_len);
2160 }
2161 
rand_cleanup_nonce(const OSSL_CORE_HANDLE * handle,unsigned char * buf,size_t len)2162 static void rand_cleanup_nonce(const OSSL_CORE_HANDLE *handle,
2163                                unsigned char *buf, size_t len)
2164 {
2165     ossl_rand_cleanup_nonce((OSSL_LIB_CTX *)core_get_libctx(handle),
2166                             buf, len);
2167 }
2168 
rand_cleanup_user_nonce(const OSSL_CORE_HANDLE * handle,unsigned char * buf,size_t len)2169 static void rand_cleanup_user_nonce(const OSSL_CORE_HANDLE *handle,
2170                                unsigned char *buf, size_t len)
2171 {
2172     ossl_rand_cleanup_user_nonce((OSSL_LIB_CTX *)core_get_libctx(handle),
2173                                  buf, len);
2174 }
2175 
core_provider_get0_name(const OSSL_CORE_HANDLE * prov)2176 static const char *core_provider_get0_name(const OSSL_CORE_HANDLE *prov)
2177 {
2178     return OSSL_PROVIDER_get0_name((const OSSL_PROVIDER *)prov);
2179 }
2180 
core_provider_get0_provider_ctx(const OSSL_CORE_HANDLE * prov)2181 static void *core_provider_get0_provider_ctx(const OSSL_CORE_HANDLE *prov)
2182 {
2183     return OSSL_PROVIDER_get0_provider_ctx((const OSSL_PROVIDER *)prov);
2184 }
2185 
2186 static const OSSL_DISPATCH *
core_provider_get0_dispatch(const OSSL_CORE_HANDLE * prov)2187 core_provider_get0_dispatch(const OSSL_CORE_HANDLE *prov)
2188 {
2189     return OSSL_PROVIDER_get0_dispatch((const OSSL_PROVIDER *)prov);
2190 }
2191 
core_provider_up_ref_intern(const OSSL_CORE_HANDLE * prov,int activate)2192 static int core_provider_up_ref_intern(const OSSL_CORE_HANDLE *prov,
2193                                        int activate)
2194 {
2195     return provider_up_ref_intern((OSSL_PROVIDER *)prov, activate);
2196 }
2197 
core_provider_free_intern(const OSSL_CORE_HANDLE * prov,int deactivate)2198 static int core_provider_free_intern(const OSSL_CORE_HANDLE *prov,
2199                                      int deactivate)
2200 {
2201     return provider_free_intern((OSSL_PROVIDER *)prov, deactivate);
2202 }
2203 
core_obj_add_sigid(const OSSL_CORE_HANDLE * prov,const char * sign_name,const char * digest_name,const char * pkey_name)2204 static int core_obj_add_sigid(const OSSL_CORE_HANDLE *prov,
2205                               const char *sign_name, const char *digest_name,
2206                               const char *pkey_name)
2207 {
2208     int sign_nid = OBJ_txt2nid(sign_name);
2209     int digest_nid = NID_undef;
2210     int pkey_nid = OBJ_txt2nid(pkey_name);
2211 
2212     if (digest_name != NULL && digest_name[0] != '\0'
2213         && (digest_nid = OBJ_txt2nid(digest_name)) == NID_undef)
2214             return 0;
2215 
2216     if (sign_nid == NID_undef)
2217         return 0;
2218 
2219     /*
2220      * Check if it already exists. This is a success if so (even if we don't
2221      * have nids for the digest/pkey)
2222      */
2223     if (OBJ_find_sigid_algs(sign_nid, NULL, NULL))
2224         return 1;
2225 
2226     if (pkey_nid == NID_undef)
2227         return 0;
2228 
2229     return OBJ_add_sigid(sign_nid, digest_nid, pkey_nid);
2230 }
2231 
core_obj_create(const OSSL_CORE_HANDLE * prov,const char * oid,const char * sn,const char * ln)2232 static int core_obj_create(const OSSL_CORE_HANDLE *prov, const char *oid,
2233                            const char *sn, const char *ln)
2234 {
2235     /* Check if it already exists and create it if not */
2236     return OBJ_txt2nid(oid) != NID_undef
2237            || OBJ_create(oid, sn, ln) != NID_undef;
2238 }
2239 #endif /* FIPS_MODULE */
2240 
2241 /*
2242  * Functions provided by the core.
2243  */
2244 static const OSSL_DISPATCH core_dispatch_[] = {
2245     { OSSL_FUNC_CORE_GETTABLE_PARAMS, (void (*)(void))core_gettable_params },
2246     { OSSL_FUNC_CORE_GET_PARAMS, (void (*)(void))core_get_params },
2247     { OSSL_FUNC_CORE_GET_LIBCTX, (void (*)(void))core_get_libctx },
2248     { OSSL_FUNC_CORE_THREAD_START, (void (*)(void))core_thread_start },
2249 #ifndef FIPS_MODULE
2250     { OSSL_FUNC_CORE_NEW_ERROR, (void (*)(void))core_new_error },
2251     { OSSL_FUNC_CORE_SET_ERROR_DEBUG, (void (*)(void))core_set_error_debug },
2252     { OSSL_FUNC_CORE_VSET_ERROR, (void (*)(void))core_vset_error },
2253     { OSSL_FUNC_CORE_SET_ERROR_MARK, (void (*)(void))core_set_error_mark },
2254     { OSSL_FUNC_CORE_CLEAR_LAST_ERROR_MARK,
2255       (void (*)(void))core_clear_last_error_mark },
2256     { OSSL_FUNC_CORE_POP_ERROR_TO_MARK, (void (*)(void))core_pop_error_to_mark },
2257     { OSSL_FUNC_BIO_NEW_FILE, (void (*)(void))ossl_core_bio_new_file },
2258     { OSSL_FUNC_BIO_NEW_MEMBUF, (void (*)(void))ossl_core_bio_new_mem_buf },
2259     { OSSL_FUNC_BIO_READ_EX, (void (*)(void))ossl_core_bio_read_ex },
2260     { OSSL_FUNC_BIO_WRITE_EX, (void (*)(void))ossl_core_bio_write_ex },
2261     { OSSL_FUNC_BIO_GETS, (void (*)(void))ossl_core_bio_gets },
2262     { OSSL_FUNC_BIO_PUTS, (void (*)(void))ossl_core_bio_puts },
2263     { OSSL_FUNC_BIO_CTRL, (void (*)(void))ossl_core_bio_ctrl },
2264     { OSSL_FUNC_BIO_UP_REF, (void (*)(void))ossl_core_bio_up_ref },
2265     { OSSL_FUNC_BIO_FREE, (void (*)(void))ossl_core_bio_free },
2266     { OSSL_FUNC_BIO_VPRINTF, (void (*)(void))ossl_core_bio_vprintf },
2267     { OSSL_FUNC_BIO_VSNPRINTF, (void (*)(void))BIO_vsnprintf },
2268     { OSSL_FUNC_SELF_TEST_CB, (void (*)(void))core_self_test_get_callback },
2269     { OSSL_FUNC_INDICATOR_CB, (void (*)(void))core_indicator_get_callback },
2270     { OSSL_FUNC_GET_ENTROPY, (void (*)(void))rand_get_entropy },
2271     { OSSL_FUNC_GET_USER_ENTROPY, (void (*)(void))rand_get_user_entropy },
2272     { OSSL_FUNC_CLEANUP_ENTROPY, (void (*)(void))rand_cleanup_entropy },
2273     { OSSL_FUNC_CLEANUP_USER_ENTROPY, (void (*)(void))rand_cleanup_user_entropy },
2274     { OSSL_FUNC_GET_NONCE, (void (*)(void))rand_get_nonce },
2275     { OSSL_FUNC_GET_USER_NONCE, (void (*)(void))rand_get_user_nonce },
2276     { OSSL_FUNC_CLEANUP_NONCE, (void (*)(void))rand_cleanup_nonce },
2277     { OSSL_FUNC_CLEANUP_USER_NONCE, (void (*)(void))rand_cleanup_user_nonce },
2278 #endif
2279     { OSSL_FUNC_CRYPTO_MALLOC, (void (*)(void))CRYPTO_malloc },
2280     { OSSL_FUNC_CRYPTO_ZALLOC, (void (*)(void))CRYPTO_zalloc },
2281     { OSSL_FUNC_CRYPTO_FREE, (void (*)(void))CRYPTO_free },
2282     { OSSL_FUNC_CRYPTO_CLEAR_FREE, (void (*)(void))CRYPTO_clear_free },
2283     { OSSL_FUNC_CRYPTO_REALLOC, (void (*)(void))CRYPTO_realloc },
2284     { OSSL_FUNC_CRYPTO_CLEAR_REALLOC, (void (*)(void))CRYPTO_clear_realloc },
2285     { OSSL_FUNC_CRYPTO_SECURE_MALLOC, (void (*)(void))CRYPTO_secure_malloc },
2286     { OSSL_FUNC_CRYPTO_SECURE_ZALLOC, (void (*)(void))CRYPTO_secure_zalloc },
2287     { OSSL_FUNC_CRYPTO_SECURE_FREE, (void (*)(void))CRYPTO_secure_free },
2288     { OSSL_FUNC_CRYPTO_SECURE_CLEAR_FREE,
2289         (void (*)(void))CRYPTO_secure_clear_free },
2290     { OSSL_FUNC_CRYPTO_SECURE_ALLOCATED,
2291         (void (*)(void))CRYPTO_secure_allocated },
2292     { OSSL_FUNC_OPENSSL_CLEANSE, (void (*)(void))OPENSSL_cleanse },
2293 #ifndef FIPS_MODULE
2294     { OSSL_FUNC_PROVIDER_REGISTER_CHILD_CB,
2295         (void (*)(void))ossl_provider_register_child_cb },
2296     { OSSL_FUNC_PROVIDER_DEREGISTER_CHILD_CB,
2297         (void (*)(void))ossl_provider_deregister_child_cb },
2298     { OSSL_FUNC_PROVIDER_NAME,
2299         (void (*)(void))core_provider_get0_name },
2300     { OSSL_FUNC_PROVIDER_GET0_PROVIDER_CTX,
2301         (void (*)(void))core_provider_get0_provider_ctx },
2302     { OSSL_FUNC_PROVIDER_GET0_DISPATCH,
2303         (void (*)(void))core_provider_get0_dispatch },
2304     { OSSL_FUNC_PROVIDER_UP_REF,
2305         (void (*)(void))core_provider_up_ref_intern },
2306     { OSSL_FUNC_PROVIDER_FREE,
2307         (void (*)(void))core_provider_free_intern },
2308     { OSSL_FUNC_CORE_OBJ_ADD_SIGID, (void (*)(void))core_obj_add_sigid },
2309     { OSSL_FUNC_CORE_OBJ_CREATE, (void (*)(void))core_obj_create },
2310 #endif
2311     OSSL_DISPATCH_END
2312 };
2313 static const OSSL_DISPATCH *core_dispatch = core_dispatch_;
2314