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;
28 unsigned int flag_fallback
:1;
30 /* OpenSSL library side data */
31 CRYPTO_REF_COUNT refcnt
;
32 CRYPTO_RWLOCK
*refcnt_lock
; /* For the ref counter */
35 OSSL_provider_init_fn
*init_function
;
36 struct provider_store_st
*store
; /* The store this instance belongs to */
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
;
63 unsigned int use_fallbacks
:1;
65 static int provider_store_index
= -1;
67 static void provider_store_free(void *vstore
)
69 struct provider_store_st
*store
= vstore
;
73 sk_OSSL_PROVIDER_pop_free(store
->providers
, ossl_provider_free
);
74 CRYPTO_THREAD_lock_free(store
->lock
);
78 static void *provider_store_new(void)
80 struct provider_store_st
*store
= OPENSSL_zalloc(sizeof(*store
));
83 || (store
->providers
= sk_OSSL_PROVIDER_new(ossl_provider_cmp
)) == NULL
84 || (store
->lock
= CRYPTO_THREAD_lock_new()) == NULL
) {
85 provider_store_free(store
);
88 store
->use_fallbacks
= 1;
92 static const OPENSSL_CTX_METHOD provider_store_method
= {
97 static CRYPTO_ONCE provider_store_init_flag
= CRYPTO_ONCE_STATIC_INIT
;
98 DEFINE_RUN_ONCE_STATIC(do_provider_store_init
)
100 return OPENSSL_init_crypto(0, NULL
)
101 && (provider_store_index
=
102 openssl_ctx_new_index(&provider_store_method
)) != -1;
106 static struct provider_store_st
*get_provider_store(OPENSSL_CTX
*libctx
)
108 struct provider_store_st
*store
= NULL
;
110 if (!RUN_ONCE(&provider_store_init_flag
, do_provider_store_init
))
113 store
= openssl_ctx_get_data(libctx
, provider_store_index
);
115 CRYPTOerr(CRYPTO_F_GET_PROVIDER_STORE
, ERR_R_INTERNAL_ERROR
);
120 * Provider Object methods
121 * =======================
124 int ossl_provider_upref(OSSL_PROVIDER
*prov
)
128 CRYPTO_UP_REF(&prov
->refcnt
, &ref
, prov
->refcnt_lock
);
132 /* Finder, constructor and destructor */
133 OSSL_PROVIDER
*ossl_provider_find(OPENSSL_CTX
*libctx
, const char *name
)
135 struct provider_store_st
*store
= NULL
;
136 OSSL_PROVIDER
*prov
= NULL
;
138 if ((store
= get_provider_store(libctx
)) != NULL
) {
139 OSSL_PROVIDER tmpl
= { 0, };
142 tmpl
.name
= (char *)name
;
143 CRYPTO_THREAD_write_lock(store
->lock
);
144 if ((i
= sk_OSSL_PROVIDER_find(store
->providers
, &tmpl
)) == -1
145 || (prov
= sk_OSSL_PROVIDER_value(store
->providers
, i
)) == NULL
146 || !ossl_provider_upref(prov
))
148 CRYPTO_THREAD_unlock(store
->lock
);
154 OSSL_PROVIDER
*ossl_provider_new(OPENSSL_CTX
*libctx
, const char *name
,
155 OSSL_provider_init_fn
*init_function
)
157 struct provider_store_st
*store
= NULL
;
158 OSSL_PROVIDER
*prov
= NULL
;
160 if ((store
= get_provider_store(libctx
)) == NULL
)
163 if ((prov
= ossl_provider_find(libctx
, name
)) != NULL
) { /* refcount +1 */
164 ossl_provider_free(prov
); /* refcount -1 */
165 CRYPTOerr(CRYPTO_F_OSSL_PROVIDER_NEW
,
166 CRYPTO_R_PROVIDER_ALREADY_EXISTS
);
167 ERR_add_error_data(2, "name=", name
);
171 if ((prov
= OPENSSL_zalloc(sizeof(*prov
))) == NULL
173 || (prov
->refcnt_lock
= CRYPTO_THREAD_lock_new()) == NULL
175 || !ossl_provider_upref(prov
) /* +1 One reference to be returned */
176 || (prov
->name
= OPENSSL_strdup(name
)) == NULL
) {
177 ossl_provider_free(prov
);
178 CRYPTOerr(CRYPTO_F_OSSL_PROVIDER_NEW
, ERR_R_MALLOC_FAILURE
);
182 prov
->init_function
= init_function
;
184 CRYPTO_THREAD_write_lock(store
->lock
);
185 if (!ossl_provider_upref(prov
)) { /* +1 One reference for the store */
186 ossl_provider_free(prov
); /* -1 Reference that was to be returned */
188 } else if (sk_OSSL_PROVIDER_push(store
->providers
, prov
) == 0) {
189 ossl_provider_free(prov
); /* -1 Store reference */
190 ossl_provider_free(prov
); /* -1 Reference that was to be returned */
195 CRYPTO_THREAD_unlock(store
->lock
);
198 CRYPTOerr(CRYPTO_F_OSSL_PROVIDER_NEW
, ERR_R_MALLOC_FAILURE
);
201 * At this point, the provider is only partially "loaded". To be
202 * fully "loaded", ossl_provider_activate() must also be called.
208 void ossl_provider_free(OSSL_PROVIDER
*prov
)
213 CRYPTO_DOWN_REF(&prov
->refcnt
, &ref
, prov
->refcnt_lock
);
216 * When the refcount drops below two, the store is the only
217 * possible reference, or it has already been taken away from
218 * the store (this may happen if a provider was activated
219 * because it's a fallback, but isn't currently used)
220 * When that happens, the provider is inactivated.
222 if (ref
< 2 && prov
->flag_initialized
) {
223 if (prov
->teardown
!= NULL
)
225 prov
->flag_initialized
= 0;
229 * When the refcount drops to zero, it has been taken out of
230 * the store. All we have to do here is clean it out.
233 DSO_free(prov
->module
);
234 OPENSSL_free(prov
->name
);
236 CRYPTO_THREAD_lock_free(prov
->refcnt_lock
);
244 * Provider activation.
246 * What "activation" means depends on the provider form; for built in
247 * providers (in the library or the application alike), the provider
248 * can already be considered to be loaded, all that's needed is to
249 * initialize it. However, for dynamically loadable provider modules,
250 * we must first load that module.
252 * Built in modules are distinguished from dynamically loaded modules
253 * with an already assigned init function.
255 static const OSSL_DISPATCH
*core_dispatch
; /* Define further down */
258 * Internal version that doesn't affect the store flags, and thereby avoid
259 * locking. Direct callers must remember to set the store flags when
262 static int provider_activate(OSSL_PROVIDER
*prov
)
264 const OSSL_DISPATCH
*provider_dispatch
= NULL
;
266 if (prov
->flag_initialized
)
270 * If the init function isn't set, it indicates that this provider is
273 if (prov
->init_function
== NULL
) {
274 if (prov
->module
== NULL
) {
275 char *platform_module_name
= NULL
;
276 char *module_path
= NULL
;
277 const char *load_dir
= ossl_safe_getenv("OPENSSL_MODULES");
279 if ((prov
->module
= DSO_new()) == NULL
) {
280 /* DSO_new() generates an error already */
284 if (load_dir
== NULL
)
285 load_dir
= MODULESDIR
;
287 DSO_ctrl(prov
->module
, DSO_CTRL_SET_FLAGS
,
288 DSO_FLAG_NAME_TRANSLATION_EXT_ONLY
, NULL
);
289 if ((platform_module_name
=
290 DSO_convert_filename(prov
->module
, prov
->name
)) == NULL
292 DSO_merge(prov
->module
, platform_module_name
,
294 || DSO_load(prov
->module
, module_path
, NULL
,
295 DSO_FLAG_NAME_TRANSLATION_EXT_ONLY
) == NULL
) {
296 DSO_free(prov
->module
);
300 OPENSSL_free(platform_module_name
);
301 OPENSSL_free(module_path
);
304 if (prov
->module
!= NULL
)
305 prov
->init_function
= (OSSL_provider_init_fn
*)
306 DSO_bind_func(prov
->module
, "OSSL_provider_init");
309 if (prov
->init_function
== NULL
310 || !prov
->init_function(prov
, core_dispatch
, &provider_dispatch
)) {
311 CRYPTOerr(CRYPTO_F_PROVIDER_ACTIVATE
, ERR_R_INIT_FAIL
);
312 ERR_add_error_data(2, "name=", prov
->name
);
313 DSO_free(prov
->module
);
318 for (; provider_dispatch
->function_id
!= 0; provider_dispatch
++) {
319 switch (provider_dispatch
->function_id
) {
320 case OSSL_FUNC_PROVIDER_TEARDOWN
:
322 OSSL_get_provider_teardown(provider_dispatch
);
324 case OSSL_FUNC_PROVIDER_GET_PARAM_TYPES
:
325 prov
->get_param_types
=
326 OSSL_get_provider_get_param_types(provider_dispatch
);
328 case OSSL_FUNC_PROVIDER_GET_PARAMS
:
330 OSSL_get_provider_get_params(provider_dispatch
);
332 case OSSL_FUNC_PROVIDER_QUERY_OPERATION
:
333 prov
->query_operation
=
334 OSSL_get_provider_query_operation(provider_dispatch
);
339 /* With this flag set, this provider has become fully "loaded". */
340 prov
->flag_initialized
= 1;
345 int ossl_provider_activate(OSSL_PROVIDER
*prov
)
347 if (provider_activate(prov
)) {
348 CRYPTO_THREAD_write_lock(prov
->store
->lock
);
349 prov
->store
->use_fallbacks
= 0;
350 CRYPTO_THREAD_unlock(prov
->store
->lock
);
358 static int provider_forall_loaded(struct provider_store_st
*store
,
359 int *found_activated
,
360 int (*cb
)(OSSL_PROVIDER
*provider
,
366 int num_provs
= sk_OSSL_PROVIDER_num(store
->providers
);
368 if (found_activated
!= NULL
)
369 *found_activated
= 0;
370 for (i
= 0; i
< num_provs
; i
++) {
371 OSSL_PROVIDER
*prov
=
372 sk_OSSL_PROVIDER_value(store
->providers
, i
);
374 if (prov
->flag_initialized
) {
375 if (found_activated
!= NULL
)
376 *found_activated
= 1;
377 if (!(ret
= cb(prov
, cbdata
)))
385 int ossl_provider_forall_loaded(OPENSSL_CTX
*ctx
,
386 int (*cb
)(OSSL_PROVIDER
*provider
,
392 struct provider_store_st
*store
= get_provider_store(ctx
);
395 int found_activated
= 0;
397 CRYPTO_THREAD_read_lock(store
->lock
);
398 ret
= provider_forall_loaded(store
, &found_activated
, cb
, cbdata
);
401 * If there's nothing activated ever in this store, try to activate
404 if (!found_activated
&& store
->use_fallbacks
) {
405 int num_provs
= sk_OSSL_PROVIDER_num(store
->providers
);
406 int activated_fallback_count
= 0;
408 for (i
= 0; i
< num_provs
; i
++) {
409 OSSL_PROVIDER
*prov
=
410 sk_OSSL_PROVIDER_value(store
->providers
, i
);
413 * Note that we don't care if the activation succeeds or
414 * not. If it doesn't succeed, then the next loop will
417 if (prov
->flag_fallback
) {
418 activated_fallback_count
++;
419 provider_activate(prov
);
423 if (activated_fallback_count
> 0) {
425 * We assume that all fallbacks have been added to the store
426 * before any fallback is activated.
427 * TODO: We may have to reconsider this, IF we find ourselves
428 * adding fallbacks after any previous fallback has been
431 store
->use_fallbacks
= 0;
434 * Now that we've activated available fallbacks, try a
437 ret
= provider_forall_loaded(store
, NULL
, cb
, cbdata
);
440 CRYPTO_THREAD_unlock(store
->lock
);
446 /* Setters of Provider Object data */
447 int ossl_provider_set_fallback(OSSL_PROVIDER
*prov
)
452 prov
->flag_fallback
= 1;
456 /* Getters of Provider Object data */
457 const char *ossl_provider_name(OSSL_PROVIDER
*prov
)
462 const DSO
*ossl_provider_dso(OSSL_PROVIDER
*prov
)
467 const char *ossl_provider_module_name(OSSL_PROVIDER
*prov
)
469 return DSO_get_filename(prov
->module
);
472 const char *ossl_provider_module_path(OSSL_PROVIDER
*prov
)
474 /* FIXME: Ensure it's a full path */
475 return DSO_get_filename(prov
->module
);
478 /* Wrappers around calls to the provider */
479 void ossl_provider_teardown(const OSSL_PROVIDER
*prov
)
481 if (prov
->teardown
!= NULL
)
485 const OSSL_ITEM
*ossl_provider_get_param_types(const OSSL_PROVIDER
*prov
)
487 return prov
->get_param_types
== NULL
? NULL
: prov
->get_param_types(prov
);
490 int ossl_provider_get_params(const OSSL_PROVIDER
*prov
,
491 const OSSL_PARAM params
[])
493 return prov
->get_params
== NULL
? 0 : prov
->get_params(prov
, params
);
497 const OSSL_ALGORITHM
*ossl_provider_query_operation(const OSSL_PROVIDER
*prov
,
501 return prov
->query_operation(prov
, operation_id
, no_cache
);
505 * Core functions for the provider
506 * ===============================
508 * This is the set of functions that the core makes available to the provider
512 * This returns a list of Provider Object parameters with their types, for
513 * discovery. We do not expect that many providers will use this, but one
516 static const OSSL_ITEM param_types
[] = {
517 { OSSL_PARAM_UTF8_PTR
, "openssl-version" },
518 { OSSL_PARAM_UTF8_PTR
, "provider-name" },
522 static const OSSL_ITEM
*core_get_param_types(const OSSL_PROVIDER
*prov
)
527 static int core_get_params(const OSSL_PROVIDER
*prov
, const OSSL_PARAM params
[])
531 for (i
= 0; params
[i
].key
!= NULL
; i
++) {
532 if (strcmp(params
[i
].key
, "openssl-version") == 0) {
533 *(void **)params
[i
].data
= OPENSSL_VERSION_STR
;
534 if (params
[i
].return_size
)
535 *params
[i
].return_size
= sizeof(OPENSSL_VERSION_STR
);
536 } else if (strcmp(params
[i
].key
, "provider-name") == 0) {
537 *(void **)params
[i
].data
= prov
->name
;
538 if (params
[i
].return_size
)
539 *params
[i
].return_size
= strlen(prov
->name
) + 1;
546 static const OSSL_DISPATCH core_dispatch_
[] = {
547 { OSSL_FUNC_CORE_GET_PARAM_TYPES
, (void (*)(void))core_get_param_types
},
548 { OSSL_FUNC_CORE_GET_PARAMS
, (void (*)(void))core_get_params
},
551 static const OSSL_DISPATCH
*core_dispatch
= core_dispatch_
;