ec_asn1.c 43 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411
  1. /* crypto/ec/ec_asn1.c */
  2. /*
  3. * Written by Nils Larsch for the OpenSSL project.
  4. */
  5. /* ====================================================================
  6. * Copyright (c) 2000-2019 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. * [email protected].
  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. * ([email protected]). This product includes software written by Tim
  55. * Hudson ([email protected]).
  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, *dup = NULL;
  610. BIGNUM *p = NULL, *a = NULL, *b = NULL;
  611. EC_POINT *point = NULL;
  612. long field_bits;
  613. int curve_name = NID_undef;
  614. BN_CTX *ctx = NULL;
  615. if (!params->fieldID || !params->fieldID->fieldType ||
  616. !params->fieldID->p.ptr) {
  617. ECerr(EC_F_EC_ASN1_PARAMETERS2GROUP, EC_R_ASN1_ERROR);
  618. goto err;
  619. }
  620. /* now extract the curve parameters a and b */
  621. if (!params->curve || !params->curve->a ||
  622. !params->curve->a->data || !params->curve->b ||
  623. !params->curve->b->data) {
  624. ECerr(EC_F_EC_ASN1_PARAMETERS2GROUP, EC_R_ASN1_ERROR);
  625. goto err;
  626. }
  627. a = BN_bin2bn(params->curve->a->data, params->curve->a->length, NULL);
  628. if (a == NULL) {
  629. ECerr(EC_F_EC_ASN1_PARAMETERS2GROUP, ERR_R_BN_LIB);
  630. goto err;
  631. }
  632. b = BN_bin2bn(params->curve->b->data, params->curve->b->length, NULL);
  633. if (b == NULL) {
  634. ECerr(EC_F_EC_ASN1_PARAMETERS2GROUP, ERR_R_BN_LIB);
  635. goto err;
  636. }
  637. /* get the field parameters */
  638. tmp = OBJ_obj2nid(params->fieldID->fieldType);
  639. if (tmp == NID_X9_62_characteristic_two_field)
  640. #ifdef OPENSSL_NO_EC2M
  641. {
  642. ECerr(EC_F_EC_ASN1_PARAMETERS2GROUP, EC_R_GF2M_NOT_SUPPORTED);
  643. goto err;
  644. }
  645. #else
  646. {
  647. X9_62_CHARACTERISTIC_TWO *char_two;
  648. char_two = params->fieldID->p.char_two;
  649. field_bits = char_two->m;
  650. if (field_bits > OPENSSL_ECC_MAX_FIELD_BITS) {
  651. ECerr(EC_F_EC_ASN1_PARAMETERS2GROUP, EC_R_FIELD_TOO_LARGE);
  652. goto err;
  653. }
  654. if ((p = BN_new()) == NULL) {
  655. ECerr(EC_F_EC_ASN1_PARAMETERS2GROUP, ERR_R_MALLOC_FAILURE);
  656. goto err;
  657. }
  658. /* get the base type */
  659. tmp = OBJ_obj2nid(char_two->type);
  660. if (tmp == NID_X9_62_tpBasis) {
  661. long tmp_long;
  662. if (!char_two->p.tpBasis) {
  663. ECerr(EC_F_EC_ASN1_PARAMETERS2GROUP, EC_R_ASN1_ERROR);
  664. goto err;
  665. }
  666. tmp_long = ASN1_INTEGER_get(char_two->p.tpBasis);
  667. if (!(char_two->m > tmp_long && tmp_long > 0)) {
  668. ECerr(EC_F_EC_ASN1_PARAMETERS2GROUP,
  669. EC_R_INVALID_TRINOMIAL_BASIS);
  670. goto err;
  671. }
  672. /* create the polynomial */
  673. if (!BN_set_bit(p, (int)char_two->m))
  674. goto err;
  675. if (!BN_set_bit(p, (int)tmp_long))
  676. goto err;
  677. if (!BN_set_bit(p, 0))
  678. goto err;
  679. } else if (tmp == NID_X9_62_ppBasis) {
  680. X9_62_PENTANOMIAL *penta;
  681. penta = char_two->p.ppBasis;
  682. if (!penta) {
  683. ECerr(EC_F_EC_ASN1_PARAMETERS2GROUP, EC_R_ASN1_ERROR);
  684. goto err;
  685. }
  686. if (!
  687. (char_two->m > penta->k3 && penta->k3 > penta->k2
  688. && penta->k2 > penta->k1 && penta->k1 > 0)) {
  689. ECerr(EC_F_EC_ASN1_PARAMETERS2GROUP,
  690. EC_R_INVALID_PENTANOMIAL_BASIS);
  691. goto err;
  692. }
  693. /* create the polynomial */
  694. if (!BN_set_bit(p, (int)char_two->m))
  695. goto err;
  696. if (!BN_set_bit(p, (int)penta->k1))
  697. goto err;
  698. if (!BN_set_bit(p, (int)penta->k2))
  699. goto err;
  700. if (!BN_set_bit(p, (int)penta->k3))
  701. goto err;
  702. if (!BN_set_bit(p, 0))
  703. goto err;
  704. } else if (tmp == NID_X9_62_onBasis) {
  705. ECerr(EC_F_EC_ASN1_PARAMETERS2GROUP, EC_R_NOT_IMPLEMENTED);
  706. goto err;
  707. } else { /* error */
  708. ECerr(EC_F_EC_ASN1_PARAMETERS2GROUP, EC_R_ASN1_ERROR);
  709. goto err;
  710. }
  711. /* create the EC_GROUP structure */
  712. ret = EC_GROUP_new_curve_GF2m(p, a, b, NULL);
  713. }
  714. #endif
  715. else if (tmp == NID_X9_62_prime_field) {
  716. /* we have a curve over a prime field */
  717. /* extract the prime number */
  718. if (!params->fieldID->p.prime) {
  719. ECerr(EC_F_EC_ASN1_PARAMETERS2GROUP, EC_R_ASN1_ERROR);
  720. goto err;
  721. }
  722. p = ASN1_INTEGER_to_BN(params->fieldID->p.prime, NULL);
  723. if (p == NULL) {
  724. ECerr(EC_F_EC_ASN1_PARAMETERS2GROUP, ERR_R_ASN1_LIB);
  725. goto err;
  726. }
  727. if (BN_is_negative(p) || BN_is_zero(p)) {
  728. ECerr(EC_F_EC_ASN1_PARAMETERS2GROUP, EC_R_INVALID_FIELD);
  729. goto err;
  730. }
  731. field_bits = BN_num_bits(p);
  732. if (field_bits > OPENSSL_ECC_MAX_FIELD_BITS) {
  733. ECerr(EC_F_EC_ASN1_PARAMETERS2GROUP, EC_R_FIELD_TOO_LARGE);
  734. goto err;
  735. }
  736. /* create the EC_GROUP structure */
  737. ret = EC_GROUP_new_curve_GFp(p, a, b, NULL);
  738. } else {
  739. ECerr(EC_F_EC_ASN1_PARAMETERS2GROUP, EC_R_INVALID_FIELD);
  740. goto err;
  741. }
  742. if (ret == NULL) {
  743. ECerr(EC_F_EC_ASN1_PARAMETERS2GROUP, ERR_R_EC_LIB);
  744. goto err;
  745. }
  746. /* extract seed (optional) */
  747. if (params->curve->seed != NULL) {
  748. if (ret->seed != NULL)
  749. OPENSSL_free(ret->seed);
  750. if (!(ret->seed = OPENSSL_malloc(params->curve->seed->length))) {
  751. ECerr(EC_F_EC_ASN1_PARAMETERS2GROUP, ERR_R_MALLOC_FAILURE);
  752. goto err;
  753. }
  754. memcpy(ret->seed, params->curve->seed->data,
  755. params->curve->seed->length);
  756. ret->seed_len = params->curve->seed->length;
  757. }
  758. if (!params->order || !params->base || !params->base->data) {
  759. ECerr(EC_F_EC_ASN1_PARAMETERS2GROUP, EC_R_ASN1_ERROR);
  760. goto err;
  761. }
  762. if ((point = EC_POINT_new(ret)) == NULL)
  763. goto err;
  764. /* set the point conversion form */
  765. EC_GROUP_set_point_conversion_form(ret, (point_conversion_form_t)
  766. (params->base->data[0] & ~0x01));
  767. /* extract the ec point */
  768. if (!EC_POINT_oct2point(ret, point, params->base->data,
  769. params->base->length, NULL)) {
  770. ECerr(EC_F_EC_ASN1_PARAMETERS2GROUP, ERR_R_EC_LIB);
  771. goto err;
  772. }
  773. /* extract the order */
  774. if ((a = ASN1_INTEGER_to_BN(params->order, a)) == NULL) {
  775. ECerr(EC_F_EC_ASN1_PARAMETERS2GROUP, ERR_R_ASN1_LIB);
  776. goto err;
  777. }
  778. if (BN_is_negative(a) || BN_is_zero(a)) {
  779. ECerr(EC_F_EC_ASN1_PARAMETERS2GROUP, EC_R_INVALID_GROUP_ORDER);
  780. goto err;
  781. }
  782. if (BN_num_bits(a) > (int)field_bits + 1) { /* Hasse bound */
  783. ECerr(EC_F_EC_ASN1_PARAMETERS2GROUP, EC_R_INVALID_GROUP_ORDER);
  784. goto err;
  785. }
  786. /* extract the cofactor (optional) */
  787. if (params->cofactor == NULL) {
  788. if (b) {
  789. BN_free(b);
  790. b = NULL;
  791. }
  792. } else if ((b = ASN1_INTEGER_to_BN(params->cofactor, b)) == NULL) {
  793. ECerr(EC_F_EC_ASN1_PARAMETERS2GROUP, ERR_R_ASN1_LIB);
  794. goto err;
  795. }
  796. /* set the generator, order and cofactor (if present) */
  797. if (!EC_GROUP_set_generator(ret, point, a, b)) {
  798. ECerr(EC_F_EC_ASN1_PARAMETERS2GROUP, ERR_R_EC_LIB);
  799. goto err;
  800. }
  801. /*
  802. * Check if the explicit parameters group just created matches one of the
  803. * built-in curves.
  804. *
  805. * We create a copy of the group just built, so that we can remove optional
  806. * fields for the lookup: we do this to avoid the possibility that one of
  807. * the optional parameters is used to force the library into using a less
  808. * performant and less secure EC_METHOD instead of the specialized one.
  809. * In any case, `seed` is not really used in any computation, while a
  810. * cofactor different from the one in the built-in table is just
  811. * mathematically wrong anyway and should not be used.
  812. */
  813. if ((ctx = BN_CTX_new()) == NULL) {
  814. ECerr(EC_F_EC_ASN1_PARAMETERS2GROUP, ERR_R_BN_LIB);
  815. goto err;
  816. }
  817. if ((dup = EC_GROUP_dup(ret)) == NULL
  818. || EC_GROUP_set_seed(dup, NULL, 0) != 1
  819. || !EC_GROUP_set_generator(dup, point, a, NULL)) {
  820. ECerr(EC_F_EC_ASN1_PARAMETERS2GROUP, ERR_R_EC_LIB);
  821. goto err;
  822. }
  823. if ((curve_name = ec_curve_nid_from_params(dup, ctx)) != NID_undef) {
  824. /*
  825. * The input explicit parameters successfully matched one of the
  826. * built-in curves: often for built-in curves we have specialized
  827. * methods with better performance and hardening.
  828. *
  829. * In this case we replace the `EC_GROUP` created through explicit
  830. * parameters with one created from a named group.
  831. */
  832. EC_GROUP *named_group = NULL;
  833. #ifndef OPENSSL_NO_EC_NISTP_64_GCC_128
  834. /*
  835. * NID_wap_wsg_idm_ecid_wtls12 and NID_secp224r1 are both aliases for
  836. * the same curve, we prefer the SECP nid when matching explicit
  837. * parameters as that is associated with a specialized EC_METHOD.
  838. */
  839. if (curve_name == NID_wap_wsg_idm_ecid_wtls12)
  840. curve_name = NID_secp224r1;
  841. #endif /* !def(OPENSSL_NO_EC_NISTP_64_GCC_128) */
  842. if ((named_group = EC_GROUP_new_by_curve_name(curve_name)) == NULL) {
  843. ECerr(EC_F_EC_ASN1_PARAMETERS2GROUP, ERR_R_EC_LIB);
  844. goto err;
  845. }
  846. EC_GROUP_free(ret);
  847. ret = named_group;
  848. /*
  849. * Set the flag so that EC_GROUPs created from explicit parameters are
  850. * serialized using explicit parameters by default.
  851. *
  852. * 0x0 = OPENSSL_EC_EXPLICIT_CURVE
  853. */
  854. EC_GROUP_set_asn1_flag(ret, 0x0);
  855. /*
  856. * If the input params do not contain the optional seed field we make
  857. * sure it is not added to the returned group.
  858. *
  859. * The seed field is not really used inside libcrypto anyway, and
  860. * adding it to parsed explicit parameter keys would alter their DER
  861. * encoding output (because of the extra field) which could impact
  862. * applications fingerprinting keys by their DER encoding.
  863. */
  864. if (params->curve->seed == NULL) {
  865. if (EC_GROUP_set_seed(ret, NULL, 0) != 1)
  866. goto err;
  867. }
  868. }
  869. ok = 1;
  870. err:
  871. if (!ok) {
  872. if (ret)
  873. EC_GROUP_free(ret);
  874. ret = NULL;
  875. }
  876. if (dup)
  877. EC_GROUP_free(dup);
  878. if (p)
  879. BN_free(p);
  880. if (a)
  881. BN_free(a);
  882. if (b)
  883. BN_free(b);
  884. if (point)
  885. EC_POINT_free(point);
  886. if (ctx)
  887. BN_CTX_free(ctx);
  888. return (ret);
  889. }
  890. EC_GROUP *ec_asn1_pkparameters2group(const ECPKPARAMETERS *params)
  891. {
  892. EC_GROUP *ret = NULL;
  893. int tmp = 0;
  894. if (params == NULL) {
  895. ECerr(EC_F_EC_ASN1_PKPARAMETERS2GROUP, EC_R_MISSING_PARAMETERS);
  896. return NULL;
  897. }
  898. if (params->type == 0) { /* the curve is given by an OID */
  899. tmp = OBJ_obj2nid(params->value.named_curve);
  900. if ((ret = EC_GROUP_new_by_curve_name(tmp)) == NULL) {
  901. ECerr(EC_F_EC_ASN1_PKPARAMETERS2GROUP,
  902. EC_R_EC_GROUP_NEW_BY_NAME_FAILURE);
  903. return NULL;
  904. }
  905. EC_GROUP_set_asn1_flag(ret, OPENSSL_EC_NAMED_CURVE);
  906. } else if (params->type == 1) { /* the parameters are given by a
  907. * ECPARAMETERS structure */
  908. ret = ec_asn1_parameters2group(params->value.parameters);
  909. if (!ret) {
  910. ECerr(EC_F_EC_ASN1_PKPARAMETERS2GROUP, ERR_R_EC_LIB);
  911. return NULL;
  912. }
  913. EC_GROUP_set_asn1_flag(ret, 0x0);
  914. } else if (params->type == 2) { /* implicitlyCA */
  915. return NULL;
  916. } else {
  917. ECerr(EC_F_EC_ASN1_PKPARAMETERS2GROUP, EC_R_ASN1_ERROR);
  918. return NULL;
  919. }
  920. return ret;
  921. }
  922. /* EC_GROUP <-> DER encoding of ECPKPARAMETERS */
  923. EC_GROUP *d2i_ECPKParameters(EC_GROUP **a, const unsigned char **in, long len)
  924. {
  925. EC_GROUP *group = NULL;
  926. ECPKPARAMETERS *params = NULL;
  927. const unsigned char *p = *in;
  928. if ((params = d2i_ECPKPARAMETERS(NULL, &p, len)) == NULL) {
  929. ECerr(EC_F_D2I_ECPKPARAMETERS, EC_R_D2I_ECPKPARAMETERS_FAILURE);
  930. ECPKPARAMETERS_free(params);
  931. return NULL;
  932. }
  933. if ((group = ec_asn1_pkparameters2group(params)) == NULL) {
  934. ECerr(EC_F_D2I_ECPKPARAMETERS, EC_R_PKPARAMETERS2GROUP_FAILURE);
  935. ECPKPARAMETERS_free(params);
  936. return NULL;
  937. }
  938. if (a && *a)
  939. EC_GROUP_free(*a);
  940. if (a)
  941. *a = group;
  942. ECPKPARAMETERS_free(params);
  943. *in = p;
  944. return (group);
  945. }
  946. int i2d_ECPKParameters(const EC_GROUP *a, unsigned char **out)
  947. {
  948. int ret = 0;
  949. ECPKPARAMETERS *tmp = ec_asn1_group2pkparameters(a, NULL);
  950. if (tmp == NULL) {
  951. ECerr(EC_F_I2D_ECPKPARAMETERS, EC_R_GROUP2PKPARAMETERS_FAILURE);
  952. return 0;
  953. }
  954. if ((ret = i2d_ECPKPARAMETERS(tmp, out)) == 0) {
  955. ECerr(EC_F_I2D_ECPKPARAMETERS, EC_R_I2D_ECPKPARAMETERS_FAILURE);
  956. ECPKPARAMETERS_free(tmp);
  957. return 0;
  958. }
  959. ECPKPARAMETERS_free(tmp);
  960. return (ret);
  961. }
  962. /* some EC_KEY functions */
  963. EC_KEY *d2i_ECPrivateKey(EC_KEY **a, const unsigned char **in, long len)
  964. {
  965. int ok = 0;
  966. EC_KEY *ret = NULL;
  967. EC_PRIVATEKEY *priv_key = NULL;
  968. const unsigned char *p = *in;
  969. if ((priv_key = d2i_EC_PRIVATEKEY(NULL, &p, len)) == NULL) {
  970. ECerr(EC_F_D2I_ECPRIVATEKEY, ERR_R_EC_LIB);
  971. return NULL;
  972. }
  973. if (a == NULL || *a == NULL) {
  974. if ((ret = EC_KEY_new()) == NULL) {
  975. ECerr(EC_F_D2I_ECPRIVATEKEY, ERR_R_MALLOC_FAILURE);
  976. goto err;
  977. }
  978. } else
  979. ret = *a;
  980. if (priv_key->parameters) {
  981. if (ret->group)
  982. EC_GROUP_free(ret->group);
  983. ret->group = ec_asn1_pkparameters2group(priv_key->parameters);
  984. }
  985. if (ret->group == NULL) {
  986. ECerr(EC_F_D2I_ECPRIVATEKEY, ERR_R_EC_LIB);
  987. goto err;
  988. }
  989. ret->version = priv_key->version;
  990. if (priv_key->privateKey) {
  991. ret->priv_key = BN_bin2bn(M_ASN1_STRING_data(priv_key->privateKey),
  992. M_ASN1_STRING_length(priv_key->privateKey),
  993. ret->priv_key);
  994. if (ret->priv_key == NULL) {
  995. ECerr(EC_F_D2I_ECPRIVATEKEY, ERR_R_BN_LIB);
  996. goto err;
  997. }
  998. } else {
  999. ECerr(EC_F_D2I_ECPRIVATEKEY, EC_R_MISSING_PRIVATE_KEY);
  1000. goto err;
  1001. }
  1002. if (ret->pub_key)
  1003. EC_POINT_clear_free(ret->pub_key);
  1004. ret->pub_key = EC_POINT_new(ret->group);
  1005. if (ret->pub_key == NULL) {
  1006. ECerr(EC_F_D2I_ECPRIVATEKEY, ERR_R_EC_LIB);
  1007. goto err;
  1008. }
  1009. if (priv_key->publicKey) {
  1010. const unsigned char *pub_oct;
  1011. int pub_oct_len;
  1012. pub_oct = M_ASN1_STRING_data(priv_key->publicKey);
  1013. pub_oct_len = M_ASN1_STRING_length(priv_key->publicKey);
  1014. /*
  1015. * The first byte - point conversion form - must be present.
  1016. */
  1017. if (pub_oct_len <= 0) {
  1018. ECerr(EC_F_D2I_ECPRIVATEKEY, EC_R_BUFFER_TOO_SMALL);
  1019. goto err;
  1020. }
  1021. /* Save the point conversion form. */
  1022. ret->conv_form = (point_conversion_form_t) (pub_oct[0] & ~0x01);
  1023. if (!EC_POINT_oct2point(ret->group, ret->pub_key,
  1024. pub_oct, (size_t)(pub_oct_len), NULL)) {
  1025. ECerr(EC_F_D2I_ECPRIVATEKEY, ERR_R_EC_LIB);
  1026. goto err;
  1027. }
  1028. } else {
  1029. if (!EC_POINT_mul
  1030. (ret->group, ret->pub_key, ret->priv_key, NULL, NULL, NULL)) {
  1031. ECerr(EC_F_D2I_ECPRIVATEKEY, ERR_R_EC_LIB);
  1032. goto err;
  1033. }
  1034. /* Remember the original private-key-only encoding. */
  1035. ret->enc_flag |= EC_PKEY_NO_PUBKEY;
  1036. }
  1037. if (a)
  1038. *a = ret;
  1039. *in = p;
  1040. ok = 1;
  1041. err:
  1042. if (!ok) {
  1043. if (ret && (a == NULL || *a != ret))
  1044. EC_KEY_free(ret);
  1045. ret = NULL;
  1046. }
  1047. if (priv_key)
  1048. EC_PRIVATEKEY_free(priv_key);
  1049. return (ret);
  1050. }
  1051. int i2d_ECPrivateKey(EC_KEY *a, unsigned char **out)
  1052. {
  1053. int ret = 0, ok = 0;
  1054. unsigned char *buffer = NULL;
  1055. size_t buf_len = 0, tmp_len, bn_len;
  1056. EC_PRIVATEKEY *priv_key = NULL;
  1057. if (a == NULL || a->group == NULL || a->priv_key == NULL ||
  1058. (!(a->enc_flag & EC_PKEY_NO_PUBKEY) && a->pub_key == NULL)) {
  1059. ECerr(EC_F_I2D_ECPRIVATEKEY, ERR_R_PASSED_NULL_PARAMETER);
  1060. goto err;
  1061. }
  1062. if ((priv_key = EC_PRIVATEKEY_new()) == NULL) {
  1063. ECerr(EC_F_I2D_ECPRIVATEKEY, ERR_R_MALLOC_FAILURE);
  1064. goto err;
  1065. }
  1066. priv_key->version = a->version;
  1067. bn_len = (size_t)BN_num_bytes(a->priv_key);
  1068. /* Octetstring may need leading zeros if BN is to short */
  1069. buf_len = (EC_GROUP_get_degree(a->group) + 7) / 8;
  1070. if (bn_len > buf_len) {
  1071. ECerr(EC_F_I2D_ECPRIVATEKEY, EC_R_BUFFER_TOO_SMALL);
  1072. goto err;
  1073. }
  1074. buffer = OPENSSL_malloc(buf_len);
  1075. if (buffer == NULL) {
  1076. ECerr(EC_F_I2D_ECPRIVATEKEY, ERR_R_MALLOC_FAILURE);
  1077. goto err;
  1078. }
  1079. if (!BN_bn2bin(a->priv_key, buffer + buf_len - bn_len)) {
  1080. ECerr(EC_F_I2D_ECPRIVATEKEY, ERR_R_BN_LIB);
  1081. goto err;
  1082. }
  1083. if (buf_len - bn_len > 0) {
  1084. memset(buffer, 0, buf_len - bn_len);
  1085. }
  1086. if (!M_ASN1_OCTET_STRING_set(priv_key->privateKey, buffer, buf_len)) {
  1087. ECerr(EC_F_I2D_ECPRIVATEKEY, ERR_R_ASN1_LIB);
  1088. goto err;
  1089. }
  1090. if (!(a->enc_flag & EC_PKEY_NO_PARAMETERS)) {
  1091. if ((priv_key->parameters =
  1092. ec_asn1_group2pkparameters(a->group,
  1093. priv_key->parameters)) == NULL) {
  1094. ECerr(EC_F_I2D_ECPRIVATEKEY, ERR_R_EC_LIB);
  1095. goto err;
  1096. }
  1097. }
  1098. if (!(a->enc_flag & EC_PKEY_NO_PUBKEY)) {
  1099. priv_key->publicKey = M_ASN1_BIT_STRING_new();
  1100. if (priv_key->publicKey == NULL) {
  1101. ECerr(EC_F_I2D_ECPRIVATEKEY, ERR_R_MALLOC_FAILURE);
  1102. goto err;
  1103. }
  1104. tmp_len = EC_POINT_point2oct(a->group, a->pub_key,
  1105. a->conv_form, NULL, 0, NULL);
  1106. if (tmp_len > buf_len) {
  1107. unsigned char *tmp_buffer = OPENSSL_realloc(buffer, tmp_len);
  1108. if (!tmp_buffer) {
  1109. ECerr(EC_F_I2D_ECPRIVATEKEY, ERR_R_MALLOC_FAILURE);
  1110. goto err;
  1111. }
  1112. buffer = tmp_buffer;
  1113. buf_len = tmp_len;
  1114. }
  1115. if (!EC_POINT_point2oct(a->group, a->pub_key,
  1116. a->conv_form, buffer, buf_len, NULL)) {
  1117. ECerr(EC_F_I2D_ECPRIVATEKEY, ERR_R_EC_LIB);
  1118. goto err;
  1119. }
  1120. priv_key->publicKey->flags &= ~(ASN1_STRING_FLAG_BITS_LEFT | 0x07);
  1121. priv_key->publicKey->flags |= ASN1_STRING_FLAG_BITS_LEFT;
  1122. if (!M_ASN1_BIT_STRING_set(priv_key->publicKey, buffer, buf_len)) {
  1123. ECerr(EC_F_I2D_ECPRIVATEKEY, ERR_R_ASN1_LIB);
  1124. goto err;
  1125. }
  1126. }
  1127. if ((ret = i2d_EC_PRIVATEKEY(priv_key, out)) == 0) {
  1128. ECerr(EC_F_I2D_ECPRIVATEKEY, ERR_R_EC_LIB);
  1129. goto err;
  1130. }
  1131. ok = 1;
  1132. err:
  1133. if (buffer)
  1134. OPENSSL_free(buffer);
  1135. if (priv_key)
  1136. EC_PRIVATEKEY_free(priv_key);
  1137. return (ok ? ret : 0);
  1138. }
  1139. int i2d_ECParameters(EC_KEY *a, unsigned char **out)
  1140. {
  1141. if (a == NULL) {
  1142. ECerr(EC_F_I2D_ECPARAMETERS, ERR_R_PASSED_NULL_PARAMETER);
  1143. return 0;
  1144. }
  1145. return i2d_ECPKParameters(a->group, out);
  1146. }
  1147. EC_KEY *d2i_ECParameters(EC_KEY **a, const unsigned char **in, long len)
  1148. {
  1149. EC_KEY *ret;
  1150. if (in == NULL || *in == NULL) {
  1151. ECerr(EC_F_D2I_ECPARAMETERS, ERR_R_PASSED_NULL_PARAMETER);
  1152. return NULL;
  1153. }
  1154. if (a == NULL || *a == NULL) {
  1155. if ((ret = EC_KEY_new()) == NULL) {
  1156. ECerr(EC_F_D2I_ECPARAMETERS, ERR_R_MALLOC_FAILURE);
  1157. return NULL;
  1158. }
  1159. } else
  1160. ret = *a;
  1161. if (!d2i_ECPKParameters(&ret->group, in, len)) {
  1162. ECerr(EC_F_D2I_ECPARAMETERS, ERR_R_EC_LIB);
  1163. if (a == NULL || *a != ret)
  1164. EC_KEY_free(ret);
  1165. return NULL;
  1166. }
  1167. if (a)
  1168. *a = ret;
  1169. return ret;
  1170. }
  1171. EC_KEY *o2i_ECPublicKey(EC_KEY **a, const unsigned char **in, long len)
  1172. {
  1173. EC_KEY *ret = NULL;
  1174. if (a == NULL || (*a) == NULL || (*a)->group == NULL) {
  1175. /*
  1176. * sorry, but a EC_GROUP-structur is necessary to set the public key
  1177. */
  1178. ECerr(EC_F_O2I_ECPUBLICKEY, ERR_R_PASSED_NULL_PARAMETER);
  1179. return 0;
  1180. }
  1181. ret = *a;
  1182. if (ret->pub_key == NULL &&
  1183. (ret->pub_key = EC_POINT_new(ret->group)) == NULL) {
  1184. ECerr(EC_F_O2I_ECPUBLICKEY, ERR_R_MALLOC_FAILURE);
  1185. return 0;
  1186. }
  1187. if (!EC_POINT_oct2point(ret->group, ret->pub_key, *in, len, NULL)) {
  1188. ECerr(EC_F_O2I_ECPUBLICKEY, ERR_R_EC_LIB);
  1189. return 0;
  1190. }
  1191. /* save the point conversion form */
  1192. ret->conv_form = (point_conversion_form_t) (*in[0] & ~0x01);
  1193. *in += len;
  1194. return ret;
  1195. }
  1196. int i2o_ECPublicKey(EC_KEY *a, unsigned char **out)
  1197. {
  1198. size_t buf_len = 0;
  1199. int new_buffer = 0;
  1200. if (a == NULL) {
  1201. ECerr(EC_F_I2O_ECPUBLICKEY, ERR_R_PASSED_NULL_PARAMETER);
  1202. return 0;
  1203. }
  1204. buf_len = EC_POINT_point2oct(a->group, a->pub_key,
  1205. a->conv_form, NULL, 0, NULL);
  1206. if (out == NULL || buf_len == 0)
  1207. /* out == NULL => just return the length of the octet string */
  1208. return buf_len;
  1209. if (*out == NULL) {
  1210. if ((*out = OPENSSL_malloc(buf_len)) == NULL) {
  1211. ECerr(EC_F_I2O_ECPUBLICKEY, ERR_R_MALLOC_FAILURE);
  1212. return 0;
  1213. }
  1214. new_buffer = 1;
  1215. }
  1216. if (!EC_POINT_point2oct(a->group, a->pub_key, a->conv_form,
  1217. *out, buf_len, NULL)) {
  1218. ECerr(EC_F_I2O_ECPUBLICKEY, ERR_R_EC_LIB);
  1219. if (new_buffer) {
  1220. OPENSSL_free(*out);
  1221. *out = NULL;
  1222. }
  1223. return 0;
  1224. }
  1225. if (!new_buffer)
  1226. *out += buf_len;
  1227. return buf_len;
  1228. }