rsa.c 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370
  1. /*
  2. * Copyright 1995-2021 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. #include <openssl/encoder.h>
  24. /*
  25. * TODO: This include is to get OSSL_KEYMGMT_SELECT_*, which feels a bit
  26. * much just for those macros... they might serve better as EVP macros.
  27. */
  28. #include <openssl/core_dispatch.h>
  29. #ifndef OPENSSL_NO_RC4
  30. # define DEFAULT_PVK_ENCR_STRENGTH 2
  31. #else
  32. # define DEFAULT_PVK_ENCR_STRENGTH 0
  33. #endif
  34. typedef enum OPTION_choice {
  35. OPT_ERR = -1, OPT_EOF = 0, OPT_HELP,
  36. OPT_INFORM, OPT_OUTFORM, OPT_ENGINE, OPT_IN, OPT_OUT,
  37. OPT_PUBIN, OPT_PUBOUT, OPT_PASSOUT, OPT_PASSIN,
  38. OPT_RSAPUBKEY_IN, OPT_RSAPUBKEY_OUT,
  39. /* Do not change the order here; see case statements below */
  40. OPT_PVK_NONE, OPT_PVK_WEAK, OPT_PVK_STRONG,
  41. OPT_NOOUT, OPT_TEXT, OPT_MODULUS, OPT_CHECK, OPT_CIPHER,
  42. OPT_PROV_ENUM, OPT_TRADITIONAL
  43. } OPTION_CHOICE;
  44. const OPTIONS rsa_options[] = {
  45. OPT_SECTION("General"),
  46. {"help", OPT_HELP, '-', "Display this summary"},
  47. {"check", OPT_CHECK, '-', "Verify key consistency"},
  48. {"", OPT_CIPHER, '-', "Any supported cipher"},
  49. #ifndef OPENSSL_NO_ENGINE
  50. {"engine", OPT_ENGINE, 's', "Use engine, possibly a hardware device"},
  51. #endif
  52. OPT_SECTION("Input"),
  53. {"in", OPT_IN, 's', "Input file"},
  54. {"inform", OPT_INFORM, 'f', "Input format (DER/PEM/P12/ENGINE"},
  55. {"pubin", OPT_PUBIN, '-', "Expect a public key in input file"},
  56. {"RSAPublicKey_in", OPT_RSAPUBKEY_IN, '-', "Input is an RSAPublicKey"},
  57. {"passin", OPT_PASSIN, 's', "Input file pass phrase source"},
  58. OPT_SECTION("Output"),
  59. {"out", OPT_OUT, '>', "Output file"},
  60. {"outform", OPT_OUTFORM, 'f', "Output format, one of DER PEM PVK"},
  61. {"pubout", OPT_PUBOUT, '-', "Output a public key"},
  62. {"RSAPublicKey_out", OPT_RSAPUBKEY_OUT, '-', "Output is an RSAPublicKey"},
  63. {"passout", OPT_PASSOUT, 's', "Output file pass phrase source"},
  64. {"noout", OPT_NOOUT, '-', "Don't print key out"},
  65. {"text", OPT_TEXT, '-', "Print the key in text"},
  66. {"modulus", OPT_MODULUS, '-', "Print the RSA key modulus"},
  67. {"traditional", OPT_TRADITIONAL, '-',
  68. "Use traditional format for private keys"},
  69. #ifndef OPENSSL_NO_RC4
  70. OPT_SECTION("PVK"),
  71. {"pvk-strong", OPT_PVK_STRONG, '-', "Enable 'Strong' PVK encoding level (default)"},
  72. {"pvk-weak", OPT_PVK_WEAK, '-', "Enable 'Weak' PVK encoding level"},
  73. {"pvk-none", OPT_PVK_NONE, '-', "Don't enforce PVK encoding"},
  74. #endif
  75. OPT_PROV_OPTIONS,
  76. {NULL}
  77. };
  78. int rsa_main(int argc, char **argv)
  79. {
  80. ENGINE *e = NULL;
  81. BIO *out = NULL;
  82. EVP_PKEY *pkey = NULL;
  83. EVP_PKEY_CTX *pctx;
  84. const EVP_CIPHER *enc = NULL;
  85. char *infile = NULL, *outfile = NULL, *ciphername = NULL, *prog;
  86. char *passin = NULL, *passout = NULL, *passinarg = NULL, *passoutarg = NULL;
  87. int private = 0;
  88. int informat = FORMAT_PEM, outformat = FORMAT_PEM, text = 0, check = 0;
  89. int noout = 0, modulus = 0, pubin = 0, pubout = 0, ret = 1;
  90. int pvk_encr = DEFAULT_PVK_ENCR_STRENGTH;
  91. OPTION_CHOICE o;
  92. int traditional = 0;
  93. const char *output_type = NULL;
  94. const char *output_structure = NULL;
  95. int selection = 0;
  96. OSSL_ENCODER_CTX *ectx = NULL;
  97. prog = opt_init(argc, argv, rsa_options);
  98. while ((o = opt_next()) != OPT_EOF) {
  99. switch (o) {
  100. case OPT_EOF:
  101. case OPT_ERR:
  102. opthelp:
  103. BIO_printf(bio_err, "%s: Use -help for summary.\n", prog);
  104. goto end;
  105. case OPT_HELP:
  106. opt_help(rsa_options);
  107. ret = 0;
  108. goto end;
  109. case OPT_INFORM:
  110. if (!opt_format(opt_arg(), OPT_FMT_ANY, &informat))
  111. goto opthelp;
  112. break;
  113. case OPT_IN:
  114. infile = opt_arg();
  115. break;
  116. case OPT_OUTFORM:
  117. if (!opt_format(opt_arg(), OPT_FMT_ANY, &outformat))
  118. goto opthelp;
  119. break;
  120. case OPT_OUT:
  121. outfile = opt_arg();
  122. break;
  123. case OPT_PASSIN:
  124. passinarg = opt_arg();
  125. break;
  126. case OPT_PASSOUT:
  127. passoutarg = opt_arg();
  128. break;
  129. case OPT_ENGINE:
  130. e = setup_engine(opt_arg(), 0);
  131. break;
  132. case OPT_PUBIN:
  133. pubin = 1;
  134. break;
  135. case OPT_PUBOUT:
  136. pubout = 1;
  137. break;
  138. case OPT_RSAPUBKEY_IN:
  139. pubin = 2;
  140. break;
  141. case OPT_RSAPUBKEY_OUT:
  142. pubout = 2;
  143. break;
  144. case OPT_PVK_STRONG: /* pvk_encr:= 2 */
  145. case OPT_PVK_WEAK: /* pvk_encr:= 1 */
  146. case OPT_PVK_NONE: /* pvk_encr:= 0 */
  147. pvk_encr = (o - OPT_PVK_NONE);
  148. break;
  149. case OPT_NOOUT:
  150. noout = 1;
  151. break;
  152. case OPT_TEXT:
  153. text = 1;
  154. break;
  155. case OPT_MODULUS:
  156. modulus = 1;
  157. break;
  158. case OPT_CHECK:
  159. check = 1;
  160. break;
  161. case OPT_CIPHER:
  162. ciphername = opt_unknown();
  163. break;
  164. case OPT_PROV_CASES:
  165. if (!opt_provider(o))
  166. goto end;
  167. break;
  168. case OPT_TRADITIONAL:
  169. traditional = 1;
  170. break;
  171. }
  172. }
  173. /* No extra arguments. */
  174. argc = opt_num_rest();
  175. if (argc != 0)
  176. goto opthelp;
  177. if (ciphername != NULL) {
  178. if (!opt_cipher(ciphername, &enc))
  179. goto opthelp;
  180. }
  181. private = (text && !pubin) || (!pubout && !noout) ? 1 : 0;
  182. if (!app_passwd(passinarg, passoutarg, &passin, &passout)) {
  183. BIO_printf(bio_err, "Error getting passwords\n");
  184. goto end;
  185. }
  186. if (check && pubin) {
  187. BIO_printf(bio_err, "Only private keys can be checked\n");
  188. goto end;
  189. }
  190. if (pubin) {
  191. int tmpformat = -1;
  192. if (pubin == 2) {
  193. if (informat == FORMAT_PEM)
  194. tmpformat = FORMAT_PEMRSA;
  195. else if (informat == FORMAT_ASN1)
  196. tmpformat = FORMAT_ASN1RSA;
  197. } else {
  198. tmpformat = informat;
  199. }
  200. pkey = load_pubkey(infile, tmpformat, 1, passin, e, "public key");
  201. } else {
  202. pkey = load_key(infile, informat, 1, passin, e, "private key");
  203. }
  204. if (pkey == NULL) {
  205. ERR_print_errors(bio_err);
  206. goto end;
  207. }
  208. if (!EVP_PKEY_is_a(pkey, "RSA")) {
  209. BIO_printf(bio_err, "Not an RSA key\n");
  210. goto end;
  211. }
  212. out = bio_open_owner(outfile, outformat, private);
  213. if (out == NULL)
  214. goto end;
  215. if (text) {
  216. assert(pubin || private);
  217. if ((pubin && EVP_PKEY_print_public(out, pkey, 0, NULL) <= 0)
  218. || (!pubin && EVP_PKEY_print_private(out, pkey, 0, NULL) <= 0)) {
  219. perror(outfile);
  220. ERR_print_errors(bio_err);
  221. goto end;
  222. }
  223. }
  224. if (modulus) {
  225. BIGNUM *n = NULL;
  226. /* Every RSA key has an 'n' */
  227. EVP_PKEY_get_bn_param(pkey, "n", &n);
  228. BIO_printf(out, "Modulus=");
  229. BN_print(out, n);
  230. BIO_printf(out, "\n");
  231. BN_free(n);
  232. }
  233. if (check) {
  234. int r;
  235. pctx = EVP_PKEY_CTX_new_from_pkey(NULL, pkey, NULL);
  236. if (pctx == NULL) {
  237. BIO_printf(out, "RSA unable to create PKEY context\n");
  238. ERR_print_errors(bio_err);
  239. goto end;
  240. }
  241. r = EVP_PKEY_check(pctx);
  242. EVP_PKEY_CTX_free(pctx);
  243. if (r == 1) {
  244. BIO_printf(out, "RSA key ok\n");
  245. } else if (r == 0) {
  246. unsigned long err;
  247. while ((err = ERR_peek_error()) != 0 &&
  248. ERR_GET_LIB(err) == ERR_LIB_RSA &&
  249. ERR_GET_REASON(err) != ERR_R_MALLOC_FAILURE) {
  250. BIO_printf(out, "RSA key error: %s\n",
  251. ERR_reason_error_string(err));
  252. ERR_get_error(); /* remove err from error stack */
  253. }
  254. } else if (r == -1) {
  255. ERR_print_errors(bio_err);
  256. goto end;
  257. }
  258. }
  259. if (noout) {
  260. ret = 0;
  261. goto end;
  262. }
  263. BIO_printf(bio_err, "writing RSA key\n");
  264. /* Choose output type for the format */
  265. if (outformat == FORMAT_ASN1) {
  266. output_type = "DER";
  267. } else if (outformat == FORMAT_PEM) {
  268. output_type = "PEM";
  269. } else if (outformat == FORMAT_MSBLOB) {
  270. output_type = "MSBLOB";
  271. } else if (outformat == FORMAT_PVK) {
  272. if (pubin) {
  273. BIO_printf(bio_err, "PVK form impossible with public key input\n");
  274. goto end;
  275. }
  276. output_type = "PVK";
  277. } else {
  278. BIO_printf(bio_err, "bad output format specified for outfile\n");
  279. goto end;
  280. }
  281. /* Select what you want in the output */
  282. if (pubout || pubin) {
  283. selection = OSSL_KEYMGMT_SELECT_PUBLIC_KEY;
  284. } else {
  285. assert(private);
  286. selection = (OSSL_KEYMGMT_SELECT_KEYPAIR
  287. | OSSL_KEYMGMT_SELECT_ALL_PARAMETERS);
  288. }
  289. /* For DER based output, select the desired output structure */
  290. if (outformat == FORMAT_ASN1 || outformat == FORMAT_PEM) {
  291. if (pubout || pubin) {
  292. if (pubout == 2)
  293. output_structure = "pkcs1"; /* "type-specific" would work too */
  294. else
  295. output_structure = "SubjectPublicKeyInfo";
  296. } else {
  297. assert(private);
  298. if (traditional)
  299. output_structure = "pkcs1"; /* "type-specific" would work too */
  300. else
  301. output_structure = "pkcs8";
  302. }
  303. }
  304. /* Now, perform the encoding */
  305. ectx = OSSL_ENCODER_CTX_new_for_pkey(pkey, selection,
  306. output_type, output_structure,
  307. NULL);
  308. if (OSSL_ENCODER_CTX_get_num_encoders(ectx) == 0) {
  309. BIO_printf(bio_err, "%s format not supported\n", output_type);
  310. goto end;
  311. }
  312. /* PVK is a bit special... */
  313. if (outformat == FORMAT_PVK) {
  314. OSSL_PARAM params[2] = { OSSL_PARAM_END, OSSL_PARAM_END };
  315. params[0] = OSSL_PARAM_construct_int("encrypt-level", &pvk_encr);
  316. if (!OSSL_ENCODER_CTX_set_params(ectx, params)) {
  317. BIO_printf(bio_err, "invalid PVK encryption level\n");
  318. goto end;
  319. }
  320. }
  321. if (!OSSL_ENCODER_to_bio(ectx, out)) {
  322. BIO_printf(bio_err, "unable to write key\n");
  323. ERR_print_errors(bio_err);
  324. goto end;
  325. }
  326. ret = 0;
  327. end:
  328. OSSL_ENCODER_CTX_free(ectx);
  329. release_engine(e);
  330. BIO_free_all(out);
  331. EVP_PKEY_free(pkey);
  332. OPENSSL_free(passin);
  333. OPENSSL_free(passout);
  334. return ret;
  335. }