2 * Copyright 2019-2021 The OpenSSL Project Authors. All Rights Reserved.
4 * Licensed under the Apache License 2.0 (the "License"). You may not use
5 * this file except in compliance with the License. You can obtain a copy
6 * in the file LICENSE in the source distribution or at
7 * https://www.openssl.org/source/license.html
11 #include <openssl/core.h>
12 #include <openssl/core_dispatch.h>
13 #include <openssl/core_names.h>
14 #include <openssl/provider.h>
15 #include <openssl/params.h>
16 #include <openssl/opensslv.h>
17 #include "crypto/cryptlib.h"
18 #include "crypto/evp.h" /* evp_method_store_flush */
19 #include "crypto/rand.h"
20 #include "internal/nelem.h"
21 #include "internal/thread_once.h"
22 #include "internal/provider.h"
23 #include "internal/refcount.h"
24 #include "internal/bio.h"
25 #include "provider_local.h"
27 # include <openssl/self_test.h>
30 static OSSL_PROVIDER *provider_new(const char *name,
31 OSSL_provider_init_fn *init_function);
34 * Provider Object structure
35 * =========================
42 DEFINE_STACK_OF(INFOPAIR)
44 struct provider_store_st; /* Forward declaration */
46 struct ossl_provider_st {
48 unsigned int flag_initialized:1;
49 unsigned int flag_activated:1;
50 unsigned int flag_fallback:1; /* Can be used as fallback */
52 /* Getting and setting the flags require synchronization */
53 CRYPTO_RWLOCK *flag_lock;
55 /* OpenSSL library side data */
56 CRYPTO_REF_COUNT refcnt;
57 CRYPTO_RWLOCK *refcnt_lock; /* For the ref counter */
62 OSSL_provider_init_fn *init_function;
63 STACK_OF(INFOPAIR) *parameters;
64 OSSL_LIB_CTX *libctx; /* The library context this instance is in */
65 struct provider_store_st *store; /* The store this instance belongs to */
68 * In the FIPS module inner provider, this isn't needed, since the
69 * error upcalls are always direct calls to the outer provider.
71 int error_lib; /* ERR library number, one for each provider */
72 # ifndef OPENSSL_NO_ERR
73 ERR_STRING_DATA *error_strings; /* Copy of what the provider gives us */
77 /* Provider side functions */
78 OSSL_FUNC_provider_teardown_fn *teardown;
79 OSSL_FUNC_provider_gettable_params_fn *gettable_params;
80 OSSL_FUNC_provider_get_params_fn *get_params;
81 OSSL_FUNC_provider_get_capabilities_fn *get_capabilities;
82 OSSL_FUNC_provider_self_test_fn *self_test;
83 OSSL_FUNC_provider_query_operation_fn *query_operation;
84 OSSL_FUNC_provider_unquery_operation_fn *unquery_operation;
87 * Cache of bit to indicate of query_operation() has been called on
88 * a specific operation or not.
90 unsigned char *operation_bits;
91 size_t operation_bits_sz;
92 CRYPTO_RWLOCK *opbits_lock;
94 /* Provider side data */
97 DEFINE_STACK_OF(OSSL_PROVIDER)
99 static int ossl_provider_cmp(const OSSL_PROVIDER * const *a,
100 const OSSL_PROVIDER * const *b)
102 return strcmp((*a)->name, (*b)->name);
106 * Provider Object store
107 * =====================
109 * The Provider Object store is a library context object, and therefore needs
113 struct provider_store_st {
114 STACK_OF(OSSL_PROVIDER) *providers;
115 CRYPTO_RWLOCK *default_path_lock;
118 unsigned int use_fallbacks:1;
119 unsigned int freeing:1;
123 * provider_deactivate_free() is a wrapper around ossl_provider_deactivate()
124 * and ossl_provider_free(), called as needed.
125 * Since this is only called when the provider store is being emptied, we
126 * don't need to care about any lock.
128 static void provider_deactivate_free(OSSL_PROVIDER *prov)
130 if (prov->flag_activated)
131 ossl_provider_deactivate(prov);
132 ossl_provider_free(prov);
135 static void provider_store_free(void *vstore)
137 struct provider_store_st *store = vstore;
142 OPENSSL_free(store->default_path);
143 sk_OSSL_PROVIDER_pop_free(store->providers, provider_deactivate_free);
144 CRYPTO_THREAD_lock_free(store->default_path_lock);
145 CRYPTO_THREAD_lock_free(store->lock);
149 static void *provider_store_new(OSSL_LIB_CTX *ctx)
151 struct provider_store_st *store = OPENSSL_zalloc(sizeof(*store));
152 const struct predefined_providers_st *p = NULL;
155 || (store->providers = sk_OSSL_PROVIDER_new(ossl_provider_cmp)) == NULL
156 || (store->default_path_lock = CRYPTO_THREAD_lock_new()) == NULL
157 || (store->lock = CRYPTO_THREAD_lock_new()) == NULL) {
158 provider_store_free(store);
161 store->use_fallbacks = 1;
163 for (p = ossl_predefined_providers; p->name != NULL; p++) {
164 OSSL_PROVIDER *prov = NULL;
167 * We use the internal constructor directly here,
168 * otherwise we get a call loop
170 prov = provider_new(p->name, p->init);
173 || sk_OSSL_PROVIDER_push(store->providers, prov) == 0) {
174 ossl_provider_free(prov);
175 provider_store_free(store);
176 ERR_raise(ERR_LIB_CRYPTO, ERR_R_INTERNAL_ERROR);
182 prov->error_lib = ERR_get_next_error_library();
185 ossl_provider_set_fallback(prov);
191 static const OSSL_LIB_CTX_METHOD provider_store_method = {
196 static struct provider_store_st *get_provider_store(OSSL_LIB_CTX *libctx)
198 struct provider_store_st *store = NULL;
200 store = ossl_lib_ctx_get_data(libctx, OSSL_LIB_CTX_PROVIDER_STORE_INDEX,
201 &provider_store_method);
203 ERR_raise(ERR_LIB_CRYPTO, ERR_R_INTERNAL_ERROR);
207 int ossl_provider_disable_fallback_loading(OSSL_LIB_CTX *libctx)
209 struct provider_store_st *store;
211 if ((store = get_provider_store(libctx)) != NULL) {
212 if (!CRYPTO_THREAD_write_lock(store->lock))
214 store->use_fallbacks = 0;
215 CRYPTO_THREAD_unlock(store->lock);
221 OSSL_PROVIDER *ossl_provider_find(OSSL_LIB_CTX *libctx, const char *name,
224 struct provider_store_st *store = NULL;
225 OSSL_PROVIDER *prov = NULL;
227 if ((store = get_provider_store(libctx)) != NULL) {
228 OSSL_PROVIDER tmpl = { 0, };
233 * Make sure any providers are loaded from config before we try to find
236 if (!noconfig && ossl_lib_ctx_is_default(libctx))
237 OPENSSL_init_crypto(OPENSSL_INIT_LOAD_CONFIG, NULL);
240 tmpl.name = (char *)name;
241 if (!CRYPTO_THREAD_read_lock(store->lock))
243 if ((i = sk_OSSL_PROVIDER_find(store->providers, &tmpl)) == -1
244 || (prov = sk_OSSL_PROVIDER_value(store->providers, i)) == NULL
245 || !ossl_provider_up_ref(prov))
247 CRYPTO_THREAD_unlock(store->lock);
254 * Provider Object methods
255 * =======================
258 static OSSL_PROVIDER *provider_new(const char *name,
259 OSSL_provider_init_fn *init_function)
261 OSSL_PROVIDER *prov = NULL;
263 if ((prov = OPENSSL_zalloc(sizeof(*prov))) == NULL
265 || (prov->refcnt_lock = CRYPTO_THREAD_lock_new()) == NULL
267 || !ossl_provider_up_ref(prov) /* +1 One reference to be returned */
268 || (prov->opbits_lock = CRYPTO_THREAD_lock_new()) == NULL
269 || (prov->flag_lock = CRYPTO_THREAD_lock_new()) == NULL
270 || (prov->name = OPENSSL_strdup(name)) == NULL) {
271 ossl_provider_free(prov);
272 ERR_raise(ERR_LIB_CRYPTO, ERR_R_MALLOC_FAILURE);
276 prov->init_function = init_function;
280 int ossl_provider_up_ref(OSSL_PROVIDER *prov)
284 if (CRYPTO_UP_REF(&prov->refcnt, &ref, prov->refcnt_lock) <= 0)
289 OSSL_PROVIDER *ossl_provider_new(OSSL_LIB_CTX *libctx, const char *name,
290 OSSL_provider_init_fn *init_function,
293 struct provider_store_st *store = NULL;
294 OSSL_PROVIDER *prov = NULL;
296 if ((store = get_provider_store(libctx)) == NULL)
299 if ((prov = ossl_provider_find(libctx, name,
300 noconfig)) != NULL) { /* refcount +1 */
301 ossl_provider_free(prov); /* refcount -1 */
302 ERR_raise_data(ERR_LIB_CRYPTO, CRYPTO_R_PROVIDER_ALREADY_EXISTS,
307 /* provider_new() generates an error, so no need here */
308 if ((prov = provider_new(name, init_function)) == NULL)
311 if (!CRYPTO_THREAD_write_lock(store->lock))
313 if (!ossl_provider_up_ref(prov)) { /* +1 One reference for the store */
314 ossl_provider_free(prov); /* -1 Reference that was to be returned */
316 } else if (sk_OSSL_PROVIDER_push(store->providers, prov) == 0) {
317 ossl_provider_free(prov); /* -1 Store reference */
318 ossl_provider_free(prov); /* -1 Reference that was to be returned */
321 prov->libctx = libctx;
324 prov->error_lib = ERR_get_next_error_library();
327 CRYPTO_THREAD_unlock(store->lock);
330 ERR_raise(ERR_LIB_CRYPTO, ERR_R_MALLOC_FAILURE);
333 * At this point, the provider is only partially "loaded". To be
334 * fully "loaded", ossl_provider_activate() must also be called.
340 static void free_infopair(INFOPAIR *pair)
342 OPENSSL_free(pair->name);
343 OPENSSL_free(pair->value);
347 void ossl_provider_free(OSSL_PROVIDER *prov)
352 CRYPTO_DOWN_REF(&prov->refcnt, &ref, prov->refcnt_lock);
355 * When the refcount drops to zero, we clean up the provider.
356 * Note that this also does teardown, which may seem late,
357 * considering that init happens on first activation. However,
358 * there may be other structures hanging on to the provider after
359 * the last deactivation and may therefore need full access to the
360 * provider's services. Therefore, we deinit late.
363 if (prov->flag_initialized) {
364 if (prov->teardown != NULL)
365 prov->teardown(prov->provctx);
366 #ifndef OPENSSL_NO_ERR
368 if (prov->error_strings != NULL) {
369 ERR_unload_strings(prov->error_lib, prov->error_strings);
370 OPENSSL_free(prov->error_strings);
371 prov->error_strings = NULL;
375 OPENSSL_free(prov->operation_bits);
376 prov->operation_bits = NULL;
377 prov->operation_bits_sz = 0;
378 prov->flag_initialized = 0;
383 * We deregister thread handling whether or not the provider was
384 * initialized. If init was attempted but was not successful then
385 * the provider may still have registered a thread handler.
387 ossl_init_thread_deregister(prov);
388 DSO_free(prov->module);
390 OPENSSL_free(prov->name);
391 OPENSSL_free(prov->path);
392 sk_INFOPAIR_pop_free(prov->parameters, free_infopair);
393 CRYPTO_THREAD_lock_free(prov->opbits_lock);
394 CRYPTO_THREAD_lock_free(prov->flag_lock);
396 CRYPTO_THREAD_lock_free(prov->refcnt_lock);
404 int ossl_provider_set_module_path(OSSL_PROVIDER *prov, const char *module_path)
406 OPENSSL_free(prov->path);
407 if (module_path == NULL)
409 if ((prov->path = OPENSSL_strdup(module_path)) != NULL)
411 ERR_raise(ERR_LIB_CRYPTO, ERR_R_MALLOC_FAILURE);
415 int ossl_provider_add_parameter(OSSL_PROVIDER *prov,
416 const char *name, const char *value)
418 INFOPAIR *pair = NULL;
420 if ((pair = OPENSSL_zalloc(sizeof(*pair))) != NULL
421 && (prov->parameters != NULL
422 || (prov->parameters = sk_INFOPAIR_new_null()) != NULL)
423 && (pair->name = OPENSSL_strdup(name)) != NULL
424 && (pair->value = OPENSSL_strdup(value)) != NULL
425 && sk_INFOPAIR_push(prov->parameters, pair) > 0)
429 OPENSSL_free(pair->name);
430 OPENSSL_free(pair->value);
433 ERR_raise(ERR_LIB_CRYPTO, ERR_R_MALLOC_FAILURE);
438 * Provider activation.
440 * What "activation" means depends on the provider form; for built in
441 * providers (in the library or the application alike), the provider
442 * can already be considered to be loaded, all that's needed is to
443 * initialize it. However, for dynamically loadable provider modules,
444 * we must first load that module.
446 * Built in modules are distinguished from dynamically loaded modules
447 * with an already assigned init function.
449 static const OSSL_DISPATCH *core_dispatch; /* Define further down */
451 int OSSL_PROVIDER_set_default_search_path(OSSL_LIB_CTX *libctx,
454 struct provider_store_st *store;
458 p = OPENSSL_strdup(path);
460 ERR_raise(ERR_LIB_CRYPTO, ERR_R_MALLOC_FAILURE);
464 if ((store = get_provider_store(libctx)) != NULL
465 && CRYPTO_THREAD_write_lock(store->default_path_lock)) {
466 OPENSSL_free(store->default_path);
467 store->default_path = p;
468 CRYPTO_THREAD_unlock(store->default_path_lock);
476 * Internal version that doesn't affect the store flags, and thereby avoid
477 * locking. Direct callers must remember to set the store flags when
480 static int provider_init(OSSL_PROVIDER *prov, int flag_lock)
482 const OSSL_DISPATCH *provider_dispatch = NULL;
483 void *tmp_provctx = NULL; /* safety measure */
484 #ifndef OPENSSL_NO_ERR
486 OSSL_FUNC_provider_get_reason_strings_fn *p_get_reason_strings = NULL;
492 * The flag lock is used to lock init, not only because the flag is
493 * checked here and set at the end, but also because this function
494 * modifies a number of things in the provider structure that this
495 * function needs to perform under lock anyway.
497 if (flag_lock && !CRYPTO_THREAD_write_lock(prov->flag_lock))
499 if (prov->flag_initialized) {
505 * If the init function isn't set, it indicates that this provider is
508 if (prov->init_function == NULL) {
512 if (prov->module == NULL) {
513 char *allocated_path = NULL;
514 const char *module_path = NULL;
515 char *merged_path = NULL;
516 const char *load_dir = NULL;
517 char *allocated_load_dir = NULL;
518 struct provider_store_st *store;
520 if ((prov->module = DSO_new()) == NULL) {
521 /* DSO_new() generates an error already */
525 if ((store = get_provider_store(prov->libctx)) == NULL
526 || !CRYPTO_THREAD_read_lock(store->default_path_lock))
529 if (store->default_path != NULL) {
530 allocated_load_dir = OPENSSL_strdup(store->default_path);
531 CRYPTO_THREAD_unlock(store->default_path_lock);
532 if (allocated_load_dir == NULL) {
533 ERR_raise(ERR_LIB_CRYPTO, ERR_R_MALLOC_FAILURE);
536 load_dir = allocated_load_dir;
538 CRYPTO_THREAD_unlock(store->default_path_lock);
541 if (load_dir == NULL) {
542 load_dir = ossl_safe_getenv("OPENSSL_MODULES");
543 if (load_dir == NULL)
544 load_dir = MODULESDIR;
547 DSO_ctrl(prov->module, DSO_CTRL_SET_FLAGS,
548 DSO_FLAG_NAME_TRANSLATION_EXT_ONLY, NULL);
550 module_path = prov->path;
551 if (module_path == NULL)
552 module_path = allocated_path =
553 DSO_convert_filename(prov->module, prov->name);
554 if (module_path != NULL)
555 merged_path = DSO_merge(prov->module, module_path, load_dir);
557 if (merged_path == NULL
558 || (DSO_load(prov->module, merged_path, NULL, 0)) == NULL) {
559 DSO_free(prov->module);
563 OPENSSL_free(merged_path);
564 OPENSSL_free(allocated_path);
565 OPENSSL_free(allocated_load_dir);
568 if (prov->module != NULL)
569 prov->init_function = (OSSL_provider_init_fn *)
570 DSO_bind_func(prov->module, "OSSL_provider_init");
574 /* Call the initialise function for the provider. */
575 if (prov->init_function == NULL
576 || !prov->init_function((OSSL_CORE_HANDLE *)prov, core_dispatch,
577 &provider_dispatch, &tmp_provctx)) {
578 ERR_raise_data(ERR_LIB_CRYPTO, ERR_R_INIT_FAIL,
579 "name=%s", prov->name);
582 prov->provctx = tmp_provctx;
584 for (; provider_dispatch->function_id != 0; provider_dispatch++) {
585 switch (provider_dispatch->function_id) {
586 case OSSL_FUNC_PROVIDER_TEARDOWN:
588 OSSL_FUNC_provider_teardown(provider_dispatch);
590 case OSSL_FUNC_PROVIDER_GETTABLE_PARAMS:
591 prov->gettable_params =
592 OSSL_FUNC_provider_gettable_params(provider_dispatch);
594 case OSSL_FUNC_PROVIDER_GET_PARAMS:
596 OSSL_FUNC_provider_get_params(provider_dispatch);
598 case OSSL_FUNC_PROVIDER_SELF_TEST:
600 OSSL_FUNC_provider_self_test(provider_dispatch);
602 case OSSL_FUNC_PROVIDER_GET_CAPABILITIES:
603 prov->get_capabilities =
604 OSSL_FUNC_provider_get_capabilities(provider_dispatch);
606 case OSSL_FUNC_PROVIDER_QUERY_OPERATION:
607 prov->query_operation =
608 OSSL_FUNC_provider_query_operation(provider_dispatch);
610 case OSSL_FUNC_PROVIDER_UNQUERY_OPERATION:
611 prov->unquery_operation =
612 OSSL_FUNC_provider_unquery_operation(provider_dispatch);
614 #ifndef OPENSSL_NO_ERR
616 case OSSL_FUNC_PROVIDER_GET_REASON_STRINGS:
617 p_get_reason_strings =
618 OSSL_FUNC_provider_get_reason_strings(provider_dispatch);
625 #ifndef OPENSSL_NO_ERR
627 if (p_get_reason_strings != NULL) {
628 const OSSL_ITEM *reasonstrings = p_get_reason_strings(prov->provctx);
632 * ERR_load_strings() handles ERR_STRING_DATA rather than OSSL_ITEM,
633 * although they are essentially the same type.
634 * Furthermore, ERR_load_strings() patches the array's error number
635 * with the error library number, so we need to make a copy of that
639 while (reasonstrings[cnt].id != 0) {
640 if (ERR_GET_LIB(reasonstrings[cnt].id) != 0)
644 cnt++; /* One for the terminating item */
646 /* Allocate one extra item for the "library" name */
647 prov->error_strings =
648 OPENSSL_zalloc(sizeof(ERR_STRING_DATA) * (cnt + 1));
649 if (prov->error_strings == NULL)
653 * Set the "library" name.
655 prov->error_strings[0].error = ERR_PACK(prov->error_lib, 0, 0);
656 prov->error_strings[0].string = prov->name;
658 * Copy reasonstrings item 0..cnt-1 to prov->error_trings positions
661 for (cnt2 = 1; cnt2 <= cnt; cnt2++) {
662 prov->error_strings[cnt2].error = (int)reasonstrings[cnt2-1].id;
663 prov->error_strings[cnt2].string = reasonstrings[cnt2-1].ptr;
666 ERR_load_strings(prov->error_lib, prov->error_strings);
671 /* With this flag set, this provider has become fully "loaded". */
672 prov->flag_initialized = 1;
677 CRYPTO_THREAD_unlock(prov->flag_lock);
682 * Deactivate a provider.
683 * Return -1 on failure and the activation count on success
685 static int provider_deactivate(OSSL_PROVIDER *prov)
689 if (!ossl_assert(prov != NULL))
692 if (!CRYPTO_THREAD_write_lock(prov->flag_lock))
695 if ((count = --prov->activatecnt) < 1)
696 prov->flag_activated = 0;
698 CRYPTO_THREAD_unlock(prov->flag_lock);
700 /* We don't deinit here, that's done in ossl_provider_free() */
705 * Activate a provider.
706 * Return -1 on failure and the activation count on success
708 static int provider_activate(OSSL_PROVIDER *prov, int flag_lock)
712 if (provider_init(prov, flag_lock)) {
713 if (flag_lock && !CRYPTO_THREAD_write_lock(prov->flag_lock))
715 count = ++prov->activatecnt;
716 prov->flag_activated = 1;
718 CRYPTO_THREAD_unlock(prov->flag_lock);
726 static int provider_flush_store_cache(const OSSL_PROVIDER *prov)
728 struct provider_store_st *store;
731 if ((store = get_provider_store(prov->libctx)) == NULL)
734 if (!CRYPTO_THREAD_read_lock(store->lock))
736 freeing = store->freeing;
737 CRYPTO_THREAD_unlock(store->lock);
740 return evp_method_store_flush(prov->libctx);
744 int ossl_provider_activate(OSSL_PROVIDER *prov, int retain_fallbacks)
750 if ((count = provider_activate(prov, 1)) > 0) {
751 if (!retain_fallbacks) {
752 if (!CRYPTO_THREAD_write_lock(prov->store->lock)) {
753 provider_deactivate(prov);
756 prov->store->use_fallbacks = 0;
757 CRYPTO_THREAD_unlock(prov->store->lock);
759 return count == 1 ? provider_flush_store_cache(prov) : 1;
764 int ossl_provider_deactivate(OSSL_PROVIDER *prov)
768 if (prov == NULL || (count = provider_deactivate(prov)) < 0)
770 return count == 0 ? provider_flush_store_cache(prov) : 1;
773 void *ossl_provider_ctx(const OSSL_PROVIDER *prov)
775 return prov->provctx;
779 * This function only does something once when store->use_fallbacks == 1,
780 * and then sets store->use_fallbacks = 0, so the second call and so on is
781 * effectively a no-op.
783 static void provider_activate_fallbacks(struct provider_store_st *store)
787 int activated_fallback_count = 0;
790 if (!CRYPTO_THREAD_read_lock(store->lock))
792 use_fallbacks = store->use_fallbacks;
793 CRYPTO_THREAD_unlock(store->lock);
797 if (!CRYPTO_THREAD_write_lock(store->lock))
799 /* Check again, just in case another thread changed it */
800 use_fallbacks = store->use_fallbacks;
801 if (!use_fallbacks) {
802 CRYPTO_THREAD_unlock(store->lock);
806 num_provs = sk_OSSL_PROVIDER_num(store->providers);
807 for (i = 0; i < num_provs; i++) {
808 OSSL_PROVIDER *prov = sk_OSSL_PROVIDER_value(store->providers, i);
810 if (ossl_provider_up_ref(prov)) {
811 if (prov->flag_fallback) {
812 if (provider_activate(prov, 1) > 0)
813 activated_fallback_count++;
815 ossl_provider_free(prov);
820 * We assume that all fallbacks have been added to the store before
821 * any fallback is activated.
822 * TODO: We may have to reconsider this, IF we find ourselves adding
823 * fallbacks after any previous fallback has been activated.
825 if (activated_fallback_count > 0)
826 store->use_fallbacks = 0;
828 CRYPTO_THREAD_unlock(store->lock);
831 int ossl_provider_doall_activated(OSSL_LIB_CTX *ctx,
832 int (*cb)(OSSL_PROVIDER *provider,
836 int ret = 0, curr, max;
837 struct provider_store_st *store = get_provider_store(ctx);
838 STACK_OF(OSSL_PROVIDER) *provs = NULL;
842 * Make sure any providers are loaded from config before we try to use
845 if (ossl_lib_ctx_is_default(ctx))
846 OPENSSL_init_crypto(OPENSSL_INIT_LOAD_CONFIG, NULL);
851 provider_activate_fallbacks(store);
854 * Under lock, grab a copy of the provider list and up_ref each
855 * provider so that they don't disappear underneath us.
857 if (!CRYPTO_THREAD_read_lock(store->lock))
859 provs = sk_OSSL_PROVIDER_dup(store->providers);
861 CRYPTO_THREAD_unlock(store->lock);
864 max = sk_OSSL_PROVIDER_num(provs);
866 * We work backwards through the stack so that we can safely delete items
869 for (curr = max - 1; curr >= 0; curr--) {
870 OSSL_PROVIDER *prov = sk_OSSL_PROVIDER_value(provs, curr);
872 if (!CRYPTO_THREAD_write_lock(prov->flag_lock))
874 if (prov->flag_activated) {
875 if (!ossl_provider_up_ref(prov)){
876 CRYPTO_THREAD_unlock(prov->flag_lock);
880 * It's already activated, but we up the activated count to ensure
881 * it remains activated until after we've called the user callback.
883 if (provider_activate(prov, 0) < 0) {
884 ossl_provider_free(prov);
885 CRYPTO_THREAD_unlock(prov->flag_lock);
889 sk_OSSL_PROVIDER_delete(provs, curr);
892 CRYPTO_THREAD_unlock(prov->flag_lock);
894 CRYPTO_THREAD_unlock(store->lock);
897 * Now, we sweep through all providers not under lock
899 for (curr = 0; curr < max; curr++) {
900 OSSL_PROVIDER *prov = sk_OSSL_PROVIDER_value(provs, curr);
902 if (!cb(prov, cbdata))
911 CRYPTO_THREAD_unlock(store->lock);
914 * The pop_free call doesn't do what we want on an error condition. We
915 * either start from the first item in the stack, or part way through if
916 * we only processed some of the items.
918 for (curr++; curr < max; curr++) {
919 OSSL_PROVIDER *prov = sk_OSSL_PROVIDER_value(provs, curr);
921 provider_deactivate(prov);
922 ossl_provider_free(prov);
924 sk_OSSL_PROVIDER_free(provs);
928 int ossl_provider_available(OSSL_PROVIDER *prov)
933 provider_activate_fallbacks(prov->store);
935 if (!CRYPTO_THREAD_read_lock(prov->flag_lock))
937 ret = prov->flag_activated;
938 CRYPTO_THREAD_unlock(prov->flag_lock);
944 /* Setters of Provider Object data */
945 int ossl_provider_set_fallback(OSSL_PROVIDER *prov)
950 prov->flag_fallback = 1;
954 /* Getters of Provider Object data */
955 const char *ossl_provider_name(const OSSL_PROVIDER *prov)
960 const DSO *ossl_provider_dso(const OSSL_PROVIDER *prov)
965 const char *ossl_provider_module_name(const OSSL_PROVIDER *prov)
970 return DSO_get_filename(prov->module);
974 const char *ossl_provider_module_path(const OSSL_PROVIDER *prov)
979 /* FIXME: Ensure it's a full path */
980 return DSO_get_filename(prov->module);
984 void *ossl_provider_prov_ctx(const OSSL_PROVIDER *prov)
987 return prov->provctx;
992 OSSL_LIB_CTX *ossl_provider_libctx(const OSSL_PROVIDER *prov)
994 return prov != NULL ? prov->libctx : NULL;
997 /* Wrappers around calls to the provider */
998 void ossl_provider_teardown(const OSSL_PROVIDER *prov)
1000 if (prov->teardown != NULL)
1001 prov->teardown(prov->provctx);
1004 const OSSL_PARAM *ossl_provider_gettable_params(const OSSL_PROVIDER *prov)
1006 return prov->gettable_params == NULL
1007 ? NULL : prov->gettable_params(prov->provctx);
1010 int ossl_provider_get_params(const OSSL_PROVIDER *prov, OSSL_PARAM params[])
1012 return prov->get_params == NULL
1013 ? 0 : prov->get_params(prov->provctx, params);
1016 int ossl_provider_self_test(const OSSL_PROVIDER *prov)
1020 if (prov->self_test == NULL)
1022 ret = prov->self_test(prov->provctx);
1024 (void)provider_flush_store_cache(prov);
1028 int ossl_provider_get_capabilities(const OSSL_PROVIDER *prov,
1029 const char *capability,
1033 return prov->get_capabilities == NULL
1034 ? 1 : prov->get_capabilities(prov->provctx, capability, cb, arg);
1037 const OSSL_ALGORITHM *ossl_provider_query_operation(const OSSL_PROVIDER *prov,
1041 const OSSL_ALGORITHM *res;
1043 if (prov->query_operation == NULL)
1045 res = prov->query_operation(prov->provctx, operation_id, no_cache);
1046 #if defined(OPENSSL_NO_CACHED_FETCH)
1047 /* Forcing the non-caching of queries */
1048 if (no_cache != NULL)
1054 void ossl_provider_unquery_operation(const OSSL_PROVIDER *prov,
1056 const OSSL_ALGORITHM *algs)
1058 if (prov->unquery_operation != NULL)
1059 prov->unquery_operation(prov->provctx, operation_id, algs);
1062 int ossl_provider_clear_all_operation_bits(OSSL_LIB_CTX *libctx)
1064 struct provider_store_st *store;
1065 OSSL_PROVIDER *provider;
1066 int i, num, res = 1;
1068 if ((store = get_provider_store(libctx)) != NULL) {
1069 if (!CRYPTO_THREAD_read_lock(store->lock))
1071 num = sk_OSSL_PROVIDER_num(store->providers);
1072 for (i = 0; i < num; i++) {
1073 provider = sk_OSSL_PROVIDER_value(store->providers, i);
1074 if (!CRYPTO_THREAD_write_lock(provider->opbits_lock)) {
1078 if (provider->operation_bits != NULL)
1079 memset(provider->operation_bits, 0,
1080 provider->operation_bits_sz);
1081 CRYPTO_THREAD_unlock(provider->opbits_lock);
1083 CRYPTO_THREAD_unlock(store->lock);
1089 int ossl_provider_set_operation_bit(OSSL_PROVIDER *provider, size_t bitnum)
1091 size_t byte = bitnum / 8;
1092 unsigned char bit = (1 << (bitnum % 8)) & 0xFF;
1094 if (!CRYPTO_THREAD_write_lock(provider->opbits_lock))
1096 if (provider->operation_bits_sz <= byte) {
1097 unsigned char *tmp = OPENSSL_realloc(provider->operation_bits,
1101 CRYPTO_THREAD_unlock(provider->opbits_lock);
1102 ERR_raise(ERR_LIB_CRYPTO, ERR_R_MALLOC_FAILURE);
1105 provider->operation_bits = tmp;
1106 memset(provider->operation_bits + provider->operation_bits_sz,
1107 '\0', byte + 1 - provider->operation_bits_sz);
1108 provider->operation_bits_sz = byte + 1;
1110 provider->operation_bits[byte] |= bit;
1111 CRYPTO_THREAD_unlock(provider->opbits_lock);
1115 int ossl_provider_test_operation_bit(OSSL_PROVIDER *provider, size_t bitnum,
1118 size_t byte = bitnum / 8;
1119 unsigned char bit = (1 << (bitnum % 8)) & 0xFF;
1121 if (!ossl_assert(result != NULL)) {
1122 ERR_raise(ERR_LIB_CRYPTO, ERR_R_PASSED_NULL_PARAMETER);
1127 if (!CRYPTO_THREAD_read_lock(provider->opbits_lock))
1129 if (provider->operation_bits_sz > byte)
1130 *result = ((provider->operation_bits[byte] & bit) != 0);
1131 CRYPTO_THREAD_unlock(provider->opbits_lock);
1136 * Core functions for the provider
1137 * ===============================
1139 * This is the set of functions that the core makes available to the provider
1143 * This returns a list of Provider Object parameters with their types, for
1144 * discovery. We do not expect that many providers will use this, but one
1147 static const OSSL_PARAM param_types[] = {
1148 OSSL_PARAM_DEFN(OSSL_PROV_PARAM_CORE_VERSION, OSSL_PARAM_UTF8_PTR, NULL, 0),
1149 OSSL_PARAM_DEFN(OSSL_PROV_PARAM_CORE_PROV_NAME, OSSL_PARAM_UTF8_PTR,
1152 OSSL_PARAM_DEFN(OSSL_PROV_PARAM_CORE_MODULE_FILENAME, OSSL_PARAM_UTF8_PTR,
1159 * Forward declare all the functions that are provided aa dispatch.
1160 * This ensures that the compiler will complain if they aren't defined
1161 * with the correct signature.
1163 static OSSL_FUNC_core_gettable_params_fn core_gettable_params;
1164 static OSSL_FUNC_core_get_params_fn core_get_params;
1165 static OSSL_FUNC_core_thread_start_fn core_thread_start;
1166 static OSSL_FUNC_core_get_libctx_fn core_get_libctx;
1168 static OSSL_FUNC_core_new_error_fn core_new_error;
1169 static OSSL_FUNC_core_set_error_debug_fn core_set_error_debug;
1170 static OSSL_FUNC_core_vset_error_fn core_vset_error;
1171 static OSSL_FUNC_core_set_error_mark_fn core_set_error_mark;
1172 static OSSL_FUNC_core_clear_last_error_mark_fn core_clear_last_error_mark;
1173 static OSSL_FUNC_core_pop_error_to_mark_fn core_pop_error_to_mark;
1176 static const OSSL_PARAM *core_gettable_params(const OSSL_CORE_HANDLE *handle)
1181 static int core_get_params(const OSSL_CORE_HANDLE *handle, OSSL_PARAM params[])
1186 * We created this object originally and we know it is actually an
1187 * OSSL_PROVIDER *, so the cast is safe
1189 OSSL_PROVIDER *prov = (OSSL_PROVIDER *)handle;
1191 if ((p = OSSL_PARAM_locate(params, OSSL_PROV_PARAM_CORE_VERSION)) != NULL)
1192 OSSL_PARAM_set_utf8_ptr(p, OPENSSL_VERSION_STR);
1193 if ((p = OSSL_PARAM_locate(params, OSSL_PROV_PARAM_CORE_PROV_NAME)) != NULL)
1194 OSSL_PARAM_set_utf8_ptr(p, prov->name);
1197 if ((p = OSSL_PARAM_locate(params,
1198 OSSL_PROV_PARAM_CORE_MODULE_FILENAME)) != NULL)
1199 OSSL_PARAM_set_utf8_ptr(p, ossl_provider_module_path(prov));
1202 if (prov->parameters == NULL)
1205 for (i = 0; i < sk_INFOPAIR_num(prov->parameters); i++) {
1206 INFOPAIR *pair = sk_INFOPAIR_value(prov->parameters, i);
1208 if ((p = OSSL_PARAM_locate(params, pair->name)) != NULL)
1209 OSSL_PARAM_set_utf8_ptr(p, pair->value);
1214 static OPENSSL_CORE_CTX *core_get_libctx(const OSSL_CORE_HANDLE *handle)
1217 * We created this object originally and we know it is actually an
1218 * OSSL_PROVIDER *, so the cast is safe
1220 OSSL_PROVIDER *prov = (OSSL_PROVIDER *)handle;
1223 * Using ossl_provider_libctx would be wrong as that returns
1224 * NULL for |prov| == NULL and NULL libctx has a special meaning
1225 * that does not apply here. Here |prov| == NULL can happen only in
1226 * case of a coding error.
1228 assert(prov != NULL);
1229 return (OPENSSL_CORE_CTX *)prov->libctx;
1232 static int core_thread_start(const OSSL_CORE_HANDLE *handle,
1233 OSSL_thread_stop_handler_fn handfn)
1236 * We created this object originally and we know it is actually an
1237 * OSSL_PROVIDER *, so the cast is safe
1239 OSSL_PROVIDER *prov = (OSSL_PROVIDER *)handle;
1241 return ossl_init_thread_start(prov, prov->provctx, handfn);
1245 * The FIPS module inner provider doesn't implement these. They aren't
1246 * needed there, since the FIPS module upcalls are always the outer provider
1251 * These error functions should use |handle| to select the proper
1252 * library context to report in the correct error stack if error
1253 * stacks become tied to the library context.
1254 * We cannot currently do that since there's no support for it in the
1257 static void core_new_error(const OSSL_CORE_HANDLE *handle)
1262 static void core_set_error_debug(const OSSL_CORE_HANDLE *handle,
1263 const char *file, int line, const char *func)
1265 ERR_set_debug(file, line, func);
1268 static void core_vset_error(const OSSL_CORE_HANDLE *handle,
1269 uint32_t reason, const char *fmt, va_list args)
1272 * We created this object originally and we know it is actually an
1273 * OSSL_PROVIDER *, so the cast is safe
1275 OSSL_PROVIDER *prov = (OSSL_PROVIDER *)handle;
1278 * If the uppermost 8 bits are non-zero, it's an OpenSSL library
1279 * error and will be treated as such. Otherwise, it's a new style
1280 * provider error and will be treated as such.
1282 if (ERR_GET_LIB(reason) != 0) {
1283 ERR_vset_error(ERR_GET_LIB(reason), ERR_GET_REASON(reason), fmt, args);
1285 ERR_vset_error(prov->error_lib, (int)reason, fmt, args);
1289 static int core_set_error_mark(const OSSL_CORE_HANDLE *handle)
1291 return ERR_set_mark();
1294 static int core_clear_last_error_mark(const OSSL_CORE_HANDLE *handle)
1296 return ERR_clear_last_mark();
1299 static int core_pop_error_to_mark(const OSSL_CORE_HANDLE *handle)
1301 return ERR_pop_to_mark();
1303 #endif /* FIPS_MODULE */
1306 * Functions provided by the core.
1308 static const OSSL_DISPATCH core_dispatch_[] = {
1309 { OSSL_FUNC_CORE_GETTABLE_PARAMS, (void (*)(void))core_gettable_params },
1310 { OSSL_FUNC_CORE_GET_PARAMS, (void (*)(void))core_get_params },
1311 { OSSL_FUNC_CORE_GET_LIBCTX, (void (*)(void))core_get_libctx },
1312 { OSSL_FUNC_CORE_THREAD_START, (void (*)(void))core_thread_start },
1314 { OSSL_FUNC_CORE_NEW_ERROR, (void (*)(void))core_new_error },
1315 { OSSL_FUNC_CORE_SET_ERROR_DEBUG, (void (*)(void))core_set_error_debug },
1316 { OSSL_FUNC_CORE_VSET_ERROR, (void (*)(void))core_vset_error },
1317 { OSSL_FUNC_CORE_SET_ERROR_MARK, (void (*)(void))core_set_error_mark },
1318 { OSSL_FUNC_CORE_CLEAR_LAST_ERROR_MARK,
1319 (void (*)(void))core_clear_last_error_mark },
1320 { OSSL_FUNC_CORE_POP_ERROR_TO_MARK, (void (*)(void))core_pop_error_to_mark },
1321 { OSSL_FUNC_BIO_NEW_FILE, (void (*)(void))ossl_core_bio_new_file },
1322 { OSSL_FUNC_BIO_NEW_MEMBUF, (void (*)(void))ossl_core_bio_new_mem_buf },
1323 { OSSL_FUNC_BIO_READ_EX, (void (*)(void))ossl_core_bio_read_ex },
1324 { OSSL_FUNC_BIO_WRITE_EX, (void (*)(void))ossl_core_bio_write_ex },
1325 { OSSL_FUNC_BIO_GETS, (void (*)(void))ossl_core_bio_gets },
1326 { OSSL_FUNC_BIO_PUTS, (void (*)(void))ossl_core_bio_puts },
1327 { OSSL_FUNC_BIO_CTRL, (void (*)(void))ossl_core_bio_ctrl },
1328 { OSSL_FUNC_BIO_UP_REF, (void (*)(void))ossl_core_bio_up_ref },
1329 { OSSL_FUNC_BIO_FREE, (void (*)(void))ossl_core_bio_free },
1330 { OSSL_FUNC_BIO_VPRINTF, (void (*)(void))ossl_core_bio_vprintf },
1331 { OSSL_FUNC_BIO_VSNPRINTF, (void (*)(void))BIO_vsnprintf },
1332 { OSSL_FUNC_SELF_TEST_CB, (void (*)(void))OSSL_SELF_TEST_get_callback },
1333 { OSSL_FUNC_GET_ENTROPY, (void (*)(void))ossl_rand_get_entropy },
1334 { OSSL_FUNC_CLEANUP_ENTROPY, (void (*)(void))ossl_rand_cleanup_entropy },
1335 { OSSL_FUNC_GET_NONCE, (void (*)(void))ossl_rand_get_nonce },
1336 { OSSL_FUNC_CLEANUP_NONCE, (void (*)(void))ossl_rand_cleanup_nonce },
1338 { OSSL_FUNC_CRYPTO_MALLOC, (void (*)(void))CRYPTO_malloc },
1339 { OSSL_FUNC_CRYPTO_ZALLOC, (void (*)(void))CRYPTO_zalloc },
1340 { OSSL_FUNC_CRYPTO_FREE, (void (*)(void))CRYPTO_free },
1341 { OSSL_FUNC_CRYPTO_CLEAR_FREE, (void (*)(void))CRYPTO_clear_free },
1342 { OSSL_FUNC_CRYPTO_REALLOC, (void (*)(void))CRYPTO_realloc },
1343 { OSSL_FUNC_CRYPTO_CLEAR_REALLOC, (void (*)(void))CRYPTO_clear_realloc },
1344 { OSSL_FUNC_CRYPTO_SECURE_MALLOC, (void (*)(void))CRYPTO_secure_malloc },
1345 { OSSL_FUNC_CRYPTO_SECURE_ZALLOC, (void (*)(void))CRYPTO_secure_zalloc },
1346 { OSSL_FUNC_CRYPTO_SECURE_FREE, (void (*)(void))CRYPTO_secure_free },
1347 { OSSL_FUNC_CRYPTO_SECURE_CLEAR_FREE,
1348 (void (*)(void))CRYPTO_secure_clear_free },
1349 { OSSL_FUNC_CRYPTO_SECURE_ALLOCATED,
1350 (void (*)(void))CRYPTO_secure_allocated },
1351 { OSSL_FUNC_OPENSSL_CLEANSE, (void (*)(void))OPENSSL_cleanse },
1355 static const OSSL_DISPATCH *core_dispatch = core_dispatch_;