rsa.c 9.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316
  1. /*
  2. * Copyright 1995-2018 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 <openssl/opensslconf.h>
  10. #ifdef OPENSSL_NO_RSA
  11. NON_EMPTY_TRANSLATION_UNIT
  12. #else
  13. # include <stdio.h>
  14. # include <stdlib.h>
  15. # include <string.h>
  16. # include <time.h>
  17. # include "apps.h"
  18. # include "progs.h"
  19. # include <openssl/bio.h>
  20. # include <openssl/err.h>
  21. # include <openssl/rsa.h>
  22. # include <openssl/evp.h>
  23. # include <openssl/x509.h>
  24. # include <openssl/pem.h>
  25. # include <openssl/bn.h>
  26. typedef enum OPTION_choice {
  27. OPT_ERR = -1, OPT_EOF = 0, OPT_HELP,
  28. OPT_INFORM, OPT_OUTFORM, OPT_ENGINE, OPT_IN, OPT_OUT,
  29. OPT_PUBIN, OPT_PUBOUT, OPT_PASSOUT, OPT_PASSIN,
  30. OPT_RSAPUBKEY_IN, OPT_RSAPUBKEY_OUT,
  31. /* Do not change the order here; see case statements below */
  32. OPT_PVK_NONE, OPT_PVK_WEAK, OPT_PVK_STRONG,
  33. OPT_NOOUT, OPT_TEXT, OPT_MODULUS, OPT_CHECK, OPT_CIPHER
  34. } OPTION_CHOICE;
  35. const OPTIONS rsa_options[] = {
  36. {"help", OPT_HELP, '-', "Display this summary"},
  37. {"inform", OPT_INFORM, 'f', "Input format, one of DER PEM"},
  38. {"outform", OPT_OUTFORM, 'f', "Output format, one of DER PEM PVK"},
  39. {"in", OPT_IN, 's', "Input file"},
  40. {"out", OPT_OUT, '>', "Output file"},
  41. {"pubin", OPT_PUBIN, '-', "Expect a public key in input file"},
  42. {"pubout", OPT_PUBOUT, '-', "Output a public key"},
  43. {"passout", OPT_PASSOUT, 's', "Output file pass phrase source"},
  44. {"passin", OPT_PASSIN, 's', "Input file pass phrase source"},
  45. {"RSAPublicKey_in", OPT_RSAPUBKEY_IN, '-', "Input is an RSAPublicKey"},
  46. {"RSAPublicKey_out", OPT_RSAPUBKEY_OUT, '-', "Output is an RSAPublicKey"},
  47. {"noout", OPT_NOOUT, '-', "Don't print key out"},
  48. {"text", OPT_TEXT, '-', "Print the key in text"},
  49. {"modulus", OPT_MODULUS, '-', "Print the RSA key modulus"},
  50. {"check", OPT_CHECK, '-', "Verify key consistency"},
  51. {"", OPT_CIPHER, '-', "Any supported cipher"},
  52. # if !defined(OPENSSL_NO_DSA) && !defined(OPENSSL_NO_RC4)
  53. {"pvk-strong", OPT_PVK_STRONG, '-', "Enable 'Strong' PVK encoding level (default)"},
  54. {"pvk-weak", OPT_PVK_WEAK, '-', "Enable 'Weak' PVK encoding level"},
  55. {"pvk-none", OPT_PVK_NONE, '-', "Don't enforce PVK encoding"},
  56. # endif
  57. # ifndef OPENSSL_NO_ENGINE
  58. {"engine", OPT_ENGINE, 's', "Use engine, possibly a hardware device"},
  59. # endif
  60. {NULL}
  61. };
  62. int rsa_main(int argc, char **argv)
  63. {
  64. ENGINE *e = NULL;
  65. BIO *out = NULL;
  66. RSA *rsa = NULL;
  67. const EVP_CIPHER *enc = NULL;
  68. char *infile = NULL, *outfile = NULL, *prog;
  69. char *passin = NULL, *passout = NULL, *passinarg = NULL, *passoutarg = NULL;
  70. int i, private = 0;
  71. int informat = FORMAT_PEM, outformat = FORMAT_PEM, text = 0, check = 0;
  72. int noout = 0, modulus = 0, pubin = 0, pubout = 0, ret = 1;
  73. # if !defined(OPENSSL_NO_DSA) && !defined(OPENSSL_NO_RC4)
  74. int pvk_encr = 2;
  75. # endif
  76. OPTION_CHOICE o;
  77. prog = opt_init(argc, argv, rsa_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(rsa_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_IN:
  94. infile = opt_arg();
  95. break;
  96. case OPT_OUTFORM:
  97. if (!opt_format(opt_arg(), OPT_FMT_ANY, &outformat))
  98. goto opthelp;
  99. break;
  100. case OPT_OUT:
  101. outfile = opt_arg();
  102. break;
  103. case OPT_PASSIN:
  104. passinarg = opt_arg();
  105. break;
  106. case OPT_PASSOUT:
  107. passoutarg = opt_arg();
  108. break;
  109. case OPT_ENGINE:
  110. e = setup_engine(opt_arg(), 0);
  111. break;
  112. case OPT_PUBIN:
  113. pubin = 1;
  114. break;
  115. case OPT_PUBOUT:
  116. pubout = 1;
  117. break;
  118. case OPT_RSAPUBKEY_IN:
  119. pubin = 2;
  120. break;
  121. case OPT_RSAPUBKEY_OUT:
  122. pubout = 2;
  123. break;
  124. case OPT_PVK_STRONG: /* pvk_encr:= 2 */
  125. case OPT_PVK_WEAK: /* pvk_encr:= 1 */
  126. case OPT_PVK_NONE: /* pvk_encr:= 0 */
  127. # if !defined(OPENSSL_NO_DSA) && !defined(OPENSSL_NO_RC4)
  128. pvk_encr = (o - OPT_PVK_NONE);
  129. # endif
  130. break;
  131. case OPT_NOOUT:
  132. noout = 1;
  133. break;
  134. case OPT_TEXT:
  135. text = 1;
  136. break;
  137. case OPT_MODULUS:
  138. modulus = 1;
  139. break;
  140. case OPT_CHECK:
  141. check = 1;
  142. break;
  143. case OPT_CIPHER:
  144. if (!opt_cipher(opt_unknown(), &enc))
  145. goto opthelp;
  146. break;
  147. }
  148. }
  149. argc = opt_num_rest();
  150. if (argc != 0)
  151. goto opthelp;
  152. private = (text && !pubin) || (!pubout && !noout) ? 1 : 0;
  153. if (!app_passwd(passinarg, passoutarg, &passin, &passout)) {
  154. BIO_printf(bio_err, "Error getting passwords\n");
  155. goto end;
  156. }
  157. if (check && pubin) {
  158. BIO_printf(bio_err, "Only private keys can be checked\n");
  159. goto end;
  160. }
  161. {
  162. EVP_PKEY *pkey;
  163. if (pubin) {
  164. int tmpformat = -1;
  165. if (pubin == 2) {
  166. if (informat == FORMAT_PEM)
  167. tmpformat = FORMAT_PEMRSA;
  168. else if (informat == FORMAT_ASN1)
  169. tmpformat = FORMAT_ASN1RSA;
  170. } else {
  171. tmpformat = informat;
  172. }
  173. pkey = load_pubkey(infile, tmpformat, 1, passin, e, "Public Key");
  174. } else {
  175. pkey = load_key(infile, informat, 1, passin, e, "Private Key");
  176. }
  177. if (pkey != NULL)
  178. rsa = EVP_PKEY_get1_RSA(pkey);
  179. EVP_PKEY_free(pkey);
  180. }
  181. if (rsa == NULL) {
  182. ERR_print_errors(bio_err);
  183. goto end;
  184. }
  185. out = bio_open_owner(outfile, outformat, private);
  186. if (out == NULL)
  187. goto end;
  188. if (text) {
  189. assert(pubin || private);
  190. if (!RSA_print(out, rsa, 0)) {
  191. perror(outfile);
  192. ERR_print_errors(bio_err);
  193. goto end;
  194. }
  195. }
  196. if (modulus) {
  197. const BIGNUM *n;
  198. RSA_get0_key(rsa, &n, NULL, NULL);
  199. BIO_printf(out, "Modulus=");
  200. BN_print(out, n);
  201. BIO_printf(out, "\n");
  202. }
  203. if (check) {
  204. int r = RSA_check_key_ex(rsa, NULL);
  205. if (r == 1) {
  206. BIO_printf(out, "RSA key ok\n");
  207. } else if (r == 0) {
  208. unsigned long err;
  209. while ((err = ERR_peek_error()) != 0 &&
  210. ERR_GET_LIB(err) == ERR_LIB_RSA &&
  211. ERR_GET_FUNC(err) == RSA_F_RSA_CHECK_KEY_EX &&
  212. ERR_GET_REASON(err) != ERR_R_MALLOC_FAILURE) {
  213. BIO_printf(out, "RSA key error: %s\n",
  214. ERR_reason_error_string(err));
  215. ERR_get_error(); /* remove err from error stack */
  216. }
  217. } else if (r == -1) {
  218. ERR_print_errors(bio_err);
  219. goto end;
  220. }
  221. }
  222. if (noout) {
  223. ret = 0;
  224. goto end;
  225. }
  226. BIO_printf(bio_err, "writing RSA key\n");
  227. if (outformat == FORMAT_ASN1) {
  228. if (pubout || pubin) {
  229. if (pubout == 2)
  230. i = i2d_RSAPublicKey_bio(out, rsa);
  231. else
  232. i = i2d_RSA_PUBKEY_bio(out, rsa);
  233. } else {
  234. assert(private);
  235. i = i2d_RSAPrivateKey_bio(out, rsa);
  236. }
  237. } else if (outformat == FORMAT_PEM) {
  238. if (pubout || pubin) {
  239. if (pubout == 2)
  240. i = PEM_write_bio_RSAPublicKey(out, rsa);
  241. else
  242. i = PEM_write_bio_RSA_PUBKEY(out, rsa);
  243. } else {
  244. assert(private);
  245. i = PEM_write_bio_RSAPrivateKey(out, rsa,
  246. enc, NULL, 0, NULL, passout);
  247. }
  248. # ifndef OPENSSL_NO_DSA
  249. } else if (outformat == FORMAT_MSBLOB || outformat == FORMAT_PVK) {
  250. EVP_PKEY *pk;
  251. pk = EVP_PKEY_new();
  252. if (pk == NULL)
  253. goto end;
  254. EVP_PKEY_set1_RSA(pk, rsa);
  255. if (outformat == FORMAT_PVK) {
  256. if (pubin) {
  257. BIO_printf(bio_err, "PVK form impossible with public key input\n");
  258. EVP_PKEY_free(pk);
  259. goto end;
  260. }
  261. assert(private);
  262. # ifdef OPENSSL_NO_RC4
  263. BIO_printf(bio_err, "PVK format not supported\n");
  264. EVP_PKEY_free(pk);
  265. goto end;
  266. # else
  267. i = i2b_PVK_bio(out, pk, pvk_encr, 0, passout);
  268. # endif
  269. } else if (pubin || pubout) {
  270. i = i2b_PublicKey_bio(out, pk);
  271. } else {
  272. assert(private);
  273. i = i2b_PrivateKey_bio(out, pk);
  274. }
  275. EVP_PKEY_free(pk);
  276. # endif
  277. } else {
  278. BIO_printf(bio_err, "bad output format specified for outfile\n");
  279. goto end;
  280. }
  281. if (i <= 0) {
  282. BIO_printf(bio_err, "unable to write key\n");
  283. ERR_print_errors(bio_err);
  284. } else {
  285. ret = 0;
  286. }
  287. end:
  288. release_engine(e);
  289. BIO_free_all(out);
  290. RSA_free(rsa);
  291. OPENSSL_free(passin);
  292. OPENSSL_free(passout);
  293. return ret;
  294. }
  295. #endif