2 * Copyright 1995-2018 The OpenSSL Project Authors. All Rights Reserved.
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
10 /* We need to use the deprecated RSA low level calls */
11 #define OPENSSL_SUPPRESS_DEPRECATED
13 #include <openssl/opensslconf.h>
15 NON_EMPTY_TRANSLATION_UNIT
20 # include <sys/types.h>
21 # include <sys/stat.h>
24 # include <openssl/bio.h>
25 # include <openssl/err.h>
26 # include <openssl/bn.h>
27 # include <openssl/rsa.h>
28 # include <openssl/evp.h>
29 # include <openssl/x509.h>
30 # include <openssl/pem.h>
31 # include <openssl/rand.h>
36 static int verbose = 0;
38 static int genrsa_cb(int p, int n, BN_GENCB *cb);
40 typedef enum OPTION_choice {
41 OPT_ERR = -1, OPT_EOF = 0, OPT_HELP,
42 OPT_3, OPT_F4, OPT_ENGINE,
43 OPT_OUT, OPT_PASSOUT, OPT_CIPHER, OPT_PRIMES, OPT_VERBOSE,
44 OPT_R_ENUM, OPT_PROV_ENUM
47 const OPTIONS genrsa_options[] = {
48 {OPT_HELP_STR, 1, '-', "Usage: %s [options] numbits\n"},
50 OPT_SECTION("General"),
51 {"help", OPT_HELP, '-', "Display this summary"},
52 # ifndef OPENSSL_NO_ENGINE
53 {"engine", OPT_ENGINE, 's', "Use engine, possibly a hardware device"},
57 {"3", OPT_3, '-', "Use 3 for the E value"},
58 {"F4", OPT_F4, '-', "Use F4 (0x10001) for the E value"},
59 {"f4", OPT_F4, '-', "Use F4 (0x10001) for the E value"},
61 OPT_SECTION("Output"),
62 {"out", OPT_OUT, '>', "Output the key to specified file"},
63 {"passout", OPT_PASSOUT, 's', "Output file pass phrase source"},
64 {"primes", OPT_PRIMES, 'p', "Specify number of primes"},
65 {"verbose", OPT_VERBOSE, '-', "Verbose output"},
66 {"", OPT_CIPHER, '-', "Encrypt the output with any supported cipher"},
72 {"numbits", 0, 0, "Size of key in bits"},
76 int genrsa_main(int argc, char **argv)
78 BN_GENCB *cb = BN_GENCB_new();
81 BIGNUM *bn = BN_new();
85 const EVP_CIPHER *enc = NULL;
86 int ret = 1, num = DEFBITS, private = 0, primes = DEFPRIMES;
87 unsigned long f4 = RSA_F4;
88 char *outfile = NULL, *passoutarg = NULL, *passout = NULL;
89 char *prog, *hexe, *dece;
92 if (bn == NULL || cb == NULL)
95 BN_GENCB_set(cb, genrsa_cb, bio_err);
97 prog = opt_init(argc, argv, genrsa_options);
98 while ((o = opt_next()) != OPT_EOF) {
103 BIO_printf(bio_err, "%s: Use -help for summary.\n", prog);
107 opt_help(genrsa_options);
119 eng = setup_engine(opt_arg(), 0);
126 if (!opt_provider(o))
130 passoutarg = opt_arg();
133 if (!opt_cipher(opt_unknown(), &enc))
137 if (!opt_int(opt_arg(), &primes))
145 argc = opt_num_rest();
149 if (!opt_int(argv[0], &num) || num <= 0)
151 if (num > OPENSSL_RSA_MAX_MODULUS_BITS)
153 "Warning: It is not recommended to use more than %d bit for RSA keys.\n"
154 " Your key size is %d! Larger key size may behave not as expected.\n",
155 OPENSSL_RSA_MAX_MODULUS_BITS, num);
156 } else if (argc > 0) {
157 BIO_printf(bio_err, "Extra arguments given.\n");
162 if (!app_passwd(NULL, passoutarg, NULL, &passout)) {
163 BIO_printf(bio_err, "Error getting password\n");
167 out = bio_open_owner(outfile, FORMAT_PEM, private);
172 BIO_printf(bio_err, "Generating RSA private key, %d bit long modulus (%d primes)\n",
174 rsa = eng ? RSA_new_method(eng) : RSA_new();
178 if (!BN_set_word(bn, f4)
179 || !RSA_generate_multi_prime_key(rsa, num, primes, bn, cb))
182 RSA_get0_key(rsa, NULL, &e, NULL);
185 if (hexe && dece && verbose) {
186 BIO_printf(bio_err, "e is %s (0x%s)\n", dece, hexe);
190 cb_data.password = passout;
191 cb_data.prompt_info = outfile;
193 if (!PEM_write_bio_RSAPrivateKey(out, rsa, enc, NULL, 0,
194 (pem_password_cb *)password_callback,
205 OPENSSL_free(passout);
207 ERR_print_errors(bio_err);
211 static int genrsa_cb(int p, int n, BN_GENCB *cb)
226 BIO_write(BN_GENCB_get_arg(cb), &c, 1);
227 (void)BIO_flush(BN_GENCB_get_arg(cb));