2 * Copyright 2020-2023 The OpenSSL Project Authors. All Rights Reserved.
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
11 * low level APIs are deprecated for public use, but still ok for
14 #include "internal/deprecated.h"
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"
36 #include "prov/implementations.h"
37 #include "endecoder_local.h"
38 #include "internal/nelem.h"
40 struct der2key_ctx_st
; /* Forward declaration */
41 typedef int check_key_fn(void *, struct der2key_ctx_st
*ctx
);
42 typedef void adjust_key_fn(void *, struct der2key_ctx_st
*ctx
);
43 typedef void free_key_fn(void *);
44 typedef void *d2i_PKCS8_fn(void **, const unsigned char **, long,
45 struct der2key_ctx_st
*);
46 struct keytype_desc_st
{
47 const char *keytype_name
;
48 const OSSL_DISPATCH
*fns
; /* Keymgmt (to pilfer functions from) */
50 /* The input structure name */
51 const char *structure_name
;
54 * The EVP_PKEY_xxx type macro. Should be zero for type specific
55 * structures, non-zero when the outermost structure is PKCS#8 or
56 * SubjectPublicKeyInfo. This determines which of the function
57 * pointers below will be used.
61 /* The selection mask for OSSL_FUNC_decoder_does_selection() */
64 /* For type specific decoders, we use the corresponding d2i */
65 d2i_of_void
*d2i_private_key
; /* From type-specific DER */
66 d2i_of_void
*d2i_public_key
; /* From type-specific DER */
67 d2i_of_void
*d2i_key_params
; /* From type-specific DER */
68 d2i_PKCS8_fn
*d2i_PKCS8
; /* Wrapped in a PrivateKeyInfo */
69 d2i_of_void
*d2i_PUBKEY
; /* Wrapped in a SubjectPublicKeyInfo */
72 * For any key, we may need to check that the key meets expectations.
73 * This is useful when the same functions can decode several variants
76 check_key_fn
*check_key
;
79 * For any key, we may need to make provider specific adjustments, such
80 * as ensure the key carries the correct library context.
82 adjust_key_fn
*adjust_key
;
84 free_key_fn
*free_key
;
88 * Context used for DER to key decoding.
90 struct der2key_ctx_st
{
92 char propq
[OSSL_MAX_PROPQUERY_SIZE
];
93 const struct keytype_desc_st
*desc
;
94 /* The selection that is passed to der2key_decode() */
96 /* Flag used to signal that a failure is fatal */
97 unsigned int flag_fatal
: 1;
100 typedef void *key_from_pkcs8_t(const PKCS8_PRIV_KEY_INFO
*p8inf
,
101 OSSL_LIB_CTX
*libctx
, const char *propq
);
102 static void *der2key_decode_p8(const unsigned char **input_der
,
103 long input_der_len
, struct der2key_ctx_st
*ctx
,
104 key_from_pkcs8_t
*key_from_pkcs8
)
106 PKCS8_PRIV_KEY_INFO
*p8inf
= NULL
;
107 const X509_ALGOR
*alg
= NULL
;
110 if ((p8inf
= d2i_PKCS8_PRIV_KEY_INFO(NULL
, input_der
, input_der_len
)) != NULL
111 && PKCS8_pkey_get0(NULL
, NULL
, NULL
, &alg
, p8inf
)
112 && OBJ_obj2nid(alg
->algorithm
) == ctx
->desc
->evp_type
)
113 key
= key_from_pkcs8(p8inf
, PROV_LIBCTX_OF(ctx
->provctx
), ctx
->propq
);
114 PKCS8_PRIV_KEY_INFO_free(p8inf
);
119 /* ---------------------------------------------------------------------- */
121 static OSSL_FUNC_decoder_freectx_fn der2key_freectx
;
122 static OSSL_FUNC_decoder_decode_fn der2key_decode
;
123 static OSSL_FUNC_decoder_export_object_fn der2key_export_object
;
124 static OSSL_FUNC_decoder_settable_ctx_params_fn der2key_settable_ctx_params
;
125 static OSSL_FUNC_decoder_set_ctx_params_fn der2key_set_ctx_params
;
127 static struct der2key_ctx_st
*
128 der2key_newctx(void *provctx
, const struct keytype_desc_st
*desc
)
130 struct der2key_ctx_st
*ctx
= OPENSSL_zalloc(sizeof(*ctx
));
133 ctx
->provctx
= provctx
;
139 static const OSSL_PARAM
*der2key_settable_ctx_params(ossl_unused
void *provctx
)
141 static const OSSL_PARAM settables
[] = {
142 OSSL_PARAM_utf8_string(OSSL_DECODER_PARAM_PROPERTIES
, NULL
, 0),
148 static int der2key_set_ctx_params(void *vctx
, const OSSL_PARAM params
[])
150 struct der2key_ctx_st
*ctx
= vctx
;
152 char *str
= ctx
->propq
;
154 p
= OSSL_PARAM_locate_const(params
, OSSL_DECODER_PARAM_PROPERTIES
);
155 if (p
!= NULL
&& !OSSL_PARAM_get_utf8_string(p
, &str
, sizeof(ctx
->propq
)))
161 static void der2key_freectx(void *vctx
)
163 struct der2key_ctx_st
*ctx
= vctx
;
168 static int der2key_check_selection(int selection
,
169 const struct keytype_desc_st
*desc
)
172 * The selections are kinda sorta "levels", i.e. each selection given
173 * here is assumed to include those following.
176 OSSL_KEYMGMT_SELECT_PRIVATE_KEY
,
177 OSSL_KEYMGMT_SELECT_PUBLIC_KEY
,
178 OSSL_KEYMGMT_SELECT_ALL_PARAMETERS
182 /* The decoder implementations made here support guessing */
186 for (i
= 0; i
< OSSL_NELEM(checks
); i
++) {
187 int check1
= (selection
& checks
[i
]) != 0;
188 int check2
= (desc
->selection_mask
& checks
[i
]) != 0;
191 * If the caller asked for the currently checked bit(s), return
192 * whether the decoder description says it's supported.
198 /* This should be dead code, but just to be safe... */
202 static int der2key_decode(void *vctx
, OSSL_CORE_BIO
*cin
, int selection
,
203 OSSL_CALLBACK
*data_cb
, void *data_cbarg
,
204 OSSL_PASSPHRASE_CALLBACK
*pw_cb
, void *pw_cbarg
)
206 struct der2key_ctx_st
*ctx
= vctx
;
207 unsigned char *der
= NULL
;
208 const unsigned char *derp
;
213 ctx
->selection
= selection
;
215 * The caller is allowed to specify 0 as a selection mark, to have the
216 * structure and key type guessed. For type-specific structures, this
217 * is not recommended, as some structures are very similar.
218 * Note that 0 isn't the same as OSSL_KEYMGMT_SELECT_ALL, as the latter
219 * signifies a private key structure, where everything else is assumed
220 * to be present as well.
223 selection
= ctx
->desc
->selection_mask
;
224 if ((selection
& ctx
->desc
->selection_mask
) == 0) {
225 ERR_raise(ERR_LIB_PROV
, ERR_R_PASSED_INVALID_ARGUMENT
);
229 ok
= ossl_read_der(ctx
->provctx
, cin
, &der
, &der_len
);
233 ok
= 0; /* Assume that we fail */
236 if ((selection
& OSSL_KEYMGMT_SELECT_PRIVATE_KEY
) != 0) {
238 if (ctx
->desc
->d2i_PKCS8
!= NULL
) {
239 key
= ctx
->desc
->d2i_PKCS8(NULL
, &derp
, der_len
, ctx
);
240 if (ctx
->flag_fatal
) {
241 ERR_clear_last_mark();
244 } else if (ctx
->desc
->d2i_private_key
!= NULL
) {
245 key
= ctx
->desc
->d2i_private_key(NULL
, &derp
, der_len
);
247 if (key
== NULL
&& ctx
->selection
!= 0) {
248 ERR_clear_last_mark();
252 if (key
== NULL
&& (selection
& OSSL_KEYMGMT_SELECT_PUBLIC_KEY
) != 0) {
254 if (ctx
->desc
->d2i_PUBKEY
!= NULL
)
255 key
= ctx
->desc
->d2i_PUBKEY(NULL
, &derp
, der_len
);
256 else if (ctx
->desc
->d2i_public_key
!= NULL
)
257 key
= ctx
->desc
->d2i_public_key(NULL
, &derp
, der_len
);
258 if (key
== NULL
&& ctx
->selection
!= 0) {
259 ERR_clear_last_mark();
263 if (key
== NULL
&& (selection
& OSSL_KEYMGMT_SELECT_ALL_PARAMETERS
) != 0) {
265 if (ctx
->desc
->d2i_key_params
!= NULL
)
266 key
= ctx
->desc
->d2i_key_params(NULL
, &derp
, der_len
);
267 if (key
== NULL
&& ctx
->selection
!= 0) {
268 ERR_clear_last_mark();
273 ERR_clear_last_mark();
278 * Last minute check to see if this was the correct type of key. This
279 * should never lead to a fatal error, i.e. the decoding itself was
280 * correct, it was just an unexpected key type. This is generally for
281 * classes of key types that have subtle variants, like RSA-PSS keys as
282 * opposed to plain RSA keys.
285 && ctx
->desc
->check_key
!= NULL
286 && !ctx
->desc
->check_key(key
, ctx
)) {
287 ctx
->desc
->free_key(key
);
291 if (key
!= NULL
&& ctx
->desc
->adjust_key
!= NULL
)
292 ctx
->desc
->adjust_key(key
, ctx
);
296 * Indicated that we successfully decoded something, or not at all.
297 * Ending up "empty handed" is not an error.
302 * We free memory here so it's not held up during the callback, because
303 * we know the process is recursive and the allocated chunks of memory
310 OSSL_PARAM params
[4];
311 int object_type
= OSSL_OBJECT_PKEY
;
314 OSSL_PARAM_construct_int(OSSL_OBJECT_PARAM_TYPE
, &object_type
);
316 OSSL_PARAM_construct_utf8_string(OSSL_OBJECT_PARAM_DATA_TYPE
,
317 (char *)ctx
->desc
->keytype_name
,
319 /* The address of the key becomes the octet string */
321 OSSL_PARAM_construct_octet_string(OSSL_OBJECT_PARAM_REFERENCE
,
323 params
[3] = OSSL_PARAM_construct_end();
325 ok
= data_cb(params
, data_cbarg
);
329 ctx
->desc
->free_key(key
);
335 static int der2key_export_object(void *vctx
,
336 const void *reference
, size_t reference_sz
,
337 OSSL_CALLBACK
*export_cb
, void *export_cbarg
)
339 struct der2key_ctx_st
*ctx
= vctx
;
340 OSSL_FUNC_keymgmt_export_fn
*export
=
341 ossl_prov_get_keymgmt_export(ctx
->desc
->fns
);
344 if (reference_sz
== sizeof(keydata
) && export
!= NULL
) {
345 int selection
= ctx
->selection
;
348 selection
= OSSL_KEYMGMT_SELECT_ALL
;
349 /* The contents of the reference is the address to our object */
350 keydata
= *(void **)reference
;
352 return export(keydata
, selection
, export_cb
, export_cbarg
);
357 /* ---------------------------------------------------------------------- */
359 #ifndef OPENSSL_NO_DH
360 # define dh_evp_type EVP_PKEY_DH
361 # define dh_d2i_private_key NULL
362 # define dh_d2i_public_key NULL
363 # define dh_d2i_key_params (d2i_of_void *)d2i_DHparams
365 static void *dh_d2i_PKCS8(void **key
, const unsigned char **der
, long der_len
,
366 struct der2key_ctx_st
*ctx
)
368 return der2key_decode_p8(der
, der_len
, ctx
,
369 (key_from_pkcs8_t
*)ossl_dh_key_from_pkcs8
);
372 # define dh_d2i_PUBKEY (d2i_of_void *)ossl_d2i_DH_PUBKEY
373 # define dh_free (free_key_fn *)DH_free
374 # define dh_check NULL
376 static void dh_adjust(void *key
, struct der2key_ctx_st
*ctx
)
378 ossl_dh_set0_libctx(key
, PROV_LIBCTX_OF(ctx
->provctx
));
381 # define dhx_evp_type EVP_PKEY_DHX
382 # define dhx_d2i_private_key NULL
383 # define dhx_d2i_public_key NULL
384 # define dhx_d2i_key_params (d2i_of_void *)d2i_DHxparams
385 # define dhx_d2i_PKCS8 dh_d2i_PKCS8
386 # define dhx_d2i_PUBKEY (d2i_of_void *)ossl_d2i_DHx_PUBKEY
387 # define dhx_free (free_key_fn *)DH_free
388 # define dhx_check NULL
389 # define dhx_adjust dh_adjust
392 /* ---------------------------------------------------------------------- */
394 #ifndef OPENSSL_NO_DSA
395 # define dsa_evp_type EVP_PKEY_DSA
396 # define dsa_d2i_private_key (d2i_of_void *)d2i_DSAPrivateKey
397 # define dsa_d2i_public_key (d2i_of_void *)d2i_DSAPublicKey
398 # define dsa_d2i_key_params (d2i_of_void *)d2i_DSAparams
400 static void *dsa_d2i_PKCS8(void **key
, const unsigned char **der
, long der_len
,
401 struct der2key_ctx_st
*ctx
)
403 return der2key_decode_p8(der
, der_len
, ctx
,
404 (key_from_pkcs8_t
*)ossl_dsa_key_from_pkcs8
);
407 # define dsa_d2i_PUBKEY (d2i_of_void *)ossl_d2i_DSA_PUBKEY
408 # define dsa_free (free_key_fn *)DSA_free
409 # define dsa_check NULL
411 static void dsa_adjust(void *key
, struct der2key_ctx_st
*ctx
)
413 ossl_dsa_set0_libctx(key
, PROV_LIBCTX_OF(ctx
->provctx
));
417 /* ---------------------------------------------------------------------- */
419 #ifndef OPENSSL_NO_EC
420 # define ec_evp_type EVP_PKEY_EC
421 # define ec_d2i_private_key (d2i_of_void *)d2i_ECPrivateKey
422 # define ec_d2i_public_key NULL
423 # define ec_d2i_key_params (d2i_of_void *)d2i_ECParameters
425 static void *ec_d2i_PKCS8(void **key
, const unsigned char **der
, long der_len
,
426 struct der2key_ctx_st
*ctx
)
428 return der2key_decode_p8(der
, der_len
, ctx
,
429 (key_from_pkcs8_t
*)ossl_ec_key_from_pkcs8
);
432 # define ec_d2i_PUBKEY (d2i_of_void *)d2i_EC_PUBKEY
433 # define ec_free (free_key_fn *)EC_KEY_free
435 static int ec_check(void *key
, struct der2key_ctx_st
*ctx
)
437 /* We're trying to be clever by comparing two truths */
439 int sm2
= (EC_KEY_get_flags(key
) & EC_FLAG_SM2_RANGE
) != 0;
441 return sm2
== (ctx
->desc
->evp_type
== EVP_PKEY_SM2
);
444 static void ec_adjust(void *key
, struct der2key_ctx_st
*ctx
)
446 ossl_ec_key_set0_libctx(key
, PROV_LIBCTX_OF(ctx
->provctx
));
449 # ifndef OPENSSL_NO_ECX
451 * ED25519, ED448, X25519, X448 only implement PKCS#8 and SubjectPublicKeyInfo,
452 * so no d2i functions to be had.
455 static void *ecx_d2i_PKCS8(void **key
, const unsigned char **der
, long der_len
,
456 struct der2key_ctx_st
*ctx
)
458 return der2key_decode_p8(der
, der_len
, ctx
,
459 (key_from_pkcs8_t
*)ossl_ecx_key_from_pkcs8
);
462 static void ecx_key_adjust(void *key
, struct der2key_ctx_st
*ctx
)
464 ossl_ecx_key_set0_libctx(key
, PROV_LIBCTX_OF(ctx
->provctx
));
467 # define ed25519_evp_type EVP_PKEY_ED25519
468 # define ed25519_d2i_private_key NULL
469 # define ed25519_d2i_public_key NULL
470 # define ed25519_d2i_key_params NULL
471 # define ed25519_d2i_PKCS8 ecx_d2i_PKCS8
472 # define ed25519_d2i_PUBKEY (d2i_of_void *)ossl_d2i_ED25519_PUBKEY
473 # define ed25519_free (free_key_fn *)ossl_ecx_key_free
474 # define ed25519_check NULL
475 # define ed25519_adjust ecx_key_adjust
477 # define ed448_evp_type EVP_PKEY_ED448
478 # define ed448_d2i_private_key NULL
479 # define ed448_d2i_public_key NULL
480 # define ed448_d2i_key_params NULL
481 # define ed448_d2i_PKCS8 ecx_d2i_PKCS8
482 # define ed448_d2i_PUBKEY (d2i_of_void *)ossl_d2i_ED448_PUBKEY
483 # define ed448_free (free_key_fn *)ossl_ecx_key_free
484 # define ed448_check NULL
485 # define ed448_adjust ecx_key_adjust
487 # define x25519_evp_type EVP_PKEY_X25519
488 # define x25519_d2i_private_key NULL
489 # define x25519_d2i_public_key NULL
490 # define x25519_d2i_key_params NULL
491 # define x25519_d2i_PKCS8 ecx_d2i_PKCS8
492 # define x25519_d2i_PUBKEY (d2i_of_void *)ossl_d2i_X25519_PUBKEY
493 # define x25519_free (free_key_fn *)ossl_ecx_key_free
494 # define x25519_check NULL
495 # define x25519_adjust ecx_key_adjust
497 # define x448_evp_type EVP_PKEY_X448
498 # define x448_d2i_private_key NULL
499 # define x448_d2i_public_key NULL
500 # define x448_d2i_key_params NULL
501 # define x448_d2i_PKCS8 ecx_d2i_PKCS8
502 # define x448_d2i_PUBKEY (d2i_of_void *)ossl_d2i_X448_PUBKEY
503 # define x448_free (free_key_fn *)ossl_ecx_key_free
504 # define x448_check NULL
505 # define x448_adjust ecx_key_adjust
506 # endif /* OPENSSL_NO_ECX */
508 # ifndef OPENSSL_NO_SM2
509 # define sm2_evp_type EVP_PKEY_SM2
510 # define sm2_d2i_private_key (d2i_of_void *)d2i_ECPrivateKey
511 # define sm2_d2i_public_key NULL
512 # define sm2_d2i_key_params (d2i_of_void *)d2i_ECParameters
514 static void *sm2_d2i_PKCS8(void **key
, const unsigned char **der
, long der_len
,
515 struct der2key_ctx_st
*ctx
)
517 return der2key_decode_p8(der
, der_len
, ctx
,
518 (key_from_pkcs8_t
*)ossl_ec_key_from_pkcs8
);
521 # define sm2_d2i_PUBKEY (d2i_of_void *)d2i_EC_PUBKEY
522 # define sm2_free (free_key_fn *)EC_KEY_free
523 # define sm2_check ec_check
524 # define sm2_adjust ec_adjust
528 /* ---------------------------------------------------------------------- */
530 #define rsa_evp_type EVP_PKEY_RSA
531 #define rsa_d2i_private_key (d2i_of_void *)d2i_RSAPrivateKey
532 #define rsa_d2i_public_key (d2i_of_void *)d2i_RSAPublicKey
533 #define rsa_d2i_key_params NULL
535 static void *rsa_d2i_PKCS8(void **key
, const unsigned char **der
, long der_len
,
536 struct der2key_ctx_st
*ctx
)
538 return der2key_decode_p8(der
, der_len
, ctx
,
539 (key_from_pkcs8_t
*)ossl_rsa_key_from_pkcs8
);
542 #define rsa_d2i_PUBKEY (d2i_of_void *)d2i_RSA_PUBKEY
543 #define rsa_free (free_key_fn *)RSA_free
545 static int rsa_check(void *key
, struct der2key_ctx_st
*ctx
)
547 switch (RSA_test_flags(key
, RSA_FLAG_TYPE_MASK
)) {
548 case RSA_FLAG_TYPE_RSA
:
549 return ctx
->desc
->evp_type
== EVP_PKEY_RSA
;
550 case RSA_FLAG_TYPE_RSASSAPSS
:
551 return ctx
->desc
->evp_type
== EVP_PKEY_RSA_PSS
;
554 /* Currently unsupported RSA key type */
558 static void rsa_adjust(void *key
, struct der2key_ctx_st
*ctx
)
560 ossl_rsa_set0_libctx(key
, PROV_LIBCTX_OF(ctx
->provctx
));
563 #define rsapss_evp_type EVP_PKEY_RSA_PSS
564 #define rsapss_d2i_private_key (d2i_of_void *)d2i_RSAPrivateKey
565 #define rsapss_d2i_public_key (d2i_of_void *)d2i_RSAPublicKey
566 #define rsapss_d2i_key_params NULL
567 #define rsapss_d2i_PKCS8 rsa_d2i_PKCS8
568 #define rsapss_d2i_PUBKEY (d2i_of_void *)d2i_RSA_PUBKEY
569 #define rsapss_free (free_key_fn *)RSA_free
570 #define rsapss_check rsa_check
571 #define rsapss_adjust rsa_adjust
573 /* ---------------------------------------------------------------------- */
576 * The DO_ macros help define the selection mask and the method functions
577 * for each kind of object we want to decode.
579 #define DO_type_specific_keypair(keytype) \
580 "type-specific", keytype##_evp_type, \
581 ( OSSL_KEYMGMT_SELECT_KEYPAIR ), \
582 keytype##_d2i_private_key, \
583 keytype##_d2i_public_key, \
591 #define DO_type_specific_pub(keytype) \
592 "type-specific", keytype##_evp_type, \
593 ( OSSL_KEYMGMT_SELECT_PUBLIC_KEY ), \
595 keytype##_d2i_public_key, \
603 #define DO_type_specific_priv(keytype) \
604 "type-specific", keytype##_evp_type, \
605 ( OSSL_KEYMGMT_SELECT_PRIVATE_KEY ), \
606 keytype##_d2i_private_key, \
615 #define DO_type_specific_params(keytype) \
616 "type-specific", keytype##_evp_type, \
617 ( OSSL_KEYMGMT_SELECT_ALL_PARAMETERS ), \
620 keytype##_d2i_key_params, \
627 #define DO_type_specific(keytype) \
628 "type-specific", keytype##_evp_type, \
629 ( OSSL_KEYMGMT_SELECT_ALL ), \
630 keytype##_d2i_private_key, \
631 keytype##_d2i_public_key, \
632 keytype##_d2i_key_params, \
639 #define DO_type_specific_no_pub(keytype) \
640 "type-specific", keytype##_evp_type, \
641 ( OSSL_KEYMGMT_SELECT_PRIVATE_KEY \
642 | OSSL_KEYMGMT_SELECT_ALL_PARAMETERS ), \
643 keytype##_d2i_private_key, \
645 keytype##_d2i_key_params, \
652 #define DO_PrivateKeyInfo(keytype) \
653 "PrivateKeyInfo", keytype##_evp_type, \
654 ( OSSL_KEYMGMT_SELECT_PRIVATE_KEY ), \
658 keytype##_d2i_PKCS8, \
664 #define DO_SubjectPublicKeyInfo(keytype) \
665 "SubjectPublicKeyInfo", keytype##_evp_type, \
666 ( OSSL_KEYMGMT_SELECT_PUBLIC_KEY ), \
671 keytype##_d2i_PUBKEY, \
676 #define DO_DH(keytype) \
677 "DH", keytype##_evp_type, \
678 ( OSSL_KEYMGMT_SELECT_ALL_PARAMETERS ), \
681 keytype##_d2i_key_params, \
688 #define DO_DHX(keytype) \
689 "DHX", keytype##_evp_type, \
690 ( OSSL_KEYMGMT_SELECT_ALL_PARAMETERS ), \
693 keytype##_d2i_key_params, \
700 #define DO_DSA(keytype) \
701 "DSA", keytype##_evp_type, \
702 ( OSSL_KEYMGMT_SELECT_ALL ), \
703 keytype##_d2i_private_key, \
704 keytype##_d2i_public_key, \
705 keytype##_d2i_key_params, \
712 #define DO_EC(keytype) \
713 "EC", keytype##_evp_type, \
714 ( OSSL_KEYMGMT_SELECT_PRIVATE_KEY \
715 | OSSL_KEYMGMT_SELECT_ALL_PARAMETERS ), \
716 keytype##_d2i_private_key, \
718 keytype##_d2i_key_params, \
725 #define DO_RSA(keytype) \
726 "RSA", keytype##_evp_type, \
727 ( OSSL_KEYMGMT_SELECT_KEYPAIR ), \
728 keytype##_d2i_private_key, \
729 keytype##_d2i_public_key, \
738 * MAKE_DECODER is the single driver for creating OSSL_DISPATCH tables.
739 * It takes the following arguments:
741 * keytype_name The implementation key type as a string.
742 * keytype The implementation key type. This must correspond exactly
743 * to our existing keymgmt keytype names... in other words,
744 * there must exist an ossl_##keytype##_keymgmt_functions.
745 * type The type name for the set of functions that implement the
746 * decoder for the key type. This isn't necessarily the same
747 * as keytype. For example, the key types ed25519, ed448,
748 * x25519 and x448 are all handled by the same functions with
749 * the common type name ecx.
750 * kind The kind of support to implement. This translates into
751 * the DO_##kind macros above, to populate the keytype_desc_st
754 #define MAKE_DECODER(keytype_name, keytype, type, kind) \
755 static const struct keytype_desc_st kind##_##keytype##_desc = \
756 { keytype_name, ossl_##keytype##_keymgmt_functions, \
757 DO_##kind(keytype) }; \
759 static OSSL_FUNC_decoder_newctx_fn kind##_der2##keytype##_newctx; \
761 static void *kind##_der2##keytype##_newctx(void *provctx) \
763 return der2key_newctx(provctx, &kind##_##keytype##_desc); \
765 static int kind##_der2##keytype##_does_selection(void *provctx, \
768 return der2key_check_selection(selection, \
769 &kind##_##keytype##_desc); \
771 const OSSL_DISPATCH \
772 ossl_##kind##_der_to_##keytype##_decoder_functions[] = { \
773 { OSSL_FUNC_DECODER_NEWCTX, \
774 (void (*)(void))kind##_der2##keytype##_newctx }, \
775 { OSSL_FUNC_DECODER_FREECTX, \
776 (void (*)(void))der2key_freectx }, \
777 { OSSL_FUNC_DECODER_DOES_SELECTION, \
778 (void (*)(void))kind##_der2##keytype##_does_selection }, \
779 { OSSL_FUNC_DECODER_DECODE, \
780 (void (*)(void))der2key_decode }, \
781 { OSSL_FUNC_DECODER_EXPORT_OBJECT, \
782 (void (*)(void))der2key_export_object }, \
783 { OSSL_FUNC_DECODER_SETTABLE_CTX_PARAMS, \
784 (void (*)(void))der2key_settable_ctx_params }, \
785 { OSSL_FUNC_DECODER_SET_CTX_PARAMS, \
786 (void (*)(void))der2key_set_ctx_params }, \
790 #ifndef OPENSSL_NO_DH
791 MAKE_DECODER("DH", dh
, dh
, PrivateKeyInfo
);
792 MAKE_DECODER("DH", dh
, dh
, SubjectPublicKeyInfo
);
793 MAKE_DECODER("DH", dh
, dh
, type_specific_params
);
794 MAKE_DECODER("DH", dh
, dh
, DH
);
795 MAKE_DECODER("DHX", dhx
, dhx
, PrivateKeyInfo
);
796 MAKE_DECODER("DHX", dhx
, dhx
, SubjectPublicKeyInfo
);
797 MAKE_DECODER("DHX", dhx
, dhx
, type_specific_params
);
798 MAKE_DECODER("DHX", dhx
, dhx
, DHX
);
800 #ifndef OPENSSL_NO_DSA
801 MAKE_DECODER("DSA", dsa
, dsa
, PrivateKeyInfo
);
802 MAKE_DECODER("DSA", dsa
, dsa
, SubjectPublicKeyInfo
);
803 MAKE_DECODER("DSA", dsa
, dsa
, type_specific
);
804 MAKE_DECODER("DSA", dsa
, dsa
, DSA
);
806 #ifndef OPENSSL_NO_EC
807 MAKE_DECODER("EC", ec
, ec
, PrivateKeyInfo
);
808 MAKE_DECODER("EC", ec
, ec
, SubjectPublicKeyInfo
);
809 MAKE_DECODER("EC", ec
, ec
, type_specific_no_pub
);
810 MAKE_DECODER("EC", ec
, ec
, EC
);
811 # ifndef OPENSSL_NO_ECX
812 MAKE_DECODER("X25519", x25519
, ecx
, PrivateKeyInfo
);
813 MAKE_DECODER("X25519", x25519
, ecx
, SubjectPublicKeyInfo
);
814 MAKE_DECODER("X448", x448
, ecx
, PrivateKeyInfo
);
815 MAKE_DECODER("X448", x448
, ecx
, SubjectPublicKeyInfo
);
816 MAKE_DECODER("ED25519", ed25519
, ecx
, PrivateKeyInfo
);
817 MAKE_DECODER("ED25519", ed25519
, ecx
, SubjectPublicKeyInfo
);
818 MAKE_DECODER("ED448", ed448
, ecx
, PrivateKeyInfo
);
819 MAKE_DECODER("ED448", ed448
, ecx
, SubjectPublicKeyInfo
);
821 # ifndef OPENSSL_NO_SM2
822 MAKE_DECODER("SM2", sm2
, ec
, PrivateKeyInfo
);
823 MAKE_DECODER("SM2", sm2
, ec
, SubjectPublicKeyInfo
);
824 MAKE_DECODER("SM2", sm2
, sm2
, type_specific_no_pub
);
827 MAKE_DECODER("RSA", rsa
, rsa
, PrivateKeyInfo
);
828 MAKE_DECODER("RSA", rsa
, rsa
, SubjectPublicKeyInfo
);
829 MAKE_DECODER("RSA", rsa
, rsa
, type_specific_keypair
);
830 MAKE_DECODER("RSA", rsa
, rsa
, RSA
);
831 MAKE_DECODER("RSA-PSS", rsapss
, rsapss
, PrivateKeyInfo
);
832 MAKE_DECODER("RSA-PSS", rsapss
, rsapss
, SubjectPublicKeyInfo
);