Coverage Report

Created: 2026-07-16 06:59

next uncovered line (L), next uncovered region (R), next uncovered branch (B)
/src/openssl/crypto/store/store_meth.c
Line
Count
Source
1
/*
2
 * Copyright 2020-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 <openssl/crypto.h>
11
#include "crypto/store.h"
12
#include "internal/core.h"
13
#include "internal/namemap.h"
14
#include "internal/property.h"
15
#include "internal/provider.h"
16
#include "store_local.h"
17
#include "crypto/context.h"
18
19
static int up_ref_loader(void *method)
20
0
{
21
0
    OSSL_STORE_LOADER *loader = (OSSL_STORE_LOADER *)method;
22
0
    int ref = 0;
23
24
0
    if (loader->prov != NULL)
25
0
        CRYPTO_UP_REF(&loader->refcnt, &ref);
26
0
    return 1;
27
0
}
28
29
static void free_loader(void *method)
30
0
{
31
0
    OSSL_STORE_LOADER *loader = (OSSL_STORE_LOADER *)method;
32
33
0
    if (loader != NULL && loader->prov != NULL) {
34
0
        int i;
35
36
0
        CRYPTO_DOWN_REF(&loader->refcnt, &i);
37
0
        if (i > 0)
38
0
            return;
39
0
        ossl_provider_free(loader->prov);
40
0
        CRYPTO_FREE_REF(&loader->refcnt);
41
0
    }
42
0
    OPENSSL_free(loader);
43
0
}
44
45
int OSSL_STORE_LOADER_up_ref(OSSL_STORE_LOADER *loader)
46
0
{
47
#ifdef OPENSSL_NO_CACHED_FETCH
48
    return up_ref_loader(loader);
49
#else
50
0
    if (loader->no_store != 0)
51
0
        return up_ref_loader(loader);
52
0
    return 1;
53
0
#endif
54
0
}
55
56
void OSSL_STORE_LOADER_free(OSSL_STORE_LOADER *loader)
57
0
{
58
#ifdef OPENSSL_NO_CACHED_FETCH
59
    free_loader(loader);
60
#else
61
0
    if (loader != NULL && (loader->no_store != 0))
62
0
        free_loader(loader);
63
0
#endif
64
0
}
65
66
/*
67
 * OSSL_STORE_LOADER_new() expects the scheme as a constant string,
68
 * which we currently don't have, so we need an alternative allocator.
69
 */
70
static OSSL_STORE_LOADER *new_loader(OSSL_PROVIDER *prov)
71
0
{
72
0
    OSSL_STORE_LOADER *loader;
73
74
0
    if ((loader = OPENSSL_zalloc(sizeof(*loader))) == NULL
75
0
        || !CRYPTO_NEW_REF(&loader->refcnt, 1)
76
0
        || !ossl_provider_up_ref(prov)) {
77
0
        if (loader != NULL)
78
0
            CRYPTO_FREE_REF(&loader->refcnt);
79
0
        OPENSSL_free(loader);
80
0
        return NULL;
81
0
    }
82
83
0
    loader->prov = prov;
84
85
0
    return loader;
86
0
}
87
88
/* Data to be passed through ossl_method_construct() */
89
struct loader_data_st {
90
    OSSL_LIB_CTX *libctx;
91
    int scheme_id; /* For get_loader_from_store() */
92
    const char *scheme; /* For get_loader_from_store() */
93
    const char *propquery; /* For get_loader_from_store() */
94
95
    OSSL_METHOD_STORE *tmp_store; /* For get_tmp_loader_store() */
96
97
    unsigned int flag_construct_error_occurred : 1;
98
};
99
100
/*
101
 * Generic routines to fetch / create OSSL_STORE methods with
102
 * ossl_method_construct()
103
 */
