]> git.ipfire.org Git - thirdparty/openssl.git/blame - providers/implementations/encode_decode/encode_key2text.c
Deprecate EC_POINT_bn2point and EC_POINT_point2bn.
[thirdparty/openssl.git] / providers / implementations / encode_decode / encode_key2text.c
CommitLineData
8ae40cf5
RL
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
35DEFINE_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
52static 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;
121err:
122 OPENSSL_free(hex_str);
123 return ret;
124}
125
126/* Number of octets per line */
127#define LABELED_BUF_PRINT_WIDTH 15
128
8ce3244c 129#if !defined(OPENSSL_NO_DH) || !defined(OPENSSL_NO_DSA) || !defined(OPENSSL_NO_EC)
8ae40cf5
RL
130static 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}
8ce3244c 155#endif
8ae40cf5
RL
156
157#if !defined(OPENSSL_NO_DH) || !defined(OPENSSL_NO_DSA)
158static int ffc_params_to_text(BIO *out, const FFC_PARAMS *ffc)
159{
160 if (ffc->nid != NID_undef) {
161#ifndef OPENSSL_NO_DH
5357c106 162 const char *name = ossl_ffc_named_group_from_uid(ffc->nid);
8ae40cf5
RL
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;
204err:
205 return 0;
206}
207#endif
208
209/* ---------------------------------------------------------------------- */
210
211#ifndef OPENSSL_NO_DH
8ae40cf5
RL
212static 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}
111dc4b0
RL
274
275# define dh_input_type "DH"
276# define dhx_input_type "DHX"
8ae40cf5
RL
277#endif
278
279/* ---------------------------------------------------------------------- */
280
281#ifndef OPENSSL_NO_DSA
8ae40cf5
RL
282static 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}
111dc4b0
RL
344
345# define dsa_input_type "DSA"
8ae40cf5
RL
346#endif
347
348/* ---------------------------------------------------------------------- */
349
350#ifndef OPENSSL_NO_EC
8ae40cf5
RL
351static 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
378static int ec_param_explicit_gen_to_text(BIO *out, const EC_GROUP *group,
379 BN_CTX *ctx)
380{
c1131e6a
SL
381 int ret;
382 size_t buflen;
383 point_conversion_form_t form;
8ae40cf5 384 const EC_POINT *point = NULL;
8ae40cf5 385 const char *glabel = NULL;
c1131e6a 386 unsigned char *buf = NULL;
8ae40cf5
RL
387
388 form = EC_GROUP_get_point_conversion_form(group);
389 point = EC_GROUP_get0_generator(group);
8ae40cf5 390
c1131e6a 391 if (point == NULL)
8ae40cf5
RL
392 return 0;
393
ad2dbfb5
SL
394 switch (form) {
395 case POINT_CONVERSION_COMPRESSED:
396 glabel = "Generator (compressed):";
397 break;
398 case POINT_CONVERSION_UNCOMPRESSED:
399 glabel = "Generator (uncompressed):";
400 break;
401 case POINT_CONVERSION_HYBRID:
402 glabel = "Generator (hybrid):";
403 break;
404 default:
405 return 0;
8ae40cf5 406 }
c1131e6a
SL
407
408 buflen = EC_POINT_point2buf(group, point, form, &buf, ctx);
409 if (buflen == 0)
410 return 0;
411
412 ret = print_labeled_buf(out, glabel, buf, buflen);
413 OPENSSL_clear_free(buf, buflen);
414 return ret;
8ae40cf5
RL
415}
416
417/* Print explicit parameters */
418static int ec_param_explicit_to_text(BIO *out, const EC_GROUP *group,
b4250010 419 OSSL_LIB_CTX *libctx)
8ae40cf5
RL
420{
421 int ret = 0, tmp_nid;
422 BN_CTX *ctx = NULL;
423 const BIGNUM *order = NULL, *cofactor = NULL;
424 const unsigned char *seed;
425 size_t seed_len = 0;
426
427 ctx = BN_CTX_new_ex(libctx);
428 if (ctx == NULL)
429 return 0;
430 BN_CTX_start(ctx);
431
432 tmp_nid = EC_GROUP_get_field_type(group);
433 order = EC_GROUP_get0_order(group);
434 if (order == NULL)
435 goto err;
436
437 seed = EC_GROUP_get0_seed(group);
438 if (seed != NULL)
439 seed_len = EC_GROUP_get_seed_len(group);
440 cofactor = EC_GROUP_get0_cofactor(group);
441
442 /* print the 'short name' of the field type */
443 if (BIO_printf(out, "Field Type: %s\n", OBJ_nid2sn(tmp_nid)) <= 0
444 || !ec_param_explicit_curve_to_text(out, group, ctx)
445 || !ec_param_explicit_gen_to_text(out, group, ctx)
446 || !print_labeled_bignum(out, "Order: ", order)
447 || (cofactor != NULL
448 && !print_labeled_bignum(out, "Cofactor: ", cofactor))
449 || (seed != NULL
450 && !print_labeled_buf(out, "Seed:", seed, seed_len)))
451 goto err;
452 ret = 1;
453err:
454 BN_CTX_end(ctx);
455 BN_CTX_free(ctx);
456 return ret;
457}
458
459static int ec_param_to_text(BIO *out, const EC_GROUP *group,
b4250010 460 OSSL_LIB_CTX *libctx)
8ae40cf5
RL
461{
462 if (EC_GROUP_get_asn1_flag(group) & OPENSSL_EC_NAMED_CURVE) {
463 const char *curve_name;
464 int curve_nid = EC_GROUP_get_curve_name(group);
465
466 /* Explicit parameters */
467 if (curve_nid == NID_undef)
468 return 0;
469
470 if (BIO_printf(out, "%s: %s\n", "ASN1 OID", OBJ_nid2sn(curve_nid)) <= 0)
471 return 0;
472
473 curve_name = EC_curve_nid2nist(curve_nid);
474 return (curve_name == NULL
475 || BIO_printf(out, "%s: %s\n", "NIST CURVE", curve_name) > 0);
476 } else {
477 return ec_param_explicit_to_text(out, group, libctx);
478 }
479}
480
481static int ec_to_text(BIO *out, const void *key, int selection)
482{
483 const EC_KEY *ec = key;
484 const char *type_label = NULL;
485 unsigned char *priv = NULL, *pub = NULL;
486 size_t priv_len = 0, pub_len = 0;
487 const EC_GROUP *group;
488 int ret = 0;
489
490 if (out == NULL || ec == NULL) {
491 ERR_raise(ERR_LIB_PROV, ERR_R_PASSED_NULL_PARAMETER);
492 return 0;
493 }
494
495 if ((group = EC_KEY_get0_group(ec)) == NULL) {
496 ERR_raise(ERR_LIB_PROV, PROV_R_INVALID_KEY);
497 return 0;
498 }
499
500 if ((selection & OSSL_KEYMGMT_SELECT_PRIVATE_KEY) != 0)
501 type_label = "Private-Key";
502 else if ((selection & OSSL_KEYMGMT_SELECT_PUBLIC_KEY) != 0)
503 type_label = "Public-Key";
504 else if ((selection & OSSL_KEYMGMT_SELECT_DOMAIN_PARAMETERS) != 0)
505 type_label = "EC-Parameters";
506
507 if ((selection & OSSL_KEYMGMT_SELECT_PRIVATE_KEY) != 0) {
508 const BIGNUM *priv_key = EC_KEY_get0_private_key(ec);
509
510 if (priv_key == NULL) {
511 ERR_raise(ERR_LIB_PROV, PROV_R_NOT_A_PRIVATE_KEY);
512 goto err;
513 }
514 priv_len = EC_KEY_priv2buf(ec, &priv);
515 if (priv_len == 0)
516 goto err;
517 }
518 if ((selection & OSSL_KEYMGMT_SELECT_PUBLIC_KEY) != 0) {
519 const EC_POINT *pub_pt = EC_KEY_get0_public_key(ec);
520
521 if (pub_pt == NULL) {
522 ERR_raise(ERR_LIB_PROV, PROV_R_NOT_A_PUBLIC_KEY);
523 goto err;
524 }
525
526 pub_len = EC_KEY_key2buf(ec, EC_KEY_get_conv_form(ec), &pub, NULL);
527 if (pub_len == 0)
528 goto err;
529 }
530
531 if (BIO_printf(out, "%s: (%d bit)\n", type_label,
532 EC_GROUP_order_bits(group)) <= 0)
533 goto err;
534 if (priv != NULL
535 && !print_labeled_buf(out, "priv:", priv, priv_len))
536 goto err;
537 if (pub != NULL
538 && !print_labeled_buf(out, "pub:", pub, pub_len))
539 goto err;
540 if ((selection & OSSL_KEYMGMT_SELECT_DOMAIN_PARAMETERS) != 0)
541 ret = ec_param_to_text(out, group, ec_key_get_libctx(ec));
542err:
543 OPENSSL_clear_free(priv, priv_len);
544 OPENSSL_free(pub);
545 return ret;
546}
111dc4b0
RL
547
548# define ec_input_type "EC"
8ae40cf5
RL
549#endif
550
551/* ---------------------------------------------------------------------- */
552
553#ifndef OPENSSL_NO_EC
8ae40cf5
RL
554static int ecx_to_text(BIO *out, const void *key, int selection)
555{
556 const ECX_KEY *ecx = key;
557 const char *type_label = NULL;
558
559 if (out == NULL || ecx == NULL) {
560 ERR_raise(ERR_LIB_PROV, ERR_R_PASSED_NULL_PARAMETER);
561 return 0;
562 }
563
564 if ((selection & OSSL_KEYMGMT_SELECT_PRIVATE_KEY) != 0) {
565 if (ecx->privkey == NULL) {
566 ERR_raise(ERR_LIB_PROV, PROV_R_NOT_A_PRIVATE_KEY);
567 return 0;
568 }
569
570 switch (ecx->type) {
571 case ECX_KEY_TYPE_X25519:
572 type_label = "X25519 Private-Key";
573 break;
574 case ECX_KEY_TYPE_X448:
575 type_label = "X448 Private-Key";
576 break;
577 case ECX_KEY_TYPE_ED25519:
578 type_label = "ED25519 Private-Key";
579 break;
580 case ECX_KEY_TYPE_ED448:
581 type_label = "ED448 Private-Key";
582 break;
583 }
584 } else if ((selection & OSSL_KEYMGMT_SELECT_PUBLIC_KEY) != 0) {
585 /* ecx->pubkey is an array, not a pointer... */
586 if (!ecx->haspubkey) {
587 ERR_raise(ERR_LIB_PROV, PROV_R_NOT_A_PUBLIC_KEY);
588 return 0;
589 }
590
591 switch (ecx->type) {
592 case ECX_KEY_TYPE_X25519:
593 type_label = "X25519 Public-Key";
594 break;
595 case ECX_KEY_TYPE_X448:
596 type_label = "X448 Public-Key";
597 break;
598 case ECX_KEY_TYPE_ED25519:
599 type_label = "ED25519 Public-Key";
600 break;
601 case ECX_KEY_TYPE_ED448:
602 type_label = "ED448 Public-Key";
603 break;
604 }
605 }
606
607 if (BIO_printf(out, "%s:\n", type_label) <= 0)
608 return 0;
609 if ((selection & OSSL_KEYMGMT_SELECT_PRIVATE_KEY) != 0
610 && !print_labeled_buf(out, "priv:", ecx->privkey, ecx->keylen))
611 return 0;
612 if ((selection & OSSL_KEYMGMT_SELECT_PUBLIC_KEY) != 0
613 && !print_labeled_buf(out, "pub:", ecx->pubkey, ecx->keylen))
614 return 0;
615
616 return 1;
617}
111dc4b0
RL
618
619# define ed25519_input_type "ED25519"
620# define ed448_input_type "ED448"
621# define x25519_input_type "X25519"
622# define x448_input_type "X448"
8ae40cf5
RL
623#endif
624
625/* ---------------------------------------------------------------------- */
626
8ae40cf5
RL
627static int rsa_to_text(BIO *out, const void *key, int selection)
628{
629 const RSA *rsa = key;
630 const char *type_label = "RSA key";
631 const char *modulus_label;
632 const char *exponent_label;
633 const BIGNUM *rsa_d = NULL, *rsa_n = NULL, *rsa_e = NULL;
634 STACK_OF(BIGNUM_const) *factors = NULL;
635 STACK_OF(BIGNUM_const) *exps = NULL;
636 STACK_OF(BIGNUM_const) *coeffs = NULL;
637 int primes;
23b2fc0b 638 const RSA_PSS_PARAMS_30 *pss_params = ossl_rsa_get0_pss_params_30((RSA *)rsa);
8ae40cf5
RL
639 int ret = 0;
640
641 if (out == NULL || rsa == NULL) {
642 ERR_raise(ERR_LIB_PROV, ERR_R_PASSED_NULL_PARAMETER);
643 goto err;
644 }
645
646 factors = sk_BIGNUM_const_new_null();
647 exps = sk_BIGNUM_const_new_null();
648 coeffs = sk_BIGNUM_const_new_null();
649
650 if (factors == NULL || exps == NULL || coeffs == NULL) {
651 ERR_raise(ERR_LIB_PROV, ERR_R_MALLOC_FAILURE);
652 goto err;
653 }
654
655 if ((selection & OSSL_KEYMGMT_SELECT_PRIVATE_KEY) != 0) {
656 type_label = "Private-Key";
657 modulus_label = "modulus:";
658 exponent_label = "publicExponent:";
659 } else if ((selection & OSSL_KEYMGMT_SELECT_PUBLIC_KEY) != 0) {
660 type_label = "Public-Key";
661 modulus_label = "Modulus:";
662 exponent_label = "Exponent:";
663 }
664
665 RSA_get0_key(rsa, &rsa_n, &rsa_e, &rsa_d);
23b2fc0b 666 ossl_rsa_get0_all_params((RSA *)rsa, factors, exps, coeffs);
8ae40cf5
RL
667 primes = sk_BIGNUM_const_num(factors);
668
669 if ((selection & OSSL_KEYMGMT_SELECT_PRIVATE_KEY) != 0) {
670 if (BIO_printf(out, "%s: (%d bit, %d primes)\n",
671 type_label, BN_num_bits(rsa_n), primes) <= 0)
672 goto err;
673 } else {
674 if (BIO_printf(out, "%s: (%d bit)\n",
675 type_label, BN_num_bits(rsa_n)) <= 0)
676 goto err;
677 }
678
679 if (!print_labeled_bignum(out, modulus_label, rsa_n))
680 goto err;
681 if (!print_labeled_bignum(out, exponent_label, rsa_e))
682 goto err;
683 if ((selection & OSSL_KEYMGMT_SELECT_PRIVATE_KEY) != 0) {
684 int i;
685
686 if (!print_labeled_bignum(out, "privateExponent:", rsa_d))
687 goto err;
688 if (!print_labeled_bignum(out, "prime1:",
689 sk_BIGNUM_const_value(factors, 0)))
690 goto err;
691 if (!print_labeled_bignum(out, "prime2:",
692 sk_BIGNUM_const_value(factors, 1)))
693 goto err;
694 if (!print_labeled_bignum(out, "exponent1:",
695 sk_BIGNUM_const_value(exps, 0)))
696 goto err;
697 if (!print_labeled_bignum(out, "exponent2:",
698 sk_BIGNUM_const_value(exps, 1)))
699 goto err;
700 if (!print_labeled_bignum(out, "coefficient:",
701 sk_BIGNUM_const_value(coeffs, 0)))
702 goto err;
703 for (i = 2; i < sk_BIGNUM_const_num(factors); i++) {
704 if (BIO_printf(out, "prime%d:", i + 1) <= 0)
705 goto err;
706 if (!print_labeled_bignum(out, NULL,
707 sk_BIGNUM_const_value(factors, i)))
708 goto err;
709 if (BIO_printf(out, "exponent%d:", i + 1) <= 0)
710 goto err;
711 if (!print_labeled_bignum(out, NULL,
712 sk_BIGNUM_const_value(exps, i)))
713 goto err;
714 if (BIO_printf(out, "coefficient%d:", i + 1) <= 0)
715 goto err;
716 if (!print_labeled_bignum(out, NULL,
717 sk_BIGNUM_const_value(coeffs, i - 1)))
718 goto err;
719 }
720 }
721
722 if ((selection & OSSL_KEYMGMT_SELECT_OTHER_PARAMETERS) != 0) {
723 switch (RSA_test_flags(rsa, RSA_FLAG_TYPE_MASK)) {
724 case RSA_FLAG_TYPE_RSA:
23b2fc0b 725 if (!ossl_rsa_pss_params_30_is_unrestricted(pss_params)) {
8ae40cf5
RL
726 if (BIO_printf(out, "(INVALID PSS PARAMETERS)\n") <= 0)
727 goto err;
728 }
729 break;
730 case RSA_FLAG_TYPE_RSASSAPSS:
23b2fc0b 731 if (ossl_rsa_pss_params_30_is_unrestricted(pss_params)) {
8ae40cf5
RL
732 if (BIO_printf(out, "No PSS parameter restrictions\n") <= 0)
733 goto err;
734 } else {
23b2fc0b
P
735 int hashalg_nid = ossl_rsa_pss_params_30_hashalg(pss_params);
736 int maskgenalg_nid =
737 ossl_rsa_pss_params_30_maskgenalg(pss_params);
8ae40cf5 738 int maskgenhashalg_nid =
23b2fc0b
P
739 ossl_rsa_pss_params_30_maskgenhashalg(pss_params);
740 int saltlen = ossl_rsa_pss_params_30_saltlen(pss_params);
741 int trailerfield =
742 ossl_rsa_pss_params_30_trailerfield(pss_params);
8ae40cf5
RL
743
744 if (BIO_printf(out, "PSS parameter restrictions:\n") <= 0)
745 goto err;
746 if (BIO_printf(out, " Hash Algorithm: %s%s\n",
23b2fc0b 747 ossl_rsa_oaeppss_nid2name(hashalg_nid),
8ae40cf5
RL
748 (hashalg_nid == NID_sha1
749 ? " (default)" : "")) <= 0)
750 goto err;
751 if (BIO_printf(out, " Mask Algorithm: %s with %s%s\n",
23b2fc0b
P
752 ossl_rsa_mgf_nid2name(maskgenalg_nid),
753 ossl_rsa_oaeppss_nid2name(maskgenhashalg_nid),
8ae40cf5
RL
754 (maskgenalg_nid == NID_mgf1
755 && maskgenhashalg_nid == NID_sha1
756 ? " (default)" : "")) <= 0)
757 goto err;
758 if (BIO_printf(out, " Minimum Salt Length: %d%s\n",
759 saltlen,
760 (saltlen == 20 ? " (default)" : "")) <= 0)
761 goto err;
762 /*
763 * TODO(3.0) Should we show the ASN.1 trailerField value, or
764 * the actual trailerfield byte (i.e. 0xBC for 1)?
765 * crypto/rsa/rsa_ameth.c isn't very clear on that, as it
766 * does display 0xBC when the default applies, but the ASN.1
767 * trailerField value otherwise...
768 */
769 if (BIO_printf(out, " Trailer Field: 0x%x%s\n",
770 trailerfield,
771 (trailerfield == 1 ? " (default)" : "")) <= 0)
772 goto err;
773 }
774 break;
775 }
776 }
777
778 ret = 1;
779 err:
780 sk_BIGNUM_const_free(factors);
781 sk_BIGNUM_const_free(exps);
782 sk_BIGNUM_const_free(coeffs);
783 return ret;
784}
785
111dc4b0
RL
786#define rsa_input_type "RSA"
787#define rsapss_input_type "RSA-PSS"
788
8ae40cf5
RL
789/* ---------------------------------------------------------------------- */
790
791static void *key2text_newctx(void *provctx)
792{
793 return provctx;
794}
795
796static void key2text_freectx(ossl_unused void *vctx)
797{
798}
799
111dc4b0
RL
800static const OSSL_PARAM *key2text_gettable_params(void *provctx)
801{
802 static const OSSL_PARAM gettables[] = {
803 { OSSL_ENCODER_PARAM_OUTPUT_TYPE, OSSL_PARAM_UTF8_PTR, NULL, 0, 0 },
804 OSSL_PARAM_END,
805 };
806
807 return gettables;
808}
809
810static int key2text_get_params(OSSL_PARAM params[], const char *input_type)
811{
812 OSSL_PARAM *p;
813
814 p = OSSL_PARAM_locate(params, OSSL_ENCODER_PARAM_INPUT_TYPE);
815 if (p != NULL && !OSSL_PARAM_set_utf8_ptr(p, input_type))
816 return 0;
817
818 p = OSSL_PARAM_locate(params, OSSL_ENCODER_PARAM_OUTPUT_TYPE);
819 if (p != NULL && !OSSL_PARAM_set_utf8_ptr(p, "TEXT"))
820 return 0;
821
822 return 1;
823}
824
8ae40cf5
RL
825static int key2text_encode(void *vctx, const void *key, int selection,
826 OSSL_CORE_BIO *cout,
827 int (*key2text)(BIO *out, const void *key,
828 int selection),
829 OSSL_PASSPHRASE_CALLBACK *cb, void *cbarg)
830{
831 BIO *out = bio_new_from_core_bio(vctx, cout);
832 int ret;
833
834 if (out == NULL)
835 return 0;
836
837 ret = key2text(out, key, selection);
838 BIO_free(out);
839
840 return ret;
841}
842
111dc4b0
RL
843#define MAKE_TEXT_ENCODER(impl, type) \
844 static OSSL_FUNC_encoder_get_params_fn \
845 impl##2text_get_params; \
846 static OSSL_FUNC_encoder_import_object_fn \
847 impl##2text_import_object; \
848 static OSSL_FUNC_encoder_free_object_fn \
849 impl##2text_free_object; \
850 static OSSL_FUNC_encoder_encode_fn impl##2text_encode; \
8ae40cf5 851 \
111dc4b0
RL
852 static int impl##2text_get_params(OSSL_PARAM params[]) \
853 { \
1be63951 854 return key2text_get_params(params, impl##_input_type); \
111dc4b0
RL
855 } \
856 static void *impl##2text_import_object(void *ctx, int selection, \
857 const OSSL_PARAM params[]) \
858 { \
1be63951 859 return ossl_prov_import_key(ossl_##impl##_keymgmt_functions, \
111dc4b0
RL
860 ctx, selection, params); \
861 } \
862 static void impl##2text_free_object(void *key) \
863 { \
1be63951 864 ossl_prov_free_key(ossl_##impl##_keymgmt_functions, key); \
8ae40cf5 865 } \
111dc4b0
RL
866 static int impl##2text_encode(void *vctx, OSSL_CORE_BIO *cout, \
867 const void *key, \
868 const OSSL_PARAM key_abstract[], \
869 int selection, \
8ae40cf5
RL
870 OSSL_PASSPHRASE_CALLBACK *cb, \
871 void *cbarg) \
872 { \
111dc4b0
RL
873 /* We don't deal with abstract objects */ \
874 if (key_abstract != NULL) { \
875 ERR_raise(ERR_LIB_PROV, ERR_R_PASSED_INVALID_ARGUMENT); \
876 return 0; \
877 } \
8ae40cf5
RL
878 return key2text_encode(vctx, key, selection, cout, \
879 type##_to_text, cb, cbarg); \
880 } \
1be63951 881 const OSSL_DISPATCH ossl_##impl##_to_text_encoder_functions[] = { \
8ae40cf5
RL
882 { OSSL_FUNC_ENCODER_NEWCTX, \
883 (void (*)(void))key2text_newctx }, \
884 { OSSL_FUNC_ENCODER_FREECTX, \
885 (void (*)(void))key2text_freectx }, \
111dc4b0
RL
886 { OSSL_FUNC_ENCODER_GETTABLE_PARAMS, \
887 (void (*)(void))key2text_gettable_params }, \
888 { OSSL_FUNC_ENCODER_GET_PARAMS, \
889 (void (*)(void))impl##2text_get_params }, \
890 { OSSL_FUNC_ENCODER_IMPORT_OBJECT, \
891 (void (*)(void))impl##2text_import_object }, \
892 { OSSL_FUNC_ENCODER_FREE_OBJECT, \
893 (void (*)(void))impl##2text_free_object }, \
894 { OSSL_FUNC_ENCODER_ENCODE, \
895 (void (*)(void))impl##2text_encode }, \
8ae40cf5
RL
896 { 0, NULL } \
897 }
898
8ae40cf5
RL
899#ifndef OPENSSL_NO_DH
900MAKE_TEXT_ENCODER(dh, dh);
111dc4b0 901MAKE_TEXT_ENCODER(dhx, dh);
8ae40cf5
RL
902#endif
903#ifndef OPENSSL_NO_DSA
904MAKE_TEXT_ENCODER(dsa, dsa);
905#endif
906#ifndef OPENSSL_NO_EC
907MAKE_TEXT_ENCODER(ec, ec);
111dc4b0
RL
908MAKE_TEXT_ENCODER(ed25519, ecx);
909MAKE_TEXT_ENCODER(ed448, ecx);
910MAKE_TEXT_ENCODER(x25519, ecx);
911MAKE_TEXT_ENCODER(x448, ecx);
8ae40cf5 912#endif
111dc4b0
RL
913MAKE_TEXT_ENCODER(rsa, rsa);
914MAKE_TEXT_ENCODER(rsapss, rsa);