ec_asn1.c 40 KB

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