Coverage Report

Created: 2025-12-31 06:58

next uncovered line (L), next uncovered region (R), next uncovered branch (B)
/src/openssl30/crypto/provider_core.c
Line
Count
Source
1
/*
2
 * Copyright 2019-2024 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
#ifndef FIPS_MODULE
33
#include <openssl/self_test.h>
34
#endif
35
36
/*
37
 * This file defines and uses a number of different structures:
38
 *
39
 * OSSL_PROVIDER (provider_st): Used to represent all information related to a
40
 * single instance of a provider.
41
 *
42
 * provider_store_st: Holds information about the collection of providers that
43
 * are available within the current library context (OSSL_LIB_CTX). It also
44
 * holds configuration information about providers that could be loaded at some
45
 * future point.
46
 *
47
 * OSSL_PROVIDER_CHILD_CB: An instance of this structure holds the callbacks
48
 * that have been registered for a child library context and the associated
49
 * provider that registered those callbacks.
50
 *
51
 * Where a child library context exists then it has its own instance of the
52
 * provider store. Each provider that exists in the parent provider store, has
53
 * an associated child provider in the child library context's provider store.
54
 * As providers get activated or deactivated this needs to be mirrored in the
55
 * associated child providers.
56
 *
57
 * LOCKING
58
 * =======
59
 *
60
 * There are a number of different locks used in this file and it is important
61
 * to understand how they should be used in order to avoid deadlocks.
62
 *
63
 * Fields within a structure can often be "write once" on creation, and then
64
 * "read many". Creation of a structure is done by a single thread, and
65
 * therefore no lock is required for the "write once/read many" fields. It is
66
 * safe for multiple threads to read these fields without a lock, because they
67
 * will never be changed.
68
 *
69
 * However some fields may be changed after a structure has been created and
70
 * shared between multiple threads. Where this is the case a lock is required.
71
 *
72
 * The locks available are:
73
 *
74
 * The provider flag_lock: Used to control updates to the various provider
75
 * "flags" (flag_initialized, flag_activated, flag_fallback) and associated
76
 * "counts" (activatecnt).
77
 *
78
 * The provider refcnt_lock: Only ever used to control updates to the provider
79
 * refcnt value.
80
 *
81
 * The provider optbits_lock: Used to control access to the provider's
82
 * operation_bits and operation_bits_sz fields.
83
 *
84
 * The store default_path_lock: Used to control access to the provider store's
85
 * default search path value (default_path)
86
 *
87
 * The store lock: Used to control the stack of provider's held within the
88
 * provider store, as well as the stack of registered child provider callbacks.
89
 *
90
 * As a general rule-of-thumb it is best to:
91
 *  - keep the scope of the code that is protected by a lock to the absolute
92
 *    minimum possible;
93
 *  - try to keep the scope of the lock to within a single function (i.e. avoid
94
 *    making calls to other functions while holding a lock);
95
 *  - try to only ever hold one lock at a time.
96
 *
97
 * Unfortunately, it is not always possible to stick to the above guidelines.
98
 * Where they are not adhered to there is always a danger of inadvertently
99
 * introducing the possibility of deadlock. The following rules MUST be adhered
100
 * to in order to avoid that:
101
 *  - Holding multiple locks at the same time is only allowed for the
102
 *    provider store lock, the provider flag_lock and the provider refcnt_lock.
103
 *  - When holding multiple locks they must be acquired in the following order of
104
 *    precedence:
105
 *        1) provider store lock
106
 *        2) provider flag_lock
107
 *        3) provider refcnt_lock
108
 *  - When releasing locks they must be released in the reverse order to which
109
 *    they were acquired
110
 *  - No locks may be held when making an upcall. NOTE: Some common functions
111
 *    can make upcalls as part of their normal operation. If you need to call
112
 *    some other function while holding a lock make sure you know whether it
113
 *    will make any upcalls or not. For example ossl_provider_up_ref() can call
114
 *    ossl_provider_up_ref_parent() which can call the c_prov_up_ref() upcall.
115
 *  - It is permissible to hold the store and flag locks when calling child
116
 *    provider callbacks. No other locks may be held during such callbacks.
117
 */
118
119
static OSSL_PROVIDER *provider_new(const char *name,
120
    OSSL_provider_init_fn *init_function,
121
    STACK_OF(INFOPAIR) *parameters);
122
123
/*-
124
 * Provider Object structure
125
 * =========================
126
 */
127
128
#ifndef FIPS_MODULE
129
typedef struct {
130
    OSSL_PROVIDER *prov;
131
    int (*create_cb)(const OSSL_CORE_HANDLE *provider, void *cbdata);
132
    int (*remove_cb)(const OSSL_CORE_HANDLE *provider, void *cbdata);
133
    int (*global_props_cb)(const char *props, void *cbdata);
134
    void *cbdata;
135
} OSSL_PROVIDER_CHILD_CB;
136
DEFINE_STACK_OF(OSSL_PROVIDER_CHILD_CB)
137
#endif
138
139
struct provider_store_st; /* Forward declaration */
140
141
struct ossl_provider_st {
142
    /* Flag bits */
143
    unsigned int flag_initialized : 1;
144
    unsigned int flag_activated : 1;
145
    unsigned int flag_fallback : 1; /* Can be used as fallback */
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 *refcnt_lock; /* For the ref 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_query_operation_fn *query_operation;
179
    OSSL_FUNC_provider_unquery_operation_fn *unquery_operation;
180
181
    /*
182
     * Cache of bit to indicate of query_operation() has been called on
183
     * a specific operation or not.
184
     */
185
    unsigned char *operation_bits;
186
    size_t operation_bits_sz;
187
    CRYPTO_RWLOCK *opbits_lock;
188
189
#ifndef FIPS_MODULE
190
    /* Whether this provider is the child of some other provider */
191
    const OSSL_CORE_HANDLE *handle;
192
    unsigned int ischild : 1;
193
#endif
194
195
    /* Provider side data */
196
    void *provctx;
197
    const OSSL_DISPATCH *dispatch;
198
};
199
DEFINE_STACK_OF(OSSL_PROVIDER)
200
201
static int ossl_provider_cmp(const OSSL_PROVIDER *const *a,
202
    const OSSL_PROVIDER *const *b)
203
479
{
204
479
    return strcmp((*a)->name, (*b)->name);
205
479
}
206
207
/*-
208
 * Provider Object store
209
 * =====================
210
 *
211
 * The Provider Object store is a library context object, and therefore needs
212
 * an index.
213
 */
214
215
struct provider_store_st {
216
    OSSL_LIB_CTX *libctx;
217
    STACK_OF(OSSL_PROVIDER) *providers;
218
    STACK_OF(OSSL_PROVIDER_CHILD_CB) *child_cbs;
219
    CRYPTO_RWLOCK *default_path_lock;
220
    CRYPTO_RWLOCK *lock;
221
    char *default_path;
222
    OSSL_PROVIDER_INFO *provinfo;
223
    size_t numprovinfo;
224
    size_t provinfosz;
225
    unsigned int use_fallbacks : 1;
226
    unsigned int freeing : 1;
227
};
228
229
/*
230
 * provider_deactivate_free() is a wrapper around ossl_provider_deactivate()
231
 * and ossl_provider_free(), called as needed.
232
 * Since this is only called when the provider store is being emptied, we
233
 * don't need to care about any lock.
234
 */
235
static void provider_deactivate_free(OSSL_PROVIDER *prov)
236
182
{
237
182
    if (prov->flag_activated)
238
182
        ossl_provider_deactivate(prov, 1);
239
182
    ossl_provider_free(prov);
240
182
}
241
242
#ifndef FIPS_MODULE
243
static void ossl_provider_child_cb_free(OSSL_PROVIDER_CHILD_CB *cb)
244
0
{
245
0
    OPENSSL_free(cb);
246
0
}
247
#endif
248
249
static void infopair_free(INFOPAIR *pair)
250
0
{
251
0
    OPENSSL_free(pair->name);
252
0
    OPENSSL_free(pair->value);
253
0
    OPENSSL_free(pair);
254
0
}
255
256
static INFOPAIR *infopair_copy(const INFOPAIR *src)
257
0
{
258
0
    INFOPAIR *dest = OPENSSL_zalloc(sizeof(*dest));
259
260
0
    if (dest == NULL)
261
0
        return NULL;
262
0
    if (src->name != NULL) {
263
0
        dest->name = OPENSSL_strdup(src->name);
264
0
        if (dest->name == NULL)
265
0
            goto err;
266
0
    }
267
0
    if (src->value != NULL) {
268
0
        dest->value = OPENSSL_strdup(src->value);
269
0
        if (dest->value == NULL)
270
0
            goto err;
271
0
    }
272
0
    return dest;
273
0
err:
274
0
    OPENSSL_free(dest->name);
275
0
    OPENSSL_free(dest);
276
0
    return NULL;
277
0
}
278
279
void ossl_provider_info_clear(OSSL_PROVIDER_INFO *info)
280
110
{
281
110
    OPENSSL_free(info->name);
282
110
    OPENSSL_free(info->path);
283
110
    sk_INFOPAIR_pop_free(info->parameters, infopair_free);
284
110
}
285
286
static void provider_store_free(void *vstore)
287
12
{
288
12
    struct provider_store_st *store = vstore;
289
12
    size_t i;
290
291
12
    if (store == NULL)
292
0
        return;
293
12
    store->freeing = 1;
294
12
    OPENSSL_free(store->default_path);
295
12
    sk_OSSL_PROVIDER_pop_free(store->providers, provider_deactivate_free);
296
12
#ifndef FIPS_MODULE
297
12
    sk_OSSL_PROVIDER_CHILD_CB_pop_free(store->child_cbs,
298
12
        ossl_provider_child_cb_free);
299
12
#endif
300
12
    CRYPTO_THREAD_lock_free(store->default_path_lock);
301
12
    CRYPTO_THREAD_lock_free(store->lock);
302
22
    for (i = 0; i < store->numprovinfo; i++)
303
10
        ossl_provider_info_clear(&store->provinfo[i]);
304
12
    OPENSSL_free(store->provinfo);
305
12
    OPENSSL_free(store);
306
12
}
307
308
static void *provider_store_new(OSSL_LIB_CTX *ctx)
309
12
{
310
12
    struct provider_store_st *store = OPENSSL_zalloc(sizeof(*store));
311
312
12
    if (store == NULL
313
12
        || (store->providers = sk_OSSL_PROVIDER_new(ossl_provider_cmp)) == NULL
314
12
        || (store->default_path_lock = CRYPTO_THREAD_lock_new()) == NULL
315
12
#ifndef FIPS_MODULE
316
12
        || (store->child_cbs = sk_OSSL_PROVIDER_CHILD_CB_new_null()) == NULL
317
12
#endif
318
12
        || (store->lock = CRYPTO_THREAD_lock_new()) == NULL) {
319
0
        provider_store_free(store);
320
0
        return NULL;
321
0
    }
322
12
    store->libctx = ctx;
323
12
    store->use_fallbacks = 1;
324
325
12
    return store;
326
12
}
327
328
static const OSSL_LIB_CTX_METHOD provider_store_method = {
329
    /* Needs to be freed before the child provider data is freed */
330
    OSSL_LIB_CTX_METHOD_PRIORITY_1,
331
    provider_store_new,
332
    provider_store_free,
333
};
334
335
static struct provider_store_st *get_provider_store(OSSL_LIB_CTX *libctx)
336
5.69M
{
337
5.69M
    struct provider_store_st *store = NULL;
338
339
5.69M
    store = ossl_lib_ctx_get_data(libctx, OSSL_LIB_CTX_PROVIDER_STORE_INDEX,
340
5.69M
        &provider_store_method);
341
5.69M
    if (store == NULL)
342
5.69M
        ERR_raise(ERR_LIB_CRYPTO, ERR_R_INTERNAL_ERROR);
343
5.69M
    return store;
344
5.69M
}
345
346
int ossl_provider_disable_fallback_loading(OSSL_LIB_CTX *libctx)
347
0
{
348
0
    struct provider_store_st *store;
349
350
0
    if ((store = get_provider_store(libctx)) != NULL) {
351
0
        if (!CRYPTO_THREAD_write_lock(store->lock))
352
0
            return 0;
353
0
        store->use_fallbacks = 0;
354
0
        CRYPTO_THREAD_unlock(store->lock);
355
0
        return 1;
356
0
    }
357
0
    return 0;
358
0
}
359
360
136
#define BUILTINS_BLOCK_SIZE 10
361
362
int ossl_provider_info_add_to_store(OSSL_LIB_CTX *libctx,
363
    OSSL_PROVIDER_INFO *entry)
