ec.c 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403
  1. /* apps/ec.c */
  2. /*
  3. * Written by Nils Larsch for the OpenSSL project.
  4. */
  5. /* ====================================================================
  6. * Copyright (c) 1998-2005 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. #include <openssl/opensslconf.h>
  59. #ifndef OPENSSL_NO_EC
  60. #include <stdio.h>
  61. #include <stdlib.h>
  62. #include <string.h>
  63. #include "apps.h"
  64. #include <openssl/bio.h>
  65. #include <openssl/err.h>
  66. #include <openssl/evp.h>
  67. #include <openssl/pem.h>
  68. #undef PROG
  69. #define PROG ec_main
  70. /* -inform arg - input format - default PEM (one of DER, NET or PEM)
  71. * -outform arg - output format - default PEM
  72. * -in arg - input file - default stdin
  73. * -out arg - output file - default stdout
  74. * -des - encrypt output if PEM format with DES in cbc mode
  75. * -text - print a text version
  76. * -param_out - print the elliptic curve parameters
  77. * -conv_form arg - specifies the point encoding form
  78. * -param_enc arg - specifies the parameter encoding
  79. */
  80. int MAIN(int, char **);
  81. int MAIN(int argc, char **argv)
  82. {
  83. #ifndef OPENSSL_NO_ENGINE
  84. ENGINE *e = NULL;
  85. #endif
  86. int ret = 1;
  87. EC_KEY *eckey = NULL;
  88. const EC_GROUP *group;
  89. int i, badops = 0;
  90. const EVP_CIPHER *enc = NULL;
  91. BIO *in = NULL, *out = NULL;
  92. int informat, outformat, text=0, noout=0;
  93. int pubin = 0, pubout = 0, param_out = 0;
  94. char *infile, *outfile, *prog, *engine;
  95. char *passargin = NULL, *passargout = NULL;
  96. char *passin = NULL, *passout = NULL;
  97. point_conversion_form_t form = POINT_CONVERSION_UNCOMPRESSED;
  98. int new_form = 0;
  99. int asn1_flag = OPENSSL_EC_NAMED_CURVE;
  100. int new_asn1_flag = 0;
  101. apps_startup();
  102. if (bio_err == NULL)
  103. if ((bio_err=BIO_new(BIO_s_file())) != NULL)
  104. BIO_set_fp(bio_err, stderr, BIO_NOCLOSE|BIO_FP_TEXT);
  105. if (!load_config(bio_err, NULL))
  106. goto end;
  107. engine = NULL;
  108. infile = NULL;
  109. outfile = NULL;
  110. informat = FORMAT_PEM;
  111. outformat = FORMAT_PEM;
  112. prog = argv[0];
  113. argc--;
  114. argv++;
  115. while (argc >= 1)
  116. {
  117. if (strcmp(*argv,"-inform") == 0)
  118. {
  119. if (--argc < 1) goto bad;
  120. informat=str2fmt(*(++argv));
  121. }
  122. else if (strcmp(*argv,"-outform") == 0)
  123. {
  124. if (--argc < 1) goto bad;
  125. outformat=str2fmt(*(++argv));
  126. }
  127. else if (strcmp(*argv,"-in") == 0)
  128. {
  129. if (--argc < 1) goto bad;
  130. infile= *(++argv);
  131. }
  132. else if (strcmp(*argv,"-out") == 0)
  133. {
  134. if (--argc < 1) goto bad;
  135. outfile= *(++argv);
  136. }
  137. else if (strcmp(*argv,"-passin") == 0)
  138. {
  139. if (--argc < 1) goto bad;
  140. passargin= *(++argv);
  141. }
  142. else if (strcmp(*argv,"-passout") == 0)
  143. {
  144. if (--argc < 1) goto bad;
  145. passargout= *(++argv);
  146. }
  147. else if (strcmp(*argv, "-engine") == 0)
  148. {
  149. if (--argc < 1) goto bad;
  150. engine= *(++argv);
  151. }
  152. else if (strcmp(*argv, "-noout") == 0)
  153. noout = 1;
  154. else if (strcmp(*argv, "-text") == 0)
  155. text = 1;
  156. else if (strcmp(*argv, "-conv_form") == 0)
  157. {
  158. if (--argc < 1)
  159. goto bad;
  160. ++argv;
  161. new_form = 1;
  162. if (strcmp(*argv, "compressed") == 0)
  163. form = POINT_CONVERSION_COMPRESSED;
  164. else if (strcmp(*argv, "uncompressed") == 0)
  165. form = POINT_CONVERSION_UNCOMPRESSED;
  166. else if (strcmp(*argv, "hybrid") == 0)
  167. form = POINT_CONVERSION_HYBRID;
  168. else
  169. goto bad;
  170. }
  171. else if (strcmp(*argv, "-param_enc") == 0)
  172. {
  173. if (--argc < 1)
  174. goto bad;
  175. ++argv;
  176. new_asn1_flag = 1;
  177. if (strcmp(*argv, "named_curve") == 0)
  178. asn1_flag = OPENSSL_EC_NAMED_CURVE;
  179. else if (strcmp(*argv, "explicit") == 0)
  180. asn1_flag = 0;
  181. else
  182. goto bad;
  183. }
  184. else if (strcmp(*argv, "-param_out") == 0)
  185. param_out = 1;
  186. else if (strcmp(*argv, "-pubin") == 0)
  187. pubin=1;
  188. else if (strcmp(*argv, "-pubout") == 0)
  189. pubout=1;
  190. else if ((enc=EVP_get_cipherbyname(&(argv[0][1]))) == NULL)
  191. {
  192. BIO_printf(bio_err, "unknown option %s\n", *argv);
  193. badops=1;
  194. break;
  195. }
  196. argc--;
  197. argv++;
  198. }
  199. if (badops)
  200. {
  201. bad:
  202. BIO_printf(bio_err, "%s [options] <infile >outfile\n", prog);
  203. BIO_printf(bio_err, "where options are\n");
  204. BIO_printf(bio_err, " -inform arg input format - "
  205. "DER or PEM\n");
  206. BIO_printf(bio_err, " -outform arg output format - "
  207. "DER or PEM\n");
  208. BIO_printf(bio_err, " -in arg input file\n");
  209. BIO_printf(bio_err, " -passin arg input file pass "
  210. "phrase source\n");
  211. BIO_printf(bio_err, " -out arg output file\n");
  212. BIO_printf(bio_err, " -passout arg output file pass "
  213. "phrase source\n");
  214. BIO_printf(bio_err, " -engine e use engine e, "
  215. "possibly a hardware device.\n");
  216. BIO_printf(bio_err, " -des encrypt PEM output, "
  217. "instead of 'des' every other \n"
  218. " cipher "
  219. "supported by OpenSSL can be used\n");
  220. BIO_printf(bio_err, " -text print the key\n");
  221. BIO_printf(bio_err, " -noout don't print key out\n");
  222. BIO_printf(bio_err, " -param_out print the elliptic "
  223. "curve parameters\n");
  224. BIO_printf(bio_err, " -conv_form arg specifies the "
  225. "point conversion form \n");
  226. BIO_printf(bio_err, " possible values:"
  227. " compressed\n");
  228. BIO_printf(bio_err, " "
  229. " uncompressed (default)\n");
  230. BIO_printf(bio_err, " "
  231. " hybrid\n");
  232. BIO_printf(bio_err, " -param_enc arg specifies the way"
  233. " the ec parameters are encoded\n");
  234. BIO_printf(bio_err, " in the asn1 der "
  235. "encoding\n");
  236. BIO_printf(bio_err, " possible values:"
  237. " named_curve (default)\n");
  238. BIO_printf(bio_err," "
  239. "explicit\n");
  240. goto end;
  241. }
  242. ERR_load_crypto_strings();
  243. #ifndef OPENSSL_NO_ENGINE
  244. e = setup_engine(bio_err, engine, 0);
  245. #endif
  246. if(!app_passwd(bio_err, passargin, passargout, &passin, &passout))
  247. {
  248. BIO_printf(bio_err, "Error getting passwords\n");
  249. goto end;
  250. }
  251. in = BIO_new(BIO_s_file());
  252. out = BIO_new(BIO_s_file());
  253. if ((in == NULL) || (out == NULL))
  254. {
  255. ERR_print_errors(bio_err);
  256. goto end;
  257. }
  258. if (infile == NULL)
  259. BIO_set_fp(in, stdin, BIO_NOCLOSE);
  260. else
  261. {
  262. if (BIO_read_filename(in, infile) <= 0)
  263. {
  264. perror(infile);
  265. goto end;
  266. }
  267. }
  268. BIO_printf(bio_err, "read EC key\n");
  269. if (informat == FORMAT_ASN1)
  270. {
  271. if (pubin)
  272. eckey = d2i_EC_PUBKEY_bio(in, NULL);
  273. else
  274. eckey = d2i_ECPrivateKey_bio(in, NULL);
  275. }
  276. else if (informat == FORMAT_PEM)
  277. {
  278. if (pubin)
  279. eckey = PEM_read_bio_EC_PUBKEY(in, NULL, NULL,
  280. NULL);
  281. else
  282. eckey = PEM_read_bio_ECPrivateKey(in, NULL, NULL,
  283. passin);
  284. }
  285. else
  286. {
  287. BIO_printf(bio_err, "bad input format specified for key\n");
  288. goto end;
  289. }
  290. if (eckey == NULL)
  291. {
  292. BIO_printf(bio_err,"unable to load Key\n");
  293. ERR_print_errors(bio_err);
  294. goto end;
  295. }
  296. if (outfile == NULL)
  297. {
  298. BIO_set_fp(out, stdout, BIO_NOCLOSE);
  299. #ifdef OPENSSL_SYS_VMS
  300. {
  301. BIO *tmpbio = BIO_new(BIO_f_linebuffer());
  302. out = BIO_push(tmpbio, out);
  303. }
  304. #endif
  305. }
  306. else
  307. {
  308. if (BIO_write_filename(out, outfile) <= 0)
  309. {
  310. perror(outfile);
  311. goto end;
  312. }
  313. }
  314. group = EC_KEY_get0_group(eckey);
  315. if (new_form)
  316. EC_KEY_set_conv_form(eckey, form);
  317. if (new_asn1_flag)
  318. EC_KEY_set_asn1_flag(eckey, asn1_flag);
  319. if (text)
  320. if (!EC_KEY_print(out, eckey, 0))
  321. {
  322. perror(outfile);
  323. ERR_print_errors(bio_err);
  324. goto end;
  325. }
  326. if (noout)
  327. {
  328. ret = 0;
  329. goto end;
  330. }
  331. BIO_printf(bio_err, "writing EC key\n");
  332. if (outformat == FORMAT_ASN1)
  333. {
  334. if (param_out)
  335. i = i2d_ECPKParameters_bio(out, group);
  336. else if (pubin || pubout)
  337. i = i2d_EC_PUBKEY_bio(out, eckey);
  338. else
  339. i = i2d_ECPrivateKey_bio(out, eckey);
  340. }
  341. else if (outformat == FORMAT_PEM)
  342. {
  343. if (param_out)
  344. i = PEM_write_bio_ECPKParameters(out, group);
  345. else if (pubin || pubout)
  346. i = PEM_write_bio_EC_PUBKEY(out, eckey);
  347. else
  348. i = PEM_write_bio_ECPrivateKey(out, eckey, enc,
  349. NULL, 0, NULL, passout);
  350. }
  351. else
  352. {
  353. BIO_printf(bio_err, "bad output format specified for "
  354. "outfile\n");
  355. goto end;
  356. }
  357. if (!i)
  358. {
  359. BIO_printf(bio_err, "unable to write private key\n");
  360. ERR_print_errors(bio_err);
  361. }
  362. else
  363. ret=0;
  364. end:
  365. if (in)
  366. BIO_free(in);
  367. if (out)
  368. BIO_free_all(out);
  369. if (eckey)
  370. EC_KEY_free(eckey);
  371. if (passin)
  372. OPENSSL_free(passin);
  373. if (passout)
  374. OPENSSL_free(passout);
  375. apps_shutdown();
  376. OPENSSL_EXIT(ret);
  377. }
  378. #endif