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;
122 * provider_deactivate_free() is a wrapper around ossl_provider_deactivate()
123 * and ossl_provider_free(), called as needed.
124 * Since this is only called when the provider store is being emptied, we
125 * don't need to care about any lock.
127 static void provider_deactivate_free(OSSL_PROVIDER *prov)
129 if (prov->flag_activated)
130 ossl_provider_deactivate(prov);
131 ossl_provider_free(prov);
134 static void provider_store_free(void *vstore)
136 struct provider_store_st *store = vstore;
140 OPENSSL_free(store->default_path);
141 sk_OSSL_PROVIDER_pop_free(store->providers, provider_deactivate_free);
142 CRYPTO_THREAD_lock_free(store->default_path_lock);
143 CRYPTO_THREAD_lock_free(store->lock);
147 static void *provider_store_new(OSSL_LIB_CTX *ctx)
149 struct provider_store_st *store = OPENSSL_zalloc(sizeof(*store));
150 const struct predefined_providers_st *p = NULL;
153 || (store->providers = sk_OSSL_PROVIDER_new(ossl_provider_cmp)) == NULL
154 || (store->default_path_lock = CRYPTO_THREAD_lock_new()) == NULL
155 || (store->lock = CRYPTO_THREAD_lock_new()) == NULL) {
156 provider_store_free(store);
159 store->use_fallbacks = 1;
161 for (p = ossl_predefined_providers; p->name != NULL; p++) {
162 OSSL_PROVIDER *prov = NULL;
165 * We use the internal constructor directly here,
166 * otherwise we get a call loop
168 prov = provider_new(p->name, p->init);
171 || sk_OSSL_PROVIDER_push(store->providers, prov) == 0) {
172 ossl_provider_free(prov);
173 provider_store_free(store);
174 ERR_raise(ERR_LIB_CRYPTO, ERR_R_INTERNAL_ERROR);
180 prov->error_lib = ERR_get_next_error_library();
183 ossl_provider_set_fallback(prov);
189 static const OSSL_LIB_CTX_METHOD provider_store_method = {
194 static struct provider_store_st *get_provider_store(OSSL_LIB_CTX *libctx)
196 struct provider_store_st *store = NULL;
198 store = ossl_lib_ctx_get_data(libctx, OSSL_LIB_CTX_PROVIDER_STORE_INDEX,
199 &provider_store_method);
201 ERR_raise(ERR_LIB_CRYPTO, ERR_R_INTERNAL_ERROR);
205 int ossl_provider_disable_fallback_loading(OSSL_LIB_CTX *libctx)
207 struct provider_store_st *store;
209 if ((store = get_provider_store(libctx)) != NULL) {
210 if (!CRYPTO_THREAD_write_lock(store->lock))
212 store->use_fallbacks = 0;
213 CRYPTO_THREAD_unlock(store->lock);
219 OSSL_PROVIDER *ossl_provider_find(OSSL_LIB_CTX *libctx, const char *name,
222 struct provider_store_st *store = NULL;
223 OSSL_PROVIDER *prov = NULL;
225 if ((store = get_provider_store(libctx)) != NULL) {
226 OSSL_PROVIDER tmpl = { 0, };
231 * Make sure any providers are loaded from config before we try to find
235 OPENSSL_init_crypto(OPENSSL_INIT_LOAD_CONFIG, NULL);
238 tmpl.name = (char *)name;
239 if (!CRYPTO_THREAD_write_lock(store->lock))
241 if ((i = sk_OSSL_PROVIDER_find(store->providers, &tmpl)) == -1
242 || (prov = sk_OSSL_PROVIDER_value(store->providers, i)) == NULL
243 || !ossl_provider_up_ref(prov))
245 CRYPTO_THREAD_unlock(store->lock);
252 * Provider Object methods
253 * =======================
256 static OSSL_PROVIDER *provider_new(const char *name,
257 OSSL_provider_init_fn *init_function)
259 OSSL_PROVIDER *prov = NULL;
261 if ((prov = OPENSSL_zalloc(sizeof(*prov))) == NULL
263 || (prov->refcnt_lock = CRYPTO_THREAD_lock_new()) == NULL
265 || !ossl_provider_up_ref(prov) /* +1 One reference to be returned */
266 || (prov->opbits_lock = CRYPTO_THREAD_lock_new()) == NULL
267 || (prov->flag_lock = CRYPTO_THREAD_lock_new()) == NULL
268 || (prov->name = OPENSSL_strdup(name)) == NULL) {
269 ossl_provider_free(prov);
270 ERR_raise(ERR_LIB_CRYPTO, ERR_R_MALLOC_FAILURE);
274 prov->init_function = init_function;
278 int ossl_provider_up_ref(OSSL_PROVIDER *prov)
282 if (CRYPTO_UP_REF(&prov->refcnt, &ref, prov->refcnt_lock) <= 0)
287 OSSL_PROVIDER *ossl_provider_new(OSSL_LIB_CTX *libctx, const char *name,
288 OSSL_provider_init_fn *init_function,
291 struct provider_store_st *store = NULL;
292 OSSL_PROVIDER *prov = NULL;
294 if ((store = get_provider_store(libctx)) == NULL)
297 if ((prov = ossl_provider_find(libctx, name,
298 noconfig)) != NULL) { /* refcount +1 */
299 ossl_provider_free(prov); /* refcount -1 */
300 ERR_raise_data(ERR_LIB_CRYPTO, CRYPTO_R_PROVIDER_ALREADY_EXISTS,
305 /* provider_new() generates an error, so no need here */
306 if ((prov = provider_new(name, init_function)) == NULL)
309 if (!CRYPTO_THREAD_write_lock(store->lock))
311 if (!ossl_provider_up_ref(prov)) { /* +1 One reference for the store */
312 ossl_provider_free(prov); /* -1 Reference that was to be returned */
314 } else if (sk_OSSL_PROVIDER_push(store->providers, prov) == 0) {
315 ossl_provider_free(prov); /* -1 Store reference */
316 ossl_provider_free(prov); /* -1 Reference that was to be returned */
319 prov->libctx = libctx;
322 prov->error_lib = ERR_get_next_error_library();
325 CRYPTO_THREAD_unlock(store->lock);
328 ERR_raise(ERR_LIB_CRYPTO, ERR_R_MALLOC_FAILURE);
331 * At this point, the provider is only partially "loaded". To be
332 * fully "loaded", ossl_provider_activate() must also be called.
338 static void free_infopair(INFOPAIR *pair)
340 OPENSSL_free(pair->name);
341 OPENSSL_free(pair->value);
345 void ossl_provider_free(OSSL_PROVIDER *prov)
350 CRYPTO_DOWN_REF(&prov->refcnt, &ref, prov->refcnt_lock);
353 * When the refcount drops to zero, we clean up the provider.
354 * Note that this also does teardown, which may seem late,
355 * considering that init happens on first activation. However,
356 * there may be other structures hanging on to the provider after
357 * the last deactivation and may therefore need full access to the
358 * provider's services. Therefore, we deinit late.
361 if (prov->flag_initialized) {
362 if (prov->teardown != NULL)
363 prov->teardown(prov->provctx);
364 #ifndef OPENSSL_NO_ERR
366 if (prov->error_strings != NULL) {
367 ERR_unload_strings(prov->error_lib, prov->error_strings);
368 OPENSSL_free(prov->error_strings);
369 prov->error_strings = NULL;
373 OPENSSL_free(prov->operation_bits);
374 prov->operation_bits = NULL;
375 prov->operation_bits_sz = 0;
376 prov->flag_initialized = 0;
381 * We deregister thread handling whether or not the provider was
382 * initialized. If init was attempted but was not successful then
383 * the provider may still have registered a thread handler.
385 ossl_init_thread_deregister(prov);
386 DSO_free(prov->module);
388 OPENSSL_free(prov->name);
389 OPENSSL_free(prov->path);
390 sk_INFOPAIR_pop_free(prov->parameters, free_infopair);
391 CRYPTO_THREAD_lock_free(prov->opbits_lock);
392 CRYPTO_THREAD_lock_free(prov->flag_lock);
394 CRYPTO_THREAD_lock_free(prov->refcnt_lock);
402 int ossl_provider_set_module_path(OSSL_PROVIDER *prov, const char *module_path)
404 OPENSSL_free(prov->path);
405 if (module_path == NULL)
407 if ((prov->path = OPENSSL_strdup(module_path)) != NULL)
409 ERR_raise(ERR_LIB_CRYPTO, ERR_R_MALLOC_FAILURE);
413 int ossl_provider_add_parameter(OSSL_PROVIDER *prov,
414 const char *name, const char *value)
416 INFOPAIR *pair = NULL;
418 if ((pair = OPENSSL_zalloc(sizeof(*pair))) != NULL
419 && (prov->parameters != NULL
420 || (prov->parameters = sk_INFOPAIR_new_null()) != NULL)
421 && (pair->name = OPENSSL_strdup(name)) != NULL
422 && (pair->value = OPENSSL_strdup(value)) != NULL
423 && sk_INFOPAIR_push(prov->parameters, pair) > 0)
427 OPENSSL_free(pair->name);
428 OPENSSL_free(pair->value);
431 ERR_raise(ERR_LIB_CRYPTO, ERR_R_MALLOC_FAILURE);
436 * Provider activation.
438 * What "activation" means depends on the provider form; for built in
439 * providers (in the library or the application alike), the provider
440 * can already be considered to be loaded, all that's needed is to
441 * initialize it. However, for dynamically loadable provider modules,
442 * we must first load that module.
444 * Built in modules are distinguished from dynamically loaded modules
445 * with an already assigned init function.
447 static const OSSL_DISPATCH *core_dispatch; /* Define further down */
449 int OSSL_PROVIDER_set_default_search_path(OSSL_LIB_CTX *libctx,
452 struct provider_store_st *store;
456 p = OPENSSL_strdup(path);
458 ERR_raise(ERR_LIB_CRYPTO, ERR_R_MALLOC_FAILURE);
462 if ((store = get_provider_store(libctx)) != NULL
463 && CRYPTO_THREAD_write_lock(store->default_path_lock)) {
464 OPENSSL_free(store->default_path);
465 store->default_path = p;
466 CRYPTO_THREAD_unlock(store->default_path_lock);
474 * Internal version that doesn't affect the store flags, and thereby avoid
475 * locking. Direct callers must remember to set the store flags when
478 static int provider_init(OSSL_PROVIDER *prov, int flag_lock)
480 const OSSL_DISPATCH *provider_dispatch = NULL;
481 void *tmp_provctx = NULL; /* safety measure */
482 #ifndef OPENSSL_NO_ERR
484 OSSL_FUNC_provider_get_reason_strings_fn *p_get_reason_strings = NULL;
490 * The flag lock is used to lock init, not only because the flag is
491 * checked here and set at the end, but also because this function
492 * modifies a number of things in the provider structure that this
493 * function needs to perform under lock anyway.
495 if (flag_lock && !CRYPTO_THREAD_write_lock(prov->flag_lock))
497 if (prov->flag_initialized) {
503 * If the init function isn't set, it indicates that this provider is
506 if (prov->init_function == NULL) {
510 if (prov->module == NULL) {
511 char *allocated_path = NULL;
512 const char *module_path = NULL;
513 char *merged_path = NULL;
514 const char *load_dir = NULL;
515 char *allocated_load_dir = NULL;
516 struct provider_store_st *store;
518 if ((prov->module = DSO_new()) == NULL) {
519 /* DSO_new() generates an error already */
523 if ((store = get_provider_store(prov->libctx)) == NULL
524 || !CRYPTO_THREAD_read_lock(store->default_path_lock))
527 if (store->default_path != NULL) {
528 allocated_load_dir = OPENSSL_strdup(store->default_path);
529 CRYPTO_THREAD_unlock(store->default_path_lock);
530 if (allocated_load_dir == NULL) {
531 ERR_raise(ERR_LIB_CRYPTO, ERR_R_MALLOC_FAILURE);
534 load_dir = allocated_load_dir;
536 CRYPTO_THREAD_unlock(store->default_path_lock);
539 if (load_dir == NULL) {
540 load_dir = ossl_safe_getenv("OPENSSL_MODULES");
541 if (load_dir == NULL)
542 load_dir = MODULESDIR;
545 DSO_ctrl(prov->module, DSO_CTRL_SET_FLAGS,
546 DSO_FLAG_NAME_TRANSLATION_EXT_ONLY, NULL);
548 module_path = prov->path;
549 if (module_path == NULL)
550 module_path = allocated_path =
551 DSO_convert_filename(prov->module, prov->name);
552 if (module_path != NULL)
553 merged_path = DSO_merge(prov->module, module_path, load_dir);
555 if (merged_path == NULL
556 || (DSO_load(prov->module, merged_path, NULL, 0)) == NULL) {
557 DSO_free(prov->module);
561 OPENSSL_free(merged_path);
562 OPENSSL_free(allocated_path);
563 OPENSSL_free(allocated_load_dir);
566 if (prov->module != NULL)
567 prov->init_function = (OSSL_provider_init_fn *)
568 DSO_bind_func(prov->module, "OSSL_provider_init");
572 /* Call the initialise function for the provider. */
573 if (prov->init_function == NULL
574 || !prov->init_function((OSSL_CORE_HANDLE *)prov, core_dispatch,
575 &provider_dispatch, &tmp_provctx)) {
576 ERR_raise_data(ERR_LIB_CRYPTO, ERR_R_INIT_FAIL,
577 "name=%s", prov->name);
580 prov->provctx = tmp_provctx;
582 for (; provider_dispatch->function_id != 0; provider_dispatch++) {
583 switch (provider_dispatch->function_id) {
584 case OSSL_FUNC_PROVIDER_TEARDOWN:
586 OSSL_FUNC_provider_teardown(provider_dispatch);
588 case OSSL_FUNC_PROVIDER_GETTABLE_PARAMS:
589 prov->gettable_params =
590 OSSL_FUNC_provider_gettable_params(provider_dispatch);
592 case OSSL_FUNC_PROVIDER_GET_PARAMS:
594 OSSL_FUNC_provider_get_params(provider_dispatch);
596 case OSSL_FUNC_PROVIDER_SELF_TEST:
598 OSSL_FUNC_provider_self_test(provider_dispatch);
600 case OSSL_FUNC_PROVIDER_GET_CAPABILITIES:
601 prov->get_capabilities =
602 OSSL_FUNC_provider_get_capabilities(provider_dispatch);
604 case OSSL_FUNC_PROVIDER_QUERY_OPERATION:
605 prov->query_operation =
606 OSSL_FUNC_provider_query_operation(provider_dispatch);
608 case OSSL_FUNC_PROVIDER_UNQUERY_OPERATION:
609 prov->unquery_operation =
610 OSSL_FUNC_provider_unquery_operation(provider_dispatch);
612 #ifndef OPENSSL_NO_ERR
614 case OSSL_FUNC_PROVIDER_GET_REASON_STRINGS:
615 p_get_reason_strings =
616 OSSL_FUNC_provider_get_reason_strings(provider_dispatch);
623 #ifndef OPENSSL_NO_ERR
625 if (p_get_reason_strings != NULL) {
626 const OSSL_ITEM *reasonstrings = p_get_reason_strings(prov->provctx);
630 * ERR_load_strings() handles ERR_STRING_DATA rather than OSSL_ITEM,
631 * although they are essentially the same type.
632 * Furthermore, ERR_load_strings() patches the array's error number
633 * with the error library number, so we need to make a copy of that
637 while (reasonstrings[cnt].id != 0) {
638 if (ERR_GET_LIB(reasonstrings[cnt].id) != 0)
642 cnt++; /* One for the terminating item */
644 /* Allocate one extra item for the "library" name */
645 prov->error_strings =
646 OPENSSL_zalloc(sizeof(ERR_STRING_DATA) * (cnt + 1));
647 if (prov->error_strings == NULL)
651 * Set the "library" name.
653 prov->error_strings[0].error = ERR_PACK(prov->error_lib, 0, 0);
654 prov->error_strings[0].string = prov->name;
656 * Copy reasonstrings item 0..cnt-1 to prov->error_trings positions
659 for (cnt2 = 1; cnt2 <= cnt; cnt2++) {
660 prov->error_strings[cnt2].error = (int)reasonstrings[cnt2-1].id;
661 prov->error_strings[cnt2].string = reasonstrings[cnt2-1].ptr;
664 ERR_load_strings(prov->error_lib, prov->error_strings);
669 /* With this flag set, this provider has become fully "loaded". */
670 prov->flag_initialized = 1;
675 CRYPTO_THREAD_unlock(prov->flag_lock);
679 static int provider_deactivate(OSSL_PROVIDER *prov)
681 if (!ossl_assert(prov != NULL))
684 if (!CRYPTO_THREAD_write_lock(prov->flag_lock))
687 if (--prov->activatecnt < 1)
688 prov->flag_activated = 0;
690 CRYPTO_THREAD_unlock(prov->flag_lock);
692 /* We don't deinit here, that's done in ossl_provider_free() */
696 static int provider_activate(OSSL_PROVIDER *prov, int flag_lock)
698 if (provider_init(prov, flag_lock)) {
699 if (flag_lock && !CRYPTO_THREAD_write_lock(prov->flag_lock))
702 prov->flag_activated = 1;
704 CRYPTO_THREAD_unlock(prov->flag_lock);
712 int ossl_provider_activate(OSSL_PROVIDER *prov, int retain_fallbacks)
716 if (provider_activate(prov, 1)) {
717 if (!retain_fallbacks) {
718 if (!CRYPTO_THREAD_write_lock(prov->store->lock)) {
719 provider_deactivate(prov);
722 prov->store->use_fallbacks = 0;
723 CRYPTO_THREAD_unlock(prov->store->lock);
730 int ossl_provider_deactivate(OSSL_PROVIDER *prov)
734 return provider_deactivate(prov);
737 void *ossl_provider_ctx(const OSSL_PROVIDER *prov)
739 return prov->provctx;
743 * This function only does something once when store->use_fallbacks == 1,
744 * and then sets store->use_fallbacks = 0, so the second call and so on is
745 * effectively a no-op.
747 static void provider_activate_fallbacks(struct provider_store_st *store)
751 int activated_fallback_count = 0;
754 if (!CRYPTO_THREAD_read_lock(store->lock))
756 use_fallbacks = store->use_fallbacks;
757 CRYPTO_THREAD_unlock(store->lock);
761 if (!CRYPTO_THREAD_write_lock(store->lock))
763 /* Check again, just in case another thread changed it */
764 use_fallbacks = store->use_fallbacks;
765 if (!use_fallbacks) {
766 CRYPTO_THREAD_unlock(store->lock);
770 num_provs = sk_OSSL_PROVIDER_num(store->providers);
771 for (i = 0; i < num_provs; i++) {
772 OSSL_PROVIDER *prov = sk_OSSL_PROVIDER_value(store->providers, i);
774 if (ossl_provider_up_ref(prov)) {
775 if (prov->flag_fallback) {
776 if (provider_activate(prov, 1))
777 activated_fallback_count++;
779 ossl_provider_free(prov);
784 * We assume that all fallbacks have been added to the store before
785 * any fallback is activated.
786 * TODO: We may have to reconsider this, IF we find ourselves adding
787 * fallbacks after any previous fallback has been activated.
789 if (activated_fallback_count > 0)
790 store->use_fallbacks = 0;
792 CRYPTO_THREAD_unlock(store->lock);
795 int ossl_provider_doall_activated(OSSL_LIB_CTX *ctx,
796 int (*cb)(OSSL_PROVIDER *provider,
800 int ret = 0, curr, max;
801 struct provider_store_st *store = get_provider_store(ctx);
802 STACK_OF(OSSL_PROVIDER) *provs = NULL;
806 * Make sure any providers are loaded from config before we try to use
809 OPENSSL_init_crypto(OPENSSL_INIT_LOAD_CONFIG, NULL);
814 provider_activate_fallbacks(store);
817 * Under lock, grab a copy of the provider list and up_ref each
818 * provider so that they don't disappear underneath us.
820 if (!CRYPTO_THREAD_read_lock(store->lock))
822 provs = sk_OSSL_PROVIDER_dup(store->providers);
824 CRYPTO_THREAD_unlock(store->lock);
827 max = sk_OSSL_PROVIDER_num(provs);
829 * We work backwards through the stack so that we can safely delete items
832 for (curr = max - 1; curr >= 0; curr--) {
833 OSSL_PROVIDER *prov = sk_OSSL_PROVIDER_value(provs, curr);
835 if (!CRYPTO_THREAD_write_lock(prov->flag_lock))
837 if (prov->flag_activated) {
838 if (!ossl_provider_up_ref(prov)){
839 CRYPTO_THREAD_unlock(prov->flag_lock);
843 * It's already activated, but we up the activated count to ensure
844 * it remains activated until after we've called the user callback.
846 if (!provider_activate(prov, 0)) {
847 ossl_provider_free(prov);
848 CRYPTO_THREAD_unlock(prov->flag_lock);
852 sk_OSSL_PROVIDER_delete(provs, curr);
855 CRYPTO_THREAD_unlock(prov->flag_lock);
857 CRYPTO_THREAD_unlock(store->lock);
860 * Now, we sweep through all providers not under lock
862 for (curr = 0; curr < max; curr++) {
863 OSSL_PROVIDER *prov = sk_OSSL_PROVIDER_value(provs, curr);
865 if (!cb(prov, cbdata))
874 CRYPTO_THREAD_unlock(store->lock);
877 * The pop_free call doesn't do what we want on an error condition. We
878 * either start from the first item in the stack, or part way through if
879 * we only processed some of the items.
881 for (curr++; curr < max; curr++) {
882 OSSL_PROVIDER *prov = sk_OSSL_PROVIDER_value(provs, curr);
884 provider_deactivate(prov);
885 ossl_provider_free(prov);
887 sk_OSSL_PROVIDER_free(provs);
891 int ossl_provider_available(OSSL_PROVIDER *prov)
896 provider_activate_fallbacks(prov->store);
898 if (!CRYPTO_THREAD_read_lock(prov->flag_lock))
900 ret = prov->flag_activated;
901 CRYPTO_THREAD_unlock(prov->flag_lock);
907 /* Setters of Provider Object data */
908 int ossl_provider_set_fallback(OSSL_PROVIDER *prov)
913 prov->flag_fallback = 1;
917 /* Getters of Provider Object data */
918 const char *ossl_provider_name(const OSSL_PROVIDER *prov)
923 const DSO *ossl_provider_dso(const OSSL_PROVIDER *prov)
928 const char *ossl_provider_module_name(const OSSL_PROVIDER *prov)
933 return DSO_get_filename(prov->module);
937 const char *ossl_provider_module_path(const OSSL_PROVIDER *prov)
942 /* FIXME: Ensure it's a full path */
943 return DSO_get_filename(prov->module);
947 void *ossl_provider_prov_ctx(const OSSL_PROVIDER *prov)
950 return prov->provctx;
955 OSSL_LIB_CTX *ossl_provider_libctx(const OSSL_PROVIDER *prov)
957 return prov != NULL ? prov->libctx : NULL;
960 /* Wrappers around calls to the provider */
961 void ossl_provider_teardown(const OSSL_PROVIDER *prov)
963 if (prov->teardown != NULL)
964 prov->teardown(prov->provctx);
967 const OSSL_PARAM *ossl_provider_gettable_params(const OSSL_PROVIDER *prov)
969 return prov->gettable_params == NULL
970 ? NULL : prov->gettable_params(prov->provctx);
973 int ossl_provider_get_params(const OSSL_PROVIDER *prov, OSSL_PARAM params[])
975 return prov->get_params == NULL
976 ? 0 : prov->get_params(prov->provctx, params);
979 int ossl_provider_self_test(const OSSL_PROVIDER *prov)
983 if (prov->self_test == NULL)
985 ret = prov->self_test(prov->provctx);
987 (void)evp_method_store_flush(ossl_provider_libctx(prov));
991 int ossl_provider_get_capabilities(const OSSL_PROVIDER *prov,
992 const char *capability,
996 return prov->get_capabilities == NULL
997 ? 1 : prov->get_capabilities(prov->provctx, capability, cb, arg);
1000 const OSSL_ALGORITHM *ossl_provider_query_operation(const OSSL_PROVIDER *prov,
1004 const OSSL_ALGORITHM *res;
1006 if (prov->query_operation == NULL)
1008 res = prov->query_operation(prov->provctx, operation_id, no_cache);
1009 #if defined(OPENSSL_NO_CACHED_FETCH)
1010 /* Forcing the non-caching of queries */
1011 if (no_cache != NULL)
1017 void ossl_provider_unquery_operation(const OSSL_PROVIDER *prov,
1019 const OSSL_ALGORITHM *algs)
1021 if (prov->unquery_operation != NULL)
1022 prov->unquery_operation(prov->provctx, operation_id, algs);
1025 int ossl_provider_set_operation_bit(OSSL_PROVIDER *provider, size_t bitnum)
1027 size_t byte = bitnum / 8;
1028 unsigned char bit = (1 << (bitnum % 8)) & 0xFF;
1030 if (!CRYPTO_THREAD_write_lock(provider->opbits_lock))
1032 if (provider->operation_bits_sz <= byte) {
1033 unsigned char *tmp = OPENSSL_realloc(provider->operation_bits,
1037 CRYPTO_THREAD_unlock(provider->opbits_lock);
1038 ERR_raise(ERR_LIB_CRYPTO, ERR_R_MALLOC_FAILURE);
1041 provider->operation_bits = tmp;
1042 memset(provider->operation_bits + provider->operation_bits_sz,
1043 '\0', byte + 1 - provider->operation_bits_sz);
1044 provider->operation_bits_sz = byte + 1;
1046 provider->operation_bits[byte] |= bit;
1047 CRYPTO_THREAD_unlock(provider->opbits_lock);
1051 int ossl_provider_test_operation_bit(OSSL_PROVIDER *provider, size_t bitnum,
1054 size_t byte = bitnum / 8;
1055 unsigned char bit = (1 << (bitnum % 8)) & 0xFF;
1057 if (!ossl_assert(result != NULL)) {
1058 ERR_raise(ERR_LIB_CRYPTO, ERR_R_PASSED_NULL_PARAMETER);
1063 if (!CRYPTO_THREAD_read_lock(provider->opbits_lock))
1065 if (provider->operation_bits_sz > byte)
1066 *result = ((provider->operation_bits[byte] & bit) != 0);
1067 CRYPTO_THREAD_unlock(provider->opbits_lock);
1072 * Core functions for the provider
1073 * ===============================
1075 * This is the set of functions that the core makes available to the provider
1079 * This returns a list of Provider Object parameters with their types, for
1080 * discovery. We do not expect that many providers will use this, but one
1083 static const OSSL_PARAM param_types[] = {
1084 OSSL_PARAM_DEFN(OSSL_PROV_PARAM_CORE_VERSION, OSSL_PARAM_UTF8_PTR, NULL, 0),
1085 OSSL_PARAM_DEFN(OSSL_PROV_PARAM_CORE_PROV_NAME, OSSL_PARAM_UTF8_PTR,
1088 OSSL_PARAM_DEFN(OSSL_PROV_PARAM_CORE_MODULE_FILENAME, OSSL_PARAM_UTF8_PTR,
1095 * Forward declare all the functions that are provided aa dispatch.
1096 * This ensures that the compiler will complain if they aren't defined
1097 * with the correct signature.
1099 static OSSL_FUNC_core_gettable_params_fn core_gettable_params;
1100 static OSSL_FUNC_core_get_params_fn core_get_params;
1101 static OSSL_FUNC_core_thread_start_fn core_thread_start;
1102 static OSSL_FUNC_core_get_libctx_fn core_get_libctx;
1104 static OSSL_FUNC_core_new_error_fn core_new_error;
1105 static OSSL_FUNC_core_set_error_debug_fn core_set_error_debug;
1106 static OSSL_FUNC_core_vset_error_fn core_vset_error;
1107 static OSSL_FUNC_core_set_error_mark_fn core_set_error_mark;
1108 static OSSL_FUNC_core_clear_last_error_mark_fn core_clear_last_error_mark;
1109 static OSSL_FUNC_core_pop_error_to_mark_fn core_pop_error_to_mark;
1112 static const OSSL_PARAM *core_gettable_params(const OSSL_CORE_HANDLE *handle)
1117 static int core_get_params(const OSSL_CORE_HANDLE *handle, OSSL_PARAM params[])
1122 * We created this object originally and we know it is actually an
1123 * OSSL_PROVIDER *, so the cast is safe
1125 OSSL_PROVIDER *prov = (OSSL_PROVIDER *)handle;
1127 if ((p = OSSL_PARAM_locate(params, OSSL_PROV_PARAM_CORE_VERSION)) != NULL)
1128 OSSL_PARAM_set_utf8_ptr(p, OPENSSL_VERSION_STR);
1129 if ((p = OSSL_PARAM_locate(params, OSSL_PROV_PARAM_CORE_PROV_NAME)) != NULL)
1130 OSSL_PARAM_set_utf8_ptr(p, prov->name);
1133 if ((p = OSSL_PARAM_locate(params,
1134 OSSL_PROV_PARAM_CORE_MODULE_FILENAME)) != NULL)
1135 OSSL_PARAM_set_utf8_ptr(p, ossl_provider_module_path(prov));
1138 if (prov->parameters == NULL)
1141 for (i = 0; i < sk_INFOPAIR_num(prov->parameters); i++) {
1142 INFOPAIR *pair = sk_INFOPAIR_value(prov->parameters, i);
1144 if ((p = OSSL_PARAM_locate(params, pair->name)) != NULL)
1145 OSSL_PARAM_set_utf8_ptr(p, pair->value);
1150 static OPENSSL_CORE_CTX *core_get_libctx(const OSSL_CORE_HANDLE *handle)
1153 * We created this object originally and we know it is actually an
1154 * OSSL_PROVIDER *, so the cast is safe
1156 OSSL_PROVIDER *prov = (OSSL_PROVIDER *)handle;
1159 * Using ossl_provider_libctx would be wrong as that returns
1160 * NULL for |prov| == NULL and NULL libctx has a special meaning
1161 * that does not apply here. Here |prov| == NULL can happen only in
1162 * case of a coding error.
1164 assert(prov != NULL);
1165 return (OPENSSL_CORE_CTX *)prov->libctx;
1168 static int core_thread_start(const OSSL_CORE_HANDLE *handle,
1169 OSSL_thread_stop_handler_fn handfn)
1172 * We created this object originally and we know it is actually an
1173 * OSSL_PROVIDER *, so the cast is safe
1175 OSSL_PROVIDER *prov = (OSSL_PROVIDER *)handle;
1177 return ossl_init_thread_start(prov, prov->provctx, handfn);
1181 * The FIPS module inner provider doesn't implement these. They aren't
1182 * needed there, since the FIPS module upcalls are always the outer provider
1187 * These error functions should use |handle| to select the proper
1188 * library context to report in the correct error stack if error
1189 * stacks become tied to the library context.
1190 * We cannot currently do that since there's no support for it in the
1193 static void core_new_error(const OSSL_CORE_HANDLE *handle)
1198 static void core_set_error_debug(const OSSL_CORE_HANDLE *handle,
1199 const char *file, int line, const char *func)
1201 ERR_set_debug(file, line, func);
1204 static void core_vset_error(const OSSL_CORE_HANDLE *handle,
1205 uint32_t reason, const char *fmt, va_list args)
1208 * We created this object originally and we know it is actually an
1209 * OSSL_PROVIDER *, so the cast is safe
1211 OSSL_PROVIDER *prov = (OSSL_PROVIDER *)handle;
1214 * If the uppermost 8 bits are non-zero, it's an OpenSSL library
1215 * error and will be treated as such. Otherwise, it's a new style
1216 * provider error and will be treated as such.
1218 if (ERR_GET_LIB(reason) != 0) {
1219 ERR_vset_error(ERR_GET_LIB(reason), ERR_GET_REASON(reason), fmt, args);
1221 ERR_vset_error(prov->error_lib, (int)reason, fmt, args);
1225 static int core_set_error_mark(const OSSL_CORE_HANDLE *handle)
1227 return ERR_set_mark();
1230 static int core_clear_last_error_mark(const OSSL_CORE_HANDLE *handle)
1232 return ERR_clear_last_mark();
1235 static int core_pop_error_to_mark(const OSSL_CORE_HANDLE *handle)
1237 return ERR_pop_to_mark();
1239 #endif /* FIPS_MODULE */
1242 * Functions provided by the core.
1244 static const OSSL_DISPATCH core_dispatch_[] = {
1245 { OSSL_FUNC_CORE_GETTABLE_PARAMS, (void (*)(void))core_gettable_params },
1246 { OSSL_FUNC_CORE_GET_PARAMS, (void (*)(void))core_get_params },
1247 { OSSL_FUNC_CORE_GET_LIBCTX, (void (*)(void))core_get_libctx },
1248 { OSSL_FUNC_CORE_THREAD_START, (void (*)(void))core_thread_start },
1250 { OSSL_FUNC_CORE_NEW_ERROR, (void (*)(void))core_new_error },
1251 { OSSL_FUNC_CORE_SET_ERROR_DEBUG, (void (*)(void))core_set_error_debug },
1252 { OSSL_FUNC_CORE_VSET_ERROR, (void (*)(void))core_vset_error },
1253 { OSSL_FUNC_CORE_SET_ERROR_MARK, (void (*)(void))core_set_error_mark },
1254 { OSSL_FUNC_CORE_CLEAR_LAST_ERROR_MARK,
1255 (void (*)(void))core_clear_last_error_mark },
1256 { OSSL_FUNC_CORE_POP_ERROR_TO_MARK, (void (*)(void))core_pop_error_to_mark },
1257 { OSSL_FUNC_BIO_NEW_FILE, (void (*)(void))ossl_core_bio_new_file },
1258 { OSSL_FUNC_BIO_NEW_MEMBUF, (void (*)(void))ossl_core_bio_new_mem_buf },
1259 { OSSL_FUNC_BIO_READ_EX, (void (*)(void))ossl_core_bio_read_ex },
1260 { OSSL_FUNC_BIO_WRITE_EX, (void (*)(void))ossl_core_bio_write_ex },
1261 { OSSL_FUNC_BIO_GETS, (void (*)(void))ossl_core_bio_gets },
1262 { OSSL_FUNC_BIO_PUTS, (void (*)(void))ossl_core_bio_puts },
1263 { OSSL_FUNC_BIO_CTRL, (void (*)(void))ossl_core_bio_ctrl },
1264 { OSSL_FUNC_BIO_UP_REF, (void (*)(void))ossl_core_bio_up_ref },
1265 { OSSL_FUNC_BIO_FREE, (void (*)(void))ossl_core_bio_free },
1266 { OSSL_FUNC_BIO_VPRINTF, (void (*)(void))ossl_core_bio_vprintf },
1267 { OSSL_FUNC_BIO_VSNPRINTF, (void (*)(void))BIO_vsnprintf },
1268 { OSSL_FUNC_SELF_TEST_CB, (void (*)(void))OSSL_SELF_TEST_get_callback },
1269 { OSSL_FUNC_GET_ENTROPY, (void (*)(void))ossl_rand_get_entropy },
1270 { OSSL_FUNC_CLEANUP_ENTROPY, (void (*)(void))ossl_rand_cleanup_entropy },
1271 { OSSL_FUNC_GET_NONCE, (void (*)(void))ossl_rand_get_nonce },
1272 { OSSL_FUNC_CLEANUP_NONCE, (void (*)(void))ossl_rand_cleanup_nonce },
1274 { OSSL_FUNC_CRYPTO_MALLOC, (void (*)(void))CRYPTO_malloc },
1275 { OSSL_FUNC_CRYPTO_ZALLOC, (void (*)(void))CRYPTO_zalloc },
1276 { OSSL_FUNC_CRYPTO_FREE, (void (*)(void))CRYPTO_free },
1277 { OSSL_FUNC_CRYPTO_CLEAR_FREE, (void (*)(void))CRYPTO_clear_free },
1278 { OSSL_FUNC_CRYPTO_REALLOC, (void (*)(void))CRYPTO_realloc },
1279 { OSSL_FUNC_CRYPTO_CLEAR_REALLOC, (void (*)(void))CRYPTO_clear_realloc },
1280 { OSSL_FUNC_CRYPTO_SECURE_MALLOC, (void (*)(void))CRYPTO_secure_malloc },
1281 { OSSL_FUNC_CRYPTO_SECURE_ZALLOC, (void (*)(void))CRYPTO_secure_zalloc },
1282 { OSSL_FUNC_CRYPTO_SECURE_FREE, (void (*)(void))CRYPTO_secure_free },
1283 { OSSL_FUNC_CRYPTO_SECURE_CLEAR_FREE,
1284 (void (*)(void))CRYPTO_secure_clear_free },
1285 { OSSL_FUNC_CRYPTO_SECURE_ALLOCATED,
1286 (void (*)(void))CRYPTO_secure_allocated },
1287 { OSSL_FUNC_OPENSSL_CLEANSE, (void (*)(void))OPENSSL_cleanse },
1291 static const OSSL_DISPATCH *core_dispatch = core_dispatch_;