選択できるのは25トピックまでです。 トピックは、先頭が英数字で、英数字とダッシュ('-')を使用した35文字以内のものにしてください。
 
 
 
 
 
 

521 行
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 "../internal.h"
  61. #include "../rsa/internal.h"
  62. static int bn_print(BIO *bp, const char *number, const BIGNUM *num,
  63. uint8_t *buf, int off) {
  64. if (num == NULL) {
  65. return 1;
  66. }
  67. if (!BIO_indent(bp, off, 128)) {
  68. return 0;
  69. }
  70. if (BN_is_zero(num)) {
  71. if (BIO_printf(bp, "%s 0\n", number) <= 0) {
  72. return 0;
  73. }
  74. return 1;
  75. }
  76. if (BN_num_bytes(num) <= sizeof(long)) {
  77. const char *neg = BN_is_negative(num) ? "-" : "";
  78. if (BIO_printf(bp, "%s %s%lu (%s0x%lx)\n", number, neg,
  79. (unsigned long)num->d[0], neg,
  80. (unsigned long)num->d[0]) <= 0) {
  81. return 0;
  82. }
  83. } else {
  84. buf[0] = 0;
  85. if (BIO_printf(bp, "%s%s", number,
  86. (BN_is_negative(num)) ? " (Negative)" : "") <= 0) {
  87. return 0;
  88. }
  89. int n = BN_bn2bin(num, &buf[1]);
  90. if (buf[1] & 0x80) {
  91. n++;
  92. } else {
  93. buf++;
  94. }
  95. int i;
  96. for (i = 0; i < n; i++) {
  97. if ((i % 15) == 0) {
  98. if (BIO_puts(bp, "\n") <= 0 ||
  99. !BIO_indent(bp, off + 4, 128)) {
  100. return 0;
  101. }
  102. }
  103. if (BIO_printf(bp, "%02x%s", buf[i], ((i + 1) == n) ? "" : ":") <= 0) {
  104. return 0;
  105. }
  106. }
  107. if (BIO_write(bp, "\n", 1) <= 0) {
  108. return 0;
  109. }
  110. }
  111. return 1;
  112. }
  113. static void update_buflen(const BIGNUM *b, size_t *pbuflen) {
  114. if (!b) {
  115. return;
  116. }
  117. size_t len = BN_num_bytes(b);
  118. if (*pbuflen < len) {
  119. *pbuflen = len;
  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. for (size_t i = 0;
  140. i < sk_RSA_additional_prime_num(rsa->additional_primes); i++) {
  141. const RSA_additional_prime *ap =
  142. sk_RSA_additional_prime_value(rsa->additional_primes, i);
  143. update_buflen(ap->prime, &buf_len);
  144. update_buflen(ap->exp, &buf_len);
  145. update_buflen(ap->coeff, &buf_len);
  146. }
  147. }
  148. }
  149. m = (uint8_t *)OPENSSL_malloc(buf_len + 10);
  150. if (m == NULL) {
  151. OPENSSL_PUT_ERROR(EVP, ERR_R_MALLOC_FAILURE);
  152. goto err;
  153. }
  154. if (rsa->n != NULL) {
  155. mod_len = BN_num_bits(rsa->n);
  156. }
  157. if (!BIO_indent(out, off, 128)) {
  158. goto err;
  159. }
  160. if (include_private && rsa->d) {
  161. if (BIO_printf(out, "Private-Key: (%d bit)\n", mod_len) <= 0) {
  162. goto err;
  163. }
  164. str = "modulus:";
  165. s = "publicExponent:";
  166. } else {
  167. if (BIO_printf(out, "Public-Key: (%d bit)\n", mod_len) <= 0) {
  168. goto err;
  169. }
  170. str = "Modulus:";
  171. s = "Exponent:";
  172. }
  173. if (!bn_print(out, str, rsa->n, m, off) ||
  174. !bn_print(out, s, rsa->e, m, off)) {
  175. goto err;
  176. }
  177. if (include_private) {
  178. if (!bn_print(out, "privateExponent:", rsa->d, m, off) ||
  179. !bn_print(out, "prime1:", rsa->p, m, off) ||
  180. !bn_print(out, "prime2:", rsa->q, m, off) ||
  181. !bn_print(out, "exponent1:", rsa->dmp1, m, off) ||
  182. !bn_print(out, "exponent2:", rsa->dmq1, m, off) ||
  183. !bn_print(out, "coefficient:", rsa->iqmp, m, off)) {
  184. goto err;
  185. }
  186. if (rsa->additional_primes != NULL &&
  187. sk_RSA_additional_prime_num(rsa->additional_primes) > 0) {
  188. if (BIO_printf(out, "otherPrimeInfos:\n") <= 0) {
  189. goto err;
  190. }
  191. for (size_t i = 0;
  192. i < sk_RSA_additional_prime_num(rsa->additional_primes); i++) {
  193. const RSA_additional_prime *ap =
  194. sk_RSA_additional_prime_value(rsa->additional_primes, i);
  195. if (BIO_printf(out, "otherPrimeInfo (prime %u):\n",
  196. (unsigned)(i + 3)) <= 0 ||
  197. !bn_print(out, "prime:", ap->prime, m, off) ||
  198. !bn_print(out, "exponent:", ap->exp, m, off) ||
  199. !bn_print(out, "coeff:", ap->coeff, m, off)) {
  200. goto err;
  201. }
  202. }
  203. }
  204. }
  205. ret = 1;
  206. err:
  207. OPENSSL_free(m);
  208. return ret;
  209. }
  210. static int rsa_pub_print(BIO *bp, const EVP_PKEY *pkey, int indent,
  211. ASN1_PCTX *ctx) {
  212. return do_rsa_print(bp, pkey->pkey.rsa, indent, 0);
  213. }
  214. static int rsa_priv_print(BIO *bp, const EVP_PKEY *pkey, int indent,
  215. ASN1_PCTX *ctx) {
  216. return do_rsa_print(bp, pkey->pkey.rsa, indent, 1);
  217. }
  218. /* DSA keys. */
  219. static int do_dsa_print(BIO *bp, const DSA *x, int off, int ptype) {
  220. uint8_t *m = NULL;
  221. int ret = 0;
  222. size_t buf_len = 0;
  223. const char *ktype = NULL;
  224. const BIGNUM *priv_key, *pub_key;
  225. priv_key = NULL;
  226. if (ptype == 2) {
  227. priv_key = x->priv_key;
  228. }
  229. pub_key = NULL;
  230. if (ptype > 0) {
  231. pub_key = x->pub_key;
  232. }
  233. ktype = "DSA-Parameters";
  234. if (ptype == 2) {
  235. ktype = "Private-Key";
  236. } else if (ptype == 1) {
  237. ktype = "Public-Key";
  238. }
  239. update_buflen(x->p, &buf_len);
  240. update_buflen(x->q, &buf_len);
  241. update_buflen(x->g, &buf_len);
  242. update_buflen(priv_key, &buf_len);
  243. update_buflen(pub_key, &buf_len);
  244. m = (uint8_t *)OPENSSL_malloc(buf_len + 10);
  245. if (m == NULL) {
  246. OPENSSL_PUT_ERROR(EVP, ERR_R_MALLOC_FAILURE);
  247. goto err;
  248. }
  249. if (priv_key) {
  250. if (!BIO_indent(bp, off, 128) ||
  251. BIO_printf(bp, "%s: (%d bit)\n", ktype, BN_num_bits(x->p)) <= 0) {
  252. goto err;
  253. }
  254. }
  255. if (!bn_print(bp, "priv:", priv_key, m, off) ||
  256. !bn_print(bp, "pub: ", pub_key, m, off) ||
  257. !bn_print(bp, "P: ", x->p, m, off) ||
  258. !bn_print(bp, "Q: ", x->q, m, off) ||
  259. !bn_print(bp, "G: ", x->g, m, off)) {
  260. goto err;
  261. }
  262. ret = 1;
  263. err:
  264. OPENSSL_free(m);
  265. return ret;
  266. }
  267. static int dsa_param_print(BIO *bp, const EVP_PKEY *pkey, int indent,
  268. ASN1_PCTX *ctx) {
  269. return do_dsa_print(bp, pkey->pkey.dsa, indent, 0);
  270. }
  271. static int dsa_pub_print(BIO *bp, const EVP_PKEY *pkey, int indent,
  272. ASN1_PCTX *ctx) {
  273. return do_dsa_print(bp, pkey->pkey.dsa, indent, 1);
  274. }
  275. static int dsa_priv_print(BIO *bp, const EVP_PKEY *pkey, int indent,
  276. ASN1_PCTX *ctx) {
  277. return do_dsa_print(bp, pkey->pkey.dsa, indent, 2);
  278. }
  279. /* EC keys. */
  280. static int do_EC_KEY_print(BIO *bp, const EC_KEY *x, int off, int ktype) {
  281. uint8_t *buffer = NULL;
  282. const char *ecstr;
  283. size_t buf_len = 0, i;
  284. int ret = 0, reason = ERR_R_BIO_LIB;
  285. BIGNUM *order = NULL;
  286. BN_CTX *ctx = NULL;
  287. const EC_GROUP *group;
  288. const EC_POINT *public_key;
  289. const BIGNUM *priv_key;
  290. uint8_t *pub_key_bytes = NULL;
  291. size_t pub_key_bytes_len = 0;
  292. if (x == NULL || (group = EC_KEY_get0_group(x)) == NULL) {
  293. reason = ERR_R_PASSED_NULL_PARAMETER;
  294. goto err;
  295. }
  296. ctx = BN_CTX_new();
  297. if (ctx == NULL) {
  298. reason = ERR_R_MALLOC_FAILURE;
  299. goto err;
  300. }
  301. if (ktype > 0) {
  302. public_key = EC_KEY_get0_public_key(x);
  303. if (public_key != NULL) {
  304. pub_key_bytes_len = EC_POINT_point2oct(
  305. group, public_key, EC_KEY_get_conv_form(x), NULL, 0, ctx);
  306. if (pub_key_bytes_len == 0) {
  307. reason = ERR_R_MALLOC_FAILURE;
  308. goto err;
  309. }
  310. pub_key_bytes = OPENSSL_malloc(pub_key_bytes_len);
  311. if (pub_key_bytes == NULL) {
  312. reason = ERR_R_MALLOC_FAILURE;
  313. goto err;
  314. }
  315. pub_key_bytes_len =
  316. EC_POINT_point2oct(group, public_key, EC_KEY_get_conv_form(x),
  317. pub_key_bytes, pub_key_bytes_len, ctx);
  318. if (pub_key_bytes_len == 0) {
  319. reason = ERR_R_MALLOC_FAILURE;
  320. goto err;
  321. }
  322. buf_len = pub_key_bytes_len;
  323. }
  324. }
  325. if (ktype == 2) {
  326. priv_key = EC_KEY_get0_private_key(x);
  327. if (priv_key && (i = (size_t)BN_num_bytes(priv_key)) > buf_len) {
  328. buf_len = i;
  329. }
  330. } else {
  331. priv_key = NULL;
  332. }
  333. if (ktype > 0) {
  334. buf_len += 10;
  335. if ((buffer = OPENSSL_malloc(buf_len)) == NULL) {
  336. reason = ERR_R_MALLOC_FAILURE;
  337. goto err;
  338. }
  339. }
  340. if (ktype == 2) {
  341. ecstr = "Private-Key";
  342. } else if (ktype == 1) {
  343. ecstr = "Public-Key";
  344. } else {
  345. ecstr = "ECDSA-Parameters";
  346. }
  347. if (!BIO_indent(bp, off, 128)) {
  348. goto err;
  349. }
  350. order = BN_new();
  351. if (order == NULL || !EC_GROUP_get_order(group, order, NULL) ||
  352. BIO_printf(bp, "%s: (%d bit)\n", ecstr, BN_num_bits(order)) <= 0) {
  353. goto err;
  354. }
  355. if ((priv_key != NULL) &&
  356. !bn_print(bp, "priv:", priv_key, buffer, off)) {
  357. goto err;
  358. }
  359. if (pub_key_bytes != NULL) {
  360. BIO_hexdump(bp, pub_key_bytes, pub_key_bytes_len, off);
  361. }
  362. /* TODO(fork): implement */
  363. /*
  364. if (!ECPKParameters_print(bp, group, off))
  365. goto err; */
  366. ret = 1;
  367. err:
  368. if (!ret) {
  369. OPENSSL_PUT_ERROR(EVP, reason);
  370. }
  371. OPENSSL_free(pub_key_bytes);
  372. BN_free(order);
  373. BN_CTX_free(ctx);
  374. OPENSSL_free(buffer);
  375. return ret;
  376. }
  377. static int eckey_param_print(BIO *bp, const EVP_PKEY *pkey, int indent,
  378. ASN1_PCTX *ctx) {
  379. return do_EC_KEY_print(bp, pkey->pkey.ec, indent, 0);
  380. }
  381. static int eckey_pub_print(BIO *bp, const EVP_PKEY *pkey, int indent,
  382. ASN1_PCTX *ctx) {
  383. return do_EC_KEY_print(bp, pkey->pkey.ec, indent, 1);
  384. }
  385. static int eckey_priv_print(BIO *bp, const EVP_PKEY *pkey, int indent,
  386. ASN1_PCTX *ctx) {
  387. return do_EC_KEY_print(bp, pkey->pkey.ec, indent, 2);
  388. }
  389. typedef struct {
  390. int type;
  391. int (*pub_print)(BIO *out, const EVP_PKEY *pkey, int indent, ASN1_PCTX *pctx);
  392. int (*priv_print)(BIO *out, const EVP_PKEY *pkey, int indent,
  393. ASN1_PCTX *pctx);
  394. int (*param_print)(BIO *out, const EVP_PKEY *pkey, int indent,
  395. ASN1_PCTX *pctx);
  396. } EVP_PKEY_PRINT_METHOD;
  397. static EVP_PKEY_PRINT_METHOD kPrintMethods[] = {
  398. {
  399. EVP_PKEY_RSA,
  400. rsa_pub_print,
  401. rsa_priv_print,
  402. NULL /* param_print */,
  403. },
  404. {
  405. EVP_PKEY_DSA,
  406. dsa_pub_print,
  407. dsa_priv_print,
  408. dsa_param_print,
  409. },
  410. {
  411. EVP_PKEY_EC,
  412. eckey_pub_print,
  413. eckey_priv_print,
  414. eckey_param_print,
  415. },
  416. };
  417. static size_t kPrintMethodsLen = OPENSSL_ARRAY_SIZE(kPrintMethods);
  418. static EVP_PKEY_PRINT_METHOD *find_method(int type) {
  419. for (size_t i = 0; i < kPrintMethodsLen; i++) {
  420. if (kPrintMethods[i].type == type) {
  421. return &kPrintMethods[i];
  422. }
  423. }
  424. return NULL;
  425. }
  426. static int print_unsupported(BIO *out, const EVP_PKEY *pkey, int indent,
  427. const char *kstr) {
  428. BIO_indent(out, indent, 128);
  429. BIO_printf(out, "%s algorithm unsupported\n", kstr);
  430. return 1;
  431. }
  432. int EVP_PKEY_print_public(BIO *out, const EVP_PKEY *pkey, int indent,
  433. ASN1_PCTX *pctx) {
  434. EVP_PKEY_PRINT_METHOD *method = find_method(pkey->type);
  435. if (method != NULL && method->pub_print != NULL) {
  436. return method->pub_print(out, pkey, indent, pctx);
  437. }
  438. return print_unsupported(out, pkey, indent, "Public Key");
  439. }
  440. int EVP_PKEY_print_private(BIO *out, const EVP_PKEY *pkey, int indent,
  441. ASN1_PCTX *pctx) {
  442. EVP_PKEY_PRINT_METHOD *method = find_method(pkey->type);
  443. if (method != NULL && method->priv_print != NULL) {
  444. return method->priv_print(out, pkey, indent, pctx);
  445. }
  446. return print_unsupported(out, pkey, indent, "Private Key");
  447. }
  448. int EVP_PKEY_print_params(BIO *out, const EVP_PKEY *pkey, int indent,
  449. ASN1_PCTX *pctx) {
  450. EVP_PKEY_PRINT_METHOD *method = find_method(pkey->type);
  451. if (method != NULL && method->param_print != NULL) {
  452. return method->param_print(out, pkey, indent, pctx);
  453. }
  454. return print_unsupported(out, pkey, indent, "Parameters");
  455. }