2 * Copyright 1995-2020 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 #ifndef OPENSSL_NO_DEPRECATED_3_0
40 OPT_OUT
, OPT_PASSOUT
, OPT_CIPHER
, OPT_PRIMES
, OPT_VERBOSE
,
41 OPT_R_ENUM
, OPT_PROV_ENUM
, OPT_TRADITIONAL
44 const OPTIONS genrsa_options
[] = {
45 {OPT_HELP_STR
, 1, '-', "Usage: %s [options] numbits\n"},
47 OPT_SECTION("General"),
48 {"help", OPT_HELP
, '-', "Display this summary"},
49 #ifndef OPENSSL_NO_ENGINE
50 {"engine", OPT_ENGINE
, 's', "Use engine, possibly a hardware device"},
54 #ifndef OPENSSL_NO_DEPRECATED_3_0
55 {"3", OPT_3
, '-', "(deprecated) Use 3 for the E value"},
57 {"F4", OPT_F4
, '-', "Use the Fermat number F4 (0x10001) for the E value"},
58 {"f4", OPT_F4
, '-', "Use the Fermat number F4 (0x10001) for the E value"},
60 OPT_SECTION("Output"),
61 {"out", OPT_OUT
, '>', "Output the key to specified file"},
62 {"passout", OPT_PASSOUT
, 's', "Output file pass phrase source"},
63 {"primes", OPT_PRIMES
, 'p', "Specify number of primes"},
64 {"verbose", OPT_VERBOSE
, '-', "Verbose output"},
65 {"traditional", OPT_TRADITIONAL
, '-',
66 "Use traditional format for private keys"},
67 {"", OPT_CIPHER
, '-', "Encrypt the output with any supported cipher"},
73 {"numbits", 0, 0, "Size of key in bits"},
77 int genrsa_main(int argc
, char **argv
)
79 BN_GENCB
*cb
= BN_GENCB_new();
81 BIGNUM
*bn
= BN_new();
83 EVP_PKEY
*pkey
= NULL
;
84 EVP_PKEY_CTX
*ctx
= NULL
;
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
;
93 if (bn
== NULL
|| cb
== NULL
)
96 prog
= opt_init(argc
, argv
, genrsa_options
);
97 while ((o
= opt_next()) != OPT_EOF
) {
102 BIO_printf(bio_err
, "%s: Use -help for summary.\n", prog
);
106 opt_help(genrsa_options
);
108 #ifndef OPENSSL_NO_DEPRECATED_3_0
120 eng
= setup_engine(opt_arg(), 0);
127 if (!opt_provider(o
))
131 passoutarg
= opt_arg();
134 if (!opt_cipher(opt_unknown(), &enc
))
138 if (!opt_int(opt_arg(), &primes
))
144 case OPT_TRADITIONAL
:
150 /* One optional argument, the bitsize. */
151 argc
= opt_num_rest();
155 if (!opt_int(argv
[0], &num
) || num
<= 0)
157 if (num
> OPENSSL_RSA_MAX_MODULUS_BITS
)
159 "Warning: It is not recommended to use more than %d bit for RSA keys.\n"
160 " Your key size is %d! Larger key size may behave not as expected.\n",
161 OPENSSL_RSA_MAX_MODULUS_BITS
, num
);
162 } else if (argc
> 0) {
163 BIO_printf(bio_err
, "Extra arguments given.\n");
169 if (!app_passwd(NULL
, passoutarg
, NULL
, &passout
)) {
170 BIO_printf(bio_err
, "Error getting password\n");
174 out
= bio_open_owner(outfile
, FORMAT_PEM
, private);
178 if (!init_gen_str(&ctx
, "RSA", eng
, 0, NULL
, NULL
))
181 EVP_PKEY_CTX_set_cb(ctx
, genrsa_cb
);
182 EVP_PKEY_CTX_set_app_data(ctx
, bio_err
);
184 if (EVP_PKEY_CTX_set_rsa_keygen_bits(ctx
, num
) <= 0) {
185 BIO_printf(bio_err
, "Error setting RSA length\n");
188 if (!BN_set_word(bn
, f4
)) {
189 BIO_printf(bio_err
, "Error allocating RSA public exponent\n");
192 if (EVP_PKEY_CTX_set1_rsa_keygen_pubexp(ctx
, bn
) <= 0) {
193 BIO_printf(bio_err
, "Error setting RSA public exponent\n");
196 if (EVP_PKEY_CTX_set_rsa_keygen_primes(ctx
, primes
) <= 0) {
197 BIO_printf(bio_err
, "Error setting number of primes\n");
201 BIO_printf(bio_err
, "Generating RSA private key, %d bit long modulus (%d primes)\n",
203 if (!EVP_PKEY_keygen(ctx
, &pkey
)) {
204 BIO_printf(bio_err
, "Error generating RSA key\n");
211 /* Every RSA key has an 'e' */
212 EVP_PKEY_get_bn_param(pkey
, "e", &e
);
214 BIO_printf(bio_err
, "Error cannot access RSA e\n");
220 BIO_printf(bio_err
, "e is %s (0x%s)\n", dece
, hexe
);
227 if (!PEM_write_bio_PrivateKey_traditional(out
, pkey
, enc
, NULL
, 0,
231 if (!PEM_write_bio_PrivateKey(out
, pkey
, enc
, NULL
, 0, NULL
, passout
))
239 EVP_PKEY_CTX_free(ctx
);
243 OPENSSL_free(passout
);
245 ERR_print_errors(bio_err
);
249 static int genrsa_cb(EVP_PKEY_CTX
*ctx
)
252 BIO
*b
= EVP_PKEY_CTX_get_app_data(ctx
);
253 int p
= EVP_PKEY_CTX_get_keygen_info(ctx
, 0);