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_lib.c
Line
Count
Source
1
/*
2
 * Copyright 2016-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 <stdlib.h>
11
#include <string.h>
12
#include <assert.h>
13
14
/* We need to use some STORE deprecated APIs */
15
#define OPENSSL_SUPPRESS_DEPRECATED
16
17
#include "internal/e_os.h"
18
19
#include <openssl/crypto.h>
20
#include <openssl/err.h>
21
#include <openssl/trace.h>
22
#include <openssl/core_names.h>
23
#include <openssl/provider.h>
24
#include <openssl/param_build.h>
25
#include <openssl/store.h>
26
#include "internal/thread_once.h"
27
#include "internal/cryptlib.h"
28
#include "internal/provider.h"
29
#include "internal/bio.h"
30
#include "crypto/store.h"
31
#include "store_local.h"
32
33
static int ossl_store_close_it(OSSL_STORE_CTX *ctx);
34
35
static int loader_set_params(OSSL_STORE_LOADER *loader,
36
    OSSL_STORE_LOADER_CTX *loader_ctx,
37
    const OSSL_PARAM params[], const char *propq)
38
0
{
39
0
    if (params != NULL) {
40
0
        if (!loader->p_set_ctx_params(loader_ctx, params))
41
0
            return 0;
42
0
    }
43
44
0
    if (propq != NULL) {
45
0
        OSSL_PARAM propp[2];
46
47
0
        if (OSSL_PARAM_locate_const(params,
48
0
                OSSL_STORE_PARAM_PROPERTIES)
49
0
            != NULL)
50
            /* use the propq from params */
51
0
            return 1;
52
53
0
        propp[0] = OSSL_PARAM_construct_utf8_string(OSSL_STORE_PARAM_PROPERTIES,
54
0
            (char *)propq, 0);
55
0
        propp[1] = OSSL_PARAM_construct_end();
56
57
0
        if (!loader->p_set_ctx_params(loader_ctx, propp))
58
0
            return 0;
59
0
    }
60
0
    return 1;
61
0
}
62
63
OSSL_STORE_CTX *
64
OSSL_STORE_open_ex(const char *uri, OSSL_LIB_CTX *libctx, const char *propq,
65
    const UI_METHOD *ui_method, void *ui_data,
66
    const OSSL_PARAM params[],
67
    OSSL_STORE_post_process_info_fn post_process,
68
    void *post_process_data)
