Coverage Report

Created: 2025-06-13 06:58

/src/openssl32/crypto/encode_decode/decoder_pkey.c
Line
Count
Source (jump to first uncovered line)
1
/*
2
 * Copyright 2020-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 <openssl/core_names.h>
11
#include <openssl/core_object.h>
12
#include <openssl/provider.h>
13
#include <openssl/evp.h>
14
#include <openssl/ui.h>
15
#include <openssl/decoder.h>
16
#include <openssl/safestack.h>
17
#include <openssl/trace.h>
18
#include "crypto/evp.h"
19
#include "crypto/decoder.h"
20
#include "crypto/evp/evp_local.h"
21
#include "crypto/lhash.h"
22
#include "encoder_local.h"
23
#include "internal/namemap.h"
24
#include "internal/sizes.h"
25
26
int OSSL_DECODER_CTX_set_passphrase(OSSL_DECODER_CTX *ctx,
27
                                    const unsigned char *kstr,
28
                                    size_t klen)
29
0
{
30
0
    return ossl_pw_set_passphrase(&ctx->pwdata, kstr, klen);
31
0
}
32
33
int OSSL_DECODER_CTX_set_passphrase_ui(OSSL_DECODER_CTX *ctx,
34
                                       const UI_METHOD *ui_method,
35
                                       void *ui_data)
36
0
{
37
0
    return ossl_pw_set_ui_method(&ctx->pwdata, ui_method, ui_data);
38
0
}
39
40
int OSSL_DECODER_CTX_set_pem_password_cb(OSSL_DECODER_CTX *ctx,
41
                                         pem_password_cb *cb, void *cbarg)
42
55.4k
{
43
55.4k
    return ossl_pw_set_pem_password_cb(&ctx->pwdata, cb, cbarg);
44
55.4k
}
45
46
int OSSL_DECODER_CTX_set_passphrase_cb(OSSL_DECODER_CTX *ctx,
47
                                       OSSL_PASSPHRASE_CALLBACK *cb,
48
                                       void *cbarg)
49
0
{
50
0
    return ossl_pw_set_ossl_passphrase_cb(&ctx->pwdata, cb, cbarg);
51
0
}
52
53
/*
54
 * Support for OSSL_DECODER_CTX_new_for_pkey:
55
 * The construct data, and collecting keymgmt information for it
56
 */
57
58
DEFINE_STACK_OF(EVP_KEYMGMT)
59
60
struct decoder_pkey_data_st {
61
    OSSL_LIB_CTX *libctx;
62
    char *propq;
63
    int selection;
64
65
    STACK_OF(EVP_KEYMGMT) *keymgmts;
66
    char *object_type;           /* recorded object data type, may be NULL */
67
    void **object;               /* Where the result should end up */
68
};
69
70
static int decoder_construct_pkey(OSSL_DECODER_INSTANCE *decoder_inst,
71
                                  const OSSL_PARAM *params,
72
                                  void *construct_data)
