Coverage Report

Created: 2025-12-31 06:58

next uncovered line (L), next uncovered region (R), next uncovered branch (B)
/src/openssl30/crypto/encode_decode/decoder_pkey.c
Line
Count
Source
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 "encoder_local.h"
21
22
int OSSL_DECODER_CTX_set_passphrase(OSSL_DECODER_CTX *ctx,
23
    const unsigned char *kstr,
24
    size_t klen)
25
0
{
26
0
    return ossl_pw_set_passphrase(&ctx->pwdata, kstr, klen);
27
0
}
28
29
int OSSL_DECODER_CTX_set_passphrase_ui(OSSL_DECODER_CTX *ctx,
30
    const UI_METHOD *ui_method,
31
    void *ui_data)
32
0
{
33
0
    return ossl_pw_set_ui_method(&ctx->pwdata, ui_method, ui_data);
34
0
}
35
36
int OSSL_DECODER_CTX_set_pem_password_cb(OSSL_DECODER_CTX *ctx,
37
    pem_password_cb *cb, void *cbarg)
38
124k
{
39
124k
    return ossl_pw_set_pem_password_cb(&ctx->pwdata, cb, cbarg);
40
124k
}
41
42
int OSSL_DECODER_CTX_set_passphrase_cb(OSSL_DECODER_CTX *ctx,
43
    OSSL_PASSPHRASE_CALLBACK *cb,
44
    void *cbarg)
45
0
{
46
0
    return ossl_pw_set_ossl_passphrase_cb(&ctx->pwdata, cb, cbarg);
47
0
}
48
49
/*
50
 * Support for OSSL_DECODER_CTX_new_for_pkey:
51
 * The construct data, and collecting keymgmt information for it
52
 */
53
54
DEFINE_STACK_OF(EVP_KEYMGMT)
55
56
struct decoder_pkey_data_st {
57
    OSSL_LIB_CTX *libctx;
58
    char *propq;
59
    int selection;
60
61
    STACK_OF(EVP_KEYMGMT) *keymgmts;
62
    char *object_type; /* recorded object data type, may be NULL */
63
    void **object; /* Where the result should end up */
64
};
65
66
static int decoder_construct_pkey(OSSL_DECODER_INSTANCE *decoder_inst,
67
    const OSSL_PARAM *params,
68
    void *construct_data)
