]>
Commit | Line | Data |
---|---|---|
846e33c7 | 1 | /* |
33388b44 | 2 | * Copyright 1995-2020 The OpenSSL Project Authors. All Rights Reserved. |
d02b48c6 | 3 | * |
dffa7520 | 4 | * Licensed under the Apache License 2.0 (the "License"). You may not use |
846e33c7 RS |
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 | |
d02b48c6 RE |
8 | */ |
9 | ||
3eeaab4b | 10 | #include <openssl/opensslconf.h> |
1ae56f2f RS |
11 | |
12 | #include <stdio.h> | |
13 | #include <string.h> | |
14 | #include <sys/types.h> | |
15 | #include <sys/stat.h> | |
16 | #include "apps.h" | |
17 | #include "progs.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> | |
26 | ||
27 | #define DEFBITS 2048 | |
28 | #define DEFPRIMES 2 | |
d02b48c6 | 29 | |
c43fa566 PP |
30 | static int verbose = 0; |
31 | ||
8f7e1f68 | 32 | static int genrsa_cb(EVP_PKEY_CTX *ctx); |
667ac4ec | 33 | |
7e1b7485 RS |
34 | typedef enum OPTION_choice { |
35 | OPT_ERR = -1, OPT_EOF = 0, OPT_HELP, | |
8bf37709 SL |
36 | #ifndef OPENSSL_NO_DEPRECATED_3_0 |
37 | OPT_3, | |
38 | #endif | |
39 | OPT_F4, OPT_ENGINE, | |
c43fa566 | 40 | OPT_OUT, OPT_PASSOUT, OPT_CIPHER, OPT_PRIMES, OPT_VERBOSE, |
10203a34 | 41 | OPT_R_ENUM, OPT_PROV_ENUM, OPT_TRADITIONAL |
7e1b7485 | 42 | } OPTION_CHOICE; |
667ac4ec | 43 | |
44c83ebd | 44 | const OPTIONS genrsa_options[] = { |
92de469f | 45 | {OPT_HELP_STR, 1, '-', "Usage: %s [options] numbits\n"}, |
5388f986 RS |
46 | |
47 | OPT_SECTION("General"), | |
7e1b7485 | 48 | {"help", OPT_HELP, '-', "Display this summary"}, |
1ae56f2f | 49 | #ifndef OPENSSL_NO_ENGINE |
5388f986 | 50 | {"engine", OPT_ENGINE, 's', "Use engine, possibly a hardware device"}, |
1ae56f2f | 51 | #endif |
5388f986 RS |
52 | |
53 | OPT_SECTION("Input"), | |
8bf37709 SL |
54 | #ifndef OPENSSL_NO_DEPRECATED_3_0 |
55 | {"3", OPT_3, '-', "(deprecated) Use 3 for the E value"}, | |
56 | #endif | |
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"}, | |
5388f986 RS |
59 | |
60 | OPT_SECTION("Output"), | |
6f007824 | 61 | {"out", OPT_OUT, '>', "Output the key to specified file"}, |
7e1b7485 | 62 | {"passout", OPT_PASSOUT, 's', "Output file pass phrase source"}, |
665d899f | 63 | {"primes", OPT_PRIMES, 'p', "Specify number of primes"}, |
c43fa566 | 64 | {"verbose", OPT_VERBOSE, '-', "Verbose output"}, |
10203a34 KR |
65 | {"traditional", OPT_TRADITIONAL, '-', |
66 | "Use traditional format for private keys"}, | |
5388f986 RS |
67 | {"", OPT_CIPHER, '-', "Encrypt the output with any supported cipher"}, |
68 | ||
69 | OPT_R_OPTIONS, | |
6bd4e3f2 | 70 | OPT_PROV_OPTIONS, |
92de469f RS |
71 | |
72 | OPT_PARAMETERS(), | |
73 | {"numbits", 0, 0, "Size of key in bits"}, | |
7e1b7485 RS |
74 | {NULL} |
75 | }; | |
76 | ||
77 | int genrsa_main(int argc, char **argv) | |
78 | { | |
79 | BN_GENCB *cb = BN_GENCB_new(); | |
9862e9aa | 80 | ENGINE *eng = NULL; |
7e1b7485 | 81 | BIGNUM *bn = BN_new(); |
8f7e1f68 | 82 | RSA *rsa; |
7e1b7485 | 83 | BIO *out = NULL; |
2ac6115d | 84 | const BIGNUM *e; |
8f7e1f68 P |
85 | EVP_PKEY *pkey = NULL; |
86 | EVP_PKEY_CTX *ctx = NULL; | |
0f113f3e | 87 | const EVP_CIPHER *enc = NULL; |
665d899f | 88 | int ret = 1, num = DEFBITS, private = 0, primes = DEFPRIMES; |
0f113f3e | 89 | unsigned long f4 = RSA_F4; |
7e1b7485 | 90 | char *outfile = NULL, *passoutarg = NULL, *passout = NULL; |
3ee1eac2 | 91 | char *prog, *hexe, *dece; |
7e1b7485 | 92 | OPTION_CHOICE o; |
10203a34 | 93 | int traditional = 0; |
348d0d14 | 94 | |
96487cdd | 95 | if (bn == NULL || cb == NULL) |
7e1b7485 | 96 | goto end; |
348d0d14 | 97 | |
7e1b7485 RS |
98 | prog = opt_init(argc, argv, genrsa_options); |
99 | while ((o = opt_next()) != OPT_EOF) { | |
100 | switch (o) { | |
101 | case OPT_EOF: | |
102 | case OPT_ERR: | |
c27363f5 | 103 | opthelp: |
7e1b7485 RS |
104 | BIO_printf(bio_err, "%s: Use -help for summary.\n", prog); |
105 | goto end; | |
106 | case OPT_HELP: | |
107 | ret = 0; | |
108 | opt_help(genrsa_options); | |
109 | goto end; | |
8bf37709 | 110 | #ifndef OPENSSL_NO_DEPRECATED_3_0 |
7e1b7485 | 111 | case OPT_3: |
8f7e1f68 | 112 | f4 = RSA_3; |
7e1b7485 | 113 | break; |
8bf37709 | 114 | #endif |
7e1b7485 | 115 | case OPT_F4: |
0f113f3e | 116 | f4 = RSA_F4; |
7e1b7485 | 117 | break; |
7e1b7485 RS |
118 | case OPT_OUT: |
119 | outfile = opt_arg(); | |
902c6b95 | 120 | break; |
7e1b7485 | 121 | case OPT_ENGINE: |
9862e9aa | 122 | eng = setup_engine(opt_arg(), 0); |
7e1b7485 | 123 | break; |
3ee1eac2 RS |
124 | case OPT_R_CASES: |
125 | if (!opt_rand(o)) | |
126 | goto end; | |
7e1b7485 | 127 | break; |
6bd4e3f2 P |
128 | case OPT_PROV_CASES: |
129 | if (!opt_provider(o)) | |
130 | goto end; | |
131 | break; | |
7e1b7485 RS |
132 | case OPT_PASSOUT: |
133 | passoutarg = opt_arg(); | |
134 | break; | |
135 | case OPT_CIPHER: | |
136 | if (!opt_cipher(opt_unknown(), &enc)) | |
137 | goto end; | |
138 | break; | |
665d899f PY |
139 | case OPT_PRIMES: |
140 | if (!opt_int(opt_arg(), &primes)) | |
141 | goto end; | |
142 | break; | |
c43fa566 PP |
143 | case OPT_VERBOSE: |
144 | verbose = 1; | |
145 | break; | |
10203a34 KR |
146 | case OPT_TRADITIONAL: |
147 | traditional = 1; | |
148 | break; | |
7e1b7485 | 149 | } |
0f113f3e | 150 | } |
7e1b7485 RS |
151 | argc = opt_num_rest(); |
152 | argv = opt_rest(); | |
a3fe382e | 153 | |
c27363f5 RS |
154 | if (argc == 1) { |
155 | if (!opt_int(argv[0], &num) || num <= 0) | |
156 | goto end; | |
0336df2f GS |
157 | if (num > OPENSSL_RSA_MAX_MODULUS_BITS) |
158 | BIO_printf(bio_err, | |
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); | |
c27363f5 RS |
162 | } else if (argc > 0) { |
163 | BIO_printf(bio_err, "Extra arguments given.\n"); | |
164 | goto opthelp; | |
165 | } | |
a3fe382e | 166 | |
c27363f5 | 167 | private = 1; |
7e1b7485 | 168 | if (!app_passwd(NULL, passoutarg, NULL, &passout)) { |
0f113f3e | 169 | BIO_printf(bio_err, "Error getting password\n"); |
7e1b7485 | 170 | goto end; |
0f113f3e | 171 | } |
5270e702 | 172 | |
bdd58d98 | 173 | out = bio_open_owner(outfile, FORMAT_PEM, private); |
7e1b7485 RS |
174 | if (out == NULL) |
175 | goto end; | |
d02b48c6 | 176 | |
7c9a7cf1 | 177 | if (!init_gen_str(&ctx, "RSA", eng, 0, NULL, NULL)) |
8f7e1f68 P |
178 | goto end; |
179 | ||
180 | EVP_PKEY_CTX_set_cb(ctx, genrsa_cb); | |
181 | EVP_PKEY_CTX_set_app_data(ctx, bio_err); | |
182 | ||
183 | if (EVP_PKEY_CTX_set_rsa_keygen_bits(ctx, num) <= 0) { | |
184 | BIO_printf(bio_err, "Error setting RSA length\n"); | |
185 | goto end; | |
186 | } | |
187 | if (!BN_set_word(bn, f4)) { | |
188 | BIO_printf(bio_err, "Error allocating RSA public exponent\n"); | |
189 | goto end; | |
190 | } | |
3786d748 | 191 | if (EVP_PKEY_CTX_set1_rsa_keygen_pubexp(ctx, bn) <= 0) { |
8f7e1f68 P |
192 | BIO_printf(bio_err, "Error setting RSA public exponent\n"); |
193 | goto end; | |
194 | } | |
195 | if (EVP_PKEY_CTX_set_rsa_keygen_primes(ctx, primes) <= 0) { | |
196 | BIO_printf(bio_err, "Error setting number of primes\n"); | |
197 | goto end; | |
198 | } | |
c43fa566 PP |
199 | if (verbose) |
200 | BIO_printf(bio_err, "Generating RSA private key, %d bit long modulus (%d primes)\n", | |
201 | num, primes); | |
8f7e1f68 P |
202 | if (!EVP_PKEY_keygen(ctx, &pkey)) { |
203 | BIO_printf(bio_err, "Error generating RSA key\n"); | |
7e1b7485 | 204 | goto end; |
8f7e1f68 | 205 | } |
dc03504d | 206 | |
8f7e1f68 P |
207 | if (verbose) { |
208 | if ((rsa = EVP_PKEY_get0_RSA(pkey)) != NULL) { | |
209 | RSA_get0_key(rsa, NULL, &e, NULL); | |
210 | } else { | |
211 | BIO_printf(bio_err, "Error cannot access RSA e\n"); | |
212 | goto end; | |
213 | } | |
214 | hexe = BN_bn2hex(e); | |
215 | dece = BN_bn2dec(e); | |
216 | if (hexe && dece) { | |
217 | BIO_printf(bio_err, "e is %s (0x%s)\n", dece, hexe); | |
218 | } | |
219 | OPENSSL_free(hexe); | |
220 | OPENSSL_free(dece); | |
0f113f3e | 221 | } |
10203a34 KR |
222 | if (traditional) { |
223 | if (!PEM_write_bio_PrivateKey_traditional(out, pkey, enc, NULL, 0, | |
224 | NULL, passout)) | |
225 | goto end; | |
226 | } else { | |
227 | if (!PEM_write_bio_PrivateKey(out, pkey, enc, NULL, 0, NULL, passout)) | |
228 | goto end; | |
229 | } | |
d02b48c6 | 230 | |
0f113f3e | 231 | ret = 0; |
7e1b7485 | 232 | end: |
23a1d5e9 RS |
233 | BN_free(bn); |
234 | BN_GENCB_free(cb); | |
8f7e1f68 P |
235 | EVP_PKEY_CTX_free(ctx); |
236 | EVP_PKEY_free(pkey); | |
ca3a82c3 | 237 | BIO_free_all(out); |
dd1abd44 | 238 | release_engine(eng); |
b548a1f1 | 239 | OPENSSL_free(passout); |
0f113f3e MC |
240 | if (ret != 0) |
241 | ERR_print_errors(bio_err); | |
26a7d938 | 242 | return ret; |
0f113f3e | 243 | } |
d02b48c6 | 244 | |
8f7e1f68 | 245 | static int genrsa_cb(EVP_PKEY_CTX *ctx) |
0f113f3e MC |
246 | { |
247 | char c = '*'; | |
8f7e1f68 P |
248 | BIO *b = EVP_PKEY_CTX_get_app_data(ctx); |
249 | int p = EVP_PKEY_CTX_get_keygen_info(ctx, 0); | |
d02b48c6 | 250 | |
c43fa566 PP |
251 | if (!verbose) |
252 | return 1; | |
253 | ||
0f113f3e MC |
254 | if (p == 0) |
255 | c = '.'; | |
256 | if (p == 1) | |
257 | c = '+'; | |
258 | if (p == 2) | |
259 | c = '*'; | |
260 | if (p == 3) | |
261 | c = '\n'; | |
8f7e1f68 P |
262 | BIO_write(b, &c, 1); |
263 | (void)BIO_flush(b); | |
0f113f3e MC |
264 | return 1; |
265 | } |