ec.c 8.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283
  1. /*
  2. * Copyright 2002-2018 The OpenSSL Project Authors. All Rights Reserved.
  3. *
  4. * Licensed under the Apache License 2.0 (the "License"). You may not use
  5. * this file except in compliance with the License. You can obtain a copy
  6. * in the file LICENSE in the source distribution or at
  7. * https://www.openssl.org/source/license.html
  8. */
  9. #include <openssl/opensslconf.h>
  10. #ifdef OPENSSL_NO_EC
  11. NON_EMPTY_TRANSLATION_UNIT
  12. #else
  13. # include <stdio.h>
  14. # include <stdlib.h>
  15. # include <string.h>
  16. # include "apps.h"
  17. # include "progs.h"
  18. # include <openssl/bio.h>
  19. # include <openssl/err.h>
  20. # include <openssl/evp.h>
  21. # include <openssl/pem.h>
  22. static OPT_PAIR conv_forms[] = {
  23. {"compressed", POINT_CONVERSION_COMPRESSED},
  24. {"uncompressed", POINT_CONVERSION_UNCOMPRESSED},
  25. {"hybrid", POINT_CONVERSION_HYBRID},
  26. {NULL}
  27. };
  28. static OPT_PAIR param_enc[] = {
  29. {"named_curve", OPENSSL_EC_NAMED_CURVE},
  30. {"explicit", 0},
  31. {NULL}
  32. };
  33. typedef enum OPTION_choice {
  34. OPT_ERR = -1, OPT_EOF = 0, OPT_HELP,
  35. OPT_INFORM, OPT_OUTFORM, OPT_ENGINE, OPT_IN, OPT_OUT,
  36. OPT_NOOUT, OPT_TEXT, OPT_PARAM_OUT, OPT_PUBIN, OPT_PUBOUT,
  37. OPT_PASSIN, OPT_PASSOUT, OPT_PARAM_ENC, OPT_CONV_FORM, OPT_CIPHER,
  38. OPT_NO_PUBLIC, OPT_CHECK
  39. } OPTION_CHOICE;
  40. const OPTIONS ec_options[] = {
  41. {"help", OPT_HELP, '-', "Display this summary"},
  42. {"in", OPT_IN, 's', "Input file"},
  43. {"inform", OPT_INFORM, 'f', "Input format - DER or PEM"},
  44. {"out", OPT_OUT, '>', "Output file"},
  45. {"outform", OPT_OUTFORM, 'F', "Output format - DER or PEM"},
  46. {"noout", OPT_NOOUT, '-', "Don't print key out"},
  47. {"text", OPT_TEXT, '-', "Print the key"},
  48. {"param_out", OPT_PARAM_OUT, '-', "Print the elliptic curve parameters"},
  49. {"pubin", OPT_PUBIN, '-', "Expect a public key in input file"},
  50. {"pubout", OPT_PUBOUT, '-', "Output public key, not private"},
  51. {"no_public", OPT_NO_PUBLIC, '-', "exclude public key from private key"},
  52. {"check", OPT_CHECK, '-', "check key consistency"},
  53. {"passin", OPT_PASSIN, 's', "Input file pass phrase source"},
  54. {"passout", OPT_PASSOUT, 's', "Output file pass phrase source"},
  55. {"param_enc", OPT_PARAM_ENC, 's',
  56. "Specifies the way the ec parameters are encoded"},
  57. {"conv_form", OPT_CONV_FORM, 's', "Specifies the point conversion form "},
  58. {"", OPT_CIPHER, '-', "Any supported cipher"},
  59. # ifndef OPENSSL_NO_ENGINE
  60. {"engine", OPT_ENGINE, 's', "Use engine, possibly a hardware device"},
  61. # endif
  62. {NULL}
  63. };
  64. int ec_main(int argc, char **argv)
  65. {
  66. BIO *in = NULL, *out = NULL;
  67. ENGINE *e = NULL;
  68. EC_KEY *eckey = NULL;
  69. const EC_GROUP *group;
  70. const EVP_CIPHER *enc = NULL;
  71. point_conversion_form_t form = POINT_CONVERSION_UNCOMPRESSED;
  72. char *infile = NULL, *outfile = NULL, *prog;
  73. char *passin = NULL, *passout = NULL, *passinarg = NULL, *passoutarg = NULL;
  74. OPTION_CHOICE o;
  75. int asn1_flag = OPENSSL_EC_NAMED_CURVE, new_form = 0, new_asn1_flag = 0;
  76. int informat = FORMAT_PEM, outformat = FORMAT_PEM, text = 0, noout = 0;
  77. int pubin = 0, pubout = 0, param_out = 0, i, ret = 1, private = 0;
  78. int no_public = 0, check = 0;
  79. prog = opt_init(argc, argv, ec_options);
  80. while ((o = opt_next()) != OPT_EOF) {
  81. switch (o) {
  82. case OPT_EOF:
  83. case OPT_ERR:
  84. opthelp:
  85. BIO_printf(bio_err, "%s: Use -help for summary.\n", prog);
  86. goto end;
  87. case OPT_HELP:
  88. opt_help(ec_options);
  89. ret = 0;
  90. goto end;
  91. case OPT_INFORM:
  92. if (!opt_format(opt_arg(), OPT_FMT_ANY, &informat))
  93. goto opthelp;
  94. break;
  95. case OPT_IN:
  96. infile = opt_arg();
  97. break;
  98. case OPT_OUTFORM:
  99. if (!opt_format(opt_arg(), OPT_FMT_PEMDER, &outformat))
  100. goto opthelp;
  101. break;
  102. case OPT_OUT:
  103. outfile = opt_arg();
  104. break;
  105. case OPT_NOOUT:
  106. noout = 1;
  107. break;
  108. case OPT_TEXT:
  109. text = 1;
  110. break;
  111. case OPT_PARAM_OUT:
  112. param_out = 1;
  113. break;
  114. case OPT_PUBIN:
  115. pubin = 1;
  116. break;
  117. case OPT_PUBOUT:
  118. pubout = 1;
  119. break;
  120. case OPT_PASSIN:
  121. passinarg = opt_arg();
  122. break;
  123. case OPT_PASSOUT:
  124. passoutarg = opt_arg();
  125. break;
  126. case OPT_ENGINE:
  127. e = setup_engine(opt_arg(), 0);
  128. break;
  129. case OPT_CIPHER:
  130. if (!opt_cipher(opt_unknown(), &enc))
  131. goto opthelp;
  132. break;
  133. case OPT_CONV_FORM:
  134. if (!opt_pair(opt_arg(), conv_forms, &i))
  135. goto opthelp;
  136. new_form = 1;
  137. form = i;
  138. break;
  139. case OPT_PARAM_ENC:
  140. if (!opt_pair(opt_arg(), param_enc, &i))
  141. goto opthelp;
  142. new_asn1_flag = 1;
  143. asn1_flag = i;
  144. break;
  145. case OPT_NO_PUBLIC:
  146. no_public = 1;
  147. break;
  148. case OPT_CHECK:
  149. check = 1;
  150. break;
  151. }
  152. }
  153. argc = opt_num_rest();
  154. if (argc != 0)
  155. goto opthelp;
  156. private = param_out || pubin || pubout ? 0 : 1;
  157. if (text && !pubin)
  158. private = 1;
  159. if (!app_passwd(passinarg, passoutarg, &passin, &passout)) {
  160. BIO_printf(bio_err, "Error getting passwords\n");
  161. goto end;
  162. }
  163. if (informat != FORMAT_ENGINE) {
  164. in = bio_open_default(infile, 'r', informat);
  165. if (in == NULL)
  166. goto end;
  167. }
  168. BIO_printf(bio_err, "read EC key\n");
  169. if (informat == FORMAT_ASN1) {
  170. if (pubin)
  171. eckey = d2i_EC_PUBKEY_bio(in, NULL);
  172. else
  173. eckey = d2i_ECPrivateKey_bio(in, NULL);
  174. } else if (informat == FORMAT_ENGINE) {
  175. EVP_PKEY *pkey;
  176. if (pubin)
  177. pkey = load_pubkey(infile, informat, 1, passin, e, "Public Key");
  178. else
  179. pkey = load_key(infile, informat, 1, passin, e, "Private Key");
  180. if (pkey != NULL) {
  181. eckey = EVP_PKEY_get1_EC_KEY(pkey);
  182. EVP_PKEY_free(pkey);
  183. }
  184. } else {
  185. if (pubin)
  186. eckey = PEM_read_bio_EC_PUBKEY(in, NULL, NULL, NULL);
  187. else
  188. eckey = PEM_read_bio_ECPrivateKey(in, NULL, NULL, passin);
  189. }
  190. if (eckey == NULL) {
  191. BIO_printf(bio_err, "unable to load Key\n");
  192. ERR_print_errors(bio_err);
  193. goto end;
  194. }
  195. out = bio_open_owner(outfile, outformat, private);
  196. if (out == NULL)
  197. goto end;
  198. group = EC_KEY_get0_group(eckey);
  199. if (new_form)
  200. EC_KEY_set_conv_form(eckey, form);
  201. if (new_asn1_flag)
  202. EC_KEY_set_asn1_flag(eckey, asn1_flag);
  203. if (no_public)
  204. EC_KEY_set_enc_flags(eckey, EC_PKEY_NO_PUBKEY);
  205. if (text) {
  206. assert(pubin || private);
  207. if (!EC_KEY_print(out, eckey, 0)) {
  208. perror(outfile);
  209. ERR_print_errors(bio_err);
  210. goto end;
  211. }
  212. }
  213. if (check) {
  214. if (EC_KEY_check_key(eckey) == 1) {
  215. BIO_printf(bio_err, "EC Key valid.\n");
  216. } else {
  217. BIO_printf(bio_err, "EC Key Invalid!\n");
  218. ERR_print_errors(bio_err);
  219. }
  220. }
  221. if (noout) {
  222. ret = 0;
  223. goto end;
  224. }
  225. BIO_printf(bio_err, "writing EC key\n");
  226. if (outformat == FORMAT_ASN1) {
  227. if (param_out) {
  228. i = i2d_ECPKParameters_bio(out, group);
  229. } else if (pubin || pubout) {
  230. i = i2d_EC_PUBKEY_bio(out, eckey);
  231. } else {
  232. assert(private);
  233. i = i2d_ECPrivateKey_bio(out, eckey);
  234. }
  235. } else {
  236. if (param_out) {
  237. i = PEM_write_bio_ECPKParameters(out, group);
  238. } else if (pubin || pubout) {
  239. i = PEM_write_bio_EC_PUBKEY(out, eckey);
  240. } else {
  241. assert(private);
  242. i = PEM_write_bio_ECPrivateKey(out, eckey, enc,
  243. NULL, 0, NULL, passout);
  244. }
  245. }
  246. if (!i) {
  247. BIO_printf(bio_err, "unable to write private key\n");
  248. ERR_print_errors(bio_err);
  249. } else {
  250. ret = 0;
  251. }
  252. end:
  253. BIO_free(in);
  254. BIO_free_all(out);
  255. EC_KEY_free(eckey);
  256. release_engine(e);
  257. OPENSSL_free(passin);
  258. OPENSSL_free(passout);
  259. return ret;
  260. }
  261. #endif