encode_key2text.c 29 KB

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