rsa.c 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345
  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, OPT_TRADITIONAL
  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. {"traditional", OPT_TRADITIONAL, '-',
  57. "Use traditional format for private keys"},
  58. #if !defined(OPENSSL_NO_DSA) && !defined(OPENSSL_NO_RC4)
  59. OPT_SECTION("PVK"),
  60. {"pvk-strong", OPT_PVK_STRONG, '-', "Enable 'Strong' PVK encoding level (default)"},
  61. {"pvk-weak", OPT_PVK_WEAK, '-', "Enable 'Weak' PVK encoding level"},
  62. {"pvk-none", OPT_PVK_NONE, '-', "Don't enforce PVK encoding"},
  63. #endif
  64. OPT_PROV_OPTIONS,
  65. {NULL}
  66. };
  67. int rsa_main(int argc, char **argv)
  68. {
  69. ENGINE *e = NULL;
  70. BIO *out = NULL;
  71. RSA *rsa = NULL;
  72. EVP_PKEY *pkey = NULL;
  73. EVP_PKEY_CTX *pctx;
  74. const EVP_CIPHER *enc = NULL;
  75. char *infile = NULL, *outfile = NULL, *prog;
  76. char *passin = NULL, *passout = NULL, *passinarg = NULL, *passoutarg = NULL;
  77. int i, private = 0;
  78. int informat = FORMAT_PEM, outformat = FORMAT_PEM, text = 0, check = 0;
  79. int noout = 0, modulus = 0, pubin = 0, pubout = 0, ret = 1;
  80. #if !defined(OPENSSL_NO_DSA) && !defined(OPENSSL_NO_RC4)
  81. int pvk_encr = 2;
  82. #endif
  83. OPTION_CHOICE o;
  84. int traditional = 0;
  85. prog = opt_init(argc, argv, rsa_options);
  86. while ((o = opt_next()) != OPT_EOF) {
  87. switch (o) {
  88. case OPT_EOF:
  89. case OPT_ERR:
  90. opthelp:
  91. BIO_printf(bio_err, "%s: Use -help for summary.\n", prog);
  92. goto end;
  93. case OPT_HELP:
  94. opt_help(rsa_options);
  95. ret = 0;
  96. goto end;
  97. case OPT_INFORM:
  98. if (!opt_format(opt_arg(), OPT_FMT_ANY, &informat))
  99. goto opthelp;
  100. break;
  101. case OPT_IN:
  102. infile = opt_arg();
  103. break;
  104. case OPT_OUTFORM:
  105. if (!opt_format(opt_arg(), OPT_FMT_ANY, &outformat))
  106. goto opthelp;
  107. break;
  108. case OPT_OUT:
  109. outfile = opt_arg();
  110. break;
  111. case OPT_PASSIN:
  112. passinarg = opt_arg();
  113. break;
  114. case OPT_PASSOUT:
  115. passoutarg = opt_arg();
  116. break;
  117. case OPT_ENGINE:
  118. e = setup_engine(opt_arg(), 0);
  119. break;
  120. case OPT_PUBIN:
  121. pubin = 1;
  122. break;
  123. case OPT_PUBOUT:
  124. pubout = 1;
  125. break;
  126. case OPT_RSAPUBKEY_IN:
  127. pubin = 2;
  128. break;
  129. case OPT_RSAPUBKEY_OUT:
  130. pubout = 2;
  131. break;
  132. case OPT_PVK_STRONG: /* pvk_encr:= 2 */
  133. case OPT_PVK_WEAK: /* pvk_encr:= 1 */
  134. case OPT_PVK_NONE: /* pvk_encr:= 0 */
  135. #if !defined(OPENSSL_NO_DSA) && !defined(OPENSSL_NO_RC4)
  136. pvk_encr = (o - OPT_PVK_NONE);
  137. #endif
  138. break;
  139. case OPT_NOOUT:
  140. noout = 1;
  141. break;
  142. case OPT_TEXT:
  143. text = 1;
  144. break;
  145. case OPT_MODULUS:
  146. modulus = 1;
  147. break;
  148. case OPT_CHECK:
  149. check = 1;
  150. break;
  151. case OPT_CIPHER:
  152. if (!opt_cipher(opt_unknown(), &enc))
  153. goto opthelp;
  154. break;
  155. case OPT_PROV_CASES:
  156. if (!opt_provider(o))
  157. goto end;
  158. break;
  159. case OPT_TRADITIONAL:
  160. traditional = 1;
  161. break;
  162. }
  163. }
  164. argc = opt_num_rest();
  165. if (argc != 0)
  166. goto opthelp;
  167. private = (text && !pubin) || (!pubout && !noout) ? 1 : 0;
  168. if (!app_passwd(passinarg, passoutarg, &passin, &passout)) {
  169. BIO_printf(bio_err, "Error getting passwords\n");
  170. goto end;
  171. }
  172. if (check && pubin) {
  173. BIO_printf(bio_err, "Only private keys can be checked\n");
  174. goto end;
  175. }
  176. if (pubin) {
  177. int tmpformat = -1;
  178. if (pubin == 2) {
  179. if (informat == FORMAT_PEM)
  180. tmpformat = FORMAT_PEMRSA;
  181. else if (informat == FORMAT_ASN1)
  182. tmpformat = FORMAT_ASN1RSA;
  183. } else {
  184. tmpformat = informat;
  185. }
  186. pkey = load_pubkey(infile, tmpformat, 1, passin, e, "public key");
  187. } else {
  188. pkey = load_key(infile, informat, 1, passin, e, "private key");
  189. }
  190. if (pkey != NULL)
  191. rsa = EVP_PKEY_get1_RSA(pkey);
  192. if (rsa == NULL) {
  193. ERR_print_errors(bio_err);
  194. goto end;
  195. }
  196. out = bio_open_owner(outfile, outformat, private);
  197. if (out == NULL)
  198. goto end;
  199. if (text) {
  200. assert(pubin || private);
  201. if ((pubin && EVP_PKEY_print_public(out, pkey, 0, NULL) <= 0)
  202. || (!pubin && EVP_PKEY_print_private(out, pkey, 0, NULL) <= 0)) {
  203. perror(outfile);
  204. ERR_print_errors(bio_err);
  205. goto end;
  206. }
  207. }
  208. if (modulus) {
  209. const BIGNUM *n;
  210. RSA_get0_key(rsa, &n, NULL, NULL);
  211. BIO_printf(out, "Modulus=");
  212. BN_print(out, n);
  213. BIO_printf(out, "\n");
  214. }
  215. if (check) {
  216. int r;
  217. pctx = EVP_PKEY_CTX_new_from_pkey(NULL, pkey, NULL);
  218. if (pctx == NULL) {
  219. BIO_printf(out, "RSA unable to create PKEY context\n");
  220. ERR_print_errors(bio_err);
  221. goto end;
  222. }
  223. r = EVP_PKEY_check(pctx);
  224. EVP_PKEY_CTX_free(pctx);
  225. if (r == 1) {
  226. BIO_printf(out, "RSA key ok\n");
  227. } else if (r == 0) {
  228. unsigned long err;
  229. while ((err = ERR_peek_error()) != 0 &&
  230. ERR_GET_LIB(err) == ERR_LIB_RSA &&
  231. ERR_GET_REASON(err) != ERR_R_MALLOC_FAILURE) {
  232. BIO_printf(out, "RSA key error: %s\n",
  233. ERR_reason_error_string(err));
  234. ERR_get_error(); /* remove err from error stack */
  235. }
  236. } else if (r == -1) {
  237. ERR_print_errors(bio_err);
  238. goto end;
  239. }
  240. }
  241. if (noout) {
  242. ret = 0;
  243. goto end;
  244. }
  245. BIO_printf(bio_err, "writing RSA key\n");
  246. if (outformat == FORMAT_ASN1) {
  247. if (pubout || pubin) {
  248. if (pubout == 2)
  249. i = i2d_RSAPublicKey_bio(out, rsa);
  250. else
  251. i = i2d_RSA_PUBKEY_bio(out, rsa);
  252. } else {
  253. assert(private);
  254. i = i2d_RSAPrivateKey_bio(out, rsa);
  255. }
  256. } else if (outformat == FORMAT_PEM) {
  257. if (pubout || pubin) {
  258. if (pubout == 2)
  259. i = PEM_write_bio_RSAPublicKey(out, rsa);
  260. else
  261. i = PEM_write_bio_RSA_PUBKEY(out, rsa);
  262. } else {
  263. assert(private);
  264. if (traditional) {
  265. i = PEM_write_bio_PrivateKey_traditional(out, pkey, enc, NULL, 0,
  266. NULL, passout);
  267. } else {
  268. i = PEM_write_bio_PrivateKey(out, pkey,
  269. enc, NULL, 0, NULL, passout);
  270. }
  271. }
  272. #ifndef OPENSSL_NO_DSA
  273. } else if (outformat == FORMAT_MSBLOB || outformat == FORMAT_PVK) {
  274. EVP_PKEY *pk;
  275. pk = EVP_PKEY_new();
  276. if (pk == NULL)
  277. goto end;
  278. EVP_PKEY_set1_RSA(pk, rsa);
  279. if (outformat == FORMAT_PVK) {
  280. if (pubin) {
  281. BIO_printf(bio_err, "PVK form impossible with public key input\n");
  282. EVP_PKEY_free(pk);
  283. goto end;
  284. }
  285. assert(private);
  286. # ifdef OPENSSL_NO_RC4
  287. BIO_printf(bio_err, "PVK format not supported\n");
  288. EVP_PKEY_free(pk);
  289. goto end;
  290. # else
  291. i = i2b_PVK_bio(out, pk, pvk_encr, 0, passout);
  292. # endif
  293. } else if (pubin || pubout) {
  294. i = i2b_PublicKey_bio(out, pk);
  295. } else {
  296. assert(private);
  297. i = i2b_PrivateKey_bio(out, pk);
  298. }
  299. EVP_PKEY_free(pk);
  300. #endif
  301. } else {
  302. BIO_printf(bio_err, "bad output format specified for outfile\n");
  303. goto end;
  304. }
  305. if (i <= 0) {
  306. BIO_printf(bio_err, "unable to write key\n");
  307. ERR_print_errors(bio_err);
  308. } else {
  309. ret = 0;
  310. }
  311. end:
  312. release_engine(e);
  313. BIO_free_all(out);
  314. EVP_PKEY_free(pkey);
  315. RSA_free(rsa);
  316. OPENSSL_free(passin);
  317. OPENSSL_free(passout);
  318. return ret;
  319. }