encode_key2text.c 28 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880
  1. /*
  2. * Copyright 2020-2023 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. * Low level APIs are deprecated for public use, but still ok for internal use.
  11. */
  12. #include "internal/deprecated.h"
  13. #include <ctype.h>
  14. #include <openssl/core.h>
  15. #include <openssl/core_dispatch.h>
  16. #include <openssl/core_names.h>
  17. #include <openssl/bn.h>
  18. #include <openssl/err.h>
  19. #include <openssl/safestack.h>
  20. #include <openssl/proverr.h>
  21. #include "internal/ffc.h"
  22. #include "crypto/bn.h" /* bn_get_words() */
  23. #include "crypto/dh.h" /* ossl_dh_get0_params() */
  24. #include "crypto/dsa.h" /* ossl_dsa_get0_params() */
  25. #include "crypto/ec.h" /* ossl_ec_key_get_libctx */
  26. #include "crypto/ecx.h" /* ECX_KEY, etc... */
  27. #include "crypto/rsa.h" /* RSA_PSS_PARAMS_30, etc... */
  28. #include "prov/bio.h"
  29. #include "prov/implementations.h"
  30. #include "endecoder_local.h"
  31. DEFINE_SPECIAL_STACK_OF_CONST(BIGNUM_const, BIGNUM)
  32. # ifdef SIXTY_FOUR_BIT_LONG
  33. # define BN_FMTu "%lu"
  34. # define BN_FMTx "%lx"
  35. # endif
  36. # ifdef SIXTY_FOUR_BIT
  37. # define BN_FMTu "%llu"
  38. # define BN_FMTx "%llx"
  39. # endif
  40. # ifdef THIRTY_TWO_BIT
  41. # define BN_FMTu "%u"
  42. # define BN_FMTx "%x"
  43. # endif
  44. static int print_labeled_bignum(BIO *out, const char *label, const BIGNUM *bn)
  45. {
  46. int ret = 0, use_sep = 0;
  47. char *hex_str = NULL, *p;
  48. const char spaces[] = " ";
  49. const char *post_label_spc = " ";
  50. const char *neg = "";
  51. int bytes;
  52. if (bn == NULL)
  53. return 0;
  54. if (label == NULL) {
  55. label = "";
  56. post_label_spc = "";
  57. }
  58. if (BN_is_zero(bn))
  59. return BIO_printf(out, "%s%s0\n", label, post_label_spc);
  60. if (BN_num_bytes(bn) <= BN_BYTES) {
  61. BN_ULONG *words = bn_get_words(bn);
  62. if (BN_is_negative(bn))
  63. neg = "-";
  64. return BIO_printf(out, "%s%s%s" BN_FMTu " (%s0x" BN_FMTx ")\n",
  65. label, post_label_spc, neg, words[0], neg, words[0]);
  66. }
  67. hex_str = BN_bn2hex(bn);
  68. if (hex_str == NULL)
  69. return 0;
  70. p = hex_str;
  71. if (*p == '-') {
  72. ++p;
  73. neg = " (Negative)";
  74. }
  75. if (BIO_printf(out, "%s%s\n", label, neg) <= 0)
  76. goto err;
  77. /* Keep track of how many bytes we have printed out so far */
  78. bytes = 0;
  79. if (BIO_printf(out, "%s", spaces) <= 0)
  80. goto err;
  81. /* Add a leading 00 if the top bit is set */
  82. if (*p >= '8') {
  83. if (BIO_printf(out, "%02x", 0) <= 0)
  84. goto err;
  85. ++bytes;
  86. use_sep = 1;
  87. }
  88. while (*p != '\0') {
  89. /* Do a newline after every 15 hex bytes + add the space indent */
  90. if ((bytes % 15) == 0 && bytes > 0) {
  91. if (BIO_printf(out, ":\n%s", spaces) <= 0)
  92. goto err;
  93. use_sep = 0; /* The first byte on the next line doesn't have a : */
  94. }
  95. if (BIO_printf(out, "%s%c%c", use_sep ? ":" : "",
  96. tolower(p[0]), tolower(p[1])) <= 0)
  97. goto err;
  98. ++bytes;
  99. p += 2;
  100. use_sep = 1;
  101. }
  102. if (BIO_printf(out, "\n") <= 0)
  103. goto err;
  104. ret = 1;
  105. err:
  106. OPENSSL_free(hex_str);
  107. return ret;
  108. }
  109. /* Number of octets per line */
  110. #define LABELED_BUF_PRINT_WIDTH 15
  111. #if !defined(OPENSSL_NO_DH) || !defined(OPENSSL_NO_DSA) || !defined(OPENSSL_NO_EC)
  112. static int print_labeled_buf(BIO *out, const char *label,
  113. const unsigned char *buf, size_t buflen)
  114. {
  115. size_t i;
  116. if (BIO_printf(out, "%s\n", label) <= 0)
  117. return 0;
  118. for (i = 0; i < buflen; i++) {
  119. if ((i % LABELED_BUF_PRINT_WIDTH) == 0) {
  120. if (i > 0 && BIO_printf(out, "\n") <= 0)
  121. return 0;
  122. if (BIO_printf(out, " ") <= 0)
  123. return 0;
  124. }
  125. if (BIO_printf(out, "%02x%s", buf[i],
  126. (i == buflen - 1) ? "" : ":") <= 0)
  127. return 0;
  128. }
  129. if (BIO_printf(out, "\n") <= 0)
  130. return 0;
  131. return 1;
  132. }
  133. #endif
  134. #if !defined(OPENSSL_NO_DH) || !defined(OPENSSL_NO_DSA)
  135. static int ffc_params_to_text(BIO *out, const FFC_PARAMS *ffc)
  136. {
  137. if (ffc->nid != NID_undef) {
  138. #ifndef OPENSSL_NO_DH
  139. const DH_NAMED_GROUP *group = ossl_ffc_uid_to_dh_named_group(ffc->nid);
  140. const char *name = ossl_ffc_named_group_get_name(group);
  141. if (name == NULL)
  142. goto err;
  143. if (BIO_printf(out, "GROUP: %s\n", name) <= 0)
  144. goto err;
  145. return 1;
  146. #else
  147. /* How could this be? We should not have a nid in a no-dh build. */
  148. goto err;
  149. #endif
  150. }
  151. if (!print_labeled_bignum(out, "P: ", ffc->p))
  152. goto err;
  153. if (ffc->q != NULL) {
  154. if (!print_labeled_bignum(out, "Q: ", ffc->q))
  155. goto err;
  156. }
  157. if (!print_labeled_bignum(out, "G: ", ffc->g))
  158. goto err;
  159. if (ffc->j != NULL) {
  160. if (!print_labeled_bignum(out, "J: ", ffc->j))
  161. goto err;
  162. }
  163. if (ffc->seed != NULL) {
  164. if (!print_labeled_buf(out, "SEED:", ffc->seed, ffc->seedlen))
  165. goto err;
  166. }
  167. if (ffc->gindex != -1) {
  168. if (BIO_printf(out, "gindex: %d\n", ffc->gindex) <= 0)
  169. goto err;
  170. }
  171. if (ffc->pcounter != -1) {
  172. if (BIO_printf(out, "pcounter: %d\n", ffc->pcounter) <= 0)
  173. goto err;
  174. }
  175. if (ffc->h != 0) {
  176. if (BIO_printf(out, "h: %d\n", ffc->h) <= 0)
  177. goto err;
  178. }
  179. return 1;
  180. err:
  181. return 0;
  182. }
  183. #endif
  184. /* ---------------------------------------------------------------------- */
  185. #ifndef OPENSSL_NO_DH
  186. static int dh_to_text(BIO *out, const void *key, int selection)
  187. {
  188. const DH *dh = key;
  189. const char *type_label = NULL;
  190. const BIGNUM *priv_key = NULL, *pub_key = NULL;
  191. const FFC_PARAMS *params = NULL;
  192. const BIGNUM *p = NULL;
  193. long length;
  194. if (out == NULL || dh == NULL) {
  195. ERR_raise(ERR_LIB_PROV, ERR_R_PASSED_NULL_PARAMETER);
  196. return 0;
  197. }
  198. if ((selection & OSSL_KEYMGMT_SELECT_PRIVATE_KEY) != 0)
  199. type_label = "DH Private-Key";
  200. else if ((selection & OSSL_KEYMGMT_SELECT_PUBLIC_KEY) != 0)
  201. type_label = "DH Public-Key";
  202. else if ((selection & OSSL_KEYMGMT_SELECT_DOMAIN_PARAMETERS) != 0)
  203. type_label = "DH Parameters";
  204. if ((selection & OSSL_KEYMGMT_SELECT_PRIVATE_KEY) != 0) {
  205. priv_key = DH_get0_priv_key(dh);
  206. if (priv_key == NULL) {
  207. ERR_raise(ERR_LIB_PROV, PROV_R_NOT_A_PRIVATE_KEY);
  208. return 0;
  209. }
  210. }
  211. if ((selection & OSSL_KEYMGMT_SELECT_KEYPAIR) != 0) {
  212. pub_key = DH_get0_pub_key(dh);
  213. if (pub_key == NULL) {
  214. ERR_raise(ERR_LIB_PROV, PROV_R_NOT_A_PUBLIC_KEY);
  215. return 0;
  216. }
  217. }
  218. if ((selection & OSSL_KEYMGMT_SELECT_DOMAIN_PARAMETERS) != 0) {
  219. params = ossl_dh_get0_params((DH *)dh);
  220. if (params == NULL) {
  221. ERR_raise(ERR_LIB_PROV, PROV_R_NOT_PARAMETERS);
  222. return 0;
  223. }
  224. }
  225. p = DH_get0_p(dh);
  226. if (p == NULL) {
  227. ERR_raise(ERR_LIB_PROV, PROV_R_INVALID_KEY);
  228. return 0;
  229. }
  230. if (BIO_printf(out, "%s: (%d bit)\n", type_label, BN_num_bits(p)) <= 0)
  231. return 0;
  232. if (priv_key != NULL
  233. && !print_labeled_bignum(out, "private-key:", priv_key))
  234. return 0;
  235. if (pub_key != NULL
  236. && !print_labeled_bignum(out, "public-key:", pub_key))
  237. return 0;
  238. if (params != NULL
  239. && !ffc_params_to_text(out, params))
  240. return 0;
  241. length = DH_get_length(dh);
  242. if (length > 0
  243. && BIO_printf(out, "recommended-private-length: %ld bits\n",
  244. length) <= 0)
  245. return 0;
  246. return 1;
  247. }
  248. # define dh_input_type "DH"
  249. # define dhx_input_type "DHX"
  250. #endif
  251. /* ---------------------------------------------------------------------- */
  252. #ifndef OPENSSL_NO_DSA
  253. static int dsa_to_text(BIO *out, const void *key, int selection)
  254. {
  255. const DSA *dsa = key;
  256. const char *type_label = NULL;
  257. const BIGNUM *priv_key = NULL, *pub_key = NULL;
  258. const FFC_PARAMS *params = NULL;
  259. const BIGNUM *p = NULL;
  260. if (out == NULL || dsa == NULL) {
  261. ERR_raise(ERR_LIB_PROV, ERR_R_PASSED_NULL_PARAMETER);
  262. return 0;
  263. }
  264. if ((selection & OSSL_KEYMGMT_SELECT_PRIVATE_KEY) != 0)
  265. type_label = "Private-Key";
  266. else if ((selection & OSSL_KEYMGMT_SELECT_PUBLIC_KEY) != 0)
  267. type_label = "Public-Key";
  268. else if ((selection & OSSL_KEYMGMT_SELECT_DOMAIN_PARAMETERS) != 0)
  269. type_label = "DSA-Parameters";
  270. if ((selection & OSSL_KEYMGMT_SELECT_PRIVATE_KEY) != 0) {
  271. priv_key = DSA_get0_priv_key(dsa);
  272. if (priv_key == NULL) {
  273. ERR_raise(ERR_LIB_PROV, PROV_R_NOT_A_PRIVATE_KEY);
  274. return 0;
  275. }
  276. }
  277. if ((selection & OSSL_KEYMGMT_SELECT_KEYPAIR) != 0) {
  278. pub_key = DSA_get0_pub_key(dsa);
  279. if (pub_key == NULL) {
  280. ERR_raise(ERR_LIB_PROV, PROV_R_NOT_A_PUBLIC_KEY);
  281. return 0;
  282. }
  283. }
  284. if ((selection & OSSL_KEYMGMT_SELECT_DOMAIN_PARAMETERS) != 0) {
  285. params = ossl_dsa_get0_params((DSA *)dsa);
  286. if (params == NULL) {
  287. ERR_raise(ERR_LIB_PROV, PROV_R_NOT_PARAMETERS);
  288. return 0;
  289. }
  290. }
  291. p = DSA_get0_p(dsa);
  292. if (p == NULL) {
  293. ERR_raise(ERR_LIB_PROV, PROV_R_INVALID_KEY);
  294. return 0;
  295. }
  296. if (BIO_printf(out, "%s: (%d bit)\n", type_label, BN_num_bits(p)) <= 0)
  297. return 0;
  298. if (priv_key != NULL
  299. && !print_labeled_bignum(out, "priv:", priv_key))
  300. return 0;
  301. if (pub_key != NULL
  302. && !print_labeled_bignum(out, "pub: ", pub_key))
  303. return 0;
  304. if (params != NULL
  305. && !ffc_params_to_text(out, params))
  306. return 0;
  307. return 1;
  308. }
  309. # define dsa_input_type "DSA"
  310. #endif
  311. /* ---------------------------------------------------------------------- */
  312. #ifndef OPENSSL_NO_EC
  313. static int ec_param_explicit_curve_to_text(BIO *out, const EC_GROUP *group,
  314. BN_CTX *ctx)
  315. {
  316. const char *plabel = "Prime:";
  317. BIGNUM *p = NULL, *a = NULL, *b = NULL;
  318. p = BN_CTX_get(ctx);
  319. a = BN_CTX_get(ctx);
  320. b = BN_CTX_get(ctx);
  321. if (b == NULL
  322. || !EC_GROUP_get_curve(group, p, a, b, ctx))
  323. return 0;
  324. if (EC_GROUP_get_field_type(group) == NID_X9_62_characteristic_two_field) {
  325. int basis_type = EC_GROUP_get_basis_type(group);
  326. /* print the 'short name' of the base type OID */
  327. if (basis_type == NID_undef
  328. || BIO_printf(out, "Basis Type: %s\n", OBJ_nid2sn(basis_type)) <= 0)
  329. return 0;
  330. plabel = "Polynomial:";
  331. }
  332. return print_labeled_bignum(out, plabel, p)
  333. && print_labeled_bignum(out, "A: ", a)
  334. && print_labeled_bignum(out, "B: ", b);
  335. }
  336. static int ec_param_explicit_gen_to_text(BIO *out, const EC_GROUP *group,
  337. BN_CTX *ctx)
  338. {
  339. int ret;
  340. size_t buflen;
  341. point_conversion_form_t form;
  342. const EC_POINT *point = NULL;
  343. const char *glabel = NULL;
  344. unsigned char *buf = NULL;
  345. form = EC_GROUP_get_point_conversion_form(group);
  346. point = EC_GROUP_get0_generator(group);
  347. if (point == NULL)
  348. return 0;
  349. switch (form) {
  350. case POINT_CONVERSION_COMPRESSED:
  351. glabel = "Generator (compressed):";
  352. break;
  353. case POINT_CONVERSION_UNCOMPRESSED:
  354. glabel = "Generator (uncompressed):";
  355. break;
  356. case POINT_CONVERSION_HYBRID:
  357. glabel = "Generator (hybrid):";
  358. break;
  359. default:
  360. return 0;
  361. }
  362. buflen = EC_POINT_point2buf(group, point, form, &buf, ctx);
  363. if (buflen == 0)
  364. return 0;
  365. ret = print_labeled_buf(out, glabel, buf, buflen);
  366. OPENSSL_clear_free(buf, buflen);
  367. return ret;
  368. }
  369. /* Print explicit parameters */
  370. static int ec_param_explicit_to_text(BIO *out, const EC_GROUP *group,
  371. OSSL_LIB_CTX *libctx)
  372. {
  373. int ret = 0, tmp_nid;
  374. BN_CTX *ctx = NULL;
  375. const BIGNUM *order = NULL, *cofactor = NULL;
  376. const unsigned char *seed;
  377. size_t seed_len = 0;
  378. ctx = BN_CTX_new_ex(libctx);
  379. if (ctx == NULL)
  380. return 0;
  381. BN_CTX_start(ctx);
  382. tmp_nid = EC_GROUP_get_field_type(group);
  383. order = EC_GROUP_get0_order(group);
  384. if (order == NULL)
  385. goto err;
  386. seed = EC_GROUP_get0_seed(group);
  387. if (seed != NULL)
  388. seed_len = EC_GROUP_get_seed_len(group);
  389. cofactor = EC_GROUP_get0_cofactor(group);
  390. /* print the 'short name' of the field type */
  391. if (BIO_printf(out, "Field Type: %s\n", OBJ_nid2sn(tmp_nid)) <= 0
  392. || !ec_param_explicit_curve_to_text(out, group, ctx)
  393. || !ec_param_explicit_gen_to_text(out, group, ctx)
  394. || !print_labeled_bignum(out, "Order: ", order)
  395. || (cofactor != NULL
  396. && !print_labeled_bignum(out, "Cofactor: ", cofactor))
  397. || (seed != NULL
  398. && !print_labeled_buf(out, "Seed:", seed, seed_len)))
  399. goto err;
  400. ret = 1;
  401. err:
  402. BN_CTX_end(ctx);
  403. BN_CTX_free(ctx);
  404. return ret;
  405. }
  406. static int ec_param_to_text(BIO *out, const EC_GROUP *group,
  407. OSSL_LIB_CTX *libctx)
  408. {
  409. if (EC_GROUP_get_asn1_flag(group) & OPENSSL_EC_NAMED_CURVE) {
  410. const char *curve_name;
  411. int curve_nid = EC_GROUP_get_curve_name(group);
  412. /* Explicit parameters */
  413. if (curve_nid == NID_undef)
  414. return 0;
  415. if (BIO_printf(out, "%s: %s\n", "ASN1 OID", OBJ_nid2sn(curve_nid)) <= 0)
  416. return 0;
  417. curve_name = EC_curve_nid2nist(curve_nid);
  418. return (curve_name == NULL
  419. || BIO_printf(out, "%s: %s\n", "NIST CURVE", curve_name) > 0);
  420. } else {
  421. return ec_param_explicit_to_text(out, group, libctx);
  422. }
  423. }
  424. static int ec_to_text(BIO *out, const void *key, int selection)
  425. {
  426. const EC_KEY *ec = key;
  427. const char *type_label = NULL;
  428. unsigned char *priv = NULL, *pub = NULL;
  429. size_t priv_len = 0, pub_len = 0;
  430. const EC_GROUP *group;
  431. int ret = 0;
  432. if (out == NULL || ec == NULL) {
  433. ERR_raise(ERR_LIB_PROV, ERR_R_PASSED_NULL_PARAMETER);
  434. return 0;
  435. }
  436. if ((group = EC_KEY_get0_group(ec)) == NULL) {
  437. ERR_raise(ERR_LIB_PROV, PROV_R_INVALID_KEY);
  438. return 0;
  439. }
  440. if ((selection & OSSL_KEYMGMT_SELECT_PRIVATE_KEY) != 0)
  441. type_label = "Private-Key";
  442. else if ((selection & OSSL_KEYMGMT_SELECT_PUBLIC_KEY) != 0)
  443. type_label = "Public-Key";
  444. else if ((selection & OSSL_KEYMGMT_SELECT_DOMAIN_PARAMETERS) != 0)
  445. if (EC_GROUP_get_curve_name(group) != NID_sm2)
  446. type_label = "EC-Parameters";
  447. if ((selection & OSSL_KEYMGMT_SELECT_PRIVATE_KEY) != 0) {
  448. const BIGNUM *priv_key = EC_KEY_get0_private_key(ec);
  449. if (priv_key == NULL) {
  450. ERR_raise(ERR_LIB_PROV, PROV_R_NOT_A_PRIVATE_KEY);
  451. goto err;
  452. }
  453. priv_len = EC_KEY_priv2buf(ec, &priv);
  454. if (priv_len == 0)
  455. goto err;
  456. }
  457. if ((selection & OSSL_KEYMGMT_SELECT_KEYPAIR) != 0) {
  458. const EC_POINT *pub_pt = EC_KEY_get0_public_key(ec);
  459. if (pub_pt == NULL) {
  460. ERR_raise(ERR_LIB_PROV, PROV_R_NOT_A_PUBLIC_KEY);
  461. goto err;
  462. }
  463. pub_len = EC_KEY_key2buf(ec, EC_KEY_get_conv_form(ec), &pub, NULL);
  464. if (pub_len == 0)
  465. goto err;
  466. }
  467. if (type_label != NULL
  468. && BIO_printf(out, "%s: (%d bit)\n", type_label,
  469. EC_GROUP_order_bits(group)) <= 0)
  470. goto err;
  471. if (priv != NULL
  472. && !print_labeled_buf(out, "priv:", priv, priv_len))
  473. goto err;
  474. if (pub != NULL
  475. && !print_labeled_buf(out, "pub:", pub, pub_len))
  476. goto err;
  477. if ((selection & OSSL_KEYMGMT_SELECT_DOMAIN_PARAMETERS) != 0)
  478. ret = ec_param_to_text(out, group, ossl_ec_key_get_libctx(ec));
  479. err:
  480. OPENSSL_clear_free(priv, priv_len);
  481. OPENSSL_free(pub);
  482. return ret;
  483. }
  484. # define ec_input_type "EC"
  485. # ifndef OPENSSL_NO_SM2
  486. # define sm2_input_type "SM2"
  487. # endif
  488. #endif
  489. /* ---------------------------------------------------------------------- */
  490. #ifndef OPENSSL_NO_ECX
  491. static int ecx_to_text(BIO *out, const void *key, int selection)
  492. {
  493. const ECX_KEY *ecx = key;
  494. const char *type_label = NULL;
  495. if (out == NULL || ecx == NULL) {
  496. ERR_raise(ERR_LIB_PROV, ERR_R_PASSED_NULL_PARAMETER);
  497. return 0;
  498. }
  499. switch (ecx->type) {
  500. case ECX_KEY_TYPE_X25519:
  501. type_label = "X25519";
  502. break;
  503. case ECX_KEY_TYPE_X448:
  504. type_label = "X448";
  505. break;
  506. case ECX_KEY_TYPE_ED25519:
  507. type_label = "ED25519";
  508. break;
  509. case ECX_KEY_TYPE_ED448:
  510. type_label = "ED448";
  511. break;
  512. }
  513. if ((selection & OSSL_KEYMGMT_SELECT_PRIVATE_KEY) != 0) {
  514. if (ecx->privkey == NULL) {
  515. ERR_raise(ERR_LIB_PROV, PROV_R_NOT_A_PRIVATE_KEY);
  516. return 0;
  517. }
  518. if (BIO_printf(out, "%s Private-Key:\n", type_label) <= 0)
  519. return 0;
  520. if (!print_labeled_buf(out, "priv:", ecx->privkey, ecx->keylen))
  521. return 0;
  522. } else if ((selection & OSSL_KEYMGMT_SELECT_PUBLIC_KEY) != 0) {
  523. /* ecx->pubkey is an array, not a pointer... */
  524. if (!ecx->haspubkey) {
  525. ERR_raise(ERR_LIB_PROV, PROV_R_NOT_A_PUBLIC_KEY);
  526. return 0;
  527. }
  528. if (BIO_printf(out, "%s Public-Key:\n", type_label) <= 0)
  529. return 0;
  530. }
  531. if (!print_labeled_buf(out, "pub:", ecx->pubkey, ecx->keylen))
  532. return 0;
  533. return 1;
  534. }
  535. # define ed25519_input_type "ED25519"
  536. # define ed448_input_type "ED448"
  537. # define x25519_input_type "X25519"
  538. # define x448_input_type "X448"
  539. #endif
  540. /* ---------------------------------------------------------------------- */
  541. static int rsa_to_text(BIO *out, const void *key, int selection)
  542. {
  543. const RSA *rsa = key;
  544. const char *type_label = "RSA key";
  545. const char *modulus_label = NULL;
  546. const char *exponent_label = NULL;
  547. const BIGNUM *rsa_d = NULL, *rsa_n = NULL, *rsa_e = NULL;
  548. STACK_OF(BIGNUM_const) *factors = NULL;
  549. STACK_OF(BIGNUM_const) *exps = NULL;
  550. STACK_OF(BIGNUM_const) *coeffs = NULL;
  551. int primes;
  552. const RSA_PSS_PARAMS_30 *pss_params = ossl_rsa_get0_pss_params_30((RSA *)rsa);
  553. int ret = 0;
  554. if (out == NULL || rsa == NULL) {
  555. ERR_raise(ERR_LIB_PROV, ERR_R_PASSED_NULL_PARAMETER);
  556. goto err;
  557. }
  558. factors = sk_BIGNUM_const_new_null();
  559. exps = sk_BIGNUM_const_new_null();
  560. coeffs = sk_BIGNUM_const_new_null();
  561. if (factors == NULL || exps == NULL || coeffs == NULL) {
  562. ERR_raise(ERR_LIB_PROV, ERR_R_CRYPTO_LIB);
  563. goto err;
  564. }
  565. if ((selection & OSSL_KEYMGMT_SELECT_PRIVATE_KEY) != 0) {
  566. type_label = "Private-Key";
  567. modulus_label = "modulus:";
  568. exponent_label = "publicExponent:";
  569. } else if ((selection & OSSL_KEYMGMT_SELECT_PUBLIC_KEY) != 0) {
  570. type_label = "Public-Key";
  571. modulus_label = "Modulus:";
  572. exponent_label = "Exponent:";
  573. }
  574. RSA_get0_key(rsa, &rsa_n, &rsa_e, &rsa_d);
  575. ossl_rsa_get0_all_params((RSA *)rsa, factors, exps, coeffs);
  576. primes = sk_BIGNUM_const_num(factors);
  577. if ((selection & OSSL_KEYMGMT_SELECT_PRIVATE_KEY) != 0) {
  578. if (BIO_printf(out, "%s: (%d bit, %d primes)\n",
  579. type_label, BN_num_bits(rsa_n), primes) <= 0)
  580. goto err;
  581. } else {
  582. if (BIO_printf(out, "%s: (%d bit)\n",
  583. type_label, BN_num_bits(rsa_n)) <= 0)
  584. goto err;
  585. }
  586. if (!print_labeled_bignum(out, modulus_label, rsa_n))
  587. goto err;
  588. if (!print_labeled_bignum(out, exponent_label, rsa_e))
  589. goto err;
  590. if ((selection & OSSL_KEYMGMT_SELECT_PRIVATE_KEY) != 0) {
  591. int i;
  592. if (!print_labeled_bignum(out, "privateExponent:", rsa_d))
  593. goto err;
  594. if (!print_labeled_bignum(out, "prime1:",
  595. sk_BIGNUM_const_value(factors, 0)))
  596. goto err;
  597. if (!print_labeled_bignum(out, "prime2:",
  598. sk_BIGNUM_const_value(factors, 1)))
  599. goto err;
  600. if (!print_labeled_bignum(out, "exponent1:",
  601. sk_BIGNUM_const_value(exps, 0)))
  602. goto err;
  603. if (!print_labeled_bignum(out, "exponent2:",
  604. sk_BIGNUM_const_value(exps, 1)))
  605. goto err;
  606. if (!print_labeled_bignum(out, "coefficient:",
  607. sk_BIGNUM_const_value(coeffs, 0)))
  608. goto err;
  609. for (i = 2; i < sk_BIGNUM_const_num(factors); i++) {
  610. if (BIO_printf(out, "prime%d:", i + 1) <= 0)
  611. goto err;
  612. if (!print_labeled_bignum(out, NULL,
  613. sk_BIGNUM_const_value(factors, i)))
  614. goto err;
  615. if (BIO_printf(out, "exponent%d:", i + 1) <= 0)
  616. goto err;
  617. if (!print_labeled_bignum(out, NULL,
  618. sk_BIGNUM_const_value(exps, i)))
  619. goto err;
  620. if (BIO_printf(out, "coefficient%d:", i + 1) <= 0)
  621. goto err;
  622. if (!print_labeled_bignum(out, NULL,
  623. sk_BIGNUM_const_value(coeffs, i - 1)))
  624. goto err;
  625. }
  626. }
  627. if ((selection & OSSL_KEYMGMT_SELECT_OTHER_PARAMETERS) != 0) {
  628. switch (RSA_test_flags(rsa, RSA_FLAG_TYPE_MASK)) {
  629. case RSA_FLAG_TYPE_RSA:
  630. if (!ossl_rsa_pss_params_30_is_unrestricted(pss_params)) {
  631. if (BIO_printf(out, "(INVALID PSS PARAMETERS)\n") <= 0)
  632. goto err;
  633. }
  634. break;
  635. case RSA_FLAG_TYPE_RSASSAPSS:
  636. if (ossl_rsa_pss_params_30_is_unrestricted(pss_params)) {
  637. if (BIO_printf(out, "No PSS parameter restrictions\n") <= 0)
  638. goto err;
  639. } else {
  640. int hashalg_nid = ossl_rsa_pss_params_30_hashalg(pss_params);
  641. int maskgenalg_nid =
  642. ossl_rsa_pss_params_30_maskgenalg(pss_params);
  643. int maskgenhashalg_nid =
  644. ossl_rsa_pss_params_30_maskgenhashalg(pss_params);
  645. int saltlen = ossl_rsa_pss_params_30_saltlen(pss_params);
  646. int trailerfield =
  647. ossl_rsa_pss_params_30_trailerfield(pss_params);
  648. if (BIO_printf(out, "PSS parameter restrictions:\n") <= 0)
  649. goto err;
  650. if (BIO_printf(out, " Hash Algorithm: %s%s\n",
  651. ossl_rsa_oaeppss_nid2name(hashalg_nid),
  652. (hashalg_nid == NID_sha1
  653. ? " (default)" : "")) <= 0)
  654. goto err;
  655. if (BIO_printf(out, " Mask Algorithm: %s with %s%s\n",
  656. ossl_rsa_mgf_nid2name(maskgenalg_nid),
  657. ossl_rsa_oaeppss_nid2name(maskgenhashalg_nid),
  658. (maskgenalg_nid == NID_mgf1
  659. && maskgenhashalg_nid == NID_sha1
  660. ? " (default)" : "")) <= 0)
  661. goto err;
  662. if (BIO_printf(out, " Minimum Salt Length: %d%s\n",
  663. saltlen,
  664. (saltlen == 20 ? " (default)" : "")) <= 0)
  665. goto err;
  666. if (BIO_printf(out, " Trailer Field: 0x%x%s\n",
  667. trailerfield,
  668. (trailerfield == 1 ? " (default)" : "")) <= 0)
  669. goto err;
  670. }
  671. break;
  672. }
  673. }
  674. ret = 1;
  675. err:
  676. sk_BIGNUM_const_free(factors);
  677. sk_BIGNUM_const_free(exps);
  678. sk_BIGNUM_const_free(coeffs);
  679. return ret;
  680. }
  681. #define rsa_input_type "RSA"
  682. #define rsapss_input_type "RSA-PSS"
  683. /* ---------------------------------------------------------------------- */
  684. static void *key2text_newctx(void *provctx)
  685. {
  686. return provctx;
  687. }
  688. static void key2text_freectx(ossl_unused void *vctx)
  689. {
  690. }
  691. static int key2text_encode(void *vctx, const void *key, int selection,
  692. OSSL_CORE_BIO *cout,
  693. int (*key2text)(BIO *out, const void *key,
  694. int selection),
  695. OSSL_PASSPHRASE_CALLBACK *cb, void *cbarg)
  696. {
  697. BIO *out = ossl_bio_new_from_core_bio(vctx, cout);
  698. int ret;
  699. if (out == NULL)
  700. return 0;
  701. ret = key2text(out, key, selection);
  702. BIO_free(out);
  703. return ret;
  704. }
  705. #define MAKE_TEXT_ENCODER(impl, type) \
  706. static OSSL_FUNC_encoder_import_object_fn \
  707. impl##2text_import_object; \
  708. static OSSL_FUNC_encoder_free_object_fn \
  709. impl##2text_free_object; \
  710. static OSSL_FUNC_encoder_encode_fn impl##2text_encode; \
  711. \
  712. static void *impl##2text_import_object(void *ctx, int selection, \
  713. const OSSL_PARAM params[]) \
  714. { \
  715. return ossl_prov_import_key(ossl_##impl##_keymgmt_functions, \
  716. ctx, selection, params); \
  717. } \
  718. static void impl##2text_free_object(void *key) \
  719. { \
  720. ossl_prov_free_key(ossl_##impl##_keymgmt_functions, key); \
  721. } \
  722. static int impl##2text_encode(void *vctx, OSSL_CORE_BIO *cout, \
  723. const void *key, \
  724. const OSSL_PARAM key_abstract[], \
  725. int selection, \
  726. OSSL_PASSPHRASE_CALLBACK *cb, \
  727. void *cbarg) \
  728. { \
  729. /* We don't deal with abstract objects */ \
  730. if (key_abstract != NULL) { \
  731. ERR_raise(ERR_LIB_PROV, ERR_R_PASSED_INVALID_ARGUMENT); \
  732. return 0; \
  733. } \
  734. return key2text_encode(vctx, key, selection, cout, \
  735. type##_to_text, cb, cbarg); \
  736. } \
  737. const OSSL_DISPATCH ossl_##impl##_to_text_encoder_functions[] = { \
  738. { OSSL_FUNC_ENCODER_NEWCTX, \
  739. (void (*)(void))key2text_newctx }, \
  740. { OSSL_FUNC_ENCODER_FREECTX, \
  741. (void (*)(void))key2text_freectx }, \
  742. { OSSL_FUNC_ENCODER_IMPORT_OBJECT, \
  743. (void (*)(void))impl##2text_import_object }, \
  744. { OSSL_FUNC_ENCODER_FREE_OBJECT, \
  745. (void (*)(void))impl##2text_free_object }, \
  746. { OSSL_FUNC_ENCODER_ENCODE, \
  747. (void (*)(void))impl##2text_encode }, \
  748. OSSL_DISPATCH_END \
  749. }
  750. #ifndef OPENSSL_NO_DH
  751. MAKE_TEXT_ENCODER(dh, dh);
  752. MAKE_TEXT_ENCODER(dhx, dh);
  753. #endif
  754. #ifndef OPENSSL_NO_DSA
  755. MAKE_TEXT_ENCODER(dsa, dsa);
  756. #endif
  757. #ifndef OPENSSL_NO_EC
  758. MAKE_TEXT_ENCODER(ec, ec);
  759. # ifndef OPENSSL_NO_SM2
  760. MAKE_TEXT_ENCODER(sm2, ec);
  761. # endif
  762. # ifndef OPENSSL_NO_ECX
  763. MAKE_TEXT_ENCODER(ed25519, ecx);
  764. MAKE_TEXT_ENCODER(ed448, ecx);
  765. MAKE_TEXT_ENCODER(x25519, ecx);
  766. MAKE_TEXT_ENCODER(x448, ecx);
  767. # endif
  768. #endif
  769. MAKE_TEXT_ENCODER(rsa, rsa);
  770. MAKE_TEXT_ENCODER(rsapss, rsa);