VirtualBox

source: vbox/trunk/src/libs/openssl-3.4.1/crypto/provider_core.c

Last change on this file was 109052, checked in by vboxsync, 3 weeks ago

openssl-3.4.1: Applied our changes, regenerated files, added missing files and functions. This time with a three way merge. ​bugref:10890

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

© 2025 Oracle Support Privacy / Do Not Sell My Info Terms of Use Trademark Policy Automated Access Etiquette