2 * Copyright 2020-2021 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"
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 OSSL_PASSPHRASE_CALLBACK *pw_cb, void *pw_cbarg);
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 PKCS#8, possibly encrypted */
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 const struct keytype_desc_st *desc;
93 /* Flag used to signal that a failure is fatal */
94 unsigned int flag_fatal : 1;
97 static int read_der(PROV_CTX *provctx, OSSL_CORE_BIO *cin,
98 unsigned char **data, long *len)
101 BIO *in = ossl_bio_new_from_core_bio(provctx, cin);
102 int ok = (asn1_d2i_read_bio(in, &mem) >= 0);
105 *data = (unsigned char *)mem->data;
106 *len = (long)mem->length;
113 typedef void *key_from_pkcs8_t(const PKCS8_PRIV_KEY_INFO *p8inf,
114 OSSL_LIB_CTX *libctx, const char *propq);
115 static void *der2key_decode_p8(const unsigned char **input_der,
116 long input_der_len, struct der2key_ctx_st *ctx,
117 OSSL_PASSPHRASE_CALLBACK *pw_cb, void *pw_cbarg,
118 key_from_pkcs8_t *key_from_pkcs8)
121 PKCS8_PRIV_KEY_INFO *p8inf = NULL;
122 const X509_ALGOR *alg = NULL;
128 if ((p8 = d2i_X509_SIG(NULL, input_der, input_der_len)) != NULL) {
129 char pbuf[PEM_BUFSIZE];
132 ERR_clear_last_mark();
134 if (!pw_cb(pbuf, sizeof(pbuf), &plen, NULL, pw_cbarg))
135 ERR_raise(ERR_LIB_PROV, PROV_R_UNABLE_TO_GET_PASSPHRASE);
137 p8inf = PKCS8_decrypt_ex(p8, pbuf, plen, PROV_LIBCTX_OF(ctx->provctx), NULL);
142 /* Pop any errors that might have been raised by d2i_X509_SIG. */
144 p8inf = d2i_PKCS8_PRIV_KEY_INFO(NULL, input_der, input_der_len);
147 && PKCS8_pkey_get0(NULL, NULL, NULL, &alg, p8inf)
148 && OBJ_obj2nid(alg->algorithm) == ctx->desc->evp_type)
149 key = key_from_pkcs8(p8inf, PROV_LIBCTX_OF(ctx->provctx), NULL);
150 PKCS8_PRIV_KEY_INFO_free(p8inf);
155 /* ---------------------------------------------------------------------- */
157 static OSSL_FUNC_decoder_freectx_fn der2key_freectx;
158 static OSSL_FUNC_decoder_decode_fn der2key_decode;
159 static OSSL_FUNC_decoder_export_object_fn der2key_export_object;
161 static struct der2key_ctx_st *
162 der2key_newctx(void *provctx, const struct keytype_desc_st *desc)
164 struct der2key_ctx_st *ctx = OPENSSL_zalloc(sizeof(*ctx));
167 ctx->provctx = provctx;
173 static void der2key_freectx(void *vctx)
175 struct der2key_ctx_st *ctx = vctx;
180 static const OSSL_PARAM *
181 der2key_gettable_params(void *provctx, const struct keytype_desc_st *desc)
183 static const OSSL_PARAM gettables[] = {
184 { OSSL_DECODER_PARAM_INPUT_TYPE, OSSL_PARAM_UTF8_PTR, NULL, 0, 0 },
187 static const OSSL_PARAM gettables_w_structure[] = {
188 { OSSL_DECODER_PARAM_INPUT_TYPE, OSSL_PARAM_UTF8_PTR, NULL, 0, 0 },
189 { OSSL_DECODER_PARAM_INPUT_STRUCTURE, OSSL_PARAM_UTF8_PTR, NULL, 0, 0 },
193 return desc->structure_name != NULL ? gettables_w_structure : gettables;
196 static int der2key_get_params(OSSL_PARAM params[],
197 const struct keytype_desc_st *desc)
201 p = OSSL_PARAM_locate(params, OSSL_DECODER_PARAM_INPUT_TYPE);
202 if (p != NULL && !OSSL_PARAM_set_utf8_ptr(p, "DER"))
204 if (desc->structure_name != NULL) {
205 p = OSSL_PARAM_locate(params, OSSL_DECODER_PARAM_INPUT_STRUCTURE);
206 if (p != NULL && !OSSL_PARAM_set_utf8_ptr(p, desc->structure_name))
213 static int der2key_check_selection(int selection,
214 const struct keytype_desc_st *desc)
217 * The selections are kinda sorta "levels", i.e. each selection given
218 * here is assumed to include those following.
221 OSSL_KEYMGMT_SELECT_PRIVATE_KEY,
222 OSSL_KEYMGMT_SELECT_PUBLIC_KEY,
223 OSSL_KEYMGMT_SELECT_ALL_PARAMETERS
227 /* The decoder implementations made here support guessing */
231 for (i = 0; i < OSSL_NELEM(checks); i++) {
232 int check1 = (selection & checks[i]) != 0;
233 int check2 = (desc->selection_mask & checks[i]) != 0;
236 * If the caller asked for the currently checked bit(s), return
237 * whether the decoder description says it's supported.
243 /* This should be dead code, but just to be safe... */
247 static int der2key_decode(void *vctx, OSSL_CORE_BIO *cin, int selection,
248 OSSL_CALLBACK *data_cb, void *data_cbarg,
249 OSSL_PASSPHRASE_CALLBACK *pw_cb, void *pw_cbarg)
251 struct der2key_ctx_st *ctx = vctx;
252 unsigned char *der = NULL;
253 const unsigned char *derp;
256 int orig_selection = selection;
260 * The caller is allowed to specify 0 as a selection mark, to have the
261 * structure and key type guessed. For type-specific structures, this
262 * is not recommended, as some structures are very similar.
263 * Note that 0 isn't the same as OSSL_KEYMGMT_SELECT_ALL, as the latter
264 * signifies a private key structure, where everything else is assumed
265 * to be present as well.
268 selection = ctx->desc->selection_mask;
269 if ((selection & ctx->desc->selection_mask) == 0) {
270 ERR_raise(ERR_LIB_PROV, ERR_R_PASSED_INVALID_ARGUMENT);
274 ok = read_der(ctx->provctx, cin, &der, &der_len);
278 ok = 0; /* Assume that we fail */
280 if ((selection & OSSL_KEYMGMT_SELECT_PRIVATE_KEY) != 0) {
282 if (ctx->desc->d2i_PKCS8 != NULL) {
283 key = ctx->desc->d2i_PKCS8(NULL, &derp, der_len, ctx,
287 } else if (ctx->desc->d2i_private_key != NULL) {
288 key = ctx->desc->d2i_private_key(NULL, &derp, der_len);
290 if (key == NULL && orig_selection != 0)
293 if (key == NULL && (selection & OSSL_KEYMGMT_SELECT_PUBLIC_KEY) != 0) {
295 if (ctx->desc->d2i_PUBKEY != NULL)
296 key = ctx->desc->d2i_PUBKEY(NULL, &derp, der_len);
298 key = ctx->desc->d2i_public_key(NULL, &derp, der_len);
299 if (key == NULL && orig_selection != 0)
302 if (key == NULL && (selection & OSSL_KEYMGMT_SELECT_ALL_PARAMETERS) != 0) {
304 if (ctx->desc->d2i_key_params != NULL)
305 key = ctx->desc->d2i_key_params(NULL, &derp, der_len);
306 if (key == NULL && orig_selection != 0)
311 * Last minute check to see if this was the correct type of key. This
312 * should never lead to a fatal error, i.e. the decoding itself was
313 * correct, it was just an unexpected key type. This is generally for
314 * classes of key types that have subtle variants, like RSA-PSS keys as
315 * opposed to plain RSA keys.
318 && ctx->desc->check_key != NULL
319 && !ctx->desc->check_key(key, ctx)) {
320 ctx->desc->free_key(key);
324 if (key != NULL && ctx->desc->adjust_key != NULL)
325 ctx->desc->adjust_key(key, ctx);
329 * Indicated that we successfully decoded something, or not at all.
330 * Ending up "empty handed" is not an error.
335 * We free memory here so it's not held up during the callback, because
336 * we know the process is recursive and the allocated chunks of memory
343 OSSL_PARAM params[4];
344 int object_type = OSSL_OBJECT_PKEY;
347 OSSL_PARAM_construct_int(OSSL_OBJECT_PARAM_TYPE, &object_type);
349 OSSL_PARAM_construct_utf8_string(OSSL_OBJECT_PARAM_DATA_TYPE,
350 (char *)ctx->desc->keytype_name,
352 /* The address of the key becomes the octet string */
354 OSSL_PARAM_construct_octet_string(OSSL_OBJECT_PARAM_REFERENCE,
356 params[3] = OSSL_PARAM_construct_end();
358 ok = data_cb(params, data_cbarg);
362 ctx->desc->free_key(key);
368 static int der2key_export_object(void *vctx,
369 const void *reference, size_t reference_sz,
370 OSSL_CALLBACK *export_cb, void *export_cbarg)
372 struct der2key_ctx_st *ctx = vctx;
373 OSSL_FUNC_keymgmt_export_fn *export =
374 ossl_prov_get_keymgmt_export(ctx->desc->fns);
377 if (reference_sz == sizeof(keydata) && export != NULL) {
378 /* The contents of the reference is the address to our object */
379 keydata = *(void **)reference;
381 return export(keydata, OSSL_KEYMGMT_SELECT_ALL,
382 export_cb, export_cbarg);
387 /* ---------------------------------------------------------------------- */
389 #ifndef OPENSSL_NO_DH
390 # define dh_evp_type EVP_PKEY_DH
391 # define dh_d2i_private_key NULL
392 # define dh_d2i_public_key NULL
393 # define dh_d2i_key_params (d2i_of_void *)d2i_DHparams
395 static void *dh_d2i_PKCS8(void **key, const unsigned char **der, long der_len,
396 struct der2key_ctx_st *ctx,
397 OSSL_PASSPHRASE_CALLBACK *pw_cb, void *pw_cbarg)
399 return der2key_decode_p8(der, der_len, ctx, pw_cb, pw_cbarg,
400 (key_from_pkcs8_t *)ossl_dh_key_from_pkcs8);
403 # define dh_d2i_PUBKEY (d2i_of_void *)ossl_d2i_DH_PUBKEY
404 # define dh_free (free_key_fn *)DH_free
405 # define dh_check NULL
407 static void dh_adjust(void *key, struct der2key_ctx_st *ctx)
409 ossl_dh_set0_libctx(key, PROV_LIBCTX_OF(ctx->provctx));
412 # define dhx_evp_type EVP_PKEY_DHX
413 # define dhx_d2i_private_key NULL
414 # define dhx_d2i_public_key NULL
415 # define dhx_d2i_key_params (d2i_of_void *)d2i_DHxparams
416 # define dhx_d2i_PKCS8 dh_d2i_PKCS8
417 # define dhx_d2i_PUBKEY (d2i_of_void *)ossl_d2i_DHx_PUBKEY
418 # define dhx_free (free_key_fn *)DH_free
419 # define dhx_check NULL
420 # define dhx_adjust dh_adjust
423 /* ---------------------------------------------------------------------- */
425 #ifndef OPENSSL_NO_DSA
426 # define dsa_evp_type EVP_PKEY_DSA
427 # define dsa_d2i_private_key (d2i_of_void *)d2i_DSAPrivateKey
428 # define dsa_d2i_public_key (d2i_of_void *)d2i_DSAPublicKey
429 # define dsa_d2i_key_params (d2i_of_void *)d2i_DSAparams
431 static void *dsa_d2i_PKCS8(void **key, const unsigned char **der, long der_len,
432 struct der2key_ctx_st *ctx,
433 OSSL_PASSPHRASE_CALLBACK *pw_cb, void *pw_cbarg)
435 return der2key_decode_p8(der, der_len, ctx, pw_cb, pw_cbarg,
436 (key_from_pkcs8_t *)ossl_dsa_key_from_pkcs8);
439 # define dsa_d2i_PUBKEY (d2i_of_void *)d2i_DSA_PUBKEY
440 # define dsa_free (free_key_fn *)DSA_free
441 # define dsa_check NULL
443 static void dsa_adjust(void *key, struct der2key_ctx_st *ctx)
445 ossl_dsa_set0_libctx(key, PROV_LIBCTX_OF(ctx->provctx));
449 /* ---------------------------------------------------------------------- */
451 #ifndef OPENSSL_NO_EC
452 # define ec_evp_type EVP_PKEY_EC
453 # define ec_d2i_private_key (d2i_of_void *)d2i_ECPrivateKey
454 # define ec_d2i_public_key NULL
455 # define ec_d2i_key_params (d2i_of_void *)d2i_ECParameters
457 static void *ec_d2i_PKCS8(void **key, const unsigned char **der, long der_len,
458 struct der2key_ctx_st *ctx,
459 OSSL_PASSPHRASE_CALLBACK *pw_cb, void *pw_cbarg)
461 return der2key_decode_p8(der, der_len, ctx, pw_cb, pw_cbarg,
462 (key_from_pkcs8_t *)ossl_ec_key_from_pkcs8);
465 # define ec_d2i_PUBKEY (d2i_of_void *)d2i_EC_PUBKEY
466 # define ec_free (free_key_fn *)EC_KEY_free
468 static int ec_check(void *key, struct der2key_ctx_st *ctx)
470 /* We're trying to be clever by comparing two truths */
472 int sm2 = (EC_KEY_get_flags(key) & EC_FLAG_SM2_RANGE) != 0;
474 return sm2 == (ctx->desc->evp_type == EVP_PKEY_SM2);
477 static void ec_adjust(void *key, struct der2key_ctx_st *ctx)
479 ossl_ec_key_set0_libctx(key, PROV_LIBCTX_OF(ctx->provctx));
483 * ED25519, ED448, X25519, X448 only implement PKCS#8 and SubjectPublicKeyInfo,
484 * so no d2i functions to be had.
487 static void *ecx_d2i_PKCS8(void **key, const unsigned char **der, long der_len,
488 struct der2key_ctx_st *ctx,
489 OSSL_PASSPHRASE_CALLBACK *pw_cb, void *pw_cbarg)
491 return der2key_decode_p8(der, der_len, ctx, pw_cb, pw_cbarg,
492 (key_from_pkcs8_t *)ossl_ecx_key_from_pkcs8);
495 static void ecx_key_adjust(void *key, struct der2key_ctx_st *ctx)
497 ossl_ecx_key_set0_libctx(key, PROV_LIBCTX_OF(ctx->provctx));
500 # define ed25519_evp_type EVP_PKEY_ED25519
501 # define ed25519_d2i_private_key NULL
502 # define ed25519_d2i_public_key NULL
503 # define ed25519_d2i_key_params NULL
504 # define ed25519_d2i_PKCS8 ecx_d2i_PKCS8
505 # define ed25519_d2i_PUBKEY (d2i_of_void *)ossl_d2i_ED25519_PUBKEY
506 # define ed25519_free (free_key_fn *)ossl_ecx_key_free
507 # define ed25519_check NULL
508 # define ed25519_adjust ecx_key_adjust
510 # define ed448_evp_type EVP_PKEY_ED448
511 # define ed448_d2i_private_key NULL
512 # define ed448_d2i_public_key NULL
513 # define ed448_d2i_key_params NULL
514 # define ed448_d2i_PKCS8 ecx_d2i_PKCS8
515 # define ed448_d2i_PUBKEY (d2i_of_void *)ossl_d2i_ED448_PUBKEY
516 # define ed448_free (free_key_fn *)ossl_ecx_key_free
517 # define ed448_check NULL
518 # define ed448_adjust ecx_key_adjust
520 # define x25519_evp_type EVP_PKEY_X25519
521 # define x25519_d2i_private_key NULL
522 # define x25519_d2i_public_key NULL
523 # define x25519_d2i_key_params NULL
524 # define x25519_d2i_PKCS8 ecx_d2i_PKCS8
525 # define x25519_d2i_PUBKEY (d2i_of_void *)ossl_d2i_X25519_PUBKEY
526 # define x25519_free (free_key_fn *)ossl_ecx_key_free
527 # define x25519_check NULL
528 # define x25519_adjust ecx_key_adjust
530 # define x448_evp_type EVP_PKEY_X448
531 # define x448_d2i_private_key NULL
532 # define x448_d2i_public_key NULL
533 # define x448_d2i_key_params NULL
534 # define x448_d2i_PKCS8 ecx_d2i_PKCS8
535 # define x448_d2i_PUBKEY (d2i_of_void *)ossl_d2i_X448_PUBKEY
536 # define x448_free (free_key_fn *)ossl_ecx_key_free
537 # define x448_check NULL
538 # define x448_adjust ecx_key_adjust
540 # ifndef OPENSSL_NO_SM2
541 # define sm2_evp_type EVP_PKEY_SM2
542 # define sm2_d2i_private_key (d2i_of_void *)d2i_ECPrivateKey
543 # define sm2_d2i_public_key NULL
544 # define sm2_d2i_key_params (d2i_of_void *)d2i_ECParameters
546 static void *sm2_d2i_PKCS8(void **key, const unsigned char **der, long der_len,
547 struct der2key_ctx_st *ctx,
548 OSSL_PASSPHRASE_CALLBACK *pw_cb, void *pw_cbarg)
550 return der2key_decode_p8(der, der_len, ctx, pw_cb, pw_cbarg,
551 (key_from_pkcs8_t *)ossl_ec_key_from_pkcs8);
554 # define sm2_d2i_PUBKEY (d2i_of_void *)d2i_EC_PUBKEY
555 # define sm2_free (free_key_fn *)EC_KEY_free
556 # define sm2_check ec_check
557 # define sm2_adjust ec_adjust
561 /* ---------------------------------------------------------------------- */
563 #define rsa_evp_type EVP_PKEY_RSA
564 #define rsa_d2i_private_key (d2i_of_void *)d2i_RSAPrivateKey
565 #define rsa_d2i_public_key (d2i_of_void *)d2i_RSAPublicKey
566 #define rsa_d2i_key_params NULL
568 static void *rsa_d2i_PKCS8(void **key, const unsigned char **der, long der_len,
569 struct der2key_ctx_st *ctx,
570 OSSL_PASSPHRASE_CALLBACK *pw_cb, void *pw_cbarg)
572 return der2key_decode_p8(der, der_len, ctx, pw_cb, pw_cbarg,
573 (key_from_pkcs8_t *)ossl_rsa_key_from_pkcs8);
576 #define rsa_d2i_PUBKEY (d2i_of_void *)d2i_RSA_PUBKEY
577 #define rsa_free (free_key_fn *)RSA_free
579 static int rsa_check(void *key, struct der2key_ctx_st *ctx)
581 switch (RSA_test_flags(key, RSA_FLAG_TYPE_MASK)) {
582 case RSA_FLAG_TYPE_RSA:
583 return ctx->desc->evp_type == EVP_PKEY_RSA;
584 case RSA_FLAG_TYPE_RSASSAPSS:
585 return ctx->desc->evp_type == EVP_PKEY_RSA_PSS;
588 /* Currently unsupported RSA key type */
592 static void rsa_adjust(void *key, struct der2key_ctx_st *ctx)
594 ossl_rsa_set0_libctx(key, PROV_LIBCTX_OF(ctx->provctx));
597 #define rsapss_evp_type EVP_PKEY_RSA_PSS
598 #define rsapss_d2i_private_key (d2i_of_void *)d2i_RSAPrivateKey
599 #define rsapss_d2i_public_key (d2i_of_void *)d2i_RSAPublicKey
600 #define rsapss_d2i_key_params NULL
601 #define rsapss_d2i_PKCS8 rsa_d2i_PKCS8
602 #define rsapss_d2i_PUBKEY (d2i_of_void *)d2i_RSA_PUBKEY
603 #define rsapss_free (free_key_fn *)RSA_free
604 #define rsapss_check rsa_check
605 #define rsapss_adjust rsa_adjust
607 /* ---------------------------------------------------------------------- */
610 * The DO_ macros help define the selection mask and the method functions
611 * for each kind of object we want to decode.
613 #define DO_type_specific_keypair(keytype) \
614 "type-specific", keytype##_evp_type, \
615 ( OSSL_KEYMGMT_SELECT_KEYPAIR ), \
616 keytype##_d2i_private_key, \
617 keytype##_d2i_public_key, \
625 #define DO_type_specific_pub(keytype) \
626 "type-specific", keytype##_evp_type, \
627 ( OSSL_KEYMGMT_SELECT_PUBLIC_KEY ), \
629 keytype##_d2i_public_key, \
637 #define DO_type_specific_priv(keytype) \
638 "type-specific", keytype##_evp_type, \
639 ( OSSL_KEYMGMT_SELECT_PRIVATE_KEY ), \
640 keytype##_d2i_private_key, \
649 #define DO_type_specific_params(keytype) \
650 "type-specific", keytype##_evp_type, \
651 ( OSSL_KEYMGMT_SELECT_ALL_PARAMETERS ), \
654 keytype##_d2i_key_params, \
661 #define DO_type_specific(keytype) \
662 "type-specific", keytype##_evp_type, \
663 ( OSSL_KEYMGMT_SELECT_ALL ), \
664 keytype##_d2i_private_key, \
665 keytype##_d2i_public_key, \
666 keytype##_d2i_key_params, \
673 #define DO_type_specific_no_pub(keytype) \
674 "type-specific", keytype##_evp_type, \
675 ( OSSL_KEYMGMT_SELECT_PRIVATE_KEY \
676 | OSSL_KEYMGMT_SELECT_ALL_PARAMETERS ), \
677 keytype##_d2i_private_key, \
679 keytype##_d2i_key_params, \
686 #define DO_PKCS8(keytype) \
687 "pkcs8", keytype##_evp_type, \
688 ( OSSL_KEYMGMT_SELECT_PRIVATE_KEY ), \
692 keytype##_d2i_PKCS8, \
698 #define DO_SubjectPublicKeyInfo(keytype) \
699 "SubjectPublicKeyInfo", keytype##_evp_type, \
700 ( OSSL_KEYMGMT_SELECT_PUBLIC_KEY ), \
705 keytype##_d2i_PUBKEY, \
710 #define DO_DH(keytype) \
711 "DH", keytype##_evp_type, \
712 ( OSSL_KEYMGMT_SELECT_ALL_PARAMETERS ), \
715 keytype##_d2i_key_params, \
722 #define DO_DHX(keytype) \
723 "DHX", keytype##_evp_type, \
724 ( OSSL_KEYMGMT_SELECT_ALL_PARAMETERS ), \
727 keytype##_d2i_key_params, \
734 #define DO_DSA(keytype) \
735 "DSA", keytype##_evp_type, \
736 ( OSSL_KEYMGMT_SELECT_ALL ), \
737 keytype##_d2i_private_key, \
738 keytype##_d2i_public_key, \
739 keytype##_d2i_key_params, \
746 #define DO_EC(keytype) \
747 "EC", keytype##_evp_type, \
748 ( OSSL_KEYMGMT_SELECT_PRIVATE_KEY \
749 | OSSL_KEYMGMT_SELECT_ALL_PARAMETERS ), \
750 keytype##_d2i_private_key, \
752 keytype##_d2i_key_params, \
759 #define DO_RSA(keytype) \
760 "RSA", keytype##_evp_type, \
761 ( OSSL_KEYMGMT_SELECT_KEYPAIR ), \
762 keytype##_d2i_private_key, \
763 keytype##_d2i_public_key, \
772 * MAKE_DECODER is the single driver for creating OSSL_DISPATCH tables.
773 * It takes the following arguments:
775 * keytype_name The implementation key type as a string.
776 * keytype The implementation key type. This must correspond exactly
777 * to our existing keymgmt keytype names... in other words,
778 * there must exist an ossl_##keytype##_keymgmt_functions.
779 * type The type name for the set of functions that implement the
780 * decoder for the key type. This isn't necessarily the same
781 * as keytype. For example, the key types ed25519, ed448,
782 * x25519 and x448 are all handled by the same functions with
783 * the common type name ecx.
784 * kind The kind of support to implement. This translates into
785 * the DO_##kind macros above, to populate the keytype_desc_st
788 #define MAKE_DECODER(keytype_name, keytype, type, kind) \
789 static const struct keytype_desc_st kind##_##keytype##_desc = \
790 { keytype_name, ossl_##keytype##_keymgmt_functions, \
791 DO_##kind(keytype) }; \
793 static OSSL_FUNC_decoder_newctx_fn kind##_der2##keytype##_newctx; \
794 static OSSL_FUNC_decoder_gettable_params_fn \
795 kind##_der2##keytype##_gettable_params; \
796 static OSSL_FUNC_decoder_get_params_fn \
797 kind##_der2##keytype##_get_params; \
799 static void *kind##_der2##keytype##_newctx(void *provctx) \
801 return der2key_newctx(provctx, &kind##_##keytype##_desc); \
803 static const OSSL_PARAM * \
804 kind##_der2##keytype##_gettable_params(void *provctx) \
807 der2key_gettable_params(provctx, &kind##_##keytype##_desc); \
809 static int kind##_der2##keytype##_get_params(OSSL_PARAM params[]) \
811 return der2key_get_params(params, &kind##_##keytype##_desc); \
813 static int kind##_der2##keytype##_does_selection(void *provctx, \
816 return der2key_check_selection(selection, \
817 &kind##_##keytype##_desc); \
819 const OSSL_DISPATCH \
820 ossl_##kind##_der_to_##keytype##_decoder_functions[] = { \
821 { OSSL_FUNC_DECODER_NEWCTX, \
822 (void (*)(void))kind##_der2##keytype##_newctx }, \
823 { OSSL_FUNC_DECODER_FREECTX, \
824 (void (*)(void))der2key_freectx }, \
825 { OSSL_FUNC_DECODER_GETTABLE_PARAMS, \
826 (void (*)(void))kind##_der2##keytype##_gettable_params }, \
827 { OSSL_FUNC_DECODER_GET_PARAMS, \
828 (void (*)(void))kind##_der2##keytype##_get_params }, \
829 { OSSL_FUNC_DECODER_DOES_SELECTION, \
830 (void (*)(void))kind##_der2##keytype##_does_selection }, \
831 { OSSL_FUNC_DECODER_DECODE, \
832 (void (*)(void))der2key_decode }, \
833 { OSSL_FUNC_DECODER_EXPORT_OBJECT, \
834 (void (*)(void))der2key_export_object }, \
838 #ifndef OPENSSL_NO_DH
839 MAKE_DECODER("DH", dh, dh, PKCS8);
840 MAKE_DECODER("DH", dh, dh, SubjectPublicKeyInfo);
841 MAKE_DECODER("DH", dh, dh, type_specific_params);
842 MAKE_DECODER("DH", dh, dh, DH);
843 MAKE_DECODER("DHX", dhx, dhx, PKCS8);
844 MAKE_DECODER("DHX", dhx, dhx, SubjectPublicKeyInfo);
845 MAKE_DECODER("DHX", dhx, dhx, type_specific_params);
846 MAKE_DECODER("DHX", dhx, dhx, DHX);
848 #ifndef OPENSSL_NO_DSA
849 MAKE_DECODER("DSA", dsa, dsa, PKCS8);
850 MAKE_DECODER("DSA", dsa, dsa, SubjectPublicKeyInfo);
851 MAKE_DECODER("DSA", dsa, dsa, type_specific);
852 MAKE_DECODER("DSA", dsa, dsa, DSA);
854 #ifndef OPENSSL_NO_EC
855 MAKE_DECODER("EC", ec, ec, PKCS8);
856 MAKE_DECODER("EC", ec, ec, SubjectPublicKeyInfo);
857 MAKE_DECODER("EC", ec, ec, type_specific_no_pub);
858 MAKE_DECODER("EC", ec, ec, EC);
859 MAKE_DECODER("X25519", x25519, ecx, PKCS8);
860 MAKE_DECODER("X25519", x25519, ecx, SubjectPublicKeyInfo);
861 MAKE_DECODER("X448", x448, ecx, PKCS8);
862 MAKE_DECODER("X448", x448, ecx, SubjectPublicKeyInfo);
863 MAKE_DECODER("ED25519", ed25519, ecx, PKCS8);
864 MAKE_DECODER("ED25519", ed25519, ecx, SubjectPublicKeyInfo);
865 MAKE_DECODER("ED448", ed448, ecx, PKCS8);
866 MAKE_DECODER("ED448", ed448, ecx, SubjectPublicKeyInfo);
867 # ifndef OPENSSL_NO_SM2
868 MAKE_DECODER("SM2", sm2, ec, PKCS8);
869 MAKE_DECODER("SM2", sm2, ec, SubjectPublicKeyInfo);
872 MAKE_DECODER("RSA", rsa, rsa, PKCS8);
873 MAKE_DECODER("RSA", rsa, rsa, SubjectPublicKeyInfo);
874 MAKE_DECODER("RSA", rsa, rsa, type_specific_keypair);
875 MAKE_DECODER("RSA", rsa, rsa, RSA);
876 MAKE_DECODER("RSA-PSS", rsapss, rsapss, PKCS8);
877 MAKE_DECODER("RSA-PSS", rsapss, rsapss, SubjectPublicKeyInfo);