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/opensslv.h>
13 #include "internal/cryptlib.h"
14 #include "internal/thread_once.h"
15 #include "internal/provider.h"
16 #include "internal/refcount.h"
19 * Provider Object structure
20 * =========================
23 struct provider_store_st; /* Forward declaration */
25 struct ossl_provider_st {
27 unsigned int flag_initialized:1;
29 /* OpenSSL library side data */
30 CRYPTO_REF_COUNT refcnt;
31 CRYPTO_RWLOCK *refcnt_lock; /* For the ref counter */
34 OSSL_provider_init_fn *init_function;
36 /* Provider side functions */
37 OSSL_provider_teardown_fn *teardown;
38 OSSL_provider_get_param_types_fn *get_param_types;
39 OSSL_provider_get_params_fn *get_params;
40 OSSL_provider_query_operation_fn *query_operation;
42 DEFINE_STACK_OF(OSSL_PROVIDER)
44 static int ossl_provider_cmp(const OSSL_PROVIDER * const *a,
45 const OSSL_PROVIDER * const *b)
47 return strcmp((*a)->name, (*b)->name);
51 * Provider Object store
52 * =====================
54 * The Provider Object store is a library context object, and therefore needs
58 struct provider_store_st {
59 STACK_OF(OSSL_PROVIDER) *providers;
62 static int provider_store_index = -1;
64 static void provider_store_free(void *vstore)
66 struct provider_store_st *store = vstore;
70 sk_OSSL_PROVIDER_pop_free(store->providers, ossl_provider_free);
71 CRYPTO_THREAD_lock_free(store->lock);
75 static void *provider_store_new(void)
77 struct provider_store_st *store = OPENSSL_zalloc(sizeof(*store));
80 || (store->providers = sk_OSSL_PROVIDER_new(ossl_provider_cmp)) == NULL
81 || (store->lock = CRYPTO_THREAD_lock_new()) == NULL) {
82 provider_store_free(store);
88 static const OPENSSL_CTX_METHOD provider_store_method = {
93 static CRYPTO_ONCE provider_store_init_flag = CRYPTO_ONCE_STATIC_INIT;
94 DEFINE_RUN_ONCE_STATIC(do_provider_store_init)
96 return OPENSSL_init_crypto(0, NULL)
97 && (provider_store_index =
98 openssl_ctx_new_index(&provider_store_method)) != -1;
102 static struct provider_store_st *get_provider_store(OPENSSL_CTX *libctx)
104 struct provider_store_st *store = NULL;
106 if (!RUN_ONCE(&provider_store_init_flag, do_provider_store_init))
109 store = openssl_ctx_get_data(libctx, provider_store_index);
111 CRYPTOerr(CRYPTO_F_GET_PROVIDER_STORE, ERR_R_INTERNAL_ERROR);
116 * Provider Object methods
117 * =======================
120 int ossl_provider_upref(OSSL_PROVIDER *prov)
124 CRYPTO_UP_REF(&prov->refcnt, &ref, prov->refcnt_lock);
128 /* Finder, constructor and destructor */
129 OSSL_PROVIDER *ossl_provider_find(OPENSSL_CTX *libctx, const char *name)
131 struct provider_store_st *store = NULL;
132 OSSL_PROVIDER *prov = NULL;
134 if ((store = get_provider_store(libctx)) != NULL) {
135 OSSL_PROVIDER tmpl = { 0, };
138 tmpl.name = (char *)name;
139 CRYPTO_THREAD_write_lock(store->lock);
140 if ((i = sk_OSSL_PROVIDER_find(store->providers, &tmpl)) == -1
141 || (prov = sk_OSSL_PROVIDER_value(store->providers, i)) == NULL
142 || !ossl_provider_upref(prov))
144 CRYPTO_THREAD_unlock(store->lock);
150 OSSL_PROVIDER *ossl_provider_new(OPENSSL_CTX *libctx, const char *name,
151 OSSL_provider_init_fn *init_function)
153 struct provider_store_st *store = NULL;
154 OSSL_PROVIDER *prov = NULL;
156 if ((store = get_provider_store(libctx)) == NULL)
159 if ((prov = ossl_provider_find(libctx, name)) != NULL) { /* refcount +1 */
160 ossl_provider_free(prov); /* refcount -1 */
161 CRYPTOerr(CRYPTO_F_OSSL_PROVIDER_NEW,
162 CRYPTO_R_PROVIDER_ALREADY_EXISTS);
163 ERR_add_error_data(2, "name=", name);
167 if ((prov = OPENSSL_zalloc(sizeof(*prov))) == NULL
169 || (prov->refcnt_lock = CRYPTO_THREAD_lock_new()) == NULL
171 || !ossl_provider_upref(prov) /* +1 One reference to be returned */
172 || (prov->name = OPENSSL_strdup(name)) == NULL) {
173 ossl_provider_free(prov);
174 CRYPTOerr(CRYPTO_F_OSSL_PROVIDER_NEW, ERR_R_MALLOC_FAILURE);
178 prov->init_function = init_function;
180 CRYPTO_THREAD_write_lock(store->lock);
181 if (!ossl_provider_upref(prov)) { /* +1 One reference for the store */
182 ossl_provider_free(prov); /* -1 Reference that was to be returned */
184 } else if (sk_OSSL_PROVIDER_push(store->providers, prov) == 0) {
185 ossl_provider_free(prov); /* -1 Store reference */
186 ossl_provider_free(prov); /* -1 Reference that was to be returned */
189 CRYPTO_THREAD_unlock(store->lock);
192 CRYPTOerr(CRYPTO_F_OSSL_PROVIDER_NEW, ERR_R_MALLOC_FAILURE);
195 * At this point, the provider is only partially "loaded". To be
196 * fully "loaded", ossl_provider_activate() must also be called.
202 void ossl_provider_free(OSSL_PROVIDER *prov)
207 CRYPTO_DOWN_REF(&prov->refcnt, &ref, prov->refcnt_lock);
210 * When the refcount drops down to one, there is only one reference,
212 * When that happens, the provider is inactivated.
214 if (ref == 1 && prov->flag_initialized) {
215 if (prov->teardown != NULL)
217 prov->flag_initialized = 0;
221 * When the refcount drops to zero, it has been taken out of
222 * the store. All we have to do here is clean it out.
225 DSO_free(prov->module);
226 OPENSSL_free(prov->name);
228 CRYPTO_THREAD_lock_free(prov->refcnt_lock);
236 * Provider activation.
238 * What "activation" means depends on the provider form; for built in
239 * providers (in the library or the application alike), the provider
240 * can already be considered to be loaded, all that's needed is to
241 * initialize it. However, for dynamically loadable provider modules,
242 * we must first load that module.
244 * Built in modules are distinguished from dynamically loaded modules
245 * with an already assigned init function.
247 static const OSSL_DISPATCH *core_dispatch; /* Define further down */
249 int ossl_provider_activate(OSSL_PROVIDER *prov)
251 const OSSL_DISPATCH *provider_dispatch = NULL;
253 if (prov->flag_initialized)
257 * If the init function isn't set, it indicates that this provider is
260 if (prov->init_function == NULL) {
261 if (prov->module == NULL) {
262 char *platform_module_name = NULL;
263 char *module_path = NULL;
264 const char *load_dir = ossl_safe_getenv("OPENSSL_MODULES");
266 if ((prov->module = DSO_new()) == NULL) {
267 /* DSO_new() generates an error already */
271 if (load_dir == NULL)
272 load_dir = MODULESDIR;
274 DSO_ctrl(prov->module, DSO_CTRL_SET_FLAGS,
275 DSO_FLAG_NAME_TRANSLATION_EXT_ONLY, NULL);
276 if ((platform_module_name =
277 DSO_convert_filename(prov->module, prov->name)) == NULL
279 DSO_merge(prov->module, platform_module_name,
281 || DSO_load(prov->module, module_path, NULL,
282 DSO_FLAG_NAME_TRANSLATION_EXT_ONLY) == NULL) {
283 DSO_free(prov->module);
287 OPENSSL_free(platform_module_name);
288 OPENSSL_free(module_path);
291 if (prov->module != NULL)
292 prov->init_function = (OSSL_provider_init_fn *)
293 DSO_bind_func(prov->module, "OSSL_provider_init");
296 if (prov->init_function == NULL
297 || !prov->init_function(prov, core_dispatch, &provider_dispatch)) {
298 CRYPTOerr(CRYPTO_F_OSSL_PROVIDER_ACTIVATE, ERR_R_INIT_FAIL);
299 ERR_add_error_data(2, "name=", prov->name);
300 DSO_free(prov->module);
305 for (; provider_dispatch->function_id != 0; provider_dispatch++) {
306 switch (provider_dispatch->function_id) {
307 case OSSL_FUNC_PROVIDER_TEARDOWN:
309 OSSL_get_provider_teardown(provider_dispatch);
311 case OSSL_FUNC_PROVIDER_GET_PARAM_TYPES:
312 prov->get_param_types =
313 OSSL_get_provider_get_param_types(provider_dispatch);
315 case OSSL_FUNC_PROVIDER_GET_PARAMS:
317 OSSL_get_provider_get_params(provider_dispatch);
319 case OSSL_FUNC_PROVIDER_QUERY_OPERATION:
320 prov->query_operation =
321 OSSL_get_provider_query_operation(provider_dispatch);
326 /* With this flag set, this provider has become fully "loaded". */
327 prov->flag_initialized = 1;
332 int ossl_provider_forall_loaded(OPENSSL_CTX *ctx,
333 int (*cb)(OSSL_PROVIDER *provider,
339 struct provider_store_st *store = get_provider_store(ctx);
342 CRYPTO_THREAD_read_lock(store->lock);
343 for (i = 0; i < sk_OSSL_PROVIDER_num(store->providers); i++) {
344 OSSL_PROVIDER *prov = sk_OSSL_PROVIDER_value(store->providers, i);
346 if (prov->flag_initialized
347 && !(ret = cb(prov, cbdata)))
350 CRYPTO_THREAD_unlock(store->lock);
356 /* Getters of Provider Object data */
357 const char *ossl_provider_name(OSSL_PROVIDER *prov)
362 const DSO *ossl_provider_dso(OSSL_PROVIDER *prov)
367 const char *ossl_provider_module_name(OSSL_PROVIDER *prov)
369 return DSO_get_filename(prov->module);
372 const char *ossl_provider_module_path(OSSL_PROVIDER *prov)
374 /* FIXME: Ensure it's a full path */
375 return DSO_get_filename(prov->module);
378 /* Wrappers around calls to the provider */
379 void ossl_provider_teardown(const OSSL_PROVIDER *prov)
381 if (prov->teardown != NULL)
385 const OSSL_ITEM *ossl_provider_get_param_types(const OSSL_PROVIDER *prov)
387 return prov->get_param_types == NULL ? NULL : prov->get_param_types(prov);
390 int ossl_provider_get_params(const OSSL_PROVIDER *prov,
391 const OSSL_PARAM params[])
393 return prov->get_params == NULL ? 0 : prov->get_params(prov, params);
397 const OSSL_ALGORITHM *ossl_provider_query_operation(const OSSL_PROVIDER *prov,
401 return prov->query_operation(prov, operation_id, no_cache);
405 * Core functions for the provider
406 * ===============================
408 * This is the set of functions that the core makes available to the provider
412 * This returns a list of Provider Object parameters with their types, for
413 * discovery. We do not expect that many providers will use this, but one
416 static const OSSL_ITEM param_types[] = {
417 { OSSL_PARAM_UTF8_PTR, "openssl-version" },
418 { OSSL_PARAM_UTF8_PTR, "provider-name" },
422 static const OSSL_ITEM *core_get_param_types(const OSSL_PROVIDER *prov)
427 static int core_get_params(const OSSL_PROVIDER *prov, const OSSL_PARAM params[])
431 for (i = 0; params[i].key != NULL; i++) {
432 if (strcmp(params[i].key, "openssl-version") == 0) {
433 *(void **)params[i].data = OPENSSL_VERSION_STR;
434 if (params[i].return_size)
435 *params[i].return_size = sizeof(OPENSSL_VERSION_STR);
436 } else if (strcmp(params[i].key, "provider-name") == 0) {
437 *(void **)params[i].data = prov->name;
438 if (params[i].return_size)
439 *params[i].return_size = strlen(prov->name) + 1;
446 static const OSSL_DISPATCH core_dispatch_[] = {
447 { OSSL_FUNC_CORE_GET_PARAM_TYPES, (void (*)(void))core_get_param_types },
448 { OSSL_FUNC_CORE_GET_PARAMS, (void (*)(void))core_get_params },
451 static const OSSL_DISPATCH *core_dispatch = core_dispatch_;