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