genrsa.c 7.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271
  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. static int genrsa_cb(EVP_PKEY_CTX *ctx);
  28. typedef enum OPTION_choice {
  29. OPT_COMMON,
  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. 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. prog = opt_init(argc, argv, genrsa_options);
  82. while ((o = opt_next()) != OPT_EOF) {
  83. switch (o) {
  84. case OPT_EOF:
  85. case OPT_ERR:
  86. opthelp:
  87. BIO_printf(bio_err, "%s: Use -help for summary.\n", prog);
  88. goto end;
  89. case OPT_HELP:
  90. ret = 0;
  91. opt_help(genrsa_options);
  92. goto end;
  93. #ifndef OPENSSL_NO_DEPRECATED_3_0
  94. case OPT_3:
  95. f4 = RSA_3;
  96. break;
  97. #endif
  98. case OPT_F4:
  99. f4 = RSA_F4;
  100. break;
  101. case OPT_OUT:
  102. outfile = opt_arg();
  103. break;
  104. case OPT_ENGINE:
  105. eng = setup_engine(opt_arg(), 0);
  106. break;
  107. case OPT_R_CASES:
  108. if (!opt_rand(o))
  109. goto end;
  110. break;
  111. case OPT_PROV_CASES:
  112. if (!opt_provider(o))
  113. goto end;
  114. break;
  115. case OPT_PASSOUT:
  116. passoutarg = opt_arg();
  117. break;
  118. case OPT_CIPHER:
  119. ciphername = opt_unknown();
  120. break;
  121. case OPT_PRIMES:
  122. primes = opt_int_arg();
  123. break;
  124. case OPT_VERBOSE:
  125. verbose = 1;
  126. break;
  127. case OPT_TRADITIONAL:
  128. traditional = 1;
  129. break;
  130. }
  131. }
  132. /* One optional argument, the bitsize. */
  133. argc = opt_num_rest();
  134. argv = opt_rest();
  135. if (argc == 1) {
  136. if (!opt_int(argv[0], &num) || num <= 0)
  137. goto end;
  138. if (num > OPENSSL_RSA_MAX_MODULUS_BITS)
  139. BIO_printf(bio_err,
  140. "Warning: It is not recommended to use more than %d bit for RSA keys.\n"
  141. " Your key size is %d! Larger key size may behave not as expected.\n",
  142. OPENSSL_RSA_MAX_MODULUS_BITS, num);
  143. } else if (argc > 0) {
  144. BIO_printf(bio_err, "Extra arguments given.\n");
  145. goto opthelp;
  146. }
  147. if (!app_RAND_load())
  148. goto end;
  149. private = 1;
  150. if (ciphername != NULL) {
  151. if (!opt_cipher(ciphername, &enc))
  152. goto end;
  153. }
  154. if (!app_passwd(NULL, passoutarg, NULL, &passout)) {
  155. BIO_printf(bio_err, "Error getting password\n");
  156. goto end;
  157. }
  158. out = bio_open_owner(outfile, FORMAT_PEM, private);
  159. if (out == NULL)
  160. goto end;
  161. if (!init_gen_str(&ctx, "RSA", eng, 0, app_get0_libctx(),
  162. app_get0_propq()))
  163. goto end;
  164. EVP_PKEY_CTX_set_cb(ctx, genrsa_cb);
  165. EVP_PKEY_CTX_set_app_data(ctx, bio_err);
  166. if (EVP_PKEY_CTX_set_rsa_keygen_bits(ctx, num) <= 0) {
  167. BIO_printf(bio_err, "Error setting RSA length\n");
  168. goto end;
  169. }
  170. if (!BN_set_word(bn, f4)) {
  171. BIO_printf(bio_err, "Error allocating RSA public exponent\n");
  172. goto end;
  173. }
  174. if (EVP_PKEY_CTX_set1_rsa_keygen_pubexp(ctx, bn) <= 0) {
  175. BIO_printf(bio_err, "Error setting RSA public exponent\n");
  176. goto end;
  177. }
  178. if (EVP_PKEY_CTX_set_rsa_keygen_primes(ctx, primes) <= 0) {
  179. BIO_printf(bio_err, "Error setting number of primes\n");
  180. goto end;
  181. }
  182. pkey = app_keygen(ctx, "RSA", num, verbose);
  183. if (pkey == NULL)
  184. goto end;
  185. if (verbose) {
  186. BIGNUM *e = NULL;
  187. /* Every RSA key has an 'e' */
  188. EVP_PKEY_get_bn_param(pkey, "e", &e);
  189. if (e == NULL) {
  190. BIO_printf(bio_err, "Error cannot access RSA e\n");
  191. goto end;
  192. }
  193. hexe = BN_bn2hex(e);
  194. dece = BN_bn2dec(e);
  195. if (hexe && dece) {
  196. BIO_printf(bio_err, "e is %s (0x%s)\n", dece, hexe);
  197. }
  198. OPENSSL_free(hexe);
  199. OPENSSL_free(dece);
  200. BN_free(e);
  201. }
  202. if (traditional) {
  203. if (!PEM_write_bio_PrivateKey_traditional(out, pkey, enc, NULL, 0,
  204. NULL, passout))
  205. goto end;
  206. } else {
  207. if (!PEM_write_bio_PrivateKey(out, pkey, enc, NULL, 0, NULL, passout))
  208. goto end;
  209. }
  210. ret = 0;
  211. end:
  212. BN_free(bn);
  213. BN_GENCB_free(cb);
  214. EVP_PKEY_CTX_free(ctx);
  215. EVP_PKEY_free(pkey);
  216. EVP_CIPHER_free(enc);
  217. BIO_free_all(out);
  218. release_engine(eng);
  219. OPENSSL_free(passout);
  220. if (ret != 0)
  221. ERR_print_errors(bio_err);
  222. return ret;
  223. }
  224. static int genrsa_cb(EVP_PKEY_CTX *ctx)
  225. {
  226. char c = '*';
  227. BIO *b = EVP_PKEY_CTX_get_app_data(ctx);
  228. int p = EVP_PKEY_CTX_get_keygen_info(ctx, 0);
  229. if (!verbose)
  230. return 1;
  231. if (p == 0)
  232. c = '.';
  233. if (p == 1)
  234. c = '+';
  235. if (p == 2)
  236. c = '*';
  237. if (p == 3)
  238. c = '\n';
  239. BIO_write(b, &c, 1);
  240. (void)BIO_flush(b);
  241. return 1;
  242. }