Coverage Report

Created: 2025-06-13 06:58

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