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