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