ec_asn1.c 38 KB

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