ec_asn1.c 38 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284
  1. /*
  2. * Copyright 2002-2019 The OpenSSL Project Authors. All Rights Reserved.
  3. *
  4. * Licensed under the OpenSSL license (the "License"). You may not use
  5. * this file except in compliance with the License. You can obtain a copy
  6. * in the file LICENSE in the source distribution or at
  7. * https://www.openssl.org/source/license.html
  8. */
  9. #include <string.h>
  10. #include "ec_lcl.h"
  11. #include <openssl/err.h>
  12. #include <openssl/asn1t.h>
  13. #include <openssl/objects.h>
  14. #include "internal/nelem.h"
  15. int EC_GROUP_get_basis_type(const EC_GROUP *group)
  16. {
  17. int i;
  18. if (EC_METHOD_get_field_type(EC_GROUP_method_of(group)) !=
  19. NID_X9_62_characteristic_two_field)
  20. /* everything else is currently not supported */
  21. return 0;
  22. /* Find the last non-zero element of group->poly[] */
  23. for (i = 0;
  24. i < (int)OSSL_NELEM(group->poly) && group->poly[i] != 0;
  25. i++)
  26. continue;
  27. if (i == 4)
  28. return NID_X9_62_ppBasis;
  29. else if (i == 2)
  30. return NID_X9_62_tpBasis;
  31. else
  32. /* everything else is currently not supported */
  33. return 0;
  34. }
  35. #ifndef OPENSSL_NO_EC2M
  36. int EC_GROUP_get_trinomial_basis(const EC_GROUP *group, unsigned int *k)
  37. {
  38. if (group == NULL)
  39. return 0;
  40. if (EC_METHOD_get_field_type(EC_GROUP_method_of(group)) !=
  41. NID_X9_62_characteristic_two_field
  42. || !((group->poly[0] != 0) && (group->poly[1] != 0)
  43. && (group->poly[2] == 0))) {
  44. ECerr(EC_F_EC_GROUP_GET_TRINOMIAL_BASIS,
  45. ERR_R_SHOULD_NOT_HAVE_BEEN_CALLED);
  46. return 0;
  47. }
  48. if (k)
  49. *k = group->poly[1];
  50. return 1;
  51. }
  52. int EC_GROUP_get_pentanomial_basis(const EC_GROUP *group, unsigned int *k1,
  53. unsigned int *k2, unsigned int *k3)
  54. {
  55. if (group == NULL)
  56. return 0;
  57. if (EC_METHOD_get_field_type(EC_GROUP_method_of(group)) !=
  58. NID_X9_62_characteristic_two_field
  59. || !((group->poly[0] != 0) && (group->poly[1] != 0)
  60. && (group->poly[2] != 0) && (group->poly[3] != 0)
  61. && (group->poly[4] == 0))) {
  62. ECerr(EC_F_EC_GROUP_GET_PENTANOMIAL_BASIS,
  63. ERR_R_SHOULD_NOT_HAVE_BEEN_CALLED);
  64. return 0;
  65. }
  66. if (k1)
  67. *k1 = group->poly[3];
  68. if (k2)
  69. *k2 = group->poly[2];
  70. if (k3)
  71. *k3 = group->poly[1];
  72. return 1;
  73. }
  74. #endif
  75. /* some structures needed for the asn1 encoding */
  76. typedef struct x9_62_pentanomial_st {
  77. int32_t k1;
  78. int32_t k2;
  79. int32_t k3;
  80. } X9_62_PENTANOMIAL;
  81. typedef struct x9_62_characteristic_two_st {
  82. int32_t m;
  83. ASN1_OBJECT *type;
  84. union {
  85. char *ptr;
  86. /* NID_X9_62_onBasis */
  87. ASN1_NULL *onBasis;
  88. /* NID_X9_62_tpBasis */
  89. ASN1_INTEGER *tpBasis;
  90. /* NID_X9_62_ppBasis */
  91. X9_62_PENTANOMIAL *ppBasis;
  92. /* anything else */
  93. ASN1_TYPE *other;
  94. } p;
  95. } X9_62_CHARACTERISTIC_TWO;
  96. typedef struct x9_62_fieldid_st {
  97. ASN1_OBJECT *fieldType;
  98. union {
  99. char *ptr;
  100. /* NID_X9_62_prime_field */
  101. ASN1_INTEGER *prime;
  102. /* NID_X9_62_characteristic_two_field */
  103. X9_62_CHARACTERISTIC_TWO *char_two;
  104. /* anything else */
  105. ASN1_TYPE *other;
  106. } p;
  107. } X9_62_FIELDID;
  108. typedef struct x9_62_curve_st {
  109. ASN1_OCTET_STRING *a;
  110. ASN1_OCTET_STRING *b;
  111. ASN1_BIT_STRING *seed;
  112. } X9_62_CURVE;
  113. struct ec_parameters_st {
  114. int32_t version;
  115. X9_62_FIELDID *fieldID;
  116. X9_62_CURVE *curve;
  117. ASN1_OCTET_STRING *base;
  118. ASN1_INTEGER *order;
  119. ASN1_INTEGER *cofactor;
  120. } /* ECPARAMETERS */ ;
  121. struct ecpk_parameters_st {
  122. int type;
  123. union {
  124. ASN1_OBJECT *named_curve;
  125. ECPARAMETERS *parameters;
  126. ASN1_NULL *implicitlyCA;
  127. } value;
  128. } /* ECPKPARAMETERS */ ;
  129. /* SEC1 ECPrivateKey */
  130. typedef struct ec_privatekey_st {
  131. int32_t version;
  132. ASN1_OCTET_STRING *privateKey;
  133. ECPKPARAMETERS *parameters;
  134. ASN1_BIT_STRING *publicKey;
  135. } EC_PRIVATEKEY;
  136. /* the OpenSSL ASN.1 definitions */
  137. ASN1_SEQUENCE(X9_62_PENTANOMIAL) = {
  138. ASN1_EMBED(X9_62_PENTANOMIAL, k1, INT32),
  139. ASN1_EMBED(X9_62_PENTANOMIAL, k2, INT32),
  140. ASN1_EMBED(X9_62_PENTANOMIAL, k3, INT32)
  141. } static_ASN1_SEQUENCE_END(X9_62_PENTANOMIAL)
  142. DECLARE_ASN1_ALLOC_FUNCTIONS(X9_62_PENTANOMIAL)
  143. IMPLEMENT_ASN1_ALLOC_FUNCTIONS(X9_62_PENTANOMIAL)
  144. ASN1_ADB_TEMPLATE(char_two_def) = ASN1_SIMPLE(X9_62_CHARACTERISTIC_TWO, p.other, ASN1_ANY);
  145. ASN1_ADB(X9_62_CHARACTERISTIC_TWO) = {
  146. ADB_ENTRY(NID_X9_62_onBasis, ASN1_SIMPLE(X9_62_CHARACTERISTIC_TWO, p.onBasis, ASN1_NULL)),
  147. ADB_ENTRY(NID_X9_62_tpBasis, ASN1_SIMPLE(X9_62_CHARACTERISTIC_TWO, p.tpBasis, ASN1_INTEGER)),
  148. ADB_ENTRY(NID_X9_62_ppBasis, ASN1_SIMPLE(X9_62_CHARACTERISTIC_TWO, p.ppBasis, X9_62_PENTANOMIAL))
  149. } ASN1_ADB_END(X9_62_CHARACTERISTIC_TWO, 0, type, 0, &char_two_def_tt, NULL);
  150. ASN1_SEQUENCE(X9_62_CHARACTERISTIC_TWO) = {
  151. ASN1_EMBED(X9_62_CHARACTERISTIC_TWO, m, INT32),
  152. ASN1_SIMPLE(X9_62_CHARACTERISTIC_TWO, type, ASN1_OBJECT),
  153. ASN1_ADB_OBJECT(X9_62_CHARACTERISTIC_TWO)
  154. } static_ASN1_SEQUENCE_END(X9_62_CHARACTERISTIC_TWO)
  155. DECLARE_ASN1_ALLOC_FUNCTIONS(X9_62_CHARACTERISTIC_TWO)
  156. IMPLEMENT_ASN1_ALLOC_FUNCTIONS(X9_62_CHARACTERISTIC_TWO)
  157. ASN1_ADB_TEMPLATE(fieldID_def) = ASN1_SIMPLE(X9_62_FIELDID, p.other, ASN1_ANY);
  158. ASN1_ADB(X9_62_FIELDID) = {
  159. ADB_ENTRY(NID_X9_62_prime_field, ASN1_SIMPLE(X9_62_FIELDID, p.prime, ASN1_INTEGER)),
  160. ADB_ENTRY(NID_X9_62_characteristic_two_field, ASN1_SIMPLE(X9_62_FIELDID, p.char_two, X9_62_CHARACTERISTIC_TWO))
  161. } ASN1_ADB_END(X9_62_FIELDID, 0, fieldType, 0, &fieldID_def_tt, NULL);
  162. ASN1_SEQUENCE(X9_62_FIELDID) = {
  163. ASN1_SIMPLE(X9_62_FIELDID, fieldType, ASN1_OBJECT),
  164. ASN1_ADB_OBJECT(X9_62_FIELDID)
  165. } static_ASN1_SEQUENCE_END(X9_62_FIELDID)
  166. ASN1_SEQUENCE(X9_62_CURVE) = {
  167. ASN1_SIMPLE(X9_62_CURVE, a, ASN1_OCTET_STRING),
  168. ASN1_SIMPLE(X9_62_CURVE, b, ASN1_OCTET_STRING),
  169. ASN1_OPT(X9_62_CURVE, seed, ASN1_BIT_STRING)
  170. } static_ASN1_SEQUENCE_END(X9_62_CURVE)
  171. ASN1_SEQUENCE(ECPARAMETERS) = {
  172. ASN1_EMBED(ECPARAMETERS, version, INT32),
  173. ASN1_SIMPLE(ECPARAMETERS, fieldID, X9_62_FIELDID),
  174. ASN1_SIMPLE(ECPARAMETERS, curve, X9_62_CURVE),
  175. ASN1_SIMPLE(ECPARAMETERS, base, ASN1_OCTET_STRING),
  176. ASN1_SIMPLE(ECPARAMETERS, order, ASN1_INTEGER),
  177. ASN1_OPT(ECPARAMETERS, cofactor, ASN1_INTEGER)
  178. } ASN1_SEQUENCE_END(ECPARAMETERS)
  179. DECLARE_ASN1_ALLOC_FUNCTIONS(ECPARAMETERS)
  180. IMPLEMENT_ASN1_ALLOC_FUNCTIONS(ECPARAMETERS)
  181. ASN1_CHOICE(ECPKPARAMETERS) = {
  182. ASN1_SIMPLE(ECPKPARAMETERS, value.named_curve, ASN1_OBJECT),
  183. ASN1_SIMPLE(ECPKPARAMETERS, value.parameters, ECPARAMETERS),
  184. ASN1_SIMPLE(ECPKPARAMETERS, value.implicitlyCA, ASN1_NULL)
  185. } ASN1_CHOICE_END(ECPKPARAMETERS)
  186. DECLARE_ASN1_FUNCTIONS_const(ECPKPARAMETERS)
  187. DECLARE_ASN1_ENCODE_FUNCTIONS_const(ECPKPARAMETERS, ECPKPARAMETERS)
  188. IMPLEMENT_ASN1_FUNCTIONS_const(ECPKPARAMETERS)
  189. ASN1_SEQUENCE(EC_PRIVATEKEY) = {
  190. ASN1_EMBED(EC_PRIVATEKEY, version, INT32),
  191. ASN1_SIMPLE(EC_PRIVATEKEY, privateKey, ASN1_OCTET_STRING),
  192. ASN1_EXP_OPT(EC_PRIVATEKEY, parameters, ECPKPARAMETERS, 0),
  193. ASN1_EXP_OPT(EC_PRIVATEKEY, publicKey, ASN1_BIT_STRING, 1)
  194. } static_ASN1_SEQUENCE_END(EC_PRIVATEKEY)
  195. DECLARE_ASN1_FUNCTIONS_const(EC_PRIVATEKEY)
  196. DECLARE_ASN1_ENCODE_FUNCTIONS_const(EC_PRIVATEKEY, EC_PRIVATEKEY)
  197. IMPLEMENT_ASN1_FUNCTIONS_const(EC_PRIVATEKEY)
  198. /* some declarations of internal function */
  199. /* ec_asn1_group2field() sets the values in a X9_62_FIELDID object */
  200. static int ec_asn1_group2fieldid(const EC_GROUP *, X9_62_FIELDID *);
  201. /* ec_asn1_group2curve() sets the values in a X9_62_CURVE object */
  202. static int ec_asn1_group2curve(const EC_GROUP *, X9_62_CURVE *);
  203. /* the function definitions */
  204. static int ec_asn1_group2fieldid(const EC_GROUP *group, X9_62_FIELDID *field)
  205. {
  206. int ok = 0, nid;
  207. BIGNUM *tmp = NULL;
  208. if (group == NULL || field == NULL)
  209. return 0;
  210. /* clear the old values (if necessary) */
  211. ASN1_OBJECT_free(field->fieldType);
  212. ASN1_TYPE_free(field->p.other);
  213. nid = EC_METHOD_get_field_type(EC_GROUP_method_of(group));
  214. /* set OID for the field */
  215. if ((field->fieldType = OBJ_nid2obj(nid)) == NULL) {
  216. ECerr(EC_F_EC_ASN1_GROUP2FIELDID, ERR_R_OBJ_LIB);
  217. goto err;
  218. }
  219. if (nid == NID_X9_62_prime_field) {
  220. if ((tmp = BN_new()) == NULL) {
  221. ECerr(EC_F_EC_ASN1_GROUP2FIELDID, ERR_R_MALLOC_FAILURE);
  222. goto err;
  223. }
  224. /* the parameters are specified by the prime number p */
  225. if (!EC_GROUP_get_curve(group, tmp, NULL, NULL, NULL)) {
  226. ECerr(EC_F_EC_ASN1_GROUP2FIELDID, ERR_R_EC_LIB);
  227. goto err;
  228. }
  229. /* set the prime number */
  230. field->p.prime = BN_to_ASN1_INTEGER(tmp, NULL);
  231. if (field->p.prime == NULL) {
  232. ECerr(EC_F_EC_ASN1_GROUP2FIELDID, ERR_R_ASN1_LIB);
  233. goto err;
  234. }
  235. } else if (nid == NID_X9_62_characteristic_two_field)
  236. #ifdef OPENSSL_NO_EC2M
  237. {
  238. ECerr(EC_F_EC_ASN1_GROUP2FIELDID, EC_R_GF2M_NOT_SUPPORTED);
  239. goto err;
  240. }
  241. #else
  242. {
  243. int field_type;
  244. X9_62_CHARACTERISTIC_TWO *char_two;
  245. field->p.char_two = X9_62_CHARACTERISTIC_TWO_new();
  246. char_two = field->p.char_two;
  247. if (char_two == NULL) {
  248. ECerr(EC_F_EC_ASN1_GROUP2FIELDID, ERR_R_MALLOC_FAILURE);
  249. goto err;
  250. }
  251. char_two->m = (long)EC_GROUP_get_degree(group);
  252. field_type = EC_GROUP_get_basis_type(group);
  253. if (field_type == 0) {
  254. ECerr(EC_F_EC_ASN1_GROUP2FIELDID, ERR_R_EC_LIB);
  255. goto err;
  256. }
  257. /* set base type OID */
  258. if ((char_two->type = OBJ_nid2obj(field_type)) == NULL) {
  259. ECerr(EC_F_EC_ASN1_GROUP2FIELDID, ERR_R_OBJ_LIB);
  260. goto err;
  261. }
  262. if (field_type == NID_X9_62_tpBasis) {
  263. unsigned int k;
  264. if (!EC_GROUP_get_trinomial_basis(group, &k))
  265. goto err;
  266. char_two->p.tpBasis = ASN1_INTEGER_new();
  267. if (char_two->p.tpBasis == NULL) {
  268. ECerr(EC_F_EC_ASN1_GROUP2FIELDID, ERR_R_MALLOC_FAILURE);
  269. goto err;
  270. }
  271. if (!ASN1_INTEGER_set(char_two->p.tpBasis, (long)k)) {
  272. ECerr(EC_F_EC_ASN1_GROUP2FIELDID, ERR_R_ASN1_LIB);
  273. goto err;
  274. }
  275. } else if (field_type == NID_X9_62_ppBasis) {
  276. unsigned int k1, k2, k3;
  277. if (!EC_GROUP_get_pentanomial_basis(group, &k1, &k2, &k3))
  278. goto err;
  279. char_two->p.ppBasis = X9_62_PENTANOMIAL_new();
  280. if (char_two->p.ppBasis == NULL) {
  281. ECerr(EC_F_EC_ASN1_GROUP2FIELDID, ERR_R_MALLOC_FAILURE);
  282. goto err;
  283. }
  284. /* set k? values */
  285. char_two->p.ppBasis->k1 = (long)k1;
  286. char_two->p.ppBasis->k2 = (long)k2;
  287. char_two->p.ppBasis->k3 = (long)k3;
  288. } else { /* field_type == NID_X9_62_onBasis */
  289. /* for ONB the parameters are (asn1) NULL */
  290. char_two->p.onBasis = ASN1_NULL_new();
  291. if (char_two->p.onBasis == NULL) {
  292. ECerr(EC_F_EC_ASN1_GROUP2FIELDID, ERR_R_MALLOC_FAILURE);
  293. goto err;
  294. }
  295. }
  296. }
  297. #endif
  298. else {
  299. ECerr(EC_F_EC_ASN1_GROUP2FIELDID, EC_R_UNSUPPORTED_FIELD);
  300. goto err;
  301. }
  302. ok = 1;
  303. err:
  304. BN_free(tmp);
  305. return ok;
  306. }
  307. static int ec_asn1_group2curve(const EC_GROUP *group, X9_62_CURVE *curve)
  308. {
  309. int ok = 0;
  310. BIGNUM *tmp_1 = NULL, *tmp_2 = NULL;
  311. unsigned char *a_buf = NULL, *b_buf = NULL;
  312. size_t len;
  313. if (!group || !curve || !curve->a || !curve->b)
  314. return 0;
  315. if ((tmp_1 = BN_new()) == NULL || (tmp_2 = BN_new()) == NULL) {
  316. ECerr(EC_F_EC_ASN1_GROUP2CURVE, ERR_R_MALLOC_FAILURE);
  317. goto err;
  318. }
  319. /* get a and b */
  320. if (!EC_GROUP_get_curve(group, NULL, tmp_1, tmp_2, NULL)) {
  321. ECerr(EC_F_EC_ASN1_GROUP2CURVE, ERR_R_EC_LIB);
  322. goto err;
  323. }
  324. /*
  325. * Per SEC 1, the curve coefficients must be padded up to size. See C.2's
  326. * definition of Curve, C.1's definition of FieldElement, and 2.3.5's
  327. * definition of how to encode the field elements.
  328. */
  329. len = ((size_t)EC_GROUP_get_degree(group) + 7) / 8;
  330. if ((a_buf = OPENSSL_malloc(len)) == NULL
  331. || (b_buf = OPENSSL_malloc(len)) == NULL) {
  332. ECerr(EC_F_EC_ASN1_GROUP2CURVE, ERR_R_MALLOC_FAILURE);
  333. goto err;
  334. }
  335. if (BN_bn2binpad(tmp_1, a_buf, len) < 0
  336. || BN_bn2binpad(tmp_2, b_buf, len) < 0) {
  337. ECerr(EC_F_EC_ASN1_GROUP2CURVE, ERR_R_BN_LIB);
  338. goto err;
  339. }
  340. /* set a and b */
  341. if (!ASN1_OCTET_STRING_set(curve->a, a_buf, len)
  342. || !ASN1_OCTET_STRING_set(curve->b, b_buf, len)) {
  343. ECerr(EC_F_EC_ASN1_GROUP2CURVE, ERR_R_ASN1_LIB);
  344. goto err;
  345. }
  346. /* set the seed (optional) */
  347. if (group->seed) {
  348. if (!curve->seed)
  349. if ((curve->seed = ASN1_BIT_STRING_new()) == NULL) {
  350. ECerr(EC_F_EC_ASN1_GROUP2CURVE, ERR_R_MALLOC_FAILURE);
  351. goto err;
  352. }
  353. curve->seed->flags &= ~(ASN1_STRING_FLAG_BITS_LEFT | 0x07);
  354. curve->seed->flags |= ASN1_STRING_FLAG_BITS_LEFT;
  355. if (!ASN1_BIT_STRING_set(curve->seed, group->seed,
  356. (int)group->seed_len)) {
  357. ECerr(EC_F_EC_ASN1_GROUP2CURVE, ERR_R_ASN1_LIB);
  358. goto err;
  359. }
  360. } else {
  361. ASN1_BIT_STRING_free(curve->seed);
  362. curve->seed = NULL;
  363. }
  364. ok = 1;
  365. err:
  366. OPENSSL_free(a_buf);
  367. OPENSSL_free(b_buf);
  368. BN_free(tmp_1);
  369. BN_free(tmp_2);
  370. return ok;
  371. }
  372. ECPARAMETERS *EC_GROUP_get_ecparameters(const EC_GROUP *group,
  373. ECPARAMETERS *params)
  374. {
  375. size_t len = 0;
  376. ECPARAMETERS *ret = NULL;
  377. const BIGNUM *tmp;
  378. unsigned char *buffer = NULL;
  379. const EC_POINT *point = NULL;
  380. point_conversion_form_t form;
  381. if (params == NULL) {
  382. if ((ret = ECPARAMETERS_new()) == NULL) {
  383. ECerr(EC_F_EC_GROUP_GET_ECPARAMETERS, ERR_R_MALLOC_FAILURE);
  384. goto err;
  385. }
  386. } else
  387. ret = params;
  388. /* set the version (always one) */
  389. ret->version = (long)0x1;
  390. /* set the fieldID */
  391. if (!ec_asn1_group2fieldid(group, ret->fieldID)) {
  392. ECerr(EC_F_EC_GROUP_GET_ECPARAMETERS, ERR_R_EC_LIB);
  393. goto err;
  394. }
  395. /* set the curve */
  396. if (!ec_asn1_group2curve(group, ret->curve)) {
  397. ECerr(EC_F_EC_GROUP_GET_ECPARAMETERS, ERR_R_EC_LIB);
  398. goto err;
  399. }
  400. /* set the base point */
  401. if ((point = EC_GROUP_get0_generator(group)) == NULL) {
  402. ECerr(EC_F_EC_GROUP_GET_ECPARAMETERS, EC_R_UNDEFINED_GENERATOR);
  403. goto err;
  404. }
  405. form = EC_GROUP_get_point_conversion_form(group);
  406. len = EC_POINT_point2buf(group, point, form, &buffer, NULL);
  407. if (len == 0) {
  408. ECerr(EC_F_EC_GROUP_GET_ECPARAMETERS, ERR_R_EC_LIB);
  409. goto err;
  410. }
  411. if (ret->base == NULL && (ret->base = ASN1_OCTET_STRING_new()) == NULL) {
  412. OPENSSL_free(buffer);
  413. ECerr(EC_F_EC_GROUP_GET_ECPARAMETERS, ERR_R_MALLOC_FAILURE);
  414. goto err;
  415. }
  416. ASN1_STRING_set0(ret->base, buffer, len);
  417. /* set the order */
  418. tmp = EC_GROUP_get0_order(group);
  419. if (tmp == NULL) {
  420. ECerr(EC_F_EC_GROUP_GET_ECPARAMETERS, ERR_R_EC_LIB);
  421. goto err;
  422. }
  423. ret->order = BN_to_ASN1_INTEGER(tmp, ret->order);
  424. if (ret->order == NULL) {
  425. ECerr(EC_F_EC_GROUP_GET_ECPARAMETERS, ERR_R_ASN1_LIB);
  426. goto err;
  427. }
  428. /* set the cofactor (optional) */
  429. tmp = EC_GROUP_get0_cofactor(group);
  430. if (tmp != NULL) {
  431. ret->cofactor = BN_to_ASN1_INTEGER(tmp, ret->cofactor);
  432. if (ret->cofactor == NULL) {
  433. ECerr(EC_F_EC_GROUP_GET_ECPARAMETERS, ERR_R_ASN1_LIB);
  434. goto err;
  435. }
  436. }
  437. return ret;
  438. err:
  439. if (params == NULL)
  440. ECPARAMETERS_free(ret);
  441. return NULL;
  442. }
  443. ECPKPARAMETERS *EC_GROUP_get_ecpkparameters(const EC_GROUP *group,
  444. ECPKPARAMETERS *params)
  445. {
  446. int ok = 1, tmp;
  447. ECPKPARAMETERS *ret = params;
  448. if (ret == NULL) {
  449. if ((ret = ECPKPARAMETERS_new()) == NULL) {
  450. ECerr(EC_F_EC_GROUP_GET_ECPKPARAMETERS, ERR_R_MALLOC_FAILURE);
  451. return NULL;
  452. }
  453. } else {
  454. if (ret->type == 0)
  455. ASN1_OBJECT_free(ret->value.named_curve);
  456. else if (ret->type == 1 && ret->value.parameters)
  457. ECPARAMETERS_free(ret->value.parameters);
  458. }
  459. if (EC_GROUP_get_asn1_flag(group)) {
  460. /*
  461. * use the asn1 OID to describe the elliptic curve parameters
  462. */
  463. tmp = EC_GROUP_get_curve_name(group);
  464. if (tmp) {
  465. ret->type = 0;
  466. if ((ret->value.named_curve = OBJ_nid2obj(tmp)) == NULL)
  467. ok = 0;
  468. } else
  469. /* we don't know the nid => ERROR */
  470. ok = 0;
  471. } else {
  472. /* use the ECPARAMETERS structure */
  473. ret->type = 1;
  474. if ((ret->value.parameters =
  475. EC_GROUP_get_ecparameters(group, NULL)) == NULL)
  476. ok = 0;
  477. }
  478. if (!ok) {
  479. ECPKPARAMETERS_free(ret);
  480. return NULL;
  481. }
  482. return ret;
  483. }
  484. EC_GROUP *EC_GROUP_new_from_ecparameters(const ECPARAMETERS *params)
  485. {
  486. int ok = 0, tmp;
  487. EC_GROUP *ret = NULL, *dup = NULL;
  488. BIGNUM *p = NULL, *a = NULL, *b = NULL;
  489. EC_POINT *point = NULL;
  490. long field_bits;
  491. int curve_name = NID_undef;
  492. BN_CTX *ctx = NULL;
  493. if (!params->fieldID || !params->fieldID->fieldType ||
  494. !params->fieldID->p.ptr) {
  495. ECerr(EC_F_EC_GROUP_NEW_FROM_ECPARAMETERS, EC_R_ASN1_ERROR);
  496. goto err;
  497. }
  498. /*
  499. * Now extract the curve parameters a and b. Note that, although SEC 1
  500. * specifies the length of their encodings, historical versions of OpenSSL
  501. * encoded them incorrectly, so we must accept any length for backwards
  502. * compatibility.
  503. */
  504. if (!params->curve || !params->curve->a ||
  505. !params->curve->a->data || !params->curve->b ||
  506. !params->curve->b->data) {
  507. ECerr(EC_F_EC_GROUP_NEW_FROM_ECPARAMETERS, EC_R_ASN1_ERROR);
  508. goto err;
  509. }
  510. a = BN_bin2bn(params->curve->a->data, params->curve->a->length, NULL);
  511. if (a == NULL) {
  512. ECerr(EC_F_EC_GROUP_NEW_FROM_ECPARAMETERS, ERR_R_BN_LIB);
  513. goto err;
  514. }
  515. b = BN_bin2bn(params->curve->b->data, params->curve->b->length, NULL);
  516. if (b == NULL) {
  517. ECerr(EC_F_EC_GROUP_NEW_FROM_ECPARAMETERS, ERR_R_BN_LIB);
  518. goto err;
  519. }
  520. /* get the field parameters */
  521. tmp = OBJ_obj2nid(params->fieldID->fieldType);
  522. if (tmp == NID_X9_62_characteristic_two_field)
  523. #ifdef OPENSSL_NO_EC2M
  524. {
  525. ECerr(EC_F_EC_GROUP_NEW_FROM_ECPARAMETERS, EC_R_GF2M_NOT_SUPPORTED);
  526. goto err;
  527. }
  528. #else
  529. {
  530. X9_62_CHARACTERISTIC_TWO *char_two;
  531. char_two = params->fieldID->p.char_two;
  532. field_bits = char_two->m;
  533. if (field_bits > OPENSSL_ECC_MAX_FIELD_BITS) {
  534. ECerr(EC_F_EC_GROUP_NEW_FROM_ECPARAMETERS, EC_R_FIELD_TOO_LARGE);
  535. goto err;
  536. }
  537. if ((p = BN_new()) == NULL) {
  538. ECerr(EC_F_EC_GROUP_NEW_FROM_ECPARAMETERS, ERR_R_MALLOC_FAILURE);
  539. goto err;
  540. }
  541. /* get the base type */
  542. tmp = OBJ_obj2nid(char_two->type);
  543. if (tmp == NID_X9_62_tpBasis) {
  544. long tmp_long;
  545. if (!char_two->p.tpBasis) {
  546. ECerr(EC_F_EC_GROUP_NEW_FROM_ECPARAMETERS, EC_R_ASN1_ERROR);
  547. goto err;
  548. }
  549. tmp_long = ASN1_INTEGER_get(char_two->p.tpBasis);
  550. if (!(char_two->m > tmp_long && tmp_long > 0)) {
  551. ECerr(EC_F_EC_GROUP_NEW_FROM_ECPARAMETERS,
  552. EC_R_INVALID_TRINOMIAL_BASIS);
  553. goto err;
  554. }
  555. /* create the polynomial */
  556. if (!BN_set_bit(p, (int)char_two->m))
  557. goto err;
  558. if (!BN_set_bit(p, (int)tmp_long))
  559. goto err;
  560. if (!BN_set_bit(p, 0))
  561. goto err;
  562. } else if (tmp == NID_X9_62_ppBasis) {
  563. X9_62_PENTANOMIAL *penta;
  564. penta = char_two->p.ppBasis;
  565. if (!penta) {
  566. ECerr(EC_F_EC_GROUP_NEW_FROM_ECPARAMETERS, EC_R_ASN1_ERROR);
  567. goto err;
  568. }
  569. if (!
  570. (char_two->m > penta->k3 && penta->k3 > penta->k2
  571. && penta->k2 > penta->k1 && penta->k1 > 0)) {
  572. ECerr(EC_F_EC_GROUP_NEW_FROM_ECPARAMETERS,
  573. EC_R_INVALID_PENTANOMIAL_BASIS);
  574. goto err;
  575. }
  576. /* create the polynomial */
  577. if (!BN_set_bit(p, (int)char_two->m))
  578. goto err;
  579. if (!BN_set_bit(p, (int)penta->k1))
  580. goto err;
  581. if (!BN_set_bit(p, (int)penta->k2))
  582. goto err;
  583. if (!BN_set_bit(p, (int)penta->k3))
  584. goto err;
  585. if (!BN_set_bit(p, 0))
  586. goto err;
  587. } else if (tmp == NID_X9_62_onBasis) {
  588. ECerr(EC_F_EC_GROUP_NEW_FROM_ECPARAMETERS, EC_R_NOT_IMPLEMENTED);
  589. goto err;
  590. } else { /* error */
  591. ECerr(EC_F_EC_GROUP_NEW_FROM_ECPARAMETERS, EC_R_ASN1_ERROR);
  592. goto err;
  593. }
  594. /* create the EC_GROUP structure */
  595. ret = EC_GROUP_new_curve_GF2m(p, a, b, NULL);
  596. }
  597. #endif
  598. else if (tmp == NID_X9_62_prime_field) {
  599. /* we have a curve over a prime field */
  600. /* extract the prime number */
  601. if (!params->fieldID->p.prime) {
  602. ECerr(EC_F_EC_GROUP_NEW_FROM_ECPARAMETERS, EC_R_ASN1_ERROR);
  603. goto err;
  604. }
  605. p = ASN1_INTEGER_to_BN(params->fieldID->p.prime, NULL);
  606. if (p == NULL) {
  607. ECerr(EC_F_EC_GROUP_NEW_FROM_ECPARAMETERS, ERR_R_ASN1_LIB);
  608. goto err;
  609. }
  610. if (BN_is_negative(p) || BN_is_zero(p)) {
  611. ECerr(EC_F_EC_GROUP_NEW_FROM_ECPARAMETERS, EC_R_INVALID_FIELD);
  612. goto err;
  613. }
  614. field_bits = BN_num_bits(p);
  615. if (field_bits > OPENSSL_ECC_MAX_FIELD_BITS) {
  616. ECerr(EC_F_EC_GROUP_NEW_FROM_ECPARAMETERS, EC_R_FIELD_TOO_LARGE);
  617. goto err;
  618. }
  619. /* create the EC_GROUP structure */
  620. ret = EC_GROUP_new_curve_GFp(p, a, b, NULL);
  621. } else {
  622. ECerr(EC_F_EC_GROUP_NEW_FROM_ECPARAMETERS, EC_R_INVALID_FIELD);
  623. goto err;
  624. }
  625. if (ret == NULL) {
  626. ECerr(EC_F_EC_GROUP_NEW_FROM_ECPARAMETERS, ERR_R_EC_LIB);
  627. goto err;
  628. }
  629. /* extract seed (optional) */
  630. if (params->curve->seed != NULL) {
  631. OPENSSL_free(ret->seed);
  632. if ((ret->seed = OPENSSL_malloc(params->curve->seed->length)) == NULL) {
  633. ECerr(EC_F_EC_GROUP_NEW_FROM_ECPARAMETERS, ERR_R_MALLOC_FAILURE);
  634. goto err;
  635. }
  636. memcpy(ret->seed, params->curve->seed->data,
  637. params->curve->seed->length);
  638. ret->seed_len = params->curve->seed->length;
  639. }
  640. if (!params->order || !params->base || !params->base->data) {
  641. ECerr(EC_F_EC_GROUP_NEW_FROM_ECPARAMETERS, EC_R_ASN1_ERROR);
  642. goto err;
  643. }
  644. if ((point = EC_POINT_new(ret)) == NULL)
  645. goto err;
  646. /* set the point conversion form */
  647. EC_GROUP_set_point_conversion_form(ret, (point_conversion_form_t)
  648. (params->base->data[0] & ~0x01));
  649. /* extract the ec point */
  650. if (!EC_POINT_oct2point(ret, point, params->base->data,
  651. params->base->length, NULL)) {
  652. ECerr(EC_F_EC_GROUP_NEW_FROM_ECPARAMETERS, ERR_R_EC_LIB);
  653. goto err;
  654. }
  655. /* extract the order */
  656. if ((a = ASN1_INTEGER_to_BN(params->order, a)) == NULL) {
  657. ECerr(EC_F_EC_GROUP_NEW_FROM_ECPARAMETERS, ERR_R_ASN1_LIB);
  658. goto err;
  659. }
  660. if (BN_is_negative(a) || BN_is_zero(a)) {
  661. ECerr(EC_F_EC_GROUP_NEW_FROM_ECPARAMETERS, EC_R_INVALID_GROUP_ORDER);
  662. goto err;
  663. }
  664. if (BN_num_bits(a) > (int)field_bits + 1) { /* Hasse bound */
  665. ECerr(EC_F_EC_GROUP_NEW_FROM_ECPARAMETERS, EC_R_INVALID_GROUP_ORDER);
  666. goto err;
  667. }
  668. /* extract the cofactor (optional) */
  669. if (params->cofactor == NULL) {
  670. BN_free(b);
  671. b = NULL;
  672. } else if ((b = ASN1_INTEGER_to_BN(params->cofactor, b)) == NULL) {
  673. ECerr(EC_F_EC_GROUP_NEW_FROM_ECPARAMETERS, ERR_R_ASN1_LIB);
  674. goto err;
  675. }
  676. /* set the generator, order and cofactor (if present) */
  677. if (!EC_GROUP_set_generator(ret, point, a, b)) {
  678. ECerr(EC_F_EC_GROUP_NEW_FROM_ECPARAMETERS, ERR_R_EC_LIB);
  679. goto err;
  680. }
  681. /*
  682. * Check if the explicit parameters group just created matches one of the
  683. * built-in curves.
  684. *
  685. * We create a copy of the group just built, so that we can remove optional
  686. * fields for the lookup: we do this to avoid the possibility that one of
  687. * the optional parameters is used to force the library into using a less
  688. * performant and less secure EC_METHOD instead of the specialized one.
  689. * In any case, `seed` is not really used in any computation, while a
  690. * cofactor different from the one in the built-in table is just
  691. * mathematically wrong anyway and should not be used.
  692. */
  693. if ((ctx = BN_CTX_new()) == NULL) {
  694. ECerr(EC_F_EC_GROUP_NEW_FROM_ECPARAMETERS, ERR_R_BN_LIB);
  695. goto err;
  696. }
  697. if ((dup = EC_GROUP_dup(ret)) == NULL
  698. || EC_GROUP_set_seed(dup, NULL, 0) != 1
  699. || !EC_GROUP_set_generator(dup, point, a, NULL)) {
  700. ECerr(EC_F_EC_GROUP_NEW_FROM_ECPARAMETERS, ERR_R_EC_LIB);
  701. goto err;
  702. }
  703. if ((curve_name = ec_curve_nid_from_params(dup, ctx)) != NID_undef) {
  704. /*
  705. * The input explicit parameters successfully matched one of the
  706. * built-in curves: often for built-in curves we have specialized
  707. * methods with better performance and hardening.
  708. *
  709. * In this case we replace the `EC_GROUP` created through explicit
  710. * parameters with one created from a named group.
  711. */
  712. EC_GROUP *named_group = NULL;
  713. #ifndef OPENSSL_NO_EC_NISTP_64_GCC_128
  714. /*
  715. * NID_wap_wsg_idm_ecid_wtls12 and NID_secp224r1 are both aliases for
  716. * the same curve, we prefer the SECP nid when matching explicit
  717. * parameters as that is associated with a specialized EC_METHOD.
  718. */
  719. if (curve_name == NID_wap_wsg_idm_ecid_wtls12)
  720. curve_name = NID_secp224r1;
  721. #endif /* !def(OPENSSL_NO_EC_NISTP_64_GCC_128) */
  722. if ((named_group = EC_GROUP_new_by_curve_name(curve_name)) == NULL) {
  723. ECerr(EC_F_EC_GROUP_NEW_FROM_ECPARAMETERS, ERR_R_EC_LIB);
  724. goto err;
  725. }
  726. EC_GROUP_free(ret);
  727. ret = named_group;
  728. /*
  729. * Set the flag so that EC_GROUPs created from explicit parameters are
  730. * serialized using explicit parameters by default.
  731. */
  732. EC_GROUP_set_asn1_flag(ret, OPENSSL_EC_EXPLICIT_CURVE);
  733. }
  734. ok = 1;
  735. err:
  736. if (!ok) {
  737. EC_GROUP_free(ret);
  738. ret = NULL;
  739. }
  740. EC_GROUP_free(dup);
  741. BN_free(p);
  742. BN_free(a);
  743. BN_free(b);
  744. EC_POINT_free(point);
  745. BN_CTX_free(ctx);
  746. return ret;
  747. }
  748. EC_GROUP *EC_GROUP_new_from_ecpkparameters(const ECPKPARAMETERS *params)
  749. {
  750. EC_GROUP *ret = NULL;
  751. int tmp = 0;
  752. if (params == NULL) {
  753. ECerr(EC_F_EC_GROUP_NEW_FROM_ECPKPARAMETERS, EC_R_MISSING_PARAMETERS);
  754. return NULL;
  755. }
  756. if (params->type == 0) { /* the curve is given by an OID */
  757. tmp = OBJ_obj2nid(params->value.named_curve);
  758. if ((ret = EC_GROUP_new_by_curve_name(tmp)) == NULL) {
  759. ECerr(EC_F_EC_GROUP_NEW_FROM_ECPKPARAMETERS,
  760. EC_R_EC_GROUP_NEW_BY_NAME_FAILURE);
  761. return NULL;
  762. }
  763. EC_GROUP_set_asn1_flag(ret, OPENSSL_EC_NAMED_CURVE);
  764. } else if (params->type == 1) { /* the parameters are given by a
  765. * ECPARAMETERS structure */
  766. ret = EC_GROUP_new_from_ecparameters(params->value.parameters);
  767. if (!ret) {
  768. ECerr(EC_F_EC_GROUP_NEW_FROM_ECPKPARAMETERS, ERR_R_EC_LIB);
  769. return NULL;
  770. }
  771. EC_GROUP_set_asn1_flag(ret, OPENSSL_EC_EXPLICIT_CURVE);
  772. } else if (params->type == 2) { /* implicitlyCA */
  773. return NULL;
  774. } else {
  775. ECerr(EC_F_EC_GROUP_NEW_FROM_ECPKPARAMETERS, EC_R_ASN1_ERROR);
  776. return NULL;
  777. }
  778. return ret;
  779. }
  780. /* EC_GROUP <-> DER encoding of ECPKPARAMETERS */
  781. EC_GROUP *d2i_ECPKParameters(EC_GROUP **a, const unsigned char **in, long len)
  782. {
  783. EC_GROUP *group = NULL;
  784. ECPKPARAMETERS *params = NULL;
  785. const unsigned char *p = *in;
  786. if ((params = d2i_ECPKPARAMETERS(NULL, &p, len)) == NULL) {
  787. ECerr(EC_F_D2I_ECPKPARAMETERS, EC_R_D2I_ECPKPARAMETERS_FAILURE);
  788. ECPKPARAMETERS_free(params);
  789. return NULL;
  790. }
  791. if ((group = EC_GROUP_new_from_ecpkparameters(params)) == NULL) {
  792. ECerr(EC_F_D2I_ECPKPARAMETERS, EC_R_PKPARAMETERS2GROUP_FAILURE);
  793. ECPKPARAMETERS_free(params);
  794. return NULL;
  795. }
  796. if (a) {
  797. EC_GROUP_free(*a);
  798. *a = group;
  799. }
  800. ECPKPARAMETERS_free(params);
  801. *in = p;
  802. return group;
  803. }
  804. int i2d_ECPKParameters(const EC_GROUP *a, unsigned char **out)
  805. {
  806. int ret = 0;
  807. ECPKPARAMETERS *tmp = EC_GROUP_get_ecpkparameters(a, NULL);
  808. if (tmp == NULL) {
  809. ECerr(EC_F_I2D_ECPKPARAMETERS, EC_R_GROUP2PKPARAMETERS_FAILURE);
  810. return 0;
  811. }
  812. if ((ret = i2d_ECPKPARAMETERS(tmp, out)) == 0) {
  813. ECerr(EC_F_I2D_ECPKPARAMETERS, EC_R_I2D_ECPKPARAMETERS_FAILURE);
  814. ECPKPARAMETERS_free(tmp);
  815. return 0;
  816. }
  817. ECPKPARAMETERS_free(tmp);
  818. return ret;
  819. }
  820. /* some EC_KEY functions */
  821. EC_KEY *d2i_ECPrivateKey(EC_KEY **a, const unsigned char **in, long len)
  822. {
  823. EC_KEY *ret = NULL;
  824. EC_PRIVATEKEY *priv_key = NULL;
  825. const unsigned char *p = *in;
  826. if ((priv_key = d2i_EC_PRIVATEKEY(NULL, &p, len)) == NULL) {
  827. ECerr(EC_F_D2I_ECPRIVATEKEY, ERR_R_EC_LIB);
  828. return NULL;
  829. }
  830. if (a == NULL || *a == NULL) {
  831. if ((ret = EC_KEY_new()) == NULL) {
  832. ECerr(EC_F_D2I_ECPRIVATEKEY, ERR_R_MALLOC_FAILURE);
  833. goto err;
  834. }
  835. } else
  836. ret = *a;
  837. if (priv_key->parameters) {
  838. EC_GROUP_free(ret->group);
  839. ret->group = EC_GROUP_new_from_ecpkparameters(priv_key->parameters);
  840. }
  841. if (ret->group == NULL) {
  842. ECerr(EC_F_D2I_ECPRIVATEKEY, ERR_R_EC_LIB);
  843. goto err;
  844. }
  845. ret->version = priv_key->version;
  846. if (priv_key->privateKey) {
  847. ASN1_OCTET_STRING *pkey = priv_key->privateKey;
  848. if (EC_KEY_oct2priv(ret, ASN1_STRING_get0_data(pkey),
  849. ASN1_STRING_length(pkey)) == 0)
  850. goto err;
  851. } else {
  852. ECerr(EC_F_D2I_ECPRIVATEKEY, EC_R_MISSING_PRIVATE_KEY);
  853. goto err;
  854. }
  855. EC_POINT_clear_free(ret->pub_key);
  856. ret->pub_key = EC_POINT_new(ret->group);
  857. if (ret->pub_key == NULL) {
  858. ECerr(EC_F_D2I_ECPRIVATEKEY, ERR_R_EC_LIB);
  859. goto err;
  860. }
  861. if (priv_key->publicKey) {
  862. const unsigned char *pub_oct;
  863. int pub_oct_len;
  864. pub_oct = ASN1_STRING_get0_data(priv_key->publicKey);
  865. pub_oct_len = ASN1_STRING_length(priv_key->publicKey);
  866. if (!EC_KEY_oct2key(ret, pub_oct, pub_oct_len, NULL)) {
  867. ECerr(EC_F_D2I_ECPRIVATEKEY, ERR_R_EC_LIB);
  868. goto err;
  869. }
  870. } else {
  871. if (ret->group->meth->keygenpub == NULL
  872. || ret->group->meth->keygenpub(ret) == 0)
  873. goto err;
  874. /* Remember the original private-key-only encoding. */
  875. ret->enc_flag |= EC_PKEY_NO_PUBKEY;
  876. }
  877. if (a)
  878. *a = ret;
  879. EC_PRIVATEKEY_free(priv_key);
  880. *in = p;
  881. return ret;
  882. err:
  883. if (a == NULL || *a != ret)
  884. EC_KEY_free(ret);
  885. EC_PRIVATEKEY_free(priv_key);
  886. return NULL;
  887. }
  888. int i2d_ECPrivateKey(EC_KEY *a, unsigned char **out)
  889. {
  890. int ret = 0, ok = 0;
  891. unsigned char *priv= NULL, *pub= NULL;
  892. size_t privlen = 0, publen = 0;
  893. EC_PRIVATEKEY *priv_key = NULL;
  894. if (a == NULL || a->group == NULL ||
  895. (!(a->enc_flag & EC_PKEY_NO_PUBKEY) && a->pub_key == NULL)) {
  896. ECerr(EC_F_I2D_ECPRIVATEKEY, ERR_R_PASSED_NULL_PARAMETER);
  897. goto err;
  898. }
  899. if ((priv_key = EC_PRIVATEKEY_new()) == NULL) {
  900. ECerr(EC_F_I2D_ECPRIVATEKEY, ERR_R_MALLOC_FAILURE);
  901. goto err;
  902. }
  903. priv_key->version = a->version;
  904. privlen = EC_KEY_priv2buf(a, &priv);
  905. if (privlen == 0) {
  906. ECerr(EC_F_I2D_ECPRIVATEKEY, ERR_R_EC_LIB);
  907. goto err;
  908. }
  909. ASN1_STRING_set0(priv_key->privateKey, priv, privlen);
  910. priv = NULL;
  911. if (!(a->enc_flag & EC_PKEY_NO_PARAMETERS)) {
  912. if ((priv_key->parameters =
  913. EC_GROUP_get_ecpkparameters(a->group,
  914. priv_key->parameters)) == NULL) {
  915. ECerr(EC_F_I2D_ECPRIVATEKEY, ERR_R_EC_LIB);
  916. goto err;
  917. }
  918. }
  919. if (!(a->enc_flag & EC_PKEY_NO_PUBKEY)) {
  920. priv_key->publicKey = ASN1_BIT_STRING_new();
  921. if (priv_key->publicKey == NULL) {
  922. ECerr(EC_F_I2D_ECPRIVATEKEY, ERR_R_MALLOC_FAILURE);
  923. goto err;
  924. }
  925. publen = EC_KEY_key2buf(a, a->conv_form, &pub, NULL);
  926. if (publen == 0) {
  927. ECerr(EC_F_I2D_ECPRIVATEKEY, ERR_R_EC_LIB);
  928. goto err;
  929. }
  930. priv_key->publicKey->flags &= ~(ASN1_STRING_FLAG_BITS_LEFT | 0x07);
  931. priv_key->publicKey->flags |= ASN1_STRING_FLAG_BITS_LEFT;
  932. ASN1_STRING_set0(priv_key->publicKey, pub, publen);
  933. pub = NULL;
  934. }
  935. if ((ret = i2d_EC_PRIVATEKEY(priv_key, out)) == 0) {
  936. ECerr(EC_F_I2D_ECPRIVATEKEY, ERR_R_EC_LIB);
  937. goto err;
  938. }
  939. ok = 1;
  940. err:
  941. OPENSSL_clear_free(priv, privlen);
  942. OPENSSL_free(pub);
  943. EC_PRIVATEKEY_free(priv_key);
  944. return (ok ? ret : 0);
  945. }
  946. int i2d_ECParameters(EC_KEY *a, unsigned char **out)
  947. {
  948. if (a == NULL) {
  949. ECerr(EC_F_I2D_ECPARAMETERS, ERR_R_PASSED_NULL_PARAMETER);
  950. return 0;
  951. }
  952. return i2d_ECPKParameters(a->group, out);
  953. }
  954. EC_KEY *d2i_ECParameters(EC_KEY **a, const unsigned char **in, long len)
  955. {
  956. EC_KEY *ret;
  957. if (in == NULL || *in == NULL) {
  958. ECerr(EC_F_D2I_ECPARAMETERS, ERR_R_PASSED_NULL_PARAMETER);
  959. return NULL;
  960. }
  961. if (a == NULL || *a == NULL) {
  962. if ((ret = EC_KEY_new()) == NULL) {
  963. ECerr(EC_F_D2I_ECPARAMETERS, ERR_R_MALLOC_FAILURE);
  964. return NULL;
  965. }
  966. } else
  967. ret = *a;
  968. if (!d2i_ECPKParameters(&ret->group, in, len)) {
  969. ECerr(EC_F_D2I_ECPARAMETERS, ERR_R_EC_LIB);
  970. if (a == NULL || *a != ret)
  971. EC_KEY_free(ret);
  972. return NULL;
  973. }
  974. if (a)
  975. *a = ret;
  976. return ret;
  977. }
  978. EC_KEY *o2i_ECPublicKey(EC_KEY **a, const unsigned char **in, long len)
  979. {
  980. EC_KEY *ret = NULL;
  981. if (a == NULL || (*a) == NULL || (*a)->group == NULL) {
  982. /*
  983. * sorry, but a EC_GROUP-structure is necessary to set the public key
  984. */
  985. ECerr(EC_F_O2I_ECPUBLICKEY, ERR_R_PASSED_NULL_PARAMETER);
  986. return 0;
  987. }
  988. ret = *a;
  989. if (!EC_KEY_oct2key(ret, *in, len, NULL)) {
  990. ECerr(EC_F_O2I_ECPUBLICKEY, ERR_R_EC_LIB);
  991. return 0;
  992. }
  993. *in += len;
  994. return ret;
  995. }
  996. int i2o_ECPublicKey(const EC_KEY *a, unsigned char **out)
  997. {
  998. size_t buf_len = 0;
  999. int new_buffer = 0;
  1000. if (a == NULL) {
  1001. ECerr(EC_F_I2O_ECPUBLICKEY, ERR_R_PASSED_NULL_PARAMETER);
  1002. return 0;
  1003. }
  1004. buf_len = EC_POINT_point2oct(a->group, a->pub_key,
  1005. a->conv_form, NULL, 0, NULL);
  1006. if (out == NULL || buf_len == 0)
  1007. /* out == NULL => just return the length of the octet string */
  1008. return buf_len;
  1009. if (*out == NULL) {
  1010. if ((*out = OPENSSL_malloc(buf_len)) == NULL) {
  1011. ECerr(EC_F_I2O_ECPUBLICKEY, ERR_R_MALLOC_FAILURE);
  1012. return 0;
  1013. }
  1014. new_buffer = 1;
  1015. }
  1016. if (!EC_POINT_point2oct(a->group, a->pub_key, a->conv_form,
  1017. *out, buf_len, NULL)) {
  1018. ECerr(EC_F_I2O_ECPUBLICKEY, ERR_R_EC_LIB);
  1019. if (new_buffer) {
  1020. OPENSSL_free(*out);
  1021. *out = NULL;
  1022. }
  1023. return 0;
  1024. }
  1025. if (!new_buffer)
  1026. *out += buf_len;
  1027. return buf_len;
  1028. }
  1029. ASN1_SEQUENCE(ECDSA_SIG) = {
  1030. ASN1_SIMPLE(ECDSA_SIG, r, CBIGNUM),
  1031. ASN1_SIMPLE(ECDSA_SIG, s, CBIGNUM)
  1032. } static_ASN1_SEQUENCE_END(ECDSA_SIG)
  1033. DECLARE_ASN1_FUNCTIONS_const(ECDSA_SIG)
  1034. DECLARE_ASN1_ENCODE_FUNCTIONS_const(ECDSA_SIG, ECDSA_SIG)
  1035. IMPLEMENT_ASN1_ENCODE_FUNCTIONS_const_fname(ECDSA_SIG, ECDSA_SIG, ECDSA_SIG)
  1036. ECDSA_SIG *ECDSA_SIG_new(void)
  1037. {
  1038. ECDSA_SIG *sig = OPENSSL_zalloc(sizeof(*sig));
  1039. if (sig == NULL)
  1040. ECerr(EC_F_ECDSA_SIG_NEW, ERR_R_MALLOC_FAILURE);
  1041. return sig;
  1042. }
  1043. void ECDSA_SIG_free(ECDSA_SIG *sig)
  1044. {
  1045. if (sig == NULL)
  1046. return;
  1047. BN_clear_free(sig->r);
  1048. BN_clear_free(sig->s);
  1049. OPENSSL_free(sig);
  1050. }
  1051. void ECDSA_SIG_get0(const ECDSA_SIG *sig, const BIGNUM **pr, const BIGNUM **ps)
  1052. {
  1053. if (pr != NULL)
  1054. *pr = sig->r;
  1055. if (ps != NULL)
  1056. *ps = sig->s;
  1057. }
  1058. const BIGNUM *ECDSA_SIG_get0_r(const ECDSA_SIG *sig)
  1059. {
  1060. return sig->r;
  1061. }
  1062. const BIGNUM *ECDSA_SIG_get0_s(const ECDSA_SIG *sig)
  1063. {
  1064. return sig->s;
  1065. }
  1066. int ECDSA_SIG_set0(ECDSA_SIG *sig, BIGNUM *r, BIGNUM *s)
  1067. {
  1068. if (r == NULL || s == NULL)
  1069. return 0;
  1070. BN_clear_free(sig->r);
  1071. BN_clear_free(sig->s);
  1072. sig->r = r;
  1073. sig->s = s;
  1074. return 1;
  1075. }
  1076. int ECDSA_size(const EC_KEY *r)
  1077. {
  1078. int ret, i;
  1079. ASN1_INTEGER bs;
  1080. unsigned char buf[4];
  1081. const EC_GROUP *group;
  1082. if (r == NULL)
  1083. return 0;
  1084. group = EC_KEY_get0_group(r);
  1085. if (group == NULL)
  1086. return 0;
  1087. i = EC_GROUP_order_bits(group);
  1088. if (i == 0)
  1089. return 0;
  1090. bs.length = (i + 7) / 8;
  1091. bs.data = buf;
  1092. bs.type = V_ASN1_INTEGER;
  1093. /* If the top bit is set the asn1 encoding is 1 larger. */
  1094. buf[0] = 0xff;
  1095. i = i2d_ASN1_INTEGER(&bs, NULL);
  1096. i += i; /* r and s */
  1097. ret = ASN1_object_size(1, i, V_ASN1_SEQUENCE);
  1098. return ret;
  1099. }