2
0

ec_asn1.c 39 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294
  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. ASN1_OBJECT_free(field->fieldType);
  279. ASN1_TYPE_free(field->p.other);
  280. nid = EC_METHOD_get_field_type(EC_GROUP_method_of(group));
  281. /* set OID for the field */
  282. if ((field->fieldType = OBJ_nid2obj(nid)) == NULL) {
  283. ECerr(EC_F_EC_ASN1_GROUP2FIELDID, ERR_R_OBJ_LIB);
  284. goto err;
  285. }
  286. if (nid == NID_X9_62_prime_field) {
  287. if ((tmp = BN_new()) == NULL) {
  288. ECerr(EC_F_EC_ASN1_GROUP2FIELDID, ERR_R_MALLOC_FAILURE);
  289. goto err;
  290. }
  291. /* the parameters are specified by the prime number p */
  292. if (!EC_GROUP_get_curve_GFp(group, tmp, NULL, NULL, NULL)) {
  293. ECerr(EC_F_EC_ASN1_GROUP2FIELDID, ERR_R_EC_LIB);
  294. goto err;
  295. }
  296. /* set the prime number */
  297. field->p.prime = BN_to_ASN1_INTEGER(tmp, NULL);
  298. if (field->p.prime == NULL) {
  299. ECerr(EC_F_EC_ASN1_GROUP2FIELDID, ERR_R_ASN1_LIB);
  300. goto err;
  301. }
  302. } else /* nid == NID_X9_62_characteristic_two_field */
  303. #ifdef OPENSSL_NO_EC2M
  304. {
  305. ECerr(EC_F_EC_ASN1_GROUP2FIELDID, EC_R_GF2M_NOT_SUPPORTED);
  306. goto err;
  307. }
  308. #else
  309. {
  310. int field_type;
  311. X9_62_CHARACTERISTIC_TWO *char_two;
  312. field->p.char_two = X9_62_CHARACTERISTIC_TWO_new();
  313. char_two = field->p.char_two;
  314. if (char_two == NULL) {
  315. ECerr(EC_F_EC_ASN1_GROUP2FIELDID, ERR_R_MALLOC_FAILURE);
  316. goto err;
  317. }
  318. char_two->m = (long)EC_GROUP_get_degree(group);
  319. field_type = EC_GROUP_get_basis_type(group);
  320. if (field_type == 0) {
  321. ECerr(EC_F_EC_ASN1_GROUP2FIELDID, ERR_R_EC_LIB);
  322. goto err;
  323. }
  324. /* set base type OID */
  325. if ((char_two->type = OBJ_nid2obj(field_type)) == NULL) {
  326. ECerr(EC_F_EC_ASN1_GROUP2FIELDID, ERR_R_OBJ_LIB);
  327. goto err;
  328. }
  329. if (field_type == NID_X9_62_tpBasis) {
  330. unsigned int k;
  331. if (!EC_GROUP_get_trinomial_basis(group, &k))
  332. goto err;
  333. char_two->p.tpBasis = ASN1_INTEGER_new();
  334. if (!char_two->p.tpBasis) {
  335. ECerr(EC_F_EC_ASN1_GROUP2FIELDID, ERR_R_MALLOC_FAILURE);
  336. goto err;
  337. }
  338. if (!ASN1_INTEGER_set(char_two->p.tpBasis, (long)k)) {
  339. ECerr(EC_F_EC_ASN1_GROUP2FIELDID, ERR_R_ASN1_LIB);
  340. goto err;
  341. }
  342. } else if (field_type == NID_X9_62_ppBasis) {
  343. unsigned int k1, k2, k3;
  344. if (!EC_GROUP_get_pentanomial_basis(group, &k1, &k2, &k3))
  345. goto err;
  346. char_two->p.ppBasis = X9_62_PENTANOMIAL_new();
  347. if (!char_two->p.ppBasis) {
  348. ECerr(EC_F_EC_ASN1_GROUP2FIELDID, ERR_R_MALLOC_FAILURE);
  349. goto err;
  350. }
  351. /* set k? values */
  352. char_two->p.ppBasis->k1 = (long)k1;
  353. char_two->p.ppBasis->k2 = (long)k2;
  354. char_two->p.ppBasis->k3 = (long)k3;
  355. } else { /* field_type == NID_X9_62_onBasis */
  356. /* for ONB the parameters are (asn1) NULL */
  357. char_two->p.onBasis = ASN1_NULL_new();
  358. if (!char_two->p.onBasis) {
  359. ECerr(EC_F_EC_ASN1_GROUP2FIELDID, ERR_R_MALLOC_FAILURE);
  360. goto err;
  361. }
  362. }
  363. }
  364. #endif
  365. ok = 1;
  366. err:
  367. BN_free(tmp);
  368. return (ok);
  369. }
  370. static int ec_asn1_group2curve(const EC_GROUP *group, X9_62_CURVE *curve)
  371. {
  372. int ok = 0, nid;
  373. BIGNUM *tmp_1 = NULL, *tmp_2 = NULL;
  374. unsigned char *buffer_1 = NULL, *buffer_2 = NULL,
  375. *a_buf = NULL, *b_buf = NULL;
  376. size_t len_1, len_2;
  377. unsigned char char_zero = 0;
  378. if (!group || !curve || !curve->a || !curve->b)
  379. return 0;
  380. if ((tmp_1 = BN_new()) == NULL || (tmp_2 = BN_new()) == NULL) {
  381. ECerr(EC_F_EC_ASN1_GROUP2CURVE, ERR_R_MALLOC_FAILURE);
  382. goto err;
  383. }
  384. nid = EC_METHOD_get_field_type(EC_GROUP_method_of(group));
  385. /* get a and b */
  386. if (nid == NID_X9_62_prime_field) {
  387. if (!EC_GROUP_get_curve_GFp(group, NULL, tmp_1, tmp_2, NULL)) {
  388. ECerr(EC_F_EC_ASN1_GROUP2CURVE, ERR_R_EC_LIB);
  389. goto err;
  390. }
  391. }
  392. #ifndef OPENSSL_NO_EC2M
  393. else { /* nid == NID_X9_62_characteristic_two_field */
  394. if (!EC_GROUP_get_curve_GF2m(group, NULL, tmp_1, tmp_2, NULL)) {
  395. ECerr(EC_F_EC_ASN1_GROUP2CURVE, ERR_R_EC_LIB);
  396. goto err;
  397. }
  398. }
  399. #endif
  400. len_1 = (size_t)BN_num_bytes(tmp_1);
  401. len_2 = (size_t)BN_num_bytes(tmp_2);
  402. if (len_1 == 0) {
  403. /* len_1 == 0 => a == 0 */
  404. a_buf = &char_zero;
  405. len_1 = 1;
  406. } else {
  407. if ((buffer_1 = OPENSSL_malloc(len_1)) == NULL) {
  408. ECerr(EC_F_EC_ASN1_GROUP2CURVE, ERR_R_MALLOC_FAILURE);
  409. goto err;
  410. }
  411. if ((len_1 = BN_bn2bin(tmp_1, buffer_1)) == 0) {
  412. ECerr(EC_F_EC_ASN1_GROUP2CURVE, ERR_R_BN_LIB);
  413. goto err;
  414. }
  415. a_buf = buffer_1;
  416. }
  417. if (len_2 == 0) {
  418. /* len_2 == 0 => b == 0 */
  419. b_buf = &char_zero;
  420. len_2 = 1;
  421. } else {
  422. if ((buffer_2 = OPENSSL_malloc(len_2)) == NULL) {
  423. ECerr(EC_F_EC_ASN1_GROUP2CURVE, ERR_R_MALLOC_FAILURE);
  424. goto err;
  425. }
  426. if ((len_2 = BN_bn2bin(tmp_2, buffer_2)) == 0) {
  427. ECerr(EC_F_EC_ASN1_GROUP2CURVE, ERR_R_BN_LIB);
  428. goto err;
  429. }
  430. b_buf = buffer_2;
  431. }
  432. /* set a and b */
  433. if (!ASN1_OCTET_STRING_set(curve->a, a_buf, len_1) ||
  434. !ASN1_OCTET_STRING_set(curve->b, b_buf, len_2)) {
  435. ECerr(EC_F_EC_ASN1_GROUP2CURVE, ERR_R_ASN1_LIB);
  436. goto err;
  437. }
  438. /* set the seed (optional) */
  439. if (group->seed) {
  440. if (!curve->seed)
  441. if ((curve->seed = ASN1_BIT_STRING_new()) == NULL) {
  442. ECerr(EC_F_EC_ASN1_GROUP2CURVE, ERR_R_MALLOC_FAILURE);
  443. goto err;
  444. }
  445. curve->seed->flags &= ~(ASN1_STRING_FLAG_BITS_LEFT | 0x07);
  446. curve->seed->flags |= ASN1_STRING_FLAG_BITS_LEFT;
  447. if (!ASN1_BIT_STRING_set(curve->seed, group->seed,
  448. (int)group->seed_len)) {
  449. ECerr(EC_F_EC_ASN1_GROUP2CURVE, ERR_R_ASN1_LIB);
  450. goto err;
  451. }
  452. } else {
  453. ASN1_BIT_STRING_free(curve->seed);
  454. curve->seed = NULL;
  455. }
  456. ok = 1;
  457. err:
  458. OPENSSL_free(buffer_1);
  459. OPENSSL_free(buffer_2);
  460. BN_free(tmp_1);
  461. BN_free(tmp_2);
  462. return (ok);
  463. }
  464. static ECPARAMETERS *ec_asn1_group2parameters(const EC_GROUP *group,
  465. ECPARAMETERS *param)
  466. {
  467. size_t len = 0;
  468. ECPARAMETERS *ret = NULL;
  469. BIGNUM *tmp = NULL;
  470. unsigned char *buffer = NULL;
  471. const EC_POINT *point = NULL;
  472. point_conversion_form_t form;
  473. if ((tmp = BN_new()) == NULL) {
  474. ECerr(EC_F_EC_ASN1_GROUP2PARAMETERS, ERR_R_MALLOC_FAILURE);
  475. goto err;
  476. }
  477. if (param == NULL) {
  478. if ((ret = ECPARAMETERS_new()) == NULL) {
  479. ECerr(EC_F_EC_ASN1_GROUP2PARAMETERS, ERR_R_MALLOC_FAILURE);
  480. goto err;
  481. }
  482. } else
  483. ret = param;
  484. /* set the version (always one) */
  485. ret->version = (long)0x1;
  486. /* set the fieldID */
  487. if (!ec_asn1_group2fieldid(group, ret->fieldID)) {
  488. ECerr(EC_F_EC_ASN1_GROUP2PARAMETERS, ERR_R_EC_LIB);
  489. goto err;
  490. }
  491. /* set the curve */
  492. if (!ec_asn1_group2curve(group, ret->curve)) {
  493. ECerr(EC_F_EC_ASN1_GROUP2PARAMETERS, ERR_R_EC_LIB);
  494. goto err;
  495. }
  496. /* set the base point */
  497. if ((point = EC_GROUP_get0_generator(group)) == NULL) {
  498. ECerr(EC_F_EC_ASN1_GROUP2PARAMETERS, EC_R_UNDEFINED_GENERATOR);
  499. goto err;
  500. }
  501. form = EC_GROUP_get_point_conversion_form(group);
  502. len = EC_POINT_point2oct(group, point, form, NULL, len, NULL);
  503. if (len == 0) {
  504. ECerr(EC_F_EC_ASN1_GROUP2PARAMETERS, ERR_R_EC_LIB);
  505. goto err;
  506. }
  507. if ((buffer = OPENSSL_malloc(len)) == NULL) {
  508. ECerr(EC_F_EC_ASN1_GROUP2PARAMETERS, ERR_R_MALLOC_FAILURE);
  509. goto err;
  510. }
  511. if (!EC_POINT_point2oct(group, point, form, buffer, len, NULL)) {
  512. ECerr(EC_F_EC_ASN1_GROUP2PARAMETERS, ERR_R_EC_LIB);
  513. goto err;
  514. }
  515. if (ret->base == NULL && (ret->base = ASN1_OCTET_STRING_new()) == NULL) {
  516. ECerr(EC_F_EC_ASN1_GROUP2PARAMETERS, ERR_R_MALLOC_FAILURE);
  517. goto err;
  518. }
  519. if (!ASN1_OCTET_STRING_set(ret->base, buffer, len)) {
  520. ECerr(EC_F_EC_ASN1_GROUP2PARAMETERS, ERR_R_ASN1_LIB);
  521. goto err;
  522. }
  523. /* set the order */
  524. if (!EC_GROUP_get_order(group, tmp, NULL)) {
  525. ECerr(EC_F_EC_ASN1_GROUP2PARAMETERS, ERR_R_EC_LIB);
  526. goto err;
  527. }
  528. ret->order = BN_to_ASN1_INTEGER(tmp, ret->order);
  529. if (ret->order == NULL) {
  530. ECerr(EC_F_EC_ASN1_GROUP2PARAMETERS, ERR_R_ASN1_LIB);
  531. goto err;
  532. }
  533. /* set the cofactor (optional) */
  534. if (EC_GROUP_get_cofactor(group, tmp, NULL)) {
  535. ret->cofactor = BN_to_ASN1_INTEGER(tmp, ret->cofactor);
  536. if (ret->cofactor == NULL) {
  537. ECerr(EC_F_EC_ASN1_GROUP2PARAMETERS, ERR_R_ASN1_LIB);
  538. goto err;
  539. }
  540. }
  541. return ret;
  542. err:
  543. if (!param)
  544. ECPARAMETERS_free(ret);
  545. BN_free(tmp);
  546. OPENSSL_free(buffer);
  547. return NULL;
  548. }
  549. ECPKPARAMETERS *ec_asn1_group2pkparameters(const EC_GROUP *group,
  550. ECPKPARAMETERS *params)
  551. {
  552. int ok = 1, tmp;
  553. ECPKPARAMETERS *ret = params;
  554. if (ret == NULL) {
  555. if ((ret = ECPKPARAMETERS_new()) == NULL) {
  556. ECerr(EC_F_EC_ASN1_GROUP2PKPARAMETERS, ERR_R_MALLOC_FAILURE);
  557. return NULL;
  558. }
  559. } else {
  560. if (ret->type == 0)
  561. ASN1_OBJECT_free(ret->value.named_curve);
  562. else if (ret->type == 1 && ret->value.parameters)
  563. ECPARAMETERS_free(ret->value.parameters);
  564. }
  565. if (EC_GROUP_get_asn1_flag(group)) {
  566. /*
  567. * use the asn1 OID to describe the the elliptic curve parameters
  568. */
  569. tmp = EC_GROUP_get_curve_name(group);
  570. if (tmp) {
  571. ret->type = 0;
  572. if ((ret->value.named_curve = OBJ_nid2obj(tmp)) == NULL)
  573. ok = 0;
  574. } else
  575. /* we don't kmow the nid => ERROR */
  576. ok = 0;
  577. } else {
  578. /* use the ECPARAMETERS structure */
  579. ret->type = 1;
  580. if ((ret->value.parameters =
  581. ec_asn1_group2parameters(group, NULL)) == NULL)
  582. ok = 0;
  583. }
  584. if (!ok) {
  585. ECPKPARAMETERS_free(ret);
  586. return NULL;
  587. }
  588. return ret;
  589. }
  590. static EC_GROUP *ec_asn1_parameters2group(const ECPARAMETERS *params)
  591. {
  592. int ok = 0, tmp;
  593. EC_GROUP *ret = NULL;
  594. BIGNUM *p = NULL, *a = NULL, *b = NULL;
  595. EC_POINT *point = NULL;
  596. long field_bits;
  597. if (!params->fieldID || !params->fieldID->fieldType ||
  598. !params->fieldID->p.ptr) {
  599. ECerr(EC_F_EC_ASN1_PARAMETERS2GROUP, EC_R_ASN1_ERROR);
  600. goto err;
  601. }
  602. /* now extract the curve parameters a and b */
  603. if (!params->curve || !params->curve->a ||
  604. !params->curve->a->data || !params->curve->b ||
  605. !params->curve->b->data) {
  606. ECerr(EC_F_EC_ASN1_PARAMETERS2GROUP, EC_R_ASN1_ERROR);
  607. goto err;
  608. }
  609. a = BN_bin2bn(params->curve->a->data, params->curve->a->length, NULL);
  610. if (a == NULL) {
  611. ECerr(EC_F_EC_ASN1_PARAMETERS2GROUP, ERR_R_BN_LIB);
  612. goto err;
  613. }
  614. b = BN_bin2bn(params->curve->b->data, params->curve->b->length, NULL);
  615. if (b == NULL) {
  616. ECerr(EC_F_EC_ASN1_PARAMETERS2GROUP, ERR_R_BN_LIB);
  617. goto err;
  618. }
  619. /* get the field parameters */
  620. tmp = OBJ_obj2nid(params->fieldID->fieldType);
  621. if (tmp == NID_X9_62_characteristic_two_field)
  622. #ifdef OPENSSL_NO_EC2M
  623. {
  624. ECerr(EC_F_EC_ASN1_PARAMETERS2GROUP, EC_R_GF2M_NOT_SUPPORTED);
  625. goto err;
  626. }
  627. #else
  628. {
  629. X9_62_CHARACTERISTIC_TWO *char_two;
  630. char_two = params->fieldID->p.char_two;
  631. field_bits = char_two->m;
  632. if (field_bits > OPENSSL_ECC_MAX_FIELD_BITS) {
  633. ECerr(EC_F_EC_ASN1_PARAMETERS2GROUP, EC_R_FIELD_TOO_LARGE);
  634. goto err;
  635. }
  636. if ((p = BN_new()) == NULL) {
  637. ECerr(EC_F_EC_ASN1_PARAMETERS2GROUP, ERR_R_MALLOC_FAILURE);
  638. goto err;
  639. }
  640. /* get the base type */
  641. tmp = OBJ_obj2nid(char_two->type);
  642. if (tmp == NID_X9_62_tpBasis) {
  643. long tmp_long;
  644. if (!char_two->p.tpBasis) {
  645. ECerr(EC_F_EC_ASN1_PARAMETERS2GROUP, EC_R_ASN1_ERROR);
  646. goto err;
  647. }
  648. tmp_long = ASN1_INTEGER_get(char_two->p.tpBasis);
  649. if (!(char_two->m > tmp_long && tmp_long > 0)) {
  650. ECerr(EC_F_EC_ASN1_PARAMETERS2GROUP,
  651. EC_R_INVALID_TRINOMIAL_BASIS);
  652. goto err;
  653. }
  654. /* create the polynomial */
  655. if (!BN_set_bit(p, (int)char_two->m))
  656. goto err;
  657. if (!BN_set_bit(p, (int)tmp_long))
  658. goto err;
  659. if (!BN_set_bit(p, 0))
  660. goto err;
  661. } else if (tmp == NID_X9_62_ppBasis) {
  662. X9_62_PENTANOMIAL *penta;
  663. penta = char_two->p.ppBasis;
  664. if (!penta) {
  665. ECerr(EC_F_EC_ASN1_PARAMETERS2GROUP, EC_R_ASN1_ERROR);
  666. goto err;
  667. }
  668. if (!
  669. (char_two->m > penta->k3 && penta->k3 > penta->k2
  670. && penta->k2 > penta->k1 && penta->k1 > 0)) {
  671. ECerr(EC_F_EC_ASN1_PARAMETERS2GROUP,
  672. EC_R_INVALID_PENTANOMIAL_BASIS);
  673. goto err;
  674. }
  675. /* create the polynomial */
  676. if (!BN_set_bit(p, (int)char_two->m))
  677. goto err;
  678. if (!BN_set_bit(p, (int)penta->k1))
  679. goto err;
  680. if (!BN_set_bit(p, (int)penta->k2))
  681. goto err;
  682. if (!BN_set_bit(p, (int)penta->k3))
  683. goto err;
  684. if (!BN_set_bit(p, 0))
  685. goto err;
  686. } else if (tmp == NID_X9_62_onBasis) {
  687. ECerr(EC_F_EC_ASN1_PARAMETERS2GROUP, EC_R_NOT_IMPLEMENTED);
  688. goto err;
  689. } else { /* error */
  690. ECerr(EC_F_EC_ASN1_PARAMETERS2GROUP, EC_R_ASN1_ERROR);
  691. goto err;
  692. }
  693. /* create the EC_GROUP structure */
  694. ret = EC_GROUP_new_curve_GF2m(p, a, b, NULL);
  695. }
  696. #endif
  697. else if (tmp == NID_X9_62_prime_field) {
  698. /* we have a curve over a prime field */
  699. /* extract the prime number */
  700. if (!params->fieldID->p.prime) {
  701. ECerr(EC_F_EC_ASN1_PARAMETERS2GROUP, EC_R_ASN1_ERROR);
  702. goto err;
  703. }
  704. p = ASN1_INTEGER_to_BN(params->fieldID->p.prime, NULL);
  705. if (p == NULL) {
  706. ECerr(EC_F_EC_ASN1_PARAMETERS2GROUP, ERR_R_ASN1_LIB);
  707. goto err;
  708. }
  709. if (BN_is_negative(p) || BN_is_zero(p)) {
  710. ECerr(EC_F_EC_ASN1_PARAMETERS2GROUP, EC_R_INVALID_FIELD);
  711. goto err;
  712. }
  713. field_bits = BN_num_bits(p);
  714. if (field_bits > OPENSSL_ECC_MAX_FIELD_BITS) {
  715. ECerr(EC_F_EC_ASN1_PARAMETERS2GROUP, EC_R_FIELD_TOO_LARGE);
  716. goto err;
  717. }
  718. /* create the EC_GROUP structure */
  719. ret = EC_GROUP_new_curve_GFp(p, a, b, NULL);
  720. } else {
  721. ECerr(EC_F_EC_ASN1_PARAMETERS2GROUP, EC_R_INVALID_FIELD);
  722. goto err;
  723. }
  724. if (ret == NULL) {
  725. ECerr(EC_F_EC_ASN1_PARAMETERS2GROUP, ERR_R_EC_LIB);
  726. goto err;
  727. }
  728. /* extract seed (optional) */
  729. if (params->curve->seed != NULL) {
  730. OPENSSL_free(ret->seed);
  731. if ((ret->seed = OPENSSL_malloc(params->curve->seed->length)) == NULL) {
  732. ECerr(EC_F_EC_ASN1_PARAMETERS2GROUP, ERR_R_MALLOC_FAILURE);
  733. goto err;
  734. }
  735. memcpy(ret->seed, params->curve->seed->data,
  736. params->curve->seed->length);
  737. ret->seed_len = params->curve->seed->length;
  738. }
  739. if (!params->order || !params->base || !params->base->data) {
  740. ECerr(EC_F_EC_ASN1_PARAMETERS2GROUP, EC_R_ASN1_ERROR);
  741. goto err;
  742. }
  743. if ((point = EC_POINT_new(ret)) == NULL)
  744. goto err;
  745. /* set the point conversion form */
  746. EC_GROUP_set_point_conversion_form(ret, (point_conversion_form_t)
  747. (params->base->data[0] & ~0x01));
  748. /* extract the ec point */
  749. if (!EC_POINT_oct2point(ret, point, params->base->data,
  750. params->base->length, NULL)) {
  751. ECerr(EC_F_EC_ASN1_PARAMETERS2GROUP, ERR_R_EC_LIB);
  752. goto err;
  753. }
  754. /* extract the order */
  755. if ((a = ASN1_INTEGER_to_BN(params->order, a)) == NULL) {
  756. ECerr(EC_F_EC_ASN1_PARAMETERS2GROUP, ERR_R_ASN1_LIB);
  757. goto err;
  758. }
  759. if (BN_is_negative(a) || BN_is_zero(a)) {
  760. ECerr(EC_F_EC_ASN1_PARAMETERS2GROUP, EC_R_INVALID_GROUP_ORDER);
  761. goto err;
  762. }
  763. if (BN_num_bits(a) > (int)field_bits + 1) { /* Hasse bound */
  764. ECerr(EC_F_EC_ASN1_PARAMETERS2GROUP, EC_R_INVALID_GROUP_ORDER);
  765. goto err;
  766. }
  767. /* extract the cofactor (optional) */
  768. if (params->cofactor == NULL) {
  769. BN_free(b);
  770. b = NULL;
  771. } else if ((b = ASN1_INTEGER_to_BN(params->cofactor, b)) == NULL) {
  772. ECerr(EC_F_EC_ASN1_PARAMETERS2GROUP, ERR_R_ASN1_LIB);
  773. goto err;
  774. }
  775. /* set the generator, order and cofactor (if present) */
  776. if (!EC_GROUP_set_generator(ret, point, a, b)) {
  777. ECerr(EC_F_EC_ASN1_PARAMETERS2GROUP, ERR_R_EC_LIB);
  778. goto err;
  779. }
  780. ok = 1;
  781. err:
  782. if (!ok) {
  783. EC_GROUP_clear_free(ret);
  784. ret = NULL;
  785. }
  786. BN_free(p);
  787. BN_free(a);
  788. BN_free(b);
  789. EC_POINT_free(point);
  790. return (ret);
  791. }
  792. EC_GROUP *ec_asn1_pkparameters2group(const ECPKPARAMETERS *params)
  793. {
  794. EC_GROUP *ret = NULL;
  795. int tmp = 0;
  796. if (params == NULL) {
  797. ECerr(EC_F_EC_ASN1_PKPARAMETERS2GROUP, EC_R_MISSING_PARAMETERS);
  798. return NULL;
  799. }
  800. if (params->type == 0) { /* the curve is given by an OID */
  801. tmp = OBJ_obj2nid(params->value.named_curve);
  802. if ((ret = EC_GROUP_new_by_curve_name(tmp)) == NULL) {
  803. ECerr(EC_F_EC_ASN1_PKPARAMETERS2GROUP,
  804. EC_R_EC_GROUP_NEW_BY_NAME_FAILURE);
  805. return NULL;
  806. }
  807. EC_GROUP_set_asn1_flag(ret, OPENSSL_EC_NAMED_CURVE);
  808. } else if (params->type == 1) { /* the parameters are given by a
  809. * ECPARAMETERS structure */
  810. ret = ec_asn1_parameters2group(params->value.parameters);
  811. if (!ret) {
  812. ECerr(EC_F_EC_ASN1_PKPARAMETERS2GROUP, ERR_R_EC_LIB);
  813. return NULL;
  814. }
  815. EC_GROUP_set_asn1_flag(ret, 0x0);
  816. } else if (params->type == 2) { /* implicitlyCA */
  817. return NULL;
  818. } else {
  819. ECerr(EC_F_EC_ASN1_PKPARAMETERS2GROUP, EC_R_ASN1_ERROR);
  820. return NULL;
  821. }
  822. return ret;
  823. }
  824. /* EC_GROUP <-> DER encoding of ECPKPARAMETERS */
  825. EC_GROUP *d2i_ECPKParameters(EC_GROUP **a, const unsigned char **in, long len)
  826. {
  827. EC_GROUP *group = NULL;
  828. ECPKPARAMETERS *params = NULL;
  829. if ((params = d2i_ECPKPARAMETERS(NULL, in, len)) == NULL) {
  830. ECerr(EC_F_D2I_ECPKPARAMETERS, EC_R_D2I_ECPKPARAMETERS_FAILURE);
  831. ECPKPARAMETERS_free(params);
  832. return NULL;
  833. }
  834. if ((group = ec_asn1_pkparameters2group(params)) == NULL) {
  835. ECerr(EC_F_D2I_ECPKPARAMETERS, EC_R_PKPARAMETERS2GROUP_FAILURE);
  836. ECPKPARAMETERS_free(params);
  837. return NULL;
  838. }
  839. if (a) {
  840. EC_GROUP_clear_free(*a);
  841. *a = group;
  842. }
  843. ECPKPARAMETERS_free(params);
  844. return (group);
  845. }
  846. int i2d_ECPKParameters(const EC_GROUP *a, unsigned char **out)
  847. {
  848. int ret = 0;
  849. ECPKPARAMETERS *tmp = ec_asn1_group2pkparameters(a, NULL);
  850. if (tmp == NULL) {
  851. ECerr(EC_F_I2D_ECPKPARAMETERS, EC_R_GROUP2PKPARAMETERS_FAILURE);
  852. return 0;
  853. }
  854. if ((ret = i2d_ECPKPARAMETERS(tmp, out)) == 0) {
  855. ECerr(EC_F_I2D_ECPKPARAMETERS, EC_R_I2D_ECPKPARAMETERS_FAILURE);
  856. ECPKPARAMETERS_free(tmp);
  857. return 0;
  858. }
  859. ECPKPARAMETERS_free(tmp);
  860. return (ret);
  861. }
  862. /* some EC_KEY functions */
  863. EC_KEY *d2i_ECPrivateKey(EC_KEY **a, const unsigned char **in, long len)
  864. {
  865. EC_KEY *ret = NULL;
  866. EC_PRIVATEKEY *priv_key = NULL;
  867. if ((priv_key = d2i_EC_PRIVATEKEY(NULL, in, len)) == NULL) {
  868. ECerr(EC_F_D2I_ECPRIVATEKEY, ERR_R_EC_LIB);
  869. return NULL;
  870. }
  871. if (a == NULL || *a == NULL) {
  872. if ((ret = EC_KEY_new()) == NULL) {
  873. ECerr(EC_F_D2I_ECPRIVATEKEY, ERR_R_MALLOC_FAILURE);
  874. goto err;
  875. }
  876. } else
  877. ret = *a;
  878. if (priv_key->parameters) {
  879. EC_GROUP_clear_free(ret->group);
  880. ret->group = ec_asn1_pkparameters2group(priv_key->parameters);
  881. }
  882. if (ret->group == NULL) {
  883. ECerr(EC_F_D2I_ECPRIVATEKEY, ERR_R_EC_LIB);
  884. goto err;
  885. }
  886. ret->version = priv_key->version;
  887. if (priv_key->privateKey) {
  888. ret->priv_key = BN_bin2bn(ASN1_STRING_data(priv_key->privateKey),
  889. ASN1_STRING_length(priv_key->privateKey),
  890. ret->priv_key);
  891. if (ret->priv_key == NULL) {
  892. ECerr(EC_F_D2I_ECPRIVATEKEY, ERR_R_BN_LIB);
  893. goto err;
  894. }
  895. } else {
  896. ECerr(EC_F_D2I_ECPRIVATEKEY, EC_R_MISSING_PRIVATE_KEY);
  897. goto err;
  898. }
  899. EC_POINT_clear_free(ret->pub_key);
  900. ret->pub_key = EC_POINT_new(ret->group);
  901. if (ret->pub_key == NULL) {
  902. ECerr(EC_F_D2I_ECPRIVATEKEY, ERR_R_EC_LIB);
  903. goto err;
  904. }
  905. if (priv_key->publicKey) {
  906. const unsigned char *pub_oct;
  907. int pub_oct_len;
  908. pub_oct = ASN1_STRING_data(priv_key->publicKey);
  909. pub_oct_len = ASN1_STRING_length(priv_key->publicKey);
  910. /*
  911. * The first byte - point conversion form - must be present.
  912. */
  913. if (pub_oct_len <= 0) {
  914. ECerr(EC_F_D2I_ECPRIVATEKEY, EC_R_BUFFER_TOO_SMALL);
  915. goto err;
  916. }
  917. /* Save the point conversion form. */
  918. ret->conv_form = (point_conversion_form_t) (pub_oct[0] & ~0x01);
  919. if (!EC_POINT_oct2point(ret->group, ret->pub_key,
  920. pub_oct, (size_t)(pub_oct_len), NULL)) {
  921. ECerr(EC_F_D2I_ECPRIVATEKEY, ERR_R_EC_LIB);
  922. goto err;
  923. }
  924. } else {
  925. if (!EC_POINT_mul
  926. (ret->group, ret->pub_key, ret->priv_key, NULL, NULL, NULL)) {
  927. ECerr(EC_F_D2I_ECPRIVATEKEY, ERR_R_EC_LIB);
  928. goto err;
  929. }
  930. /* Remember the original private-key-only encoding. */
  931. ret->enc_flag |= EC_PKEY_NO_PUBKEY;
  932. }
  933. if (a)
  934. *a = ret;
  935. EC_PRIVATEKEY_free(priv_key);
  936. return (ret);
  937. err:
  938. if (a == NULL || *a != ret)
  939. EC_KEY_free(ret);
  940. EC_PRIVATEKEY_free(priv_key);
  941. return NULL;
  942. }
  943. int i2d_ECPrivateKey(EC_KEY *a, unsigned char **out)
  944. {
  945. int ret = 0, ok = 0;
  946. unsigned char *buffer = NULL;
  947. size_t buf_len = 0, tmp_len, bn_len;
  948. EC_PRIVATEKEY *priv_key = NULL;
  949. if (a == NULL || a->group == NULL || a->priv_key == NULL ||
  950. (!(a->enc_flag & EC_PKEY_NO_PUBKEY) && a->pub_key == NULL)) {
  951. ECerr(EC_F_I2D_ECPRIVATEKEY, ERR_R_PASSED_NULL_PARAMETER);
  952. goto err;
  953. }
  954. if ((priv_key = EC_PRIVATEKEY_new()) == NULL) {
  955. ECerr(EC_F_I2D_ECPRIVATEKEY, ERR_R_MALLOC_FAILURE);
  956. goto err;
  957. }
  958. priv_key->version = a->version;
  959. bn_len = (size_t)BN_num_bytes(a->priv_key);
  960. /* Octetstring may need leading zeros if BN is to short */
  961. buf_len = (EC_GROUP_get_degree(a->group) + 7) / 8;
  962. if (bn_len > buf_len) {
  963. ECerr(EC_F_I2D_ECPRIVATEKEY, EC_R_BUFFER_TOO_SMALL);
  964. goto err;
  965. }
  966. buffer = OPENSSL_malloc(buf_len);
  967. if (buffer == NULL) {
  968. ECerr(EC_F_I2D_ECPRIVATEKEY, ERR_R_MALLOC_FAILURE);
  969. goto err;
  970. }
  971. if (!BN_bn2bin(a->priv_key, buffer + buf_len - bn_len)) {
  972. ECerr(EC_F_I2D_ECPRIVATEKEY, ERR_R_BN_LIB);
  973. goto err;
  974. }
  975. if (buf_len - bn_len > 0) {
  976. memset(buffer, 0, buf_len - bn_len);
  977. }
  978. if (!ASN1_OCTET_STRING_set(priv_key->privateKey, buffer, buf_len)) {
  979. ECerr(EC_F_I2D_ECPRIVATEKEY, ERR_R_ASN1_LIB);
  980. goto err;
  981. }
  982. if (!(a->enc_flag & EC_PKEY_NO_PARAMETERS)) {
  983. if ((priv_key->parameters =
  984. ec_asn1_group2pkparameters(a->group,
  985. priv_key->parameters)) == NULL) {
  986. ECerr(EC_F_I2D_ECPRIVATEKEY, ERR_R_EC_LIB);
  987. goto err;
  988. }
  989. }
  990. if (!(a->enc_flag & EC_PKEY_NO_PUBKEY)) {
  991. priv_key->publicKey = ASN1_BIT_STRING_new();
  992. if (priv_key->publicKey == NULL) {
  993. ECerr(EC_F_I2D_ECPRIVATEKEY, ERR_R_MALLOC_FAILURE);
  994. goto err;
  995. }
  996. tmp_len = EC_POINT_point2oct(a->group, a->pub_key,
  997. a->conv_form, NULL, 0, NULL);
  998. if (tmp_len > buf_len) {
  999. unsigned char *tmp_buffer = OPENSSL_realloc(buffer, tmp_len);
  1000. if (!tmp_buffer) {
  1001. ECerr(EC_F_I2D_ECPRIVATEKEY, ERR_R_MALLOC_FAILURE);
  1002. goto err;
  1003. }
  1004. buffer = tmp_buffer;
  1005. buf_len = tmp_len;
  1006. }
  1007. if (!EC_POINT_point2oct(a->group, a->pub_key,
  1008. a->conv_form, buffer, buf_len, NULL)) {
  1009. ECerr(EC_F_I2D_ECPRIVATEKEY, ERR_R_EC_LIB);
  1010. goto err;
  1011. }
  1012. priv_key->publicKey->flags &= ~(ASN1_STRING_FLAG_BITS_LEFT | 0x07);
  1013. priv_key->publicKey->flags |= ASN1_STRING_FLAG_BITS_LEFT;
  1014. if (!ASN1_BIT_STRING_set(priv_key->publicKey, buffer, buf_len)) {
  1015. ECerr(EC_F_I2D_ECPRIVATEKEY, ERR_R_ASN1_LIB);
  1016. goto err;
  1017. }
  1018. }
  1019. if ((ret = i2d_EC_PRIVATEKEY(priv_key, out)) == 0) {
  1020. ECerr(EC_F_I2D_ECPRIVATEKEY, ERR_R_EC_LIB);
  1021. goto err;
  1022. }
  1023. ok = 1;
  1024. err:
  1025. OPENSSL_free(buffer);
  1026. EC_PRIVATEKEY_free(priv_key);
  1027. return (ok ? ret : 0);
  1028. }
  1029. int i2d_ECParameters(EC_KEY *a, unsigned char **out)
  1030. {
  1031. if (a == NULL) {
  1032. ECerr(EC_F_I2D_ECPARAMETERS, ERR_R_PASSED_NULL_PARAMETER);
  1033. return 0;
  1034. }
  1035. return i2d_ECPKParameters(a->group, out);
  1036. }
  1037. EC_KEY *d2i_ECParameters(EC_KEY **a, const unsigned char **in, long len)
  1038. {
  1039. EC_KEY *ret;
  1040. if (in == NULL || *in == NULL) {
  1041. ECerr(EC_F_D2I_ECPARAMETERS, ERR_R_PASSED_NULL_PARAMETER);
  1042. return NULL;
  1043. }
  1044. if (a == NULL || *a == NULL) {
  1045. if ((ret = EC_KEY_new()) == NULL) {
  1046. ECerr(EC_F_D2I_ECPARAMETERS, ERR_R_MALLOC_FAILURE);
  1047. return NULL;
  1048. }
  1049. } else
  1050. ret = *a;
  1051. if (!d2i_ECPKParameters(&ret->group, in, len)) {
  1052. ECerr(EC_F_D2I_ECPARAMETERS, ERR_R_EC_LIB);
  1053. if (a == NULL || *a != ret)
  1054. EC_KEY_free(ret);
  1055. return NULL;
  1056. }
  1057. if (a)
  1058. *a = ret;
  1059. return ret;
  1060. }
  1061. EC_KEY *o2i_ECPublicKey(EC_KEY **a, const unsigned char **in, long len)
  1062. {
  1063. EC_KEY *ret = NULL;
  1064. if (a == NULL || (*a) == NULL || (*a)->group == NULL) {
  1065. /*
  1066. * sorry, but a EC_GROUP-structur is necessary to set the public key
  1067. */
  1068. ECerr(EC_F_O2I_ECPUBLICKEY, ERR_R_PASSED_NULL_PARAMETER);
  1069. return 0;
  1070. }
  1071. ret = *a;
  1072. if (ret->pub_key == NULL &&
  1073. (ret->pub_key = EC_POINT_new(ret->group)) == NULL) {
  1074. ECerr(EC_F_O2I_ECPUBLICKEY, ERR_R_MALLOC_FAILURE);
  1075. return 0;
  1076. }
  1077. if (!EC_POINT_oct2point(ret->group, ret->pub_key, *in, len, NULL)) {
  1078. ECerr(EC_F_O2I_ECPUBLICKEY, ERR_R_EC_LIB);
  1079. return 0;
  1080. }
  1081. /* save the point conversion form */
  1082. ret->conv_form = (point_conversion_form_t) (*in[0] & ~0x01);
  1083. *in += len;
  1084. return ret;
  1085. }
  1086. int i2o_ECPublicKey(EC_KEY *a, unsigned char **out)
  1087. {
  1088. size_t buf_len = 0;
  1089. int new_buffer = 0;
  1090. if (a == NULL) {
  1091. ECerr(EC_F_I2O_ECPUBLICKEY, ERR_R_PASSED_NULL_PARAMETER);
  1092. return 0;
  1093. }
  1094. buf_len = EC_POINT_point2oct(a->group, a->pub_key,
  1095. a->conv_form, NULL, 0, NULL);
  1096. if (out == NULL || buf_len == 0)
  1097. /* out == NULL => just return the length of the octet string */
  1098. return buf_len;
  1099. if (*out == NULL) {
  1100. if ((*out = OPENSSL_malloc(buf_len)) == NULL) {
  1101. ECerr(EC_F_I2O_ECPUBLICKEY, ERR_R_MALLOC_FAILURE);
  1102. return 0;
  1103. }
  1104. new_buffer = 1;
  1105. }
  1106. if (!EC_POINT_point2oct(a->group, a->pub_key, a->conv_form,
  1107. *out, buf_len, NULL)) {
  1108. ECerr(EC_F_I2O_ECPUBLICKEY, ERR_R_EC_LIB);
  1109. if (new_buffer) {
  1110. OPENSSL_free(*out);
  1111. *out = NULL;
  1112. }
  1113. return 0;
  1114. }
  1115. if (!new_buffer)
  1116. *out += buf_len;
  1117. return buf_len;
  1118. }