xref: /freebsd/crypto/openssl/providers/implementations/encode_decode/encode_key2text.c (revision e7be843b4a162e68651d3911f0357ed464915629)
1b077aed3SPierre Pronchery /*
2*e7be843bSPierre Pronchery  * Copyright 2020-2025 The OpenSSL Project Authors. All Rights Reserved.
3b077aed3SPierre Pronchery  *
4b077aed3SPierre Pronchery  * Licensed under the Apache License 2.0 (the "License").  You may not use
5b077aed3SPierre Pronchery  * this file except in compliance with the License.  You can obtain a copy
6b077aed3SPierre Pronchery  * in the file LICENSE in the source distribution or at
7b077aed3SPierre Pronchery  * https://www.openssl.org/source/license.html
8b077aed3SPierre Pronchery  */
9b077aed3SPierre Pronchery 
10b077aed3SPierre Pronchery /*
11b077aed3SPierre Pronchery  * Low level APIs are deprecated for public use, but still ok for internal use.
12b077aed3SPierre Pronchery  */
13b077aed3SPierre Pronchery #include "internal/deprecated.h"
14b077aed3SPierre Pronchery 
15b077aed3SPierre Pronchery #include <openssl/core.h>
16b077aed3SPierre Pronchery #include <openssl/core_dispatch.h>
17b077aed3SPierre Pronchery #include <openssl/core_names.h>
18b077aed3SPierre Pronchery #include <openssl/bn.h>
19b077aed3SPierre Pronchery #include <openssl/err.h>
20b077aed3SPierre Pronchery #include <openssl/safestack.h>
21b077aed3SPierre Pronchery #include <openssl/proverr.h>
22b077aed3SPierre Pronchery #include "crypto/dh.h"           /* ossl_dh_get0_params() */
23b077aed3SPierre Pronchery #include "crypto/dsa.h"          /* ossl_dsa_get0_params() */
24b077aed3SPierre Pronchery #include "crypto/ec.h"           /* ossl_ec_key_get_libctx */
25b077aed3SPierre Pronchery #include "crypto/ecx.h"          /* ECX_KEY, etc... */
26*e7be843bSPierre Pronchery #include "crypto/ml_kem.h"       /* ML_KEM_KEY, etc... */
27b077aed3SPierre Pronchery #include "crypto/rsa.h"          /* RSA_PSS_PARAMS_30, etc... */
28*e7be843bSPierre Pronchery #include "crypto/ml_dsa.h"
29*e7be843bSPierre Pronchery #include "crypto/slh_dsa.h"
30b077aed3SPierre Pronchery #include "prov/bio.h"
31b077aed3SPierre Pronchery #include "prov/implementations.h"
32*e7be843bSPierre Pronchery #include "internal/encoder.h"
33b077aed3SPierre Pronchery #include "endecoder_local.h"
34*e7be843bSPierre Pronchery #include "ml_dsa_codecs.h"
35*e7be843bSPierre Pronchery #include "ml_kem_codecs.h"
36b077aed3SPierre Pronchery 
DEFINE_SPECIAL_STACK_OF_CONST(BIGNUM_const,BIGNUM)37b077aed3SPierre Pronchery DEFINE_SPECIAL_STACK_OF_CONST(BIGNUM_const, BIGNUM)
38b077aed3SPierre Pronchery 
39b077aed3SPierre Pronchery /* ---------------------------------------------------------------------- */
40b077aed3SPierre Pronchery 
41b077aed3SPierre Pronchery #ifndef OPENSSL_NO_DH
42b077aed3SPierre Pronchery static int dh_to_text(BIO *out, const void *key, int selection)
43b077aed3SPierre Pronchery {
44b077aed3SPierre Pronchery     const DH *dh = key;
45b077aed3SPierre Pronchery     const char *type_label = NULL;
46b077aed3SPierre Pronchery     const BIGNUM *priv_key = NULL, *pub_key = NULL;
47b077aed3SPierre Pronchery     const FFC_PARAMS *params = NULL;
48b077aed3SPierre Pronchery     const BIGNUM *p = NULL;
49b077aed3SPierre Pronchery     long length;
50b077aed3SPierre Pronchery 
51b077aed3SPierre Pronchery     if (out == NULL || dh == NULL) {
52b077aed3SPierre Pronchery         ERR_raise(ERR_LIB_PROV, ERR_R_PASSED_NULL_PARAMETER);
53b077aed3SPierre Pronchery         return 0;
54b077aed3SPierre Pronchery     }
55b077aed3SPierre Pronchery 
56b077aed3SPierre Pronchery     if ((selection & OSSL_KEYMGMT_SELECT_PRIVATE_KEY) != 0)
57b077aed3SPierre Pronchery         type_label = "DH Private-Key";
58b077aed3SPierre Pronchery     else if ((selection & OSSL_KEYMGMT_SELECT_PUBLIC_KEY) != 0)
59b077aed3SPierre Pronchery         type_label = "DH Public-Key";
60b077aed3SPierre Pronchery     else if ((selection & OSSL_KEYMGMT_SELECT_DOMAIN_PARAMETERS) != 0)
61b077aed3SPierre Pronchery         type_label = "DH Parameters";
62b077aed3SPierre Pronchery 
63b077aed3SPierre Pronchery     if ((selection & OSSL_KEYMGMT_SELECT_PRIVATE_KEY) != 0) {
64b077aed3SPierre Pronchery         priv_key = DH_get0_priv_key(dh);
65b077aed3SPierre Pronchery         if (priv_key == NULL) {
66b077aed3SPierre Pronchery             ERR_raise(ERR_LIB_PROV, PROV_R_NOT_A_PRIVATE_KEY);
67b077aed3SPierre Pronchery             return 0;
68b077aed3SPierre Pronchery         }
69b077aed3SPierre Pronchery     }
70ad991e4cSEd Maste     if ((selection & OSSL_KEYMGMT_SELECT_KEYPAIR) != 0) {
71b077aed3SPierre Pronchery         pub_key = DH_get0_pub_key(dh);
72b077aed3SPierre Pronchery         if (pub_key == NULL) {
73b077aed3SPierre Pronchery             ERR_raise(ERR_LIB_PROV, PROV_R_NOT_A_PUBLIC_KEY);
74b077aed3SPierre Pronchery             return 0;
75b077aed3SPierre Pronchery         }
76b077aed3SPierre Pronchery     }
77b077aed3SPierre Pronchery     if ((selection & OSSL_KEYMGMT_SELECT_DOMAIN_PARAMETERS) != 0) {
78b077aed3SPierre Pronchery         params = ossl_dh_get0_params((DH *)dh);
79b077aed3SPierre Pronchery         if (params == NULL) {
80b077aed3SPierre Pronchery             ERR_raise(ERR_LIB_PROV, PROV_R_NOT_PARAMETERS);
81b077aed3SPierre Pronchery             return 0;
82b077aed3SPierre Pronchery         }
83b077aed3SPierre Pronchery     }
84b077aed3SPierre Pronchery 
85b077aed3SPierre Pronchery     p = DH_get0_p(dh);
86b077aed3SPierre Pronchery     if (p == NULL) {
87b077aed3SPierre Pronchery         ERR_raise(ERR_LIB_PROV, PROV_R_INVALID_KEY);
88b077aed3SPierre Pronchery         return 0;
89b077aed3SPierre Pronchery     }
90b077aed3SPierre Pronchery 
91b077aed3SPierre Pronchery     if (BIO_printf(out, "%s: (%d bit)\n", type_label, BN_num_bits(p)) <= 0)
92b077aed3SPierre Pronchery         return 0;
93b077aed3SPierre Pronchery     if (priv_key != NULL
94*e7be843bSPierre Pronchery         && !ossl_bio_print_labeled_bignum(out, "private-key:", priv_key))
95b077aed3SPierre Pronchery         return 0;
96b077aed3SPierre Pronchery     if (pub_key != NULL
97*e7be843bSPierre Pronchery         && !ossl_bio_print_labeled_bignum(out, "public-key:", pub_key))
98b077aed3SPierre Pronchery         return 0;
99b077aed3SPierre Pronchery     if (params != NULL
100*e7be843bSPierre Pronchery         && !ossl_bio_print_ffc_params(out, params))
101b077aed3SPierre Pronchery         return 0;
102b077aed3SPierre Pronchery     length = DH_get_length(dh);
103b077aed3SPierre Pronchery     if (length > 0
104b077aed3SPierre Pronchery         && BIO_printf(out, "recommended-private-length: %ld bits\n",
105b077aed3SPierre Pronchery                       length) <= 0)
106b077aed3SPierre Pronchery         return 0;
107b077aed3SPierre Pronchery 
108b077aed3SPierre Pronchery     return 1;
109b077aed3SPierre Pronchery }
110b077aed3SPierre Pronchery #endif
111b077aed3SPierre Pronchery 
112b077aed3SPierre Pronchery /* ---------------------------------------------------------------------- */
113b077aed3SPierre Pronchery 
114b077aed3SPierre Pronchery #ifndef OPENSSL_NO_DSA
dsa_to_text(BIO * out,const void * key,int selection)115b077aed3SPierre Pronchery static int dsa_to_text(BIO *out, const void *key, int selection)
116b077aed3SPierre Pronchery {
117b077aed3SPierre Pronchery     const DSA *dsa = key;
118b077aed3SPierre Pronchery     const char *type_label = NULL;
119b077aed3SPierre Pronchery     const BIGNUM *priv_key = NULL, *pub_key = NULL;
120b077aed3SPierre Pronchery     const FFC_PARAMS *params = NULL;
121b077aed3SPierre Pronchery     const BIGNUM *p = NULL;
122b077aed3SPierre Pronchery 
123b077aed3SPierre Pronchery     if (out == NULL || dsa == NULL) {
124b077aed3SPierre Pronchery         ERR_raise(ERR_LIB_PROV, ERR_R_PASSED_NULL_PARAMETER);
125b077aed3SPierre Pronchery         return 0;
126b077aed3SPierre Pronchery     }
127b077aed3SPierre Pronchery 
128b077aed3SPierre Pronchery     if ((selection & OSSL_KEYMGMT_SELECT_PRIVATE_KEY) != 0)
129b077aed3SPierre Pronchery         type_label = "Private-Key";
130b077aed3SPierre Pronchery     else if ((selection & OSSL_KEYMGMT_SELECT_PUBLIC_KEY) != 0)
131b077aed3SPierre Pronchery         type_label = "Public-Key";
132b077aed3SPierre Pronchery     else if ((selection & OSSL_KEYMGMT_SELECT_DOMAIN_PARAMETERS) != 0)
133b077aed3SPierre Pronchery         type_label = "DSA-Parameters";
134b077aed3SPierre Pronchery 
135b077aed3SPierre Pronchery     if ((selection & OSSL_KEYMGMT_SELECT_PRIVATE_KEY) != 0) {
136b077aed3SPierre Pronchery         priv_key = DSA_get0_priv_key(dsa);
137b077aed3SPierre Pronchery         if (priv_key == NULL) {
138b077aed3SPierre Pronchery             ERR_raise(ERR_LIB_PROV, PROV_R_NOT_A_PRIVATE_KEY);
139b077aed3SPierre Pronchery             return 0;
140b077aed3SPierre Pronchery         }
141b077aed3SPierre Pronchery     }
142ad991e4cSEd Maste     if ((selection & OSSL_KEYMGMT_SELECT_KEYPAIR) != 0) {
143b077aed3SPierre Pronchery         pub_key = DSA_get0_pub_key(dsa);
144b077aed3SPierre Pronchery         if (pub_key == NULL) {
145b077aed3SPierre Pronchery             ERR_raise(ERR_LIB_PROV, PROV_R_NOT_A_PUBLIC_KEY);
146b077aed3SPierre Pronchery             return 0;
147b077aed3SPierre Pronchery         }
148b077aed3SPierre Pronchery     }
149b077aed3SPierre Pronchery     if ((selection & OSSL_KEYMGMT_SELECT_DOMAIN_PARAMETERS) != 0) {
150b077aed3SPierre Pronchery         params = ossl_dsa_get0_params((DSA *)dsa);
151b077aed3SPierre Pronchery         if (params == NULL) {
152b077aed3SPierre Pronchery             ERR_raise(ERR_LIB_PROV, PROV_R_NOT_PARAMETERS);
153b077aed3SPierre Pronchery             return 0;
154b077aed3SPierre Pronchery         }
155b077aed3SPierre Pronchery     }
156b077aed3SPierre Pronchery 
157b077aed3SPierre Pronchery     p = DSA_get0_p(dsa);
158b077aed3SPierre Pronchery     if (p == NULL) {
159b077aed3SPierre Pronchery         ERR_raise(ERR_LIB_PROV, PROV_R_INVALID_KEY);
160b077aed3SPierre Pronchery         return 0;
161b077aed3SPierre Pronchery     }
162b077aed3SPierre Pronchery 
163b077aed3SPierre Pronchery     if (BIO_printf(out, "%s: (%d bit)\n", type_label, BN_num_bits(p)) <= 0)
164b077aed3SPierre Pronchery         return 0;
165b077aed3SPierre Pronchery     if (priv_key != NULL
166*e7be843bSPierre Pronchery         && !ossl_bio_print_labeled_bignum(out, "priv:", priv_key))
167b077aed3SPierre Pronchery         return 0;
168b077aed3SPierre Pronchery     if (pub_key != NULL
169*e7be843bSPierre Pronchery         && !ossl_bio_print_labeled_bignum(out, "pub: ", pub_key))
170b077aed3SPierre Pronchery         return 0;
171b077aed3SPierre Pronchery     if (params != NULL
172*e7be843bSPierre Pronchery         && !ossl_bio_print_ffc_params(out, params))
173b077aed3SPierre Pronchery         return 0;
174b077aed3SPierre Pronchery 
175b077aed3SPierre Pronchery     return 1;
176b077aed3SPierre Pronchery }
177b077aed3SPierre Pronchery #endif
178b077aed3SPierre Pronchery 
179b077aed3SPierre Pronchery /* ---------------------------------------------------------------------- */
180b077aed3SPierre Pronchery 
181b077aed3SPierre Pronchery #ifndef OPENSSL_NO_EC
ec_param_explicit_curve_to_text(BIO * out,const EC_GROUP * group,BN_CTX * ctx)182b077aed3SPierre Pronchery static int ec_param_explicit_curve_to_text(BIO *out, const EC_GROUP *group,
183b077aed3SPierre Pronchery                                            BN_CTX *ctx)
184b077aed3SPierre Pronchery {
185b077aed3SPierre Pronchery     const char *plabel = "Prime:";
186b077aed3SPierre Pronchery     BIGNUM *p = NULL, *a = NULL, *b = NULL;
187b077aed3SPierre Pronchery 
188b077aed3SPierre Pronchery     p = BN_CTX_get(ctx);
189b077aed3SPierre Pronchery     a = BN_CTX_get(ctx);
190b077aed3SPierre Pronchery     b = BN_CTX_get(ctx);
191b077aed3SPierre Pronchery     if (b == NULL
192b077aed3SPierre Pronchery         || !EC_GROUP_get_curve(group, p, a, b, ctx))
193b077aed3SPierre Pronchery         return 0;
194b077aed3SPierre Pronchery 
195b077aed3SPierre Pronchery     if (EC_GROUP_get_field_type(group) == NID_X9_62_characteristic_two_field) {
196b077aed3SPierre Pronchery         int basis_type = EC_GROUP_get_basis_type(group);
197b077aed3SPierre Pronchery 
198b077aed3SPierre Pronchery         /* print the 'short name' of the base type OID */
199b077aed3SPierre Pronchery         if (basis_type == NID_undef
200b077aed3SPierre Pronchery             || BIO_printf(out, "Basis Type: %s\n", OBJ_nid2sn(basis_type)) <= 0)
201b077aed3SPierre Pronchery             return 0;
202b077aed3SPierre Pronchery         plabel = "Polynomial:";
203b077aed3SPierre Pronchery     }
204*e7be843bSPierre Pronchery     return ossl_bio_print_labeled_bignum(out, plabel, p)
205*e7be843bSPierre Pronchery         && ossl_bio_print_labeled_bignum(out, "A:   ", a)
206*e7be843bSPierre Pronchery         && ossl_bio_print_labeled_bignum(out, "B:   ", b);
207b077aed3SPierre Pronchery }
208b077aed3SPierre Pronchery 
ec_param_explicit_gen_to_text(BIO * out,const EC_GROUP * group,BN_CTX * ctx)209b077aed3SPierre Pronchery static int ec_param_explicit_gen_to_text(BIO *out, const EC_GROUP *group,
210b077aed3SPierre Pronchery                                          BN_CTX *ctx)
211b077aed3SPierre Pronchery {
212b077aed3SPierre Pronchery     int ret;
213b077aed3SPierre Pronchery     size_t buflen;
214b077aed3SPierre Pronchery     point_conversion_form_t form;
215b077aed3SPierre Pronchery     const EC_POINT *point = NULL;
216b077aed3SPierre Pronchery     const char *glabel = NULL;
217b077aed3SPierre Pronchery     unsigned char *buf = NULL;
218b077aed3SPierre Pronchery 
219b077aed3SPierre Pronchery     form = EC_GROUP_get_point_conversion_form(group);
220b077aed3SPierre Pronchery     point = EC_GROUP_get0_generator(group);
221b077aed3SPierre Pronchery 
222b077aed3SPierre Pronchery     if (point == NULL)
223b077aed3SPierre Pronchery         return 0;
224b077aed3SPierre Pronchery 
225b077aed3SPierre Pronchery     switch (form) {
226b077aed3SPierre Pronchery     case POINT_CONVERSION_COMPRESSED:
227b077aed3SPierre Pronchery        glabel = "Generator (compressed):";
228b077aed3SPierre Pronchery        break;
229b077aed3SPierre Pronchery     case POINT_CONVERSION_UNCOMPRESSED:
230b077aed3SPierre Pronchery         glabel = "Generator (uncompressed):";
231b077aed3SPierre Pronchery         break;
232b077aed3SPierre Pronchery     case POINT_CONVERSION_HYBRID:
233b077aed3SPierre Pronchery         glabel = "Generator (hybrid):";
234b077aed3SPierre Pronchery         break;
235b077aed3SPierre Pronchery     default:
236b077aed3SPierre Pronchery         return 0;
237b077aed3SPierre Pronchery     }
238b077aed3SPierre Pronchery 
239b077aed3SPierre Pronchery     buflen = EC_POINT_point2buf(group, point, form, &buf, ctx);
240b077aed3SPierre Pronchery     if (buflen == 0)
241b077aed3SPierre Pronchery         return 0;
242b077aed3SPierre Pronchery 
243*e7be843bSPierre Pronchery     ret = ossl_bio_print_labeled_buf(out, glabel, buf, buflen);
244b077aed3SPierre Pronchery     OPENSSL_clear_free(buf, buflen);
245b077aed3SPierre Pronchery     return ret;
246b077aed3SPierre Pronchery }
247b077aed3SPierre Pronchery 
248b077aed3SPierre Pronchery /* Print explicit parameters */
ec_param_explicit_to_text(BIO * out,const EC_GROUP * group,OSSL_LIB_CTX * libctx)249b077aed3SPierre Pronchery static int ec_param_explicit_to_text(BIO *out, const EC_GROUP *group,
250b077aed3SPierre Pronchery                                      OSSL_LIB_CTX *libctx)
251b077aed3SPierre Pronchery {
252b077aed3SPierre Pronchery     int ret = 0, tmp_nid;
253b077aed3SPierre Pronchery     BN_CTX *ctx = NULL;
254b077aed3SPierre Pronchery     const BIGNUM *order = NULL, *cofactor = NULL;
255b077aed3SPierre Pronchery     const unsigned char *seed;
256b077aed3SPierre Pronchery     size_t seed_len = 0;
257b077aed3SPierre Pronchery 
258b077aed3SPierre Pronchery     ctx = BN_CTX_new_ex(libctx);
259b077aed3SPierre Pronchery     if (ctx == NULL)
260b077aed3SPierre Pronchery         return 0;
261b077aed3SPierre Pronchery     BN_CTX_start(ctx);
262b077aed3SPierre Pronchery 
263b077aed3SPierre Pronchery     tmp_nid = EC_GROUP_get_field_type(group);
264b077aed3SPierre Pronchery     order = EC_GROUP_get0_order(group);
265b077aed3SPierre Pronchery     if (order == NULL)
266b077aed3SPierre Pronchery         goto err;
267b077aed3SPierre Pronchery 
268b077aed3SPierre Pronchery     seed = EC_GROUP_get0_seed(group);
269b077aed3SPierre Pronchery     if (seed != NULL)
270b077aed3SPierre Pronchery         seed_len = EC_GROUP_get_seed_len(group);
271b077aed3SPierre Pronchery     cofactor = EC_GROUP_get0_cofactor(group);
272b077aed3SPierre Pronchery 
273b077aed3SPierre Pronchery     /* print the 'short name' of the field type */
274b077aed3SPierre Pronchery     if (BIO_printf(out, "Field Type: %s\n", OBJ_nid2sn(tmp_nid)) <= 0
275b077aed3SPierre Pronchery         || !ec_param_explicit_curve_to_text(out, group, ctx)
276b077aed3SPierre Pronchery         || !ec_param_explicit_gen_to_text(out, group, ctx)
277*e7be843bSPierre Pronchery         || !ossl_bio_print_labeled_bignum(out, "Order: ", order)
278b077aed3SPierre Pronchery         || (cofactor != NULL
279*e7be843bSPierre Pronchery             && !ossl_bio_print_labeled_bignum(out, "Cofactor: ", cofactor))
280b077aed3SPierre Pronchery         || (seed != NULL
281*e7be843bSPierre Pronchery             && !ossl_bio_print_labeled_buf(out, "Seed:", seed, seed_len)))
282b077aed3SPierre Pronchery         goto err;
283b077aed3SPierre Pronchery     ret = 1;
284b077aed3SPierre Pronchery err:
285b077aed3SPierre Pronchery     BN_CTX_end(ctx);
286b077aed3SPierre Pronchery     BN_CTX_free(ctx);
287b077aed3SPierre Pronchery     return ret;
288b077aed3SPierre Pronchery }
289b077aed3SPierre Pronchery 
ec_param_to_text(BIO * out,const EC_GROUP * group,OSSL_LIB_CTX * libctx)290b077aed3SPierre Pronchery static int ec_param_to_text(BIO *out, const EC_GROUP *group,
291b077aed3SPierre Pronchery                             OSSL_LIB_CTX *libctx)
292b077aed3SPierre Pronchery {
293b077aed3SPierre Pronchery     if (EC_GROUP_get_asn1_flag(group) & OPENSSL_EC_NAMED_CURVE) {
294b077aed3SPierre Pronchery         const char *curve_name;
295b077aed3SPierre Pronchery         int curve_nid = EC_GROUP_get_curve_name(group);
296b077aed3SPierre Pronchery 
297b077aed3SPierre Pronchery         /* Explicit parameters */
298b077aed3SPierre Pronchery         if (curve_nid == NID_undef)
299b077aed3SPierre Pronchery             return 0;
300b077aed3SPierre Pronchery 
301b077aed3SPierre Pronchery         if (BIO_printf(out, "%s: %s\n", "ASN1 OID", OBJ_nid2sn(curve_nid)) <= 0)
302b077aed3SPierre Pronchery             return 0;
303b077aed3SPierre Pronchery 
304b077aed3SPierre Pronchery         curve_name = EC_curve_nid2nist(curve_nid);
305b077aed3SPierre Pronchery         return (curve_name == NULL
306b077aed3SPierre Pronchery                 || BIO_printf(out, "%s: %s\n", "NIST CURVE", curve_name) > 0);
307b077aed3SPierre Pronchery     } else {
308b077aed3SPierre Pronchery         return ec_param_explicit_to_text(out, group, libctx);
309b077aed3SPierre Pronchery     }
310b077aed3SPierre Pronchery }
311b077aed3SPierre Pronchery 
ec_to_text(BIO * out,const void * key,int selection)312b077aed3SPierre Pronchery static int ec_to_text(BIO *out, const void *key, int selection)
313b077aed3SPierre Pronchery {
314b077aed3SPierre Pronchery     const EC_KEY *ec = key;
315b077aed3SPierre Pronchery     const char *type_label = NULL;
316b077aed3SPierre Pronchery     unsigned char *priv = NULL, *pub = NULL;
317b077aed3SPierre Pronchery     size_t priv_len = 0, pub_len = 0;
318b077aed3SPierre Pronchery     const EC_GROUP *group;
319b077aed3SPierre Pronchery     int ret = 0;
320b077aed3SPierre Pronchery 
321b077aed3SPierre Pronchery     if (out == NULL || ec == NULL) {
322b077aed3SPierre Pronchery         ERR_raise(ERR_LIB_PROV, ERR_R_PASSED_NULL_PARAMETER);
323b077aed3SPierre Pronchery         return 0;
324b077aed3SPierre Pronchery     }
325b077aed3SPierre Pronchery 
326b077aed3SPierre Pronchery     if ((group = EC_KEY_get0_group(ec)) == NULL) {
327b077aed3SPierre Pronchery         ERR_raise(ERR_LIB_PROV, PROV_R_INVALID_KEY);
328b077aed3SPierre Pronchery         return 0;
329b077aed3SPierre Pronchery     }
330b077aed3SPierre Pronchery 
331b077aed3SPierre Pronchery     if ((selection & OSSL_KEYMGMT_SELECT_PRIVATE_KEY) != 0)
332b077aed3SPierre Pronchery         type_label = "Private-Key";
333b077aed3SPierre Pronchery     else if ((selection & OSSL_KEYMGMT_SELECT_PUBLIC_KEY) != 0)
334b077aed3SPierre Pronchery         type_label = "Public-Key";
335b077aed3SPierre Pronchery     else if ((selection & OSSL_KEYMGMT_SELECT_DOMAIN_PARAMETERS) != 0)
336*e7be843bSPierre Pronchery         if (EC_GROUP_get_curve_name(group) != NID_sm2)
337b077aed3SPierre Pronchery             type_label = "EC-Parameters";
338b077aed3SPierre Pronchery 
339b077aed3SPierre Pronchery     if ((selection & OSSL_KEYMGMT_SELECT_PRIVATE_KEY) != 0) {
340b077aed3SPierre Pronchery         const BIGNUM *priv_key = EC_KEY_get0_private_key(ec);
341b077aed3SPierre Pronchery 
342b077aed3SPierre Pronchery         if (priv_key == NULL) {
343b077aed3SPierre Pronchery             ERR_raise(ERR_LIB_PROV, PROV_R_NOT_A_PRIVATE_KEY);
344b077aed3SPierre Pronchery             goto err;
345b077aed3SPierre Pronchery         }
346b077aed3SPierre Pronchery         priv_len = EC_KEY_priv2buf(ec, &priv);
347b077aed3SPierre Pronchery         if (priv_len == 0)
348b077aed3SPierre Pronchery             goto err;
349b077aed3SPierre Pronchery     }
350ad991e4cSEd Maste     if ((selection & OSSL_KEYMGMT_SELECT_KEYPAIR) != 0) {
351b077aed3SPierre Pronchery         const EC_POINT *pub_pt = EC_KEY_get0_public_key(ec);
352b077aed3SPierre Pronchery 
353b077aed3SPierre Pronchery         if (pub_pt == NULL) {
354b077aed3SPierre Pronchery             ERR_raise(ERR_LIB_PROV, PROV_R_NOT_A_PUBLIC_KEY);
355b077aed3SPierre Pronchery             goto err;
356b077aed3SPierre Pronchery         }
357b077aed3SPierre Pronchery 
358b077aed3SPierre Pronchery         pub_len = EC_KEY_key2buf(ec, EC_KEY_get_conv_form(ec), &pub, NULL);
359b077aed3SPierre Pronchery         if (pub_len == 0)
360b077aed3SPierre Pronchery             goto err;
361b077aed3SPierre Pronchery     }
362b077aed3SPierre Pronchery 
363*e7be843bSPierre Pronchery     if (type_label != NULL
364*e7be843bSPierre Pronchery         && BIO_printf(out, "%s: (%d bit)\n", type_label,
365b077aed3SPierre Pronchery                       EC_GROUP_order_bits(group)) <= 0)
366b077aed3SPierre Pronchery         goto err;
367b077aed3SPierre Pronchery     if (priv != NULL
368*e7be843bSPierre Pronchery         && !ossl_bio_print_labeled_buf(out, "priv:", priv, priv_len))
369b077aed3SPierre Pronchery         goto err;
370b077aed3SPierre Pronchery     if (pub != NULL
371*e7be843bSPierre Pronchery         && !ossl_bio_print_labeled_buf(out, "pub:", pub, pub_len))
372b077aed3SPierre Pronchery         goto err;
373b077aed3SPierre Pronchery     if ((selection & OSSL_KEYMGMT_SELECT_DOMAIN_PARAMETERS) != 0)
374b077aed3SPierre Pronchery         ret = ec_param_to_text(out, group, ossl_ec_key_get_libctx(ec));
375b077aed3SPierre Pronchery err:
376b077aed3SPierre Pronchery     OPENSSL_clear_free(priv, priv_len);
377b077aed3SPierre Pronchery     OPENSSL_free(pub);
378b077aed3SPierre Pronchery     return ret;
379b077aed3SPierre Pronchery }
380b077aed3SPierre Pronchery #endif
381b077aed3SPierre Pronchery 
382b077aed3SPierre Pronchery /* ---------------------------------------------------------------------- */
383b077aed3SPierre Pronchery 
384*e7be843bSPierre Pronchery #ifndef OPENSSL_NO_ECX
ecx_to_text(BIO * out,const void * key,int selection)385b077aed3SPierre Pronchery static int ecx_to_text(BIO *out, const void *key, int selection)
386b077aed3SPierre Pronchery {
387b077aed3SPierre Pronchery     const ECX_KEY *ecx = key;
388b077aed3SPierre Pronchery     const char *type_label = NULL;
389b077aed3SPierre Pronchery 
390b077aed3SPierre Pronchery     if (out == NULL || ecx == NULL) {
391b077aed3SPierre Pronchery         ERR_raise(ERR_LIB_PROV, ERR_R_PASSED_NULL_PARAMETER);
392b077aed3SPierre Pronchery         return 0;
393b077aed3SPierre Pronchery     }
394b077aed3SPierre Pronchery 
395ad991e4cSEd Maste     switch (ecx->type) {
396ad991e4cSEd Maste     case ECX_KEY_TYPE_X25519:
397ad991e4cSEd Maste         type_label = "X25519";
398ad991e4cSEd Maste         break;
399ad991e4cSEd Maste     case ECX_KEY_TYPE_X448:
400ad991e4cSEd Maste         type_label = "X448";
401ad991e4cSEd Maste         break;
402ad991e4cSEd Maste     case ECX_KEY_TYPE_ED25519:
403ad991e4cSEd Maste         type_label = "ED25519";
404ad991e4cSEd Maste         break;
405ad991e4cSEd Maste     case ECX_KEY_TYPE_ED448:
406ad991e4cSEd Maste         type_label = "ED448";
407ad991e4cSEd Maste         break;
408ad991e4cSEd Maste     }
409ad991e4cSEd Maste 
410b077aed3SPierre Pronchery     if ((selection & OSSL_KEYMGMT_SELECT_PRIVATE_KEY) != 0) {
411b077aed3SPierre Pronchery         if (ecx->privkey == NULL) {
412b077aed3SPierre Pronchery             ERR_raise(ERR_LIB_PROV, PROV_R_NOT_A_PRIVATE_KEY);
413b077aed3SPierre Pronchery             return 0;
414b077aed3SPierre Pronchery         }
415b077aed3SPierre Pronchery 
416ad991e4cSEd Maste         if (BIO_printf(out, "%s Private-Key:\n", type_label) <= 0)
417ad991e4cSEd Maste             return 0;
418*e7be843bSPierre Pronchery         if (!ossl_bio_print_labeled_buf(out, "priv:", ecx->privkey, ecx->keylen))
419ad991e4cSEd Maste             return 0;
420b077aed3SPierre Pronchery     } else if ((selection & OSSL_KEYMGMT_SELECT_PUBLIC_KEY) != 0) {
421b077aed3SPierre Pronchery         /* ecx->pubkey is an array, not a pointer... */
422b077aed3SPierre Pronchery         if (!ecx->haspubkey) {
423b077aed3SPierre Pronchery             ERR_raise(ERR_LIB_PROV, PROV_R_NOT_A_PUBLIC_KEY);
424b077aed3SPierre Pronchery             return 0;
425b077aed3SPierre Pronchery         }
426b077aed3SPierre Pronchery 
427ad991e4cSEd Maste         if (BIO_printf(out, "%s Public-Key:\n", type_label) <= 0)
428ad991e4cSEd Maste             return 0;
429b077aed3SPierre Pronchery     }
430b077aed3SPierre Pronchery 
431*e7be843bSPierre Pronchery     if (!ossl_bio_print_labeled_buf(out, "pub:", ecx->pubkey, ecx->keylen))
432b077aed3SPierre Pronchery         return 0;
433b077aed3SPierre Pronchery 
434b077aed3SPierre Pronchery     return 1;
435b077aed3SPierre Pronchery }
436b077aed3SPierre Pronchery #endif
437b077aed3SPierre Pronchery 
438b077aed3SPierre Pronchery /* ---------------------------------------------------------------------- */
439b077aed3SPierre Pronchery 
440*e7be843bSPierre Pronchery #ifndef OPENSSL_NO_ML_KEM
ml_kem_to_text(BIO * out,const void * vkey,int selection)441*e7be843bSPierre Pronchery static int ml_kem_to_text(BIO *out, const void *vkey, int selection)
442*e7be843bSPierre Pronchery {
443*e7be843bSPierre Pronchery     return ossl_ml_kem_key_to_text(out, (ML_KEM_KEY *)vkey, selection);
444*e7be843bSPierre Pronchery }
445*e7be843bSPierre Pronchery #endif
446*e7be843bSPierre Pronchery 
447*e7be843bSPierre Pronchery /* ---------------------------------------------------------------------- */
448*e7be843bSPierre Pronchery 
449*e7be843bSPierre Pronchery #ifndef OPENSSL_NO_SLH_DSA
slh_dsa_to_text(BIO * out,const void * key,int selection)450*e7be843bSPierre Pronchery static int slh_dsa_to_text(BIO *out, const void *key, int selection)
451*e7be843bSPierre Pronchery {
452*e7be843bSPierre Pronchery     return ossl_slh_dsa_key_to_text(out, (SLH_DSA_KEY *)key, selection);
453*e7be843bSPierre Pronchery }
454*e7be843bSPierre Pronchery #endif /* OPENSSL_NO_SLH_DSA */
455*e7be843bSPierre Pronchery 
rsa_to_text(BIO * out,const void * key,int selection)456b077aed3SPierre Pronchery static int rsa_to_text(BIO *out, const void *key, int selection)
457b077aed3SPierre Pronchery {
458b077aed3SPierre Pronchery     const RSA *rsa = key;
459b077aed3SPierre Pronchery     const char *type_label = "RSA key";
460b077aed3SPierre Pronchery     const char *modulus_label = NULL;
461b077aed3SPierre Pronchery     const char *exponent_label = NULL;
462b077aed3SPierre Pronchery     const BIGNUM *rsa_d = NULL, *rsa_n = NULL, *rsa_e = NULL;
463b077aed3SPierre Pronchery     STACK_OF(BIGNUM_const) *factors = NULL;
464b077aed3SPierre Pronchery     STACK_OF(BIGNUM_const) *exps = NULL;
465b077aed3SPierre Pronchery     STACK_OF(BIGNUM_const) *coeffs = NULL;
466b077aed3SPierre Pronchery     int primes;
467b077aed3SPierre Pronchery     const RSA_PSS_PARAMS_30 *pss_params = ossl_rsa_get0_pss_params_30((RSA *)rsa);
468b077aed3SPierre Pronchery     int ret = 0;
469b077aed3SPierre Pronchery 
470b077aed3SPierre Pronchery     if (out == NULL || rsa == NULL) {
471b077aed3SPierre Pronchery         ERR_raise(ERR_LIB_PROV, ERR_R_PASSED_NULL_PARAMETER);
472b077aed3SPierre Pronchery         goto err;
473b077aed3SPierre Pronchery     }
474b077aed3SPierre Pronchery 
475b077aed3SPierre Pronchery     factors = sk_BIGNUM_const_new_null();
476b077aed3SPierre Pronchery     exps = sk_BIGNUM_const_new_null();
477b077aed3SPierre Pronchery     coeffs = sk_BIGNUM_const_new_null();
478b077aed3SPierre Pronchery 
479b077aed3SPierre Pronchery     if (factors == NULL || exps == NULL || coeffs == NULL) {
480*e7be843bSPierre Pronchery         ERR_raise(ERR_LIB_PROV, ERR_R_CRYPTO_LIB);
481b077aed3SPierre Pronchery         goto err;
482b077aed3SPierre Pronchery     }
483b077aed3SPierre Pronchery 
484b077aed3SPierre Pronchery     if ((selection & OSSL_KEYMGMT_SELECT_PRIVATE_KEY) != 0) {
485b077aed3SPierre Pronchery         type_label = "Private-Key";
486b077aed3SPierre Pronchery         modulus_label = "modulus:";
487b077aed3SPierre Pronchery         exponent_label = "publicExponent:";
488b077aed3SPierre Pronchery     } else if ((selection & OSSL_KEYMGMT_SELECT_PUBLIC_KEY) != 0) {
489b077aed3SPierre Pronchery         type_label = "Public-Key";
490b077aed3SPierre Pronchery         modulus_label = "Modulus:";
491b077aed3SPierre Pronchery         exponent_label = "Exponent:";
492b077aed3SPierre Pronchery     }
493b077aed3SPierre Pronchery 
494b077aed3SPierre Pronchery     RSA_get0_key(rsa, &rsa_n, &rsa_e, &rsa_d);
495b077aed3SPierre Pronchery     ossl_rsa_get0_all_params((RSA *)rsa, factors, exps, coeffs);
496b077aed3SPierre Pronchery     primes = sk_BIGNUM_const_num(factors);
497b077aed3SPierre Pronchery 
498b077aed3SPierre Pronchery     if ((selection & OSSL_KEYMGMT_SELECT_PRIVATE_KEY) != 0) {
499b077aed3SPierre Pronchery         if (BIO_printf(out, "%s: (%d bit, %d primes)\n",
500b077aed3SPierre Pronchery                        type_label, BN_num_bits(rsa_n), primes) <= 0)
501b077aed3SPierre Pronchery             goto err;
502b077aed3SPierre Pronchery     } else {
503b077aed3SPierre Pronchery         if (BIO_printf(out, "%s: (%d bit)\n",
504b077aed3SPierre Pronchery                        type_label, BN_num_bits(rsa_n)) <= 0)
505b077aed3SPierre Pronchery             goto err;
506b077aed3SPierre Pronchery     }
507b077aed3SPierre Pronchery 
508*e7be843bSPierre Pronchery     if (!ossl_bio_print_labeled_bignum(out, modulus_label, rsa_n))
509b077aed3SPierre Pronchery         goto err;
510*e7be843bSPierre Pronchery     if (!ossl_bio_print_labeled_bignum(out, exponent_label, rsa_e))
511b077aed3SPierre Pronchery         goto err;
512b077aed3SPierre Pronchery     if ((selection & OSSL_KEYMGMT_SELECT_PRIVATE_KEY) != 0) {
513b077aed3SPierre Pronchery         int i;
514b077aed3SPierre Pronchery 
515*e7be843bSPierre Pronchery         if (!ossl_bio_print_labeled_bignum(out, "privateExponent:", rsa_d))
516b077aed3SPierre Pronchery             goto err;
517*e7be843bSPierre Pronchery         if (!ossl_bio_print_labeled_bignum(out, "prime1:",
518b077aed3SPierre Pronchery                                            sk_BIGNUM_const_value(factors, 0)))
519b077aed3SPierre Pronchery             goto err;
520*e7be843bSPierre Pronchery         if (!ossl_bio_print_labeled_bignum(out, "prime2:",
521b077aed3SPierre Pronchery                                            sk_BIGNUM_const_value(factors, 1)))
522b077aed3SPierre Pronchery             goto err;
523*e7be843bSPierre Pronchery         if (!ossl_bio_print_labeled_bignum(out, "exponent1:",
524b077aed3SPierre Pronchery                                            sk_BIGNUM_const_value(exps, 0)))
525b077aed3SPierre Pronchery             goto err;
526*e7be843bSPierre Pronchery         if (!ossl_bio_print_labeled_bignum(out, "exponent2:",
527b077aed3SPierre Pronchery                                            sk_BIGNUM_const_value(exps, 1)))
528b077aed3SPierre Pronchery             goto err;
529*e7be843bSPierre Pronchery         if (!ossl_bio_print_labeled_bignum(out, "coefficient:",
530b077aed3SPierre Pronchery                                            sk_BIGNUM_const_value(coeffs, 0)))
531b077aed3SPierre Pronchery             goto err;
532b077aed3SPierre Pronchery         for (i = 2; i < sk_BIGNUM_const_num(factors); i++) {
533b077aed3SPierre Pronchery             if (BIO_printf(out, "prime%d:", i + 1) <= 0)
534b077aed3SPierre Pronchery                 goto err;
535*e7be843bSPierre Pronchery             if (!ossl_bio_print_labeled_bignum(out, NULL,
536b077aed3SPierre Pronchery                                                sk_BIGNUM_const_value(factors, i)))
537b077aed3SPierre Pronchery                 goto err;
538b077aed3SPierre Pronchery             if (BIO_printf(out, "exponent%d:", i + 1) <= 0)
539b077aed3SPierre Pronchery                 goto err;
540*e7be843bSPierre Pronchery             if (!ossl_bio_print_labeled_bignum(out, NULL,
541b077aed3SPierre Pronchery                                                sk_BIGNUM_const_value(exps, i)))
542b077aed3SPierre Pronchery                 goto err;
543b077aed3SPierre Pronchery             if (BIO_printf(out, "coefficient%d:", i + 1) <= 0)
544b077aed3SPierre Pronchery                 goto err;
545*e7be843bSPierre Pronchery             if (!ossl_bio_print_labeled_bignum(out, NULL,
546b077aed3SPierre Pronchery                                                sk_BIGNUM_const_value(coeffs, i - 1)))
547b077aed3SPierre Pronchery                 goto err;
548b077aed3SPierre Pronchery         }
549b077aed3SPierre Pronchery     }
550b077aed3SPierre Pronchery 
551b077aed3SPierre Pronchery     if ((selection & OSSL_KEYMGMT_SELECT_OTHER_PARAMETERS) != 0) {
552b077aed3SPierre Pronchery         switch (RSA_test_flags(rsa, RSA_FLAG_TYPE_MASK)) {
553b077aed3SPierre Pronchery         case RSA_FLAG_TYPE_RSA:
554b077aed3SPierre Pronchery             if (!ossl_rsa_pss_params_30_is_unrestricted(pss_params)) {
555b077aed3SPierre Pronchery                 if (BIO_printf(out, "(INVALID PSS PARAMETERS)\n") <= 0)
556b077aed3SPierre Pronchery                     goto err;
557b077aed3SPierre Pronchery             }
558b077aed3SPierre Pronchery             break;
559b077aed3SPierre Pronchery         case RSA_FLAG_TYPE_RSASSAPSS:
560b077aed3SPierre Pronchery             if (ossl_rsa_pss_params_30_is_unrestricted(pss_params)) {
561b077aed3SPierre Pronchery                 if (BIO_printf(out, "No PSS parameter restrictions\n") <= 0)
562b077aed3SPierre Pronchery                     goto err;
563b077aed3SPierre Pronchery             } else {
564b077aed3SPierre Pronchery                 int hashalg_nid = ossl_rsa_pss_params_30_hashalg(pss_params);
565b077aed3SPierre Pronchery                 int maskgenalg_nid =
566b077aed3SPierre Pronchery                     ossl_rsa_pss_params_30_maskgenalg(pss_params);
567b077aed3SPierre Pronchery                 int maskgenhashalg_nid =
568b077aed3SPierre Pronchery                     ossl_rsa_pss_params_30_maskgenhashalg(pss_params);
569b077aed3SPierre Pronchery                 int saltlen = ossl_rsa_pss_params_30_saltlen(pss_params);
570b077aed3SPierre Pronchery                 int trailerfield =
571b077aed3SPierre Pronchery                     ossl_rsa_pss_params_30_trailerfield(pss_params);
572b077aed3SPierre Pronchery 
573b077aed3SPierre Pronchery                 if (BIO_printf(out, "PSS parameter restrictions:\n") <= 0)
574b077aed3SPierre Pronchery                     goto err;
575b077aed3SPierre Pronchery                 if (BIO_printf(out, "  Hash Algorithm: %s%s\n",
576b077aed3SPierre Pronchery                                ossl_rsa_oaeppss_nid2name(hashalg_nid),
577b077aed3SPierre Pronchery                                (hashalg_nid == NID_sha1
578b077aed3SPierre Pronchery                                 ? " (default)" : "")) <= 0)
579b077aed3SPierre Pronchery                     goto err;
580b077aed3SPierre Pronchery                 if (BIO_printf(out, "  Mask Algorithm: %s with %s%s\n",
581b077aed3SPierre Pronchery                                ossl_rsa_mgf_nid2name(maskgenalg_nid),
582b077aed3SPierre Pronchery                                ossl_rsa_oaeppss_nid2name(maskgenhashalg_nid),
583b077aed3SPierre Pronchery                                (maskgenalg_nid == NID_mgf1
584b077aed3SPierre Pronchery                                 && maskgenhashalg_nid == NID_sha1
585b077aed3SPierre Pronchery                                 ? " (default)" : "")) <= 0)
586b077aed3SPierre Pronchery                     goto err;
587b077aed3SPierre Pronchery                 if (BIO_printf(out, "  Minimum Salt Length: %d%s\n",
588b077aed3SPierre Pronchery                                saltlen,
589b077aed3SPierre Pronchery                                (saltlen == 20 ? " (default)" : "")) <= 0)
590b077aed3SPierre Pronchery                     goto err;
591b077aed3SPierre Pronchery                 if (BIO_printf(out, "  Trailer Field: 0x%x%s\n",
592b077aed3SPierre Pronchery                                trailerfield,
593b077aed3SPierre Pronchery                                (trailerfield == 1 ? " (default)" : "")) <= 0)
594b077aed3SPierre Pronchery                     goto err;
595b077aed3SPierre Pronchery             }
596b077aed3SPierre Pronchery             break;
597b077aed3SPierre Pronchery         }
598b077aed3SPierre Pronchery     }
599b077aed3SPierre Pronchery 
600b077aed3SPierre Pronchery     ret = 1;
601b077aed3SPierre Pronchery  err:
602b077aed3SPierre Pronchery     sk_BIGNUM_const_free(factors);
603b077aed3SPierre Pronchery     sk_BIGNUM_const_free(exps);
604b077aed3SPierre Pronchery     sk_BIGNUM_const_free(coeffs);
605b077aed3SPierre Pronchery     return ret;
606b077aed3SPierre Pronchery }
607b077aed3SPierre Pronchery 
608*e7be843bSPierre Pronchery /* ---------------------------------------------------------------------- */
609b077aed3SPierre Pronchery 
610*e7be843bSPierre Pronchery #ifndef OPENSSL_NO_ML_DSA
ml_dsa_to_text(BIO * out,const void * key,int selection)611*e7be843bSPierre Pronchery static int ml_dsa_to_text(BIO *out, const void *key, int selection)
612*e7be843bSPierre Pronchery {
613*e7be843bSPierre Pronchery     return ossl_ml_dsa_key_to_text(out, (ML_DSA_KEY *)key, selection);
614*e7be843bSPierre Pronchery }
615*e7be843bSPierre Pronchery #endif /* OPENSSL_NO_ML_DSA */
616b077aed3SPierre Pronchery /* ---------------------------------------------------------------------- */
617b077aed3SPierre Pronchery 
key2text_newctx(void * provctx)618b077aed3SPierre Pronchery static void *key2text_newctx(void *provctx)
619b077aed3SPierre Pronchery {
620b077aed3SPierre Pronchery     return provctx;
621b077aed3SPierre Pronchery }
622b077aed3SPierre Pronchery 
key2text_freectx(ossl_unused void * vctx)623b077aed3SPierre Pronchery static void key2text_freectx(ossl_unused void *vctx)
624b077aed3SPierre Pronchery {
625b077aed3SPierre Pronchery }
626b077aed3SPierre Pronchery 
key2text_encode(void * vctx,const void * key,int selection,OSSL_CORE_BIO * cout,int (* key2text)(BIO * out,const void * key,int selection),OSSL_PASSPHRASE_CALLBACK * cb,void * cbarg)627b077aed3SPierre Pronchery static int key2text_encode(void *vctx, const void *key, int selection,
628b077aed3SPierre Pronchery                            OSSL_CORE_BIO *cout,
629b077aed3SPierre Pronchery                            int (*key2text)(BIO *out, const void *key,
630b077aed3SPierre Pronchery                                            int selection),
631b077aed3SPierre Pronchery                            OSSL_PASSPHRASE_CALLBACK *cb, void *cbarg)
632b077aed3SPierre Pronchery {
633b077aed3SPierre Pronchery     BIO *out = ossl_bio_new_from_core_bio(vctx, cout);
634b077aed3SPierre Pronchery     int ret;
635b077aed3SPierre Pronchery 
636b077aed3SPierre Pronchery     if (out == NULL)
637b077aed3SPierre Pronchery         return 0;
638b077aed3SPierre Pronchery 
639b077aed3SPierre Pronchery     ret = key2text(out, key, selection);
640b077aed3SPierre Pronchery     BIO_free(out);
641b077aed3SPierre Pronchery 
642b077aed3SPierre Pronchery     return ret;
643b077aed3SPierre Pronchery }
644b077aed3SPierre Pronchery 
645b077aed3SPierre Pronchery #define MAKE_TEXT_ENCODER(impl, type)                                   \
646b077aed3SPierre Pronchery     static OSSL_FUNC_encoder_import_object_fn                           \
647b077aed3SPierre Pronchery     impl##2text_import_object;                                          \
648b077aed3SPierre Pronchery     static OSSL_FUNC_encoder_free_object_fn                             \
649b077aed3SPierre Pronchery     impl##2text_free_object;                                            \
650b077aed3SPierre Pronchery     static OSSL_FUNC_encoder_encode_fn impl##2text_encode;              \
651b077aed3SPierre Pronchery                                                                         \
652b077aed3SPierre Pronchery     static void *impl##2text_import_object(void *ctx, int selection,    \
653b077aed3SPierre Pronchery                                            const OSSL_PARAM params[])   \
654b077aed3SPierre Pronchery     {                                                                   \
655b077aed3SPierre Pronchery         return ossl_prov_import_key(ossl_##impl##_keymgmt_functions,    \
656b077aed3SPierre Pronchery                                     ctx, selection, params);            \
657b077aed3SPierre Pronchery     }                                                                   \
658b077aed3SPierre Pronchery     static void impl##2text_free_object(void *key)                      \
659b077aed3SPierre Pronchery     {                                                                   \
660b077aed3SPierre Pronchery         ossl_prov_free_key(ossl_##impl##_keymgmt_functions, key);       \
661b077aed3SPierre Pronchery     }                                                                   \
662b077aed3SPierre Pronchery     static int impl##2text_encode(void *vctx, OSSL_CORE_BIO *cout,      \
663b077aed3SPierre Pronchery                                   const void *key,                      \
664b077aed3SPierre Pronchery                                   const OSSL_PARAM key_abstract[],      \
665b077aed3SPierre Pronchery                                   int selection,                        \
666b077aed3SPierre Pronchery                                   OSSL_PASSPHRASE_CALLBACK *cb,         \
667b077aed3SPierre Pronchery                                   void *cbarg)                          \
668b077aed3SPierre Pronchery     {                                                                   \
669b077aed3SPierre Pronchery         /* We don't deal with abstract objects */                       \
670b077aed3SPierre Pronchery         if (key_abstract != NULL) {                                     \
671b077aed3SPierre Pronchery             ERR_raise(ERR_LIB_PROV, ERR_R_PASSED_INVALID_ARGUMENT);     \
672b077aed3SPierre Pronchery             return 0;                                                   \
673b077aed3SPierre Pronchery         }                                                               \
674b077aed3SPierre Pronchery         return key2text_encode(vctx, key, selection, cout,              \
675b077aed3SPierre Pronchery                                type##_to_text, cb, cbarg);              \
676b077aed3SPierre Pronchery     }                                                                   \
677b077aed3SPierre Pronchery     const OSSL_DISPATCH ossl_##impl##_to_text_encoder_functions[] = {   \
678b077aed3SPierre Pronchery         { OSSL_FUNC_ENCODER_NEWCTX,                                     \
679b077aed3SPierre Pronchery           (void (*)(void))key2text_newctx },                            \
680b077aed3SPierre Pronchery         { OSSL_FUNC_ENCODER_FREECTX,                                    \
681b077aed3SPierre Pronchery           (void (*)(void))key2text_freectx },                           \
682b077aed3SPierre Pronchery         { OSSL_FUNC_ENCODER_IMPORT_OBJECT,                              \
683b077aed3SPierre Pronchery           (void (*)(void))impl##2text_import_object },                  \
684b077aed3SPierre Pronchery         { OSSL_FUNC_ENCODER_FREE_OBJECT,                                \
685b077aed3SPierre Pronchery           (void (*)(void))impl##2text_free_object },                    \
686b077aed3SPierre Pronchery         { OSSL_FUNC_ENCODER_ENCODE,                                     \
687b077aed3SPierre Pronchery           (void (*)(void))impl##2text_encode },                         \
688*e7be843bSPierre Pronchery         OSSL_DISPATCH_END                                               \
689b077aed3SPierre Pronchery     }
690b077aed3SPierre Pronchery 
691b077aed3SPierre Pronchery #ifndef OPENSSL_NO_DH
692b077aed3SPierre Pronchery MAKE_TEXT_ENCODER(dh, dh);
693b077aed3SPierre Pronchery MAKE_TEXT_ENCODER(dhx, dh);
694b077aed3SPierre Pronchery #endif
695b077aed3SPierre Pronchery #ifndef OPENSSL_NO_DSA
696b077aed3SPierre Pronchery MAKE_TEXT_ENCODER(dsa, dsa);
697b077aed3SPierre Pronchery #endif
698b077aed3SPierre Pronchery #ifndef OPENSSL_NO_EC
699b077aed3SPierre Pronchery MAKE_TEXT_ENCODER(ec, ec);
700b077aed3SPierre Pronchery # ifndef OPENSSL_NO_SM2
701b077aed3SPierre Pronchery MAKE_TEXT_ENCODER(sm2, ec);
702b077aed3SPierre Pronchery # endif
703*e7be843bSPierre Pronchery # ifndef OPENSSL_NO_ECX
704b077aed3SPierre Pronchery MAKE_TEXT_ENCODER(ed25519, ecx);
705b077aed3SPierre Pronchery MAKE_TEXT_ENCODER(ed448, ecx);
706b077aed3SPierre Pronchery MAKE_TEXT_ENCODER(x25519, ecx);
707b077aed3SPierre Pronchery MAKE_TEXT_ENCODER(x448, ecx);
708b077aed3SPierre Pronchery # endif
709*e7be843bSPierre Pronchery #endif
710*e7be843bSPierre Pronchery #ifndef OPENSSL_NO_ML_KEM
711*e7be843bSPierre Pronchery MAKE_TEXT_ENCODER(ml_kem_512, ml_kem);
712*e7be843bSPierre Pronchery MAKE_TEXT_ENCODER(ml_kem_768, ml_kem);
713*e7be843bSPierre Pronchery MAKE_TEXT_ENCODER(ml_kem_1024, ml_kem);
714*e7be843bSPierre Pronchery #endif
715b077aed3SPierre Pronchery MAKE_TEXT_ENCODER(rsa, rsa);
716b077aed3SPierre Pronchery MAKE_TEXT_ENCODER(rsapss, rsa);
717*e7be843bSPierre Pronchery 
718*e7be843bSPierre Pronchery #ifndef OPENSSL_NO_ML_DSA
719*e7be843bSPierre Pronchery MAKE_TEXT_ENCODER(ml_dsa_44, ml_dsa);
720*e7be843bSPierre Pronchery MAKE_TEXT_ENCODER(ml_dsa_65, ml_dsa);
721*e7be843bSPierre Pronchery MAKE_TEXT_ENCODER(ml_dsa_87, ml_dsa);
722*e7be843bSPierre Pronchery #endif
723*e7be843bSPierre Pronchery 
724*e7be843bSPierre Pronchery #ifndef OPENSSL_NO_SLH_DSA
725*e7be843bSPierre Pronchery MAKE_TEXT_ENCODER(slh_dsa_sha2_128s, slh_dsa);
726*e7be843bSPierre Pronchery MAKE_TEXT_ENCODER(slh_dsa_sha2_128f, slh_dsa);
727*e7be843bSPierre Pronchery MAKE_TEXT_ENCODER(slh_dsa_sha2_192s, slh_dsa);
728*e7be843bSPierre Pronchery MAKE_TEXT_ENCODER(slh_dsa_sha2_192f, slh_dsa);
729*e7be843bSPierre Pronchery MAKE_TEXT_ENCODER(slh_dsa_sha2_256s, slh_dsa);
730*e7be843bSPierre Pronchery MAKE_TEXT_ENCODER(slh_dsa_sha2_256f, slh_dsa);
731*e7be843bSPierre Pronchery MAKE_TEXT_ENCODER(slh_dsa_shake_128s, slh_dsa);
732*e7be843bSPierre Pronchery MAKE_TEXT_ENCODER(slh_dsa_shake_128f, slh_dsa);
733*e7be843bSPierre Pronchery MAKE_TEXT_ENCODER(slh_dsa_shake_192s, slh_dsa);
734*e7be843bSPierre Pronchery MAKE_TEXT_ENCODER(slh_dsa_shake_192f, slh_dsa);
735*e7be843bSPierre Pronchery MAKE_TEXT_ENCODER(slh_dsa_shake_256s, slh_dsa);
736*e7be843bSPierre Pronchery MAKE_TEXT_ENCODER(slh_dsa_shake_256f, slh_dsa);
737*e7be843bSPierre Pronchery #endif
738