1 /*
2 * Copyright 2020-2023 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 <ctype.h>
16
17 #include <openssl/core.h>
18 #include <openssl/core_dispatch.h>
19 #include <openssl/core_names.h>
20 #include <openssl/bn.h>
21 #include <openssl/err.h>
22 #include <openssl/safestack.h>
23 #include <openssl/proverr.h>
24 #include "internal/ffc.h"
25 #include "crypto/bn.h" /* bn_get_words() */
26 #include "crypto/dh.h" /* ossl_dh_get0_params() */
27 #include "crypto/dsa.h" /* ossl_dsa_get0_params() */
28 #include "crypto/ec.h" /* ossl_ec_key_get_libctx */
29 #include "crypto/ecx.h" /* ECX_KEY, etc... */
30 #include "crypto/rsa.h" /* RSA_PSS_PARAMS_30, etc... */
31 #include "prov/bio.h"
32 #include "prov/implementations.h"
33 #include "endecoder_local.h"
34
DEFINE_SPECIAL_STACK_OF_CONST(BIGNUM_const,BIGNUM)35 DEFINE_SPECIAL_STACK_OF_CONST(BIGNUM_const, BIGNUM)
36
37 # ifdef SIXTY_FOUR_BIT_LONG
38 # define BN_FMTu "%lu"
39 # define BN_FMTx "%lx"
40 # endif
41
42 # ifdef SIXTY_FOUR_BIT
43 # define BN_FMTu "%llu"
44 # define BN_FMTx "%llx"
45 # endif
46
47 # ifdef THIRTY_TWO_BIT
48 # define BN_FMTu "%u"
49 # define BN_FMTx "%x"
50 # endif
51
52 static int print_labeled_bignum(BIO *out, const char *label, const BIGNUM *bn)
53 {
54 int ret = 0, use_sep = 0;
55 char *hex_str = NULL, *p;
56 const char spaces[] = " ";
57 const char *post_label_spc = " ";
58
59 const char *neg = "";
60 int bytes;
61
62 if (bn == NULL)
63 return 0;
64 if (label == NULL) {
65 label = "";
66 post_label_spc = "";
67 }
68
69 if (BN_is_zero(bn))
70 return BIO_printf(out, "%s%s0\n", label, post_label_spc);
71
72 if (BN_num_bytes(bn) <= BN_BYTES) {
73 BN_ULONG *words = bn_get_words(bn);
74
75 if (BN_is_negative(bn))
76 neg = "-";
77
78 return BIO_printf(out, "%s%s%s" BN_FMTu " (%s0x" BN_FMTx ")\n",
79 label, post_label_spc, neg, words[0], neg, words[0]);
80 }
81
82 hex_str = BN_bn2hex(bn);
83 if (hex_str == NULL)
84 return 0;
85
86 p = hex_str;
87 if (*p == '-') {
88 ++p;
89 neg = " (Negative)";
90 }
91 if (BIO_printf(out, "%s%s\n", label, neg) <= 0)
92 goto err;
93
94 /* Keep track of how many bytes we have printed out so far */
95 bytes = 0;
96
97 if (BIO_printf(out, "%s", spaces) <= 0)
98 goto err;
99
100 /* Add a leading 00 if the top bit is set */
101 if (*p >= '8') {
102 if (BIO_printf(out, "%02x", 0) <= 0)
103 goto err;
104 ++bytes;
105 use_sep = 1;
106 }
107 while (*p != '\0') {
108 /* Do a newline after every 15 hex bytes + add the space indent */
109 if ((bytes % 15) == 0 && bytes > 0) {
110 if (BIO_printf(out, ":\n%s", spaces) <= 0)
111 goto err;
112 use_sep = 0; /* The first byte on the next line doesn't have a : */
113 }
114 if (BIO_printf(out, "%s%c%c", use_sep ? ":" : "",
115 tolower((unsigned char)p[0]),
116 tolower((unsigned char)p[1])) <= 0)
117 goto err;
118 ++bytes;
119 p += 2;
120 use_sep = 1;
121 }
122 if (BIO_printf(out, "\n") <= 0)
123 goto err;
124 ret = 1;
125 err:
126 OPENSSL_free(hex_str);
127 return ret;
128 }
129
130 /* Number of octets per line */
131 #define LABELED_BUF_PRINT_WIDTH 15
132
133 #if !defined(OPENSSL_NO_DH) || !defined(OPENSSL_NO_DSA) || !defined(OPENSSL_NO_EC)
print_labeled_buf(BIO * out,const char * label,const unsigned char * buf,size_t buflen)134 static int print_labeled_buf(BIO *out, const char *label,
135 const unsigned char *buf, size_t buflen)
136 {
137 size_t i;
138
139 if (BIO_printf(out, "%s\n", label) <= 0)
140 return 0;
141
142 for (i = 0; i < buflen; i++) {
143 if ((i % LABELED_BUF_PRINT_WIDTH) == 0) {
144 if (i > 0 && BIO_printf(out, "\n") <= 0)
145 return 0;
146 if (BIO_printf(out, " ") <= 0)
147 return 0;
148 }
149
150 if (BIO_printf(out, "%02x%s", buf[i],
151 (i == buflen - 1) ? "" : ":") <= 0)
152 return 0;
153 }
154 if (BIO_printf(out, "\n") <= 0)
155 return 0;
156
157 return 1;
158 }
159 #endif
160
161 #if !defined(OPENSSL_NO_DH) || !defined(OPENSSL_NO_DSA)
ffc_params_to_text(BIO * out,const FFC_PARAMS * ffc)162 static int ffc_params_to_text(BIO *out, const FFC_PARAMS *ffc)
163 {
164 if (ffc->nid != NID_undef) {
165 #ifndef OPENSSL_NO_DH
166 const DH_NAMED_GROUP *group = ossl_ffc_uid_to_dh_named_group(ffc->nid);
167 const char *name = ossl_ffc_named_group_get_name(group);
168
169 if (name == NULL)
170 goto err;
171 if (BIO_printf(out, "GROUP: %s\n", name) <= 0)
172 goto err;
173 return 1;
174 #else
175 /* How could this be? We should not have a nid in a no-dh build. */
176 goto err;
177 #endif
178 }
179
180 if (!print_labeled_bignum(out, "P: ", ffc->p))
181 goto err;
182 if (ffc->q != NULL) {
183 if (!print_labeled_bignum(out, "Q: ", ffc->q))
184 goto err;
185 }
186 if (!print_labeled_bignum(out, "G: ", ffc->g))
187 goto err;
188 if (ffc->j != NULL) {
189 if (!print_labeled_bignum(out, "J: ", ffc->j))
190 goto err;
191 }
192 if (ffc->seed != NULL) {
193 if (!print_labeled_buf(out, "SEED:", ffc->seed, ffc->seedlen))
194 goto err;
195 }
196 if (ffc->gindex != -1) {
197 if (BIO_printf(out, "gindex: %d\n", ffc->gindex) <= 0)
198 goto err;
199 }
200 if (ffc->pcounter != -1) {
201 if (BIO_printf(out, "pcounter: %d\n", ffc->pcounter) <= 0)
202 goto err;
203 }
204 if (ffc->h != 0) {
205 if (BIO_printf(out, "h: %d\n", ffc->h) <= 0)
206 goto err;
207 }
208 return 1;
209 err:
210 return 0;
211 }
212 #endif
213
214 /* ---------------------------------------------------------------------- */
215
216 #ifndef OPENSSL_NO_DH
dh_to_text(BIO * out,const void * key,int selection)217 static int dh_to_text(BIO *out, const void *key, int selection)
218 {
219 const DH *dh = key;
220 const char *type_label = NULL;
221 const BIGNUM *priv_key = NULL, *pub_key = NULL;
222 const FFC_PARAMS *params = NULL;
223 const BIGNUM *p = NULL;
224 long length;
225
226 if (out == NULL || dh == NULL) {
227 ERR_raise(ERR_LIB_PROV, ERR_R_PASSED_NULL_PARAMETER);
228 return 0;
229 }
230
231 if ((selection & OSSL_KEYMGMT_SELECT_PRIVATE_KEY) != 0)
232 type_label = "DH Private-Key";
233 else if ((selection & OSSL_KEYMGMT_SELECT_PUBLIC_KEY) != 0)
234 type_label = "DH Public-Key";
235 else if ((selection & OSSL_KEYMGMT_SELECT_DOMAIN_PARAMETERS) != 0)
236 type_label = "DH Parameters";
237
238 if ((selection & OSSL_KEYMGMT_SELECT_PRIVATE_KEY) != 0) {
239 priv_key = DH_get0_priv_key(dh);
240 if (priv_key == NULL) {
241 ERR_raise(ERR_LIB_PROV, PROV_R_NOT_A_PRIVATE_KEY);
242 return 0;
243 }
244 }
245 if ((selection & OSSL_KEYMGMT_SELECT_KEYPAIR) != 0) {
246 pub_key = DH_get0_pub_key(dh);
247 if (pub_key == NULL) {
248 ERR_raise(ERR_LIB_PROV, PROV_R_NOT_A_PUBLIC_KEY);
249 return 0;
250 }
251 }
252 if ((selection & OSSL_KEYMGMT_SELECT_DOMAIN_PARAMETERS) != 0) {
253 params = ossl_dh_get0_params((DH *)dh);
254 if (params == NULL) {
255 ERR_raise(ERR_LIB_PROV, PROV_R_NOT_PARAMETERS);
256 return 0;
257 }
258 }
259
260 p = DH_get0_p(dh);
261 if (p == NULL) {
262 ERR_raise(ERR_LIB_PROV, PROV_R_INVALID_KEY);
263 return 0;
264 }
265
266 if (BIO_printf(out, "%s: (%d bit)\n", type_label, BN_num_bits(p)) <= 0)
267 return 0;
268 if (priv_key != NULL
269 && !print_labeled_bignum(out, "private-key:", priv_key))
270 return 0;
271 if (pub_key != NULL
272 && !print_labeled_bignum(out, "public-key:", pub_key))
273 return 0;
274 if (params != NULL
275 && !ffc_params_to_text(out, params))
276 return 0;
277 length = DH_get_length(dh);
278 if (length > 0
279 && BIO_printf(out, "recommended-private-length: %ld bits\n",
280 length) <= 0)
281 return 0;
282
283 return 1;
284 }
285
286 # define dh_input_type "DH"
287 # define dhx_input_type "DHX"
288 #endif
289
290 /* ---------------------------------------------------------------------- */
291
292 #ifndef OPENSSL_NO_DSA
dsa_to_text(BIO * out,const void * key,int selection)293 static int dsa_to_text(BIO *out, const void *key, int selection)
294 {
295 const DSA *dsa = key;
296 const char *type_label = NULL;
297 const BIGNUM *priv_key = NULL, *pub_key = NULL;
298 const FFC_PARAMS *params = NULL;
299 const BIGNUM *p = NULL;
300
301 if (out == NULL || dsa == NULL) {
302 ERR_raise(ERR_LIB_PROV, ERR_R_PASSED_NULL_PARAMETER);
303 return 0;
304 }
305
306 if ((selection & OSSL_KEYMGMT_SELECT_PRIVATE_KEY) != 0)
307 type_label = "Private-Key";
308 else if ((selection & OSSL_KEYMGMT_SELECT_PUBLIC_KEY) != 0)
309 type_label = "Public-Key";
310 else if ((selection & OSSL_KEYMGMT_SELECT_DOMAIN_PARAMETERS) != 0)
311 type_label = "DSA-Parameters";
312
313 if ((selection & OSSL_KEYMGMT_SELECT_PRIVATE_KEY) != 0) {
314 priv_key = DSA_get0_priv_key(dsa);
315 if (priv_key == NULL) {
316 ERR_raise(ERR_LIB_PROV, PROV_R_NOT_A_PRIVATE_KEY);
317 return 0;
318 }
319 }
320 if ((selection & OSSL_KEYMGMT_SELECT_KEYPAIR) != 0) {
321 pub_key = DSA_get0_pub_key(dsa);
322 if (pub_key == NULL) {
323 ERR_raise(ERR_LIB_PROV, PROV_R_NOT_A_PUBLIC_KEY);
324 return 0;
325 }
326 }
327 if ((selection & OSSL_KEYMGMT_SELECT_DOMAIN_PARAMETERS) != 0) {
328 params = ossl_dsa_get0_params((DSA *)dsa);
329 if (params == NULL) {
330 ERR_raise(ERR_LIB_PROV, PROV_R_NOT_PARAMETERS);
331 return 0;
332 }
333 }
334
335 p = DSA_get0_p(dsa);
336 if (p == NULL) {
337 ERR_raise(ERR_LIB_PROV, PROV_R_INVALID_KEY);
338 return 0;
339 }
340
341 if (BIO_printf(out, "%s: (%d bit)\n", type_label, BN_num_bits(p)) <= 0)
342 return 0;
343 if (priv_key != NULL
344 && !print_labeled_bignum(out, "priv:", priv_key))
345 return 0;
346 if (pub_key != NULL
347 && !print_labeled_bignum(out, "pub: ", pub_key))
348 return 0;
349 if (params != NULL
350 && !ffc_params_to_text(out, params))
351 return 0;
352
353 return 1;
354 }
355
356 # define dsa_input_type "DSA"
357 #endif
358
359 /* ---------------------------------------------------------------------- */
360
361 #ifndef OPENSSL_NO_EC
ec_param_explicit_curve_to_text(BIO * out,const EC_GROUP * group,BN_CTX * ctx)362 static int ec_param_explicit_curve_to_text(BIO *out, const EC_GROUP *group,
363 BN_CTX *ctx)
364 {
365 const char *plabel = "Prime:";
366 BIGNUM *p = NULL, *a = NULL, *b = NULL;
367
368 p = BN_CTX_get(ctx);
369 a = BN_CTX_get(ctx);
370 b = BN_CTX_get(ctx);
371 if (b == NULL
372 || !EC_GROUP_get_curve(group, p, a, b, ctx))
373 return 0;
374
375 if (EC_GROUP_get_field_type(group) == NID_X9_62_characteristic_two_field) {
376 int basis_type = EC_GROUP_get_basis_type(group);
377
378 /* print the 'short name' of the base type OID */
379 if (basis_type == NID_undef
380 || BIO_printf(out, "Basis Type: %s\n", OBJ_nid2sn(basis_type)) <= 0)
381 return 0;
382 plabel = "Polynomial:";
383 }
384 return print_labeled_bignum(out, plabel, p)
385 && print_labeled_bignum(out, "A: ", a)
386 && print_labeled_bignum(out, "B: ", b);
387 }
388
ec_param_explicit_gen_to_text(BIO * out,const EC_GROUP * group,BN_CTX * ctx)389 static int ec_param_explicit_gen_to_text(BIO *out, const EC_GROUP *group,
390 BN_CTX *ctx)
391 {
392 int ret;
393 size_t buflen;
394 point_conversion_form_t form;
395 const EC_POINT *point = NULL;
396 const char *glabel = NULL;
397 unsigned char *buf = NULL;
398
399 form = EC_GROUP_get_point_conversion_form(group);
400 point = EC_GROUP_get0_generator(group);
401
402 if (point == NULL)
403 return 0;
404
405 switch (form) {
406 case POINT_CONVERSION_COMPRESSED:
407 glabel = "Generator (compressed):";
408 break;
409 case POINT_CONVERSION_UNCOMPRESSED:
410 glabel = "Generator (uncompressed):";
411 break;
412 case POINT_CONVERSION_HYBRID:
413 glabel = "Generator (hybrid):";
414 break;
415 default:
416 return 0;
417 }
418
419 buflen = EC_POINT_point2buf(group, point, form, &buf, ctx);
420 if (buflen == 0)
421 return 0;
422
423 ret = print_labeled_buf(out, glabel, buf, buflen);
424 OPENSSL_clear_free(buf, buflen);
425 return ret;
426 }
427
428 /* Print explicit parameters */
ec_param_explicit_to_text(BIO * out,const EC_GROUP * group,OSSL_LIB_CTX * libctx)429 static int ec_param_explicit_to_text(BIO *out, const EC_GROUP *group,
430 OSSL_LIB_CTX *libctx)
431 {
432 int ret = 0, tmp_nid;
433 BN_CTX *ctx = NULL;
434 const BIGNUM *order = NULL, *cofactor = NULL;
435 const unsigned char *seed;
436 size_t seed_len = 0;
437
438 ctx = BN_CTX_new_ex(libctx);
439 if (ctx == NULL)
440 return 0;
441 BN_CTX_start(ctx);
442
443 tmp_nid = EC_GROUP_get_field_type(group);
444 order = EC_GROUP_get0_order(group);
445 if (order == NULL)
446 goto err;
447
448 seed = EC_GROUP_get0_seed(group);
449 if (seed != NULL)
450 seed_len = EC_GROUP_get_seed_len(group);
451 cofactor = EC_GROUP_get0_cofactor(group);
452
453 /* print the 'short name' of the field type */
454 if (BIO_printf(out, "Field Type: %s\n", OBJ_nid2sn(tmp_nid)) <= 0
455 || !ec_param_explicit_curve_to_text(out, group, ctx)
456 || !ec_param_explicit_gen_to_text(out, group, ctx)
457 || !print_labeled_bignum(out, "Order: ", order)
458 || (cofactor != NULL
459 && !print_labeled_bignum(out, "Cofactor: ", cofactor))
460 || (seed != NULL
461 && !print_labeled_buf(out, "Seed:", seed, seed_len)))
462 goto err;
463 ret = 1;
464 err:
465 BN_CTX_end(ctx);
466 BN_CTX_free(ctx);
467 return ret;
468 }
469
ec_param_to_text(BIO * out,const EC_GROUP * group,OSSL_LIB_CTX * libctx)470 static int ec_param_to_text(BIO *out, const EC_GROUP *group,
471 OSSL_LIB_CTX *libctx)
472 {
473 if (EC_GROUP_get_asn1_flag(group) & OPENSSL_EC_NAMED_CURVE) {
474 const char *curve_name;
475 int curve_nid = EC_GROUP_get_curve_name(group);
476
477 /* Explicit parameters */
478 if (curve_nid == NID_undef)
479 return 0;
480
481 if (BIO_printf(out, "%s: %s\n", "ASN1 OID", OBJ_nid2sn(curve_nid)) <= 0)
482 return 0;
483
484 curve_name = EC_curve_nid2nist(curve_nid);
485 return (curve_name == NULL
486 || BIO_printf(out, "%s: %s\n", "NIST CURVE", curve_name) > 0);
487 } else {
488 return ec_param_explicit_to_text(out, group, libctx);
489 }
490 }
491
ec_to_text(BIO * out,const void * key,int selection)492 static int ec_to_text(BIO *out, const void *key, int selection)
493 {
494 const EC_KEY *ec = key;
495 const char *type_label = NULL;
496 unsigned char *priv = NULL, *pub = NULL;
497 size_t priv_len = 0, pub_len = 0;
498 const EC_GROUP *group;
499 int ret = 0;
500
501 if (out == NULL || ec == NULL) {
502 ERR_raise(ERR_LIB_PROV, ERR_R_PASSED_NULL_PARAMETER);
503 return 0;
504 }
505
506 if ((group = EC_KEY_get0_group(ec)) == NULL) {
507 ERR_raise(ERR_LIB_PROV, PROV_R_INVALID_KEY);
508 return 0;
509 }
510
511 if ((selection & OSSL_KEYMGMT_SELECT_PRIVATE_KEY) != 0)
512 type_label = "Private-Key";
513 else if ((selection & OSSL_KEYMGMT_SELECT_PUBLIC_KEY) != 0)
514 type_label = "Public-Key";
515 else if ((selection & OSSL_KEYMGMT_SELECT_DOMAIN_PARAMETERS) != 0)
516 if (EC_GROUP_get_curve_name(group) != NID_sm2)
517 type_label = "EC-Parameters";
518
519 if ((selection & OSSL_KEYMGMT_SELECT_PRIVATE_KEY) != 0) {
520 const BIGNUM *priv_key = EC_KEY_get0_private_key(ec);
521
522 if (priv_key == NULL) {
523 ERR_raise(ERR_LIB_PROV, PROV_R_NOT_A_PRIVATE_KEY);
524 goto err;
525 }
526 priv_len = EC_KEY_priv2buf(ec, &priv);
527 if (priv_len == 0)
528 goto err;
529 }
530 if ((selection & OSSL_KEYMGMT_SELECT_KEYPAIR) != 0) {
531 const EC_POINT *pub_pt = EC_KEY_get0_public_key(ec);
532
533 if (pub_pt == NULL) {
534 ERR_raise(ERR_LIB_PROV, PROV_R_NOT_A_PUBLIC_KEY);
535 goto err;
536 }
537
538 pub_len = EC_KEY_key2buf(ec, EC_KEY_get_conv_form(ec), &pub, NULL);
539 if (pub_len == 0)
540 goto err;
541 }
542
543 if (type_label != NULL
544 && BIO_printf(out, "%s: (%d bit)\n", type_label,
545 EC_GROUP_order_bits(group)) <= 0)
546 goto err;
547 if (priv != NULL
548 && !print_labeled_buf(out, "priv:", priv, priv_len))
549 goto err;
550 if (pub != NULL
551 && !print_labeled_buf(out, "pub:", pub, pub_len))
552 goto err;
553 if ((selection & OSSL_KEYMGMT_SELECT_DOMAIN_PARAMETERS) != 0)
554 ret = ec_param_to_text(out, group, ossl_ec_key_get_libctx(ec));
555 err:
556 OPENSSL_clear_free(priv, priv_len);
557 OPENSSL_free(pub);
558 return ret;
559 }
560
561 # define ec_input_type "EC"
562
563 # ifndef OPENSSL_NO_SM2
564 # define sm2_input_type "SM2"
565 # endif
566 #endif
567
568 /* ---------------------------------------------------------------------- */
569
570 #ifndef OPENSSL_NO_ECX
ecx_to_text(BIO * out,const void * key,int selection)571 static int ecx_to_text(BIO *out, const void *key, int selection)
572 {
573 const ECX_KEY *ecx = key;
574 const char *type_label = NULL;
575
576 if (out == NULL || ecx == NULL) {
577 ERR_raise(ERR_LIB_PROV, ERR_R_PASSED_NULL_PARAMETER);
578 return 0;
579 }
580
581 switch (ecx->type) {
582 case ECX_KEY_TYPE_X25519:
583 type_label = "X25519";
584 break;
585 case ECX_KEY_TYPE_X448:
586 type_label = "X448";
587 break;
588 case ECX_KEY_TYPE_ED25519:
589 type_label = "ED25519";
590 break;
591 case ECX_KEY_TYPE_ED448:
592 type_label = "ED448";
593 break;
594 }
595
596 if ((selection & OSSL_KEYMGMT_SELECT_PRIVATE_KEY) != 0) {
597 if (ecx->privkey == NULL) {
598 ERR_raise(ERR_LIB_PROV, PROV_R_NOT_A_PRIVATE_KEY);
599 return 0;
600 }
601
602 if (BIO_printf(out, "%s Private-Key:\n", type_label) <= 0)
603 return 0;
604 if (!print_labeled_buf(out, "priv:", ecx->privkey, ecx->keylen))
605 return 0;
606 } else if ((selection & OSSL_KEYMGMT_SELECT_PUBLIC_KEY) != 0) {
607 /* ecx->pubkey is an array, not a pointer... */
608 if (!ecx->haspubkey) {
609 ERR_raise(ERR_LIB_PROV, PROV_R_NOT_A_PUBLIC_KEY);
610 return 0;
611 }
612
613 if (BIO_printf(out, "%s Public-Key:\n", type_label) <= 0)
614 return 0;
615 }
616
617 if (!print_labeled_buf(out, "pub:", ecx->pubkey, ecx->keylen))
618 return 0;
619
620 return 1;
621 }
622
623 # define ed25519_input_type "ED25519"
624 # define ed448_input_type "ED448"
625 # define x25519_input_type "X25519"
626 # define x448_input_type "X448"
627 #endif
628
629 /* ---------------------------------------------------------------------- */
630
rsa_to_text(BIO * out,const void * key,int selection)631 static int rsa_to_text(BIO *out, const void *key, int selection)
632 {
633 const RSA *rsa = key;
634 const char *type_label = "RSA key";
635 const char *modulus_label = NULL;
636 const char *exponent_label = NULL;
637 const BIGNUM *rsa_d = NULL, *rsa_n = NULL, *rsa_e = NULL;
638 STACK_OF(BIGNUM_const) *factors = NULL;
639 STACK_OF(BIGNUM_const) *exps = NULL;
640 STACK_OF(BIGNUM_const) *coeffs = NULL;
641 int primes;
642 const RSA_PSS_PARAMS_30 *pss_params = ossl_rsa_get0_pss_params_30((RSA *)rsa);
643 int ret = 0;
644
645 if (out == NULL || rsa == NULL) {
646 ERR_raise(ERR_LIB_PROV, ERR_R_PASSED_NULL_PARAMETER);
647 goto err;
648 }
649
650 factors = sk_BIGNUM_const_new_null();
651 exps = sk_BIGNUM_const_new_null();
652 coeffs = sk_BIGNUM_const_new_null();
653
654 if (factors == NULL || exps == NULL || coeffs == NULL) {
655 ERR_raise(ERR_LIB_PROV, ERR_R_CRYPTO_LIB);
656 goto err;
657 }
658
659 if ((selection & OSSL_KEYMGMT_SELECT_PRIVATE_KEY) != 0) {
660 type_label = "Private-Key";
661 modulus_label = "modulus:";
662 exponent_label = "publicExponent:";
663 } else if ((selection & OSSL_KEYMGMT_SELECT_PUBLIC_KEY) != 0) {
664 type_label = "Public-Key";
665 modulus_label = "Modulus:";
666 exponent_label = "Exponent:";
667 }
668
669 RSA_get0_key(rsa, &rsa_n, &rsa_e, &rsa_d);
670 ossl_rsa_get0_all_params((RSA *)rsa, factors, exps, coeffs);
671 primes = sk_BIGNUM_const_num(factors);
672
673 if ((selection & OSSL_KEYMGMT_SELECT_PRIVATE_KEY) != 0) {
674 if (BIO_printf(out, "%s: (%d bit, %d primes)\n",
675 type_label, BN_num_bits(rsa_n), primes) <= 0)
676 goto err;
677 } else {
678 if (BIO_printf(out, "%s: (%d bit)\n",
679 type_label, BN_num_bits(rsa_n)) <= 0)
680 goto err;
681 }
682
683 if (!print_labeled_bignum(out, modulus_label, rsa_n))
684 goto err;
685 if (!print_labeled_bignum(out, exponent_label, rsa_e))
686 goto err;
687 if ((selection & OSSL_KEYMGMT_SELECT_PRIVATE_KEY) != 0) {
688 int i;
689
690 if (!print_labeled_bignum(out, "privateExponent:", rsa_d))
691 goto err;
692 if (!print_labeled_bignum(out, "prime1:",
693 sk_BIGNUM_const_value(factors, 0)))
694 goto err;
695 if (!print_labeled_bignum(out, "prime2:",
696 sk_BIGNUM_const_value(factors, 1)))
697 goto err;
698 if (!print_labeled_bignum(out, "exponent1:",
699 sk_BIGNUM_const_value(exps, 0)))
700 goto err;
701 if (!print_labeled_bignum(out, "exponent2:",
702 sk_BIGNUM_const_value(exps, 1)))
703 goto err;
704 if (!print_labeled_bignum(out, "coefficient:",
705 sk_BIGNUM_const_value(coeffs, 0)))
706 goto err;
707 for (i = 2; i < sk_BIGNUM_const_num(factors); i++) {
708 if (BIO_printf(out, "prime%d:", i + 1) <= 0)
709 goto err;
710 if (!print_labeled_bignum(out, NULL,
711 sk_BIGNUM_const_value(factors, i)))
712 goto err;
713 if (BIO_printf(out, "exponent%d:", i + 1) <= 0)
714 goto err;
715 if (!print_labeled_bignum(out, NULL,
716 sk_BIGNUM_const_value(exps, i)))
717 goto err;
718 if (BIO_printf(out, "coefficient%d:", i + 1) <= 0)
719 goto err;
720 if (!print_labeled_bignum(out, NULL,
721 sk_BIGNUM_const_value(coeffs, i - 1)))
722 goto err;
723 }
724 }
725
726 if ((selection & OSSL_KEYMGMT_SELECT_OTHER_PARAMETERS) != 0) {
727 switch (RSA_test_flags(rsa, RSA_FLAG_TYPE_MASK)) {
728 case RSA_FLAG_TYPE_RSA:
729 if (!ossl_rsa_pss_params_30_is_unrestricted(pss_params)) {
730 if (BIO_printf(out, "(INVALID PSS PARAMETERS)\n") <= 0)
731 goto err;
732 }
733 break;
734 case RSA_FLAG_TYPE_RSASSAPSS:
735 if (ossl_rsa_pss_params_30_is_unrestricted(pss_params)) {
736 if (BIO_printf(out, "No PSS parameter restrictions\n") <= 0)
737 goto err;
738 } else {
739 int hashalg_nid = ossl_rsa_pss_params_30_hashalg(pss_params);
740 int maskgenalg_nid =
741 ossl_rsa_pss_params_30_maskgenalg(pss_params);
742 int maskgenhashalg_nid =
743 ossl_rsa_pss_params_30_maskgenhashalg(pss_params);
744 int saltlen = ossl_rsa_pss_params_30_saltlen(pss_params);
745 int trailerfield =
746 ossl_rsa_pss_params_30_trailerfield(pss_params);
747
748 if (BIO_printf(out, "PSS parameter restrictions:\n") <= 0)
749 goto err;
750 if (BIO_printf(out, " Hash Algorithm: %s%s\n",
751 ossl_rsa_oaeppss_nid2name(hashalg_nid),
752 (hashalg_nid == NID_sha1
753 ? " (default)" : "")) <= 0)
754 goto err;
755 if (BIO_printf(out, " Mask Algorithm: %s with %s%s\n",
756 ossl_rsa_mgf_nid2name(maskgenalg_nid),
757 ossl_rsa_oaeppss_nid2name(maskgenhashalg_nid),
758 (maskgenalg_nid == NID_mgf1
759 && maskgenhashalg_nid == NID_sha1
760 ? " (default)" : "")) <= 0)
761 goto err;
762 if (BIO_printf(out, " Minimum Salt Length: %d%s\n",
763 saltlen,
764 (saltlen == 20 ? " (default)" : "")) <= 0)
765 goto err;
766 if (BIO_printf(out, " Trailer Field: 0x%x%s\n",
767 trailerfield,
768 (trailerfield == 1 ? " (default)" : "")) <= 0)
769 goto err;
770 }
771 break;
772 }
773 }
774
775 ret = 1;
776 err:
777 sk_BIGNUM_const_free(factors);
778 sk_BIGNUM_const_free(exps);
779 sk_BIGNUM_const_free(coeffs);
780 return ret;
781 }
782
783 #define rsa_input_type "RSA"
784 #define rsapss_input_type "RSA-PSS"
785
786 /* ---------------------------------------------------------------------- */
787
key2text_newctx(void * provctx)788 static void *key2text_newctx(void *provctx)
789 {
790 return provctx;
791 }
792
key2text_freectx(ossl_unused void * vctx)793 static void key2text_freectx(ossl_unused void *vctx)
794 {
795 }
796
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)797 static int key2text_encode(void *vctx, const void *key, int selection,
798 OSSL_CORE_BIO *cout,
799 int (*key2text)(BIO *out, const void *key,
800 int selection),
801 OSSL_PASSPHRASE_CALLBACK *cb, void *cbarg)
802 {
803 BIO *out = ossl_bio_new_from_core_bio(vctx, cout);
804 int ret;
805
806 if (out == NULL)
807 return 0;
808
809 ret = key2text(out, key, selection);
810 BIO_free(out);
811
812 return ret;
813 }
814
815 #define MAKE_TEXT_ENCODER(impl, type) \
816 static OSSL_FUNC_encoder_import_object_fn \
817 impl##2text_import_object; \
818 static OSSL_FUNC_encoder_free_object_fn \
819 impl##2text_free_object; \
820 static OSSL_FUNC_encoder_encode_fn impl##2text_encode; \
821 \
822 static void *impl##2text_import_object(void *ctx, int selection, \
823 const OSSL_PARAM params[]) \
824 { \
825 return ossl_prov_import_key(ossl_##impl##_keymgmt_functions, \
826 ctx, selection, params); \
827 } \
828 static void impl##2text_free_object(void *key) \
829 { \
830 ossl_prov_free_key(ossl_##impl##_keymgmt_functions, key); \
831 } \
832 static int impl##2text_encode(void *vctx, OSSL_CORE_BIO *cout, \
833 const void *key, \
834 const OSSL_PARAM key_abstract[], \
835 int selection, \
836 OSSL_PASSPHRASE_CALLBACK *cb, \
837 void *cbarg) \
838 { \
839 /* We don't deal with abstract objects */ \
840 if (key_abstract != NULL) { \
841 ERR_raise(ERR_LIB_PROV, ERR_R_PASSED_INVALID_ARGUMENT); \
842 return 0; \
843 } \
844 return key2text_encode(vctx, key, selection, cout, \
845 type##_to_text, cb, cbarg); \
846 } \
847 const OSSL_DISPATCH ossl_##impl##_to_text_encoder_functions[] = { \
848 { OSSL_FUNC_ENCODER_NEWCTX, \
849 (void (*)(void))key2text_newctx }, \
850 { OSSL_FUNC_ENCODER_FREECTX, \
851 (void (*)(void))key2text_freectx }, \
852 { OSSL_FUNC_ENCODER_IMPORT_OBJECT, \
853 (void (*)(void))impl##2text_import_object }, \
854 { OSSL_FUNC_ENCODER_FREE_OBJECT, \
855 (void (*)(void))impl##2text_free_object }, \
856 { OSSL_FUNC_ENCODER_ENCODE, \
857 (void (*)(void))impl##2text_encode }, \
858 OSSL_DISPATCH_END \
859 }
860
861 #ifndef OPENSSL_NO_DH
862 MAKE_TEXT_ENCODER(dh, dh);
863 MAKE_TEXT_ENCODER(dhx, dh);
864 #endif
865 #ifndef OPENSSL_NO_DSA
866 MAKE_TEXT_ENCODER(dsa, dsa);
867 #endif
868 #ifndef OPENSSL_NO_EC
869 MAKE_TEXT_ENCODER(ec, ec);
870 # ifndef OPENSSL_NO_SM2
871 MAKE_TEXT_ENCODER(sm2, ec);
872 # endif
873 # ifndef OPENSSL_NO_ECX
874 MAKE_TEXT_ENCODER(ed25519, ecx);
875 MAKE_TEXT_ENCODER(ed448, ecx);
876 MAKE_TEXT_ENCODER(x25519, ecx);
877 MAKE_TEXT_ENCODER(x448, ecx);
878 # endif
879 #endif
880 MAKE_TEXT_ENCODER(rsa, rsa);
881 MAKE_TEXT_ENCODER(rsapss, rsa);
882