73
960k
{
74
960k
    struct decoder_pkey_data_st *data = construct_data;
75
960k
    OSSL_DECODER *decoder = OSSL_DECODER_INSTANCE_get_decoder(decoder_inst);
76
960k
    void *decoderctx = OSSL_DECODER_INSTANCE_get_decoder_ctx(decoder_inst);
77
960k
    const OSSL_PROVIDER *decoder_prov = OSSL_DECODER_get0_provider(decoder);
78
960k
    EVP_KEYMGMT *keymgmt = NULL;
79
960k
    const OSSL_PROVIDER *keymgmt_prov = NULL;
80
960k
    int i, end;
81
    /*
82
     * |object_ref| points to a provider reference to an object, its exact
83
     * contents entirely opaque to us, but may be passed to any provider
84
     * function that expects this (such as OSSL_FUNC_keymgmt_load().
85
     *
86
     * This pointer is considered volatile, i.e. whatever it points at
87
     * is assumed to be freed as soon as this function returns.
88
     */
89
960k
    void *object_ref = NULL;
90
960k
    size_t object_ref_sz = 0;
91
960k
    const OSSL_PARAM *p;
92
93
960k
    p = OSSL_PARAM_locate_const(params, OSSL_OBJECT_PARAM_DATA_TYPE);
94
960k
    if (p != NULL) {
95
960k
        char *object_type = NULL;
96
97
960k
        if (!OSSL_PARAM_get_utf8_string(p, &object_type, 0))
98
0
            return 0;
99
960k
        OPENSSL_free(data->object_type);
100
960k
        data->object_type = object_type;
101
960k
    }
102
103
    /*
104
     * For stuff that should end up in an EVP_PKEY, we only accept an object
105
     * reference for the moment.  This enforces that the key data itself
106
     * remains with the provider.
107
     */
108
960k
    p = OSSL_PARAM_locate_const(params, OSSL_OBJECT_PARAM_REFERENCE);
109
960k
    if (p == NULL || p->data_type != OSSL_PARAM_OCTET_STRING)
110
618k
        return 0;
111
342k
    object_ref = p->data;
112
342k
    object_ref_sz = p->data_size;
113
114
    /*
115
     * First, we try to find a keymgmt that comes from the same provider as
116
     * the decoder that passed the params.
117
     */
118
342k
    end = sk_EVP_KEYMGMT_num(data->keymgmts);
119
712k
    for (i = 0; i < end; i++) {
120
712k
        keymgmt = sk_EVP_KEYMGMT_value(data->keymgmts, i);
121
712k
        keymgmt_prov = EVP_KEYMGMT_get0_provider(keymgmt);
122
123
712k
        if (keymgmt_prov == decoder_prov
124
712k
            && evp_keymgmt_has_load(keymgmt)
125
712k
            && EVP_KEYMGMT_is_a(keymgmt, data->object_type))
126
342k
            break;
127
712k
    }
128
342k
    if (i < end) {
129
        /* To allow it to be freed further down */
130
342k
        if (!EVP_KEYMGMT_up_ref(keymgmt))
131
0
            return 0;
132
342k
    } else if ((keymgmt = EVP_KEYMGMT_fetch(data->libctx,
133
0
                                            data->object_type,
134
0
                                            data->propq)) != NULL) {
135
0
        keymgmt_prov = EVP_KEYMGMT_get0_provider(keymgmt);
136
0
    }
137
138
342k
    if (keymgmt != NULL) {
139
342k
        EVP_PKEY *pkey = NULL;
140
342k
        void *keydata = NULL;
141
142
        /*
143
         * If the EVP_KEYMGMT and the OSSL_DECODER are from the
144
         * same provider, we assume that the KEYMGMT has a key loading
145
         * function that can handle the provider reference we hold.
146
         *
147
         * Otherwise, we export from the decoder and import the
148
         * result in the keymgmt.
149
         */
150
342k
        if (keymgmt_prov == decoder_prov) {
151
342k
            keydata = evp_keymgmt_load(keymgmt, object_ref, object_ref_sz);
152
342k
        } else {
153
0
            struct evp_keymgmt_util_try_import_data_st import_data;
154
155
0
            import_data.keymgmt = keymgmt;
156
0
            import_data.keydata = NULL;
157
0
            if (data->selection == 0)
158
                /* import/export functions do not tolerate 0 selection */
159
0
                import_data.selection = OSSL_KEYMGMT_SELECT_ALL;
160
0
            else
161
0
                import_data.selection = data->selection;
162
163
            /*
164
             * No need to check for errors here, the value of
165
             * |import_data.keydata| is as much an indicator.
166
             */
167
0
            (void)decoder->export_object(decoderctx,
168
0
                                         object_ref, object_ref_sz,
169
0
                                         &evp_keymgmt_util_try_import,
170
0
                                         &import_data);
171
0
            keydata = import_data.keydata;
172
0
            import_data.keydata = NULL;
173
0
        }
174
175
342k
        if (keydata != NULL
176
342k
            && (pkey = evp_keymgmt_util_make_pkey(keymgmt, keydata)) == NULL)
177
0
            evp_keymgmt_freedata(keymgmt, keydata);
178
179
342k
        *data->object = pkey;
180
181
        /*
182
         * evp_keymgmt_util_make_pkey() increments the reference count when
183
         * assigning the EVP_PKEY, so we can free the keymgmt here.
184
         */
185
342k
        EVP_KEYMGMT_free(keymgmt);
186
342k
    }
187
    /*
188
     * We successfully looked through, |*ctx->object| determines if we
189
     * actually found something.
190
     */
191
342k
    return (*data->object != NULL);
192
342k
}
193
194
static void decoder_clean_pkey_construct_arg(void *construct_data)
195
1.42M
{
196
1.42M
    struct decoder_pkey_data_st *data = construct_data;
197
198
1.42M
    if (data != NULL) {
199
959k
        sk_EVP_KEYMGMT_pop_free(data->keymgmts, EVP_KEYMGMT_free);
200
959k
        OPENSSL_free(data->propq);
201
959k
        OPENSSL_free(data->object_type);
202
959k
        OPENSSL_free(data);
203
959k
    }
204
1.42M
}
205
206
struct collect_data_st {
207
    OSSL_LIB_CTX *libctx;
208
    OSSL_DECODER_CTX *ctx;
209
210
    const char *keytype; /* the keytype requested, if any */
211
    int keytype_id; /* if keytype_resolved is set, keymgmt name_id; else 0 */
212
    int sm2_id;     /* if keytype_resolved is set and EC, SM2 name_id; else 0 */
213
    int total;      /* number of matching results */
214
    char error_occurred;
215
    char keytype_resolved;
216
217
    STACK_OF(EVP_KEYMGMT) *keymgmts;
218
};
219
220
static void collect_decoder_keymgmt(EVP_KEYMGMT *keymgmt, OSSL_DECODER *decoder,
221
                                    void *provctx, struct collect_data_st *data)
