Coverage Report

Created: 2025-06-13 06:58

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