2
0

pkey.c 7.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235
  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. PEM_write_bio_PUBKEY(out, pkey);
  174. } else {
  175. assert(private);
  176. if (traditional)
  177. PEM_write_bio_PrivateKey_traditional(out, pkey, cipher,
  178. NULL, 0, NULL,
  179. passout);
  180. else
  181. PEM_write_bio_PrivateKey(out, pkey, cipher,
  182. NULL, 0, NULL, passout);
  183. }
  184. } else if (outformat == FORMAT_ASN1) {
  185. if (pubout) {
  186. i2d_PUBKEY_bio(out, pkey);
  187. } else {
  188. assert(private);
  189. i2d_PrivateKey_bio(out, pkey);
  190. }
  191. } else {
  192. BIO_printf(bio_err, "Bad format specified for key\n");
  193. goto end;
  194. }
  195. }
  196. if (text) {
  197. if (pubtext) {
  198. EVP_PKEY_print_public(out, pkey, 0, NULL);
  199. } else {
  200. assert(private);
  201. EVP_PKEY_print_private(out, pkey, 0, NULL);
  202. }
  203. }
  204. ret = 0;
  205. end:
  206. if (ret != 0)
  207. ERR_print_errors(bio_err);
  208. EVP_PKEY_free(pkey);
  209. release_engine(e);
  210. BIO_free_all(out);
  211. BIO_free(in);
  212. OPENSSL_free(passin);
  213. OPENSSL_free(passout);
  214. return ret;
  215. }