222
16.4M
{
223
16.4M
    void *decoderctx = NULL;
224
16.4M
    OSSL_DECODER_INSTANCE *di = NULL;
225
226
    /*
227
     * We already checked the EVP_KEYMGMT is applicable in check_keymgmt so we
228
     * don't check it again here.
229
     */
230
231
16.4M
    if (keymgmt->name_id != decoder->base.id)
232
        /* Mismatch is not an error, continue. */
233
15.5M
        return;
234
235
925k
    if ((decoderctx = decoder->newctx(provctx)) == NULL) {
236
0
        data->error_occurred = 1;
237
0
        return;
238
0
    }
239
240
925k
    if ((di = ossl_decoder_instance_new(decoder, decoderctx)) == NULL) {
241
0
        decoder->freectx(decoderctx);
242
0
        data->error_occurred = 1;
243
0
        return;
244
0
    }
245
246
925k
    OSSL_TRACE_BEGIN(DECODER) {
247
0
        BIO_printf(trc_out,
248
0
                   "(ctx %p) Checking out decoder %p:\n"
249
0
                   "    %s with %s\n",
250
0
                   (void *)data->ctx, (void *)decoder,
251
0
                   OSSL_DECODER_get0_name(decoder),
252
0
                   OSSL_DECODER_get0_properties(decoder));
253
925k
    } OSSL_TRACE_END(DECODER);
254
255
925k
    if (!ossl_decoder_ctx_add_decoder_inst(data->ctx, di)) {
256
0
        ossl_decoder_instance_free(di);
257
0
        data->error_occurred = 1;
258
0
        return;
259
0
    }
260
261
925k
    ++data->total;
262
925k
}
263
264
static void collect_decoder(OSSL_DECODER *decoder, void *arg)
265
9.42M
{
266
9.42M
    struct collect_data_st *data = arg;
267
9.42M
    STACK_OF(EVP_KEYMGMT) *keymgmts = data->keymgmts;
268
9.42M
    int i, end_i;
269
9.42M
    EVP_KEYMGMT *keymgmt;
270
9.42M
    const OSSL_PROVIDER *prov;
271
9.42M
    void *provctx;
272
273
9.42M
    if (data->error_occurred)
274
0
        return;
275
276
9.42M
    prov = OSSL_DECODER_get0_provider(decoder);
277
9.42M
    provctx = OSSL_PROVIDER_get0_provider_ctx(prov);
278
279
    /*
280
     * Either the caller didn't give us a selection, or if they did, the decoder
281
     * must tell us if it supports that selection to be accepted. If the decoder
282
     * doesn't have |does_selection|, it's seen as taking anything.
283
     */
284
9.42M
    if (decoder->does_selection != NULL
285
9.42M
            && !decoder->does_selection(provctx, data->ctx->selection))
286
4.49M
        return;
287
288
4.92M
    OSSL_TRACE_BEGIN(DECODER) {
289
0
        BIO_printf(trc_out,
290
0
                   "(ctx %p) Checking out decoder %p:\n"
291
0
                   "    %s with %s\n",
292
0
                   (void *)data->ctx, (void *)decoder,
293
0
                   OSSL_DECODER_get0_name(decoder),
294
0
                   OSSL_DECODER_get0_properties(decoder));
295
4.92M
    } OSSL_TRACE_END(DECODER);
296
297
4.92M
    end_i = sk_EVP_KEYMGMT_num(keymgmts);
298
21.3M
    for (i = 0; i < end_i; ++i) {
299
16.4M
        keymgmt = sk_EVP_KEYMGMT_value(keymgmts, i);
300
301
16.4M
        collect_decoder_keymgmt(keymgmt, decoder, provctx, data);
302
16.4M
        if (data->error_occurred)
303
0
            return;
304
16.4M
    }
305
4.92M
}
306
307
/*
308
 * Is this EVP_KEYMGMT applicable given the key type given in the call to
309
 * ossl_decoder_ctx_setup_for_pkey (if any)?
310
 */
311
static int check_keymgmt(EVP_KEYMGMT *keymgmt, struct collect_data_st *data)
312
4.36M
{
313
    /* If no keytype was specified, everything matches. */
314
4.36M
    if (data->keytype == NULL)
315
436k
        return 1;
316
317
3.93M
    if (!data->keytype_resolved) {
318
        /* We haven't cached the IDs from the keytype string yet. */
319
213k
        OSSL_NAMEMAP *namemap = ossl_namemap_stored(data->libctx);
320
213k
        data->keytype_id = ossl_namemap_name2num(namemap, data->keytype);
321
322
        /*
323
         * If keytype is a value ambiguously used for both EC and SM2,
324
         * collect the ID for SM2 as well.
325
         */
326
213k
        if (data->keytype_id != 0
327
213k
            && (strcmp(data->keytype, "id-ecPublicKey") == 0
328
188k
                || strcmp(data->keytype, "1.2.840.10045.2.1") == 0))
329
111k
            data->sm2_id = ossl_namemap_name2num(namemap, "SM2");
330
331
        /*
332
         * If keytype_id is zero the name was not found, but we still
333
         * set keytype_resolved to avoid trying all this again.
334
         */
335
213k
        data->keytype_resolved = 1;
336
213k
    }
337
338
    /* Specified keytype could not be resolved, so nothing matches. */
339
3.93M
    if (data->keytype_id == 0)
340
535k
        return 0;
341
342
    /* Does not match the keytype specified, so skip. */
343
3.39M
    if (keymgmt->name_id != data->keytype_id
344
3.39M
        && keymgmt->name_id != data->sm2_id)
345
3.09M
        return 0;
346
347
299k
    return 1;
348
3.39M
}
349
350
static void collect_keymgmt(EVP_KEYMGMT *keymgmt, void *arg)
351
4.36M
{
352
4.36M
    struct collect_data_st *data = arg;
353
354
4.36M
    if (!check_keymgmt(keymgmt, data))
355
3.63M
        return;
356
357
    /*
358
     * We have to ref EVP_KEYMGMT here because in the success case,
359
     * data->keymgmts is referenced by the constructor we register in the
360
     * OSSL_DECODER_CTX. The registered cleanup function
361
     * (decoder_clean_pkey_construct_arg) unrefs every element of the stack and
362
     * frees it.
363
     */
364
735k
    if (!EVP_KEYMGMT_up_ref(keymgmt))
365
0
        return;
366
367
735k
    if (sk_EVP_KEYMGMT_push(data->keymgmts, keymgmt) <= 0) {
368
0
        EVP_KEYMGMT_free(keymgmt);
369
0
        data->error_occurred = 1;
370
0
    }
371
735k
}
372
373
/*
374
 * This function does the actual binding of decoders to the OSSL_DECODER_CTX. It
375
 * searches for decoders matching 'keytype', which is a string like "RSA", "DH",
376
 * etc. If 'keytype' is NULL, decoders for all keytypes are bound.
377
 */
