Coverage Report

Created: 2025-12-31 06:58

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