104
105
/* Temporary loader method store, constructor and destructor */
106
static void *get_tmp_loader_store(void *data)
107
0
{
108
0
    struct loader_data_st *methdata = data;
109
110
0
    if (methdata->tmp_store == NULL)
111
0
        methdata->tmp_store = ossl_method_store_new(methdata->libctx);
112
0
    return methdata->tmp_store;
113
0
}
114
115
static void dealloc_tmp_loader_store(void *store)
116
0
{
117
0
    if (store != NULL)
118
0
        ossl_method_store_free(store);
119
0
}
120
121
/* Get the permanent loader store */
122
static OSSL_METHOD_STORE *get_loader_store(OSSL_LIB_CTX *libctx)
123
12
{
124
12
    return ossl_lib_ctx_get_data(libctx, OSSL_LIB_CTX_STORE_LOADER_STORE_INDEX);
125
12
}
126
127
static int reserve_loader_store(void *store, void *data)
128
0
{
129
0
    struct loader_data_st *methdata = data;
130
131
0
    if (store == NULL
132
0
        && (store = get_loader_store(methdata->libctx)) == NULL)
133
0
        return 0;
134
135
0
    return ossl_method_lock_store(store);
136
0
}
137
138
static int unreserve_loader_store(void *store, void *data)
139
0
{
140
0
    struct loader_data_st *methdata = data;
141
142
0
    if (store == NULL
143
0
        && (store = get_loader_store(methdata->libctx)) == NULL)
144
0
        return 0;
145
146
0
    return ossl_method_unlock_store(store);
147
0
}
148
149
/* Get loader methods from a store, or put one in */
150
static void *get_loader_from_store(void *store, const OSSL_PROVIDER **prov,
151
    void *data)
152
0
{
153
0
    struct loader_data_st *methdata = data;
154
0
    void *method = NULL;
155
0
    int id;
156
157
0
    if ((id = methdata->scheme_id) == 0) {
158
0
        OSSL_NAMEMAP *namemap = ossl_namemap_stored(methdata->libctx);
159
160
0
        id = ossl_namemap_name2num(namemap, methdata->scheme);
161
0
    }
162
163
0
    if (store == NULL
164
0
        && (store = get_loader_store(methdata->libctx)) == NULL)
165
0
        return NULL;
166
167
0
    if (!ossl_method_store_fetch(store, id, methdata->propquery, prov, &method))
168
0
        return NULL;
169
0
    return method;
170
0
}
171
172
static int put_loader_in_store(void *store, void *method,
173
    const OSSL_PROVIDER *prov,
174
    const char *scheme, const char *propdef,
175
    void *data)
176
0
{
177
0
    struct loader_data_st *methdata = data;
178
0
    OSSL_NAMEMAP *namemap;
179
0
    int id;
180
181
0
    if ((namemap = ossl_namemap_stored(methdata->libctx)) == NULL
182
0
        || (id = ossl_namemap_name2num(namemap, scheme)) == 0)
183
0
        return 0;
184
185
0
    if (store == NULL && (store = get_loader_store(methdata->libctx)) == NULL)
186
0
        return 0;
187
188
0
    return ossl_method_store_add(store, prov, id, propdef, method,
189
0
        up_ref_loader, free_loader);
190
0
}
191
192
static void *loader_from_algorithm(int scheme_id, const OSSL_ALGORITHM *algodef,
193
    OSSL_PROVIDER *prov, int no_store)
