]> git.ipfire.org Git - thirdparty/openssl.git/blame - apps/genrsa.c
APPS: Add check for multiple 'unknown' options
[thirdparty/openssl.git] / apps / genrsa.c
CommitLineData
846e33c7 1/*
a28d06f3 2 * Copyright 1995-2021 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
30static int verbose = 0;
31
8f7e1f68 32static int genrsa_cb(EVP_PKEY_CTX *ctx);
667ac4ec 33
7e1b7485 34typedef enum OPTION_choice {
b0f96018 35 OPT_COMMON,
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 44const 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
77int genrsa_main(int argc, char **argv)
78{
79 BN_GENCB *cb = BN_GENCB_new();
9862e9aa 80 ENGINE *eng = NULL;
7e1b7485
RS
81 BIGNUM *bn = BN_new();
82 BIO *out = NULL;
8f7e1f68
P
83 EVP_PKEY *pkey = NULL;
84 EVP_PKEY_CTX *ctx = NULL;
606a417f 85 EVP_CIPHER *enc = NULL;
665d899f 86 int ret = 1, num = DEFBITS, private = 0, primes = DEFPRIMES;
0f113f3e 87 unsigned long f4 = RSA_F4;
7e1b7485 88 char *outfile = NULL, *passoutarg = NULL, *passout = NULL;
03bbd346 89 char *prog, *hexe, *dece, *ciphername = NULL;
7e1b7485 90 OPTION_CHOICE o;
10203a34 91 int traditional = 0;
348d0d14 92
96487cdd 93 if (bn == NULL || cb == NULL)
7e1b7485 94 goto end;
348d0d14 95
2c272447 96 opt_set_unknown_name("cipher");
7e1b7485
RS
97 prog = opt_init(argc, argv, genrsa_options);
98 while ((o = opt_next()) != OPT_EOF) {
99 switch (o) {
100 case OPT_EOF:
101 case OPT_ERR:
c27363f5 102opthelp:
7e1b7485
RS
103 BIO_printf(bio_err, "%s: Use -help for summary.\n", prog);
104 goto end;
105 case OPT_HELP:
106 ret = 0;
107 opt_help(genrsa_options);
108 goto end;
8bf37709 109#ifndef OPENSSL_NO_DEPRECATED_3_0
7e1b7485 110 case OPT_3:
8f7e1f68 111 f4 = RSA_3;
7e1b7485 112 break;
8bf37709 113#endif
7e1b7485 114 case OPT_F4:
0f113f3e 115 f4 = RSA_F4;
7e1b7485 116 break;
7e1b7485
RS
117 case OPT_OUT:
118 outfile = opt_arg();
902c6b95 119 break;
7e1b7485 120 case OPT_ENGINE:
9862e9aa 121 eng = setup_engine(opt_arg(), 0);
7e1b7485 122 break;
3ee1eac2
RS
123 case OPT_R_CASES:
124 if (!opt_rand(o))
125 goto end;
7e1b7485 126 break;
6bd4e3f2
P
127 case OPT_PROV_CASES:
128 if (!opt_provider(o))
129 goto end;
130 break;
7e1b7485
RS
131 case OPT_PASSOUT:
132 passoutarg = opt_arg();
133 break;
134 case OPT_CIPHER:
03bbd346 135 ciphername = opt_unknown();
7e1b7485 136 break;
665d899f 137 case OPT_PRIMES:
d830526c 138 primes = opt_int_arg();
665d899f 139 break;
c43fa566
PP
140 case OPT_VERBOSE:
141 verbose = 1;
142 break;
10203a34
KR
143 case OPT_TRADITIONAL:
144 traditional = 1;
145 break;
7e1b7485 146 }
0f113f3e 147 }
021410ea
RS
148
149 /* One optional argument, the bitsize. */
7e1b7485
RS
150 argc = opt_num_rest();
151 argv = opt_rest();
a3fe382e 152
c27363f5
RS
153 if (argc == 1) {
154 if (!opt_int(argv[0], &num) || num <= 0)
155 goto end;
0336df2f
GS
156 if (num > OPENSSL_RSA_MAX_MODULUS_BITS)
157 BIO_printf(bio_err,
158 "Warning: It is not recommended to use more than %d bit for RSA keys.\n"
159 " Your key size is %d! Larger key size may behave not as expected.\n",
160 OPENSSL_RSA_MAX_MODULUS_BITS, num);
d9f07357 161 } else if (!opt_check_rest_arg(NULL)) {
c27363f5
RS
162 goto opthelp;
163 }
a3fe382e 164
3ad60309
DDO
165 if (!app_RAND_load())
166 goto end;
167
c27363f5 168 private = 1;
d9f07357
DDO
169 if (!opt_cipher(ciphername, &enc))
170 goto end;
7e1b7485 171 if (!app_passwd(NULL, passoutarg, NULL, &passout)) {
0f113f3e 172 BIO_printf(bio_err, "Error getting password\n");
7e1b7485 173 goto end;
0f113f3e 174 }
5270e702 175
bdd58d98 176 out = bio_open_owner(outfile, FORMAT_PEM, private);
7e1b7485
RS
177 if (out == NULL)
178 goto end;
d02b48c6 179
7c9a7cf1 180 if (!init_gen_str(&ctx, "RSA", eng, 0, NULL, NULL))
8f7e1f68
P
181 goto end;
182
183 EVP_PKEY_CTX_set_cb(ctx, genrsa_cb);
184 EVP_PKEY_CTX_set_app_data(ctx, bio_err);
185
186 if (EVP_PKEY_CTX_set_rsa_keygen_bits(ctx, num) <= 0) {
187 BIO_printf(bio_err, "Error setting RSA length\n");
188 goto end;
189 }
190 if (!BN_set_word(bn, f4)) {
191 BIO_printf(bio_err, "Error allocating RSA public exponent\n");
192 goto end;
193 }
3786d748 194 if (EVP_PKEY_CTX_set1_rsa_keygen_pubexp(ctx, bn) <= 0) {
8f7e1f68
P
195 BIO_printf(bio_err, "Error setting RSA public exponent\n");
196 goto end;
197 }
198 if (EVP_PKEY_CTX_set_rsa_keygen_primes(ctx, primes) <= 0) {
199 BIO_printf(bio_err, "Error setting number of primes\n");
200 goto end;
201 }
a7e4ca5b 202 pkey = app_keygen(ctx, "RSA", num, verbose);
dc03504d 203
8f7e1f68 204 if (verbose) {
d7e498ac
RL
205 BIGNUM *e = NULL;
206
207 /* Every RSA key has an 'e' */
208 EVP_PKEY_get_bn_param(pkey, "e", &e);
209 if (e == NULL) {
8f7e1f68
P
210 BIO_printf(bio_err, "Error cannot access RSA e\n");
211 goto end;
212 }
213 hexe = BN_bn2hex(e);
214 dece = BN_bn2dec(e);
215 if (hexe && dece) {
216 BIO_printf(bio_err, "e is %s (0x%s)\n", dece, hexe);
217 }
218 OPENSSL_free(hexe);
219 OPENSSL_free(dece);
d7e498ac 220 BN_free(e);
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);
606a417f 237 EVP_CIPHER_free(enc);
ca3a82c3 238 BIO_free_all(out);
dd1abd44 239 release_engine(eng);
b548a1f1 240 OPENSSL_free(passout);
0f113f3e
MC
241 if (ret != 0)
242 ERR_print_errors(bio_err);
26a7d938 243 return ret;
0f113f3e 244}
d02b48c6 245
8f7e1f68 246static int genrsa_cb(EVP_PKEY_CTX *ctx)
0f113f3e
MC
247{
248 char c = '*';
8f7e1f68
P
249 BIO *b = EVP_PKEY_CTX_get_app_data(ctx);
250 int p = EVP_PKEY_CTX_get_keygen_info(ctx, 0);
d02b48c6 251
c43fa566
PP
252 if (!verbose)
253 return 1;
254
0f113f3e
MC
255 if (p == 0)
256 c = '.';
257 if (p == 1)
258 c = '+';
259 if (p == 2)
260 c = '*';
261 if (p == 3)
262 c = '\n';
8f7e1f68
P
263 BIO_write(b, &c, 1);
264 (void)BIO_flush(b);
0f113f3e
MC
265 return 1;
266}