2 * Copyright 2020 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 "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... */
31 #include "prov/implementations.h"
32 #include "prov/providercommonerr.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);
88 if (BIO_printf(out, "%s%s\n", label, neg) <= 0)
91 /* Keep track of how many bytes we have printed out so far */
94 if (BIO_printf(out, "%s", spaces) <= 0)
97 /* Add a leading 00 if the top bit is set */
99 if (BIO_printf(out, "%02x", 0) <= 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)
109 use_sep = 0; /* The first byte on the next line doesnt have a : */
111 if (BIO_printf(out, "%s%c%c", use_sep ? ":" : "",
112 tolower(p[0]), tolower(p[1])) <= 0)
118 if (BIO_printf(out, "\n") <= 0)
122 OPENSSL_free(hex_str);
126 /* Number of octets per line */
127 #define LABELED_BUF_PRINT_WIDTH 15
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)
135 if (BIO_printf(out, "%s\n", label) <= 0)
138 for (i = 0; i < buflen; i++) {
139 if ((i % LABELED_BUF_PRINT_WIDTH) == 0) {
140 if (i > 0 && BIO_printf(out, "\n") <= 0)
142 if (BIO_printf(out, " ") <= 0)
146 if (BIO_printf(out, "%02x%s", buf[i],
147 (i == buflen - 1) ? "" : ":") <= 0)
150 if (BIO_printf(out, "\n") <= 0)
157 #if !defined(OPENSSL_NO_DH) || !defined(OPENSSL_NO_DSA)
158 static int ffc_params_to_text(BIO *out, const FFC_PARAMS *ffc)
160 if (ffc->nid != NID_undef) {
161 #ifndef OPENSSL_NO_DH
162 const DH_NAMED_GROUP *group = ossl_ffc_uid_to_dh_named_group(ffc->nid);
163 const char *name = ossl_ffc_named_group_get_name(group);
167 if (BIO_printf(out, "GROUP: %s\n", name) <= 0)
171 /* How could this be? We should not have a nid in a no-dh build. */
176 if (!print_labeled_bignum(out, "P: ", ffc->p))
178 if (ffc->q != NULL) {
179 if (!print_labeled_bignum(out, "Q: ", ffc->q))
182 if (!print_labeled_bignum(out, "G: ", ffc->g))
184 if (ffc->j != NULL) {
185 if (!print_labeled_bignum(out, "J: ", ffc->j))
188 if (ffc->seed != NULL) {
189 if (!print_labeled_buf(out, "SEED:", ffc->seed, ffc->seedlen))
192 if (ffc->gindex != -1) {
193 if (BIO_printf(out, "gindex: %d\n", ffc->gindex) <= 0)
196 if (ffc->pcounter != -1) {
197 if (BIO_printf(out, "pcounter: %d\n", ffc->pcounter) <= 0)
201 if (BIO_printf(out, "h: %d\n", ffc->h) <= 0)
210 /* ---------------------------------------------------------------------- */
212 #ifndef OPENSSL_NO_DH
213 static int dh_to_text(BIO *out, const void *key, int selection)
216 const char *type_label = NULL;
217 const BIGNUM *priv_key = NULL, *pub_key = NULL;
218 const FFC_PARAMS *params = NULL;
219 const BIGNUM *p = NULL;
221 if (out == NULL || dh == NULL) {
222 ERR_raise(ERR_LIB_PROV, ERR_R_PASSED_NULL_PARAMETER);
226 if ((selection & OSSL_KEYMGMT_SELECT_PRIVATE_KEY) != 0)
227 type_label = "DH Private-Key";
228 else if ((selection & OSSL_KEYMGMT_SELECT_PUBLIC_KEY) != 0)
229 type_label = "DH Public-Key";
230 else if ((selection & OSSL_KEYMGMT_SELECT_DOMAIN_PARAMETERS) != 0)
231 type_label = "DH Parameters";
233 if ((selection & OSSL_KEYMGMT_SELECT_PRIVATE_KEY) != 0) {
234 priv_key = DH_get0_priv_key(dh);
235 if (priv_key == NULL) {
236 ERR_raise(ERR_LIB_PROV, PROV_R_NOT_A_PRIVATE_KEY);
240 if ((selection & OSSL_KEYMGMT_SELECT_PUBLIC_KEY) != 0) {
241 pub_key = DH_get0_pub_key(dh);
242 if (pub_key == NULL) {
243 ERR_raise(ERR_LIB_PROV, PROV_R_NOT_A_PUBLIC_KEY);
247 if ((selection & OSSL_KEYMGMT_SELECT_DOMAIN_PARAMETERS) != 0) {
248 params = dh_get0_params((DH *)dh);
249 if (params == NULL) {
250 ERR_raise(ERR_LIB_PROV, PROV_R_NOT_PARAMETERS);
257 ERR_raise(ERR_LIB_PROV, PROV_R_INVALID_KEY);
261 if (BIO_printf(out, "%s: (%d bit)\n", type_label, BN_num_bits(p)) <= 0)
264 && !print_labeled_bignum(out, "private-key:", priv_key))
267 && !print_labeled_bignum(out, "public-key:", pub_key))
270 && !ffc_params_to_text(out, params))
276 # define dh_input_type "DH"
277 # define dhx_input_type "DHX"
280 /* ---------------------------------------------------------------------- */
282 #ifndef OPENSSL_NO_DSA
283 static int dsa_to_text(BIO *out, const void *key, int selection)
285 const DSA *dsa = key;
286 const char *type_label = NULL;
287 const BIGNUM *priv_key = NULL, *pub_key = NULL;
288 const FFC_PARAMS *params = NULL;
289 const BIGNUM *p = NULL;
291 if (out == NULL || dsa == NULL) {
292 ERR_raise(ERR_LIB_PROV, ERR_R_PASSED_NULL_PARAMETER);
296 if ((selection & OSSL_KEYMGMT_SELECT_PRIVATE_KEY) != 0)
297 type_label = "Private-Key";
298 else if ((selection & OSSL_KEYMGMT_SELECT_PUBLIC_KEY) != 0)
299 type_label = "Public-Key";
300 else if ((selection & OSSL_KEYMGMT_SELECT_DOMAIN_PARAMETERS) != 0)
301 type_label = "DSA-Parameters";
303 if ((selection & OSSL_KEYMGMT_SELECT_PRIVATE_KEY) != 0) {
304 priv_key = DSA_get0_priv_key(dsa);
305 if (priv_key == NULL) {
306 ERR_raise(ERR_LIB_PROV, PROV_R_NOT_A_PRIVATE_KEY);
310 if ((selection & OSSL_KEYMGMT_SELECT_PUBLIC_KEY) != 0) {
311 pub_key = DSA_get0_pub_key(dsa);
312 if (pub_key == NULL) {
313 ERR_raise(ERR_LIB_PROV, PROV_R_NOT_A_PUBLIC_KEY);
317 if ((selection & OSSL_KEYMGMT_SELECT_DOMAIN_PARAMETERS) != 0) {
318 params = dsa_get0_params((DSA *)dsa);
319 if (params == NULL) {
320 ERR_raise(ERR_LIB_PROV, PROV_R_NOT_PARAMETERS);
327 ERR_raise(ERR_LIB_PROV, PROV_R_INVALID_KEY);
331 if (BIO_printf(out, "%s: (%d bit)\n", type_label, BN_num_bits(p)) <= 0)
334 && !print_labeled_bignum(out, "priv:", priv_key))
337 && !print_labeled_bignum(out, "pub: ", pub_key))
340 && !ffc_params_to_text(out, params))
346 # define dsa_input_type "DSA"
349 /* ---------------------------------------------------------------------- */
351 #ifndef OPENSSL_NO_EC
352 static int ec_param_explicit_curve_to_text(BIO *out, const EC_GROUP *group,
355 const char *plabel = "Prime:";
356 BIGNUM *p = NULL, *a = NULL, *b = NULL;
362 || !EC_GROUP_get_curve(group, p, a, b, ctx))
365 if (EC_GROUP_get_field_type(group) == NID_X9_62_characteristic_two_field) {
366 int basis_type = EC_GROUP_get_basis_type(group);
368 /* print the 'short name' of the base type OID */
369 if (basis_type == NID_undef
370 || BIO_printf(out, "Basis Type: %s\n", OBJ_nid2sn(basis_type)) <= 0)
372 plabel = "Polynomial:";
374 return print_labeled_bignum(out, plabel, p)
375 && print_labeled_bignum(out, "A: ", a)
376 && print_labeled_bignum(out, "B: ", b);
379 static int ec_param_explicit_gen_to_text(BIO *out, const EC_GROUP *group,
384 point_conversion_form_t form;
385 const EC_POINT *point = NULL;
386 const char *glabel = NULL;
387 unsigned char *buf = NULL;
389 form = EC_GROUP_get_point_conversion_form(group);
390 point = EC_GROUP_get0_generator(group);
396 case POINT_CONVERSION_COMPRESSED:
397 glabel = "Generator (compressed):";
399 case POINT_CONVERSION_UNCOMPRESSED:
400 glabel = "Generator (uncompressed):";
402 case POINT_CONVERSION_HYBRID:
403 glabel = "Generator (hybrid):";
409 buflen = EC_POINT_point2buf(group, point, form, &buf, ctx);
413 ret = print_labeled_buf(out, glabel, buf, buflen);
414 OPENSSL_clear_free(buf, buflen);
418 /* Print explicit parameters */
419 static int ec_param_explicit_to_text(BIO *out, const EC_GROUP *group,
420 OSSL_LIB_CTX *libctx)
422 int ret = 0, tmp_nid;
424 const BIGNUM *order = NULL, *cofactor = NULL;
425 const unsigned char *seed;
428 ctx = BN_CTX_new_ex(libctx);
433 tmp_nid = EC_GROUP_get_field_type(group);
434 order = EC_GROUP_get0_order(group);
438 seed = EC_GROUP_get0_seed(group);
440 seed_len = EC_GROUP_get_seed_len(group);
441 cofactor = EC_GROUP_get0_cofactor(group);
443 /* print the 'short name' of the field type */
444 if (BIO_printf(out, "Field Type: %s\n", OBJ_nid2sn(tmp_nid)) <= 0
445 || !ec_param_explicit_curve_to_text(out, group, ctx)
446 || !ec_param_explicit_gen_to_text(out, group, ctx)
447 || !print_labeled_bignum(out, "Order: ", order)
449 && !print_labeled_bignum(out, "Cofactor: ", cofactor))
451 && !print_labeled_buf(out, "Seed:", seed, seed_len)))
460 static int ec_param_to_text(BIO *out, const EC_GROUP *group,
461 OSSL_LIB_CTX *libctx)
463 if (EC_GROUP_get_asn1_flag(group) & OPENSSL_EC_NAMED_CURVE) {
464 const char *curve_name;
465 int curve_nid = EC_GROUP_get_curve_name(group);
467 /* Explicit parameters */
468 if (curve_nid == NID_undef)
471 if (BIO_printf(out, "%s: %s\n", "ASN1 OID", OBJ_nid2sn(curve_nid)) <= 0)
474 curve_name = EC_curve_nid2nist(curve_nid);
475 return (curve_name == NULL
476 || BIO_printf(out, "%s: %s\n", "NIST CURVE", curve_name) > 0);
478 return ec_param_explicit_to_text(out, group, libctx);
482 static int ec_to_text(BIO *out, const void *key, int selection)
484 const EC_KEY *ec = key;
485 const char *type_label = NULL;
486 unsigned char *priv = NULL, *pub = NULL;
487 size_t priv_len = 0, pub_len = 0;
488 const EC_GROUP *group;
491 if (out == NULL || ec == NULL) {
492 ERR_raise(ERR_LIB_PROV, ERR_R_PASSED_NULL_PARAMETER);
496 if ((group = EC_KEY_get0_group(ec)) == NULL) {
497 ERR_raise(ERR_LIB_PROV, PROV_R_INVALID_KEY);
501 if ((selection & OSSL_KEYMGMT_SELECT_PRIVATE_KEY) != 0)
502 type_label = "Private-Key";
503 else if ((selection & OSSL_KEYMGMT_SELECT_PUBLIC_KEY) != 0)
504 type_label = "Public-Key";
505 else if ((selection & OSSL_KEYMGMT_SELECT_DOMAIN_PARAMETERS) != 0)
506 type_label = "EC-Parameters";
508 if ((selection & OSSL_KEYMGMT_SELECT_PRIVATE_KEY) != 0) {
509 const BIGNUM *priv_key = EC_KEY_get0_private_key(ec);
511 if (priv_key == NULL) {
512 ERR_raise(ERR_LIB_PROV, PROV_R_NOT_A_PRIVATE_KEY);
515 priv_len = EC_KEY_priv2buf(ec, &priv);
519 if ((selection & OSSL_KEYMGMT_SELECT_PUBLIC_KEY) != 0) {
520 const EC_POINT *pub_pt = EC_KEY_get0_public_key(ec);
522 if (pub_pt == NULL) {
523 ERR_raise(ERR_LIB_PROV, PROV_R_NOT_A_PUBLIC_KEY);
527 pub_len = EC_KEY_key2buf(ec, EC_KEY_get_conv_form(ec), &pub, NULL);
532 if (BIO_printf(out, "%s: (%d bit)\n", type_label,
533 EC_GROUP_order_bits(group)) <= 0)
536 && !print_labeled_buf(out, "priv:", priv, priv_len))
539 && !print_labeled_buf(out, "pub:", pub, pub_len))
541 if ((selection & OSSL_KEYMGMT_SELECT_DOMAIN_PARAMETERS) != 0)
542 ret = ec_param_to_text(out, group, ec_key_get_libctx(ec));
544 OPENSSL_clear_free(priv, priv_len);
549 # define ec_input_type "EC"
551 # ifndef OPENSSL_NO_SM2
552 # define sm2_input_type "SM2"
556 /* ---------------------------------------------------------------------- */
558 #ifndef OPENSSL_NO_EC
559 static int ecx_to_text(BIO *out, const void *key, int selection)
561 const ECX_KEY *ecx = key;
562 const char *type_label = NULL;
564 if (out == NULL || ecx == NULL) {
565 ERR_raise(ERR_LIB_PROV, ERR_R_PASSED_NULL_PARAMETER);
569 if ((selection & OSSL_KEYMGMT_SELECT_PRIVATE_KEY) != 0) {
570 if (ecx->privkey == NULL) {
571 ERR_raise(ERR_LIB_PROV, PROV_R_NOT_A_PRIVATE_KEY);
576 case ECX_KEY_TYPE_X25519:
577 type_label = "X25519 Private-Key";
579 case ECX_KEY_TYPE_X448:
580 type_label = "X448 Private-Key";
582 case ECX_KEY_TYPE_ED25519:
583 type_label = "ED25519 Private-Key";
585 case ECX_KEY_TYPE_ED448:
586 type_label = "ED448 Private-Key";
589 } else if ((selection & OSSL_KEYMGMT_SELECT_PUBLIC_KEY) != 0) {
590 /* ecx->pubkey is an array, not a pointer... */
591 if (!ecx->haspubkey) {
592 ERR_raise(ERR_LIB_PROV, PROV_R_NOT_A_PUBLIC_KEY);
597 case ECX_KEY_TYPE_X25519:
598 type_label = "X25519 Public-Key";
600 case ECX_KEY_TYPE_X448:
601 type_label = "X448 Public-Key";
603 case ECX_KEY_TYPE_ED25519:
604 type_label = "ED25519 Public-Key";
606 case ECX_KEY_TYPE_ED448:
607 type_label = "ED448 Public-Key";
612 if (BIO_printf(out, "%s:\n", type_label) <= 0)
614 if ((selection & OSSL_KEYMGMT_SELECT_PRIVATE_KEY) != 0
615 && !print_labeled_buf(out, "priv:", ecx->privkey, ecx->keylen))
617 if ((selection & OSSL_KEYMGMT_SELECT_PUBLIC_KEY) != 0
618 && !print_labeled_buf(out, "pub:", ecx->pubkey, ecx->keylen))
624 # define ed25519_input_type "ED25519"
625 # define ed448_input_type "ED448"
626 # define x25519_input_type "X25519"
627 # define x448_input_type "X448"
630 /* ---------------------------------------------------------------------- */
632 static int rsa_to_text(BIO *out, const void *key, int selection)
634 const RSA *rsa = key;
635 const char *type_label = "RSA key";
636 const char *modulus_label;
637 const char *exponent_label;
638 const BIGNUM *rsa_d = NULL, *rsa_n = NULL, *rsa_e = NULL;
639 STACK_OF(BIGNUM_const) *factors = NULL;
640 STACK_OF(BIGNUM_const) *exps = NULL;
641 STACK_OF(BIGNUM_const) *coeffs = NULL;
643 const RSA_PSS_PARAMS_30 *pss_params = ossl_rsa_get0_pss_params_30((RSA *)rsa);
646 if (out == NULL || rsa == NULL) {
647 ERR_raise(ERR_LIB_PROV, ERR_R_PASSED_NULL_PARAMETER);
651 factors = sk_BIGNUM_const_new_null();
652 exps = sk_BIGNUM_const_new_null();
653 coeffs = sk_BIGNUM_const_new_null();
655 if (factors == NULL || exps == NULL || coeffs == NULL) {
656 ERR_raise(ERR_LIB_PROV, ERR_R_MALLOC_FAILURE);
660 if ((selection & OSSL_KEYMGMT_SELECT_PRIVATE_KEY) != 0) {
661 type_label = "Private-Key";
662 modulus_label = "modulus:";
663 exponent_label = "publicExponent:";
664 } else if ((selection & OSSL_KEYMGMT_SELECT_PUBLIC_KEY) != 0) {
665 type_label = "Public-Key";
666 modulus_label = "Modulus:";
667 exponent_label = "Exponent:";
670 RSA_get0_key(rsa, &rsa_n, &rsa_e, &rsa_d);
671 ossl_rsa_get0_all_params((RSA *)rsa, factors, exps, coeffs);
672 primes = sk_BIGNUM_const_num(factors);
674 if ((selection & OSSL_KEYMGMT_SELECT_PRIVATE_KEY) != 0) {
675 if (BIO_printf(out, "%s: (%d bit, %d primes)\n",
676 type_label, BN_num_bits(rsa_n), primes) <= 0)
679 if (BIO_printf(out, "%s: (%d bit)\n",
680 type_label, BN_num_bits(rsa_n)) <= 0)
684 if (!print_labeled_bignum(out, modulus_label, rsa_n))
686 if (!print_labeled_bignum(out, exponent_label, rsa_e))
688 if ((selection & OSSL_KEYMGMT_SELECT_PRIVATE_KEY) != 0) {
691 if (!print_labeled_bignum(out, "privateExponent:", rsa_d))
693 if (!print_labeled_bignum(out, "prime1:",
694 sk_BIGNUM_const_value(factors, 0)))
696 if (!print_labeled_bignum(out, "prime2:",
697 sk_BIGNUM_const_value(factors, 1)))
699 if (!print_labeled_bignum(out, "exponent1:",
700 sk_BIGNUM_const_value(exps, 0)))
702 if (!print_labeled_bignum(out, "exponent2:",
703 sk_BIGNUM_const_value(exps, 1)))
705 if (!print_labeled_bignum(out, "coefficient:",
706 sk_BIGNUM_const_value(coeffs, 0)))
708 for (i = 2; i < sk_BIGNUM_const_num(factors); i++) {
709 if (BIO_printf(out, "prime%d:", i + 1) <= 0)
711 if (!print_labeled_bignum(out, NULL,
712 sk_BIGNUM_const_value(factors, i)))
714 if (BIO_printf(out, "exponent%d:", i + 1) <= 0)
716 if (!print_labeled_bignum(out, NULL,
717 sk_BIGNUM_const_value(exps, i)))
719 if (BIO_printf(out, "coefficient%d:", i + 1) <= 0)
721 if (!print_labeled_bignum(out, NULL,
722 sk_BIGNUM_const_value(coeffs, i - 1)))
727 if ((selection & OSSL_KEYMGMT_SELECT_OTHER_PARAMETERS) != 0) {
728 switch (RSA_test_flags(rsa, RSA_FLAG_TYPE_MASK)) {
729 case RSA_FLAG_TYPE_RSA:
730 if (!ossl_rsa_pss_params_30_is_unrestricted(pss_params)) {
731 if (BIO_printf(out, "(INVALID PSS PARAMETERS)\n") <= 0)
735 case RSA_FLAG_TYPE_RSASSAPSS:
736 if (ossl_rsa_pss_params_30_is_unrestricted(pss_params)) {
737 if (BIO_printf(out, "No PSS parameter restrictions\n") <= 0)
740 int hashalg_nid = ossl_rsa_pss_params_30_hashalg(pss_params);
742 ossl_rsa_pss_params_30_maskgenalg(pss_params);
743 int maskgenhashalg_nid =
744 ossl_rsa_pss_params_30_maskgenhashalg(pss_params);
745 int saltlen = ossl_rsa_pss_params_30_saltlen(pss_params);
747 ossl_rsa_pss_params_30_trailerfield(pss_params);
749 if (BIO_printf(out, "PSS parameter restrictions:\n") <= 0)
751 if (BIO_printf(out, " Hash Algorithm: %s%s\n",
752 ossl_rsa_oaeppss_nid2name(hashalg_nid),
753 (hashalg_nid == NID_sha1
754 ? " (default)" : "")) <= 0)
756 if (BIO_printf(out, " Mask Algorithm: %s with %s%s\n",
757 ossl_rsa_mgf_nid2name(maskgenalg_nid),
758 ossl_rsa_oaeppss_nid2name(maskgenhashalg_nid),
759 (maskgenalg_nid == NID_mgf1
760 && maskgenhashalg_nid == NID_sha1
761 ? " (default)" : "")) <= 0)
763 if (BIO_printf(out, " Minimum Salt Length: %d%s\n",
765 (saltlen == 20 ? " (default)" : "")) <= 0)
768 * TODO(3.0) Should we show the ASN.1 trailerField value, or
769 * the actual trailerfield byte (i.e. 0xBC for 1)?
770 * crypto/rsa/rsa_ameth.c isn't very clear on that, as it
771 * does display 0xBC when the default applies, but the ASN.1
772 * trailerField value otherwise...
774 if (BIO_printf(out, " Trailer Field: 0x%x%s\n",
776 (trailerfield == 1 ? " (default)" : "")) <= 0)
785 sk_BIGNUM_const_free(factors);
786 sk_BIGNUM_const_free(exps);
787 sk_BIGNUM_const_free(coeffs);
791 #define rsa_input_type "RSA"
792 #define rsapss_input_type "RSA-PSS"
794 /* ---------------------------------------------------------------------- */
796 static void *key2text_newctx(void *provctx)
801 static void key2text_freectx(ossl_unused void *vctx)
805 static const OSSL_PARAM *key2text_gettable_params(void *provctx)
807 static const OSSL_PARAM gettables[] = {
808 { OSSL_ENCODER_PARAM_OUTPUT_TYPE, OSSL_PARAM_UTF8_PTR, NULL, 0, 0 },
815 static int key2text_get_params(OSSL_PARAM params[], const char *input_type)
819 p = OSSL_PARAM_locate(params, OSSL_ENCODER_PARAM_INPUT_TYPE);
820 if (p != NULL && !OSSL_PARAM_set_utf8_ptr(p, input_type))
823 p = OSSL_PARAM_locate(params, OSSL_ENCODER_PARAM_OUTPUT_TYPE);
824 if (p != NULL && !OSSL_PARAM_set_utf8_ptr(p, "TEXT"))
830 static int key2text_encode(void *vctx, const void *key, int selection,
832 int (*key2text)(BIO *out, const void *key,
834 OSSL_PASSPHRASE_CALLBACK *cb, void *cbarg)
836 BIO *out = bio_new_from_core_bio(vctx, cout);
842 ret = key2text(out, key, selection);
848 #define MAKE_TEXT_ENCODER(impl, type) \
849 static OSSL_FUNC_encoder_get_params_fn \
850 impl##2text_get_params; \
851 static OSSL_FUNC_encoder_import_object_fn \
852 impl##2text_import_object; \
853 static OSSL_FUNC_encoder_free_object_fn \
854 impl##2text_free_object; \
855 static OSSL_FUNC_encoder_encode_fn impl##2text_encode; \
857 static int impl##2text_get_params(OSSL_PARAM params[]) \
859 return key2text_get_params(params, impl##_input_type); \
861 static void *impl##2text_import_object(void *ctx, int selection, \
862 const OSSL_PARAM params[]) \
864 return ossl_prov_import_key(ossl_##impl##_keymgmt_functions, \
865 ctx, selection, params); \
867 static void impl##2text_free_object(void *key) \
869 ossl_prov_free_key(ossl_##impl##_keymgmt_functions, key); \
871 static int impl##2text_encode(void *vctx, OSSL_CORE_BIO *cout, \
873 const OSSL_PARAM key_abstract[], \
875 OSSL_PASSPHRASE_CALLBACK *cb, \
878 /* We don't deal with abstract objects */ \
879 if (key_abstract != NULL) { \
880 ERR_raise(ERR_LIB_PROV, ERR_R_PASSED_INVALID_ARGUMENT); \
883 return key2text_encode(vctx, key, selection, cout, \
884 type##_to_text, cb, cbarg); \
886 const OSSL_DISPATCH ossl_##impl##_to_text_encoder_functions[] = { \
887 { OSSL_FUNC_ENCODER_NEWCTX, \
888 (void (*)(void))key2text_newctx }, \
889 { OSSL_FUNC_ENCODER_FREECTX, \
890 (void (*)(void))key2text_freectx }, \
891 { OSSL_FUNC_ENCODER_GETTABLE_PARAMS, \
892 (void (*)(void))key2text_gettable_params }, \
893 { OSSL_FUNC_ENCODER_GET_PARAMS, \
894 (void (*)(void))impl##2text_get_params }, \
895 { OSSL_FUNC_ENCODER_IMPORT_OBJECT, \
896 (void (*)(void))impl##2text_import_object }, \
897 { OSSL_FUNC_ENCODER_FREE_OBJECT, \
898 (void (*)(void))impl##2text_free_object }, \
899 { OSSL_FUNC_ENCODER_ENCODE, \
900 (void (*)(void))impl##2text_encode }, \
904 #ifndef OPENSSL_NO_DH
905 MAKE_TEXT_ENCODER(dh, dh);
906 MAKE_TEXT_ENCODER(dhx, dh);
908 #ifndef OPENSSL_NO_DSA
909 MAKE_TEXT_ENCODER(dsa, dsa);
911 #ifndef OPENSSL_NO_EC
912 MAKE_TEXT_ENCODER(ec, ec);
913 # ifndef OPENSSL_NO_SM2
914 MAKE_TEXT_ENCODER(sm2, ec);
916 MAKE_TEXT_ENCODER(ed25519, ecx);
917 MAKE_TEXT_ENCODER(ed448, ecx);
918 MAKE_TEXT_ENCODER(x25519, ecx);
919 MAKE_TEXT_ENCODER(x448, ecx);
921 MAKE_TEXT_ENCODER(rsa, rsa);
922 MAKE_TEXT_ENCODER(rsapss, rsa);