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