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