2 * Copyright 1995-2016 The OpenSSL Project Authors. All Rights Reserved.
4 * Licensed under the OpenSSL license (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 #include <openssl/opensslconf.h>
12 NON_EMPTY_TRANSLATION_UNIT
17 # include <sys/types.h>
18 # include <sys/stat.h>
20 # include <openssl/bio.h>
21 # include <openssl/err.h>
22 # include <openssl/bn.h>
23 # include <openssl/rsa.h>
24 # include <openssl/evp.h>
25 # include <openssl/x509.h>
26 # include <openssl/pem.h>
27 # include <openssl/rand.h>
31 static int genrsa_cb(int p, int n, BN_GENCB *cb);
33 typedef enum OPTION_choice {
34 OPT_ERR = -1, OPT_EOF = 0, OPT_HELP,
35 OPT_3, OPT_F4, OPT_ENGINE,
36 OPT_OUT, OPT_RAND, OPT_PASSOUT, OPT_CIPHER
39 OPTIONS genrsa_options[] = {
40 {"help", OPT_HELP, '-', "Display this summary"},
41 {"3", OPT_3, '-', "Use 3 for the E value"},
42 {"F4", OPT_F4, '-', "Use F4 (0x10001) for the E value"},
43 {"f4", OPT_F4, '-', "Use F4 (0x10001) for the E value"},
44 {"out", OPT_OUT, 's', "Output the key to specified file"},
45 {"rand", OPT_RAND, 's',
46 "Load the file(s) into the random number generator"},
47 {"passout", OPT_PASSOUT, 's', "Output file pass phrase source"},
48 {"", OPT_CIPHER, '-', "Encrypt the output with any supported cipher"},
49 # ifndef OPENSSL_NO_ENGINE
50 {"engine", OPT_ENGINE, 's', "Use engine, possibly a hardware device"},
55 int genrsa_main(int argc, char **argv)
57 BN_GENCB *cb = BN_GENCB_new();
60 BIGNUM *bn = BN_new();
64 const EVP_CIPHER *enc = NULL;
65 int ret = 1, num = DEFBITS, private = 0;
66 unsigned long f4 = RSA_F4;
67 char *outfile = NULL, *passoutarg = NULL, *passout = NULL;
68 char *inrand = NULL, *prog, *hexe, *dece;
71 if (bn == NULL || cb == NULL)
74 BN_GENCB_set(cb, genrsa_cb, bio_err);
76 prog = opt_init(argc, argv, genrsa_options);
77 while ((o = opt_next()) != OPT_EOF) {
82 BIO_printf(bio_err, "%s: Use -help for summary.\n", prog);
86 opt_help(genrsa_options);
98 eng = setup_engine(opt_arg(), 0);
104 passoutarg = opt_arg();
107 if (!opt_cipher(opt_unknown(), &enc))
112 argc = opt_num_rest();
116 if (!opt_int(argv[0], &num) || num <= 0)
118 } else if (argc > 0) {
119 BIO_printf(bio_err, "Extra arguments given.\n");
124 if (!app_passwd(NULL, passoutarg, NULL, &passout)) {
125 BIO_printf(bio_err, "Error getting password\n");
129 out = bio_open_owner(outfile, FORMAT_PEM, private);
133 if (!app_RAND_load_file(NULL, 1) && inrand == NULL
136 "warning, not much extra random data, consider using the -rand option\n");
139 BIO_printf(bio_err, "%ld semi-random bytes loaded\n",
140 app_RAND_load_files(inrand));
142 BIO_printf(bio_err, "Generating RSA private key, %d bit long modulus\n",
144 rsa = eng ? RSA_new_method(eng) : RSA_new();
148 if (!BN_set_word(bn, f4) || !RSA_generate_key_ex(rsa, num, bn, cb))
151 app_RAND_write_file(NULL);
153 RSA_get0_key(rsa, NULL, &e, NULL);
157 BIO_printf(bio_err, "e is %s (0x%s)\n", dece, hexe);
161 cb_data.password = passout;
162 cb_data.prompt_info = outfile;
164 if (!PEM_write_bio_RSAPrivateKey(out, rsa, enc, NULL, 0,
165 (pem_password_cb *)password_callback,
176 OPENSSL_free(passout);
178 ERR_print_errors(bio_err);
182 static int genrsa_cb(int p, int n, BN_GENCB *cb)
194 BIO_write(BN_GENCB_get_arg(cb), &c, 1);
195 (void)BIO_flush(BN_GENCB_get_arg(cb));