genrsa.c 7.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268
  1. /*
  2. * Copyright 1995-2021 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, NULL, NULL))
  162. goto end;
  163. EVP_PKEY_CTX_set_cb(ctx, genrsa_cb);
  164. EVP_PKEY_CTX_set_app_data(ctx, bio_err);
  165. if (EVP_PKEY_CTX_set_rsa_keygen_bits(ctx, num) <= 0) {
  166. BIO_printf(bio_err, "Error setting RSA length\n");
  167. goto end;
  168. }
  169. if (!BN_set_word(bn, f4)) {
  170. BIO_printf(bio_err, "Error allocating RSA public exponent\n");
  171. goto end;
  172. }
  173. if (EVP_PKEY_CTX_set1_rsa_keygen_pubexp(ctx, bn) <= 0) {
  174. BIO_printf(bio_err, "Error setting RSA public exponent\n");
  175. goto end;
  176. }
  177. if (EVP_PKEY_CTX_set_rsa_keygen_primes(ctx, primes) <= 0) {
  178. BIO_printf(bio_err, "Error setting number of primes\n");
  179. goto end;
  180. }
  181. pkey = app_keygen(ctx, "RSA", num, verbose);
  182. if (verbose) {
  183. BIGNUM *e = NULL;
  184. /* Every RSA key has an 'e' */
  185. EVP_PKEY_get_bn_param(pkey, "e", &e);
  186. if (e == NULL) {
  187. BIO_printf(bio_err, "Error cannot access RSA e\n");
  188. goto end;
  189. }
  190. hexe = BN_bn2hex(e);
  191. dece = BN_bn2dec(e);
  192. if (hexe && dece) {
  193. BIO_printf(bio_err, "e is %s (0x%s)\n", dece, hexe);
  194. }
  195. OPENSSL_free(hexe);
  196. OPENSSL_free(dece);
  197. BN_free(e);
  198. }
  199. if (traditional) {
  200. if (!PEM_write_bio_PrivateKey_traditional(out, pkey, enc, NULL, 0,
  201. NULL, passout))
  202. goto end;
  203. } else {
  204. if (!PEM_write_bio_PrivateKey(out, pkey, enc, NULL, 0, NULL, passout))
  205. goto end;
  206. }
  207. ret = 0;
  208. end:
  209. BN_free(bn);
  210. BN_GENCB_free(cb);
  211. EVP_PKEY_CTX_free(ctx);
  212. EVP_PKEY_free(pkey);
  213. EVP_CIPHER_free(enc);
  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. }