194
0
{
195
0
    OSSL_STORE_LOADER *loader = NULL;
196
0
    const OSSL_DISPATCH *fns = algodef->implementation;
197
198
0
    if ((loader = new_loader(prov)) == NULL)
199
0
        return NULL;
200
0
    loader->scheme_id = scheme_id;
201
0
    loader->propdef = algodef->property_definition;
202
0
    loader->description = algodef->algorithm_description;
203
0
    loader->no_store = no_store;
204
205
0
    for (; fns->function_id != 0; fns++) {
206
0
        switch (fns->function_id) {
207
0
        case OSSL_FUNC_STORE_OPEN:
208
0
            if (loader->p_open == NULL)
209
0
                loader->p_open = OSSL_FUNC_store_open(fns);
210
0
            break;
211
0
        case OSSL_FUNC_STORE_ATTACH:
212
0
            if (loader->p_attach == NULL)
213
0
                loader->p_attach = OSSL_FUNC_store_attach(fns);
214
0
            break;
215
0
        case OSSL_FUNC_STORE_SETTABLE_CTX_PARAMS:
216
0
            if (loader->p_settable_ctx_params == NULL)
217
0
                loader->p_settable_ctx_params = OSSL_FUNC_store_settable_ctx_params(fns);
218
0
            break;
219
0
        case OSSL_FUNC_STORE_SET_CTX_PARAMS:
220
0
            if (loader->p_set_ctx_params == NULL)
221
0
                loader->p_set_ctx_params = OSSL_FUNC_store_set_ctx_params(fns);
222
0
            break;
223
0
        case OSSL_FUNC_STORE_LOAD:
224
0
            if (loader->p_load == NULL)
225
0
                loader->p_load = OSSL_FUNC_store_load(fns);
226
0
            break;
227
0
        case OSSL_FUNC_STORE_EOF:
228
0
            if (loader->p_eof == NULL)
229
0
                loader->p_eof = OSSL_FUNC_store_eof(fns);
230
0
            break;
231
0
        case OSSL_FUNC_STORE_CLOSE:
232
0
            if (loader->p_close == NULL)
233
0
                loader->p_close = OSSL_FUNC_store_close(fns);
234
0
            break;
235
0
        case OSSL_FUNC_STORE_EXPORT_OBJECT:
236
0
            if (loader->p_export_object == NULL)
237
0
                loader->p_export_object = OSSL_FUNC_store_export_object(fns);
238
0
            break;
239
0
        case OSSL_FUNC_STORE_DELETE:
240
0
            if (loader->p_delete == NULL)
241
0
                loader->p_delete = OSSL_FUNC_store_delete(fns);
242
0
            break;
243
0
        case OSSL_FUNC_STORE_OPEN_EX:
244
0
            if (loader->p_open_ex == NULL)
245
0
                loader->p_open_ex = OSSL_FUNC_store_open_ex(fns);
246
0
            break;
247
0
        }
248
0
    }
249
250
0
    if ((loader->p_open == NULL && loader->p_attach == NULL)
251
0
        || loader->p_load == NULL
252
0
        || loader->p_eof == NULL
253
0
        || loader->p_close == NULL) {
254
        /* Only set_ctx_params is optional */
255
0
        free_loader(loader);
256
0
        ERR_raise(ERR_LIB_OSSL_STORE, OSSL_STORE_R_LOADER_INCOMPLETE);
257
0
        return NULL;
258
0
    }
259
0
    return loader;
260
0
}
261
262
/*
263
 * The core fetching functionality passes the scheme of the implementation.
264
 * This function is responsible to getting an identity number for them,
265
 * then call loader_from_algorithm() with that identity number.
266
 */
267
static void *construct_loader(const OSSL_ALGORITHM *algodef,
268
    OSSL_PROVIDER *prov, void *data, int no_store)
269
0
{
270
    /*
271
     * This function is only called if get_loader_from_store() returned
272
     * NULL, so it's safe to say that of all the spots to create a new
273
     * namemap entry, this is it.  Should the scheme already exist there, we
274
     * know that ossl_namemap_add() will return its corresponding number.
275
     */
276
0
    struct loader_data_st *methdata = data;
277
0
    OSSL_LIB_CTX *libctx = ossl_provider_libctx(prov);
278
0
    OSSL_NAMEMAP *namemap = ossl_namemap_stored(libctx);
279
0
    const char *scheme = algodef->algorithm_names;
280
0
    int id = ossl_namemap_add_name(namemap, 0, scheme);
281
0
    void *method = NULL;
282
283
0
    if (id != 0)
284
0
        method = loader_from_algorithm(id, algodef, prov, no_store);
285
286
    /*
287
     * Flag to indicate that there was actual construction errors.  This
288
     * helps inner_loader_fetch() determine what error it should
289
     * record on inaccessible algorithms.
290
     */
291
0
    if (method == NULL)
292
0
        methdata->flag_construct_error_occurred = 1;
293
294
0
    return method;
295
0
}
296
297
/* Intermediary function to avoid ugly casts, used below */
298
static void destruct_loader(void *method, void *data)
299
0
{
300
0
    free_loader(method);
301
0
}
302
303
/* Fetching support.  Can fetch by numeric identity or by scheme */
304
static OSSL_STORE_LOADER *
305
inner_loader_fetch(struct loader_data_st *methdata,
306
    const char *scheme, const char *properties)
