genrsa.c 7.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266
  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. BIO *out = NULL;
  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. 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. 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. case OPT_TRADITIONAL:
  130. traditional = 1;
  131. break;
  132. }
  133. }
  134. argc = opt_num_rest();
  135. argv = opt_rest();
  136. if (argc == 1) {
  137. if (!opt_int(argv[0], &num) || num <= 0)
  138. goto end;
  139. if (num > OPENSSL_RSA_MAX_MODULUS_BITS)
  140. BIO_printf(bio_err,
  141. "Warning: It is not recommended to use more than %d bit for RSA keys.\n"
  142. " Your key size is %d! Larger key size may behave not as expected.\n",
  143. OPENSSL_RSA_MAX_MODULUS_BITS, num);
  144. } else if (argc > 0) {
  145. BIO_printf(bio_err, "Extra arguments given.\n");
  146. goto opthelp;
  147. }
  148. private = 1;
  149. if (!app_passwd(NULL, passoutarg, NULL, &passout)) {
  150. BIO_printf(bio_err, "Error getting password\n");
  151. goto end;
  152. }
  153. out = bio_open_owner(outfile, FORMAT_PEM, private);
  154. if (out == NULL)
  155. goto end;
  156. if (!init_gen_str(&ctx, "RSA", eng, 0, NULL, NULL))
  157. goto end;
  158. EVP_PKEY_CTX_set_cb(ctx, genrsa_cb);
  159. EVP_PKEY_CTX_set_app_data(ctx, bio_err);
  160. if (EVP_PKEY_CTX_set_rsa_keygen_bits(ctx, num) <= 0) {
  161. BIO_printf(bio_err, "Error setting RSA length\n");
  162. goto end;
  163. }
  164. if (!BN_set_word(bn, f4)) {
  165. BIO_printf(bio_err, "Error allocating RSA public exponent\n");
  166. goto end;
  167. }
  168. if (EVP_PKEY_CTX_set1_rsa_keygen_pubexp(ctx, bn) <= 0) {
  169. BIO_printf(bio_err, "Error setting RSA public exponent\n");
  170. goto end;
  171. }
  172. if (EVP_PKEY_CTX_set_rsa_keygen_primes(ctx, primes) <= 0) {
  173. BIO_printf(bio_err, "Error setting number of primes\n");
  174. goto end;
  175. }
  176. if (verbose)
  177. BIO_printf(bio_err, "Generating RSA private key, %d bit long modulus (%d primes)\n",
  178. num, primes);
  179. if (!EVP_PKEY_keygen(ctx, &pkey)) {
  180. BIO_printf(bio_err, "Error generating RSA key\n");
  181. goto end;
  182. }
  183. if (verbose) {
  184. BIGNUM *e = NULL;
  185. /* Every RSA key has an 'e' */
  186. EVP_PKEY_get_bn_param(pkey, "e", &e);
  187. if (e == NULL) {
  188. BIO_printf(bio_err, "Error cannot access RSA e\n");
  189. goto end;
  190. }
  191. hexe = BN_bn2hex(e);
  192. dece = BN_bn2dec(e);
  193. if (hexe && dece) {
  194. BIO_printf(bio_err, "e is %s (0x%s)\n", dece, hexe);
  195. }
  196. OPENSSL_free(hexe);
  197. OPENSSL_free(dece);
  198. BN_free(e);
  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. }