69
0
{
70
0
    struct ossl_passphrase_data_st pwdata = { 0 };
71
0
    const OSSL_STORE_LOADER *loader = NULL;
72
0
    OSSL_STORE_LOADER *fetched_loader = NULL;
73
0
    OSSL_STORE_LOADER_CTX *loader_ctx = NULL;
74
0
    OSSL_STORE_CTX *ctx = NULL;
75
0
    char *propq_copy = NULL;
76
0
    int no_loader_found = 1;
77
0
    char scheme_copy[256], *p, *schemes[2], *scheme = NULL;
78
0
    size_t schemes_n = 0;
79
0
    size_t i;
80
81
0
    if (uri == NULL) {
82
0
        ERR_raise(ERR_LIB_OSSL_STORE, ERR_R_PASSED_NULL_PARAMETER);
83
0
        return 0;
84
0
    }
85
86
    /*
87
     * Put the file scheme first.  If the uri does represent an existing file,
88
     * possible device name and all, then it should be loaded.  Only a failed
89
     * attempt at loading a local file should have us try something else.
90
     */
91
0
    schemes[schemes_n++] = "file";
92
93
    /*
94
     * Now, check if we have something that looks like a scheme, and add it
95
     * as a second scheme.  However, also check if there's an authority start
96
     * (://), because that will invalidate the previous file scheme.  Also,
97
     * check that this isn't actually the file scheme, as there's no point
98
     * going through that one twice!
99
     */
100
0
    OPENSSL_strlcpy(scheme_copy, uri, sizeof(scheme_copy));
101
0
    if ((p = strchr(scheme_copy, ':')) != NULL) {
102
0
        *p++ = '\0';
103
0
        if (OPENSSL_strcasecmp(scheme_copy, "file") != 0) {
104
0
            if (HAS_PREFIX(p, "//"))
105
0
                schemes_n--; /* Invalidate the file scheme */
106
0
            schemes[schemes_n++] = scheme_copy;
107
0
        }
108
0
    }
109
110
0
    ERR_set_mark();
111
112
0
    if (ui_method != NULL
113
0
        && (!ossl_pw_set_ui_method(&pwdata, ui_method, ui_data)
114
0
            || !ossl_pw_enable_passphrase_caching(&pwdata))) {
115
0
        ERR_raise(ERR_LIB_OSSL_STORE, ERR_R_CRYPTO_LIB);
116
0
        goto err;
117
0
    }
118
119
    /*
120
     * Try each scheme until we find one that could open the URI.
121
     *
122
     * For each scheme, we look for the engine implementation first, and
123
     * failing that, we then try to fetch a provided implementation.
124
     * This is consistent with how we handle legacy / engine implementations
125
     * elsewhere.
126
     */
127
0
    for (i = 0; loader_ctx == NULL && i < schemes_n; i++) {
128
0
        scheme = schemes[i];
129
0
        OSSL_TRACE1(STORE, "Looking up scheme %s\n", scheme);
130
0
#ifndef OPENSSL_NO_DEPRECATED_3_0
131
0
        ERR_set_mark();
132
0
        if ((loader = ossl_store_get0_loader_int(scheme)) != NULL) {
133
0
            ERR_clear_last_mark();
134
0
            no_loader_found = 0;
135
0
            if (loader->open_ex != NULL)
136
0
                loader_ctx = loader->open_ex(loader, uri, libctx, propq,
137
0
                    ui_method, ui_data);
138
0
            else
139
0
                loader_ctx = loader->open(loader, uri, ui_method, ui_data);
140
0
        } else {
141
0
            ERR_pop_to_mark();
142
0
        }
143
0
#endif
144
0
        if (loader == NULL
145
0
            && (fetched_loader = OSSL_STORE_LOADER_fetch(libctx, scheme, propq)) != NULL) {
146
0
            const OSSL_PROVIDER *provider = OSSL_STORE_LOADER_get0_provider(fetched_loader);
147
0
            void *provctx = OSSL_PROVIDER_get0_provider_ctx(provider);
148
149
0
            no_loader_found = 0;
150
0
            if (fetched_loader->p_open_ex != NULL) {
151
0
                loader_ctx = fetched_loader->p_open_ex(provctx, uri, params,
152
0
                    ossl_pw_passphrase_callback_dec,
153
0
                    &pwdata);
154
0
            } else {
155
0
                if (fetched_loader->p_open != NULL && (loader_ctx = fetched_loader->p_open(provctx, uri)) != NULL && !loader_set_params(fetched_loader, loader_ctx, params, propq)) {
156
0
                    (void)fetched_loader->p_close(loader_ctx);
157
0
                    loader_ctx = NULL;
158
0
                }
159
0
            }
160
0
            if (loader_ctx == NULL) {
161
0
                OSSL_STORE_LOADER_free(fetched_loader);
162
0
                fetched_loader = NULL;
163
0
            }
164
0
            loader = fetched_loader;
165
166
            /* Clear any internally cached passphrase */
167
0
            (void)ossl_pw_clear_passphrase_cache(&pwdata);
168
0
        }
169
0
    }
170
171
0
    if (no_loader_found)
172
        /*
173
         * It's assumed that ossl_store_get0_loader_int() and
174
         * OSSL_STORE_LOADER_fetch() report their own errors
175
         */
176
0
        goto err;
177
178
0
    OSSL_TRACE1(STORE, "Found loader for scheme %s\n", scheme);
179
180
0
    if (loader_ctx == NULL)
181
        /*
182
         * It's assumed that the loader's open() method reports its own
183
         * errors
184
         */
185
0
        goto err;
186
187
0
    OSSL_TRACE2(STORE, "Opened %s => %p\n", uri, (void *)loader_ctx);
188
189
0
    if ((propq != NULL && (propq_copy = OPENSSL_strdup(propq)) == NULL)
190
0
        || (ctx = OPENSSL_zalloc(sizeof(*ctx))) == NULL)
191
0
        goto err;
192
193
0
    ctx->properties = propq_copy;
194
0
    ctx->fetched_loader = fetched_loader;
195
0
    ctx->loader = loader;
196
0
    ctx->loader_ctx = loader_ctx;
197
0
    ctx->post_process = post_process;
198
0
    ctx->post_process_data = post_process_data;
199
0
    ctx->pwdata = pwdata;
200
201
    /*
202
     * If the attempt to open with the 'file' scheme loader failed and the
203
     * other scheme loader succeeded, the failure to open with the 'file'
204
     * scheme loader leaves an error on the error stack.  Let's remove it.
205
     */
206
0
    ERR_pop_to_mark();
207
208
0
    return ctx;
209
210
0
err:
211
0
    ERR_clear_last_mark();
212
0
    if (loader_ctx != NULL) {
213
        /*
214
         * Temporary structure so OSSL_STORE_close() can work even when
215
         * |ctx| couldn't be allocated properly
216
         */
217
0
        OSSL_STORE_CTX tmpctx = {
218
0
            NULL,
219
0
        };
220
221
0
        tmpctx.fetched_loader = fetched_loader;
222
0
        tmpctx.loader = loader;
223
0
        tmpctx.loader_ctx = loader_ctx;
224
225
        /*
226
         * We ignore a returned error because we will return NULL anyway in
227
         * this case, so if something goes wrong when closing, that'll simply
228
         * just add another entry on the error stack.
229
         */
230
0
        (void)ossl_store_close_it(&tmpctx);
231
0
    }
232
    /* Coverity false positive, the reference counting is confusing it */
233
    /* coverity[pass_freed_arg] */
234
0
    OSSL_STORE_LOADER_free(fetched_loader);
235
0
    OPENSSL_free(propq_copy);
236
0
    OPENSSL_free(ctx);
237
0
    return NULL;
238
0
}
239
240
OSSL_STORE_CTX *OSSL_STORE_open(const char *uri,
241
    const UI_METHOD *ui_method, void *ui_data,
242
    OSSL_STORE_post_process_info_fn post_process,
243
    void *post_process_data)
