2 * Copyright 2019-2022 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"
19 #include "crypto/decoder.h" /* ossl_decoder_store_cache_flush */
20 #include "crypto/encoder.h" /* ossl_encoder_store_cache_flush */
21 #include "crypto/store.h" /* ossl_store_loader_store_cache_flush */
23 #include "crypto/evp.h" /* evp_method_store_cache_flush */
24 #include "crypto/rand.h"
25 #include "internal/nelem.h"
26 #include "internal/thread_once.h"
27 #include "internal/provider.h"
28 #include "internal/refcount.h"
29 #include "internal/bio.h"
30 #include "internal/core.h"
31 #include "provider_local.h"
33 # include <openssl/self_test.h>
37 * This file defines and uses a number of different structures:
39 * OSSL_PROVIDER (provider_st): Used to represent all information related to a
40 * single instance of a provider.
42 * provider_store_st: Holds information about the collection of providers that
43 * are available within the current library context (OSSL_LIB_CTX). It also
44 * holds configuration information about providers that could be loaded at some
47 * OSSL_PROVIDER_CHILD_CB: An instance of this structure holds the callbacks
48 * that have been registered for a child library context and the associated
49 * provider that registered those callbacks.
51 * Where a child library context exists then it has its own instance of the
52 * provider store. Each provider that exists in the parent provider store, has
53 * an associated child provider in the child library context's provider store.
54 * As providers get activated or deactivated this needs to be mirrored in the
55 * associated child providers.
60 * There are a number of different locks used in this file and it is important
61 * to understand how they should be used in order to avoid deadlocks.
63 * Fields within a structure can often be "write once" on creation, and then
64 * "read many". Creation of a structure is done by a single thread, and
65 * therefore no lock is required for the "write once/read many" fields. It is
66 * safe for multiple threads to read these fields without a lock, because they
67 * will never be changed.
69 * However some fields may be changed after a structure has been created and
70 * shared between multiple threads. Where this is the case a lock is required.
72 * The locks available are:
74 * The provider flag_lock: Used to control updates to the various provider
75 * "flags" (flag_initialized, flag_activated, flag_fallback) and associated
76 * "counts" (activatecnt).
78 * The provider refcnt_lock: Only ever used to control updates to the provider
81 * The provider optbits_lock: Used to control access to the provider's
82 * operation_bits and operation_bits_sz fields.
84 * The store default_path_lock: Used to control access to the provider store's
85 * default search path value (default_path)
87 * The store lock: Used to control the stack of provider's held within the
88 * provider store, as well as the stack of registered child provider callbacks.
90 * As a general rule-of-thumb it is best to:
91 * - keep the scope of the code that is protected by a lock to the absolute
93 * - try to keep the scope of the lock to within a single function (i.e. avoid
94 * making calls to other functions while holding a lock);
95 * - try to only ever hold one lock at a time.
97 * Unfortunately, it is not always possible to stick to the above guidelines.
98 * Where they are not adhered to there is always a danger of inadvertently
99 * introducing the possibility of deadlock. The following rules MUST be adhered
100 * to in order to avoid that:
101 * - Holding multiple locks at the same time is only allowed for the
102 * provider store lock, the provider flag_lock and the provider refcnt_lock.
103 * - When holding multiple locks they must be acquired in the following order of
105 * 1) provider store lock
106 * 2) provider flag_lock
107 * 3) provider refcnt_lock
108 * - When releasing locks they must be released in the reverse order to which
110 * - No locks may be held when making an upcall. NOTE: Some common functions
111 * can make upcalls as part of their normal operation. If you need to call
112 * some other function while holding a lock make sure you know whether it
113 * will make any upcalls or not. For example ossl_provider_up_ref() can call
114 * ossl_provider_up_ref_parent() which can call the c_prov_up_ref() upcall.
115 * - It is permissible to hold the store and flag locks when calling child
116 * provider callbacks. No other locks may be held during such callbacks.
119 static OSSL_PROVIDER *provider_new(const char *name,
120 OSSL_provider_init_fn *init_function,
121 STACK_OF(INFOPAIR) *parameters);
124 * Provider Object structure
125 * =========================
131 int (*create_cb)(const OSSL_CORE_HANDLE *provider, void *cbdata);
132 int (*remove_cb)(const OSSL_CORE_HANDLE *provider, void *cbdata);
133 int (*global_props_cb)(const char *props, void *cbdata);
135 } OSSL_PROVIDER_CHILD_CB;
136 DEFINE_STACK_OF(OSSL_PROVIDER_CHILD_CB)
139 struct provider_store_st; /* Forward declaration */
141 struct ossl_provider_st {
143 unsigned int flag_initialized:1;
144 unsigned int flag_activated:1;
145 unsigned int flag_fallback:1; /* Can be used as fallback */
147 /* Getting and setting the flags require synchronization */
148 CRYPTO_RWLOCK *flag_lock;
150 /* OpenSSL library side data */
151 CRYPTO_REF_COUNT refcnt;
152 CRYPTO_RWLOCK *refcnt_lock; /* For the ref counter */
157 OSSL_provider_init_fn *init_function;
158 STACK_OF(INFOPAIR) *parameters;
159 OSSL_LIB_CTX *libctx; /* The library context this instance is in */
160 struct provider_store_st *store; /* The store this instance belongs to */
163 * In the FIPS module inner provider, this isn't needed, since the
164 * error upcalls are always direct calls to the outer provider.
166 int error_lib; /* ERR library number, one for each provider */
167 # ifndef OPENSSL_NO_ERR
168 ERR_STRING_DATA *error_strings; /* Copy of what the provider gives us */
172 /* Provider side functions */
173 OSSL_FUNC_provider_teardown_fn *teardown;
174 OSSL_FUNC_provider_gettable_params_fn *gettable_params;
175 OSSL_FUNC_provider_get_params_fn *get_params;
176 OSSL_FUNC_provider_get_capabilities_fn *get_capabilities;
177 OSSL_FUNC_provider_self_test_fn *self_test;
178 OSSL_FUNC_provider_query_operation_fn *query_operation;
179 OSSL_FUNC_provider_unquery_operation_fn *unquery_operation;
182 * Cache of bit to indicate of query_operation() has been called on
183 * a specific operation or not.
185 unsigned char *operation_bits;
186 size_t operation_bits_sz;
187 CRYPTO_RWLOCK *opbits_lock;
190 /* Whether this provider is the child of some other provider */
191 const OSSL_CORE_HANDLE *handle;
192 unsigned int ischild:1;
195 /* Provider side data */
197 const OSSL_DISPATCH *dispatch;
199 DEFINE_STACK_OF(OSSL_PROVIDER)
201 static int ossl_provider_cmp(const OSSL_PROVIDER * const *a,
202 const OSSL_PROVIDER * const *b)
204 return strcmp((*a)->name, (*b)->name);
208 * Provider Object store
209 * =====================
211 * The Provider Object store is a library context object, and therefore needs
215 struct provider_store_st {
216 OSSL_LIB_CTX *libctx;
217 STACK_OF(OSSL_PROVIDER) *providers;
218 STACK_OF(OSSL_PROVIDER_CHILD_CB) *child_cbs;
219 CRYPTO_RWLOCK *default_path_lock;
222 OSSL_PROVIDER_INFO *provinfo;
225 unsigned int use_fallbacks:1;
226 unsigned int freeing:1;
230 * provider_deactivate_free() is a wrapper around ossl_provider_deactivate()
231 * and ossl_provider_free(), called as needed.
232 * Since this is only called when the provider store is being emptied, we
233 * don't need to care about any lock.
235 static void provider_deactivate_free(OSSL_PROVIDER *prov)
237 if (prov->flag_activated)
238 ossl_provider_deactivate(prov, 1);
239 ossl_provider_free(prov);
243 static void ossl_provider_child_cb_free(OSSL_PROVIDER_CHILD_CB *cb)
249 static void infopair_free(INFOPAIR *pair)
251 OPENSSL_free(pair->name);
252 OPENSSL_free(pair->value);
256 static INFOPAIR *infopair_copy(const INFOPAIR *src)
258 INFOPAIR *dest = OPENSSL_zalloc(sizeof(*dest));
262 if (src->name != NULL) {
263 dest->name = OPENSSL_strdup(src->name);
264 if (dest->name == NULL)
267 if (src->value != NULL) {
268 dest->value = OPENSSL_strdup(src->value);
269 if (dest->value == NULL)
274 OPENSSL_free(dest->name);
279 void ossl_provider_info_clear(OSSL_PROVIDER_INFO *info)
281 OPENSSL_free(info->name);
282 OPENSSL_free(info->path);
283 sk_INFOPAIR_pop_free(info->parameters, infopair_free);
286 static void provider_store_free(void *vstore)
288 struct provider_store_st *store = vstore;
294 OPENSSL_free(store->default_path);
295 sk_OSSL_PROVIDER_pop_free(store->providers, provider_deactivate_free);
297 sk_OSSL_PROVIDER_CHILD_CB_pop_free(store->child_cbs,
298 ossl_provider_child_cb_free);
300 CRYPTO_THREAD_lock_free(store->default_path_lock);
301 CRYPTO_THREAD_lock_free(store->lock);
302 for (i = 0; i < store->numprovinfo; i++)
303 ossl_provider_info_clear(&store->provinfo[i]);
304 OPENSSL_free(store->provinfo);
308 static void *provider_store_new(OSSL_LIB_CTX *ctx)
310 struct provider_store_st *store = OPENSSL_zalloc(sizeof(*store));
313 || (store->providers = sk_OSSL_PROVIDER_new(ossl_provider_cmp)) == NULL
314 || (store->default_path_lock = CRYPTO_THREAD_lock_new()) == NULL
316 || (store->child_cbs = sk_OSSL_PROVIDER_CHILD_CB_new_null()) == NULL
318 || (store->lock = CRYPTO_THREAD_lock_new()) == NULL) {
319 provider_store_free(store);
323 store->use_fallbacks = 1;
328 static const OSSL_LIB_CTX_METHOD provider_store_method = {
329 /* Needs to be freed before the child provider data is freed */
330 OSSL_LIB_CTX_METHOD_PRIORITY_1,
335 static struct provider_store_st *get_provider_store(OSSL_LIB_CTX *libctx)
337 struct provider_store_st *store = NULL;
339 store = ossl_lib_ctx_get_data(libctx, OSSL_LIB_CTX_PROVIDER_STORE_INDEX,
340 &provider_store_method);
342 ERR_raise(ERR_LIB_CRYPTO, ERR_R_INTERNAL_ERROR);
346 int ossl_provider_disable_fallback_loading(OSSL_LIB_CTX *libctx)
348 struct provider_store_st *store;
350 if ((store = get_provider_store(libctx)) != NULL) {
351 if (!CRYPTO_THREAD_write_lock(store->lock))
353 store->use_fallbacks = 0;
354 CRYPTO_THREAD_unlock(store->lock);
360 #define BUILTINS_BLOCK_SIZE 10
362 int ossl_provider_info_add_to_store(OSSL_LIB_CTX *libctx,
363 OSSL_PROVIDER_INFO *entry)
365 struct provider_store_st *store = get_provider_store(libctx);
368 if (entry->name == NULL) {
369 ERR_raise(ERR_LIB_CRYPTO, ERR_R_PASSED_NULL_PARAMETER);
374 ERR_raise(ERR_LIB_CRYPTO, ERR_R_INTERNAL_ERROR);
378 if (!CRYPTO_THREAD_write_lock(store->lock))
380 if (store->provinfosz == 0) {
381 store->provinfo = OPENSSL_zalloc(sizeof(*store->provinfo)
382 * BUILTINS_BLOCK_SIZE);
383 if (store->provinfo == NULL) {
384 ERR_raise(ERR_LIB_CRYPTO, ERR_R_MALLOC_FAILURE);
387 store->provinfosz = BUILTINS_BLOCK_SIZE;
388 } else if (store->numprovinfo == store->provinfosz) {
389 OSSL_PROVIDER_INFO *tmpbuiltins;
390 size_t newsz = store->provinfosz + BUILTINS_BLOCK_SIZE;
392 tmpbuiltins = OPENSSL_realloc(store->provinfo,
393 sizeof(*store->provinfo) * newsz);
394 if (tmpbuiltins == NULL) {
395 ERR_raise(ERR_LIB_CRYPTO, ERR_R_MALLOC_FAILURE);
398 store->provinfo = tmpbuiltins;
399 store->provinfosz = newsz;
401 store->provinfo[store->numprovinfo] = *entry;
402 store->numprovinfo++;
406 CRYPTO_THREAD_unlock(store->lock);
410 OSSL_PROVIDER *ossl_provider_find(OSSL_LIB_CTX *libctx, const char *name,
413 struct provider_store_st *store = NULL;
414 OSSL_PROVIDER *prov = NULL;
416 if ((store = get_provider_store(libctx)) != NULL) {
417 OSSL_PROVIDER tmpl = { 0, };
422 * Make sure any providers are loaded from config before we try to find
426 if (ossl_lib_ctx_is_default(libctx))
427 OPENSSL_init_crypto(OPENSSL_INIT_LOAD_CONFIG, NULL);
431 tmpl.name = (char *)name;
433 * A "find" operation can sort the stack, and therefore a write lock is
436 if (!CRYPTO_THREAD_write_lock(store->lock))
438 if ((i = sk_OSSL_PROVIDER_find(store->providers, &tmpl)) != -1)
439 prov = sk_OSSL_PROVIDER_value(store->providers, i);
440 CRYPTO_THREAD_unlock(store->lock);
441 if (prov != NULL && !ossl_provider_up_ref(prov))
449 * Provider Object methods
450 * =======================
453 static OSSL_PROVIDER *provider_new(const char *name,
454 OSSL_provider_init_fn *init_function,
455 STACK_OF(INFOPAIR) *parameters)
457 OSSL_PROVIDER *prov = NULL;
459 if ((prov = OPENSSL_zalloc(sizeof(*prov))) == NULL
461 || (prov->refcnt_lock = CRYPTO_THREAD_lock_new()) == NULL
465 ERR_raise(ERR_LIB_CRYPTO, ERR_R_MALLOC_FAILURE);
469 prov->refcnt = 1; /* 1 One reference to be returned */
471 if ((prov->opbits_lock = CRYPTO_THREAD_lock_new()) == NULL
472 || (prov->flag_lock = CRYPTO_THREAD_lock_new()) == NULL
473 || (prov->name = OPENSSL_strdup(name)) == NULL
474 || (prov->parameters = sk_INFOPAIR_deep_copy(parameters,
476 infopair_free)) == NULL) {
477 ossl_provider_free(prov);
478 ERR_raise(ERR_LIB_CRYPTO, ERR_R_MALLOC_FAILURE);
482 prov->init_function = init_function;
487 int ossl_provider_up_ref(OSSL_PROVIDER *prov)
491 if (CRYPTO_UP_REF(&prov->refcnt, &ref, prov->refcnt_lock) <= 0)
496 if (!ossl_provider_up_ref_parent(prov, 0)) {
497 ossl_provider_free(prov);
507 static int provider_up_ref_intern(OSSL_PROVIDER *prov, int activate)
510 return ossl_provider_activate(prov, 1, 0);
512 return ossl_provider_up_ref(prov);
515 static int provider_free_intern(OSSL_PROVIDER *prov, int deactivate)
518 return ossl_provider_deactivate(prov, 1);
520 ossl_provider_free(prov);
526 * We assume that the requested provider does not already exist in the store.
527 * The caller should check. If it does exist then adding it to the store later
530 OSSL_PROVIDER *ossl_provider_new(OSSL_LIB_CTX *libctx, const char *name,
531 OSSL_provider_init_fn *init_function,
534 struct provider_store_st *store = NULL;
535 OSSL_PROVIDER_INFO template;
536 OSSL_PROVIDER *prov = NULL;
538 if ((store = get_provider_store(libctx)) == NULL)
541 memset(&template, 0, sizeof(template));
542 if (init_function == NULL) {
543 const OSSL_PROVIDER_INFO *p;
546 /* Check if this is a predefined builtin provider */
547 for (p = ossl_predefined_providers; p->name != NULL; p++) {
548 if (strcmp(p->name, name) == 0) {
553 if (p->name == NULL) {
554 /* Check if this is a user added builtin provider */
555 if (!CRYPTO_THREAD_read_lock(store->lock))
557 for (i = 0, p = store->provinfo; i < store->numprovinfo; p++, i++) {
558 if (strcmp(p->name, name) == 0) {
563 CRYPTO_THREAD_unlock(store->lock);
566 template.init = init_function;
569 /* provider_new() generates an error, so no need here */
570 if ((prov = provider_new(name, template.init, template.parameters)) == NULL)
573 prov->libctx = libctx;
575 prov->error_lib = ERR_get_next_error_library();
579 * At this point, the provider is only partially "loaded". To be
580 * fully "loaded", ossl_provider_activate() must also be called and it must
581 * then be added to the provider store.
587 /* Assumes that the store lock is held */
588 static int create_provider_children(OSSL_PROVIDER *prov)
592 struct provider_store_st *store = prov->store;
593 OSSL_PROVIDER_CHILD_CB *child_cb;
596 max = sk_OSSL_PROVIDER_CHILD_CB_num(store->child_cbs);
597 for (i = 0; i < max; i++) {
599 * This is newly activated (activatecnt == 1), so we need to
600 * create child providers as necessary.
602 child_cb = sk_OSSL_PROVIDER_CHILD_CB_value(store->child_cbs, i);
603 ret &= child_cb->create_cb((OSSL_CORE_HANDLE *)prov, child_cb->cbdata);
610 int ossl_provider_add_to_store(OSSL_PROVIDER *prov, OSSL_PROVIDER **actualprov,
611 int retain_fallbacks)
613 struct provider_store_st *store;
615 OSSL_PROVIDER tmpl = { 0, };
616 OSSL_PROVIDER *actualtmp = NULL;
618 if (actualprov != NULL)
621 if ((store = get_provider_store(prov->libctx)) == NULL)
624 if (!CRYPTO_THREAD_write_lock(store->lock))
627 tmpl.name = (char *)prov->name;
628 idx = sk_OSSL_PROVIDER_find(store->providers, &tmpl);
632 actualtmp = sk_OSSL_PROVIDER_value(store->providers, idx);
635 if (sk_OSSL_PROVIDER_push(store->providers, prov) == 0)
638 if (!create_provider_children(prov)) {
639 sk_OSSL_PROVIDER_delete_ptr(store->providers, prov);
642 if (!retain_fallbacks)
643 store->use_fallbacks = 0;
646 CRYPTO_THREAD_unlock(store->lock);
648 if (actualprov != NULL) {
649 if (!ossl_provider_up_ref(actualtmp)) {
650 ERR_raise(ERR_LIB_CRYPTO, ERR_R_MALLOC_FAILURE);
654 *actualprov = actualtmp;
659 * The provider is already in the store. Probably two threads
660 * independently initialised their own provider objects with the same
661 * name and raced to put them in the store. This thread lost. We
662 * deactivate the one we just created and use the one that already
664 * If we get here then we know we did not create provider children
665 * above, so we inform ossl_provider_deactivate not to attempt to remove
668 ossl_provider_deactivate(prov, 0);
669 ossl_provider_free(prov);
675 CRYPTO_THREAD_unlock(store->lock);
679 void ossl_provider_free(OSSL_PROVIDER *prov)
684 CRYPTO_DOWN_REF(&prov->refcnt, &ref, prov->refcnt_lock);
687 * When the refcount drops to zero, we clean up the provider.
688 * Note that this also does teardown, which may seem late,
689 * considering that init happens on first activation. However,
690 * there may be other structures hanging on to the provider after
691 * the last deactivation and may therefore need full access to the
692 * provider's services. Therefore, we deinit late.
695 if (prov->flag_initialized) {
696 ossl_provider_teardown(prov);
697 #ifndef OPENSSL_NO_ERR
699 if (prov->error_strings != NULL) {
700 ERR_unload_strings(prov->error_lib, prov->error_strings);
701 OPENSSL_free(prov->error_strings);
702 prov->error_strings = NULL;
706 OPENSSL_free(prov->operation_bits);
707 prov->operation_bits = NULL;
708 prov->operation_bits_sz = 0;
709 prov->flag_initialized = 0;
714 * We deregister thread handling whether or not the provider was
715 * initialized. If init was attempted but was not successful then
716 * the provider may still have registered a thread handler.
718 ossl_init_thread_deregister(prov);
719 DSO_free(prov->module);
721 OPENSSL_free(prov->name);
722 OPENSSL_free(prov->path);
723 sk_INFOPAIR_pop_free(prov->parameters, infopair_free);
724 CRYPTO_THREAD_lock_free(prov->opbits_lock);
725 CRYPTO_THREAD_lock_free(prov->flag_lock);
727 CRYPTO_THREAD_lock_free(prov->refcnt_lock);
732 else if (prov->ischild) {
733 ossl_provider_free_parent(prov, 0);
740 int ossl_provider_set_module_path(OSSL_PROVIDER *prov, const char *module_path)
742 OPENSSL_free(prov->path);
744 if (module_path == NULL)
746 if ((prov->path = OPENSSL_strdup(module_path)) != NULL)
748 ERR_raise(ERR_LIB_CRYPTO, ERR_R_MALLOC_FAILURE);
752 static int infopair_add(STACK_OF(INFOPAIR) **infopairsk, const char *name,
755 INFOPAIR *pair = NULL;
757 if ((pair = OPENSSL_zalloc(sizeof(*pair))) != NULL
758 && (*infopairsk != NULL
759 || (*infopairsk = sk_INFOPAIR_new_null()) != NULL)
760 && (pair->name = OPENSSL_strdup(name)) != NULL
761 && (pair->value = OPENSSL_strdup(value)) != NULL
762 && sk_INFOPAIR_push(*infopairsk, pair) > 0)
766 OPENSSL_free(pair->name);
767 OPENSSL_free(pair->value);
770 ERR_raise(ERR_LIB_CRYPTO, ERR_R_MALLOC_FAILURE);
774 int ossl_provider_add_parameter(OSSL_PROVIDER *prov,
775 const char *name, const char *value)
777 return infopair_add(&prov->parameters, name, value);
780 int ossl_provider_info_add_parameter(OSSL_PROVIDER_INFO *provinfo,
784 return infopair_add(&provinfo->parameters, name, value);
788 * Provider activation.
790 * What "activation" means depends on the provider form; for built in
791 * providers (in the library or the application alike), the provider
792 * can already be considered to be loaded, all that's needed is to
793 * initialize it. However, for dynamically loadable provider modules,
794 * we must first load that module.
796 * Built in modules are distinguished from dynamically loaded modules
797 * with an already assigned init function.
799 static const OSSL_DISPATCH *core_dispatch; /* Define further down */
801 int OSSL_PROVIDER_set_default_search_path(OSSL_LIB_CTX *libctx,
804 struct provider_store_st *store;
808 p = OPENSSL_strdup(path);
810 ERR_raise(ERR_LIB_CRYPTO, ERR_R_MALLOC_FAILURE);
814 if ((store = get_provider_store(libctx)) != NULL
815 && CRYPTO_THREAD_write_lock(store->default_path_lock)) {
816 OPENSSL_free(store->default_path);
817 store->default_path = p;
818 CRYPTO_THREAD_unlock(store->default_path_lock);
826 * Internal version that doesn't affect the store flags, and thereby avoid
827 * locking. Direct callers must remember to set the store flags when
830 static int provider_init(OSSL_PROVIDER *prov)
832 const OSSL_DISPATCH *provider_dispatch = NULL;
833 void *tmp_provctx = NULL; /* safety measure */
834 #ifndef OPENSSL_NO_ERR
836 OSSL_FUNC_provider_get_reason_strings_fn *p_get_reason_strings = NULL;
841 if (!ossl_assert(!prov->flag_initialized)) {
842 ERR_raise(ERR_LIB_CRYPTO, ERR_R_INTERNAL_ERROR);
847 * If the init function isn't set, it indicates that this provider is
850 if (prov->init_function == NULL) {
854 if (prov->module == NULL) {
855 char *allocated_path = NULL;
856 const char *module_path = NULL;
857 char *merged_path = NULL;
858 const char *load_dir = NULL;
859 char *allocated_load_dir = NULL;
860 struct provider_store_st *store;
862 if ((prov->module = DSO_new()) == NULL) {
863 /* DSO_new() generates an error already */
867 if ((store = get_provider_store(prov->libctx)) == NULL
868 || !CRYPTO_THREAD_read_lock(store->default_path_lock))
871 if (store->default_path != NULL) {
872 allocated_load_dir = OPENSSL_strdup(store->default_path);
873 CRYPTO_THREAD_unlock(store->default_path_lock);
874 if (allocated_load_dir == NULL) {
875 ERR_raise(ERR_LIB_CRYPTO, ERR_R_MALLOC_FAILURE);
878 load_dir = allocated_load_dir;
880 CRYPTO_THREAD_unlock(store->default_path_lock);
883 if (load_dir == NULL) {
884 load_dir = ossl_safe_getenv("OPENSSL_MODULES");
885 if (load_dir == NULL)
886 load_dir = MODULESDIR;
889 DSO_ctrl(prov->module, DSO_CTRL_SET_FLAGS,
890 DSO_FLAG_NAME_TRANSLATION_EXT_ONLY, NULL);
892 module_path = prov->path;
893 if (module_path == NULL)
894 module_path = allocated_path =
895 DSO_convert_filename(prov->module, prov->name);
896 if (module_path != NULL)
897 merged_path = DSO_merge(prov->module, module_path, load_dir);
899 if (merged_path == NULL
900 || (DSO_load(prov->module, merged_path, NULL, 0)) == NULL) {
901 DSO_free(prov->module);
905 OPENSSL_free(merged_path);
906 OPENSSL_free(allocated_path);
907 OPENSSL_free(allocated_load_dir);
910 if (prov->module != NULL)
911 prov->init_function = (OSSL_provider_init_fn *)
912 DSO_bind_func(prov->module, "OSSL_provider_init");
916 /* Call the initialise function for the provider. */
917 if (prov->init_function == NULL
918 || !prov->init_function((OSSL_CORE_HANDLE *)prov, core_dispatch,
919 &provider_dispatch, &tmp_provctx)) {
920 ERR_raise_data(ERR_LIB_CRYPTO, ERR_R_INIT_FAIL,
921 "name=%s", prov->name);
924 prov->provctx = tmp_provctx;
925 prov->dispatch = provider_dispatch;
927 for (; provider_dispatch->function_id != 0; provider_dispatch++) {
928 switch (provider_dispatch->function_id) {
929 case OSSL_FUNC_PROVIDER_TEARDOWN:
931 OSSL_FUNC_provider_teardown(provider_dispatch);
933 case OSSL_FUNC_PROVIDER_GETTABLE_PARAMS:
934 prov->gettable_params =
935 OSSL_FUNC_provider_gettable_params(provider_dispatch);
937 case OSSL_FUNC_PROVIDER_GET_PARAMS:
939 OSSL_FUNC_provider_get_params(provider_dispatch);
941 case OSSL_FUNC_PROVIDER_SELF_TEST:
943 OSSL_FUNC_provider_self_test(provider_dispatch);
945 case OSSL_FUNC_PROVIDER_GET_CAPABILITIES:
946 prov->get_capabilities =
947 OSSL_FUNC_provider_get_capabilities(provider_dispatch);
949 case OSSL_FUNC_PROVIDER_QUERY_OPERATION:
950 prov->query_operation =
951 OSSL_FUNC_provider_query_operation(provider_dispatch);
953 case OSSL_FUNC_PROVIDER_UNQUERY_OPERATION:
954 prov->unquery_operation =
955 OSSL_FUNC_provider_unquery_operation(provider_dispatch);
957 #ifndef OPENSSL_NO_ERR
959 case OSSL_FUNC_PROVIDER_GET_REASON_STRINGS:
960 p_get_reason_strings =
961 OSSL_FUNC_provider_get_reason_strings(provider_dispatch);
968 #ifndef OPENSSL_NO_ERR
970 if (p_get_reason_strings != NULL) {
971 const OSSL_ITEM *reasonstrings = p_get_reason_strings(prov->provctx);
975 * ERR_load_strings() handles ERR_STRING_DATA rather than OSSL_ITEM,
976 * although they are essentially the same type.
977 * Furthermore, ERR_load_strings() patches the array's error number
978 * with the error library number, so we need to make a copy of that
982 while (reasonstrings[cnt].id != 0) {
983 if (ERR_GET_LIB(reasonstrings[cnt].id) != 0)
987 cnt++; /* One for the terminating item */
989 /* Allocate one extra item for the "library" name */
990 prov->error_strings =
991 OPENSSL_zalloc(sizeof(ERR_STRING_DATA) * (cnt + 1));
992 if (prov->error_strings == NULL)
996 * Set the "library" name.
998 prov->error_strings[0].error = ERR_PACK(prov->error_lib, 0, 0);
999 prov->error_strings[0].string = prov->name;
1001 * Copy reasonstrings item 0..cnt-1 to prov->error_trings positions
1004 for (cnt2 = 1; cnt2 <= cnt; cnt2++) {
1005 prov->error_strings[cnt2].error = (int)reasonstrings[cnt2-1].id;
1006 prov->error_strings[cnt2].string = reasonstrings[cnt2-1].ptr;
1009 ERR_load_strings(prov->error_lib, prov->error_strings);
1014 /* With this flag set, this provider has become fully "loaded". */
1015 prov->flag_initialized = 1;
1023 * Deactivate a provider. If upcalls is 0 then we suppress any upcalls to a
1024 * parent provider. If removechildren is 0 then we suppress any calls to remove
1026 * Return -1 on failure and the activation count on success
1028 static int provider_deactivate(OSSL_PROVIDER *prov, int upcalls,
1032 struct provider_store_st *store;
1038 if (!ossl_assert(prov != NULL))
1042 * No need to lock if we've got no store because we've not been shared with
1045 store = get_provider_store(prov->libctx);
1049 if (lock && !CRYPTO_THREAD_read_lock(store->lock))
1051 if (lock && !CRYPTO_THREAD_write_lock(prov->flag_lock)) {
1052 CRYPTO_THREAD_unlock(store->lock);
1057 if (prov->activatecnt >= 2 && prov->ischild && upcalls) {
1059 * We have had a direct activation in this child libctx so we need to
1060 * now down the ref count in the parent provider. We do the actual down
1061 * ref outside of the flag_lock, since it could involve getting other
1068 if ((count = --prov->activatecnt) < 1)
1069 prov->flag_activated = 0;
1076 if (removechildren && store != NULL) {
1077 int i, max = sk_OSSL_PROVIDER_CHILD_CB_num(store->child_cbs);
1078 OSSL_PROVIDER_CHILD_CB *child_cb;
1080 for (i = 0; i < max; i++) {
1081 child_cb = sk_OSSL_PROVIDER_CHILD_CB_value(store->child_cbs, i);
1082 child_cb->remove_cb((OSSL_CORE_HANDLE *)prov, child_cb->cbdata);
1087 CRYPTO_THREAD_unlock(prov->flag_lock);
1088 CRYPTO_THREAD_unlock(store->lock);
1092 ossl_provider_free_parent(prov, 1);
1095 /* We don't deinit here, that's done in ossl_provider_free() */
1100 * Activate a provider.
1101 * Return -1 on failure and the activation count on success
1103 static int provider_activate(OSSL_PROVIDER *prov, int lock, int upcalls)
1106 struct provider_store_st *store;
1109 store = prov->store;
1111 * If the provider hasn't been added to the store, then we don't need
1112 * any locks because we've not shared it with other threads.
1114 if (store == NULL) {
1116 if (!provider_init(prov))
1121 if (prov->ischild && upcalls && !ossl_provider_up_ref_parent(prov, 1))
1125 if (lock && !CRYPTO_THREAD_read_lock(store->lock)) {
1127 if (prov->ischild && upcalls)
1128 ossl_provider_free_parent(prov, 1);
1133 if (lock && !CRYPTO_THREAD_write_lock(prov->flag_lock)) {
1134 CRYPTO_THREAD_unlock(store->lock);
1136 if (prov->ischild && upcalls)
1137 ossl_provider_free_parent(prov, 1);
1142 count = ++prov->activatecnt;
1143 prov->flag_activated = 1;
1145 if (prov->activatecnt == 1 && store != NULL) {
1146 ret = create_provider_children(prov);
1149 CRYPTO_THREAD_unlock(prov->flag_lock);
1150 CRYPTO_THREAD_unlock(store->lock);
1159 static int provider_flush_store_cache(const OSSL_PROVIDER *prov)
1161 struct provider_store_st *store;
1164 if ((store = get_provider_store(prov->libctx)) == NULL)
1167 if (!CRYPTO_THREAD_read_lock(store->lock))
1169 freeing = store->freeing;
1170 CRYPTO_THREAD_unlock(store->lock);
1174 = evp_method_store_cache_flush(prov->libctx)
1176 + ossl_encoder_store_cache_flush(prov->libctx)
1177 + ossl_decoder_store_cache_flush(prov->libctx)
1178 + ossl_store_loader_store_cache_flush(prov->libctx)
1191 static int provider_remove_store_methods(OSSL_PROVIDER *prov)
1193 struct provider_store_st *store;
1196 if ((store = get_provider_store(prov->libctx)) == NULL)
1199 if (!CRYPTO_THREAD_read_lock(store->lock))
1201 freeing = store->freeing;
1202 CRYPTO_THREAD_unlock(store->lock);
1207 if (!CRYPTO_THREAD_read_lock(prov->opbits_lock))
1209 OPENSSL_free(prov->operation_bits);
1210 prov->operation_bits = NULL;
1211 prov->operation_bits_sz = 0;
1212 CRYPTO_THREAD_unlock(prov->opbits_lock);
1214 acc = evp_method_store_remove_all_provided(prov)
1216 + ossl_encoder_store_remove_all_provided(prov)
1217 + ossl_decoder_store_remove_all_provided(prov)
1218 + ossl_store_loader_store_remove_all_provided(prov)
1231 int ossl_provider_activate(OSSL_PROVIDER *prov, int upcalls, int aschild)
1239 * If aschild is true, then we only actually do the activation if the
1240 * provider is a child. If its not, this is still success.
1242 if (aschild && !prov->ischild)
1245 if ((count = provider_activate(prov, 1, upcalls)) > 0)
1246 return count == 1 ? provider_flush_store_cache(prov) : 1;
1251 int ossl_provider_deactivate(OSSL_PROVIDER *prov, int removechildren)
1256 || (count = provider_deactivate(prov, 1, removechildren)) < 0)
1258 return count == 0 ? provider_remove_store_methods(prov) : 1;
1261 void *ossl_provider_ctx(const OSSL_PROVIDER *prov)
1263 return prov != NULL ? prov->provctx : NULL;
1267 * This function only does something once when store->use_fallbacks == 1,
1268 * and then sets store->use_fallbacks = 0, so the second call and so on is
1269 * effectively a no-op.
1271 static int provider_activate_fallbacks(struct provider_store_st *store)
1274 int activated_fallback_count = 0;
1276 const OSSL_PROVIDER_INFO *p;
1278 if (!CRYPTO_THREAD_read_lock(store->lock))
1280 use_fallbacks = store->use_fallbacks;
1281 CRYPTO_THREAD_unlock(store->lock);
1285 if (!CRYPTO_THREAD_write_lock(store->lock))
1287 /* Check again, just in case another thread changed it */
1288 use_fallbacks = store->use_fallbacks;
1289 if (!use_fallbacks) {
1290 CRYPTO_THREAD_unlock(store->lock);
1294 for (p = ossl_predefined_providers; p->name != NULL; p++) {
1295 OSSL_PROVIDER *prov = NULL;
1297 if (!p->is_fallback)
1300 * We use the internal constructor directly here,
1301 * otherwise we get a call loop
1303 prov = provider_new(p->name, p->init, NULL);
1306 prov->libctx = store->libctx;
1308 prov->error_lib = ERR_get_next_error_library();
1312 * We are calling provider_activate while holding the store lock. This
1313 * means the init function will be called while holding a lock. Normally
1314 * we try to avoid calling a user callback while holding a lock.
1315 * However, fallbacks are never third party providers so we accept this.
1317 if (provider_activate(prov, 0, 0) < 0) {
1318 ossl_provider_free(prov);
1321 prov->store = store;
1322 if (sk_OSSL_PROVIDER_push(store->providers, prov) == 0) {
1323 ossl_provider_free(prov);
1326 activated_fallback_count++;
1329 if (activated_fallback_count > 0) {
1330 store->use_fallbacks = 0;
1334 CRYPTO_THREAD_unlock(store->lock);
1338 int ossl_provider_doall_activated(OSSL_LIB_CTX *ctx,
1339 int (*cb)(OSSL_PROVIDER *provider,
1343 int ret = 0, curr, max, ref = 0;
1344 struct provider_store_st *store = get_provider_store(ctx);
1345 STACK_OF(OSSL_PROVIDER) *provs = NULL;
1349 * Make sure any providers are loaded from config before we try to use
1352 if (ossl_lib_ctx_is_default(ctx))
1353 OPENSSL_init_crypto(OPENSSL_INIT_LOAD_CONFIG, NULL);
1358 if (!provider_activate_fallbacks(store))
1362 * Under lock, grab a copy of the provider list and up_ref each
1363 * provider so that they don't disappear underneath us.
1365 if (!CRYPTO_THREAD_read_lock(store->lock))
1367 provs = sk_OSSL_PROVIDER_dup(store->providers);
1368 if (provs == NULL) {
1369 CRYPTO_THREAD_unlock(store->lock);
1372 max = sk_OSSL_PROVIDER_num(provs);
1374 * We work backwards through the stack so that we can safely delete items
1377 for (curr = max - 1; curr >= 0; curr--) {
1378 OSSL_PROVIDER *prov = sk_OSSL_PROVIDER_value(provs, curr);
1380 if (!CRYPTO_THREAD_write_lock(prov->flag_lock))
1382 if (prov->flag_activated) {
1384 * We call CRYPTO_UP_REF directly rather than ossl_provider_up_ref
1385 * to avoid upping the ref count on the parent provider, which we
1386 * must not do while holding locks.
1388 if (CRYPTO_UP_REF(&prov->refcnt, &ref, prov->refcnt_lock) <= 0) {
1389 CRYPTO_THREAD_unlock(prov->flag_lock);
1393 * It's already activated, but we up the activated count to ensure
1394 * it remains activated until after we've called the user callback.
1395 * We do this with no locking (because we already hold the locks)
1396 * and no upcalls (which must not be called when locks are held). In
1397 * theory this could mean the parent provider goes inactive, whilst
1398 * still activated in the child for a short period. That's ok.
1400 if (provider_activate(prov, 0, 0) < 0) {
1401 CRYPTO_DOWN_REF(&prov->refcnt, &ref, prov->refcnt_lock);
1402 CRYPTO_THREAD_unlock(prov->flag_lock);
1406 sk_OSSL_PROVIDER_delete(provs, curr);
1409 CRYPTO_THREAD_unlock(prov->flag_lock);
1411 CRYPTO_THREAD_unlock(store->lock);
1414 * Now, we sweep through all providers not under lock
1416 for (curr = 0; curr < max; curr++) {
1417 OSSL_PROVIDER *prov = sk_OSSL_PROVIDER_value(provs, curr);
1419 if (!cb(prov, cbdata)) {
1430 CRYPTO_THREAD_unlock(store->lock);
1433 * The pop_free call doesn't do what we want on an error condition. We
1434 * either start from the first item in the stack, or part way through if
1435 * we only processed some of the items.
1437 for (curr++; curr < max; curr++) {
1438 OSSL_PROVIDER *prov = sk_OSSL_PROVIDER_value(provs, curr);
1440 provider_deactivate(prov, 0, 1);
1442 * As above where we did the up-ref, we don't call ossl_provider_free
1443 * to avoid making upcalls. There should always be at least one ref
1444 * to the provider in the store, so this should never drop to 0.
1446 CRYPTO_DOWN_REF(&prov->refcnt, &ref, prov->refcnt_lock);
1448 * Not much we can do if this assert ever fails. So we don't use
1453 sk_OSSL_PROVIDER_free(provs);
1457 int OSSL_PROVIDER_available(OSSL_LIB_CTX *libctx, const char *name)
1459 OSSL_PROVIDER *prov = NULL;
1461 struct provider_store_st *store = get_provider_store(libctx);
1463 if (store == NULL || !provider_activate_fallbacks(store))
1466 prov = ossl_provider_find(libctx, name, 0);
1468 if (!CRYPTO_THREAD_read_lock(prov->flag_lock))
1470 available = prov->flag_activated;
1471 CRYPTO_THREAD_unlock(prov->flag_lock);
1472 ossl_provider_free(prov);
1477 /* Setters of Provider Object data */
1478 int ossl_provider_set_fallback(OSSL_PROVIDER *prov)
1483 prov->flag_fallback = 1;
1487 /* Getters of Provider Object data */
1488 const char *ossl_provider_name(const OSSL_PROVIDER *prov)
1493 const DSO *ossl_provider_dso(const OSSL_PROVIDER *prov)
1495 return prov->module;
1498 const char *ossl_provider_module_name(const OSSL_PROVIDER *prov)
1503 return DSO_get_filename(prov->module);
1507 const char *ossl_provider_module_path(const OSSL_PROVIDER *prov)
1512 /* FIXME: Ensure it's a full path */
1513 return DSO_get_filename(prov->module);
1517 void *ossl_provider_prov_ctx(const OSSL_PROVIDER *prov)
1520 return prov->provctx;
1525 const OSSL_DISPATCH *ossl_provider_get0_dispatch(const OSSL_PROVIDER *prov)
1528 return prov->dispatch;
1533 OSSL_LIB_CTX *ossl_provider_libctx(const OSSL_PROVIDER *prov)
1535 return prov != NULL ? prov->libctx : NULL;
1538 /* Wrappers around calls to the provider */
1539 void ossl_provider_teardown(const OSSL_PROVIDER *prov)
1541 if (prov->teardown != NULL
1546 prov->teardown(prov->provctx);
1549 const OSSL_PARAM *ossl_provider_gettable_params(const OSSL_PROVIDER *prov)
1551 return prov->gettable_params == NULL
1552 ? NULL : prov->gettable_params(prov->provctx);
1555 int ossl_provider_get_params(const OSSL_PROVIDER *prov, OSSL_PARAM params[])
1557 return prov->get_params == NULL
1558 ? 0 : prov->get_params(prov->provctx, params);
1561 int ossl_provider_self_test(const OSSL_PROVIDER *prov)
1565 if (prov->self_test == NULL)
1567 ret = prov->self_test(prov->provctx);
1569 (void)provider_remove_store_methods((OSSL_PROVIDER *)prov);
1573 int ossl_provider_get_capabilities(const OSSL_PROVIDER *prov,
1574 const char *capability,
1578 return prov->get_capabilities == NULL
1579 ? 1 : prov->get_capabilities(prov->provctx, capability, cb, arg);
1582 const OSSL_ALGORITHM *ossl_provider_query_operation(const OSSL_PROVIDER *prov,
1586 const OSSL_ALGORITHM *res;
1588 if (prov->query_operation == NULL)
1590 res = prov->query_operation(prov->provctx, operation_id, no_cache);
1591 #if defined(OPENSSL_NO_CACHED_FETCH)
1592 /* Forcing the non-caching of queries */
1593 if (no_cache != NULL)
1599 void ossl_provider_unquery_operation(const OSSL_PROVIDER *prov,
1601 const OSSL_ALGORITHM *algs)
1603 if (prov->unquery_operation != NULL)
1604 prov->unquery_operation(prov->provctx, operation_id, algs);
1607 int ossl_provider_set_operation_bit(OSSL_PROVIDER *provider, size_t bitnum)
1609 size_t byte = bitnum / 8;
1610 unsigned char bit = (1 << (bitnum % 8)) & 0xFF;
1612 if (!CRYPTO_THREAD_write_lock(provider->opbits_lock))
1614 if (provider->operation_bits_sz <= byte) {
1615 unsigned char *tmp = OPENSSL_realloc(provider->operation_bits,
1619 CRYPTO_THREAD_unlock(provider->opbits_lock);
1620 ERR_raise(ERR_LIB_CRYPTO, ERR_R_MALLOC_FAILURE);
1623 provider->operation_bits = tmp;
1624 memset(provider->operation_bits + provider->operation_bits_sz,
1625 '\0', byte + 1 - provider->operation_bits_sz);
1626 provider->operation_bits_sz = byte + 1;
1628 provider->operation_bits[byte] |= bit;
1629 CRYPTO_THREAD_unlock(provider->opbits_lock);
1633 int ossl_provider_test_operation_bit(OSSL_PROVIDER *provider, size_t bitnum,
1636 size_t byte = bitnum / 8;
1637 unsigned char bit = (1 << (bitnum % 8)) & 0xFF;
1639 if (!ossl_assert(result != NULL)) {
1640 ERR_raise(ERR_LIB_CRYPTO, ERR_R_PASSED_NULL_PARAMETER);
1645 if (!CRYPTO_THREAD_read_lock(provider->opbits_lock))
1647 if (provider->operation_bits_sz > byte)
1648 *result = ((provider->operation_bits[byte] & bit) != 0);
1649 CRYPTO_THREAD_unlock(provider->opbits_lock);
1654 const OSSL_CORE_HANDLE *ossl_provider_get_parent(OSSL_PROVIDER *prov)
1656 return prov->handle;
1659 int ossl_provider_is_child(const OSSL_PROVIDER *prov)
1661 return prov->ischild;
1664 int ossl_provider_set_child(OSSL_PROVIDER *prov, const OSSL_CORE_HANDLE *handle)
1666 prov->handle = handle;
1672 int ossl_provider_default_props_update(OSSL_LIB_CTX *libctx, const char *props)
1675 struct provider_store_st *store = NULL;
1677 OSSL_PROVIDER_CHILD_CB *child_cb;
1679 if ((store = get_provider_store(libctx)) == NULL)
1682 if (!CRYPTO_THREAD_read_lock(store->lock))
1685 max = sk_OSSL_PROVIDER_CHILD_CB_num(store->child_cbs);
1686 for (i = 0; i < max; i++) {
1687 child_cb = sk_OSSL_PROVIDER_CHILD_CB_value(store->child_cbs, i);
1688 child_cb->global_props_cb(props, child_cb->cbdata);
1691 CRYPTO_THREAD_unlock(store->lock);
1696 static int ossl_provider_register_child_cb(const OSSL_CORE_HANDLE *handle,
1698 const OSSL_CORE_HANDLE *provider,
1701 const OSSL_CORE_HANDLE *provider,
1703 int (*global_props_cb)(
1709 * This is really an OSSL_PROVIDER that we created and cast to
1710 * OSSL_CORE_HANDLE originally. Therefore it is safe to cast it back.
1712 OSSL_PROVIDER *thisprov = (OSSL_PROVIDER *)handle;
1713 OSSL_PROVIDER *prov;
1714 OSSL_LIB_CTX *libctx = thisprov->libctx;
1715 struct provider_store_st *store = NULL;
1716 int ret = 0, i, max;
1717 OSSL_PROVIDER_CHILD_CB *child_cb;
1718 char *propsstr = NULL;
1720 if ((store = get_provider_store(libctx)) == NULL)
1723 child_cb = OPENSSL_malloc(sizeof(*child_cb));
1724 if (child_cb == NULL)
1726 child_cb->prov = thisprov;
1727 child_cb->create_cb = create_cb;
1728 child_cb->remove_cb = remove_cb;
1729 child_cb->global_props_cb = global_props_cb;
1730 child_cb->cbdata = cbdata;
1732 if (!CRYPTO_THREAD_write_lock(store->lock)) {
1733 OPENSSL_free(child_cb);
1736 propsstr = evp_get_global_properties_str(libctx, 0);
1738 if (propsstr != NULL) {
1739 global_props_cb(propsstr, cbdata);
1740 OPENSSL_free(propsstr);
1742 max = sk_OSSL_PROVIDER_num(store->providers);
1743 for (i = 0; i < max; i++) {
1746 prov = sk_OSSL_PROVIDER_value(store->providers, i);
1748 if (!CRYPTO_THREAD_read_lock(prov->flag_lock))
1750 activated = prov->flag_activated;
1751 CRYPTO_THREAD_unlock(prov->flag_lock);
1753 * We hold the store lock while calling the user callback. This means
1754 * that the user callback must be short and simple and not do anything
1755 * likely to cause a deadlock. We don't hold the flag_lock during this
1756 * call. In theory this means that another thread could deactivate it
1757 * while we are calling create. This is ok because the other thread
1758 * will also call remove_cb, but won't be able to do so until we release
1761 if (activated && !create_cb((OSSL_CORE_HANDLE *)prov, cbdata))
1766 ret = sk_OSSL_PROVIDER_CHILD_CB_push(store->child_cbs, child_cb);
1768 if (i != max || ret <= 0) {
1769 /* Failed during creation. Remove everything we just added */
1770 for (; i >= 0; i--) {
1771 prov = sk_OSSL_PROVIDER_value(store->providers, i);
1772 remove_cb((OSSL_CORE_HANDLE *)prov, cbdata);
1774 OPENSSL_free(child_cb);
1777 CRYPTO_THREAD_unlock(store->lock);
1782 static void ossl_provider_deregister_child_cb(const OSSL_CORE_HANDLE *handle)
1785 * This is really an OSSL_PROVIDER that we created and cast to
1786 * OSSL_CORE_HANDLE originally. Therefore it is safe to cast it back.
1788 OSSL_PROVIDER *thisprov = (OSSL_PROVIDER *)handle;
1789 OSSL_LIB_CTX *libctx = thisprov->libctx;
1790 struct provider_store_st *store = NULL;
1792 OSSL_PROVIDER_CHILD_CB *child_cb;
1794 if ((store = get_provider_store(libctx)) == NULL)
1797 if (!CRYPTO_THREAD_write_lock(store->lock))
1799 max = sk_OSSL_PROVIDER_CHILD_CB_num(store->child_cbs);
1800 for (i = 0; i < max; i++) {
1801 child_cb = sk_OSSL_PROVIDER_CHILD_CB_value(store->child_cbs, i);
1802 if (child_cb->prov == thisprov) {
1803 /* Found an entry */
1804 sk_OSSL_PROVIDER_CHILD_CB_delete(store->child_cbs, i);
1805 OPENSSL_free(child_cb);
1809 CRYPTO_THREAD_unlock(store->lock);
1814 * Core functions for the provider
1815 * ===============================
1817 * This is the set of functions that the core makes available to the provider
1821 * This returns a list of Provider Object parameters with their types, for
1822 * discovery. We do not expect that many providers will use this, but one
1825 static const OSSL_PARAM param_types[] = {
1826 OSSL_PARAM_DEFN(OSSL_PROV_PARAM_CORE_VERSION, OSSL_PARAM_UTF8_PTR, NULL, 0),
1827 OSSL_PARAM_DEFN(OSSL_PROV_PARAM_CORE_PROV_NAME, OSSL_PARAM_UTF8_PTR,
1830 OSSL_PARAM_DEFN(OSSL_PROV_PARAM_CORE_MODULE_FILENAME, OSSL_PARAM_UTF8_PTR,
1837 * Forward declare all the functions that are provided aa dispatch.
1838 * This ensures that the compiler will complain if they aren't defined
1839 * with the correct signature.
1841 static OSSL_FUNC_core_gettable_params_fn core_gettable_params;
1842 static OSSL_FUNC_core_get_params_fn core_get_params;
1843 static OSSL_FUNC_core_get_libctx_fn core_get_libctx;
1844 static OSSL_FUNC_core_thread_start_fn core_thread_start;
1846 static OSSL_FUNC_core_new_error_fn core_new_error;
1847 static OSSL_FUNC_core_set_error_debug_fn core_set_error_debug;
1848 static OSSL_FUNC_core_vset_error_fn core_vset_error;
1849 static OSSL_FUNC_core_set_error_mark_fn core_set_error_mark;
1850 static OSSL_FUNC_core_clear_last_error_mark_fn core_clear_last_error_mark;
1851 static OSSL_FUNC_core_pop_error_to_mark_fn core_pop_error_to_mark;
1852 OSSL_FUNC_BIO_new_file_fn ossl_core_bio_new_file;
1853 OSSL_FUNC_BIO_new_membuf_fn ossl_core_bio_new_mem_buf;
1854 OSSL_FUNC_BIO_read_ex_fn ossl_core_bio_read_ex;
1855 OSSL_FUNC_BIO_write_ex_fn ossl_core_bio_write_ex;
1856 OSSL_FUNC_BIO_gets_fn ossl_core_bio_gets;
1857 OSSL_FUNC_BIO_puts_fn ossl_core_bio_puts;
1858 OSSL_FUNC_BIO_up_ref_fn ossl_core_bio_up_ref;
1859 OSSL_FUNC_BIO_free_fn ossl_core_bio_free;
1860 OSSL_FUNC_BIO_vprintf_fn ossl_core_bio_vprintf;
1861 OSSL_FUNC_BIO_vsnprintf_fn BIO_vsnprintf;
1862 static OSSL_FUNC_self_test_cb_fn core_self_test_get_callback;
1863 OSSL_FUNC_get_entropy_fn ossl_rand_get_entropy;
1864 OSSL_FUNC_cleanup_entropy_fn ossl_rand_cleanup_entropy;
1865 OSSL_FUNC_get_nonce_fn ossl_rand_get_nonce;
1866 OSSL_FUNC_cleanup_nonce_fn ossl_rand_cleanup_nonce;
1868 OSSL_FUNC_CRYPTO_malloc_fn CRYPTO_malloc;
1869 OSSL_FUNC_CRYPTO_zalloc_fn CRYPTO_zalloc;
1870 OSSL_FUNC_CRYPTO_free_fn CRYPTO_free;
1871 OSSL_FUNC_CRYPTO_clear_free_fn CRYPTO_clear_free;
1872 OSSL_FUNC_CRYPTO_realloc_fn CRYPTO_realloc;
1873 OSSL_FUNC_CRYPTO_clear_realloc_fn CRYPTO_clear_realloc;
1874 OSSL_FUNC_CRYPTO_secure_malloc_fn CRYPTO_secure_malloc;
1875 OSSL_FUNC_CRYPTO_secure_zalloc_fn CRYPTO_secure_zalloc;
1876 OSSL_FUNC_CRYPTO_secure_free_fn CRYPTO_secure_free;
1877 OSSL_FUNC_CRYPTO_secure_clear_free_fn CRYPTO_secure_clear_free;
1878 OSSL_FUNC_CRYPTO_secure_allocated_fn CRYPTO_secure_allocated;
1879 OSSL_FUNC_OPENSSL_cleanse_fn OPENSSL_cleanse;
1881 OSSL_FUNC_provider_register_child_cb_fn ossl_provider_register_child_cb;
1882 OSSL_FUNC_provider_deregister_child_cb_fn ossl_provider_deregister_child_cb;
1883 static OSSL_FUNC_provider_name_fn core_provider_get0_name;
1884 static OSSL_FUNC_provider_get0_provider_ctx_fn core_provider_get0_provider_ctx;
1885 static OSSL_FUNC_provider_get0_dispatch_fn core_provider_get0_dispatch;
1886 static OSSL_FUNC_provider_up_ref_fn core_provider_up_ref_intern;
1887 static OSSL_FUNC_provider_free_fn core_provider_free_intern;
1888 static OSSL_FUNC_core_obj_add_sigid_fn core_obj_add_sigid;
1889 static OSSL_FUNC_core_obj_create_fn core_obj_create;
1892 static const OSSL_PARAM *core_gettable_params(const OSSL_CORE_HANDLE *handle)
1897 static int core_get_params(const OSSL_CORE_HANDLE *handle, OSSL_PARAM params[])
1902 * We created this object originally and we know it is actually an
1903 * OSSL_PROVIDER *, so the cast is safe
1905 OSSL_PROVIDER *prov = (OSSL_PROVIDER *)handle;
1907 if ((p = OSSL_PARAM_locate(params, OSSL_PROV_PARAM_CORE_VERSION)) != NULL)
1908 OSSL_PARAM_set_utf8_ptr(p, OPENSSL_VERSION_STR);
1909 if ((p = OSSL_PARAM_locate(params, OSSL_PROV_PARAM_CORE_PROV_NAME)) != NULL)
1910 OSSL_PARAM_set_utf8_ptr(p, prov->name);
1913 if ((p = OSSL_PARAM_locate(params,
1914 OSSL_PROV_PARAM_CORE_MODULE_FILENAME)) != NULL)
1915 OSSL_PARAM_set_utf8_ptr(p, ossl_provider_module_path(prov));
1918 if (prov->parameters == NULL)
1921 for (i = 0; i < sk_INFOPAIR_num(prov->parameters); i++) {
1922 INFOPAIR *pair = sk_INFOPAIR_value(prov->parameters, i);
1924 if ((p = OSSL_PARAM_locate(params, pair->name)) != NULL)
1925 OSSL_PARAM_set_utf8_ptr(p, pair->value);
1930 static OPENSSL_CORE_CTX *core_get_libctx(const OSSL_CORE_HANDLE *handle)
1933 * We created this object originally and we know it is actually an
1934 * OSSL_PROVIDER *, so the cast is safe
1936 OSSL_PROVIDER *prov = (OSSL_PROVIDER *)handle;
1939 * Using ossl_provider_libctx would be wrong as that returns
1940 * NULL for |prov| == NULL and NULL libctx has a special meaning
1941 * that does not apply here. Here |prov| == NULL can happen only in
1942 * case of a coding error.
1944 assert(prov != NULL);
1945 return (OPENSSL_CORE_CTX *)prov->libctx;
1948 static int core_thread_start(const OSSL_CORE_HANDLE *handle,
1949 OSSL_thread_stop_handler_fn handfn,
1953 * We created this object originally and we know it is actually an
1954 * OSSL_PROVIDER *, so the cast is safe
1956 OSSL_PROVIDER *prov = (OSSL_PROVIDER *)handle;
1958 return ossl_init_thread_start(prov, arg, handfn);
1962 * The FIPS module inner provider doesn't implement these. They aren't
1963 * needed there, since the FIPS module upcalls are always the outer provider
1968 * These error functions should use |handle| to select the proper
1969 * library context to report in the correct error stack if error
1970 * stacks become tied to the library context.
1971 * We cannot currently do that since there's no support for it in the
1974 static void core_new_error(const OSSL_CORE_HANDLE *handle)
1979 static void core_set_error_debug(const OSSL_CORE_HANDLE *handle,
1980 const char *file, int line, const char *func)
1982 ERR_set_debug(file, line, func);
1985 static void core_vset_error(const OSSL_CORE_HANDLE *handle,
1986 uint32_t reason, const char *fmt, va_list args)
1989 * We created this object originally and we know it is actually an
1990 * OSSL_PROVIDER *, so the cast is safe
1992 OSSL_PROVIDER *prov = (OSSL_PROVIDER *)handle;
1995 * If the uppermost 8 bits are non-zero, it's an OpenSSL library
1996 * error and will be treated as such. Otherwise, it's a new style
1997 * provider error and will be treated as such.
1999 if (ERR_GET_LIB(reason) != 0) {
2000 ERR_vset_error(ERR_GET_LIB(reason), ERR_GET_REASON(reason), fmt, args);
2002 ERR_vset_error(prov->error_lib, (int)reason, fmt, args);
2006 static int core_set_error_mark(const OSSL_CORE_HANDLE *handle)
2008 return ERR_set_mark();
2011 static int core_clear_last_error_mark(const OSSL_CORE_HANDLE *handle)
2013 return ERR_clear_last_mark();
2016 static int core_pop_error_to_mark(const OSSL_CORE_HANDLE *handle)
2018 return ERR_pop_to_mark();
2021 static void core_self_test_get_callback(OPENSSL_CORE_CTX *libctx,
2022 OSSL_CALLBACK **cb, void **cbarg)
2024 OSSL_SELF_TEST_get_callback((OSSL_LIB_CTX *)libctx, cb, cbarg);
2027 static const char *core_provider_get0_name(const OSSL_CORE_HANDLE *prov)
2029 return OSSL_PROVIDER_get0_name((const OSSL_PROVIDER *)prov);
2032 static void *core_provider_get0_provider_ctx(const OSSL_CORE_HANDLE *prov)
2034 return OSSL_PROVIDER_get0_provider_ctx((const OSSL_PROVIDER *)prov);
2037 static const OSSL_DISPATCH *
2038 core_provider_get0_dispatch(const OSSL_CORE_HANDLE *prov)
2040 return OSSL_PROVIDER_get0_dispatch((const OSSL_PROVIDER *)prov);
2043 static int core_provider_up_ref_intern(const OSSL_CORE_HANDLE *prov,
2046 return provider_up_ref_intern((OSSL_PROVIDER *)prov, activate);
2049 static int core_provider_free_intern(const OSSL_CORE_HANDLE *prov,
2052 return provider_free_intern((OSSL_PROVIDER *)prov, deactivate);
2055 static int core_obj_add_sigid(const OSSL_CORE_HANDLE *prov,
2056 const char *sign_name, const char *digest_name,
2057 const char *pkey_name)
2059 int sign_nid = OBJ_txt2nid(sign_name);
2060 int digest_nid = NID_undef;
2061 int pkey_nid = OBJ_txt2nid(pkey_name);
2063 if (digest_name != NULL && digest_name[0] != '\0'
2064 && (digest_nid = OBJ_txt2nid(digest_name)) == NID_undef)
2067 if (sign_nid == NID_undef)
2071 * Check if it already exists. This is a success if so (even if we don't
2072 * have nids for the digest/pkey)
2074 if (OBJ_find_sigid_algs(sign_nid, NULL, NULL))
2077 if (pkey_nid == NID_undef)
2080 return OBJ_add_sigid(sign_nid, digest_nid, pkey_nid);
2083 static int core_obj_create(const OSSL_CORE_HANDLE *prov, const char *oid,
2084 const char *sn, const char *ln)
2086 /* Check if it already exists and create it if not */
2087 return OBJ_txt2nid(oid) != NID_undef
2088 || OBJ_create(oid, sn, ln) != NID_undef;
2090 #endif /* FIPS_MODULE */
2093 * Functions provided by the core.
2095 static const OSSL_DISPATCH core_dispatch_[] = {
2096 { OSSL_FUNC_CORE_GETTABLE_PARAMS, (void (*)(void))core_gettable_params },
2097 { OSSL_FUNC_CORE_GET_PARAMS, (void (*)(void))core_get_params },
2098 { OSSL_FUNC_CORE_GET_LIBCTX, (void (*)(void))core_get_libctx },
2099 { OSSL_FUNC_CORE_THREAD_START, (void (*)(void))core_thread_start },
2101 { OSSL_FUNC_CORE_NEW_ERROR, (void (*)(void))core_new_error },
2102 { OSSL_FUNC_CORE_SET_ERROR_DEBUG, (void (*)(void))core_set_error_debug },
2103 { OSSL_FUNC_CORE_VSET_ERROR, (void (*)(void))core_vset_error },
2104 { OSSL_FUNC_CORE_SET_ERROR_MARK, (void (*)(void))core_set_error_mark },
2105 { OSSL_FUNC_CORE_CLEAR_LAST_ERROR_MARK,
2106 (void (*)(void))core_clear_last_error_mark },
2107 { OSSL_FUNC_CORE_POP_ERROR_TO_MARK, (void (*)(void))core_pop_error_to_mark },
2108 { OSSL_FUNC_BIO_NEW_FILE, (void (*)(void))ossl_core_bio_new_file },
2109 { OSSL_FUNC_BIO_NEW_MEMBUF, (void (*)(void))ossl_core_bio_new_mem_buf },
2110 { OSSL_FUNC_BIO_READ_EX, (void (*)(void))ossl_core_bio_read_ex },
2111 { OSSL_FUNC_BIO_WRITE_EX, (void (*)(void))ossl_core_bio_write_ex },
2112 { OSSL_FUNC_BIO_GETS, (void (*)(void))ossl_core_bio_gets },
2113 { OSSL_FUNC_BIO_PUTS, (void (*)(void))ossl_core_bio_puts },
2114 { OSSL_FUNC_BIO_CTRL, (void (*)(void))ossl_core_bio_ctrl },
2115 { OSSL_FUNC_BIO_UP_REF, (void (*)(void))ossl_core_bio_up_ref },
2116 { OSSL_FUNC_BIO_FREE, (void (*)(void))ossl_core_bio_free },
2117 { OSSL_FUNC_BIO_VPRINTF, (void (*)(void))ossl_core_bio_vprintf },
2118 { OSSL_FUNC_BIO_VSNPRINTF, (void (*)(void))BIO_vsnprintf },
2119 { OSSL_FUNC_SELF_TEST_CB, (void (*)(void))core_self_test_get_callback },
2120 { OSSL_FUNC_GET_ENTROPY, (void (*)(void))ossl_rand_get_entropy },
2121 { OSSL_FUNC_CLEANUP_ENTROPY, (void (*)(void))ossl_rand_cleanup_entropy },
2122 { OSSL_FUNC_GET_NONCE, (void (*)(void))ossl_rand_get_nonce },
2123 { OSSL_FUNC_CLEANUP_NONCE, (void (*)(void))ossl_rand_cleanup_nonce },
2125 { OSSL_FUNC_CRYPTO_MALLOC, (void (*)(void))CRYPTO_malloc },
2126 { OSSL_FUNC_CRYPTO_ZALLOC, (void (*)(void))CRYPTO_zalloc },
2127 { OSSL_FUNC_CRYPTO_FREE, (void (*)(void))CRYPTO_free },
2128 { OSSL_FUNC_CRYPTO_CLEAR_FREE, (void (*)(void))CRYPTO_clear_free },
2129 { OSSL_FUNC_CRYPTO_REALLOC, (void (*)(void))CRYPTO_realloc },
2130 { OSSL_FUNC_CRYPTO_CLEAR_REALLOC, (void (*)(void))CRYPTO_clear_realloc },
2131 { OSSL_FUNC_CRYPTO_SECURE_MALLOC, (void (*)(void))CRYPTO_secure_malloc },
2132 { OSSL_FUNC_CRYPTO_SECURE_ZALLOC, (void (*)(void))CRYPTO_secure_zalloc },
2133 { OSSL_FUNC_CRYPTO_SECURE_FREE, (void (*)(void))CRYPTO_secure_free },
2134 { OSSL_FUNC_CRYPTO_SECURE_CLEAR_FREE,
2135 (void (*)(void))CRYPTO_secure_clear_free },
2136 { OSSL_FUNC_CRYPTO_SECURE_ALLOCATED,
2137 (void (*)(void))CRYPTO_secure_allocated },
2138 { OSSL_FUNC_OPENSSL_CLEANSE, (void (*)(void))OPENSSL_cleanse },
2140 { OSSL_FUNC_PROVIDER_REGISTER_CHILD_CB,
2141 (void (*)(void))ossl_provider_register_child_cb },
2142 { OSSL_FUNC_PROVIDER_DEREGISTER_CHILD_CB,
2143 (void (*)(void))ossl_provider_deregister_child_cb },
2144 { OSSL_FUNC_PROVIDER_NAME,
2145 (void (*)(void))core_provider_get0_name },
2146 { OSSL_FUNC_PROVIDER_GET0_PROVIDER_CTX,
2147 (void (*)(void))core_provider_get0_provider_ctx },
2148 { OSSL_FUNC_PROVIDER_GET0_DISPATCH,
2149 (void (*)(void))core_provider_get0_dispatch },
2150 { OSSL_FUNC_PROVIDER_UP_REF,
2151 (void (*)(void))core_provider_up_ref_intern },
2152 { OSSL_FUNC_PROVIDER_FREE,
2153 (void (*)(void))core_provider_free_intern },
2154 { OSSL_FUNC_CORE_OBJ_ADD_SIGID, (void (*)(void))core_obj_add_sigid },
2155 { OSSL_FUNC_CORE_OBJ_CREATE, (void (*)(void))core_obj_create },
2159 static const OSSL_DISPATCH *core_dispatch = core_dispatch_;