ec.c 9.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294
  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_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. 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. ciphername = opt_unknown();
  126. break;
  127. case OPT_CONV_FORM:
  128. point_format = opt_arg();
  129. if (!opt_string(point_format, point_format_options))
  130. goto opthelp;
  131. break;
  132. case OPT_PARAM_ENC:
  133. asn1_encoding = opt_arg();
  134. if (!opt_string(asn1_encoding, asn1_encoding_options))
  135. goto opthelp;
  136. break;
  137. case OPT_NO_PUBLIC:
  138. no_public = 1;
  139. break;
  140. case OPT_CHECK:
  141. check = 1;
  142. break;
  143. case OPT_PROV_CASES:
  144. if (!opt_provider(o))
  145. goto end;
  146. break;
  147. }
  148. }
  149. /* No extra arguments. */
  150. argc = opt_num_rest();
  151. if (argc != 0)
  152. goto opthelp;
  153. if (ciphername != NULL) {
  154. if (!opt_cipher(ciphername, &enc))
  155. goto opthelp;
  156. }
  157. private = param_out || pubin || pubout ? 0 : 1;
  158. if (text && !pubin)
  159. private = 1;
  160. if (!app_passwd(passinarg, passoutarg, &passin, &passout)) {
  161. BIO_printf(bio_err, "Error getting passwords\n");
  162. goto end;
  163. }
  164. BIO_printf(bio_err, "read EC key\n");
  165. if (pubin)
  166. eckey = load_pubkey(infile, informat, 1, passin, e, "public key");
  167. else
  168. eckey = load_key(infile, informat, 1, passin, e, "private key");
  169. if (eckey == NULL) {
  170. BIO_printf(bio_err, "unable to load Key\n");
  171. goto end;
  172. }
  173. out = bio_open_owner(outfile, outformat, private);
  174. if (out == NULL)
  175. goto end;
  176. if (point_format
  177. && !EVP_PKEY_set_utf8_string_param(
  178. eckey, OSSL_PKEY_PARAM_EC_POINT_CONVERSION_FORMAT,
  179. point_format)) {
  180. BIO_printf(bio_err, "unable to set point conversion format\n");
  181. goto end;
  182. }
  183. if (asn1_encoding != NULL
  184. && !EVP_PKEY_set_utf8_string_param(
  185. eckey, OSSL_PKEY_PARAM_EC_ENCODING, asn1_encoding)) {
  186. BIO_printf(bio_err, "unable to set asn1 encoding format\n");
  187. goto end;
  188. }
  189. if (no_public) {
  190. if (!EVP_PKEY_set_int_param(eckey, OSSL_PKEY_PARAM_EC_INCLUDE_PUBLIC, 0)) {
  191. BIO_printf(bio_err, "unable to disable public key encoding\n");
  192. goto end;
  193. }
  194. } else {
  195. if (!EVP_PKEY_set_int_param(eckey, OSSL_PKEY_PARAM_EC_INCLUDE_PUBLIC, 1)) {
  196. BIO_printf(bio_err, "unable to enable public key encoding\n");
  197. goto end;
  198. }
  199. }
  200. if (text) {
  201. assert(pubin || private);
  202. if ((pubin && EVP_PKEY_print_public(out, eckey, 0, NULL) <= 0)
  203. || (!pubin && EVP_PKEY_print_private(out, eckey, 0, NULL) <= 0)) {
  204. BIO_printf(bio_err, "unable to print EC key\n");
  205. goto end;
  206. }
  207. }
  208. if (check) {
  209. pctx = EVP_PKEY_CTX_new_from_pkey(NULL, eckey, NULL);
  210. if (pctx == NULL) {
  211. BIO_printf(bio_err, "unable to check EC key\n");
  212. goto end;
  213. }
  214. if (!EVP_PKEY_check(pctx))
  215. BIO_printf(bio_err, "EC Key Invalid!\n");
  216. else
  217. BIO_printf(bio_err, "EC Key valid.\n");
  218. ERR_print_errors(bio_err);
  219. }
  220. if (!noout) {
  221. int selection;
  222. const char *output_type = outformat == FORMAT_ASN1 ? "DER" : "PEM";
  223. const char *output_structure = "type-specific";
  224. BIO_printf(bio_err, "writing EC key\n");
  225. if (param_out) {
  226. selection = OSSL_KEYMGMT_SELECT_DOMAIN_PARAMETERS;
  227. } else if (pubin || pubout) {
  228. selection = OSSL_KEYMGMT_SELECT_DOMAIN_PARAMETERS
  229. | OSSL_KEYMGMT_SELECT_PUBLIC_KEY;
  230. output_structure = "SubjectPublicKeyInfo";
  231. } else {
  232. selection = OSSL_KEYMGMT_SELECT_ALL;
  233. assert(private);
  234. }
  235. ectx = OSSL_ENCODER_CTX_new_for_pkey(eckey, selection,
  236. output_type, output_structure,
  237. NULL);
  238. if (enc != NULL) {
  239. OSSL_ENCODER_CTX_set_cipher(ectx, EVP_CIPHER_get0_name(enc), NULL);
  240. /* Default passphrase prompter */
  241. OSSL_ENCODER_CTX_set_passphrase_ui(ectx, get_ui_method(), NULL);
  242. if (passout != NULL)
  243. /* When passout given, override the passphrase prompter */
  244. OSSL_ENCODER_CTX_set_passphrase(ectx,
  245. (const unsigned char *)passout,
  246. strlen(passout));
  247. }
  248. if (!OSSL_ENCODER_to_bio(ectx, out)) {
  249. BIO_printf(bio_err, "unable to write EC key\n");
  250. goto end;
  251. }
  252. }
  253. ret = 0;
  254. end:
  255. if (ret != 0)
  256. ERR_print_errors(bio_err);
  257. BIO_free_all(out);
  258. EVP_PKEY_free(eckey);
  259. EVP_CIPHER_free(enc);
  260. OSSL_ENCODER_CTX_free(ectx);
  261. OSSL_DECODER_CTX_free(dctx);
  262. EVP_PKEY_CTX_free(pctx);
  263. release_engine(e);
  264. if (passin != NULL)
  265. OPENSSL_clear_free(passin, strlen(passin));
  266. if (passout != NULL)
  267. OPENSSL_clear_free(passout, strlen(passout));
  268. return ret;
  269. }