1 /*
2 * Copyright 2019-2025 The OpenSSL Project Authors. All Rights Reserved.
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 <assert.h>
11 #include <openssl/core.h>
12 #include <openssl/core_dispatch.h>
13 #include <openssl/core_names.h>
14 #include <openssl/provider.h>
15 #include <openssl/params.h>
16 #include <openssl/opensslv.h>
17 #include "crypto/cryptlib.h"
18 #ifndef FIPS_MODULE
19 #include "crypto/decoder.h" /* ossl_decoder_store_cache_flush */
20 #include "crypto/encoder.h" /* ossl_encoder_store_cache_flush */
21 #include "crypto/store.h" /* ossl_store_loader_store_cache_flush */
22 #endif
23 #include "crypto/evp.h" /* evp_method_store_cache_flush */
24 #include "crypto/rand.h"
25 #include "internal/nelem.h"
26 #include "internal/thread_once.h"
27 #include "internal/provider.h"
28 #include "internal/refcount.h"
29 #include "internal/bio.h"
30 #include "internal/core.h"
31 #include "provider_local.h"
32 #include "crypto/context.h"
33 #ifndef FIPS_MODULE
34 # include <openssl/self_test.h>
35 # include <openssl/indicator.h>
36 #endif
37
38 /*
39 * This file defines and uses a number of different structures:
40 *
41 * OSSL_PROVIDER (provider_st): Used to represent all information related to a
42 * single instance of a provider.
43 *
44 * provider_store_st: Holds information about the collection of providers that
45 * are available within the current library context (OSSL_LIB_CTX). It also
46 * holds configuration information about providers that could be loaded at some
47 * future point.
48 *
49 * OSSL_PROVIDER_CHILD_CB: An instance of this structure holds the callbacks
50 * that have been registered for a child library context and the associated
51 * provider that registered those callbacks.
52 *
53 * Where a child library context exists then it has its own instance of the
54 * provider store. Each provider that exists in the parent provider store, has
55 * an associated child provider in the child library context's provider store.
56 * As providers get activated or deactivated this needs to be mirrored in the
57 * associated child providers.
58 *
59 * LOCKING
60 * =======
61 *
62 * There are a number of different locks used in this file and it is important
63 * to understand how they should be used in order to avoid deadlocks.
64 *
65 * Fields within a structure can often be "write once" on creation, and then
66 * "read many". Creation of a structure is done by a single thread, and
67 * therefore no lock is required for the "write once/read many" fields. It is
68 * safe for multiple threads to read these fields without a lock, because they
69 * will never be changed.
70 *
71 * However some fields may be changed after a structure has been created and
72 * shared between multiple threads. Where this is the case a lock is required.
73 *
74 * The locks available are:
75 *
76 * The provider flag_lock: Used to control updates to the various provider
77 * "flags" (flag_initialized and flag_activated).
78 *
79 * The provider activatecnt_lock: Used to control updates to the provider
80 * activatecnt 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 activatecnt_lock and the provider flag_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 activatecnt_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.
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.
118 */
119
120 static OSSL_PROVIDER *provider_new(const char *name,
121 OSSL_provider_init_fn *init_function,
122 STACK_OF(INFOPAIR) *parameters);
123
124 /*-
125 * Provider Object structure
126 * =========================
127 */
128
129 #ifndef FIPS_MODULE
130 typedef struct {
131 OSSL_PROVIDER *prov;
132 int (*create_cb)(const OSSL_CORE_HANDLE *provider, void *cbdata);
133 int (*remove_cb)(const OSSL_CORE_HANDLE *provider, void *cbdata);
134 int (*global_props_cb)(const char *props, void *cbdata);
135 void *cbdata;
136 } OSSL_PROVIDER_CHILD_CB;
137 DEFINE_STACK_OF(OSSL_PROVIDER_CHILD_CB)
138 #endif
139
140 struct provider_store_st; /* Forward declaration */
141
142 struct ossl_provider_st {
143 /* Flag bits */
144 unsigned int flag_initialized:1;
145 unsigned int flag_activated:1;
146
147 /* Getting and setting the flags require synchronization */
148 CRYPTO_RWLOCK *flag_lock;
149
150 /* OpenSSL library side data */
151 CRYPTO_REF_COUNT refcnt;
152 CRYPTO_RWLOCK *activatecnt_lock; /* For the activatecnt counter */
153 int activatecnt;
154 char *name;
155 char *path;
156 DSO *module;
157 OSSL_provider_init_fn *init_function;
158 STACK_OF(INFOPAIR) *parameters;
159 OSSL_LIB_CTX *libctx; /* The library context this instance is in */
160 struct provider_store_st *store; /* The store this instance belongs to */
161 #ifndef FIPS_MODULE
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 */
166 int error_lib; /* ERR library number, one for each provider */
167 # ifndef OPENSSL_NO_ERR
168 ERR_STRING_DATA *error_strings; /* Copy of what the provider gives us */
169 # endif
170 #endif
171
172 /* Provider side functions */
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;
177 OSSL_FUNC_provider_self_test_fn *self_test;
178 OSSL_FUNC_provider_random_bytes_fn *random_bytes;
179 OSSL_FUNC_provider_query_operation_fn *query_operation;
180 OSSL_FUNC_provider_unquery_operation_fn *unquery_operation;
181
182 /*
183 * Cache of bit to indicate of query_operation() has been called on
184 * a specific operation or not.
185 */
186 unsigned char *operation_bits;
187 size_t operation_bits_sz;
188 CRYPTO_RWLOCK *opbits_lock;
189
190 #ifndef FIPS_MODULE
191 /* Whether this provider is the child of some other provider */
192 const OSSL_CORE_HANDLE *handle;
193 unsigned int ischild:1;
194 #endif
195
196 /* Provider side data */
197 void *provctx;
198 const OSSL_DISPATCH *dispatch;
199 };
DEFINE_STACK_OF(OSSL_PROVIDER)200 DEFINE_STACK_OF(OSSL_PROVIDER)
201
202 static int ossl_provider_cmp(const OSSL_PROVIDER * const *a,
203 const OSSL_PROVIDER * const *b)
204 {
205 return strcmp((*a)->name, (*b)->name);
206 }
207
208 /*-
209 * Provider Object store
210 * =====================
211 *
212 * The Provider Object store is a library context object, and therefore needs
213 * an index.
214 */
215
216 struct provider_store_st {
217 OSSL_LIB_CTX *libctx;
218 STACK_OF(OSSL_PROVIDER) *providers;
219 STACK_OF(OSSL_PROVIDER_CHILD_CB) *child_cbs;
220 CRYPTO_RWLOCK *default_path_lock;
221 CRYPTO_RWLOCK *lock;
222 char *default_path;
223 OSSL_PROVIDER_INFO *provinfo;
224 size_t numprovinfo;
225 size_t provinfosz;
226 unsigned int use_fallbacks:1;
227 unsigned int freeing:1;
228 };
229
230 /*
231 * provider_deactivate_free() is a wrapper around ossl_provider_deactivate()
232 * and ossl_provider_free(), called as needed.
233 * Since this is only called when the provider store is being emptied, we
234 * don't need to care about any lock.
235 */
provider_deactivate_free(OSSL_PROVIDER * prov)236 static void provider_deactivate_free(OSSL_PROVIDER *prov)
237 {
238 if (prov->flag_activated)
239 ossl_provider_deactivate(prov, 1);
240 ossl_provider_free(prov);
241 }
242
243 #ifndef FIPS_MODULE
ossl_provider_child_cb_free(OSSL_PROVIDER_CHILD_CB * cb)244 static void ossl_provider_child_cb_free(OSSL_PROVIDER_CHILD_CB *cb)
245 {
246 OPENSSL_free(cb);
247 }
248 #endif
249
infopair_free(INFOPAIR * pair)250 static void infopair_free(INFOPAIR *pair)
251 {
252 OPENSSL_free(pair->name);
253 OPENSSL_free(pair->value);
254 OPENSSL_free(pair);
255 }
256
infopair_copy(const INFOPAIR * src)257 static INFOPAIR *infopair_copy(const INFOPAIR *src)
258 {
259 INFOPAIR *dest = OPENSSL_zalloc(sizeof(*dest));
260
261 if (dest == NULL)
262 return NULL;
263 if (src->name != NULL) {
264 dest->name = OPENSSL_strdup(src->name);
265 if (dest->name == NULL)
266 goto err;
267 }
268 if (src->value != NULL) {
269 dest->value = OPENSSL_strdup(src->value);
270 if (dest->value == NULL)
271 goto err;
272 }
273 return dest;
274 err:
275 OPENSSL_free(dest->name);
276 OPENSSL_free(dest);
277 return NULL;
278 }
279
ossl_provider_info_clear(OSSL_PROVIDER_INFO * info)280 void ossl_provider_info_clear(OSSL_PROVIDER_INFO *info)
281 {
282 OPENSSL_free(info->name);
283 OPENSSL_free(info->path);
284 sk_INFOPAIR_pop_free(info->parameters, infopair_free);
285 }
286
ossl_provider_store_free(void * vstore)287 void ossl_provider_store_free(void *vstore)
288 {
289 struct provider_store_st *store = vstore;
290 size_t i;
291
292 if (store == NULL)
293 return;
294 store->freeing = 1;
295 OPENSSL_free(store->default_path);
296 sk_OSSL_PROVIDER_pop_free(store->providers, provider_deactivate_free);
297 #ifndef FIPS_MODULE
298 sk_OSSL_PROVIDER_CHILD_CB_pop_free(store->child_cbs,
299 ossl_provider_child_cb_free);
300 #endif
301 CRYPTO_THREAD_lock_free(store->default_path_lock);
302 CRYPTO_THREAD_lock_free(store->lock);
303 for (i = 0; i < store->numprovinfo; i++)
304 ossl_provider_info_clear(&store->provinfo[i]);
305 OPENSSL_free(store->provinfo);
306 OPENSSL_free(store);
307 }
308
ossl_provider_store_new(OSSL_LIB_CTX * ctx)309 void *ossl_provider_store_new(OSSL_LIB_CTX *ctx)
310 {
311 struct provider_store_st *store = OPENSSL_zalloc(sizeof(*store));
312
313 if (store == NULL
314 || (store->providers = sk_OSSL_PROVIDER_new(ossl_provider_cmp)) == NULL
315 || (store->default_path_lock = CRYPTO_THREAD_lock_new()) == NULL
316 #ifndef FIPS_MODULE
317 || (store->child_cbs = sk_OSSL_PROVIDER_CHILD_CB_new_null()) == NULL
318 #endif
319 || (store->lock = CRYPTO_THREAD_lock_new()) == NULL) {
320 ossl_provider_store_free(store);
321 return NULL;
322 }
323 store->libctx = ctx;
324 store->use_fallbacks = 1;
325
326 return store;
327 }
328
get_provider_store(OSSL_LIB_CTX * libctx)329 static struct provider_store_st *get_provider_store(OSSL_LIB_CTX *libctx)
330 {
331 struct provider_store_st *store = NULL;
332
333 store = ossl_lib_ctx_get_data(libctx, OSSL_LIB_CTX_PROVIDER_STORE_INDEX);
334 if (store == NULL)
335 ERR_raise(ERR_LIB_CRYPTO, ERR_R_INTERNAL_ERROR);
336 return store;
337 }
338
ossl_provider_disable_fallback_loading(OSSL_LIB_CTX * libctx)339 int ossl_provider_disable_fallback_loading(OSSL_LIB_CTX *libctx)
340 {
341 struct provider_store_st *store;
342
343 if ((store = get_provider_store(libctx)) != NULL) {
344 if (!CRYPTO_THREAD_write_lock(store->lock))
345 return 0;
346 store->use_fallbacks = 0;
347 CRYPTO_THREAD_unlock(store->lock);
348 return 1;
349 }
350 return 0;
351 }
352
353 #define BUILTINS_BLOCK_SIZE 10
354
ossl_provider_info_add_to_store(OSSL_LIB_CTX * libctx,OSSL_PROVIDER_INFO * entry)355 int ossl_provider_info_add_to_store(OSSL_LIB_CTX *libctx,
356 OSSL_PROVIDER_INFO *entry)
357 {
358 struct provider_store_st *store = get_provider_store(libctx);
359 int ret = 0;
360
361 if (entry->name == NULL) {
362 ERR_raise(ERR_LIB_CRYPTO, ERR_R_PASSED_NULL_PARAMETER);
363 return 0;
364 }
365
366 if (store == NULL) {
367 ERR_raise(ERR_LIB_CRYPTO, ERR_R_INTERNAL_ERROR);
368 return 0;
369 }
370
371 if (!CRYPTO_THREAD_write_lock(store->lock))
372 return 0;
373 if (store->provinfosz == 0) {
374 store->provinfo = OPENSSL_zalloc(sizeof(*store->provinfo)
375 * BUILTINS_BLOCK_SIZE);
376 if (store->provinfo == NULL)
377 goto err;
378 store->provinfosz = BUILTINS_BLOCK_SIZE;
379 } else if (store->numprovinfo == store->provinfosz) {
380 OSSL_PROVIDER_INFO *tmpbuiltins;
381 size_t newsz = store->provinfosz + BUILTINS_BLOCK_SIZE;
382
383 tmpbuiltins = OPENSSL_realloc(store->provinfo,
384 sizeof(*store->provinfo) * newsz);
385 if (tmpbuiltins == NULL)
386 goto err;
387 store->provinfo = tmpbuiltins;
388 store->provinfosz = newsz;
389 }
390 store->provinfo[store->numprovinfo] = *entry;
391 store->numprovinfo++;
392
393 ret = 1;
394 err:
395 CRYPTO_THREAD_unlock(store->lock);
396 return ret;
397 }
398
ossl_provider_find(OSSL_LIB_CTX * libctx,const char * name,ossl_unused int noconfig)399 OSSL_PROVIDER *ossl_provider_find(OSSL_LIB_CTX *libctx, const char *name,
400 ossl_unused int noconfig)
401 {
402 struct provider_store_st *store = NULL;
403 OSSL_PROVIDER *prov = NULL;
404
405 if ((store = get_provider_store(libctx)) != NULL) {
406 OSSL_PROVIDER tmpl = { 0, };
407 int i;
408
409 #if !defined(FIPS_MODULE) && !defined(OPENSSL_NO_AUTOLOAD_CONFIG)
410 /*
411 * Make sure any providers are loaded from config before we try to find
412 * them.
413 */
414 if (!noconfig) {
415 if (ossl_lib_ctx_is_default(libctx))
416 OPENSSL_init_crypto(OPENSSL_INIT_LOAD_CONFIG, NULL);
417 }
418 #endif
419
420 tmpl.name = (char *)name;
421 if (!CRYPTO_THREAD_write_lock(store->lock))
422 return NULL;
423 sk_OSSL_PROVIDER_sort(store->providers);
424 if ((i = sk_OSSL_PROVIDER_find(store->providers, &tmpl)) != -1)
425 prov = sk_OSSL_PROVIDER_value(store->providers, i);
426 CRYPTO_THREAD_unlock(store->lock);
427 if (prov != NULL && !ossl_provider_up_ref(prov))
428 prov = NULL;
429 }
430
431 return prov;
432 }
433
434 /*-
435 * Provider Object methods
436 * =======================
437 */
438
provider_new(const char * name,OSSL_provider_init_fn * init_function,STACK_OF (INFOPAIR)* parameters)439 static OSSL_PROVIDER *provider_new(const char *name,
440 OSSL_provider_init_fn *init_function,
441 STACK_OF(INFOPAIR) *parameters)
442 {
443 OSSL_PROVIDER *prov = NULL;
444
445 if ((prov = OPENSSL_zalloc(sizeof(*prov))) == NULL)
446 return NULL;
447 if (!CRYPTO_NEW_REF(&prov->refcnt, 1)) {
448 OPENSSL_free(prov);
449 return NULL;
450 }
451 if ((prov->activatecnt_lock = CRYPTO_THREAD_lock_new()) == NULL) {
452 ossl_provider_free(prov);
453 ERR_raise(ERR_LIB_CRYPTO, ERR_R_CRYPTO_LIB);
454 return NULL;
455 }
456
457 if ((prov->opbits_lock = CRYPTO_THREAD_lock_new()) == NULL
458 || (prov->flag_lock = CRYPTO_THREAD_lock_new()) == NULL
459 || (prov->parameters = sk_INFOPAIR_deep_copy(parameters,
460 infopair_copy,
461 infopair_free)) == NULL) {
462 ossl_provider_free(prov);
463 ERR_raise(ERR_LIB_CRYPTO, ERR_R_CRYPTO_LIB);
464 return NULL;
465 }
466 if ((prov->name = OPENSSL_strdup(name)) == NULL) {
467 ossl_provider_free(prov);
468 return NULL;
469 }
470
471 prov->init_function = init_function;
472
473 return prov;
474 }
475
ossl_provider_up_ref(OSSL_PROVIDER * prov)476 int ossl_provider_up_ref(OSSL_PROVIDER *prov)
477 {
478 int ref = 0;
479
480 if (CRYPTO_UP_REF(&prov->refcnt, &ref) <= 0)
481 return 0;
482
483 #ifndef FIPS_MODULE
484 if (prov->ischild) {
485 if (!ossl_provider_up_ref_parent(prov, 0)) {
486 ossl_provider_free(prov);
487 return 0;
488 }
489 }
490 #endif
491
492 return ref;
493 }
494
495 #ifndef FIPS_MODULE
provider_up_ref_intern(OSSL_PROVIDER * prov,int activate)496 static int provider_up_ref_intern(OSSL_PROVIDER *prov, int activate)
497 {
498 if (activate)
499 return ossl_provider_activate(prov, 1, 0);
500
501 return ossl_provider_up_ref(prov);
502 }
503
provider_free_intern(OSSL_PROVIDER * prov,int deactivate)504 static int provider_free_intern(OSSL_PROVIDER *prov, int deactivate)
505 {
506 if (deactivate)
507 return ossl_provider_deactivate(prov, 1);
508
509 ossl_provider_free(prov);
510 return 1;
511 }
512 #endif
513
514 /*
515 * We assume that the requested provider does not already exist in the store.
516 * The caller should check. If it does exist then adding it to the store later
517 * will fail.
518 */
ossl_provider_new(OSSL_LIB_CTX * libctx,const char * name,OSSL_provider_init_fn * init_function,OSSL_PARAM * params,int noconfig)519 OSSL_PROVIDER *ossl_provider_new(OSSL_LIB_CTX *libctx, const char *name,
520 OSSL_provider_init_fn *init_function,
521 OSSL_PARAM *params, int noconfig)
522 {
523 struct provider_store_st *store = NULL;
524 OSSL_PROVIDER_INFO template;
525 OSSL_PROVIDER *prov = NULL;
526
527 if ((store = get_provider_store(libctx)) == NULL)
528 return NULL;
529
530 memset(&template, 0, sizeof(template));
531 if (init_function == NULL) {
532 const OSSL_PROVIDER_INFO *p;
533 size_t i;
534 int chosen = 0;
535
536 /* Check if this is a predefined builtin provider */
537 for (p = ossl_predefined_providers; p->name != NULL; p++) {
538 if (strcmp(p->name, name) != 0)
539 continue;
540 /* These compile-time templates always have NULL parameters */
541 template = *p;
542 chosen = 1;
543 break;
544 }
545 if (!CRYPTO_THREAD_read_lock(store->lock))
546 return NULL;
547 for (i = 0, p = store->provinfo; i < store->numprovinfo; p++, i++) {
548 if (strcmp(p->name, name) != 0)
549 continue;
550 /* For built-in providers, copy just implicit parameters. */
551 if (!chosen)
552 template = *p;
553 /*
554 * Explicit parameters override config-file defaults. If an empty
555 * parameter set is desired, a non-NULL empty set must be provided.
556 */
557 if (params != NULL || p->parameters == NULL) {
558 template.parameters = NULL;
559 break;
560 }
561 /* Always copy to avoid sharing/mutation. */
562 template.parameters = sk_INFOPAIR_deep_copy(p->parameters,
563 infopair_copy,
564 infopair_free);
565 if (template.parameters == NULL)
566 return NULL;
567 break;
568 }
569 CRYPTO_THREAD_unlock(store->lock);
570 } else {
571 template.init = init_function;
572 }
573
574 if (params != NULL) {
575 int i;
576
577 /* Don't leak if already non-NULL */
578 if (template.parameters == NULL)
579 template.parameters = sk_INFOPAIR_new_null();
580 if (template.parameters == NULL)
581 return NULL;
582
583 for (i = 0; params[i].key != NULL; i++) {
584 if (params[i].data_type != OSSL_PARAM_UTF8_STRING)
585 continue;
586 if (ossl_provider_info_add_parameter(&template, params[i].key,
587 (char *)params[i].data) <= 0) {
588 sk_INFOPAIR_pop_free(template.parameters, infopair_free);
589 return NULL;
590 }
591 }
592 }
593
594 /* provider_new() generates an error, so no need here */
595 prov = provider_new(name, template.init, template.parameters);
596
597 /* If we copied the parameters, free them */
598 if (template.parameters != NULL)
599 sk_INFOPAIR_pop_free(template.parameters, infopair_free);
600
601 if (prov == NULL)
602 return NULL;
603
604 if (!ossl_provider_set_module_path(prov, template.path)) {
605 ossl_provider_free(prov);
606 return NULL;
607 }
608
609 prov->libctx = libctx;
610 #ifndef FIPS_MODULE
611 prov->error_lib = ERR_get_next_error_library();
612 #endif
613
614 /*
615 * At this point, the provider is only partially "loaded". To be
616 * fully "loaded", ossl_provider_activate() must also be called and it must
617 * then be added to the provider store.
618 */
619
620 return prov;
621 }
622
623 /* Assumes that the store lock is held */
create_provider_children(OSSL_PROVIDER * prov)624 static int create_provider_children(OSSL_PROVIDER *prov)
625 {
626 int ret = 1;
627 #ifndef FIPS_MODULE
628 struct provider_store_st *store = prov->store;
629 OSSL_PROVIDER_CHILD_CB *child_cb;
630 int i, max;
631
632 max = sk_OSSL_PROVIDER_CHILD_CB_num(store->child_cbs);
633 for (i = 0; i < max; i++) {
634 /*
635 * This is newly activated (activatecnt == 1), so we need to
636 * create child providers as necessary.
637 */
638 child_cb = sk_OSSL_PROVIDER_CHILD_CB_value(store->child_cbs, i);
639 ret &= child_cb->create_cb((OSSL_CORE_HANDLE *)prov, child_cb->cbdata);
640 }
641 #endif
642
643 return ret;
644 }
645
ossl_provider_add_to_store(OSSL_PROVIDER * prov,OSSL_PROVIDER ** actualprov,int retain_fallbacks)646 int ossl_provider_add_to_store(OSSL_PROVIDER *prov, OSSL_PROVIDER **actualprov,
647 int retain_fallbacks)
648 {
649 struct provider_store_st *store;
650 int idx;
651 OSSL_PROVIDER tmpl = { 0, };
652 OSSL_PROVIDER *actualtmp = NULL;
653
654 if (actualprov != NULL)
655 *actualprov = NULL;
656
657 if ((store = get_provider_store(prov->libctx)) == NULL)
658 return 0;
659
660 if (!CRYPTO_THREAD_write_lock(store->lock))
661 return 0;
662
663 tmpl.name = (char *)prov->name;
664 idx = sk_OSSL_PROVIDER_find(store->providers, &tmpl);
665 if (idx == -1)
666 actualtmp = prov;
667 else
668 actualtmp = sk_OSSL_PROVIDER_value(store->providers, idx);
669
670 if (idx == -1) {
671 if (sk_OSSL_PROVIDER_push(store->providers, prov) == 0)
672 goto err;
673 prov->store = store;
674 if (!create_provider_children(prov)) {
675 sk_OSSL_PROVIDER_delete_ptr(store->providers, prov);
676 goto err;
677 }
678 if (!retain_fallbacks)
679 store->use_fallbacks = 0;
680 }
681
682 CRYPTO_THREAD_unlock(store->lock);
683
684 if (actualprov != NULL) {
685 if (!ossl_provider_up_ref(actualtmp)) {
686 ERR_raise(ERR_LIB_CRYPTO, ERR_R_CRYPTO_LIB);
687 actualtmp = NULL;
688 return 0;
689 }
690 *actualprov = actualtmp;
691 }
692
693 if (idx >= 0) {
694 /*
695 * The provider is already in the store. Probably two threads
696 * independently initialised their own provider objects with the same
697 * name and raced to put them in the store. This thread lost. We
698 * deactivate the one we just created and use the one that already
699 * exists instead.
700 * If we get here then we know we did not create provider children
701 * above, so we inform ossl_provider_deactivate not to attempt to remove
702 * any.
703 */
704 ossl_provider_deactivate(prov, 0);
705 ossl_provider_free(prov);
706 }
707 #ifndef FIPS_MODULE
708 else {
709 /*
710 * This can be done outside the lock. We tolerate other threads getting
711 * the wrong result briefly when creating OSSL_DECODER_CTXs.
712 */
713 ossl_decoder_cache_flush(prov->libctx);
714 }
715 #endif
716
717 return 1;
718
719 err:
720 CRYPTO_THREAD_unlock(store->lock);
721 return 0;
722 }
723
ossl_provider_free(OSSL_PROVIDER * prov)724 void ossl_provider_free(OSSL_PROVIDER *prov)
725 {
726 if (prov != NULL) {
727 int ref = 0;
728
729 CRYPTO_DOWN_REF(&prov->refcnt, &ref);
730
731 /*
732 * When the refcount drops to zero, we clean up the provider.
733 * Note that this also does teardown, which may seem late,
734 * considering that init happens on first activation. However,
735 * there may be other structures hanging on to the provider after
736 * the last deactivation and may therefore need full access to the
737 * provider's services. Therefore, we deinit late.
738 */
739 if (ref == 0) {
740 if (prov->flag_initialized) {
741 ossl_provider_teardown(prov);
742 #ifndef OPENSSL_NO_ERR
743 # ifndef FIPS_MODULE
744 if (prov->error_strings != NULL) {
745 ERR_unload_strings(prov->error_lib, prov->error_strings);
746 OPENSSL_free(prov->error_strings);
747 prov->error_strings = NULL;
748 }
749 # endif
750 #endif
751 OPENSSL_free(prov->operation_bits);
752 prov->operation_bits = NULL;
753 prov->operation_bits_sz = 0;
754 prov->flag_initialized = 0;
755 }
756
757 #ifndef FIPS_MODULE
758 /*
759 * We deregister thread handling whether or not the provider was
760 * initialized. If init was attempted but was not successful then
761 * the provider may still have registered a thread handler.
762 */
763 ossl_init_thread_deregister(prov);
764 DSO_free(prov->module);
765 #endif
766 OPENSSL_free(prov->name);
767 OPENSSL_free(prov->path);
768 sk_INFOPAIR_pop_free(prov->parameters, infopair_free);
769 CRYPTO_THREAD_lock_free(prov->opbits_lock);
770 CRYPTO_THREAD_lock_free(prov->flag_lock);
771 CRYPTO_THREAD_lock_free(prov->activatecnt_lock);
772 CRYPTO_FREE_REF(&prov->refcnt);
773 OPENSSL_free(prov);
774 }
775 #ifndef FIPS_MODULE
776 else if (prov->ischild) {
777 ossl_provider_free_parent(prov, 0);
778 }
779 #endif
780 }
781 }
782
783 /* Setters */
ossl_provider_set_module_path(OSSL_PROVIDER * prov,const char * module_path)784 int ossl_provider_set_module_path(OSSL_PROVIDER *prov, const char *module_path)
785 {
786 OPENSSL_free(prov->path);
787 prov->path = NULL;
788 if (module_path == NULL)
789 return 1;
790 if ((prov->path = OPENSSL_strdup(module_path)) != NULL)
791 return 1;
792 return 0;
793 }
794
infopair_add(STACK_OF (INFOPAIR)** infopairsk,const char * name,const char * value)795 static int infopair_add(STACK_OF(INFOPAIR) **infopairsk, const char *name,
796 const char *value)
797 {
798 INFOPAIR *pair = NULL;
799
800 if ((pair = OPENSSL_zalloc(sizeof(*pair))) == NULL
801 || (pair->name = OPENSSL_strdup(name)) == NULL
802 || (pair->value = OPENSSL_strdup(value)) == NULL)
803 goto err;
804
805 if ((*infopairsk == NULL
806 && (*infopairsk = sk_INFOPAIR_new_null()) == NULL)
807 || sk_INFOPAIR_push(*infopairsk, pair) <= 0) {
808 ERR_raise(ERR_LIB_CRYPTO, ERR_R_CRYPTO_LIB);
809 goto err;
810 }
811
812 return 1;
813
814 err:
815 if (pair != NULL) {
816 OPENSSL_free(pair->name);
817 OPENSSL_free(pair->value);
818 OPENSSL_free(pair);
819 }
820 return 0;
821 }
822
OSSL_PROVIDER_add_conf_parameter(OSSL_PROVIDER * prov,const char * name,const char * value)823 int OSSL_PROVIDER_add_conf_parameter(OSSL_PROVIDER *prov,
824 const char *name, const char *value)
825 {
826 return infopair_add(&prov->parameters, name, value);
827 }
828
OSSL_PROVIDER_get_conf_parameters(const OSSL_PROVIDER * prov,OSSL_PARAM params[])829 int OSSL_PROVIDER_get_conf_parameters(const OSSL_PROVIDER *prov,
830 OSSL_PARAM params[])
831 {
832 int i;
833
834 if (prov->parameters == NULL)
835 return 1;
836
837 for (i = 0; i < sk_INFOPAIR_num(prov->parameters); i++) {
838 INFOPAIR *pair = sk_INFOPAIR_value(prov->parameters, i);
839 OSSL_PARAM *p = OSSL_PARAM_locate(params, pair->name);
840
841 if (p != NULL
842 && !OSSL_PARAM_set_utf8_ptr(p, pair->value))
843 return 0;
844 }
845 return 1;
846 }
847
OSSL_PROVIDER_conf_get_bool(const OSSL_PROVIDER * prov,const char * name,int defval)848 int OSSL_PROVIDER_conf_get_bool(const OSSL_PROVIDER *prov,
849 const char *name, int defval)
850 {
851 char *val = NULL;
852 OSSL_PARAM param[2] = { OSSL_PARAM_END, OSSL_PARAM_END };
853
854 param[0].key = (char *)name;
855 param[0].data_type = OSSL_PARAM_UTF8_PTR;
856 param[0].data = (void *) &val;
857 param[0].data_size = sizeof(val);
858 param[0].return_size = OSSL_PARAM_UNMODIFIED;
859
860 /* Errors are ignored, returning the default value */
861 if (OSSL_PROVIDER_get_conf_parameters(prov, param)
862 && OSSL_PARAM_modified(param)
863 && val != NULL) {
864 if ((strcmp(val, "1") == 0)
865 || (OPENSSL_strcasecmp(val, "yes") == 0)
866 || (OPENSSL_strcasecmp(val, "true") == 0)
867 || (OPENSSL_strcasecmp(val, "on") == 0))
868 return 1;
869 else if ((strcmp(val, "0") == 0)
870 || (OPENSSL_strcasecmp(val, "no") == 0)
871 || (OPENSSL_strcasecmp(val, "false") == 0)
872 || (OPENSSL_strcasecmp(val, "off") == 0))
873 return 0;
874 }
875 return defval;
876 }
877
ossl_provider_info_add_parameter(OSSL_PROVIDER_INFO * provinfo,const char * name,const char * value)878 int ossl_provider_info_add_parameter(OSSL_PROVIDER_INFO *provinfo,
879 const char *name,
880 const char *value)
881 {
882 return infopair_add(&provinfo->parameters, name, value);
883 }
884
885 /*
886 * Provider activation.
887 *
888 * What "activation" means depends on the provider form; for built in
889 * providers (in the library or the application alike), the provider
890 * can already be considered to be loaded, all that's needed is to
891 * initialize it. However, for dynamically loadable provider modules,
892 * we must first load that module.
893 *
894 * Built in modules are distinguished from dynamically loaded modules
895 * with an already assigned init function.
896 */
897 static const OSSL_DISPATCH *core_dispatch; /* Define further down */
898
OSSL_PROVIDER_set_default_search_path(OSSL_LIB_CTX * libctx,const char * path)899 int OSSL_PROVIDER_set_default_search_path(OSSL_LIB_CTX *libctx,
900 const char *path)
901 {
902 struct provider_store_st *store;
903 char *p = NULL;
904
905 if (path != NULL) {
906 p = OPENSSL_strdup(path);
907 if (p == NULL)
908 return 0;
909 }
910 if ((store = get_provider_store(libctx)) != NULL
911 && CRYPTO_THREAD_write_lock(store->default_path_lock)) {
912 OPENSSL_free(store->default_path);
913 store->default_path = p;
914 CRYPTO_THREAD_unlock(store->default_path_lock);
915 return 1;
916 }
917 OPENSSL_free(p);
918 return 0;
919 }
920
OSSL_PROVIDER_get0_default_search_path(OSSL_LIB_CTX * libctx)921 const char *OSSL_PROVIDER_get0_default_search_path(OSSL_LIB_CTX *libctx)
922 {
923 struct provider_store_st *store;
924 char *path = NULL;
925
926 if ((store = get_provider_store(libctx)) != NULL
927 && CRYPTO_THREAD_read_lock(store->default_path_lock)) {
928 path = store->default_path;
929 CRYPTO_THREAD_unlock(store->default_path_lock);
930 }
931 return path;
932 }
933
934 /*
935 * Internal version that doesn't affect the store flags, and thereby avoid
936 * locking. Direct callers must remember to set the store flags when
937 * appropriate.
938 */
provider_init(OSSL_PROVIDER * prov)939 static int provider_init(OSSL_PROVIDER *prov)
940 {
941 const OSSL_DISPATCH *provider_dispatch = NULL;
942 void *tmp_provctx = NULL; /* safety measure */
943 #ifndef OPENSSL_NO_ERR
944 # ifndef FIPS_MODULE
945 OSSL_FUNC_provider_get_reason_strings_fn *p_get_reason_strings = NULL;
946 # endif
947 #endif
948 int ok = 0;
949
950 if (!ossl_assert(!prov->flag_initialized)) {
951 ERR_raise(ERR_LIB_CRYPTO, ERR_R_INTERNAL_ERROR);
952 goto end;
953 }
954
955 /*
956 * If the init function isn't set, it indicates that this provider is
957 * a loadable module.
958 */
959 if (prov->init_function == NULL) {
960 #ifdef FIPS_MODULE
961 goto end;
962 #else
963 if (prov->module == NULL) {
964 char *allocated_path = NULL;
965 const char *module_path = NULL;
966 char *merged_path = NULL;
967 const char *load_dir = NULL;
968 char *allocated_load_dir = NULL;
969 struct provider_store_st *store;
970
971 if ((prov->module = DSO_new()) == NULL) {
972 /* DSO_new() generates an error already */
973 goto end;
974 }
975
976 if ((store = get_provider_store(prov->libctx)) == NULL
977 || !CRYPTO_THREAD_read_lock(store->default_path_lock))
978 goto end;
979
980 if (store->default_path != NULL) {
981 allocated_load_dir = OPENSSL_strdup(store->default_path);
982 CRYPTO_THREAD_unlock(store->default_path_lock);
983 if (allocated_load_dir == NULL)
984 goto end;
985 load_dir = allocated_load_dir;
986 } else {
987 CRYPTO_THREAD_unlock(store->default_path_lock);
988 }
989
990 if (load_dir == NULL) {
991 load_dir = ossl_safe_getenv("OPENSSL_MODULES");
992 if (load_dir == NULL)
993 load_dir = ossl_get_modulesdir();
994 }
995
996 DSO_ctrl(prov->module, DSO_CTRL_SET_FLAGS,
997 DSO_FLAG_NAME_TRANSLATION_EXT_ONLY, NULL);
998
999 module_path = prov->path;
1000 if (module_path == NULL)
1001 module_path = allocated_path =
1002 DSO_convert_filename(prov->module, prov->name);
1003 if (module_path != NULL)
1004 merged_path = DSO_merge(prov->module, module_path, load_dir);
1005
1006 if (merged_path == NULL
1007 || (DSO_load(prov->module, merged_path, NULL, 0)) == NULL) {
1008 DSO_free(prov->module);
1009 prov->module = NULL;
1010 }
1011
1012 OPENSSL_free(merged_path);
1013 OPENSSL_free(allocated_path);
1014 OPENSSL_free(allocated_load_dir);
1015 }
1016
1017 if (prov->module == NULL) {
1018 /* DSO has already recorded errors, this is just a tracepoint */
1019 ERR_raise_data(ERR_LIB_CRYPTO, ERR_R_DSO_LIB,
1020 "name=%s", prov->name);
1021 goto end;
1022 }
1023
1024 prov->init_function = (OSSL_provider_init_fn *)
1025 DSO_bind_func(prov->module, "OSSL_provider_init");
1026 #endif
1027 }
1028
1029 /* Check for and call the initialise function for the provider. */
1030 if (prov->init_function == NULL) {
1031 ERR_raise_data(ERR_LIB_CRYPTO, ERR_R_UNSUPPORTED,
1032 "name=%s, provider has no provider init function",
1033 prov->name);
1034 goto end;
1035 }
1036 #ifndef FIPS_MODULE
1037 OSSL_TRACE_BEGIN(PROVIDER) {
1038 BIO_printf(trc_out,
1039 "(provider %s) initalizing\n", prov->name);
1040 } OSSL_TRACE_END(PROVIDER);
1041 #endif
1042
1043 if (!prov->init_function((OSSL_CORE_HANDLE *)prov, core_dispatch,
1044 &provider_dispatch, &tmp_provctx)) {
1045 ERR_raise_data(ERR_LIB_CRYPTO, ERR_R_INIT_FAIL,
1046 "name=%s", prov->name);
1047 goto end;
1048 }
1049 prov->provctx = tmp_provctx;
1050 prov->dispatch = provider_dispatch;
1051
1052 if (provider_dispatch != NULL) {
1053 for (; provider_dispatch->function_id != 0; provider_dispatch++) {
1054 switch (provider_dispatch->function_id) {
1055 case OSSL_FUNC_PROVIDER_TEARDOWN:
1056 prov->teardown =
1057 OSSL_FUNC_provider_teardown(provider_dispatch);
1058 break;
1059 case OSSL_FUNC_PROVIDER_GETTABLE_PARAMS:
1060 prov->gettable_params =
1061 OSSL_FUNC_provider_gettable_params(provider_dispatch);
1062 break;
1063 case OSSL_FUNC_PROVIDER_GET_PARAMS:
1064 prov->get_params =
1065 OSSL_FUNC_provider_get_params(provider_dispatch);
1066 break;
1067 case OSSL_FUNC_PROVIDER_SELF_TEST:
1068 prov->self_test =
1069 OSSL_FUNC_provider_self_test(provider_dispatch);
1070 break;
1071 case OSSL_FUNC_PROVIDER_RANDOM_BYTES:
1072 prov->random_bytes =
1073 OSSL_FUNC_provider_random_bytes(provider_dispatch);
1074 break;
1075 case OSSL_FUNC_PROVIDER_GET_CAPABILITIES:
1076 prov->get_capabilities =
1077 OSSL_FUNC_provider_get_capabilities(provider_dispatch);
1078 break;
1079 case OSSL_FUNC_PROVIDER_QUERY_OPERATION:
1080 prov->query_operation =
1081 OSSL_FUNC_provider_query_operation(provider_dispatch);
1082 break;
1083 case OSSL_FUNC_PROVIDER_UNQUERY_OPERATION:
1084 prov->unquery_operation =
1085 OSSL_FUNC_provider_unquery_operation(provider_dispatch);
1086 break;
1087 #ifndef OPENSSL_NO_ERR
1088 # ifndef FIPS_MODULE
1089 case OSSL_FUNC_PROVIDER_GET_REASON_STRINGS:
1090 p_get_reason_strings =
1091 OSSL_FUNC_provider_get_reason_strings(provider_dispatch);
1092 break;
1093 # endif
1094 #endif
1095 }
1096 }
1097 }
1098
1099 #ifndef OPENSSL_NO_ERR
1100 # ifndef FIPS_MODULE
1101 if (p_get_reason_strings != NULL) {
1102 const OSSL_ITEM *reasonstrings = p_get_reason_strings(prov->provctx);
1103 size_t cnt, cnt2;
1104
1105 /*
1106 * ERR_load_strings() handles ERR_STRING_DATA rather than OSSL_ITEM,
1107 * although they are essentially the same type.
1108 * Furthermore, ERR_load_strings() patches the array's error number
1109 * with the error library number, so we need to make a copy of that
1110 * array either way.
1111 */
1112 cnt = 0;
1113 while (reasonstrings[cnt].id != 0) {
1114 if (ERR_GET_LIB(reasonstrings[cnt].id) != 0)
1115 goto end;
1116 cnt++;
1117 }
1118 cnt++; /* One for the terminating item */
1119
1120 /* Allocate one extra item for the "library" name */
1121 prov->error_strings =
1122 OPENSSL_zalloc(sizeof(ERR_STRING_DATA) * (cnt + 1));
1123 if (prov->error_strings == NULL)
1124 goto end;
1125
1126 /*
1127 * Set the "library" name.
1128 */
1129 prov->error_strings[0].error = ERR_PACK(prov->error_lib, 0, 0);
1130 prov->error_strings[0].string = prov->name;
1131 /*
1132 * Copy reasonstrings item 0..cnt-1 to prov->error_trings positions
1133 * 1..cnt.
1134 */
1135 for (cnt2 = 1; cnt2 <= cnt; cnt2++) {
1136 prov->error_strings[cnt2].error = (int)reasonstrings[cnt2-1].id;
1137 prov->error_strings[cnt2].string = reasonstrings[cnt2-1].ptr;
1138 }
1139
1140 ERR_load_strings(prov->error_lib, prov->error_strings);
1141 }
1142 # endif
1143 #endif
1144
1145 /* With this flag set, this provider has become fully "loaded". */
1146 prov->flag_initialized = 1;
1147 ok = 1;
1148
1149 end:
1150 return ok;
1151 }
1152
1153 /*
1154 * Deactivate a provider. If upcalls is 0 then we suppress any upcalls to a
1155 * parent provider. If removechildren is 0 then we suppress any calls to remove
1156 * child providers.
1157 * Return -1 on failure and the activation count on success
1158 */
provider_deactivate(OSSL_PROVIDER * prov,int upcalls,int removechildren)1159 static int provider_deactivate(OSSL_PROVIDER *prov, int upcalls,
1160 int removechildren)
1161 {
1162 int count;
1163 struct provider_store_st *store;
1164 #ifndef FIPS_MODULE
1165 int freeparent = 0;
1166 #endif
1167 int lock = 1;
1168
1169 if (!ossl_assert(prov != NULL))
1170 return -1;
1171
1172 #ifndef FIPS_MODULE
1173 if (prov->random_bytes != NULL
1174 && !ossl_rand_check_random_provider_on_unload(prov->libctx, prov))
1175 return -1;
1176 #endif
1177
1178 /*
1179 * No need to lock if we've got no store because we've not been shared with
1180 * other threads.
1181 */
1182 store = get_provider_store(prov->libctx);
1183 if (store == NULL)
1184 lock = 0;
1185
1186 if (lock && !CRYPTO_THREAD_read_lock(store->lock))
1187 return -1;
1188 if (lock && !CRYPTO_THREAD_write_lock(prov->flag_lock)) {
1189 CRYPTO_THREAD_unlock(store->lock);
1190 return -1;
1191 }
1192
1193 if (!CRYPTO_atomic_add(&prov->activatecnt, -1, &count, prov->activatecnt_lock)) {
1194 if (lock) {
1195 CRYPTO_THREAD_unlock(prov->flag_lock);
1196 CRYPTO_THREAD_unlock(store->lock);
1197 }
1198 return -1;
1199 }
1200
1201 #ifndef FIPS_MODULE
1202 if (count >= 1 && prov->ischild && upcalls) {
1203 /*
1204 * We have had a direct activation in this child libctx so we need to
1205 * now down the ref count in the parent provider. We do the actual down
1206 * ref outside of the flag_lock, since it could involve getting other
1207 * locks.
1208 */
1209 freeparent = 1;
1210 }
1211 #endif
1212
1213 if (count < 1)
1214 prov->flag_activated = 0;
1215 #ifndef FIPS_MODULE
1216 else
1217 removechildren = 0;
1218 #endif
1219
1220 #ifndef FIPS_MODULE
1221 if (removechildren && store != NULL) {
1222 int i, max = sk_OSSL_PROVIDER_CHILD_CB_num(store->child_cbs);
1223 OSSL_PROVIDER_CHILD_CB *child_cb;
1224
1225 for (i = 0; i < max; i++) {
1226 child_cb = sk_OSSL_PROVIDER_CHILD_CB_value(store->child_cbs, i);
1227 child_cb->remove_cb((OSSL_CORE_HANDLE *)prov, child_cb->cbdata);
1228 }
1229 }
1230 #endif
1231 if (lock) {
1232 CRYPTO_THREAD_unlock(prov->flag_lock);
1233 CRYPTO_THREAD_unlock(store->lock);
1234 /*
1235 * This can be done outside the lock. We tolerate other threads getting
1236 * the wrong result briefly when creating OSSL_DECODER_CTXs.
1237 */
1238 #ifndef FIPS_MODULE
1239 if (count < 1)
1240 ossl_decoder_cache_flush(prov->libctx);
1241 #endif
1242 }
1243 #ifndef FIPS_MODULE
1244 if (freeparent)
1245 ossl_provider_free_parent(prov, 1);
1246 #endif
1247
1248 /* We don't deinit here, that's done in ossl_provider_free() */
1249 return count;
1250 }
1251
1252 /*
1253 * Activate a provider.
1254 * Return -1 on failure and the activation count on success
1255 */
provider_activate(OSSL_PROVIDER * prov,int lock,int upcalls)1256 static int provider_activate(OSSL_PROVIDER *prov, int lock, int upcalls)
1257 {
1258 int count = -1;
1259 struct provider_store_st *store;
1260 int ret = 1;
1261
1262 store = prov->store;
1263 /*
1264 * If the provider hasn't been added to the store, then we don't need
1265 * any locks because we've not shared it with other threads.
1266 */
1267 if (store == NULL) {
1268 lock = 0;
1269 if (!provider_init(prov))
1270 return -1;
1271 }
1272
1273 #ifndef FIPS_MODULE
1274 if (prov->random_bytes != NULL
1275 && !ossl_rand_check_random_provider_on_load(prov->libctx, prov))
1276 return -1;
1277
1278 if (prov->ischild && upcalls && !ossl_provider_up_ref_parent(prov, 1))
1279 return -1;
1280 #endif
1281
1282 if (lock && !CRYPTO_THREAD_read_lock(store->lock)) {
1283 #ifndef FIPS_MODULE
1284 if (prov->ischild && upcalls)
1285 ossl_provider_free_parent(prov, 1);
1286 #endif
1287 return -1;
1288 }
1289
1290 if (lock && !CRYPTO_THREAD_write_lock(prov->flag_lock)) {
1291 CRYPTO_THREAD_unlock(store->lock);
1292 #ifndef FIPS_MODULE
1293 if (prov->ischild && upcalls)
1294 ossl_provider_free_parent(prov, 1);
1295 #endif
1296 return -1;
1297 }
1298 if (CRYPTO_atomic_add(&prov->activatecnt, 1, &count, prov->activatecnt_lock)) {
1299 prov->flag_activated = 1;
1300
1301 if (count == 1 && store != NULL) {
1302 ret = create_provider_children(prov);
1303 }
1304 }
1305 if (lock) {
1306 CRYPTO_THREAD_unlock(prov->flag_lock);
1307 CRYPTO_THREAD_unlock(store->lock);
1308 /*
1309 * This can be done outside the lock. We tolerate other threads getting
1310 * the wrong result briefly when creating OSSL_DECODER_CTXs.
1311 */
1312 #ifndef FIPS_MODULE
1313 if (count == 1)
1314 ossl_decoder_cache_flush(prov->libctx);
1315 #endif
1316 }
1317
1318 if (!ret)
1319 return -1;
1320
1321 return count;
1322 }
1323
provider_flush_store_cache(const OSSL_PROVIDER * prov)1324 static int provider_flush_store_cache(const OSSL_PROVIDER *prov)
1325 {
1326 struct provider_store_st *store;
1327 int freeing;
1328
1329 if ((store = get_provider_store(prov->libctx)) == NULL)
1330 return 0;
1331
1332 if (!CRYPTO_THREAD_read_lock(store->lock))
1333 return 0;
1334 freeing = store->freeing;
1335 CRYPTO_THREAD_unlock(store->lock);
1336
1337 if (!freeing) {
1338 int acc
1339 = evp_method_store_cache_flush(prov->libctx)
1340 #ifndef FIPS_MODULE
1341 + ossl_encoder_store_cache_flush(prov->libctx)
1342 + ossl_decoder_store_cache_flush(prov->libctx)
1343 + ossl_store_loader_store_cache_flush(prov->libctx)
1344 #endif
1345 ;
1346
1347 #ifndef FIPS_MODULE
1348 return acc == 4;
1349 #else
1350 return acc == 1;
1351 #endif
1352 }
1353 return 1;
1354 }
1355
provider_remove_store_methods(OSSL_PROVIDER * prov)1356 static int provider_remove_store_methods(OSSL_PROVIDER *prov)
1357 {
1358 struct provider_store_st *store;
1359 int freeing;
1360
1361 if ((store = get_provider_store(prov->libctx)) == NULL)
1362 return 0;
1363
1364 if (!CRYPTO_THREAD_read_lock(store->lock))
1365 return 0;
1366 freeing = store->freeing;
1367 CRYPTO_THREAD_unlock(store->lock);
1368
1369 if (!freeing) {
1370 int acc;
1371
1372 if (!CRYPTO_THREAD_write_lock(prov->opbits_lock))
1373 return 0;
1374 OPENSSL_free(prov->operation_bits);
1375 prov->operation_bits = NULL;
1376 prov->operation_bits_sz = 0;
1377 CRYPTO_THREAD_unlock(prov->opbits_lock);
1378
1379 acc = evp_method_store_remove_all_provided(prov)
1380 #ifndef FIPS_MODULE
1381 + ossl_encoder_store_remove_all_provided(prov)
1382 + ossl_decoder_store_remove_all_provided(prov)
1383 + ossl_store_loader_store_remove_all_provided(prov)
1384 #endif
1385 ;
1386
1387 #ifndef FIPS_MODULE
1388 return acc == 4;
1389 #else
1390 return acc == 1;
1391 #endif
1392 }
1393 return 1;
1394 }
1395
ossl_provider_activate(OSSL_PROVIDER * prov,int upcalls,int aschild)1396 int ossl_provider_activate(OSSL_PROVIDER *prov, int upcalls, int aschild)
1397 {
1398 int count;
1399
1400 if (prov == NULL)
1401 return 0;
1402 #ifndef FIPS_MODULE
1403 /*
1404 * If aschild is true, then we only actually do the activation if the
1405 * provider is a child. If its not, this is still success.
1406 */
1407 if (aschild && !prov->ischild)
1408 return 1;
1409 #endif
1410 if ((count = provider_activate(prov, 1, upcalls)) > 0)
1411 return count == 1 ? provider_flush_store_cache(prov) : 1;
1412
1413 return 0;
1414 }
1415
ossl_provider_deactivate(OSSL_PROVIDER * prov,int removechildren)1416 int ossl_provider_deactivate(OSSL_PROVIDER *prov, int removechildren)
1417 {
1418 int count;
1419
1420 if (prov == NULL
1421 || (count = provider_deactivate(prov, 1, removechildren)) < 0)
1422 return 0;
1423 return count == 0 ? provider_remove_store_methods(prov) : 1;
1424 }
1425
ossl_provider_ctx(const OSSL_PROVIDER * prov)1426 void *ossl_provider_ctx(const OSSL_PROVIDER *prov)
1427 {
1428 return prov != NULL ? prov->provctx : NULL;
1429 }
1430
1431 /*
1432 * This function only does something once when store->use_fallbacks == 1,
1433 * and then sets store->use_fallbacks = 0, so the second call and so on is
1434 * effectively a no-op.
1435 */
provider_activate_fallbacks(struct provider_store_st * store)1436 static int provider_activate_fallbacks(struct provider_store_st *store)
1437 {
1438 int use_fallbacks;
1439 int activated_fallback_count = 0;
1440 int ret = 0;
1441 const OSSL_PROVIDER_INFO *p;
1442
1443 if (!CRYPTO_THREAD_read_lock(store->lock))
1444 return 0;
1445 use_fallbacks = store->use_fallbacks;
1446 CRYPTO_THREAD_unlock(store->lock);
1447 if (!use_fallbacks)
1448 return 1;
1449
1450 if (!CRYPTO_THREAD_write_lock(store->lock))
1451 return 0;
1452 /* Check again, just in case another thread changed it */
1453 use_fallbacks = store->use_fallbacks;
1454 if (!use_fallbacks) {
1455 CRYPTO_THREAD_unlock(store->lock);
1456 return 1;
1457 }
1458
1459 for (p = ossl_predefined_providers; p->name != NULL; p++) {
1460 OSSL_PROVIDER *prov = NULL;
1461 OSSL_PROVIDER_INFO *info = store->provinfo;
1462 STACK_OF(INFOPAIR) *params = NULL;
1463 size_t i;
1464
1465 if (!p->is_fallback)
1466 continue;
1467
1468 for (i = 0; i < store->numprovinfo; info++, i++) {
1469 if (strcmp(info->name, p->name) != 0)
1470 continue;
1471 params = info->parameters;
1472 break;
1473 }
1474
1475 /*
1476 * We use the internal constructor directly here,
1477 * otherwise we get a call loop
1478 */
1479 prov = provider_new(p->name, p->init, params);
1480 if (prov == NULL)
1481 goto err;
1482 prov->libctx = store->libctx;
1483 #ifndef FIPS_MODULE
1484 prov->error_lib = ERR_get_next_error_library();
1485 #endif
1486
1487 /*
1488 * We are calling provider_activate while holding the store lock. This
1489 * means the init function will be called while holding a lock. Normally
1490 * we try to avoid calling a user callback while holding a lock.
1491 * However, fallbacks are never third party providers so we accept this.
1492 */
1493 if (provider_activate(prov, 0, 0) < 0) {
1494 ossl_provider_free(prov);
1495 goto err;
1496 }
1497 prov->store = store;
1498 if (sk_OSSL_PROVIDER_push(store->providers, prov) == 0) {
1499 ossl_provider_free(prov);
1500 goto err;
1501 }
1502 activated_fallback_count++;
1503 }
1504
1505 if (activated_fallback_count > 0) {
1506 store->use_fallbacks = 0;
1507 ret = 1;
1508 }
1509 err:
1510 CRYPTO_THREAD_unlock(store->lock);
1511 return ret;
1512 }
1513
ossl_provider_activate_fallbacks(OSSL_LIB_CTX * ctx)1514 int ossl_provider_activate_fallbacks(OSSL_LIB_CTX *ctx)
1515 {
1516 struct provider_store_st *store = get_provider_store(ctx);
1517
1518 if (store == NULL)
1519 return 0;
1520
1521 return provider_activate_fallbacks(store);
1522 }
1523
ossl_provider_doall_activated(OSSL_LIB_CTX * ctx,int (* cb)(OSSL_PROVIDER * provider,void * cbdata),void * cbdata)1524 int ossl_provider_doall_activated(OSSL_LIB_CTX *ctx,
1525 int (*cb)(OSSL_PROVIDER *provider,
1526 void *cbdata),
1527 void *cbdata)
1528 {
1529 int ret = 0, curr, max, ref = 0;
1530 struct provider_store_st *store = get_provider_store(ctx);
1531 STACK_OF(OSSL_PROVIDER) *provs = NULL;
1532
1533 #if !defined(FIPS_MODULE) && !defined(OPENSSL_NO_AUTOLOAD_CONFIG)
1534 /*
1535 * Make sure any providers are loaded from config before we try to use
1536 * them.
1537 */
1538 if (ossl_lib_ctx_is_default(ctx))
1539 OPENSSL_init_crypto(OPENSSL_INIT_LOAD_CONFIG, NULL);
1540 #endif
1541
1542 if (store == NULL)
1543 return 1;
1544 if (!provider_activate_fallbacks(store))
1545 return 0;
1546
1547 /*
1548 * Under lock, grab a copy of the provider list and up_ref each
1549 * provider so that they don't disappear underneath us.
1550 */
1551 if (!CRYPTO_THREAD_read_lock(store->lock))
1552 return 0;
1553 provs = sk_OSSL_PROVIDER_dup(store->providers);
1554 if (provs == NULL) {
1555 CRYPTO_THREAD_unlock(store->lock);
1556 return 0;
1557 }
1558 max = sk_OSSL_PROVIDER_num(provs);
1559 /*
1560 * We work backwards through the stack so that we can safely delete items
1561 * as we go.
1562 */
1563 for (curr = max - 1; curr >= 0; curr--) {
1564 OSSL_PROVIDER *prov = sk_OSSL_PROVIDER_value(provs, curr);
1565
1566 if (!CRYPTO_THREAD_read_lock(prov->flag_lock))
1567 goto err_unlock;
1568 if (prov->flag_activated) {
1569 /*
1570 * We call CRYPTO_UP_REF directly rather than ossl_provider_up_ref
1571 * to avoid upping the ref count on the parent provider, which we
1572 * must not do while holding locks.
1573 */
1574 if (CRYPTO_UP_REF(&prov->refcnt, &ref) <= 0) {
1575 CRYPTO_THREAD_unlock(prov->flag_lock);
1576 goto err_unlock;
1577 }
1578 /*
1579 * It's already activated, but we up the activated count to ensure
1580 * it remains activated until after we've called the user callback.
1581 * In theory this could mean the parent provider goes inactive,
1582 * whilst still activated in the child for a short period. That's ok.
1583 */
1584 if (!CRYPTO_atomic_add(&prov->activatecnt, 1, &ref,
1585 prov->activatecnt_lock)) {
1586 CRYPTO_DOWN_REF(&prov->refcnt, &ref);
1587 CRYPTO_THREAD_unlock(prov->flag_lock);
1588 goto err_unlock;
1589 }
1590 } else {
1591 sk_OSSL_PROVIDER_delete(provs, curr);
1592 max--;
1593 }
1594 CRYPTO_THREAD_unlock(prov->flag_lock);
1595 }
1596 CRYPTO_THREAD_unlock(store->lock);
1597
1598 /*
1599 * Now, we sweep through all providers not under lock
1600 */
1601 for (curr = 0; curr < max; curr++) {
1602 OSSL_PROVIDER *prov = sk_OSSL_PROVIDER_value(provs, curr);
1603
1604 if (!cb(prov, cbdata)) {
1605 curr = -1;
1606 goto finish;
1607 }
1608 }
1609 curr = -1;
1610
1611 ret = 1;
1612 goto finish;
1613
1614 err_unlock:
1615 CRYPTO_THREAD_unlock(store->lock);
1616 finish:
1617 /*
1618 * The pop_free call doesn't do what we want on an error condition. We
1619 * either start from the first item in the stack, or part way through if
1620 * we only processed some of the items.
1621 */
1622 for (curr++; curr < max; curr++) {
1623 OSSL_PROVIDER *prov = sk_OSSL_PROVIDER_value(provs, curr);
1624
1625 if (!CRYPTO_atomic_add(&prov->activatecnt, -1, &ref,
1626 prov->activatecnt_lock)) {
1627 ret = 0;
1628 continue;
1629 }
1630 if (ref < 1) {
1631 /*
1632 * Looks like we need to deactivate properly. We could just have
1633 * done this originally, but it involves taking a write lock so
1634 * we avoid it. We up the count again and do a full deactivation
1635 */
1636 if (CRYPTO_atomic_add(&prov->activatecnt, 1, &ref,
1637 prov->activatecnt_lock))
1638 provider_deactivate(prov, 0, 1);
1639 else
1640 ret = 0;
1641 }
1642 /*
1643 * As above where we did the up-ref, we don't call ossl_provider_free
1644 * to avoid making upcalls. There should always be at least one ref
1645 * to the provider in the store, so this should never drop to 0.
1646 */
1647 if (!CRYPTO_DOWN_REF(&prov->refcnt, &ref)) {
1648 ret = 0;
1649 continue;
1650 }
1651 /*
1652 * Not much we can do if this assert ever fails. So we don't use
1653 * ossl_assert here.
1654 */
1655 assert(ref > 0);
1656 }
1657 sk_OSSL_PROVIDER_free(provs);
1658 return ret;
1659 }
1660
OSSL_PROVIDER_available(OSSL_LIB_CTX * libctx,const char * name)1661 int OSSL_PROVIDER_available(OSSL_LIB_CTX *libctx, const char *name)
1662 {
1663 OSSL_PROVIDER *prov = NULL;
1664 int available = 0;
1665 struct provider_store_st *store = get_provider_store(libctx);
1666
1667 if (store == NULL || !provider_activate_fallbacks(store))
1668 return 0;
1669
1670 prov = ossl_provider_find(libctx, name, 0);
1671 if (prov != NULL) {
1672 if (!CRYPTO_THREAD_read_lock(prov->flag_lock))
1673 return 0;
1674 available = prov->flag_activated;
1675 CRYPTO_THREAD_unlock(prov->flag_lock);
1676 ossl_provider_free(prov);
1677 }
1678 return available;
1679 }
1680
1681 /* Getters of Provider Object data */
ossl_provider_name(const OSSL_PROVIDER * prov)1682 const char *ossl_provider_name(const OSSL_PROVIDER *prov)
1683 {
1684 return prov->name;
1685 }
1686
ossl_provider_dso(const OSSL_PROVIDER * prov)1687 const DSO *ossl_provider_dso(const OSSL_PROVIDER *prov)
1688 {
1689 return prov->module;
1690 }
1691
ossl_provider_module_name(const OSSL_PROVIDER * prov)1692 const char *ossl_provider_module_name(const OSSL_PROVIDER *prov)
1693 {
1694 #ifdef FIPS_MODULE
1695 return NULL;
1696 #else
1697 return DSO_get_filename(prov->module);
1698 #endif
1699 }
1700
ossl_provider_module_path(const OSSL_PROVIDER * prov)1701 const char *ossl_provider_module_path(const OSSL_PROVIDER *prov)
1702 {
1703 #ifdef FIPS_MODULE
1704 return NULL;
1705 #else
1706 /* FIXME: Ensure it's a full path */
1707 return DSO_get_filename(prov->module);
1708 #endif
1709 }
1710
ossl_provider_get0_dispatch(const OSSL_PROVIDER * prov)1711 const OSSL_DISPATCH *ossl_provider_get0_dispatch(const OSSL_PROVIDER *prov)
1712 {
1713 if (prov != NULL)
1714 return prov->dispatch;
1715
1716 return NULL;
1717 }
1718
ossl_provider_libctx(const OSSL_PROVIDER * prov)1719 OSSL_LIB_CTX *ossl_provider_libctx(const OSSL_PROVIDER *prov)
1720 {
1721 return prov != NULL ? prov->libctx : NULL;
1722 }
1723
1724 /**
1725 * @brief Tears down the given provider.
1726 *
1727 * This function calls the `teardown` callback of the given provider to release
1728 * any resources associated with it. The teardown is skipped if the callback is
1729 * not defined or, in non-FIPS builds, if the provider is a child.
1730 *
1731 * @param prov Pointer to the OSSL_PROVIDER structure representing the provider.
1732 *
1733 * If tracing is enabled, a message is printed indicating that the teardown is
1734 * being called.
1735 */
ossl_provider_teardown(const OSSL_PROVIDER * prov)1736 void ossl_provider_teardown(const OSSL_PROVIDER *prov)
1737 {
1738 if (prov->teardown != NULL
1739 #ifndef FIPS_MODULE
1740 && !prov->ischild
1741 #endif
1742 ) {
1743 #ifndef FIPS_MODULE
1744 OSSL_TRACE_BEGIN(PROVIDER) {
1745 BIO_printf(trc_out, "(provider %s) calling teardown\n",
1746 ossl_provider_name(prov));
1747 } OSSL_TRACE_END(PROVIDER);
1748 #endif
1749 prov->teardown(prov->provctx);
1750 }
1751 }
1752
1753 /**
1754 * @brief Retrieves the parameters that can be obtained from a provider.
1755 *
1756 * This function calls the `gettable_params` callback of the given provider to
1757 * get a list of parameters that can be retrieved.
1758 *
1759 * @param prov Pointer to the OSSL_PROVIDER structure representing the provider.
1760 *
1761 * @return Pointer to an array of OSSL_PARAM structures that represent the
1762 * gettable parameters, or NULL if the callback is not defined.
1763 *
1764 * If tracing is enabled, the gettable parameters are printed for debugging.
1765 */
ossl_provider_gettable_params(const OSSL_PROVIDER * prov)1766 const OSSL_PARAM *ossl_provider_gettable_params(const OSSL_PROVIDER *prov)
1767 {
1768 const OSSL_PARAM *ret = NULL;
1769
1770 if (prov->gettable_params != NULL)
1771 ret = prov->gettable_params(prov->provctx);
1772
1773 #ifndef FIPS_MODULE
1774 OSSL_TRACE_BEGIN(PROVIDER) {
1775 char *buf = NULL;
1776
1777 BIO_printf(trc_out, "(provider %s) gettable params\n",
1778 ossl_provider_name(prov));
1779 BIO_printf(trc_out, "Parameters:\n");
1780 if (prov->gettable_params != NULL) {
1781 if (!OSSL_PARAM_print_to_bio(ret, trc_out, 0))
1782 BIO_printf(trc_out, "Failed to parse param values\n");
1783 OPENSSL_free(buf);
1784 } else {
1785 BIO_printf(trc_out, "Provider doesn't implement gettable_params\n");
1786 }
1787 } OSSL_TRACE_END(PROVIDER);
1788 #endif
1789
1790 return ret;
1791 }
1792
1793 /**
1794 * @brief Retrieves parameters from a provider.
1795 *
1796 * This function calls the `get_params` callback of the given provider to
1797 * retrieve its parameters. If the callback is defined, it is invoked with the
1798 * provider context and the parameters array.
1799 *
1800 * @param prov Pointer to the OSSL_PROVIDER structure representing the provider.
1801 * @param params Array of OSSL_PARAM structures to store the retrieved parameters.
1802 *
1803 * @return 1 on success, 0 if the `get_params` callback is not defined or fails.
1804 *
1805 * If tracing is enabled, the retrieved parameters are printed for debugging.
1806 */
ossl_provider_get_params(const OSSL_PROVIDER * prov,OSSL_PARAM params[])1807 int ossl_provider_get_params(const OSSL_PROVIDER *prov, OSSL_PARAM params[])
1808 {
1809 int ret;
1810
1811 if (prov->get_params == NULL)
1812 return 0;
1813
1814 ret = prov->get_params(prov->provctx, params);
1815 #ifndef FIPS_MODULE
1816 OSSL_TRACE_BEGIN(PROVIDER) {
1817
1818 BIO_printf(trc_out,
1819 "(provider %s) calling get_params\n", prov->name);
1820 if (ret == 1) {
1821 BIO_printf(trc_out, "Parameters:\n");
1822 if (!OSSL_PARAM_print_to_bio(params, trc_out, 1))
1823 BIO_printf(trc_out, "Failed to parse param values\n");
1824 } else {
1825 BIO_printf(trc_out, "get_params call failed\n");
1826 }
1827 } OSSL_TRACE_END(PROVIDER);
1828 #endif
1829 return ret;
1830 }
1831
1832 /**
1833 * @brief Performs a self-test on the given provider.
1834 *
1835 * This function calls the `self_test` callback of the given provider to
1836 * perform a self-test. If the callback is not defined, it assumes the test
1837 * passed.
1838 *
1839 * @param prov Pointer to the OSSL_PROVIDER structure representing the provider.
1840 *
1841 * @return 1 if the self-test passes or the callback is not defined, 0 on failure.
1842 *
1843 * If tracing is enabled, the result of the self-test is printed for debugging.
1844 * If the test fails, the provider's store methods are removed.
1845 */
ossl_provider_self_test(const OSSL_PROVIDER * prov)1846 int ossl_provider_self_test(const OSSL_PROVIDER *prov)
1847 {
1848 int ret = 1;
1849
1850 if (prov->self_test != NULL)
1851 ret = prov->self_test(prov->provctx);
1852
1853 #ifndef FIPS_MODULE
1854 OSSL_TRACE_BEGIN(PROVIDER) {
1855 if (prov->self_test != NULL)
1856 BIO_printf(trc_out,
1857 "(provider %s) Calling self_test, ret = %d\n",
1858 prov->name, ret);
1859 else
1860 BIO_printf(trc_out,
1861 "(provider %s) doesn't implement self_test\n",
1862 prov->name);
1863 } OSSL_TRACE_END(PROVIDER);
1864 #endif
1865 if (ret == 0)
1866 (void)provider_remove_store_methods((OSSL_PROVIDER *)prov);
1867 return ret;
1868 }
1869
1870 /**
1871 * @brief Retrieves capabilities from the given provider.
1872 *
1873 * This function calls the `get_capabilities` callback of the specified provider
1874 * to retrieve capabilities information. The callback is invoked with the
1875 * provider context, capability name, a callback function, and an argument.
1876 *
1877 * @param prov Pointer to the OSSL_PROVIDER structure representing the provider.
1878 * @param capability String representing the capability to be retrieved.
1879 * @param cb Callback function to process the capability data.
1880 * @param arg Argument to be passed to the callback function.
1881 *
1882 * @return 1 if the capabilities are successfully retrieved or if the callback
1883 * is not defined, otherwise the value returned by `get_capabilities`.
1884 *
1885 * If tracing is enabled, a message is printed indicating the requested
1886 * capabilities.
1887 */
ossl_provider_random_bytes(const OSSL_PROVIDER * prov,int which,void * buf,size_t n,unsigned int strength)1888 int ossl_provider_random_bytes(const OSSL_PROVIDER *prov, int which,
1889 void *buf, size_t n, unsigned int strength)
1890 {
1891 return prov->random_bytes == NULL ? 0
1892 : prov->random_bytes(prov->provctx, which,
1893 buf, n, strength);
1894 }
1895
ossl_provider_get_capabilities(const OSSL_PROVIDER * prov,const char * capability,OSSL_CALLBACK * cb,void * arg)1896 int ossl_provider_get_capabilities(const OSSL_PROVIDER *prov,
1897 const char *capability,
1898 OSSL_CALLBACK *cb,
1899 void *arg)
1900 {
1901 if (prov->get_capabilities != NULL) {
1902 #ifndef FIPS_MODULE
1903 OSSL_TRACE_BEGIN(PROVIDER) {
1904 BIO_printf(trc_out,
1905 "(provider %s) Calling get_capabilities "
1906 "with capabilities %s\n", prov->name,
1907 capability == NULL ? "none" : capability);
1908 } OSSL_TRACE_END(PROVIDER);
1909 #endif
1910 return prov->get_capabilities(prov->provctx, capability, cb, arg);
1911 }
1912 return 1;
1913 }
1914
1915 /**
1916 * @brief Queries the provider for available algorithms for a given operation.
1917 *
1918 * This function calls the `query_operation` callback of the specified provider
1919 * to obtain a list of algorithms that can perform the given operation. It may
1920 * also set a flag indicating whether the result should be cached.
1921 *
1922 * @param prov Pointer to the OSSL_PROVIDER structure representing the provider.
1923 * @param operation_id Identifier of the operation to query.
1924 * @param no_cache Pointer to an integer flag to indicate whether caching is allowed.
1925 *
1926 * @return Pointer to an array of OSSL_ALGORITHM structures representing the
1927 * available algorithms, or NULL if the callback is not defined or
1928 * there are no available algorithms.
1929 *
1930 * If tracing is enabled, the available algorithms and their properties are
1931 * printed for debugging.
1932 */
ossl_provider_query_operation(const OSSL_PROVIDER * prov,int operation_id,int * no_cache)1933 const OSSL_ALGORITHM *ossl_provider_query_operation(const OSSL_PROVIDER *prov,
1934 int operation_id,
1935 int *no_cache)
1936 {
1937 const OSSL_ALGORITHM *res;
1938
1939 if (prov->query_operation == NULL) {
1940 #ifndef FIPS_MODULE
1941 OSSL_TRACE_BEGIN(PROVIDER) {
1942 BIO_printf(trc_out, "provider %s lacks query operation!\n",
1943 prov->name);
1944 } OSSL_TRACE_END(PROVIDER);
1945 #endif
1946 return NULL;
1947 }
1948
1949 res = prov->query_operation(prov->provctx, operation_id, no_cache);
1950 #ifndef FIPS_MODULE
1951 OSSL_TRACE_BEGIN(PROVIDER) {
1952 const OSSL_ALGORITHM *idx;
1953 if (res != NULL) {
1954 BIO_printf(trc_out,
1955 "(provider %s) Calling query, available algs are:\n", prov->name);
1956
1957 for (idx = res; idx->algorithm_names != NULL; idx++) {
1958 BIO_printf(trc_out,
1959 "(provider %s) names %s, prop_def %s, desc %s\n",
1960 prov->name,
1961 idx->algorithm_names == NULL ? "none" :
1962 idx->algorithm_names,
1963 idx->property_definition == NULL ? "none" :
1964 idx->property_definition,
1965 idx->algorithm_description == NULL ? "none" :
1966 idx->algorithm_description);
1967 }
1968 } else {
1969 BIO_printf(trc_out, "(provider %s) query_operation failed\n", prov->name);
1970 }
1971 } OSSL_TRACE_END(PROVIDER);
1972 #endif
1973
1974 #if defined(OPENSSL_NO_CACHED_FETCH)
1975 /* Forcing the non-caching of queries */
1976 if (no_cache != NULL)
1977 *no_cache = 1;
1978 #endif
1979 return res;
1980 }
1981
1982 /**
1983 * @brief Releases resources associated with a queried operation.
1984 *
1985 * This function calls the `unquery_operation` callback of the specified
1986 * provider to release any resources related to a previously queried operation.
1987 *
1988 * @param prov Pointer to the OSSL_PROVIDER structure representing the provider.
1989 * @param operation_id Identifier of the operation to unquery.
1990 * @param algs Pointer to the OSSL_ALGORITHM structures representing the
1991 * algorithms associated with the operation.
1992 *
1993 * If tracing is enabled, a message is printed indicating that the operation
1994 * is being unqueried.
1995 */
ossl_provider_unquery_operation(const OSSL_PROVIDER * prov,int operation_id,const OSSL_ALGORITHM * algs)1996 void ossl_provider_unquery_operation(const OSSL_PROVIDER *prov,
1997 int operation_id,
1998 const OSSL_ALGORITHM *algs)
1999 {
2000 if (prov->unquery_operation != NULL) {
2001 #ifndef FIPS_MODULE
2002 OSSL_TRACE_BEGIN(PROVIDER) {
2003 BIO_printf(trc_out,
2004 "(provider %s) Calling unquery"
2005 " with operation %d\n",
2006 prov->name,
2007 operation_id);
2008 } OSSL_TRACE_END(PROVIDER);
2009 #endif
2010 prov->unquery_operation(prov->provctx, operation_id, algs);
2011 }
2012 }
2013
ossl_provider_set_operation_bit(OSSL_PROVIDER * provider,size_t bitnum)2014 int ossl_provider_set_operation_bit(OSSL_PROVIDER *provider, size_t bitnum)
2015 {
2016 size_t byte = bitnum / 8;
2017 unsigned char bit = (1 << (bitnum % 8)) & 0xFF;
2018
2019 if (!CRYPTO_THREAD_write_lock(provider->opbits_lock))
2020 return 0;
2021 if (provider->operation_bits_sz <= byte) {
2022 unsigned char *tmp = OPENSSL_realloc(provider->operation_bits,
2023 byte + 1);
2024
2025 if (tmp == NULL) {
2026 CRYPTO_THREAD_unlock(provider->opbits_lock);
2027 return 0;
2028 }
2029 provider->operation_bits = tmp;
2030 memset(provider->operation_bits + provider->operation_bits_sz,
2031 '\0', byte + 1 - provider->operation_bits_sz);
2032 provider->operation_bits_sz = byte + 1;
2033 }
2034 provider->operation_bits[byte] |= bit;
2035 CRYPTO_THREAD_unlock(provider->opbits_lock);
2036 return 1;
2037 }
2038
ossl_provider_test_operation_bit(OSSL_PROVIDER * provider,size_t bitnum,int * result)2039 int ossl_provider_test_operation_bit(OSSL_PROVIDER *provider, size_t bitnum,
2040 int *result)
2041 {
2042 size_t byte = bitnum / 8;
2043 unsigned char bit = (1 << (bitnum % 8)) & 0xFF;
2044
2045 if (!ossl_assert(result != NULL)) {
2046 ERR_raise(ERR_LIB_CRYPTO, ERR_R_PASSED_NULL_PARAMETER);
2047 return 0;
2048 }
2049
2050 *result = 0;
2051 if (!CRYPTO_THREAD_read_lock(provider->opbits_lock))
2052 return 0;
2053 if (provider->operation_bits_sz > byte)
2054 *result = ((provider->operation_bits[byte] & bit) != 0);
2055 CRYPTO_THREAD_unlock(provider->opbits_lock);
2056 return 1;
2057 }
2058
2059 #ifndef FIPS_MODULE
ossl_provider_get_parent(OSSL_PROVIDER * prov)2060 const OSSL_CORE_HANDLE *ossl_provider_get_parent(OSSL_PROVIDER *prov)
2061 {
2062 return prov->handle;
2063 }
2064
ossl_provider_is_child(const OSSL_PROVIDER * prov)2065 int ossl_provider_is_child(const OSSL_PROVIDER *prov)
2066 {
2067 return prov->ischild;
2068 }
2069
ossl_provider_set_child(OSSL_PROVIDER * prov,const OSSL_CORE_HANDLE * handle)2070 int ossl_provider_set_child(OSSL_PROVIDER *prov, const OSSL_CORE_HANDLE *handle)
2071 {
2072 prov->handle = handle;
2073 prov->ischild = 1;
2074
2075 return 1;
2076 }
2077
ossl_provider_default_props_update(OSSL_LIB_CTX * libctx,const char * props)2078 int ossl_provider_default_props_update(OSSL_LIB_CTX *libctx, const char *props)
2079 {
2080 #ifndef FIPS_MODULE
2081 struct provider_store_st *store = NULL;
2082 int i, max;
2083 OSSL_PROVIDER_CHILD_CB *child_cb;
2084
2085 if ((store = get_provider_store(libctx)) == NULL)
2086 return 0;
2087
2088 if (!CRYPTO_THREAD_read_lock(store->lock))
2089 return 0;
2090
2091 max = sk_OSSL_PROVIDER_CHILD_CB_num(store->child_cbs);
2092 for (i = 0; i < max; i++) {
2093 child_cb = sk_OSSL_PROVIDER_CHILD_CB_value(store->child_cbs, i);
2094 child_cb->global_props_cb(props, child_cb->cbdata);
2095 }
2096
2097 CRYPTO_THREAD_unlock(store->lock);
2098 #endif
2099 return 1;
2100 }
2101
ossl_provider_register_child_cb(const OSSL_CORE_HANDLE * handle,int (* create_cb)(const OSSL_CORE_HANDLE * provider,void * cbdata),int (* remove_cb)(const OSSL_CORE_HANDLE * provider,void * cbdata),int (* global_props_cb)(const char * props,void * cbdata),void * cbdata)2102 static int ossl_provider_register_child_cb(const OSSL_CORE_HANDLE *handle,
2103 int (*create_cb)(
2104 const OSSL_CORE_HANDLE *provider,
2105 void *cbdata),
2106 int (*remove_cb)(
2107 const OSSL_CORE_HANDLE *provider,
2108 void *cbdata),
2109 int (*global_props_cb)(
2110 const char *props,
2111 void *cbdata),
2112 void *cbdata)
2113 {
2114 /*
2115 * This is really an OSSL_PROVIDER that we created and cast to
2116 * OSSL_CORE_HANDLE originally. Therefore it is safe to cast it back.
2117 */
2118 OSSL_PROVIDER *thisprov = (OSSL_PROVIDER *)handle;
2119 OSSL_PROVIDER *prov;
2120 OSSL_LIB_CTX *libctx = thisprov->libctx;
2121 struct provider_store_st *store = NULL;
2122 int ret = 0, i, max;
2123 OSSL_PROVIDER_CHILD_CB *child_cb;
2124 char *propsstr = NULL;
2125
2126 if ((store = get_provider_store(libctx)) == NULL)
2127 return 0;
2128
2129 child_cb = OPENSSL_malloc(sizeof(*child_cb));
2130 if (child_cb == NULL)
2131 return 0;
2132 child_cb->prov = thisprov;
2133 child_cb->create_cb = create_cb;
2134 child_cb->remove_cb = remove_cb;
2135 child_cb->global_props_cb = global_props_cb;
2136 child_cb->cbdata = cbdata;
2137
2138 if (!CRYPTO_THREAD_write_lock(store->lock)) {
2139 OPENSSL_free(child_cb);
2140 return 0;
2141 }
2142 propsstr = evp_get_global_properties_str(libctx, 0);
2143
2144 if (propsstr != NULL) {
2145 global_props_cb(propsstr, cbdata);
2146 OPENSSL_free(propsstr);
2147 }
2148 max = sk_OSSL_PROVIDER_num(store->providers);
2149 for (i = 0; i < max; i++) {
2150 int activated;
2151
2152 prov = sk_OSSL_PROVIDER_value(store->providers, i);
2153
2154 if (!CRYPTO_THREAD_read_lock(prov->flag_lock))
2155 break;
2156 activated = prov->flag_activated;
2157 CRYPTO_THREAD_unlock(prov->flag_lock);
2158 /*
2159 * We hold the store lock while calling the user callback. This means
2160 * that the user callback must be short and simple and not do anything
2161 * likely to cause a deadlock. We don't hold the flag_lock during this
2162 * call. In theory this means that another thread could deactivate it
2163 * while we are calling create. This is ok because the other thread
2164 * will also call remove_cb, but won't be able to do so until we release
2165 * the store lock.
2166 */
2167 if (activated && !create_cb((OSSL_CORE_HANDLE *)prov, cbdata))
2168 break;
2169 }
2170 if (i == max) {
2171 /* Success */
2172 ret = sk_OSSL_PROVIDER_CHILD_CB_push(store->child_cbs, child_cb);
2173 }
2174 if (i != max || ret <= 0) {
2175 /* Failed during creation. Remove everything we just added */
2176 for (; i >= 0; i--) {
2177 prov = sk_OSSL_PROVIDER_value(store->providers, i);
2178 remove_cb((OSSL_CORE_HANDLE *)prov, cbdata);
2179 }
2180 OPENSSL_free(child_cb);
2181 ret = 0;
2182 }
2183 CRYPTO_THREAD_unlock(store->lock);
2184
2185 return ret;
2186 }
2187
ossl_provider_deregister_child_cb(const OSSL_CORE_HANDLE * handle)2188 static void ossl_provider_deregister_child_cb(const OSSL_CORE_HANDLE *handle)
2189 {
2190 /*
2191 * This is really an OSSL_PROVIDER that we created and cast to
2192 * OSSL_CORE_HANDLE originally. Therefore it is safe to cast it back.
2193 */
2194 OSSL_PROVIDER *thisprov = (OSSL_PROVIDER *)handle;
2195 OSSL_LIB_CTX *libctx = thisprov->libctx;
2196 struct provider_store_st *store = NULL;
2197 int i, max;
2198 OSSL_PROVIDER_CHILD_CB *child_cb;
2199
2200 if ((store = get_provider_store(libctx)) == NULL)
2201 return;
2202
2203 if (!CRYPTO_THREAD_write_lock(store->lock))
2204 return;
2205 max = sk_OSSL_PROVIDER_CHILD_CB_num(store->child_cbs);
2206 for (i = 0; i < max; i++) {
2207 child_cb = sk_OSSL_PROVIDER_CHILD_CB_value(store->child_cbs, i);
2208 if (child_cb->prov == thisprov) {
2209 /* Found an entry */
2210 sk_OSSL_PROVIDER_CHILD_CB_delete(store->child_cbs, i);
2211 OPENSSL_free(child_cb);
2212 break;
2213 }
2214 }
2215 CRYPTO_THREAD_unlock(store->lock);
2216 }
2217 #endif
2218
2219 /*-
2220 * Core functions for the provider
2221 * ===============================
2222 *
2223 * This is the set of functions that the core makes available to the provider
2224 */
2225
2226 /*
2227 * This returns a list of Provider Object parameters with their types, for
2228 * discovery. We do not expect that many providers will use this, but one
2229 * never knows.
2230 */
2231 static const OSSL_PARAM param_types[] = {
2232 OSSL_PARAM_DEFN(OSSL_PROV_PARAM_CORE_VERSION, OSSL_PARAM_UTF8_PTR, NULL, 0),
2233 OSSL_PARAM_DEFN(OSSL_PROV_PARAM_CORE_PROV_NAME, OSSL_PARAM_UTF8_PTR,
2234 NULL, 0),
2235 #ifndef FIPS_MODULE
2236 OSSL_PARAM_DEFN(OSSL_PROV_PARAM_CORE_MODULE_FILENAME, OSSL_PARAM_UTF8_PTR,
2237 NULL, 0),
2238 #endif
2239 OSSL_PARAM_END
2240 };
2241
2242 /*
2243 * Forward declare all the functions that are provided aa dispatch.
2244 * This ensures that the compiler will complain if they aren't defined
2245 * with the correct signature.
2246 */
2247 static OSSL_FUNC_core_gettable_params_fn core_gettable_params;
2248 static OSSL_FUNC_core_get_params_fn core_get_params;
2249 static OSSL_FUNC_core_get_libctx_fn core_get_libctx;
2250 static OSSL_FUNC_core_thread_start_fn core_thread_start;
2251 #ifndef FIPS_MODULE
2252 static OSSL_FUNC_core_new_error_fn core_new_error;
2253 static OSSL_FUNC_core_set_error_debug_fn core_set_error_debug;
2254 static OSSL_FUNC_core_vset_error_fn core_vset_error;
2255 static OSSL_FUNC_core_set_error_mark_fn core_set_error_mark;
2256 static OSSL_FUNC_core_clear_last_error_mark_fn core_clear_last_error_mark;
2257 static OSSL_FUNC_core_pop_error_to_mark_fn core_pop_error_to_mark;
2258 OSSL_FUNC_BIO_new_file_fn ossl_core_bio_new_file;
2259 OSSL_FUNC_BIO_new_membuf_fn ossl_core_bio_new_mem_buf;
2260 OSSL_FUNC_BIO_read_ex_fn ossl_core_bio_read_ex;
2261 OSSL_FUNC_BIO_write_ex_fn ossl_core_bio_write_ex;
2262 OSSL_FUNC_BIO_gets_fn ossl_core_bio_gets;
2263 OSSL_FUNC_BIO_puts_fn ossl_core_bio_puts;
2264 OSSL_FUNC_BIO_up_ref_fn ossl_core_bio_up_ref;
2265 OSSL_FUNC_BIO_free_fn ossl_core_bio_free;
2266 OSSL_FUNC_BIO_vprintf_fn ossl_core_bio_vprintf;
2267 OSSL_FUNC_BIO_vsnprintf_fn BIO_vsnprintf;
2268 static OSSL_FUNC_indicator_cb_fn core_indicator_get_callback;
2269 static OSSL_FUNC_self_test_cb_fn core_self_test_get_callback;
2270 static OSSL_FUNC_get_entropy_fn rand_get_entropy;
2271 static OSSL_FUNC_get_user_entropy_fn rand_get_user_entropy;
2272 static OSSL_FUNC_cleanup_entropy_fn rand_cleanup_entropy;
2273 static OSSL_FUNC_cleanup_user_entropy_fn rand_cleanup_user_entropy;
2274 static OSSL_FUNC_get_nonce_fn rand_get_nonce;
2275 static OSSL_FUNC_get_user_nonce_fn rand_get_user_nonce;
2276 static OSSL_FUNC_cleanup_nonce_fn rand_cleanup_nonce;
2277 static OSSL_FUNC_cleanup_user_nonce_fn rand_cleanup_user_nonce;
2278 #endif
2279 OSSL_FUNC_CRYPTO_malloc_fn CRYPTO_malloc;
2280 OSSL_FUNC_CRYPTO_zalloc_fn CRYPTO_zalloc;
2281 OSSL_FUNC_CRYPTO_free_fn CRYPTO_free;
2282 OSSL_FUNC_CRYPTO_clear_free_fn CRYPTO_clear_free;
2283 OSSL_FUNC_CRYPTO_realloc_fn CRYPTO_realloc;
2284 OSSL_FUNC_CRYPTO_clear_realloc_fn CRYPTO_clear_realloc;
2285 OSSL_FUNC_CRYPTO_secure_malloc_fn CRYPTO_secure_malloc;
2286 OSSL_FUNC_CRYPTO_secure_zalloc_fn CRYPTO_secure_zalloc;
2287 OSSL_FUNC_CRYPTO_secure_free_fn CRYPTO_secure_free;
2288 OSSL_FUNC_CRYPTO_secure_clear_free_fn CRYPTO_secure_clear_free;
2289 OSSL_FUNC_CRYPTO_secure_allocated_fn CRYPTO_secure_allocated;
2290 OSSL_FUNC_OPENSSL_cleanse_fn OPENSSL_cleanse;
2291 #ifndef FIPS_MODULE
2292 OSSL_FUNC_provider_register_child_cb_fn ossl_provider_register_child_cb;
2293 OSSL_FUNC_provider_deregister_child_cb_fn ossl_provider_deregister_child_cb;
2294 static OSSL_FUNC_provider_name_fn core_provider_get0_name;
2295 static OSSL_FUNC_provider_get0_provider_ctx_fn core_provider_get0_provider_ctx;
2296 static OSSL_FUNC_provider_get0_dispatch_fn core_provider_get0_dispatch;
2297 static OSSL_FUNC_provider_up_ref_fn core_provider_up_ref_intern;
2298 static OSSL_FUNC_provider_free_fn core_provider_free_intern;
2299 static OSSL_FUNC_core_obj_add_sigid_fn core_obj_add_sigid;
2300 static OSSL_FUNC_core_obj_create_fn core_obj_create;
2301 #endif
2302
core_gettable_params(const OSSL_CORE_HANDLE * handle)2303 static const OSSL_PARAM *core_gettable_params(const OSSL_CORE_HANDLE *handle)
2304 {
2305 return param_types;
2306 }
2307
core_get_params(const OSSL_CORE_HANDLE * handle,OSSL_PARAM params[])2308 static int core_get_params(const OSSL_CORE_HANDLE *handle, OSSL_PARAM params[])
2309 {
2310 OSSL_PARAM *p;
2311 /*
2312 * We created this object originally and we know it is actually an
2313 * OSSL_PROVIDER *, so the cast is safe
2314 */
2315 OSSL_PROVIDER *prov = (OSSL_PROVIDER *)handle;
2316
2317 if ((p = OSSL_PARAM_locate(params, OSSL_PROV_PARAM_CORE_VERSION)) != NULL)
2318 OSSL_PARAM_set_utf8_ptr(p, OPENSSL_VERSION_STR);
2319 if ((p = OSSL_PARAM_locate(params, OSSL_PROV_PARAM_CORE_PROV_NAME)) != NULL)
2320 OSSL_PARAM_set_utf8_ptr(p, prov->name);
2321
2322 #ifndef FIPS_MODULE
2323 if ((p = OSSL_PARAM_locate(params,
2324 OSSL_PROV_PARAM_CORE_MODULE_FILENAME)) != NULL)
2325 OSSL_PARAM_set_utf8_ptr(p, ossl_provider_module_path(prov));
2326 #endif
2327
2328 return OSSL_PROVIDER_get_conf_parameters(prov, params);
2329 }
2330
core_get_libctx(const OSSL_CORE_HANDLE * handle)2331 static OPENSSL_CORE_CTX *core_get_libctx(const OSSL_CORE_HANDLE *handle)
2332 {
2333 /*
2334 * We created this object originally and we know it is actually an
2335 * OSSL_PROVIDER *, so the cast is safe
2336 */
2337 OSSL_PROVIDER *prov = (OSSL_PROVIDER *)handle;
2338
2339 /*
2340 * Using ossl_provider_libctx would be wrong as that returns
2341 * NULL for |prov| == NULL and NULL libctx has a special meaning
2342 * that does not apply here. Here |prov| == NULL can happen only in
2343 * case of a coding error.
2344 */
2345 assert(prov != NULL);
2346 return (OPENSSL_CORE_CTX *)prov->libctx;
2347 }
2348
core_thread_start(const OSSL_CORE_HANDLE * handle,OSSL_thread_stop_handler_fn handfn,void * arg)2349 static int core_thread_start(const OSSL_CORE_HANDLE *handle,
2350 OSSL_thread_stop_handler_fn handfn,
2351 void *arg)
2352 {
2353 /*
2354 * We created this object originally and we know it is actually an
2355 * OSSL_PROVIDER *, so the cast is safe
2356 */
2357 OSSL_PROVIDER *prov = (OSSL_PROVIDER *)handle;
2358
2359 return ossl_init_thread_start(prov, arg, handfn);
2360 }
2361
2362 /*
2363 * The FIPS module inner provider doesn't implement these. They aren't
2364 * needed there, since the FIPS module upcalls are always the outer provider
2365 * ones.
2366 */
2367 #ifndef FIPS_MODULE
2368 /*
2369 * These error functions should use |handle| to select the proper
2370 * library context to report in the correct error stack if error
2371 * stacks become tied to the library context.
2372 * We cannot currently do that since there's no support for it in the
2373 * ERR subsystem.
2374 */
core_new_error(const OSSL_CORE_HANDLE * handle)2375 static void core_new_error(const OSSL_CORE_HANDLE *handle)
2376 {
2377 ERR_new();
2378 }
2379
core_set_error_debug(const OSSL_CORE_HANDLE * handle,const char * file,int line,const char * func)2380 static void core_set_error_debug(const OSSL_CORE_HANDLE *handle,
2381 const char *file, int line, const char *func)
2382 {
2383 ERR_set_debug(file, line, func);
2384 }
2385
core_vset_error(const OSSL_CORE_HANDLE * handle,uint32_t reason,const char * fmt,va_list args)2386 static void core_vset_error(const OSSL_CORE_HANDLE *handle,
2387 uint32_t reason, const char *fmt, va_list args)
2388 {
2389 /*
2390 * We created this object originally and we know it is actually an
2391 * OSSL_PROVIDER *, so the cast is safe
2392 */
2393 OSSL_PROVIDER *prov = (OSSL_PROVIDER *)handle;
2394
2395 /*
2396 * If the uppermost 8 bits are non-zero, it's an OpenSSL library
2397 * error and will be treated as such. Otherwise, it's a new style
2398 * provider error and will be treated as such.
2399 */
2400 if (ERR_GET_LIB(reason) != 0) {
2401 ERR_vset_error(ERR_GET_LIB(reason), ERR_GET_REASON(reason), fmt, args);
2402 } else {
2403 ERR_vset_error(prov->error_lib, (int)reason, fmt, args);
2404 }
2405 }
2406
core_set_error_mark(const OSSL_CORE_HANDLE * handle)2407 static int core_set_error_mark(const OSSL_CORE_HANDLE *handle)
2408 {
2409 return ERR_set_mark();
2410 }
2411
core_clear_last_error_mark(const OSSL_CORE_HANDLE * handle)2412 static int core_clear_last_error_mark(const OSSL_CORE_HANDLE *handle)
2413 {
2414 return ERR_clear_last_mark();
2415 }
2416
core_pop_error_to_mark(const OSSL_CORE_HANDLE * handle)2417 static int core_pop_error_to_mark(const OSSL_CORE_HANDLE *handle)
2418 {
2419 return ERR_pop_to_mark();
2420 }
2421
core_indicator_get_callback(OPENSSL_CORE_CTX * libctx,OSSL_INDICATOR_CALLBACK ** cb)2422 static void core_indicator_get_callback(OPENSSL_CORE_CTX *libctx,
2423 OSSL_INDICATOR_CALLBACK **cb)
2424 {
2425 OSSL_INDICATOR_get_callback((OSSL_LIB_CTX *)libctx, cb);
2426 }
2427
core_self_test_get_callback(OPENSSL_CORE_CTX * libctx,OSSL_CALLBACK ** cb,void ** cbarg)2428 static void core_self_test_get_callback(OPENSSL_CORE_CTX *libctx,
2429 OSSL_CALLBACK **cb, void **cbarg)
2430 {
2431 OSSL_SELF_TEST_get_callback((OSSL_LIB_CTX *)libctx, cb, cbarg);
2432 }
2433
2434 # ifdef OPENSSL_NO_FIPS_JITTER
rand_get_entropy(const OSSL_CORE_HANDLE * handle,unsigned char ** pout,int entropy,size_t min_len,size_t max_len)2435 static size_t rand_get_entropy(const OSSL_CORE_HANDLE *handle,
2436 unsigned char **pout, int entropy,
2437 size_t min_len, size_t max_len)
2438 {
2439 return ossl_rand_get_entropy((OSSL_LIB_CTX *)core_get_libctx(handle),
2440 pout, entropy, min_len, max_len);
2441 }
2442 # else
2443 /*
2444 * OpenSSL FIPS providers prior to 3.2 call rand_get_entropy API from
2445 * core, instead of the newer get_user_entropy. Newer API call honors
2446 * runtime configuration of random seed source and can be configured
2447 * to use os getranom() or another seed source, such as
2448 * JITTER. However, 3.0.9 only calls this API. Note that no other
2449 * providers known to use this, and it is core <-> provider only
2450 * API. Public facing EVP and getrandom bytes already correctly honor
2451 * runtime configuration for seed source. There are no other providers
2452 * packaged in Wolfi, or even known to exist that use this api. Thus
2453 * it is safe to say any caller of this API is in fact 3.0.9 FIPS
2454 * provider. Also note that the passed in handle is invalid and cannot
2455 * be safely dereferences in such cases. Due to a bug in FIPS
2456 * providers 3.0.0, 3.0.8 and 3.0.9. See
2457 * https://github.com/openssl/openssl/blob/master/doc/internal/man3/ossl_rand_get_entropy.pod#notes
2458 */
2459 size_t ossl_rand_jitter_get_seed(unsigned char **, int, size_t, size_t);
rand_get_entropy(const OSSL_CORE_HANDLE * handle,unsigned char ** pout,int entropy,size_t min_len,size_t max_len)2460 static size_t rand_get_entropy(const OSSL_CORE_HANDLE *handle,
2461 unsigned char **pout, int entropy,
2462 size_t min_len, size_t max_len)
2463 {
2464 return ossl_rand_jitter_get_seed(pout, entropy, min_len, max_len);
2465 }
2466 # endif
2467
rand_get_user_entropy(const OSSL_CORE_HANDLE * handle,unsigned char ** pout,int entropy,size_t min_len,size_t max_len)2468 static size_t rand_get_user_entropy(const OSSL_CORE_HANDLE *handle,
2469 unsigned char **pout, int entropy,
2470 size_t min_len, size_t max_len)
2471 {
2472 return ossl_rand_get_user_entropy((OSSL_LIB_CTX *)core_get_libctx(handle),
2473 pout, entropy, min_len, max_len);
2474 }
2475
rand_cleanup_entropy(const OSSL_CORE_HANDLE * handle,unsigned char * buf,size_t len)2476 static void rand_cleanup_entropy(const OSSL_CORE_HANDLE *handle,
2477 unsigned char *buf, size_t len)
2478 {
2479 ossl_rand_cleanup_entropy((OSSL_LIB_CTX *)core_get_libctx(handle),
2480 buf, len);
2481 }
2482
rand_cleanup_user_entropy(const OSSL_CORE_HANDLE * handle,unsigned char * buf,size_t len)2483 static void rand_cleanup_user_entropy(const OSSL_CORE_HANDLE *handle,
2484 unsigned char *buf, size_t len)
2485 {
2486 ossl_rand_cleanup_user_entropy((OSSL_LIB_CTX *)core_get_libctx(handle),
2487 buf, len);
2488 }
2489
rand_get_nonce(const OSSL_CORE_HANDLE * handle,unsigned char ** pout,size_t min_len,size_t max_len,const void * salt,size_t salt_len)2490 static size_t rand_get_nonce(const OSSL_CORE_HANDLE *handle,
2491 unsigned char **pout,
2492 size_t min_len, size_t max_len,
2493 const void *salt, size_t salt_len)
2494 {
2495 return ossl_rand_get_nonce((OSSL_LIB_CTX *)core_get_libctx(handle),
2496 pout, min_len, max_len, salt, salt_len);
2497 }
2498
rand_get_user_nonce(const OSSL_CORE_HANDLE * handle,unsigned char ** pout,size_t min_len,size_t max_len,const void * salt,size_t salt_len)2499 static size_t rand_get_user_nonce(const OSSL_CORE_HANDLE *handle,
2500 unsigned char **pout,
2501 size_t min_len, size_t max_len,
2502 const void *salt, size_t salt_len)
2503 {
2504 return ossl_rand_get_user_nonce((OSSL_LIB_CTX *)core_get_libctx(handle),
2505 pout, min_len, max_len, salt, salt_len);
2506 }
2507
rand_cleanup_nonce(const OSSL_CORE_HANDLE * handle,unsigned char * buf,size_t len)2508 static void rand_cleanup_nonce(const OSSL_CORE_HANDLE *handle,
2509 unsigned char *buf, size_t len)
2510 {
2511 ossl_rand_cleanup_nonce((OSSL_LIB_CTX *)core_get_libctx(handle),
2512 buf, len);
2513 }
2514
rand_cleanup_user_nonce(const OSSL_CORE_HANDLE * handle,unsigned char * buf,size_t len)2515 static void rand_cleanup_user_nonce(const OSSL_CORE_HANDLE *handle,
2516 unsigned char *buf, size_t len)
2517 {
2518 ossl_rand_cleanup_user_nonce((OSSL_LIB_CTX *)core_get_libctx(handle),
2519 buf, len);
2520 }
2521
core_provider_get0_name(const OSSL_CORE_HANDLE * prov)2522 static const char *core_provider_get0_name(const OSSL_CORE_HANDLE *prov)
2523 {
2524 return OSSL_PROVIDER_get0_name((const OSSL_PROVIDER *)prov);
2525 }
2526
core_provider_get0_provider_ctx(const OSSL_CORE_HANDLE * prov)2527 static void *core_provider_get0_provider_ctx(const OSSL_CORE_HANDLE *prov)
2528 {
2529 return OSSL_PROVIDER_get0_provider_ctx((const OSSL_PROVIDER *)prov);
2530 }
2531
2532 static const OSSL_DISPATCH *
core_provider_get0_dispatch(const OSSL_CORE_HANDLE * prov)2533 core_provider_get0_dispatch(const OSSL_CORE_HANDLE *prov)
2534 {
2535 return OSSL_PROVIDER_get0_dispatch((const OSSL_PROVIDER *)prov);
2536 }
2537
core_provider_up_ref_intern(const OSSL_CORE_HANDLE * prov,int activate)2538 static int core_provider_up_ref_intern(const OSSL_CORE_HANDLE *prov,
2539 int activate)
2540 {
2541 return provider_up_ref_intern((OSSL_PROVIDER *)prov, activate);
2542 }
2543
core_provider_free_intern(const OSSL_CORE_HANDLE * prov,int deactivate)2544 static int core_provider_free_intern(const OSSL_CORE_HANDLE *prov,
2545 int deactivate)
2546 {
2547 return provider_free_intern((OSSL_PROVIDER *)prov, deactivate);
2548 }
2549
core_obj_add_sigid(const OSSL_CORE_HANDLE * prov,const char * sign_name,const char * digest_name,const char * pkey_name)2550 static int core_obj_add_sigid(const OSSL_CORE_HANDLE *prov,
2551 const char *sign_name, const char *digest_name,
2552 const char *pkey_name)
2553 {
2554 int sign_nid = OBJ_txt2nid(sign_name);
2555 int digest_nid = NID_undef;
2556 int pkey_nid = OBJ_txt2nid(pkey_name);
2557
2558 if (digest_name != NULL && digest_name[0] != '\0'
2559 && (digest_nid = OBJ_txt2nid(digest_name)) == NID_undef)
2560 return 0;
2561
2562 if (sign_nid == NID_undef)
2563 return 0;
2564
2565 /*
2566 * Check if it already exists. This is a success if so (even if we don't
2567 * have nids for the digest/pkey)
2568 */
2569 if (OBJ_find_sigid_algs(sign_nid, NULL, NULL))
2570 return 1;
2571
2572 if (pkey_nid == NID_undef)
2573 return 0;
2574
2575 return OBJ_add_sigid(sign_nid, digest_nid, pkey_nid);
2576 }
2577
core_obj_create(const OSSL_CORE_HANDLE * prov,const char * oid,const char * sn,const char * ln)2578 static int core_obj_create(const OSSL_CORE_HANDLE *prov, const char *oid,
2579 const char *sn, const char *ln)
2580 {
2581 /* Check if it already exists and create it if not */
2582 return OBJ_txt2nid(oid) != NID_undef
2583 || OBJ_create(oid, sn, ln) != NID_undef;
2584 }
2585 #endif /* FIPS_MODULE */
2586
2587 /*
2588 * Functions provided by the core.
2589 */
2590 static const OSSL_DISPATCH core_dispatch_[] = {
2591 { OSSL_FUNC_CORE_GETTABLE_PARAMS, (void (*)(void))core_gettable_params },
2592 { OSSL_FUNC_CORE_GET_PARAMS, (void (*)(void))core_get_params },
2593 { OSSL_FUNC_CORE_GET_LIBCTX, (void (*)(void))core_get_libctx },
2594 { OSSL_FUNC_CORE_THREAD_START, (void (*)(void))core_thread_start },
2595 #ifndef FIPS_MODULE
2596 { OSSL_FUNC_CORE_NEW_ERROR, (void (*)(void))core_new_error },
2597 { OSSL_FUNC_CORE_SET_ERROR_DEBUG, (void (*)(void))core_set_error_debug },
2598 { OSSL_FUNC_CORE_VSET_ERROR, (void (*)(void))core_vset_error },
2599 { OSSL_FUNC_CORE_SET_ERROR_MARK, (void (*)(void))core_set_error_mark },
2600 { OSSL_FUNC_CORE_CLEAR_LAST_ERROR_MARK,
2601 (void (*)(void))core_clear_last_error_mark },
2602 { OSSL_FUNC_CORE_POP_ERROR_TO_MARK, (void (*)(void))core_pop_error_to_mark },
2603 { OSSL_FUNC_BIO_NEW_FILE, (void (*)(void))ossl_core_bio_new_file },
2604 { OSSL_FUNC_BIO_NEW_MEMBUF, (void (*)(void))ossl_core_bio_new_mem_buf },
2605 { OSSL_FUNC_BIO_READ_EX, (void (*)(void))ossl_core_bio_read_ex },
2606 { OSSL_FUNC_BIO_WRITE_EX, (void (*)(void))ossl_core_bio_write_ex },
2607 { OSSL_FUNC_BIO_GETS, (void (*)(void))ossl_core_bio_gets },
2608 { OSSL_FUNC_BIO_PUTS, (void (*)(void))ossl_core_bio_puts },
2609 { OSSL_FUNC_BIO_CTRL, (void (*)(void))ossl_core_bio_ctrl },
2610 { OSSL_FUNC_BIO_UP_REF, (void (*)(void))ossl_core_bio_up_ref },
2611 { OSSL_FUNC_BIO_FREE, (void (*)(void))ossl_core_bio_free },
2612 { OSSL_FUNC_BIO_VPRINTF, (void (*)(void))ossl_core_bio_vprintf },
2613 { OSSL_FUNC_BIO_VSNPRINTF, (void (*)(void))BIO_vsnprintf },
2614 { OSSL_FUNC_SELF_TEST_CB, (void (*)(void))core_self_test_get_callback },
2615 { OSSL_FUNC_INDICATOR_CB, (void (*)(void))core_indicator_get_callback },
2616 { OSSL_FUNC_GET_ENTROPY, (void (*)(void))rand_get_entropy },
2617 { OSSL_FUNC_GET_USER_ENTROPY, (void (*)(void))rand_get_user_entropy },
2618 { OSSL_FUNC_CLEANUP_ENTROPY, (void (*)(void))rand_cleanup_entropy },
2619 { OSSL_FUNC_CLEANUP_USER_ENTROPY, (void (*)(void))rand_cleanup_user_entropy },
2620 { OSSL_FUNC_GET_NONCE, (void (*)(void))rand_get_nonce },
2621 { OSSL_FUNC_GET_USER_NONCE, (void (*)(void))rand_get_user_nonce },
2622 { OSSL_FUNC_CLEANUP_NONCE, (void (*)(void))rand_cleanup_nonce },
2623 { OSSL_FUNC_CLEANUP_USER_NONCE, (void (*)(void))rand_cleanup_user_nonce },
2624 #endif
2625 { OSSL_FUNC_CRYPTO_MALLOC, (void (*)(void))CRYPTO_malloc },
2626 { OSSL_FUNC_CRYPTO_ZALLOC, (void (*)(void))CRYPTO_zalloc },
2627 { OSSL_FUNC_CRYPTO_FREE, (void (*)(void))CRYPTO_free },
2628 { OSSL_FUNC_CRYPTO_CLEAR_FREE, (void (*)(void))CRYPTO_clear_free },
2629 { OSSL_FUNC_CRYPTO_REALLOC, (void (*)(void))CRYPTO_realloc },
2630 { OSSL_FUNC_CRYPTO_CLEAR_REALLOC, (void (*)(void))CRYPTO_clear_realloc },
2631 { OSSL_FUNC_CRYPTO_SECURE_MALLOC, (void (*)(void))CRYPTO_secure_malloc },
2632 { OSSL_FUNC_CRYPTO_SECURE_ZALLOC, (void (*)(void))CRYPTO_secure_zalloc },
2633 { OSSL_FUNC_CRYPTO_SECURE_FREE, (void (*)(void))CRYPTO_secure_free },
2634 { OSSL_FUNC_CRYPTO_SECURE_CLEAR_FREE,
2635 (void (*)(void))CRYPTO_secure_clear_free },
2636 { OSSL_FUNC_CRYPTO_SECURE_ALLOCATED,
2637 (void (*)(void))CRYPTO_secure_allocated },
2638 { OSSL_FUNC_OPENSSL_CLEANSE, (void (*)(void))OPENSSL_cleanse },
2639 #ifndef FIPS_MODULE
2640 { OSSL_FUNC_PROVIDER_REGISTER_CHILD_CB,
2641 (void (*)(void))ossl_provider_register_child_cb },
2642 { OSSL_FUNC_PROVIDER_DEREGISTER_CHILD_CB,
2643 (void (*)(void))ossl_provider_deregister_child_cb },
2644 { OSSL_FUNC_PROVIDER_NAME,
2645 (void (*)(void))core_provider_get0_name },
2646 { OSSL_FUNC_PROVIDER_GET0_PROVIDER_CTX,
2647 (void (*)(void))core_provider_get0_provider_ctx },
2648 { OSSL_FUNC_PROVIDER_GET0_DISPATCH,
2649 (void (*)(void))core_provider_get0_dispatch },
2650 { OSSL_FUNC_PROVIDER_UP_REF,
2651 (void (*)(void))core_provider_up_ref_intern },
2652 { OSSL_FUNC_PROVIDER_FREE,
2653 (void (*)(void))core_provider_free_intern },
2654 { OSSL_FUNC_CORE_OBJ_ADD_SIGID, (void (*)(void))core_obj_add_sigid },
2655 { OSSL_FUNC_CORE_OBJ_CREATE, (void (*)(void))core_obj_create },
2656 #endif
2657 OSSL_DISPATCH_END
2658 };
2659 static const OSSL_DISPATCH *core_dispatch = core_dispatch_;
2660