PageRenderTime 27ms CodeModel.GetById 15ms RepoModel.GetById 1ms app.codeStats 0ms

/src/freebsd/crypto/openssl/crypto/ec/ec_asn1.c

https://bitbucket.org/killerpenguinassassins/open_distrib_devel
C | 1429 lines | 1127 code | 175 blank | 127 comment | 349 complexity | 25259f342f8c5d273cc8703767376ea5 MD5 | raw file
Possible License(s): CC0-1.0, MIT, LGPL-2.0, LGPL-3.0, WTFPL, GPL-2.0, BSD-2-Clause, AGPL-3.0, CC-BY-SA-3.0, MPL-2.0, JSON, BSD-3-Clause-No-Nuclear-License-2014, LGPL-2.1, CPL-1.0, AGPL-1.0, 0BSD, ISC, Apache-2.0, GPL-3.0, IPL-1.0, MPL-2.0-no-copyleft-exception, BSD-3-Clause
  1. /* crypto/ec/ec_asn1.c */
  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 "ec_lcl.h"
  60. #include <openssl/err.h>
  61. #include <openssl/asn1t.h>
  62. #include <openssl/objects.h>
  63. int EC_GROUP_get_basis_type(const EC_GROUP *group)
  64. {
  65. int i=0;
  66. if (EC_METHOD_get_field_type(EC_GROUP_method_of(group)) !=
  67. NID_X9_62_characteristic_two_field)
  68. /* everything else is currently not supported */
  69. return 0;
  70. while (group->poly[i] != 0)
  71. i++;
  72. if (i == 4)
  73. return NID_X9_62_ppBasis;
  74. else if (i == 2)
  75. return NID_X9_62_tpBasis;
  76. else
  77. /* everything else is currently not supported */
  78. return 0;
  79. }
  80. int EC_GROUP_get_trinomial_basis(const EC_GROUP *group, unsigned int *k)
  81. {
  82. if (group == NULL)
  83. return 0;
  84. if (EC_GROUP_method_of(group)->group_set_curve != ec_GF2m_simple_group_set_curve
  85. || !((group->poly[0] != 0) && (group->poly[1] != 0) && (group->poly[2] == 0)))
  86. {
  87. ECerr(EC_F_EC_GROUP_GET_TRINOMIAL_BASIS, ERR_R_SHOULD_NOT_HAVE_BEEN_CALLED);
  88. return 0;
  89. }
  90. if (k)
  91. *k = group->poly[1];
  92. return 1;
  93. }
  94. int EC_GROUP_get_pentanomial_basis(const EC_GROUP *group, unsigned int *k1,
  95. unsigned int *k2, unsigned int *k3)
  96. {
  97. if (group == NULL)
  98. return 0;
  99. if (EC_GROUP_method_of(group)->group_set_curve != ec_GF2m_simple_group_set_curve
  100. || !((group->poly[0] != 0) && (group->poly[1] != 0) && (group->poly[2] != 0) && (group->poly[3] != 0) && (group->poly[4] == 0)))
  101. {
  102. ECerr(EC_F_EC_GROUP_GET_PENTANOMIAL_BASIS, ERR_R_SHOULD_NOT_HAVE_BEEN_CALLED);
  103. return 0;
  104. }
  105. if (k1)
  106. *k1 = group->poly[3];
  107. if (k2)
  108. *k2 = group->poly[2];
  109. if (k3)
  110. *k3 = group->poly[1];
  111. return 1;
  112. }
  113. /* some structures needed for the asn1 encoding */
  114. typedef struct x9_62_pentanomial_st {
  115. long k1;
  116. long k2;
  117. long k3;
  118. } X9_62_PENTANOMIAL;
  119. typedef struct x9_62_characteristic_two_st {
  120. long m;
  121. ASN1_OBJECT *type;
  122. union {
  123. char *ptr;
  124. /* NID_X9_62_onBasis */
  125. ASN1_NULL *onBasis;
  126. /* NID_X9_62_tpBasis */
  127. ASN1_INTEGER *tpBasis;
  128. /* NID_X9_62_ppBasis */
  129. X9_62_PENTANOMIAL *ppBasis;
  130. /* anything else */
  131. ASN1_TYPE *other;
  132. } p;
  133. } X9_62_CHARACTERISTIC_TWO;
  134. typedef struct x9_62_fieldid_st {
  135. ASN1_OBJECT *fieldType;
  136. union {
  137. char *ptr;
  138. /* NID_X9_62_prime_field */
  139. ASN1_INTEGER *prime;
  140. /* NID_X9_62_characteristic_two_field */
  141. X9_62_CHARACTERISTIC_TWO *char_two;
  142. /* anything else */
  143. ASN1_TYPE *other;
  144. } p;
  145. } X9_62_FIELDID;
  146. typedef struct x9_62_curve_st {
  147. ASN1_OCTET_STRING *a;
  148. ASN1_OCTET_STRING *b;
  149. ASN1_BIT_STRING *seed;
  150. } X9_62_CURVE;
  151. typedef struct ec_parameters_st {
  152. long version;
  153. X9_62_FIELDID *fieldID;
  154. X9_62_CURVE *curve;
  155. ASN1_OCTET_STRING *base;
  156. ASN1_INTEGER *order;
  157. ASN1_INTEGER *cofactor;
  158. } ECPARAMETERS;
  159. struct ecpk_parameters_st {
  160. int type;
  161. union {
  162. ASN1_OBJECT *named_curve;
  163. ECPARAMETERS *parameters;
  164. ASN1_NULL *implicitlyCA;
  165. } value;
  166. }/* ECPKPARAMETERS */;
  167. /* SEC1 ECPrivateKey */
  168. typedef struct ec_privatekey_st {
  169. long version;
  170. ASN1_OCTET_STRING *privateKey;
  171. ECPKPARAMETERS *parameters;
  172. ASN1_BIT_STRING *publicKey;
  173. } EC_PRIVATEKEY;
  174. /* the OpenSSL ASN.1 definitions */
  175. ASN1_SEQUENCE(X9_62_PENTANOMIAL) = {
  176. ASN1_SIMPLE(X9_62_PENTANOMIAL, k1, LONG),
  177. ASN1_SIMPLE(X9_62_PENTANOMIAL, k2, LONG),
  178. ASN1_SIMPLE(X9_62_PENTANOMIAL, k3, LONG)
  179. } ASN1_SEQUENCE_END(X9_62_PENTANOMIAL)
  180. DECLARE_ASN1_ALLOC_FUNCTIONS(X9_62_PENTANOMIAL)
  181. IMPLEMENT_ASN1_ALLOC_FUNCTIONS(X9_62_PENTANOMIAL)
  182. ASN1_ADB_TEMPLATE(char_two_def) = ASN1_SIMPLE(X9_62_CHARACTERISTIC_TWO, p.other, ASN1_ANY);
  183. ASN1_ADB(X9_62_CHARACTERISTIC_TWO) = {
  184. ADB_ENTRY(NID_X9_62_onBasis, ASN1_SIMPLE(X9_62_CHARACTERISTIC_TWO, p.onBasis, ASN1_NULL)),
  185. ADB_ENTRY(NID_X9_62_tpBasis, ASN1_SIMPLE(X9_62_CHARACTERISTIC_TWO, p.tpBasis, ASN1_INTEGER)),
  186. ADB_ENTRY(NID_X9_62_ppBasis, ASN1_SIMPLE(X9_62_CHARACTERISTIC_TWO, p.ppBasis, X9_62_PENTANOMIAL))
  187. } ASN1_ADB_END(X9_62_CHARACTERISTIC_TWO, 0, type, 0, &char_two_def_tt, NULL);
  188. ASN1_SEQUENCE(X9_62_CHARACTERISTIC_TWO) = {
  189. ASN1_SIMPLE(X9_62_CHARACTERISTIC_TWO, m, LONG),
  190. ASN1_SIMPLE(X9_62_CHARACTERISTIC_TWO, type, ASN1_OBJECT),
  191. ASN1_ADB_OBJECT(X9_62_CHARACTERISTIC_TWO)
  192. } ASN1_SEQUENCE_END(X9_62_CHARACTERISTIC_TWO)
  193. DECLARE_ASN1_ALLOC_FUNCTIONS(X9_62_CHARACTERISTIC_TWO)
  194. IMPLEMENT_ASN1_ALLOC_FUNCTIONS(X9_62_CHARACTERISTIC_TWO)
  195. ASN1_ADB_TEMPLATE(fieldID_def) = ASN1_SIMPLE(X9_62_FIELDID, p.other, ASN1_ANY);
  196. ASN1_ADB(X9_62_FIELDID) = {
  197. ADB_ENTRY(NID_X9_62_prime_field, ASN1_SIMPLE(X9_62_FIELDID, p.prime, ASN1_INTEGER)),
  198. ADB_ENTRY(NID_X9_62_characteristic_two_field, ASN1_SIMPLE(X9_62_FIELDID, p.char_two, X9_62_CHARACTERISTIC_TWO))
  199. } ASN1_ADB_END(X9_62_FIELDID, 0, fieldType, 0, &fieldID_def_tt, NULL);
  200. ASN1_SEQUENCE(X9_62_FIELDID) = {
  201. ASN1_SIMPLE(X9_62_FIELDID, fieldType, ASN1_OBJECT),
  202. ASN1_ADB_OBJECT(X9_62_FIELDID)
  203. } ASN1_SEQUENCE_END(X9_62_FIELDID)
  204. ASN1_SEQUENCE(X9_62_CURVE) = {
  205. ASN1_SIMPLE(X9_62_CURVE, a, ASN1_OCTET_STRING),
  206. ASN1_SIMPLE(X9_62_CURVE, b, ASN1_OCTET_STRING),
  207. ASN1_OPT(X9_62_CURVE, seed, ASN1_BIT_STRING)
  208. } ASN1_SEQUENCE_END(X9_62_CURVE)
  209. ASN1_SEQUENCE(ECPARAMETERS) = {
  210. ASN1_SIMPLE(ECPARAMETERS, version, LONG),
  211. ASN1_SIMPLE(ECPARAMETERS, fieldID, X9_62_FIELDID),
  212. ASN1_SIMPLE(ECPARAMETERS, curve, X9_62_CURVE),
  213. ASN1_SIMPLE(ECPARAMETERS, base, ASN1_OCTET_STRING),
  214. ASN1_SIMPLE(ECPARAMETERS, order, ASN1_INTEGER),
  215. ASN1_OPT(ECPARAMETERS, cofactor, ASN1_INTEGER)
  216. } ASN1_SEQUENCE_END(ECPARAMETERS)
  217. DECLARE_ASN1_ALLOC_FUNCTIONS(ECPARAMETERS)
  218. IMPLEMENT_ASN1_ALLOC_FUNCTIONS(ECPARAMETERS)
  219. ASN1_CHOICE(ECPKPARAMETERS) = {
  220. ASN1_SIMPLE(ECPKPARAMETERS, value.named_curve, ASN1_OBJECT),
  221. ASN1_SIMPLE(ECPKPARAMETERS, value.parameters, ECPARAMETERS),
  222. ASN1_SIMPLE(ECPKPARAMETERS, value.implicitlyCA, ASN1_NULL)
  223. } ASN1_CHOICE_END(ECPKPARAMETERS)
  224. DECLARE_ASN1_FUNCTIONS_const(ECPKPARAMETERS)
  225. DECLARE_ASN1_ENCODE_FUNCTIONS_const(ECPKPARAMETERS, ECPKPARAMETERS)
  226. IMPLEMENT_ASN1_FUNCTIONS_const(ECPKPARAMETERS)
  227. ASN1_SEQUENCE(EC_PRIVATEKEY) = {
  228. ASN1_SIMPLE(EC_PRIVATEKEY, version, LONG),
  229. ASN1_SIMPLE(EC_PRIVATEKEY, privateKey, ASN1_OCTET_STRING),
  230. ASN1_EXP_OPT(EC_PRIVATEKEY, parameters, ECPKPARAMETERS, 0),
  231. ASN1_EXP_OPT(EC_PRIVATEKEY, publicKey, ASN1_BIT_STRING, 1)
  232. } ASN1_SEQUENCE_END(EC_PRIVATEKEY)
  233. DECLARE_ASN1_FUNCTIONS_const(EC_PRIVATEKEY)
  234. DECLARE_ASN1_ENCODE_FUNCTIONS_const(EC_PRIVATEKEY, EC_PRIVATEKEY)
  235. IMPLEMENT_ASN1_FUNCTIONS_const(EC_PRIVATEKEY)
  236. /* some declarations of internal function */
  237. /* ec_asn1_group2field() sets the values in a X9_62_FIELDID object */
  238. static int ec_asn1_group2fieldid(const EC_GROUP *, X9_62_FIELDID *);
  239. /* ec_asn1_group2curve() sets the values in a X9_62_CURVE object */
  240. static int ec_asn1_group2curve(const EC_GROUP *, X9_62_CURVE *);
  241. /* ec_asn1_parameters2group() creates a EC_GROUP object from a
  242. * ECPARAMETERS object */
  243. static EC_GROUP *ec_asn1_parameters2group(const ECPARAMETERS *);
  244. /* ec_asn1_group2parameters() creates a ECPARAMETERS object from a
  245. * EC_GROUP object */
  246. static ECPARAMETERS *ec_asn1_group2parameters(const EC_GROUP *,ECPARAMETERS *);
  247. /* ec_asn1_pkparameters2group() creates a EC_GROUP object from a
  248. * ECPKPARAMETERS object */
  249. static EC_GROUP *ec_asn1_pkparameters2group(const ECPKPARAMETERS *);
  250. /* ec_asn1_group2pkparameters() creates a ECPKPARAMETERS object from a
  251. * EC_GROUP object */
  252. static ECPKPARAMETERS *ec_asn1_group2pkparameters(const EC_GROUP *,
  253. ECPKPARAMETERS *);
  254. /* the function definitions */
  255. static int ec_asn1_group2fieldid(const EC_GROUP *group, X9_62_FIELDID *field)
  256. {
  257. int ok=0, nid;
  258. BIGNUM *tmp = NULL;
  259. if (group == NULL || field == NULL)
  260. return 0;
  261. /* clear the old values (if necessary) */
  262. if (field->fieldType != NULL)
  263. ASN1_OBJECT_free(field->fieldType);
  264. if (field->p.other != NULL)
  265. ASN1_TYPE_free(field->p.other);
  266. nid = EC_METHOD_get_field_type(EC_GROUP_method_of(group));
  267. /* set OID for the field */
  268. if ((field->fieldType = OBJ_nid2obj(nid)) == NULL)
  269. {
  270. ECerr(EC_F_EC_ASN1_GROUP2FIELDID, ERR_R_OBJ_LIB);
  271. goto err;
  272. }
  273. if (nid == NID_X9_62_prime_field)
  274. {
  275. if ((tmp = BN_new()) == NULL)
  276. {
  277. ECerr(EC_F_EC_ASN1_GROUP2FIELDID, ERR_R_MALLOC_FAILURE);
  278. goto err;
  279. }
  280. /* the parameters are specified by the prime number p */
  281. if (!EC_GROUP_get_curve_GFp(group, tmp, NULL, NULL, NULL))
  282. {
  283. ECerr(EC_F_EC_ASN1_GROUP2FIELDID, ERR_R_EC_LIB);
  284. goto err;
  285. }
  286. /* set the prime number */
  287. field->p.prime = BN_to_ASN1_INTEGER(tmp,NULL);
  288. if (field->p.prime == NULL)
  289. {
  290. ECerr(EC_F_EC_ASN1_GROUP2FIELDID, ERR_R_ASN1_LIB);
  291. goto err;
  292. }
  293. }
  294. else /* nid == NID_X9_62_characteristic_two_field */
  295. {
  296. int field_type;
  297. X9_62_CHARACTERISTIC_TWO *char_two;
  298. field->p.char_two = X9_62_CHARACTERISTIC_TWO_new();
  299. char_two = field->p.char_two;
  300. if (char_two == NULL)
  301. {
  302. ECerr(EC_F_EC_ASN1_GROUP2FIELDID, ERR_R_MALLOC_FAILURE);
  303. goto err;
  304. }
  305. char_two->m = (long)EC_GROUP_get_degree(group);
  306. field_type = EC_GROUP_get_basis_type(group);
  307. if (field_type == 0)
  308. {
  309. ECerr(EC_F_EC_ASN1_GROUP2FIELDID, ERR_R_EC_LIB);
  310. goto err;
  311. }
  312. /* set base type OID */
  313. if ((char_two->type = OBJ_nid2obj(field_type)) == NULL)
  314. {
  315. ECerr(EC_F_EC_ASN1_GROUP2FIELDID, ERR_R_OBJ_LIB);
  316. goto err;
  317. }
  318. if (field_type == NID_X9_62_tpBasis)
  319. {
  320. unsigned int k;
  321. if (!EC_GROUP_get_trinomial_basis(group, &k))
  322. goto err;
  323. char_two->p.tpBasis = ASN1_INTEGER_new();
  324. if (!char_two->p.tpBasis)
  325. {
  326. ECerr(EC_F_EC_ASN1_GROUP2FIELDID, ERR_R_MALLOC_FAILURE);
  327. goto err;
  328. }
  329. if (!ASN1_INTEGER_set(char_two->p.tpBasis, (long)k))
  330. {
  331. ECerr(EC_F_EC_ASN1_GROUP2FIELDID,
  332. ERR_R_ASN1_LIB);
  333. goto err;
  334. }
  335. }
  336. else if (field_type == NID_X9_62_ppBasis)
  337. {
  338. unsigned int k1, k2, k3;
  339. if (!EC_GROUP_get_pentanomial_basis(group, &k1, &k2, &k3))
  340. goto err;
  341. char_two->p.ppBasis = X9_62_PENTANOMIAL_new();
  342. if (!char_two->p.ppBasis)
  343. {
  344. ECerr(EC_F_EC_ASN1_GROUP2FIELDID, ERR_R_MALLOC_FAILURE);
  345. goto err;
  346. }
  347. /* set k? values */
  348. char_two->p.ppBasis->k1 = (long)k1;
  349. char_two->p.ppBasis->k2 = (long)k2;
  350. char_two->p.ppBasis->k3 = (long)k3;
  351. }
  352. else /* field_type == NID_X9_62_onBasis */
  353. {
  354. /* for ONB the parameters are (asn1) NULL */
  355. char_two->p.onBasis = ASN1_NULL_new();
  356. if (!char_two->p.onBasis)
  357. {
  358. ECerr(EC_F_EC_ASN1_GROUP2FIELDID, ERR_R_MALLOC_FAILURE);
  359. goto err;
  360. }
  361. }
  362. }
  363. ok = 1;
  364. err : if (tmp)
  365. BN_free(tmp);
  366. return(ok);
  367. }
  368. static int ec_asn1_group2curve(const EC_GROUP *group, X9_62_CURVE *curve)
  369. {
  370. int ok=0, nid;
  371. BIGNUM *tmp_1=NULL, *tmp_2=NULL;
  372. unsigned char *buffer_1=NULL, *buffer_2=NULL,
  373. *a_buf=NULL, *b_buf=NULL;
  374. size_t len_1, len_2;
  375. unsigned char char_zero = 0;
  376. if (!group || !curve || !curve->a || !curve->b)
  377. return 0;
  378. if ((tmp_1 = BN_new()) == NULL || (tmp_2 = BN_new()) == NULL)
  379. {
  380. ECerr(EC_F_EC_ASN1_GROUP2CURVE, ERR_R_MALLOC_FAILURE);
  381. goto err;
  382. }
  383. nid = EC_METHOD_get_field_type(EC_GROUP_method_of(group));
  384. /* get a and b */
  385. if (nid == NID_X9_62_prime_field)
  386. {
  387. if (!EC_GROUP_get_curve_GFp(group, NULL, tmp_1, tmp_2, NULL))
  388. {
  389. ECerr(EC_F_EC_ASN1_GROUP2CURVE, ERR_R_EC_LIB);
  390. goto err;
  391. }
  392. }
  393. else /* nid == NID_X9_62_characteristic_two_field */
  394. {
  395. if (!EC_GROUP_get_curve_GF2m(group, NULL, tmp_1, tmp_2, NULL))
  396. {
  397. ECerr(EC_F_EC_ASN1_GROUP2CURVE, ERR_R_EC_LIB);
  398. goto err;
  399. }
  400. }
  401. len_1 = (size_t)BN_num_bytes(tmp_1);
  402. len_2 = (size_t)BN_num_bytes(tmp_2);
  403. if (len_1 == 0)
  404. {
  405. /* len_1 == 0 => a == 0 */
  406. a_buf = &char_zero;
  407. len_1 = 1;
  408. }
  409. else
  410. {
  411. if ((buffer_1 = OPENSSL_malloc(len_1)) == NULL)
  412. {
  413. ECerr(EC_F_EC_ASN1_GROUP2CURVE,
  414. ERR_R_MALLOC_FAILURE);
  415. goto err;
  416. }
  417. if ( (len_1 = BN_bn2bin(tmp_1, buffer_1)) == 0)
  418. {
  419. ECerr(EC_F_EC_ASN1_GROUP2CURVE, ERR_R_BN_LIB);
  420. goto err;
  421. }
  422. a_buf = buffer_1;
  423. }
  424. if (len_2 == 0)
  425. {
  426. /* len_2 == 0 => b == 0 */
  427. b_buf = &char_zero;
  428. len_2 = 1;
  429. }
  430. else
  431. {
  432. if ((buffer_2 = OPENSSL_malloc(len_2)) == NULL)
  433. {
  434. ECerr(EC_F_EC_ASN1_GROUP2CURVE,
  435. ERR_R_MALLOC_FAILURE);
  436. goto err;
  437. }
  438. if ( (len_2 = BN_bn2bin(tmp_2, buffer_2)) == 0)
  439. {
  440. ECerr(EC_F_EC_ASN1_GROUP2CURVE, ERR_R_BN_LIB);
  441. goto err;
  442. }
  443. b_buf = buffer_2;
  444. }
  445. /* set a and b */
  446. if (!M_ASN1_OCTET_STRING_set(curve->a, a_buf, len_1) ||
  447. !M_ASN1_OCTET_STRING_set(curve->b, b_buf, len_2))
  448. {
  449. ECerr(EC_F_EC_ASN1_GROUP2CURVE, ERR_R_ASN1_LIB);
  450. goto err;
  451. }
  452. /* set the seed (optional) */
  453. if (group->seed)
  454. {
  455. if (!curve->seed)
  456. if ((curve->seed = ASN1_BIT_STRING_new()) == NULL)
  457. {
  458. ECerr(EC_F_EC_ASN1_GROUP2CURVE, ERR_R_MALLOC_FAILURE);
  459. goto err;
  460. }
  461. curve->seed->flags &= ~(ASN1_STRING_FLAG_BITS_LEFT|0x07);
  462. curve->seed->flags |= ASN1_STRING_FLAG_BITS_LEFT;
  463. if (!ASN1_BIT_STRING_set(curve->seed, group->seed,
  464. (int)group->seed_len))
  465. {
  466. ECerr(EC_F_EC_ASN1_GROUP2CURVE, ERR_R_ASN1_LIB);
  467. goto err;
  468. }
  469. }
  470. else
  471. {
  472. if (curve->seed)
  473. {
  474. ASN1_BIT_STRING_free(curve->seed);
  475. curve->seed = NULL;
  476. }
  477. }
  478. ok = 1;
  479. err: if (buffer_1)
  480. OPENSSL_free(buffer_1);
  481. if (buffer_2)
  482. OPENSSL_free(buffer_2);
  483. if (tmp_1)
  484. BN_free(tmp_1);
  485. if (tmp_2)
  486. BN_free(tmp_2);
  487. return(ok);
  488. }
  489. static ECPARAMETERS *ec_asn1_group2parameters(const EC_GROUP *group,
  490. ECPARAMETERS *param)
  491. {
  492. int ok=0;
  493. size_t len=0;
  494. ECPARAMETERS *ret=NULL;
  495. BIGNUM *tmp=NULL;
  496. unsigned char *buffer=NULL;
  497. const EC_POINT *point=NULL;
  498. point_conversion_form_t form;
  499. if ((tmp = BN_new()) == NULL)
  500. {
  501. ECerr(EC_F_EC_ASN1_GROUP2PARAMETERS, ERR_R_MALLOC_FAILURE);
  502. goto err;
  503. }
  504. if (param == NULL)
  505. {
  506. if ((ret = ECPARAMETERS_new()) == NULL)
  507. {
  508. ECerr(EC_F_EC_ASN1_GROUP2PARAMETERS,
  509. ERR_R_MALLOC_FAILURE);
  510. goto err;
  511. }
  512. }
  513. else
  514. ret = param;
  515. /* set the version (always one) */
  516. ret->version = (long)0x1;
  517. /* set the fieldID */
  518. if (!ec_asn1_group2fieldid(group, ret->fieldID))
  519. {
  520. ECerr(EC_F_EC_ASN1_GROUP2PARAMETERS, ERR_R_EC_LIB);
  521. goto err;
  522. }
  523. /* set the curve */
  524. if (!ec_asn1_group2curve(group, ret->curve))
  525. {
  526. ECerr(EC_F_EC_ASN1_GROUP2PARAMETERS, ERR_R_EC_LIB);
  527. goto err;
  528. }
  529. /* set the base point */
  530. if ((point = EC_GROUP_get0_generator(group)) == NULL)
  531. {
  532. ECerr(EC_F_EC_ASN1_GROUP2PARAMETERS, EC_R_UNDEFINED_GENERATOR);
  533. goto err;
  534. }
  535. form = EC_GROUP_get_point_conversion_form(group);
  536. len = EC_POINT_point2oct(group, point, form, NULL, len, NULL);
  537. if (len == 0)
  538. {
  539. ECerr(EC_F_EC_ASN1_GROUP2PARAMETERS, ERR_R_EC_LIB);
  540. goto err;
  541. }
  542. if ((buffer = OPENSSL_malloc(len)) == NULL)
  543. {
  544. ECerr(EC_F_EC_ASN1_GROUP2PARAMETERS, ERR_R_MALLOC_FAILURE);
  545. goto err;
  546. }
  547. if (!EC_POINT_point2oct(group, point, form, buffer, len, NULL))
  548. {
  549. ECerr(EC_F_EC_ASN1_GROUP2PARAMETERS, ERR_R_EC_LIB);
  550. goto err;
  551. }
  552. if (ret->base == NULL && (ret->base = ASN1_OCTET_STRING_new()) == NULL)
  553. {
  554. ECerr(EC_F_EC_ASN1_GROUP2PARAMETERS, ERR_R_MALLOC_FAILURE);
  555. goto err;
  556. }
  557. if (!ASN1_OCTET_STRING_set(ret->base, buffer, len))
  558. {
  559. ECerr(EC_F_EC_ASN1_GROUP2PARAMETERS, ERR_R_ASN1_LIB);
  560. goto err;
  561. }
  562. /* set the order */
  563. if (!EC_GROUP_get_order(group, tmp, NULL))
  564. {
  565. ECerr(EC_F_EC_ASN1_GROUP2PARAMETERS, ERR_R_EC_LIB);
  566. goto err;
  567. }
  568. ret->order = BN_to_ASN1_INTEGER(tmp, ret->order);
  569. if (ret->order == NULL)
  570. {
  571. ECerr(EC_F_EC_ASN1_GROUP2PARAMETERS, ERR_R_ASN1_LIB);
  572. goto err;
  573. }
  574. /* set the cofactor (optional) */
  575. if (EC_GROUP_get_cofactor(group, tmp, NULL))
  576. {
  577. ret->cofactor = BN_to_ASN1_INTEGER(tmp, ret->cofactor);
  578. if (ret->cofactor == NULL)
  579. {
  580. ECerr(EC_F_EC_ASN1_GROUP2PARAMETERS, ERR_R_ASN1_LIB);
  581. goto err;
  582. }
  583. }
  584. ok = 1;
  585. err : if(!ok)
  586. {
  587. if (ret && !param)
  588. ECPARAMETERS_free(ret);
  589. ret = NULL;
  590. }
  591. if (tmp)
  592. BN_free(tmp);
  593. if (buffer)
  594. OPENSSL_free(buffer);
  595. return(ret);
  596. }
  597. ECPKPARAMETERS *ec_asn1_group2pkparameters(const EC_GROUP *group,
  598. ECPKPARAMETERS *params)
  599. {
  600. int ok = 1, tmp;
  601. ECPKPARAMETERS *ret = params;
  602. if (ret == NULL)
  603. {
  604. if ((ret = ECPKPARAMETERS_new()) == NULL)
  605. {
  606. ECerr(EC_F_EC_ASN1_GROUP2PKPARAMETERS,
  607. ERR_R_MALLOC_FAILURE);
  608. return NULL;
  609. }
  610. }
  611. else
  612. {
  613. if (ret->type == 0 && ret->value.named_curve)
  614. ASN1_OBJECT_free(ret->value.named_curve);
  615. else if (ret->type == 1 && ret->value.parameters)
  616. ECPARAMETERS_free(ret->value.parameters);
  617. }
  618. if (EC_GROUP_get_asn1_flag(group))
  619. {
  620. /* use the asn1 OID to describe the
  621. * the elliptic curve parameters
  622. */
  623. tmp = EC_GROUP_get_curve_name(group);
  624. if (tmp)
  625. {
  626. ret->type = 0;
  627. if ((ret->value.named_curve = OBJ_nid2obj(tmp)) == NULL)
  628. ok = 0;
  629. }
  630. else
  631. /* we don't kmow the nid => ERROR */
  632. ok = 0;
  633. }
  634. else
  635. {
  636. /* use the ECPARAMETERS structure */
  637. ret->type = 1;
  638. if ((ret->value.parameters = ec_asn1_group2parameters(
  639. group, NULL)) == NULL)
  640. ok = 0;
  641. }
  642. if (!ok)
  643. {
  644. ECPKPARAMETERS_free(ret);
  645. return NULL;
  646. }
  647. return ret;
  648. }
  649. static EC_GROUP *ec_asn1_parameters2group(const ECPARAMETERS *params)
  650. {
  651. int ok = 0, tmp;
  652. EC_GROUP *ret = NULL;
  653. BIGNUM *p = NULL, *a = NULL, *b = NULL;
  654. EC_POINT *point=NULL;
  655. long field_bits;
  656. if (!params->fieldID || !params->fieldID->fieldType ||
  657. !params->fieldID->p.ptr)
  658. {
  659. ECerr(EC_F_EC_ASN1_PARAMETERS2GROUP, EC_R_ASN1_ERROR);
  660. goto err;
  661. }
  662. /* now extract the curve parameters a and b */
  663. if (!params->curve || !params->curve->a ||
  664. !params->curve->a->data || !params->curve->b ||
  665. !params->curve->b->data)
  666. {
  667. ECerr(EC_F_EC_ASN1_PARAMETERS2GROUP, EC_R_ASN1_ERROR);
  668. goto err;
  669. }
  670. a = BN_bin2bn(params->curve->a->data, params->curve->a->length, NULL);
  671. if (a == NULL)
  672. {
  673. ECerr(EC_F_EC_ASN1_PARAMETERS2GROUP, ERR_R_BN_LIB);
  674. goto err;
  675. }
  676. b = BN_bin2bn(params->curve->b->data, params->curve->b->length, NULL);
  677. if (b == NULL)
  678. {
  679. ECerr(EC_F_EC_ASN1_PARAMETERS2GROUP, ERR_R_BN_LIB);
  680. goto err;
  681. }
  682. /* get the field parameters */
  683. tmp = OBJ_obj2nid(params->fieldID->fieldType);
  684. if (tmp == NID_X9_62_characteristic_two_field)
  685. {
  686. X9_62_CHARACTERISTIC_TWO *char_two;
  687. char_two = params->fieldID->p.char_two;
  688. field_bits = char_two->m;
  689. if (field_bits > OPENSSL_ECC_MAX_FIELD_BITS)
  690. {
  691. ECerr(EC_F_EC_ASN1_PARAMETERS2GROUP, EC_R_FIELD_TOO_LARGE);
  692. goto err;
  693. }
  694. if ((p = BN_new()) == NULL)
  695. {
  696. ECerr(EC_F_EC_ASN1_PARAMETERS2GROUP, ERR_R_MALLOC_FAILURE);
  697. goto err;
  698. }
  699. /* get the base type */
  700. tmp = OBJ_obj2nid(char_two->type);
  701. if (tmp == NID_X9_62_tpBasis)
  702. {
  703. long tmp_long;
  704. if (!char_two->p.tpBasis)
  705. {
  706. ECerr(EC_F_EC_ASN1_PARAMETERS2GROUP, EC_R_ASN1_ERROR);
  707. goto err;
  708. }
  709. tmp_long = ASN1_INTEGER_get(char_two->p.tpBasis);
  710. if (!(char_two->m > tmp_long && tmp_long > 0))
  711. {
  712. ECerr(EC_F_EC_ASN1_PARAMETERS2GROUP, EC_R_INVALID_TRINOMIAL_BASIS);
  713. goto err;
  714. }
  715. /* create the polynomial */
  716. if (!BN_set_bit(p, (int)char_two->m))
  717. goto err;
  718. if (!BN_set_bit(p, (int)tmp_long))
  719. goto err;
  720. if (!BN_set_bit(p, 0))
  721. goto err;
  722. }
  723. else if (tmp == NID_X9_62_ppBasis)
  724. {
  725. X9_62_PENTANOMIAL *penta;
  726. penta = char_two->p.ppBasis;
  727. if (!penta)
  728. {
  729. ECerr(EC_F_EC_ASN1_PARAMETERS2GROUP, EC_R_ASN1_ERROR);
  730. goto err;
  731. }
  732. if (!(char_two->m > penta->k3 && penta->k3 > penta->k2 && penta->k2 > penta->k1 && penta->k1 > 0))
  733. {
  734. ECerr(EC_F_EC_ASN1_PARAMETERS2GROUP, EC_R_INVALID_PENTANOMIAL_BASIS);
  735. goto err;
  736. }
  737. /* create the polynomial */
  738. if (!BN_set_bit(p, (int)char_two->m)) goto err;
  739. if (!BN_set_bit(p, (int)penta->k1)) goto err;
  740. if (!BN_set_bit(p, (int)penta->k2)) goto err;
  741. if (!BN_set_bit(p, (int)penta->k3)) goto err;
  742. if (!BN_set_bit(p, 0)) goto err;
  743. }
  744. else if (tmp == NID_X9_62_onBasis)
  745. {
  746. ECerr(EC_F_EC_ASN1_PARAMETERS2GROUP, EC_R_NOT_IMPLEMENTED);
  747. goto err;
  748. }
  749. else /* error */
  750. {
  751. ECerr(EC_F_EC_ASN1_PARAMETERS2GROUP, EC_R_ASN1_ERROR);
  752. goto err;
  753. }
  754. /* create the EC_GROUP structure */
  755. ret = EC_GROUP_new_curve_GF2m(p, a, b, NULL);
  756. }
  757. else if (tmp == NID_X9_62_prime_field)
  758. {
  759. /* we have a curve over a prime field */
  760. /* extract the prime number */
  761. if (!params->fieldID->p.prime)
  762. {
  763. ECerr(EC_F_EC_ASN1_PARAMETERS2GROUP, EC_R_ASN1_ERROR);
  764. goto err;
  765. }
  766. p = ASN1_INTEGER_to_BN(params->fieldID->p.prime, NULL);
  767. if (p == NULL)
  768. {
  769. ECerr(EC_F_EC_ASN1_PARAMETERS2GROUP, ERR_R_ASN1_LIB);
  770. goto err;
  771. }
  772. if (BN_is_negative(p) || BN_is_zero(p))
  773. {
  774. ECerr(EC_F_EC_ASN1_PARAMETERS2GROUP, EC_R_INVALID_FIELD);
  775. goto err;
  776. }
  777. field_bits = BN_num_bits(p);
  778. if (field_bits > OPENSSL_ECC_MAX_FIELD_BITS)
  779. {
  780. ECerr(EC_F_EC_ASN1_PARAMETERS2GROUP, EC_R_FIELD_TOO_LARGE);
  781. goto err;
  782. }
  783. /* create the EC_GROUP structure */
  784. ret = EC_GROUP_new_curve_GFp(p, a, b, NULL);
  785. }
  786. else
  787. {
  788. ECerr(EC_F_EC_ASN1_PARAMETERS2GROUP, EC_R_INVALID_FIELD);
  789. goto err;
  790. }
  791. if (ret == NULL)
  792. {
  793. ECerr(EC_F_EC_ASN1_PARAMETERS2GROUP, ERR_R_EC_LIB);
  794. goto err;
  795. }
  796. /* extract seed (optional) */
  797. if (params->curve->seed != NULL)
  798. {
  799. if (ret->seed != NULL)
  800. OPENSSL_free(ret->seed);
  801. if (!(ret->seed = OPENSSL_malloc(params->curve->seed->length)))
  802. {
  803. ECerr(EC_F_EC_ASN1_PARAMETERS2GROUP,
  804. ERR_R_MALLOC_FAILURE);
  805. goto err;
  806. }
  807. memcpy(ret->seed, params->curve->seed->data,
  808. params->curve->seed->length);
  809. ret->seed_len = params->curve->seed->length;
  810. }
  811. if (!params->order || !params->base || !params->base->data)
  812. {
  813. ECerr(EC_F_EC_ASN1_PARAMETERS2GROUP, EC_R_ASN1_ERROR);
  814. goto err;
  815. }
  816. if ((point = EC_POINT_new(ret)) == NULL) goto err;
  817. /* set the point conversion form */
  818. EC_GROUP_set_point_conversion_form(ret, (point_conversion_form_t)
  819. (params->base->data[0] & ~0x01));
  820. /* extract the ec point */
  821. if (!EC_POINT_oct2point(ret, point, params->base->data,
  822. params->base->length, NULL))
  823. {
  824. ECerr(EC_F_EC_ASN1_PARAMETERS2GROUP, ERR_R_EC_LIB);
  825. goto err;
  826. }
  827. /* extract the order */
  828. if ((a = ASN1_INTEGER_to_BN(params->order, a)) == NULL)
  829. {
  830. ECerr(EC_F_EC_ASN1_PARAMETERS2GROUP, ERR_R_ASN1_LIB);
  831. goto err;
  832. }
  833. if (BN_is_negative(a) || BN_is_zero(a))
  834. {
  835. ECerr(EC_F_EC_ASN1_PARAMETERS2GROUP, EC_R_INVALID_GROUP_ORDER);
  836. goto err;
  837. }
  838. if (BN_num_bits(a) > (int)field_bits + 1) /* Hasse bound */
  839. {
  840. ECerr(EC_F_EC_ASN1_PARAMETERS2GROUP, EC_R_INVALID_GROUP_ORDER);
  841. goto err;
  842. }
  843. /* extract the cofactor (optional) */
  844. if (params->cofactor == NULL)
  845. {
  846. if (b)
  847. {
  848. BN_free(b);
  849. b = NULL;
  850. }
  851. }
  852. else
  853. if ((b = ASN1_INTEGER_to_BN(params->cofactor, b)) == NULL)
  854. {
  855. ECerr(EC_F_EC_ASN1_PARAMETERS2GROUP, ERR_R_ASN1_LIB);
  856. goto err;
  857. }
  858. /* set the generator, order and cofactor (if present) */
  859. if (!EC_GROUP_set_generator(ret, point, a, b))
  860. {
  861. ECerr(EC_F_EC_ASN1_PARAMETERS2GROUP, ERR_R_EC_LIB);
  862. goto err;
  863. }
  864. ok = 1;
  865. err: if (!ok)
  866. {
  867. if (ret)
  868. EC_GROUP_clear_free(ret);
  869. ret = NULL;
  870. }
  871. if (p)
  872. BN_free(p);
  873. if (a)
  874. BN_free(a);
  875. if (b)
  876. BN_free(b);
  877. if (point)
  878. EC_POINT_free(point);
  879. return(ret);
  880. }
  881. EC_GROUP *ec_asn1_pkparameters2group(const ECPKPARAMETERS *params)
  882. {
  883. EC_GROUP *ret=NULL;
  884. int tmp=0;
  885. if (params == NULL)
  886. {
  887. ECerr(EC_F_EC_ASN1_PKPARAMETERS2GROUP,
  888. EC_R_MISSING_PARAMETERS);
  889. return NULL;
  890. }
  891. if (params->type == 0)
  892. { /* the curve is given by an OID */
  893. tmp = OBJ_obj2nid(params->value.named_curve);
  894. if ((ret = EC_GROUP_new_by_curve_name(tmp)) == NULL)
  895. {
  896. ECerr(EC_F_EC_ASN1_PKPARAMETERS2GROUP,
  897. EC_R_EC_GROUP_NEW_BY_NAME_FAILURE);
  898. return NULL;
  899. }
  900. EC_GROUP_set_asn1_flag(ret, OPENSSL_EC_NAMED_CURVE);
  901. }
  902. else if (params->type == 1)
  903. { /* the parameters are given by a ECPARAMETERS
  904. * structure */
  905. ret = ec_asn1_parameters2group(params->value.parameters);
  906. if (!ret)
  907. {
  908. ECerr(EC_F_EC_ASN1_PKPARAMETERS2GROUP, ERR_R_EC_LIB);
  909. return NULL;
  910. }
  911. EC_GROUP_set_asn1_flag(ret, 0x0);
  912. }
  913. else if (params->type == 2)
  914. { /* implicitlyCA */
  915. return NULL;
  916. }
  917. else
  918. {
  919. ECerr(EC_F_EC_ASN1_PKPARAMETERS2GROUP, EC_R_ASN1_ERROR);
  920. return NULL;
  921. }
  922. return ret;
  923. }
  924. /* EC_GROUP <-> DER encoding of ECPKPARAMETERS */
  925. EC_GROUP *d2i_ECPKParameters(EC_GROUP **a, const unsigned char **in, long len)
  926. {
  927. EC_GROUP *group = NULL;
  928. ECPKPARAMETERS *params = NULL;
  929. if ((params = d2i_ECPKPARAMETERS(NULL, in, len)) == NULL)
  930. {
  931. ECerr(EC_F_D2I_ECPKPARAMETERS, EC_R_D2I_ECPKPARAMETERS_FAILURE);
  932. ECPKPARAMETERS_free(params);
  933. return NULL;
  934. }
  935. if ((group = ec_asn1_pkparameters2group(params)) == NULL)
  936. {
  937. ECerr(EC_F_D2I_ECPKPARAMETERS, EC_R_PKPARAMETERS2GROUP_FAILURE);
  938. return NULL;
  939. }
  940. if (a && *a)
  941. EC_GROUP_clear_free(*a);
  942. if (a)
  943. *a = group;
  944. ECPKPARAMETERS_free(params);
  945. return(group);
  946. }
  947. int i2d_ECPKParameters(const EC_GROUP *a, unsigned char **out)
  948. {
  949. int ret=0;
  950. ECPKPARAMETERS *tmp = ec_asn1_group2pkparameters(a, NULL);
  951. if (tmp == NULL)
  952. {
  953. ECerr(EC_F_I2D_ECPKPARAMETERS, EC_R_GROUP2PKPARAMETERS_FAILURE);
  954. return 0;
  955. }
  956. if ((ret = i2d_ECPKPARAMETERS(tmp, out)) == 0)
  957. {
  958. ECerr(EC_F_I2D_ECPKPARAMETERS, EC_R_I2D_ECPKPARAMETERS_FAILURE);
  959. ECPKPARAMETERS_free(tmp);
  960. return 0;
  961. }
  962. ECPKPARAMETERS_free(tmp);
  963. return(ret);
  964. }
  965. /* some EC_KEY functions */
  966. EC_KEY *d2i_ECPrivateKey(EC_KEY **a, const unsigned char **in, long len)
  967. {
  968. int ok=0;
  969. EC_KEY *ret=NULL;
  970. EC_PRIVATEKEY *priv_key=NULL;
  971. if ((priv_key = EC_PRIVATEKEY_new()) == NULL)
  972. {
  973. ECerr(EC_F_D2I_ECPRIVATEKEY, ERR_R_MALLOC_FAILURE);
  974. return NULL;
  975. }
  976. if ((priv_key = d2i_EC_PRIVATEKEY(&priv_key, in, len)) == NULL)
  977. {
  978. ECerr(EC_F_D2I_ECPRIVATEKEY, ERR_R_EC_LIB);
  979. EC_PRIVATEKEY_free(priv_key);
  980. return NULL;
  981. }
  982. if (a == NULL || *a == NULL)
  983. {
  984. if ((ret = EC_KEY_new()) == NULL)
  985. {
  986. ECerr(EC_F_D2I_ECPRIVATEKEY,
  987. ERR_R_MALLOC_FAILURE);
  988. goto err;
  989. }
  990. if (a)
  991. *a = ret;
  992. }
  993. else
  994. ret = *a;
  995. if (priv_key->parameters)
  996. {
  997. if (ret->group)
  998. EC_GROUP_clear_free(ret->group);
  999. ret->group = ec_asn1_pkparameters2group(priv_key->parameters);
  1000. }
  1001. if (ret->group == NULL)
  1002. {
  1003. ECerr(EC_F_D2I_ECPRIVATEKEY, ERR_R_EC_LIB);
  1004. goto err;
  1005. }
  1006. ret->version = priv_key->version;
  1007. if (priv_key->privateKey)
  1008. {
  1009. ret->priv_key = BN_bin2bn(
  1010. M_ASN1_STRING_data(priv_key->privateKey),
  1011. M_ASN1_STRING_length(priv_key->privateKey),
  1012. ret->priv_key);
  1013. if (ret->priv_key == NULL)
  1014. {
  1015. ECerr(EC_F_D2I_ECPRIVATEKEY,
  1016. ERR_R_BN_LIB);
  1017. goto err;
  1018. }
  1019. }
  1020. else
  1021. {
  1022. ECerr(EC_F_D2I_ECPRIVATEKEY,
  1023. EC_R_MISSING_PRIVATE_KEY);
  1024. goto err;
  1025. }
  1026. if (priv_key->publicKey)
  1027. {
  1028. const unsigned char *pub_oct;
  1029. size_t pub_oct_len;
  1030. if (ret->pub_key)
  1031. EC_POINT_clear_free(ret->pub_key);
  1032. ret->pub_key = EC_POINT_new(ret->group);
  1033. if (ret->pub_key == NULL)
  1034. {
  1035. ECerr(EC_F_D2I_ECPRIVATEKEY, ERR_R_EC_LIB);
  1036. goto err;
  1037. }
  1038. pub_oct = M_ASN1_STRING_data(priv_key->publicKey);
  1039. pub_oct_len = M_ASN1_STRING_length(priv_key->publicKey);
  1040. /* save the point conversion form */
  1041. ret->conv_form = (point_conversion_form_t)(pub_oct[0] & ~0x01);
  1042. if (!EC_POINT_oct2point(ret->group, ret->pub_key,
  1043. pub_oct, pub_oct_len, NULL))
  1044. {
  1045. ECerr(EC_F_D2I_ECPRIVATEKEY, ERR_R_EC_LIB);
  1046. goto err;
  1047. }
  1048. }
  1049. ok = 1;
  1050. err:
  1051. if (!ok)
  1052. {
  1053. if (ret)
  1054. EC_KEY_free(ret);
  1055. ret = NULL;
  1056. }
  1057. if (priv_key)
  1058. EC_PRIVATEKEY_free(priv_key);
  1059. return(ret);
  1060. }
  1061. int i2d_ECPrivateKey(EC_KEY *a, unsigned char **out)
  1062. {
  1063. int ret=0, ok=0;
  1064. unsigned char *buffer=NULL;
  1065. size_t buf_len=0, tmp_len;
  1066. EC_PRIVATEKEY *priv_key=NULL;
  1067. if (a == NULL || a->group == NULL || a->priv_key == NULL)
  1068. {
  1069. ECerr(EC_F_I2D_ECPRIVATEKEY,
  1070. ERR_R_PASSED_NULL_PARAMETER);
  1071. goto err;
  1072. }
  1073. if ((priv_key = EC_PRIVATEKEY_new()) == NULL)
  1074. {
  1075. ECerr(EC_F_I2D_ECPRIVATEKEY,
  1076. ERR_R_MALLOC_FAILURE);
  1077. goto err;
  1078. }
  1079. priv_key->version = a->version;
  1080. buf_len = (size_t)BN_num_bytes(a->priv_key);
  1081. buffer = OPENSSL_malloc(buf_len);
  1082. if (buffer == NULL)
  1083. {
  1084. ECerr(EC_F_I2D_ECPRIVATEKEY,
  1085. ERR_R_MALLOC_FAILURE);
  1086. goto err;
  1087. }
  1088. if (!BN_bn2bin(a->priv_key, buffer))
  1089. {
  1090. ECerr(EC_F_I2D_ECPRIVATEKEY, ERR_R_BN_LIB);
  1091. goto err;
  1092. }
  1093. if (!M_ASN1_OCTET_STRING_set(priv_key->privateKey, buffer, buf_len))
  1094. {
  1095. ECerr(EC_F_I2D_ECPRIVATEKEY, ERR_R_ASN1_LIB);
  1096. goto err;
  1097. }
  1098. if (!(a->enc_flag & EC_PKEY_NO_PARAMETERS))
  1099. {
  1100. if ((priv_key->parameters = ec_asn1_group2pkparameters(
  1101. a->group, priv_key->parameters)) == NULL)
  1102. {
  1103. ECerr(EC_F_I2D_ECPRIVATEKEY, ERR_R_EC_LIB);
  1104. goto err;
  1105. }
  1106. }
  1107. if (!(a->enc_flag & EC_PKEY_NO_PUBKEY))
  1108. {
  1109. priv_key->publicKey = M_ASN1_BIT_STRING_new();
  1110. if (priv_key->publicKey == NULL)
  1111. {
  1112. ECerr(EC_F_I2D_ECPRIVATEKEY,
  1113. ERR_R_MALLOC_FAILURE);
  1114. goto err;
  1115. }
  1116. tmp_len = EC_POINT_point2oct(a->group, a->pub_key,
  1117. a->conv_form, NULL, 0, NULL);
  1118. if (tmp_len > buf_len)
  1119. {
  1120. unsigned char *tmp_buffer = OPENSSL_realloc(buffer, tmp_len);
  1121. if (!tmp_buffer)
  1122. {
  1123. ECerr(EC_F_I2D_ECPRIVATEKEY, ERR_R_MALLOC_FAILURE);
  1124. goto err;
  1125. }
  1126. buffer = tmp_buffer;
  1127. buf_len = tmp_len;
  1128. }
  1129. if (!EC_POINT_point2oct(a->group, a->pub_key,
  1130. a->conv_form, buffer, buf_len, NULL))
  1131. {
  1132. ECerr(EC_F_I2D_ECPRIVATEKEY, ERR_R_EC_LIB);
  1133. goto err;
  1134. }
  1135. priv_key->publicKey->flags &= ~(ASN1_STRING_FLAG_BITS_LEFT|0x07);
  1136. priv_key->publicKey->flags |= ASN1_STRING_FLAG_BITS_LEFT;
  1137. if (!M_ASN1_BIT_STRING_set(priv_key->publicKey, buffer,
  1138. buf_len))
  1139. {
  1140. ECerr(EC_F_I2D_ECPRIVATEKEY, ERR_R_ASN1_LIB);
  1141. goto err;
  1142. }
  1143. }
  1144. if ((ret = i2d_EC_PRIVATEKEY(priv_key, out)) == 0)
  1145. {
  1146. ECerr(EC_F_I2D_ECPRIVATEKEY, ERR_R_EC_LIB);
  1147. goto err;
  1148. }
  1149. ok=1;
  1150. err:
  1151. if (buffer)
  1152. OPENSSL_free(buffer);
  1153. if (priv_key)
  1154. EC_PRIVATEKEY_free(priv_key);
  1155. return(ok?ret:0);
  1156. }
  1157. int i2d_ECParameters(EC_KEY *a, unsigned char **out)
  1158. {
  1159. if (a == NULL)
  1160. {
  1161. ECerr(EC_F_I2D_ECPARAMETERS, ERR_R_PASSED_NULL_PARAMETER);
  1162. return 0;
  1163. }
  1164. return i2d_ECPKParameters(a->group, out);
  1165. }
  1166. EC_KEY *d2i_ECParameters(EC_KEY **a, const unsigned char **in, long len)
  1167. {
  1168. EC_KEY *ret;
  1169. if (in == NULL || *in == NULL)
  1170. {
  1171. ECerr(EC_F_D2I_ECPARAMETERS, ERR_R_PASSED_NULL_PARAMETER);
  1172. return NULL;
  1173. }
  1174. if (a == NULL || *a == NULL)
  1175. {
  1176. if ((ret = EC_KEY_new()) == NULL)
  1177. {
  1178. ECerr(EC_F_D2I_ECPARAMETERS, ERR_R_MALLOC_FAILURE);
  1179. return NULL;
  1180. }
  1181. if (a)
  1182. *a = ret;
  1183. }
  1184. else
  1185. ret = *a;
  1186. if (!d2i_ECPKParameters(&ret->group, in, len))
  1187. {
  1188. ECerr(EC_F_D2I_ECPARAMETERS, ERR_R_EC_LIB);
  1189. return NULL;
  1190. }
  1191. return ret;
  1192. }
  1193. EC_KEY *o2i_ECPublicKey(EC_KEY **a, const unsigned char **in, long len)
  1194. {
  1195. EC_KEY *ret=NULL;
  1196. if (a == NULL || (*a) == NULL || (*a)->group == NULL)
  1197. {
  1198. /* sorry, but a EC_GROUP-structur is necessary
  1199. * to set the public key */
  1200. ECerr(EC_F_O2I_ECPUBLICKEY, ERR_R_PASSED_NULL_PARAMETER);
  1201. return 0;
  1202. }
  1203. ret = *a;
  1204. if (ret->pub_key == NULL &&
  1205. (ret->pub_key = EC_POINT_new(ret->group)) == NULL)
  1206. {
  1207. ECerr(EC_F_O2I_ECPUBLICKEY, ERR_R_MALLOC_FAILURE);
  1208. return 0;
  1209. }
  1210. if (!EC_POINT_oct2point(ret->group, ret->pub_key, *in, len, NULL))
  1211. {
  1212. ECerr(EC_F_O2I_ECPUBLICKEY, ERR_R_EC_LIB);
  1213. return 0;
  1214. }
  1215. /* save the point conversion form */
  1216. ret->conv_form = (point_conversion_form_t)(*in[0] & ~0x01);
  1217. *in += len;
  1218. return ret;
  1219. }
  1220. int i2o_ECPublicKey(EC_KEY *a, unsigned char **out)
  1221. {
  1222. size_t buf_len=0;
  1223. int new_buffer = 0;
  1224. if (a == NULL)
  1225. {
  1226. ECerr(EC_F_I2O_ECPUBLICKEY, ERR_R_PASSED_NULL_PARAMETER);
  1227. return 0;
  1228. }
  1229. buf_len = EC_POINT_point2oct(a->group, a->pub_key,
  1230. a->conv_form, NULL, 0, NULL);
  1231. if (out == NULL || buf_len == 0)
  1232. /* out == NULL => just return the length of the octet string */
  1233. return buf_len;
  1234. if (*out == NULL)
  1235. {
  1236. if ((*out = OPENSSL_malloc(buf_len)) == NULL)
  1237. {
  1238. ECerr(EC_F_I2O_ECPUBLICKEY, ERR_R_MALLOC_FAILURE);
  1239. return 0;
  1240. }
  1241. new_buffer = 1;
  1242. }
  1243. if (!EC_POINT_point2oct(a->group, a->pub_key, a->conv_form,
  1244. *out, buf_len, NULL))
  1245. {
  1246. ECerr(EC_F_I2O_ECPUBLICKEY, ERR_R_EC_LIB);
  1247. OPENSSL_free(*out);
  1248. *out = NULL;
  1249. return 0;
  1250. }
  1251. if (!new_buffer)
  1252. *out += buf_len;
  1253. return buf_len;
  1254. }