encode_key2text.c 29 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893
  1. /*
  2. * Copyright 2020-2022 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_PUBLIC_KEY) != 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_PUBLIC_KEY) != 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_PUBLIC_KEY) != 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. if ((selection & OSSL_KEYMGMT_SELECT_PRIVATE_KEY) != 0) {
  500. if (ecx->privkey == NULL) {
  501. ERR_raise(ERR_LIB_PROV, PROV_R_NOT_A_PRIVATE_KEY);
  502. return 0;
  503. }
  504. switch (ecx->type) {
  505. case ECX_KEY_TYPE_X25519:
  506. type_label = "X25519 Private-Key";
  507. break;
  508. case ECX_KEY_TYPE_X448:
  509. type_label = "X448 Private-Key";
  510. break;
  511. case ECX_KEY_TYPE_ED25519:
  512. type_label = "ED25519 Private-Key";
  513. break;
  514. case ECX_KEY_TYPE_ED448:
  515. type_label = "ED448 Private-Key";
  516. break;
  517. }
  518. } else if ((selection & OSSL_KEYMGMT_SELECT_PUBLIC_KEY) != 0) {
  519. /* ecx->pubkey is an array, not a pointer... */
  520. if (!ecx->haspubkey) {
  521. ERR_raise(ERR_LIB_PROV, PROV_R_NOT_A_PUBLIC_KEY);
  522. return 0;
  523. }
  524. switch (ecx->type) {
  525. case ECX_KEY_TYPE_X25519:
  526. type_label = "X25519 Public-Key";
  527. break;
  528. case ECX_KEY_TYPE_X448:
  529. type_label = "X448 Public-Key";
  530. break;
  531. case ECX_KEY_TYPE_ED25519:
  532. type_label = "ED25519 Public-Key";
  533. break;
  534. case ECX_KEY_TYPE_ED448:
  535. type_label = "ED448 Public-Key";
  536. break;
  537. }
  538. }
  539. if (BIO_printf(out, "%s:\n", type_label) <= 0)
  540. return 0;
  541. if ((selection & OSSL_KEYMGMT_SELECT_PRIVATE_KEY) != 0
  542. && !print_labeled_buf(out, "priv:", ecx->privkey, ecx->keylen))
  543. return 0;
  544. if ((selection & OSSL_KEYMGMT_SELECT_PUBLIC_KEY) != 0
  545. && !print_labeled_buf(out, "pub:", ecx->pubkey, ecx->keylen))
  546. return 0;
  547. return 1;
  548. }
  549. # define ed25519_input_type "ED25519"
  550. # define ed448_input_type "ED448"
  551. # define x25519_input_type "X25519"
  552. # define x448_input_type "X448"
  553. #endif
  554. /* ---------------------------------------------------------------------- */
  555. static int rsa_to_text(BIO *out, const void *key, int selection)
  556. {
  557. const RSA *rsa = key;
  558. const char *type_label = "RSA key";
  559. const char *modulus_label = NULL;
  560. const char *exponent_label = NULL;
  561. const BIGNUM *rsa_d = NULL, *rsa_n = NULL, *rsa_e = NULL;
  562. STACK_OF(BIGNUM_const) *factors = NULL;
  563. STACK_OF(BIGNUM_const) *exps = NULL;
  564. STACK_OF(BIGNUM_const) *coeffs = NULL;
  565. int primes;
  566. const RSA_PSS_PARAMS_30 *pss_params = ossl_rsa_get0_pss_params_30((RSA *)rsa);
  567. int ret = 0;
  568. if (out == NULL || rsa == NULL) {
  569. ERR_raise(ERR_LIB_PROV, ERR_R_PASSED_NULL_PARAMETER);
  570. goto err;
  571. }
  572. factors = sk_BIGNUM_const_new_null();
  573. exps = sk_BIGNUM_const_new_null();
  574. coeffs = sk_BIGNUM_const_new_null();
  575. if (factors == NULL || exps == NULL || coeffs == NULL) {
  576. ERR_raise(ERR_LIB_PROV, ERR_R_CRYPTO_LIB);
  577. goto err;
  578. }
  579. if ((selection & OSSL_KEYMGMT_SELECT_PRIVATE_KEY) != 0) {
  580. type_label = "Private-Key";
  581. modulus_label = "modulus:";
  582. exponent_label = "publicExponent:";
  583. } else if ((selection & OSSL_KEYMGMT_SELECT_PUBLIC_KEY) != 0) {
  584. type_label = "Public-Key";
  585. modulus_label = "Modulus:";
  586. exponent_label = "Exponent:";
  587. }
  588. RSA_get0_key(rsa, &rsa_n, &rsa_e, &rsa_d);
  589. ossl_rsa_get0_all_params((RSA *)rsa, factors, exps, coeffs);
  590. primes = sk_BIGNUM_const_num(factors);
  591. if ((selection & OSSL_KEYMGMT_SELECT_PRIVATE_KEY) != 0) {
  592. if (BIO_printf(out, "%s: (%d bit, %d primes)\n",
  593. type_label, BN_num_bits(rsa_n), primes) <= 0)
  594. goto err;
  595. } else {
  596. if (BIO_printf(out, "%s: (%d bit)\n",
  597. type_label, BN_num_bits(rsa_n)) <= 0)
  598. goto err;
  599. }
  600. if (!print_labeled_bignum(out, modulus_label, rsa_n))
  601. goto err;
  602. if (!print_labeled_bignum(out, exponent_label, rsa_e))
  603. goto err;
  604. if ((selection & OSSL_KEYMGMT_SELECT_PRIVATE_KEY) != 0) {
  605. int i;
  606. if (!print_labeled_bignum(out, "privateExponent:", rsa_d))
  607. goto err;
  608. if (!print_labeled_bignum(out, "prime1:",
  609. sk_BIGNUM_const_value(factors, 0)))
  610. goto err;
  611. if (!print_labeled_bignum(out, "prime2:",
  612. sk_BIGNUM_const_value(factors, 1)))
  613. goto err;
  614. if (!print_labeled_bignum(out, "exponent1:",
  615. sk_BIGNUM_const_value(exps, 0)))
  616. goto err;
  617. if (!print_labeled_bignum(out, "exponent2:",
  618. sk_BIGNUM_const_value(exps, 1)))
  619. goto err;
  620. if (!print_labeled_bignum(out, "coefficient:",
  621. sk_BIGNUM_const_value(coeffs, 0)))
  622. goto err;
  623. for (i = 2; i < sk_BIGNUM_const_num(factors); i++) {
  624. if (BIO_printf(out, "prime%d:", i + 1) <= 0)
  625. goto err;
  626. if (!print_labeled_bignum(out, NULL,
  627. sk_BIGNUM_const_value(factors, i)))
  628. goto err;
  629. if (BIO_printf(out, "exponent%d:", i + 1) <= 0)
  630. goto err;
  631. if (!print_labeled_bignum(out, NULL,
  632. sk_BIGNUM_const_value(exps, i)))
  633. goto err;
  634. if (BIO_printf(out, "coefficient%d:", i + 1) <= 0)
  635. goto err;
  636. if (!print_labeled_bignum(out, NULL,
  637. sk_BIGNUM_const_value(coeffs, i - 1)))
  638. goto err;
  639. }
  640. }
  641. if ((selection & OSSL_KEYMGMT_SELECT_OTHER_PARAMETERS) != 0) {
  642. switch (RSA_test_flags(rsa, RSA_FLAG_TYPE_MASK)) {
  643. case RSA_FLAG_TYPE_RSA:
  644. if (!ossl_rsa_pss_params_30_is_unrestricted(pss_params)) {
  645. if (BIO_printf(out, "(INVALID PSS PARAMETERS)\n") <= 0)
  646. goto err;
  647. }
  648. break;
  649. case RSA_FLAG_TYPE_RSASSAPSS:
  650. if (ossl_rsa_pss_params_30_is_unrestricted(pss_params)) {
  651. if (BIO_printf(out, "No PSS parameter restrictions\n") <= 0)
  652. goto err;
  653. } else {
  654. int hashalg_nid = ossl_rsa_pss_params_30_hashalg(pss_params);
  655. int maskgenalg_nid =
  656. ossl_rsa_pss_params_30_maskgenalg(pss_params);
  657. int maskgenhashalg_nid =
  658. ossl_rsa_pss_params_30_maskgenhashalg(pss_params);
  659. int saltlen = ossl_rsa_pss_params_30_saltlen(pss_params);
  660. int trailerfield =
  661. ossl_rsa_pss_params_30_trailerfield(pss_params);
  662. if (BIO_printf(out, "PSS parameter restrictions:\n") <= 0)
  663. goto err;
  664. if (BIO_printf(out, " Hash Algorithm: %s%s\n",
  665. ossl_rsa_oaeppss_nid2name(hashalg_nid),
  666. (hashalg_nid == NID_sha1
  667. ? " (default)" : "")) <= 0)
  668. goto err;
  669. if (BIO_printf(out, " Mask Algorithm: %s with %s%s\n",
  670. ossl_rsa_mgf_nid2name(maskgenalg_nid),
  671. ossl_rsa_oaeppss_nid2name(maskgenhashalg_nid),
  672. (maskgenalg_nid == NID_mgf1
  673. && maskgenhashalg_nid == NID_sha1
  674. ? " (default)" : "")) <= 0)
  675. goto err;
  676. if (BIO_printf(out, " Minimum Salt Length: %d%s\n",
  677. saltlen,
  678. (saltlen == 20 ? " (default)" : "")) <= 0)
  679. goto err;
  680. if (BIO_printf(out, " Trailer Field: 0x%x%s\n",
  681. trailerfield,
  682. (trailerfield == 1 ? " (default)" : "")) <= 0)
  683. goto err;
  684. }
  685. break;
  686. }
  687. }
  688. ret = 1;
  689. err:
  690. sk_BIGNUM_const_free(factors);
  691. sk_BIGNUM_const_free(exps);
  692. sk_BIGNUM_const_free(coeffs);
  693. return ret;
  694. }
  695. #define rsa_input_type "RSA"
  696. #define rsapss_input_type "RSA-PSS"
  697. /* ---------------------------------------------------------------------- */
  698. static void *key2text_newctx(void *provctx)
  699. {
  700. return provctx;
  701. }
  702. static void key2text_freectx(ossl_unused void *vctx)
  703. {
  704. }
  705. static int key2text_encode(void *vctx, const void *key, int selection,
  706. OSSL_CORE_BIO *cout,
  707. int (*key2text)(BIO *out, const void *key,
  708. int selection),
  709. OSSL_PASSPHRASE_CALLBACK *cb, void *cbarg)
  710. {
  711. BIO *out = ossl_bio_new_from_core_bio(vctx, cout);
  712. int ret;
  713. if (out == NULL)
  714. return 0;
  715. ret = key2text(out, key, selection);
  716. BIO_free(out);
  717. return ret;
  718. }
  719. #define MAKE_TEXT_ENCODER(impl, type) \
  720. static OSSL_FUNC_encoder_import_object_fn \
  721. impl##2text_import_object; \
  722. static OSSL_FUNC_encoder_free_object_fn \
  723. impl##2text_free_object; \
  724. static OSSL_FUNC_encoder_encode_fn impl##2text_encode; \
  725. \
  726. static void *impl##2text_import_object(void *ctx, int selection, \
  727. const OSSL_PARAM params[]) \
  728. { \
  729. return ossl_prov_import_key(ossl_##impl##_keymgmt_functions, \
  730. ctx, selection, params); \
  731. } \
  732. static void impl##2text_free_object(void *key) \
  733. { \
  734. ossl_prov_free_key(ossl_##impl##_keymgmt_functions, key); \
  735. } \
  736. static int impl##2text_encode(void *vctx, OSSL_CORE_BIO *cout, \
  737. const void *key, \
  738. const OSSL_PARAM key_abstract[], \
  739. int selection, \
  740. OSSL_PASSPHRASE_CALLBACK *cb, \
  741. void *cbarg) \
  742. { \
  743. /* We don't deal with abstract objects */ \
  744. if (key_abstract != NULL) { \
  745. ERR_raise(ERR_LIB_PROV, ERR_R_PASSED_INVALID_ARGUMENT); \
  746. return 0; \
  747. } \
  748. return key2text_encode(vctx, key, selection, cout, \
  749. type##_to_text, cb, cbarg); \
  750. } \
  751. const OSSL_DISPATCH ossl_##impl##_to_text_encoder_functions[] = { \
  752. { OSSL_FUNC_ENCODER_NEWCTX, \
  753. (void (*)(void))key2text_newctx }, \
  754. { OSSL_FUNC_ENCODER_FREECTX, \
  755. (void (*)(void))key2text_freectx }, \
  756. { OSSL_FUNC_ENCODER_IMPORT_OBJECT, \
  757. (void (*)(void))impl##2text_import_object }, \
  758. { OSSL_FUNC_ENCODER_FREE_OBJECT, \
  759. (void (*)(void))impl##2text_free_object }, \
  760. { OSSL_FUNC_ENCODER_ENCODE, \
  761. (void (*)(void))impl##2text_encode }, \
  762. OSSL_DISPATCH_END \
  763. }
  764. #ifndef OPENSSL_NO_DH
  765. MAKE_TEXT_ENCODER(dh, dh);
  766. MAKE_TEXT_ENCODER(dhx, dh);
  767. #endif
  768. #ifndef OPENSSL_NO_DSA
  769. MAKE_TEXT_ENCODER(dsa, dsa);
  770. #endif
  771. #ifndef OPENSSL_NO_EC
  772. MAKE_TEXT_ENCODER(ec, ec);
  773. # ifndef OPENSSL_NO_SM2
  774. MAKE_TEXT_ENCODER(sm2, ec);
  775. # endif
  776. # ifndef OPENSSL_NO_ECX
  777. MAKE_TEXT_ENCODER(ed25519, ecx);
  778. MAKE_TEXT_ENCODER(ed448, ecx);
  779. MAKE_TEXT_ENCODER(x25519, ecx);
  780. MAKE_TEXT_ENCODER(x448, ecx);
  781. # endif
  782. #endif
  783. MAKE_TEXT_ENCODER(rsa, rsa);
  784. MAKE_TEXT_ENCODER(rsapss, rsa);