Coverage Report

Created: 2025-12-31 06:58

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