Coverage Report

Created: 2025-12-31 06:58

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