pkey.c 8.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251
  1. /* apps/pkey.c */
  2. /*
  3. * Written by Dr Stephen N Henson (steve@openssl.org) for the OpenSSL project
  4. * 2006
  5. */
  6. /* ====================================================================
  7. * Copyright (c) 2006 The OpenSSL Project. All rights reserved.
  8. *
  9. * Redistribution and use in source and binary forms, with or without
  10. * modification, are permitted provided that the following conditions
  11. * are met:
  12. *
  13. * 1. Redistributions of source code must retain the above copyright
  14. * notice, this list of conditions and the following disclaimer.
  15. *
  16. * 2. Redistributions in binary form must reproduce the above copyright
  17. * notice, this list of conditions and the following disclaimer in
  18. * the documentation and/or other materials provided with the
  19. * distribution.
  20. *
  21. * 3. All advertising materials mentioning features or use of this
  22. * software must display the following acknowledgment:
  23. * "This product includes software developed by the OpenSSL Project
  24. * for use in the OpenSSL Toolkit. (http://www.OpenSSL.org/)"
  25. *
  26. * 4. The names "OpenSSL Toolkit" and "OpenSSL Project" must not be used to
  27. * endorse or promote products derived from this software without
  28. * prior written permission. For written permission, please contact
  29. * licensing@OpenSSL.org.
  30. *
  31. * 5. Products derived from this software may not be called "OpenSSL"
  32. * nor may "OpenSSL" appear in their names without prior written
  33. * permission of the OpenSSL Project.
  34. *
  35. * 6. Redistributions of any form whatsoever must retain the following
  36. * acknowledgment:
  37. * "This product includes software developed by the OpenSSL Project
  38. * for use in the OpenSSL Toolkit (http://www.OpenSSL.org/)"
  39. *
  40. * THIS SOFTWARE IS PROVIDED BY THE OpenSSL PROJECT ``AS IS'' AND ANY
  41. * EXPRESSED OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
  42. * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
  43. * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE OpenSSL PROJECT OR
  44. * ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
  45. * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
  46. * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
  47. * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
  48. * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
  49. * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
  50. * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED
  51. * OF THE POSSIBILITY OF SUCH DAMAGE.
  52. * ====================================================================
  53. *
  54. * This product includes cryptographic software written by Eric Young
  55. * (eay@cryptsoft.com). This product includes software written by Tim
  56. * Hudson (tjh@cryptsoft.com).
  57. *
  58. */
  59. #include <stdio.h>
  60. #include <string.h>
  61. #include "apps.h"
  62. #include <openssl/pem.h>
  63. #include <openssl/err.h>
  64. #include <openssl/evp.h>
  65. #define PROG pkey_main
  66. int MAIN(int, char **);
  67. int MAIN(int argc, char **argv)
  68. {
  69. ENGINE *e = NULL;
  70. char **args, *infile = NULL, *outfile = NULL;
  71. char *passargin = NULL, *passargout = NULL;
  72. BIO *in = NULL, *out = NULL;
  73. const EVP_CIPHER *cipher = NULL;
  74. int informat, outformat;
  75. int pubin = 0, pubout = 0, pubtext = 0, text = 0, noout = 0;
  76. EVP_PKEY *pkey = NULL;
  77. char *passin = NULL, *passout = NULL;
  78. int badarg = 0;
  79. #ifndef OPENSSL_NO_ENGINE
  80. char *engine = NULL;
  81. #endif
  82. int ret = 1;
  83. if (bio_err == NULL)
  84. bio_err = BIO_new_fp(stderr, BIO_NOCLOSE);
  85. if (!load_config(bio_err, NULL))
  86. goto end;
  87. informat = FORMAT_PEM;
  88. outformat = FORMAT_PEM;
  89. ERR_load_crypto_strings();
  90. OpenSSL_add_all_algorithms();
  91. args = argv + 1;
  92. while (!badarg && *args && *args[0] == '-') {
  93. if (!strcmp(*args, "-inform")) {
  94. if (args[1]) {
  95. args++;
  96. informat = str2fmt(*args);
  97. } else
  98. badarg = 1;
  99. } else if (!strcmp(*args, "-outform")) {
  100. if (args[1]) {
  101. args++;
  102. outformat = str2fmt(*args);
  103. } else
  104. badarg = 1;
  105. } else if (!strcmp(*args, "-passin")) {
  106. if (!args[1])
  107. goto bad;
  108. passargin = *(++args);
  109. } else if (!strcmp(*args, "-passout")) {
  110. if (!args[1])
  111. goto bad;
  112. passargout = *(++args);
  113. }
  114. #ifndef OPENSSL_NO_ENGINE
  115. else if (strcmp(*args, "-engine") == 0) {
  116. if (!args[1])
  117. goto bad;
  118. engine = *(++args);
  119. }
  120. #endif
  121. else if (!strcmp(*args, "-in")) {
  122. if (args[1]) {
  123. args++;
  124. infile = *args;
  125. } else
  126. badarg = 1;
  127. } else if (!strcmp(*args, "-out")) {
  128. if (args[1]) {
  129. args++;
  130. outfile = *args;
  131. } else
  132. badarg = 1;
  133. } else if (strcmp(*args, "-pubin") == 0) {
  134. pubin = 1;
  135. pubout = 1;
  136. pubtext = 1;
  137. } else if (strcmp(*args, "-pubout") == 0)
  138. pubout = 1;
  139. else if (strcmp(*args, "-text_pub") == 0) {
  140. pubtext = 1;
  141. text = 1;
  142. } else if (strcmp(*args, "-text") == 0)
  143. text = 1;
  144. else if (strcmp(*args, "-noout") == 0)
  145. noout = 1;
  146. else {
  147. cipher = EVP_get_cipherbyname(*args + 1);
  148. if (!cipher) {
  149. BIO_printf(bio_err, "Unknown cipher %s\n", *args + 1);
  150. badarg = 1;
  151. }
  152. }
  153. args++;
  154. }
  155. if (badarg) {
  156. bad:
  157. BIO_printf(bio_err, "Usage pkey [options]\n");
  158. BIO_printf(bio_err, "where options are\n");
  159. BIO_printf(bio_err, "-in file input file\n");
  160. BIO_printf(bio_err, "-inform X input format (DER or PEM)\n");
  161. BIO_printf(bio_err,
  162. "-passin arg input file pass phrase source\n");
  163. BIO_printf(bio_err, "-outform X output format (DER or PEM)\n");
  164. BIO_printf(bio_err, "-out file output file\n");
  165. BIO_printf(bio_err,
  166. "-passout arg output file pass phrase source\n");
  167. #ifndef OPENSSL_NO_ENGINE
  168. BIO_printf(bio_err,
  169. "-engine e use engine e, possibly a hardware device.\n");
  170. #endif
  171. return 1;
  172. }
  173. #ifndef OPENSSL_NO_ENGINE
  174. e = setup_engine(bio_err, engine, 0);
  175. #endif
  176. if (!app_passwd(bio_err, passargin, passargout, &passin, &passout)) {
  177. BIO_printf(bio_err, "Error getting passwords\n");
  178. goto end;
  179. }
  180. if (outfile) {
  181. if (!(out = BIO_new_file(outfile, "wb"))) {
  182. BIO_printf(bio_err, "Can't open output file %s\n", outfile);
  183. goto end;
  184. }
  185. } else {
  186. out = BIO_new_fp(stdout, BIO_NOCLOSE);
  187. #ifdef OPENSSL_SYS_VMS
  188. {
  189. BIO *tmpbio = BIO_new(BIO_f_linebuffer());
  190. out = BIO_push(tmpbio, out);
  191. }
  192. #endif
  193. }
  194. if (pubin)
  195. pkey = load_pubkey(bio_err, infile, informat, 1,
  196. passin, e, "Public Key");
  197. else
  198. pkey = load_key(bio_err, infile, informat, 1, passin, e, "key");
  199. if (!pkey)
  200. goto end;
  201. if (!noout) {
  202. if (outformat == FORMAT_PEM) {
  203. if (pubout)
  204. PEM_write_bio_PUBKEY(out, pkey);
  205. else
  206. PEM_write_bio_PrivateKey(out, pkey, cipher,
  207. NULL, 0, NULL, passout);
  208. } else if (outformat == FORMAT_ASN1) {
  209. if (pubout)
  210. i2d_PUBKEY_bio(out, pkey);
  211. else
  212. i2d_PrivateKey_bio(out, pkey);
  213. } else {
  214. BIO_printf(bio_err, "Bad format specified for key\n");
  215. goto end;
  216. }
  217. }
  218. if (text) {
  219. if (pubtext)
  220. EVP_PKEY_print_public(out, pkey, 0, NULL);
  221. else
  222. EVP_PKEY_print_private(out, pkey, 0, NULL);
  223. }
  224. ret = 0;
  225. end:
  226. EVP_PKEY_free(pkey);
  227. BIO_free_all(out);
  228. BIO_free(in);
  229. if (passin)
  230. OPENSSL_free(passin);
  231. if (passout)
  232. OPENSSL_free(passout);
  233. return ret;
  234. }