364
136
{
365
136
    struct provider_store_st *store = get_provider_store(libctx);
366
136
    int ret = 0;
367
368
136
    if (entry->name == NULL) {
369
0
        ERR_raise(ERR_LIB_CRYPTO, ERR_R_PASSED_NULL_PARAMETER);
370
0
        return 0;
371
0
    }
372
373
136
    if (store == NULL) {
374
0
        ERR_raise(ERR_LIB_CRYPTO, ERR_R_INTERNAL_ERROR);
375
0
        return 0;
376
0
    }
377
378
136
    if (!CRYPTO_THREAD_write_lock(store->lock))
379
0
        return 0;
380
136
    if (store->provinfosz == 0) {
381
136
        store->provinfo = OPENSSL_zalloc(sizeof(*store->provinfo)
382
136
            * BUILTINS_BLOCK_SIZE);
383
136
        if (store->provinfo == NULL) {
384
0
            ERR_raise(ERR_LIB_CRYPTO, ERR_R_MALLOC_FAILURE);
385
0
            goto err;
386
0
        }
387
136
        store->provinfosz = BUILTINS_BLOCK_SIZE;
388
136
    } else if (store->numprovinfo == store->provinfosz) {
389
0
        OSSL_PROVIDER_INFO *tmpbuiltins;
390
0
        size_t newsz = store->provinfosz + BUILTINS_BLOCK_SIZE;
391
392
0
        tmpbuiltins = OPENSSL_realloc(store->provinfo,
393
0
            sizeof(*store->provinfo) * newsz);
394
0
        if (tmpbuiltins == NULL) {
395
0
            ERR_raise(ERR_LIB_CRYPTO, ERR_R_MALLOC_FAILURE);
396
0
            goto err;
397
0
        }
398
0
        store->provinfo = tmpbuiltins;
399
0
        store->provinfosz = newsz;
400
0
    }
401
136
    store->provinfo[store->numprovinfo] = *entry;
402
136
    store->numprovinfo++;
403
404
136
    ret = 1;
405
136
err:
406
136
    CRYPTO_THREAD_unlock(store->lock);
407
136
    return ret;
408
136
}
409
410
OSSL_PROVIDER *ossl_provider_find(OSSL_LIB_CTX *libctx, const char *name,
411
    ossl_unused int noconfig)
412
336
{
413
336
    struct provider_store_st *store = NULL;
414
336
    OSSL_PROVIDER *prov = NULL;
415
416
336
    if ((store = get_provider_store(libctx)) != NULL) {
417
336
        OSSL_PROVIDER tmpl = {
418
336
            0,
419
336
        };
420
336
        int i;
421
422
336
#if !defined(FIPS_MODULE) && !defined(OPENSSL_NO_AUTOLOAD_CONFIG)
423
        /*
424
         * Make sure any providers are loaded from config before we try to find
425
         * them.
426
         */
427
336
        if (!noconfig) {
428
110
            if (ossl_lib_ctx_is_default(libctx))
429
110
                OPENSSL_init_crypto(OPENSSL_INIT_LOAD_CONFIG, NULL);
430
110
        }
431
336
#endif
432
433
336
        tmpl.name = (char *)name;
434
        /*
435
         * A "find" operation can sort the stack, and therefore a write lock is
436
         * required.
437
         */
438
336
        if (!CRYPTO_THREAD_write_lock(store->lock))
439
0
            return NULL;
440
336
        if ((i = sk_OSSL_PROVIDER_find(store->providers, &tmpl)) != -1)
441
42
            prov = sk_OSSL_PROVIDER_value(store->providers, i);
442
336
        CRYPTO_THREAD_unlock(store->lock);
443
336
        if (prov != NULL && !ossl_provider_up_ref(prov))
444
0
            prov = NULL;
445
336
    }
446
447
336
    return prov;
448
336
}
449
450
/*-
451
 * Provider Object methods
452
 * =======================
453
 */
454
455
static OSSL_PROVIDER *provider_new(const char *name,
456
    OSSL_provider_init_fn *init_function,
457
    STACK_OF(INFOPAIR) *parameters)
458
17
{
459
17
    OSSL_PROVIDER *prov = NULL;
460
461
17
    if ((prov = OPENSSL_zalloc(sizeof(*prov))) == NULL
462
#ifndef HAVE_ATOMICS
463
        || (prov->refcnt_lock = CRYPTO_THREAD_lock_new()) == NULL
464
#endif
465
17
    ) {
466
0
        OPENSSL_free(prov);
467
0
        ERR_raise(ERR_LIB_CRYPTO, ERR_R_MALLOC_FAILURE);
468
0
        return NULL;
469
0
    }
470
471
17
    prov->refcnt = 1; /* 1 One reference to be returned */
472
473
17
    if ((prov->opbits_lock = CRYPTO_THREAD_lock_new()) == NULL
474
17
        || (prov->flag_lock = CRYPTO_THREAD_lock_new()) == NULL
475
17
        || (prov->name = OPENSSL_strdup(name)) == NULL
476
17
        || (prov->parameters = sk_INFOPAIR_deep_copy(parameters,
477
17
                infopair_copy,
478
17
                infopair_free))
479
17
            == NULL) {
480
0
        ossl_provider_free(prov);
481
0
        ERR_raise(ERR_LIB_CRYPTO, ERR_R_MALLOC_FAILURE);
482
0
        return NULL;
483
0
    }
484
485
17
    prov->init_function = init_function;
486
487
17
    return prov;
488
17
}
489
490
int ossl_provider_up_ref(OSSL_PROVIDER *prov)
491
24.7k
{
492
24.7k
    int ref = 0;
493
494
24.7k
    if (CRYPTO_UP_REF(&prov->refcnt, &ref, prov->refcnt_lock) <= 0)
495
0
        return 0;
496
497
24.7k
#ifndef FIPS_MODULE
498
24.7k
    if (prov->ischild) {
499
0
        if (!ossl_provider_up_ref_parent(prov, 0)) {
500
0
            ossl_provider_free(prov);
501
0
            return 0;
502
0
        }
503
0
    }
504
24.7k
#endif
505
506
24.7k
    return ref;
507
24.7k
}
508
509
#ifndef FIPS_MODULE
510
static int provider_up_ref_intern(OSSL_PROVIDER *prov, int activate)
511
0
{
512
0
    if (activate)
513
0
        return ossl_provider_activate(prov, 1, 0);
514
515
0
    return ossl_provider_up_ref(prov);
516
0
}
517
518
static int provider_free_intern(OSSL_PROVIDER *prov, int deactivate)
519
0
{
520
0
    if (deactivate)
521
0
        return ossl_provider_deactivate(prov, 1);
522
523
0
    ossl_provider_free(prov);
524
0
    return 1;
525
0
}
526
#endif
527
528
/*
529
 * We assume that the requested provider does not already exist in the store.
530
 * The caller should check. If it does exist then adding it to the store later
531
 * will fail.
532
 */
533
OSSL_PROVIDER *ossl_provider_new(OSSL_LIB_CTX *libctx, const char *name,
534
    OSSL_provider_init_fn *init_function,
535
    int noconfig)
536
10
{
537
10
    struct provider_store_st *store = NULL;
538
10
    OSSL_PROVIDER_INFO template;
539
10
    OSSL_PROVIDER *prov = NULL;
540
541
10
    if ((store = get_provider_store(libctx)) == NULL)
542
0
        return NULL;
543
544
10
    memset(&template, 0, sizeof(template));
545
10
    if (init_function == NULL) {
546
10
        const OSSL_PROVIDER_INFO *p;
547
10
        size_t i;
548
549
        /* Check if this is a predefined builtin provider */
550
40
        for (p = ossl_predefined_providers; p->name != NULL; p++) {
551
30
            if (strcmp(p->name, name) == 0) {
552
0
                template = *p;
553
0
                break;
554
0
            }
555
30
        }
556
10
        if (p->name == NULL) {
557
            /* Check if this is a user added builtin provider */
558
10
            if (!CRYPTO_THREAD_read_lock(store->lock))
559
0
                return NULL;
560
10
            for (i = 0, p = store->provinfo; i < store->numprovinfo; p++, i++) {
561
10
                if (strcmp(p->name, name) == 0) {
562
10
                    template = *p;
563
10
                    break;
564
10
                }
565
10
            }
566
10
            CRYPTO_THREAD_unlock(store->lock);
567
10
        }
568
10
    } else {
569
0
        template.init = init_function;
570
0
    }
571
572
    /* provider_new() generates an error, so no need here */
573
10
    prov = provider_new(name, template.init, template.parameters);
574
575
10
    if (prov == NULL)
576
0
        return NULL;
577
578
10
    if (!ossl_provider_set_module_path(prov, template.path)) {
579
0
        ossl_provider_free(prov);
580
0
        return NULL;
581
0
    }
582
583
10
    prov->libctx = libctx;
584
10
#ifndef FIPS_MODULE
585
10
    prov->error_lib = ERR_get_next_error_library();
586
10
#endif
587
588
    /*
589
     * At this point, the provider is only partially "loaded".  To be
590
     * fully "loaded", ossl_provider_activate() must also be called and it must
591
     * then be added to the provider store.
592
     */
593
594
10
    return prov;
595
10
}
596
597
/* Assumes that the store lock is held */
598
static int create_provider_children(OSSL_PROVIDER *prov)
599
136
{
600
136
    int ret = 1;
601
136
#ifndef FIPS_MODULE
602
136
    struct provider_store_st *store = prov->store;
603
136
    OSSL_PROVIDER_CHILD_CB *child_cb;
604
136
    int i, max;
605
606
136
    max = sk_OSSL_PROVIDER_CHILD_CB_num(store->child_cbs);
607
136
    for (i = 0; i < max; i++) {
608
        /*
609
         * This is newly activated (activatecnt == 1), so we need to
610
         * create child providers as necessary.
611
         */
612
0
        child_cb = sk_OSSL_PROVIDER_CHILD_CB_value(store->child_cbs, i);
613
0
        ret &= child_cb->create_cb((OSSL_CORE_HANDLE *)prov, child_cb->cbdata);
614
0
    }
615
136
#endif
616
617
136
    return ret;
618
136
}
619
620
int ossl_provider_add_to_store(OSSL_PROVIDER *prov, OSSL_PROVIDER **actualprov,
621
    int retain_fallbacks)
