ec.c 9.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292
  1. /*
  2. * Copyright 2002-2022 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_COMMON,
  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 *out = NULL;
  63. ENGINE *e = NULL;
  64. EVP_CIPHER *enc = NULL;
  65. char *infile = NULL, *outfile = NULL, *ciphername = NULL, *prog;
  66. char *passin = NULL, *passout = NULL, *passinarg = NULL, *passoutarg = NULL;
  67. OPTION_CHOICE o;
  68. int informat = FORMAT_UNDEF, 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. opt_set_unknown_name("cipher");
  75. prog = opt_init(argc, argv, ec_options);
  76. while ((o = opt_next()) != OPT_EOF) {
  77. switch (o) {
  78. case OPT_EOF:
  79. case OPT_ERR:
  80. opthelp:
  81. BIO_printf(bio_err, "%s: Use -help for summary.\n", prog);
  82. goto end;
  83. case OPT_HELP:
  84. opt_help(ec_options);
  85. ret = 0;
  86. goto end;
  87. case OPT_INFORM:
  88. if (!opt_format(opt_arg(), OPT_FMT_ANY, &informat))
  89. goto opthelp;
  90. break;
  91. case OPT_IN:
  92. infile = opt_arg();
  93. break;
  94. case OPT_OUTFORM:
  95. if (!opt_format(opt_arg(), OPT_FMT_PEMDER, &outformat))
  96. goto opthelp;
  97. break;
  98. case OPT_OUT:
  99. outfile = opt_arg();
  100. break;
  101. case OPT_NOOUT:
  102. noout = 1;
  103. break;
  104. case OPT_TEXT:
  105. text = 1;
  106. break;
  107. case OPT_PARAM_OUT:
  108. param_out = 1;
  109. break;
  110. case OPT_PUBIN:
  111. pubin = 1;
  112. break;
  113. case OPT_PUBOUT:
  114. pubout = 1;
  115. break;
  116. case OPT_PASSIN:
  117. passinarg = opt_arg();
  118. break;
  119. case OPT_PASSOUT:
  120. passoutarg = opt_arg();
  121. break;
  122. case OPT_ENGINE:
  123. e = setup_engine(opt_arg(), 0);
  124. break;
  125. case OPT_CIPHER:
  126. ciphername = opt_unknown();
  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. if (!opt_check_rest_arg(NULL))
  152. goto opthelp;
  153. if (!opt_cipher(ciphername, &enc))
  154. goto opthelp;
  155. private = param_out || pubin || pubout ? 0 : 1;
  156. if (text && !pubin)
  157. private = 1;
  158. if (!app_passwd(passinarg, passoutarg, &passin, &passout)) {
  159. BIO_printf(bio_err, "Error getting passwords\n");
  160. goto end;
  161. }
  162. BIO_printf(bio_err, "read EC key\n");
  163. if (pubin)
  164. eckey = load_pubkey(infile, informat, 1, passin, e, "public key");
  165. else
  166. eckey = load_key(infile, informat, 1, passin, e, "private key");
  167. if (eckey == NULL) {
  168. BIO_printf(bio_err, "unable to load Key\n");
  169. goto end;
  170. }
  171. out = bio_open_owner(outfile, outformat, private);
  172. if (out == NULL)
  173. goto end;
  174. if (point_format
  175. && !EVP_PKEY_set_utf8_string_param(
  176. eckey, OSSL_PKEY_PARAM_EC_POINT_CONVERSION_FORMAT,
  177. point_format)) {
  178. BIO_printf(bio_err, "unable to set point conversion format\n");
  179. goto end;
  180. }
  181. if (asn1_encoding != NULL
  182. && !EVP_PKEY_set_utf8_string_param(
  183. eckey, OSSL_PKEY_PARAM_EC_ENCODING, asn1_encoding)) {
  184. BIO_printf(bio_err, "unable to set asn1 encoding format\n");
  185. goto end;
  186. }
  187. if (no_public) {
  188. if (!EVP_PKEY_set_int_param(eckey, OSSL_PKEY_PARAM_EC_INCLUDE_PUBLIC, 0)) {
  189. BIO_printf(bio_err, "unable to disable public key encoding\n");
  190. goto end;
  191. }
  192. } else {
  193. if (!EVP_PKEY_set_int_param(eckey, OSSL_PKEY_PARAM_EC_INCLUDE_PUBLIC, 1)) {
  194. BIO_printf(bio_err, "unable to enable public key encoding\n");
  195. goto end;
  196. }
  197. }
  198. if (text) {
  199. assert(pubin || private);
  200. if ((pubin && EVP_PKEY_print_public(out, eckey, 0, NULL) <= 0)
  201. || (!pubin && EVP_PKEY_print_private(out, eckey, 0, NULL) <= 0)) {
  202. BIO_printf(bio_err, "unable to print EC key\n");
  203. goto end;
  204. }
  205. }
  206. if (check) {
  207. pctx = EVP_PKEY_CTX_new_from_pkey(NULL, eckey, NULL);
  208. if (pctx == NULL) {
  209. BIO_printf(bio_err, "unable to check EC key\n");
  210. goto end;
  211. }
  212. if (EVP_PKEY_check(pctx) <= 0)
  213. BIO_printf(bio_err, "EC Key Invalid!\n");
  214. else
  215. BIO_printf(bio_err, "EC Key valid.\n");
  216. ERR_print_errors(bio_err);
  217. }
  218. if (!noout) {
  219. int selection;
  220. const char *output_type = outformat == FORMAT_ASN1 ? "DER" : "PEM";
  221. const char *output_structure = "type-specific";
  222. BIO_printf(bio_err, "writing EC key\n");
  223. if (param_out) {
  224. selection = OSSL_KEYMGMT_SELECT_DOMAIN_PARAMETERS;
  225. } else if (pubin || pubout) {
  226. selection = OSSL_KEYMGMT_SELECT_DOMAIN_PARAMETERS
  227. | OSSL_KEYMGMT_SELECT_PUBLIC_KEY;
  228. output_structure = "SubjectPublicKeyInfo";
  229. } else {
  230. selection = OSSL_KEYMGMT_SELECT_ALL;
  231. assert(private);
  232. }
  233. ectx = OSSL_ENCODER_CTX_new_for_pkey(eckey, selection,
  234. output_type, output_structure,
  235. NULL);
  236. if (enc != NULL) {
  237. OSSL_ENCODER_CTX_set_cipher(ectx, EVP_CIPHER_get0_name(enc), NULL);
  238. /* Default passphrase prompter */
  239. OSSL_ENCODER_CTX_set_passphrase_ui(ectx, get_ui_method(), NULL);
  240. if (passout != NULL)
  241. /* When passout given, override the passphrase prompter */
  242. OSSL_ENCODER_CTX_set_passphrase(ectx,
  243. (const unsigned char *)passout,
  244. strlen(passout));
  245. }
  246. if (!OSSL_ENCODER_to_bio(ectx, out)) {
  247. BIO_printf(bio_err, "unable to write EC key\n");
  248. goto end;
  249. }
  250. }
  251. ret = 0;
  252. end:
  253. if (ret != 0)
  254. ERR_print_errors(bio_err);
  255. BIO_free_all(out);
  256. EVP_PKEY_free(eckey);
  257. EVP_CIPHER_free(enc);
  258. OSSL_ENCODER_CTX_free(ectx);
  259. OSSL_DECODER_CTX_free(dctx);
  260. EVP_PKEY_CTX_free(pctx);
  261. release_engine(e);
  262. if (passin != NULL)
  263. OPENSSL_clear_free(passin, strlen(passin));
  264. if (passout != NULL)
  265. OPENSSL_clear_free(passout, strlen(passout));
  266. return ret;
  267. }