Coverage Report

Created: 2024-11-21 07:03

/src/openssl/providers/implementations/encode_decode/decode_der2key.c
Line
Count
Source (jump to first uncovered line)
1
/*
2
 * Copyright 2020-2024 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
/*
11
 * low level APIs are deprecated for public use, but still ok for
12
 * internal use.
13
 */
14
#include "internal/deprecated.h"
15
16
#include <openssl/core_dispatch.h>
17
#include <openssl/core_names.h>
18
#include <openssl/core_object.h>
19
#include <openssl/crypto.h>
20
#include <openssl/err.h>
21
#include <openssl/params.h>
22
#include <openssl/pem.h>         /* PEM_BUFSIZE and public PEM functions */
23
#include <openssl/pkcs12.h>
24
#include <openssl/x509.h>
25
#include <openssl/proverr.h>
26
#include "internal/cryptlib.h"   /* ossl_assert() */
27
#include "internal/asn1.h"
28
#include "crypto/dh.h"
29
#include "crypto/dsa.h"
30
#include "crypto/ec.h"
31
#include "crypto/evp.h"
32
#include "crypto/ecx.h"
33
#include "crypto/rsa.h"
34
#include "crypto/x509.h"
35
#include "openssl/obj_mac.h"
36
#include "prov/bio.h"
37
#include "prov/implementations.h"
38
#include "endecoder_local.h"
39
#include "internal/nelem.h"
40
41
struct der2key_ctx_st;           /* Forward declaration */
42
typedef int check_key_fn(void *, struct der2key_ctx_st *ctx);
43
typedef void adjust_key_fn(void *, struct der2key_ctx_st *ctx);
44
typedef void free_key_fn(void *);
45
typedef void *d2i_PKCS8_fn(void **, const unsigned char **, long,
46
                           struct der2key_ctx_st *);
47
struct keytype_desc_st {
48
    const char *keytype_name;
49
    const OSSL_DISPATCH *fns; /* Keymgmt (to pilfer functions from) */
50
51
    /* The input structure name */
52
    const char *structure_name;
53
54
    /*
55
     * The EVP_PKEY_xxx type macro.  Should be zero for type specific
56
     * structures, non-zero when the outermost structure is PKCS#8 or
57
     * SubjectPublicKeyInfo.  This determines which of the function
58
     * pointers below will be used.
59
     */
60
    int evp_type;
61
62
    /* The selection mask for OSSL_FUNC_decoder_does_selection() */
63
    int selection_mask;
64
65
    /* For type specific decoders, we use the corresponding d2i */
66
    d2i_of_void *d2i_private_key; /* From type-specific DER */
67
    d2i_of_void *d2i_public_key;  /* From type-specific DER */
68
    d2i_of_void *d2i_key_params;  /* From type-specific DER */
69
    d2i_PKCS8_fn *d2i_PKCS8;      /* Wrapped in a PrivateKeyInfo */
70
    d2i_of_void *d2i_PUBKEY;      /* Wrapped in a SubjectPublicKeyInfo */
71
72
    /*
73
     * For any key, we may need to check that the key meets expectations.
74
     * This is useful when the same functions can decode several variants
75
     * of a key.
76
     */
77
    check_key_fn *check_key;
78
79
    /*
80
     * For any key, we may need to make provider specific adjustments, such
81
     * as ensure the key carries the correct library context.
82
     */
83
    adjust_key_fn *adjust_key;
84
    /* {type}_free() */
85
    free_key_fn *free_key;
86
};
87
88
/*
89
 * Context used for DER to key decoding.
90
 */
91
struct der2key_ctx_st {
92
    PROV_CTX *provctx;
93
    char propq[OSSL_MAX_PROPQUERY_SIZE];
94
    const struct keytype_desc_st *desc;
95
    /* The selection that is passed to der2key_decode() */
96
    int selection;
97
    /* Flag used to signal that a failure is fatal */
98
    unsigned int flag_fatal : 1;
99
};
100
101
typedef void *key_from_pkcs8_t(const PKCS8_PRIV_KEY_INFO *p8inf,
102
                               OSSL_LIB_CTX *libctx, const char *propq);
103
static void *der2key_decode_p8(const unsigned char **input_der,
104
                               long input_der_len, struct der2key_ctx_st *ctx,
105
                               key_from_pkcs8_t *key_from_pkcs8)
106
0
{
107
0
    PKCS8_PRIV_KEY_INFO *p8inf = NULL;
108
0
    const X509_ALGOR *alg = NULL;
109
0
    void *key = NULL;
110
111
0
    if ((p8inf = d2i_PKCS8_PRIV_KEY_INFO(NULL, input_der, input_der_len)) != NULL
112
0
        && PKCS8_pkey_get0(NULL, NULL, NULL, &alg, p8inf)
113
0
        && (OBJ_obj2nid(alg->algorithm) == ctx->desc->evp_type
114
            /* Allow decoding sm2 private key with id_ecPublicKey */
115
0
            || (OBJ_obj2nid(alg->algorithm) == NID_X9_62_id_ecPublicKey
116
0
                && ctx->desc->evp_type == NID_sm2)))
117
0
        key = key_from_pkcs8(p8inf, PROV_LIBCTX_OF(ctx->provctx), ctx->propq);
118
0
    PKCS8_PRIV_KEY_INFO_free(p8inf);
119
120
0
    return key;
121
0
}
122
123
/* ---------------------------------------------------------------------- */
124
125
static OSSL_FUNC_decoder_freectx_fn der2key_freectx;
126
static OSSL_FUNC_decoder_decode_fn der2key_decode;
127
static OSSL_FUNC_decoder_export_object_fn der2key_export_object;
128
static OSSL_FUNC_decoder_settable_ctx_params_fn der2key_settable_ctx_params;
129
static OSSL_FUNC_decoder_set_ctx_params_fn der2key_set_ctx_params;
130
131
static struct der2key_ctx_st *
132
der2key_newctx(void *provctx, const struct keytype_desc_st *desc)
133
0
{
134
0
    struct der2key_ctx_st *ctx = OPENSSL_zalloc(sizeof(*ctx));
135
136
0
    if (ctx != NULL) {
137
0
        ctx->provctx = provctx;
138
0
        ctx->desc = desc;
139
0
    }
140
0
    return ctx;
141
0
}
142
143
static const OSSL_PARAM *der2key_settable_ctx_params(ossl_unused void *provctx)
144
0
{
145
0
    static const OSSL_PARAM settables[] = {
146
0
        OSSL_PARAM_utf8_string(OSSL_DECODER_PARAM_PROPERTIES, NULL, 0),
147
0
        OSSL_PARAM_END
148
0
    };
149
0
    return settables;
150
0
}
151
152
static int der2key_set_ctx_params(void *vctx, const OSSL_PARAM params[])
153
0
{
154
0
    struct der2key_ctx_st *ctx = vctx;
155
0
    const OSSL_PARAM *p;
156
0
    char *str = ctx->propq;
157
158
0
    p = OSSL_PARAM_locate_const(params, OSSL_DECODER_PARAM_PROPERTIES);
159
0
    if (p != NULL && !OSSL_PARAM_get_utf8_string(p, &str, sizeof(ctx->propq)))
160
0
        return 0;
161
162
0
    return 1;
163
0
}
164
165
static void der2key_freectx(void *vctx)
166
0
{
167
0
    struct der2key_ctx_st *ctx = vctx;
168
169
0
    OPENSSL_free(ctx);
170
0
}
171
172
static int der2key_check_selection(int selection,
173
                                   const struct keytype_desc_st *desc)