244
0
{
245
0
    return OSSL_STORE_open_ex(uri, NULL, NULL, ui_method, ui_data, NULL,
246
0
        post_process, post_process_data);
247
0
}
248
249
#ifndef OPENSSL_NO_DEPRECATED_3_0
250
int OSSL_STORE_ctrl(OSSL_STORE_CTX *ctx, int cmd, ...)
251
0
{
252
0
    va_list args;
253
0
    int ret;
254
255
0
    va_start(args, cmd);
256
0
    ret = OSSL_STORE_vctrl(ctx, cmd, args);
257
0
    va_end(args);
258
259
0
    return ret;
260
0
}
261
262
int OSSL_STORE_vctrl(OSSL_STORE_CTX *ctx, int cmd, va_list args)
263
0
{
264
0
    if (ctx->fetched_loader != NULL) {
265
0
        if (ctx->fetched_loader->p_set_ctx_params != NULL) {
266
0
            OSSL_PARAM params[2] = { OSSL_PARAM_END, OSSL_PARAM_END };
267
268
0
            switch (cmd) {
269
0
            case OSSL_STORE_C_USE_SECMEM: {
270
0
                int on = *(va_arg(args, int *));
271
272
0
                params[0] = OSSL_PARAM_construct_int("use_secmem", &on);
273
0
            } break;
274
0
            default:
275
0
                break;
276
0
            }
277
278
0
            return ctx->fetched_loader->p_set_ctx_params(ctx->loader_ctx,
279
0
                params);
280
0
        }
281
0
    } else if (ctx->loader->ctrl != NULL) {
282
0
        return ctx->loader->ctrl(ctx->loader_ctx, cmd, args);
283
0
    }
284
285
    /*
286
     * If the fetched loader doesn't have a set_ctx_params or a ctrl, it's as
287
     * if there was one that ignored our params, which usually returns 1.
288
     */
289
0
    return 1;
290
0
}
291
#endif
292
293
int OSSL_STORE_expect(OSSL_STORE_CTX *ctx, int expected_type)
294
0
{
295
0
    int ret = 1;
296
297
0
    if (ctx == NULL
298
0
        || expected_type < 0 || expected_type > OSSL_STORE_INFO_CRL) {
299
0
        ERR_raise(ERR_LIB_OSSL_STORE, ERR_R_PASSED_INVALID_ARGUMENT);
300
0
        return 0;
301
0
    }
302
0
    if (ctx->loading) {
303
0
        ERR_raise(ERR_LIB_OSSL_STORE, OSSL_STORE_R_LOADING_STARTED);
304
0
        return 0;
305
0
    }
306
307
0
    ctx->expected_type = expected_type;
308
0
    if (ctx->fetched_loader != NULL
309
0
        && ctx->fetched_loader->p_set_ctx_params != NULL) {
310
0
        OSSL_PARAM params[2] = { OSSL_PARAM_END, OSSL_PARAM_END };
311
312
0
        params[0] = OSSL_PARAM_construct_int(OSSL_STORE_PARAM_EXPECT, &expected_type);
313
0
        ret = ctx->fetched_loader->p_set_ctx_params(ctx->loader_ctx, params);
314
0
    }
315
0
#ifndef OPENSSL_NO_DEPRECATED_3_0
316
0
    if (ctx->fetched_loader == NULL
317
0
        && ctx->loader->expect != NULL) {
318
0
        ret = ctx->loader->expect(ctx->loader_ctx, expected_type);
319
0
    }
320
0
#endif
321
0
    return ret;
322
0
}
323
324
int OSSL_STORE_find(OSSL_STORE_CTX *ctx, const OSSL_STORE_SEARCH *search)
325
0
{
326
0
    int ret = 1;
327
328
0
    if (ctx->loading) {
329
0
        ERR_raise(ERR_LIB_OSSL_STORE, OSSL_STORE_R_LOADING_STARTED);
330
0
        return 0;
331
0
    }
332
0
    if (search == NULL) {
333
0
        ERR_raise(ERR_LIB_OSSL_STORE, ERR_R_PASSED_NULL_PARAMETER);
334
0
        return 0;
335
0
    }
336
337
0
    if (ctx->fetched_loader != NULL) {
338
0
        OSSL_PARAM_BLD *bld;
339
0
        OSSL_PARAM *params;
340
        /* OSSL_STORE_SEARCH_BY_NAME, OSSL_STORE_SEARCH_BY_ISSUER_SERIAL*/
341
0
        void *name_der = NULL;
342
0
        int name_der_sz;
343
        /* OSSL_STORE_SEARCH_BY_ISSUER_SERIAL */
344
0
        BIGNUM *number = NULL;
345
346
0
        if (ctx->fetched_loader->p_set_ctx_params == NULL) {
347
0
            ERR_raise(ERR_LIB_OSSL_STORE, OSSL_STORE_R_UNSUPPORTED_OPERATION);
348
0
            return 0;
349
0
        }
350
351
0
        if ((bld = OSSL_PARAM_BLD_new()) == NULL) {
352
0
            ERR_raise(ERR_LIB_OSSL_STORE, ERR_R_CRYPTO_LIB);
353
0
            return 0;
354
0
        }
355
356
0
        ret = 0; /* Assume the worst */
357
358
0
        switch (search->search_type) {
359
0
        case OSSL_STORE_SEARCH_BY_NAME:
360
0
            if ((name_der_sz = i2d_X509_NAME(search->name,
361
0
                     (unsigned char **)&name_der))
362
0
                    > 0
363
0
                && OSSL_PARAM_BLD_push_octet_string(bld,
364
0
                    OSSL_STORE_PARAM_SUBJECT,
365
0
                    name_der, name_der_sz))
366
0
                ret = 1;
367
0
            break;
368
0
        case OSSL_STORE_SEARCH_BY_ISSUER_SERIAL:
369
0
            if ((name_der_sz = i2d_X509_NAME(search->name,
370
0
                     (unsigned char **)&name_der))
371
0
                    > 0
372
0
                && (number = ASN1_INTEGER_to_BN(search->serial, NULL)) != NULL
373
0
                && OSSL_PARAM_BLD_push_octet_string(bld,
374
0
                    OSSL_STORE_PARAM_ISSUER,
375
0
                    name_der, name_der_sz)
376
0
                && OSSL_PARAM_BLD_push_BN(bld, OSSL_STORE_PARAM_SERIAL,
377
0
                    number))
378
0
                ret = 1;
379
0
            break;
380
0
        case OSSL_STORE_SEARCH_BY_KEY_FINGERPRINT:
381
0
            if (OSSL_PARAM_BLD_push_utf8_string(bld, OSSL_STORE_PARAM_DIGEST,
382
0
                    EVP_MD_get0_name(search->digest),
383
0
                    0)
384
0
                && OSSL_PARAM_BLD_push_octet_string(bld,
385
0
                    OSSL_STORE_PARAM_FINGERPRINT,
386
0
                    search->string,
387
0
                    search->stringlength))
388
0
                ret = 1;
389
0
            break;
390
0
        case OSSL_STORE_SEARCH_BY_ALIAS:
391
0
            if (OSSL_PARAM_BLD_push_utf8_string(bld, OSSL_STORE_PARAM_ALIAS,
392
0
                    (char *)search->string,
393
0
                    search->stringlength))
394
0
                ret = 1;
395
0
            break;
396
0
        }
397
0
        if (ret) {
398
0
            params = OSSL_PARAM_BLD_to_param(bld);
399
0
            ret = ctx->fetched_loader->p_set_ctx_params(ctx->loader_ctx,
400
0
                params);
401
0
            OSSL_PARAM_free(params);
402
0
        }
403
0
        OSSL_PARAM_BLD_free(bld);
404
0
        OPENSSL_free(name_der);
405
0
        BN_free(number);
406
0
    } else {
407
0
#ifndef OPENSSL_NO_DEPRECATED_3_0
408
        /* legacy loader section */
409
0
        if (ctx->loader->find == NULL) {
410
0
            ERR_raise(ERR_LIB_OSSL_STORE, OSSL_STORE_R_UNSUPPORTED_OPERATION);
411
0
            return 0;
412
0
        }
413
0
        ret = ctx->loader->find(ctx->loader_ctx, search);
414
0
#endif
415
0
    }
416
417
0
    return ret;
418
0
}
419
420
OSSL_STORE_INFO *OSSL_STORE_load(OSSL_STORE_CTX *ctx)
421
0
{
422
0
    OSSL_STORE_INFO *v = NULL;
423
424
0
    ctx->loading = 1;
425
0
again:
426
0
    if (OSSL_STORE_eof(ctx))
427
0
        return NULL;
428
429
0
    if (ctx->loader != NULL)
430
0
        OSSL_TRACE(STORE, "Loading next object\n");
431
432
0
    if (ctx->cached_info != NULL) {
433
0
        v = sk_OSSL_STORE_INFO_shift(ctx->cached_info);
434
0
    } else {
435
0
        if (ctx->fetched_loader != NULL) {
436
0
            struct ossl_load_result_data_st load_data;
437
438
0
            load_data.v = NULL;
439
0
            load_data.ctx = ctx;
440
0
            ctx->error_flag = 0;
441
442
0
            if (!ctx->fetched_loader->p_load(ctx->loader_ctx,
443
0
                    ossl_store_handle_load_result,
444
0
                    &load_data,
445
0
                    ossl_pw_passphrase_callback_dec,
446
0
                    &ctx->pwdata)) {
447
0
                ctx->error_flag = 1;
448
0
                return NULL;
449
0
            }
450
0
            v = load_data.v;
451
0
        }
452
0
#ifndef OPENSSL_NO_DEPRECATED_3_0
453
0
        if (ctx->fetched_loader == NULL)
454
0
            v = ctx->loader->load(ctx->loader_ctx,
455
0
                ctx->pwdata._.ui_method.ui_method,
456
0
                ctx->pwdata._.ui_method.ui_method_data);
457
0
#endif
458
0
    }
459
460
0
    if (ctx->post_process != NULL && v != NULL) {
461
0
        v = ctx->post_process(v, ctx->post_process_data);
462
463
        /*
464
         * By returning NULL, the callback decides that this object should
465
         * be ignored.
466
         */
467
0
        if (v == NULL)
468
0
            goto again;
469
0
    }
470
471
    /* Clear any internally cached passphrase */
472
0
    (void)ossl_pw_clear_passphrase_cache(&ctx->pwdata);
473
474
0
    if (v != NULL && ctx->expected_type != 0) {
475
0
        int returned_type = OSSL_STORE_INFO_get_type(v);
476
477
0
        if (returned_type != OSSL_STORE_INFO_NAME && returned_type != 0) {
478
0
            if (ctx->expected_type != returned_type) {
479
0
                OSSL_STORE_INFO_free(v);
480
0
                goto again;
481
0
            }
482
0
        }
483
0
    }
484
485
0
    if (v != NULL)
486
0
        OSSL_TRACE1(STORE, "Got a %s\n",
487
0
            OSSL_STORE_INFO_type_string(OSSL_STORE_INFO_get_type(v)));
488
489
0
    return v;
490
0
}
491
492
int OSSL_STORE_delete(const char *uri, OSSL_LIB_CTX *libctx, const char *propq,
493
    const UI_METHOD *ui_method, void *ui_data,
494
    const OSSL_PARAM params[])
