rsa.c 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405
  1. /* Copyright (C) 1995-1998 Eric Young (eay@cryptsoft.com)
  2. * All rights reserved.
  3. *
  4. * This package is an SSL implementation written
  5. * by Eric Young (eay@cryptsoft.com).
  6. * The implementation was written so as to conform with Netscapes SSL.
  7. *
  8. * This library is free for commercial and non-commercial use as long as
  9. * the following conditions are aheared to. The following conditions
  10. * apply to all code found in this distribution, be it the RC4, RSA,
  11. * lhash, DES, etc., code; not just the SSL code. The SSL documentation
  12. * included with this distribution is covered by the same copyright terms
  13. * except that the holder is Tim Hudson (tjh@cryptsoft.com).
  14. *
  15. * Copyright remains Eric Young's, and as such any Copyright notices in
  16. * the code are not to be removed.
  17. * If this package is used in a product, Eric Young should be given attribution
  18. * as the author of the parts of the library used.
  19. * This can be in the form of a textual message at program startup or
  20. * in documentation (online or textual) provided with the package.
  21. *
  22. * Redistribution and use in source and binary forms, with or without
  23. * modification, are permitted provided that the following conditions
  24. * are met:
  25. * 1. Redistributions of source code must retain the copyright
  26. * notice, this list of conditions and the following disclaimer.
  27. * 2. Redistributions in binary form must reproduce the above copyright
  28. * notice, this list of conditions and the following disclaimer in the
  29. * documentation and/or other materials provided with the distribution.
  30. * 3. All advertising materials mentioning features or use of this software
  31. * must display the following acknowledgement:
  32. * "This product includes cryptographic software written by
  33. * Eric Young (eay@cryptsoft.com)"
  34. * The word 'cryptographic' can be left out if the rouines from the library
  35. * being used are not cryptographic related :-).
  36. * 4. If you include any Windows specific code (or a derivative thereof) from
  37. * the apps directory (application code) you must include an acknowledgement:
  38. * "This product includes software written by Tim Hudson (tjh@cryptsoft.com)"
  39. *
  40. * THIS SOFTWARE IS PROVIDED BY ERIC YOUNG ``AS IS'' AND
  41. * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
  42. * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
  43. * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
  44. * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
  45. * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
  46. * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
  47. * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
  48. * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
  49. * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
  50. * SUCH DAMAGE.
  51. *
  52. * The licence and distribution terms for any publically available version or
  53. * derivative of this code cannot be changed. i.e. this code cannot simply be
  54. * copied and put under another distribution licence
  55. * [including the GNU Public Licence.]
  56. */
  57. /* ====================================================================
  58. * Copyright (c) 1999-2015 The OpenSSL Project. All rights reserved.
  59. *
  60. * Redistribution and use in source and binary forms, with or without
  61. * modification, are permitted provided that the following conditions
  62. * are met:
  63. *
  64. * 1. Redistributions of source code must retain the above copyright
  65. * notice, this list of conditions and the following disclaimer.
  66. *
  67. * 2. Redistributions in binary form must reproduce the above copyright
  68. * notice, this list of conditions and the following disclaimer in
  69. * the documentation and/or other materials provided with the
  70. * distribution.
  71. *
  72. * 3. All advertising materials mentioning features or use of this
  73. * software must display the following acknowledgment:
  74. * "This product includes software developed by the OpenSSL Project
  75. * for use in the OpenSSL Toolkit. (http://www.OpenSSL.org/)"
  76. *
  77. * 4. The names "OpenSSL Toolkit" and "OpenSSL Project" must not be used to
  78. * endorse or promote products derived from this software without
  79. * prior written permission. For written permission, please contact
  80. * licensing@OpenSSL.org.
  81. *
  82. * 5. Products derived from this software may not be called "OpenSSL"
  83. * nor may "OpenSSL" appear in their names without prior written
  84. * permission of the OpenSSL Project.
  85. *
  86. * 6. Redistributions of any form whatsoever must retain the following
  87. * acknowledgment:
  88. * "This product includes software developed by the OpenSSL Project
  89. * for use in the OpenSSL Toolkit (http://www.OpenSSL.org/)"
  90. *
  91. * THIS SOFTWARE IS PROVIDED BY THE OpenSSL PROJECT ``AS IS'' AND ANY
  92. * EXPRESSED OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
  93. * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
  94. * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE OpenSSL PROJECT OR
  95. * ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
  96. * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
  97. * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
  98. * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
  99. * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
  100. * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
  101. * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED
  102. * OF THE POSSIBILITY OF SUCH DAMAGE.
  103. * ====================================================================
  104. */
  105. #include <openssl/opensslconf.h>
  106. #ifndef OPENSSL_NO_RSA
  107. # include <stdio.h>
  108. # include <stdlib.h>
  109. # include <string.h>
  110. # include <time.h>
  111. # include "apps.h"
  112. # include <openssl/bio.h>
  113. # include <openssl/err.h>
  114. # include <openssl/rsa.h>
  115. # include <openssl/evp.h>
  116. # include <openssl/x509.h>
  117. # include <openssl/pem.h>
  118. # include <openssl/bn.h>
  119. typedef enum OPTION_choice {
  120. OPT_ERR = -1, OPT_EOF = 0, OPT_HELP,
  121. OPT_INFORM, OPT_OUTFORM, OPT_ENGINE, OPT_IN, OPT_OUT,
  122. OPT_PUBIN, OPT_PUBOUT, OPT_PASSOUT, OPT_PASSIN,
  123. OPT_RSAPUBKEY_IN, OPT_RSAPUBKEY_OUT, OPT_PVK_STRONG, OPT_PVK_WEAK,
  124. OPT_PVK_NONE, OPT_NOOUT, OPT_TEXT, OPT_MODULUS, OPT_CHECK, OPT_CIPHER
  125. } OPTION_CHOICE;
  126. OPTIONS rsa_options[] = {
  127. {"help", OPT_HELP, '-', "Display this summary"},
  128. {"inform", OPT_INFORM, 'f', "Input format, one of DER NET PEM"},
  129. {"outform", OPT_OUTFORM, 'f', "Output format, one of DER NET PEM PVK"},
  130. {"in", OPT_IN, '<', "Input file"},
  131. {"out", OPT_OUT, '>', "Output file"},
  132. {"pubin", OPT_PUBIN, '-', "Expect a public key in input file"},
  133. {"pubout", OPT_PUBOUT, '-', "Output a public key"},
  134. {"passout", OPT_PASSOUT, 's', "Output file pass phrase source"},
  135. {"passin", OPT_PASSIN, 's', "Input file pass phrase source"},
  136. {"RSAPublicKey_in", OPT_RSAPUBKEY_IN, '-', "Input is an RSAPublicKey"},
  137. {"RSAPublicKey_out", OPT_RSAPUBKEY_OUT, '-', "Output is an RSAPublicKey"},
  138. {"noout", OPT_NOOUT, '-', "Don't print key out"},
  139. {"text", OPT_TEXT, '-', "Print the key in text"},
  140. {"modulus", OPT_MODULUS, '-', "Print the RSA key modulus"},
  141. {"check", OPT_CHECK, '-', "Verify key consistency"},
  142. {"", OPT_CIPHER, '-', "Any supported cipher"},
  143. # ifdef OPENSSL_NO_RC4
  144. {"pvk-strong", OPT_PVK_STRONG, '-'},
  145. {"pvk-weak", OPT_PVK_WEAK, '-'},
  146. {"pvk-none", OPT_PVK_NONE, '-'},
  147. # endif
  148. # ifndef OPENSSL_NO_ENGINE
  149. {"engine", OPT_ENGINE, 's', "Use engine, possibly a hardware device"},
  150. # endif
  151. {NULL}
  152. };
  153. int rsa_main(int argc, char **argv)
  154. {
  155. ENGINE *e = NULL;
  156. BIO *out = NULL;
  157. RSA *rsa = NULL;
  158. const EVP_CIPHER *enc = NULL;
  159. char *infile = NULL, *outfile = NULL, *prog;
  160. char *passin = NULL, *passout = NULL, *passinarg = NULL, *passoutarg = NULL;
  161. int i, private = 0;
  162. int informat = FORMAT_PEM, outformat = FORMAT_PEM, text = 0, check = 0;
  163. int noout = 0, modulus = 0, pubin = 0, pubout = 0, pvk_encr = 2, ret = 1;
  164. OPTION_CHOICE o;
  165. prog = opt_init(argc, argv, rsa_options);
  166. while ((o = opt_next()) != OPT_EOF) {
  167. switch (o) {
  168. case OPT_EOF:
  169. case OPT_ERR:
  170. opthelp:
  171. BIO_printf(bio_err, "%s: Use -help for summary.\n", prog);
  172. goto end;
  173. case OPT_HELP:
  174. opt_help(rsa_options);
  175. ret = 0;
  176. goto end;
  177. case OPT_INFORM:
  178. if (!opt_format(opt_arg(), OPT_FMT_ANY, &informat))
  179. goto opthelp;
  180. break;
  181. case OPT_IN:
  182. infile = opt_arg();
  183. break;
  184. case OPT_OUTFORM:
  185. if (!opt_format(opt_arg(), OPT_FMT_ANY, &outformat))
  186. goto opthelp;
  187. break;
  188. case OPT_OUT:
  189. outfile = opt_arg();
  190. break;
  191. case OPT_PASSIN:
  192. passinarg = opt_arg();
  193. break;
  194. case OPT_PASSOUT:
  195. passoutarg = opt_arg();
  196. break;
  197. case OPT_ENGINE:
  198. e = setup_engine(opt_arg(), 0);
  199. break;
  200. case OPT_PUBIN:
  201. pubin = 1;
  202. break;
  203. case OPT_PUBOUT:
  204. pubout = 1;
  205. break;
  206. case OPT_RSAPUBKEY_IN:
  207. pubin = 2;
  208. break;
  209. case OPT_RSAPUBKEY_OUT:
  210. pubout = 2;
  211. break;
  212. #ifndef OPENSSL_NO_RC4
  213. case OPT_PVK_STRONG:
  214. pvk_encr = 2;
  215. break;
  216. case OPT_PVK_WEAK:
  217. pvk_encr = 1;
  218. break;
  219. case OPT_PVK_NONE:
  220. pvk_encr = 0;
  221. break;
  222. #else
  223. case OPT_PVK_STRONG:
  224. case OPT_PVK_WEAK:
  225. case OPT_PVK_NONE:
  226. break;
  227. #endif
  228. case OPT_NOOUT:
  229. noout = 1;
  230. break;
  231. case OPT_TEXT:
  232. text = 1;
  233. break;
  234. case OPT_MODULUS:
  235. modulus = 1;
  236. break;
  237. case OPT_CHECK:
  238. check = 1;
  239. break;
  240. case OPT_CIPHER:
  241. if (!opt_cipher(opt_unknown(), &enc))
  242. goto opthelp;
  243. break;
  244. }
  245. }
  246. argc = opt_num_rest();
  247. argv = opt_rest();
  248. private = (text && !pubin) || (!pubout && !noout) ? 1 : 0;
  249. if (!app_passwd(passinarg, passoutarg, &passin, &passout)) {
  250. BIO_printf(bio_err, "Error getting passwords\n");
  251. goto end;
  252. }
  253. if (check && pubin) {
  254. BIO_printf(bio_err, "Only private keys can be checked\n");
  255. goto end;
  256. }
  257. {
  258. EVP_PKEY *pkey;
  259. if (pubin) {
  260. int tmpformat = -1;
  261. if (pubin == 2) {
  262. if (informat == FORMAT_PEM)
  263. tmpformat = FORMAT_PEMRSA;
  264. else if (informat == FORMAT_ASN1)
  265. tmpformat = FORMAT_ASN1RSA;
  266. } else
  267. tmpformat = informat;
  268. pkey = load_pubkey(infile, tmpformat, 1, passin, e, "Public Key");
  269. } else
  270. pkey = load_key(infile, informat, 1, passin, e, "Private Key");
  271. if (pkey != NULL)
  272. rsa = EVP_PKEY_get1_RSA(pkey);
  273. EVP_PKEY_free(pkey);
  274. }
  275. if (rsa == NULL) {
  276. ERR_print_errors(bio_err);
  277. goto end;
  278. }
  279. out = bio_open_owner(outfile, outformat, private);
  280. if (out == NULL)
  281. goto end;
  282. if (text) {
  283. assert(pubin || private);
  284. if (!RSA_print(out, rsa, 0)) {
  285. perror(outfile);
  286. ERR_print_errors(bio_err);
  287. goto end;
  288. }
  289. }
  290. if (modulus) {
  291. BIO_printf(out, "Modulus=");
  292. BN_print(out, rsa->n);
  293. BIO_printf(out, "\n");
  294. }
  295. if (check) {
  296. int r = RSA_check_key(rsa);
  297. if (r == 1)
  298. BIO_printf(out, "RSA key ok\n");
  299. else if (r == 0) {
  300. unsigned long err;
  301. while ((err = ERR_peek_error()) != 0 &&
  302. ERR_GET_LIB(err) == ERR_LIB_RSA &&
  303. ERR_GET_FUNC(err) == RSA_F_RSA_CHECK_KEY &&
  304. ERR_GET_REASON(err) != ERR_R_MALLOC_FAILURE) {
  305. BIO_printf(out, "RSA key error: %s\n",
  306. ERR_reason_error_string(err));
  307. ERR_get_error(); /* remove e from error stack */
  308. }
  309. }
  310. /* should happen only if r == -1 */
  311. if (r == -1 || ERR_peek_error() != 0) {
  312. ERR_print_errors(bio_err);
  313. goto end;
  314. }
  315. }
  316. if (noout) {
  317. ret = 0;
  318. goto end;
  319. }
  320. BIO_printf(bio_err, "writing RSA key\n");
  321. if (outformat == FORMAT_ASN1) {
  322. if (pubout || pubin) {
  323. if (pubout == 2)
  324. i = i2d_RSAPublicKey_bio(out, rsa);
  325. else
  326. i = i2d_RSA_PUBKEY_bio(out, rsa);
  327. } else {
  328. assert(private);
  329. i = i2d_RSAPrivateKey_bio(out, rsa);
  330. }
  331. }
  332. else if (outformat == FORMAT_PEM) {
  333. if (pubout || pubin) {
  334. if (pubout == 2)
  335. i = PEM_write_bio_RSAPublicKey(out, rsa);
  336. else
  337. i = PEM_write_bio_RSA_PUBKEY(out, rsa);
  338. } else {
  339. assert(private);
  340. i = PEM_write_bio_RSAPrivateKey(out, rsa,
  341. enc, NULL, 0, NULL, passout);
  342. }
  343. # if !defined(OPENSSL_NO_DSA) && !defined(OPENSSL_NO_RC4)
  344. } else if (outformat == FORMAT_MSBLOB || outformat == FORMAT_PVK) {
  345. EVP_PKEY *pk;
  346. pk = EVP_PKEY_new();
  347. EVP_PKEY_set1_RSA(pk, rsa);
  348. if (outformat == FORMAT_PVK) {
  349. if (pubin) {
  350. BIO_printf(bio_err, "PVK form impossible with public key input\n");
  351. EVP_PKEY_free(pk);
  352. goto end;
  353. }
  354. assert(private);
  355. i = i2b_PVK_bio(out, pk, pvk_encr, 0, passout);
  356. } else if (pubin || pubout) {
  357. i = i2b_PublicKey_bio(out, pk);
  358. } else {
  359. assert(private);
  360. i = i2b_PrivateKey_bio(out, pk);
  361. }
  362. EVP_PKEY_free(pk);
  363. # endif
  364. } else {
  365. BIO_printf(bio_err, "bad output format specified for outfile\n");
  366. goto end;
  367. }
  368. if (i <= 0) {
  369. BIO_printf(bio_err, "unable to write key\n");
  370. ERR_print_errors(bio_err);
  371. } else
  372. ret = 0;
  373. end:
  374. BIO_free_all(out);
  375. RSA_free(rsa);
  376. OPENSSL_free(passin);
  377. OPENSSL_free(passout);
  378. return (ret);
  379. }
  380. #else /* !OPENSSL_NO_RSA */
  381. # if PEDANTIC
  382. static void *dummy = &dummy;
  383. # endif
  384. #endif