2 * Copyright 2019-2021 The OpenSSL Project Authors. All Rights Reserved.
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
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 "internal/core.h"
26 #include "provider_local.h"
28 # include <openssl/self_test.h>
31 static OSSL_PROVIDER *provider_new(const char *name,
32 OSSL_provider_init_fn *init_function,
33 STACK_OF(INFOPAIR) *parameters);
36 * Provider Object structure
37 * =========================
43 int (*create_cb)(const OSSL_CORE_HANDLE *provider, void *cbdata);
44 int (*remove_cb)(const OSSL_CORE_HANDLE *provider, void *cbdata);
45 int (*global_props_cb)(const char *props, void *cbdata);
47 } OSSL_PROVIDER_CHILD_CB;
48 DEFINE_STACK_OF(OSSL_PROVIDER_CHILD_CB)
51 struct provider_store_st; /* Forward declaration */
53 struct ossl_provider_st {
55 unsigned int flag_initialized:1;
56 unsigned int flag_activated:1;
57 unsigned int flag_fallback:1; /* Can be used as fallback */
59 unsigned int flag_couldbechild:1;
62 /* Getting and setting the flags require synchronization */
63 CRYPTO_RWLOCK *flag_lock;
65 /* OpenSSL library side data */
66 CRYPTO_REF_COUNT refcnt;
67 CRYPTO_RWLOCK *refcnt_lock; /* For the ref counter */
72 OSSL_provider_init_fn *init_function;
73 STACK_OF(INFOPAIR) *parameters;
74 OSSL_LIB_CTX *libctx; /* The library context this instance is in */
75 struct provider_store_st *store; /* The store this instance belongs to */
78 * In the FIPS module inner provider, this isn't needed, since the
79 * error upcalls are always direct calls to the outer provider.
81 int error_lib; /* ERR library number, one for each provider */
82 # ifndef OPENSSL_NO_ERR
83 ERR_STRING_DATA *error_strings; /* Copy of what the provider gives us */
87 /* Provider side functions */
88 OSSL_FUNC_provider_teardown_fn *teardown;
89 OSSL_FUNC_provider_gettable_params_fn *gettable_params;
90 OSSL_FUNC_provider_get_params_fn *get_params;
91 OSSL_FUNC_provider_get_capabilities_fn *get_capabilities;
92 OSSL_FUNC_provider_self_test_fn *self_test;
93 OSSL_FUNC_provider_query_operation_fn *query_operation;
94 OSSL_FUNC_provider_unquery_operation_fn *unquery_operation;
97 * Cache of bit to indicate of query_operation() has been called on
98 * a specific operation or not.
100 unsigned char *operation_bits;
101 size_t operation_bits_sz;
102 CRYPTO_RWLOCK *opbits_lock;
105 /* Whether this provider is the child of some other provider */
106 const OSSL_CORE_HANDLE *handle;
107 unsigned int ischild:1;
110 /* Provider side data */
112 const OSSL_DISPATCH *dispatch;
114 DEFINE_STACK_OF(OSSL_PROVIDER)
116 static int ossl_provider_cmp(const OSSL_PROVIDER * const *a,
117 const OSSL_PROVIDER * const *b)
119 return strcmp((*a)->name, (*b)->name);
123 * Provider Object store
124 * =====================
126 * The Provider Object store is a library context object, and therefore needs
130 struct provider_store_st {
131 OSSL_LIB_CTX *libctx;
132 STACK_OF(OSSL_PROVIDER) *providers;
133 STACK_OF(OSSL_PROVIDER_CHILD_CB) *child_cbs;
134 CRYPTO_RWLOCK *default_path_lock;
137 struct provider_info_st *provinfo;
140 unsigned int use_fallbacks:1;
141 unsigned int freeing:1;
145 * provider_deactivate_free() is a wrapper around ossl_provider_deactivate()
146 * and ossl_provider_free(), called as needed.
147 * Since this is only called when the provider store is being emptied, we
148 * don't need to care about any lock.
150 static void provider_deactivate_free(OSSL_PROVIDER *prov)
152 if (prov->flag_activated)
153 ossl_provider_deactivate(prov);
154 ossl_provider_free(prov);
158 static void ossl_provider_child_cb_free(OSSL_PROVIDER_CHILD_CB *cb)
164 static void infopair_free(INFOPAIR *pair)
166 OPENSSL_free(pair->name);
167 OPENSSL_free(pair->value);
171 static INFOPAIR *infopair_copy(const INFOPAIR *src)
173 INFOPAIR *dest = OPENSSL_zalloc(sizeof(*dest));
177 if (src->name != NULL) {
178 dest->name = OPENSSL_strdup(src->name);
179 if (dest->name == NULL)
182 if (src->value != NULL) {
183 dest->value = OPENSSL_strdup(src->value);
184 if (dest->value == NULL)
189 OPENSSL_free(dest->name);
194 void ossl_provider_info_clear(struct provider_info_st *info)
196 OPENSSL_free(info->name);
197 OPENSSL_free(info->path);
198 sk_INFOPAIR_pop_free(info->parameters, infopair_free);
201 static void provider_store_free(void *vstore)
203 struct provider_store_st *store = vstore;
209 OPENSSL_free(store->default_path);
210 sk_OSSL_PROVIDER_pop_free(store->providers, provider_deactivate_free);
212 sk_OSSL_PROVIDER_CHILD_CB_pop_free(store->child_cbs,
213 ossl_provider_child_cb_free);
215 CRYPTO_THREAD_lock_free(store->default_path_lock);
216 CRYPTO_THREAD_lock_free(store->lock);
217 for (i = 0; i < store->numprovinfo; i++)
218 ossl_provider_info_clear(&store->provinfo[i]);
219 OPENSSL_free(store->provinfo);
223 static void *provider_store_new(OSSL_LIB_CTX *ctx)
225 struct provider_store_st *store = OPENSSL_zalloc(sizeof(*store));
228 || (store->providers = sk_OSSL_PROVIDER_new(ossl_provider_cmp)) == NULL
229 || (store->default_path_lock = CRYPTO_THREAD_lock_new()) == NULL
231 || (store->child_cbs = sk_OSSL_PROVIDER_CHILD_CB_new_null()) == NULL
233 || (store->lock = CRYPTO_THREAD_lock_new()) == NULL) {
234 provider_store_free(store);
238 store->use_fallbacks = 1;
243 static const OSSL_LIB_CTX_METHOD provider_store_method = {
244 /* Needs to be freed before the child provider data is freed */
245 OSSL_LIB_CTX_METHOD_PRIORITY_1,
250 static struct provider_store_st *get_provider_store(OSSL_LIB_CTX *libctx)
252 struct provider_store_st *store = NULL;
254 store = ossl_lib_ctx_get_data(libctx, OSSL_LIB_CTX_PROVIDER_STORE_INDEX,
255 &provider_store_method);
257 ERR_raise(ERR_LIB_CRYPTO, ERR_R_INTERNAL_ERROR);
261 int ossl_provider_disable_fallback_loading(OSSL_LIB_CTX *libctx)
263 struct provider_store_st *store;
265 if ((store = get_provider_store(libctx)) != NULL) {
266 if (!CRYPTO_THREAD_write_lock(store->lock))
268 store->use_fallbacks = 0;
269 CRYPTO_THREAD_unlock(store->lock);
275 #define BUILTINS_BLOCK_SIZE 10
277 int ossl_provider_info_add_to_store(OSSL_LIB_CTX *libctx,
278 const struct provider_info_st *entry)
280 struct provider_store_st *store = get_provider_store(libctx);
283 if (entry->name == NULL) {
284 ERR_raise(ERR_LIB_CRYPTO, ERR_R_PASSED_NULL_PARAMETER);
289 ERR_raise(ERR_LIB_CRYPTO, ERR_R_INTERNAL_ERROR);
293 if (!CRYPTO_THREAD_write_lock(store->lock))
295 if (store->provinfosz == 0) {
296 store->provinfo = OPENSSL_zalloc(sizeof(*store->provinfo)
297 * BUILTINS_BLOCK_SIZE);
298 if (store->provinfo == NULL) {
299 ERR_raise(ERR_LIB_CRYPTO, ERR_R_MALLOC_FAILURE);
302 store->provinfosz = BUILTINS_BLOCK_SIZE;
303 } else if (store->numprovinfo == store->provinfosz) {
304 struct provider_info_st *tmpbuiltins;
305 size_t newsz = store->provinfosz + BUILTINS_BLOCK_SIZE;
307 tmpbuiltins = OPENSSL_realloc(store->provinfo,
308 sizeof(*store->provinfo) * newsz);
309 if (tmpbuiltins == NULL) {
310 ERR_raise(ERR_LIB_CRYPTO, ERR_R_MALLOC_FAILURE);
313 store->provinfo = tmpbuiltins;
314 store->provinfosz = newsz;
316 store->provinfo[store->numprovinfo] = *entry;
317 store->numprovinfo++;
321 CRYPTO_THREAD_unlock(store->lock);
325 int OSSL_PROVIDER_add_builtin(OSSL_LIB_CTX *libctx, const char *name,
326 OSSL_provider_init_fn *init_fn)
328 struct provider_info_st entry;
330 if (name == NULL || init_fn == NULL) {
331 ERR_raise(ERR_LIB_CRYPTO, ERR_R_PASSED_NULL_PARAMETER);
334 memset(&entry, 0, sizeof(entry));
335 entry.name = OPENSSL_strdup(name);
336 if (entry.name == NULL) {
337 ERR_raise(ERR_LIB_CRYPTO, ERR_R_MALLOC_FAILURE);
340 entry.init = init_fn;
341 if (!ossl_provider_info_add_to_store(libctx, &entry)) {
342 ossl_provider_info_clear(&entry);
348 OSSL_PROVIDER *ossl_provider_find(OSSL_LIB_CTX *libctx, const char *name,
351 struct provider_store_st *store = NULL;
352 OSSL_PROVIDER *prov = NULL;
354 if ((store = get_provider_store(libctx)) != NULL) {
355 OSSL_PROVIDER tmpl = { 0, };
360 * Make sure any providers are loaded from config before we try to find
364 if (ossl_lib_ctx_is_default(libctx))
365 OPENSSL_init_crypto(OPENSSL_INIT_LOAD_CONFIG, NULL);
369 tmpl.name = (char *)name;
370 if (!CRYPTO_THREAD_read_lock(store->lock))
372 if ((i = sk_OSSL_PROVIDER_find(store->providers, &tmpl)) == -1
373 || (prov = sk_OSSL_PROVIDER_value(store->providers, i)) == NULL
374 || !ossl_provider_up_ref(prov))
376 CRYPTO_THREAD_unlock(store->lock);
383 * Provider Object methods
384 * =======================
387 static OSSL_PROVIDER *provider_new(const char *name,
388 OSSL_provider_init_fn *init_function,
389 STACK_OF(INFOPAIR) *parameters)
391 OSSL_PROVIDER *prov = NULL;
393 if ((prov = OPENSSL_zalloc(sizeof(*prov))) == NULL
395 || (prov->refcnt_lock = CRYPTO_THREAD_lock_new()) == NULL
397 || (prov->opbits_lock = CRYPTO_THREAD_lock_new()) == NULL
398 || (prov->flag_lock = CRYPTO_THREAD_lock_new()) == NULL
399 || (prov->name = OPENSSL_strdup(name)) == NULL
400 || (prov->parameters = sk_INFOPAIR_deep_copy(parameters,
402 infopair_free)) == NULL) {
403 ossl_provider_free(prov);
404 ERR_raise(ERR_LIB_CRYPTO, ERR_R_MALLOC_FAILURE);
408 prov->refcnt = 1; /* 1 One reference to be returned */
409 prov->init_function = init_function;
411 prov->flag_couldbechild = 1;
417 int ossl_provider_up_ref(OSSL_PROVIDER *prov)
421 if (CRYPTO_UP_REF(&prov->refcnt, &ref, prov->refcnt_lock) <= 0)
426 if (!ossl_provider_up_ref_parent(prov, 0)) {
427 ossl_provider_free(prov);
437 static int provider_up_ref_intern(OSSL_PROVIDER *prov, int activate)
440 return ossl_provider_activate(prov, 0, 1);
442 return ossl_provider_up_ref(prov);
445 static int provider_free_intern(OSSL_PROVIDER *prov, int deactivate)
448 return ossl_provider_deactivate(prov);
450 ossl_provider_free(prov);
455 OSSL_PROVIDER *ossl_provider_new(OSSL_LIB_CTX *libctx, const char *name,
456 OSSL_provider_init_fn *init_function,
459 struct provider_store_st *store = NULL;
460 struct provider_info_st template;
461 OSSL_PROVIDER *prov = NULL;
463 if ((store = get_provider_store(libctx)) == NULL)
466 if ((prov = ossl_provider_find(libctx, name,
467 noconfig)) != NULL) { /* refcount +1 */
468 ossl_provider_free(prov); /* refcount -1 */
469 ERR_raise_data(ERR_LIB_CRYPTO, CRYPTO_R_PROVIDER_ALREADY_EXISTS,
474 memset(&template, 0, sizeof(template));
475 if (init_function == NULL) {
476 const struct provider_info_st *p;
479 /* Check if this is a predefined builtin provider */
480 for (p = ossl_predefined_providers; p->name != NULL; p++) {
481 if (strcmp(p->name, name) == 0) {
486 if (p->name == NULL) {
487 /* Check if this is a user added builtin provider */
488 if (!CRYPTO_THREAD_read_lock(store->lock))
490 for (i = 0, p = store->provinfo; i < store->numprovinfo; p++, i++) {
491 if (strcmp(p->name, name) == 0) {
496 CRYPTO_THREAD_unlock(store->lock);
499 template.init = init_function;
502 /* provider_new() generates an error, so no need here */
503 if ((prov = provider_new(name, template.init, template.parameters)) == NULL)
506 prov->libctx = libctx;
509 prov->error_lib = ERR_get_next_error_library();
512 if (!CRYPTO_THREAD_write_lock(store->lock))
514 if (!ossl_provider_up_ref(prov)) { /* +1 One reference for the store */
515 ossl_provider_free(prov); /* -1 Reference that was to be returned */
517 } else if (sk_OSSL_PROVIDER_push(store->providers, prov) == 0) {
518 ossl_provider_free(prov); /* -1 Store reference */
519 ossl_provider_free(prov); /* -1 Reference that was to be returned */
522 CRYPTO_THREAD_unlock(store->lock);
525 ERR_raise(ERR_LIB_CRYPTO, ERR_R_MALLOC_FAILURE);
528 * At this point, the provider is only partially "loaded". To be
529 * fully "loaded", ossl_provider_activate() must also be called.
535 void ossl_provider_free(OSSL_PROVIDER *prov)
540 CRYPTO_DOWN_REF(&prov->refcnt, &ref, prov->refcnt_lock);
543 * When the refcount drops to zero, we clean up the provider.
544 * Note that this also does teardown, which may seem late,
545 * considering that init happens on first activation. However,
546 * there may be other structures hanging on to the provider after
547 * the last deactivation and may therefore need full access to the
548 * provider's services. Therefore, we deinit late.
551 if (prov->flag_initialized) {
552 ossl_provider_teardown(prov);
553 #ifndef OPENSSL_NO_ERR
555 if (prov->error_strings != NULL) {
556 ERR_unload_strings(prov->error_lib, prov->error_strings);
557 OPENSSL_free(prov->error_strings);
558 prov->error_strings = NULL;
562 OPENSSL_free(prov->operation_bits);
563 prov->operation_bits = NULL;
564 prov->operation_bits_sz = 0;
565 prov->flag_initialized = 0;
570 * We deregister thread handling whether or not the provider was
571 * initialized. If init was attempted but was not successful then
572 * the provider may still have registered a thread handler.
574 ossl_init_thread_deregister(prov);
575 DSO_free(prov->module);
577 OPENSSL_free(prov->name);
578 OPENSSL_free(prov->path);
579 sk_INFOPAIR_pop_free(prov->parameters, infopair_free);
580 CRYPTO_THREAD_lock_free(prov->opbits_lock);
581 CRYPTO_THREAD_lock_free(prov->flag_lock);
583 CRYPTO_THREAD_lock_free(prov->refcnt_lock);
588 else if (prov->ischild) {
589 ossl_provider_free_parent(prov, 0);
596 int ossl_provider_set_module_path(OSSL_PROVIDER *prov, const char *module_path)
598 OPENSSL_free(prov->path);
600 if (module_path == NULL)
602 if ((prov->path = OPENSSL_strdup(module_path)) != NULL)
604 ERR_raise(ERR_LIB_CRYPTO, ERR_R_MALLOC_FAILURE);
608 static int infopair_add(STACK_OF(INFOPAIR) **infopairsk, const char *name,
611 INFOPAIR *pair = NULL;
613 if ((pair = OPENSSL_zalloc(sizeof(*pair))) != NULL
614 && (*infopairsk != NULL
615 || (*infopairsk = sk_INFOPAIR_new_null()) != NULL)
616 && (pair->name = OPENSSL_strdup(name)) != NULL
617 && (pair->value = OPENSSL_strdup(value)) != NULL
618 && sk_INFOPAIR_push(*infopairsk, pair) > 0)
622 OPENSSL_free(pair->name);
623 OPENSSL_free(pair->value);
626 ERR_raise(ERR_LIB_CRYPTO, ERR_R_MALLOC_FAILURE);
630 int ossl_provider_add_parameter(OSSL_PROVIDER *prov,
631 const char *name, const char *value)
633 return infopair_add(&prov->parameters, name, value);
636 int ossl_provider_info_add_parameter(struct provider_info_st *provinfo,
640 return infopair_add(&provinfo->parameters, name, value);
644 * Provider activation.
646 * What "activation" means depends on the provider form; for built in
647 * providers (in the library or the application alike), the provider
648 * can already be considered to be loaded, all that's needed is to
649 * initialize it. However, for dynamically loadable provider modules,
650 * we must first load that module.
652 * Built in modules are distinguished from dynamically loaded modules
653 * with an already assigned init function.
655 static const OSSL_DISPATCH *core_dispatch; /* Define further down */
657 int OSSL_PROVIDER_set_default_search_path(OSSL_LIB_CTX *libctx,
660 struct provider_store_st *store;
664 p = OPENSSL_strdup(path);
666 ERR_raise(ERR_LIB_CRYPTO, ERR_R_MALLOC_FAILURE);
670 if ((store = get_provider_store(libctx)) != NULL
671 && CRYPTO_THREAD_write_lock(store->default_path_lock)) {
672 OPENSSL_free(store->default_path);
673 store->default_path = p;
674 CRYPTO_THREAD_unlock(store->default_path_lock);
682 * Internal version that doesn't affect the store flags, and thereby avoid
683 * locking. Direct callers must remember to set the store flags when
686 static int provider_init(OSSL_PROVIDER *prov, int flag_lock)
688 const OSSL_DISPATCH *provider_dispatch = NULL;
689 void *tmp_provctx = NULL; /* safety measure */
690 #ifndef OPENSSL_NO_ERR
692 OSSL_FUNC_provider_get_reason_strings_fn *p_get_reason_strings = NULL;
698 * The flag lock is used to lock init, not only because the flag is
699 * checked here and set at the end, but also because this function
700 * modifies a number of things in the provider structure that this
701 * function needs to perform under lock anyway.
703 if (flag_lock && !CRYPTO_THREAD_write_lock(prov->flag_lock))
705 if (prov->flag_initialized) {
711 * If the init function isn't set, it indicates that this provider is
714 if (prov->init_function == NULL) {
718 if (prov->module == NULL) {
719 char *allocated_path = NULL;
720 const char *module_path = NULL;
721 char *merged_path = NULL;
722 const char *load_dir = NULL;
723 char *allocated_load_dir = NULL;
724 struct provider_store_st *store;
726 if ((prov->module = DSO_new()) == NULL) {
727 /* DSO_new() generates an error already */
731 if ((store = get_provider_store(prov->libctx)) == NULL
732 || !CRYPTO_THREAD_read_lock(store->default_path_lock))
735 if (store->default_path != NULL) {
736 allocated_load_dir = OPENSSL_strdup(store->default_path);
737 CRYPTO_THREAD_unlock(store->default_path_lock);
738 if (allocated_load_dir == NULL) {
739 ERR_raise(ERR_LIB_CRYPTO, ERR_R_MALLOC_FAILURE);
742 load_dir = allocated_load_dir;
744 CRYPTO_THREAD_unlock(store->default_path_lock);
747 if (load_dir == NULL) {
748 load_dir = ossl_safe_getenv("OPENSSL_MODULES");
749 if (load_dir == NULL)
750 load_dir = MODULESDIR;
753 DSO_ctrl(prov->module, DSO_CTRL_SET_FLAGS,
754 DSO_FLAG_NAME_TRANSLATION_EXT_ONLY, NULL);
756 module_path = prov->path;
757 if (module_path == NULL)
758 module_path = allocated_path =
759 DSO_convert_filename(prov->module, prov->name);
760 if (module_path != NULL)
761 merged_path = DSO_merge(prov->module, module_path, load_dir);
763 if (merged_path == NULL
764 || (DSO_load(prov->module, merged_path, NULL, 0)) == NULL) {
765 DSO_free(prov->module);
769 OPENSSL_free(merged_path);
770 OPENSSL_free(allocated_path);
771 OPENSSL_free(allocated_load_dir);
774 if (prov->module != NULL)
775 prov->init_function = (OSSL_provider_init_fn *)
776 DSO_bind_func(prov->module, "OSSL_provider_init");
780 /* Call the initialise function for the provider. */
781 if (prov->init_function == NULL
782 || !prov->init_function((OSSL_CORE_HANDLE *)prov, core_dispatch,
783 &provider_dispatch, &tmp_provctx)) {
784 ERR_raise_data(ERR_LIB_CRYPTO, ERR_R_INIT_FAIL,
785 "name=%s", prov->name);
788 prov->provctx = tmp_provctx;
789 prov->dispatch = provider_dispatch;
791 prov->flag_couldbechild = 0;
794 for (; provider_dispatch->function_id != 0; provider_dispatch++) {
795 switch (provider_dispatch->function_id) {
796 case OSSL_FUNC_PROVIDER_TEARDOWN:
798 OSSL_FUNC_provider_teardown(provider_dispatch);
800 case OSSL_FUNC_PROVIDER_GETTABLE_PARAMS:
801 prov->gettable_params =
802 OSSL_FUNC_provider_gettable_params(provider_dispatch);
804 case OSSL_FUNC_PROVIDER_GET_PARAMS:
806 OSSL_FUNC_provider_get_params(provider_dispatch);
808 case OSSL_FUNC_PROVIDER_SELF_TEST:
810 OSSL_FUNC_provider_self_test(provider_dispatch);
812 case OSSL_FUNC_PROVIDER_GET_CAPABILITIES:
813 prov->get_capabilities =
814 OSSL_FUNC_provider_get_capabilities(provider_dispatch);
816 case OSSL_FUNC_PROVIDER_QUERY_OPERATION:
817 prov->query_operation =
818 OSSL_FUNC_provider_query_operation(provider_dispatch);
820 case OSSL_FUNC_PROVIDER_UNQUERY_OPERATION:
821 prov->unquery_operation =
822 OSSL_FUNC_provider_unquery_operation(provider_dispatch);
824 #ifndef OPENSSL_NO_ERR
826 case OSSL_FUNC_PROVIDER_GET_REASON_STRINGS:
827 p_get_reason_strings =
828 OSSL_FUNC_provider_get_reason_strings(provider_dispatch);
835 #ifndef OPENSSL_NO_ERR
837 if (p_get_reason_strings != NULL) {
838 const OSSL_ITEM *reasonstrings = p_get_reason_strings(prov->provctx);
842 * ERR_load_strings() handles ERR_STRING_DATA rather than OSSL_ITEM,
843 * although they are essentially the same type.
844 * Furthermore, ERR_load_strings() patches the array's error number
845 * with the error library number, so we need to make a copy of that
849 while (reasonstrings[cnt].id != 0) {
850 if (ERR_GET_LIB(reasonstrings[cnt].id) != 0)
854 cnt++; /* One for the terminating item */
856 /* Allocate one extra item for the "library" name */
857 prov->error_strings =
858 OPENSSL_zalloc(sizeof(ERR_STRING_DATA) * (cnt + 1));
859 if (prov->error_strings == NULL)
863 * Set the "library" name.
865 prov->error_strings[0].error = ERR_PACK(prov->error_lib, 0, 0);
866 prov->error_strings[0].string = prov->name;
868 * Copy reasonstrings item 0..cnt-1 to prov->error_trings positions
871 for (cnt2 = 1; cnt2 <= cnt; cnt2++) {
872 prov->error_strings[cnt2].error = (int)reasonstrings[cnt2-1].id;
873 prov->error_strings[cnt2].string = reasonstrings[cnt2-1].ptr;
876 ERR_load_strings(prov->error_lib, prov->error_strings);
881 /* With this flag set, this provider has become fully "loaded". */
882 prov->flag_initialized = 1;
887 CRYPTO_THREAD_unlock(prov->flag_lock);
892 * Deactivate a provider.
893 * Return -1 on failure and the activation count on success
895 static int provider_deactivate(OSSL_PROVIDER *prov)
898 struct provider_store_st *store;
900 if (!ossl_assert(prov != NULL))
903 store = get_provider_store(prov->libctx);
907 if (!CRYPTO_THREAD_read_lock(store->lock))
909 if (!CRYPTO_THREAD_write_lock(prov->flag_lock)) {
910 CRYPTO_THREAD_unlock(store->lock);
915 if (prov->activatecnt == 2 && prov->ischild) {
917 * We have had a direct activation in this child libctx so we need to
918 * now down the ref count in the parent provider.
920 ossl_provider_free_parent(prov, 1);
924 if ((count = --prov->activatecnt) < 1) {
925 prov->flag_activated = 0;
928 int i, max = sk_OSSL_PROVIDER_CHILD_CB_num(store->child_cbs);
929 OSSL_PROVIDER_CHILD_CB *child_cb;
931 for (i = 0; i < max; i++) {
932 child_cb = sk_OSSL_PROVIDER_CHILD_CB_value(store->child_cbs, i);
933 child_cb->remove_cb((OSSL_CORE_HANDLE *)prov, child_cb->cbdata);
939 CRYPTO_THREAD_unlock(prov->flag_lock);
940 CRYPTO_THREAD_unlock(store->lock);
942 /* We don't deinit here, that's done in ossl_provider_free() */
947 * Activate a provider.
948 * Return -1 on failure and the activation count on success
950 static int provider_activate(OSSL_PROVIDER *prov, int lock, int upcalls)
954 if (provider_init(prov, lock)) {
956 struct provider_store_st *store;
958 store = get_provider_store(prov->libctx);
962 if (lock && !CRYPTO_THREAD_read_lock(store->lock))
965 if (lock && !CRYPTO_THREAD_write_lock(prov->flag_lock)) {
966 CRYPTO_THREAD_unlock(store->lock);
971 if (prov->ischild && upcalls)
972 ret = ossl_provider_up_ref_parent(prov, 1);
976 count = ++prov->activatecnt;
977 prov->flag_activated = 1;
980 if (prov->activatecnt == 1) {
981 OSSL_PROVIDER_CHILD_CB *child_cb;
984 max = sk_OSSL_PROVIDER_CHILD_CB_num(store->child_cbs);
985 for (i = 0; i < max; i++) {
987 * This is newly activated (activatecnt == 1), so we need to
988 * create child providers as necessary.
990 child_cb = sk_OSSL_PROVIDER_CHILD_CB_value(store->child_cbs,
992 ret &= child_cb->create_cb((OSSL_CORE_HANDLE *)prov,
1000 CRYPTO_THREAD_unlock(prov->flag_lock);
1001 CRYPTO_THREAD_unlock(store->lock);
1010 static int provider_flush_store_cache(const OSSL_PROVIDER *prov)
1012 struct provider_store_st *store;
1015 if ((store = get_provider_store(prov->libctx)) == NULL)
1018 if (!CRYPTO_THREAD_read_lock(store->lock))
1020 freeing = store->freeing;
1021 CRYPTO_THREAD_unlock(store->lock);
1024 return evp_method_store_flush(prov->libctx);
1028 int ossl_provider_activate(OSSL_PROVIDER *prov, int retain_fallbacks,
1035 if ((count = provider_activate(prov, 1, upcalls)) > 0) {
1036 if (!retain_fallbacks) {
1037 if (!CRYPTO_THREAD_write_lock(prov->store->lock)) {
1038 provider_deactivate(prov);
1041 prov->store->use_fallbacks = 0;
1042 CRYPTO_THREAD_unlock(prov->store->lock);
1044 return count == 1 ? provider_flush_store_cache(prov) : 1;
1049 int ossl_provider_deactivate(OSSL_PROVIDER *prov)
1053 if (prov == NULL || (count = provider_deactivate(prov)) < 0)
1055 return count == 0 ? provider_flush_store_cache(prov) : 1;
1058 void *ossl_provider_ctx(const OSSL_PROVIDER *prov)
1060 return prov->provctx;
1064 * This function only does something once when store->use_fallbacks == 1,
1065 * and then sets store->use_fallbacks = 0, so the second call and so on is
1066 * effectively a no-op.
1068 static int provider_activate_fallbacks(struct provider_store_st *store)
1071 int activated_fallback_count = 0;
1073 const struct provider_info_st *p;
1075 if (!CRYPTO_THREAD_read_lock(store->lock))
1077 use_fallbacks = store->use_fallbacks;
1078 CRYPTO_THREAD_unlock(store->lock);
1082 if (!CRYPTO_THREAD_write_lock(store->lock))
1084 /* Check again, just in case another thread changed it */
1085 use_fallbacks = store->use_fallbacks;
1086 if (!use_fallbacks) {
1087 CRYPTO_THREAD_unlock(store->lock);
1091 for (p = ossl_predefined_providers; p->name != NULL; p++) {
1092 OSSL_PROVIDER *prov = NULL;
1094 if (!p->is_fallback)
1097 * We use the internal constructor directly here,
1098 * otherwise we get a call loop
1100 prov = provider_new(p->name, p->init, NULL);
1103 prov->libctx = store->libctx;
1104 prov->store = store;
1106 prov->error_lib = ERR_get_next_error_library();
1110 * We are calling provider_activate while holding the store lock. This
1111 * means the init function will be called while holding a lock. Normally
1112 * we try to avoid calling a user callback while holding a lock.
1113 * However, fallbacks are never third party providers so we accept this.
1115 if (provider_activate(prov, 0, 0) < 0
1116 || sk_OSSL_PROVIDER_push(store->providers, prov) == 0) {
1117 ossl_provider_free(prov);
1120 activated_fallback_count++;
1123 if (activated_fallback_count > 0) {
1124 store->use_fallbacks = 0;
1128 CRYPTO_THREAD_unlock(store->lock);
1132 int ossl_provider_doall_activated(OSSL_LIB_CTX *ctx,
1133 int (*cb)(OSSL_PROVIDER *provider,
1137 int ret = 0, curr, max;
1138 struct provider_store_st *store = get_provider_store(ctx);
1139 STACK_OF(OSSL_PROVIDER) *provs = NULL;
1143 * Make sure any providers are loaded from config before we try to use
1146 if (ossl_lib_ctx_is_default(ctx))
1147 OPENSSL_init_crypto(OPENSSL_INIT_LOAD_CONFIG, NULL);
1152 if (!provider_activate_fallbacks(store))
1156 * Under lock, grab a copy of the provider list and up_ref each
1157 * provider so that they don't disappear underneath us.
1159 if (!CRYPTO_THREAD_read_lock(store->lock))
1161 provs = sk_OSSL_PROVIDER_dup(store->providers);
1162 if (provs == NULL) {
1163 CRYPTO_THREAD_unlock(store->lock);
1166 max = sk_OSSL_PROVIDER_num(provs);
1168 * We work backwards through the stack so that we can safely delete items
1171 for (curr = max - 1; curr >= 0; curr--) {
1172 OSSL_PROVIDER *prov = sk_OSSL_PROVIDER_value(provs, curr);
1174 if (!CRYPTO_THREAD_write_lock(prov->flag_lock))
1176 if (prov->flag_activated) {
1177 if (!ossl_provider_up_ref(prov)){
1178 CRYPTO_THREAD_unlock(prov->flag_lock);
1182 * It's already activated, but we up the activated count to ensure
1183 * it remains activated until after we've called the user callback.
1185 if (provider_activate(prov, 0, 1) < 0) {
1186 ossl_provider_free(prov);
1187 CRYPTO_THREAD_unlock(prov->flag_lock);
1191 sk_OSSL_PROVIDER_delete(provs, curr);
1194 CRYPTO_THREAD_unlock(prov->flag_lock);
1196 CRYPTO_THREAD_unlock(store->lock);
1199 * Now, we sweep through all providers not under lock
1201 for (curr = 0; curr < max; curr++) {
1202 OSSL_PROVIDER *prov = sk_OSSL_PROVIDER_value(provs, curr);
1204 if (!cb(prov, cbdata))
1213 CRYPTO_THREAD_unlock(store->lock);
1216 * The pop_free call doesn't do what we want on an error condition. We
1217 * either start from the first item in the stack, or part way through if
1218 * we only processed some of the items.
1220 for (curr++; curr < max; curr++) {
1221 OSSL_PROVIDER *prov = sk_OSSL_PROVIDER_value(provs, curr);
1223 provider_deactivate(prov);
1224 ossl_provider_free(prov);
1226 sk_OSSL_PROVIDER_free(provs);
1230 int OSSL_PROVIDER_available(OSSL_LIB_CTX *libctx, const char *name)
1232 OSSL_PROVIDER *prov = NULL;
1234 struct provider_store_st *store = get_provider_store(libctx);
1236 if (store == NULL || !provider_activate_fallbacks(store))
1239 prov = ossl_provider_find(libctx, name, 0);
1241 if (!CRYPTO_THREAD_read_lock(prov->flag_lock))
1243 available = prov->flag_activated;
1244 CRYPTO_THREAD_unlock(prov->flag_lock);
1245 ossl_provider_free(prov);
1250 /* Setters of Provider Object data */
1251 int ossl_provider_set_fallback(OSSL_PROVIDER *prov)
1256 prov->flag_fallback = 1;
1260 /* Getters of Provider Object data */
1261 const char *ossl_provider_name(const OSSL_PROVIDER *prov)
1266 const DSO *ossl_provider_dso(const OSSL_PROVIDER *prov)
1268 return prov->module;
1271 const char *ossl_provider_module_name(const OSSL_PROVIDER *prov)
1276 return DSO_get_filename(prov->module);
1280 const char *ossl_provider_module_path(const OSSL_PROVIDER *prov)
1285 /* FIXME: Ensure it's a full path */
1286 return DSO_get_filename(prov->module);
1290 void *ossl_provider_prov_ctx(const OSSL_PROVIDER *prov)
1293 return prov->provctx;
1298 const OSSL_DISPATCH *ossl_provider_get0_dispatch(const OSSL_PROVIDER *prov)
1301 return prov->dispatch;
1306 OSSL_LIB_CTX *ossl_provider_libctx(const OSSL_PROVIDER *prov)
1308 return prov != NULL ? prov->libctx : NULL;
1311 /* Wrappers around calls to the provider */
1312 void ossl_provider_teardown(const OSSL_PROVIDER *prov)
1314 if (prov->teardown != NULL
1319 prov->teardown(prov->provctx);
1322 const OSSL_PARAM *ossl_provider_gettable_params(const OSSL_PROVIDER *prov)
1324 return prov->gettable_params == NULL
1325 ? NULL : prov->gettable_params(prov->provctx);
1328 int ossl_provider_get_params(const OSSL_PROVIDER *prov, OSSL_PARAM params[])
1330 return prov->get_params == NULL
1331 ? 0 : prov->get_params(prov->provctx, params);
1334 int ossl_provider_self_test(const OSSL_PROVIDER *prov)
1338 if (prov->self_test == NULL)
1340 ret = prov->self_test(prov->provctx);
1342 (void)provider_flush_store_cache(prov);
1346 int ossl_provider_get_capabilities(const OSSL_PROVIDER *prov,
1347 const char *capability,
1351 return prov->get_capabilities == NULL
1352 ? 1 : prov->get_capabilities(prov->provctx, capability, cb, arg);
1355 const OSSL_ALGORITHM *ossl_provider_query_operation(const OSSL_PROVIDER *prov,
1359 const OSSL_ALGORITHM *res;
1361 if (prov->query_operation == NULL)
1363 res = prov->query_operation(prov->provctx, operation_id, no_cache);
1364 #if defined(OPENSSL_NO_CACHED_FETCH)
1365 /* Forcing the non-caching of queries */
1366 if (no_cache != NULL)
1372 void ossl_provider_unquery_operation(const OSSL_PROVIDER *prov,
1374 const OSSL_ALGORITHM *algs)
1376 if (prov->unquery_operation != NULL)
1377 prov->unquery_operation(prov->provctx, operation_id, algs);
1380 int ossl_provider_clear_all_operation_bits(OSSL_LIB_CTX *libctx)
1382 struct provider_store_st *store;
1383 OSSL_PROVIDER *provider;
1384 int i, num, res = 1;
1386 if ((store = get_provider_store(libctx)) != NULL) {
1387 if (!CRYPTO_THREAD_read_lock(store->lock))
1389 num = sk_OSSL_PROVIDER_num(store->providers);
1390 for (i = 0; i < num; i++) {
1391 provider = sk_OSSL_PROVIDER_value(store->providers, i);
1392 if (!CRYPTO_THREAD_write_lock(provider->opbits_lock)) {
1396 if (provider->operation_bits != NULL)
1397 memset(provider->operation_bits, 0,
1398 provider->operation_bits_sz);
1399 CRYPTO_THREAD_unlock(provider->opbits_lock);
1401 CRYPTO_THREAD_unlock(store->lock);
1407 int ossl_provider_set_operation_bit(OSSL_PROVIDER *provider, size_t bitnum)
1409 size_t byte = bitnum / 8;
1410 unsigned char bit = (1 << (bitnum % 8)) & 0xFF;
1412 if (!CRYPTO_THREAD_write_lock(provider->opbits_lock))
1414 if (provider->operation_bits_sz <= byte) {
1415 unsigned char *tmp = OPENSSL_realloc(provider->operation_bits,
1419 CRYPTO_THREAD_unlock(provider->opbits_lock);
1420 ERR_raise(ERR_LIB_CRYPTO, ERR_R_MALLOC_FAILURE);
1423 provider->operation_bits = tmp;
1424 memset(provider->operation_bits + provider->operation_bits_sz,
1425 '\0', byte + 1 - provider->operation_bits_sz);
1426 provider->operation_bits_sz = byte + 1;
1428 provider->operation_bits[byte] |= bit;
1429 CRYPTO_THREAD_unlock(provider->opbits_lock);
1433 int ossl_provider_test_operation_bit(OSSL_PROVIDER *provider, size_t bitnum,
1436 size_t byte = bitnum / 8;
1437 unsigned char bit = (1 << (bitnum % 8)) & 0xFF;
1439 if (!ossl_assert(result != NULL)) {
1440 ERR_raise(ERR_LIB_CRYPTO, ERR_R_PASSED_NULL_PARAMETER);
1445 if (!CRYPTO_THREAD_read_lock(provider->opbits_lock))
1447 if (provider->operation_bits_sz > byte)
1448 *result = ((provider->operation_bits[byte] & bit) != 0);
1449 CRYPTO_THREAD_unlock(provider->opbits_lock);
1454 const OSSL_CORE_HANDLE *ossl_provider_get_parent(OSSL_PROVIDER *prov)
1456 return prov->handle;
1459 int ossl_provider_is_child(const OSSL_PROVIDER *prov)
1461 return prov->ischild;
1464 int ossl_provider_set_child(OSSL_PROVIDER *prov, const OSSL_CORE_HANDLE *handle)
1466 prov->handle = handle;
1472 int ossl_provider_convert_to_child(OSSL_PROVIDER *prov,
1473 const OSSL_CORE_HANDLE *handle,
1474 OSSL_provider_init_fn *init_function)
1478 if (!CRYPTO_THREAD_write_lock(prov->store->lock))
1480 if (!CRYPTO_THREAD_write_lock(prov->flag_lock)) {
1481 CRYPTO_THREAD_unlock(prov->store->lock);
1485 * The provider could be in one of three states: (1) Already a child,
1486 * (2) Not a child (but eligible to be one), or (3) Not a child (not
1487 * eligible to be one).
1489 if (prov->flag_couldbechild) {
1490 ossl_provider_set_child(prov, handle);
1491 prov->init_function = init_function;
1493 if (prov->ischild && provider_activate(prov, 0, 0)) {
1495 prov->store->use_fallbacks = 0;
1498 CRYPTO_THREAD_unlock(prov->flag_lock);
1499 CRYPTO_THREAD_unlock(prov->store->lock);
1502 provider_flush_store_cache(prov);
1505 * We report success whether or not the provider was eligible for conversion
1506 * to a child. If its not elgibile then it has already been loaded as a non
1507 * child provider and we should keep it like that.
1512 int ossl_provider_default_props_update(OSSL_LIB_CTX *libctx, const char *props)
1515 struct provider_store_st *store = NULL;
1517 OSSL_PROVIDER_CHILD_CB *child_cb;
1519 if ((store = get_provider_store(libctx)) == NULL)
1522 if (!CRYPTO_THREAD_read_lock(store->lock))
1525 max = sk_OSSL_PROVIDER_CHILD_CB_num(store->child_cbs);
1526 for (i = 0; i < max; i++) {
1527 child_cb = sk_OSSL_PROVIDER_CHILD_CB_value(store->child_cbs, i);
1528 child_cb->global_props_cb(props, child_cb->cbdata);
1531 CRYPTO_THREAD_unlock(store->lock);
1536 static int ossl_provider_register_child_cb(const OSSL_CORE_HANDLE *handle,
1538 const OSSL_CORE_HANDLE *provider,
1541 const OSSL_CORE_HANDLE *provider,
1543 int (*global_props_cb)(
1549 * This is really an OSSL_PROVIDER that we created and cast to
1550 * OSSL_CORE_HANDLE originally. Therefore it is safe to cast it back.
1552 OSSL_PROVIDER *thisprov = (OSSL_PROVIDER *)handle;
1553 OSSL_PROVIDER *prov;
1554 OSSL_LIB_CTX *libctx = thisprov->libctx;
1555 struct provider_store_st *store = NULL;
1556 int ret = 0, i, max;
1557 OSSL_PROVIDER_CHILD_CB *child_cb;
1558 char *propsstr = NULL;
1560 if ((store = get_provider_store(libctx)) == NULL)
1563 child_cb = OPENSSL_malloc(sizeof(*child_cb));
1564 if (child_cb == NULL)
1566 child_cb->prov = thisprov;
1567 child_cb->create_cb = create_cb;
1568 child_cb->remove_cb = remove_cb;
1569 child_cb->global_props_cb = global_props_cb;
1570 child_cb->cbdata = cbdata;
1572 if (!CRYPTO_THREAD_write_lock(store->lock)) {
1573 OPENSSL_free(child_cb);
1576 propsstr = evp_get_global_properties_str(libctx, 0);
1578 if (propsstr != NULL) {
1579 global_props_cb(propsstr, cbdata);
1580 OPENSSL_free(propsstr);
1582 max = sk_OSSL_PROVIDER_num(store->providers);
1583 for (i = 0; i < max; i++) {
1584 prov = sk_OSSL_PROVIDER_value(store->providers, i);
1586 * We require register_child_cb to be called during a provider init
1587 * function. The currently initing provider will never be activated yet
1588 * and we we should not attempt to aquire the flag_lock for it.
1590 if (prov == thisprov)
1592 if (!CRYPTO_THREAD_read_lock(prov->flag_lock))
1595 * We hold the lock while calling the user callback. This means that the
1596 * user callback must be short and simple and not do anything likely to
1599 if (prov->flag_activated
1600 && !create_cb((OSSL_CORE_HANDLE *)prov, cbdata))
1602 CRYPTO_THREAD_unlock(prov->flag_lock);
1606 ret = sk_OSSL_PROVIDER_CHILD_CB_push(store->child_cbs, child_cb);
1608 if (i != max || ret <= 0) {
1609 /* Failed during creation. Remove everything we just added */
1610 for (; i >= 0; i--) {
1611 prov = sk_OSSL_PROVIDER_value(store->providers, i);
1612 remove_cb((OSSL_CORE_HANDLE *)prov, cbdata);
1614 OPENSSL_free(child_cb);
1617 CRYPTO_THREAD_unlock(store->lock);
1622 static void ossl_provider_deregister_child_cb(const OSSL_CORE_HANDLE *handle)
1625 * This is really an OSSL_PROVIDER that we created and cast to
1626 * OSSL_CORE_HANDLE originally. Therefore it is safe to cast it back.
1628 OSSL_PROVIDER *thisprov = (OSSL_PROVIDER *)handle;
1629 OSSL_LIB_CTX *libctx = thisprov->libctx;
1630 struct provider_store_st *store = NULL;
1632 OSSL_PROVIDER_CHILD_CB *child_cb;
1634 if ((store = get_provider_store(libctx)) == NULL)
1637 if (!CRYPTO_THREAD_write_lock(store->lock))
1639 max = sk_OSSL_PROVIDER_CHILD_CB_num(store->child_cbs);
1640 for (i = 0; i < max; i++) {
1641 child_cb = sk_OSSL_PROVIDER_CHILD_CB_value(store->child_cbs, i);
1642 if (child_cb->prov == thisprov) {
1643 /* Found an entry */
1644 sk_OSSL_PROVIDER_CHILD_CB_delete(store->child_cbs, i);
1645 OPENSSL_free(child_cb);
1649 CRYPTO_THREAD_unlock(store->lock);
1654 * Core functions for the provider
1655 * ===============================
1657 * This is the set of functions that the core makes available to the provider
1661 * This returns a list of Provider Object parameters with their types, for
1662 * discovery. We do not expect that many providers will use this, but one
1665 static const OSSL_PARAM param_types[] = {
1666 OSSL_PARAM_DEFN(OSSL_PROV_PARAM_CORE_VERSION, OSSL_PARAM_UTF8_PTR, NULL, 0),
1667 OSSL_PARAM_DEFN(OSSL_PROV_PARAM_CORE_PROV_NAME, OSSL_PARAM_UTF8_PTR,
1670 OSSL_PARAM_DEFN(OSSL_PROV_PARAM_CORE_MODULE_FILENAME, OSSL_PARAM_UTF8_PTR,
1677 * Forward declare all the functions that are provided aa dispatch.
1678 * This ensures that the compiler will complain if they aren't defined
1679 * with the correct signature.
1681 static OSSL_FUNC_core_gettable_params_fn core_gettable_params;
1682 static OSSL_FUNC_core_get_params_fn core_get_params;
1683 static OSSL_FUNC_core_thread_start_fn core_thread_start;
1684 static OSSL_FUNC_core_get_libctx_fn core_get_libctx;
1686 static OSSL_FUNC_core_new_error_fn core_new_error;
1687 static OSSL_FUNC_core_set_error_debug_fn core_set_error_debug;
1688 static OSSL_FUNC_core_vset_error_fn core_vset_error;
1689 static OSSL_FUNC_core_set_error_mark_fn core_set_error_mark;
1690 static OSSL_FUNC_core_clear_last_error_mark_fn core_clear_last_error_mark;
1691 static OSSL_FUNC_core_pop_error_to_mark_fn core_pop_error_to_mark;
1692 static OSSL_FUNC_core_obj_add_sigid_fn core_obj_add_sigid;
1693 static OSSL_FUNC_core_obj_create_fn core_obj_create;
1696 static const OSSL_PARAM *core_gettable_params(const OSSL_CORE_HANDLE *handle)
1701 static int core_get_params(const OSSL_CORE_HANDLE *handle, OSSL_PARAM params[])
1706 * We created this object originally and we know it is actually an
1707 * OSSL_PROVIDER *, so the cast is safe
1709 OSSL_PROVIDER *prov = (OSSL_PROVIDER *)handle;
1711 if ((p = OSSL_PARAM_locate(params, OSSL_PROV_PARAM_CORE_VERSION)) != NULL)
1712 OSSL_PARAM_set_utf8_ptr(p, OPENSSL_VERSION_STR);
1713 if ((p = OSSL_PARAM_locate(params, OSSL_PROV_PARAM_CORE_PROV_NAME)) != NULL)
1714 OSSL_PARAM_set_utf8_ptr(p, prov->name);
1717 if ((p = OSSL_PARAM_locate(params,
1718 OSSL_PROV_PARAM_CORE_MODULE_FILENAME)) != NULL)
1719 OSSL_PARAM_set_utf8_ptr(p, ossl_provider_module_path(prov));
1722 if (prov->parameters == NULL)
1725 for (i = 0; i < sk_INFOPAIR_num(prov->parameters); i++) {
1726 INFOPAIR *pair = sk_INFOPAIR_value(prov->parameters, i);
1728 if ((p = OSSL_PARAM_locate(params, pair->name)) != NULL)
1729 OSSL_PARAM_set_utf8_ptr(p, pair->value);
1734 static OPENSSL_CORE_CTX *core_get_libctx(const OSSL_CORE_HANDLE *handle)
1737 * We created this object originally and we know it is actually an
1738 * OSSL_PROVIDER *, so the cast is safe
1740 OSSL_PROVIDER *prov = (OSSL_PROVIDER *)handle;
1743 * Using ossl_provider_libctx would be wrong as that returns
1744 * NULL for |prov| == NULL and NULL libctx has a special meaning
1745 * that does not apply here. Here |prov| == NULL can happen only in
1746 * case of a coding error.
1748 assert(prov != NULL);
1749 return (OPENSSL_CORE_CTX *)prov->libctx;
1752 static int core_thread_start(const OSSL_CORE_HANDLE *handle,
1753 OSSL_thread_stop_handler_fn handfn,
1757 * We created this object originally and we know it is actually an
1758 * OSSL_PROVIDER *, so the cast is safe
1760 OSSL_PROVIDER *prov = (OSSL_PROVIDER *)handle;
1762 return ossl_init_thread_start(prov, arg, handfn);
1766 * The FIPS module inner provider doesn't implement these. They aren't
1767 * needed there, since the FIPS module upcalls are always the outer provider
1772 * These error functions should use |handle| to select the proper
1773 * library context to report in the correct error stack if error
1774 * stacks become tied to the library context.
1775 * We cannot currently do that since there's no support for it in the
1778 static void core_new_error(const OSSL_CORE_HANDLE *handle)
1783 static void core_set_error_debug(const OSSL_CORE_HANDLE *handle,
1784 const char *file, int line, const char *func)
1786 ERR_set_debug(file, line, func);
1789 static void core_vset_error(const OSSL_CORE_HANDLE *handle,
1790 uint32_t reason, const char *fmt, va_list args)
1793 * We created this object originally and we know it is actually an
1794 * OSSL_PROVIDER *, so the cast is safe
1796 OSSL_PROVIDER *prov = (OSSL_PROVIDER *)handle;
1799 * If the uppermost 8 bits are non-zero, it's an OpenSSL library
1800 * error and will be treated as such. Otherwise, it's a new style
1801 * provider error and will be treated as such.
1803 if (ERR_GET_LIB(reason) != 0) {
1804 ERR_vset_error(ERR_GET_LIB(reason), ERR_GET_REASON(reason), fmt, args);
1806 ERR_vset_error(prov->error_lib, (int)reason, fmt, args);
1810 static int core_set_error_mark(const OSSL_CORE_HANDLE *handle)
1812 return ERR_set_mark();
1815 static int core_clear_last_error_mark(const OSSL_CORE_HANDLE *handle)
1817 return ERR_clear_last_mark();
1820 static int core_pop_error_to_mark(const OSSL_CORE_HANDLE *handle)
1822 return ERR_pop_to_mark();
1825 static int core_obj_add_sigid(const OSSL_CORE_HANDLE *prov,
1826 const char *sign_name, const char *digest_name,
1827 const char *pkey_name)
1829 int sign_nid = OBJ_txt2nid(sign_name);
1830 int digest_nid = OBJ_txt2nid(digest_name);
1831 int pkey_nid = OBJ_txt2nid(pkey_name);
1833 if (sign_nid == NID_undef)
1837 * Check if it already exists. This is a success if so (even if we don't
1838 * have nids for the digest/pkey)
1840 if (OBJ_find_sigid_algs(sign_nid, NULL, NULL))
1843 if (digest_nid == NID_undef
1844 || pkey_nid == NID_undef)
1847 return OBJ_add_sigid(sign_nid, digest_nid, pkey_nid);
1850 static int core_obj_create(const OSSL_CORE_HANDLE *prov, const char *oid,
1851 const char *sn, const char *ln)
1853 /* Check if it already exists and create it if not */
1854 return OBJ_txt2nid(oid) != NID_undef
1855 || OBJ_create(oid, sn, ln) != NID_undef;
1857 #endif /* FIPS_MODULE */
1860 * Functions provided by the core.
1862 static const OSSL_DISPATCH core_dispatch_[] = {
1863 { OSSL_FUNC_CORE_GETTABLE_PARAMS, (void (*)(void))core_gettable_params },
1864 { OSSL_FUNC_CORE_GET_PARAMS, (void (*)(void))core_get_params },
1865 { OSSL_FUNC_CORE_GET_LIBCTX, (void (*)(void))core_get_libctx },
1866 { OSSL_FUNC_CORE_THREAD_START, (void (*)(void))core_thread_start },
1868 { OSSL_FUNC_CORE_NEW_ERROR, (void (*)(void))core_new_error },
1869 { OSSL_FUNC_CORE_SET_ERROR_DEBUG, (void (*)(void))core_set_error_debug },
1870 { OSSL_FUNC_CORE_VSET_ERROR, (void (*)(void))core_vset_error },
1871 { OSSL_FUNC_CORE_SET_ERROR_MARK, (void (*)(void))core_set_error_mark },
1872 { OSSL_FUNC_CORE_CLEAR_LAST_ERROR_MARK,
1873 (void (*)(void))core_clear_last_error_mark },
1874 { OSSL_FUNC_CORE_POP_ERROR_TO_MARK, (void (*)(void))core_pop_error_to_mark },
1875 { OSSL_FUNC_BIO_NEW_FILE, (void (*)(void))ossl_core_bio_new_file },
1876 { OSSL_FUNC_BIO_NEW_MEMBUF, (void (*)(void))ossl_core_bio_new_mem_buf },
1877 { OSSL_FUNC_BIO_READ_EX, (void (*)(void))ossl_core_bio_read_ex },
1878 { OSSL_FUNC_BIO_WRITE_EX, (void (*)(void))ossl_core_bio_write_ex },
1879 { OSSL_FUNC_BIO_GETS, (void (*)(void))ossl_core_bio_gets },
1880 { OSSL_FUNC_BIO_PUTS, (void (*)(void))ossl_core_bio_puts },
1881 { OSSL_FUNC_BIO_CTRL, (void (*)(void))ossl_core_bio_ctrl },
1882 { OSSL_FUNC_BIO_UP_REF, (void (*)(void))ossl_core_bio_up_ref },
1883 { OSSL_FUNC_BIO_FREE, (void (*)(void))ossl_core_bio_free },
1884 { OSSL_FUNC_BIO_VPRINTF, (void (*)(void))ossl_core_bio_vprintf },
1885 { OSSL_FUNC_BIO_VSNPRINTF, (void (*)(void))BIO_vsnprintf },
1886 { OSSL_FUNC_SELF_TEST_CB, (void (*)(void))OSSL_SELF_TEST_get_callback },
1887 { OSSL_FUNC_GET_ENTROPY, (void (*)(void))ossl_rand_get_entropy },
1888 { OSSL_FUNC_CLEANUP_ENTROPY, (void (*)(void))ossl_rand_cleanup_entropy },
1889 { OSSL_FUNC_GET_NONCE, (void (*)(void))ossl_rand_get_nonce },
1890 { OSSL_FUNC_CLEANUP_NONCE, (void (*)(void))ossl_rand_cleanup_nonce },
1892 { OSSL_FUNC_CRYPTO_MALLOC, (void (*)(void))CRYPTO_malloc },
1893 { OSSL_FUNC_CRYPTO_ZALLOC, (void (*)(void))CRYPTO_zalloc },
1894 { OSSL_FUNC_CRYPTO_FREE, (void (*)(void))CRYPTO_free },
1895 { OSSL_FUNC_CRYPTO_CLEAR_FREE, (void (*)(void))CRYPTO_clear_free },
1896 { OSSL_FUNC_CRYPTO_REALLOC, (void (*)(void))CRYPTO_realloc },
1897 { OSSL_FUNC_CRYPTO_CLEAR_REALLOC, (void (*)(void))CRYPTO_clear_realloc },
1898 { OSSL_FUNC_CRYPTO_SECURE_MALLOC, (void (*)(void))CRYPTO_secure_malloc },
1899 { OSSL_FUNC_CRYPTO_SECURE_ZALLOC, (void (*)(void))CRYPTO_secure_zalloc },
1900 { OSSL_FUNC_CRYPTO_SECURE_FREE, (void (*)(void))CRYPTO_secure_free },
1901 { OSSL_FUNC_CRYPTO_SECURE_CLEAR_FREE,
1902 (void (*)(void))CRYPTO_secure_clear_free },
1903 { OSSL_FUNC_CRYPTO_SECURE_ALLOCATED,
1904 (void (*)(void))CRYPTO_secure_allocated },
1905 { OSSL_FUNC_OPENSSL_CLEANSE, (void (*)(void))OPENSSL_cleanse },
1907 { OSSL_FUNC_PROVIDER_REGISTER_CHILD_CB,
1908 (void (*)(void))ossl_provider_register_child_cb },
1909 { OSSL_FUNC_PROVIDER_DEREGISTER_CHILD_CB,
1910 (void (*)(void))ossl_provider_deregister_child_cb },
1911 { OSSL_FUNC_PROVIDER_NAME,
1912 (void (*)(void))OSSL_PROVIDER_get0_name },
1913 { OSSL_FUNC_PROVIDER_GET0_PROVIDER_CTX,
1914 (void (*)(void))OSSL_PROVIDER_get0_provider_ctx },
1915 { OSSL_FUNC_PROVIDER_GET0_DISPATCH,
1916 (void (*)(void))OSSL_PROVIDER_get0_dispatch },
1917 { OSSL_FUNC_PROVIDER_UP_REF,
1918 (void (*)(void))provider_up_ref_intern },
1919 { OSSL_FUNC_PROVIDER_FREE,
1920 (void (*)(void))provider_free_intern },
1921 { OSSL_FUNC_CORE_OBJ_ADD_SIGID, (void (*)(void))core_obj_add_sigid },
1922 { OSSL_FUNC_CORE_OBJ_CREATE, (void (*)(void))core_obj_create },
1926 static const OSSL_DISPATCH *core_dispatch = core_dispatch_;