622
10
{
623
10
    struct provider_store_st *store;
624
10
    int idx;
625
10
    OSSL_PROVIDER tmpl = {
626
10
        0,
627
10
    };
628
10
    OSSL_PROVIDER *actualtmp = NULL;
629
630
10
    if (actualprov != NULL)
631
10
        *actualprov = NULL;
632
633
10
    if ((store = get_provider_store(prov->libctx)) == NULL)
634
0
        return 0;
635
636
10
    if (!CRYPTO_THREAD_write_lock(store->lock))
637
0
        return 0;
638
639
10
    tmpl.name = (char *)prov->name;
640
10
    idx = sk_OSSL_PROVIDER_find(store->providers, &tmpl);
641
10
    if (idx == -1)
642
10
        actualtmp = prov;
643
0
    else
644
0
        actualtmp = sk_OSSL_PROVIDER_value(store->providers, idx);
645
646
10
    if (idx == -1) {
647
10
        if (sk_OSSL_PROVIDER_push(store->providers, prov) == 0)
648
0
            goto err;
649
10
        prov->store = store;
650
10
        if (!create_provider_children(prov)) {
651
0
            sk_OSSL_PROVIDER_delete_ptr(store->providers, prov);
652
0
            goto err;
653
0
        }
654
10
        if (!retain_fallbacks)
655
0
            store->use_fallbacks = 0;
656
10
    }
657
658
10
    CRYPTO_THREAD_unlock(store->lock);
659
660
10
    if (actualprov != NULL) {
661
10
        if (!ossl_provider_up_ref(actualtmp)) {
662
0
            ERR_raise(ERR_LIB_CRYPTO, ERR_R_MALLOC_FAILURE);
663
0
            actualtmp = NULL;
664
0
            return 0;
665
0
        }
666
10
        *actualprov = actualtmp;
667
10
    }
668
669
10
    if (idx >= 0) {
670
        /*
671
         * The provider is already in the store. Probably two threads
672
         * independently initialised their own provider objects with the same
673
         * name and raced to put them in the store. This thread lost. We
674
         * deactivate the one we just created and use the one that already
675
         * exists instead.
676
         * If we get here then we know we did not create provider children
677
         * above, so we inform ossl_provider_deactivate not to attempt to remove
678
         * any.
679
         */
680
0
        ossl_provider_deactivate(prov, 0);
681
0
        ossl_provider_free(prov);
682
0
    }
683
684
10
    return 1;
685
686
0
err:
687
0
    CRYPTO_THREAD_unlock(store->lock);
688
0
    return 0;
689
10
}
690
691
void ossl_provider_free(OSSL_PROVIDER *prov)
692
17.2k
{
693
17.2k
    if (prov != NULL) {
694
17.2k
        int ref = 0;
695
696
17.2k
        CRYPTO_DOWN_REF(&prov->refcnt, &ref, prov->refcnt_lock);
697
698
        /*
699
         * When the refcount drops to zero, we clean up the provider.
700
         * Note that this also does teardown, which may seem late,
701
         * considering that init happens on first activation.  However,
702
         * there may be other structures hanging on to the provider after
703
         * the last deactivation and may therefore need full access to the
704
         * provider's services.  Therefore, we deinit late.
705
         */
706
17.2k
        if (ref == 0) {
707
72
            if (prov->flag_initialized) {
708
72
                ossl_provider_teardown(prov);
709
72
#ifndef OPENSSL_NO_ERR
710
72
#ifndef FIPS_MODULE
711
72
                if (prov->error_strings != NULL) {
712
0
                    ERR_unload_strings(prov->error_lib, prov->error_strings);
713
0
                    OPENSSL_free(prov->error_strings);
714
0
                    prov->error_strings = NULL;
715
0
                }
716
72
#endif
717
72
#endif
718
72
                OPENSSL_free(prov->operation_bits);
719
72
                prov->operation_bits = NULL;
720
72
                prov->operation_bits_sz = 0;
721
72
                prov->flag_initialized = 0;
722
72
            }
723
724
72
#ifndef FIPS_MODULE
725
            /*
726
             * We deregister thread handling whether or not the provider was
727
             * initialized. If init was attempted but was not successful then
728
             * the provider may still have registered a thread handler.
729
             */
730
72
            ossl_init_thread_deregister(prov);
731
72
            DSO_free(prov->module);
732
72
#endif
733
72
            OPENSSL_free(prov->name);
734
72
            OPENSSL_free(prov->path);
735
72
            sk_INFOPAIR_pop_free(prov->parameters, infopair_free);
736
72
            CRYPTO_THREAD_lock_free(prov->opbits_lock);
737
72
            CRYPTO_THREAD_lock_free(prov->flag_lock);
738
#ifndef HAVE_ATOMICS
739
            CRYPTO_THREAD_lock_free(prov->refcnt_lock);
740
#endif
741
72
            OPENSSL_free(prov);
742
72
        }
743
17.1k
#ifndef FIPS_MODULE
744
17.1k
        else if (prov->ischild) {
745
0
            ossl_provider_free_parent(prov, 0);
746
0
        }
747
17.2k
#endif
748
17.2k
    }
749
17.2k
}
750
751
/* Setters */
752
int ossl_provider_set_module_path(OSSL_PROVIDER *prov, const char *module_path)
753
136
{
754
136
    OPENSSL_free(prov->path);
755
136
    prov->path = NULL;
756
136
    if (module_path == NULL)
757
136
        return 1;
758
0
    if ((prov->path = OPENSSL_strdup(module_path)) != NULL)
759
0
        return 1;
760
0
    ERR_raise(ERR_LIB_CRYPTO, ERR_R_MALLOC_FAILURE);
761
0
    return 0;
762
0
}
763
764
static int infopair_add(STACK_OF(INFOPAIR) **infopairsk, const char *name,
765
    const char *value)
766
0
{
767
0
    INFOPAIR *pair = NULL;
768
769
0
    if ((pair = OPENSSL_zalloc(sizeof(*pair))) != NULL
770
0
        && (*infopairsk != NULL
771
0
            || (*infopairsk = sk_INFOPAIR_new_null()) != NULL)
772
0
        && (pair->name = OPENSSL_strdup(name)) != NULL
773
0
        && (pair->value = OPENSSL_strdup(value)) != NULL
774
0
        && sk_INFOPAIR_push(*infopairsk, pair) > 0)
775
0
        return 1;
776
777
0
    if (pair != NULL) {
778
0
        OPENSSL_free(pair->name);
779
0
        OPENSSL_free(pair->value);
780
0
        OPENSSL_free(pair);
781
0
    }
782
0
    ERR_raise(ERR_LIB_CRYPTO, ERR_R_MALLOC_FAILURE);
783
0
    return 0;
784
0
}
785
786
int ossl_provider_add_parameter(OSSL_PROVIDER *prov,
787
    const char *name, const char *value)
788
0
{
789
0
    return infopair_add(&prov->parameters, name, value);
790
0
}
791
792
int ossl_provider_info_add_parameter(OSSL_PROVIDER_INFO *provinfo,
793
    const char *name,
794
    const char *value)
795
0
{
796
0
    return infopair_add(&provinfo->parameters, name, value);
797
0
}
798
799
/*
800
 * Provider activation.
801
 *
802
 * What "activation" means depends on the provider form; for built in
803
 * providers (in the library or the application alike), the provider
804
 * can already be considered to be loaded, all that's needed is to
805
 * initialize it.  However, for dynamically loadable provider modules,
806
 * we must first load that module.
807
 *
808
 * Built in modules are distinguished from dynamically loaded modules
809
 * with an already assigned init function.
810
 */
811
static const OSSL_DISPATCH *core_dispatch; /* Define further down */
812
813
int OSSL_PROVIDER_set_default_search_path(OSSL_LIB_CTX *libctx,
814
    const char *path)
815
0
{
816
0
    struct provider_store_st *store;
817
0
    char *p = NULL;
818
819
0
    if (path != NULL) {
820
0
        p = OPENSSL_strdup(path);
821
0
        if (p == NULL) {
822
0
            ERR_raise(ERR_LIB_CRYPTO, ERR_R_MALLOC_FAILURE);
823
0
            return 0;
824
0
        }
825
0
    }
826
0
    if ((store = get_provider_store(libctx)) != NULL
827
0
        && CRYPTO_THREAD_write_lock(store->default_path_lock)) {
828
0
        OPENSSL_free(store->default_path);
829
0
        store->default_path = p;
830
0
        CRYPTO_THREAD_unlock(store->default_path_lock);
831
0
        return 1;
832
0
    }
833
0
    OPENSSL_free(p);
834
0
    return 0;
835
0
}
836
837
/*
838
 * Internal version that doesn't affect the store flags, and thereby avoid
839
 * locking.  Direct callers must remember to set the store flags when
840
 * appropriate.
841
 */
