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 */
51 unsigned int flag_activated_as_fallback:1;
53 /* Getting and setting the flags require synchronization */
54 CRYPTO_RWLOCK *flag_lock;
56 /* OpenSSL library side data */
57 CRYPTO_REF_COUNT refcnt;
58 CRYPTO_RWLOCK *refcnt_lock; /* For the ref counter */
59 CRYPTO_REF_COUNT activatecnt;
60 CRYPTO_RWLOCK *activatecnt_lock; /* For the activate counter */
64 OSSL_provider_init_fn *init_function;
65 STACK_OF(INFOPAIR) *parameters;
66 OSSL_LIB_CTX *libctx; /* The library context this instance is in */
67 struct provider_store_st *store; /* The store this instance belongs to */
70 * In the FIPS module inner provider, this isn't needed, since the
71 * error upcalls are always direct calls to the outer provider.
73 int error_lib; /* ERR library number, one for each provider */
74 # ifndef OPENSSL_NO_ERR
75 ERR_STRING_DATA *error_strings; /* Copy of what the provider gives us */
79 /* Provider side functions */
80 OSSL_FUNC_provider_teardown_fn *teardown;
81 OSSL_FUNC_provider_gettable_params_fn *gettable_params;
82 OSSL_FUNC_provider_get_params_fn *get_params;
83 OSSL_FUNC_provider_get_capabilities_fn *get_capabilities;
84 OSSL_FUNC_provider_self_test_fn *self_test;
85 OSSL_FUNC_provider_query_operation_fn *query_operation;
86 OSSL_FUNC_provider_unquery_operation_fn *unquery_operation;
89 * Cache of bit to indicate of query_operation() has been called on
90 * a specific operation or not.
92 unsigned char *operation_bits;
93 size_t operation_bits_sz;
94 CRYPTO_RWLOCK *opbits_lock;
96 /* Provider side data */
99 DEFINE_STACK_OF(OSSL_PROVIDER)
101 static int ossl_provider_cmp(const OSSL_PROVIDER * const *a,
102 const OSSL_PROVIDER * const *b)
104 return strcmp((*a)->name, (*b)->name);
108 * Provider Object store
109 * =====================
111 * The Provider Object store is a library context object, and therefore needs
115 struct provider_store_st {
116 STACK_OF(OSSL_PROVIDER) *providers;
117 CRYPTO_RWLOCK *default_path_lock;
120 unsigned int use_fallbacks:1;
124 * provider_deactivate_free() is a wrapper around ossl_provider_deactivate()
125 * and ossl_provider_free(), called as needed.
126 * Since this is only called when the provider store is being emptied, we
127 * don't need to care about any lock.
129 static void provider_deactivate_free(OSSL_PROVIDER *prov)
131 if (prov->flag_activated)
132 ossl_provider_deactivate(prov);
133 ossl_provider_free(prov);
136 static void provider_store_free(void *vstore)
138 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
237 OPENSSL_init_crypto(OPENSSL_INIT_LOAD_CONFIG, NULL);
240 tmpl.name = (char *)name;
241 if (!CRYPTO_THREAD_write_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
266 || (prov->activatecnt_lock = CRYPTO_THREAD_lock_new()) == NULL
268 || !ossl_provider_up_ref(prov) /* +1 One reference to be returned */
269 || (prov->opbits_lock = CRYPTO_THREAD_lock_new()) == NULL
270 || (prov->flag_lock = CRYPTO_THREAD_lock_new()) == NULL
271 || (prov->name = OPENSSL_strdup(name)) == NULL) {
272 ossl_provider_free(prov);
273 ERR_raise(ERR_LIB_CRYPTO, ERR_R_MALLOC_FAILURE);
277 prov->init_function = init_function;
281 int ossl_provider_up_ref(OSSL_PROVIDER *prov)
285 if (CRYPTO_UP_REF(&prov->refcnt, &ref, prov->refcnt_lock) <= 0)
290 OSSL_PROVIDER *ossl_provider_new(OSSL_LIB_CTX *libctx, const char *name,
291 OSSL_provider_init_fn *init_function,
294 struct provider_store_st *store = NULL;
295 OSSL_PROVIDER *prov = NULL;
297 if ((store = get_provider_store(libctx)) == NULL)
300 if ((prov = ossl_provider_find(libctx, name,
301 noconfig)) != NULL) { /* refcount +1 */
302 ossl_provider_free(prov); /* refcount -1 */
303 ERR_raise_data(ERR_LIB_CRYPTO, CRYPTO_R_PROVIDER_ALREADY_EXISTS,
308 /* provider_new() generates an error, so no need here */
309 if ((prov = provider_new(name, init_function)) == NULL)
312 if (!CRYPTO_THREAD_write_lock(store->lock))
314 if (!ossl_provider_up_ref(prov)) { /* +1 One reference for the store */
315 ossl_provider_free(prov); /* -1 Reference that was to be returned */
317 } else if (sk_OSSL_PROVIDER_push(store->providers, prov) == 0) {
318 ossl_provider_free(prov); /* -1 Store reference */
319 ossl_provider_free(prov); /* -1 Reference that was to be returned */
322 prov->libctx = libctx;
325 prov->error_lib = ERR_get_next_error_library();
328 CRYPTO_THREAD_unlock(store->lock);
331 ERR_raise(ERR_LIB_CRYPTO, ERR_R_MALLOC_FAILURE);
334 * At this point, the provider is only partially "loaded". To be
335 * fully "loaded", ossl_provider_activate() must also be called.
341 static void free_infopair(INFOPAIR *pair)
343 OPENSSL_free(pair->name);
344 OPENSSL_free(pair->value);
348 void ossl_provider_free(OSSL_PROVIDER *prov)
353 CRYPTO_DOWN_REF(&prov->refcnt, &ref, prov->refcnt_lock);
356 * When the refcount drops to zero, we clean up the provider.
357 * Note that this also does teardown, which may seem late,
358 * considering that init happens on first activation. However,
359 * there may be other structures hanging on to the provider after
360 * the last deactivation and may therefore need full access to the
361 * provider's services. Therefore, we deinit late.
364 if (prov->flag_initialized) {
365 if (prov->teardown != NULL)
366 prov->teardown(prov->provctx);
367 #ifndef OPENSSL_NO_ERR
369 if (prov->error_strings != NULL) {
370 ERR_unload_strings(prov->error_lib, prov->error_strings);
371 OPENSSL_free(prov->error_strings);
372 prov->error_strings = NULL;
376 OPENSSL_free(prov->operation_bits);
377 prov->operation_bits = NULL;
378 prov->operation_bits_sz = 0;
379 prov->flag_initialized = 0;
384 * We deregister thread handling whether or not the provider was
385 * initialized. If init was attempted but was not successful then
386 * the provider may still have registered a thread handler.
388 ossl_init_thread_deregister(prov);
389 DSO_free(prov->module);
391 OPENSSL_free(prov->name);
392 OPENSSL_free(prov->path);
393 sk_INFOPAIR_pop_free(prov->parameters, free_infopair);
394 CRYPTO_THREAD_lock_free(prov->opbits_lock);
395 CRYPTO_THREAD_lock_free(prov->flag_lock);
397 CRYPTO_THREAD_lock_free(prov->refcnt_lock);
398 CRYPTO_THREAD_lock_free(prov->activatecnt_lock);
406 int ossl_provider_set_module_path(OSSL_PROVIDER *prov, const char *module_path)
408 OPENSSL_free(prov->path);
409 if (module_path == NULL)
411 if ((prov->path = OPENSSL_strdup(module_path)) != NULL)
413 ERR_raise(ERR_LIB_CRYPTO, ERR_R_MALLOC_FAILURE);
417 int ossl_provider_add_parameter(OSSL_PROVIDER *prov,
418 const char *name, const char *value)
420 INFOPAIR *pair = NULL;
422 if ((pair = OPENSSL_zalloc(sizeof(*pair))) != NULL
423 && (prov->parameters != NULL
424 || (prov->parameters = sk_INFOPAIR_new_null()) != NULL)
425 && (pair->name = OPENSSL_strdup(name)) != NULL
426 && (pair->value = OPENSSL_strdup(value)) != NULL
427 && sk_INFOPAIR_push(prov->parameters, pair) > 0)
431 OPENSSL_free(pair->name);
432 OPENSSL_free(pair->value);
435 ERR_raise(ERR_LIB_CRYPTO, ERR_R_MALLOC_FAILURE);
440 * Provider activation.
442 * What "activation" means depends on the provider form; for built in
443 * providers (in the library or the application alike), the provider
444 * can already be considered to be loaded, all that's needed is to
445 * initialize it. However, for dynamically loadable provider modules,
446 * we must first load that module.
448 * Built in modules are distinguished from dynamically loaded modules
449 * with an already assigned init function.
451 static const OSSL_DISPATCH *core_dispatch; /* Define further down */
453 int OSSL_PROVIDER_set_default_search_path(OSSL_LIB_CTX *libctx,
456 struct provider_store_st *store;
460 p = OPENSSL_strdup(path);
462 ERR_raise(ERR_LIB_CRYPTO, ERR_R_MALLOC_FAILURE);
466 if ((store = get_provider_store(libctx)) != NULL
467 && CRYPTO_THREAD_write_lock(store->default_path_lock)) {
468 OPENSSL_free(store->default_path);
469 store->default_path = p;
470 CRYPTO_THREAD_unlock(store->default_path_lock);
478 * Internal version that doesn't affect the store flags, and thereby avoid
479 * locking. Direct callers must remember to set the store flags when
482 static int provider_init(OSSL_PROVIDER *prov)
484 const OSSL_DISPATCH *provider_dispatch = NULL;
485 void *tmp_provctx = NULL; /* safety measure */
486 #ifndef OPENSSL_NO_ERR
488 OSSL_FUNC_provider_get_reason_strings_fn *p_get_reason_strings = NULL;
494 * The flag lock is used to lock init, not only because the flag is
495 * checked here and set at the end, but also because this function
496 * modifies a number of things in the provider structure that this
497 * function needs to perform under lock anyway.
499 if (!CRYPTO_THREAD_write_lock(prov->flag_lock))
501 if (prov->flag_initialized) {
507 * If the init function isn't set, it indicates that this provider is
510 if (prov->init_function == NULL) {
514 if (prov->module == NULL) {
515 char *allocated_path = NULL;
516 const char *module_path = NULL;
517 char *merged_path = NULL;
518 const char *load_dir = NULL;
519 char *allocated_load_dir = NULL;
520 struct provider_store_st *store;
522 if ((prov->module = DSO_new()) == NULL) {
523 /* DSO_new() generates an error already */
527 if ((store = get_provider_store(prov->libctx)) == NULL
528 || !CRYPTO_THREAD_read_lock(store->default_path_lock))
531 if (store->default_path != NULL) {
532 allocated_load_dir = OPENSSL_strdup(store->default_path);
533 CRYPTO_THREAD_unlock(store->default_path_lock);
534 if (allocated_load_dir == NULL) {
535 ERR_raise(ERR_LIB_CRYPTO, ERR_R_MALLOC_FAILURE);
538 load_dir = allocated_load_dir;
540 CRYPTO_THREAD_unlock(store->default_path_lock);
543 if (load_dir == NULL) {
544 load_dir = ossl_safe_getenv("OPENSSL_MODULES");
545 if (load_dir == NULL)
546 load_dir = MODULESDIR;
549 DSO_ctrl(prov->module, DSO_CTRL_SET_FLAGS,
550 DSO_FLAG_NAME_TRANSLATION_EXT_ONLY, NULL);
552 module_path = prov->path;
553 if (module_path == NULL)
554 module_path = allocated_path =
555 DSO_convert_filename(prov->module, prov->name);
556 if (module_path != NULL)
557 merged_path = DSO_merge(prov->module, module_path, load_dir);
559 if (merged_path == NULL
560 || (DSO_load(prov->module, merged_path, NULL, 0)) == NULL) {
561 DSO_free(prov->module);
565 OPENSSL_free(merged_path);
566 OPENSSL_free(allocated_path);
567 OPENSSL_free(allocated_load_dir);
570 if (prov->module != NULL)
571 prov->init_function = (OSSL_provider_init_fn *)
572 DSO_bind_func(prov->module, "OSSL_provider_init");
576 /* Call the initialise function for the provider. */
577 if (prov->init_function == NULL
578 || !prov->init_function((OSSL_CORE_HANDLE *)prov, core_dispatch,
579 &provider_dispatch, &tmp_provctx)) {
580 ERR_raise_data(ERR_LIB_CRYPTO, ERR_R_INIT_FAIL,
581 "name=%s", prov->name);
584 prov->provctx = tmp_provctx;
586 for (; provider_dispatch->function_id != 0; provider_dispatch++) {
587 switch (provider_dispatch->function_id) {
588 case OSSL_FUNC_PROVIDER_TEARDOWN:
590 OSSL_FUNC_provider_teardown(provider_dispatch);
592 case OSSL_FUNC_PROVIDER_GETTABLE_PARAMS:
593 prov->gettable_params =
594 OSSL_FUNC_provider_gettable_params(provider_dispatch);
596 case OSSL_FUNC_PROVIDER_GET_PARAMS:
598 OSSL_FUNC_provider_get_params(provider_dispatch);
600 case OSSL_FUNC_PROVIDER_SELF_TEST:
602 OSSL_FUNC_provider_self_test(provider_dispatch);
604 case OSSL_FUNC_PROVIDER_GET_CAPABILITIES:
605 prov->get_capabilities =
606 OSSL_FUNC_provider_get_capabilities(provider_dispatch);
608 case OSSL_FUNC_PROVIDER_QUERY_OPERATION:
609 prov->query_operation =
610 OSSL_FUNC_provider_query_operation(provider_dispatch);
612 case OSSL_FUNC_PROVIDER_UNQUERY_OPERATION:
613 prov->unquery_operation =
614 OSSL_FUNC_provider_unquery_operation(provider_dispatch);
616 #ifndef OPENSSL_NO_ERR
618 case OSSL_FUNC_PROVIDER_GET_REASON_STRINGS:
619 p_get_reason_strings =
620 OSSL_FUNC_provider_get_reason_strings(provider_dispatch);
627 #ifndef OPENSSL_NO_ERR
629 if (p_get_reason_strings != NULL) {
630 const OSSL_ITEM *reasonstrings = p_get_reason_strings(prov->provctx);
634 * ERR_load_strings() handles ERR_STRING_DATA rather than OSSL_ITEM,
635 * although they are essentially the same type.
636 * Furthermore, ERR_load_strings() patches the array's error number
637 * with the error library number, so we need to make a copy of that
641 while (reasonstrings[cnt].id != 0) {
642 if (ERR_GET_LIB(reasonstrings[cnt].id) != 0)
646 cnt++; /* One for the terminating item */
648 /* Allocate one extra item for the "library" name */
649 prov->error_strings =
650 OPENSSL_zalloc(sizeof(ERR_STRING_DATA) * (cnt + 1));
651 if (prov->error_strings == NULL)
655 * Set the "library" name.
657 prov->error_strings[0].error = ERR_PACK(prov->error_lib, 0, 0);
658 prov->error_strings[0].string = prov->name;
660 * Copy reasonstrings item 0..cnt-1 to prov->error_trings positions
663 for (cnt2 = 1; cnt2 <= cnt; cnt2++) {
664 prov->error_strings[cnt2].error = (int)reasonstrings[cnt2-1].id;
665 prov->error_strings[cnt2].string = reasonstrings[cnt2-1].ptr;
668 ERR_load_strings(prov->error_lib, prov->error_strings);
673 /* With this flag set, this provider has become fully "loaded". */
674 prov->flag_initialized = 1;
678 CRYPTO_THREAD_unlock(prov->flag_lock);
682 static int provider_deactivate(OSSL_PROVIDER *prov)
686 if (!ossl_assert(prov != NULL))
689 if (CRYPTO_DOWN_REF(&prov->activatecnt, &ref, prov->activatecnt_lock) <= 0)
693 if (!CRYPTO_THREAD_write_lock(prov->flag_lock))
695 prov->flag_activated = 0;
696 CRYPTO_THREAD_unlock(prov->flag_lock);
699 /* We don't deinit here, that's done in ossl_provider_free() */
703 static int provider_activate(OSSL_PROVIDER *prov)
707 if (CRYPTO_UP_REF(&prov->activatecnt, &ref, prov->activatecnt_lock) <= 0)
710 if (provider_init(prov)) {
711 if (!CRYPTO_THREAD_write_lock(prov->flag_lock))
713 prov->flag_activated = 1;
714 CRYPTO_THREAD_unlock(prov->flag_lock);
719 provider_deactivate(prov);
723 int ossl_provider_activate(OSSL_PROVIDER *prov, int retain_fallbacks)
727 if (provider_activate(prov)) {
728 if (!retain_fallbacks) {
729 if (!CRYPTO_THREAD_write_lock(prov->store->lock)) {
730 provider_deactivate(prov);
733 prov->store->use_fallbacks = 0;
734 CRYPTO_THREAD_unlock(prov->store->lock);
741 int ossl_provider_deactivate(OSSL_PROVIDER *prov)
745 return provider_deactivate(prov);
748 void *ossl_provider_ctx(const OSSL_PROVIDER *prov)
750 return prov->provctx;
754 * This function only does something once when store->use_fallbacks == 1,
755 * and then sets store->use_fallbacks = 0, so the second call and so on is
756 * effectively a no-op.
758 static void provider_activate_fallbacks(struct provider_store_st *store)
762 int activated_fallback_count = 0;
765 if (!CRYPTO_THREAD_read_lock(store->lock))
767 use_fallbacks = store->use_fallbacks;
768 CRYPTO_THREAD_unlock(store->lock);
772 if (!CRYPTO_THREAD_write_lock(store->lock))
774 /* Check again, just in case another thread changed it */
775 use_fallbacks = store->use_fallbacks;
776 if (!use_fallbacks) {
777 CRYPTO_THREAD_unlock(store->lock);
781 num_provs = sk_OSSL_PROVIDER_num(store->providers);
782 for (i = 0; i < num_provs; i++) {
783 OSSL_PROVIDER *prov = sk_OSSL_PROVIDER_value(store->providers, i);
785 if (ossl_provider_up_ref(prov)) {
786 if (prov->flag_fallback) {
787 if (provider_activate(prov)) {
788 prov->flag_activated_as_fallback = 1;
789 activated_fallback_count++;
792 ossl_provider_free(prov);
797 * We assume that all fallbacks have been added to the store before
798 * any fallback is activated.
799 * TODO: We may have to reconsider this, IF we find ourselves adding
800 * fallbacks after any previous fallback has been activated.
802 if (activated_fallback_count > 0)
803 store->use_fallbacks = 0;
805 CRYPTO_THREAD_unlock(store->lock);
808 int ossl_provider_doall_activated(OSSL_LIB_CTX *ctx,
809 int (*cb)(OSSL_PROVIDER *provider,
814 struct provider_store_st *store = get_provider_store(ctx);
815 STACK_OF(OSSL_PROVIDER) *provs = NULL;
819 * Make sure any providers are loaded from config before we try to use
822 OPENSSL_init_crypto(OPENSSL_INIT_LOAD_CONFIG, NULL);
827 provider_activate_fallbacks(store);
830 * Under lock, grab a copy of the provider list and up_ref each
831 * provider so that they don't disappear underneath us.
833 if (!CRYPTO_THREAD_read_lock(store->lock))
835 provs = sk_OSSL_PROVIDER_dup(store->providers);
837 CRYPTO_THREAD_unlock(store->lock);
840 j = sk_OSSL_PROVIDER_num(provs);
841 for (i = 0; i < j; i++)
842 if (!ossl_provider_up_ref(sk_OSSL_PROVIDER_value(provs, i)))
844 CRYPTO_THREAD_unlock(store->lock);
847 * Now, we sweep through all providers not under lock
849 for (j = 0; j < i; j++) {
850 OSSL_PROVIDER *prov = sk_OSSL_PROVIDER_value(provs, j);
852 if (prov->flag_activated && !cb(prov, cbdata))
860 CRYPTO_THREAD_unlock(store->lock);
862 /* The pop_free call doesn't do what we want on an error condition */
863 for (j = 0; j < i; j++)
864 ossl_provider_free(sk_OSSL_PROVIDER_value(provs, j));
865 sk_OSSL_PROVIDER_free(provs);
869 int ossl_provider_available(OSSL_PROVIDER *prov)
872 provider_activate_fallbacks(prov->store);
874 return prov->flag_activated;
879 /* Setters of Provider Object data */
880 int ossl_provider_set_fallback(OSSL_PROVIDER *prov)
885 prov->flag_fallback = 1;
889 /* Getters of Provider Object data */
890 const char *ossl_provider_name(const OSSL_PROVIDER *prov)
895 const DSO *ossl_provider_dso(const OSSL_PROVIDER *prov)
900 const char *ossl_provider_module_name(const OSSL_PROVIDER *prov)
905 return DSO_get_filename(prov->module);
909 const char *ossl_provider_module_path(const OSSL_PROVIDER *prov)
914 /* FIXME: Ensure it's a full path */
915 return DSO_get_filename(prov->module);
919 void *ossl_provider_prov_ctx(const OSSL_PROVIDER *prov)
922 return prov->provctx;
927 OSSL_LIB_CTX *ossl_provider_libctx(const OSSL_PROVIDER *prov)
929 return prov != NULL ? prov->libctx : NULL;
932 /* Wrappers around calls to the provider */
933 void ossl_provider_teardown(const OSSL_PROVIDER *prov)
935 if (prov->teardown != NULL)
936 prov->teardown(prov->provctx);
939 const OSSL_PARAM *ossl_provider_gettable_params(const OSSL_PROVIDER *prov)
941 return prov->gettable_params == NULL
942 ? NULL : prov->gettable_params(prov->provctx);
945 int ossl_provider_get_params(const OSSL_PROVIDER *prov, OSSL_PARAM params[])
947 return prov->get_params == NULL
948 ? 0 : prov->get_params(prov->provctx, params);
951 int ossl_provider_self_test(const OSSL_PROVIDER *prov)
955 if (prov->self_test == NULL)
957 ret = prov->self_test(prov->provctx);
959 (void)evp_method_store_flush(ossl_provider_libctx(prov));
963 int ossl_provider_get_capabilities(const OSSL_PROVIDER *prov,
964 const char *capability,
968 return prov->get_capabilities == NULL
969 ? 1 : prov->get_capabilities(prov->provctx, capability, cb, arg);
972 const OSSL_ALGORITHM *ossl_provider_query_operation(const OSSL_PROVIDER *prov,
976 const OSSL_ALGORITHM *res;
978 if (prov->query_operation == NULL)
980 res = prov->query_operation(prov->provctx, operation_id, no_cache);
981 #if defined(OPENSSL_NO_CACHED_FETCH)
982 /* Forcing the non-caching of queries */
983 if (no_cache != NULL)
989 void ossl_provider_unquery_operation(const OSSL_PROVIDER *prov,
991 const OSSL_ALGORITHM *algs)
993 if (prov->unquery_operation != NULL)
994 prov->unquery_operation(prov->provctx, operation_id, algs);
997 int ossl_provider_set_operation_bit(OSSL_PROVIDER *provider, size_t bitnum)
999 size_t byte = bitnum / 8;
1000 unsigned char bit = (1 << (bitnum % 8)) & 0xFF;
1002 if (!CRYPTO_THREAD_write_lock(provider->opbits_lock))
1004 if (provider->operation_bits_sz <= byte) {
1005 unsigned char *tmp = OPENSSL_realloc(provider->operation_bits,
1009 CRYPTO_THREAD_unlock(provider->opbits_lock);
1010 ERR_raise(ERR_LIB_CRYPTO, ERR_R_MALLOC_FAILURE);
1013 provider->operation_bits = tmp;
1014 memset(provider->operation_bits + provider->operation_bits_sz,
1015 '\0', byte + 1 - provider->operation_bits_sz);
1016 provider->operation_bits_sz = byte + 1;
1018 provider->operation_bits[byte] |= bit;
1019 CRYPTO_THREAD_unlock(provider->opbits_lock);
1023 int ossl_provider_test_operation_bit(OSSL_PROVIDER *provider, size_t bitnum,
1026 size_t byte = bitnum / 8;
1027 unsigned char bit = (1 << (bitnum % 8)) & 0xFF;
1029 if (!ossl_assert(result != NULL)) {
1030 ERR_raise(ERR_LIB_CRYPTO, ERR_R_PASSED_NULL_PARAMETER);
1035 if (!CRYPTO_THREAD_read_lock(provider->opbits_lock))
1037 if (provider->operation_bits_sz > byte)
1038 *result = ((provider->operation_bits[byte] & bit) != 0);
1039 CRYPTO_THREAD_unlock(provider->opbits_lock);
1044 * Core functions for the provider
1045 * ===============================
1047 * This is the set of functions that the core makes available to the provider
1051 * This returns a list of Provider Object parameters with their types, for
1052 * discovery. We do not expect that many providers will use this, but one
1055 static const OSSL_PARAM param_types[] = {
1056 OSSL_PARAM_DEFN(OSSL_PROV_PARAM_CORE_VERSION, OSSL_PARAM_UTF8_PTR, NULL, 0),
1057 OSSL_PARAM_DEFN(OSSL_PROV_PARAM_CORE_PROV_NAME, OSSL_PARAM_UTF8_PTR,
1060 OSSL_PARAM_DEFN(OSSL_PROV_PARAM_CORE_MODULE_FILENAME, OSSL_PARAM_UTF8_PTR,
1067 * Forward declare all the functions that are provided aa dispatch.
1068 * This ensures that the compiler will complain if they aren't defined
1069 * with the correct signature.
1071 static OSSL_FUNC_core_gettable_params_fn core_gettable_params;
1072 static OSSL_FUNC_core_get_params_fn core_get_params;
1073 static OSSL_FUNC_core_thread_start_fn core_thread_start;
1074 static OSSL_FUNC_core_get_libctx_fn core_get_libctx;
1076 static OSSL_FUNC_core_new_error_fn core_new_error;
1077 static OSSL_FUNC_core_set_error_debug_fn core_set_error_debug;
1078 static OSSL_FUNC_core_vset_error_fn core_vset_error;
1079 static OSSL_FUNC_core_set_error_mark_fn core_set_error_mark;
1080 static OSSL_FUNC_core_clear_last_error_mark_fn core_clear_last_error_mark;
1081 static OSSL_FUNC_core_pop_error_to_mark_fn core_pop_error_to_mark;
1084 static const OSSL_PARAM *core_gettable_params(const OSSL_CORE_HANDLE *handle)
1089 static int core_get_params(const OSSL_CORE_HANDLE *handle, OSSL_PARAM params[])
1094 * We created this object originally and we know it is actually an
1095 * OSSL_PROVIDER *, so the cast is safe
1097 OSSL_PROVIDER *prov = (OSSL_PROVIDER *)handle;
1099 if ((p = OSSL_PARAM_locate(params, OSSL_PROV_PARAM_CORE_VERSION)) != NULL)
1100 OSSL_PARAM_set_utf8_ptr(p, OPENSSL_VERSION_STR);
1101 if ((p = OSSL_PARAM_locate(params, OSSL_PROV_PARAM_CORE_PROV_NAME)) != NULL)
1102 OSSL_PARAM_set_utf8_ptr(p, prov->name);
1105 if ((p = OSSL_PARAM_locate(params,
1106 OSSL_PROV_PARAM_CORE_MODULE_FILENAME)) != NULL)
1107 OSSL_PARAM_set_utf8_ptr(p, ossl_provider_module_path(prov));
1110 if (prov->parameters == NULL)
1113 for (i = 0; i < sk_INFOPAIR_num(prov->parameters); i++) {
1114 INFOPAIR *pair = sk_INFOPAIR_value(prov->parameters, i);
1116 if ((p = OSSL_PARAM_locate(params, pair->name)) != NULL)
1117 OSSL_PARAM_set_utf8_ptr(p, pair->value);
1122 static OPENSSL_CORE_CTX *core_get_libctx(const OSSL_CORE_HANDLE *handle)
1125 * We created this object originally and we know it is actually an
1126 * OSSL_PROVIDER *, so the cast is safe
1128 OSSL_PROVIDER *prov = (OSSL_PROVIDER *)handle;
1131 * Using ossl_provider_libctx would be wrong as that returns
1132 * NULL for |prov| == NULL and NULL libctx has a special meaning
1133 * that does not apply here. Here |prov| == NULL can happen only in
1134 * case of a coding error.
1136 assert(prov != NULL);
1137 return (OPENSSL_CORE_CTX *)prov->libctx;
1140 static int core_thread_start(const OSSL_CORE_HANDLE *handle,
1141 OSSL_thread_stop_handler_fn handfn)
1144 * We created this object originally and we know it is actually an
1145 * OSSL_PROVIDER *, so the cast is safe
1147 OSSL_PROVIDER *prov = (OSSL_PROVIDER *)handle;
1149 return ossl_init_thread_start(prov, prov->provctx, handfn);
1153 * The FIPS module inner provider doesn't implement these. They aren't
1154 * needed there, since the FIPS module upcalls are always the outer provider
1159 * These error functions should use |handle| to select the proper
1160 * library context to report in the correct error stack if error
1161 * stacks become tied to the library context.
1162 * We cannot currently do that since there's no support for it in the
1165 static void core_new_error(const OSSL_CORE_HANDLE *handle)
1170 static void core_set_error_debug(const OSSL_CORE_HANDLE *handle,
1171 const char *file, int line, const char *func)
1173 ERR_set_debug(file, line, func);
1176 static void core_vset_error(const OSSL_CORE_HANDLE *handle,
1177 uint32_t reason, const char *fmt, va_list args)
1180 * We created this object originally and we know it is actually an
1181 * OSSL_PROVIDER *, so the cast is safe
1183 OSSL_PROVIDER *prov = (OSSL_PROVIDER *)handle;
1186 * If the uppermost 8 bits are non-zero, it's an OpenSSL library
1187 * error and will be treated as such. Otherwise, it's a new style
1188 * provider error and will be treated as such.
1190 if (ERR_GET_LIB(reason) != 0) {
1191 ERR_vset_error(ERR_GET_LIB(reason), ERR_GET_REASON(reason), fmt, args);
1193 ERR_vset_error(prov->error_lib, (int)reason, fmt, args);
1197 static int core_set_error_mark(const OSSL_CORE_HANDLE *handle)
1199 return ERR_set_mark();
1202 static int core_clear_last_error_mark(const OSSL_CORE_HANDLE *handle)
1204 return ERR_clear_last_mark();
1207 static int core_pop_error_to_mark(const OSSL_CORE_HANDLE *handle)
1209 return ERR_pop_to_mark();
1211 #endif /* FIPS_MODULE */
1214 * Functions provided by the core.
1216 static const OSSL_DISPATCH core_dispatch_[] = {
1217 { OSSL_FUNC_CORE_GETTABLE_PARAMS, (void (*)(void))core_gettable_params },
1218 { OSSL_FUNC_CORE_GET_PARAMS, (void (*)(void))core_get_params },
1219 { OSSL_FUNC_CORE_GET_LIBCTX, (void (*)(void))core_get_libctx },
1220 { OSSL_FUNC_CORE_THREAD_START, (void (*)(void))core_thread_start },
1222 { OSSL_FUNC_CORE_NEW_ERROR, (void (*)(void))core_new_error },
1223 { OSSL_FUNC_CORE_SET_ERROR_DEBUG, (void (*)(void))core_set_error_debug },
1224 { OSSL_FUNC_CORE_VSET_ERROR, (void (*)(void))core_vset_error },
1225 { OSSL_FUNC_CORE_SET_ERROR_MARK, (void (*)(void))core_set_error_mark },
1226 { OSSL_FUNC_CORE_CLEAR_LAST_ERROR_MARK,
1227 (void (*)(void))core_clear_last_error_mark },
1228 { OSSL_FUNC_CORE_POP_ERROR_TO_MARK, (void (*)(void))core_pop_error_to_mark },
1229 { OSSL_FUNC_BIO_NEW_FILE, (void (*)(void))ossl_core_bio_new_file },
1230 { OSSL_FUNC_BIO_NEW_MEMBUF, (void (*)(void))ossl_core_bio_new_mem_buf },
1231 { OSSL_FUNC_BIO_READ_EX, (void (*)(void))ossl_core_bio_read_ex },
1232 { OSSL_FUNC_BIO_WRITE_EX, (void (*)(void))ossl_core_bio_write_ex },
1233 { OSSL_FUNC_BIO_GETS, (void (*)(void))ossl_core_bio_gets },
1234 { OSSL_FUNC_BIO_PUTS, (void (*)(void))ossl_core_bio_puts },
1235 { OSSL_FUNC_BIO_CTRL, (void (*)(void))ossl_core_bio_ctrl },
1236 { OSSL_FUNC_BIO_UP_REF, (void (*)(void))ossl_core_bio_up_ref },
1237 { OSSL_FUNC_BIO_FREE, (void (*)(void))ossl_core_bio_free },
1238 { OSSL_FUNC_BIO_VPRINTF, (void (*)(void))ossl_core_bio_vprintf },
1239 { OSSL_FUNC_BIO_VSNPRINTF, (void (*)(void))BIO_vsnprintf },
1240 { OSSL_FUNC_SELF_TEST_CB, (void (*)(void))OSSL_SELF_TEST_get_callback },
1241 { OSSL_FUNC_GET_ENTROPY, (void (*)(void))ossl_rand_get_entropy },
1242 { OSSL_FUNC_CLEANUP_ENTROPY, (void (*)(void))ossl_rand_cleanup_entropy },
1243 { OSSL_FUNC_GET_NONCE, (void (*)(void))ossl_rand_get_nonce },
1244 { OSSL_FUNC_CLEANUP_NONCE, (void (*)(void))ossl_rand_cleanup_nonce },
1246 { OSSL_FUNC_CRYPTO_MALLOC, (void (*)(void))CRYPTO_malloc },
1247 { OSSL_FUNC_CRYPTO_ZALLOC, (void (*)(void))CRYPTO_zalloc },
1248 { OSSL_FUNC_CRYPTO_FREE, (void (*)(void))CRYPTO_free },
1249 { OSSL_FUNC_CRYPTO_CLEAR_FREE, (void (*)(void))CRYPTO_clear_free },
1250 { OSSL_FUNC_CRYPTO_REALLOC, (void (*)(void))CRYPTO_realloc },
1251 { OSSL_FUNC_CRYPTO_CLEAR_REALLOC, (void (*)(void))CRYPTO_clear_realloc },
1252 { OSSL_FUNC_CRYPTO_SECURE_MALLOC, (void (*)(void))CRYPTO_secure_malloc },
1253 { OSSL_FUNC_CRYPTO_SECURE_ZALLOC, (void (*)(void))CRYPTO_secure_zalloc },
1254 { OSSL_FUNC_CRYPTO_SECURE_FREE, (void (*)(void))CRYPTO_secure_free },
1255 { OSSL_FUNC_CRYPTO_SECURE_CLEAR_FREE,
1256 (void (*)(void))CRYPTO_secure_clear_free },
1257 { OSSL_FUNC_CRYPTO_SECURE_ALLOCATED,
1258 (void (*)(void))CRYPTO_secure_allocated },
1259 { OSSL_FUNC_OPENSSL_CLEANSE, (void (*)(void))OPENSSL_cleanse },
1263 static const OSSL_DISPATCH *core_dispatch = core_dispatch_;