Coverage Report

Created: 2026-10-28 07:07

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