rsa.c 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415
  1. /*
  2. * Copyright 1995-2022 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. prog = opt_init(argc, argv, rsa_options);
  126. while ((o = opt_next()) != OPT_EOF) {
  127. switch (o) {
  128. case OPT_EOF:
  129. case OPT_ERR:
  130. opthelp:
  131. BIO_printf(bio_err, "%s: Use -help for summary.\n", prog);
  132. goto end;
  133. case OPT_HELP:
  134. opt_help(rsa_options);
  135. ret = 0;
  136. goto end;
  137. case OPT_INFORM:
  138. if (!opt_format(opt_arg(), OPT_FMT_ANY, &informat))
  139. goto opthelp;
  140. break;
  141. case OPT_IN:
  142. infile = opt_arg();
  143. break;
  144. case OPT_OUTFORM:
  145. if (!opt_format(opt_arg(), OPT_FMT_ANY, &outformat))
  146. goto opthelp;
  147. break;
  148. case OPT_OUT:
  149. outfile = opt_arg();
  150. break;
  151. case OPT_PASSIN:
  152. passinarg = opt_arg();
  153. break;
  154. case OPT_PASSOUT:
  155. passoutarg = opt_arg();
  156. break;
  157. case OPT_ENGINE:
  158. e = setup_engine(opt_arg(), 0);
  159. break;
  160. case OPT_PUBIN:
  161. pubin = 1;
  162. break;
  163. case OPT_PUBOUT:
  164. pubout = 1;
  165. break;
  166. case OPT_RSAPUBKEY_IN:
  167. pubin = 2;
  168. break;
  169. case OPT_RSAPUBKEY_OUT:
  170. pubout = 2;
  171. break;
  172. case OPT_PVK_STRONG: /* pvk_encr:= 2 */
  173. case OPT_PVK_WEAK: /* pvk_encr:= 1 */
  174. case OPT_PVK_NONE: /* pvk_encr:= 0 */
  175. pvk_encr = (o - OPT_PVK_NONE);
  176. break;
  177. case OPT_NOOUT:
  178. noout = 1;
  179. break;
  180. case OPT_TEXT:
  181. text = 1;
  182. break;
  183. case OPT_MODULUS:
  184. modulus = 1;
  185. break;
  186. case OPT_CHECK:
  187. check = 1;
  188. break;
  189. case OPT_CIPHER:
  190. ciphername = opt_unknown();
  191. break;
  192. case OPT_PROV_CASES:
  193. if (!opt_provider(o))
  194. goto end;
  195. break;
  196. case OPT_TRADITIONAL:
  197. traditional = 1;
  198. break;
  199. }
  200. }
  201. /* No extra arguments. */
  202. argc = opt_num_rest();
  203. if (argc != 0)
  204. goto opthelp;
  205. if (ciphername != NULL) {
  206. if (!opt_cipher(ciphername, &enc))
  207. goto opthelp;
  208. }
  209. private = (text && !pubin) || (!pubout && !noout) ? 1 : 0;
  210. if (!app_passwd(passinarg, passoutarg, &passin, &passout)) {
  211. BIO_printf(bio_err, "Error getting passwords\n");
  212. goto end;
  213. }
  214. if (check && pubin) {
  215. BIO_printf(bio_err, "Only private keys can be checked\n");
  216. goto end;
  217. }
  218. if (pubin) {
  219. int tmpformat = FORMAT_UNDEF;
  220. if (pubin == 2) {
  221. if (informat == FORMAT_PEM)
  222. tmpformat = FORMAT_PEMRSA;
  223. else if (informat == FORMAT_ASN1)
  224. tmpformat = FORMAT_ASN1RSA;
  225. } else {
  226. tmpformat = informat;
  227. }
  228. pkey = load_pubkey(infile, tmpformat, 1, passin, e, "public key");
  229. } else {
  230. pkey = load_key(infile, informat, 1, passin, e, "private key");
  231. }
  232. if (pkey == NULL) {
  233. ERR_print_errors(bio_err);
  234. goto end;
  235. }
  236. if (!EVP_PKEY_is_a(pkey, "RSA") && !EVP_PKEY_is_a(pkey, "RSA-PSS")) {
  237. BIO_printf(bio_err, "Not an RSA key\n");
  238. goto end;
  239. }
  240. out = bio_open_owner(outfile, outformat, private);
  241. if (out == NULL)
  242. goto end;
  243. if (text) {
  244. assert(pubin || private);
  245. if ((pubin && EVP_PKEY_print_public(out, pkey, 0, NULL) <= 0)
  246. || (!pubin && EVP_PKEY_print_private(out, pkey, 0, NULL) <= 0)) {
  247. perror(outfile);
  248. ERR_print_errors(bio_err);
  249. goto end;
  250. }
  251. }
  252. if (modulus) {
  253. BIGNUM *n = NULL;
  254. /* Every RSA key has an 'n' */
  255. EVP_PKEY_get_bn_param(pkey, "n", &n);
  256. BIO_printf(out, "Modulus=");
  257. BN_print(out, n);
  258. BIO_printf(out, "\n");
  259. BN_free(n);
  260. }
  261. if (check) {
  262. int r;
  263. pctx = EVP_PKEY_CTX_new_from_pkey(NULL, pkey, NULL);
  264. if (pctx == NULL) {
  265. BIO_printf(bio_err, "RSA unable to create PKEY context\n");
  266. ERR_print_errors(bio_err);
  267. goto end;
  268. }
  269. r = EVP_PKEY_check(pctx);
  270. EVP_PKEY_CTX_free(pctx);
  271. if (r == 1) {
  272. BIO_printf(out, "RSA key ok\n");
  273. } else if (r == 0) {
  274. BIO_printf(bio_err, "RSA key not ok\n");
  275. ERR_print_errors(bio_err);
  276. } else if (r < 0) {
  277. ERR_print_errors(bio_err);
  278. goto end;
  279. }
  280. }
  281. if (noout) {
  282. ret = 0;
  283. goto end;
  284. }
  285. BIO_printf(bio_err, "writing RSA key\n");
  286. /* Choose output type for the format */
  287. if (outformat == FORMAT_ASN1) {
  288. output_type = "DER";
  289. } else if (outformat == FORMAT_PEM) {
  290. output_type = "PEM";
  291. } else if (outformat == FORMAT_MSBLOB) {
  292. output_type = "MSBLOB";
  293. } else if (outformat == FORMAT_PVK) {
  294. if (pubin) {
  295. BIO_printf(bio_err, "PVK form impossible with public key input\n");
  296. goto end;
  297. }
  298. output_type = "PVK";
  299. } else {
  300. BIO_printf(bio_err, "bad output format specified for outfile\n");
  301. goto end;
  302. }
  303. /* Select what you want in the output */
  304. if (pubout || pubin) {
  305. selection = OSSL_KEYMGMT_SELECT_PUBLIC_KEY;
  306. } else {
  307. assert(private);
  308. selection = (OSSL_KEYMGMT_SELECT_KEYPAIR
  309. | OSSL_KEYMGMT_SELECT_ALL_PARAMETERS);
  310. }
  311. /* For DER based output, select the desired output structure */
  312. if (outformat == FORMAT_ASN1 || outformat == FORMAT_PEM) {
  313. if (pubout || pubin) {
  314. if (pubout == 2)
  315. output_structure = "pkcs1"; /* "type-specific" would work too */
  316. else
  317. output_structure = "SubjectPublicKeyInfo";
  318. } else {
  319. assert(private);
  320. if (traditional)
  321. output_structure = "pkcs1"; /* "type-specific" would work too */
  322. else
  323. output_structure = "PrivateKeyInfo";
  324. }
  325. }
  326. /* Now, perform the encoding */
  327. ectx = OSSL_ENCODER_CTX_new_for_pkey(pkey, selection,
  328. output_type, output_structure,
  329. NULL);
  330. if (OSSL_ENCODER_CTX_get_num_encoders(ectx) == 0) {
  331. if ((!pubout && !pubin)
  332. || !try_legacy_encoding(pkey, outformat, pubout, out))
  333. BIO_printf(bio_err, "%s format not supported\n", output_type);
  334. else
  335. ret = 0;
  336. goto end;
  337. }
  338. /* Passphrase setup */
  339. if (enc != NULL)
  340. OSSL_ENCODER_CTX_set_cipher(ectx, EVP_CIPHER_get0_name(enc), NULL);
  341. /* Default passphrase prompter */
  342. if (enc != NULL || outformat == FORMAT_PVK) {
  343. OSSL_ENCODER_CTX_set_passphrase_ui(ectx, get_ui_method(), NULL);
  344. if (passout != NULL)
  345. /* When passout given, override the passphrase prompter */
  346. OSSL_ENCODER_CTX_set_passphrase(ectx,
  347. (const unsigned char *)passout,
  348. strlen(passout));
  349. }
  350. /* PVK is a bit special... */
  351. if (outformat == FORMAT_PVK) {
  352. OSSL_PARAM params[2] = { OSSL_PARAM_END, OSSL_PARAM_END };
  353. params[0] = OSSL_PARAM_construct_int("encrypt-level", &pvk_encr);
  354. if (!OSSL_ENCODER_CTX_set_params(ectx, params)) {
  355. BIO_printf(bio_err, "invalid PVK encryption level\n");
  356. goto end;
  357. }
  358. }
  359. if (!OSSL_ENCODER_to_bio(ectx, out)) {
  360. BIO_printf(bio_err, "unable to write key\n");
  361. ERR_print_errors(bio_err);
  362. goto end;
  363. }
  364. ret = 0;
  365. end:
  366. OSSL_ENCODER_CTX_free(ectx);
  367. release_engine(e);
  368. BIO_free_all(out);
  369. EVP_PKEY_free(pkey);
  370. EVP_CIPHER_free(enc);
  371. OPENSSL_free(passin);
  372. OPENSSL_free(passout);
  373. return ret;
  374. }