1
0

ec_asn1.c 38 KB

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