ec_asn1.c 34 KB

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