378
static int ossl_decoder_ctx_setup_for_pkey(OSSL_DECODER_CTX *ctx,
379
                                           const char *keytype,
380
                                           OSSL_LIB_CTX *libctx,
381
                                           const char *propquery)
382
8.04k
{
383
8.04k
    int ok = 0;
384
8.04k
    struct decoder_pkey_data_st *process_data = NULL;
385
8.04k
    struct collect_data_st collect_data = { NULL };
386
8.04k
    STACK_OF(EVP_KEYMGMT) *keymgmts = NULL;
387
388
8.04k
    OSSL_TRACE_BEGIN(DECODER) {
389
0
        const char *input_type = ctx->start_input_type;
390
0
        const char *input_structure = ctx->input_structure;
391
392
0
        BIO_printf(trc_out,
393
0
                   "(ctx %p) Looking for decoders producing %s%s%s%s%s%s\n",
394
0
                   (void *)ctx,
395
0
                   keytype != NULL ? keytype : "",
396
0
                   keytype != NULL ? " keys" : "keys of any type",
397
0
                   input_type != NULL ? " from " : "",
398
0
                   input_type != NULL ? input_type : "",
399
0
                   input_structure != NULL ? " with " : "",
400
0
                   input_structure != NULL ? input_structure : "");
401
8.04k
    } OSSL_TRACE_END(DECODER);
402
403
    /* Allocate data. */
404
8.04k
    if ((process_data = OPENSSL_zalloc(sizeof(*process_data))) == NULL)
405
0
        goto err;
406
8.04k
    if ((propquery != NULL
407
8.04k
            && (process_data->propq = OPENSSL_strdup(propquery)) == NULL))
408
0
        goto err;
409
410
    /* Allocate our list of EVP_KEYMGMTs. */
411
8.04k
    keymgmts = sk_EVP_KEYMGMT_new_null();
412
8.04k
    if (keymgmts == NULL) {
413
0
        ERR_raise(ERR_LIB_OSSL_DECODER, ERR_R_CRYPTO_LIB);
414
0
        goto err;
415
0
    }
416
417
8.04k
    process_data->object    = NULL;
418
8.04k
    process_data->libctx    = libctx;
419
8.04k
    process_data->selection = ctx->selection;
420
8.04k
    process_data->keymgmts  = keymgmts;
421
422
    /*
423
     * Enumerate all keymgmts into a stack.
424
     *
425
     * We could nest EVP_KEYMGMT_do_all_provided inside
426
     * OSSL_DECODER_do_all_provided or vice versa but these functions become
427
     * bottlenecks if called repeatedly, which is why we collect the
428
     * EVP_KEYMGMTs into a stack here and call both functions only once.
429
     *
430
     * We resolve the keytype string to a name ID so we don't have to resolve it
431
     * multiple times, avoiding repeated calls to EVP_KEYMGMT_is_a, which is a
432
     * performance bottleneck. However, we do this lazily on the first call to
433
     * collect_keymgmt made by EVP_KEYMGMT_do_all_provided, rather than do it
434
     * upfront, as this ensures that the names for all loaded providers have
435
     * been registered by the time we try to resolve the keytype string.
436
     */
437
8.04k
    collect_data.ctx        = ctx;
438
8.04k
    collect_data.libctx     = libctx;
439
8.04k
    collect_data.keymgmts   = keymgmts;
440
8.04k
    collect_data.keytype    = keytype;
441
8.04k
    EVP_KEYMGMT_do_all_provided(libctx, collect_keymgmt, &collect_data);
442
443
8.04k
    if (collect_data.error_occurred)
444
0
        goto err;
445
446
    /* Enumerate all matching decoders. */
447
8.04k
    OSSL_DECODER_do_all_provided(libctx, collect_decoder, &collect_data);
448
449
8.04k
    if (collect_data.error_occurred)
450
0
        goto err;
451
452
8.04k
    OSSL_TRACE_BEGIN(DECODER) {
453
0
        BIO_printf(trc_out,
454
0
                   "(ctx %p) Got %d decoders producing keys\n",
455
0
                   (void *)ctx, collect_data.total);
456
8.04k
    } OSSL_TRACE_END(DECODER);
457
458
    /*
459
     * Finish initializing the decoder context. If one or more decoders matched
460
     * above then the number of decoders attached to the OSSL_DECODER_CTX will
461
     * be nonzero. Else nothing was found and we do nothing.
462
     */
463
8.04k
    if (OSSL_DECODER_CTX_get_num_decoders(ctx) != 0) {
464
290
        if (!OSSL_DECODER_CTX_set_construct(ctx, decoder_construct_pkey)
465
290
            || !OSSL_DECODER_CTX_set_construct_data(ctx, process_data)
466
290
            || !OSSL_DECODER_CTX_set_cleanup(ctx,
467
290
                                             decoder_clean_pkey_construct_arg))
468
0
            goto err;
469
470
290
        process_data = NULL; /* Avoid it being freed */
471
290
    }
472
473
8.04k
    ok = 1;
474
8.04k
 err:
475
8.04k
    decoder_clean_pkey_construct_arg(process_data);
476
8.04k
    return ok;
477
8.04k
}
478
479
/* Only const here because deep_copy requires it */
480
static EVP_KEYMGMT *keymgmt_dup(const EVP_KEYMGMT *keymgmt)
481
2.96M
{
482
2.96M
    if (!EVP_KEYMGMT_up_ref((EVP_KEYMGMT *)keymgmt))
483
0
        return NULL;
484
485
2.96M
    return (EVP_KEYMGMT *)keymgmt;
486
2.96M
}
487
488
/*
489
 * Duplicates a template OSSL_DECODER_CTX that has been setup for an EVP_PKEY
490
 * operation and sets up the duplicate for a new operation.
491
 * It does not duplicate the pwdata on the assumption that this does not form
492
 * part of the template. That is set up later.
493
 */
