Coverage Report

Created: 2025-06-13 06:58

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