]> git.ipfire.org Git - thirdparty/openssl.git/blame - crypto/provider_core.c
Use BIO_f_readbuffer() in the decoder to support stdin.
[thirdparty/openssl.git] / crypto / provider_core.c
CommitLineData
4c2883a9 1/*
4333b89f 2 * Copyright 2019-2021 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
10#include <openssl/core.h>
23c48d94 11#include <openssl/core_dispatch.h>
25e60144 12#include <openssl/core_names.h>
6bd4e3f2 13#include <openssl/provider.h>
ac1055ef 14#include <openssl/params.h>
4c2883a9 15#include <openssl/opensslv.h>
25f2138b 16#include "crypto/cryptlib.h"
04cb5ec0 17#include "crypto/evp.h" /* evp_method_store_flush */
03bede0c 18#include "crypto/rand.h"
c41f3ae0 19#include "internal/nelem.h"
4c2883a9
RL
20#include "internal/thread_once.h"
21#include "internal/provider.h"
22#include "internal/refcount.h"
c41f3ae0 23#include "provider_local.h"
f844f9eb 24#ifndef FIPS_MODULE
36fc5fc6
SL
25# include <openssl/self_test.h>
26#endif
c41f3ae0
RL
27
28static OSSL_PROVIDER *provider_new(const char *name,
29 OSSL_provider_init_fn *init_function);
4c2883a9
RL
30
31/*-
32 * Provider Object structure
33 * =========================
34 */
35
ac1055ef
RL
36typedef struct {
37 char *name;
38 char *value;
39} INFOPAIR;
40DEFINE_STACK_OF(INFOPAIR)
41
4c2883a9
RL
42struct provider_store_st; /* Forward declaration */
43
44struct ossl_provider_st {
45 /* Flag bits */
46 unsigned int flag_initialized:1;
390f9bad 47 unsigned int flag_activated:1;
c8567c39
RL
48 unsigned int flag_fallback:1; /* Can be used as fallback */
49 unsigned int flag_activated_as_fallback:1;
4c2883a9 50
c2ec2bb7
RL
51 /* Getting and setting the flags require synchronization */
52 CRYPTO_RWLOCK *flag_lock;
53
4c2883a9
RL
54 /* OpenSSL library side data */
55 CRYPTO_REF_COUNT refcnt;
085bef9f 56 CRYPTO_RWLOCK *refcnt_lock; /* For the ref counter */
390f9bad
RL
57 CRYPTO_REF_COUNT activatecnt;
58 CRYPTO_RWLOCK *activatecnt_lock; /* For the activate counter */
4c2883a9 59 char *name;
ac1055ef 60 char *path;
4c2883a9
RL
61 DSO *module;
62 OSSL_provider_init_fn *init_function;
ac1055ef 63 STACK_OF(INFOPAIR) *parameters;
b4250010 64 OSSL_LIB_CTX *libctx; /* The library context this instance is in */
e55008a9 65 struct provider_store_st *store; /* The store this instance belongs to */
f844f9eb 66#ifndef FIPS_MODULE
6592ab81
RL
67 /*
68 * In the FIPS module inner provider, this isn't needed, since the
69 * error upcalls are always direct calls to the outer provider.
70 */
6ebc2f56 71 int error_lib; /* ERR library number, one for each provider */
6592ab81 72# ifndef OPENSSL_NO_ERR
6ebc2f56 73 ERR_STRING_DATA *error_strings; /* Copy of what the provider gives us */
6592ab81 74# endif
6ebc2f56 75#endif
4c2883a9
RL
76
77 /* Provider side functions */
363b1e5d
DMSP
78 OSSL_FUNC_provider_teardown_fn *teardown;
79 OSSL_FUNC_provider_gettable_params_fn *gettable_params;
80 OSSL_FUNC_provider_get_params_fn *get_params;
81 OSSL_FUNC_provider_get_capabilities_fn *get_capabilities;
04cb5ec0 82 OSSL_FUNC_provider_self_test_fn *self_test;
363b1e5d 83 OSSL_FUNC_provider_query_operation_fn *query_operation;
b0001d0c 84 OSSL_FUNC_provider_unquery_operation_fn *unquery_operation;
a39eb840 85
5a29b628
RL
86 /*
87 * Cache of bit to indicate of query_operation() has been called on
88 * a specific operation or not.
89 */
90 unsigned char *operation_bits;
91 size_t operation_bits_sz;
c25a1524 92 CRYPTO_RWLOCK *opbits_lock;
5a29b628 93
a39eb840
RL
94 /* Provider side data */
95 void *provctx;
4c2883a9
RL
96};
97DEFINE_STACK_OF(OSSL_PROVIDER)
98
99static int ossl_provider_cmp(const OSSL_PROVIDER * const *a,
100 const OSSL_PROVIDER * const *b)
101{
102 return strcmp((*a)->name, (*b)->name);
103}
104
105/*-
106 * Provider Object store
107 * =====================
108 *
109 * The Provider Object store is a library context object, and therefore needs
110 * an index.
111 */
112
113struct provider_store_st {
114 STACK_OF(OSSL_PROVIDER) *providers;
115 CRYPTO_RWLOCK *lock;
6bd4e3f2 116 char *default_path;
e55008a9 117 unsigned int use_fallbacks:1;
4c2883a9 118};
4c2883a9 119
c8567c39 120/*
390f9bad
RL
121 * provider_deactivate_free() is a wrapper around ossl_provider_deactivate()
122 * and ossl_provider_free(), called as needed.
c8567c39
RL
123 * Since this is only called when the provider store is being emptied, we
124 * don't need to care about any lock.
125 */
126static void provider_deactivate_free(OSSL_PROVIDER *prov)
127{
390f9bad
RL
128 if (prov->flag_activated)
129 ossl_provider_deactivate(prov);
c8567c39
RL
130 ossl_provider_free(prov);
131}
132
4c2883a9
RL
133static void provider_store_free(void *vstore)
134{
135 struct provider_store_st *store = vstore;
136
137 if (store == NULL)
138 return;
6bd4e3f2 139 OPENSSL_free(store->default_path);
c8567c39 140 sk_OSSL_PROVIDER_pop_free(store->providers, provider_deactivate_free);
4c2883a9
RL
141 CRYPTO_THREAD_lock_free(store->lock);
142 OPENSSL_free(store);
143}
144
b4250010 145static void *provider_store_new(OSSL_LIB_CTX *ctx)
4c2883a9
RL
146{
147 struct provider_store_st *store = OPENSSL_zalloc(sizeof(*store));
c41f3ae0 148 const struct predefined_providers_st *p = NULL;
4c2883a9
RL
149
150 if (store == NULL
151 || (store->providers = sk_OSSL_PROVIDER_new(ossl_provider_cmp)) == NULL
152 || (store->lock = CRYPTO_THREAD_lock_new()) == NULL) {
153 provider_store_free(store);
e55008a9 154 return NULL;
4c2883a9 155 }
e55008a9 156 store->use_fallbacks = 1;
c41f3ae0
RL
157
158 for (p = predefined_providers; p->name != NULL; p++) {
159 OSSL_PROVIDER *prov = NULL;
160
161 /*
162 * We use the internal constructor directly here,
163 * otherwise we get a call loop
164 */
165 prov = provider_new(p->name, p->init);
166
167 if (prov == NULL
168 || sk_OSSL_PROVIDER_push(store->providers, prov) == 0) {
169 ossl_provider_free(prov);
170 provider_store_free(store);
9311d0c4 171 ERR_raise(ERR_LIB_CRYPTO, ERR_R_INTERNAL_ERROR);
c41f3ae0
RL
172 return NULL;
173 }
e7706e63 174 prov->libctx = ctx;
c41f3ae0 175 prov->store = store;
f844f9eb 176#ifndef FIPS_MODULE
6ebc2f56 177 prov->error_lib = ERR_get_next_error_library();
6592ab81 178#endif
c41f3ae0
RL
179 if(p->is_fallback)
180 ossl_provider_set_fallback(prov);
181 }
182
4c2883a9
RL
183 return store;
184}
185
b4250010 186static const OSSL_LIB_CTX_METHOD provider_store_method = {
4c2883a9
RL
187 provider_store_new,
188 provider_store_free,
189};
190
b4250010 191static struct provider_store_st *get_provider_store(OSSL_LIB_CTX *libctx)
4c2883a9
RL
192{
193 struct provider_store_st *store = NULL;
194
b4250010
DMSP
195 store = ossl_lib_ctx_get_data(libctx, OSSL_LIB_CTX_PROVIDER_STORE_INDEX,
196 &provider_store_method);
4c2883a9 197 if (store == NULL)
9311d0c4 198 ERR_raise(ERR_LIB_CRYPTO, ERR_R_INTERNAL_ERROR);
4c2883a9
RL
199 return store;
200}
201
b4250010 202int ossl_provider_disable_fallback_loading(OSSL_LIB_CTX *libctx)
ebe3f24b
P
203{
204 struct provider_store_st *store;
205
206 if ((store = get_provider_store(libctx)) != NULL) {
d60a8e0a 207 CRYPTO_THREAD_write_lock(store->lock);
ebe3f24b 208 store->use_fallbacks = 0;
d60a8e0a 209 CRYPTO_THREAD_unlock(store->lock);
ebe3f24b
P
210 return 1;
211 }
212 return 0;
213}
214
b4250010 215OSSL_PROVIDER *ossl_provider_find(OSSL_LIB_CTX *libctx, const char *name,
29dc6e00 216 int noconfig)
4c2883a9
RL
217{
218 struct provider_store_st *store = NULL;
219 OSSL_PROVIDER *prov = NULL;
220
221 if ((store = get_provider_store(libctx)) != NULL) {
222 OSSL_PROVIDER tmpl = { 0, };
223 int i;
224
f844f9eb 225#ifndef FIPS_MODULE
29dc6e00
MC
226 /*
227 * Make sure any providers are loaded from config before we try to find
228 * them.
229 */
230 if (!noconfig)
231 OPENSSL_init_crypto(OPENSSL_INIT_LOAD_CONFIG, NULL);
232#endif
233
4c2883a9
RL
234 tmpl.name = (char *)name;
235 CRYPTO_THREAD_write_lock(store->lock);
236 if ((i = sk_OSSL_PROVIDER_find(store->providers, &tmpl)) == -1
237 || (prov = sk_OSSL_PROVIDER_value(store->providers, i)) == NULL
7c95390e 238 || !ossl_provider_up_ref(prov))
4c2883a9
RL
239 prov = NULL;
240 CRYPTO_THREAD_unlock(store->lock);
241 }
242
243 return prov;
244}
245
c41f3ae0
RL
246/*-
247 * Provider Object methods
248 * =======================
249 */
250
251static OSSL_PROVIDER *provider_new(const char *name,
252 OSSL_provider_init_fn *init_function)
253{
254 OSSL_PROVIDER *prov = NULL;
255
256 if ((prov = OPENSSL_zalloc(sizeof(*prov))) == NULL
257#ifndef HAVE_ATOMICS
258 || (prov->refcnt_lock = CRYPTO_THREAD_lock_new()) == NULL
390f9bad 259 || (prov->activatecnt_lock = CRYPTO_THREAD_lock_new()) == NULL
c41f3ae0 260#endif
7c95390e 261 || !ossl_provider_up_ref(prov) /* +1 One reference to be returned */
c25a1524 262 || (prov->opbits_lock = CRYPTO_THREAD_lock_new()) == NULL
c2ec2bb7 263 || (prov->flag_lock = CRYPTO_THREAD_lock_new()) == NULL
c41f3ae0
RL
264 || (prov->name = OPENSSL_strdup(name)) == NULL) {
265 ossl_provider_free(prov);
9311d0c4 266 ERR_raise(ERR_LIB_CRYPTO, ERR_R_MALLOC_FAILURE);
c41f3ae0
RL
267 return NULL;
268 }
269
270 prov->init_function = init_function;
271 return prov;
272}
273
7c95390e 274int ossl_provider_up_ref(OSSL_PROVIDER *prov)
c41f3ae0
RL
275{
276 int ref = 0;
277
ad14e8e5
SL
278 if (CRYPTO_UP_REF(&prov->refcnt, &ref, prov->refcnt_lock) <= 0)
279 return 0;
c41f3ae0
RL
280 return ref;
281}
282
b4250010 283OSSL_PROVIDER *ossl_provider_new(OSSL_LIB_CTX *libctx, const char *name,
29dc6e00
MC
284 OSSL_provider_init_fn *init_function,
285 int noconfig)
4c2883a9
RL
286{
287 struct provider_store_st *store = NULL;
288 OSSL_PROVIDER *prov = NULL;
289
290 if ((store = get_provider_store(libctx)) == NULL)
291 return NULL;
292
29dc6e00
MC
293 if ((prov = ossl_provider_find(libctx, name,
294 noconfig)) != NULL) { /* refcount +1 */
4c2883a9 295 ossl_provider_free(prov); /* refcount -1 */
105d01f1 296 ERR_raise_data(ERR_LIB_CRYPTO, CRYPTO_R_PROVIDER_ALREADY_EXISTS,
49c64346 297 "name=%s", name);
4c2883a9
RL
298 return NULL;
299 }
300
c41f3ae0
RL
301 /* provider_new() generates an error, so no need here */
302 if ((prov = provider_new(name, init_function)) == NULL)
4c2883a9 303 return NULL;
4c2883a9
RL
304
305 CRYPTO_THREAD_write_lock(store->lock);
7c95390e 306 if (!ossl_provider_up_ref(prov)) { /* +1 One reference for the store */
4c2883a9
RL
307 ossl_provider_free(prov); /* -1 Reference that was to be returned */
308 prov = NULL;
309 } else if (sk_OSSL_PROVIDER_push(store->providers, prov) == 0) {
310 ossl_provider_free(prov); /* -1 Store reference */
311 ossl_provider_free(prov); /* -1 Reference that was to be returned */
312 prov = NULL;
e55008a9 313 } else {
e7706e63 314 prov->libctx = libctx;
e55008a9 315 prov->store = store;
f844f9eb 316#ifndef FIPS_MODULE
6ebc2f56 317 prov->error_lib = ERR_get_next_error_library();
6592ab81 318#endif
4c2883a9
RL
319 }
320 CRYPTO_THREAD_unlock(store->lock);
321
322 if (prov == NULL)
9311d0c4 323 ERR_raise(ERR_LIB_CRYPTO, ERR_R_MALLOC_FAILURE);
4c2883a9
RL
324
325 /*
326 * At this point, the provider is only partially "loaded". To be
327 * fully "loaded", ossl_provider_activate() must also be called.
328 */
329
330 return prov;
331}
332
ac1055ef
RL
333static void free_infopair(INFOPAIR *pair)
334{
335 OPENSSL_free(pair->name);
336 OPENSSL_free(pair->value);
337 OPENSSL_free(pair);
338}
339
4c2883a9
RL
340void ossl_provider_free(OSSL_PROVIDER *prov)
341{
342 if (prov != NULL) {
343 int ref = 0;
344
085bef9f 345 CRYPTO_DOWN_REF(&prov->refcnt, &ref, prov->refcnt_lock);
4c2883a9
RL
346
347 /*
390f9bad
RL
348 * When the refcount drops to zero, we clean up the provider.
349 * Note that this also does teardown, which may seem late,
350 * considering that init happens on first activation. However,
351 * there may be other structures hanging on to the provider after
352 * the last deactivation and may therefore need full access to the
353 * provider's services. Therefore, we deinit late.
4c2883a9 354 */
390f9bad
RL
355 if (ref == 0) {
356 if (prov->flag_initialized) {
f844f9eb 357#ifndef FIPS_MODULE
390f9bad 358 ossl_init_thread_deregister(prov);
6913f5fe 359#endif
390f9bad
RL
360 if (prov->teardown != NULL)
361 prov->teardown(prov->provctx);
6ebc2f56 362#ifndef OPENSSL_NO_ERR
f844f9eb 363# ifndef FIPS_MODULE
390f9bad
RL
364 if (prov->error_strings != NULL) {
365 ERR_unload_strings(prov->error_lib, prov->error_strings);
366 OPENSSL_free(prov->error_strings);
367 prov->error_strings = NULL;
368 }
6ebc2f56
RL
369# endif
370#endif
390f9bad
RL
371 OPENSSL_free(prov->operation_bits);
372 prov->operation_bits = NULL;
373 prov->operation_bits_sz = 0;
374 prov->flag_initialized = 0;
375 }
4c2883a9 376
f844f9eb 377#ifndef FIPS_MODULE
4c2883a9 378 DSO_free(prov->module);
3593266d 379#endif
4c2883a9 380 OPENSSL_free(prov->name);
ac1055ef
RL
381 OPENSSL_free(prov->path);
382 sk_INFOPAIR_pop_free(prov->parameters, free_infopair);
c25a1524 383 CRYPTO_THREAD_lock_free(prov->opbits_lock);
c2ec2bb7 384 CRYPTO_THREAD_lock_free(prov->flag_lock);
085bef9f
RL
385#ifndef HAVE_ATOMICS
386 CRYPTO_THREAD_lock_free(prov->refcnt_lock);
390f9bad 387 CRYPTO_THREAD_lock_free(prov->activatecnt_lock);
085bef9f 388#endif
4c2883a9
RL
389 OPENSSL_free(prov);
390 }
391 }
392}
393
ac1055ef
RL
394/* Setters */
395int ossl_provider_set_module_path(OSSL_PROVIDER *prov, const char *module_path)
396{
397 OPENSSL_free(prov->path);
398 if (module_path == NULL)
399 return 1;
400 if ((prov->path = OPENSSL_strdup(module_path)) != NULL)
401 return 1;
9311d0c4 402 ERR_raise(ERR_LIB_CRYPTO, ERR_R_MALLOC_FAILURE);
ac1055ef
RL
403 return 0;
404}
405
406int ossl_provider_add_parameter(OSSL_PROVIDER *prov,
407 const char *name, const char *value)
408{
409 INFOPAIR *pair = NULL;
410
411 if ((pair = OPENSSL_zalloc(sizeof(*pair))) != NULL
412 && (prov->parameters != NULL
413 || (prov->parameters = sk_INFOPAIR_new_null()) != NULL)
414 && (pair->name = OPENSSL_strdup(name)) != NULL
415 && (pair->value = OPENSSL_strdup(value)) != NULL
416 && sk_INFOPAIR_push(prov->parameters, pair) > 0)
417 return 1;
418
419 if (pair != NULL) {
420 OPENSSL_free(pair->name);
421 OPENSSL_free(pair->value);
422 OPENSSL_free(pair);
423 }
9311d0c4 424 ERR_raise(ERR_LIB_CRYPTO, ERR_R_MALLOC_FAILURE);
ac1055ef
RL
425 return 0;
426}
427
4c2883a9
RL
428/*
429 * Provider activation.
430 *
431 * What "activation" means depends on the provider form; for built in
432 * providers (in the library or the application alike), the provider
433 * can already be considered to be loaded, all that's needed is to
434 * initialize it. However, for dynamically loadable provider modules,
435 * we must first load that module.
436 *
437 * Built in modules are distinguished from dynamically loaded modules
438 * with an already assigned init function.
439 */
440static const OSSL_DISPATCH *core_dispatch; /* Define further down */
441
b4250010
DMSP
442int OSSL_PROVIDER_set_default_search_path(OSSL_LIB_CTX *libctx,
443 const char *path)
6bd4e3f2
P
444{
445 struct provider_store_st *store;
446 char *p = NULL;
447
448 if (path != NULL) {
449 p = OPENSSL_strdup(path);
450 if (p == NULL) {
9311d0c4 451 ERR_raise(ERR_LIB_CRYPTO, ERR_R_MALLOC_FAILURE);
6bd4e3f2
P
452 return 0;
453 }
454 }
455 if ((store = get_provider_store(libctx)) != NULL
456 && CRYPTO_THREAD_write_lock(store->lock)) {
457 OPENSSL_free(store->default_path);
458 store->default_path = p;
459 CRYPTO_THREAD_unlock(store->lock);
460 return 1;
461 }
462 OPENSSL_free(p);
463 return 0;
464}
465
e55008a9
RL
466/*
467 * Internal version that doesn't affect the store flags, and thereby avoid
468 * locking. Direct callers must remember to set the store flags when
e7706e63 469 * appropriate.
e55008a9 470 */
390f9bad 471static int provider_init(OSSL_PROVIDER *prov)
4c2883a9
RL
472{
473 const OSSL_DISPATCH *provider_dispatch = NULL;
914db66d 474 void *tmp_provctx = NULL; /* safety measure */
6ebc2f56 475#ifndef OPENSSL_NO_ERR
f844f9eb 476# ifndef FIPS_MODULE
363b1e5d 477 OSSL_FUNC_provider_get_reason_strings_fn *p_get_reason_strings = NULL;
6592ab81 478# endif
6ebc2f56 479#endif
c2ec2bb7 480 int ok = 0;
4c2883a9 481
c2ec2bb7
RL
482 /*
483 * The flag lock is used to lock init, not only because the flag is
484 * checked here and set at the end, but also because this function
485 * modifies a number of things in the provider structure that this
486 * function needs to perform under lock anyway.
487 */
488 CRYPTO_THREAD_write_lock(prov->flag_lock);
489 if (prov->flag_initialized) {
490 ok = 1;
491 goto end;
492 }
4c2883a9
RL
493
494 /*
495 * If the init function isn't set, it indicates that this provider is
496 * a loadable module.
497 */
498 if (prov->init_function == NULL) {
f844f9eb 499#ifdef FIPS_MODULE
c2ec2bb7 500 goto end;
3593266d 501#else
4c2883a9 502 if (prov->module == NULL) {
ac1055ef
RL
503 char *allocated_path = NULL;
504 const char *module_path = NULL;
505 char *merged_path = NULL;
6bd4e3f2
P
506 const char *load_dir = NULL;
507 struct provider_store_st *store;
4c2883a9
RL
508
509 if ((prov->module = DSO_new()) == NULL) {
510 /* DSO_new() generates an error already */
c2ec2bb7 511 goto end;
4c2883a9
RL
512 }
513
6bd4e3f2
P
514 if ((store = get_provider_store(prov->libctx)) == NULL
515 || !CRYPTO_THREAD_read_lock(store->lock))
c2ec2bb7 516 goto end;
6bd4e3f2 517 load_dir = store->default_path;
c2ec2bb7 518 CRYPTO_THREAD_unlock(store->lock);
6bd4e3f2
P
519
520 if (load_dir == NULL) {
521 load_dir = ossl_safe_getenv("OPENSSL_MODULES");
522 if (load_dir == NULL)
523 load_dir = MODULESDIR;
524 }
4c2883a9
RL
525
526 DSO_ctrl(prov->module, DSO_CTRL_SET_FLAGS,
527 DSO_FLAG_NAME_TRANSLATION_EXT_ONLY, NULL);
ac1055ef
RL
528
529 module_path = prov->path;
530 if (module_path == NULL)
531 module_path = allocated_path =
532 DSO_convert_filename(prov->module, prov->name);
533 if (module_path != NULL)
534 merged_path = DSO_merge(prov->module, module_path, load_dir);
535
536 if (merged_path == NULL
537 || (DSO_load(prov->module, merged_path, NULL, 0)) == NULL) {
4c2883a9
RL
538 DSO_free(prov->module);
539 prov->module = NULL;
540 }
541
ac1055ef
RL
542 OPENSSL_free(merged_path);
543 OPENSSL_free(allocated_path);
4c2883a9
RL
544 }
545
546 if (prov->module != NULL)
547 prov->init_function = (OSSL_provider_init_fn *)
548 DSO_bind_func(prov->module, "OSSL_provider_init");
3593266d 549#endif
4c2883a9
RL
550 }
551
e7706e63 552 /* Call the initialise function for the provider. */
4c2883a9 553 if (prov->init_function == NULL
d40b42ab
MC
554 || !prov->init_function((OSSL_CORE_HANDLE *)prov, core_dispatch,
555 &provider_dispatch, &tmp_provctx)) {
105d01f1 556 ERR_raise_data(ERR_LIB_CRYPTO, ERR_R_INIT_FAIL,
49c64346 557 "name=%s", prov->name);
f844f9eb 558#ifndef FIPS_MODULE
4c2883a9
RL
559 DSO_free(prov->module);
560 prov->module = NULL;
3593266d 561#endif
c2ec2bb7 562 goto end;
4c2883a9 563 }
914db66d 564 prov->provctx = tmp_provctx;
4c2883a9
RL
565
566 for (; provider_dispatch->function_id != 0; provider_dispatch++) {
567 switch (provider_dispatch->function_id) {
568 case OSSL_FUNC_PROVIDER_TEARDOWN:
569 prov->teardown =
363b1e5d 570 OSSL_FUNC_provider_teardown(provider_dispatch);
4c2883a9 571 break;
dca97d00
RL
572 case OSSL_FUNC_PROVIDER_GETTABLE_PARAMS:
573 prov->gettable_params =
363b1e5d 574 OSSL_FUNC_provider_gettable_params(provider_dispatch);
4c2883a9
RL
575 break;
576 case OSSL_FUNC_PROVIDER_GET_PARAMS:
577 prov->get_params =
363b1e5d 578 OSSL_FUNC_provider_get_params(provider_dispatch);
4c2883a9 579 break;
04cb5ec0
SL
580 case OSSL_FUNC_PROVIDER_SELF_TEST:
581 prov->self_test =
582 OSSL_FUNC_provider_self_test(provider_dispatch);
583 break;
82ec09ec
MC
584 case OSSL_FUNC_PROVIDER_GET_CAPABILITIES:
585 prov->get_capabilities =
363b1e5d 586 OSSL_FUNC_provider_get_capabilities(provider_dispatch);
82ec09ec 587 break;
099bd339
RL
588 case OSSL_FUNC_PROVIDER_QUERY_OPERATION:
589 prov->query_operation =
363b1e5d 590 OSSL_FUNC_provider_query_operation(provider_dispatch);
099bd339 591 break;
b0001d0c
P
592 case OSSL_FUNC_PROVIDER_UNQUERY_OPERATION:
593 prov->unquery_operation =
594 OSSL_FUNC_provider_unquery_operation(provider_dispatch);
595 break;
6ebc2f56 596#ifndef OPENSSL_NO_ERR
f844f9eb 597# ifndef FIPS_MODULE
6ebc2f56
RL
598 case OSSL_FUNC_PROVIDER_GET_REASON_STRINGS:
599 p_get_reason_strings =
363b1e5d 600 OSSL_FUNC_provider_get_reason_strings(provider_dispatch);
6ebc2f56 601 break;
6592ab81 602# endif
6ebc2f56
RL
603#endif
604 }
605 }
606
607#ifndef OPENSSL_NO_ERR
f844f9eb 608# ifndef FIPS_MODULE
6ebc2f56
RL
609 if (p_get_reason_strings != NULL) {
610 const OSSL_ITEM *reasonstrings = p_get_reason_strings(prov->provctx);
611 size_t cnt, cnt2;
612
613 /*
614 * ERR_load_strings() handles ERR_STRING_DATA rather than OSSL_ITEM,
615 * although they are essentially the same type.
616 * Furthermore, ERR_load_strings() patches the array's error number
617 * with the error library number, so we need to make a copy of that
618 * array either way.
619 */
fd03868b 620 cnt = 0;
6ebc2f56
RL
621 while (reasonstrings[cnt].id != 0) {
622 if (ERR_GET_LIB(reasonstrings[cnt].id) != 0)
c2ec2bb7 623 goto end;
6ebc2f56
RL
624 cnt++;
625 }
fd03868b 626 cnt++; /* One for the terminating item */
6ebc2f56
RL
627
628 /* Allocate one extra item for the "library" name */
629 prov->error_strings =
630 OPENSSL_zalloc(sizeof(ERR_STRING_DATA) * (cnt + 1));
631 if (prov->error_strings == NULL)
c2ec2bb7 632 goto end;
6ebc2f56
RL
633
634 /*
635 * Set the "library" name.
636 */
637 prov->error_strings[0].error = ERR_PACK(prov->error_lib, 0, 0);
638 prov->error_strings[0].string = prov->name;
639 /*
640 * Copy reasonstrings item 0..cnt-1 to prov->error_trings positions
641 * 1..cnt.
642 */
643 for (cnt2 = 1; cnt2 <= cnt; cnt2++) {
644 prov->error_strings[cnt2].error = (int)reasonstrings[cnt2-1].id;
645 prov->error_strings[cnt2].string = reasonstrings[cnt2-1].ptr;
4c2883a9 646 }
6ebc2f56
RL
647
648 ERR_load_strings(prov->error_lib, prov->error_strings);
4c2883a9 649 }
6592ab81 650# endif
6ebc2f56 651#endif
4c2883a9
RL
652
653 /* With this flag set, this provider has become fully "loaded". */
654 prov->flag_initialized = 1;
c2ec2bb7
RL
655 ok = 1;
656
657 end:
658 CRYPTO_THREAD_unlock(prov->flag_lock);
659 return ok;
4c2883a9
RL
660}
661
390f9bad
RL
662static int provider_deactivate(OSSL_PROVIDER *prov)
663{
664 int ref = 0;
665
666 if (!ossl_assert(prov != NULL))
667 return 0;
668
669 if (CRYPTO_DOWN_REF(&prov->activatecnt, &ref, prov->activatecnt_lock) <= 0)
670 return 0;
671
c2ec2bb7
RL
672 if (ref < 1) {
673 CRYPTO_THREAD_write_lock(prov->flag_lock);
390f9bad 674 prov->flag_activated = 0;
c2ec2bb7
RL
675 CRYPTO_THREAD_unlock(prov->flag_lock);
676 }
390f9bad
RL
677
678 /* We don't deinit here, that's done in ossl_provider_free() */
679 return 1;
680}
681
682static int provider_activate(OSSL_PROVIDER *prov)
683{
684 int ref = 0;
685
686 if (CRYPTO_UP_REF(&prov->activatecnt, &ref, prov->activatecnt_lock) <= 0)
687 return 0;
688
689 if (provider_init(prov)) {
c2ec2bb7 690 CRYPTO_THREAD_write_lock(prov->flag_lock);
390f9bad 691 prov->flag_activated = 1;
c2ec2bb7 692 CRYPTO_THREAD_unlock(prov->flag_lock);
390f9bad
RL
693
694 return 1;
695 }
696
697 provider_deactivate(prov);
698 return 0;
699}
700
299f5ff3 701int ossl_provider_activate(OSSL_PROVIDER *prov, int retain_fallbacks)
e55008a9 702{
390f9bad
RL
703 if (prov == NULL)
704 return 0;
e7706e63 705 if (provider_activate(prov)) {
299f5ff3
P
706 if (!retain_fallbacks) {
707 CRYPTO_THREAD_write_lock(prov->store->lock);
708 prov->store->use_fallbacks = 0;
709 CRYPTO_THREAD_unlock(prov->store->lock);
710 }
e55008a9
RL
711 return 1;
712 }
e55008a9
RL
713 return 0;
714}
715
390f9bad
RL
716int ossl_provider_deactivate(OSSL_PROVIDER *prov)
717{
718 if (prov == NULL)
719 return 0;
720 return provider_deactivate(prov);
721}
722
a39eb840
RL
723void *ossl_provider_ctx(const OSSL_PROVIDER *prov)
724{
725 return prov->provctx;
726}
727
e55008a9
RL
728
729static int provider_forall_loaded(struct provider_store_st *store,
730 int *found_activated,
731 int (*cb)(OSSL_PROVIDER *provider,
732 void *cbdata),
733 void *cbdata)
734{
735 int i;
736 int ret = 1;
29dc6e00
MC
737 int num_provs;
738
29dc6e00 739 num_provs = sk_OSSL_PROVIDER_num(store->providers);
e55008a9
RL
740
741 if (found_activated != NULL)
742 *found_activated = 0;
743 for (i = 0; i < num_provs; i++) {
744 OSSL_PROVIDER *prov =
745 sk_OSSL_PROVIDER_value(store->providers, i);
746
390f9bad 747 if (prov->flag_activated) {
e55008a9
RL
748 if (found_activated != NULL)
749 *found_activated = 1;
750 if (!(ret = cb(prov, cbdata)))
751 break;
752 }
753 }
754
755 return ret;
756}
757
36f5ec55
RL
758/*
759 * This function only does something once when store->use_fallbacks == 1,
760 * and then sets store->use_fallbacks = 0, so the second call and so on is
761 * effectively a no-op.
762 */
763static void provider_activate_fallbacks(struct provider_store_st *store)
764{
7dd2cb56
MC
765 int use_fallbacks;
766 int num_provs;
767 int activated_fallback_count = 0;
768 int i;
769
770 CRYPTO_THREAD_read_lock(store->lock);
771 use_fallbacks = store->use_fallbacks;
772 CRYPTO_THREAD_unlock(store->lock);
773 if (!use_fallbacks)
774 return;
775
776 CRYPTO_THREAD_write_lock(store->lock);
777 /* Check again, just in case another thread changed it */
778 use_fallbacks = store->use_fallbacks;
779 if (!use_fallbacks) {
780 CRYPTO_THREAD_unlock(store->lock);
781 return;
782 }
36f5ec55 783
7dd2cb56
MC
784 num_provs = sk_OSSL_PROVIDER_num(store->providers);
785 for (i = 0; i < num_provs; i++) {
786 OSSL_PROVIDER *prov = sk_OSSL_PROVIDER_value(store->providers, i);
36f5ec55 787
7dd2cb56
MC
788 if (ossl_provider_up_ref(prov)) {
789 if (prov->flag_fallback) {
790 if (provider_activate(prov)) {
791 prov->flag_activated_as_fallback = 1;
792 activated_fallback_count++;
c8567c39 793 }
36f5ec55 794 }
7dd2cb56 795 ossl_provider_free(prov);
36f5ec55 796 }
36f5ec55 797 }
7dd2cb56
MC
798
799 /*
800 * We assume that all fallbacks have been added to the store before
801 * any fallback is activated.
802 * TODO: We may have to reconsider this, IF we find ourselves adding
803 * fallbacks after any previous fallback has been activated.
804 */
805 if (activated_fallback_count > 0)
806 store->use_fallbacks = 0;
807
808 CRYPTO_THREAD_unlock(store->lock);
36f5ec55
RL
809}
810
b4250010 811int ossl_provider_forall_loaded(OSSL_LIB_CTX *ctx,
85e2417c
RL
812 int (*cb)(OSSL_PROVIDER *provider,
813 void *cbdata),
814 void *cbdata)
815{
816 int ret = 1;
85e2417c
RL
817 struct provider_store_st *store = get_provider_store(ctx);
818
f844f9eb 819#ifndef FIPS_MODULE
5c5cdcd8
MC
820 /*
821 * Make sure any providers are loaded from config before we try to use
822 * them.
823 */
824 OPENSSL_init_crypto(OPENSSL_INIT_LOAD_CONFIG, NULL);
825#endif
826
85e2417c 827 if (store != NULL) {
36f5ec55 828 provider_activate_fallbacks(store);
85e2417c 829
7dd2cb56 830 CRYPTO_THREAD_read_lock(store->lock);
e55008a9 831 /*
36f5ec55 832 * Now, we sweep through all providers
e55008a9 833 */
36f5ec55 834 ret = provider_forall_loaded(store, NULL, cb, cbdata);
e55008a9 835
85e2417c
RL
836 CRYPTO_THREAD_unlock(store->lock);
837 }
838
839 return ret;
840}
841
36f5ec55
RL
842int ossl_provider_available(OSSL_PROVIDER *prov)
843{
844 if (prov != NULL) {
36f5ec55 845 provider_activate_fallbacks(prov->store);
36f5ec55 846
390f9bad 847 return prov->flag_activated;
36f5ec55
RL
848 }
849 return 0;
850}
851
e55008a9
RL
852/* Setters of Provider Object data */
853int ossl_provider_set_fallback(OSSL_PROVIDER *prov)
854{
855 if (prov == NULL)
856 return 0;
857
858 prov->flag_fallback = 1;
859 return 1;
860}
861
4c2883a9 862/* Getters of Provider Object data */
24626a47 863const char *ossl_provider_name(const OSSL_PROVIDER *prov)
4c2883a9
RL
864{
865 return prov->name;
866}
867
24626a47 868const DSO *ossl_provider_dso(const OSSL_PROVIDER *prov)
4c2883a9
RL
869{
870 return prov->module;
871}
872
24626a47 873const char *ossl_provider_module_name(const OSSL_PROVIDER *prov)
4c2883a9 874{
f844f9eb 875#ifdef FIPS_MODULE
3593266d
MC
876 return NULL;
877#else
4c2883a9 878 return DSO_get_filename(prov->module);
3593266d 879#endif
4c2883a9
RL
880}
881
24626a47 882const char *ossl_provider_module_path(const OSSL_PROVIDER *prov)
4c2883a9 883{
f844f9eb 884#ifdef FIPS_MODULE
3593266d
MC
885 return NULL;
886#else
4c2883a9
RL
887 /* FIXME: Ensure it's a full path */
888 return DSO_get_filename(prov->module);
3593266d 889#endif
4c2883a9
RL
890}
891
d01d3752
MC
892void *ossl_provider_prov_ctx(const OSSL_PROVIDER *prov)
893{
894 if (prov != NULL)
895 return prov->provctx;
896
897 return NULL;
898}
899
a829b735 900OSSL_LIB_CTX *ossl_provider_libctx(const OSSL_PROVIDER *prov)
e74bd290 901{
185ce3d9
P
902 /* TODO(3.0) just: return prov->libctx; */
903 return prov != NULL ? prov->libctx : NULL;
e74bd290
RL
904}
905
4c2883a9
RL
906/* Wrappers around calls to the provider */
907void ossl_provider_teardown(const OSSL_PROVIDER *prov)
908{
909 if (prov->teardown != NULL)
a39eb840 910 prov->teardown(prov->provctx);
4c2883a9
RL
911}
912
dca97d00 913const OSSL_PARAM *ossl_provider_gettable_params(const OSSL_PROVIDER *prov)
4c2883a9 914{
dca97d00
RL
915 return prov->gettable_params == NULL
916 ? NULL : prov->gettable_params(prov->provctx);
4c2883a9
RL
917}
918
4e7991b4 919int ossl_provider_get_params(const OSSL_PROVIDER *prov, OSSL_PARAM params[])
4c2883a9 920{
a39eb840
RL
921 return prov->get_params == NULL
922 ? 0 : prov->get_params(prov->provctx, params);
4c2883a9
RL
923}
924
04cb5ec0
SL
925int ossl_provider_self_test(const OSSL_PROVIDER *prov)
926{
927 int ret;
928
929 if (prov->self_test == NULL)
930 return 1;
931 ret = prov->self_test(prov->provctx);
932 if (ret == 0)
a829b735 933 evp_method_store_flush(ossl_provider_libctx(prov));
04cb5ec0
SL
934 return ret;
935}
936
82ec09ec
MC
937int ossl_provider_get_capabilities(const OSSL_PROVIDER *prov,
938 const char *capability,
939 OSSL_CALLBACK *cb,
940 void *arg)
941{
942 return prov->get_capabilities == NULL
08a1c9f2 943 ? 1 : prov->get_capabilities(prov->provctx, capability, cb, arg);
82ec09ec
MC
944}
945
099bd339
RL
946const OSSL_ALGORITHM *ossl_provider_query_operation(const OSSL_PROVIDER *prov,
947 int operation_id,
948 int *no_cache)
949{
7dce37e2
P
950 const OSSL_ALGORITHM *res;
951
952 if (prov->query_operation == NULL)
953 return NULL;
954 res = prov->query_operation(prov->provctx, operation_id, no_cache);
955#if defined(OPENSSL_NO_CACHED_FETCH)
956 /* Forcing the non-caching of queries */
957 if (no_cache != NULL)
958 *no_cache = 1;
959#endif
960 return res;
099bd339
RL
961}
962
b0001d0c
P
963void ossl_provider_unquery_operation(const OSSL_PROVIDER *prov,
964 int operation_id,
965 const OSSL_ALGORITHM *algs)
966{
967 if (prov->unquery_operation != NULL)
968 prov->unquery_operation(prov->provctx, operation_id, algs);
969}
970
5a29b628
RL
971int ossl_provider_set_operation_bit(OSSL_PROVIDER *provider, size_t bitnum)
972{
973 size_t byte = bitnum / 8;
974 unsigned char bit = (1 << (bitnum % 8)) & 0xFF;
975
c25a1524 976 CRYPTO_THREAD_write_lock(provider->opbits_lock);
5a29b628 977 if (provider->operation_bits_sz <= byte) {
2b748d72
TS
978 unsigned char *tmp = OPENSSL_realloc(provider->operation_bits,
979 byte + 1);
980
981 if (tmp == NULL) {
c25a1524 982 CRYPTO_THREAD_unlock(provider->opbits_lock);
5a29b628
RL
983 ERR_raise(ERR_LIB_CRYPTO, ERR_R_MALLOC_FAILURE);
984 return 0;
985 }
2b748d72 986 provider->operation_bits = tmp;
5a29b628
RL
987 memset(provider->operation_bits + provider->operation_bits_sz,
988 '\0', byte + 1 - provider->operation_bits_sz);
2b748d72 989 provider->operation_bits_sz = byte + 1;
5a29b628
RL
990 }
991 provider->operation_bits[byte] |= bit;
c25a1524 992 CRYPTO_THREAD_unlock(provider->opbits_lock);
5a29b628
RL
993 return 1;
994}
995
996int ossl_provider_test_operation_bit(OSSL_PROVIDER *provider, size_t bitnum,
997 int *result)
998{
999 size_t byte = bitnum / 8;
1000 unsigned char bit = (1 << (bitnum % 8)) & 0xFF;
1001
1002 if (!ossl_assert(result != NULL)) {
1003 ERR_raise(ERR_LIB_CRYPTO, ERR_R_PASSED_NULL_PARAMETER);
1004 return 0;
1005 }
1006
1007 *result = 0;
c25a1524 1008 CRYPTO_THREAD_read_lock(provider->opbits_lock);
5a29b628
RL
1009 if (provider->operation_bits_sz > byte)
1010 *result = ((provider->operation_bits[byte] & bit) != 0);
c25a1524 1011 CRYPTO_THREAD_unlock(provider->opbits_lock);
5a29b628
RL
1012 return 1;
1013}
1014
4c2883a9
RL
1015/*-
1016 * Core functions for the provider
1017 * ===============================
1018 *
1019 * This is the set of functions that the core makes available to the provider
1020 */
1021
1022/*
1023 * This returns a list of Provider Object parameters with their types, for
1024 * discovery. We do not expect that many providers will use this, but one
1025 * never knows.
1026 */
26175013 1027static const OSSL_PARAM param_types[] = {
b8086652
SL
1028 OSSL_PARAM_DEFN(OSSL_PROV_PARAM_CORE_VERSION, OSSL_PARAM_UTF8_PTR, NULL, 0),
1029 OSSL_PARAM_DEFN(OSSL_PROV_PARAM_CORE_PROV_NAME, OSSL_PARAM_UTF8_PTR,
1030 NULL, 0),
1031#ifndef FIPS_MODULE
1032 OSSL_PARAM_DEFN(OSSL_PROV_PARAM_CORE_MODULE_FILENAME, OSSL_PARAM_UTF8_PTR,
1033 NULL, 0),
1034#endif
26175013 1035 OSSL_PARAM_END
4c2883a9
RL
1036};
1037
49c64346
RL
1038/*
1039 * Forward declare all the functions that are provided aa dispatch.
1040 * This ensures that the compiler will complain if they aren't defined
1041 * with the correct signature.
1042 */
363b1e5d
DMSP
1043static OSSL_FUNC_core_gettable_params_fn core_gettable_params;
1044static OSSL_FUNC_core_get_params_fn core_get_params;
1045static OSSL_FUNC_core_thread_start_fn core_thread_start;
a829b735 1046static OSSL_FUNC_core_get_libctx_fn core_get_libctx;
f844f9eb 1047#ifndef FIPS_MODULE
363b1e5d
DMSP
1048static OSSL_FUNC_core_new_error_fn core_new_error;
1049static OSSL_FUNC_core_set_error_debug_fn core_set_error_debug;
1050static OSSL_FUNC_core_vset_error_fn core_vset_error;
1051static OSSL_FUNC_core_set_error_mark_fn core_set_error_mark;
1052static OSSL_FUNC_core_clear_last_error_mark_fn core_clear_last_error_mark;
1053static OSSL_FUNC_core_pop_error_to_mark_fn core_pop_error_to_mark;
49c64346
RL
1054#endif
1055
d40b42ab 1056static const OSSL_PARAM *core_gettable_params(const OSSL_CORE_HANDLE *handle)
4c2883a9
RL
1057{
1058 return param_types;
1059}
1060
d40b42ab 1061static int core_get_params(const OSSL_CORE_HANDLE *handle, OSSL_PARAM params[])
4c2883a9
RL
1062{
1063 int i;
4e7991b4 1064 OSSL_PARAM *p;
d40b42ab
MC
1065 /*
1066 * We created this object originally and we know it is actually an
1067 * OSSL_PROVIDER *, so the cast is safe
1068 */
1069 OSSL_PROVIDER *prov = (OSSL_PROVIDER *)handle;
4c2883a9 1070
b8086652 1071 if ((p = OSSL_PARAM_locate(params, OSSL_PROV_PARAM_CORE_VERSION)) != NULL)
ac1055ef 1072 OSSL_PARAM_set_utf8_ptr(p, OPENSSL_VERSION_STR);
b8086652 1073 if ((p = OSSL_PARAM_locate(params, OSSL_PROV_PARAM_CORE_PROV_NAME)) != NULL)
ac1055ef
RL
1074 OSSL_PARAM_set_utf8_ptr(p, prov->name);
1075
f844f9eb 1076#ifndef FIPS_MODULE
b8086652
SL
1077 if ((p = OSSL_PARAM_locate(params,
1078 OSSL_PROV_PARAM_CORE_MODULE_FILENAME)) != NULL)
25e60144
SL
1079 OSSL_PARAM_set_utf8_ptr(p, ossl_provider_module_path(prov));
1080#endif
1081
ac1055ef
RL
1082 if (prov->parameters == NULL)
1083 return 1;
1084
1085 for (i = 0; i < sk_INFOPAIR_num(prov->parameters); i++) {
1086 INFOPAIR *pair = sk_INFOPAIR_value(prov->parameters, i);
1087
1088 if ((p = OSSL_PARAM_locate(params, pair->name)) != NULL)
1089 OSSL_PARAM_set_utf8_ptr(p, pair->value);
4c2883a9 1090 }
4c2883a9
RL
1091 return 1;
1092}
1093
d40b42ab 1094static OPENSSL_CORE_CTX *core_get_libctx(const OSSL_CORE_HANDLE *handle)
e7706e63 1095{
d40b42ab
MC
1096 /*
1097 * We created this object originally and we know it is actually an
1098 * OSSL_PROVIDER *, so the cast is safe
1099 */
1100 OSSL_PROVIDER *prov = (OSSL_PROVIDER *)handle;
1101
a829b735 1102 return (OPENSSL_CORE_CTX *)ossl_provider_libctx(prov);
e7706e63
RL
1103}
1104
d40b42ab 1105static int core_thread_start(const OSSL_CORE_HANDLE *handle,
da747958
MC
1106 OSSL_thread_stop_handler_fn handfn)
1107{
d40b42ab
MC
1108 /*
1109 * We created this object originally and we know it is actually an
1110 * OSSL_PROVIDER *, so the cast is safe
1111 */
1112 OSSL_PROVIDER *prov = (OSSL_PROVIDER *)handle;
1113
6913f5fe 1114 return ossl_init_thread_start(prov, prov->provctx, handfn);
da747958
MC
1115}
1116
6592ab81
RL
1117/*
1118 * The FIPS module inner provider doesn't implement these. They aren't
1119 * needed there, since the FIPS module upcalls are always the outer provider
1120 * ones.
1121 */
f844f9eb 1122#ifndef FIPS_MODULE
49c64346 1123/*
d40b42ab 1124 * TODO(3.0) These error functions should use |handle| to select the proper
49c64346
RL
1125 * library context to report in the correct error stack, at least if error
1126 * stacks become tied to the library context.
1127 * We cannot currently do that since there's no support for it in the
1128 * ERR subsystem.
1129 */
d40b42ab 1130static void core_new_error(const OSSL_CORE_HANDLE *handle)
49c64346
RL
1131{
1132 ERR_new();
1133}
1134
d40b42ab 1135static void core_set_error_debug(const OSSL_CORE_HANDLE *handle,
49c64346
RL
1136 const char *file, int line, const char *func)
1137{
1138 ERR_set_debug(file, line, func);
1139}
1140
d40b42ab 1141static void core_vset_error(const OSSL_CORE_HANDLE *handle,
49c64346 1142 uint32_t reason, const char *fmt, va_list args)
6ebc2f56 1143{
d40b42ab
MC
1144 /*
1145 * We created this object originally and we know it is actually an
1146 * OSSL_PROVIDER *, so the cast is safe
1147 */
1148 OSSL_PROVIDER *prov = (OSSL_PROVIDER *)handle;
1149
6ebc2f56
RL
1150 /*
1151 * If the uppermost 8 bits are non-zero, it's an OpenSSL library
1152 * error and will be treated as such. Otherwise, it's a new style
1153 * provider error and will be treated as such.
1154 */
1155 if (ERR_GET_LIB(reason) != 0) {
49c64346 1156 ERR_vset_error(ERR_GET_LIB(reason), ERR_GET_REASON(reason), fmt, args);
6ebc2f56 1157 } else {
49c64346 1158 ERR_vset_error(prov->error_lib, (int)reason, fmt, args);
6ebc2f56
RL
1159 }
1160}
7b131de2 1161
d40b42ab 1162static int core_set_error_mark(const OSSL_CORE_HANDLE *handle)
7b131de2
RL
1163{
1164 return ERR_set_mark();
1165}
1166
d40b42ab 1167static int core_clear_last_error_mark(const OSSL_CORE_HANDLE *handle)
7b131de2
RL
1168{
1169 return ERR_clear_last_mark();
1170}
1171
d40b42ab 1172static int core_pop_error_to_mark(const OSSL_CORE_HANDLE *handle)
7b131de2
RL
1173{
1174 return ERR_pop_to_mark();
1175}
f844f9eb 1176#endif /* FIPS_MODULE */
6ebc2f56 1177
b60cba3c 1178/*
03bede0c 1179 * Functions provided by the core.
b60cba3c 1180 */
4c2883a9 1181static const OSSL_DISPATCH core_dispatch_[] = {
dca97d00 1182 { OSSL_FUNC_CORE_GETTABLE_PARAMS, (void (*)(void))core_gettable_params },
4c2883a9 1183 { OSSL_FUNC_CORE_GET_PARAMS, (void (*)(void))core_get_params },
a829b735 1184 { OSSL_FUNC_CORE_GET_LIBCTX, (void (*)(void))core_get_libctx },
da747958 1185 { OSSL_FUNC_CORE_THREAD_START, (void (*)(void))core_thread_start },
f844f9eb 1186#ifndef FIPS_MODULE
49c64346
RL
1187 { OSSL_FUNC_CORE_NEW_ERROR, (void (*)(void))core_new_error },
1188 { OSSL_FUNC_CORE_SET_ERROR_DEBUG, (void (*)(void))core_set_error_debug },
1189 { OSSL_FUNC_CORE_VSET_ERROR, (void (*)(void))core_vset_error },
7b131de2
RL
1190 { OSSL_FUNC_CORE_SET_ERROR_MARK, (void (*)(void))core_set_error_mark },
1191 { OSSL_FUNC_CORE_CLEAR_LAST_ERROR_MARK,
1192 (void (*)(void))core_clear_last_error_mark },
d16d0b71 1193 { OSSL_FUNC_CORE_POP_ERROR_TO_MARK, (void (*)(void))core_pop_error_to_mark },
25e60144
SL
1194 { OSSL_FUNC_BIO_NEW_FILE, (void (*)(void))BIO_new_file },
1195 { OSSL_FUNC_BIO_NEW_MEMBUF, (void (*)(void))BIO_new_mem_buf },
7bb82f92 1196 { OSSL_FUNC_BIO_READ_EX, (void (*)(void))BIO_read_ex },
d40b42ab 1197 { OSSL_FUNC_BIO_WRITE_EX, (void (*)(void))BIO_write_ex },
853ca128
RL
1198 { OSSL_FUNC_BIO_GETS, (void (*)(void))BIO_gets },
1199 { OSSL_FUNC_BIO_PUTS, (void (*)(void))BIO_puts },
63f187cf 1200 { OSSL_FUNC_BIO_CTRL, (void (*)(void))BIO_ctrl },
25e60144 1201 { OSSL_FUNC_BIO_FREE, (void (*)(void))BIO_free },
63665fff 1202 { OSSL_FUNC_BIO_VPRINTF, (void (*)(void))BIO_vprintf },
d16d0b71 1203 { OSSL_FUNC_BIO_VSNPRINTF, (void (*)(void))BIO_vsnprintf },
36fc5fc6 1204 { OSSL_FUNC_SELF_TEST_CB, (void (*)(void))OSSL_SELF_TEST_get_callback },
03bede0c
P
1205 { OSSL_FUNC_GET_ENTROPY, (void (*)(void))ossl_rand_get_entropy },
1206 { OSSL_FUNC_CLEANUP_ENTROPY, (void (*)(void))ossl_rand_cleanup_entropy },
1207 { OSSL_FUNC_GET_NONCE, (void (*)(void))ossl_rand_get_nonce },
1208 { OSSL_FUNC_CLEANUP_NONCE, (void (*)(void))ossl_rand_cleanup_nonce },
6592ab81 1209#endif
b60cba3c
RS
1210 { OSSL_FUNC_CRYPTO_MALLOC, (void (*)(void))CRYPTO_malloc },
1211 { OSSL_FUNC_CRYPTO_ZALLOC, (void (*)(void))CRYPTO_zalloc },
b60cba3c
RS
1212 { OSSL_FUNC_CRYPTO_FREE, (void (*)(void))CRYPTO_free },
1213 { OSSL_FUNC_CRYPTO_CLEAR_FREE, (void (*)(void))CRYPTO_clear_free },
1214 { OSSL_FUNC_CRYPTO_REALLOC, (void (*)(void))CRYPTO_realloc },
1215 { OSSL_FUNC_CRYPTO_CLEAR_REALLOC, (void (*)(void))CRYPTO_clear_realloc },
1216 { OSSL_FUNC_CRYPTO_SECURE_MALLOC, (void (*)(void))CRYPTO_secure_malloc },
1217 { OSSL_FUNC_CRYPTO_SECURE_ZALLOC, (void (*)(void))CRYPTO_secure_zalloc },
1218 { OSSL_FUNC_CRYPTO_SECURE_FREE, (void (*)(void))CRYPTO_secure_free },
1219 { OSSL_FUNC_CRYPTO_SECURE_CLEAR_FREE,
1220 (void (*)(void))CRYPTO_secure_clear_free },
1221 { OSSL_FUNC_CRYPTO_SECURE_ALLOCATED,
1222 (void (*)(void))CRYPTO_secure_allocated },
1223 { OSSL_FUNC_OPENSSL_CLEANSE, (void (*)(void))OPENSSL_cleanse },
b60cba3c 1224
4c2883a9
RL
1225 { 0, NULL }
1226};
1227static const OSSL_DISPATCH *core_dispatch = core_dispatch_;