2 * Copyright 2019 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/params.h>
13 #include <openssl/opensslv.h>
14 #include "internal/cryptlib_int.h"
15 #include "internal/nelem.h"
16 #include "internal/thread_once.h"
17 #include "internal/provider.h"
18 #include "internal/refcount.h"
19 #include "provider_local.h"
21 static OSSL_PROVIDER *provider_new(const char *name,
22 OSSL_provider_init_fn *init_function);
25 * Provider Object structure
26 * =========================
33 DEFINE_STACK_OF(INFOPAIR)
35 struct provider_store_st; /* Forward declaration */
37 struct ossl_provider_st {
39 unsigned int flag_initialized:1;
40 unsigned int flag_fallback:1;
42 /* OpenSSL library side data */
43 CRYPTO_REF_COUNT refcnt;
44 CRYPTO_RWLOCK *refcnt_lock; /* For the ref counter */
48 OSSL_provider_init_fn *init_function;
49 STACK_OF(INFOPAIR) *parameters;
50 OPENSSL_CTX *libctx; /* The library context this instance is in */
51 struct provider_store_st *store; /* The store this instance belongs to */
54 * In the FIPS module inner provider, this isn't needed, since the
55 * error upcalls are always direct calls to the outer provider.
57 int error_lib; /* ERR library number, one for each provider */
58 # ifndef OPENSSL_NO_ERR
59 ERR_STRING_DATA *error_strings; /* Copy of what the provider gives us */
63 /* Provider side functions */
64 OSSL_provider_teardown_fn *teardown;
65 OSSL_provider_get_param_types_fn *get_param_types;
66 OSSL_provider_get_params_fn *get_params;
67 OSSL_provider_query_operation_fn *query_operation;
69 /* Provider side data */
72 DEFINE_STACK_OF(OSSL_PROVIDER)
74 static int ossl_provider_cmp(const OSSL_PROVIDER * const *a,
75 const OSSL_PROVIDER * const *b)
77 return strcmp((*a)->name, (*b)->name);
81 * Provider Object store
82 * =====================
84 * The Provider Object store is a library context object, and therefore needs
88 struct provider_store_st {
89 STACK_OF(OSSL_PROVIDER) *providers;
91 unsigned int use_fallbacks:1;
94 static void provider_store_free(void *vstore)
96 struct provider_store_st *store = vstore;
100 sk_OSSL_PROVIDER_pop_free(store->providers, ossl_provider_free);
101 CRYPTO_THREAD_lock_free(store->lock);
105 static void *provider_store_new(OPENSSL_CTX *ctx)
107 struct provider_store_st *store = OPENSSL_zalloc(sizeof(*store));
108 const struct predefined_providers_st *p = NULL;
111 || (store->providers = sk_OSSL_PROVIDER_new(ossl_provider_cmp)) == NULL
112 || (store->lock = CRYPTO_THREAD_lock_new()) == NULL) {
113 provider_store_free(store);
116 store->use_fallbacks = 1;
118 for (p = predefined_providers; p->name != NULL; p++) {
119 OSSL_PROVIDER *prov = NULL;
122 * We use the internal constructor directly here,
123 * otherwise we get a call loop
125 prov = provider_new(p->name, p->init);
128 || sk_OSSL_PROVIDER_push(store->providers, prov) == 0) {
129 ossl_provider_free(prov);
130 provider_store_free(store);
131 CRYPTOerr(CRYPTO_F_PROVIDER_STORE_NEW, ERR_R_INTERNAL_ERROR);
137 prov->error_lib = ERR_get_next_error_library();
140 ossl_provider_set_fallback(prov);
146 static const OPENSSL_CTX_METHOD provider_store_method = {
151 static struct provider_store_st *get_provider_store(OPENSSL_CTX *libctx)
153 struct provider_store_st *store = NULL;
155 store = openssl_ctx_get_data(libctx, OPENSSL_CTX_PROVIDER_STORE_INDEX,
156 &provider_store_method);
158 CRYPTOerr(CRYPTO_F_GET_PROVIDER_STORE, ERR_R_INTERNAL_ERROR);
162 OSSL_PROVIDER *ossl_provider_find(OPENSSL_CTX *libctx, const char *name)
164 struct provider_store_st *store = NULL;
165 OSSL_PROVIDER *prov = NULL;
167 if ((store = get_provider_store(libctx)) != NULL) {
168 OSSL_PROVIDER tmpl = { 0, };
171 tmpl.name = (char *)name;
172 CRYPTO_THREAD_write_lock(store->lock);
173 if ((i = sk_OSSL_PROVIDER_find(store->providers, &tmpl)) == -1
174 || (prov = sk_OSSL_PROVIDER_value(store->providers, i)) == NULL
175 || !ossl_provider_up_ref(prov))
177 CRYPTO_THREAD_unlock(store->lock);
184 * Provider Object methods
185 * =======================
188 static OSSL_PROVIDER *provider_new(const char *name,
189 OSSL_provider_init_fn *init_function)
191 OSSL_PROVIDER *prov = NULL;
193 if ((prov = OPENSSL_zalloc(sizeof(*prov))) == NULL
195 || (prov->refcnt_lock = CRYPTO_THREAD_lock_new()) == NULL
197 || !ossl_provider_up_ref(prov) /* +1 One reference to be returned */
198 || (prov->name = OPENSSL_strdup(name)) == NULL) {
199 ossl_provider_free(prov);
200 CRYPTOerr(CRYPTO_F_PROVIDER_NEW, ERR_R_MALLOC_FAILURE);
204 prov->init_function = init_function;
208 int ossl_provider_up_ref(OSSL_PROVIDER *prov)
212 if (CRYPTO_UP_REF(&prov->refcnt, &ref, prov->refcnt_lock) <= 0)
217 OSSL_PROVIDER *ossl_provider_new(OPENSSL_CTX *libctx, const char *name,
218 OSSL_provider_init_fn *init_function)
220 struct provider_store_st *store = NULL;
221 OSSL_PROVIDER *prov = NULL;
223 if ((store = get_provider_store(libctx)) == NULL)
226 if ((prov = ossl_provider_find(libctx, name)) != NULL) { /* refcount +1 */
227 ossl_provider_free(prov); /* refcount -1 */
228 CRYPTOerr(CRYPTO_F_OSSL_PROVIDER_NEW,
229 CRYPTO_R_PROVIDER_ALREADY_EXISTS);
230 ERR_add_error_data(2, "name=", name);
234 /* provider_new() generates an error, so no need here */
235 if ((prov = provider_new(name, init_function)) == NULL)
238 CRYPTO_THREAD_write_lock(store->lock);
239 if (!ossl_provider_up_ref(prov)) { /* +1 One reference for the store */
240 ossl_provider_free(prov); /* -1 Reference that was to be returned */
242 } else if (sk_OSSL_PROVIDER_push(store->providers, prov) == 0) {
243 ossl_provider_free(prov); /* -1 Store reference */
244 ossl_provider_free(prov); /* -1 Reference that was to be returned */
247 prov->libctx = libctx;
250 prov->error_lib = ERR_get_next_error_library();
253 CRYPTO_THREAD_unlock(store->lock);
256 CRYPTOerr(CRYPTO_F_OSSL_PROVIDER_NEW, ERR_R_MALLOC_FAILURE);
259 * At this point, the provider is only partially "loaded". To be
260 * fully "loaded", ossl_provider_activate() must also be called.
266 static void free_infopair(INFOPAIR *pair)
268 OPENSSL_free(pair->name);
269 OPENSSL_free(pair->value);
273 void ossl_provider_free(OSSL_PROVIDER *prov)
278 CRYPTO_DOWN_REF(&prov->refcnt, &ref, prov->refcnt_lock);
281 * When the refcount drops below two, the store is the only
282 * possible reference, or it has already been taken away from
283 * the store (this may happen if a provider was activated
284 * because it's a fallback, but isn't currently used)
285 * When that happens, the provider is inactivated.
287 if (ref < 2 && prov->flag_initialized) {
289 ossl_init_thread_deregister(prov);
291 if (prov->teardown != NULL)
292 prov->teardown(prov->provctx);
293 #ifndef OPENSSL_NO_ERR
295 if (prov->error_strings != NULL) {
296 ERR_unload_strings(prov->error_lib, prov->error_strings);
297 OPENSSL_free(prov->error_strings);
298 prov->error_strings = NULL;
302 prov->flag_initialized = 0;
306 * When the refcount drops to zero, it has been taken out of
307 * the store. All we have to do here is clean it out.
311 DSO_free(prov->module);
313 OPENSSL_free(prov->name);
314 OPENSSL_free(prov->path);
315 sk_INFOPAIR_pop_free(prov->parameters, free_infopair);
317 CRYPTO_THREAD_lock_free(prov->refcnt_lock);
325 int ossl_provider_set_module_path(OSSL_PROVIDER *prov, const char *module_path)
327 OPENSSL_free(prov->path);
328 if (module_path == NULL)
330 if ((prov->path = OPENSSL_strdup(module_path)) != NULL)
332 CRYPTOerr(CRYPTO_F_OSSL_PROVIDER_SET_MODULE_PATH, ERR_R_MALLOC_FAILURE);
336 int ossl_provider_add_parameter(OSSL_PROVIDER *prov,
337 const char *name, const char *value)
339 INFOPAIR *pair = NULL;
341 if ((pair = OPENSSL_zalloc(sizeof(*pair))) != NULL
342 && (prov->parameters != NULL
343 || (prov->parameters = sk_INFOPAIR_new_null()) != NULL)
344 && (pair->name = OPENSSL_strdup(name)) != NULL
345 && (pair->value = OPENSSL_strdup(value)) != NULL
346 && sk_INFOPAIR_push(prov->parameters, pair) > 0)
350 OPENSSL_free(pair->name);
351 OPENSSL_free(pair->value);
354 CRYPTOerr(CRYPTO_F_OSSL_PROVIDER_ADD_PARAMETER, ERR_R_MALLOC_FAILURE);
359 * Provider activation.
361 * What "activation" means depends on the provider form; for built in
362 * providers (in the library or the application alike), the provider
363 * can already be considered to be loaded, all that's needed is to
364 * initialize it. However, for dynamically loadable provider modules,
365 * we must first load that module.
367 * Built in modules are distinguished from dynamically loaded modules
368 * with an already assigned init function.
370 static const OSSL_DISPATCH *core_dispatch; /* Define further down */
373 * Internal version that doesn't affect the store flags, and thereby avoid
374 * locking. Direct callers must remember to set the store flags when
377 static int provider_activate(OSSL_PROVIDER *prov)
379 const OSSL_DISPATCH *provider_dispatch = NULL;
380 #ifndef OPENSSL_NO_ERR
382 OSSL_provider_get_reason_strings_fn *p_get_reason_strings = NULL;
386 if (prov->flag_initialized)
390 * If the init function isn't set, it indicates that this provider is
393 if (prov->init_function == NULL) {
397 if (prov->module == NULL) {
398 char *allocated_path = NULL;
399 const char *module_path = NULL;
400 char *merged_path = NULL;
401 const char *load_dir = ossl_safe_getenv("OPENSSL_MODULES");
403 if ((prov->module = DSO_new()) == NULL) {
404 /* DSO_new() generates an error already */
408 if (load_dir == NULL)
409 load_dir = MODULESDIR;
411 DSO_ctrl(prov->module, DSO_CTRL_SET_FLAGS,
412 DSO_FLAG_NAME_TRANSLATION_EXT_ONLY, NULL);
414 module_path = prov->path;
415 if (module_path == NULL)
416 module_path = allocated_path =
417 DSO_convert_filename(prov->module, prov->name);
418 if (module_path != NULL)
419 merged_path = DSO_merge(prov->module, module_path, load_dir);
421 if (merged_path == NULL
422 || (DSO_load(prov->module, merged_path, NULL, 0)) == NULL) {
423 DSO_free(prov->module);
427 OPENSSL_free(merged_path);
428 OPENSSL_free(allocated_path);
431 if (prov->module != NULL)
432 prov->init_function = (OSSL_provider_init_fn *)
433 DSO_bind_func(prov->module, "OSSL_provider_init");
437 /* Call the initialise function for the provider. */
438 if (prov->init_function == NULL
439 || !prov->init_function(prov, core_dispatch, &provider_dispatch,
441 CRYPTOerr(CRYPTO_F_PROVIDER_ACTIVATE, ERR_R_INIT_FAIL);
442 ERR_add_error_data(2, "name=", prov->name);
444 DSO_free(prov->module);
450 for (; provider_dispatch->function_id != 0; provider_dispatch++) {
451 switch (provider_dispatch->function_id) {
452 case OSSL_FUNC_PROVIDER_TEARDOWN:
454 OSSL_get_provider_teardown(provider_dispatch);
456 case OSSL_FUNC_PROVIDER_GET_PARAM_TYPES:
457 prov->get_param_types =
458 OSSL_get_provider_get_param_types(provider_dispatch);
460 case OSSL_FUNC_PROVIDER_GET_PARAMS:
462 OSSL_get_provider_get_params(provider_dispatch);
464 case OSSL_FUNC_PROVIDER_QUERY_OPERATION:
465 prov->query_operation =
466 OSSL_get_provider_query_operation(provider_dispatch);
468 #ifndef OPENSSL_NO_ERR
470 case OSSL_FUNC_PROVIDER_GET_REASON_STRINGS:
471 p_get_reason_strings =
472 OSSL_get_provider_get_reason_strings(provider_dispatch);
479 #ifndef OPENSSL_NO_ERR
481 if (p_get_reason_strings != NULL) {
482 const OSSL_ITEM *reasonstrings = p_get_reason_strings(prov->provctx);
486 * ERR_load_strings() handles ERR_STRING_DATA rather than OSSL_ITEM,
487 * although they are essentially the same type.
488 * Furthermore, ERR_load_strings() patches the array's error number
489 * with the error library number, so we need to make a copy of that
492 cnt = 1; /* One for the terminating item */
493 while (reasonstrings[cnt].id != 0) {
494 if (ERR_GET_LIB(reasonstrings[cnt].id) != 0)
499 /* Allocate one extra item for the "library" name */
500 prov->error_strings =
501 OPENSSL_zalloc(sizeof(ERR_STRING_DATA) * (cnt + 1));
502 if (prov->error_strings == NULL)
506 * Set the "library" name.
508 prov->error_strings[0].error = ERR_PACK(prov->error_lib, 0, 0);
509 prov->error_strings[0].string = prov->name;
511 * Copy reasonstrings item 0..cnt-1 to prov->error_trings positions
514 for (cnt2 = 1; cnt2 <= cnt; cnt2++) {
515 prov->error_strings[cnt2].error = (int)reasonstrings[cnt2-1].id;
516 prov->error_strings[cnt2].string = reasonstrings[cnt2-1].ptr;
519 ERR_load_strings(prov->error_lib, prov->error_strings);
524 /* With this flag set, this provider has become fully "loaded". */
525 prov->flag_initialized = 1;
530 int ossl_provider_activate(OSSL_PROVIDER *prov)
532 if (provider_activate(prov)) {
533 CRYPTO_THREAD_write_lock(prov->store->lock);
534 prov->store->use_fallbacks = 0;
535 CRYPTO_THREAD_unlock(prov->store->lock);
542 void *ossl_provider_ctx(const OSSL_PROVIDER *prov)
544 return prov->provctx;
548 static int provider_forall_loaded(struct provider_store_st *store,
549 int *found_activated,
550 int (*cb)(OSSL_PROVIDER *provider,
556 int num_provs = sk_OSSL_PROVIDER_num(store->providers);
558 if (found_activated != NULL)
559 *found_activated = 0;
560 for (i = 0; i < num_provs; i++) {
561 OSSL_PROVIDER *prov =
562 sk_OSSL_PROVIDER_value(store->providers, i);
564 if (prov->flag_initialized) {
565 if (found_activated != NULL)
566 *found_activated = 1;
567 if (!(ret = cb(prov, cbdata)))
576 * This function only does something once when store->use_fallbacks == 1,
577 * and then sets store->use_fallbacks = 0, so the second call and so on is
578 * effectively a no-op.
580 static void provider_activate_fallbacks(struct provider_store_st *store)
582 if (store->use_fallbacks) {
583 int num_provs = sk_OSSL_PROVIDER_num(store->providers);
584 int activated_fallback_count = 0;
587 for (i = 0; i < num_provs; i++) {
588 OSSL_PROVIDER *prov = sk_OSSL_PROVIDER_value(store->providers, i);
591 * Note that we don't care if the activation succeeds or not.
592 * If it doesn't succeed, then any attempt to use any of the
593 * fallback providers will fail anyway.
595 if (prov->flag_fallback) {
596 activated_fallback_count++;
597 provider_activate(prov);
602 * We assume that all fallbacks have been added to the store before
603 * any fallback is activated.
604 * TODO: We may have to reconsider this, IF we find ourselves adding
605 * fallbacks after any previous fallback has been activated.
607 if (activated_fallback_count > 0)
608 store->use_fallbacks = 0;
612 int ossl_provider_forall_loaded(OPENSSL_CTX *ctx,
613 int (*cb)(OSSL_PROVIDER *provider,
618 struct provider_store_st *store = get_provider_store(ctx);
621 CRYPTO_THREAD_read_lock(store->lock);
623 provider_activate_fallbacks(store);
626 * Now, we sweep through all providers
628 ret = provider_forall_loaded(store, NULL, cb, cbdata);
630 CRYPTO_THREAD_unlock(store->lock);
636 int ossl_provider_available(OSSL_PROVIDER *prov)
639 CRYPTO_THREAD_read_lock(prov->store->lock);
640 provider_activate_fallbacks(prov->store);
641 CRYPTO_THREAD_unlock(prov->store->lock);
643 return prov->flag_initialized;
648 /* Setters of Provider Object data */
649 int ossl_provider_set_fallback(OSSL_PROVIDER *prov)
654 prov->flag_fallback = 1;
658 /* Getters of Provider Object data */
659 const char *ossl_provider_name(const OSSL_PROVIDER *prov)
664 const DSO *ossl_provider_dso(const OSSL_PROVIDER *prov)
669 const char *ossl_provider_module_name(const OSSL_PROVIDER *prov)
674 return DSO_get_filename(prov->module);
678 const char *ossl_provider_module_path(const OSSL_PROVIDER *prov)
683 /* FIXME: Ensure it's a full path */
684 return DSO_get_filename(prov->module);
688 /* Wrappers around calls to the provider */
689 void ossl_provider_teardown(const OSSL_PROVIDER *prov)
691 if (prov->teardown != NULL)
692 prov->teardown(prov->provctx);
695 const OSSL_PARAM *ossl_provider_get_param_types(const OSSL_PROVIDER *prov)
697 return prov->get_param_types == NULL
698 ? NULL : prov->get_param_types(prov->provctx);
701 int ossl_provider_get_params(const OSSL_PROVIDER *prov, OSSL_PARAM params[])
703 return prov->get_params == NULL
704 ? 0 : prov->get_params(prov->provctx, params);
708 const OSSL_ALGORITHM *ossl_provider_query_operation(const OSSL_PROVIDER *prov,
712 return prov->query_operation(prov->provctx, operation_id, no_cache);
716 * Core functions for the provider
717 * ===============================
719 * This is the set of functions that the core makes available to the provider
723 * This returns a list of Provider Object parameters with their types, for
724 * discovery. We do not expect that many providers will use this, but one
727 static const OSSL_PARAM param_types[] = {
728 OSSL_PARAM_DEFN("openssl-verstion", OSSL_PARAM_UTF8_PTR, NULL, 0),
729 OSSL_PARAM_DEFN("provider-name", OSSL_PARAM_UTF8_PTR, NULL, 0),
733 static const OSSL_PARAM *core_get_param_types(const OSSL_PROVIDER *prov)
738 static int core_get_params(const OSSL_PROVIDER *prov, OSSL_PARAM params[])
743 if ((p = OSSL_PARAM_locate(params, "openssl-version")) != NULL)
744 OSSL_PARAM_set_utf8_ptr(p, OPENSSL_VERSION_STR);
745 if ((p = OSSL_PARAM_locate(params, "provider-name")) != NULL)
746 OSSL_PARAM_set_utf8_ptr(p, prov->name);
748 if (prov->parameters == NULL)
751 for (i = 0; i < sk_INFOPAIR_num(prov->parameters); i++) {
752 INFOPAIR *pair = sk_INFOPAIR_value(prov->parameters, i);
754 if ((p = OSSL_PARAM_locate(params, pair->name)) != NULL)
755 OSSL_PARAM_set_utf8_ptr(p, pair->value);
761 static OSSL_core_get_library_context_fn core_get_libctx; /* Check */
762 static OPENSSL_CTX *core_get_libctx(const OSSL_PROVIDER *prov)
767 static int core_thread_start(const OSSL_PROVIDER *prov,
768 OSSL_thread_stop_handler_fn handfn)
770 return ossl_init_thread_start(prov, prov->provctx, handfn);
774 * The FIPS module inner provider doesn't implement these. They aren't
775 * needed there, since the FIPS module upcalls are always the outer provider
779 static void core_put_error(const OSSL_PROVIDER *prov,
780 uint32_t reason, const char *file, int line)
783 * If the uppermost 8 bits are non-zero, it's an OpenSSL library
784 * error and will be treated as such. Otherwise, it's a new style
785 * provider error and will be treated as such.
787 if (ERR_GET_LIB(reason) != 0) {
788 ERR_PUT_error(ERR_GET_LIB(reason),
789 ERR_GET_FUNC(reason),
790 ERR_GET_REASON(reason),
793 ERR_PUT_error(prov->error_lib, 0, (int)reason, file, line);
798 * TODO(3.0) This, as well as core_put_error above, should use |prov|
799 * to select the proper library context to report in the correct error
800 * stack, at least if error stacks become tied to the library context.
801 * We cannot currently do that since there's no support for it in the
804 static void core_add_error_vdata(const OSSL_PROVIDER *prov,
805 int num, va_list args)
807 ERR_add_error_vdata(num, args);
812 * Functions provided by the core. Blank line separates "families" of related
815 static const OSSL_DISPATCH core_dispatch_[] = {
816 { OSSL_FUNC_CORE_GET_PARAM_TYPES, (void (*)(void))core_get_param_types },
817 { OSSL_FUNC_CORE_GET_PARAMS, (void (*)(void))core_get_params },
818 { OSSL_FUNC_CORE_GET_LIBRARY_CONTEXT, (void (*)(void))core_get_libctx },
819 { OSSL_FUNC_CORE_THREAD_START, (void (*)(void))core_thread_start },
821 { OSSL_FUNC_CORE_PUT_ERROR, (void (*)(void))core_put_error },
822 { OSSL_FUNC_CORE_ADD_ERROR_VDATA, (void (*)(void))core_add_error_vdata },
825 { OSSL_FUNC_CRYPTO_MALLOC, (void (*)(void))CRYPTO_malloc },
826 { OSSL_FUNC_CRYPTO_ZALLOC, (void (*)(void))CRYPTO_zalloc },
827 { OSSL_FUNC_CRYPTO_FREE, (void (*)(void))CRYPTO_free },
828 { OSSL_FUNC_CRYPTO_CLEAR_FREE, (void (*)(void))CRYPTO_clear_free },
829 { OSSL_FUNC_CRYPTO_REALLOC, (void (*)(void))CRYPTO_realloc },
830 { OSSL_FUNC_CRYPTO_CLEAR_REALLOC, (void (*)(void))CRYPTO_clear_realloc },
831 { OSSL_FUNC_CRYPTO_SECURE_MALLOC, (void (*)(void))CRYPTO_secure_malloc },
832 { OSSL_FUNC_CRYPTO_SECURE_ZALLOC, (void (*)(void))CRYPTO_secure_zalloc },
833 { OSSL_FUNC_CRYPTO_SECURE_FREE, (void (*)(void))CRYPTO_secure_free },
834 { OSSL_FUNC_CRYPTO_SECURE_CLEAR_FREE,
835 (void (*)(void))CRYPTO_secure_clear_free },
836 { OSSL_FUNC_CRYPTO_SECURE_ALLOCATED,
837 (void (*)(void))CRYPTO_secure_allocated },
838 { OSSL_FUNC_OPENSSL_CLEANSE, (void (*)(void))OPENSSL_cleanse },
842 static const OSSL_DISPATCH *core_dispatch = core_dispatch_;