ec_asn1.c 39 KB

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