174
0
{
175
    /*
176
     * The selections are kinda sorta "levels", i.e. each selection given
177
     * here is assumed to include those following.
178
     */
179
0
    int checks[] = {
180
0
        OSSL_KEYMGMT_SELECT_PRIVATE_KEY,
181
0
        OSSL_KEYMGMT_SELECT_PUBLIC_KEY,
182
0
        OSSL_KEYMGMT_SELECT_ALL_PARAMETERS
183
0
    };
184
0
    size_t i;
185
186
    /* The decoder implementations made here support guessing */
187
0
    if (selection == 0)
188
0
        return 1;
189
190
0
    for (i = 0; i < OSSL_NELEM(checks); i++) {
191
0
        int check1 = (selection & checks[i]) != 0;
192
0
        int check2 = (desc->selection_mask & checks[i]) != 0;
193
194
        /*
195
         * If the caller asked for the currently checked bit(s), return
196
         * whether the decoder description says it's supported.
197
         */
198
0
        if (check1)
199
0
            return check2;
200
0
    }
201
202
    /* This should be dead code, but just to be safe... */
203
0
    return 0;
204
0
}
205
206
static int der2key_decode(void *vctx, OSSL_CORE_BIO *cin, int selection,
207
                          OSSL_CALLBACK *data_cb, void *data_cbarg,
208
                          OSSL_PASSPHRASE_CALLBACK *pw_cb, void *pw_cbarg)
