PageRenderTime 65ms CodeModel.GetById 28ms RepoModel.GetById 0ms app.codeStats 1ms

/libressl/crypto/ec/ec_asn1.c

https://bitbucket.org/jtl/nginx
C | 1620 lines | 1344 code | 145 blank | 131 comment | 354 complexity | 2bbdbf18b742bfaf7618425e4b45f0cf MD5 | raw file
Possible License(s): LGPL-2.0, BSD-3-Clause
  1. /* $OpenBSD: ec_asn1.c,v 1.25 2018/03/12 13:14:21 inoguchi Exp $ */
  2. /*
  3. * Written by Nils Larsch for the OpenSSL project.
  4. */
  5. /* ====================================================================
  6. * Copyright (c) 2000-2003 The OpenSSL Project. All rights reserved.
  7. *
  8. * Redistribution and use in source and binary forms, with or without
  9. * modification, are permitted provided that the following conditions
  10. * are met:
  11. *
  12. * 1. Redistributions of source code must retain the above copyright
  13. * notice, this list of conditions and the following disclaimer.
  14. *
  15. * 2. Redistributions in binary form must reproduce the above copyright
  16. * notice, this list of conditions and the following disclaimer in
  17. * the documentation and/or other materials provided with the
  18. * distribution.
  19. *
  20. * 3. All advertising materials mentioning features or use of this
  21. * software must display the following acknowledgment:
  22. * "This product includes software developed by the OpenSSL Project
  23. * for use in the OpenSSL Toolkit. (http://www.OpenSSL.org/)"
  24. *
  25. * 4. The names "OpenSSL Toolkit" and "OpenSSL Project" must not be used to
  26. * endorse or promote products derived from this software without
  27. * prior written permission. For written permission, please contact
  28. * licensing@OpenSSL.org.
  29. *
  30. * 5. Products derived from this software may not be called "OpenSSL"
  31. * nor may "OpenSSL" appear in their names without prior written
  32. * permission of the OpenSSL Project.
  33. *
  34. * 6. Redistributions of any form whatsoever must retain the following
  35. * acknowledgment:
  36. * "This product includes software developed by the OpenSSL Project
  37. * for use in the OpenSSL Toolkit (http://www.OpenSSL.org/)"
  38. *
  39. * THIS SOFTWARE IS PROVIDED BY THE OpenSSL PROJECT ``AS IS'' AND ANY
  40. * EXPRESSED OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
  41. * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
  42. * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE OpenSSL PROJECT OR
  43. * ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
  44. * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
  45. * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
  46. * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
  47. * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
  48. * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
  49. * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED
  50. * OF THE POSSIBILITY OF SUCH DAMAGE.
  51. * ====================================================================
  52. *
  53. * This product includes cryptographic software written by Eric Young
  54. * (eay@cryptsoft.com). This product includes software written by Tim
  55. * Hudson (tjh@cryptsoft.com).
  56. *
  57. */
  58. #include <string.h>
  59. #include <openssl/opensslconf.h>
  60. #include "ec_lcl.h"
  61. #include <openssl/err.h>
  62. #include <openssl/asn1t.h>
  63. #include <openssl/objects.h>
  64. int
  65. EC_GROUP_get_basis_type(const EC_GROUP * group)
  66. {
  67. int i = 0;
  68. if (EC_METHOD_get_field_type(EC_GROUP_method_of(group)) !=
  69. NID_X9_62_characteristic_two_field)
  70. /* everything else is currently not supported */
  71. return 0;
  72. while (group->poly[i] != 0)
  73. i++;
  74. if (i == 4)
  75. return NID_X9_62_ppBasis;
  76. else if (i == 2)
  77. return NID_X9_62_tpBasis;
  78. else
  79. /* everything else is currently not supported */
  80. return 0;
  81. }
  82. #ifndef OPENSSL_NO_EC2M
  83. int
  84. EC_GROUP_get_trinomial_basis(const EC_GROUP * group, unsigned int *k)
  85. {
  86. if (group == NULL)
  87. return 0;
  88. if (EC_METHOD_get_field_type(EC_GROUP_method_of(group)) !=
  89. NID_X9_62_characteristic_two_field
  90. || !((group->poly[0] != 0) && (group->poly[1] != 0) && (group->poly[2] == 0))) {
  91. ECerror(ERR_R_SHOULD_NOT_HAVE_BEEN_CALLED);
  92. return 0;
  93. }
  94. if (k)
  95. *k = group->poly[1];
  96. return 1;
  97. }
  98. int
  99. EC_GROUP_get_pentanomial_basis(const EC_GROUP * group, unsigned int *k1,
  100. unsigned int *k2, unsigned int *k3)
  101. {
  102. if (group == NULL)
  103. return 0;
  104. if (EC_METHOD_get_field_type(EC_GROUP_method_of(group)) !=
  105. NID_X9_62_characteristic_two_field
  106. || !((group->poly[0] != 0) && (group->poly[1] != 0) && (group->poly[2] != 0) && (group->poly[3] != 0) && (group->poly[4] == 0))) {
  107. ECerror(ERR_R_SHOULD_NOT_HAVE_BEEN_CALLED);
  108. return 0;
  109. }
  110. if (k1)
  111. *k1 = group->poly[3];
  112. if (k2)
  113. *k2 = group->poly[2];
  114. if (k3)
  115. *k3 = group->poly[1];
  116. return 1;
  117. }
  118. #endif
  119. /* some structures needed for the asn1 encoding */
  120. typedef struct x9_62_pentanomial_st {
  121. long k1;
  122. long k2;
  123. long k3;
  124. } X9_62_PENTANOMIAL;
  125. typedef struct x9_62_characteristic_two_st {
  126. long m;
  127. ASN1_OBJECT *type;
  128. union {
  129. char *ptr;
  130. /* NID_X9_62_onBasis */
  131. ASN1_NULL *onBasis;
  132. /* NID_X9_62_tpBasis */
  133. ASN1_INTEGER *tpBasis;
  134. /* NID_X9_62_ppBasis */
  135. X9_62_PENTANOMIAL *ppBasis;
  136. /* anything else */
  137. ASN1_TYPE *other;
  138. } p;
  139. } X9_62_CHARACTERISTIC_TWO;
  140. typedef struct x9_62_fieldid_st {
  141. ASN1_OBJECT *fieldType;
  142. union {
  143. char *ptr;
  144. /* NID_X9_62_prime_field */
  145. ASN1_INTEGER *prime;
  146. /* NID_X9_62_characteristic_two_field */
  147. X9_62_CHARACTERISTIC_TWO *char_two;
  148. /* anything else */
  149. ASN1_TYPE *other;
  150. } p;
  151. } X9_62_FIELDID;
  152. typedef struct x9_62_curve_st {
  153. ASN1_OCTET_STRING *a;
  154. ASN1_OCTET_STRING *b;
  155. ASN1_BIT_STRING *seed;
  156. } X9_62_CURVE;
  157. typedef struct ec_parameters_st {
  158. long version;
  159. X9_62_FIELDID *fieldID;
  160. X9_62_CURVE *curve;
  161. ASN1_OCTET_STRING *base;
  162. ASN1_INTEGER *order;
  163. ASN1_INTEGER *cofactor;
  164. } ECPARAMETERS;
  165. struct ecpk_parameters_st {
  166. int type;
  167. union {
  168. ASN1_OBJECT *named_curve;
  169. ECPARAMETERS *parameters;
  170. ASN1_NULL *implicitlyCA;
  171. } value;
  172. } /* ECPKPARAMETERS */ ;
  173. /* SEC1 ECPrivateKey */
  174. typedef struct ec_privatekey_st {
  175. long version;
  176. ASN1_OCTET_STRING *privateKey;
  177. ECPKPARAMETERS *parameters;
  178. ASN1_BIT_STRING *publicKey;
  179. } EC_PRIVATEKEY;
  180. /* the OpenSSL ASN.1 definitions */
  181. static const ASN1_TEMPLATE X9_62_PENTANOMIAL_seq_tt[] = {
  182. {
  183. .flags = 0,
  184. .tag = 0,
  185. .offset = offsetof(X9_62_PENTANOMIAL, k1),
  186. .field_name = "k1",
  187. .item = &LONG_it,
  188. },
  189. {
  190. .flags = 0,
  191. .tag = 0,
  192. .offset = offsetof(X9_62_PENTANOMIAL, k2),
  193. .field_name = "k2",
  194. .item = &LONG_it,
  195. },
  196. {
  197. .flags = 0,
  198. .tag = 0,
  199. .offset = offsetof(X9_62_PENTANOMIAL, k3),
  200. .field_name = "k3",
  201. .item = &LONG_it,
  202. },
  203. };
  204. const ASN1_ITEM X9_62_PENTANOMIAL_it = {
  205. .itype = ASN1_ITYPE_SEQUENCE,
  206. .utype = V_ASN1_SEQUENCE,
  207. .templates = X9_62_PENTANOMIAL_seq_tt,
  208. .tcount = sizeof(X9_62_PENTANOMIAL_seq_tt) / sizeof(ASN1_TEMPLATE),
  209. .funcs = NULL,
  210. .size = sizeof(X9_62_PENTANOMIAL),
  211. .sname = "X9_62_PENTANOMIAL",
  212. };
  213. X9_62_PENTANOMIAL *X9_62_PENTANOMIAL_new(void);
  214. void X9_62_PENTANOMIAL_free(X9_62_PENTANOMIAL *a);
  215. X9_62_PENTANOMIAL *
  216. X9_62_PENTANOMIAL_new(void)
  217. {
  218. return (X9_62_PENTANOMIAL*)ASN1_item_new(&X9_62_PENTANOMIAL_it);
  219. }
  220. void
  221. X9_62_PENTANOMIAL_free(X9_62_PENTANOMIAL *a)
  222. {
  223. ASN1_item_free((ASN1_VALUE *)a, &X9_62_PENTANOMIAL_it);
  224. }
  225. static const ASN1_TEMPLATE char_two_def_tt = {
  226. .flags = 0,
  227. .tag = 0,
  228. .offset = offsetof(X9_62_CHARACTERISTIC_TWO, p.other),
  229. .field_name = "p.other",
  230. .item = &ASN1_ANY_it,
  231. };
  232. static const ASN1_ADB_TABLE X9_62_CHARACTERISTIC_TWO_adbtbl[] = {
  233. {
  234. .value = NID_X9_62_onBasis,
  235. .tt = {
  236. .flags = 0,
  237. .tag = 0,
  238. .offset = offsetof(X9_62_CHARACTERISTIC_TWO, p.onBasis),
  239. .field_name = "p.onBasis",
  240. .item = &ASN1_NULL_it,
  241. },
  242. },
  243. {
  244. .value = NID_X9_62_tpBasis,
  245. .tt = {
  246. .flags = 0,
  247. .tag = 0,
  248. .offset = offsetof(X9_62_CHARACTERISTIC_TWO, p.tpBasis),
  249. .field_name = "p.tpBasis",
  250. .item = &ASN1_INTEGER_it,
  251. },
  252. },
  253. {
  254. .value = NID_X9_62_ppBasis,
  255. .tt = {
  256. .flags = 0,
  257. .tag = 0,
  258. .offset = offsetof(X9_62_CHARACTERISTIC_TWO, p.ppBasis),
  259. .field_name = "p.ppBasis",
  260. .item = &X9_62_PENTANOMIAL_it,
  261. },
  262. },
  263. };
  264. static const ASN1_ADB X9_62_CHARACTERISTIC_TWO_adb = {
  265. .flags = 0,
  266. .offset = offsetof(X9_62_CHARACTERISTIC_TWO, type),
  267. .app_items = 0,
  268. .tbl = X9_62_CHARACTERISTIC_TWO_adbtbl,
  269. .tblcount = sizeof(X9_62_CHARACTERISTIC_TWO_adbtbl) / sizeof(ASN1_ADB_TABLE),
  270. .default_tt = &char_two_def_tt,
  271. .null_tt = NULL,
  272. };
  273. static const ASN1_TEMPLATE X9_62_CHARACTERISTIC_TWO_seq_tt[] = {
  274. {
  275. .flags = 0,
  276. .tag = 0,
  277. .offset = offsetof(X9_62_CHARACTERISTIC_TWO, m),
  278. .field_name = "m",
  279. .item = &LONG_it,
  280. },
  281. {
  282. .flags = 0,
  283. .tag = 0,
  284. .offset = offsetof(X9_62_CHARACTERISTIC_TWO, type),
  285. .field_name = "type",
  286. .item = &ASN1_OBJECT_it,
  287. },
  288. {
  289. .flags = ASN1_TFLG_ADB_OID,
  290. .tag = -1,
  291. .offset = 0,
  292. .field_name = "X9_62_CHARACTERISTIC_TWO",
  293. .item = (const ASN1_ITEM *)&X9_62_CHARACTERISTIC_TWO_adb,
  294. },
  295. };
  296. const ASN1_ITEM X9_62_CHARACTERISTIC_TWO_it = {
  297. .itype = ASN1_ITYPE_SEQUENCE,
  298. .utype = V_ASN1_SEQUENCE,
  299. .templates = X9_62_CHARACTERISTIC_TWO_seq_tt,
  300. .tcount = sizeof(X9_62_CHARACTERISTIC_TWO_seq_tt) / sizeof(ASN1_TEMPLATE),
  301. .funcs = NULL,
  302. .size = sizeof(X9_62_CHARACTERISTIC_TWO),
  303. .sname = "X9_62_CHARACTERISTIC_TWO",
  304. };
  305. X9_62_CHARACTERISTIC_TWO *X9_62_CHARACTERISTIC_TWO_new(void);
  306. void X9_62_CHARACTERISTIC_TWO_free(X9_62_CHARACTERISTIC_TWO *a);
  307. X9_62_CHARACTERISTIC_TWO *
  308. X9_62_CHARACTERISTIC_TWO_new(void)
  309. {
  310. return (X9_62_CHARACTERISTIC_TWO*)ASN1_item_new(&X9_62_CHARACTERISTIC_TWO_it);
  311. }
  312. void
  313. X9_62_CHARACTERISTIC_TWO_free(X9_62_CHARACTERISTIC_TWO *a)
  314. {
  315. ASN1_item_free((ASN1_VALUE *)a, &X9_62_CHARACTERISTIC_TWO_it);
  316. }
  317. static const ASN1_TEMPLATE fieldID_def_tt = {
  318. .flags = 0,
  319. .tag = 0,
  320. .offset = offsetof(X9_62_FIELDID, p.other),
  321. .field_name = "p.other",
  322. .item = &ASN1_ANY_it,
  323. };
  324. static const ASN1_ADB_TABLE X9_62_FIELDID_adbtbl[] = {
  325. {
  326. .value = NID_X9_62_prime_field,
  327. .tt = {
  328. .flags = 0,
  329. .tag = 0,
  330. .offset = offsetof(X9_62_FIELDID, p.prime),
  331. .field_name = "p.prime",
  332. .item = &ASN1_INTEGER_it,
  333. },
  334. },
  335. {
  336. .value = NID_X9_62_characteristic_two_field,
  337. .tt = {
  338. .flags = 0,
  339. .tag = 0,
  340. .offset = offsetof(X9_62_FIELDID, p.char_two),
  341. .field_name = "p.char_two",
  342. .item = &X9_62_CHARACTERISTIC_TWO_it,
  343. },
  344. },
  345. };
  346. static const ASN1_ADB X9_62_FIELDID_adb = {
  347. .flags = 0,
  348. .offset = offsetof(X9_62_FIELDID, fieldType),
  349. .app_items = 0,
  350. .tbl = X9_62_FIELDID_adbtbl,
  351. .tblcount = sizeof(X9_62_FIELDID_adbtbl) / sizeof(ASN1_ADB_TABLE),
  352. .default_tt = &fieldID_def_tt,
  353. .null_tt = NULL,
  354. };
  355. static const ASN1_TEMPLATE X9_62_FIELDID_seq_tt[] = {
  356. {
  357. .flags = 0,
  358. .tag = 0,
  359. .offset = offsetof(X9_62_FIELDID, fieldType),
  360. .field_name = "fieldType",
  361. .item = &ASN1_OBJECT_it,
  362. },
  363. {
  364. .flags = ASN1_TFLG_ADB_OID,
  365. .tag = -1,
  366. .offset = 0,
  367. .field_name = "X9_62_FIELDID",
  368. .item = (const ASN1_ITEM *)&X9_62_FIELDID_adb,
  369. },
  370. };
  371. const ASN1_ITEM X9_62_FIELDID_it = {
  372. .itype = ASN1_ITYPE_SEQUENCE,
  373. .utype = V_ASN1_SEQUENCE,
  374. .templates = X9_62_FIELDID_seq_tt,
  375. .tcount = sizeof(X9_62_FIELDID_seq_tt) / sizeof(ASN1_TEMPLATE),
  376. .funcs = NULL,
  377. .size = sizeof(X9_62_FIELDID),
  378. .sname = "X9_62_FIELDID",
  379. };
  380. static const ASN1_TEMPLATE X9_62_CURVE_seq_tt[] = {
  381. {
  382. .flags = 0,
  383. .tag = 0,
  384. .offset = offsetof(X9_62_CURVE, a),
  385. .field_name = "a",
  386. .item = &ASN1_OCTET_STRING_it,
  387. },
  388. {
  389. .flags = 0,
  390. .tag = 0,
  391. .offset = offsetof(X9_62_CURVE, b),
  392. .field_name = "b",
  393. .item = &ASN1_OCTET_STRING_it,
  394. },
  395. {
  396. .flags = ASN1_TFLG_OPTIONAL,
  397. .tag = 0,
  398. .offset = offsetof(X9_62_CURVE, seed),
  399. .field_name = "seed",
  400. .item = &ASN1_BIT_STRING_it,
  401. },
  402. };
  403. const ASN1_ITEM X9_62_CURVE_it = {
  404. .itype = ASN1_ITYPE_SEQUENCE,
  405. .utype = V_ASN1_SEQUENCE,
  406. .templates = X9_62_CURVE_seq_tt,
  407. .tcount = sizeof(X9_62_CURVE_seq_tt) / sizeof(ASN1_TEMPLATE),
  408. .funcs = NULL,
  409. .size = sizeof(X9_62_CURVE),
  410. .sname = "X9_62_CURVE",
  411. };
  412. static const ASN1_TEMPLATE ECPARAMETERS_seq_tt[] = {
  413. {
  414. .flags = 0,
  415. .tag = 0,
  416. .offset = offsetof(ECPARAMETERS, version),
  417. .field_name = "version",
  418. .item = &LONG_it,
  419. },
  420. {
  421. .flags = 0,
  422. .tag = 0,
  423. .offset = offsetof(ECPARAMETERS, fieldID),
  424. .field_name = "fieldID",
  425. .item = &X9_62_FIELDID_it,
  426. },
  427. {
  428. .flags = 0,
  429. .tag = 0,
  430. .offset = offsetof(ECPARAMETERS, curve),
  431. .field_name = "curve",
  432. .item = &X9_62_CURVE_it,
  433. },
  434. {
  435. .flags = 0,
  436. .tag = 0,
  437. .offset = offsetof(ECPARAMETERS, base),
  438. .field_name = "base",
  439. .item = &ASN1_OCTET_STRING_it,
  440. },
  441. {
  442. .flags = 0,
  443. .tag = 0,
  444. .offset = offsetof(ECPARAMETERS, order),
  445. .field_name = "order",
  446. .item = &ASN1_INTEGER_it,
  447. },
  448. {
  449. .flags = ASN1_TFLG_OPTIONAL,
  450. .tag = 0,
  451. .offset = offsetof(ECPARAMETERS, cofactor),
  452. .field_name = "cofactor",
  453. .item = &ASN1_INTEGER_it,
  454. },
  455. };
  456. const ASN1_ITEM ECPARAMETERS_it = {
  457. .itype = ASN1_ITYPE_SEQUENCE,
  458. .utype = V_ASN1_SEQUENCE,
  459. .templates = ECPARAMETERS_seq_tt,
  460. .tcount = sizeof(ECPARAMETERS_seq_tt) / sizeof(ASN1_TEMPLATE),
  461. .funcs = NULL,
  462. .size = sizeof(ECPARAMETERS),
  463. .sname = "ECPARAMETERS",
  464. };
  465. ECPARAMETERS *ECPARAMETERS_new(void);
  466. void ECPARAMETERS_free(ECPARAMETERS *a);
  467. ECPARAMETERS *
  468. ECPARAMETERS_new(void)
  469. {
  470. return (ECPARAMETERS*)ASN1_item_new(&ECPARAMETERS_it);
  471. }
  472. void
  473. ECPARAMETERS_free(ECPARAMETERS *a)
  474. {
  475. ASN1_item_free((ASN1_VALUE *)a, &ECPARAMETERS_it);
  476. }
  477. static const ASN1_TEMPLATE ECPKPARAMETERS_ch_tt[] = {
  478. {
  479. .flags = 0,
  480. .tag = 0,
  481. .offset = offsetof(ECPKPARAMETERS, value.named_curve),
  482. .field_name = "value.named_curve",
  483. .item = &ASN1_OBJECT_it,
  484. },
  485. {
  486. .flags = 0,
  487. .tag = 0,
  488. .offset = offsetof(ECPKPARAMETERS, value.parameters),
  489. .field_name = "value.parameters",
  490. .item = &ECPARAMETERS_it,
  491. },
  492. {
  493. .flags = 0,
  494. .tag = 0,
  495. .offset = offsetof(ECPKPARAMETERS, value.implicitlyCA),
  496. .field_name = "value.implicitlyCA",
  497. .item = &ASN1_NULL_it,
  498. },
  499. };
  500. const ASN1_ITEM ECPKPARAMETERS_it = {
  501. .itype = ASN1_ITYPE_CHOICE,
  502. .utype = offsetof(ECPKPARAMETERS, type),
  503. .templates = ECPKPARAMETERS_ch_tt,
  504. .tcount = sizeof(ECPKPARAMETERS_ch_tt) / sizeof(ASN1_TEMPLATE),
  505. .funcs = NULL,
  506. .size = sizeof(ECPKPARAMETERS),
  507. .sname = "ECPKPARAMETERS",
  508. };
  509. ECPKPARAMETERS *ECPKPARAMETERS_new(void);
  510. void ECPKPARAMETERS_free(ECPKPARAMETERS *a);
  511. ECPKPARAMETERS *d2i_ECPKPARAMETERS(ECPKPARAMETERS **a, const unsigned char **in, long len);
  512. int i2d_ECPKPARAMETERS(const ECPKPARAMETERS *a, unsigned char **out);
  513. ECPKPARAMETERS *
  514. d2i_ECPKPARAMETERS(ECPKPARAMETERS **a, const unsigned char **in, long len)
  515. {
  516. return (ECPKPARAMETERS *)ASN1_item_d2i((ASN1_VALUE **)a, in, len,
  517. &ECPKPARAMETERS_it);
  518. }
  519. int
  520. i2d_ECPKPARAMETERS(const ECPKPARAMETERS *a, unsigned char **out)
  521. {
  522. return ASN1_item_i2d((ASN1_VALUE *)a, out, &ECPKPARAMETERS_it);
  523. }
  524. ECPKPARAMETERS *
  525. ECPKPARAMETERS_new(void)
  526. {
  527. return (ECPKPARAMETERS *)ASN1_item_new(&ECPKPARAMETERS_it);
  528. }
  529. void
  530. ECPKPARAMETERS_free(ECPKPARAMETERS *a)
  531. {
  532. ASN1_item_free((ASN1_VALUE *)a, &ECPKPARAMETERS_it);
  533. }
  534. static const ASN1_TEMPLATE EC_PRIVATEKEY_seq_tt[] = {
  535. {
  536. .flags = 0,
  537. .tag = 0,
  538. .offset = offsetof(EC_PRIVATEKEY, version),
  539. .field_name = "version",
  540. .item = &LONG_it,
  541. },
  542. {
  543. .flags = 0,
  544. .tag = 0,
  545. .offset = offsetof(EC_PRIVATEKEY, privateKey),
  546. .field_name = "privateKey",
  547. .item = &ASN1_OCTET_STRING_it,
  548. },
  549. {
  550. .flags = ASN1_TFLG_EXPLICIT | ASN1_TFLG_OPTIONAL,
  551. .tag = 0,
  552. .offset = offsetof(EC_PRIVATEKEY, parameters),
  553. .field_name = "parameters",
  554. .item = &ECPKPARAMETERS_it,
  555. },
  556. {
  557. .flags = ASN1_TFLG_EXPLICIT | ASN1_TFLG_OPTIONAL,
  558. .tag = 1,
  559. .offset = offsetof(EC_PRIVATEKEY, publicKey),
  560. .field_name = "publicKey",
  561. .item = &ASN1_BIT_STRING_it,
  562. },
  563. };
  564. const ASN1_ITEM EC_PRIVATEKEY_it = {
  565. .itype = ASN1_ITYPE_SEQUENCE,
  566. .utype = V_ASN1_SEQUENCE,
  567. .templates = EC_PRIVATEKEY_seq_tt,
  568. .tcount = sizeof(EC_PRIVATEKEY_seq_tt) / sizeof(ASN1_TEMPLATE),
  569. .funcs = NULL,
  570. .size = sizeof(EC_PRIVATEKEY),
  571. .sname = "EC_PRIVATEKEY",
  572. };
  573. EC_PRIVATEKEY *EC_PRIVATEKEY_new(void);
  574. void EC_PRIVATEKEY_free(EC_PRIVATEKEY *a);
  575. EC_PRIVATEKEY *d2i_EC_PRIVATEKEY(EC_PRIVATEKEY **a, const unsigned char **in, long len);
  576. int i2d_EC_PRIVATEKEY(const EC_PRIVATEKEY *a, unsigned char **out);
  577. EC_PRIVATEKEY *
  578. d2i_EC_PRIVATEKEY(EC_PRIVATEKEY **a, const unsigned char **in, long len)
  579. {
  580. return (EC_PRIVATEKEY *)ASN1_item_d2i((ASN1_VALUE **)a, in, len,
  581. &EC_PRIVATEKEY_it);
  582. }
  583. int
  584. i2d_EC_PRIVATEKEY(const EC_PRIVATEKEY *a, unsigned char **out)
  585. {
  586. return ASN1_item_i2d((ASN1_VALUE *)a, out, &EC_PRIVATEKEY_it);
  587. }
  588. EC_PRIVATEKEY *
  589. EC_PRIVATEKEY_new(void)
  590. {
  591. return (EC_PRIVATEKEY *)ASN1_item_new(&EC_PRIVATEKEY_it);
  592. }
  593. void
  594. EC_PRIVATEKEY_free(EC_PRIVATEKEY *a)
  595. {
  596. ASN1_item_free((ASN1_VALUE *)a, &EC_PRIVATEKEY_it);
  597. }
  598. /* some declarations of internal function */
  599. /* ec_asn1_group2field() sets the values in a X9_62_FIELDID object */
  600. static int ec_asn1_group2fieldid(const EC_GROUP *, X9_62_FIELDID *);
  601. /* ec_asn1_group2curve() sets the values in a X9_62_CURVE object */
  602. static int ec_asn1_group2curve(const EC_GROUP *, X9_62_CURVE *);
  603. /* ec_asn1_parameters2group() creates a EC_GROUP object from a
  604. * ECPARAMETERS object */
  605. static EC_GROUP *ec_asn1_parameters2group(const ECPARAMETERS *);
  606. /* ec_asn1_group2parameters() creates a ECPARAMETERS object from a
  607. * EC_GROUP object */
  608. static ECPARAMETERS *ec_asn1_group2parameters(const EC_GROUP *, ECPARAMETERS *);
  609. /* ec_asn1_pkparameters2group() creates a EC_GROUP object from a
  610. * ECPKPARAMETERS object */
  611. static EC_GROUP *ec_asn1_pkparameters2group(const ECPKPARAMETERS *);
  612. /* ec_asn1_group2pkparameters() creates a ECPKPARAMETERS object from a
  613. * EC_GROUP object */
  614. static ECPKPARAMETERS *ec_asn1_group2pkparameters(const EC_GROUP *,
  615. ECPKPARAMETERS *);
  616. /* the function definitions */
  617. static int
  618. ec_asn1_group2fieldid(const EC_GROUP * group, X9_62_FIELDID * field)
  619. {
  620. int ok = 0, nid;
  621. BIGNUM *tmp = NULL;
  622. if (group == NULL || field == NULL)
  623. return 0;
  624. /* clear the old values (if necessary) */
  625. if (field->fieldType != NULL)
  626. ASN1_OBJECT_free(field->fieldType);
  627. if (field->p.other != NULL)
  628. ASN1_TYPE_free(field->p.other);
  629. nid = EC_METHOD_get_field_type(EC_GROUP_method_of(group));
  630. /* set OID for the field */
  631. if ((field->fieldType = OBJ_nid2obj(nid)) == NULL) {
  632. ECerror(ERR_R_OBJ_LIB);
  633. goto err;
  634. }
  635. if (nid == NID_X9_62_prime_field) {
  636. if ((tmp = BN_new()) == NULL) {
  637. ECerror(ERR_R_MALLOC_FAILURE);
  638. goto err;
  639. }
  640. /* the parameters are specified by the prime number p */
  641. if (!EC_GROUP_get_curve_GFp(group, tmp, NULL, NULL, NULL)) {
  642. ECerror(ERR_R_EC_LIB);
  643. goto err;
  644. }
  645. /* set the prime number */
  646. field->p.prime = BN_to_ASN1_INTEGER(tmp, NULL);
  647. if (field->p.prime == NULL) {
  648. ECerror(ERR_R_ASN1_LIB);
  649. goto err;
  650. }
  651. } else /* nid == NID_X9_62_characteristic_two_field */
  652. #ifdef OPENSSL_NO_EC2M
  653. {
  654. ECerror(EC_R_GF2M_NOT_SUPPORTED);
  655. goto err;
  656. }
  657. #else
  658. {
  659. int field_type;
  660. X9_62_CHARACTERISTIC_TWO *char_two;
  661. field->p.char_two = X9_62_CHARACTERISTIC_TWO_new();
  662. char_two = field->p.char_two;
  663. if (char_two == NULL) {
  664. ECerror(ERR_R_MALLOC_FAILURE);
  665. goto err;
  666. }
  667. char_two->m = (long) EC_GROUP_get_degree(group);
  668. field_type = EC_GROUP_get_basis_type(group);
  669. if (field_type == 0) {
  670. ECerror(ERR_R_EC_LIB);
  671. goto err;
  672. }
  673. /* set base type OID */
  674. if ((char_two->type = OBJ_nid2obj(field_type)) == NULL) {
  675. ECerror(ERR_R_OBJ_LIB);
  676. goto err;
  677. }
  678. if (field_type == NID_X9_62_tpBasis) {
  679. unsigned int k;
  680. if (!EC_GROUP_get_trinomial_basis(group, &k))
  681. goto err;
  682. char_two->p.tpBasis = ASN1_INTEGER_new();
  683. if (!char_two->p.tpBasis) {
  684. ECerror(ERR_R_MALLOC_FAILURE);
  685. goto err;
  686. }
  687. if (!ASN1_INTEGER_set(char_two->p.tpBasis, (long) k)) {
  688. ECerror(ERR_R_ASN1_LIB);
  689. goto err;
  690. }
  691. } else if (field_type == NID_X9_62_ppBasis) {
  692. unsigned int k1, k2, k3;
  693. if (!EC_GROUP_get_pentanomial_basis(group, &k1, &k2, &k3))
  694. goto err;
  695. char_two->p.ppBasis = X9_62_PENTANOMIAL_new();
  696. if (!char_two->p.ppBasis) {
  697. ECerror(ERR_R_MALLOC_FAILURE);
  698. goto err;
  699. }
  700. /* set k? values */
  701. char_two->p.ppBasis->k1 = (long) k1;
  702. char_two->p.ppBasis->k2 = (long) k2;
  703. char_two->p.ppBasis->k3 = (long) k3;
  704. } else { /* field_type == NID_X9_62_onBasis */
  705. /* for ONB the parameters are (asn1) NULL */
  706. char_two->p.onBasis = ASN1_NULL_new();
  707. if (!char_two->p.onBasis) {
  708. ECerror(ERR_R_MALLOC_FAILURE);
  709. goto err;
  710. }
  711. }
  712. }
  713. #endif
  714. ok = 1;
  715. err:
  716. BN_free(tmp);
  717. return (ok);
  718. }
  719. static int
  720. ec_asn1_group2curve(const EC_GROUP * group, X9_62_CURVE * curve)
  721. {
  722. int ok = 0, nid;
  723. BIGNUM *tmp_1 = NULL, *tmp_2 = NULL;
  724. unsigned char *buffer_1 = NULL, *buffer_2 = NULL, *a_buf = NULL,
  725. *b_buf = NULL;
  726. size_t len_1, len_2;
  727. unsigned char char_zero = 0;
  728. if (!group || !curve || !curve->a || !curve->b)
  729. return 0;
  730. if ((tmp_1 = BN_new()) == NULL || (tmp_2 = BN_new()) == NULL) {
  731. ECerror(ERR_R_MALLOC_FAILURE);
  732. goto err;
  733. }
  734. nid = EC_METHOD_get_field_type(EC_GROUP_method_of(group));
  735. /* get a and b */
  736. if (nid == NID_X9_62_prime_field) {
  737. if (!EC_GROUP_get_curve_GFp(group, NULL, tmp_1, tmp_2, NULL)) {
  738. ECerror(ERR_R_EC_LIB);
  739. goto err;
  740. }
  741. }
  742. #ifndef OPENSSL_NO_EC2M
  743. else { /* nid == NID_X9_62_characteristic_two_field */
  744. if (!EC_GROUP_get_curve_GF2m(group, NULL, tmp_1, tmp_2, NULL)) {
  745. ECerror(ERR_R_EC_LIB);
  746. goto err;
  747. }
  748. }
  749. #endif
  750. len_1 = (size_t) BN_num_bytes(tmp_1);
  751. len_2 = (size_t) BN_num_bytes(tmp_2);
  752. if (len_1 == 0) {
  753. /* len_1 == 0 => a == 0 */
  754. a_buf = &char_zero;
  755. len_1 = 1;
  756. } else {
  757. if ((buffer_1 = malloc(len_1)) == NULL) {
  758. ECerror(ERR_R_MALLOC_FAILURE);
  759. goto err;
  760. }
  761. if ((len_1 = BN_bn2bin(tmp_1, buffer_1)) == 0) {
  762. ECerror(ERR_R_BN_LIB);
  763. goto err;
  764. }
  765. a_buf = buffer_1;
  766. }
  767. if (len_2 == 0) {
  768. /* len_2 == 0 => b == 0 */
  769. b_buf = &char_zero;
  770. len_2 = 1;
  771. } else {
  772. if ((buffer_2 = malloc(len_2)) == NULL) {
  773. ECerror(ERR_R_MALLOC_FAILURE);
  774. goto err;
  775. }
  776. if ((len_2 = BN_bn2bin(tmp_2, buffer_2)) == 0) {
  777. ECerror(ERR_R_BN_LIB);
  778. goto err;
  779. }
  780. b_buf = buffer_2;
  781. }
  782. /* set a and b */
  783. if (!ASN1_STRING_set(curve->a, a_buf, len_1) ||
  784. !ASN1_STRING_set(curve->b, b_buf, len_2)) {
  785. ECerror(ERR_R_ASN1_LIB);
  786. goto err;
  787. }
  788. /* set the seed (optional) */
  789. if (group->seed) {
  790. if (!curve->seed)
  791. if ((curve->seed = ASN1_BIT_STRING_new()) == NULL) {
  792. ECerror(ERR_R_MALLOC_FAILURE);
  793. goto err;
  794. }
  795. curve->seed->flags &= ~(ASN1_STRING_FLAG_BITS_LEFT | 0x07);
  796. curve->seed->flags |= ASN1_STRING_FLAG_BITS_LEFT;
  797. if (!ASN1_BIT_STRING_set(curve->seed, group->seed,
  798. (int) group->seed_len)) {
  799. ECerror(ERR_R_ASN1_LIB);
  800. goto err;
  801. }
  802. } else {
  803. if (curve->seed) {
  804. ASN1_BIT_STRING_free(curve->seed);
  805. curve->seed = NULL;
  806. }
  807. }
  808. ok = 1;
  809. err:
  810. free(buffer_1);
  811. free(buffer_2);
  812. BN_free(tmp_1);
  813. BN_free(tmp_2);
  814. return (ok);
  815. }
  816. static ECPARAMETERS *
  817. ec_asn1_group2parameters(const EC_GROUP * group, ECPARAMETERS * param)
  818. {
  819. int ok = 0;
  820. size_t len = 0;
  821. ECPARAMETERS *ret = NULL;
  822. BIGNUM *tmp = NULL;
  823. unsigned char *buffer = NULL;
  824. const EC_POINT *point = NULL;
  825. point_conversion_form_t form;
  826. if ((tmp = BN_new()) == NULL) {
  827. ECerror(ERR_R_MALLOC_FAILURE);
  828. goto err;
  829. }
  830. if (param == NULL) {
  831. if ((ret = ECPARAMETERS_new()) == NULL) {
  832. ECerror(ERR_R_MALLOC_FAILURE);
  833. goto err;
  834. }
  835. } else
  836. ret = param;
  837. /* set the version (always one) */
  838. ret->version = (long) 0x1;
  839. /* set the fieldID */
  840. if (!ec_asn1_group2fieldid(group, ret->fieldID)) {
  841. ECerror(ERR_R_EC_LIB);
  842. goto err;
  843. }
  844. /* set the curve */
  845. if (!ec_asn1_group2curve(group, ret->curve)) {
  846. ECerror(ERR_R_EC_LIB);
  847. goto err;
  848. }
  849. /* set the base point */
  850. if ((point = EC_GROUP_get0_generator(group)) == NULL) {
  851. ECerror(EC_R_UNDEFINED_GENERATOR);
  852. goto err;
  853. }
  854. form = EC_GROUP_get_point_conversion_form(group);
  855. len = EC_POINT_point2oct(group, point, form, NULL, len, NULL);
  856. if (len == 0) {
  857. ECerror(ERR_R_EC_LIB);
  858. goto err;
  859. }
  860. if ((buffer = malloc(len)) == NULL) {
  861. ECerror(ERR_R_MALLOC_FAILURE);
  862. goto err;
  863. }
  864. if (!EC_POINT_point2oct(group, point, form, buffer, len, NULL)) {
  865. ECerror(ERR_R_EC_LIB);
  866. goto err;
  867. }
  868. if (ret->base == NULL && (ret->base = ASN1_OCTET_STRING_new()) == NULL) {
  869. ECerror(ERR_R_MALLOC_FAILURE);
  870. goto err;
  871. }
  872. if (!ASN1_OCTET_STRING_set(ret->base, buffer, len)) {
  873. ECerror(ERR_R_ASN1_LIB);
  874. goto err;
  875. }
  876. /* set the order */
  877. if (!EC_GROUP_get_order(group, tmp, NULL)) {
  878. ECerror(ERR_R_EC_LIB);
  879. goto err;
  880. }
  881. ret->order = BN_to_ASN1_INTEGER(tmp, ret->order);
  882. if (ret->order == NULL) {
  883. ECerror(ERR_R_ASN1_LIB);
  884. goto err;
  885. }
  886. /* set the cofactor (optional) */
  887. if (EC_GROUP_get_cofactor(group, tmp, NULL)) {
  888. ret->cofactor = BN_to_ASN1_INTEGER(tmp, ret->cofactor);
  889. if (ret->cofactor == NULL) {
  890. ECerror(ERR_R_ASN1_LIB);
  891. goto err;
  892. }
  893. }
  894. ok = 1;
  895. err: if (!ok) {
  896. if (ret && !param)
  897. ECPARAMETERS_free(ret);
  898. ret = NULL;
  899. }
  900. BN_free(tmp);
  901. free(buffer);
  902. return (ret);
  903. }
  904. ECPKPARAMETERS *
  905. ec_asn1_group2pkparameters(const EC_GROUP * group, ECPKPARAMETERS * params)
  906. {
  907. int ok = 1, tmp;
  908. ECPKPARAMETERS *ret = params;
  909. if (ret == NULL) {
  910. if ((ret = ECPKPARAMETERS_new()) == NULL) {
  911. ECerror(ERR_R_MALLOC_FAILURE);
  912. return NULL;
  913. }
  914. } else {
  915. if (ret->type == 0 && ret->value.named_curve)
  916. ASN1_OBJECT_free(ret->value.named_curve);
  917. else if (ret->type == 1 && ret->value.parameters)
  918. ECPARAMETERS_free(ret->value.parameters);
  919. }
  920. if (EC_GROUP_get_asn1_flag(group)) {
  921. /*
  922. * use the asn1 OID to describe the elliptic curve
  923. * parameters
  924. */
  925. tmp = EC_GROUP_get_curve_name(group);
  926. if (tmp) {
  927. ret->type = 0;
  928. if ((ret->value.named_curve = OBJ_nid2obj(tmp)) == NULL)
  929. ok = 0;
  930. } else
  931. /* we don't kmow the nid => ERROR */
  932. ok = 0;
  933. } else {
  934. /* use the ECPARAMETERS structure */
  935. ret->type = 1;
  936. if ((ret->value.parameters = ec_asn1_group2parameters(
  937. group, NULL)) == NULL)
  938. ok = 0;
  939. }
  940. if (!ok) {
  941. ECPKPARAMETERS_free(ret);
  942. return NULL;
  943. }
  944. return ret;
  945. }
  946. static EC_GROUP *
  947. ec_asn1_parameters2group(const ECPARAMETERS * params)
  948. {
  949. int ok = 0, tmp;
  950. EC_GROUP *ret = NULL;
  951. BIGNUM *p = NULL, *a = NULL, *b = NULL;
  952. EC_POINT *point = NULL;
  953. long field_bits;
  954. if (!params->fieldID || !params->fieldID->fieldType ||
  955. !params->fieldID->p.ptr) {
  956. ECerror(EC_R_ASN1_ERROR);
  957. goto err;
  958. }
  959. /* now extract the curve parameters a and b */
  960. if (!params->curve || !params->curve->a ||
  961. !params->curve->a->data || !params->curve->b ||
  962. !params->curve->b->data) {
  963. ECerror(EC_R_ASN1_ERROR);
  964. goto err;
  965. }
  966. a = BN_bin2bn(params->curve->a->data, params->curve->a->length, NULL);
  967. if (a == NULL) {
  968. ECerror(ERR_R_BN_LIB);
  969. goto err;
  970. }
  971. b = BN_bin2bn(params->curve->b->data, params->curve->b->length, NULL);
  972. if (b == NULL) {
  973. ECerror(ERR_R_BN_LIB);
  974. goto err;
  975. }
  976. /* get the field parameters */
  977. tmp = OBJ_obj2nid(params->fieldID->fieldType);
  978. if (tmp == NID_X9_62_characteristic_two_field)
  979. #ifdef OPENSSL_NO_EC2M
  980. {
  981. ECerror(EC_R_GF2M_NOT_SUPPORTED);
  982. goto err;
  983. }
  984. #else
  985. {
  986. X9_62_CHARACTERISTIC_TWO *char_two;
  987. char_two = params->fieldID->p.char_two;
  988. field_bits = char_two->m;
  989. if (field_bits > OPENSSL_ECC_MAX_FIELD_BITS) {
  990. ECerror(EC_R_FIELD_TOO_LARGE);
  991. goto err;
  992. }
  993. if ((p = BN_new()) == NULL) {
  994. ECerror(ERR_R_MALLOC_FAILURE);
  995. goto err;
  996. }
  997. /* get the base type */
  998. tmp = OBJ_obj2nid(char_two->type);
  999. if (tmp == NID_X9_62_tpBasis) {
  1000. long tmp_long;
  1001. if (!char_two->p.tpBasis) {
  1002. ECerror(EC_R_ASN1_ERROR);
  1003. goto err;
  1004. }
  1005. tmp_long = ASN1_INTEGER_get(char_two->p.tpBasis);
  1006. if (!(char_two->m > tmp_long && tmp_long > 0)) {
  1007. ECerror(EC_R_INVALID_TRINOMIAL_BASIS);
  1008. goto err;
  1009. }
  1010. /* create the polynomial */
  1011. if (!BN_set_bit(p, (int) char_two->m))
  1012. goto err;
  1013. if (!BN_set_bit(p, (int) tmp_long))
  1014. goto err;
  1015. if (!BN_set_bit(p, 0))
  1016. goto err;
  1017. } else if (tmp == NID_X9_62_ppBasis) {
  1018. X9_62_PENTANOMIAL *penta;
  1019. penta = char_two->p.ppBasis;
  1020. if (!penta) {
  1021. ECerror(EC_R_ASN1_ERROR);
  1022. goto err;
  1023. }
  1024. if (!(char_two->m > penta->k3 && penta->k3 > penta->k2 && penta->k2 > penta->k1 && penta->k1 > 0)) {
  1025. ECerror(EC_R_INVALID_PENTANOMIAL_BASIS);
  1026. goto err;
  1027. }
  1028. /* create the polynomial */
  1029. if (!BN_set_bit(p, (int) char_two->m))
  1030. goto err;
  1031. if (!BN_set_bit(p, (int) penta->k1))
  1032. goto err;
  1033. if (!BN_set_bit(p, (int) penta->k2))
  1034. goto err;
  1035. if (!BN_set_bit(p, (int) penta->k3))
  1036. goto err;
  1037. if (!BN_set_bit(p, 0))
  1038. goto err;
  1039. } else if (tmp == NID_X9_62_onBasis) {
  1040. ECerror(EC_R_NOT_IMPLEMENTED);
  1041. goto err;
  1042. } else { /* error */
  1043. ECerror(EC_R_ASN1_ERROR);
  1044. goto err;
  1045. }
  1046. /* create the EC_GROUP structure */
  1047. ret = EC_GROUP_new_curve_GF2m(p, a, b, NULL);
  1048. }
  1049. #endif
  1050. else if (tmp == NID_X9_62_prime_field) {
  1051. /* we have a curve over a prime field */
  1052. /* extract the prime number */
  1053. if (!params->fieldID->p.prime) {
  1054. ECerror(EC_R_ASN1_ERROR);
  1055. goto err;
  1056. }
  1057. p = ASN1_INTEGER_to_BN(params->fieldID->p.prime, NULL);
  1058. if (p == NULL) {
  1059. ECerror(ERR_R_ASN1_LIB);
  1060. goto err;
  1061. }
  1062. if (BN_is_negative(p) || BN_is_zero(p)) {
  1063. ECerror(EC_R_INVALID_FIELD);
  1064. goto err;
  1065. }
  1066. field_bits = BN_num_bits(p);
  1067. if (field_bits > OPENSSL_ECC_MAX_FIELD_BITS) {
  1068. ECerror(EC_R_FIELD_TOO_LARGE);
  1069. goto err;
  1070. }
  1071. /* create the EC_GROUP structure */
  1072. ret = EC_GROUP_new_curve_GFp(p, a, b, NULL);
  1073. } else {
  1074. ECerror(EC_R_INVALID_FIELD);
  1075. goto err;
  1076. }
  1077. if (ret == NULL) {
  1078. ECerror(ERR_R_EC_LIB);
  1079. goto err;
  1080. }
  1081. /* extract seed (optional) */
  1082. if (params->curve->seed != NULL) {
  1083. free(ret->seed);
  1084. if (!(ret->seed = malloc(params->curve->seed->length))) {
  1085. ECerror(ERR_R_MALLOC_FAILURE);
  1086. goto err;
  1087. }
  1088. memcpy(ret->seed, params->curve->seed->data,
  1089. params->curve->seed->length);
  1090. ret->seed_len = params->curve->seed->length;
  1091. }
  1092. if (!params->order || !params->base || !params->base->data) {
  1093. ECerror(EC_R_ASN1_ERROR);
  1094. goto err;
  1095. }
  1096. if ((point = EC_POINT_new(ret)) == NULL)
  1097. goto err;
  1098. /* set the point conversion form */
  1099. EC_GROUP_set_point_conversion_form(ret, (point_conversion_form_t)
  1100. (params->base->data[0] & ~0x01));
  1101. /* extract the ec point */
  1102. if (!EC_POINT_oct2point(ret, point, params->base->data,
  1103. params->base->length, NULL)) {
  1104. ECerror(ERR_R_EC_LIB);
  1105. goto err;
  1106. }
  1107. /* extract the order */
  1108. if ((a = ASN1_INTEGER_to_BN(params->order, a)) == NULL) {
  1109. ECerror(ERR_R_ASN1_LIB);
  1110. goto err;
  1111. }
  1112. if (BN_is_negative(a) || BN_is_zero(a)) {
  1113. ECerror(EC_R_INVALID_GROUP_ORDER);
  1114. goto err;
  1115. }
  1116. if (BN_num_bits(a) > (int) field_bits + 1) { /* Hasse bound */
  1117. ECerror(EC_R_INVALID_GROUP_ORDER);
  1118. goto err;
  1119. }
  1120. /* extract the cofactor (optional) */
  1121. if (params->cofactor == NULL) {
  1122. BN_free(b);
  1123. b = NULL;
  1124. } else if ((b = ASN1_INTEGER_to_BN(params->cofactor, b)) == NULL) {
  1125. ECerror(ERR_R_ASN1_LIB);
  1126. goto err;
  1127. }
  1128. /* set the generator, order and cofactor (if present) */
  1129. if (!EC_GROUP_set_generator(ret, point, a, b)) {
  1130. ECerror(ERR_R_EC_LIB);
  1131. goto err;
  1132. }
  1133. ok = 1;
  1134. err: if (!ok) {
  1135. EC_GROUP_clear_free(ret);
  1136. ret = NULL;
  1137. }
  1138. BN_free(p);
  1139. BN_free(a);
  1140. BN_free(b);
  1141. EC_POINT_free(point);
  1142. return (ret);
  1143. }
  1144. EC_GROUP *
  1145. ec_asn1_pkparameters2group(const ECPKPARAMETERS * params)
  1146. {
  1147. EC_GROUP *ret = NULL;
  1148. int tmp = 0;
  1149. if (params == NULL) {
  1150. ECerror(EC_R_MISSING_PARAMETERS);
  1151. return NULL;
  1152. }
  1153. if (params->type == 0) {/* the curve is given by an OID */
  1154. tmp = OBJ_obj2nid(params->value.named_curve);
  1155. if ((ret = EC_GROUP_new_by_curve_name(tmp)) == NULL) {
  1156. ECerror(EC_R_EC_GROUP_NEW_BY_NAME_FAILURE);
  1157. return NULL;
  1158. }
  1159. EC_GROUP_set_asn1_flag(ret, OPENSSL_EC_NAMED_CURVE);
  1160. } else if (params->type == 1) { /* the parameters are given by a
  1161. * ECPARAMETERS structure */
  1162. ret = ec_asn1_parameters2group(params->value.parameters);
  1163. if (!ret) {
  1164. ECerror(ERR_R_EC_LIB);
  1165. return NULL;
  1166. }
  1167. EC_GROUP_set_asn1_flag(ret, 0x0);
  1168. } else if (params->type == 2) { /* implicitlyCA */
  1169. return NULL;
  1170. } else {
  1171. ECerror(EC_R_ASN1_ERROR);
  1172. return NULL;
  1173. }
  1174. return ret;
  1175. }
  1176. /* EC_GROUP <-> DER encoding of ECPKPARAMETERS */
  1177. EC_GROUP *
  1178. d2i_ECPKParameters(EC_GROUP ** a, const unsigned char **in, long len)
  1179. {
  1180. EC_GROUP *group = NULL;
  1181. ECPKPARAMETERS *params = NULL;
  1182. if ((params = d2i_ECPKPARAMETERS(NULL, in, len)) == NULL) {
  1183. ECerror(EC_R_D2I_ECPKPARAMETERS_FAILURE);
  1184. goto err;
  1185. }
  1186. if ((group = ec_asn1_pkparameters2group(params)) == NULL) {
  1187. ECerror(EC_R_PKPARAMETERS2GROUP_FAILURE);
  1188. goto err;
  1189. }
  1190. if (a != NULL) {
  1191. EC_GROUP_clear_free(*a);
  1192. *a = group;
  1193. }
  1194. err:
  1195. ECPKPARAMETERS_free(params);
  1196. return (group);
  1197. }
  1198. int
  1199. i2d_ECPKParameters(const EC_GROUP * a, unsigned char **out)
  1200. {
  1201. int ret = 0;
  1202. ECPKPARAMETERS *tmp = ec_asn1_group2pkparameters(a, NULL);
  1203. if (tmp == NULL) {
  1204. ECerror(EC_R_GROUP2PKPARAMETERS_FAILURE);
  1205. return 0;
  1206. }
  1207. if ((ret = i2d_ECPKPARAMETERS(tmp, out)) == 0) {
  1208. ECerror(EC_R_I2D_ECPKPARAMETERS_FAILURE);
  1209. ECPKPARAMETERS_free(tmp);
  1210. return 0;
  1211. }
  1212. ECPKPARAMETERS_free(tmp);
  1213. return (ret);
  1214. }
  1215. /* some EC_KEY functions */
  1216. EC_KEY *
  1217. d2i_ECPrivateKey(EC_KEY ** a, const unsigned char **in, long len)
  1218. {
  1219. EC_KEY *ret = NULL;
  1220. EC_PRIVATEKEY *priv_key = NULL;
  1221. if ((priv_key = EC_PRIVATEKEY_new()) == NULL) {
  1222. ECerror(ERR_R_MALLOC_FAILURE);
  1223. return NULL;
  1224. }
  1225. if ((priv_key = d2i_EC_PRIVATEKEY(&priv_key, in, len)) == NULL) {
  1226. ECerror(ERR_R_EC_LIB);
  1227. EC_PRIVATEKEY_free(priv_key);
  1228. return NULL;
  1229. }
  1230. if (a == NULL || *a == NULL) {
  1231. if ((ret = EC_KEY_new()) == NULL) {
  1232. ECerror(ERR_R_MALLOC_FAILURE);
  1233. goto err;
  1234. }
  1235. } else
  1236. ret = *a;
  1237. if (priv_key->parameters) {
  1238. EC_GROUP_clear_free(ret->group);
  1239. ret->group = ec_asn1_pkparameters2group(priv_key->parameters);
  1240. }
  1241. if (ret->group == NULL) {
  1242. ECerror(ERR_R_EC_LIB);
  1243. goto err;
  1244. }
  1245. ret->version = priv_key->version;
  1246. if (priv_key->privateKey) {
  1247. ret->priv_key = BN_bin2bn(
  1248. ASN1_STRING_data(priv_key->privateKey),
  1249. ASN1_STRING_length(priv_key->privateKey),
  1250. ret->priv_key);
  1251. if (ret->priv_key == NULL) {
  1252. ECerror(ERR_R_BN_LIB);
  1253. goto err;
  1254. }
  1255. } else {
  1256. ECerror(EC_R_MISSING_PRIVATE_KEY);
  1257. goto err;
  1258. }
  1259. if (ret->pub_key)
  1260. EC_POINT_clear_free(ret->pub_key);
  1261. ret->pub_key = EC_POINT_new(ret->group);
  1262. if (ret->pub_key == NULL) {
  1263. ECerror(ERR_R_EC_LIB);
  1264. goto err;
  1265. }
  1266. if (priv_key->publicKey) {
  1267. const unsigned char *pub_oct;
  1268. size_t pub_oct_len;
  1269. pub_oct = ASN1_STRING_data(priv_key->publicKey);
  1270. pub_oct_len = ASN1_STRING_length(priv_key->publicKey);
  1271. if (pub_oct == NULL || pub_oct_len <= 0) {
  1272. ECerror(EC_R_BUFFER_TOO_SMALL);
  1273. goto err;
  1274. }
  1275. /* save the point conversion form */
  1276. ret->conv_form = (point_conversion_form_t) (pub_oct[0] & ~0x01);
  1277. if (!EC_POINT_oct2point(ret->group, ret->pub_key,
  1278. pub_oct, pub_oct_len, NULL)) {
  1279. ECerror(ERR_R_EC_LIB);
  1280. goto err;
  1281. }
  1282. } else {
  1283. if (!EC_POINT_mul(ret->group, ret->pub_key, ret->priv_key,
  1284. NULL, NULL, NULL)) {
  1285. ECerror(ERR_R_EC_LIB);
  1286. goto err;
  1287. }
  1288. /* Remember the original private-key-only encoding. */
  1289. ret->enc_flag |= EC_PKEY_NO_PUBKEY;
  1290. }
  1291. EC_PRIVATEKEY_free(priv_key);
  1292. if (a != NULL)
  1293. *a = ret;
  1294. return (ret);
  1295. err:
  1296. if (a == NULL || *a != ret)
  1297. EC_KEY_free(ret);
  1298. if (priv_key)
  1299. EC_PRIVATEKEY_free(priv_key);
  1300. return (NULL);
  1301. }
  1302. int
  1303. i2d_ECPrivateKey(EC_KEY * a, unsigned char **out)
  1304. {
  1305. int ret = 0, ok = 0;
  1306. unsigned char *buffer = NULL;
  1307. size_t buf_len = 0, tmp_len;
  1308. EC_PRIVATEKEY *priv_key = NULL;
  1309. if (a == NULL || a->group == NULL || a->priv_key == NULL ||
  1310. (!(a->enc_flag & EC_PKEY_NO_PUBKEY) && a->pub_key == NULL)) {
  1311. ECerror(ERR_R_PASSED_NULL_PARAMETER);
  1312. goto err;
  1313. }
  1314. if ((priv_key = EC_PRIVATEKEY_new()) == NULL) {
  1315. ECerror(ERR_R_MALLOC_FAILURE);
  1316. goto err;
  1317. }
  1318. priv_key->version = a->version;
  1319. buf_len = (size_t) BN_num_bytes(a->priv_key);
  1320. buffer = malloc(buf_len);
  1321. if (buffer == NULL) {
  1322. ECerror(ERR_R_MALLOC_FAILURE);
  1323. goto err;
  1324. }
  1325. if (!BN_bn2bin(a->priv_key, buffer)) {
  1326. ECerror(ERR_R_BN_LIB);
  1327. goto err;
  1328. }
  1329. if (!ASN1_STRING_set(priv_key->privateKey, buffer, buf_len)) {
  1330. ECerror(ERR_R_ASN1_LIB);
  1331. goto err;
  1332. }
  1333. if (!(a->enc_flag & EC_PKEY_NO_PARAMETERS)) {
  1334. if ((priv_key->parameters = ec_asn1_group2pkparameters(
  1335. a->group, priv_key->parameters)) == NULL) {
  1336. ECerror(ERR_R_EC_LIB);
  1337. goto err;
  1338. }
  1339. }
  1340. if (!(a->enc_flag & EC_PKEY_NO_PUBKEY) && a->pub_key != NULL) {
  1341. priv_key->publicKey = ASN1_BIT_STRING_new();
  1342. if (priv_key->publicKey == NULL) {
  1343. ECerror(ERR_R_MALLOC_FAILURE);
  1344. goto err;
  1345. }
  1346. tmp_len = EC_POINT_point2oct(a->group, a->pub_key,
  1347. a->conv_form, NULL, 0, NULL);
  1348. if (tmp_len > buf_len) {
  1349. unsigned char *tmp_buffer = realloc(buffer, tmp_len);
  1350. if (!tmp_buffer) {
  1351. ECerror(ERR_R_MALLOC_FAILURE);
  1352. goto err;
  1353. }
  1354. buffer = tmp_buffer;
  1355. buf_len = tmp_len;
  1356. }
  1357. if (!EC_POINT_point2oct(a->group, a->pub_key,
  1358. a->conv_form, buffer, buf_len, NULL)) {
  1359. ECerror(ERR_R_EC_LIB);
  1360. goto err;
  1361. }
  1362. priv_key->publicKey->flags &= ~(ASN1_STRING_FLAG_BITS_LEFT | 0x07);
  1363. priv_key->publicKey->flags |= ASN1_STRING_FLAG_BITS_LEFT;
  1364. if (!ASN1_STRING_set(priv_key->publicKey, buffer,
  1365. buf_len)) {
  1366. ECerror(ERR_R_ASN1_LIB);
  1367. goto err;
  1368. }
  1369. }
  1370. if ((ret = i2d_EC_PRIVATEKEY(priv_key, out)) == 0) {
  1371. ECerror(ERR_R_EC_LIB);
  1372. goto err;
  1373. }
  1374. ok = 1;
  1375. err:
  1376. free(buffer);
  1377. if (priv_key)
  1378. EC_PRIVATEKEY_free(priv_key);
  1379. return (ok ? ret : 0);
  1380. }
  1381. int
  1382. i2d_ECParameters(EC_KEY * a, unsigned char **out)
  1383. {
  1384. if (a == NULL) {
  1385. ECerror(ERR_R_PASSED_NULL_PARAMETER);
  1386. return 0;
  1387. }
  1388. return i2d_ECPKParameters(a->group, out);
  1389. }
  1390. EC_KEY *
  1391. d2i_ECParameters(EC_KEY ** a, const unsigned char **in, long len)
  1392. {
  1393. EC_KEY *ret;
  1394. if (in == NULL || *in == NULL) {
  1395. ECerror(ERR_R_PASSED_NULL_PARAMETER);
  1396. return NULL;
  1397. }
  1398. if (a == NULL || *a == NULL) {
  1399. if ((ret = EC_KEY_new()) == NULL) {
  1400. ECerror(ERR_R_MALLOC_FAILURE);
  1401. return NULL;
  1402. }
  1403. } else
  1404. ret = *a;
  1405. if (!d2i_ECPKParameters(&ret->group, in, len)) {
  1406. ECerror(ERR_R_EC_LIB);
  1407. if (a == NULL || *a != ret)
  1408. EC_KEY_free(ret);
  1409. return NULL;
  1410. }
  1411. if (a != NULL)
  1412. *a = ret;
  1413. return ret;
  1414. }
  1415. EC_KEY *
  1416. o2i_ECPublicKey(EC_KEY ** a, const unsigned char **in, long len)
  1417. {
  1418. EC_KEY *ret = NULL;
  1419. if (a == NULL || (*a) == NULL || (*a)->group == NULL) {
  1420. /*
  1421. * sorry, but a EC_GROUP-structur is necessary to set the
  1422. * public key
  1423. */
  1424. ECerror(ERR_R_PASSED_NULL_PARAMETER);
  1425. return 0;
  1426. }
  1427. ret = *a;
  1428. if (ret->pub_key == NULL &&
  1429. (ret->pub_key = EC_POINT_new(ret->group)) == NULL) {
  1430. ECerror(ERR_R_MALLOC_FAILURE);
  1431. return 0;
  1432. }
  1433. if (!EC_POINT_oct2point(ret->group, ret->pub_key, *in, len, NULL)) {
  1434. ECerror(ERR_R_EC_LIB);
  1435. return 0;
  1436. }
  1437. /* save the point conversion form */
  1438. ret->conv_form = (point_conversion_form_t) (*in[0] & ~0x01);
  1439. *in += len;
  1440. return ret;
  1441. }
  1442. int
  1443. i2o_ECPublicKey(EC_KEY * a, unsigned char **out)
  1444. {
  1445. size_t buf_len = 0;
  1446. int new_buffer = 0;
  1447. if (a == NULL) {
  1448. ECerror(ERR_R_PASSED_NULL_PARAMETER);
  1449. return 0;
  1450. }
  1451. buf_len = EC_POINT_point2oct(a->group, a->pub_key,
  1452. a->conv_form, NULL, 0, NULL);
  1453. if (out == NULL || buf_len == 0)
  1454. /* out == NULL => just return the length of the octet string */
  1455. return buf_len;
  1456. if (*out == NULL) {
  1457. if ((*out = malloc(buf_len)) == NULL) {
  1458. ECerror(ERR_R_MALLOC_FAILURE);
  1459. return 0;
  1460. }
  1461. new_buffer = 1;
  1462. }
  1463. if (!EC_POINT_point2oct(a->group, a->pub_key, a->conv_form,
  1464. *out, buf_len, NULL)) {
  1465. ECerror(ERR_R_EC_LIB);
  1466. if (new_buffer) {
  1467. free(*out);
  1468. *out = NULL;
  1469. }
  1470. return 0;
  1471. }
  1472. if (!new_buffer)
  1473. *out += buf_len;
  1474. return buf_len;
  1475. }