Only load the config file into the default libctx if necessary
[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 "provider_local.h"
26 #ifndef FIPS_MODULE
27 # include <openssl/self_test.h>
28 #endif
29
30 static OSSL_PROVIDER *provider_new(const char *name,
31                                    OSSL_provider_init_fn *init_function);
32
33 /*-
34  * Provider Object structure
35  * =========================
36  */
37
38 typedef struct {
39     char *name;
40     char *value;
41 } INFOPAIR;
42 DEFINE_STACK_OF(INFOPAIR)
43
44 struct provider_store_st;        /* Forward declaration */
45
46 struct ossl_provider_st {
47     /* Flag bits */
48     unsigned int flag_initialized:1;
49     unsigned int flag_activated:1;
50     unsigned int flag_fallback:1; /* Can be used as fallback */
51
52     /* Getting and setting the flags require synchronization */
53     CRYPTO_RWLOCK *flag_lock;
54
55     /* OpenSSL library side data */
56     CRYPTO_REF_COUNT refcnt;
57     CRYPTO_RWLOCK *refcnt_lock;  /* For the ref counter */
58     int activatecnt;
59     char *name;
60     char *path;
61     DSO *module;
62     OSSL_provider_init_fn *init_function;
63     STACK_OF(INFOPAIR) *parameters;
64     OSSL_LIB_CTX *libctx; /* The library context this instance is in */
65     struct provider_store_st *store; /* The store this instance belongs to */
66 #ifndef FIPS_MODULE
67     /*
68      * In the FIPS module inner provider, this isn't needed, since the
69      * error upcalls are always direct calls to the outer provider.
70      */
71     int error_lib;     /* ERR library number, one for each provider */
72 # ifndef OPENSSL_NO_ERR
73     ERR_STRING_DATA *error_strings; /* Copy of what the provider gives us */
74 # endif
75 #endif
76
77     /* Provider side functions */
78     OSSL_FUNC_provider_teardown_fn *teardown;
79     OSSL_FUNC_provider_gettable_params_fn *gettable_params;
80     OSSL_FUNC_provider_get_params_fn *get_params;
81     OSSL_FUNC_provider_get_capabilities_fn *get_capabilities;
82     OSSL_FUNC_provider_self_test_fn *self_test;
83     OSSL_FUNC_provider_query_operation_fn *query_operation;
84     OSSL_FUNC_provider_unquery_operation_fn *unquery_operation;
85
86     /*
87      * Cache of bit to indicate of query_operation() has been called on
88      * a specific operation or not.
89      */
90     unsigned char *operation_bits;
91     size_t operation_bits_sz;
92     CRYPTO_RWLOCK *opbits_lock;
93
94     /* Provider side data */
95     void *provctx;
96 };
97 DEFINE_STACK_OF(OSSL_PROVIDER)
98
99 static int ossl_provider_cmp(const OSSL_PROVIDER * const *a,
100                              const OSSL_PROVIDER * const *b)
101 {
102     return strcmp((*a)->name, (*b)->name);
103 }
104
105 /*-
106  * Provider Object store
107  * =====================
108  *
109  * The Provider Object store is a library context object, and therefore needs
110  * an index.
111  */
112
113 struct provider_store_st {
114     STACK_OF(OSSL_PROVIDER) *providers;
115     CRYPTO_RWLOCK *default_path_lock;
116     CRYPTO_RWLOCK *lock;
117     char *default_path;
118     unsigned int use_fallbacks:1;
119     unsigned int freeing:1;
120 };
121
122 /*
123  * provider_deactivate_free() is a wrapper around ossl_provider_deactivate()
124  * and ossl_provider_free(), called as needed.
125  * Since this is only called when the provider store is being emptied, we
126  * don't need to care about any lock.
127  */
128 static void provider_deactivate_free(OSSL_PROVIDER *prov)
129 {
130     if (prov->flag_activated)
131         ossl_provider_deactivate(prov);
132     ossl_provider_free(prov);
133 }
134
135 static void provider_store_free(void *vstore)
136 {
137     struct provider_store_st *store = vstore;
138
139     if (store == NULL)
140         return;
141     store->freeing = 1;
142     OPENSSL_free(store->default_path);
143     sk_OSSL_PROVIDER_pop_free(store->providers, provider_deactivate_free);
144     CRYPTO_THREAD_lock_free(store->default_path_lock);
145     CRYPTO_THREAD_lock_free(store->lock);
146     OPENSSL_free(store);
147 }
148
149 static void *provider_store_new(OSSL_LIB_CTX *ctx)
150 {
151     struct provider_store_st *store = OPENSSL_zalloc(sizeof(*store));
152     const struct predefined_providers_st *p = NULL;
153
154     if (store == NULL
155         || (store->providers = sk_OSSL_PROVIDER_new(ossl_provider_cmp)) == NULL
156         || (store->default_path_lock = CRYPTO_THREAD_lock_new()) == NULL
157         || (store->lock = CRYPTO_THREAD_lock_new()) == NULL) {
158         provider_store_free(store);
159         return NULL;
160     }
161     store->use_fallbacks = 1;
162
163     for (p = ossl_predefined_providers; p->name != NULL; p++) {
164         OSSL_PROVIDER *prov = NULL;
165
166         /*
167          * We use the internal constructor directly here,
168          * otherwise we get a call loop
169          */
170         prov = provider_new(p->name, p->init);
171
172         if (prov == NULL
173             || sk_OSSL_PROVIDER_push(store->providers, prov) == 0) {
174             ossl_provider_free(prov);
175             provider_store_free(store);
176             ERR_raise(ERR_LIB_CRYPTO, ERR_R_INTERNAL_ERROR);
177             return NULL;
178         }
179         prov->libctx = ctx;
180         prov->store = store;
181 #ifndef FIPS_MODULE
182         prov->error_lib = ERR_get_next_error_library();
183 #endif
184         if(p->is_fallback)
185             ossl_provider_set_fallback(prov);
186     }
187
188     return store;
189 }
190
191 static const OSSL_LIB_CTX_METHOD provider_store_method = {
192     provider_store_new,
193     provider_store_free,
194 };
195
196 static struct provider_store_st *get_provider_store(OSSL_LIB_CTX *libctx)
197 {
198     struct provider_store_st *store = NULL;
199
200     store = ossl_lib_ctx_get_data(libctx, OSSL_LIB_CTX_PROVIDER_STORE_INDEX,
201                                   &provider_store_method);
202     if (store == NULL)
203         ERR_raise(ERR_LIB_CRYPTO, ERR_R_INTERNAL_ERROR);
204     return store;
205 }
206
207 int ossl_provider_disable_fallback_loading(OSSL_LIB_CTX *libctx)
208 {
209     struct provider_store_st *store;
210
211     if ((store = get_provider_store(libctx)) != NULL) {
212         if (!CRYPTO_THREAD_write_lock(store->lock))
213             return 0;
214         store->use_fallbacks = 0;
215         CRYPTO_THREAD_unlock(store->lock);
216         return 1;
217     }
218     return 0;
219 }
220
221 OSSL_PROVIDER *ossl_provider_find(OSSL_LIB_CTX *libctx, const char *name,
222                                   int noconfig)
223 {
224     struct provider_store_st *store = NULL;
225     OSSL_PROVIDER *prov = NULL;
226
227     if ((store = get_provider_store(libctx)) != NULL) {
228         OSSL_PROVIDER tmpl = { 0, };
229         int i;
230
231 #ifndef FIPS_MODULE
232         /*
233          * Make sure any providers are loaded from config before we try to find
234          * them.
235          */
236         if (!noconfig && ossl_lib_ctx_is_default(libctx))
237             OPENSSL_init_crypto(OPENSSL_INIT_LOAD_CONFIG, NULL);
238 #endif
239
240         tmpl.name = (char *)name;
241         if (!CRYPTO_THREAD_read_lock(store->lock))
242             return NULL;
243         if ((i = sk_OSSL_PROVIDER_find(store->providers, &tmpl)) == -1
244             || (prov = sk_OSSL_PROVIDER_value(store->providers, i)) == NULL
245             || !ossl_provider_up_ref(prov))
246             prov = NULL;
247         CRYPTO_THREAD_unlock(store->lock);
248     }
249
250     return prov;
251 }
252
253 /*-
254  * Provider Object methods
255  * =======================
256  */
257
258 static OSSL_PROVIDER *provider_new(const char *name,
259                                    OSSL_provider_init_fn *init_function)
260 {
261     OSSL_PROVIDER *prov = NULL;
262
263     if ((prov = OPENSSL_zalloc(sizeof(*prov))) == NULL
264 #ifndef HAVE_ATOMICS
265         || (prov->refcnt_lock = CRYPTO_THREAD_lock_new()) == NULL
266 #endif
267         || !ossl_provider_up_ref(prov) /* +1 One reference to be returned */
268         || (prov->opbits_lock = CRYPTO_THREAD_lock_new()) == NULL
269         || (prov->flag_lock = CRYPTO_THREAD_lock_new()) == NULL
270         || (prov->name = OPENSSL_strdup(name)) == NULL) {
271         ossl_provider_free(prov);
272         ERR_raise(ERR_LIB_CRYPTO, ERR_R_MALLOC_FAILURE);
273         return NULL;
274     }
275
276     prov->init_function = init_function;
277     return prov;
278 }
279
280 int ossl_provider_up_ref(OSSL_PROVIDER *prov)
281 {
282     int ref = 0;
283
284     if (CRYPTO_UP_REF(&prov->refcnt, &ref, prov->refcnt_lock) <= 0)
285         return 0;
286     return ref;
287 }
288
289 OSSL_PROVIDER *ossl_provider_new(OSSL_LIB_CTX *libctx, const char *name,
290                                  OSSL_provider_init_fn *init_function,
291                                  int noconfig)
292 {
293     struct provider_store_st *store = NULL;
294     OSSL_PROVIDER *prov = NULL;
295
296     if ((store = get_provider_store(libctx)) == NULL)
297         return NULL;
298
299     if ((prov = ossl_provider_find(libctx, name,
300                                    noconfig)) != NULL) { /* refcount +1 */
301         ossl_provider_free(prov); /* refcount -1 */
302         ERR_raise_data(ERR_LIB_CRYPTO, CRYPTO_R_PROVIDER_ALREADY_EXISTS,
303                        "name=%s", name);
304         return NULL;
305     }
306
307     /* provider_new() generates an error, so no need here */
308     if ((prov = provider_new(name, init_function)) == NULL)
309         return NULL;
310
311     if (!CRYPTO_THREAD_write_lock(store->lock))
312         return NULL;
313     if (!ossl_provider_up_ref(prov)) { /* +1 One reference for the store */
314         ossl_provider_free(prov); /* -1 Reference that was to be returned */
315         prov = NULL;
316     } else if (sk_OSSL_PROVIDER_push(store->providers, prov) == 0) {
317         ossl_provider_free(prov); /* -1 Store reference */
318         ossl_provider_free(prov); /* -1 Reference that was to be returned */
319         prov = NULL;
320     } else {
321         prov->libctx = libctx;
322         prov->store = store;
323 #ifndef FIPS_MODULE
324         prov->error_lib = ERR_get_next_error_library();
325 #endif
326     }
327     CRYPTO_THREAD_unlock(store->lock);
328
329     if (prov == NULL)
330         ERR_raise(ERR_LIB_CRYPTO, ERR_R_MALLOC_FAILURE);
331
332     /*
333      * At this point, the provider is only partially "loaded".  To be
334      * fully "loaded", ossl_provider_activate() must also be called.
335      */
336
337     return prov;
338 }
339
340 static void free_infopair(INFOPAIR *pair)
341 {
342     OPENSSL_free(pair->name);
343     OPENSSL_free(pair->value);
344     OPENSSL_free(pair);
345 }
346
347 void ossl_provider_free(OSSL_PROVIDER *prov)
348 {
349     if (prov != NULL) {
350         int ref = 0;
351
352         CRYPTO_DOWN_REF(&prov->refcnt, &ref, prov->refcnt_lock);
353
354         /*
355          * When the refcount drops to zero, we clean up the provider.
356          * Note that this also does teardown, which may seem late,
357          * considering that init happens on first activation.  However,
358          * there may be other structures hanging on to the provider after
359          * the last deactivation and may therefore need full access to the
360          * provider's services.  Therefore, we deinit late.
361          */
362         if (ref == 0) {
363             if (prov->flag_initialized) {
364                 if (prov->teardown != NULL)
365                     prov->teardown(prov->provctx);
366 #ifndef OPENSSL_NO_ERR
367 # ifndef FIPS_MODULE
368                 if (prov->error_strings != NULL) {
369                     ERR_unload_strings(prov->error_lib, prov->error_strings);
370                     OPENSSL_free(prov->error_strings);
371                     prov->error_strings = NULL;
372                 }
373 # endif
374 #endif
375                 OPENSSL_free(prov->operation_bits);
376                 prov->operation_bits = NULL;
377                 prov->operation_bits_sz = 0;
378                 prov->flag_initialized = 0;
379             }
380
381 #ifndef FIPS_MODULE
382             /*
383              * We deregister thread handling whether or not the provider was
384              * initialized. If init was attempted but was not successful then
385              * the provider may still have registered a thread handler.
386              */
387             ossl_init_thread_deregister(prov);
388             DSO_free(prov->module);
389 #endif
390             OPENSSL_free(prov->name);
391             OPENSSL_free(prov->path);
392             sk_INFOPAIR_pop_free(prov->parameters, free_infopair);
393             CRYPTO_THREAD_lock_free(prov->opbits_lock);
394             CRYPTO_THREAD_lock_free(prov->flag_lock);
395 #ifndef HAVE_ATOMICS
396             CRYPTO_THREAD_lock_free(prov->refcnt_lock);
397 #endif
398             OPENSSL_free(prov);
399         }
400     }
401 }
402
403 /* Setters */
404 int ossl_provider_set_module_path(OSSL_PROVIDER *prov, const char *module_path)
405 {
406     OPENSSL_free(prov->path);
407     if (module_path == NULL)
408         return 1;
409     if ((prov->path = OPENSSL_strdup(module_path)) != NULL)
410         return 1;
411     ERR_raise(ERR_LIB_CRYPTO, ERR_R_MALLOC_FAILURE);
412     return 0;
413 }
414
415 int ossl_provider_add_parameter(OSSL_PROVIDER *prov,
416                                 const char *name, const char *value)
417 {
418     INFOPAIR *pair = NULL;
419
420     if ((pair = OPENSSL_zalloc(sizeof(*pair))) != NULL
421         && (prov->parameters != NULL
422             || (prov->parameters = sk_INFOPAIR_new_null()) != NULL)
423         && (pair->name = OPENSSL_strdup(name)) != NULL
424         && (pair->value = OPENSSL_strdup(value)) != NULL
425         && sk_INFOPAIR_push(prov->parameters, pair) > 0)
426         return 1;
427
428     if (pair != NULL) {
429         OPENSSL_free(pair->name);
430         OPENSSL_free(pair->value);
431         OPENSSL_free(pair);
432     }
433     ERR_raise(ERR_LIB_CRYPTO, ERR_R_MALLOC_FAILURE);
434     return 0;
435 }
436
437 /*
438  * Provider activation.
439  *
440  * What "activation" means depends on the provider form; for built in
441  * providers (in the library or the application alike), the provider
442  * can already be considered to be loaded, all that's needed is to
443  * initialize it.  However, for dynamically loadable provider modules,
444  * we must first load that module.
445  *
446  * Built in modules are distinguished from dynamically loaded modules
447  * with an already assigned init function.
448  */
449 static const OSSL_DISPATCH *core_dispatch; /* Define further down */
450
451 int OSSL_PROVIDER_set_default_search_path(OSSL_LIB_CTX *libctx,
452                                           const char *path)
453 {
454     struct provider_store_st *store;
455     char *p = NULL;
456
457     if (path != NULL) {
458         p = OPENSSL_strdup(path);
459         if (p == NULL) {
460             ERR_raise(ERR_LIB_CRYPTO, ERR_R_MALLOC_FAILURE);
461             return 0;
462         }
463     }
464     if ((store = get_provider_store(libctx)) != NULL
465             && CRYPTO_THREAD_write_lock(store->default_path_lock)) {
466         OPENSSL_free(store->default_path);
467         store->default_path = p;
468         CRYPTO_THREAD_unlock(store->default_path_lock);
469         return 1;
470     }
471     OPENSSL_free(p);
472     return 0;
473 }
474
475 /*
476  * Internal version that doesn't affect the store flags, and thereby avoid
477  * locking.  Direct callers must remember to set the store flags when
478  * appropriate.
479  */
480 static int provider_init(OSSL_PROVIDER *prov, int flag_lock)
481 {
482     const OSSL_DISPATCH *provider_dispatch = NULL;
483     void *tmp_provctx = NULL;    /* safety measure */
484 #ifndef OPENSSL_NO_ERR
485 # ifndef FIPS_MODULE
486     OSSL_FUNC_provider_get_reason_strings_fn *p_get_reason_strings = NULL;
487 # endif
488 #endif
489     int ok = 0;
490
491     /*
492      * The flag lock is used to lock init, not only because the flag is
493      * checked here and set at the end, but also because this function
494      * modifies a number of things in the provider structure that this
495      * function needs to perform under lock anyway.
496      */
497     if (flag_lock && !CRYPTO_THREAD_write_lock(prov->flag_lock))
498         goto end;
499     if (prov->flag_initialized) {
500         ok = 1;
501         goto end;
502     }
503
504     /*
505      * If the init function isn't set, it indicates that this provider is
506      * a loadable module.
507      */
508     if (prov->init_function == NULL) {
509 #ifdef FIPS_MODULE
510         goto end;
511 #else
512         if (prov->module == NULL) {
513             char *allocated_path = NULL;
514             const char *module_path = NULL;
515             char *merged_path = NULL;
516             const char *load_dir = NULL;
517             char *allocated_load_dir = NULL;
518             struct provider_store_st *store;
519
520             if ((prov->module = DSO_new()) == NULL) {
521                 /* DSO_new() generates an error already */
522                 goto end;
523             }
524
525             if ((store = get_provider_store(prov->libctx)) == NULL
526                     || !CRYPTO_THREAD_read_lock(store->default_path_lock))
527                 goto end;
528
529             if (store->default_path != NULL) {
530                 allocated_load_dir = OPENSSL_strdup(store->default_path);
531                 CRYPTO_THREAD_unlock(store->default_path_lock);
532                 if (allocated_load_dir == NULL) {
533                     ERR_raise(ERR_LIB_CRYPTO, ERR_R_MALLOC_FAILURE);
534                     goto end;
535                 }
536                 load_dir = allocated_load_dir;
537             } else {
538                 CRYPTO_THREAD_unlock(store->default_path_lock);
539             }
540
541             if (load_dir == NULL) {
542                 load_dir = ossl_safe_getenv("OPENSSL_MODULES");
543                 if (load_dir == NULL)
544                     load_dir = MODULESDIR;
545             }
546
547             DSO_ctrl(prov->module, DSO_CTRL_SET_FLAGS,
548                      DSO_FLAG_NAME_TRANSLATION_EXT_ONLY, NULL);
549
550             module_path = prov->path;
551             if (module_path == NULL)
552                 module_path = allocated_path =
553                     DSO_convert_filename(prov->module, prov->name);
554             if (module_path != NULL)
555                 merged_path = DSO_merge(prov->module, module_path, load_dir);
556
557             if (merged_path == NULL
558                 || (DSO_load(prov->module, merged_path, NULL, 0)) == NULL) {
559                 DSO_free(prov->module);
560                 prov->module = NULL;
561             }
562
563             OPENSSL_free(merged_path);
564             OPENSSL_free(allocated_path);
565             OPENSSL_free(allocated_load_dir);
566         }
567
568         if (prov->module != NULL)
569             prov->init_function = (OSSL_provider_init_fn *)
570                 DSO_bind_func(prov->module, "OSSL_provider_init");
571 #endif
572     }
573
574     /* Call the initialise function for the provider. */
575     if (prov->init_function == NULL
576         || !prov->init_function((OSSL_CORE_HANDLE *)prov, core_dispatch,
577                                 &provider_dispatch, &tmp_provctx)) {
578         ERR_raise_data(ERR_LIB_CRYPTO, ERR_R_INIT_FAIL,
579                        "name=%s", prov->name);
580         goto end;
581     }
582     prov->provctx = tmp_provctx;
583
584     for (; provider_dispatch->function_id != 0; provider_dispatch++) {
585         switch (provider_dispatch->function_id) {
586         case OSSL_FUNC_PROVIDER_TEARDOWN:
587             prov->teardown =
588                 OSSL_FUNC_provider_teardown(provider_dispatch);
589             break;
590         case OSSL_FUNC_PROVIDER_GETTABLE_PARAMS:
591             prov->gettable_params =
592                 OSSL_FUNC_provider_gettable_params(provider_dispatch);
593             break;
594         case OSSL_FUNC_PROVIDER_GET_PARAMS:
595             prov->get_params =
596                 OSSL_FUNC_provider_get_params(provider_dispatch);
597             break;
598         case OSSL_FUNC_PROVIDER_SELF_TEST:
599             prov->self_test =
600                 OSSL_FUNC_provider_self_test(provider_dispatch);
601             break;
602         case OSSL_FUNC_PROVIDER_GET_CAPABILITIES:
603             prov->get_capabilities =
604                 OSSL_FUNC_provider_get_capabilities(provider_dispatch);
605             break;
606         case OSSL_FUNC_PROVIDER_QUERY_OPERATION:
607             prov->query_operation =
608                 OSSL_FUNC_provider_query_operation(provider_dispatch);
609             break;
610         case OSSL_FUNC_PROVIDER_UNQUERY_OPERATION:
611             prov->unquery_operation =
612                 OSSL_FUNC_provider_unquery_operation(provider_dispatch);
613             break;
614 #ifndef OPENSSL_NO_ERR
615 # ifndef FIPS_MODULE
616         case OSSL_FUNC_PROVIDER_GET_REASON_STRINGS:
617             p_get_reason_strings =
618                 OSSL_FUNC_provider_get_reason_strings(provider_dispatch);
619             break;
620 # endif
621 #endif
622         }
623     }
624
625 #ifndef OPENSSL_NO_ERR
626 # ifndef FIPS_MODULE
627     if (p_get_reason_strings != NULL) {
628         const OSSL_ITEM *reasonstrings = p_get_reason_strings(prov->provctx);
629         size_t cnt, cnt2;
630
631         /*
632          * ERR_load_strings() handles ERR_STRING_DATA rather than OSSL_ITEM,
633          * although they are essentially the same type.
634          * Furthermore, ERR_load_strings() patches the array's error number
635          * with the error library number, so we need to make a copy of that
636          * array either way.
637          */
638         cnt = 0;
639         while (reasonstrings[cnt].id != 0) {
640             if (ERR_GET_LIB(reasonstrings[cnt].id) != 0)
641                 goto end;
642             cnt++;
643         }
644         cnt++;                   /* One for the terminating item */
645
646         /* Allocate one extra item for the "library" name */
647         prov->error_strings =
648             OPENSSL_zalloc(sizeof(ERR_STRING_DATA) * (cnt + 1));
649         if (prov->error_strings == NULL)
650             goto end;
651
652         /*
653          * Set the "library" name.
654          */
655         prov->error_strings[0].error = ERR_PACK(prov->error_lib, 0, 0);
656         prov->error_strings[0].string = prov->name;
657         /*
658          * Copy reasonstrings item 0..cnt-1 to prov->error_trings positions
659          * 1..cnt.
660          */
661         for (cnt2 = 1; cnt2 <= cnt; cnt2++) {
662             prov->error_strings[cnt2].error = (int)reasonstrings[cnt2-1].id;
663             prov->error_strings[cnt2].string = reasonstrings[cnt2-1].ptr;
664         }
665
666         ERR_load_strings(prov->error_lib, prov->error_strings);
667     }
668 # endif
669 #endif
670
671     /* With this flag set, this provider has become fully "loaded". */
672     prov->flag_initialized = 1;
673     ok = 1;
674
675  end:
676     if (flag_lock)
677         CRYPTO_THREAD_unlock(prov->flag_lock);
678     return ok;
679 }
680
681 /*
682  * Deactivate a provider.
683  * Return -1 on failure and the activation count on success
684  */
685 static int provider_deactivate(OSSL_PROVIDER *prov)
686 {
687     int count;
688
689     if (!ossl_assert(prov != NULL))
690         return -1;
691
692     if (!CRYPTO_THREAD_write_lock(prov->flag_lock))
693         return -1;
694
695     if ((count = --prov->activatecnt) < 1)
696         prov->flag_activated = 0;
697
698     CRYPTO_THREAD_unlock(prov->flag_lock);
699
700     /* We don't deinit here, that's done in ossl_provider_free() */
701     return count;
702 }
703
704 /*
705  * Activate a provider.
706  * Return -1 on failure and the activation count on success
707  */
708 static int provider_activate(OSSL_PROVIDER *prov, int flag_lock)
709 {
710     int count;
711
712     if (provider_init(prov, flag_lock)) {
713         if (flag_lock && !CRYPTO_THREAD_write_lock(prov->flag_lock))
714             return -1;
715         count = ++prov->activatecnt;
716         prov->flag_activated = 1;
717         if (flag_lock)
718             CRYPTO_THREAD_unlock(prov->flag_lock);
719
720         return count;
721     }
722
723     return -1;
724 }
725
726 static int provider_flush_store_cache(const OSSL_PROVIDER *prov)
727 {
728     struct provider_store_st *store;
729     int freeing;
730
731     if ((store = get_provider_store(prov->libctx)) == NULL)
732         return 0;
733
734     if (!CRYPTO_THREAD_read_lock(store->lock))
735         return 0;
736     freeing = store->freeing;
737     CRYPTO_THREAD_unlock(store->lock);
738
739     if (!freeing)
740         return evp_method_store_flush(prov->libctx);
741     return 1;
742 }
743
744 int ossl_provider_activate(OSSL_PROVIDER *prov, int retain_fallbacks)
745 {
746     int count;
747
748     if (prov == NULL)
749         return 0;
750     if ((count = provider_activate(prov, 1)) > 0) {
751         if (!retain_fallbacks) {
752             if (!CRYPTO_THREAD_write_lock(prov->store->lock)) {
753                 provider_deactivate(prov);
754                 return 0;
755             }
756             prov->store->use_fallbacks = 0;
757             CRYPTO_THREAD_unlock(prov->store->lock);
758         }
759         return count == 1 ? provider_flush_store_cache(prov) : 1;
760     }
761     return 0;
762 }
763
764 int ossl_provider_deactivate(OSSL_PROVIDER *prov)
765 {
766     int count;
767
768     if (prov == NULL || (count = provider_deactivate(prov)) < 0)
769         return 0;
770     return count == 0 ? provider_flush_store_cache(prov) : 1;
771 }
772
773 void *ossl_provider_ctx(const OSSL_PROVIDER *prov)
774 {
775     return prov->provctx;
776 }
777
778 /*
779  * This function only does something once when store->use_fallbacks == 1,
780  * and then sets store->use_fallbacks = 0, so the second call and so on is
781  * effectively a no-op.
782  */
783 static void provider_activate_fallbacks(struct provider_store_st *store)
784 {
785     int use_fallbacks;
786     int num_provs;
787     int activated_fallback_count = 0;
788     int i;
789
790     if (!CRYPTO_THREAD_read_lock(store->lock))
791         return;
792     use_fallbacks = store->use_fallbacks;
793     CRYPTO_THREAD_unlock(store->lock);
794     if (!use_fallbacks)
795         return;
796
797     if (!CRYPTO_THREAD_write_lock(store->lock))
798         return;
799     /* Check again, just in case another thread changed it */
800     use_fallbacks = store->use_fallbacks;
801     if (!use_fallbacks) {
802         CRYPTO_THREAD_unlock(store->lock);
803         return;
804     }
805
806     num_provs = sk_OSSL_PROVIDER_num(store->providers);
807     for (i = 0; i < num_provs; i++) {
808         OSSL_PROVIDER *prov = sk_OSSL_PROVIDER_value(store->providers, i);
809
810         if (ossl_provider_up_ref(prov)) {
811             if (prov->flag_fallback) {
812                 if (provider_activate(prov, 1) > 0)
813                     activated_fallback_count++;
814             }
815             ossl_provider_free(prov);
816         }
817     }
818
819     /*
820      * We assume that all fallbacks have been added to the store before
821      * any fallback is activated.
822      * TODO: We may have to reconsider this, IF we find ourselves adding
823      * fallbacks after any previous fallback has been activated.
824      */
825     if (activated_fallback_count > 0)
826         store->use_fallbacks = 0;
827
828     CRYPTO_THREAD_unlock(store->lock);
829 }
830
831 int ossl_provider_doall_activated(OSSL_LIB_CTX *ctx,
832                                   int (*cb)(OSSL_PROVIDER *provider,
833                                             void *cbdata),
834                                   void *cbdata)
835 {
836     int ret = 0, curr, max;
837     struct provider_store_st *store = get_provider_store(ctx);
838     STACK_OF(OSSL_PROVIDER) *provs = NULL;
839
840 #ifndef FIPS_MODULE
841     /*
842      * Make sure any providers are loaded from config before we try to use
843      * them.
844      */
845     if (ossl_lib_ctx_is_default(ctx))
846         OPENSSL_init_crypto(OPENSSL_INIT_LOAD_CONFIG, NULL);
847 #endif
848
849     if (store == NULL)
850         return 1;
851     provider_activate_fallbacks(store);
852
853     /*
854      * Under lock, grab a copy of the provider list and up_ref each
855      * provider so that they don't disappear underneath us.
856      */
857     if (!CRYPTO_THREAD_read_lock(store->lock))
858         return 0;
859     provs = sk_OSSL_PROVIDER_dup(store->providers);
860     if (provs == NULL) {
861         CRYPTO_THREAD_unlock(store->lock);
862         return 0;
863     }
864     max = sk_OSSL_PROVIDER_num(provs);
865     /*
866      * We work backwards through the stack so that we can safely delete items
867      * as we go.
868      */
869     for (curr = max - 1; curr >= 0; curr--) {
870         OSSL_PROVIDER *prov = sk_OSSL_PROVIDER_value(provs, curr);
871
872         if (!CRYPTO_THREAD_write_lock(prov->flag_lock))
873             goto err_unlock;
874         if (prov->flag_activated) {
875             if (!ossl_provider_up_ref(prov)){
876                 CRYPTO_THREAD_unlock(prov->flag_lock);
877                 goto err_unlock;
878             }
879             /*
880              * It's already activated, but we up the activated count to ensure
881              * it remains activated until after we've called the user callback.
882              */
883             if (provider_activate(prov, 0) < 0) {
884                 ossl_provider_free(prov);
885                 CRYPTO_THREAD_unlock(prov->flag_lock);
886                 goto err_unlock;
887             }
888         } else {
889             sk_OSSL_PROVIDER_delete(provs, curr);
890             max--;
891         }
892         CRYPTO_THREAD_unlock(prov->flag_lock);
893     }
894     CRYPTO_THREAD_unlock(store->lock);
895
896     /*
897      * Now, we sweep through all providers not under lock
898      */
899     for (curr = 0; curr < max; curr++) {
900         OSSL_PROVIDER *prov = sk_OSSL_PROVIDER_value(provs, curr);
901
902         if (!cb(prov, cbdata))
903             goto finish;
904     }
905     curr = -1;
906
907     ret = 1;
908     goto finish;
909
910  err_unlock:
911     CRYPTO_THREAD_unlock(store->lock);
912  finish:
913     /*
914      * The pop_free call doesn't do what we want on an error condition. We
915      * either start from the first item in the stack, or part way through if
916      * we only processed some of the items.
917      */
918     for (curr++; curr < max; curr++) {
919         OSSL_PROVIDER *prov = sk_OSSL_PROVIDER_value(provs, curr);
920
921         provider_deactivate(prov);
922         ossl_provider_free(prov);
923     }
924     sk_OSSL_PROVIDER_free(provs);
925     return ret;
926 }
927
928 int ossl_provider_available(OSSL_PROVIDER *prov)
929 {
930     int ret;
931
932     if (prov != NULL) {
933         provider_activate_fallbacks(prov->store);
934
935         if (!CRYPTO_THREAD_read_lock(prov->flag_lock))
936             return 0;
937         ret = prov->flag_activated;
938         CRYPTO_THREAD_unlock(prov->flag_lock);
939         return ret;
940     }
941     return 0;
942 }
943
944 /* Setters of Provider Object data */
945 int ossl_provider_set_fallback(OSSL_PROVIDER *prov)
946 {
947     if (prov == NULL)
948         return 0;
949
950     prov->flag_fallback = 1;
951     return 1;
952 }
953
954 /* Getters of Provider Object data */
955 const char *ossl_provider_name(const OSSL_PROVIDER *prov)
956 {
957     return prov->name;
958 }
959
960 const DSO *ossl_provider_dso(const OSSL_PROVIDER *prov)
961 {
962     return prov->module;
963 }
964
965 const char *ossl_provider_module_name(const OSSL_PROVIDER *prov)
966 {
967 #ifdef FIPS_MODULE
968     return NULL;
969 #else
970     return DSO_get_filename(prov->module);
971 #endif
972 }
973
974 const char *ossl_provider_module_path(const OSSL_PROVIDER *prov)
975 {
976 #ifdef FIPS_MODULE
977     return NULL;
978 #else
979     /* FIXME: Ensure it's a full path */
980     return DSO_get_filename(prov->module);
981 #endif
982 }
983
984 void *ossl_provider_prov_ctx(const OSSL_PROVIDER *prov)
985 {
986     if (prov != NULL)
987         return prov->provctx;
988
989     return NULL;
990 }
991
992 OSSL_LIB_CTX *ossl_provider_libctx(const OSSL_PROVIDER *prov)
993 {
994     return prov != NULL ? prov->libctx : NULL;
995 }
996
997 /* Wrappers around calls to the provider */
998 void ossl_provider_teardown(const OSSL_PROVIDER *prov)
999 {
1000     if (prov->teardown != NULL)
1001         prov->teardown(prov->provctx);
1002 }
1003
1004 const OSSL_PARAM *ossl_provider_gettable_params(const OSSL_PROVIDER *prov)
1005 {
1006     return prov->gettable_params == NULL
1007         ? NULL : prov->gettable_params(prov->provctx);
1008 }
1009
1010 int ossl_provider_get_params(const OSSL_PROVIDER *prov, OSSL_PARAM params[])
1011 {
1012     return prov->get_params == NULL
1013         ? 0 : prov->get_params(prov->provctx, params);
1014 }
1015
1016 int ossl_provider_self_test(const OSSL_PROVIDER *prov)
1017 {
1018     int ret;
1019
1020     if (prov->self_test == NULL)
1021         return 1;
1022     ret = prov->self_test(prov->provctx);
1023     if (ret == 0)
1024         (void)provider_flush_store_cache(prov);
1025     return ret;
1026 }
1027
1028 int ossl_provider_get_capabilities(const OSSL_PROVIDER *prov,
1029                                    const char *capability,
1030                                    OSSL_CALLBACK *cb,
1031                                    void *arg)
1032 {
1033     return prov->get_capabilities == NULL
1034         ? 1 : prov->get_capabilities(prov->provctx, capability, cb, arg);
1035 }
1036
1037 const OSSL_ALGORITHM *ossl_provider_query_operation(const OSSL_PROVIDER *prov,
1038                                                     int operation_id,
1039                                                     int *no_cache)
1040 {
1041     const OSSL_ALGORITHM *res;
1042
1043     if (prov->query_operation == NULL)
1044         return NULL;
1045     res = prov->query_operation(prov->provctx, operation_id, no_cache);
1046 #if defined(OPENSSL_NO_CACHED_FETCH)
1047     /* Forcing the non-caching of queries */
1048     if (no_cache != NULL)
1049         *no_cache = 1;
1050 #endif
1051     return res;
1052 }
1053
1054 void ossl_provider_unquery_operation(const OSSL_PROVIDER *prov,
1055                                      int operation_id,
1056                                      const OSSL_ALGORITHM *algs)
1057 {
1058     if (prov->unquery_operation != NULL)
1059         prov->unquery_operation(prov->provctx, operation_id, algs);
1060 }
1061
1062 int ossl_provider_clear_all_operation_bits(OSSL_LIB_CTX *libctx)
1063 {
1064     struct provider_store_st *store;
1065     OSSL_PROVIDER *provider;
1066     int i, num, res = 1;
1067
1068     if ((store = get_provider_store(libctx)) != NULL) {
1069         if (!CRYPTO_THREAD_read_lock(store->lock))
1070             return 0;
1071         num = sk_OSSL_PROVIDER_num(store->providers);
1072         for (i = 0; i < num; i++) {
1073             provider = sk_OSSL_PROVIDER_value(store->providers, i);
1074             if (!CRYPTO_THREAD_write_lock(provider->opbits_lock)) {
1075                 res = 0;
1076                 continue;
1077             }
1078             if (provider->operation_bits != NULL)
1079                 memset(provider->operation_bits, 0,
1080                        provider->operation_bits_sz);
1081             CRYPTO_THREAD_unlock(provider->opbits_lock);
1082         }
1083         CRYPTO_THREAD_unlock(store->lock);
1084         return res;
1085     }
1086     return 0;
1087 }
1088
1089 int ossl_provider_set_operation_bit(OSSL_PROVIDER *provider, size_t bitnum)
1090 {
1091     size_t byte = bitnum / 8;
1092     unsigned char bit = (1 << (bitnum % 8)) & 0xFF;
1093
1094     if (!CRYPTO_THREAD_write_lock(provider->opbits_lock))
1095         return 0;
1096     if (provider->operation_bits_sz <= byte) {
1097         unsigned char *tmp = OPENSSL_realloc(provider->operation_bits,
1098                                              byte + 1);
1099
1100         if (tmp == NULL) {
1101             CRYPTO_THREAD_unlock(provider->opbits_lock);
1102             ERR_raise(ERR_LIB_CRYPTO, ERR_R_MALLOC_FAILURE);
1103             return 0;
1104         }
1105         provider->operation_bits = tmp;
1106         memset(provider->operation_bits + provider->operation_bits_sz,
1107                '\0', byte + 1 - provider->operation_bits_sz);
1108         provider->operation_bits_sz = byte + 1;
1109     }
1110     provider->operation_bits[byte] |= bit;
1111     CRYPTO_THREAD_unlock(provider->opbits_lock);
1112     return 1;
1113 }
1114
1115 int ossl_provider_test_operation_bit(OSSL_PROVIDER *provider, size_t bitnum,
1116                                      int *result)
1117 {
1118     size_t byte = bitnum / 8;
1119     unsigned char bit = (1 << (bitnum % 8)) & 0xFF;
1120
1121     if (!ossl_assert(result != NULL)) {
1122         ERR_raise(ERR_LIB_CRYPTO, ERR_R_PASSED_NULL_PARAMETER);
1123         return 0;
1124     }
1125
1126     *result = 0;
1127     if (!CRYPTO_THREAD_read_lock(provider->opbits_lock))
1128         return 0;
1129     if (provider->operation_bits_sz > byte)
1130         *result = ((provider->operation_bits[byte] & bit) != 0);
1131     CRYPTO_THREAD_unlock(provider->opbits_lock);
1132     return 1;
1133 }
1134
1135 /*-
1136  * Core functions for the provider
1137  * ===============================
1138  *
1139  * This is the set of functions that the core makes available to the provider
1140  */
1141
1142 /*
1143  * This returns a list of Provider Object parameters with their types, for
1144  * discovery.  We do not expect that many providers will use this, but one
1145  * never knows.
1146  */
1147 static const OSSL_PARAM param_types[] = {
1148     OSSL_PARAM_DEFN(OSSL_PROV_PARAM_CORE_VERSION, OSSL_PARAM_UTF8_PTR, NULL, 0),
1149     OSSL_PARAM_DEFN(OSSL_PROV_PARAM_CORE_PROV_NAME, OSSL_PARAM_UTF8_PTR,
1150                     NULL, 0),
1151 #ifndef FIPS_MODULE
1152     OSSL_PARAM_DEFN(OSSL_PROV_PARAM_CORE_MODULE_FILENAME, OSSL_PARAM_UTF8_PTR,
1153                     NULL, 0),
1154 #endif
1155     OSSL_PARAM_END
1156 };
1157
1158 /*
1159  * Forward declare all the functions that are provided aa dispatch.
1160  * This ensures that the compiler will complain if they aren't defined
1161  * with the correct signature.
1162  */
1163 static OSSL_FUNC_core_gettable_params_fn core_gettable_params;
1164 static OSSL_FUNC_core_get_params_fn core_get_params;
1165 static OSSL_FUNC_core_thread_start_fn core_thread_start;
1166 static OSSL_FUNC_core_get_libctx_fn core_get_libctx;
1167 #ifndef FIPS_MODULE
1168 static OSSL_FUNC_core_new_error_fn core_new_error;
1169 static OSSL_FUNC_core_set_error_debug_fn core_set_error_debug;
1170 static OSSL_FUNC_core_vset_error_fn core_vset_error;
1171 static OSSL_FUNC_core_set_error_mark_fn core_set_error_mark;
1172 static OSSL_FUNC_core_clear_last_error_mark_fn core_clear_last_error_mark;
1173 static OSSL_FUNC_core_pop_error_to_mark_fn core_pop_error_to_mark;
1174 #endif
1175
1176 static const OSSL_PARAM *core_gettable_params(const OSSL_CORE_HANDLE *handle)
1177 {
1178     return param_types;
1179 }
1180
1181 static int core_get_params(const OSSL_CORE_HANDLE *handle, OSSL_PARAM params[])
1182 {
1183     int i;
1184     OSSL_PARAM *p;
1185     /*
1186      * We created this object originally and we know it is actually an
1187      * OSSL_PROVIDER *, so the cast is safe
1188      */
1189     OSSL_PROVIDER *prov = (OSSL_PROVIDER *)handle;
1190
1191     if ((p = OSSL_PARAM_locate(params, OSSL_PROV_PARAM_CORE_VERSION)) != NULL)
1192         OSSL_PARAM_set_utf8_ptr(p, OPENSSL_VERSION_STR);
1193     if ((p = OSSL_PARAM_locate(params, OSSL_PROV_PARAM_CORE_PROV_NAME)) != NULL)
1194         OSSL_PARAM_set_utf8_ptr(p, prov->name);
1195
1196 #ifndef FIPS_MODULE
1197     if ((p = OSSL_PARAM_locate(params,
1198                                OSSL_PROV_PARAM_CORE_MODULE_FILENAME)) != NULL)
1199         OSSL_PARAM_set_utf8_ptr(p, ossl_provider_module_path(prov));
1200 #endif
1201
1202     if (prov->parameters == NULL)
1203         return 1;
1204
1205     for (i = 0; i < sk_INFOPAIR_num(prov->parameters); i++) {
1206         INFOPAIR *pair = sk_INFOPAIR_value(prov->parameters, i);
1207
1208         if ((p = OSSL_PARAM_locate(params, pair->name)) != NULL)
1209             OSSL_PARAM_set_utf8_ptr(p, pair->value);
1210     }
1211     return 1;
1212 }
1213
1214 static OPENSSL_CORE_CTX *core_get_libctx(const OSSL_CORE_HANDLE *handle)
1215 {
1216     /*
1217      * We created this object originally and we know it is actually an
1218      * OSSL_PROVIDER *, so the cast is safe
1219      */
1220     OSSL_PROVIDER *prov = (OSSL_PROVIDER *)handle;
1221
1222     /*
1223      * Using ossl_provider_libctx would be wrong as that returns
1224      * NULL for |prov| == NULL and NULL libctx has a special meaning
1225      * that does not apply here. Here |prov| == NULL can happen only in
1226      * case of a coding error.
1227      */
1228     assert(prov != NULL);
1229     return (OPENSSL_CORE_CTX *)prov->libctx;
1230 }
1231
1232 static int core_thread_start(const OSSL_CORE_HANDLE *handle,
1233                              OSSL_thread_stop_handler_fn handfn)
1234 {
1235     /*
1236      * We created this object originally and we know it is actually an
1237      * OSSL_PROVIDER *, so the cast is safe
1238      */
1239     OSSL_PROVIDER *prov = (OSSL_PROVIDER *)handle;
1240
1241     return ossl_init_thread_start(prov, prov->provctx, handfn);
1242 }
1243
1244 /*
1245  * The FIPS module inner provider doesn't implement these.  They aren't
1246  * needed there, since the FIPS module upcalls are always the outer provider
1247  * ones.
1248  */
1249 #ifndef FIPS_MODULE
1250 /*
1251  * These error functions should use |handle| to select the proper
1252  * library context to report in the correct error stack if error
1253  * stacks become tied to the library context.
1254  * We cannot currently do that since there's no support for it in the
1255  * ERR subsystem.
1256  */
1257 static void core_new_error(const OSSL_CORE_HANDLE *handle)
1258 {
1259     ERR_new();
1260 }
1261
1262 static void core_set_error_debug(const OSSL_CORE_HANDLE *handle,
1263                                  const char *file, int line, const char *func)
1264 {
1265     ERR_set_debug(file, line, func);
1266 }
1267
1268 static void core_vset_error(const OSSL_CORE_HANDLE *handle,
1269                             uint32_t reason, const char *fmt, va_list args)
1270 {
1271     /*
1272      * We created this object originally and we know it is actually an
1273      * OSSL_PROVIDER *, so the cast is safe
1274      */
1275     OSSL_PROVIDER *prov = (OSSL_PROVIDER *)handle;
1276
1277     /*
1278      * If the uppermost 8 bits are non-zero, it's an OpenSSL library
1279      * error and will be treated as such.  Otherwise, it's a new style
1280      * provider error and will be treated as such.
1281      */
1282     if (ERR_GET_LIB(reason) != 0) {
1283         ERR_vset_error(ERR_GET_LIB(reason), ERR_GET_REASON(reason), fmt, args);
1284     } else {
1285         ERR_vset_error(prov->error_lib, (int)reason, fmt, args);
1286     }
1287 }
1288
1289 static int core_set_error_mark(const OSSL_CORE_HANDLE *handle)
1290 {
1291     return ERR_set_mark();
1292 }
1293
1294 static int core_clear_last_error_mark(const OSSL_CORE_HANDLE *handle)
1295 {
1296     return ERR_clear_last_mark();
1297 }
1298
1299 static int core_pop_error_to_mark(const OSSL_CORE_HANDLE *handle)
1300 {
1301     return ERR_pop_to_mark();
1302 }
1303 #endif /* FIPS_MODULE */
1304
1305 /*
1306  * Functions provided by the core.
1307  */
1308 static const OSSL_DISPATCH core_dispatch_[] = {
1309     { OSSL_FUNC_CORE_GETTABLE_PARAMS, (void (*)(void))core_gettable_params },
1310     { OSSL_FUNC_CORE_GET_PARAMS, (void (*)(void))core_get_params },
1311     { OSSL_FUNC_CORE_GET_LIBCTX, (void (*)(void))core_get_libctx },
1312     { OSSL_FUNC_CORE_THREAD_START, (void (*)(void))core_thread_start },
1313 #ifndef FIPS_MODULE
1314     { OSSL_FUNC_CORE_NEW_ERROR, (void (*)(void))core_new_error },
1315     { OSSL_FUNC_CORE_SET_ERROR_DEBUG, (void (*)(void))core_set_error_debug },
1316     { OSSL_FUNC_CORE_VSET_ERROR, (void (*)(void))core_vset_error },
1317     { OSSL_FUNC_CORE_SET_ERROR_MARK, (void (*)(void))core_set_error_mark },
1318     { OSSL_FUNC_CORE_CLEAR_LAST_ERROR_MARK,
1319       (void (*)(void))core_clear_last_error_mark },
1320     { OSSL_FUNC_CORE_POP_ERROR_TO_MARK, (void (*)(void))core_pop_error_to_mark },
1321     { OSSL_FUNC_BIO_NEW_FILE, (void (*)(void))ossl_core_bio_new_file },
1322     { OSSL_FUNC_BIO_NEW_MEMBUF, (void (*)(void))ossl_core_bio_new_mem_buf },
1323     { OSSL_FUNC_BIO_READ_EX, (void (*)(void))ossl_core_bio_read_ex },
1324     { OSSL_FUNC_BIO_WRITE_EX, (void (*)(void))ossl_core_bio_write_ex },
1325     { OSSL_FUNC_BIO_GETS, (void (*)(void))ossl_core_bio_gets },
1326     { OSSL_FUNC_BIO_PUTS, (void (*)(void))ossl_core_bio_puts },
1327     { OSSL_FUNC_BIO_CTRL, (void (*)(void))ossl_core_bio_ctrl },
1328     { OSSL_FUNC_BIO_UP_REF, (void (*)(void))ossl_core_bio_up_ref },
1329     { OSSL_FUNC_BIO_FREE, (void (*)(void))ossl_core_bio_free },
1330     { OSSL_FUNC_BIO_VPRINTF, (void (*)(void))ossl_core_bio_vprintf },
1331     { OSSL_FUNC_BIO_VSNPRINTF, (void (*)(void))BIO_vsnprintf },
1332     { OSSL_FUNC_SELF_TEST_CB, (void (*)(void))OSSL_SELF_TEST_get_callback },
1333     { OSSL_FUNC_GET_ENTROPY, (void (*)(void))ossl_rand_get_entropy },
1334     { OSSL_FUNC_CLEANUP_ENTROPY, (void (*)(void))ossl_rand_cleanup_entropy },
1335     { OSSL_FUNC_GET_NONCE, (void (*)(void))ossl_rand_get_nonce },
1336     { OSSL_FUNC_CLEANUP_NONCE, (void (*)(void))ossl_rand_cleanup_nonce },
1337 #endif
1338     { OSSL_FUNC_CRYPTO_MALLOC, (void (*)(void))CRYPTO_malloc },
1339     { OSSL_FUNC_CRYPTO_ZALLOC, (void (*)(void))CRYPTO_zalloc },
1340     { OSSL_FUNC_CRYPTO_FREE, (void (*)(void))CRYPTO_free },
1341     { OSSL_FUNC_CRYPTO_CLEAR_FREE, (void (*)(void))CRYPTO_clear_free },
1342     { OSSL_FUNC_CRYPTO_REALLOC, (void (*)(void))CRYPTO_realloc },
1343     { OSSL_FUNC_CRYPTO_CLEAR_REALLOC, (void (*)(void))CRYPTO_clear_realloc },
1344     { OSSL_FUNC_CRYPTO_SECURE_MALLOC, (void (*)(void))CRYPTO_secure_malloc },
1345     { OSSL_FUNC_CRYPTO_SECURE_ZALLOC, (void (*)(void))CRYPTO_secure_zalloc },
1346     { OSSL_FUNC_CRYPTO_SECURE_FREE, (void (*)(void))CRYPTO_secure_free },
1347     { OSSL_FUNC_CRYPTO_SECURE_CLEAR_FREE,
1348         (void (*)(void))CRYPTO_secure_clear_free },
1349     { OSSL_FUNC_CRYPTO_SECURE_ALLOCATED,
1350         (void (*)(void))CRYPTO_secure_allocated },
1351     { OSSL_FUNC_OPENSSL_CLEANSE, (void (*)(void))OPENSSL_cleanse },
1352
1353     { 0, NULL }
1354 };
1355 static const OSSL_DISPATCH *core_dispatch = core_dispatch_;