209
0
{
210
0
    struct der2key_ctx_st *ctx = vctx;
211
0
    unsigned char *der = NULL;
212
0
    const unsigned char *derp;
213
0
    long der_len = 0;
214
0
    void *key = NULL;
215
0
    int ok = 0;
216
217
0
    ctx->selection = selection;
218
    /*
219
     * The caller is allowed to specify 0 as a selection mark, to have the
220
     * structure and key type guessed.  For type-specific structures, this
221
     * is not recommended, as some structures are very similar.
222
     * Note that 0 isn't the same as OSSL_KEYMGMT_SELECT_ALL, as the latter
223
     * signifies a private key structure, where everything else is assumed
224
     * to be present as well.
225
     */
226
0
    if (selection == 0)
227
0
        selection = ctx->desc->selection_mask;
228
0
    if ((selection & ctx->desc->selection_mask) == 0) {
229
0
        ERR_raise(ERR_LIB_PROV, ERR_R_PASSED_INVALID_ARGUMENT);
230
0
        return 0;
231
0
    }
232
233
0
    ok = ossl_read_der(ctx->provctx, cin, &der, &der_len);
234
0
    if (!ok)
235
0
        goto next;
236
237
0
    ok = 0; /* Assume that we fail */
238
239
0
    ERR_set_mark();
240
0
    if ((selection & OSSL_KEYMGMT_SELECT_PRIVATE_KEY) != 0) {
241
0
        derp = der;
242
0
        if (ctx->desc->d2i_PKCS8 != NULL) {
243
0
            key = ctx->desc->d2i_PKCS8(NULL, &derp, der_len, ctx);
244
0
            if (ctx->flag_fatal) {
245
0
                ERR_clear_last_mark();
246
0
                goto end;
247
0
            }
248
0
        } else if (ctx->desc->d2i_private_key != NULL) {
249
0
            key = ctx->desc->d2i_private_key(NULL, &derp, der_len);
250
0
        }
251
0
        if (key == NULL && ctx->selection != 0) {
252
0
            ERR_clear_last_mark();
253
0
            goto next;
254
0
        }
255
0
    }
256
0
    if (key == NULL && (selection & OSSL_KEYMGMT_SELECT_PUBLIC_KEY) != 0) {
257
0
        derp = der;
258
0
        if (ctx->desc->d2i_PUBKEY != NULL)
259
0
            key = ctx->desc->d2i_PUBKEY(NULL, &derp, der_len);
260
0
        else if (ctx->desc->d2i_public_key != NULL)
261
0
            key = ctx->desc->d2i_public_key(NULL, &derp, der_len);
262
0
        if (key == NULL && ctx->selection != 0) {
263
0
            ERR_clear_last_mark();
264
0
            goto next;
265
0
        }
266
0
    }
267
0
    if (key == NULL && (selection & OSSL_KEYMGMT_SELECT_ALL_PARAMETERS) != 0) {
268
0
        derp = der;
269
0
        if (ctx->desc->d2i_key_params != NULL)
270
0
            key = ctx->desc->d2i_key_params(NULL, &derp, der_len);
271
0
        if (key == NULL && ctx->selection != 0) {
272
0
            ERR_clear_last_mark();
273
0
            goto next;
274
0
        }
275
0
    }
276
0
    if (key == NULL)
277
0
        ERR_clear_last_mark();
278
0
    else
279
0
        ERR_pop_to_mark();
280
281
    /*
282
     * Last minute check to see if this was the correct type of key.  This
283
     * should never lead to a fatal error, i.e. the decoding itself was
284
     * correct, it was just an unexpected key type.  This is generally for
285
     * classes of key types that have subtle variants, like RSA-PSS keys as
286
     * opposed to plain RSA keys.
287
     */
288
0
    if (key != NULL
289
0
        && ctx->desc->check_key != NULL
290
0
        && !ctx->desc->check_key(key, ctx)) {
291
0
        ctx->desc->free_key(key);
292
0
        key = NULL;
293
0
    }
294
295
0
    if (key != NULL && ctx->desc->adjust_key != NULL)
296
0
        ctx->desc->adjust_key(key, ctx);
297
298
0
 next:
299
    /*
300
     * Indicated that we successfully decoded something, or not at all.
301
     * Ending up "empty handed" is not an error.
302
     */
303
0
    ok = 1;
304
305
    /*
306
     * We free memory here so it's not held up during the callback, because
307
     * we know the process is recursive and the allocated chunks of memory
308
     * add up.
309
     */
310
0
    OPENSSL_free(der);
311
0
    der = NULL;
312
313
0
    if (key != NULL) {
314
0
        OSSL_PARAM params[4];
315
0
        int object_type = OSSL_OBJECT_PKEY;
316
317
0
        params[0] =
318
0
            OSSL_PARAM_construct_int(OSSL_OBJECT_PARAM_TYPE, &object_type);
319
320
0
#ifndef OPENSSL_NO_SM2
321
0
        if (strcmp(ctx->desc->keytype_name, "EC") == 0
322
0
            && (EC_KEY_get_flags(key) & EC_FLAG_SM2_RANGE) != 0)
323
0
            params[1] =
324
0
                OSSL_PARAM_construct_utf8_string(OSSL_OBJECT_PARAM_DATA_TYPE,
325
0
                                                 "SM2", 0);
326
0
        else
327
0
#endif
328
0
            params[1] =
329
0
                OSSL_PARAM_construct_utf8_string(OSSL_OBJECT_PARAM_DATA_TYPE,
330
0
                                                 (char *)ctx->desc->keytype_name,
331
0
                                                 0);
332
        /* The address of the key becomes the octet string */
333
0
        params[2] =
334
0
            OSSL_PARAM_construct_octet_string(OSSL_OBJECT_PARAM_REFERENCE,
335
0
                                              &key, sizeof(key));
336
0
        params[3] = OSSL_PARAM_construct_end();
337
338
0
        ok = data_cb(params, data_cbarg);
339
0
    }
340
341
0
 end:
342
0
    ctx->desc->free_key(key);
343
0
    OPENSSL_free(der);
344
345
0
    return ok;
346
0
}
347
348
static int der2key_export_object(void *vctx,
349
                                 const void *reference, size_t reference_sz,
350
                                 OSSL_CALLBACK *export_cb, void *export_cbarg)
351
0
{
352
0
    struct der2key_ctx_st *ctx = vctx;
353
0
    OSSL_FUNC_keymgmt_export_fn *export =
354
0
        ossl_prov_get_keymgmt_export(ctx->desc->fns);
355
0
    void *keydata;
356
357
0
    if (reference_sz == sizeof(keydata) && export != NULL) {
358
0
        int selection = ctx->selection;
359
360
0
        if (selection == 0)
361
0
            selection = OSSL_KEYMGMT_SELECT_ALL;
362
        /* The contents of the reference is the address to our object */
363
0
        keydata = *(void **)reference;
364
365
0
        return export(keydata, selection, export_cb, export_cbarg);
366
0
    }
367
0
    return 0;
368
0
}
369
370
/* ---------------------------------------------------------------------- */
371
372
#ifndef OPENSSL_NO_DH
373
# define dh_evp_type                    EVP_PKEY_DH
374
# define dh_d2i_private_key             NULL
375
# define dh_d2i_public_key              NULL
376
# define dh_d2i_key_params              (d2i_of_void *)d2i_DHparams
377
378
static void *dh_d2i_PKCS8(void **key, const unsigned char **der, long der_len,
379
                          struct der2key_ctx_st *ctx)
380
0
{
381
0
    return der2key_decode_p8(der, der_len, ctx,
382
0
                             (key_from_pkcs8_t *)ossl_dh_key_from_pkcs8);
383
0
}
384
385
# define dh_d2i_PUBKEY                  (d2i_of_void *)ossl_d2i_DH_PUBKEY
386
# define dh_free                        (free_key_fn *)DH_free
387
# define dh_check                       NULL
388
389
static void dh_adjust(void *key, struct der2key_ctx_st *ctx)
390
0
{
391
0
    ossl_dh_set0_libctx(key, PROV_LIBCTX_OF(ctx->provctx));
392
0
}
393
394
# define dhx_evp_type                   EVP_PKEY_DHX
395
# define dhx_d2i_private_key            NULL
396
# define dhx_d2i_public_key             NULL
397
# define dhx_d2i_key_params             (d2i_of_void *)d2i_DHxparams
398
# define dhx_d2i_PKCS8                  dh_d2i_PKCS8
399
# define dhx_d2i_PUBKEY                 (d2i_of_void *)ossl_d2i_DHx_PUBKEY
400
# define dhx_free                       (free_key_fn *)DH_free
401
# define dhx_check                      NULL
402
# define dhx_adjust                     dh_adjust
403
#endif
404
405
/* ---------------------------------------------------------------------- */
406
407
#ifndef OPENSSL_NO_DSA
408
# define dsa_evp_type                   EVP_PKEY_DSA
409
# define dsa_d2i_private_key            (d2i_of_void *)d2i_DSAPrivateKey
410
# define dsa_d2i_public_key             (d2i_of_void *)d2i_DSAPublicKey
411
# define dsa_d2i_key_params             (d2i_of_void *)d2i_DSAparams
412
413
static void *dsa_d2i_PKCS8(void **key, const unsigned char **der, long der_len,
414
                           struct der2key_ctx_st *ctx)
