Coverage Report

Created: 2023-06-08 06:43

/src/openssl30/crypto/encode_decode/decoder_pkey.c
Line
Count
Source (jump to first uncovered line)
1
/*
2
 * Copyright 2020-2022 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
20.5k
{
39
20.5k
    return ossl_pw_set_pem_password_cb(&ctx->pwdata, cb, cbarg);
40
20.5k
}
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
597k
{
70
597k
    struct decoder_pkey_data_st *data = construct_data;
71
597k
    OSSL_DECODER *decoder = OSSL_DECODER_INSTANCE_get_decoder(decoder_inst);
72
597k
    void *decoderctx = OSSL_DECODER_INSTANCE_get_decoder_ctx(decoder_inst);
73
597k
    const OSSL_PROVIDER *decoder_prov = OSSL_DECODER_get0_provider(decoder);
74
597k
    EVP_KEYMGMT *keymgmt = NULL;
75
597k
    const OSSL_PROVIDER *keymgmt_prov = NULL;
76
597k
    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
597k
    void *object_ref = NULL;
86
597k
    size_t object_ref_sz = 0;
87
597k
    const OSSL_PARAM *p;
88
89
597k
    p = OSSL_PARAM_locate_const(params, OSSL_OBJECT_PARAM_DATA_TYPE);
90
597k
    if (p != NULL) {
91
597k
        char *object_type = NULL;
92
93
597k
        if (!OSSL_PARAM_get_utf8_string(p, &object_type, 0))
94
0
            return 0;
95
597k
        OPENSSL_free(data->object_type);
96
597k
        data->object_type = object_type;
97
597k
    }
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
597k
    p = OSSL_PARAM_locate_const(params, OSSL_OBJECT_PARAM_REFERENCE);
105
597k
    if (p == NULL || p->data_type != OSSL_PARAM_OCTET_STRING)
106
399k
        return 0;
107
197k
    object_ref = p->data;
108
197k
    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
197k
    end = sk_EVP_KEYMGMT_num(data->keymgmts);
115
519k
    for (i = 0; i < end; i++) {
116
519k
        keymgmt = sk_EVP_KEYMGMT_value(data->keymgmts, i);
117
519k
        keymgmt_prov = EVP_KEYMGMT_get0_provider(keymgmt);
118
119
519k
        if (keymgmt_prov == decoder_prov
120
519k
            && evp_keymgmt_has_load(keymgmt)
121
519k
            && EVP_KEYMGMT_is_a(keymgmt, data->object_type))
122
197k
            break;
123
519k
    }
124
197k
    if (i < end) {
125
        /* To allow it to be freed further down */
126
197k
        if (!EVP_KEYMGMT_up_ref(keymgmt))
127
0
            return 0;
128
197k
    } else if ((keymgmt = EVP_KEYMGMT_fetch(data->libctx,
129
0
                                            data->object_type,
130
0
                                            data->propq)) != NULL) {
131
0
        keymgmt_prov = EVP_KEYMGMT_get0_provider(keymgmt);
132
0
    }
133
134
197k
    if (keymgmt != NULL) {
135
197k
        EVP_PKEY *pkey = NULL;
136
197k
        void *keydata = NULL;
137
138
        /*
139
         * If the EVP_KEYMGMT and the OSSL_DECODER are from the
140
         * same provider, we assume that the KEYMGMT has a key loading
141
         * function that can handle the provider reference we hold.
142
         *
143
         * Otherwise, we export from the decoder and import the
144
         * result in the keymgmt.
145
         */
146
197k
        if (keymgmt_prov == decoder_prov) {
147
197k
            keydata = evp_keymgmt_load(keymgmt, object_ref, object_ref_sz);
148
197k
        } else {
149
0
            struct evp_keymgmt_util_try_import_data_st import_data;
150
151
0
            import_data.keymgmt = keymgmt;
152
0
            import_data.keydata = NULL;
153
0
            import_data.selection = data->selection;
154
155
            /*
156
             * No need to check for errors here, the value of
157
             * |import_data.keydata| is as much an indicator.
158
             */
159
0
            (void)decoder->export_object(decoderctx,
160
0
                                         object_ref, object_ref_sz,
161
0
                                         &evp_keymgmt_util_try_import,
162
0
                                         &import_data);
163
0
            keydata = import_data.keydata;
164
0
            import_data.keydata = NULL;
165
0
        }
166
167
197k
        if (keydata != NULL
168
197k
            && (pkey = evp_keymgmt_util_make_pkey(keymgmt, keydata)) == NULL)
169
0
            evp_keymgmt_freedata(keymgmt, keydata);
170
171
197k
        *data->object = pkey;
172
173
        /*
174
         * evp_keymgmt_util_make_pkey() increments the reference count when
175
         * assigning the EVP_PKEY, so we can free the keymgmt here.
176
         */
177
197k
        EVP_KEYMGMT_free(keymgmt);
178
197k
    }
