ec.c 12 KB

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