1e4520c8bSEnji Cooper /*
229536654SEnji Cooper * Copyright 2020-2025 The OpenSSL Project Authors. All Rights Reserved.
3e4520c8bSEnji Cooper *
4e4520c8bSEnji Cooper * Licensed under the Apache License 2.0 (the "License"). You may not use
5e4520c8bSEnji Cooper * this file except in compliance with the License. You can obtain a copy
6e4520c8bSEnji Cooper * in the file LICENSE in the source distribution or at
7e4520c8bSEnji Cooper * https://www.openssl.org/source/license.html
8e4520c8bSEnji Cooper */
9e4520c8bSEnji Cooper
10e4520c8bSEnji Cooper /*
11e4520c8bSEnji Cooper * Low level APIs are deprecated for public use, but still ok for internal use.
12e4520c8bSEnji Cooper */
13e4520c8bSEnji Cooper #include "internal/deprecated.h"
14e4520c8bSEnji Cooper
15e4520c8bSEnji Cooper #include <openssl/core.h>
16e4520c8bSEnji Cooper #include <openssl/core_dispatch.h>
17e4520c8bSEnji Cooper #include <openssl/core_names.h>
18e4520c8bSEnji Cooper #include <openssl/bn.h>
19e4520c8bSEnji Cooper #include <openssl/err.h>
20e4520c8bSEnji Cooper #include <openssl/safestack.h>
21e4520c8bSEnji Cooper #include <openssl/proverr.h>
22e4520c8bSEnji Cooper #include "crypto/dh.h" /* ossl_dh_get0_params() */
23e4520c8bSEnji Cooper #include "crypto/dsa.h" /* ossl_dsa_get0_params() */
24e4520c8bSEnji Cooper #include "crypto/ec.h" /* ossl_ec_key_get_libctx */
25e4520c8bSEnji Cooper #include "crypto/ecx.h" /* ECX_KEY, etc... */
2629536654SEnji Cooper #include "crypto/ml_kem.h" /* ML_KEM_KEY, etc... */
27e4520c8bSEnji Cooper #include "crypto/rsa.h" /* RSA_PSS_PARAMS_30, etc... */
2829536654SEnji Cooper #include "crypto/ml_dsa.h"
2929536654SEnji Cooper #include "crypto/slh_dsa.h"
30e4520c8bSEnji Cooper #include "prov/bio.h"
31e4520c8bSEnji Cooper #include "prov/implementations.h"
3229536654SEnji Cooper #include "internal/encoder.h"
33e4520c8bSEnji Cooper #include "endecoder_local.h"
3429536654SEnji Cooper #include "ml_dsa_codecs.h"
3529536654SEnji Cooper #include "ml_kem_codecs.h"
36e4520c8bSEnji Cooper
DEFINE_SPECIAL_STACK_OF_CONST(BIGNUM_const,BIGNUM)37e4520c8bSEnji Cooper DEFINE_SPECIAL_STACK_OF_CONST(BIGNUM_const, BIGNUM)
38e4520c8bSEnji Cooper
39e4520c8bSEnji Cooper /* ---------------------------------------------------------------------- */
40e4520c8bSEnji Cooper
41e4520c8bSEnji Cooper #ifndef OPENSSL_NO_DH
42e4520c8bSEnji Cooper static int dh_to_text(BIO *out, const void *key, int selection)
43e4520c8bSEnji Cooper {
44e4520c8bSEnji Cooper const DH *dh = key;
45e4520c8bSEnji Cooper const char *type_label = NULL;
46e4520c8bSEnji Cooper const BIGNUM *priv_key = NULL, *pub_key = NULL;
47e4520c8bSEnji Cooper const FFC_PARAMS *params = NULL;
48e4520c8bSEnji Cooper const BIGNUM *p = NULL;
49e4520c8bSEnji Cooper long length;
50e4520c8bSEnji Cooper
51e4520c8bSEnji Cooper if (out == NULL || dh == NULL) {
52e4520c8bSEnji Cooper ERR_raise(ERR_LIB_PROV, ERR_R_PASSED_NULL_PARAMETER);
53e4520c8bSEnji Cooper return 0;
54e4520c8bSEnji Cooper }
55e4520c8bSEnji Cooper
56e4520c8bSEnji Cooper if ((selection & OSSL_KEYMGMT_SELECT_PRIVATE_KEY) != 0)
57e4520c8bSEnji Cooper type_label = "DH Private-Key";
58e4520c8bSEnji Cooper else if ((selection & OSSL_KEYMGMT_SELECT_PUBLIC_KEY) != 0)
59e4520c8bSEnji Cooper type_label = "DH Public-Key";
60e4520c8bSEnji Cooper else if ((selection & OSSL_KEYMGMT_SELECT_DOMAIN_PARAMETERS) != 0)
61e4520c8bSEnji Cooper type_label = "DH Parameters";
62e4520c8bSEnji Cooper
63e4520c8bSEnji Cooper if ((selection & OSSL_KEYMGMT_SELECT_PRIVATE_KEY) != 0) {
64e4520c8bSEnji Cooper priv_key = DH_get0_priv_key(dh);
65e4520c8bSEnji Cooper if (priv_key == NULL) {
66e4520c8bSEnji Cooper ERR_raise(ERR_LIB_PROV, PROV_R_NOT_A_PRIVATE_KEY);
67e4520c8bSEnji Cooper return 0;
68e4520c8bSEnji Cooper }
69e4520c8bSEnji Cooper }
70825caf7eSEd Maste if ((selection & OSSL_KEYMGMT_SELECT_KEYPAIR) != 0) {
71e4520c8bSEnji Cooper pub_key = DH_get0_pub_key(dh);
72e4520c8bSEnji Cooper if (pub_key == NULL) {
73e4520c8bSEnji Cooper ERR_raise(ERR_LIB_PROV, PROV_R_NOT_A_PUBLIC_KEY);
74e4520c8bSEnji Cooper return 0;
75e4520c8bSEnji Cooper }
76e4520c8bSEnji Cooper }
77e4520c8bSEnji Cooper if ((selection & OSSL_KEYMGMT_SELECT_DOMAIN_PARAMETERS) != 0) {
78e4520c8bSEnji Cooper params = ossl_dh_get0_params((DH *)dh);
79e4520c8bSEnji Cooper if (params == NULL) {
80e4520c8bSEnji Cooper ERR_raise(ERR_LIB_PROV, PROV_R_NOT_PARAMETERS);
81e4520c8bSEnji Cooper return 0;
82e4520c8bSEnji Cooper }
83e4520c8bSEnji Cooper }
84e4520c8bSEnji Cooper
85e4520c8bSEnji Cooper p = DH_get0_p(dh);
86e4520c8bSEnji Cooper if (p == NULL) {
87e4520c8bSEnji Cooper ERR_raise(ERR_LIB_PROV, PROV_R_INVALID_KEY);
88e4520c8bSEnji Cooper return 0;
89e4520c8bSEnji Cooper }
90e4520c8bSEnji Cooper
91e4520c8bSEnji Cooper if (BIO_printf(out, "%s: (%d bit)\n", type_label, BN_num_bits(p)) <= 0)
92e4520c8bSEnji Cooper return 0;
93e4520c8bSEnji Cooper if (priv_key != NULL
9429536654SEnji Cooper && !ossl_bio_print_labeled_bignum(out, "private-key:", priv_key))
95e4520c8bSEnji Cooper return 0;
96e4520c8bSEnji Cooper if (pub_key != NULL
9729536654SEnji Cooper && !ossl_bio_print_labeled_bignum(out, "public-key:", pub_key))
98e4520c8bSEnji Cooper return 0;
99e4520c8bSEnji Cooper if (params != NULL
10029536654SEnji Cooper && !ossl_bio_print_ffc_params(out, params))
101e4520c8bSEnji Cooper return 0;
102e4520c8bSEnji Cooper length = DH_get_length(dh);
103e4520c8bSEnji Cooper if (length > 0
104e4520c8bSEnji Cooper && BIO_printf(out, "recommended-private-length: %ld bits\n",
105808413daSEnji Cooper length)
106808413daSEnji Cooper <= 0)
107e4520c8bSEnji Cooper return 0;
108e4520c8bSEnji Cooper
109e4520c8bSEnji Cooper return 1;
110e4520c8bSEnji Cooper }
111e4520c8bSEnji Cooper #endif
112e4520c8bSEnji Cooper
113e4520c8bSEnji Cooper /* ---------------------------------------------------------------------- */
114e4520c8bSEnji Cooper
115e4520c8bSEnji Cooper #ifndef OPENSSL_NO_DSA
dsa_to_text(BIO * out,const void * key,int selection)116e4520c8bSEnji Cooper static int dsa_to_text(BIO *out, const void *key, int selection)
117e4520c8bSEnji Cooper {
118e4520c8bSEnji Cooper const DSA *dsa = key;
119e4520c8bSEnji Cooper const char *type_label = NULL;
120e4520c8bSEnji Cooper const BIGNUM *priv_key = NULL, *pub_key = NULL;
121e4520c8bSEnji Cooper const FFC_PARAMS *params = NULL;
122e4520c8bSEnji Cooper const BIGNUM *p = NULL;
123e4520c8bSEnji Cooper
124e4520c8bSEnji Cooper if (out == NULL || dsa == NULL) {
125e4520c8bSEnji Cooper ERR_raise(ERR_LIB_PROV, ERR_R_PASSED_NULL_PARAMETER);
126e4520c8bSEnji Cooper return 0;
127e4520c8bSEnji Cooper }
128e4520c8bSEnji Cooper
129e4520c8bSEnji Cooper if ((selection & OSSL_KEYMGMT_SELECT_PRIVATE_KEY) != 0)
130e4520c8bSEnji Cooper type_label = "Private-Key";
131e4520c8bSEnji Cooper else if ((selection & OSSL_KEYMGMT_SELECT_PUBLIC_KEY) != 0)
132e4520c8bSEnji Cooper type_label = "Public-Key";
133e4520c8bSEnji Cooper else if ((selection & OSSL_KEYMGMT_SELECT_DOMAIN_PARAMETERS) != 0)
134e4520c8bSEnji Cooper type_label = "DSA-Parameters";
135e4520c8bSEnji Cooper
136e4520c8bSEnji Cooper if ((selection & OSSL_KEYMGMT_SELECT_PRIVATE_KEY) != 0) {
137e4520c8bSEnji Cooper priv_key = DSA_get0_priv_key(dsa);
138e4520c8bSEnji Cooper if (priv_key == NULL) {
139e4520c8bSEnji Cooper ERR_raise(ERR_LIB_PROV, PROV_R_NOT_A_PRIVATE_KEY);
140e4520c8bSEnji Cooper return 0;
141e4520c8bSEnji Cooper }
142e4520c8bSEnji Cooper }
143825caf7eSEd Maste if ((selection & OSSL_KEYMGMT_SELECT_KEYPAIR) != 0) {
144e4520c8bSEnji Cooper pub_key = DSA_get0_pub_key(dsa);
145e4520c8bSEnji Cooper if (pub_key == NULL) {
146e4520c8bSEnji Cooper ERR_raise(ERR_LIB_PROV, PROV_R_NOT_A_PUBLIC_KEY);
147e4520c8bSEnji Cooper return 0;
148e4520c8bSEnji Cooper }
149e4520c8bSEnji Cooper }
150e4520c8bSEnji Cooper if ((selection & OSSL_KEYMGMT_SELECT_DOMAIN_PARAMETERS) != 0) {
151e4520c8bSEnji Cooper params = ossl_dsa_get0_params((DSA *)dsa);
152e4520c8bSEnji Cooper if (params == NULL) {
153e4520c8bSEnji Cooper ERR_raise(ERR_LIB_PROV, PROV_R_NOT_PARAMETERS);
154e4520c8bSEnji Cooper return 0;
155e4520c8bSEnji Cooper }
156e4520c8bSEnji Cooper }
157e4520c8bSEnji Cooper
158e4520c8bSEnji Cooper p = DSA_get0_p(dsa);
159e4520c8bSEnji Cooper if (p == NULL) {
160e4520c8bSEnji Cooper ERR_raise(ERR_LIB_PROV, PROV_R_INVALID_KEY);
161e4520c8bSEnji Cooper return 0;
162e4520c8bSEnji Cooper }
163e4520c8bSEnji Cooper
164e4520c8bSEnji Cooper if (BIO_printf(out, "%s: (%d bit)\n", type_label, BN_num_bits(p)) <= 0)
165e4520c8bSEnji Cooper return 0;
166e4520c8bSEnji Cooper if (priv_key != NULL
16729536654SEnji Cooper && !ossl_bio_print_labeled_bignum(out, "priv:", priv_key))
168e4520c8bSEnji Cooper return 0;
169e4520c8bSEnji Cooper if (pub_key != NULL
17029536654SEnji Cooper && !ossl_bio_print_labeled_bignum(out, "pub: ", pub_key))
171e4520c8bSEnji Cooper return 0;
172e4520c8bSEnji Cooper if (params != NULL
17329536654SEnji Cooper && !ossl_bio_print_ffc_params(out, params))
174e4520c8bSEnji Cooper return 0;
175e4520c8bSEnji Cooper
176e4520c8bSEnji Cooper return 1;
177e4520c8bSEnji Cooper }
178e4520c8bSEnji Cooper #endif
179e4520c8bSEnji Cooper
180e4520c8bSEnji Cooper /* ---------------------------------------------------------------------- */
181e4520c8bSEnji Cooper
182e4520c8bSEnji Cooper #ifndef OPENSSL_NO_EC
ec_param_explicit_curve_to_text(BIO * out,const EC_GROUP * group,BN_CTX * ctx)183e4520c8bSEnji Cooper static int ec_param_explicit_curve_to_text(BIO *out, const EC_GROUP *group,
184e4520c8bSEnji Cooper BN_CTX *ctx)
185e4520c8bSEnji Cooper {
186e4520c8bSEnji Cooper const char *plabel = "Prime:";
187e4520c8bSEnji Cooper BIGNUM *p = NULL, *a = NULL, *b = NULL;
188e4520c8bSEnji Cooper
189e4520c8bSEnji Cooper p = BN_CTX_get(ctx);
190e4520c8bSEnji Cooper a = BN_CTX_get(ctx);
191e4520c8bSEnji Cooper b = BN_CTX_get(ctx);
192e4520c8bSEnji Cooper if (b == NULL
193e4520c8bSEnji Cooper || !EC_GROUP_get_curve(group, p, a, b, ctx))
194e4520c8bSEnji Cooper return 0;
195e4520c8bSEnji Cooper
196e4520c8bSEnji Cooper if (EC_GROUP_get_field_type(group) == NID_X9_62_characteristic_two_field) {
197e4520c8bSEnji Cooper int basis_type = EC_GROUP_get_basis_type(group);
198e4520c8bSEnji Cooper
199e4520c8bSEnji Cooper /* print the 'short name' of the base type OID */
200e4520c8bSEnji Cooper if (basis_type == NID_undef
201e4520c8bSEnji Cooper || BIO_printf(out, "Basis Type: %s\n", OBJ_nid2sn(basis_type)) <= 0)
202e4520c8bSEnji Cooper return 0;
203e4520c8bSEnji Cooper plabel = "Polynomial:";
204e4520c8bSEnji Cooper }
20529536654SEnji Cooper return ossl_bio_print_labeled_bignum(out, plabel, p)
20629536654SEnji Cooper && ossl_bio_print_labeled_bignum(out, "A: ", a)
20729536654SEnji Cooper && ossl_bio_print_labeled_bignum(out, "B: ", b);
208e4520c8bSEnji Cooper }
209e4520c8bSEnji Cooper
ec_param_explicit_gen_to_text(BIO * out,const EC_GROUP * group,BN_CTX * ctx)210e4520c8bSEnji Cooper static int ec_param_explicit_gen_to_text(BIO *out, const EC_GROUP *group,
211e4520c8bSEnji Cooper BN_CTX *ctx)
212e4520c8bSEnji Cooper {
213e4520c8bSEnji Cooper int ret;
214e4520c8bSEnji Cooper size_t buflen;
215e4520c8bSEnji Cooper point_conversion_form_t form;
216e4520c8bSEnji Cooper const EC_POINT *point = NULL;
217e4520c8bSEnji Cooper const char *glabel = NULL;
218e4520c8bSEnji Cooper unsigned char *buf = NULL;
219e4520c8bSEnji Cooper
220e4520c8bSEnji Cooper form = EC_GROUP_get_point_conversion_form(group);
221e4520c8bSEnji Cooper point = EC_GROUP_get0_generator(group);
222e4520c8bSEnji Cooper
223e4520c8bSEnji Cooper if (point == NULL)
224e4520c8bSEnji Cooper return 0;
225e4520c8bSEnji Cooper
226e4520c8bSEnji Cooper switch (form) {
227e4520c8bSEnji Cooper case POINT_CONVERSION_COMPRESSED:
228e4520c8bSEnji Cooper glabel = "Generator (compressed):";
229e4520c8bSEnji Cooper break;
230e4520c8bSEnji Cooper case POINT_CONVERSION_UNCOMPRESSED:
231e4520c8bSEnji Cooper glabel = "Generator (uncompressed):";
232e4520c8bSEnji Cooper break;
233e4520c8bSEnji Cooper case POINT_CONVERSION_HYBRID:
234e4520c8bSEnji Cooper glabel = "Generator (hybrid):";
235e4520c8bSEnji Cooper break;
236e4520c8bSEnji Cooper default:
237e4520c8bSEnji Cooper return 0;
238e4520c8bSEnji Cooper }
239e4520c8bSEnji Cooper
240e4520c8bSEnji Cooper buflen = EC_POINT_point2buf(group, point, form, &buf, ctx);
241e4520c8bSEnji Cooper if (buflen == 0)
242e4520c8bSEnji Cooper return 0;
243e4520c8bSEnji Cooper
24429536654SEnji Cooper ret = ossl_bio_print_labeled_buf(out, glabel, buf, buflen);
245e4520c8bSEnji Cooper OPENSSL_clear_free(buf, buflen);
246e4520c8bSEnji Cooper return ret;
247e4520c8bSEnji Cooper }
248e4520c8bSEnji Cooper
249e4520c8bSEnji Cooper /* Print explicit parameters */
ec_param_explicit_to_text(BIO * out,const EC_GROUP * group,OSSL_LIB_CTX * libctx)250e4520c8bSEnji Cooper static int ec_param_explicit_to_text(BIO *out, const EC_GROUP *group,
251e4520c8bSEnji Cooper OSSL_LIB_CTX *libctx)
252e4520c8bSEnji Cooper {
253e4520c8bSEnji Cooper int ret = 0, tmp_nid;
254e4520c8bSEnji Cooper BN_CTX *ctx = NULL;
255e4520c8bSEnji Cooper const BIGNUM *order = NULL, *cofactor = NULL;
256e4520c8bSEnji Cooper const unsigned char *seed;
257e4520c8bSEnji Cooper size_t seed_len = 0;
258e4520c8bSEnji Cooper
259e4520c8bSEnji Cooper ctx = BN_CTX_new_ex(libctx);
260e4520c8bSEnji Cooper if (ctx == NULL)
261e4520c8bSEnji Cooper return 0;
262e4520c8bSEnji Cooper BN_CTX_start(ctx);
263e4520c8bSEnji Cooper
264e4520c8bSEnji Cooper tmp_nid = EC_GROUP_get_field_type(group);
265e4520c8bSEnji Cooper order = EC_GROUP_get0_order(group);
266e4520c8bSEnji Cooper if (order == NULL)
267e4520c8bSEnji Cooper goto err;
268e4520c8bSEnji Cooper
269e4520c8bSEnji Cooper seed = EC_GROUP_get0_seed(group);
270e4520c8bSEnji Cooper if (seed != NULL)
271e4520c8bSEnji Cooper seed_len = EC_GROUP_get_seed_len(group);
272e4520c8bSEnji Cooper cofactor = EC_GROUP_get0_cofactor(group);
273e4520c8bSEnji Cooper
274e4520c8bSEnji Cooper /* print the 'short name' of the field type */
275e4520c8bSEnji Cooper if (BIO_printf(out, "Field Type: %s\n", OBJ_nid2sn(tmp_nid)) <= 0
276e4520c8bSEnji Cooper || !ec_param_explicit_curve_to_text(out, group, ctx)
277e4520c8bSEnji Cooper || !ec_param_explicit_gen_to_text(out, group, ctx)
27829536654SEnji Cooper || !ossl_bio_print_labeled_bignum(out, "Order: ", order)
279e4520c8bSEnji Cooper || (cofactor != NULL
28029536654SEnji Cooper && !ossl_bio_print_labeled_bignum(out, "Cofactor: ", cofactor))
281e4520c8bSEnji Cooper || (seed != NULL
28229536654SEnji Cooper && !ossl_bio_print_labeled_buf(out, "Seed:", seed, seed_len)))
283e4520c8bSEnji Cooper goto err;
284e4520c8bSEnji Cooper ret = 1;
285e4520c8bSEnji Cooper err:
286e4520c8bSEnji Cooper BN_CTX_end(ctx);
287e4520c8bSEnji Cooper BN_CTX_free(ctx);
288e4520c8bSEnji Cooper return ret;
289e4520c8bSEnji Cooper }
290e4520c8bSEnji Cooper
ec_param_to_text(BIO * out,const EC_GROUP * group,OSSL_LIB_CTX * libctx)291e4520c8bSEnji Cooper static int ec_param_to_text(BIO *out, const EC_GROUP *group,
292e4520c8bSEnji Cooper OSSL_LIB_CTX *libctx)
293e4520c8bSEnji Cooper {
294e4520c8bSEnji Cooper if (EC_GROUP_get_asn1_flag(group) & OPENSSL_EC_NAMED_CURVE) {
295e4520c8bSEnji Cooper const char *curve_name;
296e4520c8bSEnji Cooper int curve_nid = EC_GROUP_get_curve_name(group);
297e4520c8bSEnji Cooper
298e4520c8bSEnji Cooper /* Explicit parameters */
299e4520c8bSEnji Cooper if (curve_nid == NID_undef)
300e4520c8bSEnji Cooper return 0;
301e4520c8bSEnji Cooper
302e4520c8bSEnji Cooper if (BIO_printf(out, "%s: %s\n", "ASN1 OID", OBJ_nid2sn(curve_nid)) <= 0)
303e4520c8bSEnji Cooper return 0;
304e4520c8bSEnji Cooper
305e4520c8bSEnji Cooper curve_name = EC_curve_nid2nist(curve_nid);
306e4520c8bSEnji Cooper return (curve_name == NULL
307e4520c8bSEnji Cooper || BIO_printf(out, "%s: %s\n", "NIST CURVE", curve_name) > 0);
308e4520c8bSEnji Cooper } else {
309e4520c8bSEnji Cooper return ec_param_explicit_to_text(out, group, libctx);
310e4520c8bSEnji Cooper }
311e4520c8bSEnji Cooper }
312e4520c8bSEnji Cooper
ec_to_text(BIO * out,const void * key,int selection)313e4520c8bSEnji Cooper static int ec_to_text(BIO *out, const void *key, int selection)
314e4520c8bSEnji Cooper {
315e4520c8bSEnji Cooper const EC_KEY *ec = key;
316e4520c8bSEnji Cooper const char *type_label = NULL;
317e4520c8bSEnji Cooper unsigned char *priv = NULL, *pub = NULL;
318e4520c8bSEnji Cooper size_t priv_len = 0, pub_len = 0;
319e4520c8bSEnji Cooper const EC_GROUP *group;
320e4520c8bSEnji Cooper int ret = 0;
321e4520c8bSEnji Cooper
322e4520c8bSEnji Cooper if (out == NULL || ec == NULL) {
323e4520c8bSEnji Cooper ERR_raise(ERR_LIB_PROV, ERR_R_PASSED_NULL_PARAMETER);
324e4520c8bSEnji Cooper return 0;
325e4520c8bSEnji Cooper }
326e4520c8bSEnji Cooper
327e4520c8bSEnji Cooper if ((group = EC_KEY_get0_group(ec)) == NULL) {
328e4520c8bSEnji Cooper ERR_raise(ERR_LIB_PROV, PROV_R_INVALID_KEY);
329e4520c8bSEnji Cooper return 0;
330e4520c8bSEnji Cooper }
331e4520c8bSEnji Cooper
332e4520c8bSEnji Cooper if ((selection & OSSL_KEYMGMT_SELECT_PRIVATE_KEY) != 0)
333e4520c8bSEnji Cooper type_label = "Private-Key";
334e4520c8bSEnji Cooper else if ((selection & OSSL_KEYMGMT_SELECT_PUBLIC_KEY) != 0)
335e4520c8bSEnji Cooper type_label = "Public-Key";
336e4520c8bSEnji Cooper else if ((selection & OSSL_KEYMGMT_SELECT_DOMAIN_PARAMETERS) != 0)
33729536654SEnji Cooper if (EC_GROUP_get_curve_name(group) != NID_sm2)
338e4520c8bSEnji Cooper type_label = "EC-Parameters";
339e4520c8bSEnji Cooper
340e4520c8bSEnji Cooper if ((selection & OSSL_KEYMGMT_SELECT_PRIVATE_KEY) != 0) {
341e4520c8bSEnji Cooper const BIGNUM *priv_key = EC_KEY_get0_private_key(ec);
342e4520c8bSEnji Cooper
343e4520c8bSEnji Cooper if (priv_key == NULL) {
344e4520c8bSEnji Cooper ERR_raise(ERR_LIB_PROV, PROV_R_NOT_A_PRIVATE_KEY);
345e4520c8bSEnji Cooper goto err;
346e4520c8bSEnji Cooper }
347e4520c8bSEnji Cooper priv_len = EC_KEY_priv2buf(ec, &priv);
348e4520c8bSEnji Cooper if (priv_len == 0)
349e4520c8bSEnji Cooper goto err;
350e4520c8bSEnji Cooper }
351825caf7eSEd Maste if ((selection & OSSL_KEYMGMT_SELECT_KEYPAIR) != 0) {
352e4520c8bSEnji Cooper const EC_POINT *pub_pt = EC_KEY_get0_public_key(ec);
353e4520c8bSEnji Cooper
354e4520c8bSEnji Cooper if (pub_pt == NULL) {
355e4520c8bSEnji Cooper ERR_raise(ERR_LIB_PROV, PROV_R_NOT_A_PUBLIC_KEY);
356e4520c8bSEnji Cooper goto err;
357e4520c8bSEnji Cooper }
358e4520c8bSEnji Cooper
359e4520c8bSEnji Cooper pub_len = EC_KEY_key2buf(ec, EC_KEY_get_conv_form(ec), &pub, NULL);
360e4520c8bSEnji Cooper if (pub_len == 0)
361e4520c8bSEnji Cooper goto err;
362e4520c8bSEnji Cooper }
363e4520c8bSEnji Cooper
36429536654SEnji Cooper if (type_label != NULL
36529536654SEnji Cooper && BIO_printf(out, "%s: (%d bit)\n", type_label,
366808413daSEnji Cooper EC_GROUP_order_bits(group))
367808413daSEnji Cooper <= 0)
368e4520c8bSEnji Cooper goto err;
369e4520c8bSEnji Cooper if (priv != NULL
37029536654SEnji Cooper && !ossl_bio_print_labeled_buf(out, "priv:", priv, priv_len))
371e4520c8bSEnji Cooper goto err;
372e4520c8bSEnji Cooper if (pub != NULL
37329536654SEnji Cooper && !ossl_bio_print_labeled_buf(out, "pub:", pub, pub_len))
374e4520c8bSEnji Cooper goto err;
375e4520c8bSEnji Cooper if ((selection & OSSL_KEYMGMT_SELECT_DOMAIN_PARAMETERS) != 0)
376e4520c8bSEnji Cooper ret = ec_param_to_text(out, group, ossl_ec_key_get_libctx(ec));
377e4520c8bSEnji Cooper err:
378e4520c8bSEnji Cooper OPENSSL_clear_free(priv, priv_len);
379e4520c8bSEnji Cooper OPENSSL_free(pub);
380e4520c8bSEnji Cooper return ret;
381e4520c8bSEnji Cooper }
382e4520c8bSEnji Cooper #endif
383e4520c8bSEnji Cooper
384e4520c8bSEnji Cooper /* ---------------------------------------------------------------------- */
385e4520c8bSEnji Cooper
38629536654SEnji Cooper #ifndef OPENSSL_NO_ECX
ecx_to_text(BIO * out,const void * key,int selection)387e4520c8bSEnji Cooper static int ecx_to_text(BIO *out, const void *key, int selection)
388e4520c8bSEnji Cooper {
389e4520c8bSEnji Cooper const ECX_KEY *ecx = key;
390e4520c8bSEnji Cooper const char *type_label = NULL;
391e4520c8bSEnji Cooper
392e4520c8bSEnji Cooper if (out == NULL || ecx == NULL) {
393e4520c8bSEnji Cooper ERR_raise(ERR_LIB_PROV, ERR_R_PASSED_NULL_PARAMETER);
394e4520c8bSEnji Cooper return 0;
395e4520c8bSEnji Cooper }
396e4520c8bSEnji Cooper
397825caf7eSEd Maste switch (ecx->type) {
398825caf7eSEd Maste case ECX_KEY_TYPE_X25519:
399825caf7eSEd Maste type_label = "X25519";
400825caf7eSEd Maste break;
401825caf7eSEd Maste case ECX_KEY_TYPE_X448:
402825caf7eSEd Maste type_label = "X448";
403825caf7eSEd Maste break;
404825caf7eSEd Maste case ECX_KEY_TYPE_ED25519:
405825caf7eSEd Maste type_label = "ED25519";
406825caf7eSEd Maste break;
407825caf7eSEd Maste case ECX_KEY_TYPE_ED448:
408825caf7eSEd Maste type_label = "ED448";
409825caf7eSEd Maste break;
410825caf7eSEd Maste }
411825caf7eSEd Maste
412e4520c8bSEnji Cooper if ((selection & OSSL_KEYMGMT_SELECT_PRIVATE_KEY) != 0) {
413e4520c8bSEnji Cooper if (ecx->privkey == NULL) {
414e4520c8bSEnji Cooper ERR_raise(ERR_LIB_PROV, PROV_R_NOT_A_PRIVATE_KEY);
415e4520c8bSEnji Cooper return 0;
416e4520c8bSEnji Cooper }
417e4520c8bSEnji Cooper
418825caf7eSEd Maste if (BIO_printf(out, "%s Private-Key:\n", type_label) <= 0)
419825caf7eSEd Maste return 0;
42029536654SEnji Cooper if (!ossl_bio_print_labeled_buf(out, "priv:", ecx->privkey, ecx->keylen))
421825caf7eSEd Maste return 0;
422e4520c8bSEnji Cooper } else if ((selection & OSSL_KEYMGMT_SELECT_PUBLIC_KEY) != 0) {
423e4520c8bSEnji Cooper /* ecx->pubkey is an array, not a pointer... */
424e4520c8bSEnji Cooper if (!ecx->haspubkey) {
425e4520c8bSEnji Cooper ERR_raise(ERR_LIB_PROV, PROV_R_NOT_A_PUBLIC_KEY);
426e4520c8bSEnji Cooper return 0;
427e4520c8bSEnji Cooper }
428e4520c8bSEnji Cooper
429825caf7eSEd Maste if (BIO_printf(out, "%s Public-Key:\n", type_label) <= 0)
430825caf7eSEd Maste return 0;
431e4520c8bSEnji Cooper }
432e4520c8bSEnji Cooper
43329536654SEnji Cooper if (!ossl_bio_print_labeled_buf(out, "pub:", ecx->pubkey, ecx->keylen))
434e4520c8bSEnji Cooper return 0;
435e4520c8bSEnji Cooper
436e4520c8bSEnji Cooper return 1;
437e4520c8bSEnji Cooper }
438e4520c8bSEnji Cooper #endif
439e4520c8bSEnji Cooper
440e4520c8bSEnji Cooper /* ---------------------------------------------------------------------- */
441e4520c8bSEnji Cooper
44229536654SEnji Cooper #ifndef OPENSSL_NO_ML_KEM
ml_kem_to_text(BIO * out,const void * vkey,int selection)44329536654SEnji Cooper static int ml_kem_to_text(BIO *out, const void *vkey, int selection)
44429536654SEnji Cooper {
44529536654SEnji Cooper return ossl_ml_kem_key_to_text(out, (ML_KEM_KEY *)vkey, selection);
44629536654SEnji Cooper }
44729536654SEnji Cooper #endif
44829536654SEnji Cooper
44929536654SEnji Cooper /* ---------------------------------------------------------------------- */
45029536654SEnji Cooper
45129536654SEnji Cooper #ifndef OPENSSL_NO_SLH_DSA
slh_dsa_to_text(BIO * out,const void * key,int selection)45229536654SEnji Cooper static int slh_dsa_to_text(BIO *out, const void *key, int selection)
45329536654SEnji Cooper {
45429536654SEnji Cooper return ossl_slh_dsa_key_to_text(out, (SLH_DSA_KEY *)key, selection);
45529536654SEnji Cooper }
45629536654SEnji Cooper #endif /* OPENSSL_NO_SLH_DSA */
45729536654SEnji Cooper
rsa_to_text(BIO * out,const void * key,int selection)458e4520c8bSEnji Cooper static int rsa_to_text(BIO *out, const void *key, int selection)
459e4520c8bSEnji Cooper {
460e4520c8bSEnji Cooper const RSA *rsa = key;
461e4520c8bSEnji Cooper const char *type_label = "RSA key";
462e4520c8bSEnji Cooper const char *modulus_label = NULL;
463e4520c8bSEnji Cooper const char *exponent_label = NULL;
464e4520c8bSEnji Cooper const BIGNUM *rsa_d = NULL, *rsa_n = NULL, *rsa_e = NULL;
465e4520c8bSEnji Cooper STACK_OF(BIGNUM_const) *factors = NULL;
466e4520c8bSEnji Cooper STACK_OF(BIGNUM_const) *exps = NULL;
467e4520c8bSEnji Cooper STACK_OF(BIGNUM_const) *coeffs = NULL;
468e4520c8bSEnji Cooper int primes;
469e4520c8bSEnji Cooper const RSA_PSS_PARAMS_30 *pss_params = ossl_rsa_get0_pss_params_30((RSA *)rsa);
470e4520c8bSEnji Cooper int ret = 0;
471e4520c8bSEnji Cooper
472e4520c8bSEnji Cooper if (out == NULL || rsa == NULL) {
473e4520c8bSEnji Cooper ERR_raise(ERR_LIB_PROV, ERR_R_PASSED_NULL_PARAMETER);
474e4520c8bSEnji Cooper goto err;
475e4520c8bSEnji Cooper }
476e4520c8bSEnji Cooper
477e4520c8bSEnji Cooper factors = sk_BIGNUM_const_new_null();
478e4520c8bSEnji Cooper exps = sk_BIGNUM_const_new_null();
479e4520c8bSEnji Cooper coeffs = sk_BIGNUM_const_new_null();
480e4520c8bSEnji Cooper
481e4520c8bSEnji Cooper if (factors == NULL || exps == NULL || coeffs == NULL) {
48229536654SEnji Cooper ERR_raise(ERR_LIB_PROV, ERR_R_CRYPTO_LIB);
483e4520c8bSEnji Cooper goto err;
484e4520c8bSEnji Cooper }
485e4520c8bSEnji Cooper
486e4520c8bSEnji Cooper if ((selection & OSSL_KEYMGMT_SELECT_PRIVATE_KEY) != 0) {
487e4520c8bSEnji Cooper type_label = "Private-Key";
488e4520c8bSEnji Cooper modulus_label = "modulus:";
489e4520c8bSEnji Cooper exponent_label = "publicExponent:";
490e4520c8bSEnji Cooper } else if ((selection & OSSL_KEYMGMT_SELECT_PUBLIC_KEY) != 0) {
491e4520c8bSEnji Cooper type_label = "Public-Key";
492e4520c8bSEnji Cooper modulus_label = "Modulus:";
493e4520c8bSEnji Cooper exponent_label = "Exponent:";
494e4520c8bSEnji Cooper }
495e4520c8bSEnji Cooper
496e4520c8bSEnji Cooper RSA_get0_key(rsa, &rsa_n, &rsa_e, &rsa_d);
497e4520c8bSEnji Cooper ossl_rsa_get0_all_params((RSA *)rsa, factors, exps, coeffs);
498e4520c8bSEnji Cooper primes = sk_BIGNUM_const_num(factors);
499e4520c8bSEnji Cooper
500e4520c8bSEnji Cooper if ((selection & OSSL_KEYMGMT_SELECT_PRIVATE_KEY) != 0) {
501e4520c8bSEnji Cooper if (BIO_printf(out, "%s: (%d bit, %d primes)\n",
502808413daSEnji Cooper type_label, BN_num_bits(rsa_n), primes)
503808413daSEnji Cooper <= 0)
504e4520c8bSEnji Cooper goto err;
505e4520c8bSEnji Cooper } else {
506e4520c8bSEnji Cooper if (BIO_printf(out, "%s: (%d bit)\n",
507808413daSEnji Cooper type_label, BN_num_bits(rsa_n))
508808413daSEnji Cooper <= 0)
509e4520c8bSEnji Cooper goto err;
510e4520c8bSEnji Cooper }
511e4520c8bSEnji Cooper
51229536654SEnji Cooper if (!ossl_bio_print_labeled_bignum(out, modulus_label, rsa_n))
513e4520c8bSEnji Cooper goto err;
51429536654SEnji Cooper if (!ossl_bio_print_labeled_bignum(out, exponent_label, rsa_e))
515e4520c8bSEnji Cooper goto err;
516e4520c8bSEnji Cooper if ((selection & OSSL_KEYMGMT_SELECT_PRIVATE_KEY) != 0) {
517e4520c8bSEnji Cooper int i;
518e4520c8bSEnji Cooper
51929536654SEnji Cooper if (!ossl_bio_print_labeled_bignum(out, "privateExponent:", rsa_d))
520e4520c8bSEnji Cooper goto err;
52129536654SEnji Cooper if (!ossl_bio_print_labeled_bignum(out, "prime1:",
522e4520c8bSEnji Cooper sk_BIGNUM_const_value(factors, 0)))
523e4520c8bSEnji Cooper goto err;
52429536654SEnji Cooper if (!ossl_bio_print_labeled_bignum(out, "prime2:",
525e4520c8bSEnji Cooper sk_BIGNUM_const_value(factors, 1)))
526e4520c8bSEnji Cooper goto err;
52729536654SEnji Cooper if (!ossl_bio_print_labeled_bignum(out, "exponent1:",
528e4520c8bSEnji Cooper sk_BIGNUM_const_value(exps, 0)))
529e4520c8bSEnji Cooper goto err;
53029536654SEnji Cooper if (!ossl_bio_print_labeled_bignum(out, "exponent2:",
531e4520c8bSEnji Cooper sk_BIGNUM_const_value(exps, 1)))
532e4520c8bSEnji Cooper goto err;
53329536654SEnji Cooper if (!ossl_bio_print_labeled_bignum(out, "coefficient:",
534e4520c8bSEnji Cooper sk_BIGNUM_const_value(coeffs, 0)))
535e4520c8bSEnji Cooper goto err;
536e4520c8bSEnji Cooper for (i = 2; i < sk_BIGNUM_const_num(factors); i++) {
537e4520c8bSEnji Cooper if (BIO_printf(out, "prime%d:", i + 1) <= 0)
538e4520c8bSEnji Cooper goto err;
53929536654SEnji Cooper if (!ossl_bio_print_labeled_bignum(out, NULL,
540e4520c8bSEnji Cooper sk_BIGNUM_const_value(factors, i)))
541e4520c8bSEnji Cooper goto err;
542e4520c8bSEnji Cooper if (BIO_printf(out, "exponent%d:", i + 1) <= 0)
543e4520c8bSEnji Cooper goto err;
54429536654SEnji Cooper if (!ossl_bio_print_labeled_bignum(out, NULL,
545e4520c8bSEnji Cooper sk_BIGNUM_const_value(exps, i)))
546e4520c8bSEnji Cooper goto err;
547e4520c8bSEnji Cooper if (BIO_printf(out, "coefficient%d:", i + 1) <= 0)
548e4520c8bSEnji Cooper goto err;
54929536654SEnji Cooper if (!ossl_bio_print_labeled_bignum(out, NULL,
550e4520c8bSEnji Cooper sk_BIGNUM_const_value(coeffs, i - 1)))
551e4520c8bSEnji Cooper goto err;
552e4520c8bSEnji Cooper }
553e4520c8bSEnji Cooper }
554e4520c8bSEnji Cooper
555e4520c8bSEnji Cooper if ((selection & OSSL_KEYMGMT_SELECT_OTHER_PARAMETERS) != 0) {
556e4520c8bSEnji Cooper switch (RSA_test_flags(rsa, RSA_FLAG_TYPE_MASK)) {
557e4520c8bSEnji Cooper case RSA_FLAG_TYPE_RSA:
558e4520c8bSEnji Cooper if (!ossl_rsa_pss_params_30_is_unrestricted(pss_params)) {
559e4520c8bSEnji Cooper if (BIO_printf(out, "(INVALID PSS PARAMETERS)\n") <= 0)
560e4520c8bSEnji Cooper goto err;
561e4520c8bSEnji Cooper }
562e4520c8bSEnji Cooper break;
563e4520c8bSEnji Cooper case RSA_FLAG_TYPE_RSASSAPSS:
564e4520c8bSEnji Cooper if (ossl_rsa_pss_params_30_is_unrestricted(pss_params)) {
565e4520c8bSEnji Cooper if (BIO_printf(out, "No PSS parameter restrictions\n") <= 0)
566e4520c8bSEnji Cooper goto err;
567e4520c8bSEnji Cooper } else {
568e4520c8bSEnji Cooper int hashalg_nid = ossl_rsa_pss_params_30_hashalg(pss_params);
569808413daSEnji Cooper int maskgenalg_nid = ossl_rsa_pss_params_30_maskgenalg(pss_params);
570808413daSEnji Cooper int maskgenhashalg_nid = ossl_rsa_pss_params_30_maskgenhashalg(pss_params);
571e4520c8bSEnji Cooper int saltlen = ossl_rsa_pss_params_30_saltlen(pss_params);
572808413daSEnji Cooper int trailerfield = ossl_rsa_pss_params_30_trailerfield(pss_params);
573e4520c8bSEnji Cooper
574e4520c8bSEnji Cooper if (BIO_printf(out, "PSS parameter restrictions:\n") <= 0)
575e4520c8bSEnji Cooper goto err;
576e4520c8bSEnji Cooper if (BIO_printf(out, " Hash Algorithm: %s%s\n",
577e4520c8bSEnji Cooper ossl_rsa_oaeppss_nid2name(hashalg_nid),
578e4520c8bSEnji Cooper (hashalg_nid == NID_sha1
579808413daSEnji Cooper ? " (default)"
580808413daSEnji Cooper : ""))
581808413daSEnji Cooper <= 0)
582e4520c8bSEnji Cooper goto err;
583e4520c8bSEnji Cooper if (BIO_printf(out, " Mask Algorithm: %s with %s%s\n",
584e4520c8bSEnji Cooper ossl_rsa_mgf_nid2name(maskgenalg_nid),
585e4520c8bSEnji Cooper ossl_rsa_oaeppss_nid2name(maskgenhashalg_nid),
586e4520c8bSEnji Cooper (maskgenalg_nid == NID_mgf1
587e4520c8bSEnji Cooper && maskgenhashalg_nid == NID_sha1
588808413daSEnji Cooper ? " (default)"
589808413daSEnji Cooper : ""))
590808413daSEnji Cooper <= 0)
591e4520c8bSEnji Cooper goto err;
592e4520c8bSEnji Cooper if (BIO_printf(out, " Minimum Salt Length: %d%s\n",
593e4520c8bSEnji Cooper saltlen,
594808413daSEnji Cooper (saltlen == 20 ? " (default)" : ""))
595808413daSEnji Cooper <= 0)
596e4520c8bSEnji Cooper goto err;
597e4520c8bSEnji Cooper if (BIO_printf(out, " Trailer Field: 0x%x%s\n",
598e4520c8bSEnji Cooper trailerfield,
599808413daSEnji Cooper (trailerfield == 1 ? " (default)" : ""))
600808413daSEnji Cooper <= 0)
601e4520c8bSEnji Cooper goto err;
602e4520c8bSEnji Cooper }
603e4520c8bSEnji Cooper break;
604e4520c8bSEnji Cooper }
605e4520c8bSEnji Cooper }
606e4520c8bSEnji Cooper
607e4520c8bSEnji Cooper ret = 1;
608e4520c8bSEnji Cooper err:
609e4520c8bSEnji Cooper sk_BIGNUM_const_free(factors);
610e4520c8bSEnji Cooper sk_BIGNUM_const_free(exps);
611e4520c8bSEnji Cooper sk_BIGNUM_const_free(coeffs);
612e4520c8bSEnji Cooper return ret;
613e4520c8bSEnji Cooper }
614e4520c8bSEnji Cooper
61529536654SEnji Cooper /* ---------------------------------------------------------------------- */
616e4520c8bSEnji Cooper
61729536654SEnji Cooper #ifndef OPENSSL_NO_ML_DSA
ml_dsa_to_text(BIO * out,const void * key,int selection)61829536654SEnji Cooper static int ml_dsa_to_text(BIO *out, const void *key, int selection)
61929536654SEnji Cooper {
62029536654SEnji Cooper return ossl_ml_dsa_key_to_text(out, (ML_DSA_KEY *)key, selection);
62129536654SEnji Cooper }
62229536654SEnji Cooper #endif /* OPENSSL_NO_ML_DSA */
623e4520c8bSEnji Cooper /* ---------------------------------------------------------------------- */
624e4520c8bSEnji Cooper
key2text_newctx(void * provctx)625e4520c8bSEnji Cooper static void *key2text_newctx(void *provctx)
626e4520c8bSEnji Cooper {
627e4520c8bSEnji Cooper return provctx;
628e4520c8bSEnji Cooper }
629e4520c8bSEnji Cooper
key2text_freectx(ossl_unused void * vctx)630e4520c8bSEnji Cooper static void key2text_freectx(ossl_unused void *vctx)
631e4520c8bSEnji Cooper {
632e4520c8bSEnji Cooper }
633e4520c8bSEnji Cooper
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)634e4520c8bSEnji Cooper static int key2text_encode(void *vctx, const void *key, int selection,
635e4520c8bSEnji Cooper OSSL_CORE_BIO *cout,
636e4520c8bSEnji Cooper int (*key2text)(BIO *out, const void *key,
637e4520c8bSEnji Cooper int selection),
638e4520c8bSEnji Cooper OSSL_PASSPHRASE_CALLBACK *cb, void *cbarg)
639e4520c8bSEnji Cooper {
640e4520c8bSEnji Cooper BIO *out = ossl_bio_new_from_core_bio(vctx, cout);
641e4520c8bSEnji Cooper int ret;
642e4520c8bSEnji Cooper
643e4520c8bSEnji Cooper if (out == NULL)
644e4520c8bSEnji Cooper return 0;
645e4520c8bSEnji Cooper
646e4520c8bSEnji Cooper ret = key2text(out, key, selection);
647e4520c8bSEnji Cooper BIO_free(out);
648e4520c8bSEnji Cooper
649e4520c8bSEnji Cooper return ret;
650e4520c8bSEnji Cooper }
651e4520c8bSEnji Cooper
652e4520c8bSEnji Cooper #define MAKE_TEXT_ENCODER(impl, type) \
653e4520c8bSEnji Cooper static OSSL_FUNC_encoder_import_object_fn \
654e4520c8bSEnji Cooper impl##2text_import_object; \
655e4520c8bSEnji Cooper static OSSL_FUNC_encoder_free_object_fn \
656e4520c8bSEnji Cooper impl##2text_free_object; \
657e4520c8bSEnji Cooper static OSSL_FUNC_encoder_encode_fn impl##2text_encode; \
658e4520c8bSEnji Cooper \
659e4520c8bSEnji Cooper static void *impl##2text_import_object(void *ctx, int selection, \
660e4520c8bSEnji Cooper const OSSL_PARAM params[]) \
661e4520c8bSEnji Cooper { \
662e4520c8bSEnji Cooper return ossl_prov_import_key(ossl_##impl##_keymgmt_functions, \
663e4520c8bSEnji Cooper ctx, selection, params); \
664e4520c8bSEnji Cooper } \
665e4520c8bSEnji Cooper static void impl##2text_free_object(void *key) \
666e4520c8bSEnji Cooper { \
667e4520c8bSEnji Cooper ossl_prov_free_key(ossl_##impl##_keymgmt_functions, key); \
668e4520c8bSEnji Cooper } \
669e4520c8bSEnji Cooper static int impl##2text_encode(void *vctx, OSSL_CORE_BIO *cout, \
670e4520c8bSEnji Cooper const void *key, \
671e4520c8bSEnji Cooper const OSSL_PARAM key_abstract[], \
672e4520c8bSEnji Cooper int selection, \
673e4520c8bSEnji Cooper OSSL_PASSPHRASE_CALLBACK *cb, \
674e4520c8bSEnji Cooper void *cbarg) \
675e4520c8bSEnji Cooper { \
676e4520c8bSEnji Cooper /* We don't deal with abstract objects */ \
677e4520c8bSEnji Cooper if (key_abstract != NULL) { \
678e4520c8bSEnji Cooper ERR_raise(ERR_LIB_PROV, ERR_R_PASSED_INVALID_ARGUMENT); \
679e4520c8bSEnji Cooper return 0; \
680e4520c8bSEnji Cooper } \
681e4520c8bSEnji Cooper return key2text_encode(vctx, key, selection, cout, \
682e4520c8bSEnji Cooper type##_to_text, cb, cbarg); \
683e4520c8bSEnji Cooper } \
684e4520c8bSEnji Cooper const OSSL_DISPATCH ossl_##impl##_to_text_encoder_functions[] = { \
685e4520c8bSEnji Cooper { OSSL_FUNC_ENCODER_NEWCTX, \
686e4520c8bSEnji Cooper (void (*)(void))key2text_newctx }, \
687e4520c8bSEnji Cooper { OSSL_FUNC_ENCODER_FREECTX, \
688e4520c8bSEnji Cooper (void (*)(void))key2text_freectx }, \
689e4520c8bSEnji Cooper { OSSL_FUNC_ENCODER_IMPORT_OBJECT, \
690e4520c8bSEnji Cooper (void (*)(void))impl##2text_import_object }, \
691e4520c8bSEnji Cooper { OSSL_FUNC_ENCODER_FREE_OBJECT, \
692e4520c8bSEnji Cooper (void (*)(void))impl##2text_free_object }, \
693e4520c8bSEnji Cooper { OSSL_FUNC_ENCODER_ENCODE, \
694e4520c8bSEnji Cooper (void (*)(void))impl##2text_encode }, \
69529536654SEnji Cooper OSSL_DISPATCH_END \
696e4520c8bSEnji Cooper }
697e4520c8bSEnji Cooper
698e4520c8bSEnji Cooper #ifndef OPENSSL_NO_DH
699e4520c8bSEnji Cooper MAKE_TEXT_ENCODER(dh, dh);
700e4520c8bSEnji Cooper MAKE_TEXT_ENCODER(dhx, dh);
701e4520c8bSEnji Cooper #endif
702e4520c8bSEnji Cooper #ifndef OPENSSL_NO_DSA
703e4520c8bSEnji Cooper MAKE_TEXT_ENCODER(dsa, dsa);
704e4520c8bSEnji Cooper #endif
705e4520c8bSEnji Cooper #ifndef OPENSSL_NO_EC
706e4520c8bSEnji Cooper MAKE_TEXT_ENCODER(ec, ec);
707e4520c8bSEnji Cooper #ifndef OPENSSL_NO_SM2
708e4520c8bSEnji Cooper MAKE_TEXT_ENCODER(sm2, ec);
709e4520c8bSEnji Cooper #endif
71029536654SEnji Cooper #ifndef OPENSSL_NO_ECX
711e4520c8bSEnji Cooper MAKE_TEXT_ENCODER(ed25519, ecx);
712e4520c8bSEnji Cooper MAKE_TEXT_ENCODER(ed448, ecx);
713e4520c8bSEnji Cooper MAKE_TEXT_ENCODER(x25519, ecx);
714e4520c8bSEnji Cooper MAKE_TEXT_ENCODER(x448, ecx);
715e4520c8bSEnji Cooper #endif
71629536654SEnji Cooper #endif
71729536654SEnji Cooper #ifndef OPENSSL_NO_ML_KEM
71829536654SEnji Cooper MAKE_TEXT_ENCODER(ml_kem_512, ml_kem);
71929536654SEnji Cooper MAKE_TEXT_ENCODER(ml_kem_768, ml_kem);
72029536654SEnji Cooper MAKE_TEXT_ENCODER(ml_kem_1024, ml_kem);
72129536654SEnji Cooper #endif
722e4520c8bSEnji Cooper MAKE_TEXT_ENCODER(rsa, rsa);
723e4520c8bSEnji Cooper MAKE_TEXT_ENCODER(rsapss, rsa);
72429536654SEnji Cooper
72529536654SEnji Cooper #ifndef OPENSSL_NO_ML_DSA
72629536654SEnji Cooper MAKE_TEXT_ENCODER(ml_dsa_44, ml_dsa);
72729536654SEnji Cooper MAKE_TEXT_ENCODER(ml_dsa_65, ml_dsa);
72829536654SEnji Cooper MAKE_TEXT_ENCODER(ml_dsa_87, ml_dsa);
72929536654SEnji Cooper #endif
73029536654SEnji Cooper
73129536654SEnji Cooper #ifndef OPENSSL_NO_SLH_DSA
73229536654SEnji Cooper MAKE_TEXT_ENCODER(slh_dsa_sha2_128s, slh_dsa);
73329536654SEnji Cooper MAKE_TEXT_ENCODER(slh_dsa_sha2_128f, slh_dsa);
73429536654SEnji Cooper MAKE_TEXT_ENCODER(slh_dsa_sha2_192s, slh_dsa);
73529536654SEnji Cooper MAKE_TEXT_ENCODER(slh_dsa_sha2_192f, slh_dsa);
73629536654SEnji Cooper MAKE_TEXT_ENCODER(slh_dsa_sha2_256s, slh_dsa);
73729536654SEnji Cooper MAKE_TEXT_ENCODER(slh_dsa_sha2_256f, slh_dsa);
73829536654SEnji Cooper MAKE_TEXT_ENCODER(slh_dsa_shake_128s, slh_dsa);
73929536654SEnji Cooper MAKE_TEXT_ENCODER(slh_dsa_shake_128f, slh_dsa);
74029536654SEnji Cooper MAKE_TEXT_ENCODER(slh_dsa_shake_192s, slh_dsa);
74129536654SEnji Cooper MAKE_TEXT_ENCODER(slh_dsa_shake_192f, slh_dsa);
74229536654SEnji Cooper MAKE_TEXT_ENCODER(slh_dsa_shake_256s, slh_dsa);
74329536654SEnji Cooper MAKE_TEXT_ENCODER(slh_dsa_shake_256f, slh_dsa);
74429536654SEnji Cooper #endif
745