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;
42 OSSL_provider_query_operation_fn *query_operation;
44 DEFINE_STACK_OF(OSSL_PROVIDER)
46 static int ossl_provider_cmp(const OSSL_PROVIDER * const *a,
47 const OSSL_PROVIDER * const *b)
49 return strcmp((*a)->name, (*b)->name);
53 * Provider Object store
54 * =====================
56 * The Provider Object store is a library context object, and therefore needs
60 struct provider_store_st {
61 STACK_OF(OSSL_PROVIDER) *providers;
64 static int provider_store_index = -1;
66 static void provider_store_free(void *vstore)
68 struct provider_store_st *store = vstore;
72 sk_OSSL_PROVIDER_pop_free(store->providers, ossl_provider_free);
73 CRYPTO_THREAD_lock_free(store->lock);
77 static void *provider_store_new(void)
79 struct provider_store_st *store = OPENSSL_zalloc(sizeof(*store));
82 || (store->providers = sk_OSSL_PROVIDER_new(ossl_provider_cmp)) == NULL
83 || (store->lock = CRYPTO_THREAD_lock_new()) == NULL) {
84 provider_store_free(store);
90 static const OPENSSL_CTX_METHOD provider_store_method = {
95 static CRYPTO_ONCE provider_store_init_flag = CRYPTO_ONCE_STATIC_INIT;
96 DEFINE_RUN_ONCE_STATIC(do_provider_store_init)
98 return OPENSSL_init_crypto(0, NULL)
99 && (provider_store_index =
100 openssl_ctx_new_index(&provider_store_method)) != -1;
104 static struct provider_store_st *get_provider_store(OPENSSL_CTX *libctx)
106 struct provider_store_st *store = NULL;
108 if (!RUN_ONCE(&provider_store_init_flag, do_provider_store_init))
111 store = openssl_ctx_get_data(libctx, provider_store_index);
113 CRYPTOerr(CRYPTO_F_GET_PROVIDER_STORE, ERR_R_INTERNAL_ERROR);
118 * Provider Object methods
119 * =======================
122 int ossl_provider_upref(OSSL_PROVIDER *prov)
127 CRYPTO_UP_REF(&prov->refcnt, &ref, prov->refcnt_lock);
129 CRYPTO_UP_REF(&prov->refcnt, &ref, NULL);
134 /* Finder, constructor and destructor */
135 OSSL_PROVIDER *ossl_provider_find(OPENSSL_CTX *libctx, const char *name)
137 struct provider_store_st *store = NULL;
138 OSSL_PROVIDER *prov = NULL;
140 if ((store = get_provider_store(libctx)) != NULL) {
141 OSSL_PROVIDER tmpl = { 0, };
144 tmpl.name = (char *)name;
145 CRYPTO_THREAD_write_lock(store->lock);
146 if ((i = sk_OSSL_PROVIDER_find(store->providers, &tmpl)) == -1
147 || (prov = sk_OSSL_PROVIDER_value(store->providers, i)) == NULL
148 || !ossl_provider_upref(prov))
150 CRYPTO_THREAD_unlock(store->lock);
156 OSSL_PROVIDER *ossl_provider_new(OPENSSL_CTX *libctx, const char *name,
157 OSSL_provider_init_fn *init_function)
159 struct provider_store_st *store = NULL;
160 OSSL_PROVIDER *prov = NULL;
162 if ((store = get_provider_store(libctx)) == NULL)
165 if ((prov = ossl_provider_find(libctx, name)) != NULL) { /* refcount +1 */
166 ossl_provider_free(prov); /* refcount -1 */
167 CRYPTOerr(CRYPTO_F_OSSL_PROVIDER_NEW,
168 CRYPTO_R_PROVIDER_ALREADY_EXISTS);
169 ERR_add_error_data(2, "name=", name);
173 if ((prov = OPENSSL_zalloc(sizeof(*prov))) == NULL
174 || !ossl_provider_upref(prov) /* +1 One reference to be returned */
175 || (prov->name = OPENSSL_strdup(name)) == NULL) {
176 ossl_provider_free(prov);
177 CRYPTOerr(CRYPTO_F_OSSL_PROVIDER_NEW, ERR_R_MALLOC_FAILURE);
181 prov->init_function = init_function;
183 CRYPTO_THREAD_write_lock(store->lock);
184 if (!ossl_provider_upref(prov)) { /* +1 One reference for the store */
185 ossl_provider_free(prov); /* -1 Reference that was to be returned */
187 } else if (sk_OSSL_PROVIDER_push(store->providers, prov) == 0) {
188 ossl_provider_free(prov); /* -1 Store reference */
189 ossl_provider_free(prov); /* -1 Reference that was to be returned */
192 CRYPTO_THREAD_unlock(store->lock);
195 CRYPTOerr(CRYPTO_F_OSSL_PROVIDER_NEW, ERR_R_MALLOC_FAILURE);
198 * At this point, the provider is only partially "loaded". To be
199 * fully "loaded", ossl_provider_activate() must also be called.
205 void ossl_provider_free(OSSL_PROVIDER *prov)
211 CRYPTO_DOWN_REF(&prov->refcnt, &ref, provider_lock);
213 CRYPTO_DOWN_REF(&prov->refcnt, &ref, NULL);
217 * When the refcount drops down to one, there is only one reference,
219 * When that happens, the provider is inactivated.
221 if (ref == 1 && prov->flag_initialized) {
222 if (prov->teardown != NULL)
224 prov->flag_initialized = 0;
228 * When the refcount drops to zero, it has been taken out of
229 * the store. All we have to do here is clean it out.
232 DSO_free(prov->module);
233 OPENSSL_free(prov->name);
240 * Provider activation.
242 * What "activation" means depends on the provider form; for built in
243 * providers (in the library or the application alike), the provider
244 * can already be considered to be loaded, all that's needed is to
245 * initialize it. However, for dynamically loadable provider modules,
246 * we must first load that module.
248 * Built in modules are distinguished from dynamically loaded modules
249 * with an already assigned init function.
251 static const OSSL_DISPATCH *core_dispatch; /* Define further down */
253 int ossl_provider_activate(OSSL_PROVIDER *prov)
255 const OSSL_DISPATCH *provider_dispatch = NULL;
257 if (prov->flag_initialized)
261 * If the init function isn't set, it indicates that this provider is
264 if (prov->init_function == NULL) {
265 if (prov->module == NULL) {
266 char *platform_module_name = NULL;
267 char *module_path = NULL;
268 const char *load_dir = ossl_safe_getenv("OPENSSL_MODULES");
270 if ((prov->module = DSO_new()) == NULL) {
271 /* DSO_new() generates an error already */
275 if (load_dir == NULL)
276 load_dir = MODULESDIR;
278 DSO_ctrl(prov->module, DSO_CTRL_SET_FLAGS,
279 DSO_FLAG_NAME_TRANSLATION_EXT_ONLY, NULL);
280 if ((platform_module_name =
281 DSO_convert_filename(prov->module, prov->name)) == NULL
283 DSO_merge(prov->module, platform_module_name,
285 || DSO_load(prov->module, module_path, NULL,
286 DSO_FLAG_NAME_TRANSLATION_EXT_ONLY) == NULL) {
287 DSO_free(prov->module);
291 OPENSSL_free(platform_module_name);
292 OPENSSL_free(module_path);
295 if (prov->module != NULL)
296 prov->init_function = (OSSL_provider_init_fn *)
297 DSO_bind_func(prov->module, "OSSL_provider_init");
300 if (prov->init_function == NULL
301 || !prov->init_function(prov, core_dispatch, &provider_dispatch)) {
302 CRYPTOerr(CRYPTO_F_OSSL_PROVIDER_ACTIVATE, ERR_R_INIT_FAIL);
303 ERR_add_error_data(2, "name=", prov->name);
304 DSO_free(prov->module);
309 for (; provider_dispatch->function_id != 0; provider_dispatch++) {
310 switch (provider_dispatch->function_id) {
311 case OSSL_FUNC_PROVIDER_TEARDOWN:
313 OSSL_get_provider_teardown(provider_dispatch);
315 case OSSL_FUNC_PROVIDER_GET_PARAM_TYPES:
316 prov->get_param_types =
317 OSSL_get_provider_get_param_types(provider_dispatch);
319 case OSSL_FUNC_PROVIDER_GET_PARAMS:
321 OSSL_get_provider_get_params(provider_dispatch);
323 case OSSL_FUNC_PROVIDER_QUERY_OPERATION:
324 prov->query_operation =
325 OSSL_get_provider_query_operation(provider_dispatch);
330 /* With this flag set, this provider has become fully "loaded". */
331 prov->flag_initialized = 1;
336 int ossl_provider_forall_loaded(OPENSSL_CTX *ctx,
337 int (*cb)(OSSL_PROVIDER *provider,
343 struct provider_store_st *store = get_provider_store(ctx);
346 CRYPTO_THREAD_read_lock(store->lock);
347 for (i = 0; i < sk_OSSL_PROVIDER_num(store->providers); i++) {
348 OSSL_PROVIDER *prov = sk_OSSL_PROVIDER_value(store->providers, i);
350 if (prov->flag_initialized
351 && !(ret = cb(prov, cbdata)))
354 CRYPTO_THREAD_unlock(store->lock);
360 /* Getters of Provider Object data */
361 const char *ossl_provider_name(OSSL_PROVIDER *prov)
366 const DSO *ossl_provider_dso(OSSL_PROVIDER *prov)
371 const char *ossl_provider_module_name(OSSL_PROVIDER *prov)
373 return DSO_get_filename(prov->module);
376 const char *ossl_provider_module_path(OSSL_PROVIDER *prov)
378 /* FIXME: Ensure it's a full path */
379 return DSO_get_filename(prov->module);
382 /* Wrappers around calls to the provider */
383 void ossl_provider_teardown(const OSSL_PROVIDER *prov)
385 if (prov->teardown != NULL)
389 const OSSL_ITEM *ossl_provider_get_param_types(const OSSL_PROVIDER *prov)
391 return prov->get_param_types == NULL ? NULL : prov->get_param_types(prov);
394 int ossl_provider_get_params(const OSSL_PROVIDER *prov,
395 const OSSL_PARAM params[])
397 return prov->get_params == NULL ? 0 : prov->get_params(prov, params);
401 const OSSL_ALGORITHM *ossl_provider_query_operation(const OSSL_PROVIDER *prov,
405 return prov->query_operation(prov, operation_id, no_cache);
409 * Core functions for the provider
410 * ===============================
412 * This is the set of functions that the core makes available to the provider
416 * This returns a list of Provider Object parameters with their types, for
417 * discovery. We do not expect that many providers will use this, but one
420 static const OSSL_ITEM param_types[] = {
421 { OSSL_PARAM_UTF8_PTR, "openssl-version" },
422 { OSSL_PARAM_UTF8_PTR, "provider-name" },
426 static const OSSL_ITEM *core_get_param_types(const OSSL_PROVIDER *prov)
431 static int core_get_params(const OSSL_PROVIDER *prov, const OSSL_PARAM params[])
435 for (i = 0; params[i].key != NULL; i++) {
436 if (strcmp(params[i].key, "openssl-version") == 0) {
437 *(void **)params[i].data = OPENSSL_VERSION_STR;
438 if (params[i].return_size)
439 *params[i].return_size = sizeof(OPENSSL_VERSION_STR);
440 } else if (strcmp(params[i].key, "provider-name") == 0) {
441 *(void **)params[i].data = prov->name;
442 if (params[i].return_size)
443 *params[i].return_size = strlen(prov->name) + 1;
450 static const OSSL_DISPATCH core_dispatch_[] = {
451 { OSSL_FUNC_CORE_GET_PARAM_TYPES, (void (*)(void))core_get_param_types },
452 { OSSL_FUNC_CORE_GET_PARAMS, (void (*)(void))core_get_params },
455 static const OSSL_DISPATCH *core_dispatch = core_dispatch_;