PageRenderTime 26ms CodeModel.GetById 14ms RepoModel.GetById 0ms app.codeStats 0ms

/deps/openssl/openssl/apps/ec.c

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