ec.c 9.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294
  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. #ifndef OPENSSL_NO_EC
  59. # include <stdio.h>
  60. # include <stdlib.h>
  61. # include <string.h>
  62. # include "apps.h"
  63. # include <openssl/bio.h>
  64. # include <openssl/err.h>
  65. # include <openssl/evp.h>
  66. # include <openssl/pem.h>
  67. static OPT_PAIR conv_forms[] = {
  68. {"compressed", POINT_CONVERSION_COMPRESSED},
  69. {"uncompressed", POINT_CONVERSION_UNCOMPRESSED},
  70. {"hybrid", POINT_CONVERSION_HYBRID},
  71. {NULL}
  72. };
  73. static OPT_PAIR param_enc[] = {
  74. {"named_curve", OPENSSL_EC_NAMED_CURVE},
  75. {"explicit", 0},
  76. {NULL}
  77. };
  78. typedef enum OPTION_choice {
  79. OPT_ERR = -1, OPT_EOF = 0, OPT_HELP,
  80. OPT_INFORM, OPT_OUTFORM, OPT_ENGINE, OPT_IN, OPT_OUT,
  81. OPT_NOOUT, OPT_TEXT, OPT_PARAM_OUT, OPT_PUBIN, OPT_PUBOUT,
  82. OPT_PASSIN, OPT_PASSOUT, OPT_PARAM_ENC, OPT_CONV_FORM, OPT_CIPHER
  83. } OPTION_CHOICE;
  84. OPTIONS ec_options[] = {
  85. {"help", OPT_HELP, '-', "Display this summary"},
  86. {"in", OPT_IN, '<', "Input file"},
  87. {"inform", OPT_INFORM, 'F', "Input format - DER or PEM"},
  88. {"out", OPT_OUT, '>', "Output file"},
  89. {"outform", OPT_OUTFORM, 'F', "Output format - DER or PEM"},
  90. {"noout", OPT_NOOUT, '-', "Don't print key out"},
  91. {"text", OPT_TEXT, '-', "Print the key"},
  92. {"param_out", OPT_PARAM_OUT, '-', "Print the elliptic curve parameters"},
  93. {"pubin", OPT_PUBIN, '-'},
  94. {"pubout", OPT_PUBOUT, '-'},
  95. {"passin", OPT_PASSIN, 's', "Input file pass phrase source"},
  96. {"passout", OPT_PASSOUT, 's', "Output file pass phrase source"},
  97. {"param_enc", OPT_PARAM_ENC, 's',
  98. "Specifies the way the ec parameters are encoded"},
  99. {"conv_form", OPT_CONV_FORM, 's', "Specifies the point conversion form "},
  100. {"", OPT_CIPHER, '-', "Any supported cipher"},
  101. # ifndef OPENSSL_NO_ENGINE
  102. {"engine", OPT_ENGINE, 's', "Use engine, possibly a hardware device"},
  103. # endif
  104. {NULL}
  105. };
  106. int ec_main(int argc, char **argv)
  107. {
  108. BIO *in = NULL, *out = NULL;
  109. EC_KEY *eckey = NULL;
  110. const EC_GROUP *group;
  111. const EVP_CIPHER *enc = NULL;
  112. point_conversion_form_t form = POINT_CONVERSION_UNCOMPRESSED;
  113. char *infile = NULL, *outfile = NULL, *prog;
  114. char *passin = NULL, *passout = NULL, *passinarg = NULL, *passoutarg = NULL;
  115. OPTION_CHOICE o;
  116. int asn1_flag = OPENSSL_EC_NAMED_CURVE, new_form = 0, new_asn1_flag = 0;
  117. int informat = FORMAT_PEM, outformat = FORMAT_PEM, text = 0, noout = 0;
  118. int pubin = 0, pubout = 0, param_out = 0, i, ret = 1, private = 0;
  119. prog = opt_init(argc, argv, ec_options);
  120. while ((o = opt_next()) != OPT_EOF) {
  121. switch (o) {
  122. case OPT_EOF:
  123. case OPT_ERR:
  124. opthelp:
  125. BIO_printf(bio_err, "%s: Use -help for summary.\n", prog);
  126. goto end;
  127. case OPT_HELP:
  128. opt_help(ec_options);
  129. ret = 0;
  130. goto end;
  131. case OPT_INFORM:
  132. if (!opt_format(opt_arg(), OPT_FMT_PEMDER, &informat))
  133. goto opthelp;
  134. break;
  135. case OPT_IN:
  136. infile = opt_arg();
  137. break;
  138. case OPT_OUTFORM:
  139. if (!opt_format(opt_arg(), OPT_FMT_PEMDER, &outformat))
  140. goto opthelp;
  141. break;
  142. case OPT_OUT:
  143. outfile = opt_arg();
  144. break;
  145. case OPT_NOOUT:
  146. noout = 1;
  147. break;
  148. case OPT_TEXT:
  149. text = 1;
  150. break;
  151. case OPT_PARAM_OUT:
  152. param_out = 1;
  153. break;
  154. case OPT_PUBIN:
  155. pubin = 1;
  156. break;
  157. case OPT_PUBOUT:
  158. pubout = 1;
  159. break;
  160. case OPT_PASSIN:
  161. passinarg = opt_arg();
  162. break;
  163. case OPT_PASSOUT:
  164. passoutarg = opt_arg();
  165. break;
  166. case OPT_ENGINE:
  167. (void)setup_engine(opt_arg(), 0);
  168. break;
  169. case OPT_CIPHER:
  170. if (!opt_cipher(opt_unknown(), &enc))
  171. goto opthelp;
  172. break;
  173. case OPT_CONV_FORM:
  174. if (!opt_pair(opt_arg(), conv_forms, &i))
  175. goto opthelp;
  176. new_form = 1;
  177. form = i;
  178. break;
  179. case OPT_PARAM_ENC:
  180. if (!opt_pair(opt_arg(), param_enc, &i))
  181. goto opthelp;
  182. new_asn1_flag = 1;
  183. asn1_flag = i;
  184. break;
  185. }
  186. }
  187. argc = opt_num_rest();
  188. argv = opt_rest();
  189. private = param_out || pubin || pubout ? 0 : 1;
  190. if (text && !pubin)
  191. private = 1;
  192. if (!app_passwd(passinarg, passoutarg, &passin, &passout)) {
  193. BIO_printf(bio_err, "Error getting passwords\n");
  194. goto end;
  195. }
  196. in = bio_open_default(infile, 'r', informat);
  197. if (in == NULL)
  198. goto end;
  199. BIO_printf(bio_err, "read EC key\n");
  200. if (informat == FORMAT_ASN1) {
  201. if (pubin)
  202. eckey = d2i_EC_PUBKEY_bio(in, NULL);
  203. else
  204. eckey = d2i_ECPrivateKey_bio(in, NULL);
  205. } else {
  206. if (pubin)
  207. eckey = PEM_read_bio_EC_PUBKEY(in, NULL, NULL, NULL);
  208. else
  209. eckey = PEM_read_bio_ECPrivateKey(in, NULL, NULL, passin);
  210. }
  211. if (eckey == NULL) {
  212. BIO_printf(bio_err, "unable to load Key\n");
  213. ERR_print_errors(bio_err);
  214. goto end;
  215. }
  216. out = bio_open_owner(outfile, outformat, private);
  217. if (out == NULL)
  218. goto end;
  219. group = EC_KEY_get0_group(eckey);
  220. if (new_form)
  221. EC_KEY_set_conv_form(eckey, form);
  222. if (new_asn1_flag)
  223. EC_KEY_set_asn1_flag(eckey, asn1_flag);
  224. if (text) {
  225. assert(pubin || private);
  226. if (!EC_KEY_print(out, eckey, 0)) {
  227. perror(outfile);
  228. ERR_print_errors(bio_err);
  229. goto end;
  230. }
  231. }
  232. if (noout) {
  233. ret = 0;
  234. goto end;
  235. }
  236. BIO_printf(bio_err, "writing EC key\n");
  237. if (outformat == FORMAT_ASN1) {
  238. if (param_out)
  239. i = i2d_ECPKParameters_bio(out, group);
  240. else if (pubin || pubout)
  241. i = i2d_EC_PUBKEY_bio(out, eckey);
  242. else {
  243. assert(private);
  244. i = i2d_ECPrivateKey_bio(out, eckey);
  245. }
  246. } else {
  247. if (param_out)
  248. i = PEM_write_bio_ECPKParameters(out, group);
  249. else if (pubin || pubout)
  250. i = PEM_write_bio_EC_PUBKEY(out, eckey);
  251. else {
  252. assert(private);
  253. i = PEM_write_bio_ECPrivateKey(out, eckey, enc,
  254. NULL, 0, NULL, passout);
  255. }
  256. }
  257. if (!i) {
  258. BIO_printf(bio_err, "unable to write private key\n");
  259. ERR_print_errors(bio_err);
  260. } else
  261. ret = 0;
  262. end:
  263. BIO_free(in);
  264. BIO_free_all(out);
  265. EC_KEY_free(eckey);
  266. OPENSSL_free(passin);
  267. OPENSSL_free(passout);
  268. return (ret);
  269. }
  270. #else /* !OPENSSL_NO_EC */
  271. # if PEDANTIC
  272. static void *dummy = &dummy;
  273. # endif
  274. #endif