179
    /*
180
     * We successfully looked through, |*ctx->object| determines if we
181
     * actually found something.
182
     */
183
197k
    return (*data->object != NULL);
184
197k
}
185
186
static void decoder_clean_pkey_construct_arg(void *construct_data)
187
923k
{
188
923k
    struct decoder_pkey_data_st *data = construct_data;
189
190
923k
    if (data != NULL) {
191
476k
        sk_EVP_KEYMGMT_pop_free(data->keymgmts, EVP_KEYMGMT_free);
192
476k
        OPENSSL_free(data->propq);
193
476k
        OPENSSL_free(data->object_type);
194
476k
        OPENSSL_free(data);
195
476k
    }
196
923k
}
197
198
static void collect_name(const char *name, void *arg)
199
3.82M
{
200
3.82M
    STACK_OF(OPENSSL_CSTRING) *names = arg;
201
202
3.82M
    sk_OPENSSL_CSTRING_push(names, name);
203
3.82M
}
204
205
static void collect_keymgmt(EVP_KEYMGMT *keymgmt, void *arg)
206
4.88M
{
207
4.88M
    STACK_OF(EVP_KEYMGMT) *keymgmts = arg;
208
209
4.88M
    if (!EVP_KEYMGMT_up_ref(keymgmt) /* ref++ */)
210
0
        return;
211
4.88M
    if (sk_EVP_KEYMGMT_push(keymgmts, keymgmt) <= 0) {
212
0
        EVP_KEYMGMT_free(keymgmt);   /* ref-- */
213
0
        return;
214
0
    }
215
4.88M
}
216
217
struct collect_decoder_data_st {
218
    STACK_OF(OPENSSL_CSTRING) *names;
219
    OSSL_DECODER_CTX *ctx;
220
221
    int total;
222
    unsigned int error_occurred:1;
223
};
224
225
static void collect_decoder(OSSL_DECODER *decoder, void *arg)
226
10.5M
{
227
10.5M
    struct collect_decoder_data_st *data = arg;
228
10.5M
    size_t i, end_i;
229
10.5M
    const OSSL_PROVIDER *prov = OSSL_DECODER_get0_provider(decoder);
230
10.5M
    void *provctx = OSSL_PROVIDER_get0_provider_ctx(prov);
231
232
10.5M
    if (data->error_occurred)
233
0
        return;
234
235
10.5M
    if (data->names == NULL) {
236
0
        data->error_occurred = 1;
237
0
        return;
238
0
    }
239
240
    /*
241
     * Either the caller didn't give a selection, or if they did,
242
     * the decoder must tell us if it supports that selection to
243
     * be accepted.  If the decoder doesn't have |does_selection|,
244
     * it's seen as taking anything.
245
     */
246
10.5M
    if (decoder->does_selection != NULL
247
10.5M
            && !decoder->does_selection(provctx, data->ctx->selection))
248
4.52M
        return;
249
250
6.06M
    OSSL_TRACE_BEGIN(DECODER) {
251
0
        BIO_printf(trc_out,
252
0
                   "(ctx %p) Checking out decoder %p:\n"
253
0
                   "    %s with %s\n",
254
0
                   (void *)data->ctx, (void *)decoder,
255
0
                   OSSL_DECODER_get0_name(decoder),
256
0
                   OSSL_DECODER_get0_properties(decoder));
257
6.06M
    } OSSL_TRACE_END(DECODER);
258
259
6.06M
    end_i = sk_OPENSSL_CSTRING_num(data->names);
260
56.4M
    for (i = 0; i < end_i; i++) {
261
51.8M
        const char *name = sk_OPENSSL_CSTRING_value(data->names, i);
262
263
51.8M
        if (OSSL_DECODER_is_a(decoder, name)) {
264
1.50M
            void *decoderctx = NULL;
265
1.50M
            OSSL_DECODER_INSTANCE *di = NULL;
266
267
1.50M
            if ((decoderctx = decoder->newctx(provctx)) == NULL) {
268
0
                data->error_occurred = 1;
269
0
                return;
270
0
            }
271
1.50M
            if ((di = ossl_decoder_instance_new(decoder, decoderctx)) == NULL) {
272
0
                decoder->freectx(decoderctx);
273
0
                data->error_occurred = 1;
274
0
                return;
275
0
            }
276
277
1.50M
            OSSL_TRACE_BEGIN(DECODER) {
278
0
                BIO_printf(trc_out,
279
0
                           "(ctx %p) Checking out decoder %p:\n"
280
0
                           "    %s with %s\n",
281
0
                           (void *)data->ctx, (void *)decoder,
282
0
                           OSSL_DECODER_get0_name(decoder),
283
0
                           OSSL_DECODER_get0_properties(decoder));
284
1.50M
            } OSSL_TRACE_END(DECODER);
285
286
1.50M
            if (!ossl_decoder_ctx_add_decoder_inst(data->ctx, di)) {
287
0
                ossl_decoder_instance_free(di);
288
0
                data->error_occurred = 1;
289
0
                return;
290
0
            }
291
1.50M
            data->total++;
292
293
            /* Success */
294
1.50M
            return;
295
1.50M
        }
296
51.8M
    }
297
298
    /* Decoder not suitable - but not a fatal error */
299
4.56M
    data->error_occurred = 0;
300
4.56M
}
301
302
int ossl_decoder_ctx_setup_for_pkey(OSSL_DECODER_CTX *ctx,
303
                                    EVP_PKEY **pkey, const char *keytype,
304
                                    OSSL_LIB_CTX *libctx,
305
                                    const char *propquery)
