Coverage Report

Created: 2025-11-16 06:40

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) != 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
    const OSSL_STORE_LOADER *loader = NULL;
70
0
    OSSL_STORE_LOADER *fetched_loader = NULL;
71
0
    OSSL_STORE_LOADER_CTX *loader_ctx = NULL;
72
0
    OSSL_STORE_CTX *ctx = NULL;
73
0
    char *propq_copy = NULL;
74
0
    int no_loader_found = 1;
75
0
    char scheme_copy[256], *p, *schemes[2], *scheme = NULL;
76
0
    size_t schemes_n = 0;
77
0
    size_t i;
78
79
0
    if (uri == NULL) {
80
0
        ERR_raise(ERR_LIB_OSSL_STORE, CRYPTO_R_INVALID_NULL_ARGUMENT);
81
0
        return 0;
82
0
    }
83
84
    /*
85
     * Put the file scheme first.  If the uri does represent an existing file,
86
     * possible device name and all, then it should be loaded.  Only a failed
87
     * attempt at loading a local file should have us try something else.
88
     */
89
0
    schemes[schemes_n++] = "file";
90
91
    /*
92
     * Now, check if we have something that looks like a scheme, and add it
93
     * as a second scheme.  However, also check if there's an authority start
94
     * (://), because that will invalidate the previous file scheme.  Also,
95
     * check that this isn't actually the file scheme, as there's no point
96
     * going through that one twice!
97
     */
98
0
    OPENSSL_strlcpy(scheme_copy, uri, sizeof(scheme_copy));
99
0
    if ((p = strchr(scheme_copy, ':')) != NULL) {
100
0
        *p++ = '\0';
101
0
        if (OPENSSL_strcasecmp(scheme_copy, "file") != 0) {
102
0
            if (strncmp(p, "//", 2) == 0)
103
0
                schemes_n--;         /* Invalidate the file scheme */
104
0
            schemes[schemes_n++] = scheme_copy;
105
0
        }
106
0
    }
107
108
0
    ERR_set_mark();
109
110
    /*
111
     * Try each scheme until we find one that could open the URI.
112
     *
113
     * For each scheme, we look for the engine implementation first, and
114
     * failing that, we then try to fetch a provided implementation.
115
     * This is consistent with how we handle legacy / engine implementations
116
     * elsewhere.
117
     */
118
0
    for (i = 0; loader_ctx == NULL && i < schemes_n; i++) {
119
0
        scheme = schemes[i];
120
0
        OSSL_TRACE1(STORE, "Looking up scheme %s\n", scheme);
121
0
#ifndef OPENSSL_NO_DEPRECATED_3_0
122
0
        if ((loader = ossl_store_get0_loader_int(scheme)) != NULL) {
123
0
            no_loader_found = 0;
124
0
            if (loader->open_ex != NULL)
125
0
                loader_ctx = loader->open_ex(loader, uri, libctx, propq,
126
0
                                             ui_method, ui_data);
127
0
            else
128
0
                loader_ctx = loader->open(loader, uri, ui_method, ui_data);
129
0
        }
130
0
#endif
131
0
        if (loader == NULL
132
0
            && (fetched_loader =
133
0
                OSSL_STORE_LOADER_fetch(libctx, scheme, propq)) != NULL) {
134
0
            const OSSL_PROVIDER *provider =
135
0
                OSSL_STORE_LOADER_get0_provider(fetched_loader);
136
0
            void *provctx = OSSL_PROVIDER_get0_provider_ctx(provider);
137
138
0
            no_loader_found = 0;
139
0
            loader_ctx = fetched_loader->p_open(provctx, uri);
140
0
            if (loader_ctx == NULL) {
141
0
                OSSL_STORE_LOADER_free(fetched_loader);
142
0
                fetched_loader = NULL;
143
0
            } else if(!loader_set_params(fetched_loader, loader_ctx,
144
0
                                         params, propq)) {
145
0
                (void)fetched_loader->p_close(loader_ctx);
146
0
                OSSL_STORE_LOADER_free(fetched_loader);
147
0
                fetched_loader = NULL;
148
0
            }
149
0
            loader = fetched_loader;
150
0
        }
151
0
    }
152
153
0
    if (no_loader_found)
154
        /*
155
         * It's assumed that ossl_store_get0_loader_int() and
156
         * OSSL_STORE_LOADER_fetch() report their own errors
157
         */
158
0
        goto err;
159
160
0
    OSSL_TRACE1(STORE, "Found loader for scheme %s\n", scheme);
