ec_asn1.c 35 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221
  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(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;
  488. BIGNUM *p = NULL, *a = NULL, *b = NULL;
  489. EC_POINT *point = NULL;
  490. long field_bits;
  491. if (!params->fieldID || !params->fieldID->fieldType ||
  492. !params->fieldID->p.ptr) {
  493. ECerr(EC_F_EC_GROUP_NEW_FROM_ECPARAMETERS, EC_R_ASN1_ERROR);
  494. goto err;
  495. }
  496. /*
  497. * Now extract the curve parameters a and b. Note that, although SEC 1
  498. * specifies the length of their encodings, historical versions of OpenSSL
  499. * encoded them incorrectly, so we must accept any length for backwards
  500. * compatibility.
  501. */
  502. if (!params->curve || !params->curve->a ||
  503. !params->curve->a->data || !params->curve->b ||
  504. !params->curve->b->data) {
  505. ECerr(EC_F_EC_GROUP_NEW_FROM_ECPARAMETERS, EC_R_ASN1_ERROR);
  506. goto err;
  507. }
  508. a = BN_bin2bn(params->curve->a->data, params->curve->a->length, NULL);
  509. if (a == NULL) {
  510. ECerr(EC_F_EC_GROUP_NEW_FROM_ECPARAMETERS, ERR_R_BN_LIB);
  511. goto err;
  512. }
  513. b = BN_bin2bn(params->curve->b->data, params->curve->b->length, NULL);
  514. if (b == NULL) {
  515. ECerr(EC_F_EC_GROUP_NEW_FROM_ECPARAMETERS, ERR_R_BN_LIB);
  516. goto err;
  517. }
  518. /* get the field parameters */
  519. tmp = OBJ_obj2nid(params->fieldID->fieldType);
  520. if (tmp == NID_X9_62_characteristic_two_field)
  521. #ifdef OPENSSL_NO_EC2M
  522. {
  523. ECerr(EC_F_EC_GROUP_NEW_FROM_ECPARAMETERS, EC_R_GF2M_NOT_SUPPORTED);
  524. goto err;
  525. }
  526. #else
  527. {
  528. X9_62_CHARACTERISTIC_TWO *char_two;
  529. char_two = params->fieldID->p.char_two;
  530. field_bits = char_two->m;
  531. if (field_bits > OPENSSL_ECC_MAX_FIELD_BITS) {
  532. ECerr(EC_F_EC_GROUP_NEW_FROM_ECPARAMETERS, EC_R_FIELD_TOO_LARGE);
  533. goto err;
  534. }
  535. if ((p = BN_new()) == NULL) {
  536. ECerr(EC_F_EC_GROUP_NEW_FROM_ECPARAMETERS, ERR_R_MALLOC_FAILURE);
  537. goto err;
  538. }
  539. /* get the base type */
  540. tmp = OBJ_obj2nid(char_two->type);
  541. if (tmp == NID_X9_62_tpBasis) {
  542. long tmp_long;
  543. if (!char_two->p.tpBasis) {
  544. ECerr(EC_F_EC_GROUP_NEW_FROM_ECPARAMETERS, EC_R_ASN1_ERROR);
  545. goto err;
  546. }
  547. tmp_long = ASN1_INTEGER_get(char_two->p.tpBasis);
  548. if (!(char_two->m > tmp_long && tmp_long > 0)) {
  549. ECerr(EC_F_EC_GROUP_NEW_FROM_ECPARAMETERS,
  550. EC_R_INVALID_TRINOMIAL_BASIS);
  551. goto err;
  552. }
  553. /* create the polynomial */
  554. if (!BN_set_bit(p, (int)char_two->m))
  555. goto err;
  556. if (!BN_set_bit(p, (int)tmp_long))
  557. goto err;
  558. if (!BN_set_bit(p, 0))
  559. goto err;
  560. } else if (tmp == NID_X9_62_ppBasis) {
  561. X9_62_PENTANOMIAL *penta;
  562. penta = char_two->p.ppBasis;
  563. if (!penta) {
  564. ECerr(EC_F_EC_GROUP_NEW_FROM_ECPARAMETERS, EC_R_ASN1_ERROR);
  565. goto err;
  566. }
  567. if (!
  568. (char_two->m > penta->k3 && penta->k3 > penta->k2
  569. && penta->k2 > penta->k1 && penta->k1 > 0)) {
  570. ECerr(EC_F_EC_GROUP_NEW_FROM_ECPARAMETERS,
  571. EC_R_INVALID_PENTANOMIAL_BASIS);
  572. goto err;
  573. }
  574. /* create the polynomial */
  575. if (!BN_set_bit(p, (int)char_two->m))
  576. goto err;
  577. if (!BN_set_bit(p, (int)penta->k1))
  578. goto err;
  579. if (!BN_set_bit(p, (int)penta->k2))
  580. goto err;
  581. if (!BN_set_bit(p, (int)penta->k3))
  582. goto err;
  583. if (!BN_set_bit(p, 0))
  584. goto err;
  585. } else if (tmp == NID_X9_62_onBasis) {
  586. ECerr(EC_F_EC_GROUP_NEW_FROM_ECPARAMETERS, EC_R_NOT_IMPLEMENTED);
  587. goto err;
  588. } else { /* error */
  589. ECerr(EC_F_EC_GROUP_NEW_FROM_ECPARAMETERS, EC_R_ASN1_ERROR);
  590. goto err;
  591. }
  592. /* create the EC_GROUP structure */
  593. ret = EC_GROUP_new_curve_GF2m(p, a, b, NULL);
  594. }
  595. #endif
  596. else if (tmp == NID_X9_62_prime_field) {
  597. /* we have a curve over a prime field */
  598. /* extract the prime number */
  599. if (!params->fieldID->p.prime) {
  600. ECerr(EC_F_EC_GROUP_NEW_FROM_ECPARAMETERS, EC_R_ASN1_ERROR);
  601. goto err;
  602. }
  603. p = ASN1_INTEGER_to_BN(params->fieldID->p.prime, NULL);
  604. if (p == NULL) {
  605. ECerr(EC_F_EC_GROUP_NEW_FROM_ECPARAMETERS, ERR_R_ASN1_LIB);
  606. goto err;
  607. }
  608. if (BN_is_negative(p) || BN_is_zero(p)) {
  609. ECerr(EC_F_EC_GROUP_NEW_FROM_ECPARAMETERS, EC_R_INVALID_FIELD);
  610. goto err;
  611. }
  612. field_bits = BN_num_bits(p);
  613. if (field_bits > OPENSSL_ECC_MAX_FIELD_BITS) {
  614. ECerr(EC_F_EC_GROUP_NEW_FROM_ECPARAMETERS, EC_R_FIELD_TOO_LARGE);
  615. goto err;
  616. }
  617. /* create the EC_GROUP structure */
  618. ret = EC_GROUP_new_curve_GFp(p, a, b, NULL);
  619. } else {
  620. ECerr(EC_F_EC_GROUP_NEW_FROM_ECPARAMETERS, EC_R_INVALID_FIELD);
  621. goto err;
  622. }
  623. if (ret == NULL) {
  624. ECerr(EC_F_EC_GROUP_NEW_FROM_ECPARAMETERS, ERR_R_EC_LIB);
  625. goto err;
  626. }
  627. /* extract seed (optional) */
  628. if (params->curve->seed != NULL) {
  629. OPENSSL_free(ret->seed);
  630. if ((ret->seed = OPENSSL_malloc(params->curve->seed->length)) == NULL) {
  631. ECerr(EC_F_EC_GROUP_NEW_FROM_ECPARAMETERS, ERR_R_MALLOC_FAILURE);
  632. goto err;
  633. }
  634. memcpy(ret->seed, params->curve->seed->data,
  635. params->curve->seed->length);
  636. ret->seed_len = params->curve->seed->length;
  637. }
  638. if (!params->order || !params->base || !params->base->data) {
  639. ECerr(EC_F_EC_GROUP_NEW_FROM_ECPARAMETERS, EC_R_ASN1_ERROR);
  640. goto err;
  641. }
  642. if ((point = EC_POINT_new(ret)) == NULL)
  643. goto err;
  644. /* set the point conversion form */
  645. EC_GROUP_set_point_conversion_form(ret, (point_conversion_form_t)
  646. (params->base->data[0] & ~0x01));
  647. /* extract the ec point */
  648. if (!EC_POINT_oct2point(ret, point, params->base->data,
  649. params->base->length, NULL)) {
  650. ECerr(EC_F_EC_GROUP_NEW_FROM_ECPARAMETERS, ERR_R_EC_LIB);
  651. goto err;
  652. }
  653. /* extract the order */
  654. if ((a = ASN1_INTEGER_to_BN(params->order, a)) == NULL) {
  655. ECerr(EC_F_EC_GROUP_NEW_FROM_ECPARAMETERS, ERR_R_ASN1_LIB);
  656. goto err;
  657. }
  658. if (BN_is_negative(a) || BN_is_zero(a)) {
  659. ECerr(EC_F_EC_GROUP_NEW_FROM_ECPARAMETERS, EC_R_INVALID_GROUP_ORDER);
  660. goto err;
  661. }
  662. if (BN_num_bits(a) > (int)field_bits + 1) { /* Hasse bound */
  663. ECerr(EC_F_EC_GROUP_NEW_FROM_ECPARAMETERS, EC_R_INVALID_GROUP_ORDER);
  664. goto err;
  665. }
  666. /* extract the cofactor (optional) */
  667. if (params->cofactor == NULL) {
  668. BN_free(b);
  669. b = NULL;
  670. } else if ((b = ASN1_INTEGER_to_BN(params->cofactor, b)) == NULL) {
  671. ECerr(EC_F_EC_GROUP_NEW_FROM_ECPARAMETERS, ERR_R_ASN1_LIB);
  672. goto err;
  673. }
  674. /* set the generator, order and cofactor (if present) */
  675. if (!EC_GROUP_set_generator(ret, point, a, b)) {
  676. ECerr(EC_F_EC_GROUP_NEW_FROM_ECPARAMETERS, ERR_R_EC_LIB);
  677. goto err;
  678. }
  679. ok = 1;
  680. err:
  681. if (!ok) {
  682. EC_GROUP_clear_free(ret);
  683. ret = NULL;
  684. }
  685. BN_free(p);
  686. BN_free(a);
  687. BN_free(b);
  688. EC_POINT_free(point);
  689. return ret;
  690. }
  691. EC_GROUP *EC_GROUP_new_from_ecpkparameters(const ECPKPARAMETERS *params)
  692. {
  693. EC_GROUP *ret = NULL;
  694. int tmp = 0;
  695. if (params == NULL) {
  696. ECerr(EC_F_EC_GROUP_NEW_FROM_ECPKPARAMETERS, EC_R_MISSING_PARAMETERS);
  697. return NULL;
  698. }
  699. if (params->type == 0) { /* the curve is given by an OID */
  700. tmp = OBJ_obj2nid(params->value.named_curve);
  701. if ((ret = EC_GROUP_new_by_curve_name(tmp)) == NULL) {
  702. ECerr(EC_F_EC_GROUP_NEW_FROM_ECPKPARAMETERS,
  703. EC_R_EC_GROUP_NEW_BY_NAME_FAILURE);
  704. return NULL;
  705. }
  706. EC_GROUP_set_asn1_flag(ret, OPENSSL_EC_NAMED_CURVE);
  707. } else if (params->type == 1) { /* the parameters are given by a
  708. * ECPARAMETERS structure */
  709. ret = EC_GROUP_new_from_ecparameters(params->value.parameters);
  710. if (!ret) {
  711. ECerr(EC_F_EC_GROUP_NEW_FROM_ECPKPARAMETERS, ERR_R_EC_LIB);
  712. return NULL;
  713. }
  714. EC_GROUP_set_asn1_flag(ret, OPENSSL_EC_EXPLICIT_CURVE);
  715. } else if (params->type == 2) { /* implicitlyCA */
  716. return NULL;
  717. } else {
  718. ECerr(EC_F_EC_GROUP_NEW_FROM_ECPKPARAMETERS, EC_R_ASN1_ERROR);
  719. return NULL;
  720. }
  721. return ret;
  722. }
  723. /* EC_GROUP <-> DER encoding of ECPKPARAMETERS */
  724. EC_GROUP *d2i_ECPKParameters(EC_GROUP **a, const unsigned char **in, long len)
  725. {
  726. EC_GROUP *group = NULL;
  727. ECPKPARAMETERS *params = NULL;
  728. const unsigned char *p = *in;
  729. if ((params = d2i_ECPKPARAMETERS(NULL, &p, len)) == NULL) {
  730. ECerr(EC_F_D2I_ECPKPARAMETERS, EC_R_D2I_ECPKPARAMETERS_FAILURE);
  731. ECPKPARAMETERS_free(params);
  732. return NULL;
  733. }
  734. if ((group = EC_GROUP_new_from_ecpkparameters(params)) == NULL) {
  735. ECerr(EC_F_D2I_ECPKPARAMETERS, EC_R_PKPARAMETERS2GROUP_FAILURE);
  736. ECPKPARAMETERS_free(params);
  737. return NULL;
  738. }
  739. if (a) {
  740. EC_GROUP_clear_free(*a);
  741. *a = group;
  742. }
  743. ECPKPARAMETERS_free(params);
  744. *in = p;
  745. return group;
  746. }
  747. int i2d_ECPKParameters(const EC_GROUP *a, unsigned char **out)
  748. {
  749. int ret = 0;
  750. ECPKPARAMETERS *tmp = EC_GROUP_get_ecpkparameters(a, NULL);
  751. if (tmp == NULL) {
  752. ECerr(EC_F_I2D_ECPKPARAMETERS, EC_R_GROUP2PKPARAMETERS_FAILURE);
  753. return 0;
  754. }
  755. if ((ret = i2d_ECPKPARAMETERS(tmp, out)) == 0) {
  756. ECerr(EC_F_I2D_ECPKPARAMETERS, EC_R_I2D_ECPKPARAMETERS_FAILURE);
  757. ECPKPARAMETERS_free(tmp);
  758. return 0;
  759. }
  760. ECPKPARAMETERS_free(tmp);
  761. return ret;
  762. }
  763. /* some EC_KEY functions */
  764. EC_KEY *d2i_ECPrivateKey(EC_KEY **a, const unsigned char **in, long len)
  765. {
  766. EC_KEY *ret = NULL;
  767. EC_PRIVATEKEY *priv_key = NULL;
  768. const unsigned char *p = *in;
  769. if ((priv_key = d2i_EC_PRIVATEKEY(NULL, &p, len)) == NULL) {
  770. ECerr(EC_F_D2I_ECPRIVATEKEY, ERR_R_EC_LIB);
  771. return NULL;
  772. }
  773. if (a == NULL || *a == NULL) {
  774. if ((ret = EC_KEY_new()) == NULL) {
  775. ECerr(EC_F_D2I_ECPRIVATEKEY, ERR_R_MALLOC_FAILURE);
  776. goto err;
  777. }
  778. } else
  779. ret = *a;
  780. if (priv_key->parameters) {
  781. EC_GROUP_clear_free(ret->group);
  782. ret->group = EC_GROUP_new_from_ecpkparameters(priv_key->parameters);
  783. }
  784. if (ret->group == NULL) {
  785. ECerr(EC_F_D2I_ECPRIVATEKEY, ERR_R_EC_LIB);
  786. goto err;
  787. }
  788. ret->version = priv_key->version;
  789. if (priv_key->privateKey) {
  790. ASN1_OCTET_STRING *pkey = priv_key->privateKey;
  791. if (EC_KEY_oct2priv(ret, ASN1_STRING_get0_data(pkey),
  792. ASN1_STRING_length(pkey)) == 0)
  793. goto err;
  794. } else {
  795. ECerr(EC_F_D2I_ECPRIVATEKEY, EC_R_MISSING_PRIVATE_KEY);
  796. goto err;
  797. }
  798. EC_POINT_clear_free(ret->pub_key);
  799. ret->pub_key = EC_POINT_new(ret->group);
  800. if (ret->pub_key == NULL) {
  801. ECerr(EC_F_D2I_ECPRIVATEKEY, ERR_R_EC_LIB);
  802. goto err;
  803. }
  804. if (priv_key->publicKey) {
  805. const unsigned char *pub_oct;
  806. int pub_oct_len;
  807. pub_oct = ASN1_STRING_get0_data(priv_key->publicKey);
  808. pub_oct_len = ASN1_STRING_length(priv_key->publicKey);
  809. if (!EC_KEY_oct2key(ret, pub_oct, pub_oct_len, NULL)) {
  810. ECerr(EC_F_D2I_ECPRIVATEKEY, ERR_R_EC_LIB);
  811. goto err;
  812. }
  813. } else {
  814. if (ret->group->meth->keygenpub == NULL
  815. || ret->group->meth->keygenpub(ret) == 0)
  816. goto err;
  817. /* Remember the original private-key-only encoding. */
  818. ret->enc_flag |= EC_PKEY_NO_PUBKEY;
  819. }
  820. if (a)
  821. *a = ret;
  822. EC_PRIVATEKEY_free(priv_key);
  823. *in = p;
  824. return ret;
  825. err:
  826. if (a == NULL || *a != ret)
  827. EC_KEY_free(ret);
  828. EC_PRIVATEKEY_free(priv_key);
  829. return NULL;
  830. }
  831. int i2d_ECPrivateKey(EC_KEY *a, unsigned char **out)
  832. {
  833. int ret = 0, ok = 0;
  834. unsigned char *priv= NULL, *pub= NULL;
  835. size_t privlen = 0, publen = 0;
  836. EC_PRIVATEKEY *priv_key = NULL;
  837. if (a == NULL || a->group == NULL ||
  838. (!(a->enc_flag & EC_PKEY_NO_PUBKEY) && a->pub_key == NULL)) {
  839. ECerr(EC_F_I2D_ECPRIVATEKEY, ERR_R_PASSED_NULL_PARAMETER);
  840. goto err;
  841. }
  842. if ((priv_key = EC_PRIVATEKEY_new()) == NULL) {
  843. ECerr(EC_F_I2D_ECPRIVATEKEY, ERR_R_MALLOC_FAILURE);
  844. goto err;
  845. }
  846. priv_key->version = a->version;
  847. privlen = EC_KEY_priv2buf(a, &priv);
  848. if (privlen == 0) {
  849. ECerr(EC_F_I2D_ECPRIVATEKEY, ERR_R_EC_LIB);
  850. goto err;
  851. }
  852. ASN1_STRING_set0(priv_key->privateKey, priv, privlen);
  853. priv = NULL;
  854. if (!(a->enc_flag & EC_PKEY_NO_PARAMETERS)) {
  855. if ((priv_key->parameters =
  856. EC_GROUP_get_ecpkparameters(a->group,
  857. priv_key->parameters)) == NULL) {
  858. ECerr(EC_F_I2D_ECPRIVATEKEY, ERR_R_EC_LIB);
  859. goto err;
  860. }
  861. }
  862. if (!(a->enc_flag & EC_PKEY_NO_PUBKEY)) {
  863. priv_key->publicKey = ASN1_BIT_STRING_new();
  864. if (priv_key->publicKey == NULL) {
  865. ECerr(EC_F_I2D_ECPRIVATEKEY, ERR_R_MALLOC_FAILURE);
  866. goto err;
  867. }
  868. publen = EC_KEY_key2buf(a, a->conv_form, &pub, NULL);
  869. if (publen == 0) {
  870. ECerr(EC_F_I2D_ECPRIVATEKEY, ERR_R_EC_LIB);
  871. goto err;
  872. }
  873. priv_key->publicKey->flags &= ~(ASN1_STRING_FLAG_BITS_LEFT | 0x07);
  874. priv_key->publicKey->flags |= ASN1_STRING_FLAG_BITS_LEFT;
  875. ASN1_STRING_set0(priv_key->publicKey, pub, publen);
  876. pub = NULL;
  877. }
  878. if ((ret = i2d_EC_PRIVATEKEY(priv_key, out)) == 0) {
  879. ECerr(EC_F_I2D_ECPRIVATEKEY, ERR_R_EC_LIB);
  880. goto err;
  881. }
  882. ok = 1;
  883. err:
  884. OPENSSL_clear_free(priv, privlen);
  885. OPENSSL_free(pub);
  886. EC_PRIVATEKEY_free(priv_key);
  887. return (ok ? ret : 0);
  888. }
  889. int i2d_ECParameters(EC_KEY *a, unsigned char **out)
  890. {
  891. if (a == NULL) {
  892. ECerr(EC_F_I2D_ECPARAMETERS, ERR_R_PASSED_NULL_PARAMETER);
  893. return 0;
  894. }
  895. return i2d_ECPKParameters(a->group, out);
  896. }
  897. EC_KEY *d2i_ECParameters(EC_KEY **a, const unsigned char **in, long len)
  898. {
  899. EC_KEY *ret;
  900. if (in == NULL || *in == NULL) {
  901. ECerr(EC_F_D2I_ECPARAMETERS, ERR_R_PASSED_NULL_PARAMETER);
  902. return NULL;
  903. }
  904. if (a == NULL || *a == NULL) {
  905. if ((ret = EC_KEY_new()) == NULL) {
  906. ECerr(EC_F_D2I_ECPARAMETERS, ERR_R_MALLOC_FAILURE);
  907. return NULL;
  908. }
  909. } else
  910. ret = *a;
  911. if (!d2i_ECPKParameters(&ret->group, in, len)) {
  912. ECerr(EC_F_D2I_ECPARAMETERS, ERR_R_EC_LIB);
  913. if (a == NULL || *a != ret)
  914. EC_KEY_free(ret);
  915. return NULL;
  916. }
  917. if (a)
  918. *a = ret;
  919. return ret;
  920. }
  921. EC_KEY *o2i_ECPublicKey(EC_KEY **a, const unsigned char **in, long len)
  922. {
  923. EC_KEY *ret = NULL;
  924. if (a == NULL || (*a) == NULL || (*a)->group == NULL) {
  925. /*
  926. * sorry, but a EC_GROUP-structure is necessary to set the public key
  927. */
  928. ECerr(EC_F_O2I_ECPUBLICKEY, ERR_R_PASSED_NULL_PARAMETER);
  929. return 0;
  930. }
  931. ret = *a;
  932. if (!EC_KEY_oct2key(ret, *in, len, NULL)) {
  933. ECerr(EC_F_O2I_ECPUBLICKEY, ERR_R_EC_LIB);
  934. return 0;
  935. }
  936. *in += len;
  937. return ret;
  938. }
  939. int i2o_ECPublicKey(const EC_KEY *a, unsigned char **out)
  940. {
  941. size_t buf_len = 0;
  942. int new_buffer = 0;
  943. if (a == NULL) {
  944. ECerr(EC_F_I2O_ECPUBLICKEY, ERR_R_PASSED_NULL_PARAMETER);
  945. return 0;
  946. }
  947. buf_len = EC_POINT_point2oct(a->group, a->pub_key,
  948. a->conv_form, NULL, 0, NULL);
  949. if (out == NULL || buf_len == 0)
  950. /* out == NULL => just return the length of the octet string */
  951. return buf_len;
  952. if (*out == NULL) {
  953. if ((*out = OPENSSL_malloc(buf_len)) == NULL) {
  954. ECerr(EC_F_I2O_ECPUBLICKEY, ERR_R_MALLOC_FAILURE);
  955. return 0;
  956. }
  957. new_buffer = 1;
  958. }
  959. if (!EC_POINT_point2oct(a->group, a->pub_key, a->conv_form,
  960. *out, buf_len, NULL)) {
  961. ECerr(EC_F_I2O_ECPUBLICKEY, ERR_R_EC_LIB);
  962. if (new_buffer) {
  963. OPENSSL_free(*out);
  964. *out = NULL;
  965. }
  966. return 0;
  967. }
  968. if (!new_buffer)
  969. *out += buf_len;
  970. return buf_len;
  971. }
  972. ASN1_SEQUENCE(ECDSA_SIG) = {
  973. ASN1_SIMPLE(ECDSA_SIG, r, CBIGNUM),
  974. ASN1_SIMPLE(ECDSA_SIG, s, CBIGNUM)
  975. } static_ASN1_SEQUENCE_END(ECDSA_SIG)
  976. DECLARE_ASN1_FUNCTIONS_const(ECDSA_SIG)
  977. DECLARE_ASN1_ENCODE_FUNCTIONS_const(ECDSA_SIG, ECDSA_SIG)
  978. IMPLEMENT_ASN1_ENCODE_FUNCTIONS_const_fname(ECDSA_SIG, ECDSA_SIG, ECDSA_SIG)
  979. ECDSA_SIG *ECDSA_SIG_new(void)
  980. {
  981. ECDSA_SIG *sig = OPENSSL_zalloc(sizeof(*sig));
  982. if (sig == NULL)
  983. ECerr(EC_F_ECDSA_SIG_NEW, ERR_R_MALLOC_FAILURE);
  984. return sig;
  985. }
  986. void ECDSA_SIG_free(ECDSA_SIG *sig)
  987. {
  988. if (sig == NULL)
  989. return;
  990. BN_clear_free(sig->r);
  991. BN_clear_free(sig->s);
  992. OPENSSL_free(sig);
  993. }
  994. void ECDSA_SIG_get0(const ECDSA_SIG *sig, const BIGNUM **pr, const BIGNUM **ps)
  995. {
  996. if (pr != NULL)
  997. *pr = sig->r;
  998. if (ps != NULL)
  999. *ps = sig->s;
  1000. }
  1001. const BIGNUM *ECDSA_SIG_get0_r(const ECDSA_SIG *sig)
  1002. {
  1003. return sig->r;
  1004. }
  1005. const BIGNUM *ECDSA_SIG_get0_s(const ECDSA_SIG *sig)
  1006. {
  1007. return sig->s;
  1008. }
  1009. int ECDSA_SIG_set0(ECDSA_SIG *sig, BIGNUM *r, BIGNUM *s)
  1010. {
  1011. if (r == NULL || s == NULL)
  1012. return 0;
  1013. BN_clear_free(sig->r);
  1014. BN_clear_free(sig->s);
  1015. sig->r = r;
  1016. sig->s = s;
  1017. return 1;
  1018. }
  1019. int ECDSA_size(const EC_KEY *r)
  1020. {
  1021. int ret, i;
  1022. ASN1_INTEGER bs;
  1023. unsigned char buf[4];
  1024. const EC_GROUP *group;
  1025. if (r == NULL)
  1026. return 0;
  1027. group = EC_KEY_get0_group(r);
  1028. if (group == NULL)
  1029. return 0;
  1030. i = EC_GROUP_order_bits(group);
  1031. if (i == 0)
  1032. return 0;
  1033. bs.length = (i + 7) / 8;
  1034. bs.data = buf;
  1035. bs.type = V_ASN1_INTEGER;
  1036. /* If the top bit is set the asn1 encoding is 1 larger. */
  1037. buf[0] = 0xff;
  1038. i = i2d_ASN1_INTEGER(&bs, NULL);
  1039. i += i; /* r and s */
  1040. ret = ASN1_object_size(1, i, V_ASN1_SEQUENCE);
  1041. return ret;
  1042. }