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 doesnt 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 type_label = "EC-Parameters";
517
518 if ((selection & OSSL_KEYMGMT_SELECT_PRIVATE_KEY) != 0) {
519 const BIGNUM *priv_key = EC_KEY_get0_private_key(ec);
520
521 if (priv_key == NULL) {
522 ERR_raise(ERR_LIB_PROV, PROV_R_NOT_A_PRIVATE_KEY);
523 goto err;
524 }
525 priv_len = EC_KEY_priv2buf(ec, &priv);
526 if (priv_len == 0)
527 goto err;
528 }
529 if ((selection & OSSL_KEYMGMT_SELECT_KEYPAIR) != 0) {
530 const EC_POINT *pub_pt = EC_KEY_get0_public_key(ec);
531
532 if (pub_pt == NULL) {
533 ERR_raise(ERR_LIB_PROV, PROV_R_NOT_A_PUBLIC_KEY);
534 goto err;
535 }
536
537 pub_len = EC_KEY_key2buf(ec, EC_KEY_get_conv_form(ec), &pub, NULL);
538 if (pub_len == 0)
539 goto err;
540 }
541
542 if (BIO_printf(out, "%s: (%d bit)\n", type_label,
543 EC_GROUP_order_bits(group)) <= 0)
544 goto err;
545 if (priv != NULL
546 && !print_labeled_buf(out, "priv:", priv, priv_len))
547 goto err;
548 if (pub != NULL
549 && !print_labeled_buf(out, "pub:", pub, pub_len))
550 goto err;
551 if ((selection & OSSL_KEYMGMT_SELECT_DOMAIN_PARAMETERS) != 0)
552 ret = ec_param_to_text(out, group, ossl_ec_key_get_libctx(ec));
553 err:
554 OPENSSL_clear_free(priv, priv_len);
555 OPENSSL_free(pub);
556 return ret;
557 }
558
559 # define ec_input_type "EC"
560
561 # ifndef OPENSSL_NO_SM2
562 # define sm2_input_type "SM2"
563 # endif
564 #endif
565
566 /* ---------------------------------------------------------------------- */
567
568 #ifndef OPENSSL_NO_EC
ecx_to_text(BIO * out,const void * key,int selection)569 static int ecx_to_text(BIO *out, const void *key, int selection)
570 {
571 const ECX_KEY *ecx = key;
572 const char *type_label = NULL;
573
574 if (out == NULL || ecx == NULL) {
575 ERR_raise(ERR_LIB_PROV, ERR_R_PASSED_NULL_PARAMETER);
576 return 0;
577 }
578
579 switch (ecx->type) {
580 case ECX_KEY_TYPE_X25519:
581 type_label = "X25519";
582 break;
583 case ECX_KEY_TYPE_X448:
584 type_label = "X448";
585 break;
586 case ECX_KEY_TYPE_ED25519:
587 type_label = "ED25519";
588 break;
589 case ECX_KEY_TYPE_ED448:
590 type_label = "ED448";
591 break;
592 }
593
594 if ((selection & OSSL_KEYMGMT_SELECT_PRIVATE_KEY) != 0) {
595 if (ecx->privkey == NULL) {
596 ERR_raise(ERR_LIB_PROV, PROV_R_NOT_A_PRIVATE_KEY);
597 return 0;
598 }
599
600 if (BIO_printf(out, "%s Private-Key:\n", type_label) <= 0)
601 return 0;
602 if (!print_labeled_buf(out, "priv:", ecx->privkey, ecx->keylen))
603 return 0;
604 } else if ((selection & OSSL_KEYMGMT_SELECT_PUBLIC_KEY) != 0) {
605 /* ecx->pubkey is an array, not a pointer... */
606 if (!ecx->haspubkey) {
607 ERR_raise(ERR_LIB_PROV, PROV_R_NOT_A_PUBLIC_KEY);
608 return 0;
609 }
610
611 if (BIO_printf(out, "%s Public-Key:\n", type_label) <= 0)
612 return 0;
613 }
614
615 if (!print_labeled_buf(out, "pub:", ecx->pubkey, ecx->keylen))
616 return 0;
617
618 return 1;
619 }
620
621 # define ed25519_input_type "ED25519"
622 # define ed448_input_type "ED448"
623 # define x25519_input_type "X25519"
624 # define x448_input_type "X448"
625 #endif
626
627 /* ---------------------------------------------------------------------- */
628
rsa_to_text(BIO * out,const void * key,int selection)629 static int rsa_to_text(BIO *out, const void *key, int selection)
630 {
631 const RSA *rsa = key;
632 const char *type_label = "RSA key";
633 const char *modulus_label = NULL;
634 const char *exponent_label = NULL;
635 const BIGNUM *rsa_d = NULL, *rsa_n = NULL, *rsa_e = NULL;
636 STACK_OF(BIGNUM_const) *factors = NULL;
637 STACK_OF(BIGNUM_const) *exps = NULL;
638 STACK_OF(BIGNUM_const) *coeffs = NULL;
639 int primes;
640 const RSA_PSS_PARAMS_30 *pss_params = ossl_rsa_get0_pss_params_30((RSA *)rsa);
641 int ret = 0;
642
643 if (out == NULL || rsa == NULL) {
644 ERR_raise(ERR_LIB_PROV, ERR_R_PASSED_NULL_PARAMETER);
645 goto err;
646 }
647
648 factors = sk_BIGNUM_const_new_null();
649 exps = sk_BIGNUM_const_new_null();
650 coeffs = sk_BIGNUM_const_new_null();
651
652 if (factors == NULL || exps == NULL || coeffs == NULL) {
653 ERR_raise(ERR_LIB_PROV, ERR_R_MALLOC_FAILURE);
654 goto err;
655 }
656
657 if ((selection & OSSL_KEYMGMT_SELECT_PRIVATE_KEY) != 0) {
658 type_label = "Private-Key";
659 modulus_label = "modulus:";
660 exponent_label = "publicExponent:";
661 } else if ((selection & OSSL_KEYMGMT_SELECT_PUBLIC_KEY) != 0) {
662 type_label = "Public-Key";
663 modulus_label = "Modulus:";
664 exponent_label = "Exponent:";
665 }
666
667 RSA_get0_key(rsa, &rsa_n, &rsa_e, &rsa_d);
668 ossl_rsa_get0_all_params((RSA *)rsa, factors, exps, coeffs);
669 primes = sk_BIGNUM_const_num(factors);
670
671 if ((selection & OSSL_KEYMGMT_SELECT_PRIVATE_KEY) != 0) {
672 if (BIO_printf(out, "%s: (%d bit, %d primes)\n",
673 type_label, BN_num_bits(rsa_n), primes) <= 0)
674 goto err;
675 } else {
676 if (BIO_printf(out, "%s: (%d bit)\n",
677 type_label, BN_num_bits(rsa_n)) <= 0)
678 goto err;
679 }
680
681 if (!print_labeled_bignum(out, modulus_label, rsa_n))
682 goto err;
683 if (!print_labeled_bignum(out, exponent_label, rsa_e))
684 goto err;
685 if ((selection & OSSL_KEYMGMT_SELECT_PRIVATE_KEY) != 0) {
686 int i;
687
688 if (!print_labeled_bignum(out, "privateExponent:", rsa_d))
689 goto err;
690 if (!print_labeled_bignum(out, "prime1:",
691 sk_BIGNUM_const_value(factors, 0)))
692 goto err;
693 if (!print_labeled_bignum(out, "prime2:",
694 sk_BIGNUM_const_value(factors, 1)))
695 goto err;
696 if (!print_labeled_bignum(out, "exponent1:",
697 sk_BIGNUM_const_value(exps, 0)))
698 goto err;
699 if (!print_labeled_bignum(out, "exponent2:",
700 sk_BIGNUM_const_value(exps, 1)))
701 goto err;
702 if (!print_labeled_bignum(out, "coefficient:",
703 sk_BIGNUM_const_value(coeffs, 0)))
704 goto err;
705 for (i = 2; i < sk_BIGNUM_const_num(factors); i++) {
706 if (BIO_printf(out, "prime%d:", i + 1) <= 0)
707 goto err;
708 if (!print_labeled_bignum(out, NULL,
709 sk_BIGNUM_const_value(factors, i)))
710 goto err;
711 if (BIO_printf(out, "exponent%d:", i + 1) <= 0)
712 goto err;
713 if (!print_labeled_bignum(out, NULL,
714 sk_BIGNUM_const_value(exps, i)))
715 goto err;
716 if (BIO_printf(out, "coefficient%d:", i + 1) <= 0)
717 goto err;
718 if (!print_labeled_bignum(out, NULL,
719 sk_BIGNUM_const_value(coeffs, i - 1)))
720 goto err;
721 }
722 }
723
724 if ((selection & OSSL_KEYMGMT_SELECT_OTHER_PARAMETERS) != 0) {
725 switch (RSA_test_flags(rsa, RSA_FLAG_TYPE_MASK)) {
726 case RSA_FLAG_TYPE_RSA:
727 if (!ossl_rsa_pss_params_30_is_unrestricted(pss_params)) {
728 if (BIO_printf(out, "(INVALID PSS PARAMETERS)\n") <= 0)
729 goto err;
730 }
731 break;
732 case RSA_FLAG_TYPE_RSASSAPSS:
733 if (ossl_rsa_pss_params_30_is_unrestricted(pss_params)) {
734 if (BIO_printf(out, "No PSS parameter restrictions\n") <= 0)
735 goto err;
736 } else {
737 int hashalg_nid = ossl_rsa_pss_params_30_hashalg(pss_params);
738 int maskgenalg_nid =
739 ossl_rsa_pss_params_30_maskgenalg(pss_params);
740 int maskgenhashalg_nid =
741 ossl_rsa_pss_params_30_maskgenhashalg(pss_params);
742 int saltlen = ossl_rsa_pss_params_30_saltlen(pss_params);
743 int trailerfield =
744 ossl_rsa_pss_params_30_trailerfield(pss_params);
745
746 if (BIO_printf(out, "PSS parameter restrictions:\n") <= 0)
747 goto err;
748 if (BIO_printf(out, " Hash Algorithm: %s%s\n",
749 ossl_rsa_oaeppss_nid2name(hashalg_nid),
750 (hashalg_nid == NID_sha1
751 ? " (default)" : "")) <= 0)
752 goto err;
753 if (BIO_printf(out, " Mask Algorithm: %s with %s%s\n",
754 ossl_rsa_mgf_nid2name(maskgenalg_nid),
755 ossl_rsa_oaeppss_nid2name(maskgenhashalg_nid),
756 (maskgenalg_nid == NID_mgf1
757 && maskgenhashalg_nid == NID_sha1
758 ? " (default)" : "")) <= 0)
759 goto err;
760 if (BIO_printf(out, " Minimum Salt Length: %d%s\n",
761 saltlen,
762 (saltlen == 20 ? " (default)" : "")) <= 0)
763 goto err;
764 if (BIO_printf(out, " Trailer Field: 0x%x%s\n",
765 trailerfield,
766 (trailerfield == 1 ? " (default)" : "")) <= 0)
767 goto err;
768 }
769 break;
770 }
771 }
772
773 ret = 1;
774 err:
775 sk_BIGNUM_const_free(factors);
776 sk_BIGNUM_const_free(exps);
777 sk_BIGNUM_const_free(coeffs);
778 return ret;
779 }
780
781 #define rsa_input_type "RSA"
782 #define rsapss_input_type "RSA-PSS"
783
784 /* ---------------------------------------------------------------------- */
785
key2text_newctx(void * provctx)786 static void *key2text_newctx(void *provctx)
787 {
788 return provctx;
789 }
790
key2text_freectx(ossl_unused void * vctx)791 static void key2text_freectx(ossl_unused void *vctx)
792 {
793 }
794
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)795 static int key2text_encode(void *vctx, const void *key, int selection,
796 OSSL_CORE_BIO *cout,
797 int (*key2text)(BIO *out, const void *key,
798 int selection),
799 OSSL_PASSPHRASE_CALLBACK *cb, void *cbarg)
800 {
801 BIO *out = ossl_bio_new_from_core_bio(vctx, cout);
802 int ret;
803
804 if (out == NULL)
805 return 0;
806
807 ret = key2text(out, key, selection);
808 BIO_free(out);
809
810 return ret;
811 }
812
813 #define MAKE_TEXT_ENCODER(impl, type) \
814 static OSSL_FUNC_encoder_import_object_fn \
815 impl##2text_import_object; \
816 static OSSL_FUNC_encoder_free_object_fn \
817 impl##2text_free_object; \
818 static OSSL_FUNC_encoder_encode_fn impl##2text_encode; \
819 \
820 static void *impl##2text_import_object(void *ctx, int selection, \
821 const OSSL_PARAM params[]) \
822 { \
823 return ossl_prov_import_key(ossl_##impl##_keymgmt_functions, \
824 ctx, selection, params); \
825 } \
826 static void impl##2text_free_object(void *key) \
827 { \
828 ossl_prov_free_key(ossl_##impl##_keymgmt_functions, key); \
829 } \
830 static int impl##2text_encode(void *vctx, OSSL_CORE_BIO *cout, \
831 const void *key, \
832 const OSSL_PARAM key_abstract[], \
833 int selection, \
834 OSSL_PASSPHRASE_CALLBACK *cb, \
835 void *cbarg) \
836 { \
837 /* We don't deal with abstract objects */ \
838 if (key_abstract != NULL) { \
839 ERR_raise(ERR_LIB_PROV, ERR_R_PASSED_INVALID_ARGUMENT); \
840 return 0; \
841 } \
842 return key2text_encode(vctx, key, selection, cout, \
843 type##_to_text, cb, cbarg); \
844 } \
845 const OSSL_DISPATCH ossl_##impl##_to_text_encoder_functions[] = { \
846 { OSSL_FUNC_ENCODER_NEWCTX, \
847 (void (*)(void))key2text_newctx }, \
848 { OSSL_FUNC_ENCODER_FREECTX, \
849 (void (*)(void))key2text_freectx }, \
850 { OSSL_FUNC_ENCODER_IMPORT_OBJECT, \
851 (void (*)(void))impl##2text_import_object }, \
852 { OSSL_FUNC_ENCODER_FREE_OBJECT, \
853 (void (*)(void))impl##2text_free_object }, \
854 { OSSL_FUNC_ENCODER_ENCODE, \
855 (void (*)(void))impl##2text_encode }, \
856 { 0, NULL } \
857 }
858
859 #ifndef OPENSSL_NO_DH
860 MAKE_TEXT_ENCODER(dh, dh);
861 MAKE_TEXT_ENCODER(dhx, dh);
862 #endif
863 #ifndef OPENSSL_NO_DSA
864 MAKE_TEXT_ENCODER(dsa, dsa);
865 #endif
866 #ifndef OPENSSL_NO_EC
867 MAKE_TEXT_ENCODER(ec, ec);
868 # ifndef OPENSSL_NO_SM2
869 MAKE_TEXT_ENCODER(sm2, ec);
870 # endif
871 MAKE_TEXT_ENCODER(ed25519, ecx);
872 MAKE_TEXT_ENCODER(ed448, ecx);
873 MAKE_TEXT_ENCODER(x25519, ecx);
874 MAKE_TEXT_ENCODER(x448, ecx);
875 #endif
876 MAKE_TEXT_ENCODER(rsa, rsa);
877 MAKE_TEXT_ENCODER(rsapss, rsa);
878