495
0
{
496
0
    OSSL_STORE_LOADER *fetched_loader = NULL;
497
0
    char scheme[256], *p;
498
0
    int res = 0;
499
0
    struct ossl_passphrase_data_st pwdata = { 0 };
500
501
0
    OPENSSL_strlcpy(scheme, uri, sizeof(scheme));
502
0
    if ((p = strchr(scheme, ':')) != NULL)
503
0
        *p++ = '\0';
504
0
    else /* We don't work without explicit scheme */
505
0
        return 0;
506
507
0
    if (ui_method != NULL
508
0
        && (!ossl_pw_set_ui_method(&pwdata, ui_method, ui_data)
509
0
            || !ossl_pw_enable_passphrase_caching(&pwdata))) {
510
0
        ERR_raise(ERR_LIB_OSSL_STORE, ERR_R_CRYPTO_LIB);
511
0
        return 0;
512
0
    }
513
514
0
    OSSL_TRACE1(STORE, "Looking up scheme %s\n", scheme);
515
0
    fetched_loader = OSSL_STORE_LOADER_fetch(libctx, scheme, propq);
516
517
0
    if (fetched_loader != NULL && fetched_loader->p_delete != NULL) {
518
0
        const OSSL_PROVIDER *provider = OSSL_STORE_LOADER_get0_provider(fetched_loader);
519
0
        void *provctx = OSSL_PROVIDER_get0_provider_ctx(provider);
520
521
        /*
522
         * It's assumed that the loader's delete() method reports its own
523
         * errors
524
         */
525
0
        OSSL_TRACE1(STORE, "Performing URI delete %s\n", uri);
526
0
        res = fetched_loader->p_delete(provctx, uri, params,
527
0
            ossl_pw_passphrase_callback_dec,
528
0
            &pwdata);
529
0
    }
530
    /* Clear any internally cached passphrase */
531
0
    (void)ossl_pw_clear_passphrase_cache(&pwdata);
532
533
0
    OSSL_STORE_LOADER_free(fetched_loader);
534
535
0
    return res;
536
0
}
537
538
int OSSL_STORE_error(OSSL_STORE_CTX *ctx)
539
0
{
540
0
    int ret = 1;
541
542
0
    if (ctx->fetched_loader != NULL)
543
0
        ret = ctx->error_flag;
544
0
#ifndef OPENSSL_NO_DEPRECATED_3_0
545
0
    if (ctx->fetched_loader == NULL)
546
0
        ret = ctx->loader->error(ctx->loader_ctx);
547
0
#endif
548
0
    return ret;
549
0
}
550
551
int OSSL_STORE_eof(OSSL_STORE_CTX *ctx)
552
0
{
553
0
    int ret = 0;
554
555
0
    if (ctx->cached_info != NULL
556
0
        && sk_OSSL_STORE_INFO_num(ctx->cached_info) == 0) {
557
0
        sk_OSSL_STORE_INFO_free(ctx->cached_info);
558
0
        ctx->cached_info = NULL;
559
0
    }
560
561
0
    if (ctx->cached_info == NULL) {
562
0
        ret = 1;
563
0
        if (ctx->fetched_loader != NULL)
564
0
            ret = ctx->loader->p_eof(ctx->loader_ctx);
565
0
#ifndef OPENSSL_NO_DEPRECATED_3_0
566
0
        if (ctx->fetched_loader == NULL)
567
0
            ret = ctx->loader->eof(ctx->loader_ctx);
568
0
#endif
569
0
    }
570
0
    return ret != 0;
571
0
}
572
573
static int ossl_store_close_it(OSSL_STORE_CTX *ctx)
574
0
{
575
0
    int ret = 0;
576
577
0
    if (ctx == NULL)
578
0
        return 1;
579
0
    OSSL_TRACE1(STORE, "Closing %p\n", (void *)ctx->loader_ctx);
580
581
0
    if (ctx->fetched_loader != NULL)
582
0
        ret = ctx->loader->p_close(ctx->loader_ctx);
583
0
#ifndef OPENSSL_NO_DEPRECATED_3_0
584
0
    if (ctx->fetched_loader == NULL)
585
0
        ret = ctx->loader->closefn(ctx->loader_ctx);
586
0
#endif
587
588
0
    sk_OSSL_STORE_INFO_pop_free(ctx->cached_info, OSSL_STORE_INFO_free);
589
0
    OSSL_STORE_LOADER_free(ctx->fetched_loader);
590
0
    OPENSSL_free(ctx->properties);
591
0
    ossl_pw_clear_passphrase_data(&ctx->pwdata);
592
0
    return ret;
593
0
}
594
595
int OSSL_STORE_close(OSSL_STORE_CTX *ctx)
596
0
{
597
0
    int ret = ossl_store_close_it(ctx);
598
599
0
    OPENSSL_free(ctx);
600
0
    return ret;
601
0
}
602
603
/*
604
 * Functions to generate OSSL_STORE_INFOs, one function for each type we
605
 * support having in them as well as a generic constructor.
606
 *
607
 * In all cases, ownership of the object is transferred to the OSSL_STORE_INFO
608
 * and will therefore be freed when the OSSL_STORE_INFO is freed.
609
 */
