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_dispatch.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 "crypto/evp.h" /* evp_method_store_flush */
18 #include "crypto/rand.h"
19 #include "internal/nelem.h"
20 #include "internal/thread_once.h"
21 #include "internal/provider.h"
22 #include "internal/refcount.h"
23 #include "provider_local.h"
25 # include <openssl/self_test.h>
28 static OSSL_PROVIDER *provider_new(const char *name,
29 OSSL_provider_init_fn *init_function);
32 * Provider Object structure
33 * =========================
40 DEFINE_STACK_OF(INFOPAIR)
42 struct provider_store_st; /* Forward declaration */
44 struct ossl_provider_st {
46 unsigned int flag_initialized:1;
47 unsigned int flag_fallback:1; /* Can be used as fallback */
48 unsigned int flag_activated_as_fallback:1;
50 /* OpenSSL library side data */
51 CRYPTO_REF_COUNT refcnt;
52 CRYPTO_RWLOCK *refcnt_lock; /* For the ref counter */
56 OSSL_provider_init_fn *init_function;
57 STACK_OF(INFOPAIR) *parameters;
58 OSSL_LIB_CTX *libctx; /* The library context this instance is in */
59 struct provider_store_st *store; /* The store this instance belongs to */
62 * In the FIPS module inner provider, this isn't needed, since the
63 * error upcalls are always direct calls to the outer provider.
65 int error_lib; /* ERR library number, one for each provider */
66 # ifndef OPENSSL_NO_ERR
67 ERR_STRING_DATA *error_strings; /* Copy of what the provider gives us */
71 /* Provider side functions */
72 OSSL_FUNC_provider_teardown_fn *teardown;
73 OSSL_FUNC_provider_gettable_params_fn *gettable_params;
74 OSSL_FUNC_provider_get_params_fn *get_params;
75 OSSL_FUNC_provider_get_capabilities_fn *get_capabilities;
76 OSSL_FUNC_provider_self_test_fn *self_test;
77 OSSL_FUNC_provider_query_operation_fn *query_operation;
80 * Cache of bit to indicate of query_operation() has been called on
81 * a specific operation or not.
83 unsigned char *operation_bits;
84 size_t operation_bits_sz;
86 /* Provider side data */
89 DEFINE_STACK_OF(OSSL_PROVIDER)
91 static int ossl_provider_cmp(const OSSL_PROVIDER * const *a,
92 const OSSL_PROVIDER * const *b)
94 return strcmp((*a)->name, (*b)->name);
98 * Provider Object store
99 * =====================
101 * The Provider Object store is a library context object, and therefore needs
105 struct provider_store_st {
106 STACK_OF(OSSL_PROVIDER) *providers;
109 unsigned int use_fallbacks:1;
113 * provider_deactivate_free() is a wrapper around ossl_provider_free()
114 * that also makes sure that activated fallback providers are deactivated.
115 * This is simply done by freeing them an extra time, to compensate for the
116 * refcount that provider_activate_fallbacks() gives them.
117 * Since this is only called when the provider store is being emptied, we
118 * don't need to care about any lock.
120 static void provider_deactivate_free(OSSL_PROVIDER *prov)
122 int extra_free = (prov->flag_initialized
123 && prov->flag_activated_as_fallback);
126 ossl_provider_free(prov);
127 ossl_provider_free(prov);
130 static void provider_store_free(void *vstore)
132 struct provider_store_st *store = vstore;
136 OPENSSL_free(store->default_path);
137 sk_OSSL_PROVIDER_pop_free(store->providers, provider_deactivate_free);
138 CRYPTO_THREAD_lock_free(store->lock);
142 static void *provider_store_new(OSSL_LIB_CTX *ctx)
144 struct provider_store_st *store = OPENSSL_zalloc(sizeof(*store));
145 const struct predefined_providers_st *p = NULL;
148 || (store->providers = sk_OSSL_PROVIDER_new(ossl_provider_cmp)) == NULL
149 || (store->lock = CRYPTO_THREAD_lock_new()) == NULL) {
150 provider_store_free(store);
153 store->use_fallbacks = 1;
155 for (p = predefined_providers; p->name != NULL; p++) {
156 OSSL_PROVIDER *prov = NULL;
159 * We use the internal constructor directly here,
160 * otherwise we get a call loop
162 prov = provider_new(p->name, p->init);
165 || sk_OSSL_PROVIDER_push(store->providers, prov) == 0) {
166 ossl_provider_free(prov);
167 provider_store_free(store);
168 ERR_raise(ERR_LIB_CRYPTO, ERR_R_INTERNAL_ERROR);
174 prov->error_lib = ERR_get_next_error_library();
177 ossl_provider_set_fallback(prov);
183 static const OSSL_LIB_CTX_METHOD provider_store_method = {
188 static struct provider_store_st *get_provider_store(OSSL_LIB_CTX *libctx)
190 struct provider_store_st *store = NULL;
192 store = ossl_lib_ctx_get_data(libctx, OSSL_LIB_CTX_PROVIDER_STORE_INDEX,
193 &provider_store_method);
195 ERR_raise(ERR_LIB_CRYPTO, ERR_R_INTERNAL_ERROR);
199 int ossl_provider_disable_fallback_loading(OSSL_LIB_CTX *libctx)
201 struct provider_store_st *store;
203 if ((store = get_provider_store(libctx)) != NULL) {
204 store->use_fallbacks = 0;
210 OSSL_PROVIDER *ossl_provider_find(OSSL_LIB_CTX *libctx, const char *name,
213 struct provider_store_st *store = NULL;
214 OSSL_PROVIDER *prov = NULL;
216 if ((store = get_provider_store(libctx)) != NULL) {
217 OSSL_PROVIDER tmpl = { 0, };
222 * Make sure any providers are loaded from config before we try to find
226 OPENSSL_init_crypto(OPENSSL_INIT_LOAD_CONFIG, NULL);
229 tmpl.name = (char *)name;
230 CRYPTO_THREAD_write_lock(store->lock);
231 if ((i = sk_OSSL_PROVIDER_find(store->providers, &tmpl)) == -1
232 || (prov = sk_OSSL_PROVIDER_value(store->providers, i)) == NULL
233 || !ossl_provider_up_ref(prov))
235 CRYPTO_THREAD_unlock(store->lock);
242 * Provider Object methods
243 * =======================
246 static OSSL_PROVIDER *provider_new(const char *name,
247 OSSL_provider_init_fn *init_function)
249 OSSL_PROVIDER *prov = NULL;
251 if ((prov = OPENSSL_zalloc(sizeof(*prov))) == NULL
253 || (prov->refcnt_lock = CRYPTO_THREAD_lock_new()) == NULL
255 || !ossl_provider_up_ref(prov) /* +1 One reference to be returned */
256 || (prov->name = OPENSSL_strdup(name)) == NULL) {
257 ossl_provider_free(prov);
258 ERR_raise(ERR_LIB_CRYPTO, ERR_R_MALLOC_FAILURE);
262 prov->init_function = init_function;
266 int ossl_provider_up_ref(OSSL_PROVIDER *prov)
270 if (CRYPTO_UP_REF(&prov->refcnt, &ref, prov->refcnt_lock) <= 0)
275 OSSL_PROVIDER *ossl_provider_new(OSSL_LIB_CTX *libctx, const char *name,
276 OSSL_provider_init_fn *init_function,
279 struct provider_store_st *store = NULL;
280 OSSL_PROVIDER *prov = NULL;
282 if ((store = get_provider_store(libctx)) == NULL)
285 if ((prov = ossl_provider_find(libctx, name,
286 noconfig)) != NULL) { /* refcount +1 */
287 ossl_provider_free(prov); /* refcount -1 */
288 ERR_raise_data(ERR_LIB_CRYPTO, CRYPTO_R_PROVIDER_ALREADY_EXISTS,
293 /* provider_new() generates an error, so no need here */
294 if ((prov = provider_new(name, init_function)) == NULL)
297 CRYPTO_THREAD_write_lock(store->lock);
298 if (!ossl_provider_up_ref(prov)) { /* +1 One reference for the store */
299 ossl_provider_free(prov); /* -1 Reference that was to be returned */
301 } else if (sk_OSSL_PROVIDER_push(store->providers, prov) == 0) {
302 ossl_provider_free(prov); /* -1 Store reference */
303 ossl_provider_free(prov); /* -1 Reference that was to be returned */
306 prov->libctx = libctx;
309 prov->error_lib = ERR_get_next_error_library();
312 CRYPTO_THREAD_unlock(store->lock);
315 ERR_raise(ERR_LIB_CRYPTO, ERR_R_MALLOC_FAILURE);
318 * At this point, the provider is only partially "loaded". To be
319 * fully "loaded", ossl_provider_activate() must also be called.
325 static void free_infopair(INFOPAIR *pair)
327 OPENSSL_free(pair->name);
328 OPENSSL_free(pair->value);
332 void ossl_provider_free(OSSL_PROVIDER *prov)
337 CRYPTO_DOWN_REF(&prov->refcnt, &ref, prov->refcnt_lock);
340 * When the refcount drops below two, the store is the only
341 * possible reference, or it has already been taken away from
342 * the store (this may happen if a provider was activated
343 * because it's a fallback, but isn't currently used)
344 * When that happens, the provider is inactivated.
346 if (ref < 2 && prov->flag_initialized) {
348 ossl_init_thread_deregister(prov);
350 if (prov->teardown != NULL)
351 prov->teardown(prov->provctx);
352 #ifndef OPENSSL_NO_ERR
354 if (prov->error_strings != NULL) {
355 ERR_unload_strings(prov->error_lib, prov->error_strings);
356 OPENSSL_free(prov->error_strings);
357 prov->error_strings = NULL;
361 OPENSSL_free(prov->operation_bits);
362 prov->operation_bits = NULL;
363 prov->operation_bits_sz = 0;
364 prov->flag_initialized = 0;
368 * When the refcount drops to zero, it has been taken out of
369 * the store. All we have to do here is clean it out.
373 DSO_free(prov->module);
375 OPENSSL_free(prov->name);
376 OPENSSL_free(prov->path);
377 sk_INFOPAIR_pop_free(prov->parameters, free_infopair);
379 CRYPTO_THREAD_lock_free(prov->refcnt_lock);
387 int ossl_provider_set_module_path(OSSL_PROVIDER *prov, const char *module_path)
389 OPENSSL_free(prov->path);
390 if (module_path == NULL)
392 if ((prov->path = OPENSSL_strdup(module_path)) != NULL)
394 ERR_raise(ERR_LIB_CRYPTO, ERR_R_MALLOC_FAILURE);
398 int ossl_provider_add_parameter(OSSL_PROVIDER *prov,
399 const char *name, const char *value)
401 INFOPAIR *pair = NULL;
403 if ((pair = OPENSSL_zalloc(sizeof(*pair))) != NULL
404 && (prov->parameters != NULL
405 || (prov->parameters = sk_INFOPAIR_new_null()) != NULL)
406 && (pair->name = OPENSSL_strdup(name)) != NULL
407 && (pair->value = OPENSSL_strdup(value)) != NULL
408 && sk_INFOPAIR_push(prov->parameters, pair) > 0)
412 OPENSSL_free(pair->name);
413 OPENSSL_free(pair->value);
416 ERR_raise(ERR_LIB_CRYPTO, ERR_R_MALLOC_FAILURE);
421 * Provider activation.
423 * What "activation" means depends on the provider form; for built in
424 * providers (in the library or the application alike), the provider
425 * can already be considered to be loaded, all that's needed is to
426 * initialize it. However, for dynamically loadable provider modules,
427 * we must first load that module.
429 * Built in modules are distinguished from dynamically loaded modules
430 * with an already assigned init function.
432 static const OSSL_DISPATCH *core_dispatch; /* Define further down */
434 int OSSL_PROVIDER_set_default_search_path(OSSL_LIB_CTX *libctx,
437 struct provider_store_st *store;
441 p = OPENSSL_strdup(path);
443 ERR_raise(ERR_LIB_CRYPTO, ERR_R_MALLOC_FAILURE);
447 if ((store = get_provider_store(libctx)) != NULL
448 && CRYPTO_THREAD_write_lock(store->lock)) {
449 OPENSSL_free(store->default_path);
450 store->default_path = p;
451 CRYPTO_THREAD_unlock(store->lock);
459 * Internal version that doesn't affect the store flags, and thereby avoid
460 * locking. Direct callers must remember to set the store flags when
463 static int provider_activate(OSSL_PROVIDER *prov)
465 const OSSL_DISPATCH *provider_dispatch = NULL;
466 void *tmp_provctx = NULL; /* safety measure */
467 #ifndef OPENSSL_NO_ERR
469 OSSL_FUNC_provider_get_reason_strings_fn *p_get_reason_strings = NULL;
473 if (prov->flag_initialized)
477 * If the init function isn't set, it indicates that this provider is
480 if (prov->init_function == NULL) {
484 if (prov->module == NULL) {
485 char *allocated_path = NULL;
486 const char *module_path = NULL;
487 char *merged_path = NULL;
488 const char *load_dir = NULL;
489 struct provider_store_st *store;
491 if ((prov->module = DSO_new()) == NULL) {
492 /* DSO_new() generates an error already */
496 if ((store = get_provider_store(prov->libctx)) == NULL
497 || !CRYPTO_THREAD_read_lock(store->lock))
499 load_dir = store->default_path;
501 if (load_dir == NULL) {
502 load_dir = ossl_safe_getenv("OPENSSL_MODULES");
503 if (load_dir == NULL)
504 load_dir = MODULESDIR;
507 DSO_ctrl(prov->module, DSO_CTRL_SET_FLAGS,
508 DSO_FLAG_NAME_TRANSLATION_EXT_ONLY, NULL);
510 module_path = prov->path;
511 if (module_path == NULL)
512 module_path = allocated_path =
513 DSO_convert_filename(prov->module, prov->name);
514 if (module_path != NULL)
515 merged_path = DSO_merge(prov->module, module_path, load_dir);
516 CRYPTO_THREAD_unlock(store->lock);
518 if (merged_path == NULL
519 || (DSO_load(prov->module, merged_path, NULL, 0)) == NULL) {
520 DSO_free(prov->module);
524 OPENSSL_free(merged_path);
525 OPENSSL_free(allocated_path);
528 if (prov->module != NULL)
529 prov->init_function = (OSSL_provider_init_fn *)
530 DSO_bind_func(prov->module, "OSSL_provider_init");
534 /* Call the initialise function for the provider. */
535 if (prov->init_function == NULL
536 || !prov->init_function((OSSL_CORE_HANDLE *)prov, core_dispatch,
537 &provider_dispatch, &tmp_provctx)) {
538 ERR_raise_data(ERR_LIB_CRYPTO, ERR_R_INIT_FAIL,
539 "name=%s", prov->name);
541 DSO_free(prov->module);
546 prov->provctx = tmp_provctx;
548 for (; provider_dispatch->function_id != 0; provider_dispatch++) {
549 switch (provider_dispatch->function_id) {
550 case OSSL_FUNC_PROVIDER_TEARDOWN:
552 OSSL_FUNC_provider_teardown(provider_dispatch);
554 case OSSL_FUNC_PROVIDER_GETTABLE_PARAMS:
555 prov->gettable_params =
556 OSSL_FUNC_provider_gettable_params(provider_dispatch);
558 case OSSL_FUNC_PROVIDER_GET_PARAMS:
560 OSSL_FUNC_provider_get_params(provider_dispatch);
562 case OSSL_FUNC_PROVIDER_SELF_TEST:
564 OSSL_FUNC_provider_self_test(provider_dispatch);
566 case OSSL_FUNC_PROVIDER_GET_CAPABILITIES:
567 prov->get_capabilities =
568 OSSL_FUNC_provider_get_capabilities(provider_dispatch);
570 case OSSL_FUNC_PROVIDER_QUERY_OPERATION:
571 prov->query_operation =
572 OSSL_FUNC_provider_query_operation(provider_dispatch);
574 #ifndef OPENSSL_NO_ERR
576 case OSSL_FUNC_PROVIDER_GET_REASON_STRINGS:
577 p_get_reason_strings =
578 OSSL_FUNC_provider_get_reason_strings(provider_dispatch);
585 #ifndef OPENSSL_NO_ERR
587 if (p_get_reason_strings != NULL) {
588 const OSSL_ITEM *reasonstrings = p_get_reason_strings(prov->provctx);
592 * ERR_load_strings() handles ERR_STRING_DATA rather than OSSL_ITEM,
593 * although they are essentially the same type.
594 * Furthermore, ERR_load_strings() patches the array's error number
595 * with the error library number, so we need to make a copy of that
599 while (reasonstrings[cnt].id != 0) {
600 if (ERR_GET_LIB(reasonstrings[cnt].id) != 0)
604 cnt++; /* One for the terminating item */
606 /* Allocate one extra item for the "library" name */
607 prov->error_strings =
608 OPENSSL_zalloc(sizeof(ERR_STRING_DATA) * (cnt + 1));
609 if (prov->error_strings == NULL)
613 * Set the "library" name.
615 prov->error_strings[0].error = ERR_PACK(prov->error_lib, 0, 0);
616 prov->error_strings[0].string = prov->name;
618 * Copy reasonstrings item 0..cnt-1 to prov->error_trings positions
621 for (cnt2 = 1; cnt2 <= cnt; cnt2++) {
622 prov->error_strings[cnt2].error = (int)reasonstrings[cnt2-1].id;
623 prov->error_strings[cnt2].string = reasonstrings[cnt2-1].ptr;
626 ERR_load_strings(prov->error_lib, prov->error_strings);
631 /* With this flag set, this provider has become fully "loaded". */
632 prov->flag_initialized = 1;
636 int ossl_provider_activate(OSSL_PROVIDER *prov)
638 if (provider_activate(prov)) {
639 CRYPTO_THREAD_write_lock(prov->store->lock);
640 prov->store->use_fallbacks = 0;
641 CRYPTO_THREAD_unlock(prov->store->lock);
648 void *ossl_provider_ctx(const OSSL_PROVIDER *prov)
650 return prov->provctx;
654 static int provider_forall_loaded(struct provider_store_st *store,
655 int *found_activated,
656 int (*cb)(OSSL_PROVIDER *provider,
664 num_provs = sk_OSSL_PROVIDER_num(store->providers);
666 if (found_activated != NULL)
667 *found_activated = 0;
668 for (i = 0; i < num_provs; i++) {
669 OSSL_PROVIDER *prov =
670 sk_OSSL_PROVIDER_value(store->providers, i);
672 if (prov->flag_initialized) {
673 if (found_activated != NULL)
674 *found_activated = 1;
675 if (!(ret = cb(prov, cbdata)))
684 * This function only does something once when store->use_fallbacks == 1,
685 * and then sets store->use_fallbacks = 0, so the second call and so on is
686 * effectively a no-op.
688 static void provider_activate_fallbacks(struct provider_store_st *store)
690 if (store->use_fallbacks) {
691 int num_provs = sk_OSSL_PROVIDER_num(store->providers);
692 int activated_fallback_count = 0;
695 for (i = 0; i < num_provs; i++) {
696 OSSL_PROVIDER *prov = sk_OSSL_PROVIDER_value(store->providers, i);
699 * Activated fallback providers get an extra refcount, to
700 * simulate a regular load.
701 * Note that we don't care if the activation succeeds or not,
702 * other than to maintain a correct refcount. If the activation
703 * doesn't succeed, then any future attempt to use the fallback
704 * provider will fail anyway.
706 if (prov->flag_fallback) {
707 if (ossl_provider_up_ref(prov)) {
708 if (!provider_activate(prov)) {
709 ossl_provider_free(prov);
711 prov->flag_activated_as_fallback = 1;
712 activated_fallback_count++;
719 * We assume that all fallbacks have been added to the store before
720 * any fallback is activated.
721 * TODO: We may have to reconsider this, IF we find ourselves adding
722 * fallbacks after any previous fallback has been activated.
724 if (activated_fallback_count > 0)
725 store->use_fallbacks = 0;
729 int ossl_provider_forall_loaded(OSSL_LIB_CTX *ctx,
730 int (*cb)(OSSL_PROVIDER *provider,
735 struct provider_store_st *store = get_provider_store(ctx);
739 * Make sure any providers are loaded from config before we try to use
742 OPENSSL_init_crypto(OPENSSL_INIT_LOAD_CONFIG, NULL);
746 CRYPTO_THREAD_read_lock(store->lock);
748 provider_activate_fallbacks(store);
751 * Now, we sweep through all providers
753 ret = provider_forall_loaded(store, NULL, cb, cbdata);
755 CRYPTO_THREAD_unlock(store->lock);
761 int ossl_provider_available(OSSL_PROVIDER *prov)
764 CRYPTO_THREAD_read_lock(prov->store->lock);
765 provider_activate_fallbacks(prov->store);
766 CRYPTO_THREAD_unlock(prov->store->lock);
768 return prov->flag_initialized;
773 /* Setters of Provider Object data */
774 int ossl_provider_set_fallback(OSSL_PROVIDER *prov)
779 prov->flag_fallback = 1;
783 /* Getters of Provider Object data */
784 const char *ossl_provider_name(const OSSL_PROVIDER *prov)
789 const DSO *ossl_provider_dso(const OSSL_PROVIDER *prov)
794 const char *ossl_provider_module_name(const OSSL_PROVIDER *prov)
799 return DSO_get_filename(prov->module);
803 const char *ossl_provider_module_path(const OSSL_PROVIDER *prov)
808 /* FIXME: Ensure it's a full path */
809 return DSO_get_filename(prov->module);
813 void *ossl_provider_prov_ctx(const OSSL_PROVIDER *prov)
816 return prov->provctx;
821 OSSL_LIB_CTX *ossl_provider_libctx(const OSSL_PROVIDER *prov)
823 /* TODO(3.0) just: return prov->libctx; */
824 return prov != NULL ? prov->libctx : NULL;
827 /* Wrappers around calls to the provider */
828 void ossl_provider_teardown(const OSSL_PROVIDER *prov)
830 if (prov->teardown != NULL)
831 prov->teardown(prov->provctx);
834 const OSSL_PARAM *ossl_provider_gettable_params(const OSSL_PROVIDER *prov)
836 return prov->gettable_params == NULL
837 ? NULL : prov->gettable_params(prov->provctx);
840 int ossl_provider_get_params(const OSSL_PROVIDER *prov, OSSL_PARAM params[])
842 return prov->get_params == NULL
843 ? 0 : prov->get_params(prov->provctx, params);
846 int ossl_provider_self_test(const OSSL_PROVIDER *prov)
850 if (prov->self_test == NULL)
852 ret = prov->self_test(prov->provctx);
854 evp_method_store_flush(ossl_provider_libctx(prov));
858 int ossl_provider_get_capabilities(const OSSL_PROVIDER *prov,
859 const char *capability,
863 return prov->get_capabilities == NULL
864 ? 1 : prov->get_capabilities(prov->provctx, capability, cb, arg);
867 const OSSL_ALGORITHM *ossl_provider_query_operation(const OSSL_PROVIDER *prov,
871 return prov->query_operation == NULL
872 ? NULL : prov->query_operation(prov->provctx, operation_id, no_cache);
875 int ossl_provider_set_operation_bit(OSSL_PROVIDER *provider, size_t bitnum)
877 size_t byte = bitnum / 8;
878 unsigned char bit = (1 << (bitnum % 8)) & 0xFF;
880 if (provider->operation_bits_sz <= byte) {
881 unsigned char *tmp = OPENSSL_realloc(provider->operation_bits,
885 ERR_raise(ERR_LIB_CRYPTO, ERR_R_MALLOC_FAILURE);
888 provider->operation_bits = tmp;
889 memset(provider->operation_bits + provider->operation_bits_sz,
890 '\0', byte + 1 - provider->operation_bits_sz);
891 provider->operation_bits_sz = byte + 1;
893 provider->operation_bits[byte] |= bit;
897 int ossl_provider_test_operation_bit(OSSL_PROVIDER *provider, size_t bitnum,
900 size_t byte = bitnum / 8;
901 unsigned char bit = (1 << (bitnum % 8)) & 0xFF;
903 if (!ossl_assert(result != NULL)) {
904 ERR_raise(ERR_LIB_CRYPTO, ERR_R_PASSED_NULL_PARAMETER);
909 if (provider->operation_bits_sz > byte)
910 *result = ((provider->operation_bits[byte] & bit) != 0);
915 * Core functions for the provider
916 * ===============================
918 * This is the set of functions that the core makes available to the provider
922 * This returns a list of Provider Object parameters with their types, for
923 * discovery. We do not expect that many providers will use this, but one
926 static const OSSL_PARAM param_types[] = {
927 OSSL_PARAM_DEFN(OSSL_PROV_PARAM_CORE_VERSION, OSSL_PARAM_UTF8_PTR, NULL, 0),
928 OSSL_PARAM_DEFN(OSSL_PROV_PARAM_CORE_PROV_NAME, OSSL_PARAM_UTF8_PTR,
931 OSSL_PARAM_DEFN(OSSL_PROV_PARAM_CORE_MODULE_FILENAME, OSSL_PARAM_UTF8_PTR,
938 * Forward declare all the functions that are provided aa dispatch.
939 * This ensures that the compiler will complain if they aren't defined
940 * with the correct signature.
942 static OSSL_FUNC_core_gettable_params_fn core_gettable_params;
943 static OSSL_FUNC_core_get_params_fn core_get_params;
944 static OSSL_FUNC_core_thread_start_fn core_thread_start;
945 static OSSL_FUNC_core_get_libctx_fn core_get_libctx;
947 static OSSL_FUNC_core_new_error_fn core_new_error;
948 static OSSL_FUNC_core_set_error_debug_fn core_set_error_debug;
949 static OSSL_FUNC_core_vset_error_fn core_vset_error;
950 static OSSL_FUNC_core_set_error_mark_fn core_set_error_mark;
951 static OSSL_FUNC_core_clear_last_error_mark_fn core_clear_last_error_mark;
952 static OSSL_FUNC_core_pop_error_to_mark_fn core_pop_error_to_mark;
955 static const OSSL_PARAM *core_gettable_params(const OSSL_CORE_HANDLE *handle)
960 static int core_get_params(const OSSL_CORE_HANDLE *handle, OSSL_PARAM params[])
965 * We created this object originally and we know it is actually an
966 * OSSL_PROVIDER *, so the cast is safe
968 OSSL_PROVIDER *prov = (OSSL_PROVIDER *)handle;
970 if ((p = OSSL_PARAM_locate(params, OSSL_PROV_PARAM_CORE_VERSION)) != NULL)
971 OSSL_PARAM_set_utf8_ptr(p, OPENSSL_VERSION_STR);
972 if ((p = OSSL_PARAM_locate(params, OSSL_PROV_PARAM_CORE_PROV_NAME)) != NULL)
973 OSSL_PARAM_set_utf8_ptr(p, prov->name);
976 if ((p = OSSL_PARAM_locate(params,
977 OSSL_PROV_PARAM_CORE_MODULE_FILENAME)) != NULL)
978 OSSL_PARAM_set_utf8_ptr(p, ossl_provider_module_path(prov));
981 if (prov->parameters == NULL)
984 for (i = 0; i < sk_INFOPAIR_num(prov->parameters); i++) {
985 INFOPAIR *pair = sk_INFOPAIR_value(prov->parameters, i);
987 if ((p = OSSL_PARAM_locate(params, pair->name)) != NULL)
988 OSSL_PARAM_set_utf8_ptr(p, pair->value);
993 static OPENSSL_CORE_CTX *core_get_libctx(const OSSL_CORE_HANDLE *handle)
996 * We created this object originally and we know it is actually an
997 * OSSL_PROVIDER *, so the cast is safe
999 OSSL_PROVIDER *prov = (OSSL_PROVIDER *)handle;
1001 return (OPENSSL_CORE_CTX *)ossl_provider_libctx(prov);
1004 static int core_thread_start(const OSSL_CORE_HANDLE *handle,
1005 OSSL_thread_stop_handler_fn handfn)
1008 * We created this object originally and we know it is actually an
1009 * OSSL_PROVIDER *, so the cast is safe
1011 OSSL_PROVIDER *prov = (OSSL_PROVIDER *)handle;
1013 return ossl_init_thread_start(prov, prov->provctx, handfn);
1017 * The FIPS module inner provider doesn't implement these. They aren't
1018 * needed there, since the FIPS module upcalls are always the outer provider
1023 * TODO(3.0) These error functions should use |handle| to select the proper
1024 * library context to report in the correct error stack, at least if error
1025 * stacks become tied to the library context.
1026 * We cannot currently do that since there's no support for it in the
1029 static void core_new_error(const OSSL_CORE_HANDLE *handle)
1034 static void core_set_error_debug(const OSSL_CORE_HANDLE *handle,
1035 const char *file, int line, const char *func)
1037 ERR_set_debug(file, line, func);
1040 static void core_vset_error(const OSSL_CORE_HANDLE *handle,
1041 uint32_t reason, const char *fmt, va_list args)
1044 * We created this object originally and we know it is actually an
1045 * OSSL_PROVIDER *, so the cast is safe
1047 OSSL_PROVIDER *prov = (OSSL_PROVIDER *)handle;
1050 * If the uppermost 8 bits are non-zero, it's an OpenSSL library
1051 * error and will be treated as such. Otherwise, it's a new style
1052 * provider error and will be treated as such.
1054 if (ERR_GET_LIB(reason) != 0) {
1055 ERR_vset_error(ERR_GET_LIB(reason), ERR_GET_REASON(reason), fmt, args);
1057 ERR_vset_error(prov->error_lib, (int)reason, fmt, args);
1061 static int core_set_error_mark(const OSSL_CORE_HANDLE *handle)
1063 return ERR_set_mark();
1066 static int core_clear_last_error_mark(const OSSL_CORE_HANDLE *handle)
1068 return ERR_clear_last_mark();
1071 static int core_pop_error_to_mark(const OSSL_CORE_HANDLE *handle)
1073 return ERR_pop_to_mark();
1075 #endif /* FIPS_MODULE */
1078 * Functions provided by the core.
1080 static const OSSL_DISPATCH core_dispatch_[] = {
1081 { OSSL_FUNC_CORE_GETTABLE_PARAMS, (void (*)(void))core_gettable_params },
1082 { OSSL_FUNC_CORE_GET_PARAMS, (void (*)(void))core_get_params },
1083 { OSSL_FUNC_CORE_GET_LIBCTX, (void (*)(void))core_get_libctx },
1084 { OSSL_FUNC_CORE_THREAD_START, (void (*)(void))core_thread_start },
1086 { OSSL_FUNC_CORE_NEW_ERROR, (void (*)(void))core_new_error },
1087 { OSSL_FUNC_CORE_SET_ERROR_DEBUG, (void (*)(void))core_set_error_debug },
1088 { OSSL_FUNC_CORE_VSET_ERROR, (void (*)(void))core_vset_error },
1089 { OSSL_FUNC_CORE_SET_ERROR_MARK, (void (*)(void))core_set_error_mark },
1090 { OSSL_FUNC_CORE_CLEAR_LAST_ERROR_MARK,
1091 (void (*)(void))core_clear_last_error_mark },
1092 { OSSL_FUNC_CORE_POP_ERROR_TO_MARK, (void (*)(void))core_pop_error_to_mark },
1093 { OSSL_FUNC_BIO_NEW_FILE, (void (*)(void))BIO_new_file },
1094 { OSSL_FUNC_BIO_NEW_MEMBUF, (void (*)(void))BIO_new_mem_buf },
1095 { OSSL_FUNC_BIO_READ_EX, (void (*)(void))BIO_read_ex },
1096 { OSSL_FUNC_BIO_WRITE_EX, (void (*)(void))BIO_write_ex },
1097 { OSSL_FUNC_BIO_GETS, (void (*)(void))BIO_gets },
1098 { OSSL_FUNC_BIO_PUTS, (void (*)(void))BIO_puts },
1099 { OSSL_FUNC_BIO_CTRL, (void (*)(void))BIO_ctrl },
1100 { OSSL_FUNC_BIO_FREE, (void (*)(void))BIO_free },
1101 { OSSL_FUNC_BIO_VPRINTF, (void (*)(void))BIO_vprintf },
1102 { OSSL_FUNC_BIO_VSNPRINTF, (void (*)(void))BIO_vsnprintf },
1103 { OSSL_FUNC_SELF_TEST_CB, (void (*)(void))OSSL_SELF_TEST_get_callback },
1104 { OSSL_FUNC_GET_ENTROPY, (void (*)(void))ossl_rand_get_entropy },
1105 { OSSL_FUNC_CLEANUP_ENTROPY, (void (*)(void))ossl_rand_cleanup_entropy },
1106 { OSSL_FUNC_GET_NONCE, (void (*)(void))ossl_rand_get_nonce },
1107 { OSSL_FUNC_CLEANUP_NONCE, (void (*)(void))ossl_rand_cleanup_nonce },
1109 { OSSL_FUNC_CRYPTO_MALLOC, (void (*)(void))CRYPTO_malloc },
1110 { OSSL_FUNC_CRYPTO_ZALLOC, (void (*)(void))CRYPTO_zalloc },
1111 { OSSL_FUNC_CRYPTO_FREE, (void (*)(void))CRYPTO_free },
1112 { OSSL_FUNC_CRYPTO_CLEAR_FREE, (void (*)(void))CRYPTO_clear_free },
1113 { OSSL_FUNC_CRYPTO_REALLOC, (void (*)(void))CRYPTO_realloc },
1114 { OSSL_FUNC_CRYPTO_CLEAR_REALLOC, (void (*)(void))CRYPTO_clear_realloc },
1115 { OSSL_FUNC_CRYPTO_SECURE_MALLOC, (void (*)(void))CRYPTO_secure_malloc },
1116 { OSSL_FUNC_CRYPTO_SECURE_ZALLOC, (void (*)(void))CRYPTO_secure_zalloc },
1117 { OSSL_FUNC_CRYPTO_SECURE_FREE, (void (*)(void))CRYPTO_secure_free },
1118 { OSSL_FUNC_CRYPTO_SECURE_CLEAR_FREE,
1119 (void (*)(void))CRYPTO_secure_clear_free },
1120 { OSSL_FUNC_CRYPTO_SECURE_ALLOCATED,
1121 (void (*)(void))CRYPTO_secure_allocated },
1122 { OSSL_FUNC_OPENSSL_CLEANSE, (void (*)(void))OPENSSL_cleanse },
1126 static const OSSL_DISPATCH *core_dispatch = core_dispatch_;