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.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 struct provider_store_st *store; /* The store this instance belongs to */
52 /* Provider side functions */
53 OSSL_provider_teardown_fn *teardown;
54 OSSL_provider_get_param_types_fn *get_param_types;
55 OSSL_provider_get_params_fn *get_params;
56 OSSL_provider_query_operation_fn *query_operation;
58 /* Provider side data */
61 DEFINE_STACK_OF(OSSL_PROVIDER)
63 static int ossl_provider_cmp(const OSSL_PROVIDER * const *a,
64 const OSSL_PROVIDER * const *b)
66 return strcmp((*a)->name, (*b)->name);
70 * Provider Object store
71 * =====================
73 * The Provider Object store is a library context object, and therefore needs
77 struct provider_store_st {
78 STACK_OF(OSSL_PROVIDER) *providers;
80 unsigned int use_fallbacks:1;
82 static int provider_store_index = -1;
84 static void provider_store_free(void *vstore)
86 struct provider_store_st *store = vstore;
90 sk_OSSL_PROVIDER_pop_free(store->providers, ossl_provider_free);
91 CRYPTO_THREAD_lock_free(store->lock);
95 static void *provider_store_new(void)
97 struct provider_store_st *store = OPENSSL_zalloc(sizeof(*store));
98 const struct predefined_providers_st *p = NULL;
101 || (store->providers = sk_OSSL_PROVIDER_new(ossl_provider_cmp)) == NULL
102 || (store->lock = CRYPTO_THREAD_lock_new()) == NULL) {
103 provider_store_free(store);
106 store->use_fallbacks = 1;
108 for (p = predefined_providers; p->name != NULL; p++) {
109 OSSL_PROVIDER *prov = NULL;
112 * We use the internal constructor directly here,
113 * otherwise we get a call loop
115 prov = provider_new(p->name, p->init);
118 || sk_OSSL_PROVIDER_push(store->providers, prov) == 0) {
119 ossl_provider_free(prov);
120 provider_store_free(store);
121 CRYPTOerr(CRYPTO_F_PROVIDER_STORE_NEW, ERR_R_INTERNAL_ERROR);
126 ossl_provider_set_fallback(prov);
132 static const OPENSSL_CTX_METHOD provider_store_method = {
137 static CRYPTO_ONCE provider_store_init_flag = CRYPTO_ONCE_STATIC_INIT;
138 DEFINE_RUN_ONCE_STATIC(do_provider_store_init)
140 return OPENSSL_init_crypto(0, NULL)
141 && (provider_store_index =
142 openssl_ctx_new_index(&provider_store_method)) != -1;
146 static struct provider_store_st *get_provider_store(OPENSSL_CTX *libctx)
148 struct provider_store_st *store = NULL;
150 if (!RUN_ONCE(&provider_store_init_flag, do_provider_store_init))
153 store = openssl_ctx_get_data(libctx, provider_store_index);
155 CRYPTOerr(CRYPTO_F_GET_PROVIDER_STORE, ERR_R_INTERNAL_ERROR);
159 OSSL_PROVIDER *ossl_provider_find(OPENSSL_CTX *libctx, const char *name)
161 struct provider_store_st *store = NULL;
162 OSSL_PROVIDER *prov = NULL;
164 if ((store = get_provider_store(libctx)) != NULL) {
165 OSSL_PROVIDER tmpl = { 0, };
168 tmpl.name = (char *)name;
169 CRYPTO_THREAD_write_lock(store->lock);
170 if ((i = sk_OSSL_PROVIDER_find(store->providers, &tmpl)) == -1
171 || (prov = sk_OSSL_PROVIDER_value(store->providers, i)) == NULL
172 || !ossl_provider_upref(prov))
174 CRYPTO_THREAD_unlock(store->lock);
181 * Provider Object methods
182 * =======================
185 static OSSL_PROVIDER *provider_new(const char *name,
186 OSSL_provider_init_fn *init_function)
188 OSSL_PROVIDER *prov = NULL;
190 if ((prov = OPENSSL_zalloc(sizeof(*prov))) == NULL
192 || (prov->refcnt_lock = CRYPTO_THREAD_lock_new()) == NULL
194 || !ossl_provider_upref(prov) /* +1 One reference to be returned */
195 || (prov->name = OPENSSL_strdup(name)) == NULL) {
196 ossl_provider_free(prov);
197 CRYPTOerr(CRYPTO_F_PROVIDER_NEW, ERR_R_MALLOC_FAILURE);
201 prov->init_function = init_function;
205 int ossl_provider_upref(OSSL_PROVIDER *prov)
209 CRYPTO_UP_REF(&prov->refcnt, &ref, prov->refcnt_lock);
213 OSSL_PROVIDER *ossl_provider_new(OPENSSL_CTX *libctx, const char *name,
214 OSSL_provider_init_fn *init_function)
216 struct provider_store_st *store = NULL;
217 OSSL_PROVIDER *prov = NULL;
219 if ((store = get_provider_store(libctx)) == NULL)
222 if ((prov = ossl_provider_find(libctx, name)) != NULL) { /* refcount +1 */
223 ossl_provider_free(prov); /* refcount -1 */
224 CRYPTOerr(CRYPTO_F_OSSL_PROVIDER_NEW,
225 CRYPTO_R_PROVIDER_ALREADY_EXISTS);
226 ERR_add_error_data(2, "name=", name);
230 /* provider_new() generates an error, so no need here */
231 if ((prov = provider_new(name, init_function)) == NULL)
234 CRYPTO_THREAD_write_lock(store->lock);
235 if (!ossl_provider_upref(prov)) { /* +1 One reference for the store */
236 ossl_provider_free(prov); /* -1 Reference that was to be returned */
238 } else if (sk_OSSL_PROVIDER_push(store->providers, prov) == 0) {
239 ossl_provider_free(prov); /* -1 Store reference */
240 ossl_provider_free(prov); /* -1 Reference that was to be returned */
245 CRYPTO_THREAD_unlock(store->lock);
248 CRYPTOerr(CRYPTO_F_OSSL_PROVIDER_NEW, ERR_R_MALLOC_FAILURE);
251 * At this point, the provider is only partially "loaded". To be
252 * fully "loaded", ossl_provider_activate() must also be called.
258 static void free_infopair(INFOPAIR *pair)
260 OPENSSL_free(pair->name);
261 OPENSSL_free(pair->value);
265 void ossl_provider_free(OSSL_PROVIDER *prov)
270 CRYPTO_DOWN_REF(&prov->refcnt, &ref, prov->refcnt_lock);
273 * When the refcount drops below two, the store is the only
274 * possible reference, or it has already been taken away from
275 * the store (this may happen if a provider was activated
276 * because it's a fallback, but isn't currently used)
277 * When that happens, the provider is inactivated.
279 if (ref < 2 && prov->flag_initialized) {
280 if (prov->teardown != NULL)
281 prov->teardown(prov->provctx);
282 prov->flag_initialized = 0;
286 * When the refcount drops to zero, it has been taken out of
287 * the store. All we have to do here is clean it out.
290 DSO_free(prov->module);
291 OPENSSL_free(prov->name);
292 OPENSSL_free(prov->path);
293 sk_INFOPAIR_pop_free(prov->parameters, free_infopair);
295 CRYPTO_THREAD_lock_free(prov->refcnt_lock);
303 int ossl_provider_set_module_path(OSSL_PROVIDER *prov, const char *module_path)
305 OPENSSL_free(prov->path);
306 if (module_path == NULL)
308 if ((prov->path = OPENSSL_strdup(module_path)) != NULL)
310 CRYPTOerr(CRYPTO_F_OSSL_PROVIDER_SET_MODULE_PATH, ERR_R_MALLOC_FAILURE);
314 int ossl_provider_add_parameter(OSSL_PROVIDER *prov,
315 const char *name, const char *value)
317 INFOPAIR *pair = NULL;
319 if ((pair = OPENSSL_zalloc(sizeof(*pair))) != NULL
320 && (prov->parameters != NULL
321 || (prov->parameters = sk_INFOPAIR_new_null()) != NULL)
322 && (pair->name = OPENSSL_strdup(name)) != NULL
323 && (pair->value = OPENSSL_strdup(value)) != NULL
324 && sk_INFOPAIR_push(prov->parameters, pair) > 0)
328 OPENSSL_free(pair->name);
329 OPENSSL_free(pair->value);
332 CRYPTOerr(CRYPTO_F_OSSL_PROVIDER_ADD_PARAMETER, ERR_R_MALLOC_FAILURE);
337 * Provider activation.
339 * What "activation" means depends on the provider form; for built in
340 * providers (in the library or the application alike), the provider
341 * can already be considered to be loaded, all that's needed is to
342 * initialize it. However, for dynamically loadable provider modules,
343 * we must first load that module.
345 * Built in modules are distinguished from dynamically loaded modules
346 * with an already assigned init function.
348 static const OSSL_DISPATCH *core_dispatch; /* Define further down */
351 * Internal version that doesn't affect the store flags, and thereby avoid
352 * locking. Direct callers must remember to set the store flags when
355 static int provider_activate(OSSL_PROVIDER *prov)
357 const OSSL_DISPATCH *provider_dispatch = NULL;
359 if (prov->flag_initialized)
363 * If the init function isn't set, it indicates that this provider is
366 if (prov->init_function == NULL) {
367 if (prov->module == NULL) {
368 char *allocated_path = NULL;
369 const char *module_path = NULL;
370 char *merged_path = NULL;
371 const char *load_dir = ossl_safe_getenv("OPENSSL_MODULES");
373 if ((prov->module = DSO_new()) == NULL) {
374 /* DSO_new() generates an error already */
378 if (load_dir == NULL)
379 load_dir = MODULESDIR;
381 DSO_ctrl(prov->module, DSO_CTRL_SET_FLAGS,
382 DSO_FLAG_NAME_TRANSLATION_EXT_ONLY, NULL);
384 module_path = prov->path;
385 if (module_path == NULL)
386 module_path = allocated_path =
387 DSO_convert_filename(prov->module, prov->name);
388 if (module_path != NULL)
389 merged_path = DSO_merge(prov->module, module_path, load_dir);
391 if (merged_path == NULL
392 || (DSO_load(prov->module, merged_path, NULL, 0)) == NULL) {
393 DSO_free(prov->module);
397 OPENSSL_free(merged_path);
398 OPENSSL_free(allocated_path);
401 if (prov->module != NULL)
402 prov->init_function = (OSSL_provider_init_fn *)
403 DSO_bind_func(prov->module, "OSSL_provider_init");
406 if (prov->init_function == NULL
407 || !prov->init_function(prov, core_dispatch, &provider_dispatch,
409 CRYPTOerr(CRYPTO_F_PROVIDER_ACTIVATE, ERR_R_INIT_FAIL);
410 ERR_add_error_data(2, "name=", prov->name);
411 DSO_free(prov->module);
416 for (; provider_dispatch->function_id != 0; provider_dispatch++) {
417 switch (provider_dispatch->function_id) {
418 case OSSL_FUNC_PROVIDER_TEARDOWN:
420 OSSL_get_provider_teardown(provider_dispatch);
422 case OSSL_FUNC_PROVIDER_GET_PARAM_TYPES:
423 prov->get_param_types =
424 OSSL_get_provider_get_param_types(provider_dispatch);
426 case OSSL_FUNC_PROVIDER_GET_PARAMS:
428 OSSL_get_provider_get_params(provider_dispatch);
430 case OSSL_FUNC_PROVIDER_QUERY_OPERATION:
431 prov->query_operation =
432 OSSL_get_provider_query_operation(provider_dispatch);
437 /* With this flag set, this provider has become fully "loaded". */
438 prov->flag_initialized = 1;
443 int ossl_provider_activate(OSSL_PROVIDER *prov)
445 if (provider_activate(prov)) {
446 CRYPTO_THREAD_write_lock(prov->store->lock);
447 prov->store->use_fallbacks = 0;
448 CRYPTO_THREAD_unlock(prov->store->lock);
455 void *ossl_provider_ctx(const OSSL_PROVIDER *prov)
457 return prov->provctx;
461 static int provider_forall_loaded(struct provider_store_st *store,
462 int *found_activated,
463 int (*cb)(OSSL_PROVIDER *provider,
469 int num_provs = sk_OSSL_PROVIDER_num(store->providers);
471 if (found_activated != NULL)
472 *found_activated = 0;
473 for (i = 0; i < num_provs; i++) {
474 OSSL_PROVIDER *prov =
475 sk_OSSL_PROVIDER_value(store->providers, i);
477 if (prov->flag_initialized) {
478 if (found_activated != NULL)
479 *found_activated = 1;
480 if (!(ret = cb(prov, cbdata)))
488 int ossl_provider_forall_loaded(OPENSSL_CTX *ctx,
489 int (*cb)(OSSL_PROVIDER *provider,
495 struct provider_store_st *store = get_provider_store(ctx);
498 int found_activated = 0;
500 CRYPTO_THREAD_read_lock(store->lock);
501 ret = provider_forall_loaded(store, &found_activated, cb, cbdata);
504 * If there's nothing activated ever in this store, try to activate
507 if (!found_activated && store->use_fallbacks) {
508 int num_provs = sk_OSSL_PROVIDER_num(store->providers);
509 int activated_fallback_count = 0;
511 for (i = 0; i < num_provs; i++) {
512 OSSL_PROVIDER *prov =
513 sk_OSSL_PROVIDER_value(store->providers, i);
516 * Note that we don't care if the activation succeeds or
517 * not. If it doesn't succeed, then the next loop will
520 if (prov->flag_fallback) {
521 activated_fallback_count++;
522 provider_activate(prov);
526 if (activated_fallback_count > 0) {
528 * We assume that all fallbacks have been added to the store
529 * before any fallback is activated.
530 * TODO: We may have to reconsider this, IF we find ourselves
531 * adding fallbacks after any previous fallback has been
534 store->use_fallbacks = 0;
537 * Now that we've activated available fallbacks, try a
540 ret = provider_forall_loaded(store, NULL, cb, cbdata);
543 CRYPTO_THREAD_unlock(store->lock);
549 /* Setters of Provider Object data */
550 int ossl_provider_set_fallback(OSSL_PROVIDER *prov)
555 prov->flag_fallback = 1;
559 /* Getters of Provider Object data */
560 const char *ossl_provider_name(OSSL_PROVIDER *prov)
565 const DSO *ossl_provider_dso(OSSL_PROVIDER *prov)
570 const char *ossl_provider_module_name(OSSL_PROVIDER *prov)
572 return DSO_get_filename(prov->module);
575 const char *ossl_provider_module_path(OSSL_PROVIDER *prov)
577 /* FIXME: Ensure it's a full path */
578 return DSO_get_filename(prov->module);
581 /* Wrappers around calls to the provider */
582 void ossl_provider_teardown(const OSSL_PROVIDER *prov)
584 if (prov->teardown != NULL)
585 prov->teardown(prov->provctx);
588 const OSSL_ITEM *ossl_provider_get_param_types(const OSSL_PROVIDER *prov)
590 return prov->get_param_types == NULL
591 ? NULL : prov->get_param_types(prov->provctx);
594 int ossl_provider_get_params(const OSSL_PROVIDER *prov,
595 const OSSL_PARAM params[])
597 return prov->get_params == NULL
598 ? 0 : prov->get_params(prov->provctx, params);
602 const OSSL_ALGORITHM *ossl_provider_query_operation(const OSSL_PROVIDER *prov,
606 return prov->query_operation(prov->provctx, operation_id, no_cache);
610 * Core functions for the provider
611 * ===============================
613 * This is the set of functions that the core makes available to the provider
617 * This returns a list of Provider Object parameters with their types, for
618 * discovery. We do not expect that many providers will use this, but one
621 static const OSSL_ITEM param_types[] = {
622 { OSSL_PARAM_UTF8_PTR, "openssl-version" },
623 { OSSL_PARAM_UTF8_PTR, "provider-name" },
627 static const OSSL_ITEM *core_get_param_types(const OSSL_PROVIDER *prov)
632 static int core_get_params(const OSSL_PROVIDER *prov, const OSSL_PARAM params[])
637 if ((p = OSSL_PARAM_locate(params, "openssl-version")) != NULL)
638 OSSL_PARAM_set_utf8_ptr(p, OPENSSL_VERSION_STR);
639 if ((p = OSSL_PARAM_locate(params, "provider-name")) != NULL)
640 OSSL_PARAM_set_utf8_ptr(p, prov->name);
642 if (prov->parameters == NULL)
645 for (i = 0; i < sk_INFOPAIR_num(prov->parameters); i++) {
646 INFOPAIR *pair = sk_INFOPAIR_value(prov->parameters, i);
648 if ((p = OSSL_PARAM_locate(params, pair->name)) != NULL)
649 OSSL_PARAM_set_utf8_ptr(p, pair->value);
655 static const OSSL_DISPATCH core_dispatch_[] = {
656 { OSSL_FUNC_CORE_GET_PARAM_TYPES, (void (*)(void))core_get_param_types },
657 { OSSL_FUNC_CORE_GET_PARAMS, (void (*)(void))core_get_params },
660 static const OSSL_DISPATCH *core_dispatch = core_dispatch_;