ec_asn1.c 40 KB

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