2
0

ec_asn1.c 38 KB

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