ec_asn1.c 33 KB

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