610
OSSL_STORE_INFO *OSSL_STORE_INFO_new(int type, void *data)
611
0
{
612
0
    OSSL_STORE_INFO *info = OPENSSL_zalloc(sizeof(*info));
613
614
0
    if (info == NULL)
615
0
        return NULL;
616
617
0
    info->type = type;
618
0
    info->_.data = data;
619
0
    return info;
620
0
}
621
622
OSSL_STORE_INFO *OSSL_STORE_INFO_new_NAME(char *name)
623
0
{
624
0
    OSSL_STORE_INFO *info = OSSL_STORE_INFO_new(OSSL_STORE_INFO_NAME, NULL);
625
626
0
    if (info == NULL) {
627
0
        ERR_raise(ERR_LIB_OSSL_STORE, ERR_R_OSSL_STORE_LIB);
628
0
        return NULL;
629
0
    }
630
631
0
    info->_.name.name = name;
632
0
    info->_.name.desc = NULL;
633
634
0
    return info;
635
0
}
636
637
int OSSL_STORE_INFO_set0_NAME_description(OSSL_STORE_INFO *info, char *desc)
638
0
{
639
0
    if (info->type != OSSL_STORE_INFO_NAME) {
640
0
        ERR_raise(ERR_LIB_OSSL_STORE, ERR_R_PASSED_INVALID_ARGUMENT);
641
0
        return 0;
642
0
    }
643
644
0
    info->_.name.desc = desc;
645
646
0
    return 1;
647
0
}
648
OSSL_STORE_INFO *OSSL_STORE_INFO_new_PARAMS(EVP_PKEY *params)
649
0
{
650
0
    OSSL_STORE_INFO *info = OSSL_STORE_INFO_new(OSSL_STORE_INFO_PARAMS, params);
651
652
0
    if (info == NULL)
653
0
        ERR_raise(ERR_LIB_OSSL_STORE, ERR_R_OSSL_STORE_LIB);
654
0
    return info;
655
0
}
656
657
OSSL_STORE_INFO *OSSL_STORE_INFO_new_PUBKEY(EVP_PKEY *pkey)
658
0
{
659
0
    OSSL_STORE_INFO *info = OSSL_STORE_INFO_new(OSSL_STORE_INFO_PUBKEY, pkey);
660
661
0
    if (info == NULL)
662
0
        ERR_raise(ERR_LIB_OSSL_STORE, ERR_R_OSSL_STORE_LIB);
663
0
    return info;
664
0
}
665
666
OSSL_STORE_INFO *OSSL_STORE_INFO_new_PKEY(EVP_PKEY *pkey)
667
0
{
668
0
    OSSL_STORE_INFO *info = OSSL_STORE_INFO_new(OSSL_STORE_INFO_PKEY, pkey);
669
670
0
    if (info == NULL)
671
0
        ERR_raise(ERR_LIB_OSSL_STORE, ERR_R_OSSL_STORE_LIB);
672
0
    return info;
673
0
}
674
675
OSSL_STORE_INFO *OSSL_STORE_INFO_new_CERT(X509 *x509)
676
0
{
677
0
    OSSL_STORE_INFO *info = OSSL_STORE_INFO_new(OSSL_STORE_INFO_CERT, x509);
678
679
0
    if (info == NULL)
680
0
        ERR_raise(ERR_LIB_OSSL_STORE, ERR_R_OSSL_STORE_LIB);
681
0
    return info;
682
0
}
683
684
OSSL_STORE_INFO *OSSL_STORE_INFO_new_CRL(X509_CRL *crl)
685
0
{
686
0
    OSSL_STORE_INFO *info = OSSL_STORE_INFO_new(OSSL_STORE_INFO_CRL, crl);
687
688
0
    if (info == NULL)
689
0
        ERR_raise(ERR_LIB_OSSL_STORE, ERR_R_OSSL_STORE_LIB);
690
0
    return info;
691
0
}
692
693
/*
694
 * Functions to try to extract data from an OSSL_STORE_INFO.
695
 */