415
0
{
416
0
    return der2key_decode_p8(der, der_len, ctx,
417
0
                             (key_from_pkcs8_t *)ossl_dsa_key_from_pkcs8);
418
0
}
419
420
# define dsa_d2i_PUBKEY                 (d2i_of_void *)ossl_d2i_DSA_PUBKEY
421
# define dsa_free                       (free_key_fn *)DSA_free
422
# define dsa_check                      NULL
423
424
static void dsa_adjust(void *key, struct der2key_ctx_st *ctx)
425
0
{
426
0
    ossl_dsa_set0_libctx(key, PROV_LIBCTX_OF(ctx->provctx));
427
0
}
428
#endif
429
430
/* ---------------------------------------------------------------------- */
431
432
#ifndef OPENSSL_NO_EC
433
# define ec_evp_type                    EVP_PKEY_EC
434
# define ec_d2i_private_key             (d2i_of_void *)d2i_ECPrivateKey
435
# define ec_d2i_public_key              NULL
436
# define ec_d2i_key_params              (d2i_of_void *)d2i_ECParameters
437
438
static void *ec_d2i_PKCS8(void **key, const unsigned char **der, long der_len,
439
                          struct der2key_ctx_st *ctx)
440
0
{
441
0
    return der2key_decode_p8(der, der_len, ctx,
442
0
                             (key_from_pkcs8_t *)ossl_ec_key_from_pkcs8);
443
0
}
444
445
# define ec_d2i_PUBKEY                  (d2i_of_void *)d2i_EC_PUBKEY
446
# define ec_free                        (free_key_fn *)EC_KEY_free
447
448
static int ec_check(void *key, struct der2key_ctx_st *ctx)
449
0
{
450
    /* We're trying to be clever by comparing two truths */
451
0
    int ret = 0;
452
0
    int sm2 = (EC_KEY_get_flags(key) & EC_FLAG_SM2_RANGE) != 0;
453
454
0
    if (sm2)
455
0
        ret = ctx->desc->evp_type == EVP_PKEY_SM2
456
0
            || ctx->desc->evp_type == NID_X9_62_id_ecPublicKey;
457
0
    else
458
0
        ret = ctx->desc->evp_type != EVP_PKEY_SM2;
459
460
0
    return ret;
461
0
}
462
463
static void ec_adjust(void *key, struct der2key_ctx_st *ctx)
464
0
{
465
0
    ossl_ec_key_set0_libctx(key, PROV_LIBCTX_OF(ctx->provctx));
466
0
}
467
468
# ifndef OPENSSL_NO_ECX
469
/*
470
 * ED25519, ED448, X25519, X448 only implement PKCS#8 and SubjectPublicKeyInfo,
471
 * so no d2i functions to be had.
472
 */
473
474
static void *ecx_d2i_PKCS8(void **key, const unsigned char **der, long der_len,
475
                           struct der2key_ctx_st *ctx)
476
0
{
477
0
    return der2key_decode_p8(der, der_len, ctx,
478
0
                             (key_from_pkcs8_t *)ossl_ecx_key_from_pkcs8);
479
0
}
480
481
static void ecx_key_adjust(void *key, struct der2key_ctx_st *ctx)
482
0
{
483
0
    ossl_ecx_key_set0_libctx(key, PROV_LIBCTX_OF(ctx->provctx));
484
0
}
485
486
#  define ed25519_evp_type               EVP_PKEY_ED25519
487
#  define ed25519_d2i_private_key        NULL
488
#  define ed25519_d2i_public_key         NULL
489
#  define ed25519_d2i_key_params         NULL
490
#  define ed25519_d2i_PKCS8              ecx_d2i_PKCS8
491
#  define ed25519_d2i_PUBKEY             (d2i_of_void *)ossl_d2i_ED25519_PUBKEY
492
#  define ed25519_free                   (free_key_fn *)ossl_ecx_key_free
493
#  define ed25519_check                  NULL
494
#  define ed25519_adjust                 ecx_key_adjust
495
496
#  define ed448_evp_type                 EVP_PKEY_ED448
497
#  define ed448_d2i_private_key          NULL
498
#  define ed448_d2i_public_key           NULL
499
#  define ed448_d2i_key_params           NULL
500
#  define ed448_d2i_PKCS8                ecx_d2i_PKCS8
501
#  define ed448_d2i_PUBKEY               (d2i_of_void *)ossl_d2i_ED448_PUBKEY
502
#  define ed448_free                     (free_key_fn *)ossl_ecx_key_free
503
#  define ed448_check                    NULL
504
#  define ed448_adjust                   ecx_key_adjust
505
506
#  define x25519_evp_type                EVP_PKEY_X25519
507
#  define x25519_d2i_private_key         NULL
508
#  define x25519_d2i_public_key          NULL
509
#  define x25519_d2i_key_params          NULL
510
#  define x25519_d2i_PKCS8               ecx_d2i_PKCS8
511
#  define x25519_d2i_PUBKEY              (d2i_of_void *)ossl_d2i_X25519_PUBKEY
512
#  define x25519_free                    (free_key_fn *)ossl_ecx_key_free
513
#  define x25519_check                   NULL
514
#  define x25519_adjust                  ecx_key_adjust
515
516
#  define x448_evp_type                  EVP_PKEY_X448
517
#  define x448_d2i_private_key           NULL
518
#  define x448_d2i_public_key            NULL
519
#  define x448_d2i_key_params            NULL
520
#  define x448_d2i_PKCS8                 ecx_d2i_PKCS8
521
#  define x448_d2i_PUBKEY                (d2i_of_void *)ossl_d2i_X448_PUBKEY
522
#  define x448_free                      (free_key_fn *)ossl_ecx_key_free
523
#  define x448_check                     NULL
524
#  define x448_adjust                    ecx_key_adjust
525
# endif /* OPENSSL_NO_ECX */
526
527
# ifndef OPENSSL_NO_SM2
528
#  define sm2_evp_type                  EVP_PKEY_SM2
529
#  define sm2_d2i_private_key           (d2i_of_void *)d2i_ECPrivateKey
530
#  define sm2_d2i_public_key            NULL
531
#  define sm2_d2i_key_params            (d2i_of_void *)d2i_ECParameters
532
533
static void *sm2_d2i_PKCS8(void **key, const unsigned char **der, long der_len,
534
                           struct der2key_ctx_st *ctx)