306
271k
{
307
271k
    struct decoder_pkey_data_st *process_data = NULL;
308
271k
    STACK_OF(OPENSSL_CSTRING) *names = NULL;
309
271k
    const char *input_type = ctx->start_input_type;
310
271k
    const char *input_structure = ctx->input_structure;
311
271k
    int ok = 0;
312
271k
    int isecoid = 0;
313
271k
    int i, end;
314
315
271k
    if (keytype != NULL
316
271k
            && (strcmp(keytype, "id-ecPublicKey") == 0
317
227k
                || strcmp(keytype, "1.2.840.10045.2.1") == 0))
318
94.7k
        isecoid = 1;
319
320
271k
    OSSL_TRACE_BEGIN(DECODER) {
321
0
        BIO_printf(trc_out,
322
0
                   "(ctx %p) Looking for decoders producing %s%s%s%s%s%s\n",
323
0
                   (void *)ctx,
324
0
                   keytype != NULL ? keytype : "",
325
0
                   keytype != NULL ? " keys" : "keys of any type",
326
0
                   input_type != NULL ? " from " : "",
327
0
                   input_type != NULL ? input_type : "",
328
0
                   input_structure != NULL ? " with " : "",
329
0
                   input_structure != NULL ? input_structure : "");
330
271k
    } OSSL_TRACE_END(DECODER);
331
332
271k
    if ((process_data = OPENSSL_zalloc(sizeof(*process_data))) == NULL
333
271k
        || (propquery != NULL
334
271k
            && (process_data->propq = OPENSSL_strdup(propquery)) == NULL)
335
271k
        || (process_data->keymgmts = sk_EVP_KEYMGMT_new_null()) == NULL
336
271k
        || (names = sk_OPENSSL_CSTRING_new_null()) == NULL) {
337
0
        ERR_raise(ERR_LIB_OSSL_DECODER, ERR_R_MALLOC_FAILURE);
338
0
        goto err;
339
0
    }
340
341
271k
    process_data->object = (void **)pkey;
342
271k
    process_data->libctx = libctx;
343
271k
    process_data->selection = ctx->selection;
344
345
    /* First, find all keymgmts to form goals */
346
271k
    EVP_KEYMGMT_do_all_provided(libctx, collect_keymgmt,
347
271k
                                process_data->keymgmts);
348
349
    /* Then, we collect all the keymgmt names */
350
271k
    end = sk_EVP_KEYMGMT_num(process_data->keymgmts);
351
5.16M
    for (i = 0; i < end; i++) {
352
4.88M
        EVP_KEYMGMT *keymgmt = sk_EVP_KEYMGMT_value(process_data->keymgmts, i);
353
354
        /*
355
         * If the key type is given by the caller, we only use the matching
356
         * KEYMGMTs, otherwise we use them all.
357
         * We have to special case SM2 here because of its abuse of the EC OID.
358
         * The EC OID can be used to identify an EC key or an SM2 key - so if
359
         * we have seen that OID we try both key types
360
         */
361
4.88M
        if (keytype == NULL
362
4.88M
                || EVP_KEYMGMT_is_a(keymgmt, keytype)
363
4.88M
                || (isecoid && EVP_KEYMGMT_is_a(keymgmt, "SM2"))) {
364
1.10M
            if (!EVP_KEYMGMT_names_do_all(keymgmt, collect_name, names)) {
365
0
                ERR_raise(ERR_LIB_OSSL_DECODER, ERR_R_INTERNAL_ERROR);
366
0
                goto err;
367
0
            }
368
1.10M
        }
369
4.88M
    }
370
371
271k
    OSSL_TRACE_BEGIN(DECODER) {
372
0
        end = sk_OPENSSL_CSTRING_num(names);
373
0
        BIO_printf(trc_out,
374
0
                   "    Found %d keytypes (possibly with duplicates)",
375
0
                   end);
376
0
        for (i = 0; i < end; i++)
377
0
            BIO_printf(trc_out, "%s%s",
378
0
                       i == 0 ? ": " : ", ",
379
0
                       sk_OPENSSL_CSTRING_value(names, i));
380
0
        BIO_printf(trc_out, "\n");
381
271k
    } OSSL_TRACE_END(DECODER);
382
383
    /*
384
     * Finally, find all decoders that have any keymgmt of the collected
385
     * keymgmt names
386
     */
387
271k
    {
388
271k
        struct collect_decoder_data_st collect_decoder_data = { NULL, };
389
390
271k
        collect_decoder_data.names = names;
391
271k
        collect_decoder_data.ctx = ctx;
392
271k
        OSSL_DECODER_do_all_provided(libctx,
393
271k
                                     collect_decoder, &collect_decoder_data);
394
271k
        sk_OPENSSL_CSTRING_free(names);
395
271k
        names = NULL;
396
397
271k
        if (collect_decoder_data.error_occurred)
398
0
            goto err;
399
400
271k
        OSSL_TRACE_BEGIN(DECODER) {
401
0
            BIO_printf(trc_out,
402
0
                       "(ctx %p) Got %d decoders producing keys\n",
403
0
                       (void *)ctx, collect_decoder_data.total);
404
271k
        } OSSL_TRACE_END(DECODER);
405
271k
    }
406
407
271k
    if (OSSL_DECODER_CTX_get_num_decoders(ctx) != 0) {
408
257k
        if (!OSSL_DECODER_CTX_set_construct(ctx, decoder_construct_pkey)
409
257k
            || !OSSL_DECODER_CTX_set_construct_data(ctx, process_data)
410
257k
            || !OSSL_DECODER_CTX_set_cleanup(ctx,
411
257k
                                             decoder_clean_pkey_construct_arg))
412
0
            goto err;
413
414
257k
        process_data = NULL; /* Avoid it being freed */
415
257k
    }
416
417
271k
    ok = 1;
418
271k
 err:
419
271k
    decoder_clean_pkey_construct_arg(process_data);
420
271k
    sk_OPENSSL_CSTRING_free(names);
421
422
271k
    return ok;
423
271k
}
424
425
OSSL_DECODER_CTX *
426
OSSL_DECODER_CTX_new_for_pkey(EVP_PKEY **pkey,
427
                              const char *input_type,
428
                              const char *input_structure,
429
                              const char *keytype, int selection,
430
                              OSSL_LIB_CTX *libctx, const char *propquery)
