rsa.c 12 KB

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