ec.c 8.4 KB

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