535
0
{
536
0
    return der2key_decode_p8(der, der_len, ctx,
537
0
                             (key_from_pkcs8_t *)ossl_ec_key_from_pkcs8);
538
0
}
539
540
#  define sm2_d2i_PUBKEY                (d2i_of_void *)d2i_EC_PUBKEY
541
#  define sm2_free                      (free_key_fn *)EC_KEY_free
542
#  define sm2_check                     ec_check
543
#  define sm2_adjust                    ec_adjust
544
# endif
545
#endif
546
547
/* ---------------------------------------------------------------------- */
548
549
#define rsa_evp_type                    EVP_PKEY_RSA
550
#define rsa_d2i_private_key             (d2i_of_void *)d2i_RSAPrivateKey
551
#define rsa_d2i_public_key              (d2i_of_void *)d2i_RSAPublicKey
552
#define rsa_d2i_key_params              NULL
553
554
static void *rsa_d2i_PKCS8(void **key, const unsigned char **der, long der_len,
555
                           struct der2key_ctx_st *ctx)
556
0
{
557
0
    return der2key_decode_p8(der, der_len, ctx,
558
0
                             (key_from_pkcs8_t *)ossl_rsa_key_from_pkcs8);
559
0
}
560
561
#define rsa_d2i_PUBKEY                  (d2i_of_void *)d2i_RSA_PUBKEY
562
#define rsa_free                        (free_key_fn *)RSA_free
563
564
static int rsa_check(void *key, struct der2key_ctx_st *ctx)
565
0
{
566
0
    int valid;
567
568
0
    switch (RSA_test_flags(key, RSA_FLAG_TYPE_MASK)) {
569
0
    case RSA_FLAG_TYPE_RSA:
570
0
        valid = (ctx->desc->evp_type == EVP_PKEY_RSA);
571
0
        break;
572
0
    case RSA_FLAG_TYPE_RSASSAPSS:
573
0
        valid = (ctx->desc->evp_type == EVP_PKEY_RSA_PSS);
574
0
        break;
575
0
    default:
576
        /* Currently unsupported RSA key type */
577
0
        valid = 0;
578
0
    }
579
580
0
    valid = (valid && ossl_rsa_check_factors(key));
581
582
0
    return valid;
583
0
}
584
585
static void rsa_adjust(void *key, struct der2key_ctx_st *ctx)
586
0
{
587
0
    ossl_rsa_set0_libctx(key, PROV_LIBCTX_OF(ctx->provctx));
588
0
}
589
590
#define rsapss_evp_type                 EVP_PKEY_RSA_PSS
591
#define rsapss_d2i_private_key          (d2i_of_void *)d2i_RSAPrivateKey
592
#define rsapss_d2i_public_key           (d2i_of_void *)d2i_RSAPublicKey
593
#define rsapss_d2i_key_params           NULL
594
#define rsapss_d2i_PKCS8                rsa_d2i_PKCS8
595
#define rsapss_d2i_PUBKEY               (d2i_of_void *)d2i_RSA_PUBKEY
596
#define rsapss_free                     (free_key_fn *)RSA_free
597
#define rsapss_check                    rsa_check
598
#define rsapss_adjust                   rsa_adjust
599
600
/* ---------------------------------------------------------------------- */
601
602
/*
603
 * The DO_ macros help define the selection mask and the method functions
604
 * for each kind of object we want to decode.
605
 */
606
#define DO_type_specific_keypair(keytype)               \
607
    "type-specific", keytype##_evp_type,                \
608
        ( OSSL_KEYMGMT_SELECT_KEYPAIR ),                \
609
        keytype##_d2i_private_key,                      \
610
        keytype##_d2i_public_key,                       \
611
        NULL,                                           \
612
        NULL,                                           \
613
        NULL,                                           \
614
        keytype##_check,                                \
615
        keytype##_adjust,                               \
616
        keytype##_free
617
618
#define DO_type_specific_pub(keytype)                   \
619
    "type-specific", keytype##_evp_type,                \
620
        ( OSSL_KEYMGMT_SELECT_PUBLIC_KEY ),             \
621
        NULL,                                           \
622
        keytype##_d2i_public_key,                       \
623
        NULL,                                           \
624
        NULL,                                           \
625
        NULL,                                           \
626
        keytype##_check,                                \
627
        keytype##_adjust,                               \
628
        keytype##_free
629
630
#define DO_type_specific_priv(keytype)                  \
631
    "type-specific", keytype##_evp_type,                \
632
        ( OSSL_KEYMGMT_SELECT_PRIVATE_KEY ),            \
633
        keytype##_d2i_private_key,                      \
634
        NULL,                                           \
635
        NULL,                                           \
636
        NULL,                                           \
637
        NULL,                                           \
638
        keytype##_check,                                \
639
        keytype##_adjust,                               \
640
        keytype##_free
641
642
#define DO_type_specific_params(keytype)                \
643
    "type-specific", keytype##_evp_type,                \
644
        ( OSSL_KEYMGMT_SELECT_ALL_PARAMETERS ),         \
