No puede seleccionar más de 25 temas Los temas deben comenzar con una letra o número, pueden incluir guiones ('-') y pueden tener hasta 35 caracteres de largo.
 
 
 
 
 
 

490 líneas
13 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 "../fipsmodule/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. }
  139. m = (uint8_t *)OPENSSL_malloc(buf_len + 10);
  140. if (m == NULL) {
  141. OPENSSL_PUT_ERROR(EVP, ERR_R_MALLOC_FAILURE);
  142. goto err;
  143. }
  144. if (rsa->n != NULL) {
  145. mod_len = BN_num_bits(rsa->n);
  146. }
  147. if (!BIO_indent(out, off, 128)) {
  148. goto err;
  149. }
  150. if (include_private && rsa->d) {
  151. if (BIO_printf(out, "Private-Key: (%d bit)\n", mod_len) <= 0) {
  152. goto err;
  153. }
  154. str = "modulus:";
  155. s = "publicExponent:";
  156. } else {
  157. if (BIO_printf(out, "Public-Key: (%d bit)\n", mod_len) <= 0) {
  158. goto err;
  159. }
  160. str = "Modulus:";
  161. s = "Exponent:";
  162. }
  163. if (!bn_print(out, str, rsa->n, m, off) ||
  164. !bn_print(out, s, rsa->e, m, off)) {
  165. goto err;
  166. }
  167. if (include_private) {
  168. if (!bn_print(out, "privateExponent:", rsa->d, m, off) ||
  169. !bn_print(out, "prime1:", rsa->p, m, off) ||
  170. !bn_print(out, "prime2:", rsa->q, m, off) ||
  171. !bn_print(out, "exponent1:", rsa->dmp1, m, off) ||
  172. !bn_print(out, "exponent2:", rsa->dmq1, m, off) ||
  173. !bn_print(out, "coefficient:", rsa->iqmp, m, off)) {
  174. goto err;
  175. }
  176. }
  177. ret = 1;
  178. err:
  179. OPENSSL_free(m);
  180. return ret;
  181. }
  182. static int rsa_pub_print(BIO *bp, const EVP_PKEY *pkey, int indent,
  183. ASN1_PCTX *ctx) {
  184. return do_rsa_print(bp, pkey->pkey.rsa, indent, 0);
  185. }
  186. static int rsa_priv_print(BIO *bp, const EVP_PKEY *pkey, int indent,
  187. ASN1_PCTX *ctx) {
  188. return do_rsa_print(bp, pkey->pkey.rsa, indent, 1);
  189. }
  190. // DSA keys.
  191. static int do_dsa_print(BIO *bp, const DSA *x, int off, int ptype) {
  192. uint8_t *m = NULL;
  193. int ret = 0;
  194. size_t buf_len = 0;
  195. const char *ktype = NULL;
  196. const BIGNUM *priv_key, *pub_key;
  197. priv_key = NULL;
  198. if (ptype == 2) {
  199. priv_key = x->priv_key;
  200. }
  201. pub_key = NULL;
  202. if (ptype > 0) {
  203. pub_key = x->pub_key;
  204. }
  205. ktype = "DSA-Parameters";
  206. if (ptype == 2) {
  207. ktype = "Private-Key";
  208. } else if (ptype == 1) {
  209. ktype = "Public-Key";
  210. }
  211. update_buflen(x->p, &buf_len);
  212. update_buflen(x->q, &buf_len);
  213. update_buflen(x->g, &buf_len);
  214. update_buflen(priv_key, &buf_len);
  215. update_buflen(pub_key, &buf_len);
  216. m = (uint8_t *)OPENSSL_malloc(buf_len + 10);
  217. if (m == NULL) {
  218. OPENSSL_PUT_ERROR(EVP, ERR_R_MALLOC_FAILURE);
  219. goto err;
  220. }
  221. if (priv_key) {
  222. if (!BIO_indent(bp, off, 128) ||
  223. BIO_printf(bp, "%s: (%d bit)\n", ktype, BN_num_bits(x->p)) <= 0) {
  224. goto err;
  225. }
  226. }
  227. if (!bn_print(bp, "priv:", priv_key, m, off) ||
  228. !bn_print(bp, "pub: ", pub_key, m, off) ||
  229. !bn_print(bp, "P: ", x->p, m, off) ||
  230. !bn_print(bp, "Q: ", x->q, m, off) ||
  231. !bn_print(bp, "G: ", x->g, m, off)) {
  232. goto err;
  233. }
  234. ret = 1;
  235. err:
  236. OPENSSL_free(m);
  237. return ret;
  238. }
  239. static int dsa_param_print(BIO *bp, const EVP_PKEY *pkey, int indent,
  240. ASN1_PCTX *ctx) {
  241. return do_dsa_print(bp, pkey->pkey.dsa, indent, 0);
  242. }
  243. static int dsa_pub_print(BIO *bp, const EVP_PKEY *pkey, int indent,
  244. ASN1_PCTX *ctx) {
  245. return do_dsa_print(bp, pkey->pkey.dsa, indent, 1);
  246. }
  247. static int dsa_priv_print(BIO *bp, const EVP_PKEY *pkey, int indent,
  248. ASN1_PCTX *ctx) {
  249. return do_dsa_print(bp, pkey->pkey.dsa, indent, 2);
  250. }
  251. // EC keys.
  252. static int do_EC_KEY_print(BIO *bp, const EC_KEY *x, int off, int ktype) {
  253. uint8_t *buffer = NULL;
  254. const char *ecstr;
  255. size_t buf_len = 0, i;
  256. int ret = 0, reason = ERR_R_BIO_LIB;
  257. BIGNUM *order = NULL;
  258. BN_CTX *ctx = NULL;
  259. const EC_GROUP *group;
  260. const EC_POINT *public_key;
  261. const BIGNUM *priv_key;
  262. uint8_t *pub_key_bytes = NULL;
  263. size_t pub_key_bytes_len = 0;
  264. if (x == NULL || (group = EC_KEY_get0_group(x)) == NULL) {
  265. reason = ERR_R_PASSED_NULL_PARAMETER;
  266. goto err;
  267. }
  268. ctx = BN_CTX_new();
  269. if (ctx == NULL) {
  270. reason = ERR_R_MALLOC_FAILURE;
  271. goto err;
  272. }
  273. if (ktype > 0) {
  274. public_key = EC_KEY_get0_public_key(x);
  275. if (public_key != NULL) {
  276. pub_key_bytes_len = EC_POINT_point2oct(
  277. group, public_key, EC_KEY_get_conv_form(x), NULL, 0, ctx);
  278. if (pub_key_bytes_len == 0) {
  279. reason = ERR_R_MALLOC_FAILURE;
  280. goto err;
  281. }
  282. pub_key_bytes = OPENSSL_malloc(pub_key_bytes_len);
  283. if (pub_key_bytes == NULL) {
  284. reason = ERR_R_MALLOC_FAILURE;
  285. goto err;
  286. }
  287. pub_key_bytes_len =
  288. EC_POINT_point2oct(group, public_key, EC_KEY_get_conv_form(x),
  289. pub_key_bytes, pub_key_bytes_len, ctx);
  290. if (pub_key_bytes_len == 0) {
  291. reason = ERR_R_MALLOC_FAILURE;
  292. goto err;
  293. }
  294. buf_len = pub_key_bytes_len;
  295. }
  296. }
  297. if (ktype == 2) {
  298. priv_key = EC_KEY_get0_private_key(x);
  299. if (priv_key && (i = (size_t)BN_num_bytes(priv_key)) > buf_len) {
  300. buf_len = i;
  301. }
  302. } else {
  303. priv_key = NULL;
  304. }
  305. if (ktype > 0) {
  306. buf_len += 10;
  307. if ((buffer = OPENSSL_malloc(buf_len)) == NULL) {
  308. reason = ERR_R_MALLOC_FAILURE;
  309. goto err;
  310. }
  311. }
  312. if (ktype == 2) {
  313. ecstr = "Private-Key";
  314. } else if (ktype == 1) {
  315. ecstr = "Public-Key";
  316. } else {
  317. ecstr = "ECDSA-Parameters";
  318. }
  319. if (!BIO_indent(bp, off, 128)) {
  320. goto err;
  321. }
  322. order = BN_new();
  323. if (order == NULL || !EC_GROUP_get_order(group, order, NULL) ||
  324. BIO_printf(bp, "%s: (%d bit)\n", ecstr, BN_num_bits(order)) <= 0) {
  325. goto err;
  326. }
  327. if ((priv_key != NULL) &&
  328. !bn_print(bp, "priv:", priv_key, buffer, off)) {
  329. goto err;
  330. }
  331. if (pub_key_bytes != NULL) {
  332. BIO_hexdump(bp, pub_key_bytes, pub_key_bytes_len, off);
  333. }
  334. // TODO(fork): implement
  335. /*
  336. if (!ECPKParameters_print(bp, group, off))
  337. goto err; */
  338. ret = 1;
  339. err:
  340. if (!ret) {
  341. OPENSSL_PUT_ERROR(EVP, reason);
  342. }
  343. OPENSSL_free(pub_key_bytes);
  344. BN_free(order);
  345. BN_CTX_free(ctx);
  346. OPENSSL_free(buffer);
  347. return ret;
  348. }
  349. static int eckey_param_print(BIO *bp, const EVP_PKEY *pkey, int indent,
  350. ASN1_PCTX *ctx) {
  351. return do_EC_KEY_print(bp, pkey->pkey.ec, indent, 0);
  352. }
  353. static int eckey_pub_print(BIO *bp, const EVP_PKEY *pkey, int indent,
  354. ASN1_PCTX *ctx) {
  355. return do_EC_KEY_print(bp, pkey->pkey.ec, indent, 1);
  356. }
  357. static int eckey_priv_print(BIO *bp, const EVP_PKEY *pkey, int indent,
  358. ASN1_PCTX *ctx) {
  359. return do_EC_KEY_print(bp, pkey->pkey.ec, indent, 2);
  360. }
  361. typedef struct {
  362. int type;
  363. int (*pub_print)(BIO *out, const EVP_PKEY *pkey, int indent, ASN1_PCTX *pctx);
  364. int (*priv_print)(BIO *out, const EVP_PKEY *pkey, int indent,
  365. ASN1_PCTX *pctx);
  366. int (*param_print)(BIO *out, const EVP_PKEY *pkey, int indent,
  367. ASN1_PCTX *pctx);
  368. } EVP_PKEY_PRINT_METHOD;
  369. static EVP_PKEY_PRINT_METHOD kPrintMethods[] = {
  370. {
  371. EVP_PKEY_RSA,
  372. rsa_pub_print,
  373. rsa_priv_print,
  374. NULL /* param_print */,
  375. },
  376. {
  377. EVP_PKEY_DSA,
  378. dsa_pub_print,
  379. dsa_priv_print,
  380. dsa_param_print,
  381. },
  382. {
  383. EVP_PKEY_EC,
  384. eckey_pub_print,
  385. eckey_priv_print,
  386. eckey_param_print,
  387. },
  388. };
  389. static size_t kPrintMethodsLen = OPENSSL_ARRAY_SIZE(kPrintMethods);
  390. static EVP_PKEY_PRINT_METHOD *find_method(int type) {
  391. for (size_t i = 0; i < kPrintMethodsLen; i++) {
  392. if (kPrintMethods[i].type == type) {
  393. return &kPrintMethods[i];
  394. }
  395. }
  396. return NULL;
  397. }
  398. static int print_unsupported(BIO *out, const EVP_PKEY *pkey, int indent,
  399. const char *kstr) {
  400. BIO_indent(out, indent, 128);
  401. BIO_printf(out, "%s algorithm unsupported\n", kstr);
  402. return 1;
  403. }
  404. int EVP_PKEY_print_public(BIO *out, const EVP_PKEY *pkey, int indent,
  405. ASN1_PCTX *pctx) {
  406. EVP_PKEY_PRINT_METHOD *method = find_method(pkey->type);
  407. if (method != NULL && method->pub_print != NULL) {
  408. return method->pub_print(out, pkey, indent, pctx);
  409. }
  410. return print_unsupported(out, pkey, indent, "Public Key");
  411. }
  412. int EVP_PKEY_print_private(BIO *out, const EVP_PKEY *pkey, int indent,
  413. ASN1_PCTX *pctx) {
  414. EVP_PKEY_PRINT_METHOD *method = find_method(pkey->type);
  415. if (method != NULL && method->priv_print != NULL) {
  416. return method->priv_print(out, pkey, indent, pctx);
  417. }
  418. return print_unsupported(out, pkey, indent, "Private Key");
  419. }
  420. int EVP_PKEY_print_params(BIO *out, const EVP_PKEY *pkey, int indent,
  421. ASN1_PCTX *pctx) {
  422. EVP_PKEY_PRINT_METHOD *method = find_method(pkey->type);
  423. if (method != NULL && method->param_print != NULL) {
  424. return method->param_print(out, pkey, indent, pctx);
  425. }
  426. return print_unsupported(out, pkey, indent, "Parameters");
  427. }