842
static int provider_init(OSSL_PROVIDER *prov)
843
94
{
844
94
    const OSSL_DISPATCH *provider_dispatch = NULL;
845
94
    void *tmp_provctx = NULL; /* safety measure */
846
94
#ifndef OPENSSL_NO_ERR
847
94
#ifndef FIPS_MODULE
848
94
    OSSL_FUNC_provider_get_reason_strings_fn *p_get_reason_strings = NULL;
849
94
#endif
850
94
#endif
851
94
    int ok = 0;
852
853
94
    if (!ossl_assert(!prov->flag_initialized)) {
854
0
        ERR_raise(ERR_LIB_CRYPTO, ERR_R_INTERNAL_ERROR);
855
0
        goto end;
856
0
    }
857
858
    /*
859
     * If the init function isn't set, it indicates that this provider is
860
     * a loadable module.
861
     */
862
94
    if (prov->init_function == NULL) {
863
#ifdef FIPS_MODULE
864
        goto end;
865
#else
866
0
        if (prov->module == NULL) {
867
0
            char *allocated_path = NULL;
868
0
            const char *module_path = NULL;
869
0
            char *merged_path = NULL;
870
0
            const char *load_dir = NULL;
871
0
            char *allocated_load_dir = NULL;
872
0
            struct provider_store_st *store;
873
874
0
            if ((prov->module = DSO_new()) == NULL) {
875
                /* DSO_new() generates an error already */
876
0
                goto end;
877
0
            }
878
879
0
            if ((store = get_provider_store(prov->libctx)) == NULL
880
0
                || !CRYPTO_THREAD_read_lock(store->default_path_lock))
881
0
                goto end;
882
883
0
            if (store->default_path != NULL) {
884
0
                allocated_load_dir = OPENSSL_strdup(store->default_path);
885
0
                CRYPTO_THREAD_unlock(store->default_path_lock);
886
0
                if (allocated_load_dir == NULL) {
887
0
                    ERR_raise(ERR_LIB_CRYPTO, ERR_R_MALLOC_FAILURE);
888
0
                    goto end;
889
0
                }
890
0
                load_dir = allocated_load_dir;
891
0
            } else {
892
0
                CRYPTO_THREAD_unlock(store->default_path_lock);
893
0
            }
894
895
0
            if (load_dir == NULL) {
896
0
                load_dir = ossl_safe_getenv("OPENSSL_MODULES");
897
0
                if (load_dir == NULL)
898
0
                    load_dir = MODULESDIR;
899
0
            }
900
901
0
            DSO_ctrl(prov->module, DSO_CTRL_SET_FLAGS,
902
0
                DSO_FLAG_NAME_TRANSLATION_EXT_ONLY, NULL);
903
904
0
            module_path = prov->path;
905
0
            if (module_path == NULL)
906
0
                module_path = allocated_path = DSO_convert_filename(prov->module, prov->name);
907
0
            if (module_path != NULL)
908
0
                merged_path = DSO_merge(prov->module, module_path, load_dir);
909
910
0
            if (merged_path == NULL
911
0
                || (DSO_load(prov->module, merged_path, NULL, 0)) == NULL) {
912
0
                DSO_free(prov->module);
913
0
                prov->module = NULL;
914
0
            }
915
916
0
            OPENSSL_free(merged_path);
917
0
            OPENSSL_free(allocated_path);
918
0
            OPENSSL_free(allocated_load_dir);
919
0
        }
920
921
0
        if (prov->module == NULL) {
922
            /* DSO has already recorded errors, this is just a tracepoint */
923
0
            ERR_raise_data(ERR_LIB_CRYPTO, ERR_R_DSO_LIB,
924
0
                "name=%s", prov->name);
925
0
            goto end;
926
0
        }
927
928
0
        prov->init_function = (OSSL_provider_init_fn *)
929
0
            DSO_bind_func(prov->module, "OSSL_provider_init");
930
0
#endif
931
0
    }
932
933
    /* Check for and call the initialise function for the provider. */
934
94
    if (prov->init_function == NULL) {
935
0
        ERR_raise_data(ERR_LIB_CRYPTO, ERR_R_UNSUPPORTED,
936
0
            "name=%s, provider has no provider init function",
937
0
            prov->name);
938
0
        goto end;
939
0
    }
940
941
94
    if (!prov->init_function((OSSL_CORE_HANDLE *)prov, core_dispatch,
942
94
            &provider_dispatch, &tmp_provctx)) {
943
0
        ERR_raise_data(ERR_LIB_CRYPTO, ERR_R_INIT_FAIL,
944
0
            "name=%s", prov->name);
945
0
        goto end;
946
0
    }
947
94
    prov->provctx = tmp_provctx;
948
94
    prov->dispatch = provider_dispatch;
949
950
94
    if (provider_dispatch != NULL) {
951
390
        for (; provider_dispatch->function_id != 0; provider_dispatch++) {
952
296
            switch (provider_dispatch->function_id) {
953
94
            case OSSL_FUNC_PROVIDER_TEARDOWN:
954
94
                prov->teardown = OSSL_FUNC_provider_teardown(provider_dispatch);
955
94
                break;
956
36
            case OSSL_FUNC_PROVIDER_GETTABLE_PARAMS:
957
36
                prov->gettable_params = OSSL_FUNC_provider_gettable_params(provider_dispatch);
958
36
                break;
959
36
            case OSSL_FUNC_PROVIDER_GET_PARAMS:
960
36
                prov->get_params = OSSL_FUNC_provider_get_params(provider_dispatch);
961
36
                break;
962
0
            case OSSL_FUNC_PROVIDER_SELF_TEST:
963
0
                prov->self_test = OSSL_FUNC_provider_self_test(provider_dispatch);
964
0
                break;
965
36
            case OSSL_FUNC_PROVIDER_GET_CAPABILITIES:
966
36
                prov->get_capabilities = OSSL_FUNC_provider_get_capabilities(provider_dispatch);
967
36
                break;
968
94
            case OSSL_FUNC_PROVIDER_QUERY_OPERATION:
969
94
                prov->query_operation = OSSL_FUNC_provider_query_operation(provider_dispatch);
970
94
                break;
971
0
            case OSSL_FUNC_PROVIDER_UNQUERY_OPERATION:
972
0
                prov->unquery_operation = OSSL_FUNC_provider_unquery_operation(provider_dispatch);
973
0
                break;
974
0
#ifndef OPENSSL_NO_ERR
975
0
#ifndef FIPS_MODULE
976
0
            case OSSL_FUNC_PROVIDER_GET_REASON_STRINGS:
977
0
                p_get_reason_strings = OSSL_FUNC_provider_get_reason_strings(provider_dispatch);
978
0
                break;
979
296
#endif
980
296
#endif
981
296
            }
982
296
        }
983
94
    }
984
985
94
#ifndef OPENSSL_NO_ERR
986
94
#ifndef FIPS_MODULE
987
94
    if (p_get_reason_strings != NULL) {
988
0
        const OSSL_ITEM *reasonstrings = p_get_reason_strings(prov->provctx);
989
0
        size_t cnt, cnt2;
990
991
        /*
992
         * ERR_load_strings() handles ERR_STRING_DATA rather than OSSL_ITEM,
993
         * although they are essentially the same type.
994
         * Furthermore, ERR_load_strings() patches the array's error number
995
         * with the error library number, so we need to make a copy of that
996
         * array either way.
997
         */
998
0
        cnt = 0;
999
0
        while (reasonstrings[cnt].id != 0) {
1000
0
            if (ERR_GET_LIB(reasonstrings[cnt].id) != 0)
1001
0
                goto end;
1002
0
            cnt++;
1003
0
        }
1004
0
        cnt++; /* One for the terminating item */
1005
1006
        /* Allocate one extra item for the "library" name */
1007
0
        prov->error_strings = OPENSSL_zalloc(sizeof(ERR_STRING_DATA) * (cnt + 1));
1008
0
        if (prov->error_strings == NULL)
1009
0
            goto end;
1010
1011
        /*
1012
         * Set the "library" name.
1013
         */
1014
0
        prov->error_strings[0].error = ERR_PACK(prov->error_lib, 0, 0);
1015
0
        prov->error_strings[0].string = prov->name;
1016
        /*
1017
         * Copy reasonstrings item 0..cnt-1 to prov->error_trings positions
1018
         * 1..cnt.
1019
         */
1020
0
        for (cnt2 = 1; cnt2 <= cnt; cnt2++) {
1021
0
            prov->error_strings[cnt2].error = (int)reasonstrings[cnt2 - 1].id;
1022
0
            prov->error_strings[cnt2].string = reasonstrings[cnt2 - 1].ptr;
1023
0
        }
1024
1025
0
        ERR_load_strings(prov->error_lib, prov->error_strings);
1026
0
    }
1027
94
#endif
1028
94
#endif
1029
1030
    /* With this flag set, this provider has become fully "loaded". */
1031
94
    prov->flag_initialized = 1;
1032
94
    ok = 1;
1033
1034
94
end:
1035
94
    return ok;
1036
94
}
1037
1038
/*
1039
 * Deactivate a provider. If upcalls is 0 then we suppress any upcalls to a
1040
 * parent provider. If removechildren is 0 then we suppress any calls to remove
1041
 * child providers.
1042
 * Return -1 on failure and the activation count on success
1043
 */
1044
static int provider_deactivate(OSSL_PROVIDER *prov, int upcalls,
1045
    int removechildren)
1046
1.76M
{
1047
1.76M
    int count;
1048
1.76M
    struct provider_store_st *store;
1049
1.76M
#ifndef FIPS_MODULE
1050
1.76M
    int freeparent = 0;
1051
1.76M
#endif
1052
1.76M
    int lock = 1;
1053
1054
1.76M
    if (!ossl_assert(prov != NULL))
1055
0
        return -1;
1056
1057
    /*
1058
     * No need to lock if we've got no store because we've not been shared with
1059
     * other threads.
1060
     */
1061
1.76M
    store = get_provider_store(prov->libctx);
1062
1.76M
    if (store == NULL)
1063
0
        lock = 0;
1064
1065
1.76M
    if (lock && !CRYPTO_THREAD_read_lock(store->lock))
1066
0
        return -1;
1067
1.76M
    if (lock && !CRYPTO_THREAD_write_lock(prov->flag_lock)) {
1068
0
        CRYPTO_THREAD_unlock(store->lock);
1069
0
        return -1;
1070
0
    }
1071
1072
1.76M
#ifndef FIPS_MODULE
1073
1.76M
    if (prov->activatecnt >= 2 && prov->ischild && upcalls) {
1074
        /*
1075
         * We have had a direct activation in this child libctx so we need to
1076
         * now down the ref count in the parent provider. We do the actual down
1077
         * ref outside of the flag_lock, since it could involve getting other
1078
         * locks.
1079
         */
1080
0
        freeparent = 1;
1081
0
    }
1082
1.76M
#endif
1083
1084
1.76M
    if ((count = --prov->activatecnt) < 1)
1085
17
        prov->flag_activated = 0;
1086
1.76M
#ifndef FIPS_MODULE
1087
1.76M
    else
1088
1.76M
        removechildren = 0;
1089
1.76M
#endif
1090
1091
1.76M
#ifndef FIPS_MODULE
1092
1.76M
    if (removechildren && store != NULL) {
1093
17
        int i, max = sk_OSSL_PROVIDER_CHILD_CB_num(store->child_cbs);
1094
17
        OSSL_PROVIDER_CHILD_CB *child_cb;
1095
1096
17
        for (i = 0; i < max; i++) {
1097
0
            child_cb = sk_OSSL_PROVIDER_CHILD_CB_value(store->child_cbs, i);
1098
0
            child_cb->remove_cb((OSSL_CORE_HANDLE *)prov, child_cb->cbdata);
1099
0
        }
1100
17
    }
1101
1.76M
#endif
1102
1.76M
    if (lock) {
1103
1.76M
        CRYPTO_THREAD_unlock(prov->flag_lock);
1104
1.76M
        CRYPTO_THREAD_unlock(store->lock);
1105
1.76M
    }
1106
1.76M
#ifndef FIPS_MODULE
1107
1.76M
    if (freeparent)
1108
0
        ossl_provider_free_parent(prov, 1);
1109
1.76M
#endif
1110
1111
    /* We don't deinit here, that's done in ossl_provider_free() */
1112
1.76M
    return count;
1113
1.76M
}
1114
1115
/*
1116
 * Activate a provider.
1117
 * Return -1 on failure and the activation count on success
1118
 */
1119
static int provider_activate(OSSL_PROVIDER *prov, int lock, int upcalls)
1120
1.76M
{
1121
1.76M
    int count = -1;
1122
1.76M
    struct provider_store_st *store;
1123
1.76M
    int ret = 1;
1124
1125
1.76M
    store = prov->store;
1126
    /*
1127
     * If the provider hasn't been added to the store, then we don't need
1128
     * any locks because we've not shared it with other threads.
1129
     */
1130
1.76M
    if (store == NULL) {
1131
17
        lock = 0;
1132
17
        if (!provider_init(prov))
1133
0
            return -1;
1134
17
    }
1135
1136
1.76M
#ifndef FIPS_MODULE
1137
1.76M
    if (prov->ischild && upcalls && !ossl_provider_up_ref_parent(prov, 1))
1138
0
        return -1;
1139
1.76M
#endif
1140
1141
1.76M
    if (lock && !CRYPTO_THREAD_read_lock(store->lock)) {
1142
0
#ifndef FIPS_MODULE
1143
0
        if (prov->ischild && upcalls)
1144
0
            ossl_provider_free_parent(prov, 1);
1145
0
#endif
1146
0
        return -1;
1147
0
    }
1148
1149
1.76M
    if (lock && !CRYPTO_THREAD_write_lock(prov->flag_lock)) {
1150
0
        CRYPTO_THREAD_unlock(store->lock);
1151
0
#ifndef FIPS_MODULE
1152
0
        if (prov->ischild && upcalls)
1153
0
            ossl_provider_free_parent(prov, 1);
1154
0
#endif
1155
0
        return -1;
1156
0
    }
1157
1158
1.76M
    count = ++prov->activatecnt;
1159
1.76M
    prov->flag_activated = 1;
1160
1161
1.76M
    if (prov->activatecnt == 1 && store != NULL) {
1162
0
        ret = create_provider_children(prov);
1163
0
    }
1164
1.76M
    if (lock) {
1165
0
        CRYPTO_THREAD_unlock(prov->flag_lock);
1166
0
        CRYPTO_THREAD_unlock(store->lock);
1167
0
    }
1168
1169
1.76M
    if (!ret)
1170
0
        return -1;
1171
1172
1.76M
    return count;
1173
1.76M
}
1174
1175
static int provider_flush_store_cache(const OSSL_PROVIDER *prov)
1176
136
{
1177
136
    struct provider_store_st *store;
1178
136
    int freeing;
1179
1180
136
    if ((store = get_provider_store(prov->libctx)) == NULL)
1181
0
        return 0;
1182
1183
136
    if (!CRYPTO_THREAD_read_lock(store->lock))
1184
0
        return 0;
1185
136
    freeing = store->freeing;
1186
136
    CRYPTO_THREAD_unlock(store->lock);
1187
1188
136
    if (!freeing) {
1189
136
        int acc
1190
136
            = evp_method_store_cache_flush(prov->libctx)
1191
136
#ifndef FIPS_MODULE
1192
136
            + ossl_encoder_store_cache_flush(prov->libctx)
1193
136
            + ossl_decoder_store_cache_flush(prov->libctx)
1194
136
            + ossl_store_loader_store_cache_flush(prov->libctx)
1195
136
#endif
1196
136
            ;
1197
1198
136
#ifndef FIPS_MODULE
1199
136
        return acc == 4;
1200
#else
1201
        return acc == 1;
1202
#endif
1203
136
    }
1204
0
    return 1;
1205
136
}
1206
1207
static int provider_remove_store_methods(OSSL_PROVIDER *prov)
1208
182
{
1209
182
    struct provider_store_st *store;
1210
182
    int freeing;
1211
1212
182
    if ((store = get_provider_store(prov->libctx)) == NULL)
1213
0
        return 0;
1214
1215
182
    if (!CRYPTO_THREAD_read_lock(store->lock))
1216
0
        return 0;
1217
182
    freeing = store->freeing;
1218
182
    CRYPTO_THREAD_unlock(store->lock);
1219
1220
182
    if (!freeing) {
1221
0
        int acc;
1222
1223
0
        if (!CRYPTO_THREAD_write_lock(prov->opbits_lock))
1224
0
            return 0;
1225
0
        OPENSSL_free(prov->operation_bits);
1226
0
        prov->operation_bits = NULL;
1227
0
        prov->operation_bits_sz = 0;
1228
0
        CRYPTO_THREAD_unlock(prov->opbits_lock);
1229
1230
0
        acc = evp_method_store_remove_all_provided(prov)
1231
0
#ifndef FIPS_MODULE
1232
0
            + ossl_encoder_store_remove_all_provided(prov)
1233
0
            + ossl_decoder_store_remove_all_provided(prov)
1234
0
            + ossl_store_loader_store_remove_all_provided(prov)
1235
0
#endif
1236
0
            ;
1237
1238
0
#ifndef FIPS_MODULE
1239
0
        return acc == 4;
1240
#else
1241
        return acc == 1;
1242
#endif
1243
0
    }
1244
182
    return 1;
1245
182
}
1246
1247
int ossl_provider_activate(OSSL_PROVIDER *prov, int upcalls, int aschild)
1248
136
{
1249
136
    int count;
1250
1251
136
    if (prov == NULL)
1252
0
        return 0;
1253
136
#ifndef FIPS_MODULE
1254
    /*
1255
     * If aschild is true, then we only actually do the activation if the
1256
     * provider is a child. If its not, this is still success.
1257
     */
1258
136
    if (aschild && !prov->ischild)
1259
0
        return 1;
1260
136
#endif
1261
136
    if ((count = provider_activate(prov, 1, upcalls)) > 0)
1262
136
        return count == 1 ? provider_flush_store_cache(prov) : 1;
1263
1264
0
    return 0;
1265
136
}
1266
1267
int ossl_provider_deactivate(OSSL_PROVIDER *prov, int removechildren)
1268
182
{
1269
182
    int count;
1270
1271
182
    if (prov == NULL
1272
182
        || (count = provider_deactivate(prov, 1, removechildren)) < 0)
1273
0
        return 0;
1274
182
    return count == 0 ? provider_remove_store_methods(prov) : 1;
1275
182
}
1276
1277
void *ossl_provider_ctx(const OSSL_PROVIDER *prov)
1278
30.5M
{
1279
30.5M
    return prov != NULL ? prov->provctx : NULL;
1280
30.5M
}
1281
1282
/*
1283
 * This function only does something once when store->use_fallbacks == 1,
1284
 * and then sets store->use_fallbacks = 0, so the second call and so on is
1285
 * effectively a no-op.
1286
 */