645
        NULL,                                           \
646
        NULL,                                           \
647
        keytype##_d2i_key_params,                       \
648
        NULL,                                           \
649
        NULL,                                           \
650
        keytype##_check,                                \
651
        keytype##_adjust,                               \
652
        keytype##_free
653
654
#define DO_type_specific(keytype)                       \
655
    "type-specific", keytype##_evp_type,                \
656
        ( OSSL_KEYMGMT_SELECT_ALL ),                    \
657
        keytype##_d2i_private_key,                      \
658
        keytype##_d2i_public_key,                       \
659
        keytype##_d2i_key_params,                       \
660
        NULL,                                           \
661
        NULL,                                           \
662
        keytype##_check,                                \
663
        keytype##_adjust,                               \
664
        keytype##_free
665
666
#define DO_type_specific_no_pub(keytype)                \
667
    "type-specific", keytype##_evp_type,                \
668
        ( OSSL_KEYMGMT_SELECT_PRIVATE_KEY               \
669
          | OSSL_KEYMGMT_SELECT_ALL_PARAMETERS ),       \
670
        keytype##_d2i_private_key,                      \
671
        NULL,                                           \
672
        keytype##_d2i_key_params,                       \
673
        NULL,                                           \
674
        NULL,                                           \
675
        keytype##_check,                                \
676
        keytype##_adjust,                               \
677
        keytype##_free
678
679
#define DO_PrivateKeyInfo(keytype)                      \
680
    "PrivateKeyInfo", keytype##_evp_type,               \
681
        ( OSSL_KEYMGMT_SELECT_PRIVATE_KEY ),            \
682
        NULL,                                           \
683
        NULL,                                           \
684
        NULL,                                           \
685
        keytype##_d2i_PKCS8,                            \
686
        NULL,                                           \
687
        keytype##_check,                                \
688
        keytype##_adjust,                               \
689
        keytype##_free
690
691
#define DO_SubjectPublicKeyInfo(keytype)                \
692
    "SubjectPublicKeyInfo", keytype##_evp_type,         \
693
        ( OSSL_KEYMGMT_SELECT_PUBLIC_KEY ),             \
694
        NULL,                                           \
695
        NULL,                                           \
696
        NULL,                                           \
697
        NULL,                                           \
698
        keytype##_d2i_PUBKEY,                           \
699
        keytype##_check,                                \
700
        keytype##_adjust,                               \
701
        keytype##_free
702
703
#define DO_DH(keytype)                                  \
704
    "DH", keytype##_evp_type,                           \
705
        ( OSSL_KEYMGMT_SELECT_ALL_PARAMETERS ),         \
706
        NULL,                                           \
707
        NULL,                                           \
708
        keytype##_d2i_key_params,                       \
709
        NULL,                                           \
710
        NULL,                                           \
711
        keytype##_check,                                \
712
        keytype##_adjust,                               \
713
        keytype##_free
714
715
#define DO_DHX(keytype)                                 \
716
    "DHX", keytype##_evp_type,                          \
717
        ( OSSL_KEYMGMT_SELECT_ALL_PARAMETERS ),         \
718
        NULL,                                           \
719
        NULL,                                           \
720
        keytype##_d2i_key_params,                       \
721
        NULL,                                           \
722
        NULL,                                           \
723
        keytype##_check,                                \
724
        keytype##_adjust,                               \
725
        keytype##_free
726
727
#define DO_DSA(keytype)                                 \
728
    "DSA", keytype##_evp_type,                          \
729
        ( OSSL_KEYMGMT_SELECT_ALL ),                    \
730
        keytype##_d2i_private_key,                      \
731
        keytype##_d2i_public_key,                       \
732
        keytype##_d2i_key_params,                       \
733
        NULL,                                           \
734
        NULL,                                           \
735
        keytype##_check,                                \
736
        keytype##_adjust,                               \
737
        keytype##_free
738
739
#define DO_EC(keytype)                                  \
740
    "EC", keytype##_evp_type,                           \
741
        ( OSSL_KEYMGMT_SELECT_PRIVATE_KEY               \
742
          | OSSL_KEYMGMT_SELECT_ALL_PARAMETERS ),       \
743
        keytype##_d2i_private_key,                      \
744
        NULL,                                           \
745
        keytype##_d2i_key_params,                       \
746
        NULL,                                           \
747
        NULL,                                           \
748
        keytype##_check,                                \
749
        keytype##_adjust,                               \
750
        keytype##_free
751
752
#define DO_RSA(keytype)                                 \
753
    "RSA", keytype##_evp_type,                          \
754
        ( OSSL_KEYMGMT_SELECT_KEYPAIR ),                \
755
        keytype##_d2i_private_key,                      \
756
        keytype##_d2i_public_key,                       \
757
        NULL,                                           \
758
        NULL,                                           \
759
        NULL,                                           \
760
        keytype##_check,                                \
761
        keytype##_adjust,                               \
762
        keytype##_free
763
764
/*
765
 * MAKE_DECODER is the single driver for creating OSSL_DISPATCH tables.
766
 * It takes the following arguments:
767
 *
768
 * keytype_name The implementation key type as a string.
769
 * keytype      The implementation key type.  This must correspond exactly
770
 *              to our existing keymgmt keytype names...  in other words,
771
 *              there must exist an ossl_##keytype##_keymgmt_functions.
772
 * type         The type name for the set of functions that implement the
773
 *              decoder for the key type.  This isn't necessarily the same
774
 *              as keytype.  For example, the key types ed25519, ed448,
775
 *              x25519 and x448 are all handled by the same functions with
776
 *              the common type name ecx.
777
 * kind         The kind of support to implement.  This translates into
778
 *              the DO_##kind macros above, to populate the keytype_desc_st
779
 *              structure.
780
 */
781
#define MAKE_DECODER(keytype_name, keytype, type, kind)                 \
782
    static const struct keytype_desc_st kind##_##keytype##_desc =       \
