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 internal use.
13 #include "internal/deprecated.h"
15 #include <openssl/core.h>
16 #include <openssl/core_dispatch.h>
17 #include <openssl/core_names.h>
18 #include <openssl/crypto.h>
19 #include <openssl/params.h>
20 #include <openssl/asn1.h>
21 #include <openssl/err.h>
22 #include <openssl/pem.h>
23 #include <openssl/x509.h>
24 #include <openssl/pkcs12.h> /* PKCS8_encrypt() */
25 #include <openssl/dh.h>
26 #include <openssl/dsa.h>
27 #include <openssl/ec.h>
28 #include <openssl/proverr.h>
29 #include "internal/passphrase.h"
30 #include "internal/cryptlib.h"
31 #include "crypto/ecx.h"
32 #include "crypto/rsa.h"
33 #include "prov/implementations.h"
35 #include "prov/provider_ctx.h"
36 #include "prov/der_rsa.h"
37 #include "endecoder_local.h"
39 #if defined(OPENSSL_NO_DH) && defined(OPENSSL_NO_DSA) && defined(OPENSSL_NO_EC)
40 # define OPENSSL_NO_KEYPARAMS
43 struct key2any_ctx_st {
46 /* Set to 0 if parameters should not be saved (dsa only) */
49 /* Set to 1 if intending to encrypt/decrypt, otherwise 0 */
54 struct ossl_passphrase_data_st pwdata;
57 typedef int check_key_type_fn(const void *key, int nid);
58 typedef int key_to_paramstring_fn(const void *key, int nid, int save,
59 void **str, int *strtype);
60 typedef int key_to_der_fn(BIO *out, const void *key,
61 int key_nid, const char *pemname,
62 key_to_paramstring_fn *p2s, i2d_of_void *k2d,
63 struct key2any_ctx_st *ctx);
64 typedef int write_bio_of_void_fn(BIO *bp, const void *x);
67 /* Free the blob allocated during key_to_paramstring_fn */
68 static void free_asn1_data(int type, void *data)
72 ASN1_OBJECT_free(data);
75 ASN1_STRING_free(data);
80 static PKCS8_PRIV_KEY_INFO *key_to_p8info(const void *key, int key_nid,
81 void *params, int params_type,
84 /* der, derlen store the key DER output and its length */
85 unsigned char *der = NULL;
87 /* The final PKCS#8 info */
88 PKCS8_PRIV_KEY_INFO *p8info = NULL;
90 if ((p8info = PKCS8_PRIV_KEY_INFO_new()) == NULL
91 || (derlen = k2d(key, &der)) <= 0
92 || !PKCS8_pkey_set0(p8info, OBJ_nid2obj(key_nid), 0,
93 params_type, params, der, derlen)) {
94 ERR_raise(ERR_LIB_PROV, ERR_R_MALLOC_FAILURE);
95 PKCS8_PRIV_KEY_INFO_free(p8info);
103 static X509_SIG *p8info_to_encp8(PKCS8_PRIV_KEY_INFO *p8info,
104 struct key2any_ctx_st *ctx)
107 char kstr[PEM_BUFSIZE];
109 OSSL_LIB_CTX *libctx = PROV_LIBCTX_OF(ctx->provctx);
111 if (ctx->cipher == NULL)
114 if (!ossl_pw_get_passphrase(kstr, sizeof(kstr), &klen, NULL, 1,
116 ERR_raise(ERR_LIB_PROV, PROV_R_UNABLE_TO_GET_PASSPHRASE);
119 /* First argument == -1 means "standard" */
120 p8 = PKCS8_encrypt_ex(-1, ctx->cipher, kstr, klen, NULL, 0, 0, p8info, libctx, NULL);
121 OPENSSL_cleanse(kstr, klen);
125 static X509_SIG *key_to_encp8(const void *key, int key_nid,
126 void *params, int params_type,
127 i2d_of_void *k2d, struct key2any_ctx_st *ctx)
129 PKCS8_PRIV_KEY_INFO *p8info =
130 key_to_p8info(key, key_nid, params, params_type, k2d);
133 if (p8info == NULL) {
134 free_asn1_data(params_type, params);
136 p8 = p8info_to_encp8(p8info, ctx);
137 PKCS8_PRIV_KEY_INFO_free(p8info);
142 static X509_PUBKEY *key_to_pubkey(const void *key, int key_nid,
143 void *params, int params_type,
146 /* der, derlen store the key DER output and its length */
147 unsigned char *der = NULL;
149 /* The final X509_PUBKEY */
150 X509_PUBKEY *xpk = NULL;
153 if ((xpk = X509_PUBKEY_new()) == NULL
154 || (derlen = k2d(key, &der)) <= 0
155 || !X509_PUBKEY_set0_param(xpk, OBJ_nid2obj(key_nid),
156 params_type, params, der, derlen)) {
157 ERR_raise(ERR_LIB_PROV, ERR_R_MALLOC_FAILURE);
158 X509_PUBKEY_free(xpk);
167 * key_to_epki_* produce encoded output with the private key data in a
168 * EncryptedPrivateKeyInfo structure (defined by PKCS#8). They require
169 * that there's an intent to encrypt, anything else is an error.
170 * They are currently only called from the corresponding key_to_pki_ function.
172 * key_to_pki_* primarly produce encoded output with the private key data
173 * in a PrivateKeyInfo structure (also defined by PKCS#8). However, if
174 * there is an intent to encrypt the data, the corresponding key_to_epki_*
175 * function is used instead.
177 * key_to_spki_* produce encoded output with the public key data in an
178 * X.509 SubjectPublicKeyInfo.
180 * Key parameters don't have any defined envelopment of this kind, but are
181 * included in some manner in the output from the functions described above,
182 * either in the AlgorithmIdentifier's parameter field, or as part of the
186 static int key_to_epki_der_priv_bio(BIO *out, const void *key,
188 ossl_unused const char *pemname,
189 key_to_paramstring_fn *p2s,
191 struct key2any_ctx_st *ctx)
195 int strtype = V_ASN1_UNDEF;
198 if (!ctx->cipher_intent)
201 if (p2s != NULL && !p2s(key, key_nid, ctx->save_parameters,
205 p8 = key_to_encp8(key, key_nid, str, strtype, k2d, ctx);
207 ret = i2d_PKCS8_bio(out, p8);
214 static int key_to_epki_pem_priv_bio(BIO *out, const void *key,
216 ossl_unused const char *pemname,
217 key_to_paramstring_fn *p2s,
219 struct key2any_ctx_st *ctx)
223 int strtype = V_ASN1_UNDEF;
226 if (!ctx->cipher_intent)
229 if (p2s != NULL && !p2s(key, key_nid, ctx->save_parameters,
233 p8 = key_to_encp8(key, key_nid, str, strtype, k2d, ctx);
235 ret = PEM_write_bio_PKCS8(out, p8);
242 static int key_to_pki_der_priv_bio(BIO *out, const void *key,
244 ossl_unused const char *pemname,
245 key_to_paramstring_fn *p2s,
247 struct key2any_ctx_st *ctx)
251 int strtype = V_ASN1_UNDEF;
252 PKCS8_PRIV_KEY_INFO *p8info;
254 if (ctx->cipher_intent)
255 return key_to_epki_der_priv_bio(out, key, key_nid, pemname,
258 if (p2s != NULL && !p2s(key, key_nid, ctx->save_parameters,
262 p8info = key_to_p8info(key, key_nid, str, strtype, k2d);
265 ret = i2d_PKCS8_PRIV_KEY_INFO_bio(out, p8info);
267 free_asn1_data(strtype, str);
269 PKCS8_PRIV_KEY_INFO_free(p8info);
274 static int key_to_pki_pem_priv_bio(BIO *out, const void *key,
276 ossl_unused const char *pemname,
277 key_to_paramstring_fn *p2s,
279 struct key2any_ctx_st *ctx)
283 int strtype = V_ASN1_UNDEF;
284 PKCS8_PRIV_KEY_INFO *p8info;
286 if (ctx->cipher_intent)
287 return key_to_epki_pem_priv_bio(out, key, key_nid, pemname,
290 if (p2s != NULL && !p2s(key, key_nid, ctx->save_parameters,
294 p8info = key_to_p8info(key, key_nid, str, strtype, k2d);
297 ret = PEM_write_bio_PKCS8_PRIV_KEY_INFO(out, p8info);
299 free_asn1_data(strtype, str);
301 PKCS8_PRIV_KEY_INFO_free(p8info);
306 static int key_to_spki_der_pub_bio(BIO *out, const void *key,
308 ossl_unused const char *pemname,
309 key_to_paramstring_fn *p2s,
311 struct key2any_ctx_st *ctx)
315 int strtype = V_ASN1_UNDEF;
316 X509_PUBKEY *xpk = NULL;
318 if (p2s != NULL && !p2s(key, key_nid, ctx->save_parameters,
322 xpk = key_to_pubkey(key, key_nid, str, strtype, k2d);
325 ret = i2d_X509_PUBKEY_bio(out, xpk);
327 /* Also frees |str| */
328 X509_PUBKEY_free(xpk);
332 static int key_to_spki_pem_pub_bio(BIO *out, const void *key,
334 ossl_unused const char *pemname,
335 key_to_paramstring_fn *p2s,
337 struct key2any_ctx_st *ctx)
341 int strtype = V_ASN1_UNDEF;
342 X509_PUBKEY *xpk = NULL;
344 if (p2s != NULL && !p2s(key, key_nid, ctx->save_parameters,
348 xpk = key_to_pubkey(key, key_nid, str, strtype, k2d);
351 ret = PEM_write_bio_X509_PUBKEY(out, xpk);
353 free_asn1_data(strtype, str);
355 /* Also frees |str| */
356 X509_PUBKEY_free(xpk);
361 * key_to_type_specific_* produce encoded output with type specific key data,
362 * no envelopment; the same kind of output as the type specific i2d_ and
363 * PEM_write_ functions, which is often a simple SEQUENCE of INTEGER.
365 * OpenSSL tries to discourage production of new keys in this form, because
366 * of the ambiguity when trying to recognise them, but can't deny that PKCS#1
367 * et al still are live standards.
369 * Note that these functions completely ignore p2s, and rather rely entirely
370 * on k2d to do the complete work.
372 static int key_to_type_specific_der_bio(BIO *out, const void *key,
374 ossl_unused const char *pemname,
375 key_to_paramstring_fn *p2s,
377 struct key2any_ctx_st *ctx)
379 unsigned char *der = NULL;
383 if ((derlen = k2d(key, &der)) <= 0) {
384 ERR_raise(ERR_LIB_PROV, ERR_R_MALLOC_FAILURE);
388 ret = BIO_write(out, der, derlen);
392 #define key_to_type_specific_der_priv_bio key_to_type_specific_der_bio
393 #define key_to_type_specific_der_pub_bio key_to_type_specific_der_bio
394 #define key_to_type_specific_der_param_bio key_to_type_specific_der_bio
396 static int key_to_type_specific_pem_bio_cb(BIO *out, const void *key,
397 int key_nid, const char *pemname,
398 key_to_paramstring_fn *p2s,
400 struct key2any_ctx_st *ctx,
401 pem_password_cb *cb, void *cbarg)
404 PEM_ASN1_write_bio(k2d, pemname, out, key, ctx->cipher,
405 NULL, 0, ossl_pw_pem_password, &ctx->pwdata) > 0;
408 static int key_to_type_specific_pem_priv_bio(BIO *out, const void *key,
409 int key_nid, const char *pemname,
410 key_to_paramstring_fn *p2s,
412 struct key2any_ctx_st *ctx)
414 return key_to_type_specific_pem_bio_cb(out, key, key_nid, pemname,
416 ossl_pw_pem_password, &ctx->pwdata);
419 static int key_to_type_specific_pem_pub_bio(BIO *out, const void *key,
420 int key_nid, const char *pemname,
421 key_to_paramstring_fn *p2s,
423 struct key2any_ctx_st *ctx)
425 return key_to_type_specific_pem_bio_cb(out, key, key_nid, pemname,
426 p2s, k2d, ctx, NULL, NULL);
429 #ifndef OPENSSL_NO_KEYPARAMS
430 static int key_to_type_specific_pem_param_bio(BIO *out, const void *key,
431 int key_nid, const char *pemname,
432 key_to_paramstring_fn *p2s,
434 struct key2any_ctx_st *ctx)
436 return key_to_type_specific_pem_bio_cb(out, key, key_nid, pemname,
437 p2s, k2d, ctx, NULL, NULL);
441 /* ---------------------------------------------------------------------- */
443 #ifndef OPENSSL_NO_DH
444 static int prepare_dh_params(const void *dh, int nid, int save,
445 void **pstr, int *pstrtype)
447 ASN1_STRING *params = ASN1_STRING_new();
449 if (params == NULL) {
450 ERR_raise(ERR_LIB_PROV, ERR_R_MALLOC_FAILURE);
454 if (nid == EVP_PKEY_DHX)
455 params->length = i2d_DHxparams(dh, ¶ms->data);
457 params->length = i2d_DHparams(dh, ¶ms->data);
459 if (params->length <= 0) {
460 ERR_raise(ERR_LIB_PROV, ERR_R_MALLOC_FAILURE);
461 ASN1_STRING_free(params);
464 params->type = V_ASN1_SEQUENCE;
467 *pstrtype = V_ASN1_SEQUENCE;
471 static int dh_spki_pub_to_der(const void *dh, unsigned char **pder)
473 const BIGNUM *bn = NULL;
474 ASN1_INTEGER *pub_key = NULL;
477 if ((bn = DH_get0_pub_key(dh)) == NULL) {
478 ERR_raise(ERR_LIB_PROV, PROV_R_NOT_A_PUBLIC_KEY);
481 if ((pub_key = BN_to_ASN1_INTEGER(bn, NULL)) == NULL) {
482 ERR_raise(ERR_LIB_PROV, PROV_R_BN_ERROR);
486 ret = i2d_ASN1_INTEGER(pub_key, pder);
488 ASN1_STRING_clear_free(pub_key);
492 static int dh_pki_priv_to_der(const void *dh, unsigned char **pder)
494 const BIGNUM *bn = NULL;
495 ASN1_INTEGER *priv_key = NULL;
498 if ((bn = DH_get0_priv_key(dh)) == NULL) {
499 ERR_raise(ERR_LIB_PROV, PROV_R_NOT_A_PRIVATE_KEY);
502 if ((priv_key = BN_to_ASN1_INTEGER(bn, NULL)) == NULL) {
503 ERR_raise(ERR_LIB_PROV, PROV_R_BN_ERROR);
507 ret = i2d_ASN1_INTEGER(priv_key, pder);
509 ASN1_STRING_clear_free(priv_key);
513 static int dh_type_specific_params_to_der(const void *dh, unsigned char **pder)
515 if (DH_test_flags(dh, DH_FLAG_TYPE_DHX))
516 return i2d_DHxparams(dh, pder);
517 return i2d_DHparams(dh, pder);
521 * DH doesn't have i2d_DHPrivateKey or i2d_DHPublicKey, so we can't make
522 * corresponding functions here.
524 # define dh_type_specific_priv_to_der NULL
525 # define dh_type_specific_pub_to_der NULL
527 static int dh_check_key_type(const void *dh, int expected_type)
530 DH_test_flags(dh, DH_FLAG_TYPE_DHX) ? EVP_PKEY_DHX : EVP_PKEY_DH;
532 return type == expected_type;
535 # define dh_evp_type EVP_PKEY_DH
536 # define dhx_evp_type EVP_PKEY_DHX
537 # define dh_input_type "DH"
538 # define dhx_input_type "DHX"
539 # define dh_pem_type "DH"
540 # define dhx_pem_type "X9.42 DH"
543 /* ---------------------------------------------------------------------- */
545 #ifndef OPENSSL_NO_DSA
546 static int encode_dsa_params(const void *dsa, int nid,
547 void **pstr, int *pstrtype)
549 ASN1_STRING *params = ASN1_STRING_new();
551 if (params == NULL) {
552 ERR_raise(ERR_LIB_PROV, ERR_R_MALLOC_FAILURE);
556 params->length = i2d_DSAparams(dsa, ¶ms->data);
558 if (params->length <= 0) {
559 ERR_raise(ERR_LIB_PROV, ERR_R_MALLOC_FAILURE);
560 ASN1_STRING_free(params);
564 *pstrtype = V_ASN1_SEQUENCE;
569 static int prepare_dsa_params(const void *dsa, int nid, int save,
570 void **pstr, int *pstrtype)
572 const BIGNUM *p = DSA_get0_p(dsa);
573 const BIGNUM *q = DSA_get0_q(dsa);
574 const BIGNUM *g = DSA_get0_g(dsa);
576 if (save && p != NULL && q != NULL && g != NULL)
577 return encode_dsa_params(dsa, nid, pstr, pstrtype);
580 *pstrtype = V_ASN1_UNDEF;
584 static int dsa_spki_pub_to_der(const void *dsa, unsigned char **pder)
586 const BIGNUM *bn = NULL;
587 ASN1_INTEGER *pub_key = NULL;
590 if ((bn = DSA_get0_pub_key(dsa)) == NULL) {
591 ERR_raise(ERR_LIB_PROV, PROV_R_NOT_A_PUBLIC_KEY);
594 if ((pub_key = BN_to_ASN1_INTEGER(bn, NULL)) == NULL) {
595 ERR_raise(ERR_LIB_PROV, PROV_R_BN_ERROR);
599 ret = i2d_ASN1_INTEGER(pub_key, pder);
601 ASN1_STRING_clear_free(pub_key);
605 static int dsa_pki_priv_to_der(const void *dsa, unsigned char **pder)
607 const BIGNUM *bn = NULL;
608 ASN1_INTEGER *priv_key = NULL;
611 if ((bn = DSA_get0_priv_key(dsa)) == NULL) {
612 ERR_raise(ERR_LIB_PROV, PROV_R_NOT_A_PRIVATE_KEY);
615 if ((priv_key = BN_to_ASN1_INTEGER(bn, NULL)) == NULL) {
616 ERR_raise(ERR_LIB_PROV, PROV_R_BN_ERROR);
620 ret = i2d_ASN1_INTEGER(priv_key, pder);
622 ASN1_STRING_clear_free(priv_key);
626 # define dsa_type_specific_priv_to_der (i2d_of_void *)i2d_DSAPrivateKey
627 # define dsa_type_specific_pub_to_der (i2d_of_void *)i2d_DSAPublicKey
628 # define dsa_type_specific_params_to_der (i2d_of_void *)i2d_DSAparams
630 # define dsa_check_key_type NULL
631 # define dsa_evp_type EVP_PKEY_DSA
632 # define dsa_input_type "DSA"
633 # define dsa_pem_type "DSA"
636 /* ---------------------------------------------------------------------- */
638 #ifndef OPENSSL_NO_EC
639 static int prepare_ec_explicit_params(const void *eckey,
640 void **pstr, int *pstrtype)
642 ASN1_STRING *params = ASN1_STRING_new();
644 if (params == NULL) {
645 ERR_raise(ERR_LIB_PROV, ERR_R_MALLOC_FAILURE);
649 params->length = i2d_ECParameters(eckey, ¶ms->data);
650 if (params->length <= 0) {
651 ERR_raise(ERR_LIB_PROV, ERR_R_MALLOC_FAILURE);
652 ASN1_STRING_free(params);
656 *pstrtype = V_ASN1_SEQUENCE;
662 * This implements EcpkParameters, where the CHOICE is based on whether there
663 * is a curve name (curve nid) to be found or not. See RFC 3279 for details.
665 static int prepare_ec_params(const void *eckey, int nid, int save,
666 void **pstr, int *pstrtype)
669 const EC_GROUP *group = EC_KEY_get0_group(eckey);
670 ASN1_OBJECT *params = NULL;
674 curve_nid = EC_GROUP_get_curve_name(group);
675 if (curve_nid != NID_undef) {
676 params = OBJ_nid2obj(curve_nid);
681 if (curve_nid != NID_undef
682 && (EC_GROUP_get_asn1_flag(group) & OPENSSL_EC_NAMED_CURVE)) {
683 /* The CHOICE came to namedCurve */
684 if (OBJ_length(params) == 0) {
685 /* Some curves might not have an associated OID */
686 ERR_raise(ERR_LIB_PROV, PROV_R_MISSING_OID);
687 ASN1_OBJECT_free(params);
691 *pstrtype = V_ASN1_OBJECT;
694 /* The CHOICE came to ecParameters */
695 return prepare_ec_explicit_params(eckey, pstr, pstrtype);
699 static int ec_spki_pub_to_der(const void *eckey, unsigned char **pder)
701 return i2o_ECPublicKey(eckey, pder);
704 static int ec_pki_priv_to_der(const void *veckey, unsigned char **pder)
706 EC_KEY *eckey = (EC_KEY *)veckey;
707 unsigned int old_flags;
711 * For PKCS8 the curve name appears in the PKCS8_PRIV_KEY_INFO object
712 * as the pkeyalg->parameter field. (For a named curve this is an OID)
713 * The pkey field is an octet string that holds the encoded
714 * ECPrivateKey SEQUENCE with the optional parameters field omitted.
715 * We omit this by setting the EC_PKEY_NO_PARAMETERS flag.
717 old_flags = EC_KEY_get_enc_flags(eckey); /* save old flags */
718 EC_KEY_set_enc_flags(eckey, old_flags | EC_PKEY_NO_PARAMETERS);
719 ret = i2d_ECPrivateKey(eckey, pder);
720 EC_KEY_set_enc_flags(eckey, old_flags); /* restore old flags */
721 return ret; /* return the length of the der encoded data */
724 # define ec_type_specific_params_to_der (i2d_of_void *)i2d_ECParameters
725 # define ec_type_specific_pub_to_der (i2d_of_void *)i2o_ECPublicKey
726 # define ec_type_specific_priv_to_der (i2d_of_void *)i2d_ECPrivateKey
728 # define ec_check_key_type NULL
729 # define ec_evp_type EVP_PKEY_EC
730 # define ec_input_type "EC"
731 # define ec_pem_type "EC"
733 # ifndef OPENSSL_NO_SM2
734 # define sm2_evp_type EVP_PKEY_SM2
735 # define sm2_input_type "SM2"
736 # define sm2_pem_type "SM2"
740 /* ---------------------------------------------------------------------- */
742 #ifndef OPENSSL_NO_EC
743 # define prepare_ecx_params NULL
745 static int ecx_spki_pub_to_der(const void *vecxkey, unsigned char **pder)
747 const ECX_KEY *ecxkey = vecxkey;
748 unsigned char *keyblob;
750 if (ecxkey == NULL) {
751 ERR_raise(ERR_LIB_PROV, ERR_R_PASSED_NULL_PARAMETER);
755 keyblob = OPENSSL_memdup(ecxkey->pubkey, ecxkey->keylen);
756 if (keyblob == NULL) {
757 ERR_raise(ERR_LIB_PROV, ERR_R_MALLOC_FAILURE);
762 return ecxkey->keylen;
765 static int ecx_pki_priv_to_der(const void *vecxkey, unsigned char **pder)
767 const ECX_KEY *ecxkey = vecxkey;
768 ASN1_OCTET_STRING oct;
771 if (ecxkey == NULL || ecxkey->privkey == NULL) {
772 ERR_raise(ERR_LIB_PROV, ERR_R_PASSED_NULL_PARAMETER);
776 oct.data = ecxkey->privkey;
777 oct.length = ecxkey->keylen;
780 keybloblen = i2d_ASN1_OCTET_STRING(&oct, pder);
781 if (keybloblen < 0) {
782 ERR_raise(ERR_LIB_PROV, ERR_R_MALLOC_FAILURE);
790 * ED25519, ED448, X25519 and X448 only has PKCS#8 / SubjectPublicKeyInfo
791 * representation, so we don't define ecx_type_specific_[priv,pub,params]_to_der.
794 # define ecx_check_key_type NULL
796 # define ed25519_evp_type EVP_PKEY_ED25519
797 # define ed448_evp_type EVP_PKEY_ED448
798 # define x25519_evp_type EVP_PKEY_X25519
799 # define x448_evp_type EVP_PKEY_X448
800 # define ed25519_input_type "ED25519"
801 # define ed448_input_type "ED448"
802 # define x25519_input_type "X25519"
803 # define x448_input_type "X448"
804 # define ed25519_pem_type "ED25519"
805 # define ed448_pem_type "ED448"
806 # define x25519_pem_type "X25519"
807 # define x448_pem_type "X448"
810 /* ---------------------------------------------------------------------- */
813 * Helper functions to prepare RSA-PSS params for encoding. We would
814 * have simply written the whole AlgorithmIdentifier, but existing libcrypto
815 * functionality doesn't allow that.
818 static int prepare_rsa_params(const void *rsa, int nid, int save,
819 void **pstr, int *pstrtype)
821 const RSA_PSS_PARAMS_30 *pss = ossl_rsa_get0_pss_params_30((RSA *)rsa);
825 switch (RSA_test_flags(rsa, RSA_FLAG_TYPE_MASK)) {
826 case RSA_FLAG_TYPE_RSA:
827 /* If plain RSA, the parameters shall be NULL */
828 *pstrtype = V_ASN1_NULL;
830 case RSA_FLAG_TYPE_RSASSAPSS:
831 if (ossl_rsa_pss_params_30_is_unrestricted(pss)) {
832 *pstrtype = V_ASN1_UNDEF;
835 ASN1_STRING *astr = NULL;
837 unsigned char *str = NULL;
841 for (i = 0; i < 2; i++) {
844 if (!WPACKET_init_null_der(&pkt))
848 if ((str = OPENSSL_malloc(str_sz)) == NULL
849 || !WPACKET_init_der(&pkt, str, str_sz)) {
854 if (!ossl_DER_w_RSASSA_PSS_params(&pkt, -1, pss)
855 || !WPACKET_finish(&pkt)
856 || !WPACKET_get_total_written(&pkt, &str_sz))
858 WPACKET_cleanup(&pkt);
861 * If no PSS parameters are going to be written, there's no
862 * point going for another iteration.
863 * This saves us from getting |str| allocated just to have it
864 * immediately de-allocated.
870 if ((astr = ASN1_STRING_new()) == NULL)
872 *pstrtype = V_ASN1_SEQUENCE;
873 ASN1_STRING_set0(astr, str, (int)str_sz);
883 /* Currently unsupported RSA key type */
888 * RSA is extremely simple, as PKCS#1 is used for the PKCS#8 |privateKey|
889 * field as well as the SubjectPublicKeyInfo |subjectPublicKey| field.
891 #define rsa_pki_priv_to_der rsa_type_specific_priv_to_der
892 #define rsa_spki_pub_to_der rsa_type_specific_pub_to_der
893 #define rsa_type_specific_priv_to_der (i2d_of_void *)i2d_RSAPrivateKey
894 #define rsa_type_specific_pub_to_der (i2d_of_void *)i2d_RSAPublicKey
895 #define rsa_type_specific_params_to_der NULL
897 static int rsa_check_key_type(const void *rsa, int expected_type)
899 switch (RSA_test_flags(rsa, RSA_FLAG_TYPE_MASK)) {
900 case RSA_FLAG_TYPE_RSA:
901 return expected_type == EVP_PKEY_RSA;
902 case RSA_FLAG_TYPE_RSASSAPSS:
903 return expected_type == EVP_PKEY_RSA_PSS;
906 /* Currently unsupported RSA key type */
907 return EVP_PKEY_NONE;
910 #define rsa_evp_type EVP_PKEY_RSA
911 #define rsapss_evp_type EVP_PKEY_RSA_PSS
912 #define rsa_input_type "RSA"
913 #define rsapss_input_type "RSA-PSS"
914 #define rsa_pem_type "RSA"
915 #define rsapss_pem_type "RSA-PSS"
917 /* ---------------------------------------------------------------------- */
919 static OSSL_FUNC_decoder_newctx_fn key2any_newctx;
920 static OSSL_FUNC_decoder_freectx_fn key2any_freectx;
922 static void *key2any_newctx(void *provctx)
924 struct key2any_ctx_st *ctx = OPENSSL_zalloc(sizeof(*ctx));
927 ctx->provctx = provctx;
928 ctx->save_parameters = 1;
934 static void key2any_freectx(void *vctx)
936 struct key2any_ctx_st *ctx = vctx;
938 ossl_pw_clear_passphrase_data(&ctx->pwdata);
939 EVP_CIPHER_free(ctx->cipher);
943 static const OSSL_PARAM *key2any_settable_ctx_params(ossl_unused void *provctx)
945 static const OSSL_PARAM settables[] = {
946 OSSL_PARAM_utf8_string(OSSL_ENCODER_PARAM_CIPHER, NULL, 0),
947 OSSL_PARAM_utf8_string(OSSL_ENCODER_PARAM_PROPERTIES, NULL, 0),
954 static int key2any_set_ctx_params(void *vctx, const OSSL_PARAM params[])
956 struct key2any_ctx_st *ctx = vctx;
957 OSSL_LIB_CTX *libctx = ossl_prov_ctx_get0_libctx(ctx->provctx);
958 const OSSL_PARAM *cipherp =
959 OSSL_PARAM_locate_const(params, OSSL_ENCODER_PARAM_CIPHER);
960 const OSSL_PARAM *propsp =
961 OSSL_PARAM_locate_const(params, OSSL_ENCODER_PARAM_PROPERTIES);
962 const OSSL_PARAM *save_paramsp =
963 OSSL_PARAM_locate_const(params, OSSL_ENCODER_PARAM_SAVE_PARAMETERS);
965 if (cipherp != NULL) {
966 const char *ciphername = NULL;
967 const char *props = NULL;
969 if (!OSSL_PARAM_get_utf8_string_ptr(cipherp, &ciphername))
971 if (propsp != NULL && !OSSL_PARAM_get_utf8_string_ptr(propsp, &props))
974 EVP_CIPHER_free(ctx->cipher);
976 ctx->cipher_intent = ciphername != NULL;
977 if (ciphername != NULL
979 EVP_CIPHER_fetch(libctx, ciphername, props)) == NULL))
983 if (save_paramsp != NULL) {
984 if (!OSSL_PARAM_get_int(save_paramsp, &ctx->save_parameters))
990 static int key2any_check_selection(int selection, int selection_mask)
993 * The selections are kinda sorta "levels", i.e. each selection given
994 * here is assumed to include those following.
997 OSSL_KEYMGMT_SELECT_PRIVATE_KEY,
998 OSSL_KEYMGMT_SELECT_PUBLIC_KEY,
999 OSSL_KEYMGMT_SELECT_ALL_PARAMETERS
1003 /* The decoder implementations made here support guessing */
1007 for (i = 0; i < OSSL_NELEM(checks); i++) {
1008 int check1 = (selection & checks[i]) != 0;
1009 int check2 = (selection_mask & checks[i]) != 0;
1012 * If the caller asked for the currently checked bit(s), return
1013 * whether the decoder description says it's supported.
1019 /* This should be dead code, but just to be safe... */
1023 static int key2any_encode(struct key2any_ctx_st *ctx, OSSL_CORE_BIO *cout,
1024 const void *key, int type, const char *pemname,
1025 check_key_type_fn *checker,
1026 key_to_der_fn *writer,
1027 OSSL_PASSPHRASE_CALLBACK *pwcb, void *pwcbarg,
1028 key_to_paramstring_fn *key2paramstring,
1029 i2d_of_void *key2der)
1034 ERR_raise(ERR_LIB_PROV, ERR_R_PASSED_NULL_PARAMETER);
1035 } else if (writer != NULL
1036 && (checker == NULL || checker(key, type))) {
1037 BIO *out = ossl_bio_new_from_core_bio(ctx->provctx, cout);
1041 || ossl_pw_set_ossl_passphrase_cb(&ctx->pwdata, pwcb, pwcbarg)))
1043 writer(out, key, type, pemname, key2paramstring, key2der, ctx);
1047 ERR_raise(ERR_LIB_PROV, ERR_R_PASSED_INVALID_ARGUMENT);
1052 #define DO_PRIVATE_KEY_selection_mask OSSL_KEYMGMT_SELECT_PRIVATE_KEY
1053 #define DO_PRIVATE_KEY(impl, type, kind, output) \
1054 if ((selection & DO_PRIVATE_KEY_selection_mask) != 0) \
1055 return key2any_encode(ctx, cout, key, impl##_evp_type, \
1056 impl##_pem_type " PRIVATE KEY", \
1057 type##_check_key_type, \
1058 key_to_##kind##_##output##_priv_bio, \
1059 cb, cbarg, prepare_##type##_params, \
1060 type##_##kind##_priv_to_der);
1062 #define DO_PUBLIC_KEY_selection_mask OSSL_KEYMGMT_SELECT_PUBLIC_KEY
1063 #define DO_PUBLIC_KEY(impl, type, kind, output) \
1064 if ((selection & DO_PUBLIC_KEY_selection_mask) != 0) \
1065 return key2any_encode(ctx, cout, key, impl##_evp_type, \
1066 impl##_pem_type " PUBLIC KEY", \
1067 type##_check_key_type, \
1068 key_to_##kind##_##output##_pub_bio, \
1069 cb, cbarg, prepare_##type##_params, \
1070 type##_##kind##_pub_to_der);
1072 #define DO_PARAMETERS_selection_mask OSSL_KEYMGMT_SELECT_ALL_PARAMETERS
1073 #define DO_PARAMETERS(impl, type, kind, output) \
1074 if ((selection & DO_PARAMETERS_selection_mask) != 0) \
1075 return key2any_encode(ctx, cout, key, impl##_evp_type, \
1076 impl##_pem_type " PARAMETERS", \
1077 type##_check_key_type, \
1078 key_to_##kind##_##output##_param_bio, \
1080 type##_##kind##_params_to_der);
1083 * Implement the kinds of output structure that can be produced. They are
1084 * referred to by name, and for each name, the following macros are defined
1085 * (braces not included):
1087 * DO_{kind}_selection_mask
1089 * A mask of selection bits that must not be zero. This is used as a
1090 * selection criterion for each implementation.
1091 * This mask must never be zero.
1095 * The performing macro. It must use the DO_ macros defined above,
1096 * always in this order:
1102 * Any of those may be omitted, but the relative order must still be
1107 * PKCS#8 defines two structures for private keys only:
1108 * - PrivateKeyInfo (raw unencrypted form)
1109 * - EncryptedPrivateKeyInfo (encrypted wrapping)
1111 * To allow a certain amount of flexibility, we allow the routines
1112 * for PrivateKeyInfo to also produce EncryptedPrivateKeyInfo if a
1113 * passphrase callback has been passed to them.
1115 #define DO_PrivateKeyInfo_selection_mask DO_PRIVATE_KEY_selection_mask
1116 #define DO_PrivateKeyInfo(impl, type, output) \
1117 DO_PRIVATE_KEY(impl, type, pki, output)
1119 /* SubjectPublicKeyInfo is a structure for public keys only */
1120 #define DO_SubjectPublicKeyInfo_selection_mask DO_PUBLIC_KEY_selection_mask
1121 #define DO_SubjectPublicKeyInfo(impl, type, output) \
1122 DO_PUBLIC_KEY(impl, type, spki, output)
1125 * "type-specific" is a uniform name for key type specific output for private
1126 * and public keys as well as key parameters. This is used internally in
1127 * libcrypto so it doesn't have to have special knowledge about select key
1128 * types, but also when no better name has been found. If there are more
1129 * expressive DO_ names above, those are preferred.
1131 * Three forms exist:
1133 * - type_specific_keypair Only supports private and public key
1134 * - type_specific_params Only supports parameters
1135 * - type_specific Supports all parts of an EVP_PKEY
1136 * - type_specific_no_pub Supports all parts of an EVP_PKEY
1139 #define DO_type_specific_params_selection_mask DO_PARAMETERS_selection_mask
1140 #define DO_type_specific_params(impl, type, output) \
1141 DO_PARAMETERS(impl, type, type_specific, output)
1142 #define DO_type_specific_keypair_selection_mask \
1143 ( DO_PRIVATE_KEY_selection_mask | DO_PUBLIC_KEY_selection_mask )
1144 #define DO_type_specific_keypair(impl, type, output) \
1145 DO_PRIVATE_KEY(impl, type, type_specific, output) \
1146 DO_PUBLIC_KEY(impl, type, type_specific, output)
1147 #define DO_type_specific_selection_mask \
1148 ( DO_type_specific_keypair_selection_mask \
1149 | DO_type_specific_params_selection_mask )
1150 #define DO_type_specific(impl, type, output) \
1151 DO_type_specific_keypair(impl, type, output) \
1152 DO_type_specific_params(impl, type, output)
1153 #define DO_type_specific_no_pub_selection_mask \
1154 ( DO_PRIVATE_KEY_selection_mask | DO_PARAMETERS_selection_mask)
1155 #define DO_type_specific_no_pub(impl, type, output) \
1156 DO_PRIVATE_KEY(impl, type, type_specific, output) \
1157 DO_type_specific_params(impl, type, output)
1160 * Type specific aliases for the cases where we need to refer to them by
1162 * This only covers key types that are represented with i2d_{TYPE}PrivateKey,
1163 * i2d_{TYPE}PublicKey and i2d_{TYPE}params / i2d_{TYPE}Parameters.
1165 #define DO_RSA_selection_mask DO_type_specific_keypair_selection_mask
1166 #define DO_RSA(impl, type, output) DO_type_specific_keypair(impl, type, output)
1168 #define DO_DH_selection_mask DO_type_specific_params_selection_mask
1169 #define DO_DH(impl, type, output) DO_type_specific_params(impl, type, output)
1171 #define DO_DHX_selection_mask DO_type_specific_params_selection_mask
1172 #define DO_DHX(impl, type, output) DO_type_specific_params(impl, type, output)
1174 #define DO_DSA_selection_mask DO_type_specific_selection_mask
1175 #define DO_DSA(impl, type, output) DO_type_specific(impl, type, output)
1177 #define DO_EC_selection_mask DO_type_specific_selection_mask
1178 #define DO_EC(impl, type, output) DO_type_specific(impl, type, output)
1180 #define DO_SM2_selection_mask DO_type_specific_selection_mask
1181 #define DO_SM2(impl, type, output) DO_type_specific(impl, type, output)
1183 /* PKCS#1 defines a structure for RSA private and public keys */
1184 #define DO_PKCS1_selection_mask DO_RSA_selection_mask
1185 #define DO_PKCS1(impl, type, output) DO_RSA(impl, type, output)
1187 /* PKCS#3 defines a structure for DH parameters */
1188 #define DO_PKCS3_selection_mask DO_DH_selection_mask
1189 #define DO_PKCS3(impl, type, output) DO_DH(impl, type, output)
1190 /* X9.42 defines a structure for DHx parameters */
1191 #define DO_X9_42_selection_mask DO_DHX_selection_mask
1192 #define DO_X9_42(impl, type, output) DO_DHX(impl, type, output)
1194 /* X9.62 defines a structure for EC keys and parameters */
1195 #define DO_X9_62_selection_mask DO_EC_selection_mask
1196 #define DO_X9_62(impl, type, output) DO_EC(impl, type, output)
1199 * MAKE_ENCODER is the single driver for creating OSSL_DISPATCH tables.
1200 * It takes the following arguments:
1202 * impl This is the key type name that's being implemented.
1203 * type This is the type name for the set of functions that implement
1204 * the key type. For example, ed25519, ed448, x25519 and x448
1205 * are all implemented with the exact same set of functions.
1206 * evp_type The corresponding EVP_PKEY_xxx type macro for each key.
1207 * Necessary because we currently use EVP_PKEY with legacy
1208 * native keys internally. This will need to be refactored
1209 * when that legacy support goes away.
1210 * kind What kind of support to implement. These translate into
1211 * the DO_##kind macros above.
1212 * output The output type to implement. may be der or pem.
1214 * The resulting OSSL_DISPATCH array gets the following name (expressed in
1215 * C preprocessor terms) from those arguments:
1217 * ossl_##impl##_to_##kind##_##output##_encoder_functions
1219 #define MAKE_ENCODER(impl, type, evp_type, kind, output) \
1220 static OSSL_FUNC_encoder_import_object_fn \
1221 impl##_to_##kind##_##output##_import_object; \
1222 static OSSL_FUNC_encoder_free_object_fn \
1223 impl##_to_##kind##_##output##_free_object; \
1224 static OSSL_FUNC_encoder_encode_fn \
1225 impl##_to_##kind##_##output##_encode; \
1228 impl##_to_##kind##_##output##_import_object(void *vctx, int selection, \
1229 const OSSL_PARAM params[]) \
1231 struct key2any_ctx_st *ctx = vctx; \
1233 return ossl_prov_import_key(ossl_##impl##_keymgmt_functions, \
1234 ctx->provctx, selection, params); \
1236 static void impl##_to_##kind##_##output##_free_object(void *key) \
1238 ossl_prov_free_key(ossl_##impl##_keymgmt_functions, key); \
1240 static int impl##_to_##kind##_##output##_does_selection(void *ctx, \
1243 return key2any_check_selection(selection, \
1244 DO_##kind##_selection_mask); \
1247 impl##_to_##kind##_##output##_encode(void *ctx, OSSL_CORE_BIO *cout, \
1249 const OSSL_PARAM key_abstract[], \
1251 OSSL_PASSPHRASE_CALLBACK *cb, \
1254 /* We don't deal with abstract objects */ \
1255 if (key_abstract != NULL) { \
1256 ERR_raise(ERR_LIB_PROV, ERR_R_PASSED_INVALID_ARGUMENT); \
1259 DO_##kind(impl, type, output) \
1261 ERR_raise(ERR_LIB_PROV, ERR_R_PASSED_INVALID_ARGUMENT); \
1264 const OSSL_DISPATCH \
1265 ossl_##impl##_to_##kind##_##output##_encoder_functions[] = { \
1266 { OSSL_FUNC_ENCODER_NEWCTX, \
1267 (void (*)(void))key2any_newctx }, \
1268 { OSSL_FUNC_ENCODER_FREECTX, \
1269 (void (*)(void))key2any_freectx }, \
1270 { OSSL_FUNC_ENCODER_SETTABLE_CTX_PARAMS, \
1271 (void (*)(void))key2any_settable_ctx_params }, \
1272 { OSSL_FUNC_ENCODER_SET_CTX_PARAMS, \
1273 (void (*)(void))key2any_set_ctx_params }, \
1274 { OSSL_FUNC_ENCODER_DOES_SELECTION, \
1275 (void (*)(void))impl##_to_##kind##_##output##_does_selection }, \
1276 { OSSL_FUNC_ENCODER_IMPORT_OBJECT, \
1277 (void (*)(void))impl##_to_##kind##_##output##_import_object }, \
1278 { OSSL_FUNC_ENCODER_FREE_OBJECT, \
1279 (void (*)(void))impl##_to_##kind##_##output##_free_object }, \
1280 { OSSL_FUNC_ENCODER_ENCODE, \
1281 (void (*)(void))impl##_to_##kind##_##output##_encode }, \
1286 * Replacements for i2d_{TYPE}PrivateKey, i2d_{TYPE}PublicKey,
1287 * i2d_{TYPE}params, as they exist.
1289 MAKE_ENCODER(rsa, rsa, EVP_PKEY_RSA, type_specific_keypair, der);
1290 #ifndef OPENSSL_NO_DH
1291 MAKE_ENCODER(dh, dh, EVP_PKEY_DH, type_specific_params, der);
1292 MAKE_ENCODER(dhx, dh, EVP_PKEY_DHX, type_specific_params, der);
1294 #ifndef OPENSSL_NO_DSA
1295 MAKE_ENCODER(dsa, dsa, EVP_PKEY_DSA, type_specific, der);
1297 #ifndef OPENSSL_NO_EC
1298 MAKE_ENCODER(ec, ec, EVP_PKEY_EC, type_specific_no_pub, der);
1299 # ifndef OPENSSL_NO_SM2
1300 MAKE_ENCODER(sm2, ec, EVP_PKEY_EC, type_specific_no_pub, der);
1305 * Replacements for PEM_write_bio_{TYPE}PrivateKey,
1306 * PEM_write_bio_{TYPE}PublicKey, PEM_write_bio_{TYPE}params, as they exist.
1308 MAKE_ENCODER(rsa, rsa, EVP_PKEY_RSA, type_specific_keypair, pem);
1309 #ifndef OPENSSL_NO_DH
1310 MAKE_ENCODER(dh, dh, EVP_PKEY_DH, type_specific_params, pem);
1311 MAKE_ENCODER(dhx, dh, EVP_PKEY_DHX, type_specific_params, pem);
1313 #ifndef OPENSSL_NO_DSA
1314 MAKE_ENCODER(dsa, dsa, EVP_PKEY_DSA, type_specific, pem);
1316 #ifndef OPENSSL_NO_EC
1317 MAKE_ENCODER(ec, ec, EVP_PKEY_EC, type_specific_no_pub, pem);
1318 # ifndef OPENSSL_NO_SM2
1319 MAKE_ENCODER(sm2, ec, EVP_PKEY_EC, type_specific_no_pub, pem);
1324 * PKCS#8 and SubjectPublicKeyInfo support. This may duplicate some of the
1325 * implementations specified above, but are more specific.
1326 * The SubjectPublicKeyInfo implementations also replace the
1327 * PEM_write_bio_{TYPE}_PUBKEY functions.
1328 * For PEM, these are expected to be used by PEM_write_bio_PrivateKey(),
1329 * PEM_write_bio_PUBKEY() and PEM_write_bio_Parameters().
1331 MAKE_ENCODER(rsa, rsa, EVP_PKEY_RSA, PrivateKeyInfo, der);
1332 MAKE_ENCODER(rsa, rsa, EVP_PKEY_RSA, PrivateKeyInfo, pem);
1333 MAKE_ENCODER(rsa, rsa, EVP_PKEY_RSA, SubjectPublicKeyInfo, der);
1334 MAKE_ENCODER(rsa, rsa, EVP_PKEY_RSA, SubjectPublicKeyInfo, pem);
1335 MAKE_ENCODER(rsapss, rsa, EVP_PKEY_RSA_PSS, PrivateKeyInfo, der);
1336 MAKE_ENCODER(rsapss, rsa, EVP_PKEY_RSA_PSS, PrivateKeyInfo, pem);
1337 MAKE_ENCODER(rsapss, rsa, EVP_PKEY_RSA_PSS, SubjectPublicKeyInfo, der);
1338 MAKE_ENCODER(rsapss, rsa, EVP_PKEY_RSA_PSS, SubjectPublicKeyInfo, pem);
1339 #ifndef OPENSSL_NO_DH
1340 MAKE_ENCODER(dh, dh, EVP_PKEY_DH, PrivateKeyInfo, der);
1341 MAKE_ENCODER(dh, dh, EVP_PKEY_DH, PrivateKeyInfo, pem);
1342 MAKE_ENCODER(dh, dh, EVP_PKEY_DH, SubjectPublicKeyInfo, der);
1343 MAKE_ENCODER(dh, dh, EVP_PKEY_DH, SubjectPublicKeyInfo, pem);
1344 MAKE_ENCODER(dhx, dh, EVP_PKEY_DHX, PrivateKeyInfo, der);
1345 MAKE_ENCODER(dhx, dh, EVP_PKEY_DHX, PrivateKeyInfo, pem);
1346 MAKE_ENCODER(dhx, dh, EVP_PKEY_DHX, SubjectPublicKeyInfo, der);
1347 MAKE_ENCODER(dhx, dh, EVP_PKEY_DHX, SubjectPublicKeyInfo, pem);
1349 #ifndef OPENSSL_NO_DSA
1350 MAKE_ENCODER(dsa, dsa, EVP_PKEY_DSA, PrivateKeyInfo, der);
1351 MAKE_ENCODER(dsa, dsa, EVP_PKEY_DSA, PrivateKeyInfo, pem);
1352 MAKE_ENCODER(dsa, dsa, EVP_PKEY_DSA, SubjectPublicKeyInfo, der);
1353 MAKE_ENCODER(dsa, dsa, EVP_PKEY_DSA, SubjectPublicKeyInfo, pem);
1355 #ifndef OPENSSL_NO_EC
1356 MAKE_ENCODER(ec, ec, EVP_PKEY_EC, PrivateKeyInfo, der);
1357 MAKE_ENCODER(ec, ec, EVP_PKEY_EC, PrivateKeyInfo, pem);
1358 MAKE_ENCODER(ec, ec, EVP_PKEY_EC, SubjectPublicKeyInfo, der);
1359 MAKE_ENCODER(ec, ec, EVP_PKEY_EC, SubjectPublicKeyInfo, pem);
1360 # ifndef OPENSSL_NO_SM2
1361 MAKE_ENCODER(sm2, ec, EVP_PKEY_EC, PrivateKeyInfo, der);
1362 MAKE_ENCODER(sm2, ec, EVP_PKEY_EC, PrivateKeyInfo, pem);
1363 MAKE_ENCODER(sm2, ec, EVP_PKEY_EC, SubjectPublicKeyInfo, der);
1364 MAKE_ENCODER(sm2, ec, EVP_PKEY_EC, SubjectPublicKeyInfo, pem);
1366 MAKE_ENCODER(ed25519, ecx, EVP_PKEY_ED25519, PrivateKeyInfo, der);
1367 MAKE_ENCODER(ed25519, ecx, EVP_PKEY_ED25519, PrivateKeyInfo, pem);
1368 MAKE_ENCODER(ed25519, ecx, EVP_PKEY_ED25519, SubjectPublicKeyInfo, der);
1369 MAKE_ENCODER(ed25519, ecx, EVP_PKEY_ED25519, SubjectPublicKeyInfo, pem);
1370 MAKE_ENCODER(ed448, ecx, EVP_PKEY_ED448, PrivateKeyInfo, der);
1371 MAKE_ENCODER(ed448, ecx, EVP_PKEY_ED448, PrivateKeyInfo, pem);
1372 MAKE_ENCODER(ed448, ecx, EVP_PKEY_ED448, SubjectPublicKeyInfo, der);
1373 MAKE_ENCODER(ed448, ecx, EVP_PKEY_ED448, SubjectPublicKeyInfo, pem);
1374 MAKE_ENCODER(x25519, ecx, EVP_PKEY_X25519, PrivateKeyInfo, der);
1375 MAKE_ENCODER(x25519, ecx, EVP_PKEY_X25519, PrivateKeyInfo, pem);
1376 MAKE_ENCODER(x25519, ecx, EVP_PKEY_X25519, SubjectPublicKeyInfo, der);
1377 MAKE_ENCODER(x25519, ecx, EVP_PKEY_X25519, SubjectPublicKeyInfo, pem);
1378 MAKE_ENCODER(x448, ecx, EVP_PKEY_ED448, PrivateKeyInfo, der);
1379 MAKE_ENCODER(x448, ecx, EVP_PKEY_ED448, PrivateKeyInfo, pem);
1380 MAKE_ENCODER(x448, ecx, EVP_PKEY_ED448, SubjectPublicKeyInfo, der);
1381 MAKE_ENCODER(x448, ecx, EVP_PKEY_ED448, SubjectPublicKeyInfo, pem);
1385 * Support for key type specific output formats. Not all key types have
1386 * this, we only aim to duplicate what is available in 1.1.1 as
1387 * i2d_TYPEPrivateKey(), i2d_TYPEPublicKey() and i2d_TYPEparams().
1388 * For example, there are no publicly available i2d_ function for
1389 * ED25519, ED448, X25519 or X448, and they therefore only have PKCS#8
1390 * and SubjectPublicKeyInfo implementations as implemented above.
1392 MAKE_ENCODER(rsa, rsa, EVP_PKEY_RSA, RSA, der);
1393 MAKE_ENCODER(rsa, rsa, EVP_PKEY_RSA, RSA, pem);
1394 #ifndef OPENSSL_NO_DH
1395 MAKE_ENCODER(dh, dh, EVP_PKEY_DH, DH, der);
1396 MAKE_ENCODER(dh, dh, EVP_PKEY_DH, DH, pem);
1397 MAKE_ENCODER(dhx, dh, EVP_PKEY_DHX, DHX, der);
1398 MAKE_ENCODER(dhx, dh, EVP_PKEY_DHX, DHX, pem);
1400 #ifndef OPENSSL_NO_DSA
1401 MAKE_ENCODER(dsa, dsa, EVP_PKEY_DSA, DSA, der);
1402 MAKE_ENCODER(dsa, dsa, EVP_PKEY_DSA, DSA, pem);
1404 #ifndef OPENSSL_NO_EC
1405 MAKE_ENCODER(ec, ec, EVP_PKEY_EC, EC, der);
1406 MAKE_ENCODER(ec, ec, EVP_PKEY_EC, EC, pem);
1407 # ifndef OPENSSL_NO_SM2
1408 MAKE_ENCODER(sm2, ec, EVP_PKEY_EC, SM2, der);
1409 MAKE_ENCODER(sm2, ec, EVP_PKEY_EC, SM2, pem);
1413 /* Convenience structure names */
1414 MAKE_ENCODER(rsa, rsa, EVP_PKEY_RSA, PKCS1, der);
1415 MAKE_ENCODER(rsa, rsa, EVP_PKEY_RSA, PKCS1, pem);
1416 MAKE_ENCODER(rsapss, rsa, EVP_PKEY_RSA_PSS, PKCS1, der);
1417 MAKE_ENCODER(rsapss, rsa, EVP_PKEY_RSA_PSS, PKCS1, pem);
1418 #ifndef OPENSSL_NO_DH
1419 MAKE_ENCODER(dh, dh, EVP_PKEY_DH, PKCS3, der); /* parameters only */
1420 MAKE_ENCODER(dh, dh, EVP_PKEY_DH, PKCS3, pem); /* parameters only */
1421 MAKE_ENCODER(dhx, dh, EVP_PKEY_DHX, X9_42, der); /* parameters only */
1422 MAKE_ENCODER(dhx, dh, EVP_PKEY_DHX, X9_42, pem); /* parameters only */
1424 #ifndef OPENSSL_NO_EC
1425 MAKE_ENCODER(ec, ec, EVP_PKEY_EC, X9_62, der);
1426 MAKE_ENCODER(ec, ec, EVP_PKEY_EC, X9_62, pem);