Nelze vybrat více než 25 témat Téma musí začínat písmenem nebo číslem, může obsahovat pomlčky („-“) a může být dlouhé až 35 znaků.
 
 
 
 
 
 

574 řádky
15 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, eckey_param2type, 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, eckey_param2type, 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, eckey_pub_encode, 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, eckey_type2param, 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, eckey_type2param, 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, eckey_pub_decode, 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, eckey_pub_decode, 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, eckey_priv_decode, 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, eckey_priv_decode, 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, eckey_priv_decode, 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, eckey_priv_decode, 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, eckey_priv_decode, 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, eckey_priv_decode, 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, eckey_priv_encode, 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, eckey_priv_encode, 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, eckey_priv_encode, 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, eckey_priv_encode, 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. return 0;
  291. }
  292. return 1;
  293. }
  294. static int int_ec_size(const EVP_PKEY *pkey) {
  295. return ECDSA_size(pkey->pkey.ec);
  296. }
  297. static int ec_bits(const EVP_PKEY *pkey) {
  298. BIGNUM *order = BN_new();
  299. const EC_GROUP *group;
  300. int ret;
  301. if (!order) {
  302. ERR_clear_error();
  303. return 0;
  304. }
  305. group = EC_KEY_get0_group(pkey->pkey.ec);
  306. if (!EC_GROUP_get_order(group, order, NULL)) {
  307. ERR_clear_error();
  308. return 0;
  309. }
  310. ret = BN_num_bits(order);
  311. BN_free(order);
  312. return ret;
  313. }
  314. static int ec_missing_parameters(const EVP_PKEY *pkey) {
  315. return EC_KEY_get0_group(pkey->pkey.ec) == NULL;
  316. }
  317. static int ec_copy_parameters(EVP_PKEY *to, const EVP_PKEY *from) {
  318. EC_GROUP *group = EC_GROUP_dup(EC_KEY_get0_group(from->pkey.ec));
  319. if (group == NULL ||
  320. EC_KEY_set_group(to->pkey.ec, group) == 0) {
  321. return 0;
  322. }
  323. EC_GROUP_free(group);
  324. return 1;
  325. }
  326. static int ec_cmp_parameters(const EVP_PKEY *a, const EVP_PKEY *b) {
  327. const EC_GROUP *group_a = EC_KEY_get0_group(a->pkey.ec),
  328. *group_b = EC_KEY_get0_group(b->pkey.ec);
  329. if (EC_GROUP_cmp(group_a, group_b, NULL) != 0) {
  330. /* mismatch */
  331. return 0;
  332. }
  333. return 1;
  334. }
  335. static void int_ec_free(EVP_PKEY *pkey) { EC_KEY_free(pkey->pkey.ec); }
  336. static int do_EC_KEY_print(BIO *bp, const EC_KEY *x, int off, int ktype) {
  337. uint8_t *buffer = NULL;
  338. const char *ecstr;
  339. size_t buf_len = 0, i;
  340. int ret = 0, reason = ERR_R_BIO_LIB;
  341. BIGNUM *order = NULL;
  342. BN_CTX *ctx = NULL;
  343. const EC_GROUP *group;
  344. const EC_POINT *public_key;
  345. const BIGNUM *priv_key;
  346. uint8_t *pub_key_bytes = NULL;
  347. size_t pub_key_bytes_len = 0;
  348. if (x == NULL || (group = EC_KEY_get0_group(x)) == NULL) {
  349. reason = ERR_R_PASSED_NULL_PARAMETER;
  350. goto err;
  351. }
  352. ctx = BN_CTX_new();
  353. if (ctx == NULL) {
  354. reason = ERR_R_MALLOC_FAILURE;
  355. goto err;
  356. }
  357. if (ktype > 0) {
  358. public_key = EC_KEY_get0_public_key(x);
  359. if (public_key != NULL) {
  360. pub_key_bytes_len = EC_POINT_point2oct(
  361. group, public_key, EC_KEY_get_conv_form(x), NULL, 0, ctx);
  362. if (pub_key_bytes_len == 0) {
  363. reason = ERR_R_MALLOC_FAILURE;
  364. goto err;
  365. }
  366. pub_key_bytes = OPENSSL_malloc(pub_key_bytes_len);
  367. if (pub_key_bytes == NULL) {
  368. reason = ERR_R_MALLOC_FAILURE;
  369. goto err;
  370. }
  371. pub_key_bytes_len =
  372. EC_POINT_point2oct(group, public_key, EC_KEY_get_conv_form(x),
  373. pub_key_bytes, pub_key_bytes_len, ctx);
  374. if (pub_key_bytes_len == 0) {
  375. reason = ERR_R_MALLOC_FAILURE;
  376. goto err;
  377. }
  378. buf_len = pub_key_bytes_len;
  379. }
  380. }
  381. if (ktype == 2) {
  382. priv_key = EC_KEY_get0_private_key(x);
  383. if (priv_key && (i = (size_t)BN_num_bytes(priv_key)) > buf_len) {
  384. buf_len = i;
  385. }
  386. } else {
  387. priv_key = NULL;
  388. }
  389. if (ktype > 0) {
  390. buf_len += 10;
  391. if ((buffer = OPENSSL_malloc(buf_len)) == NULL) {
  392. reason = ERR_R_MALLOC_FAILURE;
  393. goto err;
  394. }
  395. }
  396. if (ktype == 2) {
  397. ecstr = "Private-Key";
  398. } else if (ktype == 1) {
  399. ecstr = "Public-Key";
  400. } else {
  401. ecstr = "ECDSA-Parameters";
  402. }
  403. if (!BIO_indent(bp, off, 128)) {
  404. goto err;
  405. }
  406. order = BN_new();
  407. if (order == NULL || !EC_GROUP_get_order(group, order, NULL) ||
  408. BIO_printf(bp, "%s: (%d bit)\n", ecstr, BN_num_bits(order)) <= 0) {
  409. goto err;
  410. }
  411. if ((priv_key != NULL) &&
  412. !ASN1_bn_print(bp, "priv:", priv_key, buffer, off)) {
  413. goto err;
  414. }
  415. if (pub_key_bytes != NULL) {
  416. BIO_hexdump(bp, pub_key_bytes, pub_key_bytes_len, off);
  417. }
  418. /* TODO(fork): implement */
  419. /*
  420. if (!ECPKParameters_print(bp, group, off))
  421. goto err; */
  422. ret = 1;
  423. err:
  424. if (!ret) {
  425. OPENSSL_PUT_ERROR(EVP, do_EC_KEY_print, reason);
  426. }
  427. OPENSSL_free(pub_key_bytes);
  428. BN_free(order);
  429. BN_CTX_free(ctx);
  430. OPENSSL_free(buffer);
  431. return ret;
  432. }
  433. static int eckey_param_decode(EVP_PKEY *pkey, const uint8_t **pder,
  434. int derlen) {
  435. EC_KEY *eckey;
  436. if (!(eckey = d2i_ECParameters(NULL, pder, derlen))) {
  437. OPENSSL_PUT_ERROR(EVP, eckey_param_decode, ERR_R_EC_LIB);
  438. return 0;
  439. }
  440. EVP_PKEY_assign_EC_KEY(pkey, eckey);
  441. return 1;
  442. }
  443. static int eckey_param_encode(const EVP_PKEY *pkey, uint8_t **pder) {
  444. return i2d_ECParameters(pkey->pkey.ec, pder);
  445. }
  446. static int eckey_param_print(BIO *bp, const EVP_PKEY *pkey, int indent,
  447. ASN1_PCTX *ctx) {
  448. return do_EC_KEY_print(bp, pkey->pkey.ec, indent, 0);
  449. }
  450. static int eckey_pub_print(BIO *bp, const EVP_PKEY *pkey, int indent,
  451. ASN1_PCTX *ctx) {
  452. return do_EC_KEY_print(bp, pkey->pkey.ec, indent, 1);
  453. }
  454. static int eckey_priv_print(BIO *bp, const EVP_PKEY *pkey, int indent,
  455. ASN1_PCTX *ctx) {
  456. return do_EC_KEY_print(bp, pkey->pkey.ec, indent, 2);
  457. }
  458. static int eckey_opaque(const EVP_PKEY *pkey) {
  459. return EC_KEY_is_opaque(pkey->pkey.ec);
  460. }
  461. static int old_ec_priv_decode(EVP_PKEY *pkey, const uint8_t **pder,
  462. int derlen) {
  463. EC_KEY *ec;
  464. if (!(ec = d2i_ECPrivateKey(NULL, pder, derlen))) {
  465. OPENSSL_PUT_ERROR(EVP, old_ec_priv_decode, EVP_R_DECODE_ERROR);
  466. return 0;
  467. }
  468. EVP_PKEY_assign_EC_KEY(pkey, ec);
  469. return 1;
  470. }
  471. static int old_ec_priv_encode(const EVP_PKEY *pkey, uint8_t **pder) {
  472. return i2d_ECPrivateKey(pkey->pkey.ec, pder);
  473. }
  474. const EVP_PKEY_ASN1_METHOD ec_asn1_meth = {
  475. EVP_PKEY_EC,
  476. EVP_PKEY_EC,
  477. 0,
  478. "EC",
  479. "OpenSSL EC algorithm",
  480. eckey_pub_decode,
  481. eckey_pub_encode,
  482. eckey_pub_cmp,
  483. eckey_pub_print,
  484. eckey_priv_decode,
  485. eckey_priv_encode,
  486. eckey_priv_print,
  487. eckey_opaque,
  488. 0 /* pkey_supports_digest */,
  489. int_ec_size,
  490. ec_bits,
  491. eckey_param_decode,
  492. eckey_param_encode,
  493. ec_missing_parameters,
  494. ec_copy_parameters,
  495. ec_cmp_parameters,
  496. eckey_param_print,
  497. 0,
  498. int_ec_free,
  499. old_ec_priv_decode,
  500. old_ec_priv_encode
  501. };