69
929k
{
70
929k
    struct decoder_pkey_data_st *data = construct_data;
71
929k
    OSSL_DECODER *decoder = OSSL_DECODER_INSTANCE_get_decoder(decoder_inst);
72
929k
    void *decoderctx = OSSL_DECODER_INSTANCE_get_decoder_ctx(decoder_inst);
73
929k
    const OSSL_PROVIDER *decoder_prov = OSSL_DECODER_get0_provider(decoder);
74
929k
    EVP_KEYMGMT *keymgmt = NULL;
75
929k
    const OSSL_PROVIDER *keymgmt_prov = NULL;
76
929k
    int i, end;
77
    /*
78
     * |object_ref| points to a provider reference to an object, its exact
79
     * contents entirely opaque to us, but may be passed to any provider
80
     * function that expects this (such as OSSL_FUNC_keymgmt_load().
81
     *
82
     * This pointer is considered volatile, i.e. whatever it points at
83
     * is assumed to be freed as soon as this function returns.
84
     */
85
929k
    void *object_ref = NULL;
86
929k
    size_t object_ref_sz = 0;
87
929k
    const OSSL_PARAM *p;
88
89
929k
    p = OSSL_PARAM_locate_const(params, OSSL_OBJECT_PARAM_DATA_TYPE);
90
929k
    if (p != NULL) {
91
920k
        char *object_type = NULL;
92
93
920k
        if (!OSSL_PARAM_get_utf8_string(p, &object_type, 0))
94
0
            return 0;
95
920k
        OPENSSL_free(data->object_type);
96
920k
        data->object_type = object_type;
97
920k
    }
98
99
    /*
100
     * For stuff that should end up in an EVP_PKEY, we only accept an object
101
     * reference for the moment.  This enforces that the key data itself
102
     * remains with the provider.
103
     */
104
929k
    p = OSSL_PARAM_locate_const(params, OSSL_OBJECT_PARAM_REFERENCE);
105
929k
    if (p == NULL || p->data_type != OSSL_PARAM_OCTET_STRING)
106
590k
        return 0;
107
339k
    object_ref = p->data;
108
339k
    object_ref_sz = p->data_size;
109
110
    /*
111
     * First, we try to find a keymgmt that comes from the same provider as
112
     * the decoder that passed the params.
113
     */
114
339k
    end = sk_EVP_KEYMGMT_num(data->keymgmts);
115
758k
    for (i = 0; i < end; i++) {
116
758k
        keymgmt = sk_EVP_KEYMGMT_value(data->keymgmts, i);
117
758k
        keymgmt_prov = EVP_KEYMGMT_get0_provider(keymgmt);
118
119
758k
        if (keymgmt_prov == decoder_prov
120
758k
            && evp_keymgmt_has_load(keymgmt)
121
758k
            && EVP_KEYMGMT_is_a(keymgmt, data->object_type))
122
339k
            break;
123
758k
    }
124
339k
    if (i < end) {
125
        /* To allow it to be freed further down */
126
339k
        if (!EVP_KEYMGMT_up_ref(keymgmt))
127
0
            return 0;
128
339k
    } else if ((keymgmt = EVP_KEYMGMT_fetch(data->libctx,
129
0
                    data->object_type,
130
0
                    data->propq))
131
0
        != NULL) {
132
0
        keymgmt_prov = EVP_KEYMGMT_get0_provider(keymgmt);
133
0
    }
134
135
339k
    if (keymgmt != NULL) {
136
339k
        EVP_PKEY *pkey = NULL;
137
339k
        void *keydata = NULL;
138
139
        /*
140
         * If the EVP_KEYMGMT and the OSSL_DECODER are from the
141
         * same provider, we assume that the KEYMGMT has a key loading
142
         * function that can handle the provider reference we hold.
143
         *
144
         * Otherwise, we export from the decoder and import the
145
         * result in the keymgmt.
146
         */
147
339k
        if (keymgmt_prov == decoder_prov) {
148
339k
            keydata = evp_keymgmt_load(keymgmt, object_ref, object_ref_sz);
149
339k
        } else {
150
0
            struct evp_keymgmt_util_try_import_data_st import_data;
151
152
0
            import_data.keymgmt = keymgmt;
153
0
            import_data.keydata = NULL;
154
0
            if (data->selection == 0)
155
                /* import/export functions do not tolerate 0 selection */
156
0
                import_data.selection = OSSL_KEYMGMT_SELECT_ALL;
157
0
            else
158
0
                import_data.selection = data->selection;
159
160
            /*
161
             * No need to check for errors here, the value of
162
             * |import_data.keydata| is as much an indicator.
163
             */
164
0
            (void)decoder->export_object(decoderctx,
165
0
                object_ref, object_ref_sz,
166
0
                &evp_keymgmt_util_try_import,
167
0
                &import_data);
168
0
            keydata = import_data.keydata;
169
0
            import_data.keydata = NULL;
170
0
        }
171
172
339k
        if (keydata != NULL
173
339k
            && (pkey = evp_keymgmt_util_make_pkey(keymgmt, keydata)) == NULL)
174
0
            evp_keymgmt_freedata(keymgmt, keydata);
175
176
339k
        *data->object = pkey;
177
178
        /*
179
         * evp_keymgmt_util_make_pkey() increments the reference count when
180
         * assigning the EVP_PKEY, so we can free the keymgmt here.
181
         */
182
339k
        EVP_KEYMGMT_free(keymgmt);
183
339k
    }
184
    /*
185
     * We successfully looked through, |*ctx->object| determines if we
186
     * actually found something.
187
     */
188
339k
    return (*data->object != NULL);
189
339k
}
190
191
static void decoder_clean_pkey_construct_arg(void *construct_data)
192
1.52M
{
193
1.52M
    struct decoder_pkey_data_st *data = construct_data;
194
195
1.52M
    if (data != NULL) {
196
1.27M
        sk_EVP_KEYMGMT_pop_free(data->keymgmts, EVP_KEYMGMT_free);
197
1.27M
        OPENSSL_free(data->propq);
198
1.27M
        OPENSSL_free(data->object_type);
199
1.27M
        OPENSSL_free(data);
200
1.27M
    }
201
1.52M
}
202
203
static void collect_name(const char *name, void *arg)
204
3.66M
{
205
3.66M
    STACK_OF(OPENSSL_CSTRING) *names = arg;
206
207
3.66M
    sk_OPENSSL_CSTRING_push(names, name);
208
3.66M
}
209
210
static void collect_keymgmt(EVP_KEYMGMT *keymgmt, void *arg)
211
4.71M
{
212
4.71M
    STACK_OF(EVP_KEYMGMT) *keymgmts = arg;
213
214
4.71M
    if (!EVP_KEYMGMT_up_ref(keymgmt) /* ref++ */)
215
0
        return;
216
4.71M
    if (sk_EVP_KEYMGMT_push(keymgmts, keymgmt) <= 0) {
217
0
        EVP_KEYMGMT_free(keymgmt); /* ref-- */
218
0
        return;
219
0
    }
220
4.71M
}
221
222
struct collect_decoder_data_st {
223
    STACK_OF(OPENSSL_CSTRING) *names;
224
    OSSL_DECODER_CTX *ctx;
225
226
    int total;
227
    unsigned int error_occurred : 1;
228
};
229
230
static void collect_decoder(OSSL_DECODER *decoder, void *arg)
231
10.4M
{
232
10.4M
    struct collect_decoder_data_st *data = arg;
233
10.4M
    size_t i, end_i;
234
10.4M
    const OSSL_PROVIDER *prov = OSSL_DECODER_get0_provider(decoder);
235
10.4M
    void *provctx = OSSL_PROVIDER_get0_provider_ctx(prov);
236
237
10.4M
    if (data->error_occurred)
238
0
        return;
239
240
10.4M
    if (data->names == NULL) {
241
0
        data->error_occurred = 1;
242
0
        return;
243
0
    }
244
245
    /*
246
     * Either the caller didn't give a selection, or if they did,
247
     * the decoder must tell us if it supports that selection to
248
     * be accepted.  If the decoder doesn't have |does_selection|,
249
     * it's seen as taking anything.
250
     */
251
10.4M
    if (decoder->does_selection != NULL
252
9.68M
        && !decoder->does_selection(provctx, data->ctx->selection))
253
5.01M
        return;
254
255
5.46M
    OSSL_TRACE_BEGIN(DECODER)
256
0
    {
257
0
        BIO_printf(trc_out,
258
0
            "(ctx %p) Checking out decoder %p:\n"
259
0
            "    %s with %s\n",
260
0
            (void *)data->ctx, (void *)decoder,
261
0
            OSSL_DECODER_get0_name(decoder),
262
0
            OSSL_DECODER_get0_properties(decoder));
263
0
    }
264
5.46M
    OSSL_TRACE_END(DECODER);
265
266
5.46M
    end_i = sk_OPENSSL_CSTRING_num(data->names);
267
52.6M
    for (i = 0; i < end_i; i++) {
268
48.6M
        const char *name = sk_OPENSSL_CSTRING_value(data->names, i);
269
270
48.6M
        if (OSSL_DECODER_is_a(decoder, name)) {
271
1.45M
            void *decoderctx = NULL;
272
1.45M
            OSSL_DECODER_INSTANCE *di = NULL;
273
274
1.45M
            if ((decoderctx = decoder->newctx(provctx)) == NULL) {
275
0
                data->error_occurred = 1;
276
0
                return;
277
0
            }
278
1.45M
            if ((di = ossl_decoder_instance_new(decoder, decoderctx)) == NULL) {
279
0
                decoder->freectx(decoderctx);
280
0
                data->error_occurred = 1;
281
0
                return;
282
0
            }
283
284
1.45M
            OSSL_TRACE_BEGIN(DECODER)
285
0
            {
286
0
                BIO_printf(trc_out,
287
0
                    "(ctx %p) Checking out decoder %p:\n"
288
0
                    "    %s with %s\n",
289
0
                    (void *)data->ctx, (void *)decoder,
290
0
                    OSSL_DECODER_get0_name(decoder),
291
0
                    OSSL_DECODER_get0_properties(decoder));
292
0
            }
293
1.45M
            OSSL_TRACE_END(DECODER);
294
295
1.45M
            if (!ossl_decoder_ctx_add_decoder_inst(data->ctx, di)) {
296
0
                ossl_decoder_instance_free(di);
297
0
                data->error_occurred = 1;
298
0
                return;
299
0
            }
300
1.45M
            data->total++;
301
302
            /* Success */
303
1.45M
            return;
304
1.45M
        }
305
48.6M
    }
306
307
    /* Decoder not suitable - but not a fatal error */
308
4.00M
    data->error_occurred = 0;
309
4.00M
}
310
311
int ossl_decoder_ctx_setup_for_pkey(OSSL_DECODER_CTX *ctx,
312
    EVP_PKEY **pkey, const char *keytype,
313
    OSSL_LIB_CTX *libctx,
314
    const char *propquery)