696
int OSSL_STORE_INFO_get_type(const OSSL_STORE_INFO *info)
697
0
{
698
0
    return info->type;
699
0
}
700
701
void *OSSL_STORE_INFO_get0_data(int type, const OSSL_STORE_INFO *info)
702
0
{
703
0
    if (info->type == type)
704
0
        return info->_.data;
705
0
    return NULL;
706
0
}
707
708
const char *OSSL_STORE_INFO_get0_NAME(const OSSL_STORE_INFO *info)
709
0
{
710
0
    if (info->type == OSSL_STORE_INFO_NAME)
711
0
        return info->_.name.name;
712
0
    return NULL;
713
0
}
714
715
char *OSSL_STORE_INFO_get1_NAME(const OSSL_STORE_INFO *info)
716
0
{
717
0
    if (info->type == OSSL_STORE_INFO_NAME)
718
0
        return OPENSSL_strdup(info->_.name.name);
719
0
    ERR_raise(ERR_LIB_OSSL_STORE, OSSL_STORE_R_NOT_A_NAME);
720
0
    return NULL;
721
0
}
722
723
const char *OSSL_STORE_INFO_get0_NAME_description(const OSSL_STORE_INFO *info)
724
0
{
725
0
    if (info->type == OSSL_STORE_INFO_NAME)
726
0
        return info->_.name.desc;
727
0
    return NULL;
728
0
}
729
730
char *OSSL_STORE_INFO_get1_NAME_description(const OSSL_STORE_INFO *info)
731
0
{
732
0
    if (info->type == OSSL_STORE_INFO_NAME)
733
0
        return OPENSSL_strdup(info->_.name.desc ? info->_.name.desc : "");
734
0
    ERR_raise(ERR_LIB_OSSL_STORE, OSSL_STORE_R_NOT_A_NAME);
735
0
    return NULL;
736
0
}
737
738
EVP_PKEY *OSSL_STORE_INFO_get0_PARAMS(const OSSL_STORE_INFO *info)
739
0
{
740
0
    if (info->type == OSSL_STORE_INFO_PARAMS)
741
0
        return info->_.params;
742
0
    return NULL;
743
0
}
744
745
EVP_PKEY *OSSL_STORE_INFO_get1_PARAMS(const OSSL_STORE_INFO *info)
746
0
{
747
0
    if (info->type == OSSL_STORE_INFO_PARAMS) {
748
0
        EVP_PKEY_up_ref(info->_.params);
749
0
        return info->_.params;
750
0
    }
751
0
    ERR_raise(ERR_LIB_OSSL_STORE, OSSL_STORE_R_NOT_PARAMETERS);
752
0
    return NULL;
753
0
}
754
755
EVP_PKEY *OSSL_STORE_INFO_get0_PUBKEY(const OSSL_STORE_INFO *info)
756
0
{
757
0
    if (info->type == OSSL_STORE_INFO_PUBKEY)
758
0
        return info->_.pubkey;
759
0
    return NULL;
760
0
}
761
762
EVP_PKEY *OSSL_STORE_INFO_get1_PUBKEY(const OSSL_STORE_INFO *info)
763
0
{
764
0
    if (info->type == OSSL_STORE_INFO_PUBKEY) {
765
0
        EVP_PKEY_up_ref(info->_.pubkey);
766
0
        return info->_.pubkey;
767
0
    }
768
0
    ERR_raise(ERR_LIB_OSSL_STORE, OSSL_STORE_R_NOT_A_PUBLIC_KEY);
769
0
    return NULL;
770
0
}
771
772
EVP_PKEY *OSSL_STORE_INFO_get0_PKEY(const OSSL_STORE_INFO *info)
773
0
{
774
0
    if (info->type == OSSL_STORE_INFO_PKEY)
775
0
        return info->_.pkey;
776
0
    return NULL;
777
0
}
778
779
EVP_PKEY *OSSL_STORE_INFO_get1_PKEY(const OSSL_STORE_INFO *info)
780
0
{
781
0
    if (info->type == OSSL_STORE_INFO_PKEY) {
782
0
        EVP_PKEY_up_ref(info->_.pkey);
783
0
        return info->_.pkey;
784
0
    }
785
0
    ERR_raise(ERR_LIB_OSSL_STORE, OSSL_STORE_R_NOT_A_PRIVATE_KEY);
786
0
    return NULL;
787
0
}
788
789
X509 *OSSL_STORE_INFO_get0_CERT(const OSSL_STORE_INFO *info)
790
0
{
791
0
    if (info->type == OSSL_STORE_INFO_CERT)
792
0
        return info->_.x509;
793
0
    return NULL;
794
0
}
795
796
X509 *OSSL_STORE_INFO_get1_CERT(const OSSL_STORE_INFO *info)
797
0
{
798
0
    if (info->type == OSSL_STORE_INFO_CERT) {
799
0
        X509_up_ref(info->_.x509);
800
0
        return info->_.x509;
801
0
    }
802
0
    ERR_raise(ERR_LIB_OSSL_STORE, OSSL_STORE_R_NOT_A_CERTIFICATE);
803
0
    return NULL;
804
0
}
805
806
X509_CRL *OSSL_STORE_INFO_get0_CRL(const OSSL_STORE_INFO *info)
807
0
{
808
0
    if (info->type == OSSL_STORE_INFO_CRL)
809
0
        return info->_.crl;
810
0
    return NULL;
811
0
}
812
813
X509_CRL *OSSL_STORE_INFO_get1_CRL(const OSSL_STORE_INFO *info)
814
0
{
815
0
    if (info->type == OSSL_STORE_INFO_CRL) {
816
0
        X509_CRL_up_ref(info->_.crl);
817
0
        return info->_.crl;
818
0
    }
819
0
    ERR_raise(ERR_LIB_OSSL_STORE, OSSL_STORE_R_NOT_A_CRL);
820
0
    return NULL;
821
0
}
822
823
/*
824
 * Free the OSSL_STORE_INFO
825
 */
