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;
83 static void provider_store_free(void *vstore)
85 struct provider_store_st *store = vstore;
89 sk_OSSL_PROVIDER_pop_free(store->providers, ossl_provider_free);
90 CRYPTO_THREAD_lock_free(store->lock);
94 static void *provider_store_new(OPENSSL_CTX *ctx)
96 struct provider_store_st *store = OPENSSL_zalloc(sizeof(*store));
97 const struct predefined_providers_st *p = NULL;
100 || (store->providers = sk_OSSL_PROVIDER_new(ossl_provider_cmp)) == NULL
101 || (store->lock = CRYPTO_THREAD_lock_new()) == NULL) {
102 provider_store_free(store);
105 store->use_fallbacks = 1;
107 for (p = predefined_providers; p->name != NULL; p++) {
108 OSSL_PROVIDER *prov = NULL;
111 * We use the internal constructor directly here,
112 * otherwise we get a call loop
114 prov = provider_new(p->name, p->init);
117 || sk_OSSL_PROVIDER_push(store->providers, prov) == 0) {
118 ossl_provider_free(prov);
119 provider_store_free(store);
120 CRYPTOerr(CRYPTO_F_PROVIDER_STORE_NEW, ERR_R_INTERNAL_ERROR);
125 ossl_provider_set_fallback(prov);
131 static const OPENSSL_CTX_METHOD provider_store_method = {
136 static struct provider_store_st *get_provider_store(OPENSSL_CTX *libctx)
138 struct provider_store_st *store = NULL;
140 store = openssl_ctx_get_data(libctx, OPENSSL_CTX_PROVIDER_STORE_INDEX,
141 &provider_store_method);
143 CRYPTOerr(CRYPTO_F_GET_PROVIDER_STORE, ERR_R_INTERNAL_ERROR);
147 OSSL_PROVIDER *ossl_provider_find(OPENSSL_CTX *libctx, const char *name)
149 struct provider_store_st *store = NULL;
150 OSSL_PROVIDER *prov = NULL;
152 if ((store = get_provider_store(libctx)) != NULL) {
153 OSSL_PROVIDER tmpl = { 0, };
156 tmpl.name = (char *)name;
157 CRYPTO_THREAD_write_lock(store->lock);
158 if ((i = sk_OSSL_PROVIDER_find(store->providers, &tmpl)) == -1
159 || (prov = sk_OSSL_PROVIDER_value(store->providers, i)) == NULL
160 || !ossl_provider_upref(prov))
162 CRYPTO_THREAD_unlock(store->lock);
169 * Provider Object methods
170 * =======================
173 static OSSL_PROVIDER *provider_new(const char *name,
174 OSSL_provider_init_fn *init_function)
176 OSSL_PROVIDER *prov = NULL;
178 if ((prov = OPENSSL_zalloc(sizeof(*prov))) == NULL
180 || (prov->refcnt_lock = CRYPTO_THREAD_lock_new()) == NULL
182 || !ossl_provider_upref(prov) /* +1 One reference to be returned */
183 || (prov->name = OPENSSL_strdup(name)) == NULL) {
184 ossl_provider_free(prov);
185 CRYPTOerr(CRYPTO_F_PROVIDER_NEW, ERR_R_MALLOC_FAILURE);
189 prov->init_function = init_function;
193 int ossl_provider_upref(OSSL_PROVIDER *prov)
197 CRYPTO_UP_REF(&prov->refcnt, &ref, prov->refcnt_lock);
201 OSSL_PROVIDER *ossl_provider_new(OPENSSL_CTX *libctx, const char *name,
202 OSSL_provider_init_fn *init_function)
204 struct provider_store_st *store = NULL;
205 OSSL_PROVIDER *prov = NULL;
207 if ((store = get_provider_store(libctx)) == NULL)
210 if ((prov = ossl_provider_find(libctx, name)) != NULL) { /* refcount +1 */
211 ossl_provider_free(prov); /* refcount -1 */
212 CRYPTOerr(CRYPTO_F_OSSL_PROVIDER_NEW,
213 CRYPTO_R_PROVIDER_ALREADY_EXISTS);
214 ERR_add_error_data(2, "name=", name);
218 /* provider_new() generates an error, so no need here */
219 if ((prov = provider_new(name, init_function)) == NULL)
222 CRYPTO_THREAD_write_lock(store->lock);
223 if (!ossl_provider_upref(prov)) { /* +1 One reference for the store */
224 ossl_provider_free(prov); /* -1 Reference that was to be returned */
226 } else if (sk_OSSL_PROVIDER_push(store->providers, prov) == 0) {
227 ossl_provider_free(prov); /* -1 Store reference */
228 ossl_provider_free(prov); /* -1 Reference that was to be returned */
233 CRYPTO_THREAD_unlock(store->lock);
236 CRYPTOerr(CRYPTO_F_OSSL_PROVIDER_NEW, ERR_R_MALLOC_FAILURE);
239 * At this point, the provider is only partially "loaded". To be
240 * fully "loaded", ossl_provider_activate() must also be called.
246 static void free_infopair(INFOPAIR *pair)
248 OPENSSL_free(pair->name);
249 OPENSSL_free(pair->value);
253 void ossl_provider_free(OSSL_PROVIDER *prov)
258 CRYPTO_DOWN_REF(&prov->refcnt, &ref, prov->refcnt_lock);
261 * When the refcount drops below two, the store is the only
262 * possible reference, or it has already been taken away from
263 * the store (this may happen if a provider was activated
264 * because it's a fallback, but isn't currently used)
265 * When that happens, the provider is inactivated.
267 if (ref < 2 && prov->flag_initialized) {
268 if (prov->teardown != NULL)
269 prov->teardown(prov->provctx);
270 prov->flag_initialized = 0;
274 * When the refcount drops to zero, it has been taken out of
275 * the store. All we have to do here is clean it out.
279 DSO_free(prov->module);
281 OPENSSL_free(prov->name);
282 OPENSSL_free(prov->path);
283 sk_INFOPAIR_pop_free(prov->parameters, free_infopair);
285 CRYPTO_THREAD_lock_free(prov->refcnt_lock);
293 int ossl_provider_set_module_path(OSSL_PROVIDER *prov, const char *module_path)
295 OPENSSL_free(prov->path);
296 if (module_path == NULL)
298 if ((prov->path = OPENSSL_strdup(module_path)) != NULL)
300 CRYPTOerr(CRYPTO_F_OSSL_PROVIDER_SET_MODULE_PATH, ERR_R_MALLOC_FAILURE);
304 int ossl_provider_add_parameter(OSSL_PROVIDER *prov,
305 const char *name, const char *value)
307 INFOPAIR *pair = NULL;
309 if ((pair = OPENSSL_zalloc(sizeof(*pair))) != NULL
310 && (prov->parameters != NULL
311 || (prov->parameters = sk_INFOPAIR_new_null()) != NULL)
312 && (pair->name = OPENSSL_strdup(name)) != NULL
313 && (pair->value = OPENSSL_strdup(value)) != NULL
314 && sk_INFOPAIR_push(prov->parameters, pair) > 0)
318 OPENSSL_free(pair->name);
319 OPENSSL_free(pair->value);
322 CRYPTOerr(CRYPTO_F_OSSL_PROVIDER_ADD_PARAMETER, ERR_R_MALLOC_FAILURE);
327 * Provider activation.
329 * What "activation" means depends on the provider form; for built in
330 * providers (in the library or the application alike), the provider
331 * can already be considered to be loaded, all that's needed is to
332 * initialize it. However, for dynamically loadable provider modules,
333 * we must first load that module.
335 * Built in modules are distinguished from dynamically loaded modules
336 * with an already assigned init function.
338 static const OSSL_DISPATCH *core_dispatch; /* Define further down */
341 * Internal version that doesn't affect the store flags, and thereby avoid
342 * locking. Direct callers must remember to set the store flags when
345 static int provider_activate(OSSL_PROVIDER *prov)
347 const OSSL_DISPATCH *provider_dispatch = NULL;
349 if (prov->flag_initialized)
353 * If the init function isn't set, it indicates that this provider is
356 if (prov->init_function == NULL) {
360 if (prov->module == NULL) {
361 char *allocated_path = NULL;
362 const char *module_path = NULL;
363 char *merged_path = NULL;
364 const char *load_dir = ossl_safe_getenv("OPENSSL_MODULES");
366 if ((prov->module = DSO_new()) == NULL) {
367 /* DSO_new() generates an error already */
371 if (load_dir == NULL)
372 load_dir = MODULESDIR;
374 DSO_ctrl(prov->module, DSO_CTRL_SET_FLAGS,
375 DSO_FLAG_NAME_TRANSLATION_EXT_ONLY, NULL);
377 module_path = prov->path;
378 if (module_path == NULL)
379 module_path = allocated_path =
380 DSO_convert_filename(prov->module, prov->name);
381 if (module_path != NULL)
382 merged_path = DSO_merge(prov->module, module_path, load_dir);
384 if (merged_path == NULL
385 || (DSO_load(prov->module, merged_path, NULL, 0)) == NULL) {
386 DSO_free(prov->module);
390 OPENSSL_free(merged_path);
391 OPENSSL_free(allocated_path);
394 if (prov->module != NULL)
395 prov->init_function = (OSSL_provider_init_fn *)
396 DSO_bind_func(prov->module, "OSSL_provider_init");
400 if (prov->init_function == NULL
401 || !prov->init_function(prov, core_dispatch, &provider_dispatch,
403 CRYPTOerr(CRYPTO_F_PROVIDER_ACTIVATE, ERR_R_INIT_FAIL);
404 ERR_add_error_data(2, "name=", prov->name);
406 DSO_free(prov->module);
412 for (; provider_dispatch->function_id != 0; provider_dispatch++) {
413 switch (provider_dispatch->function_id) {
414 case OSSL_FUNC_PROVIDER_TEARDOWN:
416 OSSL_get_provider_teardown(provider_dispatch);
418 case OSSL_FUNC_PROVIDER_GET_PARAM_TYPES:
419 prov->get_param_types =
420 OSSL_get_provider_get_param_types(provider_dispatch);
422 case OSSL_FUNC_PROVIDER_GET_PARAMS:
424 OSSL_get_provider_get_params(provider_dispatch);
426 case OSSL_FUNC_PROVIDER_QUERY_OPERATION:
427 prov->query_operation =
428 OSSL_get_provider_query_operation(provider_dispatch);
433 /* With this flag set, this provider has become fully "loaded". */
434 prov->flag_initialized = 1;
439 int ossl_provider_activate(OSSL_PROVIDER *prov)
441 if (provider_activate(prov)) {
442 CRYPTO_THREAD_write_lock(prov->store->lock);
443 prov->store->use_fallbacks = 0;
444 CRYPTO_THREAD_unlock(prov->store->lock);
451 void *ossl_provider_ctx(const OSSL_PROVIDER *prov)
453 return prov->provctx;
457 static int provider_forall_loaded(struct provider_store_st *store,
458 int *found_activated,
459 int (*cb)(OSSL_PROVIDER *provider,
465 int num_provs = sk_OSSL_PROVIDER_num(store->providers);
467 if (found_activated != NULL)
468 *found_activated = 0;
469 for (i = 0; i < num_provs; i++) {
470 OSSL_PROVIDER *prov =
471 sk_OSSL_PROVIDER_value(store->providers, i);
473 if (prov->flag_initialized) {
474 if (found_activated != NULL)
475 *found_activated = 1;
476 if (!(ret = cb(prov, cbdata)))
484 int ossl_provider_forall_loaded(OPENSSL_CTX *ctx,
485 int (*cb)(OSSL_PROVIDER *provider,
491 struct provider_store_st *store = get_provider_store(ctx);
494 int found_activated = 0;
496 CRYPTO_THREAD_read_lock(store->lock);
497 ret = provider_forall_loaded(store, &found_activated, cb, cbdata);
500 * If there's nothing activated ever in this store, try to activate
503 if (!found_activated && store->use_fallbacks) {
504 int num_provs = sk_OSSL_PROVIDER_num(store->providers);
505 int activated_fallback_count = 0;
507 for (i = 0; i < num_provs; i++) {
508 OSSL_PROVIDER *prov =
509 sk_OSSL_PROVIDER_value(store->providers, i);
512 * Note that we don't care if the activation succeeds or
513 * not. If it doesn't succeed, then the next loop will
516 if (prov->flag_fallback) {
517 activated_fallback_count++;
518 provider_activate(prov);
522 if (activated_fallback_count > 0) {
524 * We assume that all fallbacks have been added to the store
525 * before any fallback is activated.
526 * TODO: We may have to reconsider this, IF we find ourselves
527 * adding fallbacks after any previous fallback has been
530 store->use_fallbacks = 0;
533 * Now that we've activated available fallbacks, try a
536 ret = provider_forall_loaded(store, NULL, cb, cbdata);
539 CRYPTO_THREAD_unlock(store->lock);
545 /* Setters of Provider Object data */
546 int ossl_provider_set_fallback(OSSL_PROVIDER *prov)
551 prov->flag_fallback = 1;
555 /* Getters of Provider Object data */
556 const char *ossl_provider_name(OSSL_PROVIDER *prov)
561 const DSO *ossl_provider_dso(OSSL_PROVIDER *prov)
566 const char *ossl_provider_module_name(OSSL_PROVIDER *prov)
571 return DSO_get_filename(prov->module);
575 const char *ossl_provider_module_path(OSSL_PROVIDER *prov)
580 /* FIXME: Ensure it's a full path */
581 return DSO_get_filename(prov->module);
585 /* Wrappers around calls to the provider */
586 void ossl_provider_teardown(const OSSL_PROVIDER *prov)
588 if (prov->teardown != NULL)
589 prov->teardown(prov->provctx);
592 const OSSL_ITEM *ossl_provider_get_param_types(const OSSL_PROVIDER *prov)
594 return prov->get_param_types == NULL
595 ? NULL : prov->get_param_types(prov->provctx);
598 int ossl_provider_get_params(const OSSL_PROVIDER *prov,
599 const OSSL_PARAM params[])
601 return prov->get_params == NULL
602 ? 0 : prov->get_params(prov->provctx, params);
606 const OSSL_ALGORITHM *ossl_provider_query_operation(const OSSL_PROVIDER *prov,
610 return prov->query_operation(prov->provctx, operation_id, no_cache);
614 * Core functions for the provider
615 * ===============================
617 * This is the set of functions that the core makes available to the provider
621 * This returns a list of Provider Object parameters with their types, for
622 * discovery. We do not expect that many providers will use this, but one
625 static const OSSL_ITEM param_types[] = {
626 { OSSL_PARAM_UTF8_PTR, "openssl-version" },
627 { OSSL_PARAM_UTF8_PTR, "provider-name" },
631 static const OSSL_ITEM *core_get_param_types(const OSSL_PROVIDER *prov)
636 static int core_get_params(const OSSL_PROVIDER *prov, const OSSL_PARAM params[])
641 if ((p = OSSL_PARAM_locate(params, "openssl-version")) != NULL)
642 OSSL_PARAM_set_utf8_ptr(p, OPENSSL_VERSION_STR);
643 if ((p = OSSL_PARAM_locate(params, "provider-name")) != NULL)
644 OSSL_PARAM_set_utf8_ptr(p, prov->name);
646 if (prov->parameters == NULL)
649 for (i = 0; i < sk_INFOPAIR_num(prov->parameters); i++) {
650 INFOPAIR *pair = sk_INFOPAIR_value(prov->parameters, i);
652 if ((p = OSSL_PARAM_locate(params, pair->name)) != NULL)
653 OSSL_PARAM_set_utf8_ptr(p, pair->value);
659 static const OSSL_DISPATCH core_dispatch_[] = {
660 { OSSL_FUNC_CORE_GET_PARAM_TYPES, (void (*)(void))core_get_param_types },
661 { OSSL_FUNC_CORE_GET_PARAMS, (void (*)(void))core_get_params },
662 { OSSL_FUNC_CORE_PUT_ERROR, (void (*)(void))ERR_put_error },
663 { OSSL_FUNC_CORE_ADD_ERROR_VDATA, (void (*)(void))ERR_add_error_vdata },
666 static const OSSL_DISPATCH *core_dispatch = core_dispatch_;