rsa.c 9.3 KB

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