1287
static int provider_activate_fallbacks(struct provider_store_st *store)
1288
2.16M
{
1289
2.16M
    int use_fallbacks;
1290
2.16M
    int activated_fallback_count = 0;
1291
2.16M
    int ret = 0;
1292
2.16M
    const OSSL_PROVIDER_INFO *p;
1293
1294
2.16M
    if (!CRYPTO_THREAD_read_lock(store->lock))
1295
0
        return 0;
1296
2.16M
    use_fallbacks = store->use_fallbacks;
1297
2.16M
    CRYPTO_THREAD_unlock(store->lock);
1298
2.16M
    if (!use_fallbacks)
1299
2.16M
        return 1;
1300
1301
36
    if (!CRYPTO_THREAD_write_lock(store->lock))
1302
0
        return 0;
1303
    /* Check again, just in case another thread changed it */
1304
36
    use_fallbacks = store->use_fallbacks;
1305
36
    if (!use_fallbacks) {
1306
0
        CRYPTO_THREAD_unlock(store->lock);
1307
0
        return 1;
1308
0
    }
1309
1310
144
    for (p = ossl_predefined_providers; p->name != NULL; p++) {
1311
108
        OSSL_PROVIDER *prov = NULL;
1312
1313
108
        if (!p->is_fallback)
1314
72
            continue;
1315
        /*
1316
         * We use the internal constructor directly here,
1317
         * otherwise we get a call loop
1318
         */
1319
36
        prov = provider_new(p->name, p->init, NULL);
1320
36
        if (prov == NULL)
1321
0
            goto err;
1322
36
        prov->libctx = store->libctx;
1323
36
#ifndef FIPS_MODULE
1324
36
        prov->error_lib = ERR_get_next_error_library();
1325
36
#endif
1326
1327
        /*
1328
         * We are calling provider_activate while holding the store lock. This
1329
         * means the init function will be called while holding a lock. Normally
1330
         * we try to avoid calling a user callback while holding a lock.
1331
         * However, fallbacks are never third party providers so we accept this.
1332
         */
1333
36
        if (provider_activate(prov, 0, 0) < 0) {
1334
0
            ossl_provider_free(prov);
1335
0
            goto err;
1336
0
        }
1337
36
        prov->store = store;
1338
36
        if (sk_OSSL_PROVIDER_push(store->providers, prov) == 0) {
1339
0
            ossl_provider_free(prov);
1340
0
            goto err;
1341
0
        }
1342
36
        activated_fallback_count++;
1343
36
    }
1344
1345
36
    if (activated_fallback_count > 0) {
1346
36
        store->use_fallbacks = 0;
1347
36
        ret = 1;
1348
36
    }
1349
36
err:
1350
36
    CRYPTO_THREAD_unlock(store->lock);
1351
36
    return ret;
1352
36
}
1353
1354
int ossl_provider_doall_activated(OSSL_LIB_CTX *ctx,
1355
    int (*cb)(OSSL_PROVIDER *provider,
1356
        void *cbdata),
1357
    void *cbdata)
1358
973k
{
1359
973k
    int ret = 0, curr, max, ref = 0;
1360
973k
    struct provider_store_st *store = get_provider_store(ctx);
1361
973k
    STACK_OF(OSSL_PROVIDER) *provs = NULL;
1362
1363
973k
#if !defined(FIPS_MODULE) && !defined(OPENSSL_NO_AUTOLOAD_CONFIG)
1364
    /*
1365
     * Make sure any providers are loaded from config before we try to use
1366
     * them.
1367
     */
1368
973k
    if (ossl_lib_ctx_is_default(ctx))
1369
973k
        OPENSSL_init_crypto(OPENSSL_INIT_LOAD_CONFIG, NULL);
1370
973k
#endif
1371
1372
973k
    if (store == NULL)
1373
0
        return 1;
1374
973k
    if (!provider_activate_fallbacks(store))
1375
0
        return 0;
1376
1377
    /*
1378
     * Under lock, grab a copy of the provider list and up_ref each
1379
     * provider so that they don't disappear underneath us.
1380
     */
1381
973k
    if (!CRYPTO_THREAD_read_lock(store->lock))
1382
0
        return 0;
1383
973k
    provs = sk_OSSL_PROVIDER_dup(store->providers);
1384
973k
    if (provs == NULL) {
1385
0
        CRYPTO_THREAD_unlock(store->lock);
1386
0
        return 0;
1387
0
    }
1388
973k
    max = sk_OSSL_PROVIDER_num(provs);
1389
    /*
1390
     * We work backwards through the stack so that we can safely delete items
1391
     * as we go.
1392
     */
1393
2.73M
    for (curr = max - 1; curr >= 0; curr--) {
1394
1.76M
        OSSL_PROVIDER *prov = sk_OSSL_PROVIDER_value(provs, curr);
1395
1396
1.76M
        if (!CRYPTO_THREAD_write_lock(prov->flag_lock))
1397
0
            goto err_unlock;
1398
1.76M
        if (prov->flag_activated) {
1399
            /*
1400
             * We call CRYPTO_UP_REF directly rather than ossl_provider_up_ref
1401
             * to avoid upping the ref count on the parent provider, which we
1402
             * must not do while holding locks.
1403
             */
1404
1.76M
            if (CRYPTO_UP_REF(&prov->refcnt, &ref, prov->refcnt_lock) <= 0) {
1405
0
                CRYPTO_THREAD_unlock(prov->flag_lock);
1406
0
                goto err_unlock;
1407
0
            }
1408
            /*
1409
             * It's already activated, but we up the activated count to ensure
1410
             * it remains activated until after we've called the user callback.
1411
             * We do this with no locking (because we already hold the locks)
1412
             * and no upcalls (which must not be called when locks are held). In
1413
             * theory this could mean the parent provider goes inactive, whilst
1414
             * still activated in the child for a short period. That's ok.
1415
             */
1416
1.76M
            if (provider_activate(prov, 0, 0) < 0) {
1417
0
                CRYPTO_DOWN_REF(&prov->refcnt, &ref, prov->refcnt_lock);
1418
0
                CRYPTO_THREAD_unlock(prov->flag_lock);
1419
0
                goto err_unlock;
1420
0
            }
1421
1.76M
        } else {
1422
0
            sk_OSSL_PROVIDER_delete(provs, curr);
1423
0
            max--;
1424
0
        }
1425
1.76M
        CRYPTO_THREAD_unlock(prov->flag_lock);
1426
1.76M
    }
1427
973k
    CRYPTO_THREAD_unlock(store->lock);
1428
1429
    /*
1430
     * Now, we sweep through all providers not under lock
1431
     */
1432
2.73M
    for (curr = 0; curr < max; curr++) {
1433
1.76M
        OSSL_PROVIDER *prov = sk_OSSL_PROVIDER_value(provs, curr);
1434
1435
1.76M
        if (!cb(prov, cbdata)) {
1436
0
            curr = -1;
1437
0
            goto finish;
1438
0
        }
1439
1.76M
    }
1440
973k
    curr = -1;
1441
1442
973k
    ret = 1;
1443
973k
    goto finish;
1444
1445
0
err_unlock:
1446
0
    CRYPTO_THREAD_unlock(store->lock);
1447
973k
finish:
1448
    /*
1449
     * The pop_free call doesn't do what we want on an error condition. We
1450
     * either start from the first item in the stack, or part way through if
1451
     * we only processed some of the items.
1452
     */
1453
2.73M
    for (curr++; curr < max; curr++) {
1454
1.76M
        OSSL_PROVIDER *prov = sk_OSSL_PROVIDER_value(provs, curr);
1455
1456
1.76M
        provider_deactivate(prov, 0, 1);
1457
        /*
1458
         * As above where we did the up-ref, we don't call ossl_provider_free
1459
         * to avoid making upcalls. There should always be at least one ref
1460
         * to the provider in the store, so this should never drop to 0.
1461
         */
1462
1.76M
        CRYPTO_DOWN_REF(&prov->refcnt, &ref, prov->refcnt_lock);
1463
        /*
1464
         * Not much we can do if this assert ever fails. So we don't use
1465
         * ossl_assert here.
1466
         */
1467
1.76M
        assert(ref > 0);
1468
1.76M
    }
1469
973k
    sk_OSSL_PROVIDER_free(provs);
1470
973k
    return ret;
1471
973k
}
1472
1473
int OSSL_PROVIDER_available(OSSL_LIB_CTX *libctx, const char *name)
1474
0
{
1475
0
    OSSL_PROVIDER *prov = NULL;
1476
0
    int available = 0;
1477
0
    struct provider_store_st *store = get_provider_store(libctx);
1478
1479
0
    if (store == NULL || !provider_activate_fallbacks(store))
1480
0
        return 0;
1481
1482
0
    prov = ossl_provider_find(libctx, name, 0);
1483
0
    if (prov != NULL) {
1484
0
        if (!CRYPTO_THREAD_read_lock(prov->flag_lock))
1485
0
            return 0;
1486
0
        available = prov->flag_activated;
1487
0
        CRYPTO_THREAD_unlock(prov->flag_lock);
1488
0
        ossl_provider_free(prov);
1489
0
    }
1490
0
    return available;
1491
0
}
1492
1493
/* Setters of Provider Object data */
1494
int ossl_provider_set_fallback(OSSL_PROVIDER *prov)
1495
0
{
1496
0
    if (prov == NULL)
1497
0
        return 0;
1498
1499
0
    prov->flag_fallback = 1;
1500
0
    return 1;
1501
0
}
1502
1503
/* Getters of Provider Object data */
1504
const char *ossl_provider_name(const OSSL_PROVIDER *prov)
1505
1.32k
{
1506
1.32k
    return prov->name;
1507
1.32k
}
1508
1509
const DSO *ossl_provider_dso(const OSSL_PROVIDER *prov)
1510
0
{
1511
0
    return prov->module;
1512
0
}
1513
1514
const char *ossl_provider_module_name(const OSSL_PROVIDER *prov)
1515
0
{
1516
#ifdef FIPS_MODULE
1517
    return NULL;
1518
#else
1519
0
    return DSO_get_filename(prov->module);
1520
0
#endif
1521
0
}
1522
1523
const char *ossl_provider_module_path(const OSSL_PROVIDER *prov)
1524
0
{
1525
#ifdef FIPS_MODULE
1526
    return NULL;
1527
#else
1528
    /* FIXME: Ensure it's a full path */
1529
0
    return DSO_get_filename(prov->module);
1530
0
#endif
1531
0
}
1532
1533
void *ossl_provider_prov_ctx(const OSSL_PROVIDER *prov)
1534
201M
{
1535
201M
    if (prov != NULL)
1536
201M
        return prov->provctx;
1537
1538
0
    return NULL;
1539
201M
}
1540
1541
const OSSL_DISPATCH *ossl_provider_get0_dispatch(const OSSL_PROVIDER *prov)
1542
0
{
1543
0
    if (prov != NULL)
1544
0
        return prov->dispatch;
1545
1546
0
    return NULL;
1547
0
}
1548
1549
OSSL_LIB_CTX *ossl_provider_libctx(const OSSL_PROVIDER *prov)
1550
256M
{
1551
256M
    return prov != NULL ? prov->libctx : NULL;
1552
256M
}
1553
1554
/* Wrappers around calls to the provider */
1555
void ossl_provider_teardown(const OSSL_PROVIDER *prov)
1556
34
{
1557
34
    if (prov->teardown != NULL
1558
34
#ifndef FIPS_MODULE
1559
34
        && !prov->ischild
1560
34
#endif
1561
34
    )
1562
34
        prov->teardown(prov->provctx);
1563
34
}
1564
1565
const OSSL_PARAM *ossl_provider_gettable_params(const OSSL_PROVIDER *prov)
1566
0
{
1567
0
    return prov->gettable_params == NULL
1568
0
        ? NULL
1569
0
        : prov->gettable_params(prov->provctx);
1570
0
}
1571
1572
int ossl_provider_get_params(const OSSL_PROVIDER *prov, OSSL_PARAM params[])
1573
0
{
1574
0
    return prov->get_params == NULL
1575
0
        ? 0
1576
0
        : prov->get_params(prov->provctx, params);
1577
0
}
1578
1579
int ossl_provider_self_test(const OSSL_PROVIDER *prov)
1580
0
{
1581
0
    int ret;
1582
1583
0
    if (prov->self_test == NULL)
1584
0
        return 1;
1585
0
    ret = prov->self_test(prov->provctx);
1586
0
    if (ret == 0)
1587
0
        (void)provider_remove_store_methods((OSSL_PROVIDER *)prov);
1588
0
    return ret;
1589
0
}
1590
1591
int ossl_provider_get_capabilities(const OSSL_PROVIDER *prov,
1592
    const char *capability,
1593
    OSSL_CALLBACK *cb,
1594
    void *arg)
