provider: flush the store cache when providers are loaded/unloaded.
[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)
237             OPENSSL_init_crypto(OPENSSL_INIT_LOAD_CONFIG, NULL);
238 #endif
239
240         tmpl.name = (char *)name;
241         if (!CRYPTO_THREAD_write_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     OPENSSL_init_crypto(OPENSSL_INIT_LOAD_CONFIG, NULL);
846 #endif
847
848     if (store == NULL)
849         return 1;
850     provider_activate_fallbacks(store);
851
852     /*
853      * Under lock, grab a copy of the provider list and up_ref each
854      * provider so that they don't disappear underneath us.
855      */
856     if (!CRYPTO_THREAD_read_lock(store->lock))
857         return 0;
858     provs = sk_OSSL_PROVIDER_dup(store->providers);
859     if (provs == NULL) {
860         CRYPTO_THREAD_unlock(store->lock);
861         return 0;
862     }
863     max = sk_OSSL_PROVIDER_num(provs);
864     /*
865      * We work backwards through the stack so that we can safely delete items
866      * as we go.
867      */
868     for (curr = max - 1; curr >= 0; curr--) {
869         OSSL_PROVIDER *prov = sk_OSSL_PROVIDER_value(provs, curr);
870
871         if (!CRYPTO_THREAD_write_lock(prov->flag_lock))
872             goto err_unlock;
873         if (prov->flag_activated) {
874             if (!ossl_provider_up_ref(prov)){
875                 CRYPTO_THREAD_unlock(prov->flag_lock);
876                 goto err_unlock;
877             }
878             /*
879              * It's already activated, but we up the activated count to ensure
880              * it remains activated until after we've called the user callback.
881              */
882             if (provider_activate(prov, 0) < 0) {
883                 ossl_provider_free(prov);
884                 CRYPTO_THREAD_unlock(prov->flag_lock);
885                 goto err_unlock;
886             }
887         } else {
888             sk_OSSL_PROVIDER_delete(provs, curr);
889             max--;
890         }
891         CRYPTO_THREAD_unlock(prov->flag_lock);
892     }
893     CRYPTO_THREAD_unlock(store->lock);
894
895     /*
896      * Now, we sweep through all providers not under lock
897      */
898     for (curr = 0; curr < max; curr++) {
899         OSSL_PROVIDER *prov = sk_OSSL_PROVIDER_value(provs, curr);
900
901         if (!cb(prov, cbdata))
902             goto finish;
903     }
904     curr = -1;
905
906     ret = 1;
907     goto finish;
908
909  err_unlock:
910     CRYPTO_THREAD_unlock(store->lock);
911  finish:
912     /*
913      * The pop_free call doesn't do what we want on an error condition. We
914      * either start from the first item in the stack, or part way through if
915      * we only processed some of the items.
916      */
917     for (curr++; curr < max; curr++) {
918         OSSL_PROVIDER *prov = sk_OSSL_PROVIDER_value(provs, curr);
919
920         provider_deactivate(prov);
921         ossl_provider_free(prov);
922     }
923     sk_OSSL_PROVIDER_free(provs);
924     return ret;
925 }
926
927 int ossl_provider_available(OSSL_PROVIDER *prov)
928 {
929     int ret;
930
931     if (prov != NULL) {
932         provider_activate_fallbacks(prov->store);
933
934         if (!CRYPTO_THREAD_read_lock(prov->flag_lock))
935             return 0;
936         ret = prov->flag_activated;
937         CRYPTO_THREAD_unlock(prov->flag_lock);
938         return ret;
939     }
940     return 0;
941 }
942
943 /* Setters of Provider Object data */
944 int ossl_provider_set_fallback(OSSL_PROVIDER *prov)
945 {
946     if (prov == NULL)
947         return 0;
948
949     prov->flag_fallback = 1;
950     return 1;
951 }
952
953 /* Getters of Provider Object data */
954 const char *ossl_provider_name(const OSSL_PROVIDER *prov)
955 {
956     return prov->name;
957 }
958
959 const DSO *ossl_provider_dso(const OSSL_PROVIDER *prov)
960 {
961     return prov->module;
962 }
963
964 const char *ossl_provider_module_name(const OSSL_PROVIDER *prov)
965 {
966 #ifdef FIPS_MODULE
967     return NULL;
968 #else
969     return DSO_get_filename(prov->module);
970 #endif
971 }
972
973 const char *ossl_provider_module_path(const OSSL_PROVIDER *prov)
974 {
975 #ifdef FIPS_MODULE
976     return NULL;
977 #else
978     /* FIXME: Ensure it's a full path */
979     return DSO_get_filename(prov->module);
980 #endif
981 }
982
983 void *ossl_provider_prov_ctx(const OSSL_PROVIDER *prov)
984 {
985     if (prov != NULL)
986         return prov->provctx;
987
988     return NULL;
989 }
990
991 OSSL_LIB_CTX *ossl_provider_libctx(const OSSL_PROVIDER *prov)
992 {
993     return prov != NULL ? prov->libctx : NULL;
994 }
995
996 /* Wrappers around calls to the provider */
997 void ossl_provider_teardown(const OSSL_PROVIDER *prov)
998 {
999     if (prov->teardown != NULL)
1000         prov->teardown(prov->provctx);
1001 }
1002
1003 const OSSL_PARAM *ossl_provider_gettable_params(const OSSL_PROVIDER *prov)
1004 {
1005     return prov->gettable_params == NULL
1006         ? NULL : prov->gettable_params(prov->provctx);
1007 }
1008
1009 int ossl_provider_get_params(const OSSL_PROVIDER *prov, OSSL_PARAM params[])
1010 {
1011     return prov->get_params == NULL
1012         ? 0 : prov->get_params(prov->provctx, params);
1013 }
1014
1015 int ossl_provider_self_test(const OSSL_PROVIDER *prov)
1016 {
1017     int ret;
1018
1019     if (prov->self_test == NULL)
1020         return 1;
1021     ret = prov->self_test(prov->provctx);
1022     if (ret == 0)
1023         (void)provider_flush_store_cache(prov);
1024     return ret;
1025 }
1026
1027 int ossl_provider_get_capabilities(const OSSL_PROVIDER *prov,
1028                                    const char *capability,
1029                                    OSSL_CALLBACK *cb,
1030                                    void *arg)
1031 {
1032     return prov->get_capabilities == NULL
1033         ? 1 : prov->get_capabilities(prov->provctx, capability, cb, arg);
1034 }
1035
1036 const OSSL_ALGORITHM *ossl_provider_query_operation(const OSSL_PROVIDER *prov,
1037                                                     int operation_id,
1038                                                     int *no_cache)
1039 {
1040     const OSSL_ALGORITHM *res;
1041
1042     if (prov->query_operation == NULL)
1043         return NULL;
1044     res = prov->query_operation(prov->provctx, operation_id, no_cache);
1045 #if defined(OPENSSL_NO_CACHED_FETCH)
1046     /* Forcing the non-caching of queries */
1047     if (no_cache != NULL)
1048         *no_cache = 1;
1049 #endif
1050     return res;
1051 }
1052
1053 void ossl_provider_unquery_operation(const OSSL_PROVIDER *prov,
1054                                      int operation_id,
1055                                      const OSSL_ALGORITHM *algs)
1056 {
1057     if (prov->unquery_operation != NULL)
1058         prov->unquery_operation(prov->provctx, operation_id, algs);
1059 }
1060
1061 int ossl_provider_clear_all_operation_bits(OSSL_LIB_CTX *libctx)
1062 {
1063     struct provider_store_st *store;
1064     OSSL_PROVIDER *provider;
1065     int i, num, res = 1;
1066
1067     if ((store = get_provider_store(libctx)) != NULL) {
1068         if (!CRYPTO_THREAD_read_lock(store->lock))
1069             return 0;
1070         num = sk_OSSL_PROVIDER_num(store->providers);
1071         for (i = 0; i < num; i++) {
1072             provider = sk_OSSL_PROVIDER_value(store->providers, i);
1073             if (!CRYPTO_THREAD_write_lock(provider->opbits_lock)) {
1074                 res = 0;
1075                 continue;
1076             }
1077             if (provider->operation_bits != NULL)
1078                 memset(provider->operation_bits, 0,
1079                        provider->operation_bits_sz);
1080             CRYPTO_THREAD_unlock(provider->opbits_lock);
1081         }
1082         CRYPTO_THREAD_unlock(store->lock);
1083         return res;
1084     }
1085     return 0;
1086 }
1087
1088 int ossl_provider_set_operation_bit(OSSL_PROVIDER *provider, size_t bitnum)
1089 {
1090     size_t byte = bitnum / 8;
1091     unsigned char bit = (1 << (bitnum % 8)) & 0xFF;
1092
1093     if (!CRYPTO_THREAD_write_lock(provider->opbits_lock))
1094         return 0;
1095     if (provider->operation_bits_sz <= byte) {
1096         unsigned char *tmp = OPENSSL_realloc(provider->operation_bits,
1097                                              byte + 1);
1098
1099         if (tmp == NULL) {
1100             CRYPTO_THREAD_unlock(provider->opbits_lock);
1101             ERR_raise(ERR_LIB_CRYPTO, ERR_R_MALLOC_FAILURE);
1102             return 0;
1103         }
1104         provider->operation_bits = tmp;
1105         memset(provider->operation_bits + provider->operation_bits_sz,
1106                '\0', byte + 1 - provider->operation_bits_sz);
1107         provider->operation_bits_sz = byte + 1;
1108     }
1109     provider->operation_bits[byte] |= bit;
1110     CRYPTO_THREAD_unlock(provider->opbits_lock);
1111     return 1;
1112 }
1113
1114 int ossl_provider_test_operation_bit(OSSL_PROVIDER *provider, size_t bitnum,
1115                                      int *result)
1116 {
1117     size_t byte = bitnum / 8;
1118     unsigned char bit = (1 << (bitnum % 8)) & 0xFF;
1119
1120     if (!ossl_assert(result != NULL)) {
1121         ERR_raise(ERR_LIB_CRYPTO, ERR_R_PASSED_NULL_PARAMETER);
1122         return 0;
1123     }
1124
1125     *result = 0;
1126     if (!CRYPTO_THREAD_read_lock(provider->opbits_lock))
1127         return 0;
1128     if (provider->operation_bits_sz > byte)
1129         *result = ((provider->operation_bits[byte] & bit) != 0);
1130     CRYPTO_THREAD_unlock(provider->opbits_lock);
1131     return 1;
1132 }
1133
1134 /*-
1135  * Core functions for the provider
1136  * ===============================
1137  *
1138  * This is the set of functions that the core makes available to the provider
1139  */
1140
1141 /*
1142  * This returns a list of Provider Object parameters with their types, for
1143  * discovery.  We do not expect that many providers will use this, but one
1144  * never knows.
1145  */
1146 static const OSSL_PARAM param_types[] = {
1147     OSSL_PARAM_DEFN(OSSL_PROV_PARAM_CORE_VERSION, OSSL_PARAM_UTF8_PTR, NULL, 0),
1148     OSSL_PARAM_DEFN(OSSL_PROV_PARAM_CORE_PROV_NAME, OSSL_PARAM_UTF8_PTR,
1149                     NULL, 0),
1150 #ifndef FIPS_MODULE
1151     OSSL_PARAM_DEFN(OSSL_PROV_PARAM_CORE_MODULE_FILENAME, OSSL_PARAM_UTF8_PTR,
1152                     NULL, 0),
1153 #endif
1154     OSSL_PARAM_END
1155 };
1156
1157 /*
1158  * Forward declare all the functions that are provided aa dispatch.
1159  * This ensures that the compiler will complain if they aren't defined
1160  * with the correct signature.
1161  */
1162 static OSSL_FUNC_core_gettable_params_fn core_gettable_params;
1163 static OSSL_FUNC_core_get_params_fn core_get_params;
1164 static OSSL_FUNC_core_thread_start_fn core_thread_start;
1165 static OSSL_FUNC_core_get_libctx_fn core_get_libctx;
1166 #ifndef FIPS_MODULE
1167 static OSSL_FUNC_core_new_error_fn core_new_error;
1168 static OSSL_FUNC_core_set_error_debug_fn core_set_error_debug;
1169 static OSSL_FUNC_core_vset_error_fn core_vset_error;
1170 static OSSL_FUNC_core_set_error_mark_fn core_set_error_mark;
1171 static OSSL_FUNC_core_clear_last_error_mark_fn core_clear_last_error_mark;
1172 static OSSL_FUNC_core_pop_error_to_mark_fn core_pop_error_to_mark;
1173 #endif
1174
1175 static const OSSL_PARAM *core_gettable_params(const OSSL_CORE_HANDLE *handle)
1176 {
1177     return param_types;
1178 }
1179
1180 static int core_get_params(const OSSL_CORE_HANDLE *handle, OSSL_PARAM params[])
1181 {
1182     int i;
1183     OSSL_PARAM *p;
1184     /*
1185      * We created this object originally and we know it is actually an
1186      * OSSL_PROVIDER *, so the cast is safe
1187      */
1188     OSSL_PROVIDER *prov = (OSSL_PROVIDER *)handle;
1189
1190     if ((p = OSSL_PARAM_locate(params, OSSL_PROV_PARAM_CORE_VERSION)) != NULL)
1191         OSSL_PARAM_set_utf8_ptr(p, OPENSSL_VERSION_STR);
1192     if ((p = OSSL_PARAM_locate(params, OSSL_PROV_PARAM_CORE_PROV_NAME)) != NULL)
1193         OSSL_PARAM_set_utf8_ptr(p, prov->name);
1194
1195 #ifndef FIPS_MODULE
1196     if ((p = OSSL_PARAM_locate(params,
1197                                OSSL_PROV_PARAM_CORE_MODULE_FILENAME)) != NULL)
1198         OSSL_PARAM_set_utf8_ptr(p, ossl_provider_module_path(prov));
1199 #endif
1200
1201     if (prov->parameters == NULL)
1202         return 1;
1203
1204     for (i = 0; i < sk_INFOPAIR_num(prov->parameters); i++) {
1205         INFOPAIR *pair = sk_INFOPAIR_value(prov->parameters, i);
1206
1207         if ((p = OSSL_PARAM_locate(params, pair->name)) != NULL)
1208             OSSL_PARAM_set_utf8_ptr(p, pair->value);
1209     }
1210     return 1;
1211 }
1212
1213 static OPENSSL_CORE_CTX *core_get_libctx(const OSSL_CORE_HANDLE *handle)
1214 {
1215     /*
1216      * We created this object originally and we know it is actually an
1217      * OSSL_PROVIDER *, so the cast is safe
1218      */
1219     OSSL_PROVIDER *prov = (OSSL_PROVIDER *)handle;
1220
1221     /*
1222      * Using ossl_provider_libctx would be wrong as that returns
1223      * NULL for |prov| == NULL and NULL libctx has a special meaning
1224      * that does not apply here. Here |prov| == NULL can happen only in
1225      * case of a coding error.
1226      */
1227     assert(prov != NULL);
1228     return (OPENSSL_CORE_CTX *)prov->libctx;
1229 }
1230
1231 static int core_thread_start(const OSSL_CORE_HANDLE *handle,
1232                              OSSL_thread_stop_handler_fn handfn)
1233 {
1234     /*
1235      * We created this object originally and we know it is actually an
1236      * OSSL_PROVIDER *, so the cast is safe
1237      */
1238     OSSL_PROVIDER *prov = (OSSL_PROVIDER *)handle;
1239
1240     return ossl_init_thread_start(prov, prov->provctx, handfn);
1241 }
1242
1243 /*
1244  * The FIPS module inner provider doesn't implement these.  They aren't
1245  * needed there, since the FIPS module upcalls are always the outer provider
1246  * ones.
1247  */
1248 #ifndef FIPS_MODULE
1249 /*
1250  * These error functions should use |handle| to select the proper
1251  * library context to report in the correct error stack if error
1252  * stacks become tied to the library context.
1253  * We cannot currently do that since there's no support for it in the
1254  * ERR subsystem.
1255  */
1256 static void core_new_error(const OSSL_CORE_HANDLE *handle)
1257 {
1258     ERR_new();
1259 }
1260
1261 static void core_set_error_debug(const OSSL_CORE_HANDLE *handle,
1262                                  const char *file, int line, const char *func)
1263 {
1264     ERR_set_debug(file, line, func);
1265 }
1266
1267 static void core_vset_error(const OSSL_CORE_HANDLE *handle,
1268                             uint32_t reason, const char *fmt, va_list args)
1269 {
1270     /*
1271      * We created this object originally and we know it is actually an
1272      * OSSL_PROVIDER *, so the cast is safe
1273      */
1274     OSSL_PROVIDER *prov = (OSSL_PROVIDER *)handle;
1275
1276     /*
1277      * If the uppermost 8 bits are non-zero, it's an OpenSSL library
1278      * error and will be treated as such.  Otherwise, it's a new style
1279      * provider error and will be treated as such.
1280      */
1281     if (ERR_GET_LIB(reason) != 0) {
1282         ERR_vset_error(ERR_GET_LIB(reason), ERR_GET_REASON(reason), fmt, args);
1283     } else {
1284         ERR_vset_error(prov->error_lib, (int)reason, fmt, args);
1285     }
1286 }
1287
1288 static int core_set_error_mark(const OSSL_CORE_HANDLE *handle)
1289 {
1290     return ERR_set_mark();
1291 }
1292
1293 static int core_clear_last_error_mark(const OSSL_CORE_HANDLE *handle)
1294 {
1295     return ERR_clear_last_mark();
1296 }
1297
1298 static int core_pop_error_to_mark(const OSSL_CORE_HANDLE *handle)
1299 {
1300     return ERR_pop_to_mark();
1301 }
1302 #endif /* FIPS_MODULE */
1303
1304 /*
1305  * Functions provided by the core.
1306  */
1307 static const OSSL_DISPATCH core_dispatch_[] = {
1308     { OSSL_FUNC_CORE_GETTABLE_PARAMS, (void (*)(void))core_gettable_params },
1309     { OSSL_FUNC_CORE_GET_PARAMS, (void (*)(void))core_get_params },
1310     { OSSL_FUNC_CORE_GET_LIBCTX, (void (*)(void))core_get_libctx },
1311     { OSSL_FUNC_CORE_THREAD_START, (void (*)(void))core_thread_start },
1312 #ifndef FIPS_MODULE
1313     { OSSL_FUNC_CORE_NEW_ERROR, (void (*)(void))core_new_error },
1314     { OSSL_FUNC_CORE_SET_ERROR_DEBUG, (void (*)(void))core_set_error_debug },
1315     { OSSL_FUNC_CORE_VSET_ERROR, (void (*)(void))core_vset_error },
1316     { OSSL_FUNC_CORE_SET_ERROR_MARK, (void (*)(void))core_set_error_mark },
1317     { OSSL_FUNC_CORE_CLEAR_LAST_ERROR_MARK,
1318       (void (*)(void))core_clear_last_error_mark },
1319     { OSSL_FUNC_CORE_POP_ERROR_TO_MARK, (void (*)(void))core_pop_error_to_mark },
1320     { OSSL_FUNC_BIO_NEW_FILE, (void (*)(void))ossl_core_bio_new_file },
1321     { OSSL_FUNC_BIO_NEW_MEMBUF, (void (*)(void))ossl_core_bio_new_mem_buf },
1322     { OSSL_FUNC_BIO_READ_EX, (void (*)(void))ossl_core_bio_read_ex },
1323     { OSSL_FUNC_BIO_WRITE_EX, (void (*)(void))ossl_core_bio_write_ex },
1324     { OSSL_FUNC_BIO_GETS, (void (*)(void))ossl_core_bio_gets },
1325     { OSSL_FUNC_BIO_PUTS, (void (*)(void))ossl_core_bio_puts },
1326     { OSSL_FUNC_BIO_CTRL, (void (*)(void))ossl_core_bio_ctrl },
1327     { OSSL_FUNC_BIO_UP_REF, (void (*)(void))ossl_core_bio_up_ref },
1328     { OSSL_FUNC_BIO_FREE, (void (*)(void))ossl_core_bio_free },
1329     { OSSL_FUNC_BIO_VPRINTF, (void (*)(void))ossl_core_bio_vprintf },
1330     { OSSL_FUNC_BIO_VSNPRINTF, (void (*)(void))BIO_vsnprintf },
1331     { OSSL_FUNC_SELF_TEST_CB, (void (*)(void))OSSL_SELF_TEST_get_callback },
1332     { OSSL_FUNC_GET_ENTROPY, (void (*)(void))ossl_rand_get_entropy },
1333     { OSSL_FUNC_CLEANUP_ENTROPY, (void (*)(void))ossl_rand_cleanup_entropy },
1334     { OSSL_FUNC_GET_NONCE, (void (*)(void))ossl_rand_get_nonce },
1335     { OSSL_FUNC_CLEANUP_NONCE, (void (*)(void))ossl_rand_cleanup_nonce },
1336 #endif
1337     { OSSL_FUNC_CRYPTO_MALLOC, (void (*)(void))CRYPTO_malloc },
1338     { OSSL_FUNC_CRYPTO_ZALLOC, (void (*)(void))CRYPTO_zalloc },
1339     { OSSL_FUNC_CRYPTO_FREE, (void (*)(void))CRYPTO_free },
1340     { OSSL_FUNC_CRYPTO_CLEAR_FREE, (void (*)(void))CRYPTO_clear_free },
1341     { OSSL_FUNC_CRYPTO_REALLOC, (void (*)(void))CRYPTO_realloc },
1342     { OSSL_FUNC_CRYPTO_CLEAR_REALLOC, (void (*)(void))CRYPTO_clear_realloc },
1343     { OSSL_FUNC_CRYPTO_SECURE_MALLOC, (void (*)(void))CRYPTO_secure_malloc },
1344     { OSSL_FUNC_CRYPTO_SECURE_ZALLOC, (void (*)(void))CRYPTO_secure_zalloc },
1345     { OSSL_FUNC_CRYPTO_SECURE_FREE, (void (*)(void))CRYPTO_secure_free },
1346     { OSSL_FUNC_CRYPTO_SECURE_CLEAR_FREE,
1347         (void (*)(void))CRYPTO_secure_clear_free },
1348     { OSSL_FUNC_CRYPTO_SECURE_ALLOCATED,
1349         (void (*)(void))CRYPTO_secure_allocated },
1350     { OSSL_FUNC_OPENSSL_CLEANSE, (void (*)(void))OPENSSL_cleanse },
1351
1352     { 0, NULL }
1353 };
1354 static const OSSL_DISPATCH *core_dispatch = core_dispatch_;