2
0

genrsa.c 6.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251
  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. #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. typedef enum OPTION_choice {
  28. OPT_COMMON,
  29. #ifndef OPENSSL_NO_DEPRECATED_3_0
  30. OPT_3,
  31. #endif
  32. OPT_F4, OPT_ENGINE,
  33. OPT_OUT, OPT_PASSOUT, OPT_CIPHER, OPT_PRIMES, OPT_VERBOSE, OPT_QUIET,
  34. OPT_R_ENUM, OPT_PROV_ENUM, OPT_TRADITIONAL
  35. } OPTION_CHOICE;
  36. const OPTIONS genrsa_options[] = {
  37. {OPT_HELP_STR, 1, '-', "Usage: %s [options] numbits\n"},
  38. OPT_SECTION("General"),
  39. {"help", OPT_HELP, '-', "Display this summary"},
  40. #ifndef OPENSSL_NO_ENGINE
  41. {"engine", OPT_ENGINE, 's', "Use engine, possibly a hardware device"},
  42. #endif
  43. OPT_SECTION("Input"),
  44. #ifndef OPENSSL_NO_DEPRECATED_3_0
  45. {"3", OPT_3, '-', "(deprecated) Use 3 for the E value"},
  46. #endif
  47. {"F4", OPT_F4, '-', "Use the Fermat number F4 (0x10001) for the E value"},
  48. {"f4", OPT_F4, '-', "Use the Fermat number F4 (0x10001) for the E value"},
  49. OPT_SECTION("Output"),
  50. {"out", OPT_OUT, '>', "Output the key to specified file"},
  51. {"passout", OPT_PASSOUT, 's', "Output file pass phrase source"},
  52. {"primes", OPT_PRIMES, 'p', "Specify number of primes"},
  53. {"verbose", OPT_VERBOSE, '-', "Verbose output"},
  54. {"quiet", OPT_QUIET, '-', "Terse 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. BIO *out = NULL;
  70. EVP_PKEY *pkey = NULL;
  71. EVP_PKEY_CTX *ctx = NULL;
  72. EVP_CIPHER *enc = NULL;
  73. int ret = 1, num = DEFBITS, private = 0, primes = DEFPRIMES;
  74. unsigned long f4 = RSA_F4;
  75. char *outfile = NULL, *passoutarg = NULL, *passout = NULL;
  76. char *prog, *hexe, *dece, *ciphername = NULL;
  77. OPTION_CHOICE o;
  78. int traditional = 0;
  79. if (bn == NULL || cb == NULL)
  80. goto end;
  81. opt_set_unknown_name("cipher");
  82. prog = opt_init(argc, argv, genrsa_options);
  83. while ((o = opt_next()) != OPT_EOF) {
  84. switch (o) {
  85. case OPT_EOF:
  86. case OPT_ERR:
  87. opthelp:
  88. BIO_printf(bio_err, "%s: Use -help for summary.\n", prog);
  89. goto end;
  90. case OPT_HELP:
  91. ret = 0;
  92. opt_help(genrsa_options);
  93. goto end;
  94. #ifndef OPENSSL_NO_DEPRECATED_3_0
  95. case OPT_3:
  96. f4 = RSA_3;
  97. break;
  98. #endif
  99. case OPT_F4:
  100. f4 = RSA_F4;
  101. break;
  102. case OPT_OUT:
  103. outfile = opt_arg();
  104. break;
  105. case OPT_ENGINE:
  106. eng = setup_engine(opt_arg(), 0);
  107. break;
  108. case OPT_R_CASES:
  109. if (!opt_rand(o))
  110. goto end;
  111. break;
  112. case OPT_PROV_CASES:
  113. if (!opt_provider(o))
  114. goto end;
  115. break;
  116. case OPT_PASSOUT:
  117. passoutarg = opt_arg();
  118. break;
  119. case OPT_CIPHER:
  120. ciphername = opt_unknown();
  121. break;
  122. case OPT_PRIMES:
  123. primes = opt_int_arg();
  124. break;
  125. case OPT_VERBOSE:
  126. verbose = 1;
  127. break;
  128. case OPT_QUIET:
  129. verbose = 0;
  130. break;
  131. case OPT_TRADITIONAL:
  132. traditional = 1;
  133. break;
  134. }
  135. }
  136. /* One optional argument, the bitsize. */
  137. argc = opt_num_rest();
  138. argv = opt_rest();
  139. if (argc == 1) {
  140. if (!opt_int(argv[0], &num) || num <= 0)
  141. goto end;
  142. if (num > OPENSSL_RSA_MAX_MODULUS_BITS)
  143. BIO_printf(bio_err,
  144. "Warning: It is not recommended to use more than %d bit for RSA keys.\n"
  145. " Your key size is %d! Larger key size may behave not as expected.\n",
  146. OPENSSL_RSA_MAX_MODULUS_BITS, num);
  147. } else if (!opt_check_rest_arg(NULL)) {
  148. goto opthelp;
  149. }
  150. if (!app_RAND_load())
  151. goto end;
  152. private = 1;
  153. if (!opt_cipher(ciphername, &enc))
  154. goto end;
  155. if (!app_passwd(NULL, passoutarg, NULL, &passout)) {
  156. BIO_printf(bio_err, "Error getting password\n");
  157. goto end;
  158. }
  159. out = bio_open_owner(outfile, FORMAT_PEM, private);
  160. if (out == NULL)
  161. goto end;
  162. if (!init_gen_str(&ctx, "RSA", eng, 0, app_get0_libctx(),
  163. app_get0_propq()))
  164. goto end;
  165. if (verbose)
  166. EVP_PKEY_CTX_set_cb(ctx, progress_cb);
  167. EVP_PKEY_CTX_set_app_data(ctx, bio_err);
  168. if (EVP_PKEY_CTX_set_rsa_keygen_bits(ctx, num) <= 0) {
  169. BIO_printf(bio_err, "Error setting RSA length\n");
  170. goto end;
  171. }
  172. if (!BN_set_word(bn, f4)) {
  173. BIO_printf(bio_err, "Error allocating RSA public exponent\n");
  174. goto end;
  175. }
  176. if (EVP_PKEY_CTX_set1_rsa_keygen_pubexp(ctx, bn) <= 0) {
  177. BIO_printf(bio_err, "Error setting RSA public exponent\n");
  178. goto end;
  179. }
  180. if (EVP_PKEY_CTX_set_rsa_keygen_primes(ctx, primes) <= 0) {
  181. BIO_printf(bio_err, "Error setting number of primes\n");
  182. goto end;
  183. }
  184. pkey = app_keygen(ctx, "RSA", num, verbose);
  185. if (pkey == NULL)
  186. goto end;
  187. if (verbose) {
  188. BIGNUM *e = NULL;
  189. /* Every RSA key has an 'e' */
  190. EVP_PKEY_get_bn_param(pkey, "e", &e);
  191. if (e == NULL) {
  192. BIO_printf(bio_err, "Error cannot access RSA e\n");
  193. goto end;
  194. }
  195. hexe = BN_bn2hex(e);
  196. dece = BN_bn2dec(e);
  197. if (hexe && dece) {
  198. BIO_printf(bio_err, "e is %s (0x%s)\n", dece, hexe);
  199. }
  200. OPENSSL_free(hexe);
  201. OPENSSL_free(dece);
  202. BN_free(e);
  203. }
  204. if (traditional) {
  205. if (!PEM_write_bio_PrivateKey_traditional(out, pkey, enc, NULL, 0,
  206. NULL, passout))
  207. goto end;
  208. } else {
  209. if (!PEM_write_bio_PrivateKey(out, pkey, enc, NULL, 0, NULL, passout))
  210. goto end;
  211. }
  212. ret = 0;
  213. end:
  214. BN_free(bn);
  215. BN_GENCB_free(cb);
  216. EVP_PKEY_CTX_free(ctx);
  217. EVP_PKEY_free(pkey);
  218. EVP_CIPHER_free(enc);
  219. BIO_free_all(out);
  220. release_engine(eng);
  221. OPENSSL_free(passout);
  222. if (ret != 0)
  223. ERR_print_errors(bio_err);
  224. return ret;
  225. }