494
static OSSL_DECODER_CTX *
495
ossl_decoder_ctx_for_pkey_dup(OSSL_DECODER_CTX *src,
496
                              EVP_PKEY **pkey,
497
                              const char *input_type,
498
                              const char *input_structure)
499
251k
{
500
251k
    OSSL_DECODER_CTX *dest;
501
251k
    struct decoder_pkey_data_st *process_data_src, *process_data_dest = NULL;
502
503
251k
    if (src == NULL)
504
0
        return NULL;
505
506
251k
    if ((dest = OSSL_DECODER_CTX_new()) == NULL) {
507
0
        ERR_raise(ERR_LIB_OSSL_DECODER, ERR_R_OSSL_DECODER_LIB);
508
0
        return NULL;
509
0
    }
510
511
251k
    if (!OSSL_DECODER_CTX_set_input_type(dest, input_type)
512
251k
            || !OSSL_DECODER_CTX_set_input_structure(dest, input_structure)) {
513
0
        ERR_raise(ERR_LIB_OSSL_DECODER, ERR_R_OSSL_DECODER_LIB);
514
0
        goto err;
515
0
    }
516
251k
    dest->selection = src->selection;
517
518
251k
    if (src->decoder_insts != NULL) {
519
234k
        dest->decoder_insts
520
234k
            = sk_OSSL_DECODER_INSTANCE_deep_copy(src->decoder_insts,
521
234k
                                                 ossl_decoder_instance_dup,
522
234k
                                                 ossl_decoder_instance_free);
523
234k
        if (dest->decoder_insts == NULL) {
524
0
            ERR_raise(ERR_LIB_OSSL_DECODER, ERR_R_OSSL_DECODER_LIB);
525
0
            goto err;
526
0
        }
527
234k
    }
528
529
251k
    if (!OSSL_DECODER_CTX_set_construct(dest,
530
251k
                                        OSSL_DECODER_CTX_get_construct(src))) {
531
0
        ERR_raise(ERR_LIB_OSSL_DECODER, ERR_R_OSSL_DECODER_LIB);
532
0
        goto err;
533
0
    }
534
535
251k
    process_data_src = OSSL_DECODER_CTX_get_construct_data(src);
536
251k
    if (process_data_src != NULL) {
537
234k
        process_data_dest = OPENSSL_zalloc(sizeof(*process_data_dest));
538
234k
        if (process_data_dest == NULL) {
539
0
            ERR_raise(ERR_LIB_OSSL_DECODER, ERR_R_CRYPTO_LIB);
540
0
            goto err;
541
0
        }
542
234k
        if (process_data_src->propq != NULL) {
543
0
            process_data_dest->propq = OPENSSL_strdup(process_data_src->propq);
544
0
            if (process_data_dest->propq == NULL) {
545
0
                ERR_raise(ERR_LIB_OSSL_DECODER, ERR_R_CRYPTO_LIB);
546
0
                goto err;
547
0
            }
548
0
        }
549
550
234k
        if (process_data_src->keymgmts != NULL) {
551
234k
            process_data_dest->keymgmts
552
234k
                = sk_EVP_KEYMGMT_deep_copy(process_data_src->keymgmts,
553
234k
                                           keymgmt_dup,
554
234k
                                           EVP_KEYMGMT_free);
555
234k
            if (process_data_dest->keymgmts == NULL) {
556
0
                ERR_raise(ERR_LIB_OSSL_DECODER, ERR_R_EVP_LIB);
557
0
                goto err;
558
0
            }
559
234k
        }
560
561
234k
        process_data_dest->object    = (void **)pkey;
562
234k
        process_data_dest->libctx    = process_data_src->libctx;
563
234k
        process_data_dest->selection = process_data_src->selection;
564
234k
        if (!OSSL_DECODER_CTX_set_construct_data(dest, process_data_dest)) {
565
0
            ERR_raise(ERR_LIB_OSSL_DECODER, ERR_R_OSSL_DECODER_LIB);
566
0
            goto err;
567
0
        }
568
234k
        process_data_dest = NULL;
569
234k
    }
570
571
251k
    if (!OSSL_DECODER_CTX_set_cleanup(dest,
572
251k
                                      OSSL_DECODER_CTX_get_cleanup(src))) {
573
0
        ERR_raise(ERR_LIB_OSSL_DECODER, ERR_R_OSSL_DECODER_LIB);
574
0
        goto err;
575
0
    }
576
577
251k
    return dest;
578
0
 err:
579
0
    if (process_data_dest != NULL) {
580
0
        OPENSSL_free(process_data_dest->propq);
581
0
        sk_EVP_KEYMGMT_pop_free(process_data_dest->keymgmts, EVP_KEYMGMT_free);
582
0
        OPENSSL_free(process_data_dest);
583
0
    }
584
0
    OSSL_DECODER_CTX_free(dest);
585
0
    return NULL;
586
251k
}
587
588
typedef struct {
589
    char *input_type;
590
    char *input_structure;
591
    char *keytype;
592
    int selection;
593
    char *propquery;
594
    OSSL_DECODER_CTX *template;
595
} DECODER_CACHE_ENTRY;
596
597
DEFINE_LHASH_OF_EX(DECODER_CACHE_ENTRY);
598
599
typedef struct {
600
    CRYPTO_RWLOCK *lock;
601
    LHASH_OF(DECODER_CACHE_ENTRY) *hashtable;
602
} DECODER_CACHE;
603
604
static void decoder_cache_entry_free(DECODER_CACHE_ENTRY *entry)
605
8.04k
{
606
8.04k
    if (entry == NULL)
607
0
        return;
608
8.04k
    OPENSSL_free(entry->input_type);
609
8.04k
    OPENSSL_free(entry->input_structure);
610
8.04k
    OPENSSL_free(entry->keytype);
611
8.04k
    OPENSSL_free(entry->propquery);
612
8.04k
    OSSL_DECODER_CTX_free(entry->template);
613
8.04k
    OPENSSL_free(entry);
614
8.04k
}
615
616
static unsigned long decoder_cache_entry_hash(const DECODER_CACHE_ENTRY *cache)
617
506k
{
618
506k
    unsigned long hash = 17;
619
620
506k
    hash = (hash * 23)
621
506k
           + (cache->propquery == NULL
622
506k
              ? 0 : ossl_lh_strcasehash(cache->propquery));
623
506k
    hash = (hash * 23)
624
506k
           + (cache->input_structure == NULL
625
506k
              ? 0  : ossl_lh_strcasehash(cache->input_structure));
626
506k
    hash = (hash * 23)
627
506k
           + (cache->input_type == NULL
628
506k
              ? 0  : ossl_lh_strcasehash(cache->input_type));
629
506k
    hash = (hash * 23)
630
506k
           + (cache->keytype == NULL
631
506k
              ? 0  : ossl_lh_strcasehash(cache->keytype));
632
633
506k
    hash ^= cache->selection;
634
635
506k
    return hash;
636
506k
}
637
638
static ossl_inline int nullstrcmp(const char *a, const char *b, int casecmp)
639
1.92M
{
640
1.92M
    if (a == NULL || b == NULL) {
641
626k
        if (a == NULL) {
642
626k
            if (b == NULL)
643
626k
                return 0;
644
0
            else
645
0
                return 1;
646
626k
        } else {
647
0
            return -1;
648
0
        }
649
1.30M
    } else {
650
1.30M
        if (casecmp)
651
1.30M
            return OPENSSL_strcasecmp(a, b);
652
0
        else
653
0
            return strcmp(a, b);
654
1.30M
    }
655
1.92M
}
656
657
static int decoder_cache_entry_cmp(const DECODER_CACHE_ENTRY *a,
658
                                   const DECODER_CACHE_ENTRY *b)
