2
0

ec.c 9.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290
  1. /*
  2. * Copyright 2002-2023 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 = !pubin && (text || (!param_out && !pubout));
  156. if (!app_passwd(passinarg, passoutarg, &passin, &passout)) {
  157. BIO_printf(bio_err, "Error getting passwords\n");
  158. goto end;
  159. }
  160. BIO_printf(bio_err, "read EC key\n");
  161. if (pubin)
  162. eckey = load_pubkey(infile, informat, 1, passin, e, "public key");
  163. else
  164. eckey = load_key(infile, informat, 1, passin, e, "private key");
  165. if (eckey == NULL) {
  166. BIO_printf(bio_err, "unable to load Key\n");
  167. goto end;
  168. }
  169. out = bio_open_owner(outfile, outformat, private);
  170. if (out == NULL)
  171. goto end;
  172. if (point_format
  173. && !EVP_PKEY_set_utf8_string_param(
  174. eckey, OSSL_PKEY_PARAM_EC_POINT_CONVERSION_FORMAT,
  175. point_format)) {
  176. BIO_printf(bio_err, "unable to set point conversion format\n");
  177. goto end;
  178. }
  179. if (asn1_encoding != NULL
  180. && !EVP_PKEY_set_utf8_string_param(
  181. eckey, OSSL_PKEY_PARAM_EC_ENCODING, asn1_encoding)) {
  182. BIO_printf(bio_err, "unable to set asn1 encoding format\n");
  183. goto end;
  184. }
  185. if (no_public) {
  186. if (!EVP_PKEY_set_int_param(eckey, OSSL_PKEY_PARAM_EC_INCLUDE_PUBLIC, 0)) {
  187. BIO_printf(bio_err, "unable to disable public key encoding\n");
  188. goto end;
  189. }
  190. } else {
  191. if (!EVP_PKEY_set_int_param(eckey, OSSL_PKEY_PARAM_EC_INCLUDE_PUBLIC, 1)) {
  192. BIO_printf(bio_err, "unable to enable public key encoding\n");
  193. goto end;
  194. }
  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) <= 0)
  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_for_pkey(eckey, selection,
  232. output_type, output_structure,
  233. NULL);
  234. if (enc != NULL) {
  235. OSSL_ENCODER_CTX_set_cipher(ectx, EVP_CIPHER_get0_name(enc), NULL);
  236. /* Default passphrase prompter */
  237. OSSL_ENCODER_CTX_set_passphrase_ui(ectx, get_ui_method(), NULL);
  238. if (passout != NULL)
  239. /* When passout given, override the passphrase prompter */
  240. OSSL_ENCODER_CTX_set_passphrase(ectx,
  241. (const unsigned char *)passout,
  242. strlen(passout));
  243. }
  244. if (!OSSL_ENCODER_to_bio(ectx, out)) {
  245. BIO_printf(bio_err, "unable to write EC key\n");
  246. goto end;
  247. }
  248. }
  249. ret = 0;
  250. end:
  251. if (ret != 0)
  252. ERR_print_errors(bio_err);
  253. BIO_free_all(out);
  254. EVP_PKEY_free(eckey);
  255. EVP_CIPHER_free(enc);
  256. OSSL_ENCODER_CTX_free(ectx);
  257. OSSL_DECODER_CTX_free(dctx);
  258. EVP_PKEY_CTX_free(pctx);
  259. release_engine(e);
  260. if (passin != NULL)
  261. OPENSSL_clear_free(passin, strlen(passin));
  262. if (passout != NULL)
  263. OPENSSL_clear_free(passout, strlen(passout));
  264. return ret;
  265. }