ec_asn1.c 40 KB

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