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