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 */
52 int error_lib; /* ERR library number, one for each provider */
53 #ifndef OPENSSL_NO_ERR
54 ERR_STRING_DATA *error_strings; /* Copy of what the provider gives us */
57 /* Provider side functions */
58 OSSL_provider_teardown_fn *teardown;
59 OSSL_provider_get_param_types_fn *get_param_types;
60 OSSL_provider_get_params_fn *get_params;
61 OSSL_provider_query_operation_fn *query_operation;
63 /* Provider side data */
66 DEFINE_STACK_OF(OSSL_PROVIDER)
68 static int ossl_provider_cmp(const OSSL_PROVIDER * const *a,
69 const OSSL_PROVIDER * const *b)
71 return strcmp((*a)->name, (*b)->name);
75 * Provider Object store
76 * =====================
78 * The Provider Object store is a library context object, and therefore needs
82 struct provider_store_st {
83 STACK_OF(OSSL_PROVIDER) *providers;
85 unsigned int use_fallbacks:1;
88 static void provider_store_free(void *vstore)
90 struct provider_store_st *store = vstore;
94 sk_OSSL_PROVIDER_pop_free(store->providers, ossl_provider_free);
95 CRYPTO_THREAD_lock_free(store->lock);
99 static void *provider_store_new(OPENSSL_CTX *ctx)
101 struct provider_store_st *store = OPENSSL_zalloc(sizeof(*store));
102 const struct predefined_providers_st *p = NULL;
105 || (store->providers = sk_OSSL_PROVIDER_new(ossl_provider_cmp)) == NULL
106 || (store->lock = CRYPTO_THREAD_lock_new()) == NULL) {
107 provider_store_free(store);
110 store->use_fallbacks = 1;
112 for (p = predefined_providers; p->name != NULL; p++) {
113 OSSL_PROVIDER *prov = NULL;
116 * We use the internal constructor directly here,
117 * otherwise we get a call loop
119 prov = provider_new(p->name, p->init);
122 || sk_OSSL_PROVIDER_push(store->providers, prov) == 0) {
123 ossl_provider_free(prov);
124 provider_store_free(store);
125 CRYPTOerr(CRYPTO_F_PROVIDER_STORE_NEW, ERR_R_INTERNAL_ERROR);
130 prov->error_lib = ERR_get_next_error_library();
132 ossl_provider_set_fallback(prov);
138 static const OPENSSL_CTX_METHOD provider_store_method = {
143 static struct provider_store_st *get_provider_store(OPENSSL_CTX *libctx)
145 struct provider_store_st *store = NULL;
147 store = openssl_ctx_get_data(libctx, OPENSSL_CTX_PROVIDER_STORE_INDEX,
148 &provider_store_method);
150 CRYPTOerr(CRYPTO_F_GET_PROVIDER_STORE, ERR_R_INTERNAL_ERROR);
154 OSSL_PROVIDER *ossl_provider_find(OPENSSL_CTX *libctx, const char *name)
156 struct provider_store_st *store = NULL;
157 OSSL_PROVIDER *prov = NULL;
159 if ((store = get_provider_store(libctx)) != NULL) {
160 OSSL_PROVIDER tmpl = { 0, };
163 tmpl.name = (char *)name;
164 CRYPTO_THREAD_write_lock(store->lock);
165 if ((i = sk_OSSL_PROVIDER_find(store->providers, &tmpl)) == -1
166 || (prov = sk_OSSL_PROVIDER_value(store->providers, i)) == NULL
167 || !ossl_provider_up_ref(prov))
169 CRYPTO_THREAD_unlock(store->lock);
176 * Provider Object methods
177 * =======================
180 static OSSL_PROVIDER *provider_new(const char *name,
181 OSSL_provider_init_fn *init_function)
183 OSSL_PROVIDER *prov = NULL;
185 if ((prov = OPENSSL_zalloc(sizeof(*prov))) == NULL
187 || (prov->refcnt_lock = CRYPTO_THREAD_lock_new()) == NULL
189 || !ossl_provider_up_ref(prov) /* +1 One reference to be returned */
190 || (prov->name = OPENSSL_strdup(name)) == NULL) {
191 ossl_provider_free(prov);
192 CRYPTOerr(CRYPTO_F_PROVIDER_NEW, ERR_R_MALLOC_FAILURE);
196 prov->init_function = init_function;
200 int ossl_provider_up_ref(OSSL_PROVIDER *prov)
204 if (CRYPTO_UP_REF(&prov->refcnt, &ref, prov->refcnt_lock) <= 0)
209 OSSL_PROVIDER *ossl_provider_new(OPENSSL_CTX *libctx, const char *name,
210 OSSL_provider_init_fn *init_function)
212 struct provider_store_st *store = NULL;
213 OSSL_PROVIDER *prov = NULL;
215 if ((store = get_provider_store(libctx)) == NULL)
218 if ((prov = ossl_provider_find(libctx, name)) != NULL) { /* refcount +1 */
219 ossl_provider_free(prov); /* refcount -1 */
220 CRYPTOerr(CRYPTO_F_OSSL_PROVIDER_NEW,
221 CRYPTO_R_PROVIDER_ALREADY_EXISTS);
222 ERR_add_error_data(2, "name=", name);
226 /* provider_new() generates an error, so no need here */
227 if ((prov = provider_new(name, init_function)) == NULL)
230 CRYPTO_THREAD_write_lock(store->lock);
231 if (!ossl_provider_up_ref(prov)) { /* +1 One reference for the store */
232 ossl_provider_free(prov); /* -1 Reference that was to be returned */
234 } else if (sk_OSSL_PROVIDER_push(store->providers, prov) == 0) {
235 ossl_provider_free(prov); /* -1 Store reference */
236 ossl_provider_free(prov); /* -1 Reference that was to be returned */
239 prov->libctx = libctx;
241 prov->error_lib = ERR_get_next_error_library();
243 CRYPTO_THREAD_unlock(store->lock);
246 CRYPTOerr(CRYPTO_F_OSSL_PROVIDER_NEW, ERR_R_MALLOC_FAILURE);
249 * At this point, the provider is only partially "loaded". To be
250 * fully "loaded", ossl_provider_activate() must also be called.
256 static void free_infopair(INFOPAIR *pair)
258 OPENSSL_free(pair->name);
259 OPENSSL_free(pair->value);
263 void ossl_provider_free(OSSL_PROVIDER *prov)
268 CRYPTO_DOWN_REF(&prov->refcnt, &ref, prov->refcnt_lock);
271 * When the refcount drops below two, the store is the only
272 * possible reference, or it has already been taken away from
273 * the store (this may happen if a provider was activated
274 * because it's a fallback, but isn't currently used)
275 * When that happens, the provider is inactivated.
277 if (ref < 2 && prov->flag_initialized) {
279 ossl_init_thread_deregister(prov);
281 if (prov->teardown != NULL)
282 prov->teardown(prov->provctx);
283 #ifndef OPENSSL_NO_ERR
285 if (prov->error_strings != NULL) {
286 ERR_unload_strings(prov->error_lib, prov->error_strings);
287 OPENSSL_free(prov->error_strings);
288 prov->error_strings = NULL;
292 prov->flag_initialized = 0;
296 * When the refcount drops to zero, it has been taken out of
297 * the store. All we have to do here is clean it out.
301 DSO_free(prov->module);
303 OPENSSL_free(prov->name);
304 OPENSSL_free(prov->path);
305 sk_INFOPAIR_pop_free(prov->parameters, free_infopair);
307 CRYPTO_THREAD_lock_free(prov->refcnt_lock);
315 int ossl_provider_set_module_path(OSSL_PROVIDER *prov, const char *module_path)
317 OPENSSL_free(prov->path);
318 if (module_path == NULL)
320 if ((prov->path = OPENSSL_strdup(module_path)) != NULL)
322 CRYPTOerr(CRYPTO_F_OSSL_PROVIDER_SET_MODULE_PATH, ERR_R_MALLOC_FAILURE);
326 int ossl_provider_add_parameter(OSSL_PROVIDER *prov,
327 const char *name, const char *value)
329 INFOPAIR *pair = NULL;
331 if ((pair = OPENSSL_zalloc(sizeof(*pair))) != NULL
332 && (prov->parameters != NULL
333 || (prov->parameters = sk_INFOPAIR_new_null()) != NULL)
334 && (pair->name = OPENSSL_strdup(name)) != NULL
335 && (pair->value = OPENSSL_strdup(value)) != NULL
336 && sk_INFOPAIR_push(prov->parameters, pair) > 0)
340 OPENSSL_free(pair->name);
341 OPENSSL_free(pair->value);
344 CRYPTOerr(CRYPTO_F_OSSL_PROVIDER_ADD_PARAMETER, ERR_R_MALLOC_FAILURE);
349 * Provider activation.
351 * What "activation" means depends on the provider form; for built in
352 * providers (in the library or the application alike), the provider
353 * can already be considered to be loaded, all that's needed is to
354 * initialize it. However, for dynamically loadable provider modules,
355 * we must first load that module.
357 * Built in modules are distinguished from dynamically loaded modules
358 * with an already assigned init function.
360 static const OSSL_DISPATCH *core_dispatch; /* Define further down */
363 * Internal version that doesn't affect the store flags, and thereby avoid
364 * locking. Direct callers must remember to set the store flags when
367 static int provider_activate(OSSL_PROVIDER *prov)
369 const OSSL_DISPATCH *provider_dispatch = NULL;
370 #ifndef OPENSSL_NO_ERR
371 OSSL_provider_get_reason_strings_fn *p_get_reason_strings = NULL;
374 if (prov->flag_initialized)
378 * If the init function isn't set, it indicates that this provider is
381 if (prov->init_function == NULL) {
385 if (prov->module == NULL) {
386 char *allocated_path = NULL;
387 const char *module_path = NULL;
388 char *merged_path = NULL;
389 const char *load_dir = ossl_safe_getenv("OPENSSL_MODULES");
391 if ((prov->module = DSO_new()) == NULL) {
392 /* DSO_new() generates an error already */
396 if (load_dir == NULL)
397 load_dir = MODULESDIR;
399 DSO_ctrl(prov->module, DSO_CTRL_SET_FLAGS,
400 DSO_FLAG_NAME_TRANSLATION_EXT_ONLY, NULL);
402 module_path = prov->path;
403 if (module_path == NULL)
404 module_path = allocated_path =
405 DSO_convert_filename(prov->module, prov->name);
406 if (module_path != NULL)
407 merged_path = DSO_merge(prov->module, module_path, load_dir);
409 if (merged_path == NULL
410 || (DSO_load(prov->module, merged_path, NULL, 0)) == NULL) {
411 DSO_free(prov->module);
415 OPENSSL_free(merged_path);
416 OPENSSL_free(allocated_path);
419 if (prov->module != NULL)
420 prov->init_function = (OSSL_provider_init_fn *)
421 DSO_bind_func(prov->module, "OSSL_provider_init");
425 /* Call the initialise function for the provider. */
426 if (prov->init_function == NULL
427 || !prov->init_function(prov, core_dispatch, &provider_dispatch,
429 CRYPTOerr(CRYPTO_F_PROVIDER_ACTIVATE, ERR_R_INIT_FAIL);
430 ERR_add_error_data(2, "name=", prov->name);
432 DSO_free(prov->module);
438 for (; provider_dispatch->function_id != 0; provider_dispatch++) {
439 switch (provider_dispatch->function_id) {
440 case OSSL_FUNC_PROVIDER_TEARDOWN:
442 OSSL_get_provider_teardown(provider_dispatch);
444 case OSSL_FUNC_PROVIDER_GET_PARAM_TYPES:
445 prov->get_param_types =
446 OSSL_get_provider_get_param_types(provider_dispatch);
448 case OSSL_FUNC_PROVIDER_GET_PARAMS:
450 OSSL_get_provider_get_params(provider_dispatch);
452 case OSSL_FUNC_PROVIDER_QUERY_OPERATION:
453 prov->query_operation =
454 OSSL_get_provider_query_operation(provider_dispatch);
456 #ifndef OPENSSL_NO_ERR
457 case OSSL_FUNC_PROVIDER_GET_REASON_STRINGS:
458 p_get_reason_strings =
459 OSSL_get_provider_get_reason_strings(provider_dispatch);
465 #ifndef OPENSSL_NO_ERR
466 if (p_get_reason_strings != NULL) {
467 const OSSL_ITEM *reasonstrings = p_get_reason_strings(prov->provctx);
471 * ERR_load_strings() handles ERR_STRING_DATA rather than OSSL_ITEM,
472 * although they are essentially the same type.
473 * Furthermore, ERR_load_strings() patches the array's error number
474 * with the error library number, so we need to make a copy of that
477 cnt = 1; /* One for the terminating item */
478 while (reasonstrings[cnt].id != 0) {
479 if (ERR_GET_LIB(reasonstrings[cnt].id) != 0)
484 /* Allocate one extra item for the "library" name */
485 prov->error_strings =
486 OPENSSL_zalloc(sizeof(ERR_STRING_DATA) * (cnt + 1));
487 if (prov->error_strings == NULL)
491 * Set the "library" name.
493 prov->error_strings[0].error = ERR_PACK(prov->error_lib, 0, 0);
494 prov->error_strings[0].string = prov->name;
496 * Copy reasonstrings item 0..cnt-1 to prov->error_trings positions
499 for (cnt2 = 1; cnt2 <= cnt; cnt2++) {
500 prov->error_strings[cnt2].error = (int)reasonstrings[cnt2-1].id;
501 prov->error_strings[cnt2].string = reasonstrings[cnt2-1].ptr;
504 ERR_load_strings(prov->error_lib, prov->error_strings);
508 /* With this flag set, this provider has become fully "loaded". */
509 prov->flag_initialized = 1;
514 int ossl_provider_activate(OSSL_PROVIDER *prov)
516 if (provider_activate(prov)) {
517 CRYPTO_THREAD_write_lock(prov->store->lock);
518 prov->store->use_fallbacks = 0;
519 CRYPTO_THREAD_unlock(prov->store->lock);
526 void *ossl_provider_ctx(const OSSL_PROVIDER *prov)
528 return prov->provctx;
532 static int provider_forall_loaded(struct provider_store_st *store,
533 int *found_activated,
534 int (*cb)(OSSL_PROVIDER *provider,
540 int num_provs = sk_OSSL_PROVIDER_num(store->providers);
542 if (found_activated != NULL)
543 *found_activated = 0;
544 for (i = 0; i < num_provs; i++) {
545 OSSL_PROVIDER *prov =
546 sk_OSSL_PROVIDER_value(store->providers, i);
548 if (prov->flag_initialized) {
549 if (found_activated != NULL)
550 *found_activated = 1;
551 if (!(ret = cb(prov, cbdata)))
559 int ossl_provider_forall_loaded(OPENSSL_CTX *ctx,
560 int (*cb)(OSSL_PROVIDER *provider,
566 struct provider_store_st *store = get_provider_store(ctx);
569 int found_activated = 0;
571 CRYPTO_THREAD_read_lock(store->lock);
572 ret = provider_forall_loaded(store, &found_activated, cb, cbdata);
575 * If there's nothing activated ever in this store, try to activate
578 if (!found_activated && store->use_fallbacks) {
579 int num_provs = sk_OSSL_PROVIDER_num(store->providers);
580 int activated_fallback_count = 0;
582 for (i = 0; i < num_provs; i++) {
583 OSSL_PROVIDER *prov =
584 sk_OSSL_PROVIDER_value(store->providers, i);
587 * Note that we don't care if the activation succeeds or
588 * not. If it doesn't succeed, then the next loop will
591 if (prov->flag_fallback) {
592 activated_fallback_count++;
593 provider_activate(prov);
597 if (activated_fallback_count > 0) {
599 * We assume that all fallbacks have been added to the store
600 * before any fallback is activated.
601 * TODO: We may have to reconsider this, IF we find ourselves
602 * adding fallbacks after any previous fallback has been
605 store->use_fallbacks = 0;
608 * Now that we've activated available fallbacks, try a
611 ret = provider_forall_loaded(store, NULL, cb, cbdata);
614 CRYPTO_THREAD_unlock(store->lock);
620 /* Setters of Provider Object data */
621 int ossl_provider_set_fallback(OSSL_PROVIDER *prov)
626 prov->flag_fallback = 1;
630 /* Getters of Provider Object data */
631 const char *ossl_provider_name(const OSSL_PROVIDER *prov)
636 const DSO *ossl_provider_dso(const OSSL_PROVIDER *prov)
641 const char *ossl_provider_module_name(const OSSL_PROVIDER *prov)
646 return DSO_get_filename(prov->module);
650 const char *ossl_provider_module_path(const OSSL_PROVIDER *prov)
655 /* FIXME: Ensure it's a full path */
656 return DSO_get_filename(prov->module);
660 /* Wrappers around calls to the provider */
661 void ossl_provider_teardown(const OSSL_PROVIDER *prov)
663 if (prov->teardown != NULL)
664 prov->teardown(prov->provctx);
667 const OSSL_ITEM *ossl_provider_get_param_types(const OSSL_PROVIDER *prov)
669 return prov->get_param_types == NULL
670 ? NULL : prov->get_param_types(prov->provctx);
673 int ossl_provider_get_params(const OSSL_PROVIDER *prov, OSSL_PARAM params[])
675 return prov->get_params == NULL
676 ? 0 : prov->get_params(prov->provctx, params);
680 const OSSL_ALGORITHM *ossl_provider_query_operation(const OSSL_PROVIDER *prov,
684 return prov->query_operation(prov->provctx, operation_id, no_cache);
688 * Core functions for the provider
689 * ===============================
691 * This is the set of functions that the core makes available to the provider
695 * This returns a list of Provider Object parameters with their types, for
696 * discovery. We do not expect that many providers will use this, but one
699 static const OSSL_ITEM param_types[] = {
700 { OSSL_PARAM_UTF8_PTR, "openssl-version" },
701 { OSSL_PARAM_UTF8_PTR, "provider-name" },
705 static const OSSL_ITEM *core_get_param_types(const OSSL_PROVIDER *prov)
710 static int core_get_params(const OSSL_PROVIDER *prov, OSSL_PARAM params[])
715 if ((p = OSSL_PARAM_locate(params, "openssl-version")) != NULL)
716 OSSL_PARAM_set_utf8_ptr(p, OPENSSL_VERSION_STR);
717 if ((p = OSSL_PARAM_locate(params, "provider-name")) != NULL)
718 OSSL_PARAM_set_utf8_ptr(p, prov->name);
720 if (prov->parameters == NULL)
723 for (i = 0; i < sk_INFOPAIR_num(prov->parameters); i++) {
724 INFOPAIR *pair = sk_INFOPAIR_value(prov->parameters, i);
726 if ((p = OSSL_PARAM_locate(params, pair->name)) != NULL)
727 OSSL_PARAM_set_utf8_ptr(p, pair->value);
733 static OSSL_core_get_library_context_fn core_get_libctx; /* Check */
734 static OPENSSL_CTX *core_get_libctx(const OSSL_PROVIDER *prov)
739 static int core_thread_start(const OSSL_PROVIDER *prov,
740 OSSL_thread_stop_handler_fn handfn)
742 return ossl_init_thread_start(prov, prov->provctx, handfn);
745 static void core_put_error(const OSSL_PROVIDER *prov,
746 uint32_t reason, const char *file, int line)
749 * If the uppermost 8 bits are non-zero, it's an OpenSSL library
750 * error and will be treated as such. Otherwise, it's a new style
751 * provider error and will be treated as such.
753 if (ERR_GET_LIB(reason) != 0) {
754 ERR_PUT_error(ERR_GET_LIB(reason),
755 ERR_GET_FUNC(reason),
756 ERR_GET_REASON(reason),
759 ERR_PUT_error(prov->error_lib, 0, (int)reason, file, line);
764 * TODO(3.0) This, as well as core_put_error above, should use |prov|
765 * to select the proper library context to report in the correct error
766 * stack, at least if error stacks become tied to the library context.
767 * We cannot currently do that since there's no support for it in the
770 static void core_add_error_vdata(const OSSL_PROVIDER *prov,
771 int num, va_list args)
773 ERR_add_error_vdata(num, args);
776 static const OSSL_DISPATCH core_dispatch_[] = {
777 { OSSL_FUNC_CORE_GET_PARAM_TYPES, (void (*)(void))core_get_param_types },
778 { OSSL_FUNC_CORE_GET_PARAMS, (void (*)(void))core_get_params },
779 { OSSL_FUNC_CORE_GET_LIBRARY_CONTEXT, (void (*)(void))core_get_libctx },
780 { OSSL_FUNC_CORE_THREAD_START, (void (*)(void))core_thread_start },
781 { OSSL_FUNC_CORE_PUT_ERROR, (void (*)(void))core_put_error },
782 { OSSL_FUNC_CORE_ADD_ERROR_VDATA, (void (*)(void))core_add_error_vdata },
785 static const OSSL_DISPATCH *core_dispatch = core_dispatch_;