659
482k
{
660
482k
    int cmp;
661
662
482k
    if (a->selection != b->selection)
663
0
        return (a->selection < b->selection) ? -1 : 1;
664
665
482k
    cmp = nullstrcmp(a->keytype, b->keytype, 1);
666
482k
    if (cmp != 0)
667
18
        return cmp;
668
669
482k
    cmp = nullstrcmp(a->input_type, b->input_type, 1);
670
482k
    if (cmp != 0)
671
0
        return cmp;
672
673
482k
    cmp = nullstrcmp(a->input_structure, b->input_structure, 1);
674
482k
    if (cmp != 0)
675
0
        return cmp;
676
677
482k
    cmp = nullstrcmp(a->propquery, b->propquery, 0);
678
679
482k
    return cmp;
680
482k
}
681
682
void *ossl_decoder_cache_new(OSSL_LIB_CTX *ctx)
683
122
{
684
122
    DECODER_CACHE *cache = OPENSSL_malloc(sizeof(*cache));
685
686
122
    if (cache == NULL)
687
0
        return NULL;
688
689
122
    cache->lock = CRYPTO_THREAD_lock_new();
690
122
    if (cache->lock == NULL) {
691
0
        OPENSSL_free(cache);
692
0
        return NULL;
693
0
    }
694
122
    cache->hashtable = lh_DECODER_CACHE_ENTRY_new(decoder_cache_entry_hash,
695
122
                                                  decoder_cache_entry_cmp);
696
122
    if (cache->hashtable == NULL) {
697
0
        CRYPTO_THREAD_lock_free(cache->lock);
698
0
        OPENSSL_free(cache);
699
0
        return NULL;
700
0
    }
701
702
122
    return cache;
703
122
}
704
705
void ossl_decoder_cache_free(void *vcache)
706
80
{
707
80
    DECODER_CACHE *cache = (DECODER_CACHE *)vcache;
708
709
80
    lh_DECODER_CACHE_ENTRY_doall(cache->hashtable, decoder_cache_entry_free);
710
80
    lh_DECODER_CACHE_ENTRY_free(cache->hashtable);
711
80
    CRYPTO_THREAD_lock_free(cache->lock);
712
80
    OPENSSL_free(cache);
713
80
}
714
715
/*
716
 * Called whenever a provider gets activated/deactivated. In that case the
717
 * decoders that are available might change so we flush our cache.
718
 */