307
0
{
308
0
    OSSL_METHOD_STORE *store = get_loader_store(methdata->libctx);
309
0
    OSSL_NAMEMAP *namemap = ossl_namemap_stored(methdata->libctx);
310
0
    const char *const propq = properties != NULL ? properties : "";
311
0
    void *method = NULL;
312
0
    int unsupported, id;
313
314
0
    if (store == NULL || namemap == NULL) {
315
0
        ERR_raise(ERR_LIB_OSSL_STORE, ERR_R_PASSED_INVALID_ARGUMENT);
316
0
        return NULL;
317
0
    }
318
319
    /* If we haven't received a name id yet, try to get one for the name */
320
0
    id = ossl_namemap_name2num(namemap, scheme);
321
322
    /*
323
     * If we haven't found the name yet, chances are that the algorithm to
324
     * be fetched is unsupported.
325
     */
326
0
    unsupported = id == 0;
327
328
0
    if (id == 0
329
0
        || !ossl_method_store_cache_get(store, NULL, id, propq, &method)) {
330
0
        OSSL_METHOD_CONSTRUCT_METHOD mcm = {
331
0
            get_tmp_loader_store,
332
0
            reserve_loader_store,
333
0
            unreserve_loader_store,
334
0
            get_loader_from_store,
335
0
            put_loader_in_store,
336
0
            construct_loader,
337
0
            destruct_loader
338
0
        };
339
0
        OSSL_PROVIDER *prov = NULL;
340
341
0
        methdata->scheme_id = id;
342
0
        methdata->scheme = scheme;
343
0
        methdata->propquery = propq;
344
0
        methdata->flag_construct_error_occurred = 0;
345
0
        if ((method = ossl_method_construct(methdata->libctx, OSSL_OP_STORE,
346
0
                 &prov, 0 /* !force_cache */,
347
0
                 &mcm, methdata))
348
0
            != NULL) {
349
            /*
350
             * If construction did create a method for us, we know that there
351
             * is a correct scheme_id, since those have already been calculated
352
             * in get_loader_from_store() and put_loader_in_store() above.
353
             */
354
0
            if (id == 0)
355
0
                id = ossl_namemap_name2num(namemap, scheme);
356
0
            if (id != 0 && methdata->tmp_store == NULL) {
357
0
                ossl_method_store_cache_set(store, prov, id, propq, method,
358
0
                    up_ref_loader, free_loader);
359
0
            } else {
360
                /*
361
                 * Like with EVP methods, if the provider requests no caching we need
362
                 * to take an extra refcount here so that the tmp_stored loader
363
                 * lives beyond the freeing of that tmp_store
364
                 */
365
0
#ifndef OPENSSL_NO_CACHED_FETCH
366
0
                OSSL_STORE_LOADER_up_ref((OSSL_STORE_LOADER *)method);
367
0
#endif
368
0
            }
369
0
        }
370
371
        /*
372
         * If we never were in the constructor, the algorithm to be fetched
373
         * is unsupported.
374
         */
375
0
        unsupported = !methdata->flag_construct_error_occurred;
376
0
    }
377
378
0
    if ((id != 0 || scheme != NULL) && method == NULL) {
379
0
        int code = unsupported ? ERR_R_UNSUPPORTED : ERR_R_FETCH_FAILED;
380
0
        const char *helpful_msg = unsupported
381
0
            ? ("No store loader found. For standard store loaders you need "
382
0
               "at least one of the default or base providers available. "
383
0
               "Did you forget to load them? Info: ")
384
0
            : "";
385
386
0
        if (scheme == NULL)
387
0
            scheme = ossl_namemap_num2name(namemap, id, 0);
388
0
        ERR_raise_data(ERR_LIB_OSSL_STORE, code,
389
0
            "%s%s, Scheme (%s : %d), Properties (%s)",
390
0
            helpful_msg,
391
0
            ossl_lib_ctx_get_descriptor(methdata->libctx),
392
0
            scheme == NULL ? "<null>" : scheme, id,
393
0
            properties == NULL ? "<null>" : properties);
394
0
    }
395
396
0
    return method;
397
0
}
398
399
OSSL_STORE_LOADER *OSSL_STORE_LOADER_fetch(OSSL_LIB_CTX *libctx,
400
    const char *scheme,
401
    const char *properties)
402
0
{
403
0
    struct loader_data_st methdata;
404
0
    void *method;
405
406
0
    methdata.libctx = libctx;
407
0
    methdata.tmp_store = NULL;
408
0
    method = inner_loader_fetch(&methdata, scheme, properties);
409
0
    dealloc_tmp_loader_store(methdata.tmp_store);
410
0
    return method;
411
0
}
412
413
int ossl_store_loader_store_cache_flush(OSSL_LIB_CTX *libctx)
414
12
{
415
12
    OSSL_METHOD_STORE *store = get_loader_store(libctx);
416
417
12
    if (store != NULL)
418
12
        return ossl_method_store_cache_flush_all(store);
419
0
    return 1;
420
12
}
421
422
int ossl_store_loader_store_remove_all_provided(const OSSL_PROVIDER *prov)
423
0
{
424
0
    OSSL_LIB_CTX *libctx = ossl_provider_libctx(prov);
425
0
    OSSL_METHOD_STORE *store = get_loader_store(libctx);
426
427
0
    if (store != NULL)
428
0
        return ossl_method_store_remove_all_provided(store, prov);
429
0
    return 1;
430
0
}
431
432
/*
433
 * Library of basic method functions
434
 */
