Coverage Report

Created: 2025-12-31 06:58

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