rsa.c 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378
  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. * 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_COMMON,
  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. 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_UNDEF, 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 = FORMAT_UNDEF;
  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(bio_err, "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. BIO_printf(bio_err, "RSA key not ok\n");
  247. ERR_print_errors(bio_err);
  248. } else if (r == -1) {
  249. ERR_print_errors(bio_err);
  250. goto end;
  251. }
  252. }
  253. if (noout) {
  254. ret = 0;
  255. goto end;
  256. }
  257. BIO_printf(bio_err, "writing RSA key\n");
  258. /* Choose output type for the format */
  259. if (outformat == FORMAT_ASN1) {
  260. output_type = "DER";
  261. } else if (outformat == FORMAT_PEM) {
  262. output_type = "PEM";
  263. } else if (outformat == FORMAT_MSBLOB) {
  264. output_type = "MSBLOB";
  265. } else if (outformat == FORMAT_PVK) {
  266. if (pubin) {
  267. BIO_printf(bio_err, "PVK form impossible with public key input\n");
  268. goto end;
  269. }
  270. output_type = "PVK";
  271. } else {
  272. BIO_printf(bio_err, "bad output format specified for outfile\n");
  273. goto end;
  274. }
  275. /* Select what you want in the output */
  276. if (pubout || pubin) {
  277. selection = OSSL_KEYMGMT_SELECT_PUBLIC_KEY;
  278. } else {
  279. assert(private);
  280. selection = (OSSL_KEYMGMT_SELECT_KEYPAIR
  281. | OSSL_KEYMGMT_SELECT_ALL_PARAMETERS);
  282. }
  283. /* For DER based output, select the desired output structure */
  284. if (outformat == FORMAT_ASN1 || outformat == FORMAT_PEM) {
  285. if (pubout || pubin) {
  286. if (pubout == 2)
  287. output_structure = "pkcs1"; /* "type-specific" would work too */
  288. else
  289. output_structure = "SubjectPublicKeyInfo";
  290. } else {
  291. assert(private);
  292. if (traditional)
  293. output_structure = "pkcs1"; /* "type-specific" would work too */
  294. else
  295. output_structure = "PrivateKeyInfo";
  296. }
  297. }
  298. /* Now, perform the encoding */
  299. ectx = OSSL_ENCODER_CTX_new_for_pkey(pkey, selection,
  300. output_type, output_structure,
  301. NULL);
  302. if (OSSL_ENCODER_CTX_get_num_encoders(ectx) == 0) {
  303. BIO_printf(bio_err, "%s format not supported\n", output_type);
  304. goto end;
  305. }
  306. /* Passphrase setup */
  307. if (enc != NULL)
  308. OSSL_ENCODER_CTX_set_cipher(ectx, EVP_CIPHER_get0_name(enc), NULL);
  309. /* Default passphrase prompter */
  310. if (enc != NULL || outformat == FORMAT_PVK) {
  311. OSSL_ENCODER_CTX_set_passphrase_ui(ectx, get_ui_method(), NULL);
  312. if (passout != NULL)
  313. /* When passout given, override the passphrase prompter */
  314. OSSL_ENCODER_CTX_set_passphrase(ectx,
  315. (const unsigned char *)passout,
  316. strlen(passout));
  317. }
  318. /* PVK is a bit special... */
  319. if (outformat == FORMAT_PVK) {
  320. OSSL_PARAM params[2] = { OSSL_PARAM_END, OSSL_PARAM_END };
  321. params[0] = OSSL_PARAM_construct_int("encrypt-level", &pvk_encr);
  322. if (!OSSL_ENCODER_CTX_set_params(ectx, params)) {
  323. BIO_printf(bio_err, "invalid PVK encryption level\n");
  324. goto end;
  325. }
  326. }
  327. if (!OSSL_ENCODER_to_bio(ectx, out)) {
  328. BIO_printf(bio_err, "unable to write key\n");
  329. ERR_print_errors(bio_err);
  330. goto end;
  331. }
  332. ret = 0;
  333. end:
  334. OSSL_ENCODER_CTX_free(ectx);
  335. release_engine(e);
  336. BIO_free_all(out);
  337. EVP_PKEY_free(pkey);
  338. EVP_CIPHER_free(enc);
  339. OPENSSL_free(passin);
  340. OPENSSL_free(passout);
  341. return ret;
  342. }