ec_asn1.c 40 KB

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