Coverage Report

Created: 2025-12-10 06:24

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