ec.c 8.9 KB

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