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