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