]> git.ipfire.org Git - thirdparty/openssl.git/blame - crypto/provider_core.c
Raise an error on syscall failure in tls_retry_write_records
[thirdparty/openssl.git] / crypto / provider_core.c
CommitLineData
4c2883a9 1/*
2fd6c12e 2 * Copyright 2019-2024 The OpenSSL Project Authors. All Rights Reserved.
4c2883a9
RL
3 *
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
8 */
9
2217d4c9 10#include <assert.h>
4c2883a9 11#include <openssl/core.h>
23c48d94 12#include <openssl/core_dispatch.h>
25e60144 13#include <openssl/core_names.h>
6bd4e3f2 14#include <openssl/provider.h>
ac1055ef 15#include <openssl/params.h>
4c2883a9 16#include <openssl/opensslv.h>
25f2138b 17#include "crypto/cryptlib.h"
b8fd15a8 18#ifndef FIPS_MODULE
32e3c071
RL
19#include "crypto/decoder.h" /* ossl_decoder_store_cache_flush */
20#include "crypto/encoder.h" /* ossl_encoder_store_cache_flush */
32e3c071 21#include "crypto/store.h" /* ossl_store_loader_store_cache_flush */
b8fd15a8
TM
22#endif
23#include "crypto/evp.h" /* evp_method_store_cache_flush */
03bede0c 24#include "crypto/rand.h"
c41f3ae0 25#include "internal/nelem.h"
4c2883a9
RL
26#include "internal/thread_once.h"
27#include "internal/provider.h"
28#include "internal/refcount.h"
141cc94e 29#include "internal/bio.h"
f12a5690 30#include "internal/core.h"
c41f3ae0 31#include "provider_local.h"
927d0566 32#include "crypto/context.h"
f844f9eb 33#ifndef FIPS_MODULE
36fc5fc6
SL
34# include <openssl/self_test.h>
35#endif
c41f3ae0 36
03c137de
MC
37/*
38 * This file defines and uses a number of different structures:
39 *
40 * OSSL_PROVIDER (provider_st): Used to represent all information related to a
41 * single instance of a provider.
42 *
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
46 * future point.
47 *
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.
51 *
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.
57 *
58 * LOCKING
59 * =======
60 *
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.
63 *
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.
69 *
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.
72 *
73 * The locks available are:
74 *
75 * The provider flag_lock: Used to control updates to the various provider
fc570b26 76 * "flags" (flag_initialized and flag_activated).
03c137de 77 *
8752694b
P
78 * The provider activatecnt_lock: Used to control updates to the provider
79 * activatecnt value.
03c137de
MC
80 *
81 * The provider optbits_lock: Used to control access to the provider's
82 * operation_bits and operation_bits_sz fields.
83 *
84 * The store default_path_lock: Used to control access to the provider store's
85 * default search path value (default_path)
86 *
87 * The store lock: Used to control the stack of provider's held within the
88 * provider store, as well as the stack of registered child provider callbacks.
89 *
90 * As a general rule-of-thumb it is best to:
91 * - keep the scope of the code that is protected by a lock to the absolute
92 * minimum possible;
93 * - try to keep the scope of the lock to within a single function (i.e. avoid
94 * making calls to other functions while holding a lock);
95 * - try to only ever hold one lock at a time.
96 *
97 * Unfortunately, it is not always possible to stick to the above guidelines.
98 * Where they are not adhered to there is always a danger of inadvertently
99 * introducing the possibility of deadlock. The following rules MUST be adhered
100 * to in order to avoid that:
101 * - Holding multiple locks at the same time is only allowed for the
8752694b 102 * provider store lock, the provider activatecnt_lock and the provider flag_lock.
03c137de
MC
103 * - When holding multiple locks they must be acquired in the following order of
104 * precedence:
105 * 1) provider store lock
106 * 2) provider flag_lock
8752694b 107 * 3) provider activatecnt_lock
03c137de
MC
108 * - When releasing locks they must be released in the reverse order to which
109 * they were acquired
110 * - No locks may be held when making an upcall. NOTE: Some common functions
111 * can make upcalls as part of their normal operation. If you need to call
112 * some other function while holding a lock make sure you know whether it
113 * will make any upcalls or not. For example ossl_provider_up_ref() can call
114 * ossl_provider_up_ref_parent() which can call the c_prov_up_ref() upcall.
addbd7c9
MC
115 * - It is permissible to hold the store and flag locks when calling child
116 * provider callbacks. No other locks may be held during such callbacks.
03c137de
MC
117 */
118
c41f3ae0 119static OSSL_PROVIDER *provider_new(const char *name,
352d482a
MC
120 OSSL_provider_init_fn *init_function,
121 STACK_OF(INFOPAIR) *parameters);
4c2883a9
RL
122
123/*-
124 * Provider Object structure
125 * =========================
126 */
127
c1fb5e07 128#ifndef FIPS_MODULE
7b88c184
MC
129typedef struct {
130 OSSL_PROVIDER *prov;
131 int (*create_cb)(const OSSL_CORE_HANDLE *provider, void *cbdata);
b1956770
MC
132 int (*remove_cb)(const OSSL_CORE_HANDLE *provider, void *cbdata);
133 int (*global_props_cb)(const char *props, void *cbdata);
7b88c184
MC
134 void *cbdata;
135} OSSL_PROVIDER_CHILD_CB;
136DEFINE_STACK_OF(OSSL_PROVIDER_CHILD_CB)
c1fb5e07 137#endif
7b88c184 138
4c2883a9
RL
139struct provider_store_st; /* Forward declaration */
140
141struct ossl_provider_st {
142 /* Flag bits */
143 unsigned int flag_initialized:1;
390f9bad 144 unsigned int flag_activated:1;
4c2883a9 145
c2ec2bb7
RL
146 /* Getting and setting the flags require synchronization */
147 CRYPTO_RWLOCK *flag_lock;
148
4c2883a9
RL
149 /* OpenSSL library side data */
150 CRYPTO_REF_COUNT refcnt;
8752694b 151 CRYPTO_RWLOCK *activatecnt_lock; /* For the activatecnt counter */
2d569501 152 int activatecnt;
4c2883a9 153 char *name;
ac1055ef 154 char *path;
4c2883a9
RL
155 DSO *module;
156 OSSL_provider_init_fn *init_function;
ac1055ef 157 STACK_OF(INFOPAIR) *parameters;
b4250010 158 OSSL_LIB_CTX *libctx; /* The library context this instance is in */
e55008a9 159 struct provider_store_st *store; /* The store this instance belongs to */
f844f9eb 160#ifndef FIPS_MODULE
6592ab81
RL
161 /*
162 * In the FIPS module inner provider, this isn't needed, since the
163 * error upcalls are always direct calls to the outer provider.
164 */
6ebc2f56 165 int error_lib; /* ERR library number, one for each provider */
6592ab81 166# ifndef OPENSSL_NO_ERR
6ebc2f56 167 ERR_STRING_DATA *error_strings; /* Copy of what the provider gives us */
6592ab81 168# endif
6ebc2f56 169#endif
4c2883a9
RL
170
171 /* Provider side functions */
363b1e5d
DMSP
172 OSSL_FUNC_provider_teardown_fn *teardown;
173 OSSL_FUNC_provider_gettable_params_fn *gettable_params;
174 OSSL_FUNC_provider_get_params_fn *get_params;
175 OSSL_FUNC_provider_get_capabilities_fn *get_capabilities;
04cb5ec0 176 OSSL_FUNC_provider_self_test_fn *self_test;
363b1e5d 177 OSSL_FUNC_provider_query_operation_fn *query_operation;
b0001d0c 178 OSSL_FUNC_provider_unquery_operation_fn *unquery_operation;
a39eb840 179
5a29b628
RL
180 /*
181 * Cache of bit to indicate of query_operation() has been called on
182 * a specific operation or not.
183 */
184 unsigned char *operation_bits;
185 size_t operation_bits_sz;
c25a1524 186 CRYPTO_RWLOCK *opbits_lock;
5a29b628 187
c1fb5e07 188#ifndef FIPS_MODULE
f12a5690 189 /* Whether this provider is the child of some other provider */
8c627075 190 const OSSL_CORE_HANDLE *handle;
f12a5690 191 unsigned int ischild:1;
c1fb5e07 192#endif
f12a5690 193
a39eb840
RL
194 /* Provider side data */
195 void *provctx;
f12a5690 196 const OSSL_DISPATCH *dispatch;
4c2883a9
RL
197};
198DEFINE_STACK_OF(OSSL_PROVIDER)
199
200static int ossl_provider_cmp(const OSSL_PROVIDER * const *a,
201 const OSSL_PROVIDER * const *b)
202{
203 return strcmp((*a)->name, (*b)->name);
204}
205
206/*-
207 * Provider Object store
208 * =====================
209 *
210 * The Provider Object store is a library context object, and therefore needs
211 * an index.
212 */
213
214struct provider_store_st {
8c627075 215 OSSL_LIB_CTX *libctx;
4c2883a9 216 STACK_OF(OSSL_PROVIDER) *providers;
7b88c184 217 STACK_OF(OSSL_PROVIDER_CHILD_CB) *child_cbs;
86522324 218 CRYPTO_RWLOCK *default_path_lock;
4c2883a9 219 CRYPTO_RWLOCK *lock;
6bd4e3f2 220 char *default_path;
b7248964 221 OSSL_PROVIDER_INFO *provinfo;
1d74203c
MC
222 size_t numprovinfo;
223 size_t provinfosz;
e55008a9 224 unsigned int use_fallbacks:1;
0090e508 225 unsigned int freeing:1;
4c2883a9 226};
4c2883a9 227
c8567c39 228/*
390f9bad
RL
229 * provider_deactivate_free() is a wrapper around ossl_provider_deactivate()
230 * and ossl_provider_free(), called as needed.
c8567c39
RL
231 * Since this is only called when the provider store is being emptied, we
232 * don't need to care about any lock.
233 */
234static void provider_deactivate_free(OSSL_PROVIDER *prov)
235{
390f9bad 236 if (prov->flag_activated)
c59fc87b 237 ossl_provider_deactivate(prov, 1);
c8567c39
RL
238 ossl_provider_free(prov);
239}
240
c1fb5e07 241#ifndef FIPS_MODULE
7b88c184
MC
242static void ossl_provider_child_cb_free(OSSL_PROVIDER_CHILD_CB *cb)
243{
244 OPENSSL_free(cb);
245}
c1fb5e07 246#endif
7b88c184 247
352d482a
MC
248static void infopair_free(INFOPAIR *pair)
249{
250 OPENSSL_free(pair->name);
251 OPENSSL_free(pair->value);
252 OPENSSL_free(pair);
253}
254
255static INFOPAIR *infopair_copy(const INFOPAIR *src)
256{
257 INFOPAIR *dest = OPENSSL_zalloc(sizeof(*dest));
258
259 if (dest == NULL)
260 return NULL;
261 if (src->name != NULL) {
262 dest->name = OPENSSL_strdup(src->name);
263 if (dest->name == NULL)
264 goto err;
265 }
266 if (src->value != NULL) {
267 dest->value = OPENSSL_strdup(src->value);
268 if (dest->value == NULL)
269 goto err;
270 }
271 return dest;
272 err:
273 OPENSSL_free(dest->name);
274 OPENSSL_free(dest);
275 return NULL;
276}
277
b7248964 278void ossl_provider_info_clear(OSSL_PROVIDER_INFO *info)
352d482a
MC
279{
280 OPENSSL_free(info->name);
281 OPENSSL_free(info->path);
282 sk_INFOPAIR_pop_free(info->parameters, infopair_free);
283}
284
927d0566 285void ossl_provider_store_free(void *vstore)
4c2883a9
RL
286{
287 struct provider_store_st *store = vstore;
1d74203c 288 size_t i;
4c2883a9
RL
289
290 if (store == NULL)
291 return;
0090e508 292 store->freeing = 1;
6bd4e3f2 293 OPENSSL_free(store->default_path);
c8567c39 294 sk_OSSL_PROVIDER_pop_free(store->providers, provider_deactivate_free);
c1fb5e07 295#ifndef FIPS_MODULE
7b88c184
MC
296 sk_OSSL_PROVIDER_CHILD_CB_pop_free(store->child_cbs,
297 ossl_provider_child_cb_free);
c1fb5e07 298#endif
86522324 299 CRYPTO_THREAD_lock_free(store->default_path_lock);
4c2883a9 300 CRYPTO_THREAD_lock_free(store->lock);
1d74203c 301 for (i = 0; i < store->numprovinfo; i++)
352d482a 302 ossl_provider_info_clear(&store->provinfo[i]);
1d74203c 303 OPENSSL_free(store->provinfo);
4c2883a9
RL
304 OPENSSL_free(store);
305}
306
927d0566 307void *ossl_provider_store_new(OSSL_LIB_CTX *ctx)
4c2883a9
RL
308{
309 struct provider_store_st *store = OPENSSL_zalloc(sizeof(*store));
310
311 if (store == NULL
312 || (store->providers = sk_OSSL_PROVIDER_new(ossl_provider_cmp)) == NULL
86522324 313 || (store->default_path_lock = CRYPTO_THREAD_lock_new()) == NULL
c1fb5e07 314#ifndef FIPS_MODULE
7b88c184 315 || (store->child_cbs = sk_OSSL_PROVIDER_CHILD_CB_new_null()) == NULL
c1fb5e07 316#endif
4c2883a9 317 || (store->lock = CRYPTO_THREAD_lock_new()) == NULL) {
927d0566 318 ossl_provider_store_free(store);
e55008a9 319 return NULL;
4c2883a9 320 }
8c627075 321 store->libctx = ctx;
e55008a9 322 store->use_fallbacks = 1;
c41f3ae0 323
4c2883a9
RL
324 return store;
325}
326
b4250010 327static struct provider_store_st *get_provider_store(OSSL_LIB_CTX *libctx)
4c2883a9
RL
328{
329 struct provider_store_st *store = NULL;
330
927d0566 331 store = ossl_lib_ctx_get_data(libctx, OSSL_LIB_CTX_PROVIDER_STORE_INDEX);
4c2883a9 332 if (store == NULL)
9311d0c4 333 ERR_raise(ERR_LIB_CRYPTO, ERR_R_INTERNAL_ERROR);
4c2883a9
RL
334 return store;
335}
336
b4250010 337int ossl_provider_disable_fallback_loading(OSSL_LIB_CTX *libctx)
ebe3f24b
P
338{
339 struct provider_store_st *store;
340
341 if ((store = get_provider_store(libctx)) != NULL) {
cd3f8c1b
RS
342 if (!CRYPTO_THREAD_write_lock(store->lock))
343 return 0;
ebe3f24b 344 store->use_fallbacks = 0;
d60a8e0a 345 CRYPTO_THREAD_unlock(store->lock);
ebe3f24b
P
346 return 1;
347 }
348 return 0;
349}
350
1d74203c
MC
351#define BUILTINS_BLOCK_SIZE 10
352
352d482a 353int ossl_provider_info_add_to_store(OSSL_LIB_CTX *libctx,
b7248964 354 OSSL_PROVIDER_INFO *entry)
1d74203c
MC
355{
356 struct provider_store_st *store = get_provider_store(libctx);
357 int ret = 0;
358
352d482a 359 if (entry->name == NULL) {
1d74203c
MC
360 ERR_raise(ERR_LIB_CRYPTO, ERR_R_PASSED_NULL_PARAMETER);
361 return 0;
362 }
363
364 if (store == NULL) {
365 ERR_raise(ERR_LIB_CRYPTO, ERR_R_INTERNAL_ERROR);
366 return 0;
367 }
368
369 if (!CRYPTO_THREAD_write_lock(store->lock))
370 return 0;
371 if (store->provinfosz == 0) {
372 store->provinfo = OPENSSL_zalloc(sizeof(*store->provinfo)
373 * BUILTINS_BLOCK_SIZE);
e077455e 374 if (store->provinfo == NULL)
1d74203c 375 goto err;
1d74203c
MC
376 store->provinfosz = BUILTINS_BLOCK_SIZE;
377 } else if (store->numprovinfo == store->provinfosz) {
b7248964 378 OSSL_PROVIDER_INFO *tmpbuiltins;
1d74203c
MC
379 size_t newsz = store->provinfosz + BUILTINS_BLOCK_SIZE;
380
381 tmpbuiltins = OPENSSL_realloc(store->provinfo,
382 sizeof(*store->provinfo) * newsz);
e077455e 383 if (tmpbuiltins == NULL)
1d74203c 384 goto err;
1d74203c
MC
385 store->provinfo = tmpbuiltins;
386 store->provinfosz = newsz;
387 }
352d482a 388 store->provinfo[store->numprovinfo] = *entry;
1d74203c
MC
389 store->numprovinfo++;
390
391 ret = 1;
392 err:
393 CRYPTO_THREAD_unlock(store->lock);
394 return ret;
395}
396
b4250010 397OSSL_PROVIDER *ossl_provider_find(OSSL_LIB_CTX *libctx, const char *name,
cb8e6413 398 ossl_unused int noconfig)
4c2883a9
RL
399{
400 struct provider_store_st *store = NULL;
401 OSSL_PROVIDER *prov = NULL;
402
403 if ((store = get_provider_store(libctx)) != NULL) {
404 OSSL_PROVIDER tmpl = { 0, };
405 int i;
406
cb8e6413 407#if !defined(FIPS_MODULE) && !defined(OPENSSL_NO_AUTOLOAD_CONFIG)
29dc6e00
MC
408 /*
409 * Make sure any providers are loaded from config before we try to find
410 * them.
411 */
f12a5690
MC
412 if (!noconfig) {
413 if (ossl_lib_ctx_is_default(libctx))
414 OPENSSL_init_crypto(OPENSSL_INIT_LOAD_CONFIG, NULL);
f12a5690 415 }
29dc6e00
MC
416#endif
417
4c2883a9 418 tmpl.name = (char *)name;
4aced117 419 if (!CRYPTO_THREAD_write_lock(store->lock))
cd3f8c1b 420 return NULL;
07f9c81d 421 sk_OSSL_PROVIDER_sort(store->providers);
2b4a611e
MC
422 if ((i = sk_OSSL_PROVIDER_find(store->providers, &tmpl)) != -1)
423 prov = sk_OSSL_PROVIDER_value(store->providers, i);
4c2883a9 424 CRYPTO_THREAD_unlock(store->lock);
2b4a611e
MC
425 if (prov != NULL && !ossl_provider_up_ref(prov))
426 prov = NULL;
4c2883a9
RL
427 }
428
429 return prov;
430}
431
c41f3ae0
RL
432/*-
433 * Provider Object methods
434 * =======================
435 */
436
437static OSSL_PROVIDER *provider_new(const char *name,
352d482a
MC
438 OSSL_provider_init_fn *init_function,
439 STACK_OF(INFOPAIR) *parameters)
c41f3ae0
RL
440{
441 OSSL_PROVIDER *prov = NULL;
442
e077455e
RL
443 if ((prov = OPENSSL_zalloc(sizeof(*prov))) == NULL)
444 return NULL;
8752694b 445 if (!CRYPTO_NEW_REF(&prov->refcnt, 1)) {
97beb77f 446 OPENSSL_free(prov);
8752694b
P
447 return NULL;
448 }
8752694b
P
449 if ((prov->activatecnt_lock = CRYPTO_THREAD_lock_new()) == NULL) {
450 ossl_provider_free(prov);
e077455e 451 ERR_raise(ERR_LIB_CRYPTO, ERR_R_CRYPTO_LIB);
c4ed6f6f
MC
452 return NULL;
453 }
454
c4ed6f6f 455 if ((prov->opbits_lock = CRYPTO_THREAD_lock_new()) == NULL
c2ec2bb7 456 || (prov->flag_lock = CRYPTO_THREAD_lock_new()) == NULL
352d482a
MC
457 || (prov->parameters = sk_INFOPAIR_deep_copy(parameters,
458 infopair_copy,
459 infopair_free)) == NULL) {
c41f3ae0 460 ossl_provider_free(prov);
e077455e
RL
461 ERR_raise(ERR_LIB_CRYPTO, ERR_R_CRYPTO_LIB);
462 return NULL;
463 }
464 if ((prov->name = OPENSSL_strdup(name)) == NULL) {
465 ossl_provider_free(prov);
c41f3ae0
RL
466 return NULL;
467 }
468
469 prov->init_function = init_function;
352d482a 470
c41f3ae0
RL
471 return prov;
472}
473
7c95390e 474int ossl_provider_up_ref(OSSL_PROVIDER *prov)
c41f3ae0
RL
475{
476 int ref = 0;
477
8752694b 478 if (CRYPTO_UP_REF(&prov->refcnt, &ref) <= 0)
ad14e8e5 479 return 0;
8c627075
MC
480
481#ifndef FIPS_MODULE
482 if (prov->ischild) {
483 if (!ossl_provider_up_ref_parent(prov, 0)) {
484 ossl_provider_free(prov);
485 return 0;
486 }
487 }
488#endif
489
c41f3ae0
RL
490 return ref;
491}
492
8c627075
MC
493#ifndef FIPS_MODULE
494static int provider_up_ref_intern(OSSL_PROVIDER *prov, int activate)
495{
496 if (activate)
814c2018 497 return ossl_provider_activate(prov, 1, 0);
8c627075
MC
498
499 return ossl_provider_up_ref(prov);
500}
501
502static int provider_free_intern(OSSL_PROVIDER *prov, int deactivate)
503{
504 if (deactivate)
c59fc87b 505 return ossl_provider_deactivate(prov, 1);
8c627075
MC
506
507 ossl_provider_free(prov);
508 return 1;
509}
510#endif
511
dc6d9ede
MC
512/*
513 * We assume that the requested provider does not already exist in the store.
514 * The caller should check. If it does exist then adding it to the store later
515 * will fail.
516 */
b4250010 517OSSL_PROVIDER *ossl_provider_new(OSSL_LIB_CTX *libctx, const char *name,
29dc6e00 518 OSSL_provider_init_fn *init_function,
9d2f7e1f 519 OSSL_PARAM *params, int noconfig)
4c2883a9
RL
520{
521 struct provider_store_st *store = NULL;
b7248964 522 OSSL_PROVIDER_INFO template;
4c2883a9
RL
523 OSSL_PROVIDER *prov = NULL;
524
525 if ((store = get_provider_store(libctx)) == NULL)
526 return NULL;
527
352d482a 528 memset(&template, 0, sizeof(template));
8d4dec0d 529 if (init_function == NULL) {
b7248964 530 const OSSL_PROVIDER_INFO *p;
1d74203c 531 size_t i;
8d4dec0d 532
1d74203c 533 /* Check if this is a predefined builtin provider */
8d4dec0d
MC
534 for (p = ossl_predefined_providers; p->name != NULL; p++) {
535 if (strcmp(p->name, name) == 0) {
352d482a 536 template = *p;
8d4dec0d
MC
537 break;
538 }
539 }
1d74203c 540 if (p->name == NULL) {
9d2f7e1f 541 /* Check if this is a user added provider */
1d74203c
MC
542 if (!CRYPTO_THREAD_read_lock(store->lock))
543 return NULL;
544 for (i = 0, p = store->provinfo; i < store->numprovinfo; p++, i++) {
545 if (strcmp(p->name, name) == 0) {
352d482a 546 template = *p;
1d74203c
MC
547 break;
548 }
549 }
550 CRYPTO_THREAD_unlock(store->lock);
551 }
552 } else {
553 template.init = init_function;
8d4dec0d
MC
554 }
555
9d2f7e1f
DB
556 if (params != NULL) {
557 int i;
558
559 template.parameters = sk_INFOPAIR_new_null();
560 if (template.parameters == NULL)
561 return NULL;
562
563 for (i = 0; params[i].key != NULL; i++) {
564 if (params[i].data_type != OSSL_PARAM_UTF8_STRING)
565 continue;
566 if (ossl_provider_info_add_parameter(&template, params[i].key,
875db35a
TM
567 (char *)params[i].data) <= 0) {
568 sk_INFOPAIR_pop_free(template.parameters, infopair_free);
9d2f7e1f 569 return NULL;
875db35a 570 }
9d2f7e1f
DB
571 }
572 }
573
c41f3ae0 574 /* provider_new() generates an error, so no need here */
9d2f7e1f
DB
575 prov = provider_new(name, template.init, template.parameters);
576
577 if (params != NULL) /* We copied the parameters, let's free them */
578 sk_INFOPAIR_pop_free(template.parameters, infopair_free);
579
580 if (prov == NULL)
4c2883a9 581 return NULL;
4c2883a9 582
4e3c1e62
NH
583 if (!ossl_provider_set_module_path(prov, template.path)) {
584 ossl_provider_free(prov);
585 return NULL;
586 }
587
8d4dec0d 588 prov->libctx = libctx;
8d4dec0d
MC
589#ifndef FIPS_MODULE
590 prov->error_lib = ERR_get_next_error_library();
591#endif
592
4c2883a9
RL
593 /*
594 * At this point, the provider is only partially "loaded". To be
29aff653
MC
595 * fully "loaded", ossl_provider_activate() must also be called and it must
596 * then be added to the provider store.
4c2883a9
RL
597 */
598
599 return prov;
600}
601
f109e965
MC
602/* Assumes that the store lock is held */
603static int create_provider_children(OSSL_PROVIDER *prov)
604{
605 int ret = 1;
606#ifndef FIPS_MODULE
607 struct provider_store_st *store = prov->store;
608 OSSL_PROVIDER_CHILD_CB *child_cb;
609 int i, max;
610
611 max = sk_OSSL_PROVIDER_CHILD_CB_num(store->child_cbs);
612 for (i = 0; i < max; i++) {
613 /*
614 * This is newly activated (activatecnt == 1), so we need to
615 * create child providers as necessary.
616 */
617 child_cb = sk_OSSL_PROVIDER_CHILD_CB_value(store->child_cbs, i);
618 ret &= child_cb->create_cb((OSSL_CORE_HANDLE *)prov, child_cb->cbdata);
619 }
620#endif
621
622 return ret;
623}
624
59a783d0
MC
625int ossl_provider_add_to_store(OSSL_PROVIDER *prov, OSSL_PROVIDER **actualprov,
626 int retain_fallbacks)
29aff653 627{
59a783d0
MC
628 struct provider_store_st *store;
629 int idx;
630 OSSL_PROVIDER tmpl = { 0, };
631 OSSL_PROVIDER *actualtmp = NULL;
29aff653 632
33df7cbe
TM
633 if (actualprov != NULL)
634 *actualprov = NULL;
635
29aff653
MC
636 if ((store = get_provider_store(prov->libctx)) == NULL)
637 return 0;
638
59a783d0 639 if (!CRYPTO_THREAD_write_lock(store->lock))
29aff653 640 return 0;
59a783d0
MC
641
642 tmpl.name = (char *)prov->name;
643 idx = sk_OSSL_PROVIDER_find(store->providers, &tmpl);
644 if (idx == -1)
645 actualtmp = prov;
646 else
647 actualtmp = sk_OSSL_PROVIDER_value(store->providers, idx);
648
59a783d0
MC
649 if (idx == -1) {
650 if (sk_OSSL_PROVIDER_push(store->providers, prov) == 0)
651 goto err;
652 prov->store = store;
653 if (!create_provider_children(prov)) {
654 sk_OSSL_PROVIDER_delete_ptr(store->providers, prov);
655 goto err;
656 }
657 if (!retain_fallbacks)
658 store->use_fallbacks = 0;
f109e965 659 }
59a783d0 660
29aff653
MC
661 CRYPTO_THREAD_unlock(store->lock);
662
2b4a611e
MC
663 if (actualprov != NULL) {
664 if (!ossl_provider_up_ref(actualtmp)) {
e077455e 665 ERR_raise(ERR_LIB_CRYPTO, ERR_R_CRYPTO_LIB);
2b4a611e 666 actualtmp = NULL;
61f51060 667 return 0;
2b4a611e
MC
668 }
669 *actualprov = actualtmp;
670 }
671
672 if (idx >= 0) {
59a783d0
MC
673 /*
674 * The provider is already in the store. Probably two threads
675 * independently initialised their own provider objects with the same
676 * name and raced to put them in the store. This thread lost. We
677 * deactivate the one we just created and use the one that already
678 * exists instead.
c59fc87b
MC
679 * If we get here then we know we did not create provider children
680 * above, so we inform ossl_provider_deactivate not to attempt to remove
681 * any.
59a783d0 682 */
c59fc87b 683 ossl_provider_deactivate(prov, 0);
59a783d0
MC
684 ossl_provider_free(prov);
685 }
32d3c3ab
MC
686#ifndef FIPS_MODULE
687 else {
688 /*
689 * This can be done outside the lock. We tolerate other threads getting
690 * the wrong result briefly when creating OSSL_DECODER_CTXs.
691 */
692 ossl_decoder_cache_flush(prov->libctx);
693 }
694#endif
59a783d0
MC
695
696 return 1;
697
698 err:
699 CRYPTO_THREAD_unlock(store->lock);
59a783d0 700 return 0;
29aff653
MC
701}
702
4c2883a9
RL
703void ossl_provider_free(OSSL_PROVIDER *prov)
704{
705 if (prov != NULL) {
706 int ref = 0;
707
8752694b 708 CRYPTO_DOWN_REF(&prov->refcnt, &ref);
4c2883a9
RL
709
710 /*
390f9bad
RL
711 * When the refcount drops to zero, we clean up the provider.
712 * Note that this also does teardown, which may seem late,
713 * considering that init happens on first activation. However,
714 * there may be other structures hanging on to the provider after
715 * the last deactivation and may therefore need full access to the
716 * provider's services. Therefore, we deinit late.
4c2883a9 717 */
390f9bad
RL
718 if (ref == 0) {
719 if (prov->flag_initialized) {
f12a5690 720 ossl_provider_teardown(prov);
6ebc2f56 721#ifndef OPENSSL_NO_ERR
f844f9eb 722# ifndef FIPS_MODULE
390f9bad
RL
723 if (prov->error_strings != NULL) {
724 ERR_unload_strings(prov->error_lib, prov->error_strings);
725 OPENSSL_free(prov->error_strings);
726 prov->error_strings = NULL;
727 }
6ebc2f56
RL
728# endif
729#endif
390f9bad
RL
730 OPENSSL_free(prov->operation_bits);
731 prov->operation_bits = NULL;
732 prov->operation_bits_sz = 0;
733 prov->flag_initialized = 0;
734 }
4c2883a9 735
f844f9eb 736#ifndef FIPS_MODULE
ee067bc0
MC
737 /*
738 * We deregister thread handling whether or not the provider was
739 * initialized. If init was attempted but was not successful then
740 * the provider may still have registered a thread handler.
741 */
742 ossl_init_thread_deregister(prov);
4c2883a9 743 DSO_free(prov->module);
3593266d 744#endif
4c2883a9 745 OPENSSL_free(prov->name);
ac1055ef 746 OPENSSL_free(prov->path);
352d482a 747 sk_INFOPAIR_pop_free(prov->parameters, infopair_free);
c25a1524 748 CRYPTO_THREAD_lock_free(prov->opbits_lock);
c2ec2bb7 749 CRYPTO_THREAD_lock_free(prov->flag_lock);
8752694b 750 CRYPTO_THREAD_lock_free(prov->activatecnt_lock);
8752694b 751 CRYPTO_FREE_REF(&prov->refcnt);
4c2883a9
RL
752 OPENSSL_free(prov);
753 }
8c627075
MC
754#ifndef FIPS_MODULE
755 else if (prov->ischild) {
756 ossl_provider_free_parent(prov, 0);
757 }
758#endif
4c2883a9
RL
759 }
760}
761
ac1055ef
RL
762/* Setters */
763int ossl_provider_set_module_path(OSSL_PROVIDER *prov, const char *module_path)
764{
765 OPENSSL_free(prov->path);
6cf811e8 766 prov->path = NULL;
ac1055ef
RL
767 if (module_path == NULL)
768 return 1;
769 if ((prov->path = OPENSSL_strdup(module_path)) != NULL)
770 return 1;
ac1055ef
RL
771 return 0;
772}
773
352d482a
MC
774static int infopair_add(STACK_OF(INFOPAIR) **infopairsk, const char *name,
775 const char *value)
ac1055ef
RL
776{
777 INFOPAIR *pair = NULL;
778
e077455e
RL
779 if ((pair = OPENSSL_zalloc(sizeof(*pair))) == NULL
780 || (pair->name = OPENSSL_strdup(name)) == NULL
781 || (pair->value = OPENSSL_strdup(value)) == NULL)
782 goto err;
783
784 if ((*infopairsk == NULL
785 && (*infopairsk = sk_INFOPAIR_new_null()) == NULL)
786 || sk_INFOPAIR_push(*infopairsk, pair) <= 0) {
787 ERR_raise(ERR_LIB_CRYPTO, ERR_R_CRYPTO_LIB);
788 goto err;
789 }
790
791 return 1;
ac1055ef 792
e077455e 793 err:
ac1055ef
RL
794 if (pair != NULL) {
795 OPENSSL_free(pair->name);
796 OPENSSL_free(pair->value);
797 OPENSSL_free(pair);
798 }
ac1055ef
RL
799 return 0;
800}
801
352d482a
MC
802int ossl_provider_add_parameter(OSSL_PROVIDER *prov,
803 const char *name, const char *value)
804{
805 return infopair_add(&prov->parameters, name, value);
806}
807
b7248964 808int ossl_provider_info_add_parameter(OSSL_PROVIDER_INFO *provinfo,
352d482a
MC
809 const char *name,
810 const char *value)
811{
812 return infopair_add(&provinfo->parameters, name, value);
813}
814
4c2883a9
RL
815/*
816 * Provider activation.
817 *
818 * What "activation" means depends on the provider form; for built in
819 * providers (in the library or the application alike), the provider
820 * can already be considered to be loaded, all that's needed is to
821 * initialize it. However, for dynamically loadable provider modules,
822 * we must first load that module.
823 *
824 * Built in modules are distinguished from dynamically loaded modules
825 * with an already assigned init function.
826 */
827static const OSSL_DISPATCH *core_dispatch; /* Define further down */
828
b4250010
DMSP
829int OSSL_PROVIDER_set_default_search_path(OSSL_LIB_CTX *libctx,
830 const char *path)
6bd4e3f2
P
831{
832 struct provider_store_st *store;
833 char *p = NULL;
834
835 if (path != NULL) {
836 p = OPENSSL_strdup(path);
e077455e 837 if (p == NULL)
6bd4e3f2 838 return 0;
6bd4e3f2
P
839 }
840 if ((store = get_provider_store(libctx)) != NULL
86522324 841 && CRYPTO_THREAD_write_lock(store->default_path_lock)) {
6bd4e3f2
P
842 OPENSSL_free(store->default_path);
843 store->default_path = p;
86522324 844 CRYPTO_THREAD_unlock(store->default_path_lock);
6bd4e3f2
P
845 return 1;
846 }
847 OPENSSL_free(p);
848 return 0;
849}
850
d3db25f5
PM
851const char *OSSL_PROVIDER_get0_default_search_path(OSSL_LIB_CTX *libctx)
852{
853 struct provider_store_st *store;
854 char *path = NULL;
855
856 if ((store = get_provider_store(libctx)) != NULL
857 && CRYPTO_THREAD_read_lock(store->default_path_lock)) {
858 path = store->default_path;
859 CRYPTO_THREAD_unlock(store->default_path_lock);
860 }
861 return path;
862}
863
e55008a9
RL
864/*
865 * Internal version that doesn't affect the store flags, and thereby avoid
866 * locking. Direct callers must remember to set the store flags when
e7706e63 867 * appropriate.
e55008a9 868 */
f109e965 869static int provider_init(OSSL_PROVIDER *prov)
4c2883a9
RL
870{
871 const OSSL_DISPATCH *provider_dispatch = NULL;
914db66d 872 void *tmp_provctx = NULL; /* safety measure */
6ebc2f56 873#ifndef OPENSSL_NO_ERR
f844f9eb 874# ifndef FIPS_MODULE
363b1e5d 875 OSSL_FUNC_provider_get_reason_strings_fn *p_get_reason_strings = NULL;
6592ab81 876# endif
6ebc2f56 877#endif
c2ec2bb7 878 int ok = 0;
4c2883a9 879
f109e965
MC
880 if (!ossl_assert(!prov->flag_initialized)) {
881 ERR_raise(ERR_LIB_CRYPTO, ERR_R_INTERNAL_ERROR);
c2ec2bb7
RL
882 goto end;
883 }
4c2883a9
RL
884
885 /*
886 * If the init function isn't set, it indicates that this provider is
887 * a loadable module.
888 */
889 if (prov->init_function == NULL) {
f844f9eb 890#ifdef FIPS_MODULE
c2ec2bb7 891 goto end;
3593266d 892#else
4c2883a9 893 if (prov->module == NULL) {
ac1055ef
RL
894 char *allocated_path = NULL;
895 const char *module_path = NULL;
896 char *merged_path = NULL;
6bd4e3f2 897 const char *load_dir = NULL;
86522324 898 char *allocated_load_dir = NULL;
6bd4e3f2 899 struct provider_store_st *store;
4c2883a9
RL
900
901 if ((prov->module = DSO_new()) == NULL) {
902 /* DSO_new() generates an error already */
c2ec2bb7 903 goto end;
4c2883a9
RL
904 }
905
6bd4e3f2 906 if ((store = get_provider_store(prov->libctx)) == NULL
86522324 907 || !CRYPTO_THREAD_read_lock(store->default_path_lock))
c2ec2bb7 908 goto end;
86522324
SP
909
910 if (store->default_path != NULL) {
911 allocated_load_dir = OPENSSL_strdup(store->default_path);
912 CRYPTO_THREAD_unlock(store->default_path_lock);
e077455e 913 if (allocated_load_dir == NULL)
86522324 914 goto end;
86522324
SP
915 load_dir = allocated_load_dir;
916 } else {
917 CRYPTO_THREAD_unlock(store->default_path_lock);
918 }
6bd4e3f2
P
919
920 if (load_dir == NULL) {
921 load_dir = ossl_safe_getenv("OPENSSL_MODULES");
922 if (load_dir == NULL)
923 load_dir = MODULESDIR;
924 }
4c2883a9
RL
925
926 DSO_ctrl(prov->module, DSO_CTRL_SET_FLAGS,
927 DSO_FLAG_NAME_TRANSLATION_EXT_ONLY, NULL);
ac1055ef
RL
928
929 module_path = prov->path;
930 if (module_path == NULL)
931 module_path = allocated_path =
932 DSO_convert_filename(prov->module, prov->name);
933 if (module_path != NULL)
934 merged_path = DSO_merge(prov->module, module_path, load_dir);
935
936 if (merged_path == NULL
937 || (DSO_load(prov->module, merged_path, NULL, 0)) == NULL) {
4c2883a9
RL
938 DSO_free(prov->module);
939 prov->module = NULL;
940 }
941
ac1055ef
RL
942 OPENSSL_free(merged_path);
943 OPENSSL_free(allocated_path);
86522324 944 OPENSSL_free(allocated_load_dir);
4c2883a9
RL
945 }
946
2d23ba14
RL
947 if (prov->module == NULL) {
948 /* DSO has already recorded errors, this is just a tracepoint */
949 ERR_raise_data(ERR_LIB_CRYPTO, ERR_R_DSO_LIB,
950 "name=%s", prov->name);
951 goto end;
952 }
953
954 prov->init_function = (OSSL_provider_init_fn *)
955 DSO_bind_func(prov->module, "OSSL_provider_init");
3593266d 956#endif
4c2883a9
RL
957 }
958
2d23ba14
RL
959 /* Check for and call the initialise function for the provider. */
960 if (prov->init_function == NULL) {
961 ERR_raise_data(ERR_LIB_CRYPTO, ERR_R_UNSUPPORTED,
962 "name=%s, provider has no provider init function",
963 prov->name);
964 goto end;
965 }
966
967 if (!prov->init_function((OSSL_CORE_HANDLE *)prov, core_dispatch,
968 &provider_dispatch, &tmp_provctx)) {
105d01f1 969 ERR_raise_data(ERR_LIB_CRYPTO, ERR_R_INIT_FAIL,
49c64346 970 "name=%s", prov->name);
c2ec2bb7 971 goto end;
4c2883a9 972 }
914db66d 973 prov->provctx = tmp_provctx;
f12a5690 974 prov->dispatch = provider_dispatch;
4c2883a9 975
8fa65a66
RL
976 if (provider_dispatch != NULL) {
977 for (; provider_dispatch->function_id != 0; provider_dispatch++) {
978 switch (provider_dispatch->function_id) {
979 case OSSL_FUNC_PROVIDER_TEARDOWN:
980 prov->teardown =
981 OSSL_FUNC_provider_teardown(provider_dispatch);
982 break;
983 case OSSL_FUNC_PROVIDER_GETTABLE_PARAMS:
984 prov->gettable_params =
985 OSSL_FUNC_provider_gettable_params(provider_dispatch);
986 break;
987 case OSSL_FUNC_PROVIDER_GET_PARAMS:
988 prov->get_params =
989 OSSL_FUNC_provider_get_params(provider_dispatch);
990 break;
991 case OSSL_FUNC_PROVIDER_SELF_TEST:
992 prov->self_test =
993 OSSL_FUNC_provider_self_test(provider_dispatch);
994 break;
995 case OSSL_FUNC_PROVIDER_GET_CAPABILITIES:
996 prov->get_capabilities =
997 OSSL_FUNC_provider_get_capabilities(provider_dispatch);
998 break;
999 case OSSL_FUNC_PROVIDER_QUERY_OPERATION:
1000 prov->query_operation =
1001 OSSL_FUNC_provider_query_operation(provider_dispatch);
1002 break;
1003 case OSSL_FUNC_PROVIDER_UNQUERY_OPERATION:
1004 prov->unquery_operation =
1005 OSSL_FUNC_provider_unquery_operation(provider_dispatch);
1006 break;
6ebc2f56 1007#ifndef OPENSSL_NO_ERR
f844f9eb 1008# ifndef FIPS_MODULE
8fa65a66
RL
1009 case OSSL_FUNC_PROVIDER_GET_REASON_STRINGS:
1010 p_get_reason_strings =
1011 OSSL_FUNC_provider_get_reason_strings(provider_dispatch);
1012 break;
6592ab81 1013# endif
6ebc2f56 1014#endif
8fa65a66 1015 }
6ebc2f56
RL
1016 }
1017 }
1018
1019#ifndef OPENSSL_NO_ERR
f844f9eb 1020# ifndef FIPS_MODULE
6ebc2f56
RL
1021 if (p_get_reason_strings != NULL) {
1022 const OSSL_ITEM *reasonstrings = p_get_reason_strings(prov->provctx);
1023 size_t cnt, cnt2;
1024
1025 /*
1026 * ERR_load_strings() handles ERR_STRING_DATA rather than OSSL_ITEM,
1027 * although they are essentially the same type.
1028 * Furthermore, ERR_load_strings() patches the array's error number
1029 * with the error library number, so we need to make a copy of that
1030 * array either way.
1031 */
fd03868b 1032 cnt = 0;
6ebc2f56
RL
1033 while (reasonstrings[cnt].id != 0) {
1034 if (ERR_GET_LIB(reasonstrings[cnt].id) != 0)
c2ec2bb7 1035 goto end;
6ebc2f56
RL
1036 cnt++;
1037 }
fd03868b 1038 cnt++; /* One for the terminating item */
6ebc2f56
RL
1039
1040 /* Allocate one extra item for the "library" name */
1041 prov->error_strings =
1042 OPENSSL_zalloc(sizeof(ERR_STRING_DATA) * (cnt + 1));
1043 if (prov->error_strings == NULL)
c2ec2bb7 1044 goto end;
6ebc2f56
RL
1045
1046 /*
1047 * Set the "library" name.
1048 */
1049 prov->error_strings[0].error = ERR_PACK(prov->error_lib, 0, 0);
1050 prov->error_strings[0].string = prov->name;
1051 /*
1052 * Copy reasonstrings item 0..cnt-1 to prov->error_trings positions
1053 * 1..cnt.
1054 */
1055 for (cnt2 = 1; cnt2 <= cnt; cnt2++) {
1056 prov->error_strings[cnt2].error = (int)reasonstrings[cnt2-1].id;
1057 prov->error_strings[cnt2].string = reasonstrings[cnt2-1].ptr;
4c2883a9 1058 }
6ebc2f56
RL
1059
1060 ERR_load_strings(prov->error_lib, prov->error_strings);
4c2883a9 1061 }
6592ab81 1062# endif
6ebc2f56 1063#endif
4c2883a9
RL
1064
1065 /* With this flag set, this provider has become fully "loaded". */
1066 prov->flag_initialized = 1;
c2ec2bb7
RL
1067 ok = 1;
1068
1069 end:
c2ec2bb7 1070 return ok;
4c2883a9
RL
1071}
1072
0090e508 1073/*
c59fc87b
MC
1074 * Deactivate a provider. If upcalls is 0 then we suppress any upcalls to a
1075 * parent provider. If removechildren is 0 then we suppress any calls to remove
1076 * child providers.
0090e508
P
1077 * Return -1 on failure and the activation count on success
1078 */
c59fc87b
MC
1079static int provider_deactivate(OSSL_PROVIDER *prov, int upcalls,
1080 int removechildren)
390f9bad 1081{
0090e508 1082 int count;
7b88c184 1083 struct provider_store_st *store;
2b4a611e 1084#ifndef FIPS_MODULE
c59fc87b 1085 int freeparent = 0;
2b4a611e 1086#endif
e39bd621 1087 int lock = 1;
0090e508 1088
390f9bad 1089 if (!ossl_assert(prov != NULL))
0090e508 1090 return -1;
390f9bad 1091
e39bd621
MC
1092 /*
1093 * No need to lock if we've got no store because we've not been shared with
1094 * other threads.
1095 */
7b88c184
MC
1096 store = get_provider_store(prov->libctx);
1097 if (store == NULL)
e39bd621 1098 lock = 0;
7b88c184 1099
e39bd621 1100 if (lock && !CRYPTO_THREAD_read_lock(store->lock))
0090e508 1101 return -1;
e39bd621 1102 if (lock && !CRYPTO_THREAD_write_lock(prov->flag_lock)) {
c1fb5e07
MC
1103 CRYPTO_THREAD_unlock(store->lock);
1104 return -1;
1105 }
390f9bad 1106
8752694b 1107 CRYPTO_atomic_add(&prov->activatecnt, -1, &count, prov->activatecnt_lock);
8c627075 1108#ifndef FIPS_MODULE
fc570b26 1109 if (count >= 1 && prov->ischild && upcalls) {
8c627075
MC
1110 /*
1111 * We have had a direct activation in this child libctx so we need to
2b4a611e
MC
1112 * now down the ref count in the parent provider. We do the actual down
1113 * ref outside of the flag_lock, since it could involve getting other
1114 * locks.
8c627075 1115 */
2b4a611e 1116 freeparent = 1;
8c627075
MC
1117 }
1118#endif
1119
fc570b26 1120 if (count < 1)
390f9bad 1121 prov->flag_activated = 0;
c1fb5e07 1122#ifndef FIPS_MODULE
c59fc87b
MC
1123 else
1124 removechildren = 0;
c1fb5e07 1125#endif
2d569501 1126
2b4a611e 1127#ifndef FIPS_MODULE
e39bd621 1128 if (removechildren && store != NULL) {
2b4a611e
MC
1129 int i, max = sk_OSSL_PROVIDER_CHILD_CB_num(store->child_cbs);
1130 OSSL_PROVIDER_CHILD_CB *child_cb;
1131
1132 for (i = 0; i < max; i++) {
1133 child_cb = sk_OSSL_PROVIDER_CHILD_CB_value(store->child_cbs, i);
1134 child_cb->remove_cb((OSSL_CORE_HANDLE *)prov, child_cb->cbdata);
1135 }
1136 }
1137#endif
addbd7c9
MC
1138 if (lock) {
1139 CRYPTO_THREAD_unlock(prov->flag_lock);
e39bd621 1140 CRYPTO_THREAD_unlock(store->lock);
32d3c3ab
MC
1141 /*
1142 * This can be done outside the lock. We tolerate other threads getting
1143 * the wrong result briefly when creating OSSL_DECODER_CTXs.
1144 */
1145#ifndef FIPS_MODULE
1146 if (count < 1)
1147 ossl_decoder_cache_flush(prov->libctx);
1148#endif
addbd7c9 1149 }
2b4a611e
MC
1150#ifndef FIPS_MODULE
1151 if (freeparent)
1152 ossl_provider_free_parent(prov, 1);
1153#endif
390f9bad
RL
1154
1155 /* We don't deinit here, that's done in ossl_provider_free() */
0090e508 1156 return count;
390f9bad
RL
1157}
1158
0090e508
P
1159/*
1160 * Activate a provider.
1161 * Return -1 on failure and the activation count on success
1162 */
8c627075 1163static int provider_activate(OSSL_PROVIDER *prov, int lock, int upcalls)
390f9bad 1164{
7b88c184 1165 int count = -1;
f109e965 1166 struct provider_store_st *store;
addbd7c9 1167 int ret = 1;
0090e508 1168
f109e965
MC
1169 store = prov->store;
1170 /*
1171 * If the provider hasn't been added to the store, then we don't need
1172 * any locks because we've not shared it with other threads.
1173 */
1174 if (store == NULL) {
1175 lock = 0;
1176 if (!provider_init(prov))
c1fb5e07 1177 return -1;
f109e965 1178 }
390f9bad 1179
2b4a611e
MC
1180#ifndef FIPS_MODULE
1181 if (prov->ischild && upcalls && !ossl_provider_up_ref_parent(prov, 1))
f109e965 1182 return -1;
2b4a611e 1183#endif
7b88c184 1184
2b4a611e
MC
1185 if (lock && !CRYPTO_THREAD_read_lock(store->lock)) {
1186#ifndef FIPS_MODULE
1187 if (prov->ischild && upcalls)
1188 ossl_provider_free_parent(prov, 1);
1189#endif
f109e965
MC
1190 return -1;
1191 }
8c627075 1192
2b4a611e
MC
1193 if (lock && !CRYPTO_THREAD_write_lock(prov->flag_lock)) {
1194 CRYPTO_THREAD_unlock(store->lock);
8c627075 1195#ifndef FIPS_MODULE
2b4a611e
MC
1196 if (prov->ischild && upcalls)
1197 ossl_provider_free_parent(prov, 1);
8c627075 1198#endif
2b4a611e
MC
1199 return -1;
1200 }
8752694b 1201 if (CRYPTO_atomic_add(&prov->activatecnt, 1, &count, prov->activatecnt_lock)) {
fc570b26 1202 prov->flag_activated = 1;
8c627075 1203
fc570b26
MC
1204 if (count == 1 && store != NULL) {
1205 ret = create_provider_children(prov);
1206 }
addbd7c9
MC
1207 }
1208 if (lock) {
1209 CRYPTO_THREAD_unlock(prov->flag_lock);
f109e965 1210 CRYPTO_THREAD_unlock(store->lock);
32d3c3ab
MC
1211 /*
1212 * This can be done outside the lock. We tolerate other threads getting
1213 * the wrong result briefly when creating OSSL_DECODER_CTXs.
1214 */
1215#ifndef FIPS_MODULE
1216 if (count == 1)
1217 ossl_decoder_cache_flush(prov->libctx);
1218#endif
addbd7c9 1219 }
2b4a611e 1220
f109e965
MC
1221 if (!ret)
1222 return -1;
390f9bad 1223
7b88c184 1224 return count;
0090e508
P
1225}
1226
1227static int provider_flush_store_cache(const OSSL_PROVIDER *prov)
1228{
1229 struct provider_store_st *store;
1230 int freeing;
1231
1232 if ((store = get_provider_store(prov->libctx)) == NULL)
1233 return 0;
1234
1235 if (!CRYPTO_THREAD_read_lock(store->lock))
1236 return 0;
1237 freeing = store->freeing;
1238 CRYPTO_THREAD_unlock(store->lock);
1239
32e3c071
RL
1240 if (!freeing) {
1241 int acc
1242 = evp_method_store_cache_flush(prov->libctx)
1243#ifndef FIPS_MODULE
1244 + ossl_encoder_store_cache_flush(prov->libctx)
1245 + ossl_decoder_store_cache_flush(prov->libctx)
1246 + ossl_store_loader_store_cache_flush(prov->libctx)
1247#endif
1248 ;
1249
1250#ifndef FIPS_MODULE
1251 return acc == 4;
1252#else
1253 return acc == 1;
1254#endif
1255 }
0090e508 1256 return 1;
390f9bad
RL
1257}
1258
2e4d0677
RL
1259static int provider_remove_store_methods(OSSL_PROVIDER *prov)
1260{
1261 struct provider_store_st *store;
1262 int freeing;
1263
1264 if ((store = get_provider_store(prov->libctx)) == NULL)
1265 return 0;
1266
1267 if (!CRYPTO_THREAD_read_lock(store->lock))
1268 return 0;
1269 freeing = store->freeing;
1270 CRYPTO_THREAD_unlock(store->lock);
1271
1272 if (!freeing) {
32e3c071
RL
1273 int acc;
1274
6962e21b 1275 if (!CRYPTO_THREAD_write_lock(prov->opbits_lock))
32e3c071 1276 return 0;
2e4d0677
RL
1277 OPENSSL_free(prov->operation_bits);
1278 prov->operation_bits = NULL;
1279 prov->operation_bits_sz = 0;
1280 CRYPTO_THREAD_unlock(prov->opbits_lock);
1281
32e3c071
RL
1282 acc = evp_method_store_remove_all_provided(prov)
1283#ifndef FIPS_MODULE
1284 + ossl_encoder_store_remove_all_provided(prov)
1285 + ossl_decoder_store_remove_all_provided(prov)
1286 + ossl_store_loader_store_remove_all_provided(prov)
1287#endif
1288 ;
1289
1290#ifndef FIPS_MODULE
1291 return acc == 4;
1292#else
1293 return acc == 1;
1294#endif
2e4d0677
RL
1295 }
1296 return 1;
1297}
1298
814c2018 1299int ossl_provider_activate(OSSL_PROVIDER *prov, int upcalls, int aschild)
e55008a9 1300{
0090e508
P
1301 int count;
1302
390f9bad
RL
1303 if (prov == NULL)
1304 return 0;
814c2018
MC
1305#ifndef FIPS_MODULE
1306 /*
1307 * If aschild is true, then we only actually do the activation if the
1308 * provider is a child. If its not, this is still success.
1309 */
1310 if (aschild && !prov->ischild)
1311 return 1;
1312#endif
eb2263da 1313 if ((count = provider_activate(prov, 1, upcalls)) > 0)
0090e508 1314 return count == 1 ? provider_flush_store_cache(prov) : 1;
eb2263da 1315
e55008a9
RL
1316 return 0;
1317}
1318
c59fc87b 1319int ossl_provider_deactivate(OSSL_PROVIDER *prov, int removechildren)
390f9bad 1320{
0090e508
P
1321 int count;
1322
c59fc87b
MC
1323 if (prov == NULL
1324 || (count = provider_deactivate(prov, 1, removechildren)) < 0)
390f9bad 1325 return 0;
2e4d0677 1326 return count == 0 ? provider_remove_store_methods(prov) : 1;
390f9bad
RL
1327}
1328
a39eb840
RL
1329void *ossl_provider_ctx(const OSSL_PROVIDER *prov)
1330{
f913c3cd 1331 return prov != NULL ? prov->provctx : NULL;
a39eb840
RL
1332}
1333
36f5ec55
RL
1334/*
1335 * This function only does something once when store->use_fallbacks == 1,
1336 * and then sets store->use_fallbacks = 0, so the second call and so on is
1337 * effectively a no-op.
1338 */
8d4dec0d 1339static int provider_activate_fallbacks(struct provider_store_st *store)
36f5ec55 1340{
7dd2cb56 1341 int use_fallbacks;
7dd2cb56 1342 int activated_fallback_count = 0;
8d4dec0d 1343 int ret = 0;
b7248964 1344 const OSSL_PROVIDER_INFO *p;
7dd2cb56 1345
cd3f8c1b 1346 if (!CRYPTO_THREAD_read_lock(store->lock))
8d4dec0d 1347 return 0;
7dd2cb56
MC
1348 use_fallbacks = store->use_fallbacks;
1349 CRYPTO_THREAD_unlock(store->lock);
1350 if (!use_fallbacks)
8d4dec0d 1351 return 1;
7dd2cb56 1352
cd3f8c1b 1353 if (!CRYPTO_THREAD_write_lock(store->lock))
8d4dec0d 1354 return 0;
7dd2cb56
MC
1355 /* Check again, just in case another thread changed it */
1356 use_fallbacks = store->use_fallbacks;
1357 if (!use_fallbacks) {
1358 CRYPTO_THREAD_unlock(store->lock);
8d4dec0d 1359 return 1;
7dd2cb56 1360 }
36f5ec55 1361
8d4dec0d
MC
1362 for (p = ossl_predefined_providers; p->name != NULL; p++) {
1363 OSSL_PROVIDER *prov = NULL;
36f5ec55 1364
8d4dec0d
MC
1365 if (!p->is_fallback)
1366 continue;
1367 /*
1368 * We use the internal constructor directly here,
1369 * otherwise we get a call loop
1370 */
352d482a 1371 prov = provider_new(p->name, p->init, NULL);
8d4dec0d
MC
1372 if (prov == NULL)
1373 goto err;
1374 prov->libctx = store->libctx;
8d4dec0d
MC
1375#ifndef FIPS_MODULE
1376 prov->error_lib = ERR_get_next_error_library();
1377#endif
1378
1379 /*
1380 * We are calling provider_activate while holding the store lock. This
1381 * means the init function will be called while holding a lock. Normally
1382 * we try to avoid calling a user callback while holding a lock.
1383 * However, fallbacks are never third party providers so we accept this.
1384 */
b91687c5
MC
1385 if (provider_activate(prov, 0, 0) < 0) {
1386 ossl_provider_free(prov);
1387 goto err;
1388 }
1389 prov->store = store;
1390 if (sk_OSSL_PROVIDER_push(store->providers, prov) == 0) {
7dd2cb56 1391 ossl_provider_free(prov);
8d4dec0d 1392 goto err;
36f5ec55 1393 }
8d4dec0d 1394 activated_fallback_count++;
36f5ec55 1395 }
7dd2cb56 1396
8d4dec0d 1397 if (activated_fallback_count > 0) {
7dd2cb56 1398 store->use_fallbacks = 0;
8d4dec0d
MC
1399 ret = 1;
1400 }
1401 err:
7dd2cb56 1402 CRYPTO_THREAD_unlock(store->lock);
8d4dec0d 1403 return ret;
36f5ec55
RL
1404}
1405
8f089576
P
1406int ossl_provider_doall_activated(OSSL_LIB_CTX *ctx,
1407 int (*cb)(OSSL_PROVIDER *provider,
1408 void *cbdata),
1409 void *cbdata)
85e2417c 1410{
2b4a611e 1411 int ret = 0, curr, max, ref = 0;
85e2417c 1412 struct provider_store_st *store = get_provider_store(ctx);
7bbfbc82 1413 STACK_OF(OSSL_PROVIDER) *provs = NULL;
85e2417c 1414
cb8e6413 1415#if !defined(FIPS_MODULE) && !defined(OPENSSL_NO_AUTOLOAD_CONFIG)
5c5cdcd8
MC
1416 /*
1417 * Make sure any providers are loaded from config before we try to use
1418 * them.
1419 */
d07af736
MC
1420 if (ossl_lib_ctx_is_default(ctx))
1421 OPENSSL_init_crypto(OPENSSL_INIT_LOAD_CONFIG, NULL);
5c5cdcd8
MC
1422#endif
1423
7bbfbc82
P
1424 if (store == NULL)
1425 return 1;
8d4dec0d
MC
1426 if (!provider_activate_fallbacks(store))
1427 return 0;
e55008a9 1428
7bbfbc82
P
1429 /*
1430 * Under lock, grab a copy of the provider list and up_ref each
1431 * provider so that they don't disappear underneath us.
1432 */
cd3f8c1b
RS
1433 if (!CRYPTO_THREAD_read_lock(store->lock))
1434 return 0;
7bbfbc82
P
1435 provs = sk_OSSL_PROVIDER_dup(store->providers);
1436 if (provs == NULL) {
85e2417c 1437 CRYPTO_THREAD_unlock(store->lock);
7bbfbc82 1438 return 0;
85e2417c 1439 }
2d569501
MC
1440 max = sk_OSSL_PROVIDER_num(provs);
1441 /*
1442 * We work backwards through the stack so that we can safely delete items
1443 * as we go.
1444 */
1445 for (curr = max - 1; curr >= 0; curr--) {
1446 OSSL_PROVIDER *prov = sk_OSSL_PROVIDER_value(provs, curr);
1447
fc570b26 1448 if (!CRYPTO_THREAD_read_lock(prov->flag_lock))
7bbfbc82 1449 goto err_unlock;
2d569501 1450 if (prov->flag_activated) {
2b4a611e
MC
1451 /*
1452 * We call CRYPTO_UP_REF directly rather than ossl_provider_up_ref
1453 * to avoid upping the ref count on the parent provider, which we
1454 * must not do while holding locks.
1455 */
8752694b 1456 if (CRYPTO_UP_REF(&prov->refcnt, &ref) <= 0) {
2d569501
MC
1457 CRYPTO_THREAD_unlock(prov->flag_lock);
1458 goto err_unlock;
1459 }
1460 /*
1461 * It's already activated, but we up the activated count to ensure
1462 * it remains activated until after we've called the user callback.
fc570b26
MC
1463 * In theory this could mean the parent provider goes inactive,
1464 * whilst still activated in the child for a short period. That's ok.
2d569501 1465 */
8752694b
P
1466 if (!CRYPTO_atomic_add(&prov->activatecnt, 1, &ref,
1467 prov->activatecnt_lock)) {
1468 CRYPTO_DOWN_REF(&prov->refcnt, &ref);
2d569501
MC
1469 CRYPTO_THREAD_unlock(prov->flag_lock);
1470 goto err_unlock;
1471 }
1472 } else {
1473 sk_OSSL_PROVIDER_delete(provs, curr);
1474 max--;
1475 }
1476 CRYPTO_THREAD_unlock(prov->flag_lock);
1477 }
7bbfbc82 1478 CRYPTO_THREAD_unlock(store->lock);
85e2417c 1479
7bbfbc82
P
1480 /*
1481 * Now, we sweep through all providers not under lock
1482 */
2d569501
MC
1483 for (curr = 0; curr < max; curr++) {
1484 OSSL_PROVIDER *prov = sk_OSSL_PROVIDER_value(provs, curr);
7bbfbc82 1485
b4be10df
MC
1486 if (!cb(prov, cbdata)) {
1487 curr = -1;
7bbfbc82 1488 goto finish;
b4be10df 1489 }
7bbfbc82 1490 }
2d569501 1491 curr = -1;
7bbfbc82
P
1492
1493 ret = 1;
1494 goto finish;
1495
1496 err_unlock:
1497 CRYPTO_THREAD_unlock(store->lock);
1498 finish:
2d569501
MC
1499 /*
1500 * The pop_free call doesn't do what we want on an error condition. We
1501 * either start from the first item in the stack, or part way through if
1502 * we only processed some of the items.
1503 */
1504 for (curr++; curr < max; curr++) {
1505 OSSL_PROVIDER *prov = sk_OSSL_PROVIDER_value(provs, curr);
1506
8752694b
P
1507 if (!CRYPTO_atomic_add(&prov->activatecnt, -1, &ref,
1508 prov->activatecnt_lock)) {
fc570b26
MC
1509 ret = 0;
1510 continue;
1511 }
1512 if (ref < 1) {
1513 /*
1514 * Looks like we need to deactivate properly. We could just have
1515 * done this originally, but it involves taking a write lock so
1516 * we avoid it. We up the count again and do a full deactivation
1517 */
8752694b
P
1518 if (CRYPTO_atomic_add(&prov->activatecnt, 1, &ref,
1519 prov->activatecnt_lock))
fc570b26
MC
1520 provider_deactivate(prov, 0, 1);
1521 else
1522 ret = 0;
1523 }
2b4a611e
MC
1524 /*
1525 * As above where we did the up-ref, we don't call ossl_provider_free
1526 * to avoid making upcalls. There should always be at least one ref
1527 * to the provider in the store, so this should never drop to 0.
1528 */
8752694b 1529 if (!CRYPTO_DOWN_REF(&prov->refcnt, &ref)) {
fc570b26
MC
1530 ret = 0;
1531 continue;
1532 }
2b4a611e
MC
1533 /*
1534 * Not much we can do if this assert ever fails. So we don't use
1535 * ossl_assert here.
1536 */
1537 assert(ref > 0);
2d569501 1538 }
7bbfbc82 1539 sk_OSSL_PROVIDER_free(provs);
85e2417c
RL
1540 return ret;
1541}
1542
8d4dec0d 1543int OSSL_PROVIDER_available(OSSL_LIB_CTX *libctx, const char *name)
36f5ec55 1544{
8d4dec0d
MC
1545 OSSL_PROVIDER *prov = NULL;
1546 int available = 0;
1547 struct provider_store_st *store = get_provider_store(libctx);
2d569501 1548
8d4dec0d
MC
1549 if (store == NULL || !provider_activate_fallbacks(store))
1550 return 0;
36f5ec55 1551
8d4dec0d
MC
1552 prov = ossl_provider_find(libctx, name, 0);
1553 if (prov != NULL) {
2d569501
MC
1554 if (!CRYPTO_THREAD_read_lock(prov->flag_lock))
1555 return 0;
8d4dec0d 1556 available = prov->flag_activated;
2d569501 1557 CRYPTO_THREAD_unlock(prov->flag_lock);
8d4dec0d 1558 ossl_provider_free(prov);
36f5ec55 1559 }
8d4dec0d 1560 return available;
36f5ec55
RL
1561}
1562
4c2883a9 1563/* Getters of Provider Object data */
24626a47 1564const char *ossl_provider_name(const OSSL_PROVIDER *prov)
4c2883a9
RL
1565{
1566 return prov->name;
1567}
1568
24626a47 1569const DSO *ossl_provider_dso(const OSSL_PROVIDER *prov)
4c2883a9
RL
1570{
1571 return prov->module;
1572}
1573
24626a47 1574const char *ossl_provider_module_name(const OSSL_PROVIDER *prov)
4c2883a9 1575{
f844f9eb 1576#ifdef FIPS_MODULE
3593266d
MC
1577 return NULL;
1578#else
4c2883a9 1579 return DSO_get_filename(prov->module);
3593266d 1580#endif
4c2883a9
RL
1581}
1582
24626a47 1583const char *ossl_provider_module_path(const OSSL_PROVIDER *prov)
4c2883a9 1584{
f844f9eb 1585#ifdef FIPS_MODULE
3593266d
MC
1586 return NULL;
1587#else
4c2883a9
RL
1588 /* FIXME: Ensure it's a full path */
1589 return DSO_get_filename(prov->module);
3593266d 1590#endif
4c2883a9
RL
1591}
1592
d01d3752
MC
1593void *ossl_provider_prov_ctx(const OSSL_PROVIDER *prov)
1594{
1595 if (prov != NULL)
1596 return prov->provctx;
1597
1598 return NULL;
1599}
1600
f12a5690
MC
1601const OSSL_DISPATCH *ossl_provider_get0_dispatch(const OSSL_PROVIDER *prov)
1602{
1603 if (prov != NULL)
1604 return prov->dispatch;
1605
1606 return NULL;
1607}
1608
a829b735 1609OSSL_LIB_CTX *ossl_provider_libctx(const OSSL_PROVIDER *prov)
e74bd290 1610{
185ce3d9 1611 return prov != NULL ? prov->libctx : NULL;
e74bd290
RL
1612}
1613
4c2883a9
RL
1614/* Wrappers around calls to the provider */
1615void ossl_provider_teardown(const OSSL_PROVIDER *prov)
1616{
c1fb5e07
MC
1617 if (prov->teardown != NULL
1618#ifndef FIPS_MODULE
1619 && !prov->ischild
1620#endif
1621 )
a39eb840 1622 prov->teardown(prov->provctx);
4c2883a9
RL
1623}
1624
dca97d00 1625const OSSL_PARAM *ossl_provider_gettable_params(const OSSL_PROVIDER *prov)
4c2883a9 1626{
dca97d00
RL
1627 return prov->gettable_params == NULL
1628 ? NULL : prov->gettable_params(prov->provctx);
4c2883a9
RL
1629}
1630
4e7991b4 1631int ossl_provider_get_params(const OSSL_PROVIDER *prov, OSSL_PARAM params[])
4c2883a9 1632{
a39eb840
RL
1633 return prov->get_params == NULL
1634 ? 0 : prov->get_params(prov->provctx, params);
4c2883a9
RL
1635}
1636
04cb5ec0
SL
1637int ossl_provider_self_test(const OSSL_PROVIDER *prov)
1638{
1639 int ret;
1640
1641 if (prov->self_test == NULL)
1642 return 1;
1643 ret = prov->self_test(prov->provctx);
1644 if (ret == 0)
2e4d0677 1645 (void)provider_remove_store_methods((OSSL_PROVIDER *)prov);
04cb5ec0
SL
1646 return ret;
1647}
1648
82ec09ec
MC
1649int ossl_provider_get_capabilities(const OSSL_PROVIDER *prov,
1650 const char *capability,
1651 OSSL_CALLBACK *cb,
1652 void *arg)
1653{
1654 return prov->get_capabilities == NULL
08a1c9f2 1655 ? 1 : prov->get_capabilities(prov->provctx, capability, cb, arg);
82ec09ec
MC
1656}
1657
099bd339
RL
1658const OSSL_ALGORITHM *ossl_provider_query_operation(const OSSL_PROVIDER *prov,
1659 int operation_id,
1660 int *no_cache)
1661{
7dce37e2
P
1662 const OSSL_ALGORITHM *res;
1663
1664 if (prov->query_operation == NULL)
1665 return NULL;
1666 res = prov->query_operation(prov->provctx, operation_id, no_cache);
1667#if defined(OPENSSL_NO_CACHED_FETCH)
1668 /* Forcing the non-caching of queries */
1669 if (no_cache != NULL)
1670 *no_cache = 1;
1671#endif
1672 return res;
099bd339
RL
1673}
1674
b0001d0c
P
1675void ossl_provider_unquery_operation(const OSSL_PROVIDER *prov,
1676 int operation_id,
1677 const OSSL_ALGORITHM *algs)
1678{
1679 if (prov->unquery_operation != NULL)
1680 prov->unquery_operation(prov->provctx, operation_id, algs);
1681}
1682
5a29b628
RL
1683int ossl_provider_set_operation_bit(OSSL_PROVIDER *provider, size_t bitnum)
1684{
1685 size_t byte = bitnum / 8;
1686 unsigned char bit = (1 << (bitnum % 8)) & 0xFF;
1687
cd3f8c1b
RS
1688 if (!CRYPTO_THREAD_write_lock(provider->opbits_lock))
1689 return 0;
5a29b628 1690 if (provider->operation_bits_sz <= byte) {
2b748d72
TS
1691 unsigned char *tmp = OPENSSL_realloc(provider->operation_bits,
1692 byte + 1);
1693
1694 if (tmp == NULL) {
c25a1524 1695 CRYPTO_THREAD_unlock(provider->opbits_lock);
5a29b628
RL
1696 return 0;
1697 }
2b748d72 1698 provider->operation_bits = tmp;
5a29b628
RL
1699 memset(provider->operation_bits + provider->operation_bits_sz,
1700 '\0', byte + 1 - provider->operation_bits_sz);
2b748d72 1701 provider->operation_bits_sz = byte + 1;
5a29b628
RL
1702 }
1703 provider->operation_bits[byte] |= bit;
c25a1524 1704 CRYPTO_THREAD_unlock(provider->opbits_lock);
5a29b628
RL
1705 return 1;
1706}
1707
1708int ossl_provider_test_operation_bit(OSSL_PROVIDER *provider, size_t bitnum,
1709 int *result)
1710{
1711 size_t byte = bitnum / 8;
1712 unsigned char bit = (1 << (bitnum % 8)) & 0xFF;
1713
1714 if (!ossl_assert(result != NULL)) {
1715 ERR_raise(ERR_LIB_CRYPTO, ERR_R_PASSED_NULL_PARAMETER);
1716 return 0;
1717 }
1718
1719 *result = 0;
cd3f8c1b
RS
1720 if (!CRYPTO_THREAD_read_lock(provider->opbits_lock))
1721 return 0;
5a29b628
RL
1722 if (provider->operation_bits_sz > byte)
1723 *result = ((provider->operation_bits[byte] & bit) != 0);
c25a1524 1724 CRYPTO_THREAD_unlock(provider->opbits_lock);
5a29b628
RL
1725 return 1;
1726}
1727
c1fb5e07 1728#ifndef FIPS_MODULE
8c627075
MC
1729const OSSL_CORE_HANDLE *ossl_provider_get_parent(OSSL_PROVIDER *prov)
1730{
1731 return prov->handle;
1732}
1733
abaa2dd2 1734int ossl_provider_is_child(const OSSL_PROVIDER *prov)
f12a5690 1735{
abaa2dd2
MC
1736 return prov->ischild;
1737}
8c627075 1738
abaa2dd2
MC
1739int ossl_provider_set_child(OSSL_PROVIDER *prov, const OSSL_CORE_HANDLE *handle)
1740{
8c627075 1741 prov->handle = handle;
f12a5690 1742 prov->ischild = 1;
8c627075 1743
abaa2dd2
MC
1744 return 1;
1745}
1746
447588b6
MC
1747int ossl_provider_default_props_update(OSSL_LIB_CTX *libctx, const char *props)
1748{
1749#ifndef FIPS_MODULE
1750 struct provider_store_st *store = NULL;
1751 int i, max;
1752 OSSL_PROVIDER_CHILD_CB *child_cb;
1753
1754 if ((store = get_provider_store(libctx)) == NULL)
1755 return 0;
1756
1757 if (!CRYPTO_THREAD_read_lock(store->lock))
1758 return 0;
1759
1760 max = sk_OSSL_PROVIDER_CHILD_CB_num(store->child_cbs);
1761 for (i = 0; i < max; i++) {
1762 child_cb = sk_OSSL_PROVIDER_CHILD_CB_value(store->child_cbs, i);
1763 child_cb->global_props_cb(props, child_cb->cbdata);
1764 }
1765
1766 CRYPTO_THREAD_unlock(store->lock);
1767#endif
1768 return 1;
1769}
1770
7b88c184
MC
1771static int ossl_provider_register_child_cb(const OSSL_CORE_HANDLE *handle,
1772 int (*create_cb)(
1773 const OSSL_CORE_HANDLE *provider,
1774 void *cbdata),
b1956770 1775 int (*remove_cb)(
7b88c184
MC
1776 const OSSL_CORE_HANDLE *provider,
1777 void *cbdata),
b1956770 1778 int (*global_props_cb)(
447588b6
MC
1779 const char *props,
1780 void *cbdata),
7b88c184
MC
1781 void *cbdata)
1782{
1783 /*
1784 * This is really an OSSL_PROVIDER that we created and cast to
1785 * OSSL_CORE_HANDLE originally. Therefore it is safe to cast it back.
1786 */
1787 OSSL_PROVIDER *thisprov = (OSSL_PROVIDER *)handle;
1788 OSSL_PROVIDER *prov;
1789 OSSL_LIB_CTX *libctx = thisprov->libctx;
1790 struct provider_store_st *store = NULL;
1791 int ret = 0, i, max;
1792 OSSL_PROVIDER_CHILD_CB *child_cb;
447588b6 1793 char *propsstr = NULL;
7b88c184
MC
1794
1795 if ((store = get_provider_store(libctx)) == NULL)
1796 return 0;
1797
1798 child_cb = OPENSSL_malloc(sizeof(*child_cb));
1799 if (child_cb == NULL)
1800 return 0;
1801 child_cb->prov = thisprov;
1802 child_cb->create_cb = create_cb;
1803 child_cb->remove_cb = remove_cb;
447588b6 1804 child_cb->global_props_cb = global_props_cb;
7b88c184
MC
1805 child_cb->cbdata = cbdata;
1806
1807 if (!CRYPTO_THREAD_write_lock(store->lock)) {
1808 OPENSSL_free(child_cb);
1809 return 0;
1810 }
447588b6
MC
1811 propsstr = evp_get_global_properties_str(libctx, 0);
1812
1813 if (propsstr != NULL) {
1814 global_props_cb(propsstr, cbdata);
1815 OPENSSL_free(propsstr);
1816 }
7b88c184
MC
1817 max = sk_OSSL_PROVIDER_num(store->providers);
1818 for (i = 0; i < max; i++) {
2b4a611e
MC
1819 int activated;
1820
7b88c184 1821 prov = sk_OSSL_PROVIDER_value(store->providers, i);
549b5cb4 1822
7b88c184
MC
1823 if (!CRYPTO_THREAD_read_lock(prov->flag_lock))
1824 break;
2b4a611e
MC
1825 activated = prov->flag_activated;
1826 CRYPTO_THREAD_unlock(prov->flag_lock);
7b88c184 1827 /*
2b4a611e
MC
1828 * We hold the store lock while calling the user callback. This means
1829 * that the user callback must be short and simple and not do anything
1830 * likely to cause a deadlock. We don't hold the flag_lock during this
1831 * call. In theory this means that another thread could deactivate it
1832 * while we are calling create. This is ok because the other thread
1833 * will also call remove_cb, but won't be able to do so until we release
1834 * the store lock.
7b88c184 1835 */
2b4a611e 1836 if (activated && !create_cb((OSSL_CORE_HANDLE *)prov, cbdata))
7b88c184 1837 break;
7b88c184
MC
1838 }
1839 if (i == max) {
1840 /* Success */
1841 ret = sk_OSSL_PROVIDER_CHILD_CB_push(store->child_cbs, child_cb);
1842 }
1843 if (i != max || ret <= 0) {
1844 /* Failed during creation. Remove everything we just added */
1845 for (; i >= 0; i--) {
1846 prov = sk_OSSL_PROVIDER_value(store->providers, i);
1847 remove_cb((OSSL_CORE_HANDLE *)prov, cbdata);
1848 }
1849 OPENSSL_free(child_cb);
1850 ret = 0;
1851 }
1852 CRYPTO_THREAD_unlock(store->lock);
1853
1854 return ret;
1855}
1856
1857static void ossl_provider_deregister_child_cb(const OSSL_CORE_HANDLE *handle)
1858{
1859 /*
1860 * This is really an OSSL_PROVIDER that we created and cast to
1861 * OSSL_CORE_HANDLE originally. Therefore it is safe to cast it back.
1862 */
1863 OSSL_PROVIDER *thisprov = (OSSL_PROVIDER *)handle;
1864 OSSL_LIB_CTX *libctx = thisprov->libctx;
1865 struct provider_store_st *store = NULL;
1866 int i, max;
1867 OSSL_PROVIDER_CHILD_CB *child_cb;
1868
1869 if ((store = get_provider_store(libctx)) == NULL)
1870 return;
1871
1872 if (!CRYPTO_THREAD_write_lock(store->lock))
1873 return;
1874 max = sk_OSSL_PROVIDER_CHILD_CB_num(store->child_cbs);
1875 for (i = 0; i < max; i++) {
1876 child_cb = sk_OSSL_PROVIDER_CHILD_CB_value(store->child_cbs, i);
1877 if (child_cb->prov == thisprov) {
1878 /* Found an entry */
1879 sk_OSSL_PROVIDER_CHILD_CB_delete(store->child_cbs, i);
1880 OPENSSL_free(child_cb);
1881 break;
1882 }
1883 }
1884 CRYPTO_THREAD_unlock(store->lock);
1885}
1886#endif
1887
4c2883a9
RL
1888/*-
1889 * Core functions for the provider
1890 * ===============================
1891 *
1892 * This is the set of functions that the core makes available to the provider
1893 */
1894
1895/*
1896 * This returns a list of Provider Object parameters with their types, for
1897 * discovery. We do not expect that many providers will use this, but one
1898 * never knows.
1899 */
26175013 1900static const OSSL_PARAM param_types[] = {
b8086652
SL
1901 OSSL_PARAM_DEFN(OSSL_PROV_PARAM_CORE_VERSION, OSSL_PARAM_UTF8_PTR, NULL, 0),
1902 OSSL_PARAM_DEFN(OSSL_PROV_PARAM_CORE_PROV_NAME, OSSL_PARAM_UTF8_PTR,
1903 NULL, 0),
1904#ifndef FIPS_MODULE
1905 OSSL_PARAM_DEFN(OSSL_PROV_PARAM_CORE_MODULE_FILENAME, OSSL_PARAM_UTF8_PTR,
1906 NULL, 0),
1907#endif
26175013 1908 OSSL_PARAM_END
4c2883a9
RL
1909};
1910
49c64346
RL
1911/*
1912 * Forward declare all the functions that are provided aa dispatch.
1913 * This ensures that the compiler will complain if they aren't defined
1914 * with the correct signature.
1915 */
363b1e5d
DMSP
1916static OSSL_FUNC_core_gettable_params_fn core_gettable_params;
1917static OSSL_FUNC_core_get_params_fn core_get_params;
a829b735 1918static OSSL_FUNC_core_get_libctx_fn core_get_libctx;
9574842e 1919static OSSL_FUNC_core_thread_start_fn core_thread_start;
f844f9eb 1920#ifndef FIPS_MODULE
363b1e5d
DMSP
1921static OSSL_FUNC_core_new_error_fn core_new_error;
1922static OSSL_FUNC_core_set_error_debug_fn core_set_error_debug;
1923static OSSL_FUNC_core_vset_error_fn core_vset_error;
1924static OSSL_FUNC_core_set_error_mark_fn core_set_error_mark;
1925static OSSL_FUNC_core_clear_last_error_mark_fn core_clear_last_error_mark;
1926static OSSL_FUNC_core_pop_error_to_mark_fn core_pop_error_to_mark;
9574842e
RL
1927OSSL_FUNC_BIO_new_file_fn ossl_core_bio_new_file;
1928OSSL_FUNC_BIO_new_membuf_fn ossl_core_bio_new_mem_buf;
1929OSSL_FUNC_BIO_read_ex_fn ossl_core_bio_read_ex;
1930OSSL_FUNC_BIO_write_ex_fn ossl_core_bio_write_ex;
1931OSSL_FUNC_BIO_gets_fn ossl_core_bio_gets;
1932OSSL_FUNC_BIO_puts_fn ossl_core_bio_puts;
1933OSSL_FUNC_BIO_up_ref_fn ossl_core_bio_up_ref;
1934OSSL_FUNC_BIO_free_fn ossl_core_bio_free;
1935OSSL_FUNC_BIO_vprintf_fn ossl_core_bio_vprintf;
1936OSSL_FUNC_BIO_vsnprintf_fn BIO_vsnprintf;
1937static OSSL_FUNC_self_test_cb_fn core_self_test_get_callback;
4cde7585 1938static OSSL_FUNC_get_entropy_fn rand_get_entropy;
5516d202 1939static OSSL_FUNC_get_user_entropy_fn rand_get_user_entropy;
4cde7585 1940static OSSL_FUNC_cleanup_entropy_fn rand_cleanup_entropy;
5516d202 1941static OSSL_FUNC_cleanup_user_entropy_fn rand_cleanup_user_entropy;
4cde7585 1942static OSSL_FUNC_get_nonce_fn rand_get_nonce;
5516d202 1943static OSSL_FUNC_get_user_nonce_fn rand_get_user_nonce;
4cde7585 1944static OSSL_FUNC_cleanup_nonce_fn rand_cleanup_nonce;
5516d202 1945static OSSL_FUNC_cleanup_user_nonce_fn rand_cleanup_user_nonce;
9574842e
RL
1946#endif
1947OSSL_FUNC_CRYPTO_malloc_fn CRYPTO_malloc;
1948OSSL_FUNC_CRYPTO_zalloc_fn CRYPTO_zalloc;
1949OSSL_FUNC_CRYPTO_free_fn CRYPTO_free;
1950OSSL_FUNC_CRYPTO_clear_free_fn CRYPTO_clear_free;
1951OSSL_FUNC_CRYPTO_realloc_fn CRYPTO_realloc;
1952OSSL_FUNC_CRYPTO_clear_realloc_fn CRYPTO_clear_realloc;
1953OSSL_FUNC_CRYPTO_secure_malloc_fn CRYPTO_secure_malloc;
1954OSSL_FUNC_CRYPTO_secure_zalloc_fn CRYPTO_secure_zalloc;
1955OSSL_FUNC_CRYPTO_secure_free_fn CRYPTO_secure_free;
1956OSSL_FUNC_CRYPTO_secure_clear_free_fn CRYPTO_secure_clear_free;
1957OSSL_FUNC_CRYPTO_secure_allocated_fn CRYPTO_secure_allocated;
1958OSSL_FUNC_OPENSSL_cleanse_fn OPENSSL_cleanse;
1959#ifndef FIPS_MODULE
1960OSSL_FUNC_provider_register_child_cb_fn ossl_provider_register_child_cb;
1961OSSL_FUNC_provider_deregister_child_cb_fn ossl_provider_deregister_child_cb;
1962static OSSL_FUNC_provider_name_fn core_provider_get0_name;
1963static OSSL_FUNC_provider_get0_provider_ctx_fn core_provider_get0_provider_ctx;
1964static OSSL_FUNC_provider_get0_dispatch_fn core_provider_get0_dispatch;
1965static OSSL_FUNC_provider_up_ref_fn core_provider_up_ref_intern;
1966static OSSL_FUNC_provider_free_fn core_provider_free_intern;
97abae6a
MC
1967static OSSL_FUNC_core_obj_add_sigid_fn core_obj_add_sigid;
1968static OSSL_FUNC_core_obj_create_fn core_obj_create;
49c64346
RL
1969#endif
1970
d40b42ab 1971static const OSSL_PARAM *core_gettable_params(const OSSL_CORE_HANDLE *handle)
4c2883a9
RL
1972{
1973 return param_types;
1974}
1975
d40b42ab 1976static int core_get_params(const OSSL_CORE_HANDLE *handle, OSSL_PARAM params[])
4c2883a9
RL
1977{
1978 int i;
4e7991b4 1979 OSSL_PARAM *p;
d40b42ab
MC
1980 /*
1981 * We created this object originally and we know it is actually an
1982 * OSSL_PROVIDER *, so the cast is safe
1983 */
1984 OSSL_PROVIDER *prov = (OSSL_PROVIDER *)handle;
4c2883a9 1985
b8086652 1986 if ((p = OSSL_PARAM_locate(params, OSSL_PROV_PARAM_CORE_VERSION)) != NULL)
ac1055ef 1987 OSSL_PARAM_set_utf8_ptr(p, OPENSSL_VERSION_STR);
b8086652 1988 if ((p = OSSL_PARAM_locate(params, OSSL_PROV_PARAM_CORE_PROV_NAME)) != NULL)
ac1055ef
RL
1989 OSSL_PARAM_set_utf8_ptr(p, prov->name);
1990
f844f9eb 1991#ifndef FIPS_MODULE
b8086652
SL
1992 if ((p = OSSL_PARAM_locate(params,
1993 OSSL_PROV_PARAM_CORE_MODULE_FILENAME)) != NULL)
25e60144
SL
1994 OSSL_PARAM_set_utf8_ptr(p, ossl_provider_module_path(prov));
1995#endif
1996
ac1055ef
RL
1997 if (prov->parameters == NULL)
1998 return 1;
1999
2000 for (i = 0; i < sk_INFOPAIR_num(prov->parameters); i++) {
2001 INFOPAIR *pair = sk_INFOPAIR_value(prov->parameters, i);
2002
2003 if ((p = OSSL_PARAM_locate(params, pair->name)) != NULL)
2004 OSSL_PARAM_set_utf8_ptr(p, pair->value);
4c2883a9 2005 }
4c2883a9
RL
2006 return 1;
2007}
2008
d40b42ab 2009static OPENSSL_CORE_CTX *core_get_libctx(const OSSL_CORE_HANDLE *handle)
e7706e63 2010{
d40b42ab
MC
2011 /*
2012 * We created this object originally and we know it is actually an
2013 * OSSL_PROVIDER *, so the cast is safe
2014 */
2015 OSSL_PROVIDER *prov = (OSSL_PROVIDER *)handle;
2016
a23deef2
TM
2017 /*
2018 * Using ossl_provider_libctx would be wrong as that returns
2019 * NULL for |prov| == NULL and NULL libctx has a special meaning
2020 * that does not apply here. Here |prov| == NULL can happen only in
2021 * case of a coding error.
2022 */
2217d4c9 2023 assert(prov != NULL);
a23deef2 2024 return (OPENSSL_CORE_CTX *)prov->libctx;
e7706e63
RL
2025}
2026
d40b42ab 2027static int core_thread_start(const OSSL_CORE_HANDLE *handle,
c9732f09
MC
2028 OSSL_thread_stop_handler_fn handfn,
2029 void *arg)
da747958 2030{
d40b42ab
MC
2031 /*
2032 * We created this object originally and we know it is actually an
2033 * OSSL_PROVIDER *, so the cast is safe
2034 */
2035 OSSL_PROVIDER *prov = (OSSL_PROVIDER *)handle;
2036
c9732f09 2037 return ossl_init_thread_start(prov, arg, handfn);
da747958
MC
2038}
2039
6592ab81
RL
2040/*
2041 * The FIPS module inner provider doesn't implement these. They aren't
2042 * needed there, since the FIPS module upcalls are always the outer provider
2043 * ones.
2044 */
f844f9eb 2045#ifndef FIPS_MODULE
49c64346 2046/*
a23deef2
TM
2047 * These error functions should use |handle| to select the proper
2048 * library context to report in the correct error stack if error
49c64346
RL
2049 * stacks become tied to the library context.
2050 * We cannot currently do that since there's no support for it in the
2051 * ERR subsystem.
2052 */
d40b42ab 2053static void core_new_error(const OSSL_CORE_HANDLE *handle)
49c64346
RL
2054{
2055 ERR_new();
2056}
2057
d40b42ab 2058static void core_set_error_debug(const OSSL_CORE_HANDLE *handle,
49c64346
RL
2059 const char *file, int line, const char *func)
2060{
2061 ERR_set_debug(file, line, func);
2062}
2063
d40b42ab 2064static void core_vset_error(const OSSL_CORE_HANDLE *handle,
49c64346 2065 uint32_t reason, const char *fmt, va_list args)
6ebc2f56 2066{
d40b42ab
MC
2067 /*
2068 * We created this object originally and we know it is actually an
2069 * OSSL_PROVIDER *, so the cast is safe
2070 */
2071 OSSL_PROVIDER *prov = (OSSL_PROVIDER *)handle;
2072
6ebc2f56
RL
2073 /*
2074 * If the uppermost 8 bits are non-zero, it's an OpenSSL library
2075 * error and will be treated as such. Otherwise, it's a new style
2076 * provider error and will be treated as such.
2077 */
2078 if (ERR_GET_LIB(reason) != 0) {
49c64346 2079 ERR_vset_error(ERR_GET_LIB(reason), ERR_GET_REASON(reason), fmt, args);
6ebc2f56 2080 } else {
49c64346 2081 ERR_vset_error(prov->error_lib, (int)reason, fmt, args);
6ebc2f56
RL
2082 }
2083}
7b131de2 2084
d40b42ab 2085static int core_set_error_mark(const OSSL_CORE_HANDLE *handle)
7b131de2
RL
2086{
2087 return ERR_set_mark();
2088}
2089
d40b42ab 2090static int core_clear_last_error_mark(const OSSL_CORE_HANDLE *handle)
7b131de2
RL
2091{
2092 return ERR_clear_last_mark();
2093}
2094
d40b42ab 2095static int core_pop_error_to_mark(const OSSL_CORE_HANDLE *handle)
7b131de2
RL
2096{
2097 return ERR_pop_to_mark();
2098}
97abae6a 2099
9574842e
RL
2100static void core_self_test_get_callback(OPENSSL_CORE_CTX *libctx,
2101 OSSL_CALLBACK **cb, void **cbarg)
2102{
2103 OSSL_SELF_TEST_get_callback((OSSL_LIB_CTX *)libctx, cb, cbarg);
2104}
2105
4cde7585
P
2106static size_t rand_get_entropy(const OSSL_CORE_HANDLE *handle,
2107 unsigned char **pout, int entropy,
2108 size_t min_len, size_t max_len)
2109{
2110 return ossl_rand_get_entropy((OSSL_LIB_CTX *)core_get_libctx(handle),
2111 pout, entropy, min_len, max_len);
2112}
2113
2114static size_t rand_get_user_entropy(const OSSL_CORE_HANDLE *handle,
2115 unsigned char **pout, int entropy,
2116 size_t min_len, size_t max_len)
2117{
2118 return ossl_rand_get_user_entropy((OSSL_LIB_CTX *)core_get_libctx(handle),
2119 pout, entropy, min_len, max_len);
2120}
2121
2122static void rand_cleanup_entropy(const OSSL_CORE_HANDLE *handle,
2123 unsigned char *buf, size_t len)
2124{
2125 ossl_rand_cleanup_entropy((OSSL_LIB_CTX *)core_get_libctx(handle),
2126 buf, len);
2127}
2128
5516d202
MSP
2129static void rand_cleanup_user_entropy(const OSSL_CORE_HANDLE *handle,
2130 unsigned char *buf, size_t len)
2131{
2132 ossl_rand_cleanup_user_entropy((OSSL_LIB_CTX *)core_get_libctx(handle),
2133 buf, len);
2134}
2135
4cde7585
P
2136static size_t rand_get_nonce(const OSSL_CORE_HANDLE *handle,
2137 unsigned char **pout,
2138 size_t min_len, size_t max_len,
2139 const void *salt, size_t salt_len)
2140{
2141 return ossl_rand_get_nonce((OSSL_LIB_CTX *)core_get_libctx(handle),
2142 pout, min_len, max_len, salt, salt_len);
2143}
2144
2145static size_t rand_get_user_nonce(const OSSL_CORE_HANDLE *handle,
2146 unsigned char **pout,
2147 size_t min_len, size_t max_len,
2148 const void *salt, size_t salt_len)
2149{
2150 return ossl_rand_get_user_nonce((OSSL_LIB_CTX *)core_get_libctx(handle),
2151 pout, min_len, max_len, salt, salt_len);
2152}
2153
2154static void rand_cleanup_nonce(const OSSL_CORE_HANDLE *handle,
2155 unsigned char *buf, size_t len)
2156{
2157 ossl_rand_cleanup_nonce((OSSL_LIB_CTX *)core_get_libctx(handle),
2158 buf, len);
2159}
2160
5516d202
MSP
2161static void rand_cleanup_user_nonce(const OSSL_CORE_HANDLE *handle,
2162 unsigned char *buf, size_t len)
2163{
2164 ossl_rand_cleanup_user_nonce((OSSL_LIB_CTX *)core_get_libctx(handle),
2165 buf, len);
2166}
2167
9574842e
RL
2168static const char *core_provider_get0_name(const OSSL_CORE_HANDLE *prov)
2169{
2170 return OSSL_PROVIDER_get0_name((const OSSL_PROVIDER *)prov);
2171}
2172
2173static void *core_provider_get0_provider_ctx(const OSSL_CORE_HANDLE *prov)
2174{
2175 return OSSL_PROVIDER_get0_provider_ctx((const OSSL_PROVIDER *)prov);
2176}
2177
2178static const OSSL_DISPATCH *
2179core_provider_get0_dispatch(const OSSL_CORE_HANDLE *prov)
2180{
2181 return OSSL_PROVIDER_get0_dispatch((const OSSL_PROVIDER *)prov);
2182}
2183
2184static int core_provider_up_ref_intern(const OSSL_CORE_HANDLE *prov,
2185 int activate)
2186{
2187 return provider_up_ref_intern((OSSL_PROVIDER *)prov, activate);
2188}
2189
2190static int core_provider_free_intern(const OSSL_CORE_HANDLE *prov,
2191 int deactivate)
2192{
2193 return provider_free_intern((OSSL_PROVIDER *)prov, deactivate);
2194}
2195
97abae6a
MC
2196static int core_obj_add_sigid(const OSSL_CORE_HANDLE *prov,
2197 const char *sign_name, const char *digest_name,
2198 const char *pkey_name)
2199{
2200 int sign_nid = OBJ_txt2nid(sign_name);
4f716249 2201 int digest_nid = NID_undef;
97abae6a
MC
2202 int pkey_nid = OBJ_txt2nid(pkey_name);
2203
4f716249
MB
2204 if (digest_name != NULL && digest_name[0] != '\0'
2205 && (digest_nid = OBJ_txt2nid(digest_name)) == NID_undef)
2206 return 0;
2207
97abae6a
MC
2208 if (sign_nid == NID_undef)
2209 return 0;
2210
2211 /*
2212 * Check if it already exists. This is a success if so (even if we don't
2213 * have nids for the digest/pkey)
2214 */
2215 if (OBJ_find_sigid_algs(sign_nid, NULL, NULL))
2216 return 1;
2217
4f716249 2218 if (pkey_nid == NID_undef)
97abae6a
MC
2219 return 0;
2220
2221 return OBJ_add_sigid(sign_nid, digest_nid, pkey_nid);
2222}
2223
2224static int core_obj_create(const OSSL_CORE_HANDLE *prov, const char *oid,
2225 const char *sn, const char *ln)
2226{
2227 /* Check if it already exists and create it if not */
2228 return OBJ_txt2nid(oid) != NID_undef
2229 || OBJ_create(oid, sn, ln) != NID_undef;
2230}
f844f9eb 2231#endif /* FIPS_MODULE */
6ebc2f56 2232
b60cba3c 2233/*
03bede0c 2234 * Functions provided by the core.
b60cba3c 2235 */
4c2883a9 2236static const OSSL_DISPATCH core_dispatch_[] = {
dca97d00 2237 { OSSL_FUNC_CORE_GETTABLE_PARAMS, (void (*)(void))core_gettable_params },
4c2883a9 2238 { OSSL_FUNC_CORE_GET_PARAMS, (void (*)(void))core_get_params },
a829b735 2239 { OSSL_FUNC_CORE_GET_LIBCTX, (void (*)(void))core_get_libctx },
da747958 2240 { OSSL_FUNC_CORE_THREAD_START, (void (*)(void))core_thread_start },
f844f9eb 2241#ifndef FIPS_MODULE
49c64346
RL
2242 { OSSL_FUNC_CORE_NEW_ERROR, (void (*)(void))core_new_error },
2243 { OSSL_FUNC_CORE_SET_ERROR_DEBUG, (void (*)(void))core_set_error_debug },
2244 { OSSL_FUNC_CORE_VSET_ERROR, (void (*)(void))core_vset_error },
7b131de2
RL
2245 { OSSL_FUNC_CORE_SET_ERROR_MARK, (void (*)(void))core_set_error_mark },
2246 { OSSL_FUNC_CORE_CLEAR_LAST_ERROR_MARK,
2247 (void (*)(void))core_clear_last_error_mark },
d16d0b71 2248 { OSSL_FUNC_CORE_POP_ERROR_TO_MARK, (void (*)(void))core_pop_error_to_mark },
141cc94e
P
2249 { OSSL_FUNC_BIO_NEW_FILE, (void (*)(void))ossl_core_bio_new_file },
2250 { OSSL_FUNC_BIO_NEW_MEMBUF, (void (*)(void))ossl_core_bio_new_mem_buf },
2251 { OSSL_FUNC_BIO_READ_EX, (void (*)(void))ossl_core_bio_read_ex },
2252 { OSSL_FUNC_BIO_WRITE_EX, (void (*)(void))ossl_core_bio_write_ex },
2253 { OSSL_FUNC_BIO_GETS, (void (*)(void))ossl_core_bio_gets },
2254 { OSSL_FUNC_BIO_PUTS, (void (*)(void))ossl_core_bio_puts },
2255 { OSSL_FUNC_BIO_CTRL, (void (*)(void))ossl_core_bio_ctrl },
2256 { OSSL_FUNC_BIO_UP_REF, (void (*)(void))ossl_core_bio_up_ref },
2257 { OSSL_FUNC_BIO_FREE, (void (*)(void))ossl_core_bio_free },
2258 { OSSL_FUNC_BIO_VPRINTF, (void (*)(void))ossl_core_bio_vprintf },
d16d0b71 2259 { OSSL_FUNC_BIO_VSNPRINTF, (void (*)(void))BIO_vsnprintf },
9574842e 2260 { OSSL_FUNC_SELF_TEST_CB, (void (*)(void))core_self_test_get_callback },
4cde7585 2261 { OSSL_FUNC_GET_ENTROPY, (void (*)(void))rand_get_entropy },
5516d202 2262 { OSSL_FUNC_GET_USER_ENTROPY, (void (*)(void))rand_get_user_entropy },
4cde7585 2263 { OSSL_FUNC_CLEANUP_ENTROPY, (void (*)(void))rand_cleanup_entropy },
5516d202 2264 { OSSL_FUNC_CLEANUP_USER_ENTROPY, (void (*)(void))rand_cleanup_user_entropy },
4cde7585 2265 { OSSL_FUNC_GET_NONCE, (void (*)(void))rand_get_nonce },
4cde7585 2266 { OSSL_FUNC_GET_USER_NONCE, (void (*)(void))rand_get_user_nonce },
5516d202
MSP
2267 { OSSL_FUNC_CLEANUP_NONCE, (void (*)(void))rand_cleanup_nonce },
2268 { OSSL_FUNC_CLEANUP_USER_NONCE, (void (*)(void))rand_cleanup_user_nonce },
6592ab81 2269#endif
b60cba3c
RS
2270 { OSSL_FUNC_CRYPTO_MALLOC, (void (*)(void))CRYPTO_malloc },
2271 { OSSL_FUNC_CRYPTO_ZALLOC, (void (*)(void))CRYPTO_zalloc },
b60cba3c
RS
2272 { OSSL_FUNC_CRYPTO_FREE, (void (*)(void))CRYPTO_free },
2273 { OSSL_FUNC_CRYPTO_CLEAR_FREE, (void (*)(void))CRYPTO_clear_free },
2274 { OSSL_FUNC_CRYPTO_REALLOC, (void (*)(void))CRYPTO_realloc },
2275 { OSSL_FUNC_CRYPTO_CLEAR_REALLOC, (void (*)(void))CRYPTO_clear_realloc },
2276 { OSSL_FUNC_CRYPTO_SECURE_MALLOC, (void (*)(void))CRYPTO_secure_malloc },
2277 { OSSL_FUNC_CRYPTO_SECURE_ZALLOC, (void (*)(void))CRYPTO_secure_zalloc },
2278 { OSSL_FUNC_CRYPTO_SECURE_FREE, (void (*)(void))CRYPTO_secure_free },
2279 { OSSL_FUNC_CRYPTO_SECURE_CLEAR_FREE,
2280 (void (*)(void))CRYPTO_secure_clear_free },
2281 { OSSL_FUNC_CRYPTO_SECURE_ALLOCATED,
2282 (void (*)(void))CRYPTO_secure_allocated },
2283 { OSSL_FUNC_OPENSSL_CLEANSE, (void (*)(void))OPENSSL_cleanse },
f12a5690 2284#ifndef FIPS_MODULE
7b88c184
MC
2285 { OSSL_FUNC_PROVIDER_REGISTER_CHILD_CB,
2286 (void (*)(void))ossl_provider_register_child_cb },
2287 { OSSL_FUNC_PROVIDER_DEREGISTER_CHILD_CB,
2288 (void (*)(void))ossl_provider_deregister_child_cb },
2289 { OSSL_FUNC_PROVIDER_NAME,
9574842e 2290 (void (*)(void))core_provider_get0_name },
7b88c184 2291 { OSSL_FUNC_PROVIDER_GET0_PROVIDER_CTX,
9574842e 2292 (void (*)(void))core_provider_get0_provider_ctx },
7b88c184 2293 { OSSL_FUNC_PROVIDER_GET0_DISPATCH,
9574842e 2294 (void (*)(void))core_provider_get0_dispatch },
8c627075 2295 { OSSL_FUNC_PROVIDER_UP_REF,
9574842e 2296 (void (*)(void))core_provider_up_ref_intern },
8c627075 2297 { OSSL_FUNC_PROVIDER_FREE,
9574842e 2298 (void (*)(void))core_provider_free_intern },
97abae6a
MC
2299 { OSSL_FUNC_CORE_OBJ_ADD_SIGID, (void (*)(void))core_obj_add_sigid },
2300 { OSSL_FUNC_CORE_OBJ_CREATE, (void (*)(void))core_obj_create },
f12a5690 2301#endif
1e6bd31e 2302 OSSL_DISPATCH_END
4c2883a9
RL
2303};
2304static const OSSL_DISPATCH *core_dispatch = core_dispatch_;