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