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