genrsa.c 6.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255
  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
  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. {"", OPT_CIPHER, '-', "Encrypt the output with any supported cipher"},
  56. OPT_R_OPTIONS,
  57. OPT_PROV_OPTIONS,
  58. OPT_PARAMETERS(),
  59. {"numbits", 0, 0, "Size of key in bits"},
  60. {NULL}
  61. };
  62. int genrsa_main(int argc, char **argv)
  63. {
  64. BN_GENCB *cb = BN_GENCB_new();
  65. ENGINE *eng = NULL;
  66. BIGNUM *bn = BN_new();
  67. RSA *rsa;
  68. BIO *out = NULL;
  69. const BIGNUM *e;
  70. EVP_PKEY *pkey = NULL;
  71. EVP_PKEY_CTX *ctx = NULL;
  72. const 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;
  77. OPTION_CHOICE o;
  78. unsigned char *ebuf = NULL;
  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. if (!opt_cipher(opt_unknown(), &enc))
  120. goto end;
  121. break;
  122. case OPT_PRIMES:
  123. if (!opt_int(opt_arg(), &primes))
  124. goto end;
  125. break;
  126. case OPT_VERBOSE:
  127. verbose = 1;
  128. break;
  129. }
  130. }
  131. argc = opt_num_rest();
  132. argv = opt_rest();
  133. if (argc == 1) {
  134. if (!opt_int(argv[0], &num) || num <= 0)
  135. goto end;
  136. if (num > OPENSSL_RSA_MAX_MODULUS_BITS)
  137. BIO_printf(bio_err,
  138. "Warning: It is not recommended to use more than %d bit for RSA keys.\n"
  139. " Your key size is %d! Larger key size may behave not as expected.\n",
  140. OPENSSL_RSA_MAX_MODULUS_BITS, num);
  141. } else if (argc > 0) {
  142. BIO_printf(bio_err, "Extra arguments given.\n");
  143. goto opthelp;
  144. }
  145. private = 1;
  146. if (!app_passwd(NULL, passoutarg, NULL, &passout)) {
  147. BIO_printf(bio_err, "Error getting password\n");
  148. goto end;
  149. }
  150. out = bio_open_owner(outfile, FORMAT_PEM, private);
  151. if (out == NULL)
  152. goto end;
  153. if (!init_gen_str(&ctx, "RSA", eng, 0))
  154. goto end;
  155. EVP_PKEY_CTX_set_cb(ctx, genrsa_cb);
  156. EVP_PKEY_CTX_set_app_data(ctx, bio_err);
  157. if (EVP_PKEY_CTX_set_rsa_keygen_bits(ctx, num) <= 0) {
  158. BIO_printf(bio_err, "Error setting RSA length\n");
  159. goto end;
  160. }
  161. if (!BN_set_word(bn, f4)) {
  162. BIO_printf(bio_err, "Error allocating RSA public exponent\n");
  163. goto end;
  164. }
  165. if (EVP_PKEY_CTX_set_rsa_keygen_pubexp(ctx, bn) <= 0) {
  166. BIO_printf(bio_err, "Error setting RSA public exponent\n");
  167. goto end;
  168. }
  169. if (EVP_PKEY_CTX_set_rsa_keygen_primes(ctx, primes) <= 0) {
  170. BIO_printf(bio_err, "Error setting number of primes\n");
  171. goto end;
  172. }
  173. if (verbose)
  174. BIO_printf(bio_err, "Generating RSA private key, %d bit long modulus (%d primes)\n",
  175. num, primes);
  176. if (!EVP_PKEY_keygen(ctx, &pkey)) {
  177. BIO_printf(bio_err, "Error generating RSA key\n");
  178. goto end;
  179. }
  180. if (verbose) {
  181. if ((rsa = EVP_PKEY_get0_RSA(pkey)) != NULL) {
  182. RSA_get0_key(rsa, NULL, &e, NULL);
  183. } else {
  184. BIO_printf(bio_err, "Error cannot access RSA e\n");
  185. goto end;
  186. }
  187. hexe = BN_bn2hex(e);
  188. dece = BN_bn2dec(e);
  189. if (hexe && dece) {
  190. BIO_printf(bio_err, "e is %s (0x%s)\n", dece, hexe);
  191. }
  192. OPENSSL_free(hexe);
  193. OPENSSL_free(dece);
  194. }
  195. if (!PEM_write_bio_PrivateKey(out, pkey, enc, NULL, 0, NULL, passout))
  196. goto end;
  197. ret = 0;
  198. end:
  199. BN_free(bn);
  200. BN_GENCB_free(cb);
  201. EVP_PKEY_CTX_free(ctx);
  202. EVP_PKEY_free(pkey);
  203. BIO_free_all(out);
  204. release_engine(eng);
  205. OPENSSL_free(passout);
  206. OPENSSL_free(ebuf);
  207. if (ret != 0)
  208. ERR_print_errors(bio_err);
  209. return ret;
  210. }
  211. static int genrsa_cb(EVP_PKEY_CTX *ctx)
  212. {
  213. char c = '*';
  214. BIO *b = EVP_PKEY_CTX_get_app_data(ctx);
  215. int p = EVP_PKEY_CTX_get_keygen_info(ctx, 0);
  216. if (!verbose)
  217. return 1;
  218. if (p == 0)
  219. c = '.';
  220. if (p == 1)
  221. c = '+';
  222. if (p == 2)
  223. c = '*';
  224. if (p == 3)
  225. c = '\n';
  226. BIO_write(b, &c, 1);
  227. (void)BIO_flush(b);
  228. return 1;
  229. }