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 #include <openssl/opensslconf.h>
14 #include <sys/types.h>
18 #include <openssl/bio.h>
19 #include <openssl/err.h>
20 #include <openssl/bn.h>
21 #include <openssl/rsa.h>
22 #include <openssl/evp.h>
23 #include <openssl/x509.h>
24 #include <openssl/pem.h>
25 #include <openssl/rand.h>
30 static int verbose
= 0;
32 static int genrsa_cb(EVP_PKEY_CTX
*ctx
);
34 typedef enum OPTION_choice
{
35 OPT_ERR
= -1, OPT_EOF
= 0, OPT_HELP
,
36 OPT_3
, OPT_F4
, OPT_ENGINE
,
37 OPT_OUT
, OPT_PASSOUT
, OPT_CIPHER
, OPT_PRIMES
, OPT_VERBOSE
,
38 OPT_R_ENUM
, OPT_PROV_ENUM
41 const OPTIONS genrsa_options
[] = {
42 {OPT_HELP_STR
, 1, '-', "Usage: %s [options] numbits\n"},
44 OPT_SECTION("General"),
45 {"help", OPT_HELP
, '-', "Display this summary"},
46 #ifndef OPENSSL_NO_ENGINE
47 {"engine", OPT_ENGINE
, 's', "Use engine, possibly a hardware device"},
51 {"3", OPT_3
, '-', "Use 3 for the E value"},
52 {"F4", OPT_F4
, '-', "Use F4 (0x10001) for the E value"},
53 {"f4", OPT_F4
, '-', "Use F4 (0x10001) for the E value"},
55 OPT_SECTION("Output"),
56 {"out", OPT_OUT
, '>', "Output the key to specified file"},
57 {"passout", OPT_PASSOUT
, 's', "Output file pass phrase source"},
58 {"primes", OPT_PRIMES
, 'p', "Specify number of primes"},
59 {"verbose", OPT_VERBOSE
, '-', "Verbose output"},
60 {"", OPT_CIPHER
, '-', "Encrypt the output with any supported cipher"},
66 {"numbits", 0, 0, "Size of key in bits"},
70 int genrsa_main(int argc
, char **argv
)
72 BN_GENCB
*cb
= BN_GENCB_new();
74 BIGNUM
*bn
= BN_new();
78 EVP_PKEY
*pkey
= NULL
;
79 EVP_PKEY_CTX
*ctx
= NULL
;
80 const EVP_CIPHER
*enc
= NULL
;
81 int ret
= 1, num
= DEFBITS
, private = 0, primes
= DEFPRIMES
;
82 unsigned long f4
= RSA_F4
;
83 char *outfile
= NULL
, *passoutarg
= NULL
, *passout
= NULL
;
84 char *prog
, *hexe
, *dece
;
86 unsigned char *ebuf
= NULL
;
88 if (bn
== NULL
|| cb
== NULL
)
91 prog
= opt_init(argc
, argv
, genrsa_options
);
92 while ((o
= opt_next()) != OPT_EOF
) {
97 BIO_printf(bio_err
, "%s: Use -help for summary.\n", prog
);
101 opt_help(genrsa_options
);
113 eng
= setup_engine(opt_arg(), 0);
120 if (!opt_provider(o
))
124 passoutarg
= opt_arg();
127 if (!opt_cipher(opt_unknown(), &enc
))
131 if (!opt_int(opt_arg(), &primes
))
139 argc
= opt_num_rest();
143 if (!opt_int(argv
[0], &num
) || num
<= 0)
145 if (num
> OPENSSL_RSA_MAX_MODULUS_BITS
)
147 "Warning: It is not recommended to use more than %d bit for RSA keys.\n"
148 " Your key size is %d! Larger key size may behave not as expected.\n",
149 OPENSSL_RSA_MAX_MODULUS_BITS
, num
);
150 } else if (argc
> 0) {
151 BIO_printf(bio_err
, "Extra arguments given.\n");
156 if (!app_passwd(NULL
, passoutarg
, NULL
, &passout
)) {
157 BIO_printf(bio_err
, "Error getting password\n");
161 out
= bio_open_owner(outfile
, FORMAT_PEM
, private);
165 if (!init_gen_str(&ctx
, "RSA", eng
, 0))
168 EVP_PKEY_CTX_set_cb(ctx
, genrsa_cb
);
169 EVP_PKEY_CTX_set_app_data(ctx
, bio_err
);
171 if (EVP_PKEY_CTX_set_rsa_keygen_bits(ctx
, num
) <= 0) {
172 BIO_printf(bio_err
, "Error setting RSA length\n");
175 if (!BN_set_word(bn
, f4
)) {
176 BIO_printf(bio_err
, "Error allocating RSA public exponent\n");
179 if (EVP_PKEY_CTX_set_rsa_keygen_pubexp(ctx
, bn
) <= 0) {
180 BIO_printf(bio_err
, "Error setting RSA public exponent\n");
183 if (EVP_PKEY_CTX_set_rsa_keygen_primes(ctx
, primes
) <= 0) {
184 BIO_printf(bio_err
, "Error setting number of primes\n");
188 BIO_printf(bio_err
, "Generating RSA private key, %d bit long modulus (%d primes)\n",
190 if (!EVP_PKEY_keygen(ctx
, &pkey
)) {
191 BIO_printf(bio_err
, "Error generating RSA key\n");
196 if ((rsa
= EVP_PKEY_get0_RSA(pkey
)) != NULL
) {
197 RSA_get0_key(rsa
, NULL
, &e
, NULL
);
199 BIO_printf(bio_err
, "Error cannot access RSA e\n");
205 BIO_printf(bio_err
, "e is %s (0x%s)\n", dece
, hexe
);
210 if (!PEM_write_bio_PrivateKey(out
, pkey
, enc
, NULL
, 0, NULL
, passout
))
217 EVP_PKEY_CTX_free(ctx
);
221 OPENSSL_free(passout
);
224 ERR_print_errors(bio_err
);
228 static int genrsa_cb(EVP_PKEY_CTX
*ctx
)
231 BIO
*b
= EVP_PKEY_CTX_get_app_data(ctx
);
232 int p
= EVP_PKEY_CTX_get_keygen_info(ctx
, 0);