Coverage Report

Created: 2025-11-11 06:20

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