1595
268k
{
1596
268k
    return prov->get_capabilities == NULL
1597
268k
        ? 1
1598
268k
        : prov->get_capabilities(prov->provctx, capability, cb, arg);
1599
268k
}
1600
1601
const OSSL_ALGORITHM *ossl_provider_query_operation(const OSSL_PROVIDER *prov,
1602
    int operation_id,
1603
    int *no_cache)
1604
3.87M
{
1605
3.87M
    const OSSL_ALGORITHM *res;
1606
1607
3.87M
    if (prov->query_operation == NULL)
1608
0
        return NULL;
1609
3.87M
    res = prov->query_operation(prov->provctx, operation_id, no_cache);
1610
#if defined(OPENSSL_NO_CACHED_FETCH)
1611
    /* Forcing the non-caching of queries */
1612
    if (no_cache != NULL)
1613
        *no_cache = 1;
1614
#endif
1615
3.87M
    return res;
1616
3.87M
}
1617
1618
void ossl_provider_unquery_operation(const OSSL_PROVIDER *prov,
1619
    int operation_id,
1620
    const OSSL_ALGORITHM *algs)
1621
3.87M
{
1622
3.87M
    if (prov->unquery_operation != NULL)
1623
0
        prov->unquery_operation(prov->provctx, operation_id, algs);
1624
3.87M
}
1625
1626
int ossl_provider_set_operation_bit(OSSL_PROVIDER *provider, size_t bitnum)
1627
1.04k
{
1628
1.04k
    size_t byte = bitnum / 8;
1629
1.04k
    unsigned char bit = (1 << (bitnum % 8)) & 0xFF;
1630
1631
1.04k
    if (!CRYPTO_THREAD_write_lock(provider->opbits_lock))
1632
0
        return 0;
1633
1.04k
    if (provider->operation_bits_sz <= byte) {
1634
358
        unsigned char *tmp = OPENSSL_realloc(provider->operation_bits,
1635
358
            byte + 1);
1636
1637
358
        if (tmp == NULL) {
1638
0
            CRYPTO_THREAD_unlock(provider->opbits_lock);
1639
0
            ERR_raise(ERR_LIB_CRYPTO, ERR_R_MALLOC_FAILURE);
1640
0
            return 0;
1641
0
        }
1642
358
        provider->operation_bits = tmp;
1643
358
        memset(provider->operation_bits + provider->operation_bits_sz,
1644
358
            '\0', byte + 1 - provider->operation_bits_sz);
1645
358
        provider->operation_bits_sz = byte + 1;
1646
358
    }
1647
1.04k
    provider->operation_bits[byte] |= bit;
1648
1.04k
    CRYPTO_THREAD_unlock(provider->opbits_lock);
1649
1.04k
    return 1;
1650
1.04k
}
1651
1652
int ossl_provider_test_operation_bit(OSSL_PROVIDER *provider, size_t bitnum,
1653
    int *result)
1654
7.02M
{
1655
7.02M
    size_t byte = bitnum / 8;
1656
7.02M
    unsigned char bit = (1 << (bitnum % 8)) & 0xFF;
1657
1658
7.02M
    if (!ossl_assert(result != NULL)) {
1659
0
        ERR_raise(ERR_LIB_CRYPTO, ERR_R_PASSED_NULL_PARAMETER);
1660
0
        return 0;
1661
0
    }
1662
1663
7.02M
    *result = 0;
1664
7.02M
    if (!CRYPTO_THREAD_read_lock(provider->opbits_lock))
1665
0
        return 0;
1666
7.02M
    if (provider->operation_bits_sz > byte)
1667
7.02M
        *result = ((provider->operation_bits[byte] & bit) != 0);
1668
7.02M
    CRYPTO_THREAD_unlock(provider->opbits_lock);
1669
7.02M
    return 1;
1670
7.02M
}
1671
1672
#ifndef FIPS_MODULE
1673
const OSSL_CORE_HANDLE *ossl_provider_get_parent(OSSL_PROVIDER *prov)
1674
0
{
1675
0
    return prov->handle;
1676
0
}
1677
1678
int ossl_provider_is_child(const OSSL_PROVIDER *prov)
1679
0
{
1680
0
    return prov->ischild;
1681
0
}
1682
1683
int ossl_provider_set_child(OSSL_PROVIDER *prov, const OSSL_CORE_HANDLE *handle)
1684
0
{
1685
0
    prov->handle = handle;
1686
0
    prov->ischild = 1;
1687
1688
0
    return 1;
1689
0
}
1690
1691
int ossl_provider_default_props_update(OSSL_LIB_CTX *libctx, const char *props)
1692
0
{
1693
0
#ifndef FIPS_MODULE
1694
0
    struct provider_store_st *store = NULL;
1695
0
    int i, max;
1696
0
    OSSL_PROVIDER_CHILD_CB *child_cb;
1697
1698
0
    if ((store = get_provider_store(libctx)) == NULL)
1699
0
        return 0;
1700
1701
0
    if (!CRYPTO_THREAD_read_lock(store->lock))
1702
0
        return 0;
1703
1704
0
    max = sk_OSSL_PROVIDER_CHILD_CB_num(store->child_cbs);
1705
0
    for (i = 0; i < max; i++) {
1706
0
        child_cb = sk_OSSL_PROVIDER_CHILD_CB_value(store->child_cbs, i);
1707
0
        child_cb->global_props_cb(props, child_cb->cbdata);
1708
0
    }
1709
1710
0
    CRYPTO_THREAD_unlock(store->lock);
1711
0
#endif
1712
0
    return 1;
1713
0
}
1714
1715
static int ossl_provider_register_child_cb(const OSSL_CORE_HANDLE *handle,
1716
    int (*create_cb)(
1717
        const OSSL_CORE_HANDLE *provider,
1718
        void *cbdata),
1719
    int (*remove_cb)(
1720
        const OSSL_CORE_HANDLE *provider,
1721
        void *cbdata),
1722
    int (*global_props_cb)(
1723
        const char *props,
1724
        void *cbdata),
1725
    void *cbdata)
