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; /* Can be used as fallback */
46 unsigned int flag_activated_as_fallback:1;
48 /* OpenSSL library side data */
49 CRYPTO_REF_COUNT refcnt;
50 CRYPTO_RWLOCK *refcnt_lock; /* For the ref counter */
54 OSSL_provider_init_fn *init_function;
55 STACK_OF(INFOPAIR) *parameters;
56 OPENSSL_CTX *libctx; /* The library context this instance is in */
57 struct provider_store_st *store; /* The store this instance belongs to */
60 * In the FIPS module inner provider, this isn't needed, since the
61 * error upcalls are always direct calls to the outer provider.
63 int error_lib; /* ERR library number, one for each provider */
64 # ifndef OPENSSL_NO_ERR
65 ERR_STRING_DATA *error_strings; /* Copy of what the provider gives us */
69 /* Provider side functions */
70 OSSL_provider_teardown_fn *teardown;
71 OSSL_provider_gettable_params_fn *gettable_params;
72 OSSL_provider_get_params_fn *get_params;
73 OSSL_provider_query_operation_fn *query_operation;
76 * Cache of bit to indicate of query_operation() has been called on
77 * a specific operation or not.
79 unsigned char *operation_bits;
80 size_t operation_bits_sz;
82 /* Provider side data */
85 DEFINE_STACK_OF(OSSL_PROVIDER)
87 static int ossl_provider_cmp(const OSSL_PROVIDER * const *a,
88 const OSSL_PROVIDER * const *b)
90 return strcmp((*a)->name, (*b)->name);
94 * Provider Object store
95 * =====================
97 * The Provider Object store is a library context object, and therefore needs
101 struct provider_store_st {
102 STACK_OF(OSSL_PROVIDER) *providers;
105 unsigned int use_fallbacks:1;
109 * provider_deactivate_free() is a wrapper around ossl_provider_free()
110 * that also makes sure that activated fallback providers are deactivated.
111 * This is simply done by freeing them an extra time, to compensate for the
112 * refcount that provider_activate_fallbacks() gives them.
113 * Since this is only called when the provider store is being emptied, we
114 * don't need to care about any lock.
116 static void provider_deactivate_free(OSSL_PROVIDER *prov)
118 int extra_free = (prov->flag_initialized
119 && prov->flag_activated_as_fallback);
122 ossl_provider_free(prov);
123 ossl_provider_free(prov);
126 static void provider_store_free(void *vstore)
128 struct provider_store_st *store = vstore;
132 OPENSSL_free(store->default_path);
133 sk_OSSL_PROVIDER_pop_free(store->providers, provider_deactivate_free);
134 CRYPTO_THREAD_lock_free(store->lock);
138 static void *provider_store_new(OPENSSL_CTX *ctx)
140 struct provider_store_st *store = OPENSSL_zalloc(sizeof(*store));
141 const struct predefined_providers_st *p = NULL;
144 || (store->providers = sk_OSSL_PROVIDER_new(ossl_provider_cmp)) == NULL
145 || (store->lock = CRYPTO_THREAD_lock_new()) == NULL) {
146 provider_store_free(store);
149 store->use_fallbacks = 1;
151 for (p = predefined_providers; p->name != NULL; p++) {
152 OSSL_PROVIDER *prov = NULL;
155 * We use the internal constructor directly here,
156 * otherwise we get a call loop
158 prov = provider_new(p->name, p->init);
161 || sk_OSSL_PROVIDER_push(store->providers, prov) == 0) {
162 ossl_provider_free(prov);
163 provider_store_free(store);
164 CRYPTOerr(CRYPTO_F_PROVIDER_STORE_NEW, ERR_R_INTERNAL_ERROR);
170 prov->error_lib = ERR_get_next_error_library();
173 ossl_provider_set_fallback(prov);
179 static const OPENSSL_CTX_METHOD provider_store_method = {
184 static struct provider_store_st *get_provider_store(OPENSSL_CTX *libctx)
186 struct provider_store_st *store = NULL;
188 store = openssl_ctx_get_data(libctx, OPENSSL_CTX_PROVIDER_STORE_INDEX,
189 &provider_store_method);
191 CRYPTOerr(CRYPTO_F_GET_PROVIDER_STORE, ERR_R_INTERNAL_ERROR);
195 OSSL_PROVIDER *ossl_provider_find(OPENSSL_CTX *libctx, const char *name,
198 struct provider_store_st *store = NULL;
199 OSSL_PROVIDER *prov = NULL;
201 if ((store = get_provider_store(libctx)) != NULL) {
202 OSSL_PROVIDER tmpl = { 0, };
207 * Make sure any providers are loaded from config before we try to find
211 OPENSSL_init_crypto(OPENSSL_INIT_LOAD_CONFIG, NULL);
214 tmpl.name = (char *)name;
215 CRYPTO_THREAD_write_lock(store->lock);
216 if ((i = sk_OSSL_PROVIDER_find(store->providers, &tmpl)) == -1
217 || (prov = sk_OSSL_PROVIDER_value(store->providers, i)) == NULL
218 || !ossl_provider_up_ref(prov))
220 CRYPTO_THREAD_unlock(store->lock);
227 * Provider Object methods
228 * =======================
231 static OSSL_PROVIDER *provider_new(const char *name,
232 OSSL_provider_init_fn *init_function)
234 OSSL_PROVIDER *prov = NULL;
236 if ((prov = OPENSSL_zalloc(sizeof(*prov))) == NULL
238 || (prov->refcnt_lock = CRYPTO_THREAD_lock_new()) == NULL
240 || !ossl_provider_up_ref(prov) /* +1 One reference to be returned */
241 || (prov->name = OPENSSL_strdup(name)) == NULL) {
242 ossl_provider_free(prov);
243 CRYPTOerr(CRYPTO_F_PROVIDER_NEW, ERR_R_MALLOC_FAILURE);
247 prov->init_function = init_function;
251 int ossl_provider_up_ref(OSSL_PROVIDER *prov)
255 if (CRYPTO_UP_REF(&prov->refcnt, &ref, prov->refcnt_lock) <= 0)
260 OSSL_PROVIDER *ossl_provider_new(OPENSSL_CTX *libctx, const char *name,
261 OSSL_provider_init_fn *init_function,
264 struct provider_store_st *store = NULL;
265 OSSL_PROVIDER *prov = NULL;
267 if ((store = get_provider_store(libctx)) == NULL)
270 if ((prov = ossl_provider_find(libctx, name,
271 noconfig)) != NULL) { /* refcount +1 */
272 ossl_provider_free(prov); /* refcount -1 */
273 ERR_raise_data(ERR_LIB_CRYPTO, CRYPTO_R_PROVIDER_ALREADY_EXISTS, NULL,
278 /* provider_new() generates an error, so no need here */
279 if ((prov = provider_new(name, init_function)) == NULL)
282 CRYPTO_THREAD_write_lock(store->lock);
283 if (!ossl_provider_up_ref(prov)) { /* +1 One reference for the store */
284 ossl_provider_free(prov); /* -1 Reference that was to be returned */
286 } else if (sk_OSSL_PROVIDER_push(store->providers, prov) == 0) {
287 ossl_provider_free(prov); /* -1 Store reference */
288 ossl_provider_free(prov); /* -1 Reference that was to be returned */
291 prov->libctx = libctx;
294 prov->error_lib = ERR_get_next_error_library();
297 CRYPTO_THREAD_unlock(store->lock);
300 CRYPTOerr(CRYPTO_F_OSSL_PROVIDER_NEW, ERR_R_MALLOC_FAILURE);
303 * At this point, the provider is only partially "loaded". To be
304 * fully "loaded", ossl_provider_activate() must also be called.
310 static void free_infopair(INFOPAIR *pair)
312 OPENSSL_free(pair->name);
313 OPENSSL_free(pair->value);
317 void ossl_provider_free(OSSL_PROVIDER *prov)
322 CRYPTO_DOWN_REF(&prov->refcnt, &ref, prov->refcnt_lock);
325 * When the refcount drops below two, the store is the only
326 * possible reference, or it has already been taken away from
327 * the store (this may happen if a provider was activated
328 * because it's a fallback, but isn't currently used)
329 * When that happens, the provider is inactivated.
331 if (ref < 2 && prov->flag_initialized) {
333 ossl_init_thread_deregister(prov);
335 if (prov->teardown != NULL)
336 prov->teardown(prov->provctx);
337 #ifndef OPENSSL_NO_ERR
339 if (prov->error_strings != NULL) {
340 ERR_unload_strings(prov->error_lib, prov->error_strings);
341 OPENSSL_free(prov->error_strings);
342 prov->error_strings = NULL;
346 OPENSSL_free(prov->operation_bits);
347 prov->operation_bits = NULL;
348 prov->operation_bits_sz = 0;
349 prov->flag_initialized = 0;
353 * When the refcount drops to zero, it has been taken out of
354 * the store. All we have to do here is clean it out.
358 DSO_free(prov->module);
360 OPENSSL_free(prov->name);
361 OPENSSL_free(prov->path);
362 sk_INFOPAIR_pop_free(prov->parameters, free_infopair);
364 CRYPTO_THREAD_lock_free(prov->refcnt_lock);
372 int ossl_provider_set_module_path(OSSL_PROVIDER *prov, const char *module_path)
374 OPENSSL_free(prov->path);
375 if (module_path == NULL)
377 if ((prov->path = OPENSSL_strdup(module_path)) != NULL)
379 CRYPTOerr(CRYPTO_F_OSSL_PROVIDER_SET_MODULE_PATH, ERR_R_MALLOC_FAILURE);
383 int ossl_provider_add_parameter(OSSL_PROVIDER *prov,
384 const char *name, const char *value)
386 INFOPAIR *pair = NULL;
388 if ((pair = OPENSSL_zalloc(sizeof(*pair))) != NULL
389 && (prov->parameters != NULL
390 || (prov->parameters = sk_INFOPAIR_new_null()) != NULL)
391 && (pair->name = OPENSSL_strdup(name)) != NULL
392 && (pair->value = OPENSSL_strdup(value)) != NULL
393 && sk_INFOPAIR_push(prov->parameters, pair) > 0)
397 OPENSSL_free(pair->name);
398 OPENSSL_free(pair->value);
401 CRYPTOerr(CRYPTO_F_OSSL_PROVIDER_ADD_PARAMETER, ERR_R_MALLOC_FAILURE);
406 * Provider activation.
408 * What "activation" means depends on the provider form; for built in
409 * providers (in the library or the application alike), the provider
410 * can already be considered to be loaded, all that's needed is to
411 * initialize it. However, for dynamically loadable provider modules,
412 * we must first load that module.
414 * Built in modules are distinguished from dynamically loaded modules
415 * with an already assigned init function.
417 static const OSSL_DISPATCH *core_dispatch; /* Define further down */
419 int OSSL_PROVIDER_set_default_search_path(OPENSSL_CTX *libctx, const char *path)
421 struct provider_store_st *store;
425 p = OPENSSL_strdup(path);
427 CRYPTOerr(0, ERR_R_MALLOC_FAILURE);
431 if ((store = get_provider_store(libctx)) != NULL
432 && CRYPTO_THREAD_write_lock(store->lock)) {
433 OPENSSL_free(store->default_path);
434 store->default_path = p;
435 CRYPTO_THREAD_unlock(store->lock);
443 * Internal version that doesn't affect the store flags, and thereby avoid
444 * locking. Direct callers must remember to set the store flags when
447 static int provider_activate(OSSL_PROVIDER *prov)
449 const OSSL_DISPATCH *provider_dispatch = NULL;
450 void *tmp_provctx = NULL; /* safety measure */
451 #ifndef OPENSSL_NO_ERR
453 OSSL_provider_get_reason_strings_fn *p_get_reason_strings = NULL;
457 if (prov->flag_initialized)
461 * If the init function isn't set, it indicates that this provider is
464 if (prov->init_function == NULL) {
468 if (prov->module == NULL) {
469 char *allocated_path = NULL;
470 const char *module_path = NULL;
471 char *merged_path = NULL;
472 const char *load_dir = NULL;
473 struct provider_store_st *store;
475 if ((prov->module = DSO_new()) == NULL) {
476 /* DSO_new() generates an error already */
480 if ((store = get_provider_store(prov->libctx)) == NULL
481 || !CRYPTO_THREAD_read_lock(store->lock))
483 load_dir = store->default_path;
485 if (load_dir == NULL) {
486 load_dir = ossl_safe_getenv("OPENSSL_MODULES");
487 if (load_dir == NULL)
488 load_dir = MODULESDIR;
491 DSO_ctrl(prov->module, DSO_CTRL_SET_FLAGS,
492 DSO_FLAG_NAME_TRANSLATION_EXT_ONLY, NULL);
494 module_path = prov->path;
495 if (module_path == NULL)
496 module_path = allocated_path =
497 DSO_convert_filename(prov->module, prov->name);
498 if (module_path != NULL)
499 merged_path = DSO_merge(prov->module, module_path, load_dir);
500 CRYPTO_THREAD_unlock(store->lock);
502 if (merged_path == NULL
503 || (DSO_load(prov->module, merged_path, NULL, 0)) == NULL) {
504 DSO_free(prov->module);
508 OPENSSL_free(merged_path);
509 OPENSSL_free(allocated_path);
512 if (prov->module != NULL)
513 prov->init_function = (OSSL_provider_init_fn *)
514 DSO_bind_func(prov->module, "OSSL_provider_init");
518 /* Call the initialise function for the provider. */
519 if (prov->init_function == NULL
520 || !prov->init_function((OSSL_CORE_HANDLE *)prov, core_dispatch,
521 &provider_dispatch, &tmp_provctx)) {
522 ERR_raise_data(ERR_LIB_CRYPTO, ERR_R_INIT_FAIL, NULL,
523 "name=%s", prov->name);
525 DSO_free(prov->module);
530 prov->provctx = tmp_provctx;
532 for (; provider_dispatch->function_id != 0; provider_dispatch++) {
533 switch (provider_dispatch->function_id) {
534 case OSSL_FUNC_PROVIDER_TEARDOWN:
536 OSSL_get_provider_teardown(provider_dispatch);
538 case OSSL_FUNC_PROVIDER_GETTABLE_PARAMS:
539 prov->gettable_params =
540 OSSL_get_provider_gettable_params(provider_dispatch);
542 case OSSL_FUNC_PROVIDER_GET_PARAMS:
544 OSSL_get_provider_get_params(provider_dispatch);
546 case OSSL_FUNC_PROVIDER_QUERY_OPERATION:
547 prov->query_operation =
548 OSSL_get_provider_query_operation(provider_dispatch);
550 #ifndef OPENSSL_NO_ERR
552 case OSSL_FUNC_PROVIDER_GET_REASON_STRINGS:
553 p_get_reason_strings =
554 OSSL_get_provider_get_reason_strings(provider_dispatch);
561 #ifndef OPENSSL_NO_ERR
563 if (p_get_reason_strings != NULL) {
564 const OSSL_ITEM *reasonstrings = p_get_reason_strings(prov->provctx);
568 * ERR_load_strings() handles ERR_STRING_DATA rather than OSSL_ITEM,
569 * although they are essentially the same type.
570 * Furthermore, ERR_load_strings() patches the array's error number
571 * with the error library number, so we need to make a copy of that
575 while (reasonstrings[cnt].id != 0) {
576 if (ERR_GET_LIB(reasonstrings[cnt].id) != 0)
580 cnt++; /* One for the terminating item */
582 /* Allocate one extra item for the "library" name */
583 prov->error_strings =
584 OPENSSL_zalloc(sizeof(ERR_STRING_DATA) * (cnt + 1));
585 if (prov->error_strings == NULL)
589 * Set the "library" name.
591 prov->error_strings[0].error = ERR_PACK(prov->error_lib, 0, 0);
592 prov->error_strings[0].string = prov->name;
594 * Copy reasonstrings item 0..cnt-1 to prov->error_trings positions
597 for (cnt2 = 1; cnt2 <= cnt; cnt2++) {
598 prov->error_strings[cnt2].error = (int)reasonstrings[cnt2-1].id;
599 prov->error_strings[cnt2].string = reasonstrings[cnt2-1].ptr;
602 ERR_load_strings(prov->error_lib, prov->error_strings);
607 /* With this flag set, this provider has become fully "loaded". */
608 prov->flag_initialized = 1;
613 int ossl_provider_activate(OSSL_PROVIDER *prov)
615 if (provider_activate(prov)) {
616 CRYPTO_THREAD_write_lock(prov->store->lock);
617 prov->store->use_fallbacks = 0;
618 CRYPTO_THREAD_unlock(prov->store->lock);
625 void *ossl_provider_ctx(const OSSL_PROVIDER *prov)
627 return prov->provctx;
631 static int provider_forall_loaded(struct provider_store_st *store,
632 int *found_activated,
633 int (*cb)(OSSL_PROVIDER *provider,
641 num_provs = sk_OSSL_PROVIDER_num(store->providers);
643 if (found_activated != NULL)
644 *found_activated = 0;
645 for (i = 0; i < num_provs; i++) {
646 OSSL_PROVIDER *prov =
647 sk_OSSL_PROVIDER_value(store->providers, i);
649 if (prov->flag_initialized) {
650 if (found_activated != NULL)
651 *found_activated = 1;
652 if (!(ret = cb(prov, cbdata)))
661 * This function only does something once when store->use_fallbacks == 1,
662 * and then sets store->use_fallbacks = 0, so the second call and so on is
663 * effectively a no-op.
665 static void provider_activate_fallbacks(struct provider_store_st *store)
667 if (store->use_fallbacks) {
668 int num_provs = sk_OSSL_PROVIDER_num(store->providers);
669 int activated_fallback_count = 0;
672 for (i = 0; i < num_provs; i++) {
673 OSSL_PROVIDER *prov = sk_OSSL_PROVIDER_value(store->providers, i);
676 * Activated fallback providers get an extra refcount, to
677 * simulate a regular load.
678 * Note that we don't care if the activation succeeds or not,
679 * other than to maintain a correct refcount. If the activation
680 * doesn't succeed, then any future attempt to use the fallback
681 * provider will fail anyway.
683 if (prov->flag_fallback) {
684 if (ossl_provider_up_ref(prov)) {
685 if (!provider_activate(prov)) {
686 ossl_provider_free(prov);
688 prov->flag_activated_as_fallback = 1;
689 activated_fallback_count++;
696 * We assume that all fallbacks have been added to the store before
697 * any fallback is activated.
698 * TODO: We may have to reconsider this, IF we find ourselves adding
699 * fallbacks after any previous fallback has been activated.
701 if (activated_fallback_count > 0)
702 store->use_fallbacks = 0;
706 int ossl_provider_forall_loaded(OPENSSL_CTX *ctx,
707 int (*cb)(OSSL_PROVIDER *provider,
712 struct provider_store_st *store = get_provider_store(ctx);
716 * Make sure any providers are loaded from config before we try to use
719 OPENSSL_init_crypto(OPENSSL_INIT_LOAD_CONFIG, NULL);
723 CRYPTO_THREAD_read_lock(store->lock);
725 provider_activate_fallbacks(store);
728 * Now, we sweep through all providers
730 ret = provider_forall_loaded(store, NULL, cb, cbdata);
732 CRYPTO_THREAD_unlock(store->lock);
738 int ossl_provider_available(OSSL_PROVIDER *prov)
741 CRYPTO_THREAD_read_lock(prov->store->lock);
742 provider_activate_fallbacks(prov->store);
743 CRYPTO_THREAD_unlock(prov->store->lock);
745 return prov->flag_initialized;
750 /* Setters of Provider Object data */
751 int ossl_provider_set_fallback(OSSL_PROVIDER *prov)
756 prov->flag_fallback = 1;
760 /* Getters of Provider Object data */
761 const char *ossl_provider_name(const OSSL_PROVIDER *prov)
766 const DSO *ossl_provider_dso(const OSSL_PROVIDER *prov)
771 const char *ossl_provider_module_name(const OSSL_PROVIDER *prov)
776 return DSO_get_filename(prov->module);
780 const char *ossl_provider_module_path(const OSSL_PROVIDER *prov)
785 /* FIXME: Ensure it's a full path */
786 return DSO_get_filename(prov->module);
790 void *ossl_provider_prov_ctx(const OSSL_PROVIDER *prov)
793 return prov->provctx;
798 OPENSSL_CTX *ossl_provider_library_context(const OSSL_PROVIDER *prov)
800 /* TODO(3.0) just: return prov->libctx; */
801 return prov != NULL ? prov->libctx : NULL;
804 /* Wrappers around calls to the provider */
805 void ossl_provider_teardown(const OSSL_PROVIDER *prov)
807 if (prov->teardown != NULL)
808 prov->teardown(prov->provctx);
811 const OSSL_PARAM *ossl_provider_gettable_params(const OSSL_PROVIDER *prov)
813 return prov->gettable_params == NULL
814 ? NULL : prov->gettable_params(prov->provctx);
817 int ossl_provider_get_params(const OSSL_PROVIDER *prov, OSSL_PARAM params[])
819 return prov->get_params == NULL
820 ? 0 : prov->get_params(prov->provctx, params);
824 const OSSL_ALGORITHM *ossl_provider_query_operation(const OSSL_PROVIDER *prov,
828 return prov->query_operation(prov->provctx, operation_id, no_cache);
831 int ossl_provider_set_operation_bit(OSSL_PROVIDER *provider, size_t bitnum)
833 size_t byte = bitnum / 8;
834 unsigned char bit = (1 << (bitnum % 8)) & 0xFF;
836 if (provider->operation_bits_sz <= byte) {
837 provider->operation_bits = OPENSSL_realloc(provider->operation_bits,
839 if (provider->operation_bits == NULL) {
840 ERR_raise(ERR_LIB_CRYPTO, ERR_R_MALLOC_FAILURE);
843 memset(provider->operation_bits + provider->operation_bits_sz,
844 '\0', byte + 1 - provider->operation_bits_sz);
846 provider->operation_bits[byte] |= bit;
850 int ossl_provider_test_operation_bit(OSSL_PROVIDER *provider, size_t bitnum,
853 size_t byte = bitnum / 8;
854 unsigned char bit = (1 << (bitnum % 8)) & 0xFF;
856 if (!ossl_assert(result != NULL)) {
857 ERR_raise(ERR_LIB_CRYPTO, ERR_R_PASSED_NULL_PARAMETER);
862 if (provider->operation_bits_sz > byte)
863 *result = ((provider->operation_bits[byte] & bit) != 0);
868 * Core functions for the provider
869 * ===============================
871 * This is the set of functions that the core makes available to the provider
875 * This returns a list of Provider Object parameters with their types, for
876 * discovery. We do not expect that many providers will use this, but one
879 static const OSSL_PARAM param_types[] = {
880 OSSL_PARAM_DEFN(OSSL_PROV_PARAM_CORE_VERSION, OSSL_PARAM_UTF8_PTR, NULL, 0),
881 OSSL_PARAM_DEFN(OSSL_PROV_PARAM_CORE_PROV_NAME, OSSL_PARAM_UTF8_PTR,
884 OSSL_PARAM_DEFN(OSSL_PROV_PARAM_CORE_MODULE_FILENAME, OSSL_PARAM_UTF8_PTR,
891 * Forward declare all the functions that are provided aa dispatch.
892 * This ensures that the compiler will complain if they aren't defined
893 * with the correct signature.
895 static OSSL_core_gettable_params_fn core_gettable_params;
896 static OSSL_core_get_params_fn core_get_params;
897 static OSSL_core_thread_start_fn core_thread_start;
898 static OSSL_core_get_library_context_fn core_get_libctx;
900 static OSSL_core_new_error_fn core_new_error;
901 static OSSL_core_set_error_debug_fn core_set_error_debug;
902 static OSSL_core_vset_error_fn core_vset_error;
903 static OSSL_core_set_error_mark_fn core_set_error_mark;
904 static OSSL_core_clear_last_error_mark_fn core_clear_last_error_mark;
905 static OSSL_core_pop_error_to_mark_fn core_pop_error_to_mark;
908 static const OSSL_PARAM *core_gettable_params(const OSSL_CORE_HANDLE *handle)
913 static int core_get_params(const OSSL_CORE_HANDLE *handle, OSSL_PARAM params[])
918 * We created this object originally and we know it is actually an
919 * OSSL_PROVIDER *, so the cast is safe
921 OSSL_PROVIDER *prov = (OSSL_PROVIDER *)handle;
923 if ((p = OSSL_PARAM_locate(params, OSSL_PROV_PARAM_CORE_VERSION)) != NULL)
924 OSSL_PARAM_set_utf8_ptr(p, OPENSSL_VERSION_STR);
925 if ((p = OSSL_PARAM_locate(params, OSSL_PROV_PARAM_CORE_PROV_NAME)) != NULL)
926 OSSL_PARAM_set_utf8_ptr(p, prov->name);
929 if ((p = OSSL_PARAM_locate(params,
930 OSSL_PROV_PARAM_CORE_MODULE_FILENAME)) != NULL)
931 OSSL_PARAM_set_utf8_ptr(p, ossl_provider_module_path(prov));
934 if (prov->parameters == NULL)
937 for (i = 0; i < sk_INFOPAIR_num(prov->parameters); i++) {
938 INFOPAIR *pair = sk_INFOPAIR_value(prov->parameters, i);
940 if ((p = OSSL_PARAM_locate(params, pair->name)) != NULL)
941 OSSL_PARAM_set_utf8_ptr(p, pair->value);
946 static OPENSSL_CORE_CTX *core_get_libctx(const OSSL_CORE_HANDLE *handle)
949 * We created this object originally and we know it is actually an
950 * OSSL_PROVIDER *, so the cast is safe
952 OSSL_PROVIDER *prov = (OSSL_PROVIDER *)handle;
954 return (OPENSSL_CORE_CTX *)ossl_provider_library_context(prov);
957 static int core_thread_start(const OSSL_CORE_HANDLE *handle,
958 OSSL_thread_stop_handler_fn handfn)
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;
966 return ossl_init_thread_start(prov, prov->provctx, handfn);
970 * The FIPS module inner provider doesn't implement these. They aren't
971 * needed there, since the FIPS module upcalls are always the outer provider
976 * TODO(3.0) These error functions should use |handle| to select the proper
977 * library context to report in the correct error stack, at least if error
978 * stacks become tied to the library context.
979 * We cannot currently do that since there's no support for it in the
982 static void core_new_error(const OSSL_CORE_HANDLE *handle)
987 static void core_set_error_debug(const OSSL_CORE_HANDLE *handle,
988 const char *file, int line, const char *func)
990 ERR_set_debug(file, line, func);
993 static void core_vset_error(const OSSL_CORE_HANDLE *handle,
994 uint32_t reason, const char *fmt, va_list args)
997 * We created this object originally and we know it is actually an
998 * OSSL_PROVIDER *, so the cast is safe
1000 OSSL_PROVIDER *prov = (OSSL_PROVIDER *)handle;
1003 * If the uppermost 8 bits are non-zero, it's an OpenSSL library
1004 * error and will be treated as such. Otherwise, it's a new style
1005 * provider error and will be treated as such.
1007 if (ERR_GET_LIB(reason) != 0) {
1008 ERR_vset_error(ERR_GET_LIB(reason), ERR_GET_REASON(reason), fmt, args);
1010 ERR_vset_error(prov->error_lib, (int)reason, fmt, args);
1014 static int core_set_error_mark(const OSSL_CORE_HANDLE *handle)
1016 return ERR_set_mark();
1019 static int core_clear_last_error_mark(const OSSL_CORE_HANDLE *handle)
1021 return ERR_clear_last_mark();
1024 static int core_pop_error_to_mark(const OSSL_CORE_HANDLE *handle)
1026 return ERR_pop_to_mark();
1028 #endif /* FIPS_MODULE */
1031 * Functions provided by the core. Blank line separates "families" of related
1034 static const OSSL_DISPATCH core_dispatch_[] = {
1035 { OSSL_FUNC_CORE_GETTABLE_PARAMS, (void (*)(void))core_gettable_params },
1036 { OSSL_FUNC_CORE_GET_PARAMS, (void (*)(void))core_get_params },
1037 { OSSL_FUNC_CORE_GET_LIBRARY_CONTEXT, (void (*)(void))core_get_libctx },
1038 { OSSL_FUNC_CORE_THREAD_START, (void (*)(void))core_thread_start },
1040 { OSSL_FUNC_CORE_NEW_ERROR, (void (*)(void))core_new_error },
1041 { OSSL_FUNC_CORE_SET_ERROR_DEBUG, (void (*)(void))core_set_error_debug },
1042 { OSSL_FUNC_CORE_VSET_ERROR, (void (*)(void))core_vset_error },
1043 { OSSL_FUNC_CORE_SET_ERROR_MARK, (void (*)(void))core_set_error_mark },
1044 { OSSL_FUNC_CORE_CLEAR_LAST_ERROR_MARK,
1045 (void (*)(void))core_clear_last_error_mark },
1046 { OSSL_FUNC_CORE_POP_ERROR_TO_MARK, (void (*)(void))core_pop_error_to_mark },
1047 { OSSL_FUNC_BIO_NEW_FILE, (void (*)(void))BIO_new_file },
1048 { OSSL_FUNC_BIO_NEW_MEMBUF, (void (*)(void))BIO_new_mem_buf },
1049 { OSSL_FUNC_BIO_READ_EX, (void (*)(void))BIO_read_ex },
1050 { OSSL_FUNC_BIO_WRITE_EX, (void (*)(void))BIO_write_ex },
1051 { OSSL_FUNC_BIO_FREE, (void (*)(void))BIO_free },
1052 { OSSL_FUNC_BIO_VPRINTF, (void (*)(void))BIO_vprintf },
1053 { OSSL_FUNC_BIO_VSNPRINTF, (void (*)(void))BIO_vsnprintf },
1054 { OSSL_FUNC_SELF_TEST_CB, (void (*)(void))OSSL_SELF_TEST_get_callback },
1056 { OSSL_FUNC_CRYPTO_MALLOC, (void (*)(void))CRYPTO_malloc },
1057 { OSSL_FUNC_CRYPTO_ZALLOC, (void (*)(void))CRYPTO_zalloc },
1058 { OSSL_FUNC_CRYPTO_FREE, (void (*)(void))CRYPTO_free },
1059 { OSSL_FUNC_CRYPTO_CLEAR_FREE, (void (*)(void))CRYPTO_clear_free },
1060 { OSSL_FUNC_CRYPTO_REALLOC, (void (*)(void))CRYPTO_realloc },
1061 { OSSL_FUNC_CRYPTO_CLEAR_REALLOC, (void (*)(void))CRYPTO_clear_realloc },
1062 { OSSL_FUNC_CRYPTO_SECURE_MALLOC, (void (*)(void))CRYPTO_secure_malloc },
1063 { OSSL_FUNC_CRYPTO_SECURE_ZALLOC, (void (*)(void))CRYPTO_secure_zalloc },
1064 { OSSL_FUNC_CRYPTO_SECURE_FREE, (void (*)(void))CRYPTO_secure_free },
1065 { OSSL_FUNC_CRYPTO_SECURE_CLEAR_FREE,
1066 (void (*)(void))CRYPTO_secure_clear_free },
1067 { OSSL_FUNC_CRYPTO_SECURE_ALLOCATED,
1068 (void (*)(void))CRYPTO_secure_allocated },
1069 { OSSL_FUNC_OPENSSL_CLEANSE, (void (*)(void))OPENSSL_cleanse },
1073 static const OSSL_DISPATCH *core_dispatch = core_dispatch_;