VirtualBox

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

Last change on this file was 108206, checked in by vboxsync, 3 months ago

openssl-3.3.2: Exported all files to OSE and removed .scm-settings ​bugref:10757

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