ec.c 8.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292
  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. /* No extra arguments. */
  159. argc = opt_num_rest();
  160. if (argc != 0)
  161. goto opthelp;
  162. private = param_out || pubin || pubout ? 0 : 1;
  163. if (text && !pubin)
  164. private = 1;
  165. if (!app_passwd(passinarg, passoutarg, &passin, &passout)) {
  166. BIO_printf(bio_err, "Error getting passwords\n");
  167. goto end;
  168. }
  169. if (informat != FORMAT_ENGINE) {
  170. in = bio_open_default(infile, 'r', informat);
  171. if (in == NULL)
  172. goto end;
  173. }
  174. BIO_printf(bio_err, "read EC key\n");
  175. if (informat == FORMAT_ASN1) {
  176. if (pubin)
  177. eckey = d2i_EC_PUBKEY_bio(in, NULL);
  178. else
  179. eckey = d2i_ECPrivateKey_bio(in, NULL);
  180. } else if (informat == FORMAT_ENGINE) {
  181. EVP_PKEY *pkey;
  182. if (pubin)
  183. pkey = load_pubkey(infile, informat, 1, passin, e, "public key");
  184. else
  185. pkey = load_key(infile, informat, 1, passin, e, "private key");
  186. if (pkey != NULL) {
  187. eckey = EVP_PKEY_get1_EC_KEY(pkey);
  188. EVP_PKEY_free(pkey);
  189. }
  190. } else {
  191. if (pubin)
  192. eckey = PEM_read_bio_EC_PUBKEY(in, NULL, NULL, NULL);
  193. else
  194. eckey = PEM_read_bio_ECPrivateKey(in, NULL, NULL, passin);
  195. }
  196. if (eckey == NULL) {
  197. BIO_printf(bio_err, "unable to load Key\n");
  198. ERR_print_errors(bio_err);
  199. goto end;
  200. }
  201. out = bio_open_owner(outfile, outformat, private);
  202. if (out == NULL)
  203. goto end;
  204. group = EC_KEY_get0_group(eckey);
  205. if (new_form)
  206. EC_KEY_set_conv_form(eckey, form);
  207. if (new_asn1_flag)
  208. EC_KEY_set_asn1_flag(eckey, asn1_flag);
  209. if (no_public)
  210. EC_KEY_set_enc_flags(eckey, EC_PKEY_NO_PUBKEY);
  211. if (text) {
  212. assert(pubin || private);
  213. if (!EC_KEY_print(out, eckey, 0)) {
  214. perror(outfile);
  215. ERR_print_errors(bio_err);
  216. goto end;
  217. }
  218. }
  219. if (check) {
  220. if (EC_KEY_check_key(eckey) == 1) {
  221. BIO_printf(bio_err, "EC Key valid.\n");
  222. } else {
  223. BIO_printf(bio_err, "EC Key Invalid!\n");
  224. ERR_print_errors(bio_err);
  225. }
  226. }
  227. if (noout) {
  228. ret = 0;
  229. goto end;
  230. }
  231. BIO_printf(bio_err, "writing EC key\n");
  232. if (outformat == FORMAT_ASN1) {
  233. if (param_out) {
  234. i = i2d_ECPKParameters_bio(out, group);
  235. } else if (pubin || pubout) {
  236. i = i2d_EC_PUBKEY_bio(out, eckey);
  237. } else {
  238. assert(private);
  239. i = i2d_ECPrivateKey_bio(out, eckey);
  240. }
  241. } else {
  242. if (param_out) {
  243. i = PEM_write_bio_ECPKParameters(out, group);
  244. } else if (pubin || pubout) {
  245. i = PEM_write_bio_EC_PUBKEY(out, eckey);
  246. } else {
  247. assert(private);
  248. i = PEM_write_bio_ECPrivateKey(out, eckey, enc,
  249. NULL, 0, NULL, passout);
  250. }
  251. }
  252. if (!i) {
  253. BIO_printf(bio_err, "unable to write private key\n");
  254. ERR_print_errors(bio_err);
  255. } else {
  256. ret = 0;
  257. }
  258. end:
  259. BIO_free(in);
  260. BIO_free_all(out);
  261. EC_KEY_free(eckey);
  262. release_engine(e);
  263. OPENSSL_free(passin);
  264. OPENSSL_free(passout);
  265. return ret;
  266. }