ec_asn1.c 39 KB

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