ec_asn1.c 40 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346
  1. /*
  2. * Written by Nils Larsch for the OpenSSL project.
  3. */
  4. /* ====================================================================
  5. * Copyright (c) 2000-2003 The OpenSSL Project. All rights reserved.
  6. *
  7. * Redistribution and use in source and binary forms, with or without
  8. * modification, are permitted provided that the following conditions
  9. * are met:
  10. *
  11. * 1. Redistributions of source code must retain the above copyright
  12. * notice, this list of conditions and the following disclaimer.
  13. *
  14. * 2. Redistributions in binary form must reproduce the above copyright
  15. * notice, this list of conditions and the following disclaimer in
  16. * the documentation and/or other materials provided with the
  17. * distribution.
  18. *
  19. * 3. All advertising materials mentioning features or use of this
  20. * software must display the following acknowledgment:
  21. * "This product includes software developed by the OpenSSL Project
  22. * for use in the OpenSSL Toolkit. (http://www.OpenSSL.org/)"
  23. *
  24. * 4. The names "OpenSSL Toolkit" and "OpenSSL Project" must not be used to
  25. * endorse or promote products derived from this software without
  26. * prior written permission. For written permission, please contact
  27. * licensing@OpenSSL.org.
  28. *
  29. * 5. Products derived from this software may not be called "OpenSSL"
  30. * nor may "OpenSSL" appear in their names without prior written
  31. * permission of the OpenSSL Project.
  32. *
  33. * 6. Redistributions of any form whatsoever must retain the following
  34. * acknowledgment:
  35. * "This product includes software developed by the OpenSSL Project
  36. * for use in the OpenSSL Toolkit (http://www.OpenSSL.org/)"
  37. *
  38. * THIS SOFTWARE IS PROVIDED BY THE OpenSSL PROJECT ``AS IS'' AND ANY
  39. * EXPRESSED OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
  40. * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
  41. * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE OpenSSL PROJECT OR
  42. * ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
  43. * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
  44. * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
  45. * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
  46. * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
  47. * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
  48. * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED
  49. * OF THE POSSIBILITY OF SUCH DAMAGE.
  50. * ====================================================================
  51. *
  52. * This product includes cryptographic software written by Eric Young
  53. * (eay@cryptsoft.com). This product includes software written by Tim
  54. * Hudson (tjh@cryptsoft.com).
  55. *
  56. */
  57. #include <string.h>
  58. #include "ec_lcl.h"
  59. #include <openssl/err.h>
  60. #include <openssl/asn1t.h>
  61. #include <openssl/objects.h>
  62. int EC_GROUP_get_basis_type(const EC_GROUP *group)
  63. {
  64. int i = 0;
  65. if (EC_METHOD_get_field_type(EC_GROUP_method_of(group)) !=
  66. NID_X9_62_characteristic_two_field)
  67. /* everything else is currently not supported */
  68. return 0;
  69. while (group->poly[i] != 0)
  70. i++;
  71. if (i == 4)
  72. return NID_X9_62_ppBasis;
  73. else if (i == 2)
  74. return NID_X9_62_tpBasis;
  75. else
  76. /* everything else is currently not supported */
  77. return 0;
  78. }
  79. #ifndef OPENSSL_NO_EC2M
  80. int EC_GROUP_get_trinomial_basis(const EC_GROUP *group, unsigned int *k)
  81. {
  82. if (group == NULL)
  83. return 0;
  84. if (EC_METHOD_get_field_type(EC_GROUP_method_of(group)) !=
  85. NID_X9_62_characteristic_two_field
  86. || !((group->poly[0] != 0) && (group->poly[1] != 0)
  87. && (group->poly[2] == 0))) {
  88. ECerr(EC_F_EC_GROUP_GET_TRINOMIAL_BASIS,
  89. ERR_R_SHOULD_NOT_HAVE_BEEN_CALLED);
  90. return 0;
  91. }
  92. if (k)
  93. *k = group->poly[1];
  94. return 1;
  95. }
  96. int EC_GROUP_get_pentanomial_basis(const EC_GROUP *group, unsigned int *k1,
  97. unsigned int *k2, unsigned int *k3)
  98. {
  99. if (group == NULL)
  100. return 0;
  101. if (EC_METHOD_get_field_type(EC_GROUP_method_of(group)) !=
  102. NID_X9_62_characteristic_two_field
  103. || !((group->poly[0] != 0) && (group->poly[1] != 0)
  104. && (group->poly[2] != 0) && (group->poly[3] != 0)
  105. && (group->poly[4] == 0))) {
  106. ECerr(EC_F_EC_GROUP_GET_PENTANOMIAL_BASIS,
  107. ERR_R_SHOULD_NOT_HAVE_BEEN_CALLED);
  108. return 0;
  109. }
  110. if (k1)
  111. *k1 = group->poly[3];
  112. if (k2)
  113. *k2 = group->poly[2];
  114. if (k3)
  115. *k3 = group->poly[1];
  116. return 1;
  117. }
  118. #endif
  119. /* some structures needed for the asn1 encoding */
  120. typedef struct x9_62_pentanomial_st {
  121. long k1;
  122. long k2;
  123. long k3;
  124. } X9_62_PENTANOMIAL;
  125. typedef struct x9_62_characteristic_two_st {
  126. long m;
  127. ASN1_OBJECT *type;
  128. union {
  129. char *ptr;
  130. /* NID_X9_62_onBasis */
  131. ASN1_NULL *onBasis;
  132. /* NID_X9_62_tpBasis */
  133. ASN1_INTEGER *tpBasis;
  134. /* NID_X9_62_ppBasis */
  135. X9_62_PENTANOMIAL *ppBasis;
  136. /* anything else */
  137. ASN1_TYPE *other;
  138. } p;
  139. } X9_62_CHARACTERISTIC_TWO;
  140. typedef struct x9_62_fieldid_st {
  141. ASN1_OBJECT *fieldType;
  142. union {
  143. char *ptr;
  144. /* NID_X9_62_prime_field */
  145. ASN1_INTEGER *prime;
  146. /* NID_X9_62_characteristic_two_field */
  147. X9_62_CHARACTERISTIC_TWO *char_two;
  148. /* anything else */
  149. ASN1_TYPE *other;
  150. } p;
  151. } X9_62_FIELDID;
  152. typedef struct x9_62_curve_st {
  153. ASN1_OCTET_STRING *a;
  154. ASN1_OCTET_STRING *b;
  155. ASN1_BIT_STRING *seed;
  156. } X9_62_CURVE;
  157. typedef struct ec_parameters_st {
  158. long version;
  159. X9_62_FIELDID *fieldID;
  160. X9_62_CURVE *curve;
  161. ASN1_OCTET_STRING *base;
  162. ASN1_INTEGER *order;
  163. ASN1_INTEGER *cofactor;
  164. } ECPARAMETERS;
  165. struct ecpk_parameters_st {
  166. int type;
  167. union {
  168. ASN1_OBJECT *named_curve;
  169. ECPARAMETERS *parameters;
  170. ASN1_NULL *implicitlyCA;
  171. } value;
  172. } /* ECPKPARAMETERS */ ;
  173. /* SEC1 ECPrivateKey */
  174. typedef struct ec_privatekey_st {
  175. long version;
  176. ASN1_OCTET_STRING *privateKey;
  177. ECPKPARAMETERS *parameters;
  178. ASN1_BIT_STRING *publicKey;
  179. } EC_PRIVATEKEY;
  180. /* the OpenSSL ASN.1 definitions */
  181. ASN1_SEQUENCE(X9_62_PENTANOMIAL) = {
  182. ASN1_SIMPLE(X9_62_PENTANOMIAL, k1, LONG),
  183. ASN1_SIMPLE(X9_62_PENTANOMIAL, k2, LONG),
  184. ASN1_SIMPLE(X9_62_PENTANOMIAL, k3, LONG)
  185. } static_ASN1_SEQUENCE_END(X9_62_PENTANOMIAL)
  186. DECLARE_ASN1_ALLOC_FUNCTIONS(X9_62_PENTANOMIAL)
  187. IMPLEMENT_ASN1_ALLOC_FUNCTIONS(X9_62_PENTANOMIAL)
  188. ASN1_ADB_TEMPLATE(char_two_def) = ASN1_SIMPLE(X9_62_CHARACTERISTIC_TWO, p.other, ASN1_ANY);
  189. ASN1_ADB(X9_62_CHARACTERISTIC_TWO) = {
  190. ADB_ENTRY(NID_X9_62_onBasis, ASN1_SIMPLE(X9_62_CHARACTERISTIC_TWO, p.onBasis, ASN1_NULL)),
  191. ADB_ENTRY(NID_X9_62_tpBasis, ASN1_SIMPLE(X9_62_CHARACTERISTIC_TWO, p.tpBasis, ASN1_INTEGER)),
  192. ADB_ENTRY(NID_X9_62_ppBasis, ASN1_SIMPLE(X9_62_CHARACTERISTIC_TWO, p.ppBasis, X9_62_PENTANOMIAL))
  193. } ASN1_ADB_END(X9_62_CHARACTERISTIC_TWO, 0, type, 0, &char_two_def_tt, NULL);
  194. ASN1_SEQUENCE(X9_62_CHARACTERISTIC_TWO) = {
  195. ASN1_SIMPLE(X9_62_CHARACTERISTIC_TWO, m, LONG),
  196. ASN1_SIMPLE(X9_62_CHARACTERISTIC_TWO, type, ASN1_OBJECT),
  197. ASN1_ADB_OBJECT(X9_62_CHARACTERISTIC_TWO)
  198. } static_ASN1_SEQUENCE_END(X9_62_CHARACTERISTIC_TWO)
  199. DECLARE_ASN1_ALLOC_FUNCTIONS(X9_62_CHARACTERISTIC_TWO)
  200. IMPLEMENT_ASN1_ALLOC_FUNCTIONS(X9_62_CHARACTERISTIC_TWO)
  201. ASN1_ADB_TEMPLATE(fieldID_def) = ASN1_SIMPLE(X9_62_FIELDID, p.other, ASN1_ANY);
  202. ASN1_ADB(X9_62_FIELDID) = {
  203. ADB_ENTRY(NID_X9_62_prime_field, ASN1_SIMPLE(X9_62_FIELDID, p.prime, ASN1_INTEGER)),
  204. ADB_ENTRY(NID_X9_62_characteristic_two_field, ASN1_SIMPLE(X9_62_FIELDID, p.char_two, X9_62_CHARACTERISTIC_TWO))
  205. } ASN1_ADB_END(X9_62_FIELDID, 0, fieldType, 0, &fieldID_def_tt, NULL);
  206. ASN1_SEQUENCE(X9_62_FIELDID) = {
  207. ASN1_SIMPLE(X9_62_FIELDID, fieldType, ASN1_OBJECT),
  208. ASN1_ADB_OBJECT(X9_62_FIELDID)
  209. } static_ASN1_SEQUENCE_END(X9_62_FIELDID)
  210. ASN1_SEQUENCE(X9_62_CURVE) = {
  211. ASN1_SIMPLE(X9_62_CURVE, a, ASN1_OCTET_STRING),
  212. ASN1_SIMPLE(X9_62_CURVE, b, ASN1_OCTET_STRING),
  213. ASN1_OPT(X9_62_CURVE, seed, ASN1_BIT_STRING)
  214. } static_ASN1_SEQUENCE_END(X9_62_CURVE)
  215. ASN1_SEQUENCE(ECPARAMETERS) = {
  216. ASN1_SIMPLE(ECPARAMETERS, version, LONG),
  217. ASN1_SIMPLE(ECPARAMETERS, fieldID, X9_62_FIELDID),
  218. ASN1_SIMPLE(ECPARAMETERS, curve, X9_62_CURVE),
  219. ASN1_SIMPLE(ECPARAMETERS, base, ASN1_OCTET_STRING),
  220. ASN1_SIMPLE(ECPARAMETERS, order, ASN1_INTEGER),
  221. ASN1_OPT(ECPARAMETERS, cofactor, ASN1_INTEGER)
  222. } static_ASN1_SEQUENCE_END(ECPARAMETERS)
  223. DECLARE_ASN1_ALLOC_FUNCTIONS(ECPARAMETERS)
  224. IMPLEMENT_ASN1_ALLOC_FUNCTIONS(ECPARAMETERS)
  225. ASN1_CHOICE(ECPKPARAMETERS) = {
  226. ASN1_SIMPLE(ECPKPARAMETERS, value.named_curve, ASN1_OBJECT),
  227. ASN1_SIMPLE(ECPKPARAMETERS, value.parameters, ECPARAMETERS),
  228. ASN1_SIMPLE(ECPKPARAMETERS, value.implicitlyCA, ASN1_NULL)
  229. } static_ASN1_CHOICE_END(ECPKPARAMETERS)
  230. DECLARE_ASN1_FUNCTIONS_const(ECPKPARAMETERS)
  231. DECLARE_ASN1_ENCODE_FUNCTIONS_const(ECPKPARAMETERS, ECPKPARAMETERS)
  232. IMPLEMENT_ASN1_FUNCTIONS_const(ECPKPARAMETERS)
  233. ASN1_SEQUENCE(EC_PRIVATEKEY) = {
  234. ASN1_SIMPLE(EC_PRIVATEKEY, version, LONG),
  235. ASN1_SIMPLE(EC_PRIVATEKEY, privateKey, ASN1_OCTET_STRING),
  236. ASN1_EXP_OPT(EC_PRIVATEKEY, parameters, ECPKPARAMETERS, 0),
  237. ASN1_EXP_OPT(EC_PRIVATEKEY, publicKey, ASN1_BIT_STRING, 1)
  238. } static_ASN1_SEQUENCE_END(EC_PRIVATEKEY)
  239. DECLARE_ASN1_FUNCTIONS_const(EC_PRIVATEKEY)
  240. DECLARE_ASN1_ENCODE_FUNCTIONS_const(EC_PRIVATEKEY, EC_PRIVATEKEY)
  241. IMPLEMENT_ASN1_FUNCTIONS_const(EC_PRIVATEKEY)
  242. /* some declarations of internal function */
  243. /* ec_asn1_group2field() sets the values in a X9_62_FIELDID object */
  244. static int ec_asn1_group2fieldid(const EC_GROUP *, X9_62_FIELDID *);
  245. /* ec_asn1_group2curve() sets the values in a X9_62_CURVE object */
  246. static int ec_asn1_group2curve(const EC_GROUP *, X9_62_CURVE *);
  247. /*
  248. * ec_asn1_parameters2group() creates a EC_GROUP object from a ECPARAMETERS
  249. * object
  250. */
  251. static EC_GROUP *ec_asn1_parameters2group(const ECPARAMETERS *);
  252. /*
  253. * ec_asn1_group2parameters() creates a ECPARAMETERS object from a EC_GROUP
  254. * object
  255. */
  256. static ECPARAMETERS *ec_asn1_group2parameters(const EC_GROUP *,
  257. ECPARAMETERS *);
  258. /*
  259. * ec_asn1_pkparameters2group() creates a EC_GROUP object from a
  260. * ECPKPARAMETERS object
  261. */
  262. static EC_GROUP *ec_asn1_pkparameters2group(const ECPKPARAMETERS *);
  263. /*
  264. * ec_asn1_group2pkparameters() creates a ECPKPARAMETERS object from a
  265. * EC_GROUP object
  266. */
  267. static ECPKPARAMETERS *ec_asn1_group2pkparameters(const EC_GROUP *,
  268. ECPKPARAMETERS *);
  269. /* the function definitions */
  270. static int ec_asn1_group2fieldid(const EC_GROUP *group, X9_62_FIELDID *field)
  271. {
  272. int ok = 0, nid;
  273. BIGNUM *tmp = NULL;
  274. if (group == NULL || field == NULL)
  275. return 0;
  276. /* clear the old values (if necessary) */
  277. ASN1_OBJECT_free(field->fieldType);
  278. ASN1_TYPE_free(field->p.other);
  279. nid = EC_METHOD_get_field_type(EC_GROUP_method_of(group));
  280. /* set OID for the field */
  281. if ((field->fieldType = OBJ_nid2obj(nid)) == NULL) {
  282. ECerr(EC_F_EC_ASN1_GROUP2FIELDID, ERR_R_OBJ_LIB);
  283. goto err;
  284. }
  285. if (nid == NID_X9_62_prime_field) {
  286. if ((tmp = BN_new()) == NULL) {
  287. ECerr(EC_F_EC_ASN1_GROUP2FIELDID, ERR_R_MALLOC_FAILURE);
  288. goto err;
  289. }
  290. /* the parameters are specified by the prime number p */
  291. if (!EC_GROUP_get_curve_GFp(group, tmp, NULL, NULL, NULL)) {
  292. ECerr(EC_F_EC_ASN1_GROUP2FIELDID, ERR_R_EC_LIB);
  293. goto err;
  294. }
  295. /* set the prime number */
  296. field->p.prime = BN_to_ASN1_INTEGER(tmp, NULL);
  297. if (field->p.prime == NULL) {
  298. ECerr(EC_F_EC_ASN1_GROUP2FIELDID, ERR_R_ASN1_LIB);
  299. goto err;
  300. }
  301. } else /* nid == NID_X9_62_characteristic_two_field */
  302. #ifdef OPENSSL_NO_EC2M
  303. {
  304. ECerr(EC_F_EC_ASN1_GROUP2FIELDID, EC_R_GF2M_NOT_SUPPORTED);
  305. goto err;
  306. }
  307. #else
  308. {
  309. int field_type;
  310. X9_62_CHARACTERISTIC_TWO *char_two;
  311. field->p.char_two = X9_62_CHARACTERISTIC_TWO_new();
  312. char_two = field->p.char_two;
  313. if (char_two == NULL) {
  314. ECerr(EC_F_EC_ASN1_GROUP2FIELDID, ERR_R_MALLOC_FAILURE);
  315. goto err;
  316. }
  317. char_two->m = (long)EC_GROUP_get_degree(group);
  318. field_type = EC_GROUP_get_basis_type(group);
  319. if (field_type == 0) {
  320. ECerr(EC_F_EC_ASN1_GROUP2FIELDID, ERR_R_EC_LIB);
  321. goto err;
  322. }
  323. /* set base type OID */
  324. if ((char_two->type = OBJ_nid2obj(field_type)) == NULL) {
  325. ECerr(EC_F_EC_ASN1_GROUP2FIELDID, ERR_R_OBJ_LIB);
  326. goto err;
  327. }
  328. if (field_type == NID_X9_62_tpBasis) {
  329. unsigned int k;
  330. if (!EC_GROUP_get_trinomial_basis(group, &k))
  331. goto err;
  332. char_two->p.tpBasis = ASN1_INTEGER_new();
  333. if (char_two->p.tpBasis == NULL) {
  334. ECerr(EC_F_EC_ASN1_GROUP2FIELDID, ERR_R_MALLOC_FAILURE);
  335. goto err;
  336. }
  337. if (!ASN1_INTEGER_set(char_two->p.tpBasis, (long)k)) {
  338. ECerr(EC_F_EC_ASN1_GROUP2FIELDID, ERR_R_ASN1_LIB);
  339. goto err;
  340. }
  341. } else if (field_type == NID_X9_62_ppBasis) {
  342. unsigned int k1, k2, k3;
  343. if (!EC_GROUP_get_pentanomial_basis(group, &k1, &k2, &k3))
  344. goto err;
  345. char_two->p.ppBasis = X9_62_PENTANOMIAL_new();
  346. if (char_two->p.ppBasis == NULL) {
  347. ECerr(EC_F_EC_ASN1_GROUP2FIELDID, ERR_R_MALLOC_FAILURE);
  348. goto err;
  349. }
  350. /* set k? values */
  351. char_two->p.ppBasis->k1 = (long)k1;
  352. char_two->p.ppBasis->k2 = (long)k2;
  353. char_two->p.ppBasis->k3 = (long)k3;
  354. } else { /* field_type == NID_X9_62_onBasis */
  355. /* for ONB the parameters are (asn1) NULL */
  356. char_two->p.onBasis = ASN1_NULL_new();
  357. if (char_two->p.onBasis == NULL) {
  358. ECerr(EC_F_EC_ASN1_GROUP2FIELDID, ERR_R_MALLOC_FAILURE);
  359. goto err;
  360. }
  361. }
  362. }
  363. #endif
  364. ok = 1;
  365. err:
  366. BN_free(tmp);
  367. return (ok);
  368. }
  369. static int ec_asn1_group2curve(const EC_GROUP *group, X9_62_CURVE *curve)
  370. {
  371. int ok = 0, nid;
  372. BIGNUM *tmp_1 = NULL, *tmp_2 = NULL;
  373. unsigned char *buffer_1 = NULL, *buffer_2 = NULL,
  374. *a_buf = NULL, *b_buf = NULL;
  375. size_t len_1, len_2;
  376. unsigned char char_zero = 0;
  377. if (!group || !curve || !curve->a || !curve->b)
  378. return 0;
  379. if ((tmp_1 = BN_new()) == NULL || (tmp_2 = BN_new()) == NULL) {
  380. ECerr(EC_F_EC_ASN1_GROUP2CURVE, ERR_R_MALLOC_FAILURE);
  381. goto err;
  382. }
  383. nid = EC_METHOD_get_field_type(EC_GROUP_method_of(group));
  384. /* get a and b */
  385. if (nid == NID_X9_62_prime_field) {
  386. if (!EC_GROUP_get_curve_GFp(group, NULL, tmp_1, tmp_2, NULL)) {
  387. ECerr(EC_F_EC_ASN1_GROUP2CURVE, ERR_R_EC_LIB);
  388. goto err;
  389. }
  390. }
  391. #ifndef OPENSSL_NO_EC2M
  392. else { /* nid == NID_X9_62_characteristic_two_field */
  393. if (!EC_GROUP_get_curve_GF2m(group, NULL, tmp_1, tmp_2, NULL)) {
  394. ECerr(EC_F_EC_ASN1_GROUP2CURVE, ERR_R_EC_LIB);
  395. goto err;
  396. }
  397. }
  398. #endif
  399. len_1 = (size_t)BN_num_bytes(tmp_1);
  400. len_2 = (size_t)BN_num_bytes(tmp_2);
  401. if (len_1 == 0) {
  402. /* len_1 == 0 => a == 0 */
  403. a_buf = &char_zero;
  404. len_1 = 1;
  405. } else {
  406. if ((buffer_1 = OPENSSL_malloc(len_1)) == NULL) {
  407. ECerr(EC_F_EC_ASN1_GROUP2CURVE, ERR_R_MALLOC_FAILURE);
  408. goto err;
  409. }
  410. if ((len_1 = BN_bn2bin(tmp_1, buffer_1)) == 0) {
  411. ECerr(EC_F_EC_ASN1_GROUP2CURVE, ERR_R_BN_LIB);
  412. goto err;
  413. }
  414. a_buf = buffer_1;
  415. }
  416. if (len_2 == 0) {
  417. /* len_2 == 0 => b == 0 */
  418. b_buf = &char_zero;
  419. len_2 = 1;
  420. } else {
  421. if ((buffer_2 = OPENSSL_malloc(len_2)) == NULL) {
  422. ECerr(EC_F_EC_ASN1_GROUP2CURVE, ERR_R_MALLOC_FAILURE);
  423. goto err;
  424. }
  425. if ((len_2 = BN_bn2bin(tmp_2, buffer_2)) == 0) {
  426. ECerr(EC_F_EC_ASN1_GROUP2CURVE, ERR_R_BN_LIB);
  427. goto err;
  428. }
  429. b_buf = buffer_2;
  430. }
  431. /* set a and b */
  432. if (!ASN1_OCTET_STRING_set(curve->a, a_buf, len_1) ||
  433. !ASN1_OCTET_STRING_set(curve->b, b_buf, len_2)) {
  434. ECerr(EC_F_EC_ASN1_GROUP2CURVE, ERR_R_ASN1_LIB);
  435. goto err;
  436. }
  437. /* set the seed (optional) */
  438. if (group->seed) {
  439. if (!curve->seed)
  440. if ((curve->seed = ASN1_BIT_STRING_new()) == NULL) {
  441. ECerr(EC_F_EC_ASN1_GROUP2CURVE, ERR_R_MALLOC_FAILURE);
  442. goto err;
  443. }
  444. curve->seed->flags &= ~(ASN1_STRING_FLAG_BITS_LEFT | 0x07);
  445. curve->seed->flags |= ASN1_STRING_FLAG_BITS_LEFT;
  446. if (!ASN1_BIT_STRING_set(curve->seed, group->seed,
  447. (int)group->seed_len)) {
  448. ECerr(EC_F_EC_ASN1_GROUP2CURVE, ERR_R_ASN1_LIB);
  449. goto err;
  450. }
  451. } else {
  452. ASN1_BIT_STRING_free(curve->seed);
  453. curve->seed = NULL;
  454. }
  455. ok = 1;
  456. err:
  457. OPENSSL_free(buffer_1);
  458. OPENSSL_free(buffer_2);
  459. BN_free(tmp_1);
  460. BN_free(tmp_2);
  461. return (ok);
  462. }
  463. static ECPARAMETERS *ec_asn1_group2parameters(const EC_GROUP *group,
  464. ECPARAMETERS *param)
  465. {
  466. size_t len = 0;
  467. ECPARAMETERS *ret = NULL;
  468. BIGNUM *tmp = NULL;
  469. unsigned char *buffer = NULL;
  470. const EC_POINT *point = NULL;
  471. point_conversion_form_t form;
  472. if ((tmp = BN_new()) == NULL) {
  473. ECerr(EC_F_EC_ASN1_GROUP2PARAMETERS, ERR_R_MALLOC_FAILURE);
  474. goto err;
  475. }
  476. if (param == NULL) {
  477. if ((ret = ECPARAMETERS_new()) == NULL) {
  478. ECerr(EC_F_EC_ASN1_GROUP2PARAMETERS, ERR_R_MALLOC_FAILURE);
  479. goto err;
  480. }
  481. } else
  482. ret = param;
  483. /* set the version (always one) */
  484. ret->version = (long)0x1;
  485. /* set the fieldID */
  486. if (!ec_asn1_group2fieldid(group, ret->fieldID)) {
  487. ECerr(EC_F_EC_ASN1_GROUP2PARAMETERS, ERR_R_EC_LIB);
  488. goto err;
  489. }
  490. /* set the curve */
  491. if (!ec_asn1_group2curve(group, ret->curve)) {
  492. ECerr(EC_F_EC_ASN1_GROUP2PARAMETERS, ERR_R_EC_LIB);
  493. goto err;
  494. }
  495. /* set the base point */
  496. if ((point = EC_GROUP_get0_generator(group)) == NULL) {
  497. ECerr(EC_F_EC_ASN1_GROUP2PARAMETERS, EC_R_UNDEFINED_GENERATOR);
  498. goto err;
  499. }
  500. form = EC_GROUP_get_point_conversion_form(group);
  501. len = EC_POINT_point2buf(group, point, form, &buffer, NULL);
  502. if (len == 0) {
  503. ECerr(EC_F_EC_ASN1_GROUP2PARAMETERS, ERR_R_EC_LIB);
  504. goto err;
  505. }
  506. if (ret->base == NULL && (ret->base = ASN1_OCTET_STRING_new()) == NULL) {
  507. ECerr(EC_F_EC_ASN1_GROUP2PARAMETERS, ERR_R_MALLOC_FAILURE);
  508. goto err;
  509. }
  510. if (!ASN1_OCTET_STRING_set(ret->base, buffer, len)) {
  511. ECerr(EC_F_EC_ASN1_GROUP2PARAMETERS, ERR_R_ASN1_LIB);
  512. goto err;
  513. }
  514. /* set the order */
  515. if (!EC_GROUP_get_order(group, tmp, NULL)) {
  516. ECerr(EC_F_EC_ASN1_GROUP2PARAMETERS, ERR_R_EC_LIB);
  517. goto err;
  518. }
  519. ret->order = BN_to_ASN1_INTEGER(tmp, ret->order);
  520. if (ret->order == NULL) {
  521. ECerr(EC_F_EC_ASN1_GROUP2PARAMETERS, ERR_R_ASN1_LIB);
  522. goto err;
  523. }
  524. /* set the cofactor (optional) */
  525. if (EC_GROUP_get_cofactor(group, tmp, NULL)) {
  526. ret->cofactor = BN_to_ASN1_INTEGER(tmp, ret->cofactor);
  527. if (ret->cofactor == NULL) {
  528. ECerr(EC_F_EC_ASN1_GROUP2PARAMETERS, ERR_R_ASN1_LIB);
  529. goto err;
  530. }
  531. }
  532. return ret;
  533. err:
  534. if (!param)
  535. ECPARAMETERS_free(ret);
  536. BN_free(tmp);
  537. OPENSSL_free(buffer);
  538. return NULL;
  539. }
  540. ECPKPARAMETERS *ec_asn1_group2pkparameters(const EC_GROUP *group,
  541. ECPKPARAMETERS *params)
  542. {
  543. int ok = 1, tmp;
  544. ECPKPARAMETERS *ret = params;
  545. if (ret == NULL) {
  546. if ((ret = ECPKPARAMETERS_new()) == NULL) {
  547. ECerr(EC_F_EC_ASN1_GROUP2PKPARAMETERS, ERR_R_MALLOC_FAILURE);
  548. return NULL;
  549. }
  550. } else {
  551. if (ret->type == 0)
  552. ASN1_OBJECT_free(ret->value.named_curve);
  553. else if (ret->type == 1 && ret->value.parameters)
  554. ECPARAMETERS_free(ret->value.parameters);
  555. }
  556. if (EC_GROUP_get_asn1_flag(group)) {
  557. /*
  558. * use the asn1 OID to describe the the elliptic curve parameters
  559. */
  560. tmp = EC_GROUP_get_curve_name(group);
  561. if (tmp) {
  562. ret->type = 0;
  563. if ((ret->value.named_curve = OBJ_nid2obj(tmp)) == NULL)
  564. ok = 0;
  565. } else
  566. /* we don't kmow the nid => ERROR */
  567. ok = 0;
  568. } else {
  569. /* use the ECPARAMETERS structure */
  570. ret->type = 1;
  571. if ((ret->value.parameters =
  572. ec_asn1_group2parameters(group, NULL)) == NULL)
  573. ok = 0;
  574. }
  575. if (!ok) {
  576. ECPKPARAMETERS_free(ret);
  577. return NULL;
  578. }
  579. return ret;
  580. }
  581. static EC_GROUP *ec_asn1_parameters2group(const ECPARAMETERS *params)
  582. {
  583. int ok = 0, tmp;
  584. EC_GROUP *ret = NULL;
  585. BIGNUM *p = NULL, *a = NULL, *b = NULL;
  586. EC_POINT *point = NULL;
  587. long field_bits;
  588. if (!params->fieldID || !params->fieldID->fieldType ||
  589. !params->fieldID->p.ptr) {
  590. ECerr(EC_F_EC_ASN1_PARAMETERS2GROUP, EC_R_ASN1_ERROR);
  591. goto err;
  592. }
  593. /* now extract the curve parameters a and b */
  594. if (!params->curve || !params->curve->a ||
  595. !params->curve->a->data || !params->curve->b ||
  596. !params->curve->b->data) {
  597. ECerr(EC_F_EC_ASN1_PARAMETERS2GROUP, EC_R_ASN1_ERROR);
  598. goto err;
  599. }
  600. a = BN_bin2bn(params->curve->a->data, params->curve->a->length, NULL);
  601. if (a == NULL) {
  602. ECerr(EC_F_EC_ASN1_PARAMETERS2GROUP, ERR_R_BN_LIB);
  603. goto err;
  604. }
  605. b = BN_bin2bn(params->curve->b->data, params->curve->b->length, NULL);
  606. if (b == NULL) {
  607. ECerr(EC_F_EC_ASN1_PARAMETERS2GROUP, ERR_R_BN_LIB);
  608. goto err;
  609. }
  610. /* get the field parameters */
  611. tmp = OBJ_obj2nid(params->fieldID->fieldType);
  612. if (tmp == NID_X9_62_characteristic_two_field)
  613. #ifdef OPENSSL_NO_EC2M
  614. {
  615. ECerr(EC_F_EC_ASN1_PARAMETERS2GROUP, EC_R_GF2M_NOT_SUPPORTED);
  616. goto err;
  617. }
  618. #else
  619. {
  620. X9_62_CHARACTERISTIC_TWO *char_two;
  621. char_two = params->fieldID->p.char_two;
  622. field_bits = char_two->m;
  623. if (field_bits > OPENSSL_ECC_MAX_FIELD_BITS) {
  624. ECerr(EC_F_EC_ASN1_PARAMETERS2GROUP, EC_R_FIELD_TOO_LARGE);
  625. goto err;
  626. }
  627. if ((p = BN_new()) == NULL) {
  628. ECerr(EC_F_EC_ASN1_PARAMETERS2GROUP, ERR_R_MALLOC_FAILURE);
  629. goto err;
  630. }
  631. /* get the base type */
  632. tmp = OBJ_obj2nid(char_two->type);
  633. if (tmp == NID_X9_62_tpBasis) {
  634. long tmp_long;
  635. if (!char_two->p.tpBasis) {
  636. ECerr(EC_F_EC_ASN1_PARAMETERS2GROUP, EC_R_ASN1_ERROR);
  637. goto err;
  638. }
  639. tmp_long = ASN1_INTEGER_get(char_two->p.tpBasis);
  640. if (!(char_two->m > tmp_long && tmp_long > 0)) {
  641. ECerr(EC_F_EC_ASN1_PARAMETERS2GROUP,
  642. EC_R_INVALID_TRINOMIAL_BASIS);
  643. goto err;
  644. }
  645. /* create the polynomial */
  646. if (!BN_set_bit(p, (int)char_two->m))
  647. goto err;
  648. if (!BN_set_bit(p, (int)tmp_long))
  649. goto err;
  650. if (!BN_set_bit(p, 0))
  651. goto err;
  652. } else if (tmp == NID_X9_62_ppBasis) {
  653. X9_62_PENTANOMIAL *penta;
  654. penta = char_two->p.ppBasis;
  655. if (!penta) {
  656. ECerr(EC_F_EC_ASN1_PARAMETERS2GROUP, EC_R_ASN1_ERROR);
  657. goto err;
  658. }
  659. if (!
  660. (char_two->m > penta->k3 && penta->k3 > penta->k2
  661. && penta->k2 > penta->k1 && penta->k1 > 0)) {
  662. ECerr(EC_F_EC_ASN1_PARAMETERS2GROUP,
  663. EC_R_INVALID_PENTANOMIAL_BASIS);
  664. goto err;
  665. }
  666. /* create the polynomial */
  667. if (!BN_set_bit(p, (int)char_two->m))
  668. goto err;
  669. if (!BN_set_bit(p, (int)penta->k1))
  670. goto err;
  671. if (!BN_set_bit(p, (int)penta->k2))
  672. goto err;
  673. if (!BN_set_bit(p, (int)penta->k3))
  674. goto err;
  675. if (!BN_set_bit(p, 0))
  676. goto err;
  677. } else if (tmp == NID_X9_62_onBasis) {
  678. ECerr(EC_F_EC_ASN1_PARAMETERS2GROUP, EC_R_NOT_IMPLEMENTED);
  679. goto err;
  680. } else { /* error */
  681. ECerr(EC_F_EC_ASN1_PARAMETERS2GROUP, EC_R_ASN1_ERROR);
  682. goto err;
  683. }
  684. /* create the EC_GROUP structure */
  685. ret = EC_GROUP_new_curve_GF2m(p, a, b, NULL);
  686. }
  687. #endif
  688. else if (tmp == NID_X9_62_prime_field) {
  689. /* we have a curve over a prime field */
  690. /* extract the prime number */
  691. if (!params->fieldID->p.prime) {
  692. ECerr(EC_F_EC_ASN1_PARAMETERS2GROUP, EC_R_ASN1_ERROR);
  693. goto err;
  694. }
  695. p = ASN1_INTEGER_to_BN(params->fieldID->p.prime, NULL);
  696. if (p == NULL) {
  697. ECerr(EC_F_EC_ASN1_PARAMETERS2GROUP, ERR_R_ASN1_LIB);
  698. goto err;
  699. }
  700. if (BN_is_negative(p) || BN_is_zero(p)) {
  701. ECerr(EC_F_EC_ASN1_PARAMETERS2GROUP, EC_R_INVALID_FIELD);
  702. goto err;
  703. }
  704. field_bits = BN_num_bits(p);
  705. if (field_bits > OPENSSL_ECC_MAX_FIELD_BITS) {
  706. ECerr(EC_F_EC_ASN1_PARAMETERS2GROUP, EC_R_FIELD_TOO_LARGE);
  707. goto err;
  708. }
  709. /* create the EC_GROUP structure */
  710. ret = EC_GROUP_new_curve_GFp(p, a, b, NULL);
  711. } else {
  712. ECerr(EC_F_EC_ASN1_PARAMETERS2GROUP, EC_R_INVALID_FIELD);
  713. goto err;
  714. }
  715. if (ret == NULL) {
  716. ECerr(EC_F_EC_ASN1_PARAMETERS2GROUP, ERR_R_EC_LIB);
  717. goto err;
  718. }
  719. /* extract seed (optional) */
  720. if (params->curve->seed != NULL) {
  721. OPENSSL_free(ret->seed);
  722. if ((ret->seed = OPENSSL_malloc(params->curve->seed->length)) == NULL) {
  723. ECerr(EC_F_EC_ASN1_PARAMETERS2GROUP, ERR_R_MALLOC_FAILURE);
  724. goto err;
  725. }
  726. memcpy(ret->seed, params->curve->seed->data,
  727. params->curve->seed->length);
  728. ret->seed_len = params->curve->seed->length;
  729. }
  730. if (!params->order || !params->base || !params->base->data) {
  731. ECerr(EC_F_EC_ASN1_PARAMETERS2GROUP, EC_R_ASN1_ERROR);
  732. goto err;
  733. }
  734. if ((point = EC_POINT_new(ret)) == NULL)
  735. goto err;
  736. /* set the point conversion form */
  737. EC_GROUP_set_point_conversion_form(ret, (point_conversion_form_t)
  738. (params->base->data[0] & ~0x01));
  739. /* extract the ec point */
  740. if (!EC_POINT_oct2point(ret, point, params->base->data,
  741. params->base->length, NULL)) {
  742. ECerr(EC_F_EC_ASN1_PARAMETERS2GROUP, ERR_R_EC_LIB);
  743. goto err;
  744. }
  745. /* extract the order */
  746. if ((a = ASN1_INTEGER_to_BN(params->order, a)) == NULL) {
  747. ECerr(EC_F_EC_ASN1_PARAMETERS2GROUP, ERR_R_ASN1_LIB);
  748. goto err;
  749. }
  750. if (BN_is_negative(a) || BN_is_zero(a)) {
  751. ECerr(EC_F_EC_ASN1_PARAMETERS2GROUP, EC_R_INVALID_GROUP_ORDER);
  752. goto err;
  753. }
  754. if (BN_num_bits(a) > (int)field_bits + 1) { /* Hasse bound */
  755. ECerr(EC_F_EC_ASN1_PARAMETERS2GROUP, EC_R_INVALID_GROUP_ORDER);
  756. goto err;
  757. }
  758. /* extract the cofactor (optional) */
  759. if (params->cofactor == NULL) {
  760. BN_free(b);
  761. b = NULL;
  762. } else if ((b = ASN1_INTEGER_to_BN(params->cofactor, b)) == NULL) {
  763. ECerr(EC_F_EC_ASN1_PARAMETERS2GROUP, ERR_R_ASN1_LIB);
  764. goto err;
  765. }
  766. /* set the generator, order and cofactor (if present) */
  767. if (!EC_GROUP_set_generator(ret, point, a, b)) {
  768. ECerr(EC_F_EC_ASN1_PARAMETERS2GROUP, ERR_R_EC_LIB);
  769. goto err;
  770. }
  771. ok = 1;
  772. err:
  773. if (!ok) {
  774. EC_GROUP_clear_free(ret);
  775. ret = NULL;
  776. }
  777. BN_free(p);
  778. BN_free(a);
  779. BN_free(b);
  780. EC_POINT_free(point);
  781. return (ret);
  782. }
  783. EC_GROUP *ec_asn1_pkparameters2group(const ECPKPARAMETERS *params)
  784. {
  785. EC_GROUP *ret = NULL;
  786. int tmp = 0;
  787. if (params == NULL) {
  788. ECerr(EC_F_EC_ASN1_PKPARAMETERS2GROUP, EC_R_MISSING_PARAMETERS);
  789. return NULL;
  790. }
  791. if (params->type == 0) { /* the curve is given by an OID */
  792. tmp = OBJ_obj2nid(params->value.named_curve);
  793. if ((ret = EC_GROUP_new_by_curve_name(tmp)) == NULL) {
  794. ECerr(EC_F_EC_ASN1_PKPARAMETERS2GROUP,
  795. EC_R_EC_GROUP_NEW_BY_NAME_FAILURE);
  796. return NULL;
  797. }
  798. EC_GROUP_set_asn1_flag(ret, OPENSSL_EC_NAMED_CURVE);
  799. } else if (params->type == 1) { /* the parameters are given by a
  800. * ECPARAMETERS structure */
  801. ret = ec_asn1_parameters2group(params->value.parameters);
  802. if (!ret) {
  803. ECerr(EC_F_EC_ASN1_PKPARAMETERS2GROUP, ERR_R_EC_LIB);
  804. return NULL;
  805. }
  806. EC_GROUP_set_asn1_flag(ret, 0x0);
  807. } else if (params->type == 2) { /* implicitlyCA */
  808. return NULL;
  809. } else {
  810. ECerr(EC_F_EC_ASN1_PKPARAMETERS2GROUP, EC_R_ASN1_ERROR);
  811. return NULL;
  812. }
  813. return ret;
  814. }
  815. /* EC_GROUP <-> DER encoding of ECPKPARAMETERS */
  816. EC_GROUP *d2i_ECPKParameters(EC_GROUP **a, const unsigned char **in, long len)
  817. {
  818. EC_GROUP *group = NULL;
  819. ECPKPARAMETERS *params = NULL;
  820. const unsigned char *p = *in;
  821. if ((params = d2i_ECPKPARAMETERS(NULL, &p, len)) == NULL) {
  822. ECerr(EC_F_D2I_ECPKPARAMETERS, EC_R_D2I_ECPKPARAMETERS_FAILURE);
  823. ECPKPARAMETERS_free(params);
  824. return NULL;
  825. }
  826. if ((group = ec_asn1_pkparameters2group(params)) == NULL) {
  827. ECerr(EC_F_D2I_ECPKPARAMETERS, EC_R_PKPARAMETERS2GROUP_FAILURE);
  828. ECPKPARAMETERS_free(params);
  829. return NULL;
  830. }
  831. if (a) {
  832. EC_GROUP_clear_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_asn1_group2pkparameters(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_clear_free(ret->group);
  874. ret->group = ec_asn1_pkparameters2group(priv_key->parameters);
  875. }
  876. if (ret->group == NULL) {
  877. ECerr(EC_F_D2I_ECPRIVATEKEY, ERR_R_EC_LIB);
  878. goto err;
  879. }
  880. ret->version = priv_key->version;
  881. if (priv_key->privateKey) {
  882. if (ret->priv_key == NULL)
  883. ret->priv_key = BN_secure_new();
  884. if (ret->priv_key == NULL) {
  885. ECerr(EC_F_D2I_ECPRIVATEKEY, ERR_R_MALLOC_FAILURE);
  886. goto err;
  887. }
  888. ret->priv_key = BN_bin2bn(ASN1_STRING_data(priv_key->privateKey),
  889. ASN1_STRING_length(priv_key->privateKey),
  890. ret->priv_key);
  891. if (ret->priv_key == NULL) {
  892. ECerr(EC_F_D2I_ECPRIVATEKEY, ERR_R_BN_LIB);
  893. goto err;
  894. }
  895. } else {
  896. ECerr(EC_F_D2I_ECPRIVATEKEY, EC_R_MISSING_PRIVATE_KEY);
  897. goto err;
  898. }
  899. EC_POINT_clear_free(ret->pub_key);
  900. ret->pub_key = EC_POINT_new(ret->group);
  901. if (ret->pub_key == NULL) {
  902. ECerr(EC_F_D2I_ECPRIVATEKEY, ERR_R_EC_LIB);
  903. goto err;
  904. }
  905. if (priv_key->publicKey) {
  906. const unsigned char *pub_oct;
  907. int pub_oct_len;
  908. pub_oct = ASN1_STRING_data(priv_key->publicKey);
  909. pub_oct_len = ASN1_STRING_length(priv_key->publicKey);
  910. /*
  911. * The first byte - point conversion form - must be present.
  912. */
  913. if (pub_oct_len <= 0) {
  914. ECerr(EC_F_D2I_ECPRIVATEKEY, EC_R_BUFFER_TOO_SMALL);
  915. goto err;
  916. }
  917. /* Save the point conversion form. */
  918. ret->conv_form = (point_conversion_form_t) (pub_oct[0] & ~0x01);
  919. if (!EC_POINT_oct2point(ret->group, ret->pub_key,
  920. pub_oct, (size_t)(pub_oct_len), NULL)) {
  921. ECerr(EC_F_D2I_ECPRIVATEKEY, ERR_R_EC_LIB);
  922. goto err;
  923. }
  924. } else {
  925. if (!EC_POINT_mul
  926. (ret->group, ret->pub_key, ret->priv_key, NULL, NULL, NULL)) {
  927. ECerr(EC_F_D2I_ECPRIVATEKEY, ERR_R_EC_LIB);
  928. goto err;
  929. }
  930. /* Remember the original private-key-only encoding. */
  931. ret->enc_flag |= EC_PKEY_NO_PUBKEY;
  932. }
  933. if (a)
  934. *a = ret;
  935. EC_PRIVATEKEY_free(priv_key);
  936. *in = p;
  937. return (ret);
  938. err:
  939. if (a == NULL || *a != ret)
  940. EC_KEY_free(ret);
  941. EC_PRIVATEKEY_free(priv_key);
  942. return NULL;
  943. }
  944. int i2d_ECPrivateKey(EC_KEY *a, unsigned char **out)
  945. {
  946. int ret = 0, ok = 0;
  947. unsigned char *buffer = NULL;
  948. size_t buf_len = 0, tmp_len, bn_len;
  949. EC_PRIVATEKEY *priv_key = NULL;
  950. if (a == NULL || a->group == NULL || a->priv_key == NULL ||
  951. (!(a->enc_flag & EC_PKEY_NO_PUBKEY) && a->pub_key == NULL)) {
  952. ECerr(EC_F_I2D_ECPRIVATEKEY, ERR_R_PASSED_NULL_PARAMETER);
  953. goto err;
  954. }
  955. if ((priv_key = EC_PRIVATEKEY_new()) == NULL) {
  956. ECerr(EC_F_I2D_ECPRIVATEKEY, ERR_R_MALLOC_FAILURE);
  957. goto err;
  958. }
  959. priv_key->version = a->version;
  960. bn_len = (size_t)BN_num_bytes(a->priv_key);
  961. /* Octetstring may need leading zeros if BN is to short */
  962. buf_len = (EC_GROUP_get_degree(a->group) + 7) / 8;
  963. if (bn_len > buf_len) {
  964. ECerr(EC_F_I2D_ECPRIVATEKEY, EC_R_BUFFER_TOO_SMALL);
  965. goto err;
  966. }
  967. buffer = OPENSSL_malloc(buf_len);
  968. if (buffer == NULL) {
  969. ECerr(EC_F_I2D_ECPRIVATEKEY, ERR_R_MALLOC_FAILURE);
  970. goto err;
  971. }
  972. if (!BN_bn2bin(a->priv_key, buffer + buf_len - bn_len)) {
  973. ECerr(EC_F_I2D_ECPRIVATEKEY, ERR_R_BN_LIB);
  974. goto err;
  975. }
  976. if (buf_len - bn_len > 0) {
  977. memset(buffer, 0, buf_len - bn_len);
  978. }
  979. if (!ASN1_OCTET_STRING_set(priv_key->privateKey, buffer, buf_len)) {
  980. ECerr(EC_F_I2D_ECPRIVATEKEY, ERR_R_ASN1_LIB);
  981. goto err;
  982. }
  983. if (!(a->enc_flag & EC_PKEY_NO_PARAMETERS)) {
  984. if ((priv_key->parameters =
  985. ec_asn1_group2pkparameters(a->group,
  986. priv_key->parameters)) == NULL) {
  987. ECerr(EC_F_I2D_ECPRIVATEKEY, ERR_R_EC_LIB);
  988. goto err;
  989. }
  990. }
  991. if (!(a->enc_flag & EC_PKEY_NO_PUBKEY)) {
  992. priv_key->publicKey = ASN1_BIT_STRING_new();
  993. if (priv_key->publicKey == NULL) {
  994. ECerr(EC_F_I2D_ECPRIVATEKEY, ERR_R_MALLOC_FAILURE);
  995. goto err;
  996. }
  997. tmp_len = EC_POINT_point2oct(a->group, a->pub_key,
  998. a->conv_form, NULL, 0, NULL);
  999. if (tmp_len > buf_len) {
  1000. unsigned char *tmp_buffer = OPENSSL_realloc(buffer, tmp_len);
  1001. if (!tmp_buffer) {
  1002. ECerr(EC_F_I2D_ECPRIVATEKEY, ERR_R_MALLOC_FAILURE);
  1003. goto err;
  1004. }
  1005. buffer = tmp_buffer;
  1006. buf_len = tmp_len;
  1007. }
  1008. if (!EC_POINT_point2oct(a->group, a->pub_key,
  1009. a->conv_form, buffer, buf_len, NULL)) {
  1010. ECerr(EC_F_I2D_ECPRIVATEKEY, ERR_R_EC_LIB);
  1011. goto err;
  1012. }
  1013. priv_key->publicKey->flags &= ~(ASN1_STRING_FLAG_BITS_LEFT | 0x07);
  1014. priv_key->publicKey->flags |= ASN1_STRING_FLAG_BITS_LEFT;
  1015. if (!ASN1_BIT_STRING_set(priv_key->publicKey, buffer, buf_len)) {
  1016. ECerr(EC_F_I2D_ECPRIVATEKEY, ERR_R_ASN1_LIB);
  1017. goto err;
  1018. }
  1019. }
  1020. if ((ret = i2d_EC_PRIVATEKEY(priv_key, out)) == 0) {
  1021. ECerr(EC_F_I2D_ECPRIVATEKEY, ERR_R_EC_LIB);
  1022. goto err;
  1023. }
  1024. ok = 1;
  1025. err:
  1026. OPENSSL_free(buffer);
  1027. EC_PRIVATEKEY_free(priv_key);
  1028. return (ok ? ret : 0);
  1029. }
  1030. int i2d_ECParameters(EC_KEY *a, unsigned char **out)
  1031. {
  1032. if (a == NULL) {
  1033. ECerr(EC_F_I2D_ECPARAMETERS, ERR_R_PASSED_NULL_PARAMETER);
  1034. return 0;
  1035. }
  1036. return i2d_ECPKParameters(a->group, out);
  1037. }
  1038. EC_KEY *d2i_ECParameters(EC_KEY **a, const unsigned char **in, long len)
  1039. {
  1040. EC_KEY *ret;
  1041. if (in == NULL || *in == NULL) {
  1042. ECerr(EC_F_D2I_ECPARAMETERS, ERR_R_PASSED_NULL_PARAMETER);
  1043. return NULL;
  1044. }
  1045. if (a == NULL || *a == NULL) {
  1046. if ((ret = EC_KEY_new()) == NULL) {
  1047. ECerr(EC_F_D2I_ECPARAMETERS, ERR_R_MALLOC_FAILURE);
  1048. return NULL;
  1049. }
  1050. } else
  1051. ret = *a;
  1052. if (!d2i_ECPKParameters(&ret->group, in, len)) {
  1053. ECerr(EC_F_D2I_ECPARAMETERS, ERR_R_EC_LIB);
  1054. if (a == NULL || *a != ret)
  1055. EC_KEY_free(ret);
  1056. return NULL;
  1057. }
  1058. if (a)
  1059. *a = ret;
  1060. return ret;
  1061. }
  1062. EC_KEY *o2i_ECPublicKey(EC_KEY **a, const unsigned char **in, long len)
  1063. {
  1064. EC_KEY *ret = NULL;
  1065. if (a == NULL || (*a) == NULL || (*a)->group == NULL) {
  1066. /*
  1067. * sorry, but a EC_GROUP-structur is necessary to set the public key
  1068. */
  1069. ECerr(EC_F_O2I_ECPUBLICKEY, ERR_R_PASSED_NULL_PARAMETER);
  1070. return 0;
  1071. }
  1072. ret = *a;
  1073. if (ret->pub_key == NULL &&
  1074. (ret->pub_key = EC_POINT_new(ret->group)) == NULL) {
  1075. ECerr(EC_F_O2I_ECPUBLICKEY, ERR_R_MALLOC_FAILURE);
  1076. return 0;
  1077. }
  1078. if (!EC_POINT_oct2point(ret->group, ret->pub_key, *in, len, NULL)) {
  1079. ECerr(EC_F_O2I_ECPUBLICKEY, ERR_R_EC_LIB);
  1080. return 0;
  1081. }
  1082. /* save the point conversion form */
  1083. ret->conv_form = (point_conversion_form_t) (*in[0] & ~0x01);
  1084. *in += len;
  1085. return ret;
  1086. }
  1087. int i2o_ECPublicKey(EC_KEY *a, unsigned char **out)
  1088. {
  1089. size_t buf_len = 0;
  1090. int new_buffer = 0;
  1091. if (a == NULL) {
  1092. ECerr(EC_F_I2O_ECPUBLICKEY, ERR_R_PASSED_NULL_PARAMETER);
  1093. return 0;
  1094. }
  1095. buf_len = EC_POINT_point2oct(a->group, a->pub_key,
  1096. a->conv_form, NULL, 0, NULL);
  1097. if (out == NULL || buf_len == 0)
  1098. /* out == NULL => just return the length of the octet string */
  1099. return buf_len;
  1100. if (*out == NULL) {
  1101. if ((*out = OPENSSL_malloc(buf_len)) == NULL) {
  1102. ECerr(EC_F_I2O_ECPUBLICKEY, ERR_R_MALLOC_FAILURE);
  1103. return 0;
  1104. }
  1105. new_buffer = 1;
  1106. }
  1107. if (!EC_POINT_point2oct(a->group, a->pub_key, a->conv_form,
  1108. *out, buf_len, NULL)) {
  1109. ECerr(EC_F_I2O_ECPUBLICKEY, ERR_R_EC_LIB);
  1110. if (new_buffer) {
  1111. OPENSSL_free(*out);
  1112. *out = NULL;
  1113. }
  1114. return 0;
  1115. }
  1116. if (!new_buffer)
  1117. *out += buf_len;
  1118. return buf_len;
  1119. }
  1120. ASN1_SEQUENCE(ECDSA_SIG) = {
  1121. ASN1_SIMPLE(ECDSA_SIG, r, CBIGNUM),
  1122. ASN1_SIMPLE(ECDSA_SIG, s, CBIGNUM)
  1123. } static_ASN1_SEQUENCE_END(ECDSA_SIG)
  1124. DECLARE_ASN1_FUNCTIONS_const(ECDSA_SIG)
  1125. DECLARE_ASN1_ENCODE_FUNCTIONS_const(ECDSA_SIG, ECDSA_SIG)
  1126. IMPLEMENT_ASN1_FUNCTIONS_const(ECDSA_SIG)
  1127. void ECDSA_SIG_get0(BIGNUM **pr, BIGNUM **ps, ECDSA_SIG *sig)
  1128. {
  1129. if (pr != NULL)
  1130. *pr = sig->r;
  1131. if (ps != NULL)
  1132. *ps = sig->s;
  1133. }
  1134. int ECDSA_size(const EC_KEY *r)
  1135. {
  1136. int ret, i;
  1137. ASN1_INTEGER bs;
  1138. BIGNUM *order = NULL;
  1139. unsigned char buf[4];
  1140. const EC_GROUP *group;
  1141. if (r == NULL)
  1142. return 0;
  1143. group = EC_KEY_get0_group(r);
  1144. if (group == NULL)
  1145. return 0;
  1146. if ((order = BN_new()) == NULL)
  1147. return 0;
  1148. if (!EC_GROUP_get_order(group, order, NULL)) {
  1149. BN_clear_free(order);
  1150. return 0;
  1151. }
  1152. i = BN_num_bits(order);
  1153. bs.length = (i + 7) / 8;
  1154. bs.data = buf;
  1155. bs.type = V_ASN1_INTEGER;
  1156. /* If the top bit is set the asn1 encoding is 1 larger. */
  1157. buf[0] = 0xff;
  1158. i = i2d_ASN1_INTEGER(&bs, NULL);
  1159. i += i; /* r and s */
  1160. ret = ASN1_object_size(1, i, V_ASN1_SEQUENCE);
  1161. BN_clear_free(order);
  1162. return (ret);
  1163. }