Coverage Report

Created: 2026-08-30 07:14

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