ec.c 9.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313
  1. /*
  2. * Written by Nils Larsch for the OpenSSL project.
  3. */
  4. /* ====================================================================
  5. * Copyright (c) 1998-2005 The OpenSSL Project. All rights reserved.
  6. *
  7. * Redistribution and use in source and binary forms, with or without
  8. * modification, are permitted provided that the following conditions
  9. * are met:
  10. *
  11. * 1. Redistributions of source code must retain the above copyright
  12. * notice, this list of conditions and the following disclaimer.
  13. *
  14. * 2. Redistributions in binary form must reproduce the above copyright
  15. * notice, this list of conditions and the following disclaimer in
  16. * the documentation and/or other materials provided with the
  17. * distribution.
  18. *
  19. * 3. All advertising materials mentioning features or use of this
  20. * software must display the following acknowledgment:
  21. * "This product includes software developed by the OpenSSL Project
  22. * for use in the OpenSSL Toolkit. (http://www.openssl.org/)"
  23. *
  24. * 4. The names "OpenSSL Toolkit" and "OpenSSL Project" must not be used to
  25. * endorse or promote products derived from this software without
  26. * prior written permission. For written permission, please contact
  27. * openssl-core@openssl.org.
  28. *
  29. * 5. Products derived from this software may not be called "OpenSSL"
  30. * nor may "OpenSSL" appear in their names without prior written
  31. * permission of the OpenSSL Project.
  32. *
  33. * 6. Redistributions of any form whatsoever must retain the following
  34. * acknowledgment:
  35. * "This product includes software developed by the OpenSSL Project
  36. * for use in the OpenSSL Toolkit (http://www.openssl.org/)"
  37. *
  38. * THIS SOFTWARE IS PROVIDED BY THE OpenSSL PROJECT ``AS IS'' AND ANY
  39. * EXPRESSED OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
  40. * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
  41. * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE OpenSSL PROJECT OR
  42. * ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
  43. * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
  44. * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
  45. * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
  46. * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
  47. * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
  48. * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED
  49. * OF THE POSSIBILITY OF SUCH DAMAGE.
  50. * ====================================================================
  51. *
  52. * This product includes cryptographic software written by Eric Young
  53. * (eay@cryptsoft.com). This product includes software written by Tim
  54. * Hudson (tjh@cryptsoft.com).
  55. *
  56. */
  57. #include <openssl/opensslconf.h>
  58. #ifdef OPENSSL_NO_EC
  59. NON_EMPTY_TRANSLATION_UNIT
  60. #else
  61. # include <stdio.h>
  62. # include <stdlib.h>
  63. # include <string.h>
  64. # include "apps.h"
  65. # include <openssl/bio.h>
  66. # include <openssl/err.h>
  67. # include <openssl/evp.h>
  68. # include <openssl/pem.h>
  69. static OPT_PAIR conv_forms[] = {
  70. {"compressed", POINT_CONVERSION_COMPRESSED},
  71. {"uncompressed", POINT_CONVERSION_UNCOMPRESSED},
  72. {"hybrid", POINT_CONVERSION_HYBRID},
  73. {NULL}
  74. };
  75. static OPT_PAIR param_enc[] = {
  76. {"named_curve", OPENSSL_EC_NAMED_CURVE},
  77. {"explicit", 0},
  78. {NULL}
  79. };
  80. typedef enum OPTION_choice {
  81. OPT_ERR = -1, OPT_EOF = 0, OPT_HELP,
  82. OPT_INFORM, OPT_OUTFORM, OPT_ENGINE, OPT_IN, OPT_OUT,
  83. OPT_NOOUT, OPT_TEXT, OPT_PARAM_OUT, OPT_PUBIN, OPT_PUBOUT,
  84. OPT_PASSIN, OPT_PASSOUT, OPT_PARAM_ENC, OPT_CONV_FORM, OPT_CIPHER,
  85. OPT_NO_PUBLIC, OPT_CHECK
  86. } OPTION_CHOICE;
  87. OPTIONS ec_options[] = {
  88. {"help", OPT_HELP, '-', "Display this summary"},
  89. {"in", OPT_IN, '<', "Input file"},
  90. {"inform", OPT_INFORM, 'F', "Input format - DER or PEM"},
  91. {"out", OPT_OUT, '>', "Output file"},
  92. {"outform", OPT_OUTFORM, 'F', "Output format - DER or PEM"},
  93. {"noout", OPT_NOOUT, '-', "Don't print key out"},
  94. {"text", OPT_TEXT, '-', "Print the key"},
  95. {"param_out", OPT_PARAM_OUT, '-', "Print the elliptic curve parameters"},
  96. {"pubin", OPT_PUBIN, '-'},
  97. {"pubout", OPT_PUBOUT, '-'},
  98. {"no_public", OPT_NO_PUBLIC, '-', "exclude public key from private key"},
  99. {"check", OPT_CHECK, '-', "check key consistency"},
  100. {"passin", OPT_PASSIN, 's', "Input file pass phrase source"},
  101. {"passout", OPT_PASSOUT, 's', "Output file pass phrase source"},
  102. {"param_enc", OPT_PARAM_ENC, 's',
  103. "Specifies the way the ec parameters are encoded"},
  104. {"conv_form", OPT_CONV_FORM, 's', "Specifies the point conversion form "},
  105. {"", OPT_CIPHER, '-', "Any supported cipher"},
  106. # ifndef OPENSSL_NO_ENGINE
  107. {"engine", OPT_ENGINE, 's', "Use engine, possibly a hardware device"},
  108. # endif
  109. {NULL}
  110. };
  111. int ec_main(int argc, char **argv)
  112. {
  113. BIO *in = NULL, *out = NULL;
  114. EC_KEY *eckey = NULL;
  115. const EC_GROUP *group;
  116. const EVP_CIPHER *enc = NULL;
  117. point_conversion_form_t form = POINT_CONVERSION_UNCOMPRESSED;
  118. char *infile = NULL, *outfile = NULL, *prog;
  119. char *passin = NULL, *passout = NULL, *passinarg = NULL, *passoutarg = NULL;
  120. OPTION_CHOICE o;
  121. int asn1_flag = OPENSSL_EC_NAMED_CURVE, new_form = 0, new_asn1_flag = 0;
  122. int informat = FORMAT_PEM, outformat = FORMAT_PEM, text = 0, noout = 0;
  123. int pubin = 0, pubout = 0, param_out = 0, i, ret = 1, private = 0;
  124. int no_public = 0, check = 0;
  125. prog = opt_init(argc, argv, ec_options);
  126. while ((o = opt_next()) != OPT_EOF) {
  127. switch (o) {
  128. case OPT_EOF:
  129. case OPT_ERR:
  130. opthelp:
  131. BIO_printf(bio_err, "%s: Use -help for summary.\n", prog);
  132. goto end;
  133. case OPT_HELP:
  134. opt_help(ec_options);
  135. ret = 0;
  136. goto end;
  137. case OPT_INFORM:
  138. if (!opt_format(opt_arg(), OPT_FMT_PEMDER, &informat))
  139. goto opthelp;
  140. break;
  141. case OPT_IN:
  142. infile = opt_arg();
  143. break;
  144. case OPT_OUTFORM:
  145. if (!opt_format(opt_arg(), OPT_FMT_PEMDER, &outformat))
  146. goto opthelp;
  147. break;
  148. case OPT_OUT:
  149. outfile = opt_arg();
  150. break;
  151. case OPT_NOOUT:
  152. noout = 1;
  153. break;
  154. case OPT_TEXT:
  155. text = 1;
  156. break;
  157. case OPT_PARAM_OUT:
  158. param_out = 1;
  159. break;
  160. case OPT_PUBIN:
  161. pubin = 1;
  162. break;
  163. case OPT_PUBOUT:
  164. pubout = 1;
  165. break;
  166. case OPT_PASSIN:
  167. passinarg = opt_arg();
  168. break;
  169. case OPT_PASSOUT:
  170. passoutarg = opt_arg();
  171. break;
  172. case OPT_ENGINE:
  173. (void)setup_engine(opt_arg(), 0);
  174. break;
  175. case OPT_CIPHER:
  176. if (!opt_cipher(opt_unknown(), &enc))
  177. goto opthelp;
  178. break;
  179. case OPT_CONV_FORM:
  180. if (!opt_pair(opt_arg(), conv_forms, &i))
  181. goto opthelp;
  182. new_form = 1;
  183. form = i;
  184. break;
  185. case OPT_PARAM_ENC:
  186. if (!opt_pair(opt_arg(), param_enc, &i))
  187. goto opthelp;
  188. new_asn1_flag = 1;
  189. asn1_flag = i;
  190. break;
  191. case OPT_NO_PUBLIC:
  192. no_public = 1;
  193. break;
  194. case OPT_CHECK:
  195. check = 1;
  196. break;
  197. }
  198. }
  199. argc = opt_num_rest();
  200. argv = opt_rest();
  201. private = param_out || pubin || pubout ? 0 : 1;
  202. if (text && !pubin)
  203. private = 1;
  204. if (!app_passwd(passinarg, passoutarg, &passin, &passout)) {
  205. BIO_printf(bio_err, "Error getting passwords\n");
  206. goto end;
  207. }
  208. in = bio_open_default(infile, 'r', informat);
  209. if (in == NULL)
  210. goto end;
  211. BIO_printf(bio_err, "read EC key\n");
  212. if (informat == FORMAT_ASN1) {
  213. if (pubin)
  214. eckey = d2i_EC_PUBKEY_bio(in, NULL);
  215. else
  216. eckey = d2i_ECPrivateKey_bio(in, NULL);
  217. } else {
  218. if (pubin)
  219. eckey = PEM_read_bio_EC_PUBKEY(in, NULL, NULL, NULL);
  220. else
  221. eckey = PEM_read_bio_ECPrivateKey(in, NULL, NULL, passin);
  222. }
  223. if (eckey == NULL) {
  224. BIO_printf(bio_err, "unable to load Key\n");
  225. ERR_print_errors(bio_err);
  226. goto end;
  227. }
  228. out = bio_open_owner(outfile, outformat, private);
  229. if (out == NULL)
  230. goto end;
  231. group = EC_KEY_get0_group(eckey);
  232. if (new_form)
  233. EC_KEY_set_conv_form(eckey, form);
  234. if (new_asn1_flag)
  235. EC_KEY_set_asn1_flag(eckey, asn1_flag);
  236. if (no_public)
  237. EC_KEY_set_enc_flags(eckey, EC_PKEY_NO_PUBKEY);
  238. if (text) {
  239. assert(pubin || private);
  240. if (!EC_KEY_print(out, eckey, 0)) {
  241. perror(outfile);
  242. ERR_print_errors(bio_err);
  243. goto end;
  244. }
  245. }
  246. if (check) {
  247. if (EC_KEY_check_key(eckey) == 1) {
  248. BIO_printf(bio_err, "EC Key valid.\n");
  249. } else {
  250. BIO_printf(bio_err, "EC Key Invalid!\n");
  251. ERR_print_errors(bio_err);
  252. }
  253. }
  254. if (noout) {
  255. ret = 0;
  256. goto end;
  257. }
  258. BIO_printf(bio_err, "writing EC key\n");
  259. if (outformat == FORMAT_ASN1) {
  260. if (param_out)
  261. i = i2d_ECPKParameters_bio(out, group);
  262. else if (pubin || pubout)
  263. i = i2d_EC_PUBKEY_bio(out, eckey);
  264. else {
  265. assert(private);
  266. i = i2d_ECPrivateKey_bio(out, eckey);
  267. }
  268. } else {
  269. if (param_out)
  270. i = PEM_write_bio_ECPKParameters(out, group);
  271. else if (pubin || pubout)
  272. i = PEM_write_bio_EC_PUBKEY(out, eckey);
  273. else {
  274. assert(private);
  275. i = PEM_write_bio_ECPrivateKey(out, eckey, enc,
  276. NULL, 0, NULL, passout);
  277. }
  278. }
  279. if (!i) {
  280. BIO_printf(bio_err, "unable to write private key\n");
  281. ERR_print_errors(bio_err);
  282. } else
  283. ret = 0;
  284. end:
  285. BIO_free(in);
  286. BIO_free_all(out);
  287. EC_KEY_free(eckey);
  288. OPENSSL_free(passin);
  289. OPENSSL_free(passout);
  290. return (ret);
  291. }
  292. #endif