1726
0
{
1727
    /*
1728
     * This is really an OSSL_PROVIDER that we created and cast to
1729
     * OSSL_CORE_HANDLE originally. Therefore it is safe to cast it back.
1730
     */
1731
0
    OSSL_PROVIDER *thisprov = (OSSL_PROVIDER *)handle;
1732
0
    OSSL_PROVIDER *prov;
1733
0
    OSSL_LIB_CTX *libctx = thisprov->libctx;
1734
0
    struct provider_store_st *store = NULL;
1735
0
    int ret = 0, i, max;
1736
0
    OSSL_PROVIDER_CHILD_CB *child_cb;
1737
0
    char *propsstr = NULL;
1738
1739
0
    if ((store = get_provider_store(libctx)) == NULL)
1740
0
        return 0;
1741
1742
0
    child_cb = OPENSSL_malloc(sizeof(*child_cb));
1743
0
    if (child_cb == NULL)
1744
0
        return 0;
1745
0
    child_cb->prov = thisprov;
1746
0
    child_cb->create_cb = create_cb;
1747
0
    child_cb->remove_cb = remove_cb;
1748
0
    child_cb->global_props_cb = global_props_cb;
1749
0
    child_cb->cbdata = cbdata;
1750
1751
0
    if (!CRYPTO_THREAD_write_lock(store->lock)) {
1752
0
        OPENSSL_free(child_cb);
1753
0
        return 0;
1754
0
    }
1755
0
    propsstr = evp_get_global_properties_str(libctx, 0);
1756
1757
0
    if (propsstr != NULL) {
1758
0
        global_props_cb(propsstr, cbdata);
1759
0
        OPENSSL_free(propsstr);
1760
0
    }
1761
0
    max = sk_OSSL_PROVIDER_num(store->providers);
1762
0
    for (i = 0; i < max; i++) {
1763
0
        int activated;
1764
1765
0
        prov = sk_OSSL_PROVIDER_value(store->providers, i);
1766
1767
0
        if (!CRYPTO_THREAD_read_lock(prov->flag_lock))
1768
0
            break;
1769
0
        activated = prov->flag_activated;
1770
0
        CRYPTO_THREAD_unlock(prov->flag_lock);
1771
        /*
1772
         * We hold the store lock while calling the user callback. This means
1773
         * that the user callback must be short and simple and not do anything
1774
         * likely to cause a deadlock. We don't hold the flag_lock during this
1775
         * call. In theory this means that another thread could deactivate it
1776
         * while we are calling create. This is ok because the other thread
1777
         * will also call remove_cb, but won't be able to do so until we release
1778
         * the store lock.
1779
         */
1780
0
        if (activated && !create_cb((OSSL_CORE_HANDLE *)prov, cbdata))
1781
0
            break;
1782
0
    }
1783
0
    if (i == max) {
1784
        /* Success */
1785
0
        ret = sk_OSSL_PROVIDER_CHILD_CB_push(store->child_cbs, child_cb);
1786
0
    }
1787
0
    if (i != max || ret <= 0) {
1788
        /* Failed during creation. Remove everything we just added */
1789
0
        for (; i >= 0; i--) {
1790
0
            prov = sk_OSSL_PROVIDER_value(store->providers, i);
1791
0
            remove_cb((OSSL_CORE_HANDLE *)prov, cbdata);
1792
0
        }
1793
0
        OPENSSL_free(child_cb);
1794
0
        ret = 0;
1795
0
    }
1796
0
    CRYPTO_THREAD_unlock(store->lock);
1797
1798
0
    return ret;
1799
0
}
1800
1801
static void ossl_provider_deregister_child_cb(const OSSL_CORE_HANDLE *handle)
1802
0
{
1803
    /*
1804
     * This is really an OSSL_PROVIDER that we created and cast to
1805
     * OSSL_CORE_HANDLE originally. Therefore it is safe to cast it back.
1806
     */
1807
0
    OSSL_PROVIDER *thisprov = (OSSL_PROVIDER *)handle;
1808
0
    OSSL_LIB_CTX *libctx = thisprov->libctx;
1809
0
    struct provider_store_st *store = NULL;
1810
0
    int i, max;
1811
0
    OSSL_PROVIDER_CHILD_CB *child_cb;
1812
1813
0
    if ((store = get_provider_store(libctx)) == NULL)
1814
0
        return;
1815
1816
0
    if (!CRYPTO_THREAD_write_lock(store->lock))
1817
0
        return;
1818
0
    max = sk_OSSL_PROVIDER_CHILD_CB_num(store->child_cbs);
1819
0
    for (i = 0; i < max; i++) {
1820
0
        child_cb = sk_OSSL_PROVIDER_CHILD_CB_value(store->child_cbs, i);
1821
0
        if (child_cb->prov == thisprov) {
1822
            /* Found an entry */
1823
0
            sk_OSSL_PROVIDER_CHILD_CB_delete(store->child_cbs, i);
1824
0
            OPENSSL_free(child_cb);
1825
0
            break;
1826
0
        }
1827
0
    }
1828
0
    CRYPTO_THREAD_unlock(store->lock);
1829
0
}
1830
#endif
1831
1832
/*-
1833
 * Core functions for the provider
1834
 * ===============================
1835
 *
1836
 * This is the set of functions that the core makes available to the provider
1837
 */
1838
1839
/*
1840
 * This returns a list of Provider Object parameters with their types, for
1841
 * discovery.  We do not expect that many providers will use this, but one
1842
 * never knows.
1843
 */
1844
static const OSSL_PARAM param_types[] = {
1845
    OSSL_PARAM_DEFN(OSSL_PROV_PARAM_CORE_VERSION, OSSL_PARAM_UTF8_PTR, NULL, 0),
1846
    OSSL_PARAM_DEFN(OSSL_PROV_PARAM_CORE_PROV_NAME, OSSL_PARAM_UTF8_PTR,
1847
        NULL, 0),
1848
#ifndef FIPS_MODULE
1849
    OSSL_PARAM_DEFN(OSSL_PROV_PARAM_CORE_MODULE_FILENAME, OSSL_PARAM_UTF8_PTR,
1850
        NULL, 0),
1851
#endif
1852
    OSSL_PARAM_END
1853
};
1854
1855
/*
1856
 * Forward declare all the functions that are provided aa dispatch.
1857
 * This ensures that the compiler will complain if they aren't defined
1858
 * with the correct signature.
1859
 */
1860
static OSSL_FUNC_core_gettable_params_fn core_gettable_params;
1861
static OSSL_FUNC_core_get_params_fn core_get_params;
1862
static OSSL_FUNC_core_get_libctx_fn core_get_libctx;
1863
static OSSL_FUNC_core_thread_start_fn core_thread_start;
1864
#ifndef FIPS_MODULE
1865
static OSSL_FUNC_core_new_error_fn core_new_error;
1866
static OSSL_FUNC_core_set_error_debug_fn core_set_error_debug;
1867
static OSSL_FUNC_core_vset_error_fn core_vset_error;
1868
static OSSL_FUNC_core_set_error_mark_fn core_set_error_mark;
1869
static OSSL_FUNC_core_clear_last_error_mark_fn core_clear_last_error_mark;
1870
static OSSL_FUNC_core_pop_error_to_mark_fn core_pop_error_to_mark;
1871
OSSL_FUNC_BIO_new_file_fn ossl_core_bio_new_file;
1872
OSSL_FUNC_BIO_new_membuf_fn ossl_core_bio_new_mem_buf;
1873
OSSL_FUNC_BIO_read_ex_fn ossl_core_bio_read_ex;
1874
OSSL_FUNC_BIO_write_ex_fn ossl_core_bio_write_ex;
1875
OSSL_FUNC_BIO_gets_fn ossl_core_bio_gets;
1876
OSSL_FUNC_BIO_puts_fn ossl_core_bio_puts;
1877
OSSL_FUNC_BIO_up_ref_fn ossl_core_bio_up_ref;
1878
OSSL_FUNC_BIO_free_fn ossl_core_bio_free;
1879
OSSL_FUNC_BIO_vprintf_fn ossl_core_bio_vprintf;
1880
OSSL_FUNC_BIO_vsnprintf_fn BIO_vsnprintf;
1881
static OSSL_FUNC_self_test_cb_fn core_self_test_get_callback;
1882
OSSL_FUNC_get_entropy_fn ossl_rand_get_entropy;
1883
OSSL_FUNC_cleanup_entropy_fn ossl_rand_cleanup_entropy;
1884
OSSL_FUNC_get_nonce_fn ossl_rand_get_nonce;
1885
OSSL_FUNC_cleanup_nonce_fn ossl_rand_cleanup_nonce;
1886
#endif
1887
OSSL_FUNC_CRYPTO_malloc_fn CRYPTO_malloc;
1888
OSSL_FUNC_CRYPTO_zalloc_fn CRYPTO_zalloc;
1889
OSSL_FUNC_CRYPTO_free_fn CRYPTO_free;
1890
OSSL_FUNC_CRYPTO_clear_free_fn CRYPTO_clear_free;
1891
OSSL_FUNC_CRYPTO_realloc_fn CRYPTO_realloc;
1892
OSSL_FUNC_CRYPTO_clear_realloc_fn CRYPTO_clear_realloc;
1893
OSSL_FUNC_CRYPTO_secure_malloc_fn CRYPTO_secure_malloc;
1894
OSSL_FUNC_CRYPTO_secure_zalloc_fn CRYPTO_secure_zalloc;
1895
OSSL_FUNC_CRYPTO_secure_free_fn CRYPTO_secure_free;
1896
OSSL_FUNC_CRYPTO_secure_clear_free_fn CRYPTO_secure_clear_free;
1897
OSSL_FUNC_CRYPTO_secure_allocated_fn CRYPTO_secure_allocated;
1898
OSSL_FUNC_OPENSSL_cleanse_fn OPENSSL_cleanse;
1899
#ifndef FIPS_MODULE
1900
OSSL_FUNC_provider_register_child_cb_fn ossl_provider_register_child_cb;
1901
OSSL_FUNC_provider_deregister_child_cb_fn ossl_provider_deregister_child_cb;
1902
static OSSL_FUNC_provider_name_fn core_provider_get0_name;
1903
static OSSL_FUNC_provider_get0_provider_ctx_fn core_provider_get0_provider_ctx;
1904
static OSSL_FUNC_provider_get0_dispatch_fn core_provider_get0_dispatch;
1905
static OSSL_FUNC_provider_up_ref_fn core_provider_up_ref_intern;
1906
static OSSL_FUNC_provider_free_fn core_provider_free_intern;
1907
static OSSL_FUNC_core_obj_add_sigid_fn core_obj_add_sigid;
1908
static OSSL_FUNC_core_obj_create_fn core_obj_create;
1909
#endif
1910
1911
static const OSSL_PARAM *core_gettable_params(const OSSL_CORE_HANDLE *handle)
1912
0
{
1913
0
    return param_types;
1914
0
}
1915
1916
static int core_get_params(const OSSL_CORE_HANDLE *handle, OSSL_PARAM params[])
1917
0
{
1918
0
    int i;
1919
0
    OSSL_PARAM *p;
1920
    /*
1921
     * We created this object originally and we know it is actually an
1922
     * OSSL_PROVIDER *, so the cast is safe
1923
     */
1924
0
    OSSL_PROVIDER *prov = (OSSL_PROVIDER *)handle;
1925
1926
0
    if ((p = OSSL_PARAM_locate(params, OSSL_PROV_PARAM_CORE_VERSION)) != NULL)
1927
0
        OSSL_PARAM_set_utf8_ptr(p, OPENSSL_VERSION_STR);
1928
0
    if ((p = OSSL_PARAM_locate(params, OSSL_PROV_PARAM_CORE_PROV_NAME)) != NULL)
1929
0
        OSSL_PARAM_set_utf8_ptr(p, prov->name);
1930
1931
0
#ifndef FIPS_MODULE
1932
0
    if ((p = OSSL_PARAM_locate(params,
1933
0
             OSSL_PROV_PARAM_CORE_MODULE_FILENAME))
1934
0
        != NULL)
1935
0
        OSSL_PARAM_set_utf8_ptr(p, ossl_provider_module_path(prov));
1936
0
#endif
1937
1938
0
    if (prov->parameters == NULL)
1939
0
        return 1;
1940
1941
0
    for (i = 0; i < sk_INFOPAIR_num(prov->parameters); i++) {
1942
0
        INFOPAIR *pair = sk_INFOPAIR_value(prov->parameters, i);
1943
1944
0
        if ((p = OSSL_PARAM_locate(params, pair->name)) != NULL)
1945
0
            OSSL_PARAM_set_utf8_ptr(p, pair->value);
1946
0
    }
1947
0
    return 1;
1948
0
}
1949
1950
static OPENSSL_CORE_CTX *core_get_libctx(const OSSL_CORE_HANDLE *handle)
1951
3.04k
{
1952
    /*
1953
     * We created this object originally and we know it is actually an
1954
     * OSSL_PROVIDER *, so the cast is safe
1955
     */
1956
3.04k
    OSSL_PROVIDER *prov = (OSSL_PROVIDER *)handle;
1957
1958
    /*
1959
     * Using ossl_provider_libctx would be wrong as that returns
1960
     * NULL for |prov| == NULL and NULL libctx has a special meaning
1961
     * that does not apply here. Here |prov| == NULL can happen only in
1962
     * case of a coding error.
1963
     */
1964
3.04k
    assert(prov != NULL);
1965
3.04k
    return (OPENSSL_CORE_CTX *)prov->libctx;
1966
3.04k
}
1967
1968
static int core_thread_start(const OSSL_CORE_HANDLE *handle,
1969
    OSSL_thread_stop_handler_fn handfn,
1970
    void *arg)
1971
0
{
1972
    /*
1973
     * We created this object originally and we know it is actually an
1974
     * OSSL_PROVIDER *, so the cast is safe
1975
     */
1976
0
    OSSL_PROVIDER *prov = (OSSL_PROVIDER *)handle;
1977
1978
0
    return ossl_init_thread_start(prov, arg, handfn);
1979
0
}
1980
1981
/*
1982
 * The FIPS module inner provider doesn't implement these.  They aren't
1983
 * needed there, since the FIPS module upcalls are always the outer provider
1984
 * ones.
1985
 */
