You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
 
 
 
 
 
 

528 lines
14 KiB

  1. /* ====================================================================
  2. * Copyright (c) 2006 The OpenSSL Project. All rights reserved.
  3. *
  4. * Redistribution and use in source and binary forms, with or without
  5. * modification, are permitted provided that the following conditions
  6. * are met:
  7. *
  8. * 1. Redistributions of source code must retain the above copyright
  9. * notice, this list of conditions and the following disclaimer.
  10. *
  11. * 2. Redistributions in binary form must reproduce the above copyright
  12. * notice, this list of conditions and the following disclaimer in
  13. * the documentation and/or other materials provided with the
  14. * distribution.
  15. *
  16. * 3. All advertising materials mentioning features or use of this
  17. * software must display the following acknowledgment:
  18. * "This product includes software developed by the OpenSSL Project
  19. * for use in the OpenSSL Toolkit. (http://www.OpenSSL.org/)"
  20. *
  21. * 4. The names "OpenSSL Toolkit" and "OpenSSL Project" must not be used to
  22. * endorse or promote products derived from this software without
  23. * prior written permission. For written permission, please contact
  24. * licensing@OpenSSL.org.
  25. *
  26. * 5. Products derived from this software may not be called "OpenSSL"
  27. * nor may "OpenSSL" appear in their names without prior written
  28. * permission of the OpenSSL Project.
  29. *
  30. * 6. Redistributions of any form whatsoever must retain the following
  31. * acknowledgment:
  32. * "This product includes software developed by the OpenSSL Project
  33. * for use in the OpenSSL Toolkit (http://www.OpenSSL.org/)"
  34. *
  35. * THIS SOFTWARE IS PROVIDED BY THE OpenSSL PROJECT ``AS IS'' AND ANY
  36. * EXPRESSED OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
  37. * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
  38. * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE OpenSSL PROJECT OR
  39. * ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
  40. * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
  41. * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
  42. * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
  43. * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
  44. * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
  45. * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED
  46. * OF THE POSSIBILITY OF SUCH DAMAGE.
  47. * ====================================================================
  48. *
  49. * This product includes cryptographic software written by Eric Young
  50. * (eay@cryptsoft.com). This product includes software written by Tim
  51. * Hudson (tjh@cryptsoft.com). */
  52. #include <openssl/evp.h>
  53. #include <openssl/bio.h>
  54. #include <openssl/bn.h>
  55. #include <openssl/dsa.h>
  56. #include <openssl/ec.h>
  57. #include <openssl/ec_key.h>
  58. #include <openssl/mem.h>
  59. #include <openssl/rsa.h>
  60. #include "../rsa/internal.h"
  61. static int bn_print(BIO *bp, const char *number, const BIGNUM *num,
  62. uint8_t *buf, int off) {
  63. if (num == NULL) {
  64. return 1;
  65. }
  66. if (!BIO_indent(bp, off, 128)) {
  67. return 0;
  68. }
  69. if (BN_is_zero(num)) {
  70. if (BIO_printf(bp, "%s 0\n", number) <= 0) {
  71. return 0;
  72. }
  73. return 1;
  74. }
  75. if (BN_num_bytes(num) <= sizeof(long)) {
  76. const char *neg = BN_is_negative(num) ? "-" : "";
  77. if (BIO_printf(bp, "%s %s%lu (%s0x%lx)\n", number, neg,
  78. (unsigned long)num->d[0], neg,
  79. (unsigned long)num->d[0]) <= 0) {
  80. return 0;
  81. }
  82. } else {
  83. buf[0] = 0;
  84. if (BIO_printf(bp, "%s%s", number,
  85. (BN_is_negative(num)) ? " (Negative)" : "") <= 0) {
  86. return 0;
  87. }
  88. int n = BN_bn2bin(num, &buf[1]);
  89. if (buf[1] & 0x80) {
  90. n++;
  91. } else {
  92. buf++;
  93. }
  94. int i;
  95. for (i = 0; i < n; i++) {
  96. if ((i % 15) == 0) {
  97. if (BIO_puts(bp, "\n") <= 0 ||
  98. !BIO_indent(bp, off + 4, 128)) {
  99. return 0;
  100. }
  101. }
  102. if (BIO_printf(bp, "%02x%s", buf[i], ((i + 1) == n) ? "" : ":") <= 0) {
  103. return 0;
  104. }
  105. }
  106. if (BIO_write(bp, "\n", 1) <= 0) {
  107. return 0;
  108. }
  109. }
  110. return 1;
  111. }
  112. static void update_buflen(const BIGNUM *b, size_t *pbuflen) {
  113. size_t i;
  114. if (!b) {
  115. return;
  116. }
  117. i = BN_num_bytes(b);
  118. if (*pbuflen < i) {
  119. *pbuflen = i;
  120. }
  121. }
  122. /* RSA keys. */
  123. static int do_rsa_print(BIO *out, const RSA *rsa, int off,
  124. int include_private) {
  125. const char *s, *str;
  126. uint8_t *m = NULL;
  127. int ret = 0, mod_len = 0;
  128. size_t buf_len = 0;
  129. update_buflen(rsa->n, &buf_len);
  130. update_buflen(rsa->e, &buf_len);
  131. if (include_private) {
  132. update_buflen(rsa->d, &buf_len);
  133. update_buflen(rsa->p, &buf_len);
  134. update_buflen(rsa->q, &buf_len);
  135. update_buflen(rsa->dmp1, &buf_len);
  136. update_buflen(rsa->dmq1, &buf_len);
  137. update_buflen(rsa->iqmp, &buf_len);
  138. if (rsa->additional_primes != NULL) {
  139. size_t i;
  140. for (i = 0; i < sk_RSA_additional_prime_num(rsa->additional_primes);
  141. i++) {
  142. const RSA_additional_prime *ap =
  143. sk_RSA_additional_prime_value(rsa->additional_primes, i);
  144. update_buflen(ap->prime, &buf_len);
  145. update_buflen(ap->exp, &buf_len);
  146. update_buflen(ap->coeff, &buf_len);
  147. }
  148. }
  149. }
  150. m = (uint8_t *)OPENSSL_malloc(buf_len + 10);
  151. if (m == NULL) {
  152. OPENSSL_PUT_ERROR(EVP, ERR_R_MALLOC_FAILURE);
  153. goto err;
  154. }
  155. if (rsa->n != NULL) {
  156. mod_len = BN_num_bits(rsa->n);
  157. }
  158. if (!BIO_indent(out, off, 128)) {
  159. goto err;
  160. }
  161. if (include_private && rsa->d) {
  162. if (BIO_printf(out, "Private-Key: (%d bit)\n", mod_len) <= 0) {
  163. goto err;
  164. }
  165. str = "modulus:";
  166. s = "publicExponent:";
  167. } else {
  168. if (BIO_printf(out, "Public-Key: (%d bit)\n", mod_len) <= 0) {
  169. goto err;
  170. }
  171. str = "Modulus:";
  172. s = "Exponent:";
  173. }
  174. if (!bn_print(out, str, rsa->n, m, off) ||
  175. !bn_print(out, s, rsa->e, m, off)) {
  176. goto err;
  177. }
  178. if (include_private) {
  179. if (!bn_print(out, "privateExponent:", rsa->d, m, off) ||
  180. !bn_print(out, "prime1:", rsa->p, m, off) ||
  181. !bn_print(out, "prime2:", rsa->q, m, off) ||
  182. !bn_print(out, "exponent1:", rsa->dmp1, m, off) ||
  183. !bn_print(out, "exponent2:", rsa->dmq1, m, off) ||
  184. !bn_print(out, "coefficient:", rsa->iqmp, m, off)) {
  185. goto err;
  186. }
  187. if (rsa->additional_primes != NULL &&
  188. sk_RSA_additional_prime_num(rsa->additional_primes) > 0) {
  189. size_t i;
  190. if (BIO_printf(out, "otherPrimeInfos:\n") <= 0) {
  191. goto err;
  192. }
  193. for (i = 0; i < sk_RSA_additional_prime_num(rsa->additional_primes);
  194. i++) {
  195. const RSA_additional_prime *ap =
  196. sk_RSA_additional_prime_value(rsa->additional_primes, i);
  197. if (BIO_printf(out, "otherPrimeInfo (prime %u):\n",
  198. (unsigned)(i + 3)) <= 0 ||
  199. !bn_print(out, "prime:", ap->prime, m, off) ||
  200. !bn_print(out, "exponent:", ap->exp, m, off) ||
  201. !bn_print(out, "coeff:", ap->coeff, m, off)) {
  202. goto err;
  203. }
  204. }
  205. }
  206. }
  207. ret = 1;
  208. err:
  209. OPENSSL_free(m);
  210. return ret;
  211. }
  212. static int rsa_pub_print(BIO *bp, const EVP_PKEY *pkey, int indent,
  213. ASN1_PCTX *ctx) {
  214. return do_rsa_print(bp, pkey->pkey.rsa, indent, 0);
  215. }
  216. static int rsa_priv_print(BIO *bp, const EVP_PKEY *pkey, int indent,
  217. ASN1_PCTX *ctx) {
  218. return do_rsa_print(bp, pkey->pkey.rsa, indent, 1);
  219. }
  220. /* DSA keys. */
  221. static int do_dsa_print(BIO *bp, const DSA *x, int off, int ptype) {
  222. uint8_t *m = NULL;
  223. int ret = 0;
  224. size_t buf_len = 0;
  225. const char *ktype = NULL;
  226. const BIGNUM *priv_key, *pub_key;
  227. priv_key = NULL;
  228. if (ptype == 2) {
  229. priv_key = x->priv_key;
  230. }
  231. pub_key = NULL;
  232. if (ptype > 0) {
  233. pub_key = x->pub_key;
  234. }
  235. ktype = "DSA-Parameters";
  236. if (ptype == 2) {
  237. ktype = "Private-Key";
  238. } else if (ptype == 1) {
  239. ktype = "Public-Key";
  240. }
  241. update_buflen(x->p, &buf_len);
  242. update_buflen(x->q, &buf_len);
  243. update_buflen(x->g, &buf_len);
  244. update_buflen(priv_key, &buf_len);
  245. update_buflen(pub_key, &buf_len);
  246. m = (uint8_t *)OPENSSL_malloc(buf_len + 10);
  247. if (m == NULL) {
  248. OPENSSL_PUT_ERROR(EVP, ERR_R_MALLOC_FAILURE);
  249. goto err;
  250. }
  251. if (priv_key) {
  252. if (!BIO_indent(bp, off, 128) ||
  253. BIO_printf(bp, "%s: (%d bit)\n", ktype, BN_num_bits(x->p)) <= 0) {
  254. goto err;
  255. }
  256. }
  257. if (!bn_print(bp, "priv:", priv_key, m, off) ||
  258. !bn_print(bp, "pub: ", pub_key, m, off) ||
  259. !bn_print(bp, "P: ", x->p, m, off) ||
  260. !bn_print(bp, "Q: ", x->q, m, off) ||
  261. !bn_print(bp, "G: ", x->g, m, off)) {
  262. goto err;
  263. }
  264. ret = 1;
  265. err:
  266. OPENSSL_free(m);
  267. return ret;
  268. }
  269. static int dsa_param_print(BIO *bp, const EVP_PKEY *pkey, int indent,
  270. ASN1_PCTX *ctx) {
  271. return do_dsa_print(bp, pkey->pkey.dsa, indent, 0);
  272. }
  273. static int dsa_pub_print(BIO *bp, const EVP_PKEY *pkey, int indent,
  274. ASN1_PCTX *ctx) {
  275. return do_dsa_print(bp, pkey->pkey.dsa, indent, 1);
  276. }
  277. static int dsa_priv_print(BIO *bp, const EVP_PKEY *pkey, int indent,
  278. ASN1_PCTX *ctx) {
  279. return do_dsa_print(bp, pkey->pkey.dsa, indent, 2);
  280. }
  281. /* EC keys. */
  282. static int do_EC_KEY_print(BIO *bp, const EC_KEY *x, int off, int ktype) {
  283. uint8_t *buffer = NULL;
  284. const char *ecstr;
  285. size_t buf_len = 0, i;
  286. int ret = 0, reason = ERR_R_BIO_LIB;
  287. BIGNUM *order = NULL;
  288. BN_CTX *ctx = NULL;
  289. const EC_GROUP *group;
  290. const EC_POINT *public_key;
  291. const BIGNUM *priv_key;
  292. uint8_t *pub_key_bytes = NULL;
  293. size_t pub_key_bytes_len = 0;
  294. if (x == NULL || (group = EC_KEY_get0_group(x)) == NULL) {
  295. reason = ERR_R_PASSED_NULL_PARAMETER;
  296. goto err;
  297. }
  298. ctx = BN_CTX_new();
  299. if (ctx == NULL) {
  300. reason = ERR_R_MALLOC_FAILURE;
  301. goto err;
  302. }
  303. if (ktype > 0) {
  304. public_key = EC_KEY_get0_public_key(x);
  305. if (public_key != NULL) {
  306. pub_key_bytes_len = EC_POINT_point2oct(
  307. group, public_key, EC_KEY_get_conv_form(x), NULL, 0, ctx);
  308. if (pub_key_bytes_len == 0) {
  309. reason = ERR_R_MALLOC_FAILURE;
  310. goto err;
  311. }
  312. pub_key_bytes = OPENSSL_malloc(pub_key_bytes_len);
  313. if (pub_key_bytes == NULL) {
  314. reason = ERR_R_MALLOC_FAILURE;
  315. goto err;
  316. }
  317. pub_key_bytes_len =
  318. EC_POINT_point2oct(group, public_key, EC_KEY_get_conv_form(x),
  319. pub_key_bytes, pub_key_bytes_len, ctx);
  320. if (pub_key_bytes_len == 0) {
  321. reason = ERR_R_MALLOC_FAILURE;
  322. goto err;
  323. }
  324. buf_len = pub_key_bytes_len;
  325. }
  326. }
  327. if (ktype == 2) {
  328. priv_key = EC_KEY_get0_private_key(x);
  329. if (priv_key && (i = (size_t)BN_num_bytes(priv_key)) > buf_len) {
  330. buf_len = i;
  331. }
  332. } else {
  333. priv_key = NULL;
  334. }
  335. if (ktype > 0) {
  336. buf_len += 10;
  337. if ((buffer = OPENSSL_malloc(buf_len)) == NULL) {
  338. reason = ERR_R_MALLOC_FAILURE;
  339. goto err;
  340. }
  341. }
  342. if (ktype == 2) {
  343. ecstr = "Private-Key";
  344. } else if (ktype == 1) {
  345. ecstr = "Public-Key";
  346. } else {
  347. ecstr = "ECDSA-Parameters";
  348. }
  349. if (!BIO_indent(bp, off, 128)) {
  350. goto err;
  351. }
  352. order = BN_new();
  353. if (order == NULL || !EC_GROUP_get_order(group, order, NULL) ||
  354. BIO_printf(bp, "%s: (%d bit)\n", ecstr, BN_num_bits(order)) <= 0) {
  355. goto err;
  356. }
  357. if ((priv_key != NULL) &&
  358. !bn_print(bp, "priv:", priv_key, buffer, off)) {
  359. goto err;
  360. }
  361. if (pub_key_bytes != NULL) {
  362. BIO_hexdump(bp, pub_key_bytes, pub_key_bytes_len, off);
  363. }
  364. /* TODO(fork): implement */
  365. /*
  366. if (!ECPKParameters_print(bp, group, off))
  367. goto err; */
  368. ret = 1;
  369. err:
  370. if (!ret) {
  371. OPENSSL_PUT_ERROR(EVP, reason);
  372. }
  373. OPENSSL_free(pub_key_bytes);
  374. BN_free(order);
  375. BN_CTX_free(ctx);
  376. OPENSSL_free(buffer);
  377. return ret;
  378. }
  379. static int eckey_param_print(BIO *bp, const EVP_PKEY *pkey, int indent,
  380. ASN1_PCTX *ctx) {
  381. return do_EC_KEY_print(bp, pkey->pkey.ec, indent, 0);
  382. }
  383. static int eckey_pub_print(BIO *bp, const EVP_PKEY *pkey, int indent,
  384. ASN1_PCTX *ctx) {
  385. return do_EC_KEY_print(bp, pkey->pkey.ec, indent, 1);
  386. }
  387. static int eckey_priv_print(BIO *bp, const EVP_PKEY *pkey, int indent,
  388. ASN1_PCTX *ctx) {
  389. return do_EC_KEY_print(bp, pkey->pkey.ec, indent, 2);
  390. }
  391. typedef struct {
  392. int type;
  393. int (*pub_print)(BIO *out, const EVP_PKEY *pkey, int indent, ASN1_PCTX *pctx);
  394. int (*priv_print)(BIO *out, const EVP_PKEY *pkey, int indent,
  395. ASN1_PCTX *pctx);
  396. int (*param_print)(BIO *out, const EVP_PKEY *pkey, int indent,
  397. ASN1_PCTX *pctx);
  398. } EVP_PKEY_PRINT_METHOD;
  399. static EVP_PKEY_PRINT_METHOD kPrintMethods[] = {
  400. {
  401. EVP_PKEY_RSA,
  402. rsa_pub_print,
  403. rsa_priv_print,
  404. NULL /* param_print */,
  405. },
  406. {
  407. EVP_PKEY_DSA,
  408. dsa_pub_print,
  409. dsa_priv_print,
  410. dsa_param_print,
  411. },
  412. {
  413. EVP_PKEY_EC,
  414. eckey_pub_print,
  415. eckey_priv_print,
  416. eckey_param_print,
  417. },
  418. };
  419. static size_t kPrintMethodsLen =
  420. sizeof(kPrintMethods) / sizeof(kPrintMethods[0]);
  421. static EVP_PKEY_PRINT_METHOD *find_method(int type) {
  422. size_t i;
  423. for (i = 0; i < kPrintMethodsLen; i++) {
  424. if (kPrintMethods[i].type == type) {
  425. return &kPrintMethods[i];
  426. }
  427. }
  428. return NULL;
  429. }
  430. static int print_unsupported(BIO *out, const EVP_PKEY *pkey, int indent,
  431. const char *kstr) {
  432. BIO_indent(out, indent, 128);
  433. BIO_printf(out, "%s algorithm unsupported\n", kstr);
  434. return 1;
  435. }
  436. int EVP_PKEY_print_public(BIO *out, const EVP_PKEY *pkey, int indent,
  437. ASN1_PCTX *pctx) {
  438. EVP_PKEY_PRINT_METHOD *method = find_method(pkey->type);
  439. if (method != NULL && method->pub_print != NULL) {
  440. return method->pub_print(out, pkey, indent, pctx);
  441. }
  442. return print_unsupported(out, pkey, indent, "Public Key");
  443. }
  444. int EVP_PKEY_print_private(BIO *out, const EVP_PKEY *pkey, int indent,
  445. ASN1_PCTX *pctx) {
  446. EVP_PKEY_PRINT_METHOD *method = find_method(pkey->type);
  447. if (method != NULL && method->priv_print != NULL) {
  448. return method->priv_print(out, pkey, indent, pctx);
  449. }
  450. return print_unsupported(out, pkey, indent, "Private Key");
  451. }
  452. int EVP_PKEY_print_params(BIO *out, const EVP_PKEY *pkey, int indent,
  453. ASN1_PCTX *pctx) {
  454. EVP_PKEY_PRINT_METHOD *method = find_method(pkey->type);
  455. if (method != NULL && method->param_print != NULL) {
  456. return method->param_print(out, pkey, indent, pctx);
  457. }
  458. return print_unsupported(out, pkey, indent, "Parameters");
  459. }