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;
32 CRYPTO_RWLOCK refcnt_lock; /* For the ref counter */
36 OSSL_provider_init_fn *init_function;
38 /* Provider side functions */
39 OSSL_provider_teardown_fn *teardown;
40 OSSL_provider_get_param_types_fn *get_param_types;
41 OSSL_provider_get_params_fn *get_params;
43 DEFINE_STACK_OF(OSSL_PROVIDER)
45 static int ossl_provider_cmp(const OSSL_PROVIDER * const *a,
46 const OSSL_PROVIDER * const *b)
48 return strcmp((*a)->name, (*b)->name);
52 * Provider Object store
53 * =====================
55 * The Provider Object store is a library context object, and therefore needs
59 struct provider_store_st {
60 STACK_OF(OSSL_PROVIDER) *providers;
63 static int provider_store_index = -1;
65 static void provider_store_free(void *vstore)
67 struct provider_store_st *store = vstore;
71 sk_OSSL_PROVIDER_pop_free(store->providers, ossl_provider_free);
72 CRYPTO_THREAD_lock_free(store->lock);
76 static void *provider_store_new(void)
78 struct provider_store_st *store = OPENSSL_zalloc(sizeof(*store));
81 || (store->providers = sk_OSSL_PROVIDER_new(ossl_provider_cmp)) == NULL
82 || (store->lock = CRYPTO_THREAD_lock_new()) == NULL) {
83 provider_store_free(store);
89 static const OPENSSL_CTX_METHOD provider_store_method = {
94 static CRYPTO_ONCE provider_store_init_flag = CRYPTO_ONCE_STATIC_INIT;
95 DEFINE_RUN_ONCE_STATIC(do_provider_store_init)
97 return OPENSSL_init_crypto(0, NULL)
98 && (provider_store_index =
99 openssl_ctx_new_index(&provider_store_method)) != -1;
103 static struct provider_store_st *get_provider_store(OPENSSL_CTX *libctx)
105 struct provider_store_st *store = NULL;
107 if (!RUN_ONCE(&provider_store_init_flag, do_provider_store_init))
110 store = openssl_ctx_get_data(libctx, provider_store_index);
112 CRYPTOerr(CRYPTO_F_GET_PROVIDER_STORE, ERR_R_INTERNAL_ERROR);
117 * Provider Object methods
118 * =======================
121 int ossl_provider_upref(OSSL_PROVIDER *prov)
126 CRYPTO_UP_REF(&prov->refcnt, &ref, prov->refcnt_lock);
128 CRYPTO_UP_REF(&prov->refcnt, &ref, NULL);
133 /* Finder, constructor and destructor */
134 OSSL_PROVIDER *ossl_provider_find(OPENSSL_CTX *libctx, const char *name)
136 struct provider_store_st *store = NULL;
137 OSSL_PROVIDER *prov = NULL;
139 if ((store = get_provider_store(libctx)) != NULL) {
140 OSSL_PROVIDER tmpl = { 0, };
143 tmpl.name = (char *)name;
144 CRYPTO_THREAD_write_lock(store->lock);
145 if ((i = sk_OSSL_PROVIDER_find(store->providers, &tmpl)) == -1
146 || (prov = sk_OSSL_PROVIDER_value(store->providers, i)) == NULL
147 || !ossl_provider_upref(prov))
149 CRYPTO_THREAD_unlock(store->lock);
155 OSSL_PROVIDER *ossl_provider_new(OPENSSL_CTX *libctx, const char *name,
156 OSSL_provider_init_fn *init_function)
158 struct provider_store_st *store = NULL;
159 OSSL_PROVIDER *prov = NULL;
161 if ((store = get_provider_store(libctx)) == NULL)
164 if ((prov = ossl_provider_find(libctx, name)) != NULL) { /* refcount +1 */
165 ossl_provider_free(prov); /* refcount -1 */
166 CRYPTOerr(CRYPTO_F_OSSL_PROVIDER_NEW,
167 CRYPTO_R_PROVIDER_ALREADY_EXISTS);
168 ERR_add_error_data(2, "name=", name);
172 if ((prov = OPENSSL_zalloc(sizeof(*prov))) == NULL
173 || !ossl_provider_upref(prov) /* +1 One reference to be returned */
174 || (prov->name = OPENSSL_strdup(name)) == NULL) {
175 ossl_provider_free(prov);
176 CRYPTOerr(CRYPTO_F_OSSL_PROVIDER_NEW, ERR_R_MALLOC_FAILURE);
180 prov->init_function = init_function;
182 CRYPTO_THREAD_write_lock(store->lock);
183 if (!ossl_provider_upref(prov)) { /* +1 One reference for the store */
184 ossl_provider_free(prov); /* -1 Reference that was to be returned */
186 } else if (sk_OSSL_PROVIDER_push(store->providers, prov) == 0) {
187 ossl_provider_free(prov); /* -1 Store reference */
188 ossl_provider_free(prov); /* -1 Reference that was to be returned */
191 CRYPTO_THREAD_unlock(store->lock);
194 CRYPTOerr(CRYPTO_F_OSSL_PROVIDER_NEW, ERR_R_MALLOC_FAILURE);
197 * At this point, the provider is only partially "loaded". To be
198 * fully "loaded", ossl_provider_activate() must also be called.
204 void ossl_provider_free(OSSL_PROVIDER *prov)
210 CRYPTO_DOWN_REF(&prov->refcnt, &ref, provider_lock);
212 CRYPTO_DOWN_REF(&prov->refcnt, &ref, NULL);
216 * When the refcount drops down to one, there is only one reference,
218 * When that happens, the provider is inactivated.
220 if (ref == 1 && prov->flag_initialized) {
221 if (prov->teardown != NULL)
223 prov->flag_initialized = 0;
227 * When the refcount drops to zero, it has been taken out of
228 * the store. All we have to do here is clean it out.
231 DSO_free(prov->module);
232 OPENSSL_free(prov->name);
239 * Provider activation.
241 * What "activation" means depends on the provider form; for built in
242 * providers (in the library or the application alike), the provider
243 * can already be considered to be loaded, all that's needed is to
244 * initialize it. However, for dynamically loadable provider modules,
245 * we must first load that module.
247 * Built in modules are distinguished from dynamically loaded modules
248 * with an already assigned init function.
250 static const OSSL_DISPATCH *core_dispatch; /* Define further down */
252 int ossl_provider_activate(OSSL_PROVIDER *prov)
254 const OSSL_DISPATCH *provider_dispatch = NULL;
256 if (prov->flag_initialized)
260 * If the init function isn't set, it indicates that this provider is
263 if (prov->init_function == NULL) {
264 if (prov->module == NULL) {
265 char *platform_module_name = NULL;
266 char *module_path = NULL;
267 const char *load_dir = ossl_safe_getenv("OPENSSL_MODULES");
269 if ((prov->module = DSO_new()) == NULL) {
270 /* DSO_new() generates an error already */
274 if (load_dir == NULL)
275 load_dir = MODULESDIR;
277 DSO_ctrl(prov->module, DSO_CTRL_SET_FLAGS,
278 DSO_FLAG_NAME_TRANSLATION_EXT_ONLY, NULL);
279 if ((platform_module_name =
280 DSO_convert_filename(prov->module, prov->name)) == NULL
282 DSO_merge(prov->module, platform_module_name,
284 || DSO_load(prov->module, module_path, NULL,
285 DSO_FLAG_NAME_TRANSLATION_EXT_ONLY) == NULL) {
286 DSO_free(prov->module);
290 OPENSSL_free(platform_module_name);
291 OPENSSL_free(module_path);
294 if (prov->module != NULL)
295 prov->init_function = (OSSL_provider_init_fn *)
296 DSO_bind_func(prov->module, "OSSL_provider_init");
299 if (prov->init_function == NULL
300 || !prov->init_function(prov, core_dispatch, &provider_dispatch)) {
301 CRYPTOerr(CRYPTO_F_OSSL_PROVIDER_ACTIVATE, ERR_R_INIT_FAIL);
302 ERR_add_error_data(2, "name=", prov->name);
303 DSO_free(prov->module);
308 for (; provider_dispatch->function_id != 0; provider_dispatch++) {
309 switch (provider_dispatch->function_id) {
310 case OSSL_FUNC_PROVIDER_TEARDOWN:
312 OSSL_get_provider_teardown(provider_dispatch);
314 case OSSL_FUNC_PROVIDER_GET_PARAM_TYPES:
315 prov->get_param_types =
316 OSSL_get_provider_get_param_types(provider_dispatch);
318 case OSSL_FUNC_PROVIDER_GET_PARAMS:
320 OSSL_get_provider_get_params(provider_dispatch);
325 /* With this flag set, this provider has become fully "loaded". */
326 prov->flag_initialized = 1;
331 /* Getters of Provider Object data */
332 const char *ossl_provider_name(OSSL_PROVIDER *prov)
337 const DSO *ossl_provider_dso(OSSL_PROVIDER *prov)
342 const char *ossl_provider_module_name(OSSL_PROVIDER *prov)
344 return DSO_get_filename(prov->module);
347 const char *ossl_provider_module_path(OSSL_PROVIDER *prov)
349 /* FIXME: Ensure it's a full path */
350 return DSO_get_filename(prov->module);
353 /* Wrappers around calls to the provider */
354 void ossl_provider_teardown(const OSSL_PROVIDER *prov)
356 if (prov->teardown != NULL)
360 const OSSL_ITEM *ossl_provider_get_param_types(const OSSL_PROVIDER *prov)
362 return prov->get_param_types == NULL ? NULL : prov->get_param_types(prov);
365 int ossl_provider_get_params(const OSSL_PROVIDER *prov,
366 const OSSL_PARAM params[])
368 return prov->get_params == NULL ? 0 : prov->get_params(prov, params);
372 * Core functions for the provider
373 * ===============================
375 * This is the set of functions that the core makes available to the provider
379 * This returns a list of Provider Object parameters with their types, for
380 * discovery. We do not expect that many providers will use this, but one
383 static const OSSL_ITEM param_types[] = {
384 { OSSL_PARAM_UTF8_PTR, "openssl-version" },
385 { OSSL_PARAM_UTF8_PTR, "provider-name" },
389 static const OSSL_ITEM *core_get_param_types(const OSSL_PROVIDER *prov)
394 static int core_get_params(const OSSL_PROVIDER *prov, const OSSL_PARAM params[])
398 for (i = 0; params[i].key != NULL; i++) {
399 if (strcmp(params[i].key, "openssl-version") == 0) {
400 *(void **)params[i].data = OPENSSL_VERSION_STR;
401 if (params[i].return_size)
402 *params[i].return_size = sizeof(OPENSSL_VERSION_STR);
403 } else if (strcmp(params[i].key, "provider-name") == 0) {
404 *(void **)params[i].data = prov->name;
405 if (params[i].return_size)
406 *params[i].return_size = strlen(prov->name) + 1;
413 static const OSSL_DISPATCH core_dispatch_[] = {
414 { OSSL_FUNC_CORE_GET_PARAM_TYPES, (void (*)(void))core_get_param_types },
415 { OSSL_FUNC_CORE_GET_PARAMS, (void (*)(void))core_get_params },
418 static const OSSL_DISPATCH *core_dispatch = core_dispatch_;