1986
#ifndef FIPS_MODULE
1987
/*
1988
 * These error functions should use |handle| to select the proper
1989
 * library context to report in the correct error stack if error
1990
 * stacks become tied to the library context.
1991
 * We cannot currently do that since there's no support for it in the
1992
 * ERR subsystem.
1993
 */
1994
static void core_new_error(const OSSL_CORE_HANDLE *handle)
1995
0
{
1996
0
    ERR_new();
1997
0
}
1998
1999
static void core_set_error_debug(const OSSL_CORE_HANDLE *handle,
2000
    const char *file, int line, const char *func)
2001
0
{
2002
0
    ERR_set_debug(file, line, func);
2003
0
}
2004
2005
static void core_vset_error(const OSSL_CORE_HANDLE *handle,
2006
    uint32_t reason, const char *fmt, va_list args)
2007
0
{
2008
    /*
2009
     * We created this object originally and we know it is actually an
2010
     * OSSL_PROVIDER *, so the cast is safe
2011
     */
2012
0
    OSSL_PROVIDER *prov = (OSSL_PROVIDER *)handle;
2013
2014
    /*
2015
     * If the uppermost 8 bits are non-zero, it's an OpenSSL library
2016
     * error and will be treated as such.  Otherwise, it's a new style
2017
     * provider error and will be treated as such.
2018
     */
2019
0
    if (ERR_GET_LIB(reason) != 0) {
2020
0
        ERR_vset_error(ERR_GET_LIB(reason), ERR_GET_REASON(reason), fmt, args);
2021
0
    } else {
2022
0
        ERR_vset_error(prov->error_lib, (int)reason, fmt, args);
2023
0
    }
2024
0
}
2025
2026
static int core_set_error_mark(const OSSL_CORE_HANDLE *handle)
2027
0
{
2028
0
    return ERR_set_mark();
2029
0
}
2030
2031
static int core_clear_last_error_mark(const OSSL_CORE_HANDLE *handle)
2032
0
{
2033
0
    return ERR_clear_last_mark();
2034
0
}
2035
2036
static int core_pop_error_to_mark(const OSSL_CORE_HANDLE *handle)
2037
0
{
2038
0
    return ERR_pop_to_mark();
2039
0
}
2040
2041
static void core_self_test_get_callback(OPENSSL_CORE_CTX *libctx,
2042
    OSSL_CALLBACK **cb, void **cbarg)
2043
0
{
2044
0
    OSSL_SELF_TEST_get_callback((OSSL_LIB_CTX *)libctx, cb, cbarg);
2045
0
}
2046
2047
static const char *core_provider_get0_name(const OSSL_CORE_HANDLE *prov)
2048
0
{
2049
0
    return OSSL_PROVIDER_get0_name((const OSSL_PROVIDER *)prov);
2050
0
}
2051
2052
static void *core_provider_get0_provider_ctx(const OSSL_CORE_HANDLE *prov)
2053
0
{
2054
0
    return OSSL_PROVIDER_get0_provider_ctx((const OSSL_PROVIDER *)prov);
2055
0
}
2056
2057
static const OSSL_DISPATCH *
2058
core_provider_get0_dispatch(const OSSL_CORE_HANDLE *prov)
2059
0
{
2060
0
    return OSSL_PROVIDER_get0_dispatch((const OSSL_PROVIDER *)prov);
2061
0
}
2062
2063
static int core_provider_up_ref_intern(const OSSL_CORE_HANDLE *prov,
2064
    int activate)
2065
0
{
2066
0
    return provider_up_ref_intern((OSSL_PROVIDER *)prov, activate);
2067
0
}
2068
2069
static int core_provider_free_intern(const OSSL_CORE_HANDLE *prov,
2070
    int deactivate)
2071
0
{
2072
0
    return provider_free_intern((OSSL_PROVIDER *)prov, deactivate);
2073
0
}
2074
2075
static int core_obj_add_sigid(const OSSL_CORE_HANDLE *prov,
2076
    const char *sign_name, const char *digest_name,
2077
    const char *pkey_name)
2078
0
{
2079
0
    int sign_nid = OBJ_txt2nid(sign_name);
2080
0
    int digest_nid = NID_undef;
2081
0
    int pkey_nid = OBJ_txt2nid(pkey_name);
2082
2083
0
    if (digest_name != NULL && digest_name[0] != '\0'
2084
0
        && (digest_nid = OBJ_txt2nid(digest_name)) == NID_undef)
2085
0
        return 0;
2086
2087
0
    if (sign_nid == NID_undef)
2088
0
        return 0;
2089
2090
    /*
2091
     * Check if it already exists. This is a success if so (even if we don't
2092
     * have nids for the digest/pkey)
2093
     */
2094
0
    if (OBJ_find_sigid_algs(sign_nid, NULL, NULL))
2095
0
        return 1;
2096
2097
0
    if (pkey_nid == NID_undef)
2098
0
        return 0;
2099
2100
0
    return OBJ_add_sigid(sign_nid, digest_nid, pkey_nid);
2101
0
}
2102
2103
static int core_obj_create(const OSSL_CORE_HANDLE *prov, const char *oid,
2104
    const char *sn, const char *ln)
2105
0
{
2106
    /* Check if it already exists and create it if not */
2107
0
    return OBJ_txt2nid(oid) != NID_undef
2108
0
        || OBJ_create(oid, sn, ln) != NID_undef;
2109
0
}
2110
#endif /* FIPS_MODULE */
2111
2112
/*
2113
 * Functions provided by the core.
2114
 */
2115
static const OSSL_DISPATCH core_dispatch_[] = {
2116
    { OSSL_FUNC_CORE_GETTABLE_PARAMS, (void (*)(void))core_gettable_params },
2117
    { OSSL_FUNC_CORE_GET_PARAMS, (void (*)(void))core_get_params },
2118
    { OSSL_FUNC_CORE_GET_LIBCTX, (void (*)(void))core_get_libctx },
2119
    { OSSL_FUNC_CORE_THREAD_START, (void (*)(void))core_thread_start },
2120
#ifndef FIPS_MODULE
2121
    { OSSL_FUNC_CORE_NEW_ERROR, (void (*)(void))core_new_error },
2122
    { OSSL_FUNC_CORE_SET_ERROR_DEBUG, (void (*)(void))core_set_error_debug },
2123
    { OSSL_FUNC_CORE_VSET_ERROR, (void (*)(void))core_vset_error },
2124
    { OSSL_FUNC_CORE_SET_ERROR_MARK, (void (*)(void))core_set_error_mark },
2125
    { OSSL_FUNC_CORE_CLEAR_LAST_ERROR_MARK,
2126
        (void (*)(void))core_clear_last_error_mark },
2127
    { OSSL_FUNC_CORE_POP_ERROR_TO_MARK, (void (*)(void))core_pop_error_to_mark },
2128
    { OSSL_FUNC_BIO_NEW_FILE, (void (*)(void))ossl_core_bio_new_file },
2129
    { OSSL_FUNC_BIO_NEW_MEMBUF, (void (*)(void))ossl_core_bio_new_mem_buf },
2130
    { OSSL_FUNC_BIO_READ_EX, (void (*)(void))ossl_core_bio_read_ex },
2131
    { OSSL_FUNC_BIO_WRITE_EX, (void (*)(void))ossl_core_bio_write_ex },
2132
    { OSSL_FUNC_BIO_GETS, (void (*)(void))ossl_core_bio_gets },
2133
    { OSSL_FUNC_BIO_PUTS, (void (*)(void))ossl_core_bio_puts },
2134
    { OSSL_FUNC_BIO_CTRL, (void (*)(void))ossl_core_bio_ctrl },
2135
    { OSSL_FUNC_BIO_UP_REF, (void (*)(void))ossl_core_bio_up_ref },
2136
    { OSSL_FUNC_BIO_FREE, (void (*)(void))ossl_core_bio_free },
2137
    { OSSL_FUNC_BIO_VPRINTF, (void (*)(void))ossl_core_bio_vprintf },
2138
    { OSSL_FUNC_BIO_VSNPRINTF, (void (*)(void))BIO_vsnprintf },
2139
    { OSSL_FUNC_SELF_TEST_CB, (void (*)(void))core_self_test_get_callback },
2140
    { OSSL_FUNC_GET_ENTROPY, (void (*)(void))ossl_rand_get_entropy },
2141
    { OSSL_FUNC_CLEANUP_ENTROPY, (void (*)(void))ossl_rand_cleanup_entropy },
2142
    { OSSL_FUNC_GET_NONCE, (void (*)(void))ossl_rand_get_nonce },
2143
    { OSSL_FUNC_CLEANUP_NONCE, (void (*)(void))ossl_rand_cleanup_nonce },
2144
#endif
2145
    { OSSL_FUNC_CRYPTO_MALLOC, (void (*)(void))CRYPTO_malloc },
2146
    { OSSL_FUNC_CRYPTO_ZALLOC, (void (*)(void))CRYPTO_zalloc },
2147
    { OSSL_FUNC_CRYPTO_FREE, (void (*)(void))CRYPTO_free },
2148
    { OSSL_FUNC_CRYPTO_CLEAR_FREE, (void (*)(void))CRYPTO_clear_free },
2149
    { OSSL_FUNC_CRYPTO_REALLOC, (void (*)(void))CRYPTO_realloc },
2150
    { OSSL_FUNC_CRYPTO_CLEAR_REALLOC, (void (*)(void))CRYPTO_clear_realloc },
2151
    { OSSL_FUNC_CRYPTO_SECURE_MALLOC, (void (*)(void))CRYPTO_secure_malloc },
2152
    { OSSL_FUNC_CRYPTO_SECURE_ZALLOC, (void (*)(void))CRYPTO_secure_zalloc },
2153
    { OSSL_FUNC_CRYPTO_SECURE_FREE, (void (*)(void))CRYPTO_secure_free },
2154
    { OSSL_FUNC_CRYPTO_SECURE_CLEAR_FREE,
2155
        (void (*)(void))CRYPTO_secure_clear_free },
2156
    { OSSL_FUNC_CRYPTO_SECURE_ALLOCATED,
2157
        (void (*)(void))CRYPTO_secure_allocated },
2158
    { OSSL_FUNC_OPENSSL_CLEANSE, (void (*)(void))OPENSSL_cleanse },
2159
#ifndef FIPS_MODULE
2160
    { OSSL_FUNC_PROVIDER_REGISTER_CHILD_CB,
2161
        (void (*)(void))ossl_provider_register_child_cb },
2162
    { OSSL_FUNC_PROVIDER_DEREGISTER_CHILD_CB,
2163
        (void (*)(void))ossl_provider_deregister_child_cb },
2164
    { OSSL_FUNC_PROVIDER_NAME,
2165
        (void (*)(void))core_provider_get0_name },
2166
    { OSSL_FUNC_PROVIDER_GET0_PROVIDER_CTX,
2167
        (void (*)(void))core_provider_get0_provider_ctx },
2168
    { OSSL_FUNC_PROVIDER_GET0_DISPATCH,
2169
        (void (*)(void))core_provider_get0_dispatch },
2170
    { OSSL_FUNC_PROVIDER_UP_REF,
2171
        (void (*)(void))core_provider_up_ref_intern },
2172
    { OSSL_FUNC_PROVIDER_FREE,
2173
        (void (*)(void))core_provider_free_intern },
2174
    { OSSL_FUNC_CORE_OBJ_ADD_SIGID, (void (*)(void))core_obj_add_sigid },
2175
    { OSSL_FUNC_CORE_OBJ_CREATE, (void (*)(void))core_obj_create },
2176
#endif
2177
    { 0, NULL }
2178
};
2179
static const OSSL_DISPATCH *core_dispatch = core_dispatch_;