2
0

ec_asn1.c 36 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233
  1. /*
  2. * Copyright 2002-2018 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_GFp(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, nid;
  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. nid = EC_METHOD_get_field_type(EC_GROUP_method_of(group));
  320. /* get a and b */
  321. if (nid == NID_X9_62_prime_field) {
  322. if (!EC_GROUP_get_curve_GFp(group, NULL, tmp_1, tmp_2, NULL)) {
  323. ECerr(EC_F_EC_ASN1_GROUP2CURVE, ERR_R_EC_LIB);
  324. goto err;
  325. }
  326. }
  327. #ifndef OPENSSL_NO_EC2M
  328. else { /* nid == NID_X9_62_characteristic_two_field */
  329. if (!EC_GROUP_get_curve_GF2m(group, NULL, tmp_1, tmp_2, NULL)) {
  330. ECerr(EC_F_EC_ASN1_GROUP2CURVE, ERR_R_EC_LIB);
  331. goto err;
  332. }
  333. }
  334. #endif
  335. /*
  336. * Per SEC 1, the curve coefficients must be padded up to size. See C.2's
  337. * definition of Curve, C.1's definition of FieldElement, and 2.3.5's
  338. * definition of how to encode the field elements.
  339. */
  340. len = ((size_t)EC_GROUP_get_degree(group) + 7) / 8;
  341. if ((a_buf = OPENSSL_malloc(len)) == NULL
  342. || (b_buf = OPENSSL_malloc(len)) == NULL) {
  343. ECerr(EC_F_EC_ASN1_GROUP2CURVE, ERR_R_MALLOC_FAILURE);
  344. goto err;
  345. }
  346. if (BN_bn2binpad(tmp_1, a_buf, len) < 0
  347. || BN_bn2binpad(tmp_2, b_buf, len) < 0) {
  348. ECerr(EC_F_EC_ASN1_GROUP2CURVE, ERR_R_BN_LIB);
  349. goto err;
  350. }
  351. /* set a and b */
  352. if (!ASN1_OCTET_STRING_set(curve->a, a_buf, len)
  353. || !ASN1_OCTET_STRING_set(curve->b, b_buf, len)) {
  354. ECerr(EC_F_EC_ASN1_GROUP2CURVE, ERR_R_ASN1_LIB);
  355. goto err;
  356. }
  357. /* set the seed (optional) */
  358. if (group->seed) {
  359. if (!curve->seed)
  360. if ((curve->seed = ASN1_BIT_STRING_new()) == NULL) {
  361. ECerr(EC_F_EC_ASN1_GROUP2CURVE, ERR_R_MALLOC_FAILURE);
  362. goto err;
  363. }
  364. curve->seed->flags &= ~(ASN1_STRING_FLAG_BITS_LEFT | 0x07);
  365. curve->seed->flags |= ASN1_STRING_FLAG_BITS_LEFT;
  366. if (!ASN1_BIT_STRING_set(curve->seed, group->seed,
  367. (int)group->seed_len)) {
  368. ECerr(EC_F_EC_ASN1_GROUP2CURVE, ERR_R_ASN1_LIB);
  369. goto err;
  370. }
  371. } else {
  372. ASN1_BIT_STRING_free(curve->seed);
  373. curve->seed = NULL;
  374. }
  375. ok = 1;
  376. err:
  377. OPENSSL_free(a_buf);
  378. OPENSSL_free(b_buf);
  379. BN_free(tmp_1);
  380. BN_free(tmp_2);
  381. return ok;
  382. }
  383. ECPARAMETERS *EC_GROUP_get_ecparameters(const EC_GROUP *group,
  384. ECPARAMETERS *params)
  385. {
  386. size_t len = 0;
  387. ECPARAMETERS *ret = NULL;
  388. const BIGNUM *tmp;
  389. unsigned char *buffer = NULL;
  390. const EC_POINT *point = NULL;
  391. point_conversion_form_t form;
  392. if (params == NULL) {
  393. if ((ret = ECPARAMETERS_new()) == NULL) {
  394. ECerr(EC_F_EC_GROUP_GET_ECPARAMETERS, ERR_R_MALLOC_FAILURE);
  395. goto err;
  396. }
  397. } else
  398. ret = params;
  399. /* set the version (always one) */
  400. ret->version = (long)0x1;
  401. /* set the fieldID */
  402. if (!ec_asn1_group2fieldid(group, ret->fieldID)) {
  403. ECerr(EC_F_EC_GROUP_GET_ECPARAMETERS, ERR_R_EC_LIB);
  404. goto err;
  405. }
  406. /* set the curve */
  407. if (!ec_asn1_group2curve(group, ret->curve)) {
  408. ECerr(EC_F_EC_GROUP_GET_ECPARAMETERS, ERR_R_EC_LIB);
  409. goto err;
  410. }
  411. /* set the base point */
  412. if ((point = EC_GROUP_get0_generator(group)) == NULL) {
  413. ECerr(EC_F_EC_GROUP_GET_ECPARAMETERS, EC_R_UNDEFINED_GENERATOR);
  414. goto err;
  415. }
  416. form = EC_GROUP_get_point_conversion_form(group);
  417. len = EC_POINT_point2buf(group, point, form, &buffer, NULL);
  418. if (len == 0) {
  419. ECerr(EC_F_EC_GROUP_GET_ECPARAMETERS, ERR_R_EC_LIB);
  420. goto err;
  421. }
  422. if (ret->base == NULL && (ret->base = ASN1_OCTET_STRING_new()) == NULL) {
  423. OPENSSL_free(buffer);
  424. ECerr(EC_F_EC_GROUP_GET_ECPARAMETERS, ERR_R_MALLOC_FAILURE);
  425. goto err;
  426. }
  427. ASN1_STRING_set0(ret->base, buffer, len);
  428. /* set the order */
  429. tmp = EC_GROUP_get0_order(group);
  430. if (tmp == NULL) {
  431. ECerr(EC_F_EC_GROUP_GET_ECPARAMETERS, ERR_R_EC_LIB);
  432. goto err;
  433. }
  434. ret->order = BN_to_ASN1_INTEGER(tmp, ret->order);
  435. if (ret->order == NULL) {
  436. ECerr(EC_F_EC_GROUP_GET_ECPARAMETERS, ERR_R_ASN1_LIB);
  437. goto err;
  438. }
  439. /* set the cofactor (optional) */
  440. tmp = EC_GROUP_get0_cofactor(group);
  441. if (tmp != NULL) {
  442. ret->cofactor = BN_to_ASN1_INTEGER(tmp, ret->cofactor);
  443. if (ret->cofactor == NULL) {
  444. ECerr(EC_F_EC_GROUP_GET_ECPARAMETERS, ERR_R_ASN1_LIB);
  445. goto err;
  446. }
  447. }
  448. return ret;
  449. err:
  450. if (params == NULL)
  451. ECPARAMETERS_free(ret);
  452. return NULL;
  453. }
  454. ECPKPARAMETERS *EC_GROUP_get_ecpkparameters(const EC_GROUP *group,
  455. ECPKPARAMETERS *params)
  456. {
  457. int ok = 1, tmp;
  458. ECPKPARAMETERS *ret = params;
  459. if (ret == NULL) {
  460. if ((ret = ECPKPARAMETERS_new()) == NULL) {
  461. ECerr(EC_F_EC_GROUP_GET_ECPKPARAMETERS, ERR_R_MALLOC_FAILURE);
  462. return NULL;
  463. }
  464. } else {
  465. if (ret->type == 0)
  466. ASN1_OBJECT_free(ret->value.named_curve);
  467. else if (ret->type == 1 && ret->value.parameters)
  468. ECPARAMETERS_free(ret->value.parameters);
  469. }
  470. if (EC_GROUP_get_asn1_flag(group)) {
  471. /*
  472. * use the asn1 OID to describe the elliptic curve parameters
  473. */
  474. tmp = EC_GROUP_get_curve_name(group);
  475. if (tmp) {
  476. ret->type = 0;
  477. if ((ret->value.named_curve = OBJ_nid2obj(tmp)) == NULL)
  478. ok = 0;
  479. } else
  480. /* we don't know the nid => ERROR */
  481. ok = 0;
  482. } else {
  483. /* use the ECPARAMETERS structure */
  484. ret->type = 1;
  485. if ((ret->value.parameters =
  486. EC_GROUP_get_ecparameters(group, NULL)) == NULL)
  487. ok = 0;
  488. }
  489. if (!ok) {
  490. ECPKPARAMETERS_free(ret);
  491. return NULL;
  492. }
  493. return ret;
  494. }
  495. EC_GROUP *EC_GROUP_new_from_ecparameters(const ECPARAMETERS *params)
  496. {
  497. int ok = 0, tmp;
  498. EC_GROUP *ret = NULL;
  499. BIGNUM *p = NULL, *a = NULL, *b = NULL;
  500. EC_POINT *point = NULL;
  501. long field_bits;
  502. if (!params->fieldID || !params->fieldID->fieldType ||
  503. !params->fieldID->p.ptr) {
  504. ECerr(EC_F_EC_GROUP_NEW_FROM_ECPARAMETERS, EC_R_ASN1_ERROR);
  505. goto err;
  506. }
  507. /*
  508. * Now extract the curve parameters a and b. Note that, although SEC 1
  509. * specifies the length of their encodings, historical versions of OpenSSL
  510. * encoded them incorrectly, so we must accept any length for backwards
  511. * compatibility.
  512. */
  513. if (!params->curve || !params->curve->a ||
  514. !params->curve->a->data || !params->curve->b ||
  515. !params->curve->b->data) {
  516. ECerr(EC_F_EC_GROUP_NEW_FROM_ECPARAMETERS, EC_R_ASN1_ERROR);
  517. goto err;
  518. }
  519. a = BN_bin2bn(params->curve->a->data, params->curve->a->length, NULL);
  520. if (a == NULL) {
  521. ECerr(EC_F_EC_GROUP_NEW_FROM_ECPARAMETERS, ERR_R_BN_LIB);
  522. goto err;
  523. }
  524. b = BN_bin2bn(params->curve->b->data, params->curve->b->length, NULL);
  525. if (b == NULL) {
  526. ECerr(EC_F_EC_GROUP_NEW_FROM_ECPARAMETERS, ERR_R_BN_LIB);
  527. goto err;
  528. }
  529. /* get the field parameters */
  530. tmp = OBJ_obj2nid(params->fieldID->fieldType);
  531. if (tmp == NID_X9_62_characteristic_two_field)
  532. #ifdef OPENSSL_NO_EC2M
  533. {
  534. ECerr(EC_F_EC_GROUP_NEW_FROM_ECPARAMETERS, EC_R_GF2M_NOT_SUPPORTED);
  535. goto err;
  536. }
  537. #else
  538. {
  539. X9_62_CHARACTERISTIC_TWO *char_two;
  540. char_two = params->fieldID->p.char_two;
  541. field_bits = char_two->m;
  542. if (field_bits > OPENSSL_ECC_MAX_FIELD_BITS) {
  543. ECerr(EC_F_EC_GROUP_NEW_FROM_ECPARAMETERS, EC_R_FIELD_TOO_LARGE);
  544. goto err;
  545. }
  546. if ((p = BN_new()) == NULL) {
  547. ECerr(EC_F_EC_GROUP_NEW_FROM_ECPARAMETERS, ERR_R_MALLOC_FAILURE);
  548. goto err;
  549. }
  550. /* get the base type */
  551. tmp = OBJ_obj2nid(char_two->type);
  552. if (tmp == NID_X9_62_tpBasis) {
  553. long tmp_long;
  554. if (!char_two->p.tpBasis) {
  555. ECerr(EC_F_EC_GROUP_NEW_FROM_ECPARAMETERS, EC_R_ASN1_ERROR);
  556. goto err;
  557. }
  558. tmp_long = ASN1_INTEGER_get(char_two->p.tpBasis);
  559. if (!(char_two->m > tmp_long && tmp_long > 0)) {
  560. ECerr(EC_F_EC_GROUP_NEW_FROM_ECPARAMETERS,
  561. EC_R_INVALID_TRINOMIAL_BASIS);
  562. goto err;
  563. }
  564. /* create the polynomial */
  565. if (!BN_set_bit(p, (int)char_two->m))
  566. goto err;
  567. if (!BN_set_bit(p, (int)tmp_long))
  568. goto err;
  569. if (!BN_set_bit(p, 0))
  570. goto err;
  571. } else if (tmp == NID_X9_62_ppBasis) {
  572. X9_62_PENTANOMIAL *penta;
  573. penta = char_two->p.ppBasis;
  574. if (!penta) {
  575. ECerr(EC_F_EC_GROUP_NEW_FROM_ECPARAMETERS, EC_R_ASN1_ERROR);
  576. goto err;
  577. }
  578. if (!
  579. (char_two->m > penta->k3 && penta->k3 > penta->k2
  580. && penta->k2 > penta->k1 && penta->k1 > 0)) {
  581. ECerr(EC_F_EC_GROUP_NEW_FROM_ECPARAMETERS,
  582. EC_R_INVALID_PENTANOMIAL_BASIS);
  583. goto err;
  584. }
  585. /* create the polynomial */
  586. if (!BN_set_bit(p, (int)char_two->m))
  587. goto err;
  588. if (!BN_set_bit(p, (int)penta->k1))
  589. goto err;
  590. if (!BN_set_bit(p, (int)penta->k2))
  591. goto err;
  592. if (!BN_set_bit(p, (int)penta->k3))
  593. goto err;
  594. if (!BN_set_bit(p, 0))
  595. goto err;
  596. } else if (tmp == NID_X9_62_onBasis) {
  597. ECerr(EC_F_EC_GROUP_NEW_FROM_ECPARAMETERS, EC_R_NOT_IMPLEMENTED);
  598. goto err;
  599. } else { /* error */
  600. ECerr(EC_F_EC_GROUP_NEW_FROM_ECPARAMETERS, EC_R_ASN1_ERROR);
  601. goto err;
  602. }
  603. /* create the EC_GROUP structure */
  604. ret = EC_GROUP_new_curve_GF2m(p, a, b, NULL);
  605. }
  606. #endif
  607. else if (tmp == NID_X9_62_prime_field) {
  608. /* we have a curve over a prime field */
  609. /* extract the prime number */
  610. if (!params->fieldID->p.prime) {
  611. ECerr(EC_F_EC_GROUP_NEW_FROM_ECPARAMETERS, EC_R_ASN1_ERROR);
  612. goto err;
  613. }
  614. p = ASN1_INTEGER_to_BN(params->fieldID->p.prime, NULL);
  615. if (p == NULL) {
  616. ECerr(EC_F_EC_GROUP_NEW_FROM_ECPARAMETERS, ERR_R_ASN1_LIB);
  617. goto err;
  618. }
  619. if (BN_is_negative(p) || BN_is_zero(p)) {
  620. ECerr(EC_F_EC_GROUP_NEW_FROM_ECPARAMETERS, EC_R_INVALID_FIELD);
  621. goto err;
  622. }
  623. field_bits = BN_num_bits(p);
  624. if (field_bits > OPENSSL_ECC_MAX_FIELD_BITS) {
  625. ECerr(EC_F_EC_GROUP_NEW_FROM_ECPARAMETERS, EC_R_FIELD_TOO_LARGE);
  626. goto err;
  627. }
  628. /* create the EC_GROUP structure */
  629. ret = EC_GROUP_new_curve_GFp(p, a, b, NULL);
  630. } else {
  631. ECerr(EC_F_EC_GROUP_NEW_FROM_ECPARAMETERS, EC_R_INVALID_FIELD);
  632. goto err;
  633. }
  634. if (ret == NULL) {
  635. ECerr(EC_F_EC_GROUP_NEW_FROM_ECPARAMETERS, ERR_R_EC_LIB);
  636. goto err;
  637. }
  638. /* extract seed (optional) */
  639. if (params->curve->seed != NULL) {
  640. OPENSSL_free(ret->seed);
  641. if ((ret->seed = OPENSSL_malloc(params->curve->seed->length)) == NULL) {
  642. ECerr(EC_F_EC_GROUP_NEW_FROM_ECPARAMETERS, ERR_R_MALLOC_FAILURE);
  643. goto err;
  644. }
  645. memcpy(ret->seed, params->curve->seed->data,
  646. params->curve->seed->length);
  647. ret->seed_len = params->curve->seed->length;
  648. }
  649. if (!params->order || !params->base || !params->base->data) {
  650. ECerr(EC_F_EC_GROUP_NEW_FROM_ECPARAMETERS, EC_R_ASN1_ERROR);
  651. goto err;
  652. }
  653. if ((point = EC_POINT_new(ret)) == NULL)
  654. goto err;
  655. /* set the point conversion form */
  656. EC_GROUP_set_point_conversion_form(ret, (point_conversion_form_t)
  657. (params->base->data[0] & ~0x01));
  658. /* extract the ec point */
  659. if (!EC_POINT_oct2point(ret, point, params->base->data,
  660. params->base->length, NULL)) {
  661. ECerr(EC_F_EC_GROUP_NEW_FROM_ECPARAMETERS, ERR_R_EC_LIB);
  662. goto err;
  663. }
  664. /* extract the order */
  665. if ((a = ASN1_INTEGER_to_BN(params->order, a)) == NULL) {
  666. ECerr(EC_F_EC_GROUP_NEW_FROM_ECPARAMETERS, ERR_R_ASN1_LIB);
  667. goto err;
  668. }
  669. if (BN_is_negative(a) || BN_is_zero(a)) {
  670. ECerr(EC_F_EC_GROUP_NEW_FROM_ECPARAMETERS, EC_R_INVALID_GROUP_ORDER);
  671. goto err;
  672. }
  673. if (BN_num_bits(a) > (int)field_bits + 1) { /* Hasse bound */
  674. ECerr(EC_F_EC_GROUP_NEW_FROM_ECPARAMETERS, EC_R_INVALID_GROUP_ORDER);
  675. goto err;
  676. }
  677. /* extract the cofactor (optional) */
  678. if (params->cofactor == NULL) {
  679. BN_free(b);
  680. b = NULL;
  681. } else if ((b = ASN1_INTEGER_to_BN(params->cofactor, b)) == NULL) {
  682. ECerr(EC_F_EC_GROUP_NEW_FROM_ECPARAMETERS, ERR_R_ASN1_LIB);
  683. goto err;
  684. }
  685. /* set the generator, order and cofactor (if present) */
  686. if (!EC_GROUP_set_generator(ret, point, a, b)) {
  687. ECerr(EC_F_EC_GROUP_NEW_FROM_ECPARAMETERS, ERR_R_EC_LIB);
  688. goto err;
  689. }
  690. ok = 1;
  691. err:
  692. if (!ok) {
  693. EC_GROUP_clear_free(ret);
  694. ret = NULL;
  695. }
  696. BN_free(p);
  697. BN_free(a);
  698. BN_free(b);
  699. EC_POINT_free(point);
  700. return ret;
  701. }
  702. EC_GROUP *EC_GROUP_new_from_ecpkparameters(const ECPKPARAMETERS *params)
  703. {
  704. EC_GROUP *ret = NULL;
  705. int tmp = 0;
  706. if (params == NULL) {
  707. ECerr(EC_F_EC_GROUP_NEW_FROM_ECPKPARAMETERS, EC_R_MISSING_PARAMETERS);
  708. return NULL;
  709. }
  710. if (params->type == 0) { /* the curve is given by an OID */
  711. tmp = OBJ_obj2nid(params->value.named_curve);
  712. if ((ret = EC_GROUP_new_by_curve_name(tmp)) == NULL) {
  713. ECerr(EC_F_EC_GROUP_NEW_FROM_ECPKPARAMETERS,
  714. EC_R_EC_GROUP_NEW_BY_NAME_FAILURE);
  715. return NULL;
  716. }
  717. EC_GROUP_set_asn1_flag(ret, OPENSSL_EC_NAMED_CURVE);
  718. } else if (params->type == 1) { /* the parameters are given by a
  719. * ECPARAMETERS structure */
  720. ret = EC_GROUP_new_from_ecparameters(params->value.parameters);
  721. if (!ret) {
  722. ECerr(EC_F_EC_GROUP_NEW_FROM_ECPKPARAMETERS, ERR_R_EC_LIB);
  723. return NULL;
  724. }
  725. EC_GROUP_set_asn1_flag(ret, OPENSSL_EC_EXPLICIT_CURVE);
  726. } else if (params->type == 2) { /* implicitlyCA */
  727. return NULL;
  728. } else {
  729. ECerr(EC_F_EC_GROUP_NEW_FROM_ECPKPARAMETERS, EC_R_ASN1_ERROR);
  730. return NULL;
  731. }
  732. return ret;
  733. }
  734. /* EC_GROUP <-> DER encoding of ECPKPARAMETERS */
  735. EC_GROUP *d2i_ECPKParameters(EC_GROUP **a, const unsigned char **in, long len)
  736. {
  737. EC_GROUP *group = NULL;
  738. ECPKPARAMETERS *params = NULL;
  739. const unsigned char *p = *in;
  740. if ((params = d2i_ECPKPARAMETERS(NULL, &p, len)) == NULL) {
  741. ECerr(EC_F_D2I_ECPKPARAMETERS, EC_R_D2I_ECPKPARAMETERS_FAILURE);
  742. ECPKPARAMETERS_free(params);
  743. return NULL;
  744. }
  745. if ((group = EC_GROUP_new_from_ecpkparameters(params)) == NULL) {
  746. ECerr(EC_F_D2I_ECPKPARAMETERS, EC_R_PKPARAMETERS2GROUP_FAILURE);
  747. ECPKPARAMETERS_free(params);
  748. return NULL;
  749. }
  750. if (a) {
  751. EC_GROUP_clear_free(*a);
  752. *a = group;
  753. }
  754. ECPKPARAMETERS_free(params);
  755. *in = p;
  756. return group;
  757. }
  758. int i2d_ECPKParameters(const EC_GROUP *a, unsigned char **out)
  759. {
  760. int ret = 0;
  761. ECPKPARAMETERS *tmp = EC_GROUP_get_ecpkparameters(a, NULL);
  762. if (tmp == NULL) {
  763. ECerr(EC_F_I2D_ECPKPARAMETERS, EC_R_GROUP2PKPARAMETERS_FAILURE);
  764. return 0;
  765. }
  766. if ((ret = i2d_ECPKPARAMETERS(tmp, out)) == 0) {
  767. ECerr(EC_F_I2D_ECPKPARAMETERS, EC_R_I2D_ECPKPARAMETERS_FAILURE);
  768. ECPKPARAMETERS_free(tmp);
  769. return 0;
  770. }
  771. ECPKPARAMETERS_free(tmp);
  772. return ret;
  773. }
  774. /* some EC_KEY functions */
  775. EC_KEY *d2i_ECPrivateKey(EC_KEY **a, const unsigned char **in, long len)
  776. {
  777. EC_KEY *ret = NULL;
  778. EC_PRIVATEKEY *priv_key = NULL;
  779. const unsigned char *p = *in;
  780. if ((priv_key = d2i_EC_PRIVATEKEY(NULL, &p, len)) == NULL) {
  781. ECerr(EC_F_D2I_ECPRIVATEKEY, ERR_R_EC_LIB);
  782. return NULL;
  783. }
  784. if (a == NULL || *a == NULL) {
  785. if ((ret = EC_KEY_new()) == NULL) {
  786. ECerr(EC_F_D2I_ECPRIVATEKEY, ERR_R_MALLOC_FAILURE);
  787. goto err;
  788. }
  789. } else
  790. ret = *a;
  791. if (priv_key->parameters) {
  792. EC_GROUP_clear_free(ret->group);
  793. ret->group = EC_GROUP_new_from_ecpkparameters(priv_key->parameters);
  794. }
  795. if (ret->group == NULL) {
  796. ECerr(EC_F_D2I_ECPRIVATEKEY, ERR_R_EC_LIB);
  797. goto err;
  798. }
  799. ret->version = priv_key->version;
  800. if (priv_key->privateKey) {
  801. ASN1_OCTET_STRING *pkey = priv_key->privateKey;
  802. if (EC_KEY_oct2priv(ret, ASN1_STRING_get0_data(pkey),
  803. ASN1_STRING_length(pkey)) == 0)
  804. goto err;
  805. } else {
  806. ECerr(EC_F_D2I_ECPRIVATEKEY, EC_R_MISSING_PRIVATE_KEY);
  807. goto err;
  808. }
  809. EC_POINT_clear_free(ret->pub_key);
  810. ret->pub_key = EC_POINT_new(ret->group);
  811. if (ret->pub_key == NULL) {
  812. ECerr(EC_F_D2I_ECPRIVATEKEY, ERR_R_EC_LIB);
  813. goto err;
  814. }
  815. if (priv_key->publicKey) {
  816. const unsigned char *pub_oct;
  817. int pub_oct_len;
  818. pub_oct = ASN1_STRING_get0_data(priv_key->publicKey);
  819. pub_oct_len = ASN1_STRING_length(priv_key->publicKey);
  820. if (!EC_KEY_oct2key(ret, pub_oct, pub_oct_len, NULL)) {
  821. ECerr(EC_F_D2I_ECPRIVATEKEY, ERR_R_EC_LIB);
  822. goto err;
  823. }
  824. } else {
  825. if (ret->group->meth->keygenpub == NULL
  826. || ret->group->meth->keygenpub(ret) == 0)
  827. goto err;
  828. /* Remember the original private-key-only encoding. */
  829. ret->enc_flag |= EC_PKEY_NO_PUBKEY;
  830. }
  831. if (a)
  832. *a = ret;
  833. EC_PRIVATEKEY_free(priv_key);
  834. *in = p;
  835. return ret;
  836. err:
  837. if (a == NULL || *a != ret)
  838. EC_KEY_free(ret);
  839. EC_PRIVATEKEY_free(priv_key);
  840. return NULL;
  841. }
  842. int i2d_ECPrivateKey(EC_KEY *a, unsigned char **out)
  843. {
  844. int ret = 0, ok = 0;
  845. unsigned char *priv= NULL, *pub= NULL;
  846. size_t privlen = 0, publen = 0;
  847. EC_PRIVATEKEY *priv_key = NULL;
  848. if (a == NULL || a->group == NULL ||
  849. (!(a->enc_flag & EC_PKEY_NO_PUBKEY) && a->pub_key == NULL)) {
  850. ECerr(EC_F_I2D_ECPRIVATEKEY, ERR_R_PASSED_NULL_PARAMETER);
  851. goto err;
  852. }
  853. if ((priv_key = EC_PRIVATEKEY_new()) == NULL) {
  854. ECerr(EC_F_I2D_ECPRIVATEKEY, ERR_R_MALLOC_FAILURE);
  855. goto err;
  856. }
  857. priv_key->version = a->version;
  858. privlen = EC_KEY_priv2buf(a, &priv);
  859. if (privlen == 0) {
  860. ECerr(EC_F_I2D_ECPRIVATEKEY, ERR_R_EC_LIB);
  861. goto err;
  862. }
  863. ASN1_STRING_set0(priv_key->privateKey, priv, privlen);
  864. priv = NULL;
  865. if (!(a->enc_flag & EC_PKEY_NO_PARAMETERS)) {
  866. if ((priv_key->parameters =
  867. EC_GROUP_get_ecpkparameters(a->group,
  868. priv_key->parameters)) == NULL) {
  869. ECerr(EC_F_I2D_ECPRIVATEKEY, ERR_R_EC_LIB);
  870. goto err;
  871. }
  872. }
  873. if (!(a->enc_flag & EC_PKEY_NO_PUBKEY)) {
  874. priv_key->publicKey = ASN1_BIT_STRING_new();
  875. if (priv_key->publicKey == NULL) {
  876. ECerr(EC_F_I2D_ECPRIVATEKEY, ERR_R_MALLOC_FAILURE);
  877. goto err;
  878. }
  879. publen = EC_KEY_key2buf(a, a->conv_form, &pub, NULL);
  880. if (publen == 0) {
  881. ECerr(EC_F_I2D_ECPRIVATEKEY, ERR_R_EC_LIB);
  882. goto err;
  883. }
  884. priv_key->publicKey->flags &= ~(ASN1_STRING_FLAG_BITS_LEFT | 0x07);
  885. priv_key->publicKey->flags |= ASN1_STRING_FLAG_BITS_LEFT;
  886. ASN1_STRING_set0(priv_key->publicKey, pub, publen);
  887. pub = NULL;
  888. }
  889. if ((ret = i2d_EC_PRIVATEKEY(priv_key, out)) == 0) {
  890. ECerr(EC_F_I2D_ECPRIVATEKEY, ERR_R_EC_LIB);
  891. goto err;
  892. }
  893. ok = 1;
  894. err:
  895. OPENSSL_clear_free(priv, privlen);
  896. OPENSSL_free(pub);
  897. EC_PRIVATEKEY_free(priv_key);
  898. return (ok ? ret : 0);
  899. }
  900. int i2d_ECParameters(EC_KEY *a, unsigned char **out)
  901. {
  902. if (a == NULL) {
  903. ECerr(EC_F_I2D_ECPARAMETERS, ERR_R_PASSED_NULL_PARAMETER);
  904. return 0;
  905. }
  906. return i2d_ECPKParameters(a->group, out);
  907. }
  908. EC_KEY *d2i_ECParameters(EC_KEY **a, const unsigned char **in, long len)
  909. {
  910. EC_KEY *ret;
  911. if (in == NULL || *in == NULL) {
  912. ECerr(EC_F_D2I_ECPARAMETERS, ERR_R_PASSED_NULL_PARAMETER);
  913. return NULL;
  914. }
  915. if (a == NULL || *a == NULL) {
  916. if ((ret = EC_KEY_new()) == NULL) {
  917. ECerr(EC_F_D2I_ECPARAMETERS, ERR_R_MALLOC_FAILURE);
  918. return NULL;
  919. }
  920. } else
  921. ret = *a;
  922. if (!d2i_ECPKParameters(&ret->group, in, len)) {
  923. ECerr(EC_F_D2I_ECPARAMETERS, ERR_R_EC_LIB);
  924. if (a == NULL || *a != ret)
  925. EC_KEY_free(ret);
  926. return NULL;
  927. }
  928. if (a)
  929. *a = ret;
  930. return ret;
  931. }
  932. EC_KEY *o2i_ECPublicKey(EC_KEY **a, const unsigned char **in, long len)
  933. {
  934. EC_KEY *ret = NULL;
  935. if (a == NULL || (*a) == NULL || (*a)->group == NULL) {
  936. /*
  937. * sorry, but a EC_GROUP-structure is necessary to set the public key
  938. */
  939. ECerr(EC_F_O2I_ECPUBLICKEY, ERR_R_PASSED_NULL_PARAMETER);
  940. return 0;
  941. }
  942. ret = *a;
  943. if (!EC_KEY_oct2key(ret, *in, len, NULL)) {
  944. ECerr(EC_F_O2I_ECPUBLICKEY, ERR_R_EC_LIB);
  945. return 0;
  946. }
  947. *in += len;
  948. return ret;
  949. }
  950. int i2o_ECPublicKey(const EC_KEY *a, unsigned char **out)
  951. {
  952. size_t buf_len = 0;
  953. int new_buffer = 0;
  954. if (a == NULL) {
  955. ECerr(EC_F_I2O_ECPUBLICKEY, ERR_R_PASSED_NULL_PARAMETER);
  956. return 0;
  957. }
  958. buf_len = EC_POINT_point2oct(a->group, a->pub_key,
  959. a->conv_form, NULL, 0, NULL);
  960. if (out == NULL || buf_len == 0)
  961. /* out == NULL => just return the length of the octet string */
  962. return buf_len;
  963. if (*out == NULL) {
  964. if ((*out = OPENSSL_malloc(buf_len)) == NULL) {
  965. ECerr(EC_F_I2O_ECPUBLICKEY, ERR_R_MALLOC_FAILURE);
  966. return 0;
  967. }
  968. new_buffer = 1;
  969. }
  970. if (!EC_POINT_point2oct(a->group, a->pub_key, a->conv_form,
  971. *out, buf_len, NULL)) {
  972. ECerr(EC_F_I2O_ECPUBLICKEY, ERR_R_EC_LIB);
  973. if (new_buffer) {
  974. OPENSSL_free(*out);
  975. *out = NULL;
  976. }
  977. return 0;
  978. }
  979. if (!new_buffer)
  980. *out += buf_len;
  981. return buf_len;
  982. }
  983. ASN1_SEQUENCE(ECDSA_SIG) = {
  984. ASN1_SIMPLE(ECDSA_SIG, r, CBIGNUM),
  985. ASN1_SIMPLE(ECDSA_SIG, s, CBIGNUM)
  986. } static_ASN1_SEQUENCE_END(ECDSA_SIG)
  987. DECLARE_ASN1_FUNCTIONS_const(ECDSA_SIG)
  988. DECLARE_ASN1_ENCODE_FUNCTIONS_const(ECDSA_SIG, ECDSA_SIG)
  989. IMPLEMENT_ASN1_ENCODE_FUNCTIONS_const_fname(ECDSA_SIG, ECDSA_SIG, ECDSA_SIG)
  990. ECDSA_SIG *ECDSA_SIG_new(void)
  991. {
  992. ECDSA_SIG *sig = OPENSSL_zalloc(sizeof(*sig));
  993. if (sig == NULL)
  994. ECerr(EC_F_ECDSA_SIG_NEW, ERR_R_MALLOC_FAILURE);
  995. return sig;
  996. }
  997. void ECDSA_SIG_free(ECDSA_SIG *sig)
  998. {
  999. if (sig == NULL)
  1000. return;
  1001. BN_clear_free(sig->r);
  1002. BN_clear_free(sig->s);
  1003. OPENSSL_free(sig);
  1004. }
  1005. void ECDSA_SIG_get0(const ECDSA_SIG *sig, const BIGNUM **pr, const BIGNUM **ps)
  1006. {
  1007. if (pr != NULL)
  1008. *pr = sig->r;
  1009. if (ps != NULL)
  1010. *ps = sig->s;
  1011. }
  1012. const BIGNUM *ECDSA_SIG_get0_r(const ECDSA_SIG *sig)
  1013. {
  1014. return sig->r;
  1015. }
  1016. const BIGNUM *ECDSA_SIG_get0_s(const ECDSA_SIG *sig)
  1017. {
  1018. return sig->s;
  1019. }
  1020. int ECDSA_SIG_set0(ECDSA_SIG *sig, BIGNUM *r, BIGNUM *s)
  1021. {
  1022. if (r == NULL || s == NULL)
  1023. return 0;
  1024. BN_clear_free(sig->r);
  1025. BN_clear_free(sig->s);
  1026. sig->r = r;
  1027. sig->s = s;
  1028. return 1;
  1029. }
  1030. int ECDSA_size(const EC_KEY *r)
  1031. {
  1032. int ret, i;
  1033. ASN1_INTEGER bs;
  1034. unsigned char buf[4];
  1035. const EC_GROUP *group;
  1036. if (r == NULL)
  1037. return 0;
  1038. group = EC_KEY_get0_group(r);
  1039. if (group == NULL)
  1040. return 0;
  1041. i = EC_GROUP_order_bits(group);
  1042. if (i == 0)
  1043. return 0;
  1044. bs.length = (i + 7) / 8;
  1045. bs.data = buf;
  1046. bs.type = V_ASN1_INTEGER;
  1047. /* If the top bit is set the asn1 encoding is 1 larger. */
  1048. buf[0] = 0xff;
  1049. i = i2d_ASN1_INTEGER(&bs, NULL);
  1050. i += i; /* r and s */
  1051. ret = ASN1_object_size(1, i, V_ASN1_SEQUENCE);
  1052. return ret;
  1053. }