315
261k
{
316
261k
    struct decoder_pkey_data_st *process_data = NULL;
317
261k
    STACK_OF(OPENSSL_CSTRING) *names = NULL;
318
261k
    const char *input_type = ctx->start_input_type;
319
261k
    const char *input_structure = ctx->input_structure;
320
261k
    int ok = 0;
321
261k
    int isecoid = 0;
322
261k
    int i, end;
323
324
261k
    if (keytype != NULL
325
216k
        && (strcmp(keytype, "id-ecPublicKey") == 0
326
113k
            || strcmp(keytype, "1.2.840.10045.2.1") == 0))
327
103k
        isecoid = 1;
328
329
261k
    OSSL_TRACE_BEGIN(DECODER)
330
0
    {
331
0
        BIO_printf(trc_out,
332
0
            "(ctx %p) Looking for decoders producing %s%s%s%s%s%s\n",
333
0
            (void *)ctx,
334
0
            keytype != NULL ? keytype : "",
335
0
            keytype != NULL ? " keys" : "keys of any type",
336
0
            input_type != NULL ? " from " : "",
337
0
            input_type != NULL ? input_type : "",
338
0
            input_structure != NULL ? " with " : "",
339
0
            input_structure != NULL ? input_structure : "");
340
0
    }
341
261k
    OSSL_TRACE_END(DECODER);
342
343
261k
    if ((process_data = OPENSSL_zalloc(sizeof(*process_data))) == NULL
344
261k
        || (propquery != NULL
345
0
            && (process_data->propq = OPENSSL_strdup(propquery)) == NULL)
346
261k
        || (process_data->keymgmts = sk_EVP_KEYMGMT_new_null()) == NULL
347
261k
        || (names = sk_OPENSSL_CSTRING_new_null()) == NULL) {
348
0
        ERR_raise(ERR_LIB_OSSL_DECODER, ERR_R_MALLOC_FAILURE);
349
0
        goto err;
350
0
    }
351
352
261k
    process_data->object = (void **)pkey;
353
261k
    process_data->libctx = libctx;
354
261k
    process_data->selection = ctx->selection;
355
356
    /* First, find all keymgmts to form goals */
357
261k
    EVP_KEYMGMT_do_all_provided(libctx, collect_keymgmt,
358
261k
        process_data->keymgmts);
359
360
    /* Then, we collect all the keymgmt names */
361
261k
    end = sk_EVP_KEYMGMT_num(process_data->keymgmts);
362
4.97M
    for (i = 0; i < end; i++) {
363
4.71M
        EVP_KEYMGMT *keymgmt = sk_EVP_KEYMGMT_value(process_data->keymgmts, i);
364
365
        /*
366
         * If the key type is given by the caller, we only use the matching
367
         * KEYMGMTs, otherwise we use them all.
368
         * We have to special case SM2 here because of its abuse of the EC OID.
369
         * The EC OID can be used to identify an EC key or an SM2 key - so if
370
         * we have seen that OID we try both key types
371
         */
372
4.71M
        if (keytype == NULL
373
3.89M
            || EVP_KEYMGMT_is_a(keymgmt, keytype)
374
3.70M
            || (isecoid && EVP_KEYMGMT_is_a(keymgmt, "SM2"))) {
375
1.11M
            if (!EVP_KEYMGMT_names_do_all(keymgmt, collect_name, names)) {
376
0
                ERR_raise(ERR_LIB_OSSL_DECODER, ERR_R_INTERNAL_ERROR);
377
0
                goto err;
378
0
            }
379
1.11M
        }
380
4.71M
    }
381
382
261k
    OSSL_TRACE_BEGIN(DECODER)
383
0
    {
384
0
        end = sk_OPENSSL_CSTRING_num(names);
385
0
        BIO_printf(trc_out,
386
0
            "    Found %d keytypes (possibly with duplicates)",
387
0
            end);
388
0
        for (i = 0; i < end; i++)
389
0
            BIO_printf(trc_out, "%s%s",
390
0
                i == 0 ? ": " : ", ",
391
0
                sk_OPENSSL_CSTRING_value(names, i));
392
0
        BIO_printf(trc_out, "\n");
393
0
    }
394
261k
    OSSL_TRACE_END(DECODER);
395
396
    /*
397
     * Finally, find all decoders that have any keymgmt of the collected
398
     * keymgmt names
399
     */
400
261k
    {
401
261k
        struct collect_decoder_data_st collect_decoder_data = {
402
261k
            NULL,
403
261k
        };
404
405
261k
        collect_decoder_data.names = names;
406
261k
        collect_decoder_data.ctx = ctx;
407
261k
        OSSL_DECODER_do_all_provided(libctx,
408
261k
            collect_decoder, &collect_decoder_data);
409
261k
        sk_OPENSSL_CSTRING_free(names);
410
261k
        names = NULL;
411
412
261k
        if (collect_decoder_data.error_occurred)
413
0
            goto err;
414
415
261k
        OSSL_TRACE_BEGIN(DECODER)
416
0
        {
417
0
            BIO_printf(trc_out,
418
0
                "(ctx %p) Got %d decoders producing keys\n",
419
0
                (void *)ctx, collect_decoder_data.total);
420
0
        }
421
261k
        OSSL_TRACE_END(DECODER);
422
261k
    }
423
424
261k
    if (OSSL_DECODER_CTX_get_num_decoders(ctx) != 0) {
425
244k
        if (!OSSL_DECODER_CTX_set_construct(ctx, decoder_construct_pkey)
426
244k
            || !OSSL_DECODER_CTX_set_construct_data(ctx, process_data)
427
244k
            || !OSSL_DECODER_CTX_set_cleanup(ctx,
428
244k
                decoder_clean_pkey_construct_arg))
429
0
            goto err;
430
431
244k
        process_data = NULL; /* Avoid it being freed */
432
244k
    }
433
434
261k
    ok = 1;
435
261k
err:
436
261k
    decoder_clean_pkey_construct_arg(process_data);
437
261k
    sk_OPENSSL_CSTRING_free(names);
438
439
261k
    return ok;
440
261k
}
441
442
OSSL_DECODER_CTX *
443
OSSL_DECODER_CTX_new_for_pkey(EVP_PKEY **pkey,
444
    const char *input_type,
445
    const char *input_structure,
446
    const char *keytype, int selection,
447
    OSSL_LIB_CTX *libctx, const char *propquery)
