rsa.c 11 KB

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