2 * Copyright 2019-2020 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
10 #include <openssl/core.h>
11 #include <openssl/core_numbers.h>
12 #include <openssl/core_names.h>
13 #include <openssl/provider.h>
14 #include <openssl/params.h>
15 #include <openssl/opensslv.h>
16 #include "crypto/cryptlib.h"
17 #include "internal/nelem.h"
18 #include "internal/thread_once.h"
19 #include "internal/provider.h"
20 #include "internal/refcount.h"
21 #include "provider_local.h"
23 # include <openssl/self_test.h>
26 static OSSL_PROVIDER *provider_new(const char *name,
27 OSSL_provider_init_fn *init_function);
30 * Provider Object structure
31 * =========================
38 DEFINE_STACK_OF(INFOPAIR)
40 struct provider_store_st; /* Forward declaration */
42 struct ossl_provider_st {
44 unsigned int flag_initialized:1;
45 unsigned int flag_fallback:1;
47 /* OpenSSL library side data */
48 CRYPTO_REF_COUNT refcnt;
49 CRYPTO_RWLOCK *refcnt_lock; /* For the ref counter */
53 OSSL_provider_init_fn *init_function;
54 STACK_OF(INFOPAIR) *parameters;
55 OPENSSL_CTX *libctx; /* The library context this instance is in */
56 struct provider_store_st *store; /* The store this instance belongs to */
59 * In the FIPS module inner provider, this isn't needed, since the
60 * error upcalls are always direct calls to the outer provider.
62 int error_lib; /* ERR library number, one for each provider */
63 # ifndef OPENSSL_NO_ERR
64 ERR_STRING_DATA *error_strings; /* Copy of what the provider gives us */
68 /* Provider side functions */
69 OSSL_provider_teardown_fn *teardown;
70 OSSL_provider_gettable_params_fn *gettable_params;
71 OSSL_provider_get_params_fn *get_params;
72 OSSL_provider_query_operation_fn *query_operation;
75 * Cache of bit to indicate of query_operation() has been called on
76 * a specific operation or not.
78 unsigned char *operation_bits;
79 size_t operation_bits_sz;
81 /* Provider side data */
84 DEFINE_STACK_OF(OSSL_PROVIDER)
86 static int ossl_provider_cmp(const OSSL_PROVIDER * const *a,
87 const OSSL_PROVIDER * const *b)
89 return strcmp((*a)->name, (*b)->name);
93 * Provider Object store
94 * =====================
96 * The Provider Object store is a library context object, and therefore needs
100 struct provider_store_st {
101 STACK_OF(OSSL_PROVIDER) *providers;
104 unsigned int use_fallbacks:1;
107 static void provider_store_free(void *vstore)
109 struct provider_store_st *store = vstore;
113 OPENSSL_free(store->default_path);
114 sk_OSSL_PROVIDER_pop_free(store->providers, ossl_provider_free);
115 CRYPTO_THREAD_lock_free(store->lock);
119 static void *provider_store_new(OPENSSL_CTX *ctx)
121 struct provider_store_st *store = OPENSSL_zalloc(sizeof(*store));
122 const struct predefined_providers_st *p = NULL;
125 || (store->providers = sk_OSSL_PROVIDER_new(ossl_provider_cmp)) == NULL
126 || (store->lock = CRYPTO_THREAD_lock_new()) == NULL) {
127 provider_store_free(store);
130 store->use_fallbacks = 1;
132 for (p = predefined_providers; p->name != NULL; p++) {
133 OSSL_PROVIDER *prov = NULL;
136 * We use the internal constructor directly here,
137 * otherwise we get a call loop
139 prov = provider_new(p->name, p->init);
142 || sk_OSSL_PROVIDER_push(store->providers, prov) == 0) {
143 ossl_provider_free(prov);
144 provider_store_free(store);
145 CRYPTOerr(CRYPTO_F_PROVIDER_STORE_NEW, ERR_R_INTERNAL_ERROR);
151 prov->error_lib = ERR_get_next_error_library();
154 ossl_provider_set_fallback(prov);
160 static const OPENSSL_CTX_METHOD provider_store_method = {
165 static struct provider_store_st *get_provider_store(OPENSSL_CTX *libctx)
167 struct provider_store_st *store = NULL;
169 store = openssl_ctx_get_data(libctx, OPENSSL_CTX_PROVIDER_STORE_INDEX,
170 &provider_store_method);
172 CRYPTOerr(CRYPTO_F_GET_PROVIDER_STORE, ERR_R_INTERNAL_ERROR);
176 OSSL_PROVIDER *ossl_provider_find(OPENSSL_CTX *libctx, const char *name,
179 struct provider_store_st *store = NULL;
180 OSSL_PROVIDER *prov = NULL;
182 if ((store = get_provider_store(libctx)) != NULL) {
183 OSSL_PROVIDER tmpl = { 0, };
188 * Make sure any providers are loaded from config before we try to find
192 OPENSSL_init_crypto(OPENSSL_INIT_LOAD_CONFIG, NULL);
195 tmpl.name = (char *)name;
196 CRYPTO_THREAD_write_lock(store->lock);
197 if ((i = sk_OSSL_PROVIDER_find(store->providers, &tmpl)) == -1
198 || (prov = sk_OSSL_PROVIDER_value(store->providers, i)) == NULL
199 || !ossl_provider_up_ref(prov))
201 CRYPTO_THREAD_unlock(store->lock);
208 * Provider Object methods
209 * =======================
212 static OSSL_PROVIDER *provider_new(const char *name,
213 OSSL_provider_init_fn *init_function)
215 OSSL_PROVIDER *prov = NULL;
217 if ((prov = OPENSSL_zalloc(sizeof(*prov))) == NULL
219 || (prov->refcnt_lock = CRYPTO_THREAD_lock_new()) == NULL
221 || !ossl_provider_up_ref(prov) /* +1 One reference to be returned */
222 || (prov->name = OPENSSL_strdup(name)) == NULL) {
223 ossl_provider_free(prov);
224 CRYPTOerr(CRYPTO_F_PROVIDER_NEW, ERR_R_MALLOC_FAILURE);
228 prov->init_function = init_function;
232 int ossl_provider_up_ref(OSSL_PROVIDER *prov)
236 if (CRYPTO_UP_REF(&prov->refcnt, &ref, prov->refcnt_lock) <= 0)
241 OSSL_PROVIDER *ossl_provider_new(OPENSSL_CTX *libctx, const char *name,
242 OSSL_provider_init_fn *init_function,
245 struct provider_store_st *store = NULL;
246 OSSL_PROVIDER *prov = NULL;
248 if ((store = get_provider_store(libctx)) == NULL)
251 if ((prov = ossl_provider_find(libctx, name,
252 noconfig)) != NULL) { /* refcount +1 */
253 ossl_provider_free(prov); /* refcount -1 */
254 ERR_raise_data(ERR_LIB_CRYPTO, CRYPTO_R_PROVIDER_ALREADY_EXISTS, NULL,
259 /* provider_new() generates an error, so no need here */
260 if ((prov = provider_new(name, init_function)) == NULL)
263 CRYPTO_THREAD_write_lock(store->lock);
264 if (!ossl_provider_up_ref(prov)) { /* +1 One reference for the store */
265 ossl_provider_free(prov); /* -1 Reference that was to be returned */
267 } else if (sk_OSSL_PROVIDER_push(store->providers, prov) == 0) {
268 ossl_provider_free(prov); /* -1 Store reference */
269 ossl_provider_free(prov); /* -1 Reference that was to be returned */
272 prov->libctx = libctx;
275 prov->error_lib = ERR_get_next_error_library();
278 CRYPTO_THREAD_unlock(store->lock);
281 CRYPTOerr(CRYPTO_F_OSSL_PROVIDER_NEW, ERR_R_MALLOC_FAILURE);
284 * At this point, the provider is only partially "loaded". To be
285 * fully "loaded", ossl_provider_activate() must also be called.
291 static void free_infopair(INFOPAIR *pair)
293 OPENSSL_free(pair->name);
294 OPENSSL_free(pair->value);
298 void ossl_provider_free(OSSL_PROVIDER *prov)
303 CRYPTO_DOWN_REF(&prov->refcnt, &ref, prov->refcnt_lock);
306 * When the refcount drops below two, the store is the only
307 * possible reference, or it has already been taken away from
308 * the store (this may happen if a provider was activated
309 * because it's a fallback, but isn't currently used)
310 * When that happens, the provider is inactivated.
312 if (ref < 2 && prov->flag_initialized) {
314 ossl_init_thread_deregister(prov);
316 if (prov->teardown != NULL)
317 prov->teardown(prov->provctx);
318 #ifndef OPENSSL_NO_ERR
320 if (prov->error_strings != NULL) {
321 ERR_unload_strings(prov->error_lib, prov->error_strings);
322 OPENSSL_free(prov->error_strings);
323 prov->error_strings = NULL;
327 OPENSSL_free(prov->operation_bits);
328 prov->operation_bits = NULL;
329 prov->operation_bits_sz = 0;
330 prov->flag_initialized = 0;
334 * When the refcount drops to zero, it has been taken out of
335 * the store. All we have to do here is clean it out.
339 DSO_free(prov->module);
341 OPENSSL_free(prov->name);
342 OPENSSL_free(prov->path);
343 sk_INFOPAIR_pop_free(prov->parameters, free_infopair);
345 CRYPTO_THREAD_lock_free(prov->refcnt_lock);
353 int ossl_provider_set_module_path(OSSL_PROVIDER *prov, const char *module_path)
355 OPENSSL_free(prov->path);
356 if (module_path == NULL)
358 if ((prov->path = OPENSSL_strdup(module_path)) != NULL)
360 CRYPTOerr(CRYPTO_F_OSSL_PROVIDER_SET_MODULE_PATH, ERR_R_MALLOC_FAILURE);
364 int ossl_provider_add_parameter(OSSL_PROVIDER *prov,
365 const char *name, const char *value)
367 INFOPAIR *pair = NULL;
369 if ((pair = OPENSSL_zalloc(sizeof(*pair))) != NULL
370 && (prov->parameters != NULL
371 || (prov->parameters = sk_INFOPAIR_new_null()) != NULL)
372 && (pair->name = OPENSSL_strdup(name)) != NULL
373 && (pair->value = OPENSSL_strdup(value)) != NULL
374 && sk_INFOPAIR_push(prov->parameters, pair) > 0)
378 OPENSSL_free(pair->name);
379 OPENSSL_free(pair->value);
382 CRYPTOerr(CRYPTO_F_OSSL_PROVIDER_ADD_PARAMETER, ERR_R_MALLOC_FAILURE);
387 * Provider activation.
389 * What "activation" means depends on the provider form; for built in
390 * providers (in the library or the application alike), the provider
391 * can already be considered to be loaded, all that's needed is to
392 * initialize it. However, for dynamically loadable provider modules,
393 * we must first load that module.
395 * Built in modules are distinguished from dynamically loaded modules
396 * with an already assigned init function.
398 static const OSSL_DISPATCH *core_dispatch; /* Define further down */
400 int OSSL_PROVIDER_set_default_search_path(OPENSSL_CTX *libctx, const char *path)
402 struct provider_store_st *store;
406 p = OPENSSL_strdup(path);
408 CRYPTOerr(0, ERR_R_MALLOC_FAILURE);
412 if ((store = get_provider_store(libctx)) != NULL
413 && CRYPTO_THREAD_write_lock(store->lock)) {
414 OPENSSL_free(store->default_path);
415 store->default_path = p;
416 CRYPTO_THREAD_unlock(store->lock);
424 * Internal version that doesn't affect the store flags, and thereby avoid
425 * locking. Direct callers must remember to set the store flags when
428 static int provider_activate(OSSL_PROVIDER *prov)
430 const OSSL_DISPATCH *provider_dispatch = NULL;
431 void *tmp_provctx = NULL; /* safety measure */
432 #ifndef OPENSSL_NO_ERR
434 OSSL_provider_get_reason_strings_fn *p_get_reason_strings = NULL;
438 if (prov->flag_initialized)
442 * If the init function isn't set, it indicates that this provider is
445 if (prov->init_function == NULL) {
449 if (prov->module == NULL) {
450 char *allocated_path = NULL;
451 const char *module_path = NULL;
452 char *merged_path = NULL;
453 const char *load_dir = NULL;
454 struct provider_store_st *store;
456 if ((prov->module = DSO_new()) == NULL) {
457 /* DSO_new() generates an error already */
461 if ((store = get_provider_store(prov->libctx)) == NULL
462 || !CRYPTO_THREAD_read_lock(store->lock))
464 load_dir = store->default_path;
466 if (load_dir == NULL) {
467 load_dir = ossl_safe_getenv("OPENSSL_MODULES");
468 if (load_dir == NULL)
469 load_dir = MODULESDIR;
472 DSO_ctrl(prov->module, DSO_CTRL_SET_FLAGS,
473 DSO_FLAG_NAME_TRANSLATION_EXT_ONLY, NULL);
475 module_path = prov->path;
476 if (module_path == NULL)
477 module_path = allocated_path =
478 DSO_convert_filename(prov->module, prov->name);
479 if (module_path != NULL)
480 merged_path = DSO_merge(prov->module, module_path, load_dir);
481 CRYPTO_THREAD_unlock(store->lock);
483 if (merged_path == NULL
484 || (DSO_load(prov->module, merged_path, NULL, 0)) == NULL) {
485 DSO_free(prov->module);
489 OPENSSL_free(merged_path);
490 OPENSSL_free(allocated_path);
493 if (prov->module != NULL)
494 prov->init_function = (OSSL_provider_init_fn *)
495 DSO_bind_func(prov->module, "OSSL_provider_init");
499 /* Call the initialise function for the provider. */
500 if (prov->init_function == NULL
501 || !prov->init_function((OSSL_CORE_HANDLE *)prov, core_dispatch,
502 &provider_dispatch, &tmp_provctx)) {
503 ERR_raise_data(ERR_LIB_CRYPTO, ERR_R_INIT_FAIL, NULL,
504 "name=%s", prov->name);
506 DSO_free(prov->module);
511 prov->provctx = tmp_provctx;
513 for (; provider_dispatch->function_id != 0; provider_dispatch++) {
514 switch (provider_dispatch->function_id) {
515 case OSSL_FUNC_PROVIDER_TEARDOWN:
517 OSSL_get_provider_teardown(provider_dispatch);
519 case OSSL_FUNC_PROVIDER_GETTABLE_PARAMS:
520 prov->gettable_params =
521 OSSL_get_provider_gettable_params(provider_dispatch);
523 case OSSL_FUNC_PROVIDER_GET_PARAMS:
525 OSSL_get_provider_get_params(provider_dispatch);
527 case OSSL_FUNC_PROVIDER_QUERY_OPERATION:
528 prov->query_operation =
529 OSSL_get_provider_query_operation(provider_dispatch);
531 #ifndef OPENSSL_NO_ERR
533 case OSSL_FUNC_PROVIDER_GET_REASON_STRINGS:
534 p_get_reason_strings =
535 OSSL_get_provider_get_reason_strings(provider_dispatch);
542 #ifndef OPENSSL_NO_ERR
544 if (p_get_reason_strings != NULL) {
545 const OSSL_ITEM *reasonstrings = p_get_reason_strings(prov->provctx);
549 * ERR_load_strings() handles ERR_STRING_DATA rather than OSSL_ITEM,
550 * although they are essentially the same type.
551 * Furthermore, ERR_load_strings() patches the array's error number
552 * with the error library number, so we need to make a copy of that
556 while (reasonstrings[cnt].id != 0) {
557 if (ERR_GET_LIB(reasonstrings[cnt].id) != 0)
561 cnt++; /* One for the terminating item */
563 /* Allocate one extra item for the "library" name */
564 prov->error_strings =
565 OPENSSL_zalloc(sizeof(ERR_STRING_DATA) * (cnt + 1));
566 if (prov->error_strings == NULL)
570 * Set the "library" name.
572 prov->error_strings[0].error = ERR_PACK(prov->error_lib, 0, 0);
573 prov->error_strings[0].string = prov->name;
575 * Copy reasonstrings item 0..cnt-1 to prov->error_trings positions
578 for (cnt2 = 1; cnt2 <= cnt; cnt2++) {
579 prov->error_strings[cnt2].error = (int)reasonstrings[cnt2-1].id;
580 prov->error_strings[cnt2].string = reasonstrings[cnt2-1].ptr;
583 ERR_load_strings(prov->error_lib, prov->error_strings);
588 /* With this flag set, this provider has become fully "loaded". */
589 prov->flag_initialized = 1;
594 int ossl_provider_activate(OSSL_PROVIDER *prov)
596 if (provider_activate(prov)) {
597 CRYPTO_THREAD_write_lock(prov->store->lock);
598 prov->store->use_fallbacks = 0;
599 CRYPTO_THREAD_unlock(prov->store->lock);
606 void *ossl_provider_ctx(const OSSL_PROVIDER *prov)
608 return prov->provctx;
612 static int provider_forall_loaded(struct provider_store_st *store,
613 int *found_activated,
614 int (*cb)(OSSL_PROVIDER *provider,
622 num_provs = sk_OSSL_PROVIDER_num(store->providers);
624 if (found_activated != NULL)
625 *found_activated = 0;
626 for (i = 0; i < num_provs; i++) {
627 OSSL_PROVIDER *prov =
628 sk_OSSL_PROVIDER_value(store->providers, i);
630 if (prov->flag_initialized) {
631 if (found_activated != NULL)
632 *found_activated = 1;
633 if (!(ret = cb(prov, cbdata)))
642 * This function only does something once when store->use_fallbacks == 1,
643 * and then sets store->use_fallbacks = 0, so the second call and so on is
644 * effectively a no-op.
646 static void provider_activate_fallbacks(struct provider_store_st *store)
648 if (store->use_fallbacks) {
649 int num_provs = sk_OSSL_PROVIDER_num(store->providers);
650 int activated_fallback_count = 0;
653 for (i = 0; i < num_provs; i++) {
654 OSSL_PROVIDER *prov = sk_OSSL_PROVIDER_value(store->providers, i);
657 * Note that we don't care if the activation succeeds or not.
658 * If it doesn't succeed, then any attempt to use any of the
659 * fallback providers will fail anyway.
661 if (prov->flag_fallback) {
662 activated_fallback_count++;
663 provider_activate(prov);
668 * We assume that all fallbacks have been added to the store before
669 * any fallback is activated.
670 * TODO: We may have to reconsider this, IF we find ourselves adding
671 * fallbacks after any previous fallback has been activated.
673 if (activated_fallback_count > 0)
674 store->use_fallbacks = 0;
678 int ossl_provider_forall_loaded(OPENSSL_CTX *ctx,
679 int (*cb)(OSSL_PROVIDER *provider,
684 struct provider_store_st *store = get_provider_store(ctx);
688 * Make sure any providers are loaded from config before we try to use
691 OPENSSL_init_crypto(OPENSSL_INIT_LOAD_CONFIG, NULL);
695 CRYPTO_THREAD_read_lock(store->lock);
697 provider_activate_fallbacks(store);
700 * Now, we sweep through all providers
702 ret = provider_forall_loaded(store, NULL, cb, cbdata);
704 CRYPTO_THREAD_unlock(store->lock);
710 int ossl_provider_available(OSSL_PROVIDER *prov)
713 CRYPTO_THREAD_read_lock(prov->store->lock);
714 provider_activate_fallbacks(prov->store);
715 CRYPTO_THREAD_unlock(prov->store->lock);
717 return prov->flag_initialized;
722 /* Setters of Provider Object data */
723 int ossl_provider_set_fallback(OSSL_PROVIDER *prov)
728 prov->flag_fallback = 1;
732 /* Getters of Provider Object data */
733 const char *ossl_provider_name(const OSSL_PROVIDER *prov)
738 const DSO *ossl_provider_dso(const OSSL_PROVIDER *prov)
743 const char *ossl_provider_module_name(const OSSL_PROVIDER *prov)
748 return DSO_get_filename(prov->module);
752 const char *ossl_provider_module_path(const OSSL_PROVIDER *prov)
757 /* FIXME: Ensure it's a full path */
758 return DSO_get_filename(prov->module);
762 OPENSSL_CTX *ossl_provider_library_context(const OSSL_PROVIDER *prov)
764 /* TODO(3.0) just: return prov->libctx; */
765 return prov != NULL ? prov->libctx : NULL;
768 /* Wrappers around calls to the provider */
769 void ossl_provider_teardown(const OSSL_PROVIDER *prov)
771 if (prov->teardown != NULL)
772 prov->teardown(prov->provctx);
775 const OSSL_PARAM *ossl_provider_gettable_params(const OSSL_PROVIDER *prov)
777 return prov->gettable_params == NULL
778 ? NULL : prov->gettable_params(prov->provctx);
781 int ossl_provider_get_params(const OSSL_PROVIDER *prov, OSSL_PARAM params[])
783 return prov->get_params == NULL
784 ? 0 : prov->get_params(prov->provctx, params);
788 const OSSL_ALGORITHM *ossl_provider_query_operation(const OSSL_PROVIDER *prov,
792 return prov->query_operation(prov->provctx, operation_id, no_cache);
795 int ossl_provider_set_operation_bit(OSSL_PROVIDER *provider, size_t bitnum)
797 size_t byte = bitnum / 8;
798 unsigned char bit = (1 << (bitnum % 8)) & 0xFF;
800 if (provider->operation_bits_sz <= byte) {
801 provider->operation_bits = OPENSSL_realloc(provider->operation_bits,
803 if (provider->operation_bits == NULL) {
804 ERR_raise(ERR_LIB_CRYPTO, ERR_R_MALLOC_FAILURE);
807 memset(provider->operation_bits + provider->operation_bits_sz,
808 '\0', byte + 1 - provider->operation_bits_sz);
810 provider->operation_bits[byte] |= bit;
814 int ossl_provider_test_operation_bit(OSSL_PROVIDER *provider, size_t bitnum,
817 size_t byte = bitnum / 8;
818 unsigned char bit = (1 << (bitnum % 8)) & 0xFF;
820 if (!ossl_assert(result != NULL)) {
821 ERR_raise(ERR_LIB_CRYPTO, ERR_R_PASSED_NULL_PARAMETER);
826 if (provider->operation_bits_sz > byte)
827 *result = ((provider->operation_bits[byte] & bit) != 0);
832 * Core functions for the provider
833 * ===============================
835 * This is the set of functions that the core makes available to the provider
839 * This returns a list of Provider Object parameters with their types, for
840 * discovery. We do not expect that many providers will use this, but one
843 static const OSSL_PARAM param_types[] = {
844 OSSL_PARAM_DEFN(OSSL_PROV_PARAM_CORE_VERSION, OSSL_PARAM_UTF8_PTR, NULL, 0),
845 OSSL_PARAM_DEFN(OSSL_PROV_PARAM_CORE_PROV_NAME, OSSL_PARAM_UTF8_PTR,
848 OSSL_PARAM_DEFN(OSSL_PROV_PARAM_CORE_MODULE_FILENAME, OSSL_PARAM_UTF8_PTR,
855 * Forward declare all the functions that are provided aa dispatch.
856 * This ensures that the compiler will complain if they aren't defined
857 * with the correct signature.
859 static OSSL_core_gettable_params_fn core_gettable_params;
860 static OSSL_core_get_params_fn core_get_params;
861 static OSSL_core_thread_start_fn core_thread_start;
862 static OSSL_core_get_library_context_fn core_get_libctx;
864 static OSSL_core_new_error_fn core_new_error;
865 static OSSL_core_set_error_debug_fn core_set_error_debug;
866 static OSSL_core_vset_error_fn core_vset_error;
867 static OSSL_core_set_error_mark_fn core_set_error_mark;
868 static OSSL_core_clear_last_error_mark_fn core_clear_last_error_mark;
869 static OSSL_core_pop_error_to_mark_fn core_pop_error_to_mark;
872 static const OSSL_PARAM *core_gettable_params(const OSSL_CORE_HANDLE *handle)
877 static int core_get_params(const OSSL_CORE_HANDLE *handle, OSSL_PARAM params[])
882 * We created this object originally and we know it is actually an
883 * OSSL_PROVIDER *, so the cast is safe
885 OSSL_PROVIDER *prov = (OSSL_PROVIDER *)handle;
887 if ((p = OSSL_PARAM_locate(params, OSSL_PROV_PARAM_CORE_VERSION)) != NULL)
888 OSSL_PARAM_set_utf8_ptr(p, OPENSSL_VERSION_STR);
889 if ((p = OSSL_PARAM_locate(params, OSSL_PROV_PARAM_CORE_PROV_NAME)) != NULL)
890 OSSL_PARAM_set_utf8_ptr(p, prov->name);
893 if ((p = OSSL_PARAM_locate(params,
894 OSSL_PROV_PARAM_CORE_MODULE_FILENAME)) != NULL)
895 OSSL_PARAM_set_utf8_ptr(p, ossl_provider_module_path(prov));
898 if (prov->parameters == NULL)
901 for (i = 0; i < sk_INFOPAIR_num(prov->parameters); i++) {
902 INFOPAIR *pair = sk_INFOPAIR_value(prov->parameters, i);
904 if ((p = OSSL_PARAM_locate(params, pair->name)) != NULL)
905 OSSL_PARAM_set_utf8_ptr(p, pair->value);
910 static OPENSSL_CORE_CTX *core_get_libctx(const OSSL_CORE_HANDLE *handle)
913 * We created this object originally and we know it is actually an
914 * OSSL_PROVIDER *, so the cast is safe
916 OSSL_PROVIDER *prov = (OSSL_PROVIDER *)handle;
918 return (OPENSSL_CORE_CTX *)ossl_provider_library_context(prov);
921 static int core_thread_start(const OSSL_CORE_HANDLE *handle,
922 OSSL_thread_stop_handler_fn handfn)
925 * We created this object originally and we know it is actually an
926 * OSSL_PROVIDER *, so the cast is safe
928 OSSL_PROVIDER *prov = (OSSL_PROVIDER *)handle;
930 return ossl_init_thread_start(prov, prov->provctx, handfn);
934 * The FIPS module inner provider doesn't implement these. They aren't
935 * needed there, since the FIPS module upcalls are always the outer provider
940 * TODO(3.0) These error functions should use |handle| to select the proper
941 * library context to report in the correct error stack, at least if error
942 * stacks become tied to the library context.
943 * We cannot currently do that since there's no support for it in the
946 static void core_new_error(const OSSL_CORE_HANDLE *handle)
951 static void core_set_error_debug(const OSSL_CORE_HANDLE *handle,
952 const char *file, int line, const char *func)
954 ERR_set_debug(file, line, func);
957 static void core_vset_error(const OSSL_CORE_HANDLE *handle,
958 uint32_t reason, const char *fmt, va_list args)
961 * We created this object originally and we know it is actually an
962 * OSSL_PROVIDER *, so the cast is safe
964 OSSL_PROVIDER *prov = (OSSL_PROVIDER *)handle;
967 * If the uppermost 8 bits are non-zero, it's an OpenSSL library
968 * error and will be treated as such. Otherwise, it's a new style
969 * provider error and will be treated as such.
971 if (ERR_GET_LIB(reason) != 0) {
972 ERR_vset_error(ERR_GET_LIB(reason), ERR_GET_REASON(reason), fmt, args);
974 ERR_vset_error(prov->error_lib, (int)reason, fmt, args);
978 static int core_set_error_mark(const OSSL_CORE_HANDLE *handle)
980 return ERR_set_mark();
983 static int core_clear_last_error_mark(const OSSL_CORE_HANDLE *handle)
985 return ERR_clear_last_mark();
988 static int core_pop_error_to_mark(const OSSL_CORE_HANDLE *handle)
990 return ERR_pop_to_mark();
992 #endif /* FIPS_MODULE */
995 * Functions provided by the core. Blank line separates "families" of related
998 static const OSSL_DISPATCH core_dispatch_[] = {
999 { OSSL_FUNC_CORE_GETTABLE_PARAMS, (void (*)(void))core_gettable_params },
1000 { OSSL_FUNC_CORE_GET_PARAMS, (void (*)(void))core_get_params },
1001 { OSSL_FUNC_CORE_GET_LIBRARY_CONTEXT, (void (*)(void))core_get_libctx },
1002 { OSSL_FUNC_CORE_THREAD_START, (void (*)(void))core_thread_start },
1004 { OSSL_FUNC_CORE_NEW_ERROR, (void (*)(void))core_new_error },
1005 { OSSL_FUNC_CORE_SET_ERROR_DEBUG, (void (*)(void))core_set_error_debug },
1006 { OSSL_FUNC_CORE_VSET_ERROR, (void (*)(void))core_vset_error },
1007 { OSSL_FUNC_CORE_SET_ERROR_MARK, (void (*)(void))core_set_error_mark },
1008 { OSSL_FUNC_CORE_CLEAR_LAST_ERROR_MARK,
1009 (void (*)(void))core_clear_last_error_mark },
1010 { OSSL_FUNC_CORE_POP_ERROR_TO_MARK, (void (*)(void))core_pop_error_to_mark },
1011 { OSSL_FUNC_BIO_NEW_FILE, (void (*)(void))BIO_new_file },
1012 { OSSL_FUNC_BIO_NEW_MEMBUF, (void (*)(void))BIO_new_mem_buf },
1013 { OSSL_FUNC_BIO_READ_EX, (void (*)(void))BIO_read_ex },
1014 { OSSL_FUNC_BIO_WRITE_EX, (void (*)(void))BIO_write_ex },
1015 { OSSL_FUNC_BIO_FREE, (void (*)(void))BIO_free },
1016 { OSSL_FUNC_BIO_VPRINTF, (void (*)(void))BIO_vprintf },
1017 { OSSL_FUNC_BIO_VSNPRINTF, (void (*)(void))BIO_vsnprintf },
1018 { OSSL_FUNC_SELF_TEST_CB, (void (*)(void))OSSL_SELF_TEST_get_callback },
1020 { OSSL_FUNC_CRYPTO_MALLOC, (void (*)(void))CRYPTO_malloc },
1021 { OSSL_FUNC_CRYPTO_ZALLOC, (void (*)(void))CRYPTO_zalloc },
1022 { OSSL_FUNC_CRYPTO_FREE, (void (*)(void))CRYPTO_free },
1023 { OSSL_FUNC_CRYPTO_CLEAR_FREE, (void (*)(void))CRYPTO_clear_free },
1024 { OSSL_FUNC_CRYPTO_REALLOC, (void (*)(void))CRYPTO_realloc },
1025 { OSSL_FUNC_CRYPTO_CLEAR_REALLOC, (void (*)(void))CRYPTO_clear_realloc },
1026 { OSSL_FUNC_CRYPTO_SECURE_MALLOC, (void (*)(void))CRYPTO_secure_malloc },
1027 { OSSL_FUNC_CRYPTO_SECURE_ZALLOC, (void (*)(void))CRYPTO_secure_zalloc },
1028 { OSSL_FUNC_CRYPTO_SECURE_FREE, (void (*)(void))CRYPTO_secure_free },
1029 { OSSL_FUNC_CRYPTO_SECURE_CLEAR_FREE,
1030 (void (*)(void))CRYPTO_secure_clear_free },
1031 { OSSL_FUNC_CRYPTO_SECURE_ALLOCATED,
1032 (void (*)(void))CRYPTO_secure_allocated },
1033 { OSSL_FUNC_OPENSSL_CLEANSE, (void (*)(void))OPENSSL_cleanse },
1037 static const OSSL_DISPATCH *core_dispatch = core_dispatch_;