rsa.c 9.8 KB

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