pkey.c 10 KB

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