2 * Copyright 2019-2021 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"
18 #include "crypto/evp.h" /* evp_method_store_flush */
19 #include "crypto/rand.h"
20 #include "internal/nelem.h"
21 #include "internal/thread_once.h"
22 #include "internal/provider.h"
23 #include "internal/refcount.h"
24 #include "internal/bio.h"
25 #include "internal/core.h"
26 #include "provider_local.h"
28 # include <openssl/self_test.h>
31 static OSSL_PROVIDER
*provider_new(const char *name
,
32 OSSL_provider_init_fn
*init_function
);
35 * Provider Object structure
36 * =========================
43 DEFINE_STACK_OF(INFOPAIR
)
48 int (*create_cb
)(const OSSL_CORE_HANDLE
*provider
, void *cbdata
);
49 int (*remove_cb
)(const OSSL_CORE_HANDLE
*provider
, void *cbdata
);
50 int (*global_props_cb
)(const char *props
, void *cbdata
);
52 } OSSL_PROVIDER_CHILD_CB
;
53 DEFINE_STACK_OF(OSSL_PROVIDER_CHILD_CB
)
56 struct provider_store_st
; /* Forward declaration */
58 struct ossl_provider_st
{
60 unsigned int flag_initialized
:1;
61 unsigned int flag_activated
:1;
62 unsigned int flag_fallback
:1; /* Can be used as fallback */
64 unsigned int flag_couldbechild
:1;
67 /* Getting and setting the flags require synchronization */
68 CRYPTO_RWLOCK
*flag_lock
;
70 /* OpenSSL library side data */
71 CRYPTO_REF_COUNT refcnt
;
72 CRYPTO_RWLOCK
*refcnt_lock
; /* For the ref counter */
77 OSSL_provider_init_fn
*init_function
;
78 STACK_OF(INFOPAIR
) *parameters
;
79 OSSL_LIB_CTX
*libctx
; /* The library context this instance is in */
80 struct provider_store_st
*store
; /* The store this instance belongs to */
83 * In the FIPS module inner provider, this isn't needed, since the
84 * error upcalls are always direct calls to the outer provider.
86 int error_lib
; /* ERR library number, one for each provider */
87 # ifndef OPENSSL_NO_ERR
88 ERR_STRING_DATA
*error_strings
; /* Copy of what the provider gives us */
92 /* Provider side functions */
93 OSSL_FUNC_provider_teardown_fn
*teardown
;
94 OSSL_FUNC_provider_gettable_params_fn
*gettable_params
;
95 OSSL_FUNC_provider_get_params_fn
*get_params
;
96 OSSL_FUNC_provider_get_capabilities_fn
*get_capabilities
;
97 OSSL_FUNC_provider_self_test_fn
*self_test
;
98 OSSL_FUNC_provider_query_operation_fn
*query_operation
;
99 OSSL_FUNC_provider_unquery_operation_fn
*unquery_operation
;
102 * Cache of bit to indicate of query_operation() has been called on
103 * a specific operation or not.
105 unsigned char *operation_bits
;
106 size_t operation_bits_sz
;
107 CRYPTO_RWLOCK
*opbits_lock
;
110 /* Whether this provider is the child of some other provider */
111 const OSSL_CORE_HANDLE
*handle
;
112 unsigned int ischild
:1;
115 /* Provider side data */
117 const OSSL_DISPATCH
*dispatch
;
119 DEFINE_STACK_OF(OSSL_PROVIDER
)
121 static int ossl_provider_cmp(const OSSL_PROVIDER
* const *a
,
122 const OSSL_PROVIDER
* const *b
)
124 return strcmp((*a
)->name
, (*b
)->name
);
128 * Provider Object store
129 * =====================
131 * The Provider Object store is a library context object, and therefore needs
135 struct provider_store_st
{
136 OSSL_LIB_CTX
*libctx
;
137 STACK_OF(OSSL_PROVIDER
) *providers
;
138 STACK_OF(OSSL_PROVIDER_CHILD_CB
) *child_cbs
;
139 CRYPTO_RWLOCK
*default_path_lock
;
142 unsigned int use_fallbacks
:1;
143 unsigned int freeing
:1;
147 * provider_deactivate_free() is a wrapper around ossl_provider_deactivate()
148 * and ossl_provider_free(), called as needed.
149 * Since this is only called when the provider store is being emptied, we
150 * don't need to care about any lock.
152 static void provider_deactivate_free(OSSL_PROVIDER
*prov
)
154 if (prov
->flag_activated
)
155 ossl_provider_deactivate(prov
);
156 ossl_provider_free(prov
);
160 static void ossl_provider_child_cb_free(OSSL_PROVIDER_CHILD_CB
*cb
)
166 static void provider_store_free(void *vstore
)
168 struct provider_store_st
*store
= vstore
;
173 OPENSSL_free(store
->default_path
);
174 sk_OSSL_PROVIDER_pop_free(store
->providers
, provider_deactivate_free
);
176 sk_OSSL_PROVIDER_CHILD_CB_pop_free(store
->child_cbs
,
177 ossl_provider_child_cb_free
);
179 CRYPTO_THREAD_lock_free(store
->default_path_lock
);
180 CRYPTO_THREAD_lock_free(store
->lock
);
184 static void *provider_store_new(OSSL_LIB_CTX
*ctx
)
186 struct provider_store_st
*store
= OPENSSL_zalloc(sizeof(*store
));
187 const struct predefined_providers_st
*p
= NULL
;
190 || (store
->providers
= sk_OSSL_PROVIDER_new(ossl_provider_cmp
)) == NULL
191 || (store
->default_path_lock
= CRYPTO_THREAD_lock_new()) == NULL
193 || (store
->child_cbs
= sk_OSSL_PROVIDER_CHILD_CB_new_null()) == NULL
195 || (store
->lock
= CRYPTO_THREAD_lock_new()) == NULL
) {
196 provider_store_free(store
);
200 store
->use_fallbacks
= 1;
202 for (p
= ossl_predefined_providers
; p
->name
!= NULL
; p
++) {
203 OSSL_PROVIDER
*prov
= NULL
;
206 * We use the internal constructor directly here,
207 * otherwise we get a call loop
209 prov
= provider_new(p
->name
, p
->init
);
212 || sk_OSSL_PROVIDER_push(store
->providers
, prov
) == 0) {
213 ossl_provider_free(prov
);
214 provider_store_free(store
);
215 ERR_raise(ERR_LIB_CRYPTO
, ERR_R_INTERNAL_ERROR
);
221 prov
->error_lib
= ERR_get_next_error_library();
224 ossl_provider_set_fallback(prov
);
230 static const OSSL_LIB_CTX_METHOD provider_store_method
= {
231 /* Needs to be freed before the child provider data is freed */
232 OSSL_LIB_CTX_METHOD_PRIORITY_1
,
237 static struct provider_store_st
*get_provider_store(OSSL_LIB_CTX
*libctx
)
239 struct provider_store_st
*store
= NULL
;
241 store
= ossl_lib_ctx_get_data(libctx
, OSSL_LIB_CTX_PROVIDER_STORE_INDEX
,
242 &provider_store_method
);
244 ERR_raise(ERR_LIB_CRYPTO
, ERR_R_INTERNAL_ERROR
);
248 int ossl_provider_disable_fallback_loading(OSSL_LIB_CTX
*libctx
)
250 struct provider_store_st
*store
;
252 if ((store
= get_provider_store(libctx
)) != NULL
) {
253 if (!CRYPTO_THREAD_write_lock(store
->lock
))
255 store
->use_fallbacks
= 0;
256 CRYPTO_THREAD_unlock(store
->lock
);
262 OSSL_PROVIDER
*ossl_provider_find(OSSL_LIB_CTX
*libctx
, const char *name
,
265 struct provider_store_st
*store
= NULL
;
266 OSSL_PROVIDER
*prov
= NULL
;
268 if ((store
= get_provider_store(libctx
)) != NULL
) {
269 OSSL_PROVIDER tmpl
= { 0, };
274 * Make sure any providers are loaded from config before we try to find
278 if (ossl_lib_ctx_is_default(libctx
))
279 OPENSSL_init_crypto(OPENSSL_INIT_LOAD_CONFIG
, NULL
);
283 tmpl
.name
= (char *)name
;
284 if (!CRYPTO_THREAD_read_lock(store
->lock
))
286 if ((i
= sk_OSSL_PROVIDER_find(store
->providers
, &tmpl
)) == -1
287 || (prov
= sk_OSSL_PROVIDER_value(store
->providers
, i
)) == NULL
288 || !ossl_provider_up_ref(prov
))
290 CRYPTO_THREAD_unlock(store
->lock
);
297 * Provider Object methods
298 * =======================
301 static OSSL_PROVIDER
*provider_new(const char *name
,
302 OSSL_provider_init_fn
*init_function
)
304 OSSL_PROVIDER
*prov
= NULL
;
306 if ((prov
= OPENSSL_zalloc(sizeof(*prov
))) == NULL
308 || (prov
->refcnt_lock
= CRYPTO_THREAD_lock_new()) == NULL
310 || (prov
->opbits_lock
= CRYPTO_THREAD_lock_new()) == NULL
311 || (prov
->flag_lock
= CRYPTO_THREAD_lock_new()) == NULL
312 || (prov
->name
= OPENSSL_strdup(name
)) == NULL
) {
313 ossl_provider_free(prov
);
314 ERR_raise(ERR_LIB_CRYPTO
, ERR_R_MALLOC_FAILURE
);
318 prov
->refcnt
= 1; /* 1 One reference to be returned */
319 prov
->init_function
= init_function
;
321 prov
->flag_couldbechild
= 1;
326 int ossl_provider_up_ref(OSSL_PROVIDER
*prov
)
330 if (CRYPTO_UP_REF(&prov
->refcnt
, &ref
, prov
->refcnt_lock
) <= 0)
335 if (!ossl_provider_up_ref_parent(prov
, 0)) {
336 ossl_provider_free(prov
);
346 static int provider_up_ref_intern(OSSL_PROVIDER
*prov
, int activate
)
349 return ossl_provider_activate(prov
, 0, 1);
351 return ossl_provider_up_ref(prov
);
354 static int provider_free_intern(OSSL_PROVIDER
*prov
, int deactivate
)
357 return ossl_provider_deactivate(prov
);
359 ossl_provider_free(prov
);
364 OSSL_PROVIDER
*ossl_provider_new(OSSL_LIB_CTX
*libctx
, const char *name
,
365 OSSL_provider_init_fn
*init_function
,
368 struct provider_store_st
*store
= NULL
;
369 OSSL_PROVIDER
*prov
= NULL
;
371 if ((store
= get_provider_store(libctx
)) == NULL
)
374 if ((prov
= ossl_provider_find(libctx
, name
,
375 noconfig
)) != NULL
) { /* refcount +1 */
376 ossl_provider_free(prov
); /* refcount -1 */
377 ERR_raise_data(ERR_LIB_CRYPTO
, CRYPTO_R_PROVIDER_ALREADY_EXISTS
,
382 /* provider_new() generates an error, so no need here */
383 if ((prov
= provider_new(name
, init_function
)) == NULL
)
386 if (!CRYPTO_THREAD_write_lock(store
->lock
))
388 if (!ossl_provider_up_ref(prov
)) { /* +1 One reference for the store */
389 ossl_provider_free(prov
); /* -1 Reference that was to be returned */
391 } else if (sk_OSSL_PROVIDER_push(store
->providers
, prov
) == 0) {
392 ossl_provider_free(prov
); /* -1 Store reference */
393 ossl_provider_free(prov
); /* -1 Reference that was to be returned */
396 prov
->libctx
= libctx
;
399 prov
->error_lib
= ERR_get_next_error_library();
402 CRYPTO_THREAD_unlock(store
->lock
);
405 ERR_raise(ERR_LIB_CRYPTO
, ERR_R_MALLOC_FAILURE
);
408 * At this point, the provider is only partially "loaded". To be
409 * fully "loaded", ossl_provider_activate() must also be called.
415 static void free_infopair(INFOPAIR
*pair
)
417 OPENSSL_free(pair
->name
);
418 OPENSSL_free(pair
->value
);
422 void ossl_provider_free(OSSL_PROVIDER
*prov
)
427 CRYPTO_DOWN_REF(&prov
->refcnt
, &ref
, prov
->refcnt_lock
);
430 * When the refcount drops to zero, we clean up the provider.
431 * Note that this also does teardown, which may seem late,
432 * considering that init happens on first activation. However,
433 * there may be other structures hanging on to the provider after
434 * the last deactivation and may therefore need full access to the
435 * provider's services. Therefore, we deinit late.
438 if (prov
->flag_initialized
) {
439 ossl_provider_teardown(prov
);
440 #ifndef OPENSSL_NO_ERR
442 if (prov
->error_strings
!= NULL
) {
443 ERR_unload_strings(prov
->error_lib
, prov
->error_strings
);
444 OPENSSL_free(prov
->error_strings
);
445 prov
->error_strings
= NULL
;
449 OPENSSL_free(prov
->operation_bits
);
450 prov
->operation_bits
= NULL
;
451 prov
->operation_bits_sz
= 0;
452 prov
->flag_initialized
= 0;
457 * We deregister thread handling whether or not the provider was
458 * initialized. If init was attempted but was not successful then
459 * the provider may still have registered a thread handler.
461 ossl_init_thread_deregister(prov
);
462 DSO_free(prov
->module
);
464 OPENSSL_free(prov
->name
);
465 OPENSSL_free(prov
->path
);
466 sk_INFOPAIR_pop_free(prov
->parameters
, free_infopair
);
467 CRYPTO_THREAD_lock_free(prov
->opbits_lock
);
468 CRYPTO_THREAD_lock_free(prov
->flag_lock
);
470 CRYPTO_THREAD_lock_free(prov
->refcnt_lock
);
475 else if (prov
->ischild
) {
476 ossl_provider_free_parent(prov
, 0);
483 int ossl_provider_set_module_path(OSSL_PROVIDER
*prov
, const char *module_path
)
485 OPENSSL_free(prov
->path
);
486 if (module_path
== NULL
)
488 if ((prov
->path
= OPENSSL_strdup(module_path
)) != NULL
)
490 ERR_raise(ERR_LIB_CRYPTO
, ERR_R_MALLOC_FAILURE
);
494 int ossl_provider_add_parameter(OSSL_PROVIDER
*prov
,
495 const char *name
, const char *value
)
497 INFOPAIR
*pair
= NULL
;
499 if ((pair
= OPENSSL_zalloc(sizeof(*pair
))) != NULL
500 && (prov
->parameters
!= NULL
501 || (prov
->parameters
= sk_INFOPAIR_new_null()) != NULL
)
502 && (pair
->name
= OPENSSL_strdup(name
)) != NULL
503 && (pair
->value
= OPENSSL_strdup(value
)) != NULL
504 && sk_INFOPAIR_push(prov
->parameters
, pair
) > 0)
508 OPENSSL_free(pair
->name
);
509 OPENSSL_free(pair
->value
);
512 ERR_raise(ERR_LIB_CRYPTO
, ERR_R_MALLOC_FAILURE
);
517 * Provider activation.
519 * What "activation" means depends on the provider form; for built in
520 * providers (in the library or the application alike), the provider
521 * can already be considered to be loaded, all that's needed is to
522 * initialize it. However, for dynamically loadable provider modules,
523 * we must first load that module.
525 * Built in modules are distinguished from dynamically loaded modules
526 * with an already assigned init function.
528 static const OSSL_DISPATCH
*core_dispatch
; /* Define further down */
530 int OSSL_PROVIDER_set_default_search_path(OSSL_LIB_CTX
*libctx
,
533 struct provider_store_st
*store
;
537 p
= OPENSSL_strdup(path
);
539 ERR_raise(ERR_LIB_CRYPTO
, ERR_R_MALLOC_FAILURE
);
543 if ((store
= get_provider_store(libctx
)) != NULL
544 && CRYPTO_THREAD_write_lock(store
->default_path_lock
)) {
545 OPENSSL_free(store
->default_path
);
546 store
->default_path
= p
;
547 CRYPTO_THREAD_unlock(store
->default_path_lock
);
555 * Internal version that doesn't affect the store flags, and thereby avoid
556 * locking. Direct callers must remember to set the store flags when
559 static int provider_init(OSSL_PROVIDER
*prov
, int flag_lock
)
561 const OSSL_DISPATCH
*provider_dispatch
= NULL
;
562 void *tmp_provctx
= NULL
; /* safety measure */
563 #ifndef OPENSSL_NO_ERR
565 OSSL_FUNC_provider_get_reason_strings_fn
*p_get_reason_strings
= NULL
;
571 * The flag lock is used to lock init, not only because the flag is
572 * checked here and set at the end, but also because this function
573 * modifies a number of things in the provider structure that this
574 * function needs to perform under lock anyway.
576 if (flag_lock
&& !CRYPTO_THREAD_write_lock(prov
->flag_lock
))
578 if (prov
->flag_initialized
) {
584 * If the init function isn't set, it indicates that this provider is
587 if (prov
->init_function
== NULL
) {
591 if (prov
->module
== NULL
) {
592 char *allocated_path
= NULL
;
593 const char *module_path
= NULL
;
594 char *merged_path
= NULL
;
595 const char *load_dir
= NULL
;
596 char *allocated_load_dir
= NULL
;
597 struct provider_store_st
*store
;
599 if ((prov
->module
= DSO_new()) == NULL
) {
600 /* DSO_new() generates an error already */
604 if ((store
= get_provider_store(prov
->libctx
)) == NULL
605 || !CRYPTO_THREAD_read_lock(store
->default_path_lock
))
608 if (store
->default_path
!= NULL
) {
609 allocated_load_dir
= OPENSSL_strdup(store
->default_path
);
610 CRYPTO_THREAD_unlock(store
->default_path_lock
);
611 if (allocated_load_dir
== NULL
) {
612 ERR_raise(ERR_LIB_CRYPTO
, ERR_R_MALLOC_FAILURE
);
615 load_dir
= allocated_load_dir
;
617 CRYPTO_THREAD_unlock(store
->default_path_lock
);
620 if (load_dir
== NULL
) {
621 load_dir
= ossl_safe_getenv("OPENSSL_MODULES");
622 if (load_dir
== NULL
)
623 load_dir
= MODULESDIR
;
626 DSO_ctrl(prov
->module
, DSO_CTRL_SET_FLAGS
,
627 DSO_FLAG_NAME_TRANSLATION_EXT_ONLY
, NULL
);
629 module_path
= prov
->path
;
630 if (module_path
== NULL
)
631 module_path
= allocated_path
=
632 DSO_convert_filename(prov
->module
, prov
->name
);
633 if (module_path
!= NULL
)
634 merged_path
= DSO_merge(prov
->module
, module_path
, load_dir
);
636 if (merged_path
== NULL
637 || (DSO_load(prov
->module
, merged_path
, NULL
, 0)) == NULL
) {
638 DSO_free(prov
->module
);
642 OPENSSL_free(merged_path
);
643 OPENSSL_free(allocated_path
);
644 OPENSSL_free(allocated_load_dir
);
647 if (prov
->module
!= NULL
)
648 prov
->init_function
= (OSSL_provider_init_fn
*)
649 DSO_bind_func(prov
->module
, "OSSL_provider_init");
653 /* Call the initialise function for the provider. */
654 if (prov
->init_function
== NULL
655 || !prov
->init_function((OSSL_CORE_HANDLE
*)prov
, core_dispatch
,
656 &provider_dispatch
, &tmp_provctx
)) {
657 ERR_raise_data(ERR_LIB_CRYPTO
, ERR_R_INIT_FAIL
,
658 "name=%s", prov
->name
);
661 prov
->provctx
= tmp_provctx
;
662 prov
->dispatch
= provider_dispatch
;
664 prov
->flag_couldbechild
= 0;
667 for (; provider_dispatch
->function_id
!= 0; provider_dispatch
++) {
668 switch (provider_dispatch
->function_id
) {
669 case OSSL_FUNC_PROVIDER_TEARDOWN
:
671 OSSL_FUNC_provider_teardown(provider_dispatch
);
673 case OSSL_FUNC_PROVIDER_GETTABLE_PARAMS
:
674 prov
->gettable_params
=
675 OSSL_FUNC_provider_gettable_params(provider_dispatch
);
677 case OSSL_FUNC_PROVIDER_GET_PARAMS
:
679 OSSL_FUNC_provider_get_params(provider_dispatch
);
681 case OSSL_FUNC_PROVIDER_SELF_TEST
:
683 OSSL_FUNC_provider_self_test(provider_dispatch
);
685 case OSSL_FUNC_PROVIDER_GET_CAPABILITIES
:
686 prov
->get_capabilities
=
687 OSSL_FUNC_provider_get_capabilities(provider_dispatch
);
689 case OSSL_FUNC_PROVIDER_QUERY_OPERATION
:
690 prov
->query_operation
=
691 OSSL_FUNC_provider_query_operation(provider_dispatch
);
693 case OSSL_FUNC_PROVIDER_UNQUERY_OPERATION
:
694 prov
->unquery_operation
=
695 OSSL_FUNC_provider_unquery_operation(provider_dispatch
);
697 #ifndef OPENSSL_NO_ERR
699 case OSSL_FUNC_PROVIDER_GET_REASON_STRINGS
:
700 p_get_reason_strings
=
701 OSSL_FUNC_provider_get_reason_strings(provider_dispatch
);
708 #ifndef OPENSSL_NO_ERR
710 if (p_get_reason_strings
!= NULL
) {
711 const OSSL_ITEM
*reasonstrings
= p_get_reason_strings(prov
->provctx
);
715 * ERR_load_strings() handles ERR_STRING_DATA rather than OSSL_ITEM,
716 * although they are essentially the same type.
717 * Furthermore, ERR_load_strings() patches the array's error number
718 * with the error library number, so we need to make a copy of that
722 while (reasonstrings
[cnt
].id
!= 0) {
723 if (ERR_GET_LIB(reasonstrings
[cnt
].id
) != 0)
727 cnt
++; /* One for the terminating item */
729 /* Allocate one extra item for the "library" name */
730 prov
->error_strings
=
731 OPENSSL_zalloc(sizeof(ERR_STRING_DATA
) * (cnt
+ 1));
732 if (prov
->error_strings
== NULL
)
736 * Set the "library" name.
738 prov
->error_strings
[0].error
= ERR_PACK(prov
->error_lib
, 0, 0);
739 prov
->error_strings
[0].string
= prov
->name
;
741 * Copy reasonstrings item 0..cnt-1 to prov->error_trings positions
744 for (cnt2
= 1; cnt2
<= cnt
; cnt2
++) {
745 prov
->error_strings
[cnt2
].error
= (int)reasonstrings
[cnt2
-1].id
;
746 prov
->error_strings
[cnt2
].string
= reasonstrings
[cnt2
-1].ptr
;
749 ERR_load_strings(prov
->error_lib
, prov
->error_strings
);
754 /* With this flag set, this provider has become fully "loaded". */
755 prov
->flag_initialized
= 1;
760 CRYPTO_THREAD_unlock(prov
->flag_lock
);
765 * Deactivate a provider.
766 * Return -1 on failure and the activation count on success
768 static int provider_deactivate(OSSL_PROVIDER
*prov
)
771 struct provider_store_st
*store
;
773 if (!ossl_assert(prov
!= NULL
))
776 store
= get_provider_store(prov
->libctx
);
780 if (!CRYPTO_THREAD_read_lock(store
->lock
))
782 if (!CRYPTO_THREAD_write_lock(prov
->flag_lock
)) {
783 CRYPTO_THREAD_unlock(store
->lock
);
788 if (prov
->activatecnt
== 2 && prov
->ischild
) {
790 * We have had a direct activation in this child libctx so we need to
791 * now down the ref count in the parent provider.
793 ossl_provider_free_parent(prov
, 1);
797 if ((count
= --prov
->activatecnt
) < 1) {
798 prov
->flag_activated
= 0;
801 int i
, max
= sk_OSSL_PROVIDER_CHILD_CB_num(store
->child_cbs
);
802 OSSL_PROVIDER_CHILD_CB
*child_cb
;
804 for (i
= 0; i
< max
; i
++) {
805 child_cb
= sk_OSSL_PROVIDER_CHILD_CB_value(store
->child_cbs
, i
);
806 child_cb
->remove_cb((OSSL_CORE_HANDLE
*)prov
, child_cb
->cbdata
);
812 CRYPTO_THREAD_unlock(prov
->flag_lock
);
813 CRYPTO_THREAD_unlock(store
->lock
);
815 /* We don't deinit here, that's done in ossl_provider_free() */
820 * Activate a provider.
821 * Return -1 on failure and the activation count on success
823 static int provider_activate(OSSL_PROVIDER
*prov
, int lock
, int upcalls
)
827 if (provider_init(prov
, lock
)) {
829 struct provider_store_st
*store
;
831 store
= get_provider_store(prov
->libctx
);
835 if (lock
&& !CRYPTO_THREAD_read_lock(store
->lock
))
838 if (lock
&& !CRYPTO_THREAD_write_lock(prov
->flag_lock
)) {
839 CRYPTO_THREAD_unlock(store
->lock
);
844 if (prov
->ischild
&& upcalls
)
845 ret
= ossl_provider_up_ref_parent(prov
, 1);
849 count
= ++prov
->activatecnt
;
850 prov
->flag_activated
= 1;
853 if (prov
->activatecnt
== 1) {
854 OSSL_PROVIDER_CHILD_CB
*child_cb
;
857 max
= sk_OSSL_PROVIDER_CHILD_CB_num(store
->child_cbs
);
858 for (i
= 0; i
< max
; i
++) {
860 * This is newly activated (activatecnt == 1), so we need to
861 * create child providers as necessary.
863 child_cb
= sk_OSSL_PROVIDER_CHILD_CB_value(store
->child_cbs
,
865 ret
&= child_cb
->create_cb((OSSL_CORE_HANDLE
*)prov
,
873 CRYPTO_THREAD_unlock(prov
->flag_lock
);
874 CRYPTO_THREAD_unlock(store
->lock
);
883 static int provider_flush_store_cache(const OSSL_PROVIDER
*prov
)
885 struct provider_store_st
*store
;
888 if ((store
= get_provider_store(prov
->libctx
)) == NULL
)
891 if (!CRYPTO_THREAD_read_lock(store
->lock
))
893 freeing
= store
->freeing
;
894 CRYPTO_THREAD_unlock(store
->lock
);
897 return evp_method_store_flush(prov
->libctx
);
901 int ossl_provider_activate(OSSL_PROVIDER
*prov
, int retain_fallbacks
,
908 if ((count
= provider_activate(prov
, 1, upcalls
)) > 0) {
909 if (!retain_fallbacks
) {
910 if (!CRYPTO_THREAD_write_lock(prov
->store
->lock
)) {
911 provider_deactivate(prov
);
914 prov
->store
->use_fallbacks
= 0;
915 CRYPTO_THREAD_unlock(prov
->store
->lock
);
917 return count
== 1 ? provider_flush_store_cache(prov
) : 1;
922 int ossl_provider_deactivate(OSSL_PROVIDER
*prov
)
926 if (prov
== NULL
|| (count
= provider_deactivate(prov
)) < 0)
928 return count
== 0 ? provider_flush_store_cache(prov
) : 1;
931 void *ossl_provider_ctx(const OSSL_PROVIDER
*prov
)
933 return prov
->provctx
;
937 * This function only does something once when store->use_fallbacks == 1,
938 * and then sets store->use_fallbacks = 0, so the second call and so on is
939 * effectively a no-op.
941 static void provider_activate_fallbacks(struct provider_store_st
*store
)
945 int activated_fallback_count
= 0;
948 if (!CRYPTO_THREAD_read_lock(store
->lock
))
950 use_fallbacks
= store
->use_fallbacks
;
951 CRYPTO_THREAD_unlock(store
->lock
);
955 if (!CRYPTO_THREAD_write_lock(store
->lock
))
957 /* Check again, just in case another thread changed it */
958 use_fallbacks
= store
->use_fallbacks
;
959 if (!use_fallbacks
) {
960 CRYPTO_THREAD_unlock(store
->lock
);
964 num_provs
= sk_OSSL_PROVIDER_num(store
->providers
);
965 for (i
= 0; i
< num_provs
; i
++) {
966 OSSL_PROVIDER
*prov
= sk_OSSL_PROVIDER_value(store
->providers
, i
);
968 if (ossl_provider_up_ref(prov
)) {
969 if (CRYPTO_THREAD_write_lock(prov
->flag_lock
)) {
970 if (prov
->flag_fallback
) {
971 if (provider_activate(prov
, 0, 0) > 0)
972 activated_fallback_count
++;
974 CRYPTO_THREAD_unlock(prov
->flag_lock
);
976 ossl_provider_free(prov
);
981 * We assume that all fallbacks have been added to the store before
982 * any fallback is activated.
983 * TODO: We may have to reconsider this, IF we find ourselves adding
984 * fallbacks after any previous fallback has been activated.
986 if (activated_fallback_count
> 0)
987 store
->use_fallbacks
= 0;
989 CRYPTO_THREAD_unlock(store
->lock
);
992 int ossl_provider_doall_activated(OSSL_LIB_CTX
*ctx
,
993 int (*cb
)(OSSL_PROVIDER
*provider
,
997 int ret
= 0, curr
, max
;
998 struct provider_store_st
*store
= get_provider_store(ctx
);
999 STACK_OF(OSSL_PROVIDER
) *provs
= NULL
;
1003 * Make sure any providers are loaded from config before we try to use
1006 if (ossl_lib_ctx_is_default(ctx
))
1007 OPENSSL_init_crypto(OPENSSL_INIT_LOAD_CONFIG
, NULL
);
1012 provider_activate_fallbacks(store
);
1015 * Under lock, grab a copy of the provider list and up_ref each
1016 * provider so that they don't disappear underneath us.
1018 if (!CRYPTO_THREAD_read_lock(store
->lock
))
1020 provs
= sk_OSSL_PROVIDER_dup(store
->providers
);
1021 if (provs
== NULL
) {
1022 CRYPTO_THREAD_unlock(store
->lock
);
1025 max
= sk_OSSL_PROVIDER_num(provs
);
1027 * We work backwards through the stack so that we can safely delete items
1030 for (curr
= max
- 1; curr
>= 0; curr
--) {
1031 OSSL_PROVIDER
*prov
= sk_OSSL_PROVIDER_value(provs
, curr
);
1033 if (!CRYPTO_THREAD_write_lock(prov
->flag_lock
))
1035 if (prov
->flag_activated
) {
1036 if (!ossl_provider_up_ref(prov
)){
1037 CRYPTO_THREAD_unlock(prov
->flag_lock
);
1041 * It's already activated, but we up the activated count to ensure
1042 * it remains activated until after we've called the user callback.
1044 if (provider_activate(prov
, 0, 1) < 0) {
1045 ossl_provider_free(prov
);
1046 CRYPTO_THREAD_unlock(prov
->flag_lock
);
1050 sk_OSSL_PROVIDER_delete(provs
, curr
);
1053 CRYPTO_THREAD_unlock(prov
->flag_lock
);
1055 CRYPTO_THREAD_unlock(store
->lock
);
1058 * Now, we sweep through all providers not under lock
1060 for (curr
= 0; curr
< max
; curr
++) {
1061 OSSL_PROVIDER
*prov
= sk_OSSL_PROVIDER_value(provs
, curr
);
1063 if (!cb(prov
, cbdata
))
1072 CRYPTO_THREAD_unlock(store
->lock
);
1075 * The pop_free call doesn't do what we want on an error condition. We
1076 * either start from the first item in the stack, or part way through if
1077 * we only processed some of the items.
1079 for (curr
++; curr
< max
; curr
++) {
1080 OSSL_PROVIDER
*prov
= sk_OSSL_PROVIDER_value(provs
, curr
);
1082 provider_deactivate(prov
);
1083 ossl_provider_free(prov
);
1085 sk_OSSL_PROVIDER_free(provs
);
1089 int ossl_provider_available(OSSL_PROVIDER
*prov
)
1094 provider_activate_fallbacks(prov
->store
);
1096 if (!CRYPTO_THREAD_read_lock(prov
->flag_lock
))
1098 ret
= prov
->flag_activated
;
1099 CRYPTO_THREAD_unlock(prov
->flag_lock
);
1105 /* Setters of Provider Object data */
1106 int ossl_provider_set_fallback(OSSL_PROVIDER
*prov
)
1111 prov
->flag_fallback
= 1;
1115 /* Getters of Provider Object data */
1116 const char *ossl_provider_name(const OSSL_PROVIDER
*prov
)
1121 const DSO
*ossl_provider_dso(const OSSL_PROVIDER
*prov
)
1123 return prov
->module
;
1126 const char *ossl_provider_module_name(const OSSL_PROVIDER
*prov
)
1131 return DSO_get_filename(prov
->module
);
1135 const char *ossl_provider_module_path(const OSSL_PROVIDER
*prov
)
1140 /* FIXME: Ensure it's a full path */
1141 return DSO_get_filename(prov
->module
);
1145 void *ossl_provider_prov_ctx(const OSSL_PROVIDER
*prov
)
1148 return prov
->provctx
;
1153 const OSSL_DISPATCH
*ossl_provider_get0_dispatch(const OSSL_PROVIDER
*prov
)
1156 return prov
->dispatch
;
1161 OSSL_LIB_CTX
*ossl_provider_libctx(const OSSL_PROVIDER
*prov
)
1163 return prov
!= NULL
? prov
->libctx
: NULL
;
1166 /* Wrappers around calls to the provider */
1167 void ossl_provider_teardown(const OSSL_PROVIDER
*prov
)
1169 if (prov
->teardown
!= NULL
1174 prov
->teardown(prov
->provctx
);
1177 const OSSL_PARAM
*ossl_provider_gettable_params(const OSSL_PROVIDER
*prov
)
1179 return prov
->gettable_params
== NULL
1180 ? NULL
: prov
->gettable_params(prov
->provctx
);
1183 int ossl_provider_get_params(const OSSL_PROVIDER
*prov
, OSSL_PARAM params
[])
1185 return prov
->get_params
== NULL
1186 ? 0 : prov
->get_params(prov
->provctx
, params
);
1189 int ossl_provider_self_test(const OSSL_PROVIDER
*prov
)
1193 if (prov
->self_test
== NULL
)
1195 ret
= prov
->self_test(prov
->provctx
);
1197 (void)provider_flush_store_cache(prov
);
1201 int ossl_provider_get_capabilities(const OSSL_PROVIDER
*prov
,
1202 const char *capability
,
1206 return prov
->get_capabilities
== NULL
1207 ? 1 : prov
->get_capabilities(prov
->provctx
, capability
, cb
, arg
);
1210 const OSSL_ALGORITHM
*ossl_provider_query_operation(const OSSL_PROVIDER
*prov
,
1214 const OSSL_ALGORITHM
*res
;
1216 if (prov
->query_operation
== NULL
)
1218 res
= prov
->query_operation(prov
->provctx
, operation_id
, no_cache
);
1219 #if defined(OPENSSL_NO_CACHED_FETCH)
1220 /* Forcing the non-caching of queries */
1221 if (no_cache
!= NULL
)
1227 void ossl_provider_unquery_operation(const OSSL_PROVIDER
*prov
,
1229 const OSSL_ALGORITHM
*algs
)
1231 if (prov
->unquery_operation
!= NULL
)
1232 prov
->unquery_operation(prov
->provctx
, operation_id
, algs
);
1235 int ossl_provider_clear_all_operation_bits(OSSL_LIB_CTX
*libctx
)
1237 struct provider_store_st
*store
;
1238 OSSL_PROVIDER
*provider
;
1239 int i
, num
, res
= 1;
1241 if ((store
= get_provider_store(libctx
)) != NULL
) {
1242 if (!CRYPTO_THREAD_read_lock(store
->lock
))
1244 num
= sk_OSSL_PROVIDER_num(store
->providers
);
1245 for (i
= 0; i
< num
; i
++) {
1246 provider
= sk_OSSL_PROVIDER_value(store
->providers
, i
);
1247 if (!CRYPTO_THREAD_write_lock(provider
->opbits_lock
)) {
1251 if (provider
->operation_bits
!= NULL
)
1252 memset(provider
->operation_bits
, 0,
1253 provider
->operation_bits_sz
);
1254 CRYPTO_THREAD_unlock(provider
->opbits_lock
);
1256 CRYPTO_THREAD_unlock(store
->lock
);
1262 int ossl_provider_set_operation_bit(OSSL_PROVIDER
*provider
, size_t bitnum
)
1264 size_t byte
= bitnum
/ 8;
1265 unsigned char bit
= (1 << (bitnum
% 8)) & 0xFF;
1267 if (!CRYPTO_THREAD_write_lock(provider
->opbits_lock
))
1269 if (provider
->operation_bits_sz
<= byte
) {
1270 unsigned char *tmp
= OPENSSL_realloc(provider
->operation_bits
,
1274 CRYPTO_THREAD_unlock(provider
->opbits_lock
);
1275 ERR_raise(ERR_LIB_CRYPTO
, ERR_R_MALLOC_FAILURE
);
1278 provider
->operation_bits
= tmp
;
1279 memset(provider
->operation_bits
+ provider
->operation_bits_sz
,
1280 '\0', byte
+ 1 - provider
->operation_bits_sz
);
1281 provider
->operation_bits_sz
= byte
+ 1;
1283 provider
->operation_bits
[byte
] |= bit
;
1284 CRYPTO_THREAD_unlock(provider
->opbits_lock
);
1288 int ossl_provider_test_operation_bit(OSSL_PROVIDER
*provider
, size_t bitnum
,
1291 size_t byte
= bitnum
/ 8;
1292 unsigned char bit
= (1 << (bitnum
% 8)) & 0xFF;
1294 if (!ossl_assert(result
!= NULL
)) {
1295 ERR_raise(ERR_LIB_CRYPTO
, ERR_R_PASSED_NULL_PARAMETER
);
1300 if (!CRYPTO_THREAD_read_lock(provider
->opbits_lock
))
1302 if (provider
->operation_bits_sz
> byte
)
1303 *result
= ((provider
->operation_bits
[byte
] & bit
) != 0);
1304 CRYPTO_THREAD_unlock(provider
->opbits_lock
);
1309 const OSSL_CORE_HANDLE
*ossl_provider_get_parent(OSSL_PROVIDER
*prov
)
1311 return prov
->handle
;
1314 int ossl_provider_is_child(const OSSL_PROVIDER
*prov
)
1316 return prov
->ischild
;
1319 int ossl_provider_set_child(OSSL_PROVIDER
*prov
, const OSSL_CORE_HANDLE
*handle
)
1321 prov
->handle
= handle
;
1327 int ossl_provider_convert_to_child(OSSL_PROVIDER
*prov
,
1328 const OSSL_CORE_HANDLE
*handle
,
1329 OSSL_provider_init_fn
*init_function
)
1333 if (!CRYPTO_THREAD_write_lock(prov
->store
->lock
))
1335 if (!CRYPTO_THREAD_write_lock(prov
->flag_lock
)) {
1336 CRYPTO_THREAD_unlock(prov
->store
->lock
);
1340 * The provider could be in one of three states: (1) Already a child,
1341 * (2) Not a child (but eligible to be one), or (3) Not a child (not
1342 * eligible to be one).
1344 if (prov
->flag_couldbechild
) {
1345 ossl_provider_set_child(prov
, handle
);
1346 prov
->init_function
= init_function
;
1348 if (prov
->ischild
&& provider_activate(prov
, 0, 0)) {
1350 prov
->store
->use_fallbacks
= 0;
1353 CRYPTO_THREAD_unlock(prov
->flag_lock
);
1354 CRYPTO_THREAD_unlock(prov
->store
->lock
);
1357 provider_flush_store_cache(prov
);
1360 * We report success whether or not the provider was eligible for conversion
1361 * to a child. If its not elgibile then it has already been loaded as a non
1362 * child provider and we should keep it like that.
1367 int ossl_provider_default_props_update(OSSL_LIB_CTX
*libctx
, const char *props
)
1370 struct provider_store_st
*store
= NULL
;
1372 OSSL_PROVIDER_CHILD_CB
*child_cb
;
1374 if ((store
= get_provider_store(libctx
)) == NULL
)
1377 if (!CRYPTO_THREAD_read_lock(store
->lock
))
1380 max
= sk_OSSL_PROVIDER_CHILD_CB_num(store
->child_cbs
);
1381 for (i
= 0; i
< max
; i
++) {
1382 child_cb
= sk_OSSL_PROVIDER_CHILD_CB_value(store
->child_cbs
, i
);
1383 child_cb
->global_props_cb(props
, child_cb
->cbdata
);
1386 CRYPTO_THREAD_unlock(store
->lock
);
1391 static int ossl_provider_register_child_cb(const OSSL_CORE_HANDLE
*handle
,
1393 const OSSL_CORE_HANDLE
*provider
,
1396 const OSSL_CORE_HANDLE
*provider
,
1398 int (*global_props_cb
)(
1404 * This is really an OSSL_PROVIDER that we created and cast to
1405 * OSSL_CORE_HANDLE originally. Therefore it is safe to cast it back.
1407 OSSL_PROVIDER
*thisprov
= (OSSL_PROVIDER
*)handle
;
1408 OSSL_PROVIDER
*prov
;
1409 OSSL_LIB_CTX
*libctx
= thisprov
->libctx
;
1410 struct provider_store_st
*store
= NULL
;
1411 int ret
= 0, i
, max
;
1412 OSSL_PROVIDER_CHILD_CB
*child_cb
;
1413 char *propsstr
= NULL
;
1415 if ((store
= get_provider_store(libctx
)) == NULL
)
1418 child_cb
= OPENSSL_malloc(sizeof(*child_cb
));
1419 if (child_cb
== NULL
)
1421 child_cb
->prov
= thisprov
;
1422 child_cb
->create_cb
= create_cb
;
1423 child_cb
->remove_cb
= remove_cb
;
1424 child_cb
->global_props_cb
= global_props_cb
;
1425 child_cb
->cbdata
= cbdata
;
1427 if (!CRYPTO_THREAD_write_lock(store
->lock
)) {
1428 OPENSSL_free(child_cb
);
1431 propsstr
= evp_get_global_properties_str(libctx
, 0);
1433 if (propsstr
!= NULL
) {
1434 global_props_cb(propsstr
, cbdata
);
1435 OPENSSL_free(propsstr
);
1437 max
= sk_OSSL_PROVIDER_num(store
->providers
);
1438 for (i
= 0; i
< max
; i
++) {
1439 prov
= sk_OSSL_PROVIDER_value(store
->providers
, i
);
1441 * We require register_child_cb to be called during a provider init
1442 * function. The currently initing provider will never be activated yet
1443 * and we we should not attempt to aquire the flag_lock for it.
1445 if (prov
== thisprov
)
1447 if (!CRYPTO_THREAD_read_lock(prov
->flag_lock
))
1450 * We hold the lock while calling the user callback. This means that the
1451 * user callback must be short and simple and not do anything likely to
1454 if (prov
->flag_activated
1455 && !create_cb((OSSL_CORE_HANDLE
*)prov
, cbdata
))
1457 CRYPTO_THREAD_unlock(prov
->flag_lock
);
1461 ret
= sk_OSSL_PROVIDER_CHILD_CB_push(store
->child_cbs
, child_cb
);
1463 if (i
!= max
|| ret
<= 0) {
1464 /* Failed during creation. Remove everything we just added */
1465 for (; i
>= 0; i
--) {
1466 prov
= sk_OSSL_PROVIDER_value(store
->providers
, i
);
1467 remove_cb((OSSL_CORE_HANDLE
*)prov
, cbdata
);
1469 OPENSSL_free(child_cb
);
1472 CRYPTO_THREAD_unlock(store
->lock
);
1477 static void ossl_provider_deregister_child_cb(const OSSL_CORE_HANDLE
*handle
)
1480 * This is really an OSSL_PROVIDER that we created and cast to
1481 * OSSL_CORE_HANDLE originally. Therefore it is safe to cast it back.
1483 OSSL_PROVIDER
*thisprov
= (OSSL_PROVIDER
*)handle
;
1484 OSSL_LIB_CTX
*libctx
= thisprov
->libctx
;
1485 struct provider_store_st
*store
= NULL
;
1487 OSSL_PROVIDER_CHILD_CB
*child_cb
;
1489 if ((store
= get_provider_store(libctx
)) == NULL
)
1492 if (!CRYPTO_THREAD_write_lock(store
->lock
))
1494 max
= sk_OSSL_PROVIDER_CHILD_CB_num(store
->child_cbs
);
1495 for (i
= 0; i
< max
; i
++) {
1496 child_cb
= sk_OSSL_PROVIDER_CHILD_CB_value(store
->child_cbs
, i
);
1497 if (child_cb
->prov
== thisprov
) {
1498 /* Found an entry */
1499 sk_OSSL_PROVIDER_CHILD_CB_delete(store
->child_cbs
, i
);
1500 OPENSSL_free(child_cb
);
1504 CRYPTO_THREAD_unlock(store
->lock
);
1509 * Core functions for the provider
1510 * ===============================
1512 * This is the set of functions that the core makes available to the provider
1516 * This returns a list of Provider Object parameters with their types, for
1517 * discovery. We do not expect that many providers will use this, but one
1520 static const OSSL_PARAM param_types
[] = {
1521 OSSL_PARAM_DEFN(OSSL_PROV_PARAM_CORE_VERSION
, OSSL_PARAM_UTF8_PTR
, NULL
, 0),
1522 OSSL_PARAM_DEFN(OSSL_PROV_PARAM_CORE_PROV_NAME
, OSSL_PARAM_UTF8_PTR
,
1525 OSSL_PARAM_DEFN(OSSL_PROV_PARAM_CORE_MODULE_FILENAME
, OSSL_PARAM_UTF8_PTR
,
1532 * Forward declare all the functions that are provided aa dispatch.
1533 * This ensures that the compiler will complain if they aren't defined
1534 * with the correct signature.
1536 static OSSL_FUNC_core_gettable_params_fn core_gettable_params
;
1537 static OSSL_FUNC_core_get_params_fn core_get_params
;
1538 static OSSL_FUNC_core_thread_start_fn core_thread_start
;
1539 static OSSL_FUNC_core_get_libctx_fn core_get_libctx
;
1541 static OSSL_FUNC_core_new_error_fn core_new_error
;
1542 static OSSL_FUNC_core_set_error_debug_fn core_set_error_debug
;
1543 static OSSL_FUNC_core_vset_error_fn core_vset_error
;
1544 static OSSL_FUNC_core_set_error_mark_fn core_set_error_mark
;
1545 static OSSL_FUNC_core_clear_last_error_mark_fn core_clear_last_error_mark
;
1546 static OSSL_FUNC_core_pop_error_to_mark_fn core_pop_error_to_mark
;
1549 static const OSSL_PARAM
*core_gettable_params(const OSSL_CORE_HANDLE
*handle
)
1554 static int core_get_params(const OSSL_CORE_HANDLE
*handle
, OSSL_PARAM params
[])
1559 * We created this object originally and we know it is actually an
1560 * OSSL_PROVIDER *, so the cast is safe
1562 OSSL_PROVIDER
*prov
= (OSSL_PROVIDER
*)handle
;
1564 if ((p
= OSSL_PARAM_locate(params
, OSSL_PROV_PARAM_CORE_VERSION
)) != NULL
)
1565 OSSL_PARAM_set_utf8_ptr(p
, OPENSSL_VERSION_STR
);
1566 if ((p
= OSSL_PARAM_locate(params
, OSSL_PROV_PARAM_CORE_PROV_NAME
)) != NULL
)
1567 OSSL_PARAM_set_utf8_ptr(p
, prov
->name
);
1570 if ((p
= OSSL_PARAM_locate(params
,
1571 OSSL_PROV_PARAM_CORE_MODULE_FILENAME
)) != NULL
)
1572 OSSL_PARAM_set_utf8_ptr(p
, ossl_provider_module_path(prov
));
1575 if (prov
->parameters
== NULL
)
1578 for (i
= 0; i
< sk_INFOPAIR_num(prov
->parameters
); i
++) {
1579 INFOPAIR
*pair
= sk_INFOPAIR_value(prov
->parameters
, i
);
1581 if ((p
= OSSL_PARAM_locate(params
, pair
->name
)) != NULL
)
1582 OSSL_PARAM_set_utf8_ptr(p
, pair
->value
);
1587 static OPENSSL_CORE_CTX
*core_get_libctx(const OSSL_CORE_HANDLE
*handle
)
1590 * We created this object originally and we know it is actually an
1591 * OSSL_PROVIDER *, so the cast is safe
1593 OSSL_PROVIDER
*prov
= (OSSL_PROVIDER
*)handle
;
1596 * Using ossl_provider_libctx would be wrong as that returns
1597 * NULL for |prov| == NULL and NULL libctx has a special meaning
1598 * that does not apply here. Here |prov| == NULL can happen only in
1599 * case of a coding error.
1601 assert(prov
!= NULL
);
1602 return (OPENSSL_CORE_CTX
*)prov
->libctx
;
1605 static int core_thread_start(const OSSL_CORE_HANDLE
*handle
,
1606 OSSL_thread_stop_handler_fn handfn
,
1610 * We created this object originally and we know it is actually an
1611 * OSSL_PROVIDER *, so the cast is safe
1613 OSSL_PROVIDER
*prov
= (OSSL_PROVIDER
*)handle
;
1615 return ossl_init_thread_start(prov
, arg
, handfn
);
1619 * The FIPS module inner provider doesn't implement these. They aren't
1620 * needed there, since the FIPS module upcalls are always the outer provider
1625 * These error functions should use |handle| to select the proper
1626 * library context to report in the correct error stack if error
1627 * stacks become tied to the library context.
1628 * We cannot currently do that since there's no support for it in the
1631 static void core_new_error(const OSSL_CORE_HANDLE
*handle
)
1636 static void core_set_error_debug(const OSSL_CORE_HANDLE
*handle
,
1637 const char *file
, int line
, const char *func
)
1639 ERR_set_debug(file
, line
, func
);
1642 static void core_vset_error(const OSSL_CORE_HANDLE
*handle
,
1643 uint32_t reason
, const char *fmt
, va_list args
)
1646 * We created this object originally and we know it is actually an
1647 * OSSL_PROVIDER *, so the cast is safe
1649 OSSL_PROVIDER
*prov
= (OSSL_PROVIDER
*)handle
;
1652 * If the uppermost 8 bits are non-zero, it's an OpenSSL library
1653 * error and will be treated as such. Otherwise, it's a new style
1654 * provider error and will be treated as such.
1656 if (ERR_GET_LIB(reason
) != 0) {
1657 ERR_vset_error(ERR_GET_LIB(reason
), ERR_GET_REASON(reason
), fmt
, args
);
1659 ERR_vset_error(prov
->error_lib
, (int)reason
, fmt
, args
);
1663 static int core_set_error_mark(const OSSL_CORE_HANDLE
*handle
)
1665 return ERR_set_mark();
1668 static int core_clear_last_error_mark(const OSSL_CORE_HANDLE
*handle
)
1670 return ERR_clear_last_mark();
1673 static int core_pop_error_to_mark(const OSSL_CORE_HANDLE
*handle
)
1675 return ERR_pop_to_mark();
1677 #endif /* FIPS_MODULE */
1680 * Functions provided by the core.
1682 static const OSSL_DISPATCH core_dispatch_
[] = {
1683 { OSSL_FUNC_CORE_GETTABLE_PARAMS
, (void (*)(void))core_gettable_params
},
1684 { OSSL_FUNC_CORE_GET_PARAMS
, (void (*)(void))core_get_params
},
1685 { OSSL_FUNC_CORE_GET_LIBCTX
, (void (*)(void))core_get_libctx
},
1686 { OSSL_FUNC_CORE_THREAD_START
, (void (*)(void))core_thread_start
},
1688 { OSSL_FUNC_CORE_NEW_ERROR
, (void (*)(void))core_new_error
},
1689 { OSSL_FUNC_CORE_SET_ERROR_DEBUG
, (void (*)(void))core_set_error_debug
},
1690 { OSSL_FUNC_CORE_VSET_ERROR
, (void (*)(void))core_vset_error
},
1691 { OSSL_FUNC_CORE_SET_ERROR_MARK
, (void (*)(void))core_set_error_mark
},
1692 { OSSL_FUNC_CORE_CLEAR_LAST_ERROR_MARK
,
1693 (void (*)(void))core_clear_last_error_mark
},
1694 { OSSL_FUNC_CORE_POP_ERROR_TO_MARK
, (void (*)(void))core_pop_error_to_mark
},
1695 { OSSL_FUNC_BIO_NEW_FILE
, (void (*)(void))ossl_core_bio_new_file
},
1696 { OSSL_FUNC_BIO_NEW_MEMBUF
, (void (*)(void))ossl_core_bio_new_mem_buf
},
1697 { OSSL_FUNC_BIO_READ_EX
, (void (*)(void))ossl_core_bio_read_ex
},
1698 { OSSL_FUNC_BIO_WRITE_EX
, (void (*)(void))ossl_core_bio_write_ex
},
1699 { OSSL_FUNC_BIO_GETS
, (void (*)(void))ossl_core_bio_gets
},
1700 { OSSL_FUNC_BIO_PUTS
, (void (*)(void))ossl_core_bio_puts
},
1701 { OSSL_FUNC_BIO_CTRL
, (void (*)(void))ossl_core_bio_ctrl
},
1702 { OSSL_FUNC_BIO_UP_REF
, (void (*)(void))ossl_core_bio_up_ref
},
1703 { OSSL_FUNC_BIO_FREE
, (void (*)(void))ossl_core_bio_free
},
1704 { OSSL_FUNC_BIO_VPRINTF
, (void (*)(void))ossl_core_bio_vprintf
},
1705 { OSSL_FUNC_BIO_VSNPRINTF
, (void (*)(void))BIO_vsnprintf
},
1706 { OSSL_FUNC_SELF_TEST_CB
, (void (*)(void))OSSL_SELF_TEST_get_callback
},
1707 { OSSL_FUNC_GET_ENTROPY
, (void (*)(void))ossl_rand_get_entropy
},
1708 { OSSL_FUNC_CLEANUP_ENTROPY
, (void (*)(void))ossl_rand_cleanup_entropy
},
1709 { OSSL_FUNC_GET_NONCE
, (void (*)(void))ossl_rand_get_nonce
},
1710 { OSSL_FUNC_CLEANUP_NONCE
, (void (*)(void))ossl_rand_cleanup_nonce
},
1712 { OSSL_FUNC_CRYPTO_MALLOC
, (void (*)(void))CRYPTO_malloc
},
1713 { OSSL_FUNC_CRYPTO_ZALLOC
, (void (*)(void))CRYPTO_zalloc
},
1714 { OSSL_FUNC_CRYPTO_FREE
, (void (*)(void))CRYPTO_free
},
1715 { OSSL_FUNC_CRYPTO_CLEAR_FREE
, (void (*)(void))CRYPTO_clear_free
},
1716 { OSSL_FUNC_CRYPTO_REALLOC
, (void (*)(void))CRYPTO_realloc
},
1717 { OSSL_FUNC_CRYPTO_CLEAR_REALLOC
, (void (*)(void))CRYPTO_clear_realloc
},
1718 { OSSL_FUNC_CRYPTO_SECURE_MALLOC
, (void (*)(void))CRYPTO_secure_malloc
},
1719 { OSSL_FUNC_CRYPTO_SECURE_ZALLOC
, (void (*)(void))CRYPTO_secure_zalloc
},
1720 { OSSL_FUNC_CRYPTO_SECURE_FREE
, (void (*)(void))CRYPTO_secure_free
},
1721 { OSSL_FUNC_CRYPTO_SECURE_CLEAR_FREE
,
1722 (void (*)(void))CRYPTO_secure_clear_free
},
1723 { OSSL_FUNC_CRYPTO_SECURE_ALLOCATED
,
1724 (void (*)(void))CRYPTO_secure_allocated
},
1725 { OSSL_FUNC_OPENSSL_CLEANSE
, (void (*)(void))OPENSSL_cleanse
},
1727 { OSSL_FUNC_PROVIDER_REGISTER_CHILD_CB
,
1728 (void (*)(void))ossl_provider_register_child_cb
},
1729 { OSSL_FUNC_PROVIDER_DEREGISTER_CHILD_CB
,
1730 (void (*)(void))ossl_provider_deregister_child_cb
},
1731 { OSSL_FUNC_PROVIDER_NAME
,
1732 (void (*)(void))OSSL_PROVIDER_name
},
1733 { OSSL_FUNC_PROVIDER_GET0_PROVIDER_CTX
,
1734 (void (*)(void))OSSL_PROVIDER_get0_provider_ctx
},
1735 { OSSL_FUNC_PROVIDER_GET0_DISPATCH
,
1736 (void (*)(void))OSSL_PROVIDER_get0_dispatch
},
1737 { OSSL_FUNC_PROVIDER_UP_REF
,
1738 (void (*)(void))provider_up_ref_intern
},
1739 { OSSL_FUNC_PROVIDER_FREE
,
1740 (void (*)(void))provider_free_intern
},
1744 static const OSSL_DISPATCH
*core_dispatch
= core_dispatch_
;