826
void OSSL_STORE_INFO_free(OSSL_STORE_INFO *info)
827
0
{
828
0
    if (info != NULL) {
829
0
        switch (info->type) {
830
0
        case OSSL_STORE_INFO_NAME:
831
0
            OPENSSL_free(info->_.name.name);
832
0
            OPENSSL_free(info->_.name.desc);
833
0
            break;
834
0
        case OSSL_STORE_INFO_PARAMS:
835
0
            EVP_PKEY_free(info->_.params);
836
0
            break;
837
0
        case OSSL_STORE_INFO_PUBKEY:
838
0
            EVP_PKEY_free(info->_.pubkey);
839
0
            break;
840
0
        case OSSL_STORE_INFO_PKEY:
841
0
            EVP_PKEY_free(info->_.pkey);
842
0
            break;
843
0
        case OSSL_STORE_INFO_CERT:
844
0
            X509_free(info->_.x509);
845
0
            break;
846
0
        case OSSL_STORE_INFO_CRL:
847
0
            X509_CRL_free(info->_.crl);
848
0
            break;
849
0
        }
850
0
        OPENSSL_free(info);
851
0
    }
852
0
}
853
854
int OSSL_STORE_supports_search(OSSL_STORE_CTX *ctx, int search_type)
855
0
{
856
0
    int ret = 0;
857
858
0
    if (ctx->fetched_loader != NULL) {
859
0
        void *provctx = ossl_provider_ctx(OSSL_STORE_LOADER_get0_provider(ctx->fetched_loader));
860
0
        const OSSL_PARAM *params;
861
0
        const OSSL_PARAM *p_subject = NULL;
862
0
        const OSSL_PARAM *p_issuer = NULL;
863
0
        const OSSL_PARAM *p_serial = NULL;
864
0
        const OSSL_PARAM *p_fingerprint = NULL;
865
0
        const OSSL_PARAM *p_alias = NULL;
866
867
0
        if (ctx->fetched_loader->p_settable_ctx_params == NULL)
868
0
            return 0;
869
870
0
        params = ctx->fetched_loader->p_settable_ctx_params(provctx);
871
0
        p_subject = OSSL_PARAM_locate_const(params, OSSL_STORE_PARAM_SUBJECT);
872
0
        p_issuer = OSSL_PARAM_locate_const(params, OSSL_STORE_PARAM_ISSUER);
873
0
        p_serial = OSSL_PARAM_locate_const(params, OSSL_STORE_PARAM_SERIAL);
874
0
        p_fingerprint = OSSL_PARAM_locate_const(params, OSSL_STORE_PARAM_FINGERPRINT);
875
0
        p_alias = OSSL_PARAM_locate_const(params, OSSL_STORE_PARAM_ALIAS);
876
877
0
        switch (search_type) {
878
0
        case OSSL_STORE_SEARCH_BY_NAME:
879
0
            ret = (p_subject != NULL);
880
0
            break;
881
0
        case OSSL_STORE_SEARCH_BY_ISSUER_SERIAL:
882
0
            ret = (p_issuer != NULL && p_serial != NULL);
883
0
            break;
884
0
        case OSSL_STORE_SEARCH_BY_KEY_FINGERPRINT:
885
0
            ret = (p_fingerprint != NULL);
886
0
            break;
887
0
        case OSSL_STORE_SEARCH_BY_ALIAS:
888
0
            ret = (p_alias != NULL);
889
0
            break;
890
0
        }
891
0
    }
892
0
#ifndef OPENSSL_NO_DEPRECATED_3_0
893
0
    if (ctx->fetched_loader == NULL) {
894
0
        OSSL_STORE_SEARCH tmp_search;
895
896
0
        if (ctx->loader->find == NULL)
897
0
            return 0;
898
0
        tmp_search.search_type = search_type;
899
0
        ret = ctx->loader->find(NULL, &tmp_search);
900
0
    }
901
0
#endif
902
0
    return ret;
903
0
}
904
905
/* Search term constructors */
906
OSSL_STORE_SEARCH *OSSL_STORE_SEARCH_by_name(X509_NAME *name)
907
0
{
908
0
    OSSL_STORE_SEARCH *search = OPENSSL_zalloc(sizeof(*search));
909
910
0
    if (search == NULL)
911
0
        return NULL;
912
913
0
    search->search_type = OSSL_STORE_SEARCH_BY_NAME;
914
0
    search->name = name;
915
0
    return search;
916
0
}
917
918
OSSL_STORE_SEARCH *OSSL_STORE_SEARCH_by_issuer_serial(X509_NAME *name,
919
    const ASN1_INTEGER *serial)
920
0
{
921
0
    OSSL_STORE_SEARCH *search = OPENSSL_zalloc(sizeof(*search));
922
923
0
    if (search == NULL)
924
0
        return NULL;
925
926
0
    search->search_type = OSSL_STORE_SEARCH_BY_ISSUER_SERIAL;
927
0
    search->name = name;
928
0
    search->serial = serial;
929
0
    return search;
930
0
}
931
932
OSSL_STORE_SEARCH *OSSL_STORE_SEARCH_by_key_fingerprint(const EVP_MD *digest,
933
    const unsigned char
934
        *bytes,
935
    size_t len)
