pkey.c 9.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322
  1. /*
  2. * Copyright 2006-2020 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 <stdio.h>
  10. #include <string.h>
  11. #include "apps.h"
  12. #include "progs.h"
  13. #include <openssl/pem.h>
  14. #include <openssl/err.h>
  15. #include <openssl/evp.h>
  16. #ifndef OPENSSL_NO_EC
  17. # include <openssl/ec.h>
  18. static OPT_PAIR ec_conv_forms[] = {
  19. {"compressed", POINT_CONVERSION_COMPRESSED},
  20. {"uncompressed", POINT_CONVERSION_UNCOMPRESSED},
  21. {"hybrid", POINT_CONVERSION_HYBRID},
  22. {NULL}
  23. };
  24. static OPT_PAIR ec_param_enc[] = {
  25. {"named_curve", OPENSSL_EC_NAMED_CURVE},
  26. {"explicit", 0},
  27. {NULL}
  28. };
  29. #endif
  30. typedef enum OPTION_choice {
  31. OPT_ERR = -1, OPT_EOF = 0, OPT_HELP,
  32. OPT_INFORM, OPT_OUTFORM, OPT_PASSIN, OPT_PASSOUT, OPT_ENGINE,
  33. OPT_IN, OPT_OUT, OPT_PUBIN, OPT_PUBOUT, OPT_TEXT_PUB,
  34. OPT_TEXT, OPT_NOOUT, OPT_MD, OPT_TRADITIONAL, OPT_CHECK, OPT_PUB_CHECK,
  35. OPT_EC_PARAM_ENC, OPT_EC_CONV_FORM,
  36. OPT_PROV_ENUM
  37. } OPTION_CHOICE;
  38. const OPTIONS pkey_options[] = {
  39. OPT_SECTION("General"),
  40. {"help", OPT_HELP, '-', "Display this summary"},
  41. #ifndef OPENSSL_NO_ENGINE
  42. {"engine", OPT_ENGINE, 's', "Use engine, possibly a hardware device"},
  43. #endif
  44. {"check", OPT_CHECK, '-', "Check key consistency"},
  45. {"pubcheck", OPT_PUB_CHECK, '-', "Check public key consistency"},
  46. {"", OPT_MD, '-', "Any supported cipher"},
  47. {"ec_param_enc", OPT_EC_PARAM_ENC, 's',
  48. "Specifies the way the ec parameters are encoded"},
  49. {"ec_conv_form", OPT_EC_CONV_FORM, 's',
  50. "Specifies the point conversion form "},
  51. OPT_SECTION("Input"),
  52. {"in", OPT_IN, 's', "Input key"},
  53. {"inform", OPT_INFORM, 'f', "Input format (DER/PEM/P12/ENGINE)"},
  54. {"passin", OPT_PASSIN, 's', "Input file pass phrase source"},
  55. {"pubin", OPT_PUBIN, '-',
  56. "Read public key from input (default is private key)"},
  57. {"traditional", OPT_TRADITIONAL, '-',
  58. "Use traditional format for private keys"},
  59. OPT_SECTION("Output"),
  60. {"outform", OPT_OUTFORM, 'F', "Output format (DER or PEM)"},
  61. {"passout", OPT_PASSOUT, 's', "Output file pass phrase source"},
  62. {"out", OPT_OUT, '>', "Output file"},
  63. {"pubout", OPT_PUBOUT, '-', "Output public key, not private"},
  64. {"text_pub", OPT_TEXT_PUB, '-', "Only output public key components"},
  65. {"text", OPT_TEXT, '-', "Output in plaintext as well"},
  66. {"noout", OPT_NOOUT, '-', "Don't output the key"},
  67. OPT_PROV_OPTIONS,
  68. {NULL}
  69. };
  70. int pkey_main(int argc, char **argv)
  71. {
  72. BIO *in = NULL, *out = NULL;
  73. ENGINE *e = NULL;
  74. EVP_PKEY *pkey = NULL;
  75. const EVP_CIPHER *cipher = NULL;
  76. char *infile = NULL, *outfile = NULL, *passin = NULL, *passout = NULL;
  77. char *passinarg = NULL, *passoutarg = NULL, *prog;
  78. OPTION_CHOICE o;
  79. int informat = FORMAT_PEM, outformat = FORMAT_PEM;
  80. int pubin = 0, pubout = 0, pubtext = 0, text = 0, noout = 0, ret = 1;
  81. int private = 0, traditional = 0, check = 0, pub_check = 0;
  82. #ifndef OPENSSL_NO_EC
  83. EC_KEY *eckey;
  84. int ec_asn1_flag = OPENSSL_EC_NAMED_CURVE, new_ec_asn1_flag = 0;
  85. int i, new_ec_form = 0;
  86. point_conversion_form_t ec_form = POINT_CONVERSION_UNCOMPRESSED;
  87. #endif
  88. prog = opt_init(argc, argv, pkey_options);
  89. while ((o = opt_next()) != OPT_EOF) {
  90. switch (o) {
  91. case OPT_EOF:
  92. case OPT_ERR:
  93. opthelp:
  94. BIO_printf(bio_err, "%s: Use -help for summary.\n", prog);
  95. goto end;
  96. case OPT_HELP:
  97. opt_help(pkey_options);
  98. ret = 0;
  99. goto end;
  100. case OPT_INFORM:
  101. if (!opt_format(opt_arg(), OPT_FMT_ANY, &informat))
  102. goto opthelp;
  103. break;
  104. case OPT_OUTFORM:
  105. if (!opt_format(opt_arg(), OPT_FMT_PEMDER, &outformat))
  106. goto opthelp;
  107. break;
  108. case OPT_PASSIN:
  109. passinarg = opt_arg();
  110. break;
  111. case OPT_PASSOUT:
  112. passoutarg = opt_arg();
  113. break;
  114. case OPT_ENGINE:
  115. e = setup_engine(opt_arg(), 0);
  116. break;
  117. case OPT_IN:
  118. infile = opt_arg();
  119. break;
  120. case OPT_OUT:
  121. outfile = opt_arg();
  122. break;
  123. case OPT_PUBIN:
  124. pubin = pubout = pubtext = 1;
  125. break;
  126. case OPT_PUBOUT:
  127. pubout = 1;
  128. break;
  129. case OPT_TEXT_PUB:
  130. pubtext = text = 1;
  131. break;
  132. case OPT_TEXT:
  133. text = 1;
  134. break;
  135. case OPT_NOOUT:
  136. noout = 1;
  137. break;
  138. case OPT_TRADITIONAL:
  139. traditional = 1;
  140. break;
  141. case OPT_CHECK:
  142. check = 1;
  143. break;
  144. case OPT_PUB_CHECK:
  145. pub_check = 1;
  146. break;
  147. case OPT_MD:
  148. if (!opt_cipher(opt_unknown(), &cipher))
  149. goto opthelp;
  150. break;
  151. case OPT_EC_CONV_FORM:
  152. #ifdef OPENSSL_NO_EC
  153. goto opthelp;
  154. #else
  155. if (!opt_pair(opt_arg(), ec_conv_forms, &i))
  156. goto opthelp;
  157. new_ec_form = 1;
  158. ec_form = i;
  159. break;
  160. #endif
  161. case OPT_EC_PARAM_ENC:
  162. #ifdef OPENSSL_NO_EC
  163. goto opthelp;
  164. #else
  165. if (!opt_pair(opt_arg(), ec_param_enc, &i))
  166. goto opthelp;
  167. new_ec_asn1_flag = 1;
  168. ec_asn1_flag = i;
  169. break;
  170. #endif
  171. case OPT_PROV_CASES:
  172. if (!opt_provider(o))
  173. goto end;
  174. break;
  175. }
  176. }
  177. argc = opt_num_rest();
  178. if (argc != 0)
  179. goto opthelp;
  180. private = !noout && !pubout ? 1 : 0;
  181. if (text && !pubtext)
  182. private = 1;
  183. if (!app_passwd(passinarg, passoutarg, &passin, &passout)) {
  184. BIO_printf(bio_err, "Error getting passwords\n");
  185. goto end;
  186. }
  187. out = bio_open_owner(outfile, outformat, private);
  188. if (out == NULL)
  189. goto end;
  190. if (pubin)
  191. pkey = load_pubkey(infile, informat, 1, passin, e, "Public Key");
  192. else
  193. pkey = load_key(infile, informat, 1, passin, e, "key");
  194. if (pkey == NULL)
  195. goto end;
  196. #ifndef OPENSSL_NO_EC
  197. /*
  198. * TODO: remove this and use a set params call with a 'pkeyopt' command
  199. * line option instead.
  200. */
  201. if (new_ec_form || new_ec_asn1_flag) {
  202. if ((eckey = EVP_PKEY_get0_EC_KEY(pkey)) == NULL) {
  203. ERR_print_errors(bio_err);
  204. goto end;
  205. }
  206. if (new_ec_form)
  207. EC_KEY_set_conv_form(eckey, ec_form);
  208. if (new_ec_asn1_flag)
  209. EC_KEY_set_asn1_flag(eckey, ec_asn1_flag);
  210. }
  211. #endif
  212. if (check || pub_check) {
  213. int r;
  214. EVP_PKEY_CTX *ctx;
  215. ctx = EVP_PKEY_CTX_new(pkey, e);
  216. if (ctx == NULL) {
  217. ERR_print_errors(bio_err);
  218. goto end;
  219. }
  220. if (check)
  221. r = EVP_PKEY_check(ctx);
  222. else
  223. r = EVP_PKEY_public_check(ctx);
  224. if (r == 1) {
  225. BIO_printf(out, "Key is valid\n");
  226. } else {
  227. /*
  228. * Note: at least for RSA keys if this function returns
  229. * -1, there will be no error reasons.
  230. */
  231. unsigned long err;
  232. BIO_printf(out, "Key is invalid\n");
  233. while ((err = ERR_peek_error()) != 0) {
  234. BIO_printf(out, "Detailed error: %s\n",
  235. ERR_reason_error_string(err));
  236. ERR_get_error(); /* remove err from error stack */
  237. }
  238. }
  239. EVP_PKEY_CTX_free(ctx);
  240. }
  241. if (!noout) {
  242. if (outformat == FORMAT_PEM) {
  243. if (pubout) {
  244. if (!PEM_write_bio_PUBKEY(out, pkey))
  245. goto end;
  246. } else {
  247. assert(private);
  248. if (traditional) {
  249. if (!PEM_write_bio_PrivateKey_traditional(out, pkey, cipher,
  250. NULL, 0, NULL,
  251. passout))
  252. goto end;
  253. } else {
  254. if (!PEM_write_bio_PrivateKey(out, pkey, cipher,
  255. NULL, 0, NULL, passout))
  256. goto end;
  257. }
  258. }
  259. } else if (outformat == FORMAT_ASN1) {
  260. if (pubout) {
  261. if (!i2d_PUBKEY_bio(out, pkey))
  262. goto end;
  263. } else {
  264. assert(private);
  265. if (!i2d_PrivateKey_bio(out, pkey))
  266. goto end;
  267. }
  268. } else {
  269. BIO_printf(bio_err, "Bad format specified for key\n");
  270. goto end;
  271. }
  272. }
  273. if (text) {
  274. if (pubtext) {
  275. if (EVP_PKEY_print_public(out, pkey, 0, NULL) <= 0)
  276. goto end;
  277. } else {
  278. assert(private);
  279. if (EVP_PKEY_print_private(out, pkey, 0, NULL) <= 0)
  280. goto end;
  281. }
  282. }
  283. ret = 0;
  284. end:
  285. if (ret != 0)
  286. ERR_print_errors(bio_err);
  287. EVP_PKEY_free(pkey);
  288. release_engine(e);
  289. BIO_free_all(out);
  290. BIO_free(in);
  291. OPENSSL_free(passin);
  292. OPENSSL_free(passout);
  293. return ret;
  294. }