435
436
const OSSL_PROVIDER *OSSL_STORE_LOADER_get0_provider(const OSSL_STORE_LOADER *loader)
437
0
{
438
0
    if (!ossl_assert(loader != NULL)) {
439
0
        ERR_raise(ERR_LIB_OSSL_STORE, ERR_R_PASSED_NULL_PARAMETER);
440
0
        return 0;
441
0
    }
442
443
0
    return loader->prov;
444
0
}
445
446
const char *OSSL_STORE_LOADER_get0_properties(const OSSL_STORE_LOADER *loader)
447
0
{
448
0
    if (!ossl_assert(loader != NULL)) {
449
0
        ERR_raise(ERR_LIB_OSSL_STORE, ERR_R_PASSED_NULL_PARAMETER);
450
0
        return 0;
451
0
    }
452
453
0
    return loader->propdef;
454
0
}
455
456
int ossl_store_loader_get_number(const OSSL_STORE_LOADER *loader)
457
0
{
458
0
    if (!ossl_assert(loader != NULL)) {
459
0
        ERR_raise(ERR_LIB_OSSL_STORE, ERR_R_PASSED_NULL_PARAMETER);
460
0
        return 0;
461
0
    }
462
463
0
    return loader->scheme_id;
464
0
}
465
466
const char *OSSL_STORE_LOADER_get0_description(const OSSL_STORE_LOADER *loader)
467
0
{
468
0
    return loader->description;
469
0
}
470
471
int OSSL_STORE_LOADER_is_a(const OSSL_STORE_LOADER *loader, const char *name)
472
0
{
473
0
    if (loader->prov != NULL) {
474
0
        OSSL_LIB_CTX *libctx = ossl_provider_libctx(loader->prov);
475
0
        OSSL_NAMEMAP *namemap = ossl_namemap_stored(libctx);
476
477
0
        return ossl_namemap_name2num(namemap, name) == loader->scheme_id;
478
0
    }
479
0
    return 0;
480
0
}
481
482
struct do_one_data_st {
483
    void (*user_fn)(OSSL_STORE_LOADER *loader, void *arg);
484
    void *user_arg;
485
};
486
487
static void do_one(ossl_unused int id, void *method, void *arg)
488
0
{
489
0
    struct do_one_data_st *data = arg;
490
491
0
    data->user_fn(method, data->user_arg);
492
0
}
493
494
void OSSL_STORE_LOADER_do_all_provided(OSSL_LIB_CTX *libctx,
495
    void (*user_fn)(OSSL_STORE_LOADER *loader,
496
        void *arg),
497
    void *user_arg)
498
0
{
499
0
    struct loader_data_st methdata;
500
0
    struct do_one_data_st data;
501
502
0
    methdata.libctx = libctx;
503
0
    methdata.tmp_store = NULL;
504
0
    (void)inner_loader_fetch(&methdata, NULL, NULL /* properties */);
505
506
0
    data.user_fn = user_fn;
507
0
    data.user_arg = user_arg;
508
0
    if (methdata.tmp_store != NULL)
509
0
        ossl_method_store_do_all(methdata.tmp_store, &do_one, &data);
510
0
    ossl_method_store_do_all(get_loader_store(libctx), &do_one, &data);
511
0
    dealloc_tmp_loader_store(methdata.tmp_store);
512
0
}
513
514
int OSSL_STORE_LOADER_names_do_all(const OSSL_STORE_LOADER *loader,
515
    void (*fn)(const char *name, void *data),
516
    void *data)
517
0
{
518
0
    if (loader == NULL)
519
0
        return 0;
520
521
0
    if (loader->prov != NULL) {
522
0
        OSSL_LIB_CTX *libctx = ossl_provider_libctx(loader->prov);
523
0
        OSSL_NAMEMAP *namemap = ossl_namemap_stored(libctx);
524
525
0
        return ossl_namemap_doall_names(namemap, loader->scheme_id, fn, data);
526
0
    }
527
528
0
    return 1;
529
0
}
530
531
const OSSL_PARAM *
532
OSSL_STORE_LOADER_settable_ctx_params(const OSSL_STORE_LOADER *loader)
533
0
{
534
0
    if (loader != NULL && loader->p_settable_ctx_params != NULL)
535
0
        return loader->p_settable_ctx_params(NULL);
536
0
    return NULL;
537
0
}