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