161
162
0
    if (loader_ctx == NULL)
163
        /*
164
         * It's assumed that the loader's open() method reports its own
165
         * errors
166
         */
167
0
        goto err;
168
169
0
    OSSL_TRACE2(STORE, "Opened %s => %p\n", uri, (void *)loader_ctx);
170
171
0
    if ((propq != NULL && (propq_copy = OPENSSL_strdup(propq)) == NULL)
172
0
        || (ctx = OPENSSL_zalloc(sizeof(*ctx))) == NULL) {
173
0
        ERR_raise(ERR_LIB_OSSL_STORE, ERR_R_MALLOC_FAILURE);
174
0
        goto err;
175
0
    }
176
177
0
    if (ui_method != NULL
178
0
        && (!ossl_pw_set_ui_method(&ctx->pwdata, ui_method, ui_data)
179
0
            || !ossl_pw_enable_passphrase_caching(&ctx->pwdata))) {
180
0
        ERR_raise(ERR_LIB_OSSL_STORE, ERR_R_CRYPTO_LIB);
181
0
        goto err;
182
0
    }
183
0
    ctx->properties = propq_copy;
184
0
    ctx->fetched_loader = fetched_loader;
185
0
    ctx->loader = loader;
186
0
    ctx->loader_ctx = loader_ctx;
187
0
    ctx->post_process = post_process;
188
0
    ctx->post_process_data = post_process_data;
189
190
    /*
191
     * If the attempt to open with the 'file' scheme loader failed and the
192
     * other scheme loader succeeded, the failure to open with the 'file'
193
     * scheme loader leaves an error on the error stack.  Let's remove it.
194
     */
195
0
    ERR_pop_to_mark();
196
197
0
    return ctx;
198
199
0
 err:
200
0
    ERR_clear_last_mark();
201
0
    if (loader_ctx != NULL) {
202
        /*
203
         * Temporary structure so OSSL_STORE_close() can work even when
204
         * |ctx| couldn't be allocated properly
205
         */
206
0
        OSSL_STORE_CTX tmpctx = { NULL, };
207
208
0
        tmpctx.fetched_loader = fetched_loader;
209
0
        tmpctx.loader = loader;
210
0
        tmpctx.loader_ctx = loader_ctx;
211
212
        /*
213
         * We ignore a returned error because we will return NULL anyway in
214
         * this case, so if something goes wrong when closing, that'll simply
215
         * just add another entry on the error stack.
216
         */
217
0
        (void)ossl_store_close_it(&tmpctx);
218
0
    }
219
0
    OSSL_STORE_LOADER_free(fetched_loader);
220
0
    OPENSSL_free(propq_copy);
221
0
    OPENSSL_free(ctx);
222
0
    return NULL;
223
0
}
224
225
OSSL_STORE_CTX *OSSL_STORE_open(const char *uri,
226
                                const UI_METHOD *ui_method, void *ui_data,
227
                                OSSL_STORE_post_process_info_fn post_process,
228
                                void *post_process_data)
