2
0

ec.c 8.1 KB

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