385a6326533f057d4925643616c126efd6a18c1c
[openssl.git] / crypto / provider_core.c
1 /*
2  * Copyright 2019 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 <openssl/core.h>
11 #include <openssl/core_numbers.h>
12 #include <openssl/params.h>
13 #include <openssl/opensslv.h>
14 #include "internal/cryptlib_int.h"
15 #include "internal/nelem.h"
16 #include "internal/thread_once.h"
17 #include "internal/provider.h"
18 #include "internal/refcount.h"
19 #include "provider_local.h"
20
21 static OSSL_PROVIDER *provider_new(const char *name,
22                                    OSSL_provider_init_fn *init_function);
23
24 /*-
25  * Provider Object structure
26  * =========================
27  */
28
29 typedef struct {
30     char *name;
31     char *value;
32 } INFOPAIR;
33 DEFINE_STACK_OF(INFOPAIR)
34
35 struct provider_store_st;        /* Forward declaration */
36
37 struct ossl_provider_st {
38     /* Flag bits */
39     unsigned int flag_initialized:1;
40     unsigned int flag_fallback:1;
41
42     /* OpenSSL library side data */
43     CRYPTO_REF_COUNT refcnt;
44     CRYPTO_RWLOCK *refcnt_lock;  /* For the ref counter */
45     char *name;
46     char *path;
47     DSO *module;
48     OSSL_provider_init_fn *init_function;
49     STACK_OF(INFOPAIR) *parameters;
50     OPENSSL_CTX *libctx; /* The library context this instance is in */
51     struct provider_store_st *store; /* The store this instance belongs to */
52 #ifndef FIPS_MODE
53     /*
54      * In the FIPS module inner provider, this isn't needed, since the
55      * error upcalls are always direct calls to the outer provider.
56      */
57     int error_lib;     /* ERR library number, one for each provider */
58 # ifndef OPENSSL_NO_ERR
59     ERR_STRING_DATA *error_strings; /* Copy of what the provider gives us */
60 # endif
61 #endif
62
63     /* Provider side functions */
64     OSSL_provider_teardown_fn *teardown;
65     OSSL_provider_get_param_types_fn *get_param_types;
66     OSSL_provider_get_params_fn *get_params;
67     OSSL_provider_query_operation_fn *query_operation;
68
69     /* Provider side data */
70     void *provctx;
71 };
72 DEFINE_STACK_OF(OSSL_PROVIDER)
73
74 static int ossl_provider_cmp(const OSSL_PROVIDER * const *a,
75                              const OSSL_PROVIDER * const *b)
76 {
77     return strcmp((*a)->name, (*b)->name);
78 }
79
80 /*-
81  * Provider Object store
82  * =====================
83  *
84  * The Provider Object store is a library context object, and therefore needs
85  * an index.
86  */
87
88 struct provider_store_st {
89     STACK_OF(OSSL_PROVIDER) *providers;
90     CRYPTO_RWLOCK *lock;
91     unsigned int use_fallbacks:1;
92 };
93
94 static void provider_store_free(void *vstore)
95 {
96     struct provider_store_st *store = vstore;
97
98     if (store == NULL)
99         return;
100     sk_OSSL_PROVIDER_pop_free(store->providers, ossl_provider_free);
101     CRYPTO_THREAD_lock_free(store->lock);
102     OPENSSL_free(store);
103 }
104
105 static void *provider_store_new(OPENSSL_CTX *ctx)
106 {
107     struct provider_store_st *store = OPENSSL_zalloc(sizeof(*store));
108     const struct predefined_providers_st *p = NULL;
109
110     if (store == NULL
111         || (store->providers = sk_OSSL_PROVIDER_new(ossl_provider_cmp)) == NULL
112         || (store->lock = CRYPTO_THREAD_lock_new()) == NULL) {
113         provider_store_free(store);
114         return NULL;
115     }
116     store->use_fallbacks = 1;
117
118     for (p = predefined_providers; p->name != NULL; p++) {
119         OSSL_PROVIDER *prov = NULL;
120
121         /*
122          * We use the internal constructor directly here,
123          * otherwise we get a call loop
124          */
125         prov = provider_new(p->name, p->init);
126
127         if (prov == NULL
128             || sk_OSSL_PROVIDER_push(store->providers, prov) == 0) {
129             ossl_provider_free(prov);
130             provider_store_free(store);
131             CRYPTOerr(CRYPTO_F_PROVIDER_STORE_NEW, ERR_R_INTERNAL_ERROR);
132             return NULL;
133         }
134         prov->libctx = ctx;
135         prov->store = store;
136 #ifndef FIPS_MODE
137         prov->error_lib = ERR_get_next_error_library();
138 #endif
139         if(p->is_fallback)
140             ossl_provider_set_fallback(prov);
141     }
142
143     return store;
144 }
145
146 static const OPENSSL_CTX_METHOD provider_store_method = {
147     provider_store_new,
148     provider_store_free,
149 };
150
151 static struct provider_store_st *get_provider_store(OPENSSL_CTX *libctx)
152 {
153     struct provider_store_st *store = NULL;
154
155     store = openssl_ctx_get_data(libctx, OPENSSL_CTX_PROVIDER_STORE_INDEX,
156                                  &provider_store_method);
157     if (store == NULL)
158         CRYPTOerr(CRYPTO_F_GET_PROVIDER_STORE, ERR_R_INTERNAL_ERROR);
159     return store;
160 }
161
162 OSSL_PROVIDER *ossl_provider_find(OPENSSL_CTX *libctx, const char *name)
163 {
164     struct provider_store_st *store = NULL;
165     OSSL_PROVIDER *prov = NULL;
166
167     if ((store = get_provider_store(libctx)) != NULL) {
168         OSSL_PROVIDER tmpl = { 0, };
169         int i;
170
171         tmpl.name = (char *)name;
172         CRYPTO_THREAD_write_lock(store->lock);
173         if ((i = sk_OSSL_PROVIDER_find(store->providers, &tmpl)) == -1
174             || (prov = sk_OSSL_PROVIDER_value(store->providers, i)) == NULL
175             || !ossl_provider_up_ref(prov))
176             prov = NULL;
177         CRYPTO_THREAD_unlock(store->lock);
178     }
179
180     return prov;
181 }
182
183 /*-
184  * Provider Object methods
185  * =======================
186  */
187
188 static OSSL_PROVIDER *provider_new(const char *name,
189                                    OSSL_provider_init_fn *init_function)
190 {
191     OSSL_PROVIDER *prov = NULL;
192
193     if ((prov = OPENSSL_zalloc(sizeof(*prov))) == NULL
194 #ifndef HAVE_ATOMICS
195         || (prov->refcnt_lock = CRYPTO_THREAD_lock_new()) == NULL
196 #endif
197         || !ossl_provider_up_ref(prov) /* +1 One reference to be returned */
198         || (prov->name = OPENSSL_strdup(name)) == NULL) {
199         ossl_provider_free(prov);
200         CRYPTOerr(CRYPTO_F_PROVIDER_NEW, ERR_R_MALLOC_FAILURE);
201         return NULL;
202     }
203
204     prov->init_function = init_function;
205     return prov;
206 }
207
208 int ossl_provider_up_ref(OSSL_PROVIDER *prov)
209 {
210     int ref = 0;
211
212     if (CRYPTO_UP_REF(&prov->refcnt, &ref, prov->refcnt_lock) <= 0)
213         return 0;
214     return ref;
215 }
216
217 OSSL_PROVIDER *ossl_provider_new(OPENSSL_CTX *libctx, const char *name,
218                                  OSSL_provider_init_fn *init_function)
219 {
220     struct provider_store_st *store = NULL;
221     OSSL_PROVIDER *prov = NULL;
222
223     if ((store = get_provider_store(libctx)) == NULL)
224         return NULL;
225
226     if ((prov = ossl_provider_find(libctx, name)) != NULL) { /* refcount +1 */
227         ossl_provider_free(prov); /* refcount -1 */
228         CRYPTOerr(CRYPTO_F_OSSL_PROVIDER_NEW,
229                   CRYPTO_R_PROVIDER_ALREADY_EXISTS);
230         ERR_add_error_data(2, "name=", name);
231         return NULL;
232     }
233
234     /* provider_new() generates an error, so no need here */
235     if ((prov = provider_new(name, init_function)) == NULL)
236         return NULL;
237
238     CRYPTO_THREAD_write_lock(store->lock);
239     if (!ossl_provider_up_ref(prov)) { /* +1 One reference for the store */
240         ossl_provider_free(prov); /* -1 Reference that was to be returned */
241         prov = NULL;
242     } else if (sk_OSSL_PROVIDER_push(store->providers, prov) == 0) {
243         ossl_provider_free(prov); /* -1 Store reference */
244         ossl_provider_free(prov); /* -1 Reference that was to be returned */
245         prov = NULL;
246     } else {
247         prov->libctx = libctx;
248         prov->store = store;
249 #ifndef FIPS_MODE
250         prov->error_lib = ERR_get_next_error_library();
251 #endif
252     }
253     CRYPTO_THREAD_unlock(store->lock);
254
255     if (prov == NULL)
256         CRYPTOerr(CRYPTO_F_OSSL_PROVIDER_NEW, ERR_R_MALLOC_FAILURE);
257
258     /*
259      * At this point, the provider is only partially "loaded".  To be
260      * fully "loaded", ossl_provider_activate() must also be called.
261      */
262
263     return prov;
264 }
265
266 static void free_infopair(INFOPAIR *pair)
267 {
268     OPENSSL_free(pair->name);
269     OPENSSL_free(pair->value);
270     OPENSSL_free(pair);
271 }
272
273 void ossl_provider_free(OSSL_PROVIDER *prov)
274 {
275     if (prov != NULL) {
276         int ref = 0;
277
278         CRYPTO_DOWN_REF(&prov->refcnt, &ref, prov->refcnt_lock);
279
280         /*
281          * When the refcount drops below two, the store is the only
282          * possible reference, or it has already been taken away from
283          * the store (this may happen if a provider was activated
284          * because it's a fallback, but isn't currently used)
285          * When that happens, the provider is inactivated.
286          */
287         if (ref < 2 && prov->flag_initialized) {
288 #ifndef FIPS_MODE
289             ossl_init_thread_deregister(prov);
290 #endif
291             if (prov->teardown != NULL)
292                 prov->teardown(prov->provctx);
293 #ifndef OPENSSL_NO_ERR
294 # ifndef FIPS_MODE
295             if (prov->error_strings != NULL) {
296                 ERR_unload_strings(prov->error_lib, prov->error_strings);
297                 OPENSSL_free(prov->error_strings);
298                 prov->error_strings = NULL;
299             }
300 # endif
301 #endif
302             prov->flag_initialized = 0;
303         }
304
305         /*
306          * When the refcount drops to zero, it has been taken out of
307          * the store.  All we have to do here is clean it out.
308          */
309         if (ref == 0) {
310 #ifndef FIPS_MODE
311             DSO_free(prov->module);
312 #endif
313             OPENSSL_free(prov->name);
314             OPENSSL_free(prov->path);
315             sk_INFOPAIR_pop_free(prov->parameters, free_infopair);
316 #ifndef HAVE_ATOMICS
317             CRYPTO_THREAD_lock_free(prov->refcnt_lock);
318 #endif
319             OPENSSL_free(prov);
320         }
321     }
322 }
323
324 /* Setters */
325 int ossl_provider_set_module_path(OSSL_PROVIDER *prov, const char *module_path)
326 {
327     OPENSSL_free(prov->path);
328     if (module_path == NULL)
329         return 1;
330     if ((prov->path = OPENSSL_strdup(module_path)) != NULL)
331         return 1;
332     CRYPTOerr(CRYPTO_F_OSSL_PROVIDER_SET_MODULE_PATH, ERR_R_MALLOC_FAILURE);
333     return 0;
334 }
335
336 int ossl_provider_add_parameter(OSSL_PROVIDER *prov,
337                                 const char *name, const char *value)
338 {
339     INFOPAIR *pair = NULL;
340
341     if ((pair = OPENSSL_zalloc(sizeof(*pair))) != NULL
342         && (prov->parameters != NULL
343             || (prov->parameters = sk_INFOPAIR_new_null()) != NULL)
344         && (pair->name = OPENSSL_strdup(name)) != NULL
345         && (pair->value = OPENSSL_strdup(value)) != NULL
346         && sk_INFOPAIR_push(prov->parameters, pair) > 0)
347         return 1;
348
349     if (pair != NULL) {
350         OPENSSL_free(pair->name);
351         OPENSSL_free(pair->value);
352         OPENSSL_free(pair);
353     }
354     CRYPTOerr(CRYPTO_F_OSSL_PROVIDER_ADD_PARAMETER, ERR_R_MALLOC_FAILURE);
355     return 0;
356 }
357
358 /*
359  * Provider activation.
360  *
361  * What "activation" means depends on the provider form; for built in
362  * providers (in the library or the application alike), the provider
363  * can already be considered to be loaded, all that's needed is to
364  * initialize it.  However, for dynamically loadable provider modules,
365  * we must first load that module.
366  *
367  * Built in modules are distinguished from dynamically loaded modules
368  * with an already assigned init function.
369  */
370 static const OSSL_DISPATCH *core_dispatch; /* Define further down */
371
372 /*
373  * Internal version that doesn't affect the store flags, and thereby avoid
374  * locking.  Direct callers must remember to set the store flags when
375  * appropriate.
376  */
377 static int provider_activate(OSSL_PROVIDER *prov)
378 {
379     const OSSL_DISPATCH *provider_dispatch = NULL;
380 #ifndef OPENSSL_NO_ERR
381 # ifndef FIPS_MODE
382     OSSL_provider_get_reason_strings_fn *p_get_reason_strings = NULL;
383 # endif
384 #endif
385
386     if (prov->flag_initialized)
387         return 1;
388
389     /*
390      * If the init function isn't set, it indicates that this provider is
391      * a loadable module.
392      */
393     if (prov->init_function == NULL) {
394 #ifdef FIPS_MODE
395         return 0;
396 #else
397         if (prov->module == NULL) {
398             char *allocated_path = NULL;
399             const char *module_path = NULL;
400             char *merged_path = NULL;
401             const char *load_dir = ossl_safe_getenv("OPENSSL_MODULES");
402
403             if ((prov->module = DSO_new()) == NULL) {
404                 /* DSO_new() generates an error already */
405                 return 0;
406             }
407
408             if (load_dir == NULL)
409                 load_dir = MODULESDIR;
410
411             DSO_ctrl(prov->module, DSO_CTRL_SET_FLAGS,
412                      DSO_FLAG_NAME_TRANSLATION_EXT_ONLY, NULL);
413
414             module_path = prov->path;
415             if (module_path == NULL)
416                 module_path = allocated_path =
417                     DSO_convert_filename(prov->module, prov->name);
418             if (module_path != NULL)
419                 merged_path = DSO_merge(prov->module, module_path, load_dir);
420
421             if (merged_path == NULL
422                 || (DSO_load(prov->module, merged_path, NULL, 0)) == NULL) {
423                 DSO_free(prov->module);
424                 prov->module = NULL;
425             }
426
427             OPENSSL_free(merged_path);
428             OPENSSL_free(allocated_path);
429         }
430
431         if (prov->module != NULL)
432             prov->init_function = (OSSL_provider_init_fn *)
433                 DSO_bind_func(prov->module, "OSSL_provider_init");
434 #endif
435     }
436
437     /* Call the initialise function for the provider. */
438     if (prov->init_function == NULL
439         || !prov->init_function(prov, core_dispatch, &provider_dispatch,
440                                 &prov->provctx)) {
441         CRYPTOerr(CRYPTO_F_PROVIDER_ACTIVATE, ERR_R_INIT_FAIL);
442         ERR_add_error_data(2, "name=", prov->name);
443 #ifndef FIPS_MODE
444         DSO_free(prov->module);
445         prov->module = NULL;
446 #endif
447         return 0;
448     }
449
450     for (; provider_dispatch->function_id != 0; provider_dispatch++) {
451         switch (provider_dispatch->function_id) {
452         case OSSL_FUNC_PROVIDER_TEARDOWN:
453             prov->teardown =
454                 OSSL_get_provider_teardown(provider_dispatch);
455             break;
456         case OSSL_FUNC_PROVIDER_GET_PARAM_TYPES:
457             prov->get_param_types =
458                 OSSL_get_provider_get_param_types(provider_dispatch);
459             break;
460         case OSSL_FUNC_PROVIDER_GET_PARAMS:
461             prov->get_params =
462                 OSSL_get_provider_get_params(provider_dispatch);
463             break;
464         case OSSL_FUNC_PROVIDER_QUERY_OPERATION:
465             prov->query_operation =
466                 OSSL_get_provider_query_operation(provider_dispatch);
467             break;
468 #ifndef OPENSSL_NO_ERR
469 # ifndef FIPS_MODE
470         case OSSL_FUNC_PROVIDER_GET_REASON_STRINGS:
471             p_get_reason_strings =
472                 OSSL_get_provider_get_reason_strings(provider_dispatch);
473             break;
474 # endif
475 #endif
476         }
477     }
478
479 #ifndef OPENSSL_NO_ERR
480 # ifndef FIPS_MODE
481     if (p_get_reason_strings != NULL) {
482         const OSSL_ITEM *reasonstrings = p_get_reason_strings(prov->provctx);
483         size_t cnt, cnt2;
484
485         /*
486          * ERR_load_strings() handles ERR_STRING_DATA rather than OSSL_ITEM,
487          * although they are essentially the same type.
488          * Furthermore, ERR_load_strings() patches the array's error number
489          * with the error library number, so we need to make a copy of that
490          * array either way.
491          */
492         cnt = 1;                 /* One for the terminating item */
493         while (reasonstrings[cnt].id != 0) {
494             if (ERR_GET_LIB(reasonstrings[cnt].id) != 0)
495                 return 0;
496             cnt++;
497         }
498
499         /* Allocate one extra item for the "library" name */
500         prov->error_strings =
501             OPENSSL_zalloc(sizeof(ERR_STRING_DATA) * (cnt + 1));
502         if (prov->error_strings == NULL)
503             return 0;
504
505         /*
506          * Set the "library" name.
507          */
508         prov->error_strings[0].error = ERR_PACK(prov->error_lib, 0, 0);
509         prov->error_strings[0].string = prov->name;
510         /*
511          * Copy reasonstrings item 0..cnt-1 to prov->error_trings positions
512          * 1..cnt.
513          */
514         for (cnt2 = 1; cnt2 <= cnt; cnt2++) {
515             prov->error_strings[cnt2].error = (int)reasonstrings[cnt2-1].id;
516             prov->error_strings[cnt2].string = reasonstrings[cnt2-1].ptr;
517         }
518
519         ERR_load_strings(prov->error_lib, prov->error_strings);
520     }
521 # endif
522 #endif
523
524     /* With this flag set, this provider has become fully "loaded". */
525     prov->flag_initialized = 1;
526
527     return 1;
528 }
529
530 int ossl_provider_activate(OSSL_PROVIDER *prov)
531 {
532     if (provider_activate(prov)) {
533         CRYPTO_THREAD_write_lock(prov->store->lock);
534         prov->store->use_fallbacks = 0;
535         CRYPTO_THREAD_unlock(prov->store->lock);
536         return 1;
537     }
538
539     return 0;
540 }
541
542 void *ossl_provider_ctx(const OSSL_PROVIDER *prov)
543 {
544     return prov->provctx;
545 }
546
547
548 static int provider_forall_loaded(struct provider_store_st *store,
549                                   int *found_activated,
550                                   int (*cb)(OSSL_PROVIDER *provider,
551                                             void *cbdata),
552                                   void *cbdata)
553 {
554     int i;
555     int ret = 1;
556     int num_provs = sk_OSSL_PROVIDER_num(store->providers);
557
558     if (found_activated != NULL)
559         *found_activated = 0;
560     for (i = 0; i < num_provs; i++) {
561         OSSL_PROVIDER *prov =
562             sk_OSSL_PROVIDER_value(store->providers, i);
563
564         if (prov->flag_initialized) {
565             if (found_activated != NULL)
566                 *found_activated = 1;
567             if (!(ret = cb(prov, cbdata)))
568                 break;
569         }
570     }
571
572     return ret;
573 }
574
575 /*
576  * This function only does something once when store->use_fallbacks == 1,
577  * and then sets store->use_fallbacks = 0, so the second call and so on is
578  * effectively a no-op.
579  */
580 static void provider_activate_fallbacks(struct provider_store_st *store)
581 {
582     if (store->use_fallbacks) {
583         int num_provs = sk_OSSL_PROVIDER_num(store->providers);
584         int activated_fallback_count = 0;
585         int i;
586
587         for (i = 0; i < num_provs; i++) {
588             OSSL_PROVIDER *prov = sk_OSSL_PROVIDER_value(store->providers, i);
589
590             /*
591              * Note that we don't care if the activation succeeds or not.
592              * If it doesn't succeed, then any attempt to use any of the
593              * fallback providers will fail anyway.
594              */
595             if (prov->flag_fallback) {
596                 activated_fallback_count++;
597                 provider_activate(prov);
598             }
599         }
600
601         /*
602          * We assume that all fallbacks have been added to the store before
603          * any fallback is activated.
604          * TODO: We may have to reconsider this, IF we find ourselves adding
605          * fallbacks after any previous fallback has been activated.
606          */
607         if (activated_fallback_count > 0)
608             store->use_fallbacks = 0;
609     }
610 }
611
612 int ossl_provider_forall_loaded(OPENSSL_CTX *ctx,
613                                 int (*cb)(OSSL_PROVIDER *provider,
614                                           void *cbdata),
615                                 void *cbdata)
616 {
617     int ret = 1;
618     struct provider_store_st *store = get_provider_store(ctx);
619
620     if (store != NULL) {
621         CRYPTO_THREAD_read_lock(store->lock);
622
623         provider_activate_fallbacks(store);
624
625         /*
626          * Now, we sweep through all providers
627          */
628         ret = provider_forall_loaded(store, NULL, cb, cbdata);
629
630         CRYPTO_THREAD_unlock(store->lock);
631     }
632
633     return ret;
634 }
635
636 int ossl_provider_available(OSSL_PROVIDER *prov)
637 {
638     if (prov != NULL) {
639         CRYPTO_THREAD_read_lock(prov->store->lock);
640         provider_activate_fallbacks(prov->store);
641         CRYPTO_THREAD_unlock(prov->store->lock);
642
643         return prov->flag_initialized;
644     }
645     return 0;
646 }
647
648 /* Setters of Provider Object data */
649 int ossl_provider_set_fallback(OSSL_PROVIDER *prov)
650 {
651     if (prov == NULL)
652         return 0;
653
654     prov->flag_fallback = 1;
655     return 1;
656 }
657
658 /* Getters of Provider Object data */
659 const char *ossl_provider_name(const OSSL_PROVIDER *prov)
660 {
661     return prov->name;
662 }
663
664 const DSO *ossl_provider_dso(const OSSL_PROVIDER *prov)
665 {
666     return prov->module;
667 }
668
669 const char *ossl_provider_module_name(const OSSL_PROVIDER *prov)
670 {
671 #ifdef FIPS_MODE
672     return NULL;
673 #else
674     return DSO_get_filename(prov->module);
675 #endif
676 }
677
678 const char *ossl_provider_module_path(const OSSL_PROVIDER *prov)
679 {
680 #ifdef FIPS_MODE
681     return NULL;
682 #else
683     /* FIXME: Ensure it's a full path */
684     return DSO_get_filename(prov->module);
685 #endif
686 }
687
688 /* Wrappers around calls to the provider */
689 void ossl_provider_teardown(const OSSL_PROVIDER *prov)
690 {
691     if (prov->teardown != NULL)
692         prov->teardown(prov->provctx);
693 }
694
695 const OSSL_PARAM *ossl_provider_get_param_types(const OSSL_PROVIDER *prov)
696 {
697     return prov->get_param_types == NULL
698         ? NULL : prov->get_param_types(prov->provctx);
699 }
700
701 int ossl_provider_get_params(const OSSL_PROVIDER *prov, OSSL_PARAM params[])
702 {
703     return prov->get_params == NULL
704         ? 0 : prov->get_params(prov->provctx, params);
705 }
706
707
708 const OSSL_ALGORITHM *ossl_provider_query_operation(const OSSL_PROVIDER *prov,
709                                                     int operation_id,
710                                                     int *no_cache)
711 {
712     return prov->query_operation(prov->provctx, operation_id, no_cache);
713 }
714
715 /*-
716  * Core functions for the provider
717  * ===============================
718  *
719  * This is the set of functions that the core makes available to the provider
720  */
721
722 /*
723  * This returns a list of Provider Object parameters with their types, for
724  * discovery.  We do not expect that many providers will use this, but one
725  * never knows.
726  */
727 static const OSSL_PARAM param_types[] = {
728     OSSL_PARAM_DEFN("openssl-verstion", OSSL_PARAM_UTF8_PTR, NULL, 0),
729     OSSL_PARAM_DEFN("provider-name", OSSL_PARAM_UTF8_PTR, NULL, 0),
730     OSSL_PARAM_END
731 };
732
733 static const OSSL_PARAM *core_get_param_types(const OSSL_PROVIDER *prov)
734 {
735     return param_types;
736 }
737
738 static int core_get_params(const OSSL_PROVIDER *prov, OSSL_PARAM params[])
739 {
740     int i;
741     OSSL_PARAM *p;
742
743     if ((p = OSSL_PARAM_locate(params, "openssl-version")) != NULL)
744         OSSL_PARAM_set_utf8_ptr(p, OPENSSL_VERSION_STR);
745     if ((p = OSSL_PARAM_locate(params, "provider-name")) != NULL)
746         OSSL_PARAM_set_utf8_ptr(p, prov->name);
747
748     if (prov->parameters == NULL)
749         return 1;
750
751     for (i = 0; i < sk_INFOPAIR_num(prov->parameters); i++) {
752         INFOPAIR *pair = sk_INFOPAIR_value(prov->parameters, i);
753
754         if ((p = OSSL_PARAM_locate(params, pair->name)) != NULL)
755             OSSL_PARAM_set_utf8_ptr(p, pair->value);
756     }
757
758     return 1;
759 }
760
761 static OSSL_core_get_library_context_fn core_get_libctx; /* Check */
762 static OPENSSL_CTX *core_get_libctx(const OSSL_PROVIDER *prov)
763 {
764     return prov->libctx;
765 }
766
767 static int core_thread_start(const OSSL_PROVIDER *prov,
768                              OSSL_thread_stop_handler_fn handfn)
769 {
770     return ossl_init_thread_start(prov, prov->provctx, handfn);
771 }
772
773 /*
774  * The FIPS module inner provider doesn't implement these.  They aren't
775  * needed there, since the FIPS module upcalls are always the outer provider
776  * ones.
777  */
778 #ifndef FIPS_MODE
779 static void core_put_error(const OSSL_PROVIDER *prov,
780                            uint32_t reason, const char *file, int line)
781 {
782     /*
783      * If the uppermost 8 bits are non-zero, it's an OpenSSL library
784      * error and will be treated as such.  Otherwise, it's a new style
785      * provider error and will be treated as such.
786      */
787     if (ERR_GET_LIB(reason) != 0) {
788         ERR_PUT_error(ERR_GET_LIB(reason),
789                       ERR_GET_FUNC(reason),
790                       ERR_GET_REASON(reason),
791                       file, line);
792     } else {
793         ERR_PUT_error(prov->error_lib, 0, (int)reason, file, line);
794     }
795 }
796
797 /*
798  * TODO(3.0) This, as well as core_put_error above, should use |prov|
799  * to select the proper library context to report in the correct error
800  * stack, at least if error stacks become tied to the library context.
801  * We cannot currently do that since there's no support for it in the
802  * ERR subsystem.
803  */
804 static void core_add_error_vdata(const OSSL_PROVIDER *prov,
805                                  int num, va_list args)
806 {
807     ERR_add_error_vdata(num, args);
808 }
809 #endif
810
811 /*
812  * Functions provided by the core.  Blank line separates "families" of related
813  * functions.
814  */
815 static const OSSL_DISPATCH core_dispatch_[] = {
816     { OSSL_FUNC_CORE_GET_PARAM_TYPES, (void (*)(void))core_get_param_types },
817     { OSSL_FUNC_CORE_GET_PARAMS, (void (*)(void))core_get_params },
818     { OSSL_FUNC_CORE_GET_LIBRARY_CONTEXT, (void (*)(void))core_get_libctx },
819     { OSSL_FUNC_CORE_THREAD_START, (void (*)(void))core_thread_start },
820 #ifndef FIPS_MODE
821     { OSSL_FUNC_CORE_PUT_ERROR, (void (*)(void))core_put_error },
822     { OSSL_FUNC_CORE_ADD_ERROR_VDATA, (void (*)(void))core_add_error_vdata },
823 #endif
824
825     { OSSL_FUNC_CRYPTO_MALLOC, (void (*)(void))CRYPTO_malloc },
826     { OSSL_FUNC_CRYPTO_ZALLOC, (void (*)(void))CRYPTO_zalloc },
827     { OSSL_FUNC_CRYPTO_FREE, (void (*)(void))CRYPTO_free },
828     { OSSL_FUNC_CRYPTO_CLEAR_FREE, (void (*)(void))CRYPTO_clear_free },
829     { OSSL_FUNC_CRYPTO_REALLOC, (void (*)(void))CRYPTO_realloc },
830     { OSSL_FUNC_CRYPTO_CLEAR_REALLOC, (void (*)(void))CRYPTO_clear_realloc },
831     { OSSL_FUNC_CRYPTO_SECURE_MALLOC, (void (*)(void))CRYPTO_secure_malloc },
832     { OSSL_FUNC_CRYPTO_SECURE_ZALLOC, (void (*)(void))CRYPTO_secure_zalloc },
833     { OSSL_FUNC_CRYPTO_SECURE_FREE, (void (*)(void))CRYPTO_secure_free },
834     { OSSL_FUNC_CRYPTO_SECURE_CLEAR_FREE,
835         (void (*)(void))CRYPTO_secure_clear_free },
836     { OSSL_FUNC_CRYPTO_SECURE_ALLOCATED,
837         (void (*)(void))CRYPTO_secure_allocated },
838     { OSSL_FUNC_OPENSSL_CLEANSE, (void (*)(void))OPENSSL_cleanse },
839
840     { 0, NULL }
841 };
842 static const OSSL_DISPATCH *core_dispatch = core_dispatch_;