Coverage Report

Created: 2025-12-10 06:24

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