Coverage Report

Created: 2025-11-16 06:40

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