genrsa.c 7.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265
  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 <string.h>
  12. #include <sys/types.h>
  13. #include <sys/stat.h>
  14. #include "apps.h"
  15. #include "progs.h"
  16. #include <openssl/bio.h>
  17. #include <openssl/err.h>
  18. #include <openssl/bn.h>
  19. #include <openssl/rsa.h>
  20. #include <openssl/evp.h>
  21. #include <openssl/x509.h>
  22. #include <openssl/pem.h>
  23. #include <openssl/rand.h>
  24. #define DEFBITS 2048
  25. #define DEFPRIMES 2
  26. static int verbose = 0;
  27. static int genrsa_cb(EVP_PKEY_CTX *ctx);
  28. typedef enum OPTION_choice {
  29. OPT_ERR = -1, OPT_EOF = 0, OPT_HELP,
  30. #ifndef OPENSSL_NO_DEPRECATED_3_0
  31. OPT_3,
  32. #endif
  33. OPT_F4, OPT_ENGINE,
  34. OPT_OUT, OPT_PASSOUT, OPT_CIPHER, OPT_PRIMES, OPT_VERBOSE,
  35. OPT_R_ENUM, OPT_PROV_ENUM, OPT_TRADITIONAL
  36. } OPTION_CHOICE;
  37. const OPTIONS genrsa_options[] = {
  38. {OPT_HELP_STR, 1, '-', "Usage: %s [options] numbits\n"},
  39. OPT_SECTION("General"),
  40. {"help", OPT_HELP, '-', "Display this summary"},
  41. #ifndef OPENSSL_NO_ENGINE
  42. {"engine", OPT_ENGINE, 's', "Use engine, possibly a hardware device"},
  43. #endif
  44. OPT_SECTION("Input"),
  45. #ifndef OPENSSL_NO_DEPRECATED_3_0
  46. {"3", OPT_3, '-', "(deprecated) Use 3 for the E value"},
  47. #endif
  48. {"F4", OPT_F4, '-', "Use the Fermat number F4 (0x10001) for the E value"},
  49. {"f4", OPT_F4, '-', "Use the Fermat number F4 (0x10001) for the E value"},
  50. OPT_SECTION("Output"),
  51. {"out", OPT_OUT, '>', "Output the key to specified file"},
  52. {"passout", OPT_PASSOUT, 's', "Output file pass phrase source"},
  53. {"primes", OPT_PRIMES, 'p', "Specify number of primes"},
  54. {"verbose", OPT_VERBOSE, '-', "Verbose output"},
  55. {"traditional", OPT_TRADITIONAL, '-',
  56. "Use traditional format for private keys"},
  57. {"", OPT_CIPHER, '-', "Encrypt the output with any supported cipher"},
  58. OPT_R_OPTIONS,
  59. OPT_PROV_OPTIONS,
  60. OPT_PARAMETERS(),
  61. {"numbits", 0, 0, "Size of key in bits"},
  62. {NULL}
  63. };
  64. int genrsa_main(int argc, char **argv)
  65. {
  66. BN_GENCB *cb = BN_GENCB_new();
  67. ENGINE *eng = NULL;
  68. BIGNUM *bn = BN_new();
  69. RSA *rsa;
  70. BIO *out = NULL;
  71. const BIGNUM *e;
  72. EVP_PKEY *pkey = NULL;
  73. EVP_PKEY_CTX *ctx = NULL;
  74. const EVP_CIPHER *enc = NULL;
  75. int ret = 1, num = DEFBITS, private = 0, primes = DEFPRIMES;
  76. unsigned long f4 = RSA_F4;
  77. char *outfile = NULL, *passoutarg = NULL, *passout = NULL;
  78. char *prog, *hexe, *dece;
  79. OPTION_CHOICE o;
  80. int traditional = 0;
  81. if (bn == NULL || cb == NULL)
  82. goto end;
  83. prog = opt_init(argc, argv, genrsa_options);
  84. while ((o = opt_next()) != OPT_EOF) {
  85. switch (o) {
  86. case OPT_EOF:
  87. case OPT_ERR:
  88. opthelp:
  89. BIO_printf(bio_err, "%s: Use -help for summary.\n", prog);
  90. goto end;
  91. case OPT_HELP:
  92. ret = 0;
  93. opt_help(genrsa_options);
  94. goto end;
  95. #ifndef OPENSSL_NO_DEPRECATED_3_0
  96. case OPT_3:
  97. f4 = RSA_3;
  98. break;
  99. #endif
  100. case OPT_F4:
  101. f4 = RSA_F4;
  102. break;
  103. case OPT_OUT:
  104. outfile = opt_arg();
  105. break;
  106. case OPT_ENGINE:
  107. eng = setup_engine(opt_arg(), 0);
  108. break;
  109. case OPT_R_CASES:
  110. if (!opt_rand(o))
  111. goto end;
  112. break;
  113. case OPT_PROV_CASES:
  114. if (!opt_provider(o))
  115. goto end;
  116. break;
  117. case OPT_PASSOUT:
  118. passoutarg = opt_arg();
  119. break;
  120. case OPT_CIPHER:
  121. if (!opt_cipher(opt_unknown(), &enc))
  122. goto end;
  123. break;
  124. case OPT_PRIMES:
  125. if (!opt_int(opt_arg(), &primes))
  126. goto end;
  127. break;
  128. case OPT_VERBOSE:
  129. verbose = 1;
  130. break;
  131. case OPT_TRADITIONAL:
  132. traditional = 1;
  133. break;
  134. }
  135. }
  136. argc = opt_num_rest();
  137. argv = opt_rest();
  138. if (argc == 1) {
  139. if (!opt_int(argv[0], &num) || num <= 0)
  140. goto end;
  141. if (num > OPENSSL_RSA_MAX_MODULUS_BITS)
  142. BIO_printf(bio_err,
  143. "Warning: It is not recommended to use more than %d bit for RSA keys.\n"
  144. " Your key size is %d! Larger key size may behave not as expected.\n",
  145. OPENSSL_RSA_MAX_MODULUS_BITS, num);
  146. } else if (argc > 0) {
  147. BIO_printf(bio_err, "Extra arguments given.\n");
  148. goto opthelp;
  149. }
  150. private = 1;
  151. if (!app_passwd(NULL, passoutarg, NULL, &passout)) {
  152. BIO_printf(bio_err, "Error getting password\n");
  153. goto end;
  154. }
  155. out = bio_open_owner(outfile, FORMAT_PEM, private);
  156. if (out == NULL)
  157. goto end;
  158. if (!init_gen_str(&ctx, "RSA", eng, 0, NULL, NULL))
  159. goto end;
  160. EVP_PKEY_CTX_set_cb(ctx, genrsa_cb);
  161. EVP_PKEY_CTX_set_app_data(ctx, bio_err);
  162. if (EVP_PKEY_CTX_set_rsa_keygen_bits(ctx, num) <= 0) {
  163. BIO_printf(bio_err, "Error setting RSA length\n");
  164. goto end;
  165. }
  166. if (!BN_set_word(bn, f4)) {
  167. BIO_printf(bio_err, "Error allocating RSA public exponent\n");
  168. goto end;
  169. }
  170. if (EVP_PKEY_CTX_set1_rsa_keygen_pubexp(ctx, bn) <= 0) {
  171. BIO_printf(bio_err, "Error setting RSA public exponent\n");
  172. goto end;
  173. }
  174. if (EVP_PKEY_CTX_set_rsa_keygen_primes(ctx, primes) <= 0) {
  175. BIO_printf(bio_err, "Error setting number of primes\n");
  176. goto end;
  177. }
  178. if (verbose)
  179. BIO_printf(bio_err, "Generating RSA private key, %d bit long modulus (%d primes)\n",
  180. num, primes);
  181. if (!EVP_PKEY_keygen(ctx, &pkey)) {
  182. BIO_printf(bio_err, "Error generating RSA key\n");
  183. goto end;
  184. }
  185. if (verbose) {
  186. if ((rsa = EVP_PKEY_get0_RSA(pkey)) != NULL) {
  187. RSA_get0_key(rsa, NULL, &e, NULL);
  188. } else {
  189. BIO_printf(bio_err, "Error cannot access RSA e\n");
  190. goto end;
  191. }
  192. hexe = BN_bn2hex(e);
  193. dece = BN_bn2dec(e);
  194. if (hexe && dece) {
  195. BIO_printf(bio_err, "e is %s (0x%s)\n", dece, hexe);
  196. }
  197. OPENSSL_free(hexe);
  198. OPENSSL_free(dece);
  199. }
  200. if (traditional) {
  201. if (!PEM_write_bio_PrivateKey_traditional(out, pkey, enc, NULL, 0,
  202. NULL, passout))
  203. goto end;
  204. } else {
  205. if (!PEM_write_bio_PrivateKey(out, pkey, enc, NULL, 0, NULL, passout))
  206. goto end;
  207. }
  208. ret = 0;
  209. end:
  210. BN_free(bn);
  211. BN_GENCB_free(cb);
  212. EVP_PKEY_CTX_free(ctx);
  213. EVP_PKEY_free(pkey);
  214. BIO_free_all(out);
  215. release_engine(eng);
  216. OPENSSL_free(passout);
  217. if (ret != 0)
  218. ERR_print_errors(bio_err);
  219. return ret;
  220. }
  221. static int genrsa_cb(EVP_PKEY_CTX *ctx)
  222. {
  223. char c = '*';
  224. BIO *b = EVP_PKEY_CTX_get_app_data(ctx);
  225. int p = EVP_PKEY_CTX_get_keygen_info(ctx, 0);
  226. if (!verbose)
  227. return 1;
  228. if (p == 0)
  229. c = '.';
  230. if (p == 1)
  231. c = '+';
  232. if (p == 2)
  233. c = '*';
  234. if (p == 3)
  235. c = '\n';
  236. BIO_write(b, &c, 1);
  237. (void)BIO_flush(b);
  238. return 1;
  239. }