genrsa.c 5.4 KB

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