229
0
{
230
0
    return OSSL_STORE_open_ex(uri, NULL, NULL, ui_method, ui_data, NULL,
231
0
                              post_process, post_process_data);
232
0
}
233
234
#ifndef OPENSSL_NO_DEPRECATED_3_0
235
int OSSL_STORE_ctrl(OSSL_STORE_CTX *ctx, int cmd, ...)
236
0
{
237
0
    va_list args;
238
0
    int ret;
239
240
0
    va_start(args, cmd);
241
0
    ret = OSSL_STORE_vctrl(ctx, cmd, args);
242
0
    va_end(args);
243
244
0
    return ret;
245
0
}
246
247
int OSSL_STORE_vctrl(OSSL_STORE_CTX *ctx, int cmd, va_list args)
248
0
{
249
0
    if (ctx->fetched_loader != NULL) {
250
0
        if (ctx->fetched_loader->p_set_ctx_params != NULL) {
251
0
            OSSL_PARAM params[2] = { OSSL_PARAM_END, OSSL_PARAM_END };
252
253
0
            switch (cmd) {
254
0
            case OSSL_STORE_C_USE_SECMEM:
255
0
                {
256
0
                    int on = *(va_arg(args, int *));
257
258
0
                    params[0] = OSSL_PARAM_construct_int("use_secmem", &on);
259
0
                }
260
0
                break;
261
0
            default:
262
0
                break;
263
0
            }
264
265
0
            return ctx->fetched_loader->p_set_ctx_params(ctx->loader_ctx,
266
0
                                                         params);
267
0
        }
268
0
    } else if (ctx->loader->ctrl != NULL) {
269
0
        return ctx->loader->ctrl(ctx->loader_ctx, cmd, args);
270
0
    }
271
272
    /*
273
     * If the fetched loader doesn't have a set_ctx_params or a ctrl, it's as
274
     * if there was one that ignored our params, which usually returns 1.
275
     */
276
0
    return 1;
277
0
}
278
#endif
279
280
int OSSL_STORE_expect(OSSL_STORE_CTX *ctx, int expected_type)
281
0
{
282
0
    int ret = 1;
283
284
0
    if (ctx == NULL
285
0
            || expected_type < 0 || expected_type > OSSL_STORE_INFO_CRL) {
286
0
        ERR_raise(ERR_LIB_OSSL_STORE, ERR_R_PASSED_INVALID_ARGUMENT);
287
0
        return 0;
288
0
    }
289
0
    if (ctx->loading) {
290
0
        ERR_raise(ERR_LIB_OSSL_STORE, OSSL_STORE_R_LOADING_STARTED);
291
0
        return 0;
292
0
    }
293
294
0
    ctx->expected_type = expected_type;
295
0
    if (ctx->fetched_loader != NULL
296
0
        && ctx->fetched_loader->p_set_ctx_params != NULL) {
297
0
        OSSL_PARAM params[2] = { OSSL_PARAM_END, OSSL_PARAM_END };
298
299
0
        params[0] =
300
0
            OSSL_PARAM_construct_int(OSSL_STORE_PARAM_EXPECT, &expected_type);
301
0
        ret = ctx->fetched_loader->p_set_ctx_params(ctx->loader_ctx, params);
302
0
    }
303
0
#ifndef OPENSSL_NO_DEPRECATED_3_0
304
0
    if (ctx->fetched_loader == NULL
305
0
        && ctx->loader->expect != NULL) {
306
0
        ret = ctx->loader->expect(ctx->loader_ctx, expected_type);
307
0
    }
308
0
#endif
309
0
    return ret;
310
0
}
311
312
int OSSL_STORE_find(OSSL_STORE_CTX *ctx, const OSSL_STORE_SEARCH *search)
313
0
{
314
0
    int ret = 1;
315
316
0
    if (ctx->loading) {
317
0
        ERR_raise(ERR_LIB_OSSL_STORE, OSSL_STORE_R_LOADING_STARTED);
318
0
        return 0;
319
0
    }
320
0
    if (search == NULL) {
321
0
        ERR_raise(ERR_LIB_OSSL_STORE, ERR_R_PASSED_NULL_PARAMETER);
322
0
        return 0;
323
0
    }
324
325
0
    if (ctx->fetched_loader != NULL) {
326
0
        OSSL_PARAM_BLD *bld;
327
0
        OSSL_PARAM *params;
328
        /* OSSL_STORE_SEARCH_BY_NAME, OSSL_STORE_SEARCH_BY_ISSUER_SERIAL*/
329
0
        void *name_der = NULL;
330
0
        int name_der_sz;
331
        /* OSSL_STORE_SEARCH_BY_ISSUER_SERIAL */
332
0
        BIGNUM *number = NULL;
333
334
0
        if (ctx->fetched_loader->p_set_ctx_params == NULL) {
335
0
            ERR_raise(ERR_LIB_OSSL_STORE, OSSL_STORE_R_UNSUPPORTED_OPERATION);
336
0
            return 0;
337
0
        }
338
339
0
        if ((bld = OSSL_PARAM_BLD_new()) == NULL) {
340
0
            ERR_raise(ERR_LIB_OSSL_STORE, ERR_R_MALLOC_FAILURE);
341
0
            return 0;
342
0
        }
343
344
0
        ret = 0;                 /* Assume the worst */
345
346
0
        switch (search->search_type) {
347
0
        case OSSL_STORE_SEARCH_BY_NAME:
348
0
            if ((name_der_sz = i2d_X509_NAME(search->name,
349
0
                                             (unsigned char **)&name_der)) > 0
350
0
                && OSSL_PARAM_BLD_push_octet_string(bld,
351
0
                                                    OSSL_STORE_PARAM_SUBJECT,
352
0
                                                    name_der, name_der_sz))
353
0
                ret = 1;
354
0
            break;
355
0
        case OSSL_STORE_SEARCH_BY_ISSUER_SERIAL:
356
0
            if ((name_der_sz = i2d_X509_NAME(search->name,
357
0
                                             (unsigned char **)&name_der)) > 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
681
0
        if (ret == NULL)
682
0
            ERR_raise(ERR_LIB_OSSL_STORE, ERR_R_MALLOC_FAILURE);
683
0
        return ret;
684
0
    }
685
0
    ERR_raise(ERR_LIB_OSSL_STORE, OSSL_STORE_R_NOT_A_NAME);
686
0
    return NULL;
687
0
}
688
689
EVP_PKEY *OSSL_STORE_INFO_get0_PARAMS(const OSSL_STORE_INFO *info)
690
0
{
691
0
    if (info->type == OSSL_STORE_INFO_PARAMS)
692
0
        return info->_.params;
693
0
    return NULL;
694
0
}
695
696
EVP_PKEY *OSSL_STORE_INFO_get1_PARAMS(const OSSL_STORE_INFO *info)
697
0
{
698
0
    if (info->type == OSSL_STORE_INFO_PARAMS) {
699
0
        EVP_PKEY_up_ref(info->_.params);
700
0
        return info->_.params;
701
0
    }
702
0
    ERR_raise(ERR_LIB_OSSL_STORE, OSSL_STORE_R_NOT_PARAMETERS);
703
0
    return NULL;
704
0
}
705
706
EVP_PKEY *OSSL_STORE_INFO_get0_PUBKEY(const OSSL_STORE_INFO *info)
707
0
{
708
0
    if (info->type == OSSL_STORE_INFO_PUBKEY)
709
0
        return info->_.pubkey;
710
0
    return NULL;
711
0
}
712
713
EVP_PKEY *OSSL_STORE_INFO_get1_PUBKEY(const OSSL_STORE_INFO *info)
714
0
{
715
0
    if (info->type == OSSL_STORE_INFO_PUBKEY) {
716
0
        EVP_PKEY_up_ref(info->_.pubkey);
717
0
        return info->_.pubkey;
718
0
    }
719
0
    ERR_raise(ERR_LIB_OSSL_STORE, OSSL_STORE_R_NOT_A_PUBLIC_KEY);
720
0
    return NULL;
721
0
}
722
723
EVP_PKEY *OSSL_STORE_INFO_get0_PKEY(const OSSL_STORE_INFO *info)
724
0
{
725
0
    if (info->type == OSSL_STORE_INFO_PKEY)
726
0
        return info->_.pkey;
727
0
    return NULL;
728
0
}
729
730
EVP_PKEY *OSSL_STORE_INFO_get1_PKEY(const OSSL_STORE_INFO *info)
731
0
{
732
0
    if (info->type == OSSL_STORE_INFO_PKEY) {
733
0
        EVP_PKEY_up_ref(info->_.pkey);
734
0
        return info->_.pkey;
735
0
    }
736
0
    ERR_raise(ERR_LIB_OSSL_STORE, OSSL_STORE_R_NOT_A_PRIVATE_KEY);
737
0
    return NULL;
738
0
}
739
740
X509 *OSSL_STORE_INFO_get0_CERT(const OSSL_STORE_INFO *info)
741
0
{
742
0
    if (info->type == OSSL_STORE_INFO_CERT)
743
0
        return info->_.x509;
744
0
    return NULL;
745
0
}
746
747
X509 *OSSL_STORE_INFO_get1_CERT(const OSSL_STORE_INFO *info)
748
0
{
749
0
    if (info->type == OSSL_STORE_INFO_CERT) {
750
0
        X509_up_ref(info->_.x509);
751
0
        return info->_.x509;
752
0
    }
753
0
    ERR_raise(ERR_LIB_OSSL_STORE, OSSL_STORE_R_NOT_A_CERTIFICATE);
754
0
    return NULL;
755
0
}
756
757
X509_CRL *OSSL_STORE_INFO_get0_CRL(const OSSL_STORE_INFO *info)
758
0
{
759
0
    if (info->type == OSSL_STORE_INFO_CRL)
760
0
        return info->_.crl;
761
0
    return NULL;
762
0
}
763
764
X509_CRL *OSSL_STORE_INFO_get1_CRL(const OSSL_STORE_INFO *info)
765
0
{
766
0
    if (info->type == OSSL_STORE_INFO_CRL) {
767
0
        X509_CRL_up_ref(info->_.crl);
768
0
        return info->_.crl;
769
0
    }
770
0
    ERR_raise(ERR_LIB_OSSL_STORE, OSSL_STORE_R_NOT_A_CRL);
771
0
    return NULL;
772
0
}
773
774
/*
775
 * Free the OSSL_STORE_INFO
776
 */
777
void OSSL_STORE_INFO_free(OSSL_STORE_INFO *info)
778
0
{
779
0
    if (info != NULL) {
780
0
        switch (info->type) {
781
0
        case OSSL_STORE_INFO_NAME:
782
0
            OPENSSL_free(info->_.name.name);
783
0
            OPENSSL_free(info->_.name.desc);
784
0
            break;
785
0
        case OSSL_STORE_INFO_PARAMS:
786
0
            EVP_PKEY_free(info->_.params);
787
0
            break;
788
0
        case OSSL_STORE_INFO_PUBKEY:
789
0
            EVP_PKEY_free(info->_.pubkey);
790
0
            break;
791
0
        case OSSL_STORE_INFO_PKEY:
792
0
            EVP_PKEY_free(info->_.pkey);
793
0
            break;
794
0
        case OSSL_STORE_INFO_CERT:
795
0
            X509_free(info->_.x509);
796
0
            break;
797
0
        case OSSL_STORE_INFO_CRL:
798
0
            X509_CRL_free(info->_.crl);
799
0
            break;
800
0
        }
801
0
        OPENSSL_free(info);
802
0
    }
803
0
}
804
805
int OSSL_STORE_supports_search(OSSL_STORE_CTX *ctx, int search_type)
806
0
{
807
0
    int ret = 0;
808
809
0
    if (ctx->fetched_loader != NULL) {
810
0
        void *provctx =
811
0
            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 =
827
0
            OSSL_PARAM_locate_const(params, OSSL_STORE_PARAM_FINGERPRINT);
828
0
        p_alias = OSSL_PARAM_locate_const(params, OSSL_STORE_PARAM_ALIAS);
829
830
0
        switch (search_type) {
831
0
        case OSSL_STORE_SEARCH_BY_NAME:
832
0
            ret = (p_subject != NULL);
833
0
            break;
834
0
        case OSSL_STORE_SEARCH_BY_ISSUER_SERIAL:
835
0
            ret = (p_issuer != NULL && p_serial != NULL);
836
0
            break;
837
0
        case OSSL_STORE_SEARCH_BY_KEY_FINGERPRINT:
838
0
            ret = (p_fingerprint != NULL);
839
0
            break;
840
0
        case OSSL_STORE_SEARCH_BY_ALIAS:
841
0
            ret = (p_alias != NULL);
842
0
            break;
843
0
        }
844
0
    }
845
0
#ifndef OPENSSL_NO_DEPRECATED_3_0
846
0
    if (ctx->fetched_loader == NULL) {
847
0
        OSSL_STORE_SEARCH tmp_search;
848
849
0
        if (ctx->loader->find == NULL)
850
0
            return 0;
851
0
        tmp_search.search_type = search_type;
852
0
        ret = ctx->loader->find(NULL, &tmp_search);
853
0
    }
854
0
#endif
855
0
    return ret;
856
0
}
857
858
/* Search term constructors */
859
OSSL_STORE_SEARCH *OSSL_STORE_SEARCH_by_name(X509_NAME *name)
860
0
{
861
0
    OSSL_STORE_SEARCH *search = OPENSSL_zalloc(sizeof(*search));
862
863
0
    if (search == NULL) {
864
0
        ERR_raise(ERR_LIB_OSSL_STORE, ERR_R_MALLOC_FAILURE);
865
0
        return NULL;
866
0
    }
867
868
0
    search->search_type = OSSL_STORE_SEARCH_BY_NAME;
869
0
    search->name = name;
870
0
    return search;
871
0
}
872
873
OSSL_STORE_SEARCH *OSSL_STORE_SEARCH_by_issuer_serial(X509_NAME *name,
874
                                                      const ASN1_INTEGER *serial)
875
0
{
876
0
    OSSL_STORE_SEARCH *search = OPENSSL_zalloc(sizeof(*search));
877
878
0
    if (search == NULL) {
879
0
        ERR_raise(ERR_LIB_OSSL_STORE, ERR_R_MALLOC_FAILURE);
880
0
        return NULL;
881
0
    }
882
883
0
    search->search_type = OSSL_STORE_SEARCH_BY_ISSUER_SERIAL;
884
0
    search->name = name;
885
0
    search->serial = serial;
886
0
    return search;
887
0
}
888
889
OSSL_STORE_SEARCH *OSSL_STORE_SEARCH_by_key_fingerprint(const EVP_MD *digest,
890
                                                        const unsigned char
891
                                                        *bytes, 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, size_t *length)
956
0
{
957
0
    *length = criterion->stringlength;
958
0
    return criterion->string;
959
0
}
960
961
const char *OSSL_STORE_SEARCH_get0_string(const OSSL_STORE_SEARCH *criterion)
962
0
{
963
0
    return (const char *)criterion->string;
964
0
}
965
966
const EVP_MD *OSSL_STORE_SEARCH_get0_digest(const OSSL_STORE_SEARCH *criterion)
967
0
{
968
0
    return criterion->digest;
969
0
}
970
971
OSSL_STORE_CTX *OSSL_STORE_attach(BIO *bp, const char *scheme,
972
                                  OSSL_LIB_CTX *libctx, const char *propq,
973
                                  const UI_METHOD *ui_method, void *ui_data,
974
                                  const OSSL_PARAM params[],
975
                                  OSSL_STORE_post_process_info_fn post_process,
976
                                  void *post_process_data)
977
0
{
978
0
    const OSSL_STORE_LOADER *loader = NULL;
979
0
    OSSL_STORE_LOADER *fetched_loader = NULL;
980
0
    OSSL_STORE_LOADER_CTX *loader_ctx = NULL;
981
0
    OSSL_STORE_CTX *ctx = NULL;
982
983
0
    if (scheme == NULL)
984
0
        scheme = "file";
985
986
0
    OSSL_TRACE1(STORE, "Looking up scheme %s\n", scheme);
987
0
    ERR_set_mark();
988
0
#ifndef OPENSSL_NO_DEPRECATED_3_0
989
0
    if ((loader = ossl_store_get0_loader_int(scheme)) != NULL)
990
0
        loader_ctx = loader->attach(loader, bp, libctx, propq,
991
0
                                    ui_method, ui_data);
992
0
#endif
993
0
    if (loader == NULL
994
0
        && (fetched_loader =
995
0
            OSSL_STORE_LOADER_fetch(libctx, scheme, propq)) != NULL) {
996
0
        const OSSL_PROVIDER *provider =
997
0
            OSSL_STORE_LOADER_get0_provider(fetched_loader);
998
0
        void *provctx = OSSL_PROVIDER_get0_provider_ctx(provider);
999
0
        OSSL_CORE_BIO *cbio = ossl_core_bio_new_from_bio(bp);
1000
1001
0
        if (cbio == NULL
1002
0
            || (loader_ctx = fetched_loader->p_attach(provctx, cbio)) == NULL) {
1003
0
            OSSL_STORE_LOADER_free(fetched_loader);
1004
0
            fetched_loader = NULL;
1005
0
        } else if (!loader_set_params(fetched_loader, loader_ctx,
1006
0
                                      params, propq)) {
1007
0
            (void)fetched_loader->p_close(loader_ctx);
1008
0
            OSSL_STORE_LOADER_free(fetched_loader);
1009
0
            fetched_loader = NULL;
1010
0
        }
1011
0
        loader = fetched_loader;
1012
0
        ossl_core_bio_free(cbio);
1013
0
    }
1014
1015
0
    if (loader_ctx == NULL) {
1016
0
        ERR_clear_last_mark();
1017
0
        return NULL;
1018
0
    }
1019
1020
0
    if ((ctx = OPENSSL_zalloc(sizeof(*ctx))) == NULL) {
1021
0
        ERR_clear_last_mark();
1022
0
        ERR_raise(ERR_LIB_OSSL_STORE, ERR_R_MALLOC_FAILURE);
1023
0
        return NULL;
1024
0
    }
1025
1026
0
    if (ui_method != NULL
1027
0
        && !ossl_pw_set_ui_method(&ctx->pwdata, ui_method, ui_data)) {
1028
0
        ERR_clear_last_mark();
1029
0
        OPENSSL_free(ctx);
1030
0
        return NULL;
1031
0
    }
1032
1033
0
    ctx->fetched_loader = fetched_loader;
1034
0
    ctx->loader = loader;
1035
0
    ctx->loader_ctx = loader_ctx;
1036
0
    ctx->post_process = post_process;
1037
0
    ctx->post_process_data = post_process_data;
1038
1039
    /*
1040
     * ossl_store_get0_loader_int will raise an error if the loader for the
1041
     * the scheme cannot be retrieved. But if a loader was successfully
1042
     * fetched then we remove this error from the error stack.
1043
     */
1044
0
    ERR_pop_to_mark();
1045
1046
0
    return ctx;
1047
0
}