Coverage Report

Created: 2025-08-28 07:07

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