Coverage Report

Created: 2025-10-12 06:56

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