ec_asn1.c 40 KB

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