936
0
{
937
0
    OSSL_STORE_SEARCH *search = OPENSSL_zalloc(sizeof(*search));
938
0
    int md_size;
939
940
0
    if (search == NULL)
941
0
        return NULL;
942
943
0
    md_size = EVP_MD_get_size(digest);
944
0
    if (md_size <= 0) {
945
0
        OPENSSL_free(search);
946
0
        return NULL;
947
0
    }
948
949
0
    if (digest != NULL && len != (size_t)md_size) {
950
0
        ERR_raise_data(ERR_LIB_OSSL_STORE,
951
0
            OSSL_STORE_R_FINGERPRINT_SIZE_DOES_NOT_MATCH_DIGEST,
952
0
            "%s size is %d, fingerprint size is %zu",
953
0
            EVP_MD_get0_name(digest), md_size, len);
954
0
        OPENSSL_free(search);
955
0
        return NULL;
956
0
    }
957
958
0
    search->search_type = OSSL_STORE_SEARCH_BY_KEY_FINGERPRINT;
959
0
    search->digest = digest;
960
0
    search->string = bytes;
961
0
    search->stringlength = len;
962
0
    return search;
963
0
}
964
965
OSSL_STORE_SEARCH *OSSL_STORE_SEARCH_by_alias(const char *alias)
966
0
{
967
0
    OSSL_STORE_SEARCH *search = OPENSSL_zalloc(sizeof(*search));
968
969
0
    if (search == NULL)
970
0
        return NULL;
971
972
0
    search->search_type = OSSL_STORE_SEARCH_BY_ALIAS;
973
0
    search->string = (const unsigned char *)alias;
974
0
    search->stringlength = strlen(alias);
975
0
    return search;
976
0
}
977
978
/* Search term destructor */
979
void OSSL_STORE_SEARCH_free(OSSL_STORE_SEARCH *search)
980
0
{
981
0
    OPENSSL_free(search);
982
0
}
983
984
/* Search term accessors */
985
int OSSL_STORE_SEARCH_get_type(const OSSL_STORE_SEARCH *criterion)
986
0
{
987
0
    return criterion->search_type;
988
0
}
989
990
X509_NAME *OSSL_STORE_SEARCH_get0_name(const OSSL_STORE_SEARCH *criterion)
991
0
{
992
0
    return criterion->name;
993
0
}
994
995
const ASN1_INTEGER *OSSL_STORE_SEARCH_get0_serial(const OSSL_STORE_SEARCH
996
        *criterion)
997
0
{
998
0
    return criterion->serial;
999
0
}
1000
1001
const unsigned char *OSSL_STORE_SEARCH_get0_bytes(const OSSL_STORE_SEARCH
1002
                                                      *criterion,
1003
    size_t *length)
1004
0
{
1005
0
    *length = criterion->stringlength;
1006
0
    return criterion->string;
1007
0
}
1008
1009
const char *OSSL_STORE_SEARCH_get0_string(const OSSL_STORE_SEARCH *criterion)
1010
0
{
1011
0
    return (const char *)criterion->string;
1012
0
}
1013
1014
const EVP_MD *OSSL_STORE_SEARCH_get0_digest(const OSSL_STORE_SEARCH *criterion)
1015
0
{
1016
0
    return criterion->digest;
1017
0
}
1018
1019
OSSL_STORE_CTX *OSSL_STORE_attach(BIO *bp, const char *scheme,
1020
    OSSL_LIB_CTX *libctx, const char *propq,
1021
    const UI_METHOD *ui_method, void *ui_data,
1022
    const OSSL_PARAM params[],
1023
    OSSL_STORE_post_process_info_fn post_process,
1024
    void *post_process_data)
1025
0
{
1026
0
    const OSSL_STORE_LOADER *loader = NULL;
1027
0
    OSSL_STORE_LOADER *fetched_loader = NULL;
1028
0
    OSSL_STORE_LOADER_CTX *loader_ctx = NULL;
1029
0
    OSSL_STORE_CTX *ctx = NULL;
1030
1031
0
    if (scheme == NULL)
1032
0
        scheme = "file";
1033
1034
0
    OSSL_TRACE1(STORE, "Looking up scheme %s\n", scheme);
1035
0
    ERR_set_mark();
1036
0
#ifndef OPENSSL_NO_DEPRECATED_3_0
1037
0
    if ((loader = ossl_store_get0_loader_int(scheme)) != NULL)
1038
0
        loader_ctx = loader->attach(loader, bp, libctx, propq,
1039
0
            ui_method, ui_data);
1040
0
#endif
1041
0
    if (loader == NULL
1042
0
        && (fetched_loader = OSSL_STORE_LOADER_fetch(libctx, scheme, propq)) != NULL) {
1043
0
        const OSSL_PROVIDER *provider = OSSL_STORE_LOADER_get0_provider(fetched_loader);
1044
0
        void *provctx = OSSL_PROVIDER_get0_provider_ctx(provider);
1045
0
        OSSL_CORE_BIO *cbio = ossl_core_bio_new_from_bio(bp);
1046
1047
0
        if (cbio == NULL
1048
0
            || fetched_loader->p_attach == NULL
1049
0
            || (loader_ctx = fetched_loader->p_attach(provctx, cbio)) == NULL) {
1050
0
            OSSL_STORE_LOADER_free(fetched_loader);
1051
0
            fetched_loader = NULL;
1052
0
        } else if (!loader_set_params(fetched_loader, loader_ctx,
1053
0
                       params, propq)) {
1054
0
            (void)fetched_loader->p_close(loader_ctx);
1055
0
            OSSL_STORE_LOADER_free(fetched_loader);
1056
0
            fetched_loader = NULL;
1057
0
        }
1058
0
        loader = fetched_loader;
1059
0
        ossl_core_bio_free(cbio);
1060
0
    }
1061
1062
0
    if (loader_ctx == NULL) {
1063
0
        ERR_clear_last_mark();
1064
0
        return NULL;
1065
0
    }
1066
1067
0
    if ((ctx = OPENSSL_zalloc(sizeof(*ctx))) == NULL) {
1068
0
        ERR_clear_last_mark();
1069
0
        return NULL;
1070
0
    }
1071
1072
0
    if (ui_method != NULL
1073
0
        && !ossl_pw_set_ui_method(&ctx->pwdata, ui_method, ui_data)) {
1074
0
        ERR_clear_last_mark();
1075
0
        OPENSSL_free(ctx);
1076
0
        return NULL;
1077
0
    }
1078
1079
0
    ctx->fetched_loader = fetched_loader;
1080
0
    ctx->loader = loader;
1081
0
    ctx->loader_ctx = loader_ctx;
1082
0
    ctx->post_process = post_process;
1083
0
    ctx->post_process_data = post_process_data;
1084
1085
    /*
1086
     * ossl_store_get0_loader_int will raise an error if the loader for
1087
     * the scheme cannot be retrieved. But if a loader was successfully
1088
     * fetched then we remove this error from the error stack.
1089
     */
1090
0
    ERR_pop_to_mark();
1091
1092
0
    return ctx;
1093
0
}