2
0

pkey.c 7.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243
  1. /*
  2. * Copyright 2006-2018 The OpenSSL Project Authors. All Rights Reserved.
  3. *
  4. * Licensed under the OpenSSL license (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. typedef enum OPTION_choice {
  17. OPT_ERR = -1, OPT_EOF = 0, OPT_HELP,
  18. OPT_INFORM, OPT_OUTFORM, OPT_PASSIN, OPT_PASSOUT, OPT_ENGINE,
  19. OPT_IN, OPT_OUT, OPT_PUBIN, OPT_PUBOUT, OPT_TEXT_PUB,
  20. OPT_TEXT, OPT_NOOUT, OPT_MD, OPT_TRADITIONAL, OPT_CHECK, OPT_PUB_CHECK
  21. } OPTION_CHOICE;
  22. const OPTIONS pkey_options[] = {
  23. {"help", OPT_HELP, '-', "Display this summary"},
  24. {"inform", OPT_INFORM, 'f', "Input format (DER or PEM)"},
  25. {"outform", OPT_OUTFORM, 'F', "Output format (DER or PEM)"},
  26. {"passin", OPT_PASSIN, 's', "Input file pass phrase source"},
  27. {"passout", OPT_PASSOUT, 's', "Output file pass phrase source"},
  28. {"in", OPT_IN, 's', "Input key"},
  29. {"out", OPT_OUT, '>', "Output file"},
  30. {"pubin", OPT_PUBIN, '-',
  31. "Read public key from input (default is private key)"},
  32. {"pubout", OPT_PUBOUT, '-', "Output public key, not private"},
  33. {"text_pub", OPT_TEXT_PUB, '-', "Only output public key components"},
  34. {"text", OPT_TEXT, '-', "Output in plaintext as well"},
  35. {"noout", OPT_NOOUT, '-', "Don't output the key"},
  36. {"", OPT_MD, '-', "Any supported cipher"},
  37. {"traditional", OPT_TRADITIONAL, '-',
  38. "Use traditional format for private keys"},
  39. #ifndef OPENSSL_NO_ENGINE
  40. {"engine", OPT_ENGINE, 's', "Use engine, possibly a hardware device"},
  41. #endif
  42. {"check", OPT_CHECK, '-', "Check key consistency"},
  43. {"pubcheck", OPT_PUB_CHECK, '-', "Check public key consistency"},
  44. {NULL}
  45. };
  46. int pkey_main(int argc, char **argv)
  47. {
  48. BIO *in = NULL, *out = NULL;
  49. ENGINE *e = NULL;
  50. EVP_PKEY *pkey = NULL;
  51. const EVP_CIPHER *cipher = NULL;
  52. char *infile = NULL, *outfile = NULL, *passin = NULL, *passout = NULL;
  53. char *passinarg = NULL, *passoutarg = NULL, *prog;
  54. OPTION_CHOICE o;
  55. int informat = FORMAT_PEM, outformat = FORMAT_PEM;
  56. int pubin = 0, pubout = 0, pubtext = 0, text = 0, noout = 0, ret = 1;
  57. int private = 0, traditional = 0, check = 0, pub_check = 0;
  58. prog = opt_init(argc, argv, pkey_options);
  59. while ((o = opt_next()) != OPT_EOF) {
  60. switch (o) {
  61. case OPT_EOF:
  62. case OPT_ERR:
  63. opthelp:
  64. BIO_printf(bio_err, "%s: Use -help for summary.\n", prog);
  65. goto end;
  66. case OPT_HELP:
  67. opt_help(pkey_options);
  68. ret = 0;
  69. goto end;
  70. case OPT_INFORM:
  71. if (!opt_format(opt_arg(), OPT_FMT_ANY, &informat))
  72. goto opthelp;
  73. break;
  74. case OPT_OUTFORM:
  75. if (!opt_format(opt_arg(), OPT_FMT_PEMDER, &outformat))
  76. goto opthelp;
  77. break;
  78. case OPT_PASSIN:
  79. passinarg = opt_arg();
  80. break;
  81. case OPT_PASSOUT:
  82. passoutarg = opt_arg();
  83. break;
  84. case OPT_ENGINE:
  85. e = setup_engine(opt_arg(), 0);
  86. break;
  87. case OPT_IN:
  88. infile = opt_arg();
  89. break;
  90. case OPT_OUT:
  91. outfile = opt_arg();
  92. break;
  93. case OPT_PUBIN:
  94. pubin = pubout = pubtext = 1;
  95. break;
  96. case OPT_PUBOUT:
  97. pubout = 1;
  98. break;
  99. case OPT_TEXT_PUB:
  100. pubtext = text = 1;
  101. break;
  102. case OPT_TEXT:
  103. text = 1;
  104. break;
  105. case OPT_NOOUT:
  106. noout = 1;
  107. break;
  108. case OPT_TRADITIONAL:
  109. traditional = 1;
  110. break;
  111. case OPT_CHECK:
  112. check = 1;
  113. break;
  114. case OPT_PUB_CHECK:
  115. pub_check = 1;
  116. break;
  117. case OPT_MD:
  118. if (!opt_cipher(opt_unknown(), &cipher))
  119. goto opthelp;
  120. }
  121. }
  122. argc = opt_num_rest();
  123. if (argc != 0)
  124. goto opthelp;
  125. private = !noout && !pubout ? 1 : 0;
  126. if (text && !pubtext)
  127. private = 1;
  128. if (!app_passwd(passinarg, passoutarg, &passin, &passout)) {
  129. BIO_printf(bio_err, "Error getting passwords\n");
  130. goto end;
  131. }
  132. out = bio_open_owner(outfile, outformat, private);
  133. if (out == NULL)
  134. goto end;
  135. if (pubin)
  136. pkey = load_pubkey(infile, informat, 1, passin, e, "Public Key");
  137. else
  138. pkey = load_key(infile, informat, 1, passin, e, "key");
  139. if (pkey == NULL)
  140. goto end;
  141. if (check || pub_check) {
  142. int r;
  143. EVP_PKEY_CTX *ctx;
  144. ctx = EVP_PKEY_CTX_new(pkey, e);
  145. if (ctx == NULL) {
  146. ERR_print_errors(bio_err);
  147. goto end;
  148. }
  149. if (check)
  150. r = EVP_PKEY_check(ctx);
  151. else
  152. r = EVP_PKEY_public_check(ctx);
  153. if (r == 1) {
  154. BIO_printf(out, "Key is valid\n");
  155. } else {
  156. /*
  157. * Note: at least for RSA keys if this function returns
  158. * -1, there will be no error reasons.
  159. */
  160. unsigned long err;
  161. BIO_printf(out, "Key is invalid\n");
  162. while ((err = ERR_peek_error()) != 0) {
  163. BIO_printf(out, "Detailed error: %s\n",
  164. ERR_reason_error_string(err));
  165. ERR_get_error(); /* remove err from error stack */
  166. }
  167. }
  168. EVP_PKEY_CTX_free(ctx);
  169. }
  170. if (!noout) {
  171. if (outformat == FORMAT_PEM) {
  172. if (pubout) {
  173. if (!PEM_write_bio_PUBKEY(out, pkey))
  174. goto end;
  175. } else {
  176. assert(private);
  177. if (traditional) {
  178. if (!PEM_write_bio_PrivateKey_traditional(out, pkey, cipher,
  179. NULL, 0, NULL,
  180. passout))
  181. goto end;
  182. } else {
  183. if (!PEM_write_bio_PrivateKey(out, pkey, cipher,
  184. NULL, 0, NULL, passout))
  185. goto end;
  186. }
  187. }
  188. } else if (outformat == FORMAT_ASN1) {
  189. if (pubout) {
  190. if (!i2d_PUBKEY_bio(out, pkey))
  191. goto end;
  192. } else {
  193. assert(private);
  194. if (!i2d_PrivateKey_bio(out, pkey))
  195. goto end;
  196. }
  197. } else {
  198. BIO_printf(bio_err, "Bad format specified for key\n");
  199. goto end;
  200. }
  201. }
  202. if (text) {
  203. if (pubtext) {
  204. if (EVP_PKEY_print_public(out, pkey, 0, NULL) <= 0)
  205. goto end;
  206. } else {
  207. assert(private);
  208. if (EVP_PKEY_print_private(out, pkey, 0, NULL) <= 0)
  209. goto end;
  210. }
  211. }
  212. ret = 0;
  213. end:
  214. if (ret != 0)
  215. ERR_print_errors(bio_err);
  216. EVP_PKEY_free(pkey);
  217. release_engine(e);
  218. BIO_free_all(out);
  219. BIO_free(in);
  220. OPENSSL_free(passin);
  221. OPENSSL_free(passout);
  222. return ret;
  223. }