Coverage Report

Created: 2025-12-31 06:58

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