448
261k
{
449
261k
    OSSL_DECODER_CTX *ctx = NULL;
450
451
261k
    if ((ctx = OSSL_DECODER_CTX_new()) == NULL) {
452
0
        ERR_raise(ERR_LIB_OSSL_DECODER, ERR_R_MALLOC_FAILURE);
453
0
        return NULL;
454
0
    }
455
456
261k
    OSSL_TRACE_BEGIN(DECODER)
457
0
    {
458
0
        BIO_printf(trc_out,
459
0
            "(ctx %p) Looking for %s decoders with selection %d\n",
460
0
            (void *)ctx, keytype, selection);
461
0
        BIO_printf(trc_out, "    input type: %s, input structure: %s\n",
462
0
            input_type, input_structure);
463
0
    }
464
261k
    OSSL_TRACE_END(DECODER);
465
466
261k
    if (OSSL_DECODER_CTX_set_input_type(ctx, input_type)
467
261k
        && OSSL_DECODER_CTX_set_input_structure(ctx, input_structure)
468
261k
        && OSSL_DECODER_CTX_set_selection(ctx, selection)
469
261k
        && ossl_decoder_ctx_setup_for_pkey(ctx, pkey, keytype,
470
261k
            libctx, propquery)
471
261k
        && OSSL_DECODER_CTX_add_extra(ctx, libctx, propquery)) {
472
261k
        OSSL_TRACE_BEGIN(DECODER)
473
0
        {
474
0
            BIO_printf(trc_out, "(ctx %p) Got %d decoders\n",
475
0
                (void *)ctx, OSSL_DECODER_CTX_get_num_decoders(ctx));
476
0
        }
477
261k
        OSSL_TRACE_END(DECODER);
478
261k
        return ctx;
479
261k
    }
480
481
0
    OSSL_DECODER_CTX_free(ctx);
482
    return NULL;
483
261k
}