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