2 * Copyright 2020-2022 The OpenSSL Project Authors. All Rights Reserved.
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
11 * Low level APIs are deprecated for public use, but still ok for internal use.
13 #include "internal/deprecated.h"
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... */
32 #include "prov/implementations.h"
33 #include "endecoder_local.h"
35 DEFINE_SPECIAL_STACK_OF_CONST(BIGNUM_const
, BIGNUM
)
37 # ifdef SIXTY_FOUR_BIT_LONG
38 # define BN_FMTu "%lu"
39 # define BN_FMTx "%lx"
42 # ifdef SIXTY_FOUR_BIT
43 # define BN_FMTu "%llu"
44 # define BN_FMTx "%llx"
47 # ifdef THIRTY_TWO_BIT
52 static int print_labeled_bignum(BIO
*out
, const char *label
, const BIGNUM
*bn
)
54 int ret
= 0, use_sep
= 0;
55 char *hex_str
= NULL
, *p
;
56 const char spaces
[] = " ";
57 const char *post_label_spc
= " ";
70 return BIO_printf(out
, "%s%s0\n", label
, post_label_spc
);
72 if (BN_num_bytes(bn
) <= BN_BYTES
) {
73 BN_ULONG
*words
= bn_get_words(bn
);
75 if (BN_is_negative(bn
))
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]);
82 hex_str
= BN_bn2hex(bn
);
91 if (BIO_printf(out
, "%s%s\n", label
, neg
) <= 0)
94 /* Keep track of how many bytes we have printed out so far */
97 if (BIO_printf(out
, "%s", spaces
) <= 0)
100 /* Add a leading 00 if the top bit is set */
102 if (BIO_printf(out
, "%02x", 0) <= 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)
112 use_sep
= 0; /* The first byte on the next line doesn't have a : */
114 if (BIO_printf(out
, "%s%c%c", use_sep
? ":" : "",
115 tolower(p
[0]), tolower(p
[1])) <= 0)
121 if (BIO_printf(out
, "\n") <= 0)
125 OPENSSL_free(hex_str
);
129 /* Number of octets per line */
130 #define LABELED_BUF_PRINT_WIDTH 15
132 #if !defined(OPENSSL_NO_DH) || !defined(OPENSSL_NO_DSA) || !defined(OPENSSL_NO_EC)
133 static int print_labeled_buf(BIO
*out
, const char *label
,
134 const unsigned char *buf
, size_t buflen
)
138 if (BIO_printf(out
, "%s\n", label
) <= 0)
141 for (i
= 0; i
< buflen
; i
++) {
142 if ((i
% LABELED_BUF_PRINT_WIDTH
) == 0) {
143 if (i
> 0 && BIO_printf(out
, "\n") <= 0)
145 if (BIO_printf(out
, " ") <= 0)
149 if (BIO_printf(out
, "%02x%s", buf
[i
],
150 (i
== buflen
- 1) ? "" : ":") <= 0)
153 if (BIO_printf(out
, "\n") <= 0)
160 #if !defined(OPENSSL_NO_DH) || !defined(OPENSSL_NO_DSA)
161 static int ffc_params_to_text(BIO
*out
, const FFC_PARAMS
*ffc
)
163 if (ffc
->nid
!= NID_undef
) {
164 #ifndef OPENSSL_NO_DH
165 const DH_NAMED_GROUP
*group
= ossl_ffc_uid_to_dh_named_group(ffc
->nid
);
166 const char *name
= ossl_ffc_named_group_get_name(group
);
170 if (BIO_printf(out
, "GROUP: %s\n", name
) <= 0)
174 /* How could this be? We should not have a nid in a no-dh build. */
179 if (!print_labeled_bignum(out
, "P: ", ffc
->p
))
181 if (ffc
->q
!= NULL
) {
182 if (!print_labeled_bignum(out
, "Q: ", ffc
->q
))
185 if (!print_labeled_bignum(out
, "G: ", ffc
->g
))
187 if (ffc
->j
!= NULL
) {
188 if (!print_labeled_bignum(out
, "J: ", ffc
->j
))
191 if (ffc
->seed
!= NULL
) {
192 if (!print_labeled_buf(out
, "SEED:", ffc
->seed
, ffc
->seedlen
))
195 if (ffc
->gindex
!= -1) {
196 if (BIO_printf(out
, "gindex: %d\n", ffc
->gindex
) <= 0)
199 if (ffc
->pcounter
!= -1) {
200 if (BIO_printf(out
, "pcounter: %d\n", ffc
->pcounter
) <= 0)
204 if (BIO_printf(out
, "h: %d\n", ffc
->h
) <= 0)
213 /* ---------------------------------------------------------------------- */
215 #ifndef OPENSSL_NO_DH
216 static int dh_to_text(BIO
*out
, const void *key
, int selection
)
219 const char *type_label
= NULL
;
220 const BIGNUM
*priv_key
= NULL
, *pub_key
= NULL
;
221 const FFC_PARAMS
*params
= NULL
;
222 const BIGNUM
*p
= NULL
;
225 if (out
== NULL
|| dh
== NULL
) {
226 ERR_raise(ERR_LIB_PROV
, ERR_R_PASSED_NULL_PARAMETER
);
230 if ((selection
& OSSL_KEYMGMT_SELECT_PRIVATE_KEY
) != 0)
231 type_label
= "DH Private-Key";
232 else if ((selection
& OSSL_KEYMGMT_SELECT_PUBLIC_KEY
) != 0)
233 type_label
= "DH Public-Key";
234 else if ((selection
& OSSL_KEYMGMT_SELECT_DOMAIN_PARAMETERS
) != 0)
235 type_label
= "DH Parameters";
237 if ((selection
& OSSL_KEYMGMT_SELECT_PRIVATE_KEY
) != 0) {
238 priv_key
= DH_get0_priv_key(dh
);
239 if (priv_key
== NULL
) {
240 ERR_raise(ERR_LIB_PROV
, PROV_R_NOT_A_PRIVATE_KEY
);
244 if ((selection
& OSSL_KEYMGMT_SELECT_PUBLIC_KEY
) != 0) {
245 pub_key
= DH_get0_pub_key(dh
);
246 if (pub_key
== NULL
) {
247 ERR_raise(ERR_LIB_PROV
, PROV_R_NOT_A_PUBLIC_KEY
);
251 if ((selection
& OSSL_KEYMGMT_SELECT_DOMAIN_PARAMETERS
) != 0) {
252 params
= ossl_dh_get0_params((DH
*)dh
);
253 if (params
== NULL
) {
254 ERR_raise(ERR_LIB_PROV
, PROV_R_NOT_PARAMETERS
);
261 ERR_raise(ERR_LIB_PROV
, PROV_R_INVALID_KEY
);
265 if (BIO_printf(out
, "%s: (%d bit)\n", type_label
, BN_num_bits(p
)) <= 0)
268 && !print_labeled_bignum(out
, "private-key:", priv_key
))
271 && !print_labeled_bignum(out
, "public-key:", pub_key
))
274 && !ffc_params_to_text(out
, params
))
276 length
= DH_get_length(dh
);
278 && BIO_printf(out
, "recommended-private-length: %ld bits\n",
285 # define dh_input_type "DH"
286 # define dhx_input_type "DHX"
289 /* ---------------------------------------------------------------------- */
291 #ifndef OPENSSL_NO_DSA
292 static int dsa_to_text(BIO
*out
, const void *key
, int selection
)
294 const DSA
*dsa
= key
;
295 const char *type_label
= NULL
;
296 const BIGNUM
*priv_key
= NULL
, *pub_key
= NULL
;
297 const FFC_PARAMS
*params
= NULL
;
298 const BIGNUM
*p
= NULL
;
300 if (out
== NULL
|| dsa
== NULL
) {
301 ERR_raise(ERR_LIB_PROV
, ERR_R_PASSED_NULL_PARAMETER
);
305 if ((selection
& OSSL_KEYMGMT_SELECT_PRIVATE_KEY
) != 0)
306 type_label
= "Private-Key";
307 else if ((selection
& OSSL_KEYMGMT_SELECT_PUBLIC_KEY
) != 0)
308 type_label
= "Public-Key";
309 else if ((selection
& OSSL_KEYMGMT_SELECT_DOMAIN_PARAMETERS
) != 0)
310 type_label
= "DSA-Parameters";
312 if ((selection
& OSSL_KEYMGMT_SELECT_PRIVATE_KEY
) != 0) {
313 priv_key
= DSA_get0_priv_key(dsa
);
314 if (priv_key
== NULL
) {
315 ERR_raise(ERR_LIB_PROV
, PROV_R_NOT_A_PRIVATE_KEY
);
319 if ((selection
& OSSL_KEYMGMT_SELECT_PUBLIC_KEY
) != 0) {
320 pub_key
= DSA_get0_pub_key(dsa
);
321 if (pub_key
== NULL
) {
322 ERR_raise(ERR_LIB_PROV
, PROV_R_NOT_A_PUBLIC_KEY
);
326 if ((selection
& OSSL_KEYMGMT_SELECT_DOMAIN_PARAMETERS
) != 0) {
327 params
= ossl_dsa_get0_params((DSA
*)dsa
);
328 if (params
== NULL
) {
329 ERR_raise(ERR_LIB_PROV
, PROV_R_NOT_PARAMETERS
);
336 ERR_raise(ERR_LIB_PROV
, PROV_R_INVALID_KEY
);
340 if (BIO_printf(out
, "%s: (%d bit)\n", type_label
, BN_num_bits(p
)) <= 0)
343 && !print_labeled_bignum(out
, "priv:", priv_key
))
346 && !print_labeled_bignum(out
, "pub: ", pub_key
))
349 && !ffc_params_to_text(out
, params
))
355 # define dsa_input_type "DSA"
358 /* ---------------------------------------------------------------------- */
360 #ifndef OPENSSL_NO_EC
361 static int ec_param_explicit_curve_to_text(BIO
*out
, const EC_GROUP
*group
,
364 const char *plabel
= "Prime:";
365 BIGNUM
*p
= NULL
, *a
= NULL
, *b
= NULL
;
371 || !EC_GROUP_get_curve(group
, p
, a
, b
, ctx
))
374 if (EC_GROUP_get_field_type(group
) == NID_X9_62_characteristic_two_field
) {
375 int basis_type
= EC_GROUP_get_basis_type(group
);
377 /* print the 'short name' of the base type OID */
378 if (basis_type
== NID_undef
379 || BIO_printf(out
, "Basis Type: %s\n", OBJ_nid2sn(basis_type
)) <= 0)
381 plabel
= "Polynomial:";
383 return print_labeled_bignum(out
, plabel
, p
)
384 && print_labeled_bignum(out
, "A: ", a
)
385 && print_labeled_bignum(out
, "B: ", b
);
388 static int ec_param_explicit_gen_to_text(BIO
*out
, const EC_GROUP
*group
,
393 point_conversion_form_t form
;
394 const EC_POINT
*point
= NULL
;
395 const char *glabel
= NULL
;
396 unsigned char *buf
= NULL
;
398 form
= EC_GROUP_get_point_conversion_form(group
);
399 point
= EC_GROUP_get0_generator(group
);
405 case POINT_CONVERSION_COMPRESSED
:
406 glabel
= "Generator (compressed):";
408 case POINT_CONVERSION_UNCOMPRESSED
:
409 glabel
= "Generator (uncompressed):";
411 case POINT_CONVERSION_HYBRID
:
412 glabel
= "Generator (hybrid):";
418 buflen
= EC_POINT_point2buf(group
, point
, form
, &buf
, ctx
);
422 ret
= print_labeled_buf(out
, glabel
, buf
, buflen
);
423 OPENSSL_clear_free(buf
, buflen
);
427 /* Print explicit parameters */
428 static int ec_param_explicit_to_text(BIO
*out
, const EC_GROUP
*group
,
429 OSSL_LIB_CTX
*libctx
)
431 int ret
= 0, tmp_nid
;
433 const BIGNUM
*order
= NULL
, *cofactor
= NULL
;
434 const unsigned char *seed
;
437 ctx
= BN_CTX_new_ex(libctx
);
442 tmp_nid
= EC_GROUP_get_field_type(group
);
443 order
= EC_GROUP_get0_order(group
);
447 seed
= EC_GROUP_get0_seed(group
);
449 seed_len
= EC_GROUP_get_seed_len(group
);
450 cofactor
= EC_GROUP_get0_cofactor(group
);
452 /* print the 'short name' of the field type */
453 if (BIO_printf(out
, "Field Type: %s\n", OBJ_nid2sn(tmp_nid
)) <= 0
454 || !ec_param_explicit_curve_to_text(out
, group
, ctx
)
455 || !ec_param_explicit_gen_to_text(out
, group
, ctx
)
456 || !print_labeled_bignum(out
, "Order: ", order
)
458 && !print_labeled_bignum(out
, "Cofactor: ", cofactor
))
460 && !print_labeled_buf(out
, "Seed:", seed
, seed_len
)))
469 static int ec_param_to_text(BIO
*out
, const EC_GROUP
*group
,
470 OSSL_LIB_CTX
*libctx
)
472 if (EC_GROUP_get_asn1_flag(group
) & OPENSSL_EC_NAMED_CURVE
) {
473 const char *curve_name
;
474 int curve_nid
= EC_GROUP_get_curve_name(group
);
476 /* Explicit parameters */
477 if (curve_nid
== NID_undef
)
480 if (BIO_printf(out
, "%s: %s\n", "ASN1 OID", OBJ_nid2sn(curve_nid
)) <= 0)
483 curve_name
= EC_curve_nid2nist(curve_nid
);
484 return (curve_name
== NULL
485 || BIO_printf(out
, "%s: %s\n", "NIST CURVE", curve_name
) > 0);
487 return ec_param_explicit_to_text(out
, group
, libctx
);
491 static int ec_to_text(BIO
*out
, const void *key
, int selection
)
493 const EC_KEY
*ec
= key
;
494 const char *type_label
= NULL
;
495 unsigned char *priv
= NULL
, *pub
= NULL
;
496 size_t priv_len
= 0, pub_len
= 0;
497 const EC_GROUP
*group
;
500 if (out
== NULL
|| ec
== NULL
) {
501 ERR_raise(ERR_LIB_PROV
, ERR_R_PASSED_NULL_PARAMETER
);
505 if ((group
= EC_KEY_get0_group(ec
)) == NULL
) {
506 ERR_raise(ERR_LIB_PROV
, PROV_R_INVALID_KEY
);
510 if ((selection
& OSSL_KEYMGMT_SELECT_PRIVATE_KEY
) != 0)
511 type_label
= "Private-Key";
512 else if ((selection
& OSSL_KEYMGMT_SELECT_PUBLIC_KEY
) != 0)
513 type_label
= "Public-Key";
514 else if ((selection
& OSSL_KEYMGMT_SELECT_DOMAIN_PARAMETERS
) != 0)
515 if (EC_GROUP_get_curve_name(group
) != NID_sm2
)
516 type_label
= "EC-Parameters";
518 if ((selection
& OSSL_KEYMGMT_SELECT_PRIVATE_KEY
) != 0) {
519 const BIGNUM
*priv_key
= EC_KEY_get0_private_key(ec
);
521 if (priv_key
== NULL
) {
522 ERR_raise(ERR_LIB_PROV
, PROV_R_NOT_A_PRIVATE_KEY
);
525 priv_len
= EC_KEY_priv2buf(ec
, &priv
);
529 if ((selection
& OSSL_KEYMGMT_SELECT_PUBLIC_KEY
) != 0) {
530 const EC_POINT
*pub_pt
= EC_KEY_get0_public_key(ec
);
532 if (pub_pt
== NULL
) {
533 ERR_raise(ERR_LIB_PROV
, PROV_R_NOT_A_PUBLIC_KEY
);
537 pub_len
= EC_KEY_key2buf(ec
, EC_KEY_get_conv_form(ec
), &pub
, NULL
);
542 if (type_label
!= NULL
543 && BIO_printf(out
, "%s: (%d bit)\n", type_label
,
544 EC_GROUP_order_bits(group
)) <= 0)
547 && !print_labeled_buf(out
, "priv:", priv
, priv_len
))
550 && !print_labeled_buf(out
, "pub:", pub
, pub_len
))
552 if ((selection
& OSSL_KEYMGMT_SELECT_DOMAIN_PARAMETERS
) != 0)
553 ret
= ec_param_to_text(out
, group
, ossl_ec_key_get_libctx(ec
));
555 OPENSSL_clear_free(priv
, priv_len
);
560 # define ec_input_type "EC"
562 # ifndef OPENSSL_NO_SM2
563 # define sm2_input_type "SM2"
567 /* ---------------------------------------------------------------------- */
569 #ifndef OPENSSL_NO_ECX
570 static int ecx_to_text(BIO
*out
, const void *key
, int selection
)
572 const ECX_KEY
*ecx
= key
;
573 const char *type_label
= NULL
;
575 if (out
== NULL
|| ecx
== NULL
) {
576 ERR_raise(ERR_LIB_PROV
, ERR_R_PASSED_NULL_PARAMETER
);
580 if ((selection
& OSSL_KEYMGMT_SELECT_PRIVATE_KEY
) != 0) {
581 if (ecx
->privkey
== NULL
) {
582 ERR_raise(ERR_LIB_PROV
, PROV_R_NOT_A_PRIVATE_KEY
);
587 case ECX_KEY_TYPE_X25519
:
588 type_label
= "X25519 Private-Key";
590 case ECX_KEY_TYPE_X448
:
591 type_label
= "X448 Private-Key";
593 case ECX_KEY_TYPE_ED25519
:
594 type_label
= "ED25519 Private-Key";
596 case ECX_KEY_TYPE_ED448
:
597 type_label
= "ED448 Private-Key";
600 } else if ((selection
& OSSL_KEYMGMT_SELECT_PUBLIC_KEY
) != 0) {
601 /* ecx->pubkey is an array, not a pointer... */
602 if (!ecx
->haspubkey
) {
603 ERR_raise(ERR_LIB_PROV
, PROV_R_NOT_A_PUBLIC_KEY
);
608 case ECX_KEY_TYPE_X25519
:
609 type_label
= "X25519 Public-Key";
611 case ECX_KEY_TYPE_X448
:
612 type_label
= "X448 Public-Key";
614 case ECX_KEY_TYPE_ED25519
:
615 type_label
= "ED25519 Public-Key";
617 case ECX_KEY_TYPE_ED448
:
618 type_label
= "ED448 Public-Key";
623 if (BIO_printf(out
, "%s:\n", type_label
) <= 0)
625 if ((selection
& OSSL_KEYMGMT_SELECT_PRIVATE_KEY
) != 0
626 && !print_labeled_buf(out
, "priv:", ecx
->privkey
, ecx
->keylen
))
628 if ((selection
& OSSL_KEYMGMT_SELECT_PUBLIC_KEY
) != 0
629 && !print_labeled_buf(out
, "pub:", ecx
->pubkey
, ecx
->keylen
))
635 # define ed25519_input_type "ED25519"
636 # define ed448_input_type "ED448"
637 # define x25519_input_type "X25519"
638 # define x448_input_type "X448"
641 /* ---------------------------------------------------------------------- */
643 static int rsa_to_text(BIO
*out
, const void *key
, int selection
)
645 const RSA
*rsa
= key
;
646 const char *type_label
= "RSA key";
647 const char *modulus_label
= NULL
;
648 const char *exponent_label
= NULL
;
649 const BIGNUM
*rsa_d
= NULL
, *rsa_n
= NULL
, *rsa_e
= NULL
;
650 STACK_OF(BIGNUM_const
) *factors
= NULL
;
651 STACK_OF(BIGNUM_const
) *exps
= NULL
;
652 STACK_OF(BIGNUM_const
) *coeffs
= NULL
;
654 const RSA_PSS_PARAMS_30
*pss_params
= ossl_rsa_get0_pss_params_30((RSA
*)rsa
);
657 if (out
== NULL
|| rsa
== NULL
) {
658 ERR_raise(ERR_LIB_PROV
, ERR_R_PASSED_NULL_PARAMETER
);
662 factors
= sk_BIGNUM_const_new_null();
663 exps
= sk_BIGNUM_const_new_null();
664 coeffs
= sk_BIGNUM_const_new_null();
666 if (factors
== NULL
|| exps
== NULL
|| coeffs
== NULL
) {
667 ERR_raise(ERR_LIB_PROV
, ERR_R_CRYPTO_LIB
);
671 if ((selection
& OSSL_KEYMGMT_SELECT_PRIVATE_KEY
) != 0) {
672 type_label
= "Private-Key";
673 modulus_label
= "modulus:";
674 exponent_label
= "publicExponent:";
675 } else if ((selection
& OSSL_KEYMGMT_SELECT_PUBLIC_KEY
) != 0) {
676 type_label
= "Public-Key";
677 modulus_label
= "Modulus:";
678 exponent_label
= "Exponent:";
681 RSA_get0_key(rsa
, &rsa_n
, &rsa_e
, &rsa_d
);
682 ossl_rsa_get0_all_params((RSA
*)rsa
, factors
, exps
, coeffs
);
683 primes
= sk_BIGNUM_const_num(factors
);
685 if ((selection
& OSSL_KEYMGMT_SELECT_PRIVATE_KEY
) != 0) {
686 if (BIO_printf(out
, "%s: (%d bit, %d primes)\n",
687 type_label
, BN_num_bits(rsa_n
), primes
) <= 0)
690 if (BIO_printf(out
, "%s: (%d bit)\n",
691 type_label
, BN_num_bits(rsa_n
)) <= 0)
695 if (!print_labeled_bignum(out
, modulus_label
, rsa_n
))
697 if (!print_labeled_bignum(out
, exponent_label
, rsa_e
))
699 if ((selection
& OSSL_KEYMGMT_SELECT_PRIVATE_KEY
) != 0) {
702 if (!print_labeled_bignum(out
, "privateExponent:", rsa_d
))
704 if (!print_labeled_bignum(out
, "prime1:",
705 sk_BIGNUM_const_value(factors
, 0)))
707 if (!print_labeled_bignum(out
, "prime2:",
708 sk_BIGNUM_const_value(factors
, 1)))
710 if (!print_labeled_bignum(out
, "exponent1:",
711 sk_BIGNUM_const_value(exps
, 0)))
713 if (!print_labeled_bignum(out
, "exponent2:",
714 sk_BIGNUM_const_value(exps
, 1)))
716 if (!print_labeled_bignum(out
, "coefficient:",
717 sk_BIGNUM_const_value(coeffs
, 0)))
719 for (i
= 2; i
< sk_BIGNUM_const_num(factors
); i
++) {
720 if (BIO_printf(out
, "prime%d:", i
+ 1) <= 0)
722 if (!print_labeled_bignum(out
, NULL
,
723 sk_BIGNUM_const_value(factors
, i
)))
725 if (BIO_printf(out
, "exponent%d:", i
+ 1) <= 0)
727 if (!print_labeled_bignum(out
, NULL
,
728 sk_BIGNUM_const_value(exps
, i
)))
730 if (BIO_printf(out
, "coefficient%d:", i
+ 1) <= 0)
732 if (!print_labeled_bignum(out
, NULL
,
733 sk_BIGNUM_const_value(coeffs
, i
- 1)))
738 if ((selection
& OSSL_KEYMGMT_SELECT_OTHER_PARAMETERS
) != 0) {
739 switch (RSA_test_flags(rsa
, RSA_FLAG_TYPE_MASK
)) {
740 case RSA_FLAG_TYPE_RSA
:
741 if (!ossl_rsa_pss_params_30_is_unrestricted(pss_params
)) {
742 if (BIO_printf(out
, "(INVALID PSS PARAMETERS)\n") <= 0)
746 case RSA_FLAG_TYPE_RSASSAPSS
:
747 if (ossl_rsa_pss_params_30_is_unrestricted(pss_params
)) {
748 if (BIO_printf(out
, "No PSS parameter restrictions\n") <= 0)
751 int hashalg_nid
= ossl_rsa_pss_params_30_hashalg(pss_params
);
753 ossl_rsa_pss_params_30_maskgenalg(pss_params
);
754 int maskgenhashalg_nid
=
755 ossl_rsa_pss_params_30_maskgenhashalg(pss_params
);
756 int saltlen
= ossl_rsa_pss_params_30_saltlen(pss_params
);
758 ossl_rsa_pss_params_30_trailerfield(pss_params
);
760 if (BIO_printf(out
, "PSS parameter restrictions:\n") <= 0)
762 if (BIO_printf(out
, " Hash Algorithm: %s%s\n",
763 ossl_rsa_oaeppss_nid2name(hashalg_nid
),
764 (hashalg_nid
== NID_sha1
765 ? " (default)" : "")) <= 0)
767 if (BIO_printf(out
, " Mask Algorithm: %s with %s%s\n",
768 ossl_rsa_mgf_nid2name(maskgenalg_nid
),
769 ossl_rsa_oaeppss_nid2name(maskgenhashalg_nid
),
770 (maskgenalg_nid
== NID_mgf1
771 && maskgenhashalg_nid
== NID_sha1
772 ? " (default)" : "")) <= 0)
774 if (BIO_printf(out
, " Minimum Salt Length: %d%s\n",
776 (saltlen
== 20 ? " (default)" : "")) <= 0)
778 if (BIO_printf(out
, " Trailer Field: 0x%x%s\n",
780 (trailerfield
== 1 ? " (default)" : "")) <= 0)
789 sk_BIGNUM_const_free(factors
);
790 sk_BIGNUM_const_free(exps
);
791 sk_BIGNUM_const_free(coeffs
);
795 #define rsa_input_type "RSA"
796 #define rsapss_input_type "RSA-PSS"
798 /* ---------------------------------------------------------------------- */
800 static void *key2text_newctx(void *provctx
)
805 static void key2text_freectx(ossl_unused
void *vctx
)
809 static int key2text_encode(void *vctx
, const void *key
, int selection
,
811 int (*key2text
)(BIO
*out
, const void *key
,
813 OSSL_PASSPHRASE_CALLBACK
*cb
, void *cbarg
)
815 BIO
*out
= ossl_bio_new_from_core_bio(vctx
, cout
);
821 ret
= key2text(out
, key
, selection
);
827 #define MAKE_TEXT_ENCODER(impl, type) \
828 static OSSL_FUNC_encoder_import_object_fn \
829 impl##2text_import_object; \
830 static OSSL_FUNC_encoder_free_object_fn \
831 impl##2text_free_object; \
832 static OSSL_FUNC_encoder_encode_fn impl##2text_encode; \
834 static void *impl##2text_import_object(void *ctx, int selection, \
835 const OSSL_PARAM params[]) \
837 return ossl_prov_import_key(ossl_##impl##_keymgmt_functions, \
838 ctx, selection, params); \
840 static void impl##2text_free_object(void *key) \
842 ossl_prov_free_key(ossl_##impl##_keymgmt_functions, key); \
844 static int impl##2text_encode(void *vctx, OSSL_CORE_BIO *cout, \
846 const OSSL_PARAM key_abstract[], \
848 OSSL_PASSPHRASE_CALLBACK *cb, \
851 /* We don't deal with abstract objects */ \
852 if (key_abstract != NULL) { \
853 ERR_raise(ERR_LIB_PROV, ERR_R_PASSED_INVALID_ARGUMENT); \
856 return key2text_encode(vctx, key, selection, cout, \
857 type##_to_text, cb, cbarg); \
859 const OSSL_DISPATCH ossl_##impl##_to_text_encoder_functions[] = { \
860 { OSSL_FUNC_ENCODER_NEWCTX, \
861 (void (*)(void))key2text_newctx }, \
862 { OSSL_FUNC_ENCODER_FREECTX, \
863 (void (*)(void))key2text_freectx }, \
864 { OSSL_FUNC_ENCODER_IMPORT_OBJECT, \
865 (void (*)(void))impl##2text_import_object }, \
866 { OSSL_FUNC_ENCODER_FREE_OBJECT, \
867 (void (*)(void))impl##2text_free_object }, \
868 { OSSL_FUNC_ENCODER_ENCODE, \
869 (void (*)(void))impl##2text_encode }, \
873 #ifndef OPENSSL_NO_DH
874 MAKE_TEXT_ENCODER(dh
, dh
);
875 MAKE_TEXT_ENCODER(dhx
, dh
);
877 #ifndef OPENSSL_NO_DSA
878 MAKE_TEXT_ENCODER(dsa
, dsa
);
880 #ifndef OPENSSL_NO_EC
881 MAKE_TEXT_ENCODER(ec
, ec
);
882 # ifndef OPENSSL_NO_SM2
883 MAKE_TEXT_ENCODER(sm2
, ec
);
885 # ifndef OPENSSL_NO_ECX
886 MAKE_TEXT_ENCODER(ed25519
, ecx
);
887 MAKE_TEXT_ENCODER(ed448
, ecx
);
888 MAKE_TEXT_ENCODER(x25519
, ecx
);
889 MAKE_TEXT_ENCODER(x448
, ecx
);
892 MAKE_TEXT_ENCODER(rsa
, rsa
);
893 MAKE_TEXT_ENCODER(rsapss
, rsa
);