ec.c 11 KB

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