Nie możesz wybrać więcej, niż 25 tematów Tematy muszą się zaczynać od litery lub cyfry, mogą zawierać myślniki ('-') i mogą mieć do 35 znaków.
 
 
 
 
 
 

563 wiersze
14 KiB

  1. /* Written by Dr Stephen N Henson (steve@openssl.org) for the OpenSSL
  2. * project 2006.
  3. */
  4. /* ====================================================================
  5. * Copyright (c) 2006 The OpenSSL Project. All rights reserved.
  6. *
  7. * Redistribution and use in source and binary forms, with or without
  8. * modification, are permitted provided that the following conditions
  9. * are met:
  10. *
  11. * 1. Redistributions of source code must retain the above copyright
  12. * notice, this list of conditions and the following disclaimer.
  13. *
  14. * 2. Redistributions in binary form must reproduce the above copyright
  15. * notice, this list of conditions and the following disclaimer in
  16. * the documentation and/or other materials provided with the
  17. * distribution.
  18. *
  19. * 3. All advertising materials mentioning features or use of this
  20. * software must display the following acknowledgment:
  21. * "This product includes software developed by the OpenSSL Project
  22. * for use in the OpenSSL Toolkit. (http://www.OpenSSL.org/)"
  23. *
  24. * 4. The names "OpenSSL Toolkit" and "OpenSSL Project" must not be used to
  25. * endorse or promote products derived from this software without
  26. * prior written permission. For written permission, please contact
  27. * licensing@OpenSSL.org.
  28. *
  29. * 5. Products derived from this software may not be called "OpenSSL"
  30. * nor may "OpenSSL" appear in their names without prior written
  31. * permission of the OpenSSL Project.
  32. *
  33. * 6. Redistributions of any form whatsoever must retain the following
  34. * acknowledgment:
  35. * "This product includes software developed by the OpenSSL Project
  36. * for use in the OpenSSL Toolkit (http://www.OpenSSL.org/)"
  37. *
  38. * THIS SOFTWARE IS PROVIDED BY THE OpenSSL PROJECT ``AS IS'' AND ANY
  39. * EXPRESSED OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
  40. * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
  41. * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE OpenSSL PROJECT OR
  42. * ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
  43. * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
  44. * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
  45. * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
  46. * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
  47. * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
  48. * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED
  49. * OF THE POSSIBILITY OF SUCH DAMAGE.
  50. * ====================================================================
  51. *
  52. * This product includes cryptographic software written by Eric Young
  53. * (eay@cryptsoft.com). This product includes software written by Tim
  54. * Hudson (tjh@cryptsoft.com). */
  55. #include <openssl/evp.h>
  56. #include <openssl/asn1t.h>
  57. #include <openssl/bn.h>
  58. #include <openssl/ec.h>
  59. #include <openssl/err.h>
  60. #include <openssl/mem.h>
  61. #include <openssl/obj.h>
  62. #include <openssl/x509.h>
  63. #include "internal.h"
  64. static int eckey_param2type(int *pptype, void **ppval, EC_KEY *ec_key) {
  65. const EC_GROUP *group;
  66. int nid;
  67. if (ec_key == NULL || (group = EC_KEY_get0_group(ec_key)) == NULL) {
  68. OPENSSL_PUT_ERROR(EVP, EVP_R_MISSING_PARAMETERS);
  69. return 0;
  70. }
  71. nid = EC_GROUP_get_curve_name(group);
  72. if (nid == NID_undef) {
  73. OPENSSL_PUT_ERROR(EVP, EVP_R_NO_NID_FOR_CURVE);
  74. return 0;
  75. }
  76. *ppval = (void*) OBJ_nid2obj(nid);
  77. *pptype = V_ASN1_OBJECT;
  78. return 1;
  79. }
  80. static int eckey_pub_encode(X509_PUBKEY *pk, const EVP_PKEY *pkey) {
  81. EC_KEY *ec_key = pkey->pkey.ec;
  82. void *pval = NULL;
  83. int ptype;
  84. uint8_t *penc = NULL, *p;
  85. int penclen;
  86. if (!eckey_param2type(&ptype, &pval, ec_key)) {
  87. OPENSSL_PUT_ERROR(EVP, ERR_R_EC_LIB);
  88. return 0;
  89. }
  90. penclen = i2o_ECPublicKey(ec_key, NULL);
  91. if (penclen <= 0) {
  92. goto err;
  93. }
  94. penc = OPENSSL_malloc(penclen);
  95. if (!penc) {
  96. goto err;
  97. }
  98. p = penc;
  99. penclen = i2o_ECPublicKey(ec_key, &p);
  100. if (penclen <= 0) {
  101. goto err;
  102. }
  103. if (X509_PUBKEY_set0_param(pk, OBJ_nid2obj(EVP_PKEY_EC), ptype, pval, penc,
  104. penclen)) {
  105. return 1;
  106. }
  107. err:
  108. if (ptype == V_ASN1_OBJECT) {
  109. ASN1_OBJECT_free(pval);
  110. } else {
  111. ASN1_STRING_free(pval);
  112. }
  113. if (penc) {
  114. OPENSSL_free(penc);
  115. }
  116. return 0;
  117. }
  118. static EC_KEY *eckey_type2param(int ptype, void *pval) {
  119. EC_KEY *eckey = NULL;
  120. if (ptype == V_ASN1_SEQUENCE) {
  121. ASN1_STRING *pstr = pval;
  122. const uint8_t *pm = pstr->data;
  123. int pmlen = pstr->length;
  124. eckey = d2i_ECParameters(NULL, &pm, pmlen);
  125. if (eckey == NULL) {
  126. OPENSSL_PUT_ERROR(EVP, EVP_R_DECODE_ERROR);
  127. goto err;
  128. }
  129. } else if (ptype == V_ASN1_OBJECT) {
  130. ASN1_OBJECT *poid = pval;
  131. /* type == V_ASN1_OBJECT => the parameters are given
  132. * by an asn1 OID */
  133. eckey = EC_KEY_new_by_curve_name(OBJ_obj2nid(poid));
  134. if (eckey == NULL) {
  135. goto err;
  136. }
  137. } else {
  138. OPENSSL_PUT_ERROR(EVP, EVP_R_DECODE_ERROR);
  139. goto err;
  140. }
  141. return eckey;
  142. err:
  143. if (eckey) {
  144. EC_KEY_free(eckey);
  145. }
  146. return NULL;
  147. }
  148. static int eckey_pub_decode(EVP_PKEY *pkey, X509_PUBKEY *pubkey) {
  149. const uint8_t *p = NULL;
  150. void *pval;
  151. int ptype, pklen;
  152. EC_KEY *eckey = NULL;
  153. X509_ALGOR *palg;
  154. if (!X509_PUBKEY_get0_param(NULL, &p, &pklen, &palg, pubkey)) {
  155. return 0;
  156. }
  157. X509_ALGOR_get0(NULL, &ptype, &pval, palg);
  158. eckey = eckey_type2param(ptype, pval);
  159. if (!eckey) {
  160. OPENSSL_PUT_ERROR(EVP, ERR_R_EC_LIB);
  161. return 0;
  162. }
  163. /* We have parameters now set public key */
  164. if (!o2i_ECPublicKey(&eckey, &p, pklen)) {
  165. OPENSSL_PUT_ERROR(EVP, EVP_R_DECODE_ERROR);
  166. goto err;
  167. }
  168. EVP_PKEY_assign_EC_KEY(pkey, eckey);
  169. return 1;
  170. err:
  171. if (eckey) {
  172. EC_KEY_free(eckey);
  173. }
  174. return 0;
  175. }
  176. static int eckey_pub_cmp(const EVP_PKEY *a, const EVP_PKEY *b) {
  177. int r;
  178. const EC_GROUP *group = EC_KEY_get0_group(b->pkey.ec);
  179. const EC_POINT *pa = EC_KEY_get0_public_key(a->pkey.ec),
  180. *pb = EC_KEY_get0_public_key(b->pkey.ec);
  181. r = EC_POINT_cmp(group, pa, pb, NULL);
  182. if (r == 0) {
  183. return 1;
  184. } else if (r == 1) {
  185. return 0;
  186. } else {
  187. return -2;
  188. }
  189. }
  190. static int eckey_priv_decode(EVP_PKEY *pkey, PKCS8_PRIV_KEY_INFO *p8) {
  191. const uint8_t *p = NULL;
  192. void *pval;
  193. int ptype, pklen;
  194. EC_KEY *eckey = NULL;
  195. X509_ALGOR *palg;
  196. if (!PKCS8_pkey_get0(NULL, &p, &pklen, &palg, p8)) {
  197. return 0;
  198. }
  199. X509_ALGOR_get0(NULL, &ptype, &pval, palg);
  200. eckey = eckey_type2param(ptype, pval);
  201. if (!eckey) {
  202. goto ecliberr;
  203. }
  204. /* We have parameters now set private key */
  205. if (!d2i_ECPrivateKey(&eckey, &p, pklen)) {
  206. OPENSSL_PUT_ERROR(EVP, EVP_R_DECODE_ERROR);
  207. goto ecerr;
  208. }
  209. /* calculate public key (if necessary) */
  210. if (EC_KEY_get0_public_key(eckey) == NULL) {
  211. const BIGNUM *priv_key;
  212. const EC_GROUP *group;
  213. EC_POINT *pub_key;
  214. /* the public key was not included in the SEC1 private
  215. * key => calculate the public key */
  216. group = EC_KEY_get0_group(eckey);
  217. pub_key = EC_POINT_new(group);
  218. if (pub_key == NULL) {
  219. OPENSSL_PUT_ERROR(EVP, ERR_R_EC_LIB);
  220. goto ecliberr;
  221. }
  222. if (!EC_POINT_copy(pub_key, EC_GROUP_get0_generator(group))) {
  223. EC_POINT_free(pub_key);
  224. OPENSSL_PUT_ERROR(EVP, ERR_R_EC_LIB);
  225. goto ecliberr;
  226. }
  227. priv_key = EC_KEY_get0_private_key(eckey);
  228. if (!EC_POINT_mul(group, pub_key, priv_key, NULL, NULL, NULL)) {
  229. EC_POINT_free(pub_key);
  230. OPENSSL_PUT_ERROR(EVP, ERR_R_EC_LIB);
  231. goto ecliberr;
  232. }
  233. if (EC_KEY_set_public_key(eckey, pub_key) == 0) {
  234. EC_POINT_free(pub_key);
  235. OPENSSL_PUT_ERROR(EVP, ERR_R_EC_LIB);
  236. goto ecliberr;
  237. }
  238. EC_POINT_free(pub_key);
  239. }
  240. EVP_PKEY_assign_EC_KEY(pkey, eckey);
  241. return 1;
  242. ecliberr:
  243. OPENSSL_PUT_ERROR(EVP, ERR_R_EC_LIB);
  244. ecerr:
  245. if (eckey) {
  246. EC_KEY_free(eckey);
  247. }
  248. return 0;
  249. }
  250. static int eckey_priv_encode(PKCS8_PRIV_KEY_INFO *p8, const EVP_PKEY *pkey) {
  251. EC_KEY *ec_key;
  252. uint8_t *ep, *p;
  253. int eplen, ptype;
  254. void *pval;
  255. unsigned int tmp_flags, old_flags;
  256. ec_key = pkey->pkey.ec;
  257. if (!eckey_param2type(&ptype, &pval, ec_key)) {
  258. OPENSSL_PUT_ERROR(EVP, EVP_R_DECODE_ERROR);
  259. return 0;
  260. }
  261. /* set the private key */
  262. /* do not include the parameters in the SEC1 private key
  263. * see PKCS#11 12.11 */
  264. old_flags = EC_KEY_get_enc_flags(ec_key);
  265. tmp_flags = old_flags | EC_PKEY_NO_PARAMETERS;
  266. EC_KEY_set_enc_flags(ec_key, tmp_flags);
  267. eplen = i2d_ECPrivateKey(ec_key, NULL);
  268. if (!eplen) {
  269. EC_KEY_set_enc_flags(ec_key, old_flags);
  270. OPENSSL_PUT_ERROR(EVP, ERR_R_EC_LIB);
  271. return 0;
  272. }
  273. ep = (uint8_t *)OPENSSL_malloc(eplen);
  274. if (!ep) {
  275. EC_KEY_set_enc_flags(ec_key, old_flags);
  276. OPENSSL_PUT_ERROR(EVP, ERR_R_MALLOC_FAILURE);
  277. return 0;
  278. }
  279. p = ep;
  280. if (!i2d_ECPrivateKey(ec_key, &p)) {
  281. EC_KEY_set_enc_flags(ec_key, old_flags);
  282. OPENSSL_free(ep);
  283. OPENSSL_PUT_ERROR(EVP, ERR_R_EC_LIB);
  284. return 0;
  285. }
  286. /* restore old encoding flags */
  287. EC_KEY_set_enc_flags(ec_key, old_flags);
  288. if (!PKCS8_pkey_set0(p8, (ASN1_OBJECT *)OBJ_nid2obj(NID_X9_62_id_ecPublicKey),
  289. 0, ptype, pval, ep, eplen)) {
  290. OPENSSL_free(ep);
  291. return 0;
  292. }
  293. return 1;
  294. }
  295. static int int_ec_size(const EVP_PKEY *pkey) {
  296. return ECDSA_size(pkey->pkey.ec);
  297. }
  298. static int ec_bits(const EVP_PKEY *pkey) {
  299. const EC_GROUP *group = EC_KEY_get0_group(pkey->pkey.ec);
  300. if (group == NULL) {
  301. ERR_clear_error();
  302. return 0;
  303. }
  304. return BN_num_bits(EC_GROUP_get0_order(group));
  305. }
  306. static int ec_missing_parameters(const EVP_PKEY *pkey) {
  307. return EC_KEY_get0_group(pkey->pkey.ec) == NULL;
  308. }
  309. static int ec_copy_parameters(EVP_PKEY *to, const EVP_PKEY *from) {
  310. EC_GROUP *group = EC_GROUP_dup(EC_KEY_get0_group(from->pkey.ec));
  311. if (group == NULL ||
  312. EC_KEY_set_group(to->pkey.ec, group) == 0) {
  313. return 0;
  314. }
  315. EC_GROUP_free(group);
  316. return 1;
  317. }
  318. static int ec_cmp_parameters(const EVP_PKEY *a, const EVP_PKEY *b) {
  319. const EC_GROUP *group_a = EC_KEY_get0_group(a->pkey.ec),
  320. *group_b = EC_KEY_get0_group(b->pkey.ec);
  321. if (EC_GROUP_cmp(group_a, group_b, NULL) != 0) {
  322. /* mismatch */
  323. return 0;
  324. }
  325. return 1;
  326. }
  327. static void int_ec_free(EVP_PKEY *pkey) { EC_KEY_free(pkey->pkey.ec); }
  328. static int do_EC_KEY_print(BIO *bp, const EC_KEY *x, int off, int ktype) {
  329. uint8_t *buffer = NULL;
  330. const char *ecstr;
  331. size_t buf_len = 0, i;
  332. int ret = 0, reason = ERR_R_BIO_LIB;
  333. BN_CTX *ctx = NULL;
  334. const EC_GROUP *group;
  335. const EC_POINT *public_key;
  336. const BIGNUM *priv_key;
  337. uint8_t *pub_key_bytes = NULL;
  338. size_t pub_key_bytes_len = 0;
  339. if (x == NULL || (group = EC_KEY_get0_group(x)) == NULL) {
  340. reason = ERR_R_PASSED_NULL_PARAMETER;
  341. goto err;
  342. }
  343. ctx = BN_CTX_new();
  344. if (ctx == NULL) {
  345. reason = ERR_R_MALLOC_FAILURE;
  346. goto err;
  347. }
  348. if (ktype > 0) {
  349. public_key = EC_KEY_get0_public_key(x);
  350. if (public_key != NULL) {
  351. pub_key_bytes_len = EC_POINT_point2oct(
  352. group, public_key, EC_KEY_get_conv_form(x), NULL, 0, ctx);
  353. if (pub_key_bytes_len == 0) {
  354. reason = ERR_R_MALLOC_FAILURE;
  355. goto err;
  356. }
  357. pub_key_bytes = OPENSSL_malloc(pub_key_bytes_len);
  358. if (pub_key_bytes == NULL) {
  359. reason = ERR_R_MALLOC_FAILURE;
  360. goto err;
  361. }
  362. pub_key_bytes_len =
  363. EC_POINT_point2oct(group, public_key, EC_KEY_get_conv_form(x),
  364. pub_key_bytes, pub_key_bytes_len, ctx);
  365. if (pub_key_bytes_len == 0) {
  366. reason = ERR_R_MALLOC_FAILURE;
  367. goto err;
  368. }
  369. buf_len = pub_key_bytes_len;
  370. }
  371. }
  372. if (ktype == 2) {
  373. priv_key = EC_KEY_get0_private_key(x);
  374. if (priv_key && (i = (size_t)BN_num_bytes(priv_key)) > buf_len) {
  375. buf_len = i;
  376. }
  377. } else {
  378. priv_key = NULL;
  379. }
  380. if (ktype > 0) {
  381. buf_len += 10;
  382. if ((buffer = OPENSSL_malloc(buf_len)) == NULL) {
  383. reason = ERR_R_MALLOC_FAILURE;
  384. goto err;
  385. }
  386. }
  387. if (ktype == 2) {
  388. ecstr = "Private-Key";
  389. } else if (ktype == 1) {
  390. ecstr = "Public-Key";
  391. } else {
  392. ecstr = "ECDSA-Parameters";
  393. }
  394. if (!BIO_indent(bp, off, 128)) {
  395. goto err;
  396. }
  397. const BIGNUM *order = EC_GROUP_get0_order(group);
  398. if (BIO_printf(bp, "%s: (%d bit)\n", ecstr, BN_num_bits(order)) <= 0) {
  399. goto err;
  400. }
  401. if ((priv_key != NULL) &&
  402. !ASN1_bn_print(bp, "priv:", priv_key, buffer, off)) {
  403. goto err;
  404. }
  405. if (pub_key_bytes != NULL) {
  406. BIO_hexdump(bp, pub_key_bytes, pub_key_bytes_len, off);
  407. }
  408. /* TODO(fork): implement */
  409. /*
  410. if (!ECPKParameters_print(bp, group, off))
  411. goto err; */
  412. ret = 1;
  413. err:
  414. if (!ret) {
  415. OPENSSL_PUT_ERROR(EVP, reason);
  416. }
  417. OPENSSL_free(pub_key_bytes);
  418. BN_CTX_free(ctx);
  419. OPENSSL_free(buffer);
  420. return ret;
  421. }
  422. static int eckey_param_decode(EVP_PKEY *pkey, const uint8_t **pder,
  423. int derlen) {
  424. EC_KEY *eckey;
  425. if (!(eckey = d2i_ECParameters(NULL, pder, derlen))) {
  426. OPENSSL_PUT_ERROR(EVP, ERR_R_EC_LIB);
  427. return 0;
  428. }
  429. EVP_PKEY_assign_EC_KEY(pkey, eckey);
  430. return 1;
  431. }
  432. static int eckey_param_encode(const EVP_PKEY *pkey, uint8_t **pder) {
  433. return i2d_ECParameters(pkey->pkey.ec, pder);
  434. }
  435. static int eckey_param_print(BIO *bp, const EVP_PKEY *pkey, int indent,
  436. ASN1_PCTX *ctx) {
  437. return do_EC_KEY_print(bp, pkey->pkey.ec, indent, 0);
  438. }
  439. static int eckey_pub_print(BIO *bp, const EVP_PKEY *pkey, int indent,
  440. ASN1_PCTX *ctx) {
  441. return do_EC_KEY_print(bp, pkey->pkey.ec, indent, 1);
  442. }
  443. static int eckey_priv_print(BIO *bp, const EVP_PKEY *pkey, int indent,
  444. ASN1_PCTX *ctx) {
  445. return do_EC_KEY_print(bp, pkey->pkey.ec, indent, 2);
  446. }
  447. static int eckey_opaque(const EVP_PKEY *pkey) {
  448. return EC_KEY_is_opaque(pkey->pkey.ec);
  449. }
  450. static int old_ec_priv_decode(EVP_PKEY *pkey, const uint8_t **pder,
  451. int derlen) {
  452. EC_KEY *ec;
  453. if (!(ec = d2i_ECPrivateKey(NULL, pder, derlen))) {
  454. OPENSSL_PUT_ERROR(EVP, EVP_R_DECODE_ERROR);
  455. return 0;
  456. }
  457. EVP_PKEY_assign_EC_KEY(pkey, ec);
  458. return 1;
  459. }
  460. static int old_ec_priv_encode(const EVP_PKEY *pkey, uint8_t **pder) {
  461. return i2d_ECPrivateKey(pkey->pkey.ec, pder);
  462. }
  463. const EVP_PKEY_ASN1_METHOD ec_asn1_meth = {
  464. EVP_PKEY_EC,
  465. EVP_PKEY_EC,
  466. 0,
  467. "EC",
  468. eckey_pub_decode,
  469. eckey_pub_encode,
  470. eckey_pub_cmp,
  471. eckey_pub_print,
  472. eckey_priv_decode,
  473. eckey_priv_encode,
  474. eckey_priv_print,
  475. eckey_opaque,
  476. 0 /* pkey_supports_digest */,
  477. int_ec_size,
  478. ec_bits,
  479. eckey_param_decode,
  480. eckey_param_encode,
  481. ec_missing_parameters,
  482. ec_copy_parameters,
  483. ec_cmp_parameters,
  484. eckey_param_print,
  485. 0,
  486. int_ec_free,
  487. old_ec_priv_decode,
  488. old_ec_priv_encode,
  489. NULL /* digest_verify_init_from_algorithm */,
  490. NULL /* digest_sign_algorithm */,
  491. };