431
476k
{
432
476k
    OSSL_DECODER_CTX *ctx = NULL;
433
434
476k
    if ((ctx = OSSL_DECODER_CTX_new()) == NULL) {
435
0
        ERR_raise(ERR_LIB_OSSL_DECODER, ERR_R_MALLOC_FAILURE);
436
0
        return NULL;
437
0
    }
438
439
476k
    OSSL_TRACE_BEGIN(DECODER) {
440
0
        BIO_printf(trc_out,
441
0
                   "(ctx %p) Looking for %s decoders with selection %d\n",
442
0
                   (void *)ctx, keytype, selection);
443
0
        BIO_printf(trc_out, "    input type: %s, input structure: %s\n",
444
0
                   input_type, input_structure);
445
476k
    } OSSL_TRACE_END(DECODER);
446
447
476k
    if (OSSL_DECODER_CTX_set_input_type(ctx, input_type)
448
476k
        && OSSL_DECODER_CTX_set_input_structure(ctx, input_structure)
449
476k
        && OSSL_DECODER_CTX_set_selection(ctx, selection)
450
476k
        && ossl_decoder_ctx_setup_for_pkey(ctx, pkey, keytype,
451
476k
                                           libctx, propquery)
452
476k
        && OSSL_DECODER_CTX_add_extra(ctx, libctx, propquery)) {
453
476k
        OSSL_TRACE_BEGIN(DECODER) {
454
0
            BIO_printf(trc_out, "(ctx %p) Got %d decoders\n",
455
0
                       (void *)ctx, OSSL_DECODER_CTX_get_num_decoders(ctx));
456
476k
        } OSSL_TRACE_END(DECODER);
457
476k
        return ctx;
458
476k
    }
459
460
0
    OSSL_DECODER_CTX_free(ctx);
461
0
    return NULL;
462
476k
}