genrsa.c 5.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211
  1. /*
  2. * Copyright 1995-2018 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. #ifdef OPENSSL_NO_RSA
  11. NON_EMPTY_TRANSLATION_UNIT
  12. #else
  13. # include <stdio.h>
  14. # include <string.h>
  15. # include <sys/types.h>
  16. # include <sys/stat.h>
  17. # include "apps.h"
  18. # include "progs.h"
  19. # include <openssl/bio.h>
  20. # include <openssl/err.h>
  21. # include <openssl/bn.h>
  22. # include <openssl/rsa.h>
  23. # include <openssl/evp.h>
  24. # include <openssl/x509.h>
  25. # include <openssl/pem.h>
  26. # include <openssl/rand.h>
  27. # define DEFBITS 2048
  28. # define DEFPRIMES 2
  29. static int verbose = 0;
  30. static int genrsa_cb(int p, int n, BN_GENCB *cb);
  31. typedef enum OPTION_choice {
  32. OPT_ERR = -1, OPT_EOF = 0, OPT_HELP,
  33. OPT_3, OPT_F4, OPT_ENGINE,
  34. OPT_OUT, OPT_PASSOUT, OPT_CIPHER, OPT_PRIMES, OPT_VERBOSE,
  35. OPT_R_ENUM
  36. } OPTION_CHOICE;
  37. const OPTIONS genrsa_options[] = {
  38. {"help", OPT_HELP, '-', "Display this summary"},
  39. {"3", OPT_3, '-', "Use 3 for the E value"},
  40. {"F4", OPT_F4, '-', "Use F4 (0x10001) for the E value"},
  41. {"f4", OPT_F4, '-', "Use F4 (0x10001) for the E value"},
  42. {"out", OPT_OUT, '>', "Output the key to specified file"},
  43. OPT_R_OPTIONS,
  44. {"passout", OPT_PASSOUT, 's', "Output file pass phrase source"},
  45. {"", OPT_CIPHER, '-', "Encrypt the output with any supported cipher"},
  46. # ifndef OPENSSL_NO_ENGINE
  47. {"engine", OPT_ENGINE, 's', "Use engine, possibly a hardware device"},
  48. # endif
  49. {"primes", OPT_PRIMES, 'p', "Specify number of primes"},
  50. {"verbose", OPT_VERBOSE, '-', "Verbose output"},
  51. {NULL}
  52. };
  53. int genrsa_main(int argc, char **argv)
  54. {
  55. BN_GENCB *cb = BN_GENCB_new();
  56. PW_CB_DATA cb_data;
  57. ENGINE *eng = NULL;
  58. BIGNUM *bn = BN_new();
  59. BIO *out = NULL;
  60. const BIGNUM *e;
  61. RSA *rsa = NULL;
  62. const EVP_CIPHER *enc = NULL;
  63. int ret = 1, num = DEFBITS, private = 0, primes = DEFPRIMES;
  64. unsigned long f4 = RSA_F4;
  65. char *outfile = NULL, *passoutarg = NULL, *passout = NULL;
  66. char *prog, *hexe, *dece;
  67. OPTION_CHOICE o;
  68. if (bn == NULL || cb == NULL)
  69. goto end;
  70. BN_GENCB_set(cb, genrsa_cb, bio_err);
  71. prog = opt_init(argc, argv, genrsa_options);
  72. while ((o = opt_next()) != OPT_EOF) {
  73. switch (o) {
  74. case OPT_EOF:
  75. case OPT_ERR:
  76. opthelp:
  77. BIO_printf(bio_err, "%s: Use -help for summary.\n", prog);
  78. goto end;
  79. case OPT_HELP:
  80. ret = 0;
  81. opt_help(genrsa_options);
  82. goto end;
  83. case OPT_3:
  84. f4 = 3;
  85. break;
  86. case OPT_F4:
  87. f4 = RSA_F4;
  88. break;
  89. case OPT_OUT:
  90. outfile = opt_arg();
  91. break;
  92. case OPT_ENGINE:
  93. eng = setup_engine(opt_arg(), 0);
  94. break;
  95. case OPT_R_CASES:
  96. if (!opt_rand(o))
  97. goto end;
  98. break;
  99. case OPT_PASSOUT:
  100. passoutarg = opt_arg();
  101. break;
  102. case OPT_CIPHER:
  103. if (!opt_cipher(opt_unknown(), &enc))
  104. goto end;
  105. break;
  106. case OPT_PRIMES:
  107. if (!opt_int(opt_arg(), &primes))
  108. goto end;
  109. break;
  110. case OPT_VERBOSE:
  111. verbose = 1;
  112. break;
  113. }
  114. }
  115. argc = opt_num_rest();
  116. argv = opt_rest();
  117. if (argc == 1) {
  118. if (!opt_int(argv[0], &num) || num <= 0)
  119. goto end;
  120. if (num > OPENSSL_RSA_MAX_MODULUS_BITS)
  121. BIO_printf(bio_err,
  122. "Warning: It is not recommended to use more than %d bit for RSA keys.\n"
  123. " Your key size is %d! Larger key size may behave not as expected.\n",
  124. OPENSSL_RSA_MAX_MODULUS_BITS, num);
  125. } else if (argc > 0) {
  126. BIO_printf(bio_err, "Extra arguments given.\n");
  127. goto opthelp;
  128. }
  129. private = 1;
  130. if (!app_passwd(NULL, passoutarg, NULL, &passout)) {
  131. BIO_printf(bio_err, "Error getting password\n");
  132. goto end;
  133. }
  134. out = bio_open_owner(outfile, FORMAT_PEM, private);
  135. if (out == NULL)
  136. goto end;
  137. if (verbose)
  138. BIO_printf(bio_err, "Generating RSA private key, %d bit long modulus (%d primes)\n",
  139. num, primes);
  140. rsa = eng ? RSA_new_method(eng) : RSA_new();
  141. if (rsa == NULL)
  142. goto end;
  143. if (!BN_set_word(bn, f4)
  144. || !RSA_generate_multi_prime_key(rsa, num, primes, bn, cb))
  145. goto end;
  146. RSA_get0_key(rsa, NULL, &e, NULL);
  147. hexe = BN_bn2hex(e);
  148. dece = BN_bn2dec(e);
  149. if (hexe && dece && verbose) {
  150. BIO_printf(bio_err, "e is %s (0x%s)\n", dece, hexe);
  151. }
  152. OPENSSL_free(hexe);
  153. OPENSSL_free(dece);
  154. cb_data.password = passout;
  155. cb_data.prompt_info = outfile;
  156. assert(private);
  157. if (!PEM_write_bio_RSAPrivateKey(out, rsa, enc, NULL, 0,
  158. (pem_password_cb *)password_callback,
  159. &cb_data))
  160. goto end;
  161. ret = 0;
  162. end:
  163. BN_free(bn);
  164. BN_GENCB_free(cb);
  165. RSA_free(rsa);
  166. BIO_free_all(out);
  167. release_engine(eng);
  168. OPENSSL_free(passout);
  169. if (ret != 0)
  170. ERR_print_errors(bio_err);
  171. return ret;
  172. }
  173. static int genrsa_cb(int p, int n, BN_GENCB *cb)
  174. {
  175. char c = '*';
  176. if (!verbose)
  177. return 1;
  178. if (p == 0)
  179. c = '.';
  180. if (p == 1)
  181. c = '+';
  182. if (p == 2)
  183. c = '*';
  184. if (p == 3)
  185. c = '\n';
  186. BIO_write(BN_GENCB_get_arg(cb), &c, 1);
  187. (void)BIO_flush(BN_GENCB_get_arg(cb));
  188. return 1;
  189. }
  190. #endif