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 static int print_labeled_buf(BIO *out, const char *label,
130 const unsigned char *buf, size_t buflen)
134 if (BIO_printf(out, "%s\n", label) <= 0)
137 for (i = 0; i < buflen; i++) {
138 if ((i % LABELED_BUF_PRINT_WIDTH) == 0) {
139 if (i > 0 && BIO_printf(out, "\n") <= 0)
141 if (BIO_printf(out, " ") <= 0)
145 if (BIO_printf(out, "%02x%s", buf[i],
146 (i == buflen - 1) ? "" : ":") <= 0)
149 if (BIO_printf(out, "\n") <= 0)
155 #if !defined(OPENSSL_NO_DH) || !defined(OPENSSL_NO_DSA)
156 static int ffc_params_to_text(BIO *out, const FFC_PARAMS *ffc)
158 if (ffc->nid != NID_undef) {
159 #ifndef OPENSSL_NO_DH
160 const char *name = ffc_named_group_from_uid(ffc->nid);
164 if (BIO_printf(out, "GROUP: %s\n", name) <= 0)
168 /* How could this be? We should not have a nid in a no-dh build. */
173 if (!print_labeled_bignum(out, "P: ", ffc->p))
175 if (ffc->q != NULL) {
176 if (!print_labeled_bignum(out, "Q: ", ffc->q))
179 if (!print_labeled_bignum(out, "G: ", ffc->g))
181 if (ffc->j != NULL) {
182 if (!print_labeled_bignum(out, "J: ", ffc->j))
185 if (ffc->seed != NULL) {
186 if (!print_labeled_buf(out, "SEED:", ffc->seed, ffc->seedlen))
189 if (ffc->gindex != -1) {
190 if (BIO_printf(out, "gindex: %d\n", ffc->gindex) <= 0)
193 if (ffc->pcounter != -1) {
194 if (BIO_printf(out, "pcounter: %d\n", ffc->pcounter) <= 0)
198 if (BIO_printf(out, "h: %d\n", ffc->h) <= 0)
207 /* ---------------------------------------------------------------------- */
209 #ifndef OPENSSL_NO_DH
210 static int dh_to_text(BIO *out, const void *key, int selection)
213 const char *type_label = NULL;
214 const BIGNUM *priv_key = NULL, *pub_key = NULL;
215 const FFC_PARAMS *params = NULL;
216 const BIGNUM *p = NULL;
218 if (out == NULL || dh == NULL) {
219 ERR_raise(ERR_LIB_PROV, ERR_R_PASSED_NULL_PARAMETER);
223 if ((selection & OSSL_KEYMGMT_SELECT_PRIVATE_KEY) != 0)
224 type_label = "DH Private-Key";
225 else if ((selection & OSSL_KEYMGMT_SELECT_PUBLIC_KEY) != 0)
226 type_label = "DH Public-Key";
227 else if ((selection & OSSL_KEYMGMT_SELECT_DOMAIN_PARAMETERS) != 0)
228 type_label = "DH Parameters";
230 if ((selection & OSSL_KEYMGMT_SELECT_PRIVATE_KEY) != 0) {
231 priv_key = DH_get0_priv_key(dh);
232 if (priv_key == NULL) {
233 ERR_raise(ERR_LIB_PROV, PROV_R_NOT_A_PRIVATE_KEY);
237 if ((selection & OSSL_KEYMGMT_SELECT_PUBLIC_KEY) != 0) {
238 pub_key = DH_get0_pub_key(dh);
239 if (pub_key == NULL) {
240 ERR_raise(ERR_LIB_PROV, PROV_R_NOT_A_PUBLIC_KEY);
244 if ((selection & OSSL_KEYMGMT_SELECT_DOMAIN_PARAMETERS) != 0) {
245 params = dh_get0_params((DH *)dh);
246 if (params == NULL) {
247 ERR_raise(ERR_LIB_PROV, PROV_R_NOT_PARAMETERS);
254 ERR_raise(ERR_LIB_PROV, PROV_R_INVALID_KEY);
258 if (BIO_printf(out, "%s: (%d bit)\n", type_label, BN_num_bits(p)) <= 0)
261 && !print_labeled_bignum(out, "private-key:", priv_key))
264 && !print_labeled_bignum(out, "public-key:", pub_key))
267 && !ffc_params_to_text(out, params))
273 # define dh_input_type "DH"
274 # define dhx_input_type "DHX"
277 /* ---------------------------------------------------------------------- */
279 #ifndef OPENSSL_NO_DSA
280 static int dsa_to_text(BIO *out, const void *key, int selection)
282 const DSA *dsa = key;
283 const char *type_label = NULL;
284 const BIGNUM *priv_key = NULL, *pub_key = NULL;
285 const FFC_PARAMS *params = NULL;
286 const BIGNUM *p = NULL;
288 if (out == NULL || dsa == NULL) {
289 ERR_raise(ERR_LIB_PROV, ERR_R_PASSED_NULL_PARAMETER);
293 if ((selection & OSSL_KEYMGMT_SELECT_PRIVATE_KEY) != 0)
294 type_label = "Private-Key";
295 else if ((selection & OSSL_KEYMGMT_SELECT_PUBLIC_KEY) != 0)
296 type_label = "Public-Key";
297 else if ((selection & OSSL_KEYMGMT_SELECT_DOMAIN_PARAMETERS) != 0)
298 type_label = "DSA-Parameters";
300 if ((selection & OSSL_KEYMGMT_SELECT_PRIVATE_KEY) != 0) {
301 priv_key = DSA_get0_priv_key(dsa);
302 if (priv_key == NULL) {
303 ERR_raise(ERR_LIB_PROV, PROV_R_NOT_A_PRIVATE_KEY);
307 if ((selection & OSSL_KEYMGMT_SELECT_PUBLIC_KEY) != 0) {
308 pub_key = DSA_get0_pub_key(dsa);
309 if (pub_key == NULL) {
310 ERR_raise(ERR_LIB_PROV, PROV_R_NOT_A_PUBLIC_KEY);
314 if ((selection & OSSL_KEYMGMT_SELECT_DOMAIN_PARAMETERS) != 0) {
315 params = dsa_get0_params((DSA *)dsa);
316 if (params == NULL) {
317 ERR_raise(ERR_LIB_PROV, PROV_R_NOT_PARAMETERS);
324 ERR_raise(ERR_LIB_PROV, PROV_R_INVALID_KEY);
328 if (BIO_printf(out, "%s: (%d bit)\n", type_label, BN_num_bits(p)) <= 0)
331 && !print_labeled_bignum(out, "priv:", priv_key))
334 && !print_labeled_bignum(out, "pub: ", pub_key))
337 && !ffc_params_to_text(out, params))
343 # define dsa_input_type "DSA"
346 /* ---------------------------------------------------------------------- */
348 #ifndef OPENSSL_NO_EC
349 static int ec_param_explicit_curve_to_text(BIO *out, const EC_GROUP *group,
352 const char *plabel = "Prime:";
353 BIGNUM *p = NULL, *a = NULL, *b = NULL;
359 || !EC_GROUP_get_curve(group, p, a, b, ctx))
362 if (EC_GROUP_get_field_type(group) == NID_X9_62_characteristic_two_field) {
363 int basis_type = EC_GROUP_get_basis_type(group);
365 /* print the 'short name' of the base type OID */
366 if (basis_type == NID_undef
367 || BIO_printf(out, "Basis Type: %s\n", OBJ_nid2sn(basis_type)) <= 0)
369 plabel = "Polynomial:";
371 return print_labeled_bignum(out, plabel, p)
372 && print_labeled_bignum(out, "A: ", a)
373 && print_labeled_bignum(out, "B: ", b);
376 static int ec_param_explicit_gen_to_text(BIO *out, const EC_GROUP *group,
379 const EC_POINT *point = NULL;
381 const char *glabel = NULL;
382 point_conversion_form_t form;
384 form = EC_GROUP_get_point_conversion_form(group);
385 point = EC_GROUP_get0_generator(group);
386 gen = BN_CTX_get(ctx);
390 || EC_POINT_point2bn(group, point, form, gen, ctx) == NULL)
395 case POINT_CONVERSION_COMPRESSED:
396 glabel = "Generator (compressed):";
398 case POINT_CONVERSION_UNCOMPRESSED:
399 glabel = "Generator (uncompressed):";
401 case POINT_CONVERSION_HYBRID:
402 glabel = "Generator (hybrid):";
407 return print_labeled_bignum(out, glabel, gen);
412 /* Print explicit parameters */
413 static int ec_param_explicit_to_text(BIO *out, const EC_GROUP *group,
416 int ret = 0, tmp_nid;
418 const BIGNUM *order = NULL, *cofactor = NULL;
419 const unsigned char *seed;
422 ctx = BN_CTX_new_ex(libctx);
427 tmp_nid = EC_GROUP_get_field_type(group);
428 order = EC_GROUP_get0_order(group);
432 seed = EC_GROUP_get0_seed(group);
434 seed_len = EC_GROUP_get_seed_len(group);
435 cofactor = EC_GROUP_get0_cofactor(group);
437 /* print the 'short name' of the field type */
438 if (BIO_printf(out, "Field Type: %s\n", OBJ_nid2sn(tmp_nid)) <= 0
439 || !ec_param_explicit_curve_to_text(out, group, ctx)
440 || !ec_param_explicit_gen_to_text(out, group, ctx)
441 || !print_labeled_bignum(out, "Order: ", order)
443 && !print_labeled_bignum(out, "Cofactor: ", cofactor))
445 && !print_labeled_buf(out, "Seed:", seed, seed_len)))
454 static int ec_param_to_text(BIO *out, const EC_GROUP *group,
457 if (EC_GROUP_get_asn1_flag(group) & OPENSSL_EC_NAMED_CURVE) {
458 const char *curve_name;
459 int curve_nid = EC_GROUP_get_curve_name(group);
461 /* Explicit parameters */
462 if (curve_nid == NID_undef)
465 if (BIO_printf(out, "%s: %s\n", "ASN1 OID", OBJ_nid2sn(curve_nid)) <= 0)
468 curve_name = EC_curve_nid2nist(curve_nid);
469 return (curve_name == NULL
470 || BIO_printf(out, "%s: %s\n", "NIST CURVE", curve_name) > 0);
472 return ec_param_explicit_to_text(out, group, libctx);
476 static int ec_to_text(BIO *out, const void *key, int selection)
478 const EC_KEY *ec = key;
479 const char *type_label = NULL;
480 unsigned char *priv = NULL, *pub = NULL;
481 size_t priv_len = 0, pub_len = 0;
482 const EC_GROUP *group;
485 if (out == NULL || ec == NULL) {
486 ERR_raise(ERR_LIB_PROV, ERR_R_PASSED_NULL_PARAMETER);
490 if ((group = EC_KEY_get0_group(ec)) == NULL) {
491 ERR_raise(ERR_LIB_PROV, PROV_R_INVALID_KEY);
495 if ((selection & OSSL_KEYMGMT_SELECT_PRIVATE_KEY) != 0)
496 type_label = "Private-Key";
497 else if ((selection & OSSL_KEYMGMT_SELECT_PUBLIC_KEY) != 0)
498 type_label = "Public-Key";
499 else if ((selection & OSSL_KEYMGMT_SELECT_DOMAIN_PARAMETERS) != 0)
500 type_label = "EC-Parameters";
502 if ((selection & OSSL_KEYMGMT_SELECT_PRIVATE_KEY) != 0) {
503 const BIGNUM *priv_key = EC_KEY_get0_private_key(ec);
505 if (priv_key == NULL) {
506 ERR_raise(ERR_LIB_PROV, PROV_R_NOT_A_PRIVATE_KEY);
509 priv_len = EC_KEY_priv2buf(ec, &priv);
513 if ((selection & OSSL_KEYMGMT_SELECT_PUBLIC_KEY) != 0) {
514 const EC_POINT *pub_pt = EC_KEY_get0_public_key(ec);
516 if (pub_pt == NULL) {
517 ERR_raise(ERR_LIB_PROV, PROV_R_NOT_A_PUBLIC_KEY);
521 pub_len = EC_KEY_key2buf(ec, EC_KEY_get_conv_form(ec), &pub, NULL);
526 if (BIO_printf(out, "%s: (%d bit)\n", type_label,
527 EC_GROUP_order_bits(group)) <= 0)
530 && !print_labeled_buf(out, "priv:", priv, priv_len))
533 && !print_labeled_buf(out, "pub:", pub, pub_len))
535 if ((selection & OSSL_KEYMGMT_SELECT_DOMAIN_PARAMETERS) != 0)
536 ret = ec_param_to_text(out, group, ec_key_get_libctx(ec));
538 OPENSSL_clear_free(priv, priv_len);
543 # define ec_input_type "EC"
546 /* ---------------------------------------------------------------------- */
548 #ifndef OPENSSL_NO_EC
549 static int ecx_to_text(BIO *out, const void *key, int selection)
551 const ECX_KEY *ecx = key;
552 const char *type_label = NULL;
554 if (out == NULL || ecx == NULL) {
555 ERR_raise(ERR_LIB_PROV, ERR_R_PASSED_NULL_PARAMETER);
559 if ((selection & OSSL_KEYMGMT_SELECT_PRIVATE_KEY) != 0) {
560 if (ecx->privkey == NULL) {
561 ERR_raise(ERR_LIB_PROV, PROV_R_NOT_A_PRIVATE_KEY);
566 case ECX_KEY_TYPE_X25519:
567 type_label = "X25519 Private-Key";
569 case ECX_KEY_TYPE_X448:
570 type_label = "X448 Private-Key";
572 case ECX_KEY_TYPE_ED25519:
573 type_label = "ED25519 Private-Key";
575 case ECX_KEY_TYPE_ED448:
576 type_label = "ED448 Private-Key";
579 } else if ((selection & OSSL_KEYMGMT_SELECT_PUBLIC_KEY) != 0) {
580 /* ecx->pubkey is an array, not a pointer... */
581 if (!ecx->haspubkey) {
582 ERR_raise(ERR_LIB_PROV, PROV_R_NOT_A_PUBLIC_KEY);
587 case ECX_KEY_TYPE_X25519:
588 type_label = "X25519 Public-Key";
590 case ECX_KEY_TYPE_X448:
591 type_label = "X448 Public-Key";
593 case ECX_KEY_TYPE_ED25519:
594 type_label = "ED25519 Public-Key";
596 case ECX_KEY_TYPE_ED448:
597 type_label = "ED448 Public-Key";
602 if (BIO_printf(out, "%s:\n", type_label) <= 0)
604 if ((selection & OSSL_KEYMGMT_SELECT_PRIVATE_KEY) != 0
605 && !print_labeled_buf(out, "priv:", ecx->privkey, ecx->keylen))
607 if ((selection & OSSL_KEYMGMT_SELECT_PUBLIC_KEY) != 0
608 && !print_labeled_buf(out, "pub:", ecx->pubkey, ecx->keylen))
614 # define ed25519_input_type "ED25519"
615 # define ed448_input_type "ED448"
616 # define x25519_input_type "X25519"
617 # define x448_input_type "X448"
620 /* ---------------------------------------------------------------------- */
622 static int rsa_to_text(BIO *out, const void *key, int selection)
624 const RSA *rsa = key;
625 const char *type_label = "RSA key";
626 const char *modulus_label;
627 const char *exponent_label;
628 const BIGNUM *rsa_d = NULL, *rsa_n = NULL, *rsa_e = NULL;
629 STACK_OF(BIGNUM_const) *factors = NULL;
630 STACK_OF(BIGNUM_const) *exps = NULL;
631 STACK_OF(BIGNUM_const) *coeffs = NULL;
633 const RSA_PSS_PARAMS_30 *pss_params = rsa_get0_pss_params_30((RSA *)rsa);
636 if (out == NULL || rsa == NULL) {
637 ERR_raise(ERR_LIB_PROV, ERR_R_PASSED_NULL_PARAMETER);
641 factors = sk_BIGNUM_const_new_null();
642 exps = sk_BIGNUM_const_new_null();
643 coeffs = sk_BIGNUM_const_new_null();
645 if (factors == NULL || exps == NULL || coeffs == NULL) {
646 ERR_raise(ERR_LIB_PROV, ERR_R_MALLOC_FAILURE);
650 if ((selection & OSSL_KEYMGMT_SELECT_PRIVATE_KEY) != 0) {
651 type_label = "Private-Key";
652 modulus_label = "modulus:";
653 exponent_label = "publicExponent:";
654 } else if ((selection & OSSL_KEYMGMT_SELECT_PUBLIC_KEY) != 0) {
655 type_label = "Public-Key";
656 modulus_label = "Modulus:";
657 exponent_label = "Exponent:";
660 RSA_get0_key(rsa, &rsa_n, &rsa_e, &rsa_d);
661 rsa_get0_all_params((RSA *)rsa, factors, exps, coeffs);
662 primes = sk_BIGNUM_const_num(factors);
664 if ((selection & OSSL_KEYMGMT_SELECT_PRIVATE_KEY) != 0) {
665 if (BIO_printf(out, "%s: (%d bit, %d primes)\n",
666 type_label, BN_num_bits(rsa_n), primes) <= 0)
669 if (BIO_printf(out, "%s: (%d bit)\n",
670 type_label, BN_num_bits(rsa_n)) <= 0)
674 if (!print_labeled_bignum(out, modulus_label, rsa_n))
676 if (!print_labeled_bignum(out, exponent_label, rsa_e))
678 if ((selection & OSSL_KEYMGMT_SELECT_PRIVATE_KEY) != 0) {
681 if (!print_labeled_bignum(out, "privateExponent:", rsa_d))
683 if (!print_labeled_bignum(out, "prime1:",
684 sk_BIGNUM_const_value(factors, 0)))
686 if (!print_labeled_bignum(out, "prime2:",
687 sk_BIGNUM_const_value(factors, 1)))
689 if (!print_labeled_bignum(out, "exponent1:",
690 sk_BIGNUM_const_value(exps, 0)))
692 if (!print_labeled_bignum(out, "exponent2:",
693 sk_BIGNUM_const_value(exps, 1)))
695 if (!print_labeled_bignum(out, "coefficient:",
696 sk_BIGNUM_const_value(coeffs, 0)))
698 for (i = 2; i < sk_BIGNUM_const_num(factors); i++) {
699 if (BIO_printf(out, "prime%d:", i + 1) <= 0)
701 if (!print_labeled_bignum(out, NULL,
702 sk_BIGNUM_const_value(factors, i)))
704 if (BIO_printf(out, "exponent%d:", i + 1) <= 0)
706 if (!print_labeled_bignum(out, NULL,
707 sk_BIGNUM_const_value(exps, i)))
709 if (BIO_printf(out, "coefficient%d:", i + 1) <= 0)
711 if (!print_labeled_bignum(out, NULL,
712 sk_BIGNUM_const_value(coeffs, i - 1)))
717 if ((selection & OSSL_KEYMGMT_SELECT_OTHER_PARAMETERS) != 0) {
718 switch (RSA_test_flags(rsa, RSA_FLAG_TYPE_MASK)) {
719 case RSA_FLAG_TYPE_RSA:
720 if (!rsa_pss_params_30_is_unrestricted(pss_params)) {
721 if (BIO_printf(out, "(INVALID PSS PARAMETERS)\n") <= 0)
725 case RSA_FLAG_TYPE_RSASSAPSS:
726 if (rsa_pss_params_30_is_unrestricted(pss_params)) {
727 if (BIO_printf(out, "No PSS parameter restrictions\n") <= 0)
730 int hashalg_nid = rsa_pss_params_30_hashalg(pss_params);
731 int maskgenalg_nid = rsa_pss_params_30_maskgenalg(pss_params);
732 int maskgenhashalg_nid =
733 rsa_pss_params_30_maskgenhashalg(pss_params);
734 int saltlen = rsa_pss_params_30_saltlen(pss_params);
735 int trailerfield = rsa_pss_params_30_trailerfield(pss_params);
737 if (BIO_printf(out, "PSS parameter restrictions:\n") <= 0)
739 if (BIO_printf(out, " Hash Algorithm: %s%s\n",
740 rsa_oaeppss_nid2name(hashalg_nid),
741 (hashalg_nid == NID_sha1
742 ? " (default)" : "")) <= 0)
744 if (BIO_printf(out, " Mask Algorithm: %s with %s%s\n",
745 rsa_mgf_nid2name(maskgenalg_nid),
746 rsa_oaeppss_nid2name(maskgenhashalg_nid),
747 (maskgenalg_nid == NID_mgf1
748 && maskgenhashalg_nid == NID_sha1
749 ? " (default)" : "")) <= 0)
751 if (BIO_printf(out, " Minimum Salt Length: %d%s\n",
753 (saltlen == 20 ? " (default)" : "")) <= 0)
756 * TODO(3.0) Should we show the ASN.1 trailerField value, or
757 * the actual trailerfield byte (i.e. 0xBC for 1)?
758 * crypto/rsa/rsa_ameth.c isn't very clear on that, as it
759 * does display 0xBC when the default applies, but the ASN.1
760 * trailerField value otherwise...
762 if (BIO_printf(out, " Trailer Field: 0x%x%s\n",
764 (trailerfield == 1 ? " (default)" : "")) <= 0)
773 sk_BIGNUM_const_free(factors);
774 sk_BIGNUM_const_free(exps);
775 sk_BIGNUM_const_free(coeffs);
779 #define rsa_input_type "RSA"
780 #define rsapss_input_type "RSA-PSS"
782 /* ---------------------------------------------------------------------- */
784 static void *key2text_newctx(void *provctx)
789 static void key2text_freectx(ossl_unused void *vctx)
793 static const OSSL_PARAM *key2text_gettable_params(void *provctx)
795 static const OSSL_PARAM gettables[] = {
796 { OSSL_ENCODER_PARAM_OUTPUT_TYPE, OSSL_PARAM_UTF8_PTR, NULL, 0, 0 },
803 static int key2text_get_params(OSSL_PARAM params[], const char *input_type)
807 p = OSSL_PARAM_locate(params, OSSL_ENCODER_PARAM_INPUT_TYPE);
808 if (p != NULL && !OSSL_PARAM_set_utf8_ptr(p, input_type))
811 p = OSSL_PARAM_locate(params, OSSL_ENCODER_PARAM_OUTPUT_TYPE);
812 if (p != NULL && !OSSL_PARAM_set_utf8_ptr(p, "TEXT"))
818 static int key2text_encode(void *vctx, const void *key, int selection,
820 int (*key2text)(BIO *out, const void *key,
822 OSSL_PASSPHRASE_CALLBACK *cb, void *cbarg)
824 BIO *out = bio_new_from_core_bio(vctx, cout);
830 ret = key2text(out, key, selection);
836 #define MAKE_TEXT_ENCODER(impl, type) \
837 static OSSL_FUNC_encoder_get_params_fn \
838 impl##2text_get_params; \
839 static OSSL_FUNC_encoder_import_object_fn \
840 impl##2text_import_object; \
841 static OSSL_FUNC_encoder_free_object_fn \
842 impl##2text_free_object; \
843 static OSSL_FUNC_encoder_encode_fn impl##2text_encode; \
845 static int impl##2text_get_params(OSSL_PARAM params[]) \
847 return key2text_get_params(params, impl##_input_type); \
849 static void *impl##2text_import_object(void *ctx, int selection, \
850 const OSSL_PARAM params[]) \
852 return ossl_prov_import_key(impl##_keymgmt_functions, \
853 ctx, selection, params); \
855 static void impl##2text_free_object(void *key) \
857 ossl_prov_free_key(impl##_keymgmt_functions, key); \
859 static int impl##2text_encode(void *vctx, OSSL_CORE_BIO *cout, \
861 const OSSL_PARAM key_abstract[], \
863 OSSL_PASSPHRASE_CALLBACK *cb, \
866 /* We don't deal with abstract objects */ \
867 if (key_abstract != NULL) { \
868 ERR_raise(ERR_LIB_PROV, ERR_R_PASSED_INVALID_ARGUMENT); \
871 return key2text_encode(vctx, key, selection, cout, \
872 type##_to_text, cb, cbarg); \
874 const OSSL_DISPATCH impl##_to_text_encoder_functions[] = { \
875 { OSSL_FUNC_ENCODER_NEWCTX, \
876 (void (*)(void))key2text_newctx }, \
877 { OSSL_FUNC_ENCODER_FREECTX, \
878 (void (*)(void))key2text_freectx }, \
879 { OSSL_FUNC_ENCODER_GETTABLE_PARAMS, \
880 (void (*)(void))key2text_gettable_params }, \
881 { OSSL_FUNC_ENCODER_GET_PARAMS, \
882 (void (*)(void))impl##2text_get_params }, \
883 { OSSL_FUNC_ENCODER_IMPORT_OBJECT, \
884 (void (*)(void))impl##2text_import_object }, \
885 { OSSL_FUNC_ENCODER_FREE_OBJECT, \
886 (void (*)(void))impl##2text_free_object }, \
887 { OSSL_FUNC_ENCODER_ENCODE, \
888 (void (*)(void))impl##2text_encode }, \
892 #ifndef OPENSSL_NO_DH
893 MAKE_TEXT_ENCODER(dh, dh);
894 MAKE_TEXT_ENCODER(dhx, dh);
896 #ifndef OPENSSL_NO_DSA
897 MAKE_TEXT_ENCODER(dsa, dsa);
899 #ifndef OPENSSL_NO_EC
900 MAKE_TEXT_ENCODER(ec, ec);
901 MAKE_TEXT_ENCODER(ed25519, ecx);
902 MAKE_TEXT_ENCODER(ed448, ecx);
903 MAKE_TEXT_ENCODER(x25519, ecx);
904 MAKE_TEXT_ENCODER(x448, ecx);
906 MAKE_TEXT_ENCODER(rsa, rsa);
907 MAKE_TEXT_ENCODER(rsapss, rsa);