783
        { keytype_name, ossl_##keytype##_keymgmt_functions,             \
784
          DO_##kind(keytype) };                                         \
785
                                                                        \
786
    static OSSL_FUNC_decoder_newctx_fn kind##_der2##keytype##_newctx;   \
787
                                                                        \
788
    static void *kind##_der2##keytype##_newctx(void *provctx)           \
789
0
    {                                                                   \
790
0
        return der2key_newctx(provctx, &kind##_##keytype##_desc);       \
791
0
    }                                                                   \
Unexecuted instantiation: decode_der2key.c:PrivateKeyInfo_der2dh_newctx
Unexecuted instantiation: decode_der2key.c:SubjectPublicKeyInfo_der2dh_newctx
Unexecuted instantiation: decode_der2key.c:type_specific_params_der2dh_newctx
Unexecuted instantiation: decode_der2key.c:DH_der2dh_newctx
Unexecuted instantiation: decode_der2key.c:PrivateKeyInfo_der2dhx_newctx
Unexecuted instantiation: decode_der2key.c:SubjectPublicKeyInfo_der2dhx_newctx
Unexecuted instantiation: decode_der2key.c:type_specific_params_der2dhx_newctx
Unexecuted instantiation: decode_der2key.c:DHX_der2dhx_newctx
Unexecuted instantiation: decode_der2key.c:PrivateKeyInfo_der2dsa_newctx
Unexecuted instantiation: decode_der2key.c:SubjectPublicKeyInfo_der2dsa_newctx
Unexecuted instantiation: decode_der2key.c:type_specific_der2dsa_newctx
Unexecuted instantiation: decode_der2key.c:DSA_der2dsa_newctx
Unexecuted instantiation: decode_der2key.c:PrivateKeyInfo_der2ec_newctx
Unexecuted instantiation: decode_der2key.c:SubjectPublicKeyInfo_der2ec_newctx
Unexecuted instantiation: decode_der2key.c:type_specific_no_pub_der2ec_newctx
Unexecuted instantiation: decode_der2key.c:EC_der2ec_newctx
Unexecuted instantiation: decode_der2key.c:PrivateKeyInfo_der2x25519_newctx
Unexecuted instantiation: decode_der2key.c:SubjectPublicKeyInfo_der2x25519_newctx
Unexecuted instantiation: decode_der2key.c:PrivateKeyInfo_der2x448_newctx
Unexecuted instantiation: decode_der2key.c:SubjectPublicKeyInfo_der2x448_newctx
Unexecuted instantiation: decode_der2key.c:PrivateKeyInfo_der2ed25519_newctx
Unexecuted instantiation: decode_der2key.c:SubjectPublicKeyInfo_der2ed25519_newctx
Unexecuted instantiation: decode_der2key.c:PrivateKeyInfo_der2ed448_newctx
Unexecuted instantiation: decode_der2key.c:SubjectPublicKeyInfo_der2ed448_newctx
Unexecuted instantiation: decode_der2key.c:PrivateKeyInfo_der2sm2_newctx
Unexecuted instantiation: decode_der2key.c:SubjectPublicKeyInfo_der2sm2_newctx
Unexecuted instantiation: decode_der2key.c:type_specific_no_pub_der2sm2_newctx
Unexecuted instantiation: decode_der2key.c:PrivateKeyInfo_der2rsa_newctx
Unexecuted instantiation: decode_der2key.c:SubjectPublicKeyInfo_der2rsa_newctx
Unexecuted instantiation: decode_der2key.c:type_specific_keypair_der2rsa_newctx
Unexecuted instantiation: decode_der2key.c:RSA_der2rsa_newctx
Unexecuted instantiation: decode_der2key.c:PrivateKeyInfo_der2rsapss_newctx
Unexecuted instantiation: decode_der2key.c:SubjectPublicKeyInfo_der2rsapss_newctx
792
    static int kind##_der2##keytype##_does_selection(void *provctx,     \
793
                                                     int selection)     \
794
0
    {                                                                   \
795
0
        return der2key_check_selection(selection,                       \
796
0
                                       &kind##_##keytype##_desc);       \
797
0
    }                                                                   \
Unexecuted instantiation: decode_der2key.c:PrivateKeyInfo_der2dh_does_selection
Unexecuted instantiation: decode_der2key.c:SubjectPublicKeyInfo_der2dh_does_selection
Unexecuted instantiation: decode_der2key.c:type_specific_params_der2dh_does_selection
Unexecuted instantiation: decode_der2key.c:DH_der2dh_does_selection
Unexecuted instantiation: decode_der2key.c:PrivateKeyInfo_der2dhx_does_selection
Unexecuted instantiation: decode_der2key.c:SubjectPublicKeyInfo_der2dhx_does_selection
Unexecuted instantiation: decode_der2key.c:type_specific_params_der2dhx_does_selection
Unexecuted instantiation: decode_der2key.c:DHX_der2dhx_does_selection
Unexecuted instantiation: decode_der2key.c:PrivateKeyInfo_der2dsa_does_selection
Unexecuted instantiation: decode_der2key.c:SubjectPublicKeyInfo_der2dsa_does_selection
Unexecuted instantiation: decode_der2key.c:type_specific_der2dsa_does_selection
Unexecuted instantiation: decode_der2key.c:DSA_der2dsa_does_selection
Unexecuted instantiation: decode_der2key.c:PrivateKeyInfo_der2ec_does_selection
Unexecuted instantiation: decode_der2key.c:SubjectPublicKeyInfo_der2ec_does_selection
Unexecuted instantiation: decode_der2key.c:type_specific_no_pub_der2ec_does_selection
Unexecuted instantiation: decode_der2key.c:EC_der2ec_does_selection
Unexecuted instantiation: decode_der2key.c:PrivateKeyInfo_der2x25519_does_selection
Unexecuted instantiation: decode_der2key.c:SubjectPublicKeyInfo_der2x25519_does_selection
Unexecuted instantiation: decode_der2key.c:PrivateKeyInfo_der2x448_does_selection
Unexecuted instantiation: decode_der2key.c:SubjectPublicKeyInfo_der2x448_does_selection
Unexecuted instantiation: decode_der2key.c:PrivateKeyInfo_der2ed25519_does_selection
Unexecuted instantiation: decode_der2key.c:SubjectPublicKeyInfo_der2ed25519_does_selection
Unexecuted instantiation: decode_der2key.c:PrivateKeyInfo_der2ed448_does_selection
Unexecuted instantiation: decode_der2key.c:SubjectPublicKeyInfo_der2ed448_does_selection
Unexecuted instantiation: decode_der2key.c:PrivateKeyInfo_der2sm2_does_selection
Unexecuted instantiation: decode_der2key.c:SubjectPublicKeyInfo_der2sm2_does_selection
Unexecuted instantiation: decode_der2key.c:type_specific_no_pub_der2sm2_does_selection
Unexecuted instantiation: decode_der2key.c:PrivateKeyInfo_der2rsa_does_selection
Unexecuted instantiation: decode_der2key.c:SubjectPublicKeyInfo_der2rsa_does_selection
Unexecuted instantiation: decode_der2key.c:type_specific_keypair_der2rsa_does_selection
Unexecuted instantiation: decode_der2key.c:RSA_der2rsa_does_selection
Unexecuted instantiation: decode_der2key.c:PrivateKeyInfo_der2rsapss_does_selection
Unexecuted instantiation: decode_der2key.c:SubjectPublicKeyInfo_der2rsapss_does_selection
798
    const OSSL_DISPATCH                                                 \
799
    ossl_##kind##_der_to_##keytype##_decoder_functions[] = {            \
800
        { OSSL_FUNC_DECODER_NEWCTX,                                     \
801
          (void (*)(void))kind##_der2##keytype##_newctx },              \
802
        { OSSL_FUNC_DECODER_FREECTX,                                    \
803
          (void (*)(void))der2key_freectx },                            \
804
        { OSSL_FUNC_DECODER_DOES_SELECTION,                             \
805
          (void (*)(void))kind##_der2##keytype##_does_selection },      \
806
        { OSSL_FUNC_DECODER_DECODE,                                     \
807
          (void (*)(void))der2key_decode },                             \
808
        { OSSL_FUNC_DECODER_EXPORT_OBJECT,                              \
809
          (void (*)(void))der2key_export_object },                      \
810
        { OSSL_FUNC_DECODER_SETTABLE_CTX_PARAMS,                        \
811
          (void (*)(void))der2key_settable_ctx_params },                \
812
        { OSSL_FUNC_DECODER_SET_CTX_PARAMS,                             \
813
          (void (*)(void))der2key_set_ctx_params },                     \
814
        OSSL_DISPATCH_END                                               \
815
    }
816
817
#ifndef OPENSSL_NO_DH
818
MAKE_DECODER("DH", dh, dh, PrivateKeyInfo);
819
MAKE_DECODER("DH", dh, dh, SubjectPublicKeyInfo);
820
MAKE_DECODER("DH", dh, dh, type_specific_params);
821
MAKE_DECODER("DH", dh, dh, DH);
822
MAKE_DECODER("DHX", dhx, dhx, PrivateKeyInfo);
823
MAKE_DECODER("DHX", dhx, dhx, SubjectPublicKeyInfo);
824
MAKE_DECODER("DHX", dhx, dhx, type_specific_params);
825
MAKE_DECODER("DHX", dhx, dhx, DHX);
826
#endif
827
#ifndef OPENSSL_NO_DSA
828
MAKE_DECODER("DSA", dsa, dsa, PrivateKeyInfo);
829
MAKE_DECODER("DSA", dsa, dsa, SubjectPublicKeyInfo);
830
MAKE_DECODER("DSA", dsa, dsa, type_specific);
831
MAKE_DECODER("DSA", dsa, dsa, DSA);
832
#endif
833
#ifndef OPENSSL_NO_EC
834
MAKE_DECODER("EC", ec, ec, PrivateKeyInfo);
835
MAKE_DECODER("EC", ec, ec, SubjectPublicKeyInfo);
836
MAKE_DECODER("EC", ec, ec, type_specific_no_pub);
837
MAKE_DECODER("EC", ec, ec, EC);
838
# ifndef OPENSSL_NO_ECX
839
MAKE_DECODER("X25519", x25519, ecx, PrivateKeyInfo);
840
MAKE_DECODER("X25519", x25519, ecx, SubjectPublicKeyInfo);
841
MAKE_DECODER("X448", x448, ecx, PrivateKeyInfo);
842
MAKE_DECODER("X448", x448, ecx, SubjectPublicKeyInfo);
843
MAKE_DECODER("ED25519", ed25519, ecx, PrivateKeyInfo);
844
MAKE_DECODER("ED25519", ed25519, ecx, SubjectPublicKeyInfo);
845
MAKE_DECODER("ED448", ed448, ecx, PrivateKeyInfo);
846
MAKE_DECODER("ED448", ed448, ecx, SubjectPublicKeyInfo);
847
# endif
848
# ifndef OPENSSL_NO_SM2
849
MAKE_DECODER("SM2", sm2, ec, PrivateKeyInfo);
850
MAKE_DECODER("SM2", sm2, ec, SubjectPublicKeyInfo);
851
MAKE_DECODER("SM2", sm2, sm2, type_specific_no_pub);
852
# endif
853
#endif
854
MAKE_DECODER("RSA", rsa, rsa, PrivateKeyInfo);
855
MAKE_DECODER("RSA", rsa, rsa, SubjectPublicKeyInfo);
856
MAKE_DECODER("RSA", rsa, rsa, type_specific_keypair);
857
MAKE_DECODER("RSA", rsa, rsa, RSA);
858
MAKE_DECODER("RSA-PSS", rsapss, rsapss, PrivateKeyInfo);
859
MAKE_DECODER("RSA-PSS", rsapss, rsapss, SubjectPublicKeyInfo);