2 * Copyright 2019-2024 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"
32 #include "crypto/context.h"
34 # include <openssl/self_test.h>
38 * This file defines and uses a number of different structures:
40 * OSSL_PROVIDER (provider_st): Used to represent all information related to a
41 * single instance of a provider.
43 * provider_store_st: Holds information about the collection of providers that
44 * are available within the current library context (OSSL_LIB_CTX). It also
45 * holds configuration information about providers that could be loaded at some
48 * OSSL_PROVIDER_CHILD_CB: An instance of this structure holds the callbacks
49 * that have been registered for a child library context and the associated
50 * provider that registered those callbacks.
52 * Where a child library context exists then it has its own instance of the
53 * provider store. Each provider that exists in the parent provider store, has
54 * an associated child provider in the child library context's provider store.
55 * As providers get activated or deactivated this needs to be mirrored in the
56 * associated child providers.
61 * There are a number of different locks used in this file and it is important
62 * to understand how they should be used in order to avoid deadlocks.
64 * Fields within a structure can often be "write once" on creation, and then
65 * "read many". Creation of a structure is done by a single thread, and
66 * therefore no lock is required for the "write once/read many" fields. It is
67 * safe for multiple threads to read these fields without a lock, because they
68 * will never be changed.
70 * However some fields may be changed after a structure has been created and
71 * shared between multiple threads. Where this is the case a lock is required.
73 * The locks available are:
75 * The provider flag_lock: Used to control updates to the various provider
76 * "flags" (flag_initialized and flag_activated).
78 * The provider activatecnt_lock: 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 activatecnt_lock and the provider flag_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 activatecnt_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;
146 /* Getting and setting the flags require synchronization */
147 CRYPTO_RWLOCK *flag_lock;
149 /* OpenSSL library side data */
150 CRYPTO_REF_COUNT refcnt;
151 CRYPTO_RWLOCK *activatecnt_lock; /* For the activatecnt counter */
156 OSSL_provider_init_fn *init_function;
157 STACK_OF(INFOPAIR) *parameters;
158 OSSL_LIB_CTX *libctx; /* The library context this instance is in */
159 struct provider_store_st *store; /* The store this instance belongs to */
162 * In the FIPS module inner provider, this isn't needed, since the
163 * error upcalls are always direct calls to the outer provider.
165 int error_lib; /* ERR library number, one for each provider */
166 # ifndef OPENSSL_NO_ERR
167 ERR_STRING_DATA *error_strings; /* Copy of what the provider gives us */
171 /* Provider side functions */
172 OSSL_FUNC_provider_teardown_fn *teardown;
173 OSSL_FUNC_provider_gettable_params_fn *gettable_params;
174 OSSL_FUNC_provider_get_params_fn *get_params;
175 OSSL_FUNC_provider_get_capabilities_fn *get_capabilities;
176 OSSL_FUNC_provider_self_test_fn *self_test;
177 OSSL_FUNC_provider_query_operation_fn *query_operation;
178 OSSL_FUNC_provider_unquery_operation_fn *unquery_operation;
181 * Cache of bit to indicate of query_operation() has been called on
182 * a specific operation or not.
184 unsigned char *operation_bits;
185 size_t operation_bits_sz;
186 CRYPTO_RWLOCK *opbits_lock;
189 /* Whether this provider is the child of some other provider */
190 const OSSL_CORE_HANDLE *handle;
191 unsigned int ischild:1;
194 /* Provider side data */
196 const OSSL_DISPATCH *dispatch;
198 DEFINE_STACK_OF(OSSL_PROVIDER)
200 static int ossl_provider_cmp(const OSSL_PROVIDER * const *a,
201 const OSSL_PROVIDER * const *b)
203 return strcmp((*a)->name, (*b)->name);
207 * Provider Object store
208 * =====================
210 * The Provider Object store is a library context object, and therefore needs
214 struct provider_store_st {
215 OSSL_LIB_CTX *libctx;
216 STACK_OF(OSSL_PROVIDER) *providers;
217 STACK_OF(OSSL_PROVIDER_CHILD_CB) *child_cbs;
218 CRYPTO_RWLOCK *default_path_lock;
221 OSSL_PROVIDER_INFO *provinfo;
224 unsigned int use_fallbacks:1;
225 unsigned int freeing:1;
229 * provider_deactivate_free() is a wrapper around ossl_provider_deactivate()
230 * and ossl_provider_free(), called as needed.
231 * Since this is only called when the provider store is being emptied, we
232 * don't need to care about any lock.
234 static void provider_deactivate_free(OSSL_PROVIDER *prov)
236 if (prov->flag_activated)
237 ossl_provider_deactivate(prov, 1);
238 ossl_provider_free(prov);
242 static void ossl_provider_child_cb_free(OSSL_PROVIDER_CHILD_CB *cb)
248 static void infopair_free(INFOPAIR *pair)
250 OPENSSL_free(pair->name);
251 OPENSSL_free(pair->value);
255 static INFOPAIR *infopair_copy(const INFOPAIR *src)
257 INFOPAIR *dest = OPENSSL_zalloc(sizeof(*dest));
261 if (src->name != NULL) {
262 dest->name = OPENSSL_strdup(src->name);
263 if (dest->name == NULL)
266 if (src->value != NULL) {
267 dest->value = OPENSSL_strdup(src->value);
268 if (dest->value == NULL)
273 OPENSSL_free(dest->name);
278 void ossl_provider_info_clear(OSSL_PROVIDER_INFO *info)
280 OPENSSL_free(info->name);
281 OPENSSL_free(info->path);
282 sk_INFOPAIR_pop_free(info->parameters, infopair_free);
285 void ossl_provider_store_free(void *vstore)
287 struct provider_store_st *store = vstore;
293 OPENSSL_free(store->default_path);
294 sk_OSSL_PROVIDER_pop_free(store->providers, provider_deactivate_free);
296 sk_OSSL_PROVIDER_CHILD_CB_pop_free(store->child_cbs,
297 ossl_provider_child_cb_free);
299 CRYPTO_THREAD_lock_free(store->default_path_lock);
300 CRYPTO_THREAD_lock_free(store->lock);
301 for (i = 0; i < store->numprovinfo; i++)
302 ossl_provider_info_clear(&store->provinfo[i]);
303 OPENSSL_free(store->provinfo);
307 void *ossl_provider_store_new(OSSL_LIB_CTX *ctx)
309 struct provider_store_st *store = OPENSSL_zalloc(sizeof(*store));
312 || (store->providers = sk_OSSL_PROVIDER_new(ossl_provider_cmp)) == NULL
313 || (store->default_path_lock = CRYPTO_THREAD_lock_new()) == NULL
315 || (store->child_cbs = sk_OSSL_PROVIDER_CHILD_CB_new_null()) == NULL
317 || (store->lock = CRYPTO_THREAD_lock_new()) == NULL) {
318 ossl_provider_store_free(store);
322 store->use_fallbacks = 1;
327 static struct provider_store_st *get_provider_store(OSSL_LIB_CTX *libctx)
329 struct provider_store_st *store = NULL;
331 store = ossl_lib_ctx_get_data(libctx, OSSL_LIB_CTX_PROVIDER_STORE_INDEX);
333 ERR_raise(ERR_LIB_CRYPTO, ERR_R_INTERNAL_ERROR);
337 int ossl_provider_disable_fallback_loading(OSSL_LIB_CTX *libctx)
339 struct provider_store_st *store;
341 if ((store = get_provider_store(libctx)) != NULL) {
342 if (!CRYPTO_THREAD_write_lock(store->lock))
344 store->use_fallbacks = 0;
345 CRYPTO_THREAD_unlock(store->lock);
351 #define BUILTINS_BLOCK_SIZE 10
353 int ossl_provider_info_add_to_store(OSSL_LIB_CTX *libctx,
354 OSSL_PROVIDER_INFO *entry)
356 struct provider_store_st *store = get_provider_store(libctx);
359 if (entry->name == NULL) {
360 ERR_raise(ERR_LIB_CRYPTO, ERR_R_PASSED_NULL_PARAMETER);
365 ERR_raise(ERR_LIB_CRYPTO, ERR_R_INTERNAL_ERROR);
369 if (!CRYPTO_THREAD_write_lock(store->lock))
371 if (store->provinfosz == 0) {
372 store->provinfo = OPENSSL_zalloc(sizeof(*store->provinfo)
373 * BUILTINS_BLOCK_SIZE);
374 if (store->provinfo == NULL)
376 store->provinfosz = BUILTINS_BLOCK_SIZE;
377 } else if (store->numprovinfo == store->provinfosz) {
378 OSSL_PROVIDER_INFO *tmpbuiltins;
379 size_t newsz = store->provinfosz + BUILTINS_BLOCK_SIZE;
381 tmpbuiltins = OPENSSL_realloc(store->provinfo,
382 sizeof(*store->provinfo) * newsz);
383 if (tmpbuiltins == NULL)
385 store->provinfo = tmpbuiltins;
386 store->provinfosz = newsz;
388 store->provinfo[store->numprovinfo] = *entry;
389 store->numprovinfo++;
393 CRYPTO_THREAD_unlock(store->lock);
397 OSSL_PROVIDER *ossl_provider_find(OSSL_LIB_CTX *libctx, const char *name,
398 ossl_unused int noconfig)
400 struct provider_store_st *store = NULL;
401 OSSL_PROVIDER *prov = NULL;
403 if ((store = get_provider_store(libctx)) != NULL) {
404 OSSL_PROVIDER tmpl = { 0, };
407 #if !defined(FIPS_MODULE) && !defined(OPENSSL_NO_AUTOLOAD_CONFIG)
409 * Make sure any providers are loaded from config before we try to find
413 if (ossl_lib_ctx_is_default(libctx))
414 OPENSSL_init_crypto(OPENSSL_INIT_LOAD_CONFIG, NULL);
418 tmpl.name = (char *)name;
419 if (!CRYPTO_THREAD_write_lock(store->lock))
421 sk_OSSL_PROVIDER_sort(store->providers);
422 if ((i = sk_OSSL_PROVIDER_find(store->providers, &tmpl)) != -1)
423 prov = sk_OSSL_PROVIDER_value(store->providers, i);
424 CRYPTO_THREAD_unlock(store->lock);
425 if (prov != NULL && !ossl_provider_up_ref(prov))
433 * Provider Object methods
434 * =======================
437 static OSSL_PROVIDER *provider_new(const char *name,
438 OSSL_provider_init_fn *init_function,
439 STACK_OF(INFOPAIR) *parameters)
441 OSSL_PROVIDER *prov = NULL;
443 if ((prov = OPENSSL_zalloc(sizeof(*prov))) == NULL)
445 if (!CRYPTO_NEW_REF(&prov->refcnt, 1)) {
449 if ((prov->activatecnt_lock = CRYPTO_THREAD_lock_new()) == NULL) {
450 ossl_provider_free(prov);
451 ERR_raise(ERR_LIB_CRYPTO, ERR_R_CRYPTO_LIB);
455 if ((prov->opbits_lock = CRYPTO_THREAD_lock_new()) == NULL
456 || (prov->flag_lock = CRYPTO_THREAD_lock_new()) == NULL
457 || (prov->parameters = sk_INFOPAIR_deep_copy(parameters,
459 infopair_free)) == NULL) {
460 ossl_provider_free(prov);
461 ERR_raise(ERR_LIB_CRYPTO, ERR_R_CRYPTO_LIB);
464 if ((prov->name = OPENSSL_strdup(name)) == NULL) {
465 ossl_provider_free(prov);
469 prov->init_function = init_function;
474 int ossl_provider_up_ref(OSSL_PROVIDER *prov)
478 if (CRYPTO_UP_REF(&prov->refcnt, &ref) <= 0)
483 if (!ossl_provider_up_ref_parent(prov, 0)) {
484 ossl_provider_free(prov);
494 static int provider_up_ref_intern(OSSL_PROVIDER *prov, int activate)
497 return ossl_provider_activate(prov, 1, 0);
499 return ossl_provider_up_ref(prov);
502 static int provider_free_intern(OSSL_PROVIDER *prov, int deactivate)
505 return ossl_provider_deactivate(prov, 1);
507 ossl_provider_free(prov);
513 * We assume that the requested provider does not already exist in the store.
514 * The caller should check. If it does exist then adding it to the store later
517 OSSL_PROVIDER *ossl_provider_new(OSSL_LIB_CTX *libctx, const char *name,
518 OSSL_provider_init_fn *init_function,
519 OSSL_PARAM *params, int noconfig)
521 struct provider_store_st *store = NULL;
522 OSSL_PROVIDER_INFO template;
523 OSSL_PROVIDER *prov = NULL;
525 if ((store = get_provider_store(libctx)) == NULL)
528 memset(&template, 0, sizeof(template));
529 if (init_function == NULL) {
530 const OSSL_PROVIDER_INFO *p;
533 /* Check if this is a predefined builtin provider */
534 for (p = ossl_predefined_providers; p->name != NULL; p++) {
535 if (strcmp(p->name, name) == 0) {
540 if (p->name == NULL) {
541 /* Check if this is a user added provider */
542 if (!CRYPTO_THREAD_read_lock(store->lock))
544 for (i = 0, p = store->provinfo; i < store->numprovinfo; p++, i++) {
545 if (strcmp(p->name, name) == 0) {
550 CRYPTO_THREAD_unlock(store->lock);
553 template.init = init_function;
556 if (params != NULL) {
559 template.parameters = sk_INFOPAIR_new_null();
560 if (template.parameters == NULL)
563 for (i = 0; params[i].key != NULL; i++) {
564 if (params[i].data_type != OSSL_PARAM_UTF8_STRING)
566 if (ossl_provider_info_add_parameter(&template, params[i].key,
567 (char *)params[i].data) <= 0) {
568 sk_INFOPAIR_pop_free(template.parameters, infopair_free);
574 /* provider_new() generates an error, so no need here */
575 prov = provider_new(name, template.init, template.parameters);
577 if (params != NULL) /* We copied the parameters, let's free them */
578 sk_INFOPAIR_pop_free(template.parameters, infopair_free);
583 if (!ossl_provider_set_module_path(prov, template.path)) {
584 ossl_provider_free(prov);
588 prov->libctx = libctx;
590 prov->error_lib = ERR_get_next_error_library();
594 * At this point, the provider is only partially "loaded". To be
595 * fully "loaded", ossl_provider_activate() must also be called and it must
596 * then be added to the provider store.
602 /* Assumes that the store lock is held */
603 static int create_provider_children(OSSL_PROVIDER *prov)
607 struct provider_store_st *store = prov->store;
608 OSSL_PROVIDER_CHILD_CB *child_cb;
611 max = sk_OSSL_PROVIDER_CHILD_CB_num(store->child_cbs);
612 for (i = 0; i < max; i++) {
614 * This is newly activated (activatecnt == 1), so we need to
615 * create child providers as necessary.
617 child_cb = sk_OSSL_PROVIDER_CHILD_CB_value(store->child_cbs, i);
618 ret &= child_cb->create_cb((OSSL_CORE_HANDLE *)prov, child_cb->cbdata);
625 int ossl_provider_add_to_store(OSSL_PROVIDER *prov, OSSL_PROVIDER **actualprov,
626 int retain_fallbacks)
628 struct provider_store_st *store;
630 OSSL_PROVIDER tmpl = { 0, };
631 OSSL_PROVIDER *actualtmp = NULL;
633 if (actualprov != NULL)
636 if ((store = get_provider_store(prov->libctx)) == NULL)
639 if (!CRYPTO_THREAD_write_lock(store->lock))
642 tmpl.name = (char *)prov->name;
643 idx = sk_OSSL_PROVIDER_find(store->providers, &tmpl);
647 actualtmp = sk_OSSL_PROVIDER_value(store->providers, idx);
650 if (sk_OSSL_PROVIDER_push(store->providers, prov) == 0)
653 if (!create_provider_children(prov)) {
654 sk_OSSL_PROVIDER_delete_ptr(store->providers, prov);
657 if (!retain_fallbacks)
658 store->use_fallbacks = 0;
661 CRYPTO_THREAD_unlock(store->lock);
663 if (actualprov != NULL) {
664 if (!ossl_provider_up_ref(actualtmp)) {
665 ERR_raise(ERR_LIB_CRYPTO, ERR_R_CRYPTO_LIB);
669 *actualprov = actualtmp;
674 * The provider is already in the store. Probably two threads
675 * independently initialised their own provider objects with the same
676 * name and raced to put them in the store. This thread lost. We
677 * deactivate the one we just created and use the one that already
679 * If we get here then we know we did not create provider children
680 * above, so we inform ossl_provider_deactivate not to attempt to remove
683 ossl_provider_deactivate(prov, 0);
684 ossl_provider_free(prov);
689 * This can be done outside the lock. We tolerate other threads getting
690 * the wrong result briefly when creating OSSL_DECODER_CTXs.
692 ossl_decoder_cache_flush(prov->libctx);
699 CRYPTO_THREAD_unlock(store->lock);
703 void ossl_provider_free(OSSL_PROVIDER *prov)
708 CRYPTO_DOWN_REF(&prov->refcnt, &ref);
711 * When the refcount drops to zero, we clean up the provider.
712 * Note that this also does teardown, which may seem late,
713 * considering that init happens on first activation. However,
714 * there may be other structures hanging on to the provider after
715 * the last deactivation and may therefore need full access to the
716 * provider's services. Therefore, we deinit late.
719 if (prov->flag_initialized) {
720 ossl_provider_teardown(prov);
721 #ifndef OPENSSL_NO_ERR
723 if (prov->error_strings != NULL) {
724 ERR_unload_strings(prov->error_lib, prov->error_strings);
725 OPENSSL_free(prov->error_strings);
726 prov->error_strings = NULL;
730 OPENSSL_free(prov->operation_bits);
731 prov->operation_bits = NULL;
732 prov->operation_bits_sz = 0;
733 prov->flag_initialized = 0;
738 * We deregister thread handling whether or not the provider was
739 * initialized. If init was attempted but was not successful then
740 * the provider may still have registered a thread handler.
742 ossl_init_thread_deregister(prov);
743 DSO_free(prov->module);
745 OPENSSL_free(prov->name);
746 OPENSSL_free(prov->path);
747 sk_INFOPAIR_pop_free(prov->parameters, infopair_free);
748 CRYPTO_THREAD_lock_free(prov->opbits_lock);
749 CRYPTO_THREAD_lock_free(prov->flag_lock);
750 CRYPTO_THREAD_lock_free(prov->activatecnt_lock);
751 CRYPTO_FREE_REF(&prov->refcnt);
755 else if (prov->ischild) {
756 ossl_provider_free_parent(prov, 0);
763 int ossl_provider_set_module_path(OSSL_PROVIDER *prov, const char *module_path)
765 OPENSSL_free(prov->path);
767 if (module_path == NULL)
769 if ((prov->path = OPENSSL_strdup(module_path)) != NULL)
774 static int infopair_add(STACK_OF(INFOPAIR) **infopairsk, const char *name,
777 INFOPAIR *pair = NULL;
779 if ((pair = OPENSSL_zalloc(sizeof(*pair))) == NULL
780 || (pair->name = OPENSSL_strdup(name)) == NULL
781 || (pair->value = OPENSSL_strdup(value)) == NULL)
784 if ((*infopairsk == NULL
785 && (*infopairsk = sk_INFOPAIR_new_null()) == NULL)
786 || sk_INFOPAIR_push(*infopairsk, pair) <= 0) {
787 ERR_raise(ERR_LIB_CRYPTO, ERR_R_CRYPTO_LIB);
795 OPENSSL_free(pair->name);
796 OPENSSL_free(pair->value);
802 int ossl_provider_add_parameter(OSSL_PROVIDER *prov,
803 const char *name, const char *value)
805 return infopair_add(&prov->parameters, name, value);
808 int ossl_provider_info_add_parameter(OSSL_PROVIDER_INFO *provinfo,
812 return infopair_add(&provinfo->parameters, name, value);
816 * Provider activation.
818 * What "activation" means depends on the provider form; for built in
819 * providers (in the library or the application alike), the provider
820 * can already be considered to be loaded, all that's needed is to
821 * initialize it. However, for dynamically loadable provider modules,
822 * we must first load that module.
824 * Built in modules are distinguished from dynamically loaded modules
825 * with an already assigned init function.
827 static const OSSL_DISPATCH *core_dispatch; /* Define further down */
829 int OSSL_PROVIDER_set_default_search_path(OSSL_LIB_CTX *libctx,
832 struct provider_store_st *store;
836 p = OPENSSL_strdup(path);
840 if ((store = get_provider_store(libctx)) != NULL
841 && CRYPTO_THREAD_write_lock(store->default_path_lock)) {
842 OPENSSL_free(store->default_path);
843 store->default_path = p;
844 CRYPTO_THREAD_unlock(store->default_path_lock);
851 const char *OSSL_PROVIDER_get0_default_search_path(OSSL_LIB_CTX *libctx)
853 struct provider_store_st *store;
856 if ((store = get_provider_store(libctx)) != NULL
857 && CRYPTO_THREAD_read_lock(store->default_path_lock)) {
858 path = store->default_path;
859 CRYPTO_THREAD_unlock(store->default_path_lock);
865 * Internal version that doesn't affect the store flags, and thereby avoid
866 * locking. Direct callers must remember to set the store flags when
869 static int provider_init(OSSL_PROVIDER *prov)
871 const OSSL_DISPATCH *provider_dispatch = NULL;
872 void *tmp_provctx = NULL; /* safety measure */
873 #ifndef OPENSSL_NO_ERR
875 OSSL_FUNC_provider_get_reason_strings_fn *p_get_reason_strings = NULL;
880 if (!ossl_assert(!prov->flag_initialized)) {
881 ERR_raise(ERR_LIB_CRYPTO, ERR_R_INTERNAL_ERROR);
886 * If the init function isn't set, it indicates that this provider is
889 if (prov->init_function == NULL) {
893 if (prov->module == NULL) {
894 char *allocated_path = NULL;
895 const char *module_path = NULL;
896 char *merged_path = NULL;
897 const char *load_dir = NULL;
898 char *allocated_load_dir = NULL;
899 struct provider_store_st *store;
901 if ((prov->module = DSO_new()) == NULL) {
902 /* DSO_new() generates an error already */
906 if ((store = get_provider_store(prov->libctx)) == NULL
907 || !CRYPTO_THREAD_read_lock(store->default_path_lock))
910 if (store->default_path != NULL) {
911 allocated_load_dir = OPENSSL_strdup(store->default_path);
912 CRYPTO_THREAD_unlock(store->default_path_lock);
913 if (allocated_load_dir == NULL)
915 load_dir = allocated_load_dir;
917 CRYPTO_THREAD_unlock(store->default_path_lock);
920 if (load_dir == NULL) {
921 load_dir = ossl_safe_getenv("OPENSSL_MODULES");
922 if (load_dir == NULL)
923 load_dir = MODULESDIR;
926 DSO_ctrl(prov->module, DSO_CTRL_SET_FLAGS,
927 DSO_FLAG_NAME_TRANSLATION_EXT_ONLY, NULL);
929 module_path = prov->path;
930 if (module_path == NULL)
931 module_path = allocated_path =
932 DSO_convert_filename(prov->module, prov->name);
933 if (module_path != NULL)
934 merged_path = DSO_merge(prov->module, module_path, load_dir);
936 if (merged_path == NULL
937 || (DSO_load(prov->module, merged_path, NULL, 0)) == NULL) {
938 DSO_free(prov->module);
942 OPENSSL_free(merged_path);
943 OPENSSL_free(allocated_path);
944 OPENSSL_free(allocated_load_dir);
947 if (prov->module == NULL) {
948 /* DSO has already recorded errors, this is just a tracepoint */
949 ERR_raise_data(ERR_LIB_CRYPTO, ERR_R_DSO_LIB,
950 "name=%s", prov->name);
954 prov->init_function = (OSSL_provider_init_fn *)
955 DSO_bind_func(prov->module, "OSSL_provider_init");
959 /* Check for and call the initialise function for the provider. */
960 if (prov->init_function == NULL) {
961 ERR_raise_data(ERR_LIB_CRYPTO, ERR_R_UNSUPPORTED,
962 "name=%s, provider has no provider init function",
967 if (!prov->init_function((OSSL_CORE_HANDLE *)prov, core_dispatch,
968 &provider_dispatch, &tmp_provctx)) {
969 ERR_raise_data(ERR_LIB_CRYPTO, ERR_R_INIT_FAIL,
970 "name=%s", prov->name);
973 prov->provctx = tmp_provctx;
974 prov->dispatch = provider_dispatch;
976 if (provider_dispatch != NULL) {
977 for (; provider_dispatch->function_id != 0; provider_dispatch++) {
978 switch (provider_dispatch->function_id) {
979 case OSSL_FUNC_PROVIDER_TEARDOWN:
981 OSSL_FUNC_provider_teardown(provider_dispatch);
983 case OSSL_FUNC_PROVIDER_GETTABLE_PARAMS:
984 prov->gettable_params =
985 OSSL_FUNC_provider_gettable_params(provider_dispatch);
987 case OSSL_FUNC_PROVIDER_GET_PARAMS:
989 OSSL_FUNC_provider_get_params(provider_dispatch);
991 case OSSL_FUNC_PROVIDER_SELF_TEST:
993 OSSL_FUNC_provider_self_test(provider_dispatch);
995 case OSSL_FUNC_PROVIDER_GET_CAPABILITIES:
996 prov->get_capabilities =
997 OSSL_FUNC_provider_get_capabilities(provider_dispatch);
999 case OSSL_FUNC_PROVIDER_QUERY_OPERATION:
1000 prov->query_operation =
1001 OSSL_FUNC_provider_query_operation(provider_dispatch);
1003 case OSSL_FUNC_PROVIDER_UNQUERY_OPERATION:
1004 prov->unquery_operation =
1005 OSSL_FUNC_provider_unquery_operation(provider_dispatch);
1007 #ifndef OPENSSL_NO_ERR
1008 # ifndef FIPS_MODULE
1009 case OSSL_FUNC_PROVIDER_GET_REASON_STRINGS:
1010 p_get_reason_strings =
1011 OSSL_FUNC_provider_get_reason_strings(provider_dispatch);
1019 #ifndef OPENSSL_NO_ERR
1020 # ifndef FIPS_MODULE
1021 if (p_get_reason_strings != NULL) {
1022 const OSSL_ITEM *reasonstrings = p_get_reason_strings(prov->provctx);
1026 * ERR_load_strings() handles ERR_STRING_DATA rather than OSSL_ITEM,
1027 * although they are essentially the same type.
1028 * Furthermore, ERR_load_strings() patches the array's error number
1029 * with the error library number, so we need to make a copy of that
1033 while (reasonstrings[cnt].id != 0) {
1034 if (ERR_GET_LIB(reasonstrings[cnt].id) != 0)
1038 cnt++; /* One for the terminating item */
1040 /* Allocate one extra item for the "library" name */
1041 prov->error_strings =
1042 OPENSSL_zalloc(sizeof(ERR_STRING_DATA) * (cnt + 1));
1043 if (prov->error_strings == NULL)
1047 * Set the "library" name.
1049 prov->error_strings[0].error = ERR_PACK(prov->error_lib, 0, 0);
1050 prov->error_strings[0].string = prov->name;
1052 * Copy reasonstrings item 0..cnt-1 to prov->error_trings positions
1055 for (cnt2 = 1; cnt2 <= cnt; cnt2++) {
1056 prov->error_strings[cnt2].error = (int)reasonstrings[cnt2-1].id;
1057 prov->error_strings[cnt2].string = reasonstrings[cnt2-1].ptr;
1060 ERR_load_strings(prov->error_lib, prov->error_strings);
1065 /* With this flag set, this provider has become fully "loaded". */
1066 prov->flag_initialized = 1;
1074 * Deactivate a provider. If upcalls is 0 then we suppress any upcalls to a
1075 * parent provider. If removechildren is 0 then we suppress any calls to remove
1077 * Return -1 on failure and the activation count on success
1079 static int provider_deactivate(OSSL_PROVIDER *prov, int upcalls,
1083 struct provider_store_st *store;
1089 if (!ossl_assert(prov != NULL))
1093 * No need to lock if we've got no store because we've not been shared with
1096 store = get_provider_store(prov->libctx);
1100 if (lock && !CRYPTO_THREAD_read_lock(store->lock))
1102 if (lock && !CRYPTO_THREAD_write_lock(prov->flag_lock)) {
1103 CRYPTO_THREAD_unlock(store->lock);
1107 CRYPTO_atomic_add(&prov->activatecnt, -1, &count, prov->activatecnt_lock);
1109 if (count >= 1 && prov->ischild && upcalls) {
1111 * We have had a direct activation in this child libctx so we need to
1112 * now down the ref count in the parent provider. We do the actual down
1113 * ref outside of the flag_lock, since it could involve getting other
1121 prov->flag_activated = 0;
1128 if (removechildren && store != NULL) {
1129 int i, max = sk_OSSL_PROVIDER_CHILD_CB_num(store->child_cbs);
1130 OSSL_PROVIDER_CHILD_CB *child_cb;
1132 for (i = 0; i < max; i++) {
1133 child_cb = sk_OSSL_PROVIDER_CHILD_CB_value(store->child_cbs, i);
1134 child_cb->remove_cb((OSSL_CORE_HANDLE *)prov, child_cb->cbdata);
1139 CRYPTO_THREAD_unlock(prov->flag_lock);
1140 CRYPTO_THREAD_unlock(store->lock);
1142 * This can be done outside the lock. We tolerate other threads getting
1143 * the wrong result briefly when creating OSSL_DECODER_CTXs.
1147 ossl_decoder_cache_flush(prov->libctx);
1152 ossl_provider_free_parent(prov, 1);
1155 /* We don't deinit here, that's done in ossl_provider_free() */
1160 * Activate a provider.
1161 * Return -1 on failure and the activation count on success
1163 static int provider_activate(OSSL_PROVIDER *prov, int lock, int upcalls)
1166 struct provider_store_st *store;
1169 store = prov->store;
1171 * If the provider hasn't been added to the store, then we don't need
1172 * any locks because we've not shared it with other threads.
1174 if (store == NULL) {
1176 if (!provider_init(prov))
1181 if (prov->ischild && upcalls && !ossl_provider_up_ref_parent(prov, 1))
1185 if (lock && !CRYPTO_THREAD_read_lock(store->lock)) {
1187 if (prov->ischild && upcalls)
1188 ossl_provider_free_parent(prov, 1);
1193 if (lock && !CRYPTO_THREAD_write_lock(prov->flag_lock)) {
1194 CRYPTO_THREAD_unlock(store->lock);
1196 if (prov->ischild && upcalls)
1197 ossl_provider_free_parent(prov, 1);
1201 if (CRYPTO_atomic_add(&prov->activatecnt, 1, &count, prov->activatecnt_lock)) {
1202 prov->flag_activated = 1;
1204 if (count == 1 && store != NULL) {
1205 ret = create_provider_children(prov);
1209 CRYPTO_THREAD_unlock(prov->flag_lock);
1210 CRYPTO_THREAD_unlock(store->lock);
1212 * This can be done outside the lock. We tolerate other threads getting
1213 * the wrong result briefly when creating OSSL_DECODER_CTXs.
1217 ossl_decoder_cache_flush(prov->libctx);
1227 static int provider_flush_store_cache(const OSSL_PROVIDER *prov)
1229 struct provider_store_st *store;
1232 if ((store = get_provider_store(prov->libctx)) == NULL)
1235 if (!CRYPTO_THREAD_read_lock(store->lock))
1237 freeing = store->freeing;
1238 CRYPTO_THREAD_unlock(store->lock);
1242 = evp_method_store_cache_flush(prov->libctx)
1244 + ossl_encoder_store_cache_flush(prov->libctx)
1245 + ossl_decoder_store_cache_flush(prov->libctx)
1246 + ossl_store_loader_store_cache_flush(prov->libctx)
1259 static int provider_remove_store_methods(OSSL_PROVIDER *prov)
1261 struct provider_store_st *store;
1264 if ((store = get_provider_store(prov->libctx)) == NULL)
1267 if (!CRYPTO_THREAD_read_lock(store->lock))
1269 freeing = store->freeing;
1270 CRYPTO_THREAD_unlock(store->lock);
1275 if (!CRYPTO_THREAD_write_lock(prov->opbits_lock))
1277 OPENSSL_free(prov->operation_bits);
1278 prov->operation_bits = NULL;
1279 prov->operation_bits_sz = 0;
1280 CRYPTO_THREAD_unlock(prov->opbits_lock);
1282 acc = evp_method_store_remove_all_provided(prov)
1284 + ossl_encoder_store_remove_all_provided(prov)
1285 + ossl_decoder_store_remove_all_provided(prov)
1286 + ossl_store_loader_store_remove_all_provided(prov)
1299 int ossl_provider_activate(OSSL_PROVIDER *prov, int upcalls, int aschild)
1307 * If aschild is true, then we only actually do the activation if the
1308 * provider is a child. If its not, this is still success.
1310 if (aschild && !prov->ischild)
1313 if ((count = provider_activate(prov, 1, upcalls)) > 0)
1314 return count == 1 ? provider_flush_store_cache(prov) : 1;
1319 int ossl_provider_deactivate(OSSL_PROVIDER *prov, int removechildren)
1324 || (count = provider_deactivate(prov, 1, removechildren)) < 0)
1326 return count == 0 ? provider_remove_store_methods(prov) : 1;
1329 void *ossl_provider_ctx(const OSSL_PROVIDER *prov)
1331 return prov != NULL ? prov->provctx : NULL;
1335 * This function only does something once when store->use_fallbacks == 1,
1336 * and then sets store->use_fallbacks = 0, so the second call and so on is
1337 * effectively a no-op.
1339 static int provider_activate_fallbacks(struct provider_store_st *store)
1342 int activated_fallback_count = 0;
1344 const OSSL_PROVIDER_INFO *p;
1346 if (!CRYPTO_THREAD_read_lock(store->lock))
1348 use_fallbacks = store->use_fallbacks;
1349 CRYPTO_THREAD_unlock(store->lock);
1353 if (!CRYPTO_THREAD_write_lock(store->lock))
1355 /* Check again, just in case another thread changed it */
1356 use_fallbacks = store->use_fallbacks;
1357 if (!use_fallbacks) {
1358 CRYPTO_THREAD_unlock(store->lock);
1362 for (p = ossl_predefined_providers; p->name != NULL; p++) {
1363 OSSL_PROVIDER *prov = NULL;
1365 if (!p->is_fallback)
1368 * We use the internal constructor directly here,
1369 * otherwise we get a call loop
1371 prov = provider_new(p->name, p->init, NULL);
1374 prov->libctx = store->libctx;
1376 prov->error_lib = ERR_get_next_error_library();
1380 * We are calling provider_activate while holding the store lock. This
1381 * means the init function will be called while holding a lock. Normally
1382 * we try to avoid calling a user callback while holding a lock.
1383 * However, fallbacks are never third party providers so we accept this.
1385 if (provider_activate(prov, 0, 0) < 0) {
1386 ossl_provider_free(prov);
1389 prov->store = store;
1390 if (sk_OSSL_PROVIDER_push(store->providers, prov) == 0) {
1391 ossl_provider_free(prov);
1394 activated_fallback_count++;
1397 if (activated_fallback_count > 0) {
1398 store->use_fallbacks = 0;
1402 CRYPTO_THREAD_unlock(store->lock);
1406 int ossl_provider_doall_activated(OSSL_LIB_CTX *ctx,
1407 int (*cb)(OSSL_PROVIDER *provider,
1411 int ret = 0, curr, max, ref = 0;
1412 struct provider_store_st *store = get_provider_store(ctx);
1413 STACK_OF(OSSL_PROVIDER) *provs = NULL;
1415 #if !defined(FIPS_MODULE) && !defined(OPENSSL_NO_AUTOLOAD_CONFIG)
1417 * Make sure any providers are loaded from config before we try to use
1420 if (ossl_lib_ctx_is_default(ctx))
1421 OPENSSL_init_crypto(OPENSSL_INIT_LOAD_CONFIG, NULL);
1426 if (!provider_activate_fallbacks(store))
1430 * Under lock, grab a copy of the provider list and up_ref each
1431 * provider so that they don't disappear underneath us.
1433 if (!CRYPTO_THREAD_read_lock(store->lock))
1435 provs = sk_OSSL_PROVIDER_dup(store->providers);
1436 if (provs == NULL) {
1437 CRYPTO_THREAD_unlock(store->lock);
1440 max = sk_OSSL_PROVIDER_num(provs);
1442 * We work backwards through the stack so that we can safely delete items
1445 for (curr = max - 1; curr >= 0; curr--) {
1446 OSSL_PROVIDER *prov = sk_OSSL_PROVIDER_value(provs, curr);
1448 if (!CRYPTO_THREAD_read_lock(prov->flag_lock))
1450 if (prov->flag_activated) {
1452 * We call CRYPTO_UP_REF directly rather than ossl_provider_up_ref
1453 * to avoid upping the ref count on the parent provider, which we
1454 * must not do while holding locks.
1456 if (CRYPTO_UP_REF(&prov->refcnt, &ref) <= 0) {
1457 CRYPTO_THREAD_unlock(prov->flag_lock);
1461 * It's already activated, but we up the activated count to ensure
1462 * it remains activated until after we've called the user callback.
1463 * In theory this could mean the parent provider goes inactive,
1464 * whilst still activated in the child for a short period. That's ok.
1466 if (!CRYPTO_atomic_add(&prov->activatecnt, 1, &ref,
1467 prov->activatecnt_lock)) {
1468 CRYPTO_DOWN_REF(&prov->refcnt, &ref);
1469 CRYPTO_THREAD_unlock(prov->flag_lock);
1473 sk_OSSL_PROVIDER_delete(provs, curr);
1476 CRYPTO_THREAD_unlock(prov->flag_lock);
1478 CRYPTO_THREAD_unlock(store->lock);
1481 * Now, we sweep through all providers not under lock
1483 for (curr = 0; curr < max; curr++) {
1484 OSSL_PROVIDER *prov = sk_OSSL_PROVIDER_value(provs, curr);
1486 if (!cb(prov, cbdata)) {
1497 CRYPTO_THREAD_unlock(store->lock);
1500 * The pop_free call doesn't do what we want on an error condition. We
1501 * either start from the first item in the stack, or part way through if
1502 * we only processed some of the items.
1504 for (curr++; curr < max; curr++) {
1505 OSSL_PROVIDER *prov = sk_OSSL_PROVIDER_value(provs, curr);
1507 if (!CRYPTO_atomic_add(&prov->activatecnt, -1, &ref,
1508 prov->activatecnt_lock)) {
1514 * Looks like we need to deactivate properly. We could just have
1515 * done this originally, but it involves taking a write lock so
1516 * we avoid it. We up the count again and do a full deactivation
1518 if (CRYPTO_atomic_add(&prov->activatecnt, 1, &ref,
1519 prov->activatecnt_lock))
1520 provider_deactivate(prov, 0, 1);
1525 * As above where we did the up-ref, we don't call ossl_provider_free
1526 * to avoid making upcalls. There should always be at least one ref
1527 * to the provider in the store, so this should never drop to 0.
1529 if (!CRYPTO_DOWN_REF(&prov->refcnt, &ref)) {
1534 * Not much we can do if this assert ever fails. So we don't use
1539 sk_OSSL_PROVIDER_free(provs);
1543 int OSSL_PROVIDER_available(OSSL_LIB_CTX *libctx, const char *name)
1545 OSSL_PROVIDER *prov = NULL;
1547 struct provider_store_st *store = get_provider_store(libctx);
1549 if (store == NULL || !provider_activate_fallbacks(store))
1552 prov = ossl_provider_find(libctx, name, 0);
1554 if (!CRYPTO_THREAD_read_lock(prov->flag_lock))
1556 available = prov->flag_activated;
1557 CRYPTO_THREAD_unlock(prov->flag_lock);
1558 ossl_provider_free(prov);
1563 /* Getters of Provider Object data */
1564 const char *ossl_provider_name(const OSSL_PROVIDER *prov)
1569 const DSO *ossl_provider_dso(const OSSL_PROVIDER *prov)
1571 return prov->module;
1574 const char *ossl_provider_module_name(const OSSL_PROVIDER *prov)
1579 return DSO_get_filename(prov->module);
1583 const char *ossl_provider_module_path(const OSSL_PROVIDER *prov)
1588 /* FIXME: Ensure it's a full path */
1589 return DSO_get_filename(prov->module);
1593 void *ossl_provider_prov_ctx(const OSSL_PROVIDER *prov)
1596 return prov->provctx;
1601 const OSSL_DISPATCH *ossl_provider_get0_dispatch(const OSSL_PROVIDER *prov)
1604 return prov->dispatch;
1609 OSSL_LIB_CTX *ossl_provider_libctx(const OSSL_PROVIDER *prov)
1611 return prov != NULL ? prov->libctx : NULL;
1614 /* Wrappers around calls to the provider */
1615 void ossl_provider_teardown(const OSSL_PROVIDER *prov)
1617 if (prov->teardown != NULL
1622 prov->teardown(prov->provctx);
1625 const OSSL_PARAM *ossl_provider_gettable_params(const OSSL_PROVIDER *prov)
1627 return prov->gettable_params == NULL
1628 ? NULL : prov->gettable_params(prov->provctx);
1631 int ossl_provider_get_params(const OSSL_PROVIDER *prov, OSSL_PARAM params[])
1633 return prov->get_params == NULL
1634 ? 0 : prov->get_params(prov->provctx, params);
1637 int ossl_provider_self_test(const OSSL_PROVIDER *prov)
1641 if (prov->self_test == NULL)
1643 ret = prov->self_test(prov->provctx);
1645 (void)provider_remove_store_methods((OSSL_PROVIDER *)prov);
1649 int ossl_provider_get_capabilities(const OSSL_PROVIDER *prov,
1650 const char *capability,
1654 return prov->get_capabilities == NULL
1655 ? 1 : prov->get_capabilities(prov->provctx, capability, cb, arg);
1658 const OSSL_ALGORITHM *ossl_provider_query_operation(const OSSL_PROVIDER *prov,
1662 const OSSL_ALGORITHM *res;
1664 if (prov->query_operation == NULL)
1666 res = prov->query_operation(prov->provctx, operation_id, no_cache);
1667 #if defined(OPENSSL_NO_CACHED_FETCH)
1668 /* Forcing the non-caching of queries */
1669 if (no_cache != NULL)
1675 void ossl_provider_unquery_operation(const OSSL_PROVIDER *prov,
1677 const OSSL_ALGORITHM *algs)
1679 if (prov->unquery_operation != NULL)
1680 prov->unquery_operation(prov->provctx, operation_id, algs);
1683 int ossl_provider_set_operation_bit(OSSL_PROVIDER *provider, size_t bitnum)
1685 size_t byte = bitnum / 8;
1686 unsigned char bit = (1 << (bitnum % 8)) & 0xFF;
1688 if (!CRYPTO_THREAD_write_lock(provider->opbits_lock))
1690 if (provider->operation_bits_sz <= byte) {
1691 unsigned char *tmp = OPENSSL_realloc(provider->operation_bits,
1695 CRYPTO_THREAD_unlock(provider->opbits_lock);
1698 provider->operation_bits = tmp;
1699 memset(provider->operation_bits + provider->operation_bits_sz,
1700 '\0', byte + 1 - provider->operation_bits_sz);
1701 provider->operation_bits_sz = byte + 1;
1703 provider->operation_bits[byte] |= bit;
1704 CRYPTO_THREAD_unlock(provider->opbits_lock);
1708 int ossl_provider_test_operation_bit(OSSL_PROVIDER *provider, size_t bitnum,
1711 size_t byte = bitnum / 8;
1712 unsigned char bit = (1 << (bitnum % 8)) & 0xFF;
1714 if (!ossl_assert(result != NULL)) {
1715 ERR_raise(ERR_LIB_CRYPTO, ERR_R_PASSED_NULL_PARAMETER);
1720 if (!CRYPTO_THREAD_read_lock(provider->opbits_lock))
1722 if (provider->operation_bits_sz > byte)
1723 *result = ((provider->operation_bits[byte] & bit) != 0);
1724 CRYPTO_THREAD_unlock(provider->opbits_lock);
1729 const OSSL_CORE_HANDLE *ossl_provider_get_parent(OSSL_PROVIDER *prov)
1731 return prov->handle;
1734 int ossl_provider_is_child(const OSSL_PROVIDER *prov)
1736 return prov->ischild;
1739 int ossl_provider_set_child(OSSL_PROVIDER *prov, const OSSL_CORE_HANDLE *handle)
1741 prov->handle = handle;
1747 int ossl_provider_default_props_update(OSSL_LIB_CTX *libctx, const char *props)
1750 struct provider_store_st *store = NULL;
1752 OSSL_PROVIDER_CHILD_CB *child_cb;
1754 if ((store = get_provider_store(libctx)) == NULL)
1757 if (!CRYPTO_THREAD_read_lock(store->lock))
1760 max = sk_OSSL_PROVIDER_CHILD_CB_num(store->child_cbs);
1761 for (i = 0; i < max; i++) {
1762 child_cb = sk_OSSL_PROVIDER_CHILD_CB_value(store->child_cbs, i);
1763 child_cb->global_props_cb(props, child_cb->cbdata);
1766 CRYPTO_THREAD_unlock(store->lock);
1771 static int ossl_provider_register_child_cb(const OSSL_CORE_HANDLE *handle,
1773 const OSSL_CORE_HANDLE *provider,
1776 const OSSL_CORE_HANDLE *provider,
1778 int (*global_props_cb)(
1784 * This is really an OSSL_PROVIDER that we created and cast to
1785 * OSSL_CORE_HANDLE originally. Therefore it is safe to cast it back.
1787 OSSL_PROVIDER *thisprov = (OSSL_PROVIDER *)handle;
1788 OSSL_PROVIDER *prov;
1789 OSSL_LIB_CTX *libctx = thisprov->libctx;
1790 struct provider_store_st *store = NULL;
1791 int ret = 0, i, max;
1792 OSSL_PROVIDER_CHILD_CB *child_cb;
1793 char *propsstr = NULL;
1795 if ((store = get_provider_store(libctx)) == NULL)
1798 child_cb = OPENSSL_malloc(sizeof(*child_cb));
1799 if (child_cb == NULL)
1801 child_cb->prov = thisprov;
1802 child_cb->create_cb = create_cb;
1803 child_cb->remove_cb = remove_cb;
1804 child_cb->global_props_cb = global_props_cb;
1805 child_cb->cbdata = cbdata;
1807 if (!CRYPTO_THREAD_write_lock(store->lock)) {
1808 OPENSSL_free(child_cb);
1811 propsstr = evp_get_global_properties_str(libctx, 0);
1813 if (propsstr != NULL) {
1814 global_props_cb(propsstr, cbdata);
1815 OPENSSL_free(propsstr);
1817 max = sk_OSSL_PROVIDER_num(store->providers);
1818 for (i = 0; i < max; i++) {
1821 prov = sk_OSSL_PROVIDER_value(store->providers, i);
1823 if (!CRYPTO_THREAD_read_lock(prov->flag_lock))
1825 activated = prov->flag_activated;
1826 CRYPTO_THREAD_unlock(prov->flag_lock);
1828 * We hold the store lock while calling the user callback. This means
1829 * that the user callback must be short and simple and not do anything
1830 * likely to cause a deadlock. We don't hold the flag_lock during this
1831 * call. In theory this means that another thread could deactivate it
1832 * while we are calling create. This is ok because the other thread
1833 * will also call remove_cb, but won't be able to do so until we release
1836 if (activated && !create_cb((OSSL_CORE_HANDLE *)prov, cbdata))
1841 ret = sk_OSSL_PROVIDER_CHILD_CB_push(store->child_cbs, child_cb);
1843 if (i != max || ret <= 0) {
1844 /* Failed during creation. Remove everything we just added */
1845 for (; i >= 0; i--) {
1846 prov = sk_OSSL_PROVIDER_value(store->providers, i);
1847 remove_cb((OSSL_CORE_HANDLE *)prov, cbdata);
1849 OPENSSL_free(child_cb);
1852 CRYPTO_THREAD_unlock(store->lock);
1857 static void ossl_provider_deregister_child_cb(const OSSL_CORE_HANDLE *handle)
1860 * This is really an OSSL_PROVIDER that we created and cast to
1861 * OSSL_CORE_HANDLE originally. Therefore it is safe to cast it back.
1863 OSSL_PROVIDER *thisprov = (OSSL_PROVIDER *)handle;
1864 OSSL_LIB_CTX *libctx = thisprov->libctx;
1865 struct provider_store_st *store = NULL;
1867 OSSL_PROVIDER_CHILD_CB *child_cb;
1869 if ((store = get_provider_store(libctx)) == NULL)
1872 if (!CRYPTO_THREAD_write_lock(store->lock))
1874 max = sk_OSSL_PROVIDER_CHILD_CB_num(store->child_cbs);
1875 for (i = 0; i < max; i++) {
1876 child_cb = sk_OSSL_PROVIDER_CHILD_CB_value(store->child_cbs, i);
1877 if (child_cb->prov == thisprov) {
1878 /* Found an entry */
1879 sk_OSSL_PROVIDER_CHILD_CB_delete(store->child_cbs, i);
1880 OPENSSL_free(child_cb);
1884 CRYPTO_THREAD_unlock(store->lock);
1889 * Core functions for the provider
1890 * ===============================
1892 * This is the set of functions that the core makes available to the provider
1896 * This returns a list of Provider Object parameters with their types, for
1897 * discovery. We do not expect that many providers will use this, but one
1900 static const OSSL_PARAM param_types[] = {
1901 OSSL_PARAM_DEFN(OSSL_PROV_PARAM_CORE_VERSION, OSSL_PARAM_UTF8_PTR, NULL, 0),
1902 OSSL_PARAM_DEFN(OSSL_PROV_PARAM_CORE_PROV_NAME, OSSL_PARAM_UTF8_PTR,
1905 OSSL_PARAM_DEFN(OSSL_PROV_PARAM_CORE_MODULE_FILENAME, OSSL_PARAM_UTF8_PTR,
1912 * Forward declare all the functions that are provided aa dispatch.
1913 * This ensures that the compiler will complain if they aren't defined
1914 * with the correct signature.
1916 static OSSL_FUNC_core_gettable_params_fn core_gettable_params;
1917 static OSSL_FUNC_core_get_params_fn core_get_params;
1918 static OSSL_FUNC_core_get_libctx_fn core_get_libctx;
1919 static OSSL_FUNC_core_thread_start_fn core_thread_start;
1921 static OSSL_FUNC_core_new_error_fn core_new_error;
1922 static OSSL_FUNC_core_set_error_debug_fn core_set_error_debug;
1923 static OSSL_FUNC_core_vset_error_fn core_vset_error;
1924 static OSSL_FUNC_core_set_error_mark_fn core_set_error_mark;
1925 static OSSL_FUNC_core_clear_last_error_mark_fn core_clear_last_error_mark;
1926 static OSSL_FUNC_core_pop_error_to_mark_fn core_pop_error_to_mark;
1927 OSSL_FUNC_BIO_new_file_fn ossl_core_bio_new_file;
1928 OSSL_FUNC_BIO_new_membuf_fn ossl_core_bio_new_mem_buf;
1929 OSSL_FUNC_BIO_read_ex_fn ossl_core_bio_read_ex;
1930 OSSL_FUNC_BIO_write_ex_fn ossl_core_bio_write_ex;
1931 OSSL_FUNC_BIO_gets_fn ossl_core_bio_gets;
1932 OSSL_FUNC_BIO_puts_fn ossl_core_bio_puts;
1933 OSSL_FUNC_BIO_up_ref_fn ossl_core_bio_up_ref;
1934 OSSL_FUNC_BIO_free_fn ossl_core_bio_free;
1935 OSSL_FUNC_BIO_vprintf_fn ossl_core_bio_vprintf;
1936 OSSL_FUNC_BIO_vsnprintf_fn BIO_vsnprintf;
1937 static OSSL_FUNC_self_test_cb_fn core_self_test_get_callback;
1938 static OSSL_FUNC_get_entropy_fn rand_get_entropy;
1939 static OSSL_FUNC_get_user_entropy_fn rand_get_user_entropy;
1940 static OSSL_FUNC_cleanup_entropy_fn rand_cleanup_entropy;
1941 static OSSL_FUNC_cleanup_user_entropy_fn rand_cleanup_user_entropy;
1942 static OSSL_FUNC_get_nonce_fn rand_get_nonce;
1943 static OSSL_FUNC_get_user_nonce_fn rand_get_user_nonce;
1944 static OSSL_FUNC_cleanup_nonce_fn rand_cleanup_nonce;
1945 static OSSL_FUNC_cleanup_user_nonce_fn rand_cleanup_user_nonce;
1947 OSSL_FUNC_CRYPTO_malloc_fn CRYPTO_malloc;
1948 OSSL_FUNC_CRYPTO_zalloc_fn CRYPTO_zalloc;
1949 OSSL_FUNC_CRYPTO_free_fn CRYPTO_free;
1950 OSSL_FUNC_CRYPTO_clear_free_fn CRYPTO_clear_free;
1951 OSSL_FUNC_CRYPTO_realloc_fn CRYPTO_realloc;
1952 OSSL_FUNC_CRYPTO_clear_realloc_fn CRYPTO_clear_realloc;
1953 OSSL_FUNC_CRYPTO_secure_malloc_fn CRYPTO_secure_malloc;
1954 OSSL_FUNC_CRYPTO_secure_zalloc_fn CRYPTO_secure_zalloc;
1955 OSSL_FUNC_CRYPTO_secure_free_fn CRYPTO_secure_free;
1956 OSSL_FUNC_CRYPTO_secure_clear_free_fn CRYPTO_secure_clear_free;
1957 OSSL_FUNC_CRYPTO_secure_allocated_fn CRYPTO_secure_allocated;
1958 OSSL_FUNC_OPENSSL_cleanse_fn OPENSSL_cleanse;
1960 OSSL_FUNC_provider_register_child_cb_fn ossl_provider_register_child_cb;
1961 OSSL_FUNC_provider_deregister_child_cb_fn ossl_provider_deregister_child_cb;
1962 static OSSL_FUNC_provider_name_fn core_provider_get0_name;
1963 static OSSL_FUNC_provider_get0_provider_ctx_fn core_provider_get0_provider_ctx;
1964 static OSSL_FUNC_provider_get0_dispatch_fn core_provider_get0_dispatch;
1965 static OSSL_FUNC_provider_up_ref_fn core_provider_up_ref_intern;
1966 static OSSL_FUNC_provider_free_fn core_provider_free_intern;
1967 static OSSL_FUNC_core_obj_add_sigid_fn core_obj_add_sigid;
1968 static OSSL_FUNC_core_obj_create_fn core_obj_create;
1971 static const OSSL_PARAM *core_gettable_params(const OSSL_CORE_HANDLE *handle)
1976 static int core_get_params(const OSSL_CORE_HANDLE *handle, OSSL_PARAM params[])
1981 * We created this object originally and we know it is actually an
1982 * OSSL_PROVIDER *, so the cast is safe
1984 OSSL_PROVIDER *prov = (OSSL_PROVIDER *)handle;
1986 if ((p = OSSL_PARAM_locate(params, OSSL_PROV_PARAM_CORE_VERSION)) != NULL)
1987 OSSL_PARAM_set_utf8_ptr(p, OPENSSL_VERSION_STR);
1988 if ((p = OSSL_PARAM_locate(params, OSSL_PROV_PARAM_CORE_PROV_NAME)) != NULL)
1989 OSSL_PARAM_set_utf8_ptr(p, prov->name);
1992 if ((p = OSSL_PARAM_locate(params,
1993 OSSL_PROV_PARAM_CORE_MODULE_FILENAME)) != NULL)
1994 OSSL_PARAM_set_utf8_ptr(p, ossl_provider_module_path(prov));
1997 if (prov->parameters == NULL)
2000 for (i = 0; i < sk_INFOPAIR_num(prov->parameters); i++) {
2001 INFOPAIR *pair = sk_INFOPAIR_value(prov->parameters, i);
2003 if ((p = OSSL_PARAM_locate(params, pair->name)) != NULL)
2004 OSSL_PARAM_set_utf8_ptr(p, pair->value);
2009 static OPENSSL_CORE_CTX *core_get_libctx(const OSSL_CORE_HANDLE *handle)
2012 * We created this object originally and we know it is actually an
2013 * OSSL_PROVIDER *, so the cast is safe
2015 OSSL_PROVIDER *prov = (OSSL_PROVIDER *)handle;
2018 * Using ossl_provider_libctx would be wrong as that returns
2019 * NULL for |prov| == NULL and NULL libctx has a special meaning
2020 * that does not apply here. Here |prov| == NULL can happen only in
2021 * case of a coding error.
2023 assert(prov != NULL);
2024 return (OPENSSL_CORE_CTX *)prov->libctx;
2027 static int core_thread_start(const OSSL_CORE_HANDLE *handle,
2028 OSSL_thread_stop_handler_fn handfn,
2032 * We created this object originally and we know it is actually an
2033 * OSSL_PROVIDER *, so the cast is safe
2035 OSSL_PROVIDER *prov = (OSSL_PROVIDER *)handle;
2037 return ossl_init_thread_start(prov, arg, handfn);
2041 * The FIPS module inner provider doesn't implement these. They aren't
2042 * needed there, since the FIPS module upcalls are always the outer provider
2047 * These error functions should use |handle| to select the proper
2048 * library context to report in the correct error stack if error
2049 * stacks become tied to the library context.
2050 * We cannot currently do that since there's no support for it in the
2053 static void core_new_error(const OSSL_CORE_HANDLE *handle)
2058 static void core_set_error_debug(const OSSL_CORE_HANDLE *handle,
2059 const char *file, int line, const char *func)
2061 ERR_set_debug(file, line, func);
2064 static void core_vset_error(const OSSL_CORE_HANDLE *handle,
2065 uint32_t reason, const char *fmt, va_list args)
2068 * We created this object originally and we know it is actually an
2069 * OSSL_PROVIDER *, so the cast is safe
2071 OSSL_PROVIDER *prov = (OSSL_PROVIDER *)handle;
2074 * If the uppermost 8 bits are non-zero, it's an OpenSSL library
2075 * error and will be treated as such. Otherwise, it's a new style
2076 * provider error and will be treated as such.
2078 if (ERR_GET_LIB(reason) != 0) {
2079 ERR_vset_error(ERR_GET_LIB(reason), ERR_GET_REASON(reason), fmt, args);
2081 ERR_vset_error(prov->error_lib, (int)reason, fmt, args);
2085 static int core_set_error_mark(const OSSL_CORE_HANDLE *handle)
2087 return ERR_set_mark();
2090 static int core_clear_last_error_mark(const OSSL_CORE_HANDLE *handle)
2092 return ERR_clear_last_mark();
2095 static int core_pop_error_to_mark(const OSSL_CORE_HANDLE *handle)
2097 return ERR_pop_to_mark();
2100 static void core_self_test_get_callback(OPENSSL_CORE_CTX *libctx,
2101 OSSL_CALLBACK **cb, void **cbarg)
2103 OSSL_SELF_TEST_get_callback((OSSL_LIB_CTX *)libctx, cb, cbarg);
2106 static size_t rand_get_entropy(const OSSL_CORE_HANDLE *handle,
2107 unsigned char **pout, int entropy,
2108 size_t min_len, size_t max_len)
2110 return ossl_rand_get_entropy((OSSL_LIB_CTX *)core_get_libctx(handle),
2111 pout, entropy, min_len, max_len);
2114 static size_t rand_get_user_entropy(const OSSL_CORE_HANDLE *handle,
2115 unsigned char **pout, int entropy,
2116 size_t min_len, size_t max_len)
2118 return ossl_rand_get_user_entropy((OSSL_LIB_CTX *)core_get_libctx(handle),
2119 pout, entropy, min_len, max_len);
2122 static void rand_cleanup_entropy(const OSSL_CORE_HANDLE *handle,
2123 unsigned char *buf, size_t len)
2125 ossl_rand_cleanup_entropy((OSSL_LIB_CTX *)core_get_libctx(handle),
2129 static void rand_cleanup_user_entropy(const OSSL_CORE_HANDLE *handle,
2130 unsigned char *buf, size_t len)
2132 ossl_rand_cleanup_user_entropy((OSSL_LIB_CTX *)core_get_libctx(handle),
2136 static size_t rand_get_nonce(const OSSL_CORE_HANDLE *handle,
2137 unsigned char **pout,
2138 size_t min_len, size_t max_len,
2139 const void *salt, size_t salt_len)
2141 return ossl_rand_get_nonce((OSSL_LIB_CTX *)core_get_libctx(handle),
2142 pout, min_len, max_len, salt, salt_len);
2145 static size_t rand_get_user_nonce(const OSSL_CORE_HANDLE *handle,
2146 unsigned char **pout,
2147 size_t min_len, size_t max_len,
2148 const void *salt, size_t salt_len)
2150 return ossl_rand_get_user_nonce((OSSL_LIB_CTX *)core_get_libctx(handle),
2151 pout, min_len, max_len, salt, salt_len);
2154 static void rand_cleanup_nonce(const OSSL_CORE_HANDLE *handle,
2155 unsigned char *buf, size_t len)
2157 ossl_rand_cleanup_nonce((OSSL_LIB_CTX *)core_get_libctx(handle),
2161 static void rand_cleanup_user_nonce(const OSSL_CORE_HANDLE *handle,
2162 unsigned char *buf, size_t len)
2164 ossl_rand_cleanup_user_nonce((OSSL_LIB_CTX *)core_get_libctx(handle),
2168 static const char *core_provider_get0_name(const OSSL_CORE_HANDLE *prov)
2170 return OSSL_PROVIDER_get0_name((const OSSL_PROVIDER *)prov);
2173 static void *core_provider_get0_provider_ctx(const OSSL_CORE_HANDLE *prov)
2175 return OSSL_PROVIDER_get0_provider_ctx((const OSSL_PROVIDER *)prov);
2178 static const OSSL_DISPATCH *
2179 core_provider_get0_dispatch(const OSSL_CORE_HANDLE *prov)
2181 return OSSL_PROVIDER_get0_dispatch((const OSSL_PROVIDER *)prov);
2184 static int core_provider_up_ref_intern(const OSSL_CORE_HANDLE *prov,
2187 return provider_up_ref_intern((OSSL_PROVIDER *)prov, activate);
2190 static int core_provider_free_intern(const OSSL_CORE_HANDLE *prov,
2193 return provider_free_intern((OSSL_PROVIDER *)prov, deactivate);
2196 static int core_obj_add_sigid(const OSSL_CORE_HANDLE *prov,
2197 const char *sign_name, const char *digest_name,
2198 const char *pkey_name)
2200 int sign_nid = OBJ_txt2nid(sign_name);
2201 int digest_nid = NID_undef;
2202 int pkey_nid = OBJ_txt2nid(pkey_name);
2204 if (digest_name != NULL && digest_name[0] != '\0'
2205 && (digest_nid = OBJ_txt2nid(digest_name)) == NID_undef)
2208 if (sign_nid == NID_undef)
2212 * Check if it already exists. This is a success if so (even if we don't
2213 * have nids for the digest/pkey)
2215 if (OBJ_find_sigid_algs(sign_nid, NULL, NULL))
2218 if (pkey_nid == NID_undef)
2221 return OBJ_add_sigid(sign_nid, digest_nid, pkey_nid);
2224 static int core_obj_create(const OSSL_CORE_HANDLE *prov, const char *oid,
2225 const char *sn, const char *ln)
2227 /* Check if it already exists and create it if not */
2228 return OBJ_txt2nid(oid) != NID_undef
2229 || OBJ_create(oid, sn, ln) != NID_undef;
2231 #endif /* FIPS_MODULE */
2234 * Functions provided by the core.
2236 static const OSSL_DISPATCH core_dispatch_[] = {
2237 { OSSL_FUNC_CORE_GETTABLE_PARAMS, (void (*)(void))core_gettable_params },
2238 { OSSL_FUNC_CORE_GET_PARAMS, (void (*)(void))core_get_params },
2239 { OSSL_FUNC_CORE_GET_LIBCTX, (void (*)(void))core_get_libctx },
2240 { OSSL_FUNC_CORE_THREAD_START, (void (*)(void))core_thread_start },
2242 { OSSL_FUNC_CORE_NEW_ERROR, (void (*)(void))core_new_error },
2243 { OSSL_FUNC_CORE_SET_ERROR_DEBUG, (void (*)(void))core_set_error_debug },
2244 { OSSL_FUNC_CORE_VSET_ERROR, (void (*)(void))core_vset_error },
2245 { OSSL_FUNC_CORE_SET_ERROR_MARK, (void (*)(void))core_set_error_mark },
2246 { OSSL_FUNC_CORE_CLEAR_LAST_ERROR_MARK,
2247 (void (*)(void))core_clear_last_error_mark },
2248 { OSSL_FUNC_CORE_POP_ERROR_TO_MARK, (void (*)(void))core_pop_error_to_mark },
2249 { OSSL_FUNC_BIO_NEW_FILE, (void (*)(void))ossl_core_bio_new_file },
2250 { OSSL_FUNC_BIO_NEW_MEMBUF, (void (*)(void))ossl_core_bio_new_mem_buf },
2251 { OSSL_FUNC_BIO_READ_EX, (void (*)(void))ossl_core_bio_read_ex },
2252 { OSSL_FUNC_BIO_WRITE_EX, (void (*)(void))ossl_core_bio_write_ex },
2253 { OSSL_FUNC_BIO_GETS, (void (*)(void))ossl_core_bio_gets },
2254 { OSSL_FUNC_BIO_PUTS, (void (*)(void))ossl_core_bio_puts },
2255 { OSSL_FUNC_BIO_CTRL, (void (*)(void))ossl_core_bio_ctrl },
2256 { OSSL_FUNC_BIO_UP_REF, (void (*)(void))ossl_core_bio_up_ref },
2257 { OSSL_FUNC_BIO_FREE, (void (*)(void))ossl_core_bio_free },
2258 { OSSL_FUNC_BIO_VPRINTF, (void (*)(void))ossl_core_bio_vprintf },
2259 { OSSL_FUNC_BIO_VSNPRINTF, (void (*)(void))BIO_vsnprintf },
2260 { OSSL_FUNC_SELF_TEST_CB, (void (*)(void))core_self_test_get_callback },
2261 { OSSL_FUNC_GET_ENTROPY, (void (*)(void))rand_get_entropy },
2262 { OSSL_FUNC_GET_USER_ENTROPY, (void (*)(void))rand_get_user_entropy },
2263 { OSSL_FUNC_CLEANUP_ENTROPY, (void (*)(void))rand_cleanup_entropy },
2264 { OSSL_FUNC_CLEANUP_USER_ENTROPY, (void (*)(void))rand_cleanup_user_entropy },
2265 { OSSL_FUNC_GET_NONCE, (void (*)(void))rand_get_nonce },
2266 { OSSL_FUNC_GET_USER_NONCE, (void (*)(void))rand_get_user_nonce },
2267 { OSSL_FUNC_CLEANUP_NONCE, (void (*)(void))rand_cleanup_nonce },
2268 { OSSL_FUNC_CLEANUP_USER_NONCE, (void (*)(void))rand_cleanup_user_nonce },
2270 { OSSL_FUNC_CRYPTO_MALLOC, (void (*)(void))CRYPTO_malloc },
2271 { OSSL_FUNC_CRYPTO_ZALLOC, (void (*)(void))CRYPTO_zalloc },
2272 { OSSL_FUNC_CRYPTO_FREE, (void (*)(void))CRYPTO_free },
2273 { OSSL_FUNC_CRYPTO_CLEAR_FREE, (void (*)(void))CRYPTO_clear_free },
2274 { OSSL_FUNC_CRYPTO_REALLOC, (void (*)(void))CRYPTO_realloc },
2275 { OSSL_FUNC_CRYPTO_CLEAR_REALLOC, (void (*)(void))CRYPTO_clear_realloc },
2276 { OSSL_FUNC_CRYPTO_SECURE_MALLOC, (void (*)(void))CRYPTO_secure_malloc },
2277 { OSSL_FUNC_CRYPTO_SECURE_ZALLOC, (void (*)(void))CRYPTO_secure_zalloc },
2278 { OSSL_FUNC_CRYPTO_SECURE_FREE, (void (*)(void))CRYPTO_secure_free },
2279 { OSSL_FUNC_CRYPTO_SECURE_CLEAR_FREE,
2280 (void (*)(void))CRYPTO_secure_clear_free },
2281 { OSSL_FUNC_CRYPTO_SECURE_ALLOCATED,
2282 (void (*)(void))CRYPTO_secure_allocated },
2283 { OSSL_FUNC_OPENSSL_CLEANSE, (void (*)(void))OPENSSL_cleanse },
2285 { OSSL_FUNC_PROVIDER_REGISTER_CHILD_CB,
2286 (void (*)(void))ossl_provider_register_child_cb },
2287 { OSSL_FUNC_PROVIDER_DEREGISTER_CHILD_CB,
2288 (void (*)(void))ossl_provider_deregister_child_cb },
2289 { OSSL_FUNC_PROVIDER_NAME,
2290 (void (*)(void))core_provider_get0_name },
2291 { OSSL_FUNC_PROVIDER_GET0_PROVIDER_CTX,
2292 (void (*)(void))core_provider_get0_provider_ctx },
2293 { OSSL_FUNC_PROVIDER_GET0_DISPATCH,
2294 (void (*)(void))core_provider_get0_dispatch },
2295 { OSSL_FUNC_PROVIDER_UP_REF,
2296 (void (*)(void))core_provider_up_ref_intern },
2297 { OSSL_FUNC_PROVIDER_FREE,
2298 (void (*)(void))core_provider_free_intern },
2299 { OSSL_FUNC_CORE_OBJ_ADD_SIGID, (void (*)(void))core_obj_add_sigid },
2300 { OSSL_FUNC_CORE_OBJ_CREATE, (void (*)(void))core_obj_create },
2304 static const OSSL_DISPATCH *core_dispatch = core_dispatch_;