719
int ossl_decoder_cache_flush(OSSL_LIB_CTX *libctx)
720
109
{
721
109
    DECODER_CACHE *cache
722
109
        = ossl_lib_ctx_get_data(libctx, OSSL_LIB_CTX_DECODER_CACHE_INDEX);
723
724
109
    if (cache == NULL)
725
69
        return 0;
726
727
728
40
    if (!CRYPTO_THREAD_write_lock(cache->lock)) {
729
0
        ERR_raise(ERR_LIB_OSSL_DECODER, ERR_R_OSSL_DECODER_LIB);
730
0
        return 0;
731
0
    }
732
733
40
    lh_DECODER_CACHE_ENTRY_doall(cache->hashtable, decoder_cache_entry_free);
734
40
    lh_DECODER_CACHE_ENTRY_flush(cache->hashtable);
735
736
40
    CRYPTO_THREAD_unlock(cache->lock);
737
40
    return 1;
738
40
}
739
740
OSSL_DECODER_CTX *
741
OSSL_DECODER_CTX_new_for_pkey(EVP_PKEY **pkey,
742
                              const char *input_type,
743
                              const char *input_structure,
744
                              const char *keytype, int selection,
745
                              OSSL_LIB_CTX *libctx, const char *propquery)
746
251k
{
747
251k
    OSSL_DECODER_CTX *ctx = NULL;
748
251k
    OSSL_PARAM decoder_params[] = {
749
251k
        OSSL_PARAM_END,
750
251k
        OSSL_PARAM_END
751
251k
    };
752
251k
    DECODER_CACHE *cache
753
251k
        = ossl_lib_ctx_get_data(libctx, OSSL_LIB_CTX_DECODER_CACHE_INDEX);
754
251k
    DECODER_CACHE_ENTRY cacheent, *res, *newcache = NULL;
755
756
251k
    if (cache == NULL) {
757
0
        ERR_raise(ERR_LIB_OSSL_DECODER, ERR_R_OSSL_DECODER_LIB);
758
0
        return NULL;
759
0
    }
760
251k
    if (propquery != NULL)
761
0
        decoder_params[0] = OSSL_PARAM_construct_utf8_string(OSSL_DECODER_PARAM_PROPERTIES,
762
0
                                                             (char *)propquery, 0);
763
764
    /* It is safe to cast away the const here */
765
251k
    cacheent.input_type = (char *)input_type;
766
251k
    cacheent.input_structure = (char *)input_structure;
767
251k
    cacheent.keytype = (char *)keytype;
768
251k
    cacheent.selection = selection;
769
251k
    cacheent.propquery = (char *)propquery;
770
771
251k
    if (!CRYPTO_THREAD_read_lock(cache->lock)) {
772
0
        ERR_raise(ERR_LIB_OSSL_DECODER, ERR_R_CRYPTO_LIB);
773
0
        return NULL;
774
0
    }
775
776
    /* First see if we have a template OSSL_DECODER_CTX */
777
251k
    res = lh_DECODER_CACHE_ENTRY_retrieve(cache->hashtable, &cacheent);
778
779
251k
    if (res == NULL) {
780
        /*
781
         * There is no template so we will have to construct one. This will be
782
         * time consuming so release the lock and we will later upgrade it to a
783
         * write lock.
784
         */
785
4.01k
        CRYPTO_THREAD_unlock(cache->lock);
786
787
4.01k
        if ((ctx = OSSL_DECODER_CTX_new()) == NULL) {
788
0
            ERR_raise(ERR_LIB_OSSL_DECODER, ERR_R_OSSL_DECODER_LIB);
789
0
            return NULL;
790
0
        }
791
792
4.01k
        OSSL_TRACE_BEGIN(DECODER) {
793
0
            BIO_printf(trc_out,
794
0
                    "(ctx %p) Looking for %s decoders with selection %d\n",
795
0
                    (void *)ctx, keytype, selection);
796
0
            BIO_printf(trc_out, "    input type: %s, input structure: %s\n",
797
0
                    input_type, input_structure);
798
4.01k
        } OSSL_TRACE_END(DECODER);
799
800
4.01k
        if (OSSL_DECODER_CTX_set_input_type(ctx, input_type)
801
4.01k
            && OSSL_DECODER_CTX_set_input_structure(ctx, input_structure)
802
4.01k
            && OSSL_DECODER_CTX_set_selection(ctx, selection)
803
4.01k
            && ossl_decoder_ctx_setup_for_pkey(ctx, keytype, libctx, propquery)
804
4.01k
            && OSSL_DECODER_CTX_add_extra(ctx, libctx, propquery)
805
4.01k
            && (propquery == NULL
806
4.01k
                || OSSL_DECODER_CTX_set_params(ctx, decoder_params))) {
807
4.01k
            OSSL_TRACE_BEGIN(DECODER) {
808
0
                BIO_printf(trc_out, "(ctx %p) Got %d decoders\n",
809
0
                        (void *)ctx, OSSL_DECODER_CTX_get_num_decoders(ctx));
810
4.01k
            } OSSL_TRACE_END(DECODER);
811
4.01k
        } else {
812
0
            ERR_raise(ERR_LIB_OSSL_DECODER, ERR_R_OSSL_DECODER_LIB);
813
0
            OSSL_DECODER_CTX_free(ctx);
814
0
            return NULL;
815
0
        }
816
817
4.01k
        newcache = OPENSSL_zalloc(sizeof(*newcache));
818
4.01k
        if (newcache == NULL) {
819
0
            OSSL_DECODER_CTX_free(ctx);
820
0
            return NULL;
821
0
        }
822
823
4.01k
        if (input_type != NULL) {
824
4.01k
            newcache->input_type = OPENSSL_strdup(input_type);
825
4.01k
            if (newcache->input_type == NULL)
826
0
                goto err;
827
4.01k
        }
828
4.01k
        if (input_structure != NULL) {
829
4.01k
            newcache->input_structure = OPENSSL_strdup(input_structure);
830
4.01k
            if (newcache->input_structure == NULL)
831
0
                goto err;
832
4.01k
        }
833
4.01k
        if (keytype != NULL) {
834
4.00k
            newcache->keytype = OPENSSL_strdup(keytype);
835
4.00k
            if (newcache->keytype == NULL)
836
0
                goto err;
837
4.00k
        }
838
4.01k
        if (propquery != NULL) {
839
0
            newcache->propquery = OPENSSL_strdup(propquery);
840
0
            if (newcache->propquery == NULL)
841
0
                goto err;
842
0
        }
843
4.01k
        newcache->selection = selection;
844
4.01k
        newcache->template = ctx;
845
846
4.01k
        if (!CRYPTO_THREAD_write_lock(cache->lock)) {
847
0
            ctx = NULL;
848
0
            ERR_raise(ERR_LIB_OSSL_DECODER, ERR_R_CRYPTO_LIB);
849
0
            goto err;
850
0
        }
851
4.01k
        res = lh_DECODER_CACHE_ENTRY_retrieve(cache->hashtable, &cacheent);
852
4.01k
        if (res == NULL) {
853
4.01k
            (void)lh_DECODER_CACHE_ENTRY_insert(cache->hashtable, newcache);
854
4.01k
            if (lh_DECODER_CACHE_ENTRY_error(cache->hashtable)) {
855
0
                ctx = NULL;
856
0
                ERR_raise(ERR_LIB_OSSL_DECODER, ERR_R_CRYPTO_LIB);
857
0
                goto err;
858
0
            }
859
4.01k
        } else {
860
            /*
861
             * We raced with another thread to construct this and lost. Free
862
             * what we just created and use the entry from the hashtable instead
863
             */
864
0
            decoder_cache_entry_free(newcache);
865
0
            ctx = res->template;
866
0
        }
867
247k
    } else {
868
247k
        ctx = res->template;
869
247k
    }
870
871
251k
    ctx = ossl_decoder_ctx_for_pkey_dup(ctx, pkey, input_type, input_structure);
872
251k
    CRYPTO_THREAD_unlock(cache->lock);
873
874
251k
    return ctx;
875
0
 err:
876
0
    decoder_cache_entry_free(newcache);
877
0
    OSSL_DECODER_CTX_free(ctx);
878
0
    return NULL;
879
251k
}