2 * Copyright 2019-2022 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
11 #include <openssl/core.h>
12 #include <openssl/core_dispatch.h>
13 #include <openssl/core_names.h>
14 #include <openssl/provider.h>
15 #include <openssl/params.h>
16 #include <openssl/opensslv.h>
17 #include "crypto/cryptlib.h"
19 #include "crypto/decoder.h" /* ossl_decoder_store_cache_flush */
20 #include "crypto/encoder.h" /* ossl_encoder_store_cache_flush */
21 #include "crypto/store.h" /* ossl_store_loader_store_cache_flush */
23 #include "crypto/evp.h" /* evp_method_store_cache_flush */
24 #include "crypto/rand.h"
25 #include "internal/nelem.h"
26 #include "internal/thread_once.h"
27 #include "internal/provider.h"
28 #include "internal/refcount.h"
29 #include "internal/bio.h"
30 #include "internal/core.h"
31 #include "provider_local.h"
32 #include "crypto/context.h"
34 # include <openssl/self_test.h>
38 * This file defines and uses a number of different structures:
40 * OSSL_PROVIDER (provider_st): Used to represent all information related to a
41 * single instance of a provider.
43 * provider_store_st: Holds information about the collection of providers that
44 * are available within the current library context (OSSL_LIB_CTX). It also
45 * holds configuration information about providers that could be loaded at some
48 * OSSL_PROVIDER_CHILD_CB: An instance of this structure holds the callbacks
49 * that have been registered for a child library context and the associated
50 * provider that registered those callbacks.
52 * Where a child library context exists then it has its own instance of the
53 * provider store. Each provider that exists in the parent provider store, has
54 * an associated child provider in the child library context's provider store.
55 * As providers get activated or deactivated this needs to be mirrored in the
56 * associated child providers.
61 * There are a number of different locks used in this file and it is important
62 * to understand how they should be used in order to avoid deadlocks.
64 * Fields within a structure can often be "write once" on creation, and then
65 * "read many". Creation of a structure is done by a single thread, and
66 * therefore no lock is required for the "write once/read many" fields. It is
67 * safe for multiple threads to read these fields without a lock, because they
68 * will never be changed.
70 * However some fields may be changed after a structure has been created and
71 * shared between multiple threads. Where this is the case a lock is required.
73 * The locks available are:
75 * The provider flag_lock: Used to control updates to the various provider
76 * "flags" (flag_initialized and flag_activated) and associated
77 * "counts" (activatecnt).
79 * The provider refcnt_lock: Only ever used to control updates to the provider
82 * The provider optbits_lock: Used to control access to the provider's
83 * operation_bits and operation_bits_sz fields.
85 * The store default_path_lock: Used to control access to the provider store's
86 * default search path value (default_path)
88 * The store lock: Used to control the stack of provider's held within the
89 * provider store, as well as the stack of registered child provider callbacks.
91 * As a general rule-of-thumb it is best to:
92 * - keep the scope of the code that is protected by a lock to the absolute
94 * - try to keep the scope of the lock to within a single function (i.e. avoid
95 * making calls to other functions while holding a lock);
96 * - try to only ever hold one lock at a time.
98 * Unfortunately, it is not always possible to stick to the above guidelines.
99 * Where they are not adhered to there is always a danger of inadvertently
100 * introducing the possibility of deadlock. The following rules MUST be adhered
101 * to in order to avoid that:
102 * - Holding multiple locks at the same time is only allowed for the
103 * provider store lock, the provider flag_lock and the provider refcnt_lock.
104 * - When holding multiple locks they must be acquired in the following order of
106 * 1) provider store lock
107 * 2) provider flag_lock
108 * 3) provider refcnt_lock
109 * - When releasing locks they must be released in the reverse order to which
111 * - No locks may be held when making an upcall. NOTE: Some common functions
112 * can make upcalls as part of their normal operation. If you need to call
113 * some other function while holding a lock make sure you know whether it
114 * will make any upcalls or not. For example ossl_provider_up_ref() can call
115 * ossl_provider_up_ref_parent() which can call the c_prov_up_ref() upcall.
116 * - It is permissible to hold the store and flag locks when calling child
117 * provider callbacks. No other locks may be held during such callbacks.
120 static OSSL_PROVIDER
*provider_new(const char *name
,
121 OSSL_provider_init_fn
*init_function
,
122 STACK_OF(INFOPAIR
) *parameters
);
125 * Provider Object structure
126 * =========================
132 int (*create_cb
)(const OSSL_CORE_HANDLE
*provider
, void *cbdata
);
133 int (*remove_cb
)(const OSSL_CORE_HANDLE
*provider
, void *cbdata
);
134 int (*global_props_cb
)(const char *props
, void *cbdata
);
136 } OSSL_PROVIDER_CHILD_CB
;
137 DEFINE_STACK_OF(OSSL_PROVIDER_CHILD_CB
)
140 struct provider_store_st
; /* Forward declaration */
142 struct ossl_provider_st
{
144 unsigned int flag_initialized
:1;
145 unsigned int flag_activated
:1;
147 /* Getting and setting the flags require synchronization */
148 CRYPTO_RWLOCK
*flag_lock
;
150 /* OpenSSL library side data */
151 CRYPTO_REF_COUNT refcnt
;
152 CRYPTO_RWLOCK
*refcnt_lock
; /* For the ref counter */
157 OSSL_provider_init_fn
*init_function
;
158 STACK_OF(INFOPAIR
) *parameters
;
159 OSSL_LIB_CTX
*libctx
; /* The library context this instance is in */
160 struct provider_store_st
*store
; /* The store this instance belongs to */
163 * In the FIPS module inner provider, this isn't needed, since the
164 * error upcalls are always direct calls to the outer provider.
166 int error_lib
; /* ERR library number, one for each provider */
167 # ifndef OPENSSL_NO_ERR
168 ERR_STRING_DATA
*error_strings
; /* Copy of what the provider gives us */
172 /* Provider side functions */
173 OSSL_FUNC_provider_teardown_fn
*teardown
;
174 OSSL_FUNC_provider_gettable_params_fn
*gettable_params
;
175 OSSL_FUNC_provider_get_params_fn
*get_params
;
176 OSSL_FUNC_provider_get_capabilities_fn
*get_capabilities
;
177 OSSL_FUNC_provider_self_test_fn
*self_test
;
178 OSSL_FUNC_provider_query_operation_fn
*query_operation
;
179 OSSL_FUNC_provider_unquery_operation_fn
*unquery_operation
;
182 * Cache of bit to indicate of query_operation() has been called on
183 * a specific operation or not.
185 unsigned char *operation_bits
;
186 size_t operation_bits_sz
;
187 CRYPTO_RWLOCK
*opbits_lock
;
190 /* Whether this provider is the child of some other provider */
191 const OSSL_CORE_HANDLE
*handle
;
192 unsigned int ischild
:1;
195 /* Provider side data */
197 const OSSL_DISPATCH
*dispatch
;
199 DEFINE_STACK_OF(OSSL_PROVIDER
)
201 static int ossl_provider_cmp(const OSSL_PROVIDER
* const *a
,
202 const OSSL_PROVIDER
* const *b
)
204 return strcmp((*a
)->name
, (*b
)->name
);
208 * Provider Object store
209 * =====================
211 * The Provider Object store is a library context object, and therefore needs
215 struct provider_store_st
{
216 OSSL_LIB_CTX
*libctx
;
217 STACK_OF(OSSL_PROVIDER
) *providers
;
218 STACK_OF(OSSL_PROVIDER_CHILD_CB
) *child_cbs
;
219 CRYPTO_RWLOCK
*default_path_lock
;
222 OSSL_PROVIDER_INFO
*provinfo
;
225 unsigned int use_fallbacks
:1;
226 unsigned int freeing
:1;
230 * provider_deactivate_free() is a wrapper around ossl_provider_deactivate()
231 * and ossl_provider_free(), called as needed.
232 * Since this is only called when the provider store is being emptied, we
233 * don't need to care about any lock.
235 static void provider_deactivate_free(OSSL_PROVIDER
*prov
)
237 if (prov
->flag_activated
)
238 ossl_provider_deactivate(prov
, 1);
239 ossl_provider_free(prov
);
243 static void ossl_provider_child_cb_free(OSSL_PROVIDER_CHILD_CB
*cb
)
249 static void infopair_free(INFOPAIR
*pair
)
251 OPENSSL_free(pair
->name
);
252 OPENSSL_free(pair
->value
);
256 static INFOPAIR
*infopair_copy(const INFOPAIR
*src
)
258 INFOPAIR
*dest
= OPENSSL_zalloc(sizeof(*dest
));
262 if (src
->name
!= NULL
) {
263 dest
->name
= OPENSSL_strdup(src
->name
);
264 if (dest
->name
== NULL
)
267 if (src
->value
!= NULL
) {
268 dest
->value
= OPENSSL_strdup(src
->value
);
269 if (dest
->value
== NULL
)
274 OPENSSL_free(dest
->name
);
279 void ossl_provider_info_clear(OSSL_PROVIDER_INFO
*info
)
281 OPENSSL_free(info
->name
);
282 OPENSSL_free(info
->path
);
283 sk_INFOPAIR_pop_free(info
->parameters
, infopair_free
);
286 void ossl_provider_store_free(void *vstore
)
288 struct provider_store_st
*store
= vstore
;
294 OPENSSL_free(store
->default_path
);
295 sk_OSSL_PROVIDER_pop_free(store
->providers
, provider_deactivate_free
);
297 sk_OSSL_PROVIDER_CHILD_CB_pop_free(store
->child_cbs
,
298 ossl_provider_child_cb_free
);
300 CRYPTO_THREAD_lock_free(store
->default_path_lock
);
301 CRYPTO_THREAD_lock_free(store
->lock
);
302 for (i
= 0; i
< store
->numprovinfo
; i
++)
303 ossl_provider_info_clear(&store
->provinfo
[i
]);
304 OPENSSL_free(store
->provinfo
);
308 void *ossl_provider_store_new(OSSL_LIB_CTX
*ctx
)
310 struct provider_store_st
*store
= OPENSSL_zalloc(sizeof(*store
));
313 || (store
->providers
= sk_OSSL_PROVIDER_new(ossl_provider_cmp
)) == NULL
314 || (store
->default_path_lock
= CRYPTO_THREAD_lock_new()) == NULL
316 || (store
->child_cbs
= sk_OSSL_PROVIDER_CHILD_CB_new_null()) == NULL
318 || (store
->lock
= CRYPTO_THREAD_lock_new()) == NULL
) {
319 ossl_provider_store_free(store
);
323 store
->use_fallbacks
= 1;
328 static struct provider_store_st
*get_provider_store(OSSL_LIB_CTX
*libctx
)
330 struct provider_store_st
*store
= NULL
;
332 store
= ossl_lib_ctx_get_data(libctx
, OSSL_LIB_CTX_PROVIDER_STORE_INDEX
);
334 ERR_raise(ERR_LIB_CRYPTO
, ERR_R_INTERNAL_ERROR
);
338 int ossl_provider_disable_fallback_loading(OSSL_LIB_CTX
*libctx
)
340 struct provider_store_st
*store
;
342 if ((store
= get_provider_store(libctx
)) != NULL
) {
343 if (!CRYPTO_THREAD_write_lock(store
->lock
))
345 store
->use_fallbacks
= 0;
346 CRYPTO_THREAD_unlock(store
->lock
);
352 #define BUILTINS_BLOCK_SIZE 10
354 int ossl_provider_info_add_to_store(OSSL_LIB_CTX
*libctx
,
355 OSSL_PROVIDER_INFO
*entry
)
357 struct provider_store_st
*store
= get_provider_store(libctx
);
360 if (entry
->name
== NULL
) {
361 ERR_raise(ERR_LIB_CRYPTO
, ERR_R_PASSED_NULL_PARAMETER
);
366 ERR_raise(ERR_LIB_CRYPTO
, ERR_R_INTERNAL_ERROR
);
370 if (!CRYPTO_THREAD_write_lock(store
->lock
))
372 if (store
->provinfosz
== 0) {
373 store
->provinfo
= OPENSSL_zalloc(sizeof(*store
->provinfo
)
374 * BUILTINS_BLOCK_SIZE
);
375 if (store
->provinfo
== NULL
) {
376 ERR_raise(ERR_LIB_CRYPTO
, ERR_R_MALLOC_FAILURE
);
379 store
->provinfosz
= BUILTINS_BLOCK_SIZE
;
380 } else if (store
->numprovinfo
== store
->provinfosz
) {
381 OSSL_PROVIDER_INFO
*tmpbuiltins
;
382 size_t newsz
= store
->provinfosz
+ BUILTINS_BLOCK_SIZE
;
384 tmpbuiltins
= OPENSSL_realloc(store
->provinfo
,
385 sizeof(*store
->provinfo
) * newsz
);
386 if (tmpbuiltins
== NULL
) {
387 ERR_raise(ERR_LIB_CRYPTO
, ERR_R_MALLOC_FAILURE
);
390 store
->provinfo
= tmpbuiltins
;
391 store
->provinfosz
= newsz
;
393 store
->provinfo
[store
->numprovinfo
] = *entry
;
394 store
->numprovinfo
++;
398 CRYPTO_THREAD_unlock(store
->lock
);
402 OSSL_PROVIDER
*ossl_provider_find(OSSL_LIB_CTX
*libctx
, const char *name
,
405 struct provider_store_st
*store
= NULL
;
406 OSSL_PROVIDER
*prov
= NULL
;
408 if ((store
= get_provider_store(libctx
)) != NULL
) {
409 OSSL_PROVIDER tmpl
= { 0, };
414 * Make sure any providers are loaded from config before we try to find
418 if (ossl_lib_ctx_is_default(libctx
))
419 OPENSSL_init_crypto(OPENSSL_INIT_LOAD_CONFIG
, NULL
);
423 tmpl
.name
= (char *)name
;
425 * A "find" operation can sort the stack, and therefore a write lock is
428 if (!CRYPTO_THREAD_write_lock(store
->lock
))
430 if ((i
= sk_OSSL_PROVIDER_find(store
->providers
, &tmpl
)) != -1)
431 prov
= sk_OSSL_PROVIDER_value(store
->providers
, i
);
432 CRYPTO_THREAD_unlock(store
->lock
);
433 if (prov
!= NULL
&& !ossl_provider_up_ref(prov
))
441 * Provider Object methods
442 * =======================
445 static OSSL_PROVIDER
*provider_new(const char *name
,
446 OSSL_provider_init_fn
*init_function
,
447 STACK_OF(INFOPAIR
) *parameters
)
449 OSSL_PROVIDER
*prov
= NULL
;
451 if ((prov
= OPENSSL_zalloc(sizeof(*prov
))) == NULL
453 || (prov
->refcnt_lock
= CRYPTO_THREAD_lock_new()) == NULL
457 ERR_raise(ERR_LIB_CRYPTO
, ERR_R_MALLOC_FAILURE
);
461 prov
->refcnt
= 1; /* 1 One reference to be returned */
463 if ((prov
->opbits_lock
= CRYPTO_THREAD_lock_new()) == NULL
464 || (prov
->flag_lock
= CRYPTO_THREAD_lock_new()) == NULL
465 || (prov
->name
= OPENSSL_strdup(name
)) == NULL
466 || (prov
->parameters
= sk_INFOPAIR_deep_copy(parameters
,
468 infopair_free
)) == NULL
) {
469 ossl_provider_free(prov
);
470 ERR_raise(ERR_LIB_CRYPTO
, ERR_R_MALLOC_FAILURE
);
474 prov
->init_function
= init_function
;
479 int ossl_provider_up_ref(OSSL_PROVIDER
*prov
)
483 if (CRYPTO_UP_REF(&prov
->refcnt
, &ref
, prov
->refcnt_lock
) <= 0)
488 if (!ossl_provider_up_ref_parent(prov
, 0)) {
489 ossl_provider_free(prov
);
499 static int provider_up_ref_intern(OSSL_PROVIDER
*prov
, int activate
)
502 return ossl_provider_activate(prov
, 1, 0);
504 return ossl_provider_up_ref(prov
);
507 static int provider_free_intern(OSSL_PROVIDER
*prov
, int deactivate
)
510 return ossl_provider_deactivate(prov
, 1);
512 ossl_provider_free(prov
);
518 * We assume that the requested provider does not already exist in the store.
519 * The caller should check. If it does exist then adding it to the store later
522 OSSL_PROVIDER
*ossl_provider_new(OSSL_LIB_CTX
*libctx
, const char *name
,
523 OSSL_provider_init_fn
*init_function
,
526 struct provider_store_st
*store
= NULL
;
527 OSSL_PROVIDER_INFO
template;
528 OSSL_PROVIDER
*prov
= NULL
;
530 if ((store
= get_provider_store(libctx
)) == NULL
)
533 memset(&template, 0, sizeof(template));
534 if (init_function
== NULL
) {
535 const OSSL_PROVIDER_INFO
*p
;
538 /* Check if this is a predefined builtin provider */
539 for (p
= ossl_predefined_providers
; p
->name
!= NULL
; p
++) {
540 if (strcmp(p
->name
, name
) == 0) {
545 if (p
->name
== NULL
) {
546 /* Check if this is a user added builtin provider */
547 if (!CRYPTO_THREAD_read_lock(store
->lock
))
549 for (i
= 0, p
= store
->provinfo
; i
< store
->numprovinfo
; p
++, i
++) {
550 if (strcmp(p
->name
, name
) == 0) {
555 CRYPTO_THREAD_unlock(store
->lock
);
558 template.init
= init_function
;
561 /* provider_new() generates an error, so no need here */
562 if ((prov
= provider_new(name
, template.init
, template.parameters
)) == NULL
)
565 prov
->libctx
= libctx
;
567 prov
->error_lib
= ERR_get_next_error_library();
571 * At this point, the provider is only partially "loaded". To be
572 * fully "loaded", ossl_provider_activate() must also be called and it must
573 * then be added to the provider store.
579 /* Assumes that the store lock is held */
580 static int create_provider_children(OSSL_PROVIDER
*prov
)
584 struct provider_store_st
*store
= prov
->store
;
585 OSSL_PROVIDER_CHILD_CB
*child_cb
;
588 max
= sk_OSSL_PROVIDER_CHILD_CB_num(store
->child_cbs
);
589 for (i
= 0; i
< max
; i
++) {
591 * This is newly activated (activatecnt == 1), so we need to
592 * create child providers as necessary.
594 child_cb
= sk_OSSL_PROVIDER_CHILD_CB_value(store
->child_cbs
, i
);
595 ret
&= child_cb
->create_cb((OSSL_CORE_HANDLE
*)prov
, child_cb
->cbdata
);
602 int ossl_provider_add_to_store(OSSL_PROVIDER
*prov
, OSSL_PROVIDER
**actualprov
,
603 int retain_fallbacks
)
605 struct provider_store_st
*store
;
607 OSSL_PROVIDER tmpl
= { 0, };
608 OSSL_PROVIDER
*actualtmp
= NULL
;
610 if (actualprov
!= NULL
)
613 if ((store
= get_provider_store(prov
->libctx
)) == NULL
)
616 if (!CRYPTO_THREAD_write_lock(store
->lock
))
619 tmpl
.name
= (char *)prov
->name
;
620 idx
= sk_OSSL_PROVIDER_find(store
->providers
, &tmpl
);
624 actualtmp
= sk_OSSL_PROVIDER_value(store
->providers
, idx
);
627 if (sk_OSSL_PROVIDER_push(store
->providers
, prov
) == 0)
630 if (!create_provider_children(prov
)) {
631 sk_OSSL_PROVIDER_delete_ptr(store
->providers
, prov
);
634 if (!retain_fallbacks
)
635 store
->use_fallbacks
= 0;
638 CRYPTO_THREAD_unlock(store
->lock
);
640 if (actualprov
!= NULL
) {
641 if (!ossl_provider_up_ref(actualtmp
)) {
642 ERR_raise(ERR_LIB_CRYPTO
, ERR_R_MALLOC_FAILURE
);
646 *actualprov
= actualtmp
;
651 * The provider is already in the store. Probably two threads
652 * independently initialised their own provider objects with the same
653 * name and raced to put them in the store. This thread lost. We
654 * deactivate the one we just created and use the one that already
656 * If we get here then we know we did not create provider children
657 * above, so we inform ossl_provider_deactivate not to attempt to remove
660 ossl_provider_deactivate(prov
, 0);
661 ossl_provider_free(prov
);
667 CRYPTO_THREAD_unlock(store
->lock
);
671 void ossl_provider_free(OSSL_PROVIDER
*prov
)
676 CRYPTO_DOWN_REF(&prov
->refcnt
, &ref
, prov
->refcnt_lock
);
679 * When the refcount drops to zero, we clean up the provider.
680 * Note that this also does teardown, which may seem late,
681 * considering that init happens on first activation. However,
682 * there may be other structures hanging on to the provider after
683 * the last deactivation and may therefore need full access to the
684 * provider's services. Therefore, we deinit late.
687 if (prov
->flag_initialized
) {
688 ossl_provider_teardown(prov
);
689 #ifndef OPENSSL_NO_ERR
691 if (prov
->error_strings
!= NULL
) {
692 ERR_unload_strings(prov
->error_lib
, prov
->error_strings
);
693 OPENSSL_free(prov
->error_strings
);
694 prov
->error_strings
= NULL
;
698 OPENSSL_free(prov
->operation_bits
);
699 prov
->operation_bits
= NULL
;
700 prov
->operation_bits_sz
= 0;
701 prov
->flag_initialized
= 0;
706 * We deregister thread handling whether or not the provider was
707 * initialized. If init was attempted but was not successful then
708 * the provider may still have registered a thread handler.
710 ossl_init_thread_deregister(prov
);
711 DSO_free(prov
->module
);
713 OPENSSL_free(prov
->name
);
714 OPENSSL_free(prov
->path
);
715 sk_INFOPAIR_pop_free(prov
->parameters
, infopair_free
);
716 CRYPTO_THREAD_lock_free(prov
->opbits_lock
);
717 CRYPTO_THREAD_lock_free(prov
->flag_lock
);
719 CRYPTO_THREAD_lock_free(prov
->refcnt_lock
);
724 else if (prov
->ischild
) {
725 ossl_provider_free_parent(prov
, 0);
732 int ossl_provider_set_module_path(OSSL_PROVIDER
*prov
, const char *module_path
)
734 OPENSSL_free(prov
->path
);
736 if (module_path
== NULL
)
738 if ((prov
->path
= OPENSSL_strdup(module_path
)) != NULL
)
740 ERR_raise(ERR_LIB_CRYPTO
, ERR_R_MALLOC_FAILURE
);
744 static int infopair_add(STACK_OF(INFOPAIR
) **infopairsk
, const char *name
,
747 INFOPAIR
*pair
= NULL
;
749 if ((pair
= OPENSSL_zalloc(sizeof(*pair
))) != NULL
750 && (*infopairsk
!= NULL
751 || (*infopairsk
= sk_INFOPAIR_new_null()) != NULL
)
752 && (pair
->name
= OPENSSL_strdup(name
)) != NULL
753 && (pair
->value
= OPENSSL_strdup(value
)) != NULL
754 && sk_INFOPAIR_push(*infopairsk
, pair
) > 0)
758 OPENSSL_free(pair
->name
);
759 OPENSSL_free(pair
->value
);
762 ERR_raise(ERR_LIB_CRYPTO
, ERR_R_MALLOC_FAILURE
);
766 int ossl_provider_add_parameter(OSSL_PROVIDER
*prov
,
767 const char *name
, const char *value
)
769 return infopair_add(&prov
->parameters
, name
, value
);
772 int ossl_provider_info_add_parameter(OSSL_PROVIDER_INFO
*provinfo
,
776 return infopair_add(&provinfo
->parameters
, name
, value
);
780 * Provider activation.
782 * What "activation" means depends on the provider form; for built in
783 * providers (in the library or the application alike), the provider
784 * can already be considered to be loaded, all that's needed is to
785 * initialize it. However, for dynamically loadable provider modules,
786 * we must first load that module.
788 * Built in modules are distinguished from dynamically loaded modules
789 * with an already assigned init function.
791 static const OSSL_DISPATCH
*core_dispatch
; /* Define further down */
793 int OSSL_PROVIDER_set_default_search_path(OSSL_LIB_CTX
*libctx
,
796 struct provider_store_st
*store
;
800 p
= OPENSSL_strdup(path
);
802 ERR_raise(ERR_LIB_CRYPTO
, ERR_R_MALLOC_FAILURE
);
806 if ((store
= get_provider_store(libctx
)) != NULL
807 && CRYPTO_THREAD_write_lock(store
->default_path_lock
)) {
808 OPENSSL_free(store
->default_path
);
809 store
->default_path
= p
;
810 CRYPTO_THREAD_unlock(store
->default_path_lock
);
818 * Internal version that doesn't affect the store flags, and thereby avoid
819 * locking. Direct callers must remember to set the store flags when
822 static int provider_init(OSSL_PROVIDER
*prov
)
824 const OSSL_DISPATCH
*provider_dispatch
= NULL
;
825 void *tmp_provctx
= NULL
; /* safety measure */
826 #ifndef OPENSSL_NO_ERR
828 OSSL_FUNC_provider_get_reason_strings_fn
*p_get_reason_strings
= NULL
;
833 if (!ossl_assert(!prov
->flag_initialized
)) {
834 ERR_raise(ERR_LIB_CRYPTO
, ERR_R_INTERNAL_ERROR
);
839 * If the init function isn't set, it indicates that this provider is
842 if (prov
->init_function
== NULL
) {
846 if (prov
->module
== NULL
) {
847 char *allocated_path
= NULL
;
848 const char *module_path
= NULL
;
849 char *merged_path
= NULL
;
850 const char *load_dir
= NULL
;
851 char *allocated_load_dir
= NULL
;
852 struct provider_store_st
*store
;
854 if ((prov
->module
= DSO_new()) == NULL
) {
855 /* DSO_new() generates an error already */
859 if ((store
= get_provider_store(prov
->libctx
)) == NULL
860 || !CRYPTO_THREAD_read_lock(store
->default_path_lock
))
863 if (store
->default_path
!= NULL
) {
864 allocated_load_dir
= OPENSSL_strdup(store
->default_path
);
865 CRYPTO_THREAD_unlock(store
->default_path_lock
);
866 if (allocated_load_dir
== NULL
) {
867 ERR_raise(ERR_LIB_CRYPTO
, ERR_R_MALLOC_FAILURE
);
870 load_dir
= allocated_load_dir
;
872 CRYPTO_THREAD_unlock(store
->default_path_lock
);
875 if (load_dir
== NULL
) {
876 load_dir
= ossl_safe_getenv("OPENSSL_MODULES");
877 if (load_dir
== NULL
)
878 load_dir
= MODULESDIR
;
881 DSO_ctrl(prov
->module
, DSO_CTRL_SET_FLAGS
,
882 DSO_FLAG_NAME_TRANSLATION_EXT_ONLY
, NULL
);
884 module_path
= prov
->path
;
885 if (module_path
== NULL
)
886 module_path
= allocated_path
=
887 DSO_convert_filename(prov
->module
, prov
->name
);
888 if (module_path
!= NULL
)
889 merged_path
= DSO_merge(prov
->module
, module_path
, load_dir
);
891 if (merged_path
== NULL
892 || (DSO_load(prov
->module
, merged_path
, NULL
, 0)) == NULL
) {
893 DSO_free(prov
->module
);
897 OPENSSL_free(merged_path
);
898 OPENSSL_free(allocated_path
);
899 OPENSSL_free(allocated_load_dir
);
902 if (prov
->module
!= NULL
)
903 prov
->init_function
= (OSSL_provider_init_fn
*)
904 DSO_bind_func(prov
->module
, "OSSL_provider_init");
908 /* Call the initialise function for the provider. */
909 if (prov
->init_function
== NULL
910 || !prov
->init_function((OSSL_CORE_HANDLE
*)prov
, core_dispatch
,
911 &provider_dispatch
, &tmp_provctx
)) {
912 ERR_raise_data(ERR_LIB_CRYPTO
, ERR_R_INIT_FAIL
,
913 "name=%s", prov
->name
);
916 prov
->provctx
= tmp_provctx
;
917 prov
->dispatch
= provider_dispatch
;
919 for (; provider_dispatch
->function_id
!= 0; provider_dispatch
++) {
920 switch (provider_dispatch
->function_id
) {
921 case OSSL_FUNC_PROVIDER_TEARDOWN
:
923 OSSL_FUNC_provider_teardown(provider_dispatch
);
925 case OSSL_FUNC_PROVIDER_GETTABLE_PARAMS
:
926 prov
->gettable_params
=
927 OSSL_FUNC_provider_gettable_params(provider_dispatch
);
929 case OSSL_FUNC_PROVIDER_GET_PARAMS
:
931 OSSL_FUNC_provider_get_params(provider_dispatch
);
933 case OSSL_FUNC_PROVIDER_SELF_TEST
:
935 OSSL_FUNC_provider_self_test(provider_dispatch
);
937 case OSSL_FUNC_PROVIDER_GET_CAPABILITIES
:
938 prov
->get_capabilities
=
939 OSSL_FUNC_provider_get_capabilities(provider_dispatch
);
941 case OSSL_FUNC_PROVIDER_QUERY_OPERATION
:
942 prov
->query_operation
=
943 OSSL_FUNC_provider_query_operation(provider_dispatch
);
945 case OSSL_FUNC_PROVIDER_UNQUERY_OPERATION
:
946 prov
->unquery_operation
=
947 OSSL_FUNC_provider_unquery_operation(provider_dispatch
);
949 #ifndef OPENSSL_NO_ERR
951 case OSSL_FUNC_PROVIDER_GET_REASON_STRINGS
:
952 p_get_reason_strings
=
953 OSSL_FUNC_provider_get_reason_strings(provider_dispatch
);
960 #ifndef OPENSSL_NO_ERR
962 if (p_get_reason_strings
!= NULL
) {
963 const OSSL_ITEM
*reasonstrings
= p_get_reason_strings(prov
->provctx
);
967 * ERR_load_strings() handles ERR_STRING_DATA rather than OSSL_ITEM,
968 * although they are essentially the same type.
969 * Furthermore, ERR_load_strings() patches the array's error number
970 * with the error library number, so we need to make a copy of that
974 while (reasonstrings
[cnt
].id
!= 0) {
975 if (ERR_GET_LIB(reasonstrings
[cnt
].id
) != 0)
979 cnt
++; /* One for the terminating item */
981 /* Allocate one extra item for the "library" name */
982 prov
->error_strings
=
983 OPENSSL_zalloc(sizeof(ERR_STRING_DATA
) * (cnt
+ 1));
984 if (prov
->error_strings
== NULL
)
988 * Set the "library" name.
990 prov
->error_strings
[0].error
= ERR_PACK(prov
->error_lib
, 0, 0);
991 prov
->error_strings
[0].string
= prov
->name
;
993 * Copy reasonstrings item 0..cnt-1 to prov->error_trings positions
996 for (cnt2
= 1; cnt2
<= cnt
; cnt2
++) {
997 prov
->error_strings
[cnt2
].error
= (int)reasonstrings
[cnt2
-1].id
;
998 prov
->error_strings
[cnt2
].string
= reasonstrings
[cnt2
-1].ptr
;
1001 ERR_load_strings(prov
->error_lib
, prov
->error_strings
);
1006 /* With this flag set, this provider has become fully "loaded". */
1007 prov
->flag_initialized
= 1;
1015 * Deactivate a provider. If upcalls is 0 then we suppress any upcalls to a
1016 * parent provider. If removechildren is 0 then we suppress any calls to remove
1018 * Return -1 on failure and the activation count on success
1020 static int provider_deactivate(OSSL_PROVIDER
*prov
, int upcalls
,
1024 struct provider_store_st
*store
;
1030 if (!ossl_assert(prov
!= NULL
))
1034 * No need to lock if we've got no store because we've not been shared with
1037 store
= get_provider_store(prov
->libctx
);
1041 if (lock
&& !CRYPTO_THREAD_read_lock(store
->lock
))
1043 if (lock
&& !CRYPTO_THREAD_write_lock(prov
->flag_lock
)) {
1044 CRYPTO_THREAD_unlock(store
->lock
);
1049 if (prov
->activatecnt
>= 2 && prov
->ischild
&& upcalls
) {
1051 * We have had a direct activation in this child libctx so we need to
1052 * now down the ref count in the parent provider. We do the actual down
1053 * ref outside of the flag_lock, since it could involve getting other
1060 if ((count
= --prov
->activatecnt
) < 1)
1061 prov
->flag_activated
= 0;
1068 if (removechildren
&& store
!= NULL
) {
1069 int i
, max
= sk_OSSL_PROVIDER_CHILD_CB_num(store
->child_cbs
);
1070 OSSL_PROVIDER_CHILD_CB
*child_cb
;
1072 for (i
= 0; i
< max
; i
++) {
1073 child_cb
= sk_OSSL_PROVIDER_CHILD_CB_value(store
->child_cbs
, i
);
1074 child_cb
->remove_cb((OSSL_CORE_HANDLE
*)prov
, child_cb
->cbdata
);
1079 CRYPTO_THREAD_unlock(prov
->flag_lock
);
1080 CRYPTO_THREAD_unlock(store
->lock
);
1084 ossl_provider_free_parent(prov
, 1);
1087 /* We don't deinit here, that's done in ossl_provider_free() */
1092 * Activate a provider.
1093 * Return -1 on failure and the activation count on success
1095 static int provider_activate(OSSL_PROVIDER
*prov
, int lock
, int upcalls
)
1098 struct provider_store_st
*store
;
1101 store
= prov
->store
;
1103 * If the provider hasn't been added to the store, then we don't need
1104 * any locks because we've not shared it with other threads.
1106 if (store
== NULL
) {
1108 if (!provider_init(prov
))
1113 if (prov
->ischild
&& upcalls
&& !ossl_provider_up_ref_parent(prov
, 1))
1117 if (lock
&& !CRYPTO_THREAD_read_lock(store
->lock
)) {
1119 if (prov
->ischild
&& upcalls
)
1120 ossl_provider_free_parent(prov
, 1);
1125 if (lock
&& !CRYPTO_THREAD_write_lock(prov
->flag_lock
)) {
1126 CRYPTO_THREAD_unlock(store
->lock
);
1128 if (prov
->ischild
&& upcalls
)
1129 ossl_provider_free_parent(prov
, 1);
1134 count
= ++prov
->activatecnt
;
1135 prov
->flag_activated
= 1;
1137 if (prov
->activatecnt
== 1 && store
!= NULL
) {
1138 ret
= create_provider_children(prov
);
1141 CRYPTO_THREAD_unlock(prov
->flag_lock
);
1142 CRYPTO_THREAD_unlock(store
->lock
);
1151 static int provider_flush_store_cache(const OSSL_PROVIDER
*prov
)
1153 struct provider_store_st
*store
;
1156 if ((store
= get_provider_store(prov
->libctx
)) == NULL
)
1159 if (!CRYPTO_THREAD_read_lock(store
->lock
))
1161 freeing
= store
->freeing
;
1162 CRYPTO_THREAD_unlock(store
->lock
);
1166 = evp_method_store_cache_flush(prov
->libctx
)
1168 + ossl_encoder_store_cache_flush(prov
->libctx
)
1169 + ossl_decoder_store_cache_flush(prov
->libctx
)
1170 + ossl_store_loader_store_cache_flush(prov
->libctx
)
1183 static int provider_remove_store_methods(OSSL_PROVIDER
*prov
)
1185 struct provider_store_st
*store
;
1188 if ((store
= get_provider_store(prov
->libctx
)) == NULL
)
1191 if (!CRYPTO_THREAD_read_lock(store
->lock
))
1193 freeing
= store
->freeing
;
1194 CRYPTO_THREAD_unlock(store
->lock
);
1199 if (!CRYPTO_THREAD_read_lock(prov
->opbits_lock
))
1201 OPENSSL_free(prov
->operation_bits
);
1202 prov
->operation_bits
= NULL
;
1203 prov
->operation_bits_sz
= 0;
1204 CRYPTO_THREAD_unlock(prov
->opbits_lock
);
1206 acc
= evp_method_store_remove_all_provided(prov
)
1208 + ossl_encoder_store_remove_all_provided(prov
)
1209 + ossl_decoder_store_remove_all_provided(prov
)
1210 + ossl_store_loader_store_remove_all_provided(prov
)
1223 int ossl_provider_activate(OSSL_PROVIDER
*prov
, int upcalls
, int aschild
)
1231 * If aschild is true, then we only actually do the activation if the
1232 * provider is a child. If its not, this is still success.
1234 if (aschild
&& !prov
->ischild
)
1237 if ((count
= provider_activate(prov
, 1, upcalls
)) > 0)
1238 return count
== 1 ? provider_flush_store_cache(prov
) : 1;
1243 int ossl_provider_deactivate(OSSL_PROVIDER
*prov
, int removechildren
)
1248 || (count
= provider_deactivate(prov
, 1, removechildren
)) < 0)
1250 return count
== 0 ? provider_remove_store_methods(prov
) : 1;
1253 void *ossl_provider_ctx(const OSSL_PROVIDER
*prov
)
1255 return prov
!= NULL
? prov
->provctx
: NULL
;
1259 * This function only does something once when store->use_fallbacks == 1,
1260 * and then sets store->use_fallbacks = 0, so the second call and so on is
1261 * effectively a no-op.
1263 static int provider_activate_fallbacks(struct provider_store_st
*store
)
1266 int activated_fallback_count
= 0;
1268 const OSSL_PROVIDER_INFO
*p
;
1270 if (!CRYPTO_THREAD_read_lock(store
->lock
))
1272 use_fallbacks
= store
->use_fallbacks
;
1273 CRYPTO_THREAD_unlock(store
->lock
);
1277 if (!CRYPTO_THREAD_write_lock(store
->lock
))
1279 /* Check again, just in case another thread changed it */
1280 use_fallbacks
= store
->use_fallbacks
;
1281 if (!use_fallbacks
) {
1282 CRYPTO_THREAD_unlock(store
->lock
);
1286 for (p
= ossl_predefined_providers
; p
->name
!= NULL
; p
++) {
1287 OSSL_PROVIDER
*prov
= NULL
;
1289 if (!p
->is_fallback
)
1292 * We use the internal constructor directly here,
1293 * otherwise we get a call loop
1295 prov
= provider_new(p
->name
, p
->init
, NULL
);
1298 prov
->libctx
= store
->libctx
;
1300 prov
->error_lib
= ERR_get_next_error_library();
1304 * We are calling provider_activate while holding the store lock. This
1305 * means the init function will be called while holding a lock. Normally
1306 * we try to avoid calling a user callback while holding a lock.
1307 * However, fallbacks are never third party providers so we accept this.
1309 if (provider_activate(prov
, 0, 0) < 0) {
1310 ossl_provider_free(prov
);
1313 prov
->store
= store
;
1314 if (sk_OSSL_PROVIDER_push(store
->providers
, prov
) == 0) {
1315 ossl_provider_free(prov
);
1318 activated_fallback_count
++;
1321 if (activated_fallback_count
> 0) {
1322 store
->use_fallbacks
= 0;
1326 CRYPTO_THREAD_unlock(store
->lock
);
1330 int ossl_provider_doall_activated(OSSL_LIB_CTX
*ctx
,
1331 int (*cb
)(OSSL_PROVIDER
*provider
,
1335 int ret
= 0, curr
, max
, ref
= 0;
1336 struct provider_store_st
*store
= get_provider_store(ctx
);
1337 STACK_OF(OSSL_PROVIDER
) *provs
= NULL
;
1341 * Make sure any providers are loaded from config before we try to use
1344 if (ossl_lib_ctx_is_default(ctx
))
1345 OPENSSL_init_crypto(OPENSSL_INIT_LOAD_CONFIG
, NULL
);
1350 if (!provider_activate_fallbacks(store
))
1354 * Under lock, grab a copy of the provider list and up_ref each
1355 * provider so that they don't disappear underneath us.
1357 if (!CRYPTO_THREAD_read_lock(store
->lock
))
1359 provs
= sk_OSSL_PROVIDER_dup(store
->providers
);
1360 if (provs
== NULL
) {
1361 CRYPTO_THREAD_unlock(store
->lock
);
1364 max
= sk_OSSL_PROVIDER_num(provs
);
1366 * We work backwards through the stack so that we can safely delete items
1369 for (curr
= max
- 1; curr
>= 0; curr
--) {
1370 OSSL_PROVIDER
*prov
= sk_OSSL_PROVIDER_value(provs
, curr
);
1372 if (!CRYPTO_THREAD_write_lock(prov
->flag_lock
))
1374 if (prov
->flag_activated
) {
1376 * We call CRYPTO_UP_REF directly rather than ossl_provider_up_ref
1377 * to avoid upping the ref count on the parent provider, which we
1378 * must not do while holding locks.
1380 if (CRYPTO_UP_REF(&prov
->refcnt
, &ref
, prov
->refcnt_lock
) <= 0) {
1381 CRYPTO_THREAD_unlock(prov
->flag_lock
);
1385 * It's already activated, but we up the activated count to ensure
1386 * it remains activated until after we've called the user callback.
1387 * We do this with no locking (because we already hold the locks)
1388 * and no upcalls (which must not be called when locks are held). In
1389 * theory this could mean the parent provider goes inactive, whilst
1390 * still activated in the child for a short period. That's ok.
1392 if (provider_activate(prov
, 0, 0) < 0) {
1393 CRYPTO_DOWN_REF(&prov
->refcnt
, &ref
, prov
->refcnt_lock
);
1394 CRYPTO_THREAD_unlock(prov
->flag_lock
);
1398 sk_OSSL_PROVIDER_delete(provs
, curr
);
1401 CRYPTO_THREAD_unlock(prov
->flag_lock
);
1403 CRYPTO_THREAD_unlock(store
->lock
);
1406 * Now, we sweep through all providers not under lock
1408 for (curr
= 0; curr
< max
; curr
++) {
1409 OSSL_PROVIDER
*prov
= sk_OSSL_PROVIDER_value(provs
, curr
);
1411 if (!cb(prov
, cbdata
)) {
1422 CRYPTO_THREAD_unlock(store
->lock
);
1425 * The pop_free call doesn't do what we want on an error condition. We
1426 * either start from the first item in the stack, or part way through if
1427 * we only processed some of the items.
1429 for (curr
++; curr
< max
; curr
++) {
1430 OSSL_PROVIDER
*prov
= sk_OSSL_PROVIDER_value(provs
, curr
);
1432 provider_deactivate(prov
, 0, 1);
1434 * As above where we did the up-ref, we don't call ossl_provider_free
1435 * to avoid making upcalls. There should always be at least one ref
1436 * to the provider in the store, so this should never drop to 0.
1438 CRYPTO_DOWN_REF(&prov
->refcnt
, &ref
, prov
->refcnt_lock
);
1440 * Not much we can do if this assert ever fails. So we don't use
1445 sk_OSSL_PROVIDER_free(provs
);
1449 int OSSL_PROVIDER_available(OSSL_LIB_CTX
*libctx
, const char *name
)
1451 OSSL_PROVIDER
*prov
= NULL
;
1453 struct provider_store_st
*store
= get_provider_store(libctx
);
1455 if (store
== NULL
|| !provider_activate_fallbacks(store
))
1458 prov
= ossl_provider_find(libctx
, name
, 0);
1460 if (!CRYPTO_THREAD_read_lock(prov
->flag_lock
))
1462 available
= prov
->flag_activated
;
1463 CRYPTO_THREAD_unlock(prov
->flag_lock
);
1464 ossl_provider_free(prov
);
1469 /* Getters of Provider Object data */
1470 const char *ossl_provider_name(const OSSL_PROVIDER
*prov
)
1475 const DSO
*ossl_provider_dso(const OSSL_PROVIDER
*prov
)
1477 return prov
->module
;
1480 const char *ossl_provider_module_name(const OSSL_PROVIDER
*prov
)
1485 return DSO_get_filename(prov
->module
);
1489 const char *ossl_provider_module_path(const OSSL_PROVIDER
*prov
)
1494 /* FIXME: Ensure it's a full path */
1495 return DSO_get_filename(prov
->module
);
1499 void *ossl_provider_prov_ctx(const OSSL_PROVIDER
*prov
)
1502 return prov
->provctx
;
1507 const OSSL_DISPATCH
*ossl_provider_get0_dispatch(const OSSL_PROVIDER
*prov
)
1510 return prov
->dispatch
;
1515 OSSL_LIB_CTX
*ossl_provider_libctx(const OSSL_PROVIDER
*prov
)
1517 return prov
!= NULL
? prov
->libctx
: NULL
;
1520 /* Wrappers around calls to the provider */
1521 void ossl_provider_teardown(const OSSL_PROVIDER
*prov
)
1523 if (prov
->teardown
!= NULL
1528 prov
->teardown(prov
->provctx
);
1531 const OSSL_PARAM
*ossl_provider_gettable_params(const OSSL_PROVIDER
*prov
)
1533 return prov
->gettable_params
== NULL
1534 ? NULL
: prov
->gettable_params(prov
->provctx
);
1537 int ossl_provider_get_params(const OSSL_PROVIDER
*prov
, OSSL_PARAM params
[])
1539 return prov
->get_params
== NULL
1540 ? 0 : prov
->get_params(prov
->provctx
, params
);
1543 int ossl_provider_self_test(const OSSL_PROVIDER
*prov
)
1547 if (prov
->self_test
== NULL
)
1549 ret
= prov
->self_test(prov
->provctx
);
1551 (void)provider_remove_store_methods((OSSL_PROVIDER
*)prov
);
1555 int ossl_provider_get_capabilities(const OSSL_PROVIDER
*prov
,
1556 const char *capability
,
1560 return prov
->get_capabilities
== NULL
1561 ? 1 : prov
->get_capabilities(prov
->provctx
, capability
, cb
, arg
);
1564 const OSSL_ALGORITHM
*ossl_provider_query_operation(const OSSL_PROVIDER
*prov
,
1568 const OSSL_ALGORITHM
*res
;
1570 if (prov
->query_operation
== NULL
)
1572 res
= prov
->query_operation(prov
->provctx
, operation_id
, no_cache
);
1573 #if defined(OPENSSL_NO_CACHED_FETCH)
1574 /* Forcing the non-caching of queries */
1575 if (no_cache
!= NULL
)
1581 void ossl_provider_unquery_operation(const OSSL_PROVIDER
*prov
,
1583 const OSSL_ALGORITHM
*algs
)
1585 if (prov
->unquery_operation
!= NULL
)
1586 prov
->unquery_operation(prov
->provctx
, operation_id
, algs
);
1589 int ossl_provider_set_operation_bit(OSSL_PROVIDER
*provider
, size_t bitnum
)
1591 size_t byte
= bitnum
/ 8;
1592 unsigned char bit
= (1 << (bitnum
% 8)) & 0xFF;
1594 if (!CRYPTO_THREAD_write_lock(provider
->opbits_lock
))
1596 if (provider
->operation_bits_sz
<= byte
) {
1597 unsigned char *tmp
= OPENSSL_realloc(provider
->operation_bits
,
1601 CRYPTO_THREAD_unlock(provider
->opbits_lock
);
1602 ERR_raise(ERR_LIB_CRYPTO
, ERR_R_MALLOC_FAILURE
);
1605 provider
->operation_bits
= tmp
;
1606 memset(provider
->operation_bits
+ provider
->operation_bits_sz
,
1607 '\0', byte
+ 1 - provider
->operation_bits_sz
);
1608 provider
->operation_bits_sz
= byte
+ 1;
1610 provider
->operation_bits
[byte
] |= bit
;
1611 CRYPTO_THREAD_unlock(provider
->opbits_lock
);
1615 int ossl_provider_test_operation_bit(OSSL_PROVIDER
*provider
, size_t bitnum
,
1618 size_t byte
= bitnum
/ 8;
1619 unsigned char bit
= (1 << (bitnum
% 8)) & 0xFF;
1621 if (!ossl_assert(result
!= NULL
)) {
1622 ERR_raise(ERR_LIB_CRYPTO
, ERR_R_PASSED_NULL_PARAMETER
);
1627 if (!CRYPTO_THREAD_read_lock(provider
->opbits_lock
))
1629 if (provider
->operation_bits_sz
> byte
)
1630 *result
= ((provider
->operation_bits
[byte
] & bit
) != 0);
1631 CRYPTO_THREAD_unlock(provider
->opbits_lock
);
1636 const OSSL_CORE_HANDLE
*ossl_provider_get_parent(OSSL_PROVIDER
*prov
)
1638 return prov
->handle
;
1641 int ossl_provider_is_child(const OSSL_PROVIDER
*prov
)
1643 return prov
->ischild
;
1646 int ossl_provider_set_child(OSSL_PROVIDER
*prov
, const OSSL_CORE_HANDLE
*handle
)
1648 prov
->handle
= handle
;
1654 int ossl_provider_default_props_update(OSSL_LIB_CTX
*libctx
, const char *props
)
1657 struct provider_store_st
*store
= NULL
;
1659 OSSL_PROVIDER_CHILD_CB
*child_cb
;
1661 if ((store
= get_provider_store(libctx
)) == NULL
)
1664 if (!CRYPTO_THREAD_read_lock(store
->lock
))
1667 max
= sk_OSSL_PROVIDER_CHILD_CB_num(store
->child_cbs
);
1668 for (i
= 0; i
< max
; i
++) {
1669 child_cb
= sk_OSSL_PROVIDER_CHILD_CB_value(store
->child_cbs
, i
);
1670 child_cb
->global_props_cb(props
, child_cb
->cbdata
);
1673 CRYPTO_THREAD_unlock(store
->lock
);
1678 static int ossl_provider_register_child_cb(const OSSL_CORE_HANDLE
*handle
,
1680 const OSSL_CORE_HANDLE
*provider
,
1683 const OSSL_CORE_HANDLE
*provider
,
1685 int (*global_props_cb
)(
1691 * This is really an OSSL_PROVIDER that we created and cast to
1692 * OSSL_CORE_HANDLE originally. Therefore it is safe to cast it back.
1694 OSSL_PROVIDER
*thisprov
= (OSSL_PROVIDER
*)handle
;
1695 OSSL_PROVIDER
*prov
;
1696 OSSL_LIB_CTX
*libctx
= thisprov
->libctx
;
1697 struct provider_store_st
*store
= NULL
;
1698 int ret
= 0, i
, max
;
1699 OSSL_PROVIDER_CHILD_CB
*child_cb
;
1700 char *propsstr
= NULL
;
1702 if ((store
= get_provider_store(libctx
)) == NULL
)
1705 child_cb
= OPENSSL_malloc(sizeof(*child_cb
));
1706 if (child_cb
== NULL
)
1708 child_cb
->prov
= thisprov
;
1709 child_cb
->create_cb
= create_cb
;
1710 child_cb
->remove_cb
= remove_cb
;
1711 child_cb
->global_props_cb
= global_props_cb
;
1712 child_cb
->cbdata
= cbdata
;
1714 if (!CRYPTO_THREAD_write_lock(store
->lock
)) {
1715 OPENSSL_free(child_cb
);
1718 propsstr
= evp_get_global_properties_str(libctx
, 0);
1720 if (propsstr
!= NULL
) {
1721 global_props_cb(propsstr
, cbdata
);
1722 OPENSSL_free(propsstr
);
1724 max
= sk_OSSL_PROVIDER_num(store
->providers
);
1725 for (i
= 0; i
< max
; i
++) {
1728 prov
= sk_OSSL_PROVIDER_value(store
->providers
, i
);
1730 if (!CRYPTO_THREAD_read_lock(prov
->flag_lock
))
1732 activated
= prov
->flag_activated
;
1733 CRYPTO_THREAD_unlock(prov
->flag_lock
);
1735 * We hold the store lock while calling the user callback. This means
1736 * that the user callback must be short and simple and not do anything
1737 * likely to cause a deadlock. We don't hold the flag_lock during this
1738 * call. In theory this means that another thread could deactivate it
1739 * while we are calling create. This is ok because the other thread
1740 * will also call remove_cb, but won't be able to do so until we release
1743 if (activated
&& !create_cb((OSSL_CORE_HANDLE
*)prov
, cbdata
))
1748 ret
= sk_OSSL_PROVIDER_CHILD_CB_push(store
->child_cbs
, child_cb
);
1750 if (i
!= max
|| ret
<= 0) {
1751 /* Failed during creation. Remove everything we just added */
1752 for (; i
>= 0; i
--) {
1753 prov
= sk_OSSL_PROVIDER_value(store
->providers
, i
);
1754 remove_cb((OSSL_CORE_HANDLE
*)prov
, cbdata
);
1756 OPENSSL_free(child_cb
);
1759 CRYPTO_THREAD_unlock(store
->lock
);
1764 static void ossl_provider_deregister_child_cb(const OSSL_CORE_HANDLE
*handle
)
1767 * This is really an OSSL_PROVIDER that we created and cast to
1768 * OSSL_CORE_HANDLE originally. Therefore it is safe to cast it back.
1770 OSSL_PROVIDER
*thisprov
= (OSSL_PROVIDER
*)handle
;
1771 OSSL_LIB_CTX
*libctx
= thisprov
->libctx
;
1772 struct provider_store_st
*store
= NULL
;
1774 OSSL_PROVIDER_CHILD_CB
*child_cb
;
1776 if ((store
= get_provider_store(libctx
)) == NULL
)
1779 if (!CRYPTO_THREAD_write_lock(store
->lock
))
1781 max
= sk_OSSL_PROVIDER_CHILD_CB_num(store
->child_cbs
);
1782 for (i
= 0; i
< max
; i
++) {
1783 child_cb
= sk_OSSL_PROVIDER_CHILD_CB_value(store
->child_cbs
, i
);
1784 if (child_cb
->prov
== thisprov
) {
1785 /* Found an entry */
1786 sk_OSSL_PROVIDER_CHILD_CB_delete(store
->child_cbs
, i
);
1787 OPENSSL_free(child_cb
);
1791 CRYPTO_THREAD_unlock(store
->lock
);
1796 * Core functions for the provider
1797 * ===============================
1799 * This is the set of functions that the core makes available to the provider
1803 * This returns a list of Provider Object parameters with their types, for
1804 * discovery. We do not expect that many providers will use this, but one
1807 static const OSSL_PARAM param_types
[] = {
1808 OSSL_PARAM_DEFN(OSSL_PROV_PARAM_CORE_VERSION
, OSSL_PARAM_UTF8_PTR
, NULL
, 0),
1809 OSSL_PARAM_DEFN(OSSL_PROV_PARAM_CORE_PROV_NAME
, OSSL_PARAM_UTF8_PTR
,
1812 OSSL_PARAM_DEFN(OSSL_PROV_PARAM_CORE_MODULE_FILENAME
, OSSL_PARAM_UTF8_PTR
,
1819 * Forward declare all the functions that are provided aa dispatch.
1820 * This ensures that the compiler will complain if they aren't defined
1821 * with the correct signature.
1823 static OSSL_FUNC_core_gettable_params_fn core_gettable_params
;
1824 static OSSL_FUNC_core_get_params_fn core_get_params
;
1825 static OSSL_FUNC_core_get_libctx_fn core_get_libctx
;
1826 static OSSL_FUNC_core_thread_start_fn core_thread_start
;
1828 static OSSL_FUNC_core_new_error_fn core_new_error
;
1829 static OSSL_FUNC_core_set_error_debug_fn core_set_error_debug
;
1830 static OSSL_FUNC_core_vset_error_fn core_vset_error
;
1831 static OSSL_FUNC_core_set_error_mark_fn core_set_error_mark
;
1832 static OSSL_FUNC_core_clear_last_error_mark_fn core_clear_last_error_mark
;
1833 static OSSL_FUNC_core_pop_error_to_mark_fn core_pop_error_to_mark
;
1834 OSSL_FUNC_BIO_new_file_fn ossl_core_bio_new_file
;
1835 OSSL_FUNC_BIO_new_membuf_fn ossl_core_bio_new_mem_buf
;
1836 OSSL_FUNC_BIO_read_ex_fn ossl_core_bio_read_ex
;
1837 OSSL_FUNC_BIO_write_ex_fn ossl_core_bio_write_ex
;
1838 OSSL_FUNC_BIO_gets_fn ossl_core_bio_gets
;
1839 OSSL_FUNC_BIO_puts_fn ossl_core_bio_puts
;
1840 OSSL_FUNC_BIO_up_ref_fn ossl_core_bio_up_ref
;
1841 OSSL_FUNC_BIO_free_fn ossl_core_bio_free
;
1842 OSSL_FUNC_BIO_vprintf_fn ossl_core_bio_vprintf
;
1843 OSSL_FUNC_BIO_vsnprintf_fn BIO_vsnprintf
;
1844 static OSSL_FUNC_self_test_cb_fn core_self_test_get_callback
;
1845 OSSL_FUNC_get_entropy_fn ossl_rand_get_entropy
;
1846 OSSL_FUNC_cleanup_entropy_fn ossl_rand_cleanup_entropy
;
1847 OSSL_FUNC_get_nonce_fn ossl_rand_get_nonce
;
1848 OSSL_FUNC_cleanup_nonce_fn ossl_rand_cleanup_nonce
;
1850 OSSL_FUNC_CRYPTO_malloc_fn CRYPTO_malloc
;
1851 OSSL_FUNC_CRYPTO_zalloc_fn CRYPTO_zalloc
;
1852 OSSL_FUNC_CRYPTO_free_fn CRYPTO_free
;
1853 OSSL_FUNC_CRYPTO_clear_free_fn CRYPTO_clear_free
;
1854 OSSL_FUNC_CRYPTO_realloc_fn CRYPTO_realloc
;
1855 OSSL_FUNC_CRYPTO_clear_realloc_fn CRYPTO_clear_realloc
;
1856 OSSL_FUNC_CRYPTO_secure_malloc_fn CRYPTO_secure_malloc
;
1857 OSSL_FUNC_CRYPTO_secure_zalloc_fn CRYPTO_secure_zalloc
;
1858 OSSL_FUNC_CRYPTO_secure_free_fn CRYPTO_secure_free
;
1859 OSSL_FUNC_CRYPTO_secure_clear_free_fn CRYPTO_secure_clear_free
;
1860 OSSL_FUNC_CRYPTO_secure_allocated_fn CRYPTO_secure_allocated
;
1861 OSSL_FUNC_OPENSSL_cleanse_fn OPENSSL_cleanse
;
1863 OSSL_FUNC_provider_register_child_cb_fn ossl_provider_register_child_cb
;
1864 OSSL_FUNC_provider_deregister_child_cb_fn ossl_provider_deregister_child_cb
;
1865 static OSSL_FUNC_provider_name_fn core_provider_get0_name
;
1866 static OSSL_FUNC_provider_get0_provider_ctx_fn core_provider_get0_provider_ctx
;
1867 static OSSL_FUNC_provider_get0_dispatch_fn core_provider_get0_dispatch
;
1868 static OSSL_FUNC_provider_up_ref_fn core_provider_up_ref_intern
;
1869 static OSSL_FUNC_provider_free_fn core_provider_free_intern
;
1870 static OSSL_FUNC_core_obj_add_sigid_fn core_obj_add_sigid
;
1871 static OSSL_FUNC_core_obj_create_fn core_obj_create
;
1874 static const OSSL_PARAM
*core_gettable_params(const OSSL_CORE_HANDLE
*handle
)
1879 static int core_get_params(const OSSL_CORE_HANDLE
*handle
, OSSL_PARAM params
[])
1884 * We created this object originally and we know it is actually an
1885 * OSSL_PROVIDER *, so the cast is safe
1887 OSSL_PROVIDER
*prov
= (OSSL_PROVIDER
*)handle
;
1889 if ((p
= OSSL_PARAM_locate(params
, OSSL_PROV_PARAM_CORE_VERSION
)) != NULL
)
1890 OSSL_PARAM_set_utf8_ptr(p
, OPENSSL_VERSION_STR
);
1891 if ((p
= OSSL_PARAM_locate(params
, OSSL_PROV_PARAM_CORE_PROV_NAME
)) != NULL
)
1892 OSSL_PARAM_set_utf8_ptr(p
, prov
->name
);
1895 if ((p
= OSSL_PARAM_locate(params
,
1896 OSSL_PROV_PARAM_CORE_MODULE_FILENAME
)) != NULL
)
1897 OSSL_PARAM_set_utf8_ptr(p
, ossl_provider_module_path(prov
));
1900 if (prov
->parameters
== NULL
)
1903 for (i
= 0; i
< sk_INFOPAIR_num(prov
->parameters
); i
++) {
1904 INFOPAIR
*pair
= sk_INFOPAIR_value(prov
->parameters
, i
);
1906 if ((p
= OSSL_PARAM_locate(params
, pair
->name
)) != NULL
)
1907 OSSL_PARAM_set_utf8_ptr(p
, pair
->value
);
1912 static OPENSSL_CORE_CTX
*core_get_libctx(const OSSL_CORE_HANDLE
*handle
)
1915 * We created this object originally and we know it is actually an
1916 * OSSL_PROVIDER *, so the cast is safe
1918 OSSL_PROVIDER
*prov
= (OSSL_PROVIDER
*)handle
;
1921 * Using ossl_provider_libctx would be wrong as that returns
1922 * NULL for |prov| == NULL and NULL libctx has a special meaning
1923 * that does not apply here. Here |prov| == NULL can happen only in
1924 * case of a coding error.
1926 assert(prov
!= NULL
);
1927 return (OPENSSL_CORE_CTX
*)prov
->libctx
;
1930 static int core_thread_start(const OSSL_CORE_HANDLE
*handle
,
1931 OSSL_thread_stop_handler_fn handfn
,
1935 * We created this object originally and we know it is actually an
1936 * OSSL_PROVIDER *, so the cast is safe
1938 OSSL_PROVIDER
*prov
= (OSSL_PROVIDER
*)handle
;
1940 return ossl_init_thread_start(prov
, arg
, handfn
);
1944 * The FIPS module inner provider doesn't implement these. They aren't
1945 * needed there, since the FIPS module upcalls are always the outer provider
1950 * These error functions should use |handle| to select the proper
1951 * library context to report in the correct error stack if error
1952 * stacks become tied to the library context.
1953 * We cannot currently do that since there's no support for it in the
1956 static void core_new_error(const OSSL_CORE_HANDLE
*handle
)
1961 static void core_set_error_debug(const OSSL_CORE_HANDLE
*handle
,
1962 const char *file
, int line
, const char *func
)
1964 ERR_set_debug(file
, line
, func
);
1967 static void core_vset_error(const OSSL_CORE_HANDLE
*handle
,
1968 uint32_t reason
, const char *fmt
, va_list args
)
1971 * We created this object originally and we know it is actually an
1972 * OSSL_PROVIDER *, so the cast is safe
1974 OSSL_PROVIDER
*prov
= (OSSL_PROVIDER
*)handle
;
1977 * If the uppermost 8 bits are non-zero, it's an OpenSSL library
1978 * error and will be treated as such. Otherwise, it's a new style
1979 * provider error and will be treated as such.
1981 if (ERR_GET_LIB(reason
) != 0) {
1982 ERR_vset_error(ERR_GET_LIB(reason
), ERR_GET_REASON(reason
), fmt
, args
);
1984 ERR_vset_error(prov
->error_lib
, (int)reason
, fmt
, args
);
1988 static int core_set_error_mark(const OSSL_CORE_HANDLE
*handle
)
1990 return ERR_set_mark();
1993 static int core_clear_last_error_mark(const OSSL_CORE_HANDLE
*handle
)
1995 return ERR_clear_last_mark();
1998 static int core_pop_error_to_mark(const OSSL_CORE_HANDLE
*handle
)
2000 return ERR_pop_to_mark();
2003 static void core_self_test_get_callback(OPENSSL_CORE_CTX
*libctx
,
2004 OSSL_CALLBACK
**cb
, void **cbarg
)
2006 OSSL_SELF_TEST_get_callback((OSSL_LIB_CTX
*)libctx
, cb
, cbarg
);
2009 static const char *core_provider_get0_name(const OSSL_CORE_HANDLE
*prov
)
2011 return OSSL_PROVIDER_get0_name((const OSSL_PROVIDER
*)prov
);
2014 static void *core_provider_get0_provider_ctx(const OSSL_CORE_HANDLE
*prov
)
2016 return OSSL_PROVIDER_get0_provider_ctx((const OSSL_PROVIDER
*)prov
);
2019 static const OSSL_DISPATCH
*
2020 core_provider_get0_dispatch(const OSSL_CORE_HANDLE
*prov
)
2022 return OSSL_PROVIDER_get0_dispatch((const OSSL_PROVIDER
*)prov
);
2025 static int core_provider_up_ref_intern(const OSSL_CORE_HANDLE
*prov
,
2028 return provider_up_ref_intern((OSSL_PROVIDER
*)prov
, activate
);
2031 static int core_provider_free_intern(const OSSL_CORE_HANDLE
*prov
,
2034 return provider_free_intern((OSSL_PROVIDER
*)prov
, deactivate
);
2037 static int core_obj_add_sigid(const OSSL_CORE_HANDLE
*prov
,
2038 const char *sign_name
, const char *digest_name
,
2039 const char *pkey_name
)
2041 int sign_nid
= OBJ_txt2nid(sign_name
);
2042 int digest_nid
= NID_undef
;
2043 int pkey_nid
= OBJ_txt2nid(pkey_name
);
2045 if (digest_name
!= NULL
&& digest_name
[0] != '\0'
2046 && (digest_nid
= OBJ_txt2nid(digest_name
)) == NID_undef
)
2049 if (sign_nid
== NID_undef
)
2053 * Check if it already exists. This is a success if so (even if we don't
2054 * have nids for the digest/pkey)
2056 if (OBJ_find_sigid_algs(sign_nid
, NULL
, NULL
))
2059 if (pkey_nid
== NID_undef
)
2062 return OBJ_add_sigid(sign_nid
, digest_nid
, pkey_nid
);
2065 static int core_obj_create(const OSSL_CORE_HANDLE
*prov
, const char *oid
,
2066 const char *sn
, const char *ln
)
2068 /* Check if it already exists and create it if not */
2069 return OBJ_txt2nid(oid
) != NID_undef
2070 || OBJ_create(oid
, sn
, ln
) != NID_undef
;
2072 #endif /* FIPS_MODULE */
2075 * Functions provided by the core.
2077 static const OSSL_DISPATCH core_dispatch_
[] = {
2078 { OSSL_FUNC_CORE_GETTABLE_PARAMS
, (void (*)(void))core_gettable_params
},
2079 { OSSL_FUNC_CORE_GET_PARAMS
, (void (*)(void))core_get_params
},
2080 { OSSL_FUNC_CORE_GET_LIBCTX
, (void (*)(void))core_get_libctx
},
2081 { OSSL_FUNC_CORE_THREAD_START
, (void (*)(void))core_thread_start
},
2083 { OSSL_FUNC_CORE_NEW_ERROR
, (void (*)(void))core_new_error
},
2084 { OSSL_FUNC_CORE_SET_ERROR_DEBUG
, (void (*)(void))core_set_error_debug
},
2085 { OSSL_FUNC_CORE_VSET_ERROR
, (void (*)(void))core_vset_error
},
2086 { OSSL_FUNC_CORE_SET_ERROR_MARK
, (void (*)(void))core_set_error_mark
},
2087 { OSSL_FUNC_CORE_CLEAR_LAST_ERROR_MARK
,
2088 (void (*)(void))core_clear_last_error_mark
},
2089 { OSSL_FUNC_CORE_POP_ERROR_TO_MARK
, (void (*)(void))core_pop_error_to_mark
},
2090 { OSSL_FUNC_BIO_NEW_FILE
, (void (*)(void))ossl_core_bio_new_file
},
2091 { OSSL_FUNC_BIO_NEW_MEMBUF
, (void (*)(void))ossl_core_bio_new_mem_buf
},
2092 { OSSL_FUNC_BIO_READ_EX
, (void (*)(void))ossl_core_bio_read_ex
},
2093 { OSSL_FUNC_BIO_WRITE_EX
, (void (*)(void))ossl_core_bio_write_ex
},
2094 { OSSL_FUNC_BIO_GETS
, (void (*)(void))ossl_core_bio_gets
},
2095 { OSSL_FUNC_BIO_PUTS
, (void (*)(void))ossl_core_bio_puts
},
2096 { OSSL_FUNC_BIO_CTRL
, (void (*)(void))ossl_core_bio_ctrl
},
2097 { OSSL_FUNC_BIO_UP_REF
, (void (*)(void))ossl_core_bio_up_ref
},
2098 { OSSL_FUNC_BIO_FREE
, (void (*)(void))ossl_core_bio_free
},
2099 { OSSL_FUNC_BIO_VPRINTF
, (void (*)(void))ossl_core_bio_vprintf
},
2100 { OSSL_FUNC_BIO_VSNPRINTF
, (void (*)(void))BIO_vsnprintf
},
2101 { OSSL_FUNC_SELF_TEST_CB
, (void (*)(void))core_self_test_get_callback
},
2102 { OSSL_FUNC_GET_ENTROPY
, (void (*)(void))ossl_rand_get_entropy
},
2103 { OSSL_FUNC_CLEANUP_ENTROPY
, (void (*)(void))ossl_rand_cleanup_entropy
},
2104 { OSSL_FUNC_GET_NONCE
, (void (*)(void))ossl_rand_get_nonce
},
2105 { OSSL_FUNC_CLEANUP_NONCE
, (void (*)(void))ossl_rand_cleanup_nonce
},
2107 { OSSL_FUNC_CRYPTO_MALLOC
, (void (*)(void))CRYPTO_malloc
},
2108 { OSSL_FUNC_CRYPTO_ZALLOC
, (void (*)(void))CRYPTO_zalloc
},
2109 { OSSL_FUNC_CRYPTO_FREE
, (void (*)(void))CRYPTO_free
},
2110 { OSSL_FUNC_CRYPTO_CLEAR_FREE
, (void (*)(void))CRYPTO_clear_free
},
2111 { OSSL_FUNC_CRYPTO_REALLOC
, (void (*)(void))CRYPTO_realloc
},
2112 { OSSL_FUNC_CRYPTO_CLEAR_REALLOC
, (void (*)(void))CRYPTO_clear_realloc
},
2113 { OSSL_FUNC_CRYPTO_SECURE_MALLOC
, (void (*)(void))CRYPTO_secure_malloc
},
2114 { OSSL_FUNC_CRYPTO_SECURE_ZALLOC
, (void (*)(void))CRYPTO_secure_zalloc
},
2115 { OSSL_FUNC_CRYPTO_SECURE_FREE
, (void (*)(void))CRYPTO_secure_free
},
2116 { OSSL_FUNC_CRYPTO_SECURE_CLEAR_FREE
,
2117 (void (*)(void))CRYPTO_secure_clear_free
},
2118 { OSSL_FUNC_CRYPTO_SECURE_ALLOCATED
,
2119 (void (*)(void))CRYPTO_secure_allocated
},
2120 { OSSL_FUNC_OPENSSL_CLEANSE
, (void (*)(void))OPENSSL_cleanse
},
2122 { OSSL_FUNC_PROVIDER_REGISTER_CHILD_CB
,
2123 (void (*)(void))ossl_provider_register_child_cb
},
2124 { OSSL_FUNC_PROVIDER_DEREGISTER_CHILD_CB
,
2125 (void (*)(void))ossl_provider_deregister_child_cb
},
2126 { OSSL_FUNC_PROVIDER_NAME
,
2127 (void (*)(void))core_provider_get0_name
},
2128 { OSSL_FUNC_PROVIDER_GET0_PROVIDER_CTX
,
2129 (void (*)(void))core_provider_get0_provider_ctx
},
2130 { OSSL_FUNC_PROVIDER_GET0_DISPATCH
,
2131 (void (*)(void))core_provider_get0_dispatch
},
2132 { OSSL_FUNC_PROVIDER_UP_REF
,
2133 (void (*)(void))core_provider_up_ref_intern
},
2134 { OSSL_FUNC_PROVIDER_FREE
,
2135 (void (*)(void))core_provider_free_intern
},
2136 { OSSL_FUNC_CORE_OBJ_ADD_SIGID
, (void (*)(void))core_obj_add_sigid
},
2137 { OSSL_FUNC_CORE_OBJ_CREATE
, (void (*)(void))core_obj_create
},
2141 static const OSSL_DISPATCH
*core_dispatch
= core_dispatch_
;