ecparam.c 23 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667
  1. /* apps/ecparam.c */
  2. /*
  3. * Written by Nils Larsch for the OpenSSL project.
  4. */
  5. /* ====================================================================
  6. * Copyright (c) 1998-2018 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. * openssl-core@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. /* ====================================================================
  59. * Copyright 2002 Sun Microsystems, Inc. ALL RIGHTS RESERVED.
  60. *
  61. * Portions of the attached software ("Contribution") are developed by
  62. * SUN MICROSYSTEMS, INC., and are contributed to the OpenSSL project.
  63. *
  64. * The Contribution is licensed pursuant to the OpenSSL open source
  65. * license provided above.
  66. *
  67. * The elliptic curve binary polynomial software is originally written by
  68. * Sheueling Chang Shantz and Douglas Stebila of Sun Microsystems Laboratories.
  69. *
  70. */
  71. #include <openssl/opensslconf.h>
  72. #ifndef OPENSSL_NO_EC
  73. # include <assert.h>
  74. # include <stdio.h>
  75. # include <stdlib.h>
  76. # include <time.h>
  77. # include <string.h>
  78. # include "apps.h"
  79. # include <openssl/bio.h>
  80. # include <openssl/err.h>
  81. # include <openssl/bn.h>
  82. # include <openssl/ec.h>
  83. # include <openssl/x509.h>
  84. # include <openssl/pem.h>
  85. # undef PROG
  86. # define PROG ecparam_main
  87. /*-
  88. * -inform arg - input format - default PEM (DER or PEM)
  89. * -outform arg - output format - default PEM
  90. * -in arg - input file - default stdin
  91. * -out arg - output file - default stdout
  92. * -noout - do not print the ec parameter
  93. * -text - print the ec parameters in text form
  94. * -check - validate the ec parameters
  95. * -C - print a 'C' function creating the parameters
  96. * -name arg - use the ec parameters with 'short name' name
  97. * -list_curves - prints a list of all currently available curve 'short names'
  98. * -conv_form arg - specifies the point conversion form
  99. * - possible values: compressed
  100. * uncompressed (default)
  101. * hybrid
  102. * -param_enc arg - specifies the way the ec parameters are encoded
  103. * in the asn1 der encoding
  104. * possible values: named_curve (default)
  105. * explicit
  106. * -no_seed - if 'explicit' parameters are chosen do not use the seed
  107. * -genkey - generate ec key
  108. * -rand file - files to use for random number input
  109. * -engine e - use engine e, possibly a hardware device
  110. */
  111. static int ecparam_print_var(BIO *, BIGNUM *, const char *, int,
  112. unsigned char *);
  113. int MAIN(int, char **);
  114. int MAIN(int argc, char **argv)
  115. {
  116. EC_GROUP *group = NULL;
  117. point_conversion_form_t form = POINT_CONVERSION_UNCOMPRESSED;
  118. int new_form = 0;
  119. int asn1_flag = OPENSSL_EC_NAMED_CURVE;
  120. int new_asn1_flag = 0;
  121. char *curve_name = NULL, *inrand = NULL;
  122. int list_curves = 0, no_seed = 0, check = 0,
  123. badops = 0, text = 0, i, need_rand = 0, genkey = 0;
  124. char *infile = NULL, *outfile = NULL, *prog;
  125. BIO *in = NULL, *out = NULL;
  126. int informat, outformat, noout = 0, C = 0, ret = 1;
  127. char *engine = NULL;
  128. ENGINE *e = NULL;
  129. BIGNUM *ec_p = NULL, *ec_a = NULL, *ec_b = NULL,
  130. *ec_gen = NULL, *ec_order = NULL, *ec_cofactor = NULL;
  131. unsigned char *buffer = NULL;
  132. apps_startup();
  133. if (bio_err == NULL)
  134. if ((bio_err = BIO_new(BIO_s_file())) != NULL)
  135. BIO_set_fp(bio_err, stderr, BIO_NOCLOSE | BIO_FP_TEXT);
  136. if (!load_config(bio_err, NULL))
  137. goto end;
  138. informat = FORMAT_PEM;
  139. outformat = FORMAT_PEM;
  140. prog = argv[0];
  141. argc--;
  142. argv++;
  143. while (argc >= 1) {
  144. if (strcmp(*argv, "-inform") == 0) {
  145. if (--argc < 1)
  146. goto bad;
  147. informat = str2fmt(*(++argv));
  148. } else if (strcmp(*argv, "-outform") == 0) {
  149. if (--argc < 1)
  150. goto bad;
  151. outformat = str2fmt(*(++argv));
  152. } else if (strcmp(*argv, "-in") == 0) {
  153. if (--argc < 1)
  154. goto bad;
  155. infile = *(++argv);
  156. } else if (strcmp(*argv, "-out") == 0) {
  157. if (--argc < 1)
  158. goto bad;
  159. outfile = *(++argv);
  160. } else if (strcmp(*argv, "-text") == 0)
  161. text = 1;
  162. else if (strcmp(*argv, "-C") == 0)
  163. C = 1;
  164. else if (strcmp(*argv, "-check") == 0)
  165. check = 1;
  166. else if (strcmp(*argv, "-name") == 0) {
  167. if (--argc < 1)
  168. goto bad;
  169. curve_name = *(++argv);
  170. } else if (strcmp(*argv, "-list_curves") == 0)
  171. list_curves = 1;
  172. else if (strcmp(*argv, "-conv_form") == 0) {
  173. if (--argc < 1)
  174. goto bad;
  175. ++argv;
  176. new_form = 1;
  177. if (strcmp(*argv, "compressed") == 0)
  178. form = POINT_CONVERSION_COMPRESSED;
  179. else if (strcmp(*argv, "uncompressed") == 0)
  180. form = POINT_CONVERSION_UNCOMPRESSED;
  181. else if (strcmp(*argv, "hybrid") == 0)
  182. form = POINT_CONVERSION_HYBRID;
  183. else
  184. goto bad;
  185. } else if (strcmp(*argv, "-param_enc") == 0) {
  186. if (--argc < 1)
  187. goto bad;
  188. ++argv;
  189. new_asn1_flag = 1;
  190. if (strcmp(*argv, "named_curve") == 0)
  191. asn1_flag = OPENSSL_EC_NAMED_CURVE;
  192. else if (strcmp(*argv, "explicit") == 0)
  193. asn1_flag = 0;
  194. else
  195. goto bad;
  196. } else if (strcmp(*argv, "-no_seed") == 0)
  197. no_seed = 1;
  198. else if (strcmp(*argv, "-noout") == 0)
  199. noout = 1;
  200. else if (strcmp(*argv, "-genkey") == 0) {
  201. genkey = 1;
  202. need_rand = 1;
  203. } else if (strcmp(*argv, "-rand") == 0) {
  204. if (--argc < 1)
  205. goto bad;
  206. inrand = *(++argv);
  207. need_rand = 1;
  208. } else if (strcmp(*argv, "-engine") == 0) {
  209. if (--argc < 1)
  210. goto bad;
  211. engine = *(++argv);
  212. } else {
  213. BIO_printf(bio_err, "unknown option %s\n", *argv);
  214. badops = 1;
  215. break;
  216. }
  217. argc--;
  218. argv++;
  219. }
  220. if (badops) {
  221. bad:
  222. BIO_printf(bio_err, "%s [options] <infile >outfile\n", prog);
  223. BIO_printf(bio_err, "where options are\n");
  224. BIO_printf(bio_err, " -inform arg input format - "
  225. "default PEM (DER or PEM)\n");
  226. BIO_printf(bio_err, " -outform arg output format - "
  227. "default PEM\n");
  228. BIO_printf(bio_err, " -in arg input file - "
  229. "default stdin\n");
  230. BIO_printf(bio_err, " -out arg output file - "
  231. "default stdout\n");
  232. BIO_printf(bio_err, " -noout do not print the "
  233. "ec parameter\n");
  234. BIO_printf(bio_err, " -text print the ec "
  235. "parameters in text form\n");
  236. BIO_printf(bio_err, " -check validate the ec "
  237. "parameters\n");
  238. BIO_printf(bio_err, " -C print a 'C' "
  239. "function creating the parameters\n");
  240. BIO_printf(bio_err, " -name arg use the "
  241. "ec parameters with 'short name' name\n");
  242. BIO_printf(bio_err, " -list_curves prints a list of "
  243. "all currently available curve 'short names'\n");
  244. BIO_printf(bio_err, " -conv_form arg specifies the "
  245. "point conversion form \n");
  246. BIO_printf(bio_err, " possible values:"
  247. " compressed\n");
  248. BIO_printf(bio_err, " "
  249. " uncompressed (default)\n");
  250. BIO_printf(bio_err, " "
  251. " hybrid\n");
  252. BIO_printf(bio_err, " -param_enc arg specifies the way"
  253. " the ec parameters are encoded\n");
  254. BIO_printf(bio_err, " in the asn1 der "
  255. "encoding\n");
  256. BIO_printf(bio_err, " possible values:"
  257. " named_curve (default)\n");
  258. BIO_printf(bio_err, " "
  259. " explicit\n");
  260. BIO_printf(bio_err, " -no_seed if 'explicit'"
  261. " parameters are chosen do not" " use the seed\n");
  262. BIO_printf(bio_err, " -genkey generate ec" " key\n");
  263. BIO_printf(bio_err, " -rand file files to use for"
  264. " random number input\n");
  265. BIO_printf(bio_err, " -engine e use engine e, "
  266. "possibly a hardware device\n");
  267. goto end;
  268. }
  269. ERR_load_crypto_strings();
  270. in = BIO_new(BIO_s_file());
  271. out = BIO_new(BIO_s_file());
  272. if ((in == NULL) || (out == NULL)) {
  273. ERR_print_errors(bio_err);
  274. goto end;
  275. }
  276. if (infile == NULL)
  277. BIO_set_fp(in, stdin, BIO_NOCLOSE);
  278. else {
  279. if (BIO_read_filename(in, infile) <= 0) {
  280. perror(infile);
  281. goto end;
  282. }
  283. }
  284. if (outfile == NULL) {
  285. BIO_set_fp(out, stdout, BIO_NOCLOSE);
  286. # ifdef OPENSSL_SYS_VMS
  287. {
  288. BIO *tmpbio = BIO_new(BIO_f_linebuffer());
  289. out = BIO_push(tmpbio, out);
  290. }
  291. # endif
  292. } else {
  293. if (BIO_write_filename(out, outfile) <= 0) {
  294. perror(outfile);
  295. goto end;
  296. }
  297. }
  298. e = setup_engine(bio_err, engine, 0);
  299. if (list_curves) {
  300. EC_builtin_curve *curves = NULL;
  301. size_t crv_len = 0;
  302. size_t n = 0;
  303. crv_len = EC_get_builtin_curves(NULL, 0);
  304. curves = OPENSSL_malloc((int)(sizeof(EC_builtin_curve) * crv_len));
  305. if (curves == NULL)
  306. goto end;
  307. if (!EC_get_builtin_curves(curves, crv_len)) {
  308. OPENSSL_free(curves);
  309. goto end;
  310. }
  311. for (n = 0; n < crv_len; n++) {
  312. const char *comment;
  313. const char *sname;
  314. comment = curves[n].comment;
  315. sname = OBJ_nid2sn(curves[n].nid);
  316. if (comment == NULL)
  317. comment = "CURVE DESCRIPTION NOT AVAILABLE";
  318. if (sname == NULL)
  319. sname = "";
  320. BIO_printf(out, " %-10s: ", sname);
  321. BIO_printf(out, "%s\n", comment);
  322. }
  323. OPENSSL_free(curves);
  324. ret = 0;
  325. goto end;
  326. }
  327. if (curve_name != NULL) {
  328. int nid;
  329. /*
  330. * workaround for the SECG curve names secp192r1 and secp256r1 (which
  331. * are the same as the curves prime192v1 and prime256v1 defined in
  332. * X9.62)
  333. */
  334. if (!strcmp(curve_name, "secp192r1")) {
  335. BIO_printf(bio_err, "using curve name prime192v1 "
  336. "instead of secp192r1\n");
  337. nid = NID_X9_62_prime192v1;
  338. } else if (!strcmp(curve_name, "secp256r1")) {
  339. BIO_printf(bio_err, "using curve name prime256v1 "
  340. "instead of secp256r1\n");
  341. nid = NID_X9_62_prime256v1;
  342. } else
  343. nid = OBJ_sn2nid(curve_name);
  344. if (nid == 0)
  345. nid = EC_curve_nist2nid(curve_name);
  346. if (nid == 0) {
  347. BIO_printf(bio_err, "unknown curve name (%s)\n", curve_name);
  348. goto end;
  349. }
  350. group = EC_GROUP_new_by_curve_name(nid);
  351. if (group == NULL) {
  352. BIO_printf(bio_err, "unable to create curve (%s)\n", curve_name);
  353. goto end;
  354. }
  355. EC_GROUP_set_asn1_flag(group, asn1_flag);
  356. EC_GROUP_set_point_conversion_form(group, form);
  357. } else if (informat == FORMAT_ASN1) {
  358. group = d2i_ECPKParameters_bio(in, NULL);
  359. } else if (informat == FORMAT_PEM) {
  360. group = PEM_read_bio_ECPKParameters(in, NULL, NULL, NULL);
  361. } else {
  362. BIO_printf(bio_err, "bad input format specified\n");
  363. goto end;
  364. }
  365. if (group == NULL) {
  366. BIO_printf(bio_err, "unable to load elliptic curve parameters\n");
  367. ERR_print_errors(bio_err);
  368. goto end;
  369. }
  370. if (new_form)
  371. EC_GROUP_set_point_conversion_form(group, form);
  372. if (new_asn1_flag)
  373. EC_GROUP_set_asn1_flag(group, asn1_flag);
  374. if (no_seed) {
  375. EC_GROUP_set_seed(group, NULL, 0);
  376. }
  377. if (text) {
  378. if (!ECPKParameters_print(out, group, 0))
  379. goto end;
  380. }
  381. if (check) {
  382. BIO_printf(bio_err, "checking elliptic curve parameters: ");
  383. if (!EC_GROUP_check(group, NULL)) {
  384. BIO_printf(bio_err, "failed\n");
  385. ERR_print_errors(bio_err);
  386. goto end;
  387. }
  388. BIO_printf(bio_err, "ok\n");
  389. }
  390. if (C) {
  391. size_t buf_len = 0, tmp_len = 0;
  392. const EC_POINT *point;
  393. int is_prime, len = 0;
  394. const EC_METHOD *meth = EC_GROUP_method_of(group);
  395. if ((ec_p = BN_new()) == NULL || (ec_a = BN_new()) == NULL ||
  396. (ec_b = BN_new()) == NULL || (ec_gen = BN_new()) == NULL ||
  397. (ec_order = BN_new()) == NULL ||
  398. (ec_cofactor = BN_new()) == NULL) {
  399. perror("OPENSSL_malloc");
  400. goto end;
  401. }
  402. is_prime = (EC_METHOD_get_field_type(meth) == NID_X9_62_prime_field);
  403. if (is_prime) {
  404. if (!EC_GROUP_get_curve_GFp(group, ec_p, ec_a, ec_b, NULL))
  405. goto end;
  406. } else {
  407. /* TODO */
  408. goto end;
  409. }
  410. if ((point = EC_GROUP_get0_generator(group)) == NULL)
  411. goto end;
  412. if (!EC_POINT_point2bn(group, point,
  413. EC_GROUP_get_point_conversion_form(group),
  414. ec_gen, NULL))
  415. goto end;
  416. if (!EC_GROUP_get_order(group, ec_order, NULL))
  417. goto end;
  418. if (!EC_GROUP_get_cofactor(group, ec_cofactor, NULL))
  419. goto end;
  420. if (!ec_p || !ec_a || !ec_b || !ec_gen || !ec_order || !ec_cofactor)
  421. goto end;
  422. len = BN_num_bits(ec_order);
  423. if ((tmp_len = (size_t)BN_num_bytes(ec_p)) > buf_len)
  424. buf_len = tmp_len;
  425. if ((tmp_len = (size_t)BN_num_bytes(ec_a)) > buf_len)
  426. buf_len = tmp_len;
  427. if ((tmp_len = (size_t)BN_num_bytes(ec_b)) > buf_len)
  428. buf_len = tmp_len;
  429. if ((tmp_len = (size_t)BN_num_bytes(ec_gen)) > buf_len)
  430. buf_len = tmp_len;
  431. if ((tmp_len = (size_t)BN_num_bytes(ec_order)) > buf_len)
  432. buf_len = tmp_len;
  433. if ((tmp_len = (size_t)BN_num_bytes(ec_cofactor)) > buf_len)
  434. buf_len = tmp_len;
  435. buffer = (unsigned char *)OPENSSL_malloc(buf_len);
  436. if (buffer == NULL) {
  437. perror("OPENSSL_malloc");
  438. goto end;
  439. }
  440. ecparam_print_var(out, ec_p, "ec_p", len, buffer);
  441. ecparam_print_var(out, ec_a, "ec_a", len, buffer);
  442. ecparam_print_var(out, ec_b, "ec_b", len, buffer);
  443. ecparam_print_var(out, ec_gen, "ec_gen", len, buffer);
  444. ecparam_print_var(out, ec_order, "ec_order", len, buffer);
  445. ecparam_print_var(out, ec_cofactor, "ec_cofactor", len, buffer);
  446. BIO_printf(out, "\n\n");
  447. BIO_printf(out, "EC_GROUP *get_ec_group_%d(void)\n\t{\n", len);
  448. BIO_printf(out, "\tint ok=0;\n");
  449. BIO_printf(out, "\tEC_GROUP *group = NULL;\n");
  450. BIO_printf(out, "\tEC_POINT *point = NULL;\n");
  451. BIO_printf(out, "\tBIGNUM *tmp_1 = NULL, *tmp_2 = NULL, "
  452. "*tmp_3 = NULL;\n\n");
  453. BIO_printf(out, "\tif ((tmp_1 = BN_bin2bn(ec_p_%d, "
  454. "sizeof(ec_p_%d), NULL)) == NULL)\n\t\t"
  455. "goto err;\n", len, len);
  456. BIO_printf(out, "\tif ((tmp_2 = BN_bin2bn(ec_a_%d, "
  457. "sizeof(ec_a_%d), NULL)) == NULL)\n\t\t"
  458. "goto err;\n", len, len);
  459. BIO_printf(out, "\tif ((tmp_3 = BN_bin2bn(ec_b_%d, "
  460. "sizeof(ec_b_%d), NULL)) == NULL)\n\t\t"
  461. "goto err;\n", len, len);
  462. if (is_prime) {
  463. BIO_printf(out, "\tif ((group = EC_GROUP_new_curve_"
  464. "GFp(tmp_1, tmp_2, tmp_3, NULL)) == NULL)"
  465. "\n\t\tgoto err;\n\n");
  466. } else {
  467. /* TODO */
  468. goto end;
  469. }
  470. BIO_printf(out, "\t/* build generator */\n");
  471. BIO_printf(out, "\tif ((tmp_1 = BN_bin2bn(ec_gen_%d, "
  472. "sizeof(ec_gen_%d), tmp_1)) == NULL)"
  473. "\n\t\tgoto err;\n", len, len);
  474. BIO_printf(out, "\tpoint = EC_POINT_bn2point(group, tmp_1, "
  475. "NULL, NULL);\n");
  476. BIO_printf(out, "\tif (point == NULL)\n\t\tgoto err;\n");
  477. BIO_printf(out, "\tif ((tmp_2 = BN_bin2bn(ec_order_%d, "
  478. "sizeof(ec_order_%d), tmp_2)) == NULL)"
  479. "\n\t\tgoto err;\n", len, len);
  480. BIO_printf(out, "\tif ((tmp_3 = BN_bin2bn(ec_cofactor_%d, "
  481. "sizeof(ec_cofactor_%d), tmp_3)) == NULL)"
  482. "\n\t\tgoto err;\n", len, len);
  483. BIO_printf(out, "\tif (!EC_GROUP_set_generator(group, point,"
  484. " tmp_2, tmp_3))\n\t\tgoto err;\n");
  485. BIO_printf(out, "\n\tok=1;\n");
  486. BIO_printf(out, "err:\n");
  487. BIO_printf(out, "\tif (tmp_1)\n\t\tBN_free(tmp_1);\n");
  488. BIO_printf(out, "\tif (tmp_2)\n\t\tBN_free(tmp_2);\n");
  489. BIO_printf(out, "\tif (tmp_3)\n\t\tBN_free(tmp_3);\n");
  490. BIO_printf(out, "\tif (point)\n\t\tEC_POINT_free(point);\n");
  491. BIO_printf(out, "\tif (!ok)\n");
  492. BIO_printf(out, "\t\t{\n");
  493. BIO_printf(out, "\t\tEC_GROUP_free(group);\n");
  494. BIO_printf(out, "\t\tgroup = NULL;\n");
  495. BIO_printf(out, "\t\t}\n");
  496. BIO_printf(out, "\treturn(group);\n\t}\n");
  497. }
  498. if (outformat == FORMAT_ASN1 && genkey)
  499. noout = 1;
  500. if (!noout) {
  501. if (outformat == FORMAT_ASN1)
  502. i = i2d_ECPKParameters_bio(out, group);
  503. else if (outformat == FORMAT_PEM)
  504. i = PEM_write_bio_ECPKParameters(out, group);
  505. else {
  506. BIO_printf(bio_err, "bad output format specified for"
  507. " outfile\n");
  508. goto end;
  509. }
  510. if (!i) {
  511. BIO_printf(bio_err, "unable to write elliptic "
  512. "curve parameters\n");
  513. ERR_print_errors(bio_err);
  514. goto end;
  515. }
  516. }
  517. if (need_rand) {
  518. app_RAND_load_file(NULL, bio_err, (inrand != NULL));
  519. if (inrand != NULL)
  520. BIO_printf(bio_err, "%ld semi-random bytes loaded\n",
  521. app_RAND_load_files(inrand));
  522. }
  523. if (genkey) {
  524. EC_KEY *eckey = EC_KEY_new();
  525. if (eckey == NULL)
  526. goto end;
  527. assert(need_rand);
  528. if (EC_KEY_set_group(eckey, group) == 0)
  529. goto end;
  530. if (new_form)
  531. EC_KEY_set_conv_form(eckey, form);
  532. if (!EC_KEY_generate_key(eckey)) {
  533. EC_KEY_free(eckey);
  534. goto end;
  535. }
  536. if (outformat == FORMAT_ASN1)
  537. i = i2d_ECPrivateKey_bio(out, eckey);
  538. else if (outformat == FORMAT_PEM)
  539. i = PEM_write_bio_ECPrivateKey(out, eckey, NULL,
  540. NULL, 0, NULL, NULL);
  541. else {
  542. BIO_printf(bio_err, "bad output format specified "
  543. "for outfile\n");
  544. EC_KEY_free(eckey);
  545. goto end;
  546. }
  547. EC_KEY_free(eckey);
  548. }
  549. if (need_rand)
  550. app_RAND_write_file(NULL, bio_err);
  551. ret = 0;
  552. end:
  553. if (ec_p)
  554. BN_free(ec_p);
  555. if (ec_a)
  556. BN_free(ec_a);
  557. if (ec_b)
  558. BN_free(ec_b);
  559. if (ec_gen)
  560. BN_free(ec_gen);
  561. if (ec_order)
  562. BN_free(ec_order);
  563. if (ec_cofactor)
  564. BN_free(ec_cofactor);
  565. if (buffer)
  566. OPENSSL_free(buffer);
  567. if (group != NULL)
  568. EC_GROUP_free(group);
  569. release_engine(e);
  570. if (in != NULL)
  571. BIO_free(in);
  572. if (out != NULL)
  573. BIO_free_all(out);
  574. apps_shutdown();
  575. OPENSSL_EXIT(ret);
  576. }
  577. static int ecparam_print_var(BIO *out, BIGNUM *in, const char *var,
  578. int len, unsigned char *buffer)
  579. {
  580. BIO_printf(out, "static unsigned char %s_%d[] = {", var, len);
  581. if (BN_is_zero(in))
  582. BIO_printf(out, "\n\t0x00");
  583. else {
  584. int i, l;
  585. l = BN_bn2bin(in, buffer);
  586. for (i = 0; i < l - 1; i++) {
  587. if ((i % 12) == 0)
  588. BIO_printf(out, "\n\t");
  589. BIO_printf(out, "0x%02X,", buffer[i]);
  590. }
  591. if ((i % 12) == 0)
  592. BIO_printf(out, "\n\t");
  593. BIO_printf(out, "0x%02X", buffer[i]);
  594. }
  595. BIO_printf(out, "\n\t};\n\n");
  596. return 1;
  597. }
  598. #else /* !OPENSSL_NO_EC */
  599. # if PEDANTIC
  600. static void *dummy = &dummy;
  601. # endif
  602. #endif