encode_key2text.c 28 KB

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