]> git.ipfire.org Git - thirdparty/openssl.git/blame - apps/genrsa.c
Reduce optimization in hppa builds
[thirdparty/openssl.git] / apps / genrsa.c
CommitLineData
846e33c7 1/*
556009c5 2 * Copyright 1995-2023 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
7e1b7485 32typedef enum OPTION_choice {
b0f96018 33 OPT_COMMON,
8bf37709
SL
34#ifndef OPENSSL_NO_DEPRECATED_3_0
35 OPT_3,
36#endif
37 OPT_F4, OPT_ENGINE,
a414fd67 38 OPT_OUT, OPT_PASSOUT, OPT_CIPHER, OPT_PRIMES, OPT_VERBOSE, OPT_QUIET,
10203a34 39 OPT_R_ENUM, OPT_PROV_ENUM, OPT_TRADITIONAL
7e1b7485 40} OPTION_CHOICE;
667ac4ec 41
44c83ebd 42const OPTIONS genrsa_options[] = {
92de469f 43 {OPT_HELP_STR, 1, '-', "Usage: %s [options] numbits\n"},
5388f986
RS
44
45 OPT_SECTION("General"),
7e1b7485 46 {"help", OPT_HELP, '-', "Display this summary"},
1ae56f2f 47#ifndef OPENSSL_NO_ENGINE
5388f986 48 {"engine", OPT_ENGINE, 's', "Use engine, possibly a hardware device"},
1ae56f2f 49#endif
5388f986
RS
50
51 OPT_SECTION("Input"),
8bf37709
SL
52#ifndef OPENSSL_NO_DEPRECATED_3_0
53 {"3", OPT_3, '-', "(deprecated) Use 3 for the E value"},
54#endif
55 {"F4", OPT_F4, '-', "Use the Fermat number F4 (0x10001) for the E value"},
56 {"f4", OPT_F4, '-', "Use the Fermat number F4 (0x10001) for the E value"},
5388f986
RS
57
58 OPT_SECTION("Output"),
6f007824 59 {"out", OPT_OUT, '>', "Output the key to specified file"},
7e1b7485 60 {"passout", OPT_PASSOUT, 's', "Output file pass phrase source"},
665d899f 61 {"primes", OPT_PRIMES, 'p', "Specify number of primes"},
c43fa566 62 {"verbose", OPT_VERBOSE, '-', "Verbose output"},
a414fd67 63 {"quiet", OPT_QUIET, '-', "Terse output"},
10203a34
KR
64 {"traditional", OPT_TRADITIONAL, '-',
65 "Use traditional format for private keys"},
5388f986
RS
66 {"", OPT_CIPHER, '-', "Encrypt the output with any supported cipher"},
67
68 OPT_R_OPTIONS,
6bd4e3f2 69 OPT_PROV_OPTIONS,
92de469f
RS
70
71 OPT_PARAMETERS(),
72 {"numbits", 0, 0, "Size of key in bits"},
7e1b7485
RS
73 {NULL}
74};
75
76int genrsa_main(int argc, char **argv)
77{
78 BN_GENCB *cb = BN_GENCB_new();
9862e9aa 79 ENGINE *eng = NULL;
7e1b7485
RS
80 BIGNUM *bn = BN_new();
81 BIO *out = NULL;
8f7e1f68
P
82 EVP_PKEY *pkey = NULL;
83 EVP_PKEY_CTX *ctx = NULL;
606a417f 84 EVP_CIPHER *enc = NULL;
665d899f 85 int ret = 1, num = DEFBITS, private = 0, primes = DEFPRIMES;
0f113f3e 86 unsigned long f4 = RSA_F4;
7e1b7485 87 char *outfile = NULL, *passoutarg = NULL, *passout = NULL;
03bbd346 88 char *prog, *hexe, *dece, *ciphername = NULL;
7e1b7485 89 OPTION_CHOICE o;
10203a34 90 int traditional = 0;
348d0d14 91
96487cdd 92 if (bn == NULL || cb == NULL)
7e1b7485 93 goto end;
348d0d14 94
2c272447 95 opt_set_unknown_name("cipher");
7e1b7485
RS
96 prog = opt_init(argc, argv, genrsa_options);
97 while ((o = opt_next()) != OPT_EOF) {
98 switch (o) {
99 case OPT_EOF:
100 case OPT_ERR:
c27363f5 101opthelp:
7e1b7485
RS
102 BIO_printf(bio_err, "%s: Use -help for summary.\n", prog);
103 goto end;
104 case OPT_HELP:
105 ret = 0;
106 opt_help(genrsa_options);
107 goto end;
8bf37709 108#ifndef OPENSSL_NO_DEPRECATED_3_0
7e1b7485 109 case OPT_3:
8f7e1f68 110 f4 = RSA_3;
7e1b7485 111 break;
8bf37709 112#endif
7e1b7485 113 case OPT_F4:
0f113f3e 114 f4 = RSA_F4;
7e1b7485 115 break;
7e1b7485
RS
116 case OPT_OUT:
117 outfile = opt_arg();
902c6b95 118 break;
7e1b7485 119 case OPT_ENGINE:
9862e9aa 120 eng = setup_engine(opt_arg(), 0);
7e1b7485 121 break;
3ee1eac2
RS
122 case OPT_R_CASES:
123 if (!opt_rand(o))
124 goto end;
7e1b7485 125 break;
6bd4e3f2
P
126 case OPT_PROV_CASES:
127 if (!opt_provider(o))
128 goto end;
129 break;
7e1b7485
RS
130 case OPT_PASSOUT:
131 passoutarg = opt_arg();
132 break;
133 case OPT_CIPHER:
03bbd346 134 ciphername = opt_unknown();
7e1b7485 135 break;
665d899f 136 case OPT_PRIMES:
d830526c 137 primes = opt_int_arg();
665d899f 138 break;
c43fa566
PP
139 case OPT_VERBOSE:
140 verbose = 1;
141 break;
a414fd67
PP
142 case OPT_QUIET:
143 verbose = 0;
144 break;
10203a34
KR
145 case OPT_TRADITIONAL:
146 traditional = 1;
147 break;
7e1b7485 148 }
0f113f3e 149 }
021410ea
RS
150
151 /* One optional argument, the bitsize. */
7e1b7485
RS
152 argc = opt_num_rest();
153 argv = opt_rest();
a3fe382e 154
c27363f5
RS
155 if (argc == 1) {
156 if (!opt_int(argv[0], &num) || num <= 0)
157 goto end;
0336df2f
GS
158 if (num > OPENSSL_RSA_MAX_MODULUS_BITS)
159 BIO_printf(bio_err,
160 "Warning: It is not recommended to use more than %d bit for RSA keys.\n"
161 " Your key size is %d! Larger key size may behave not as expected.\n",
162 OPENSSL_RSA_MAX_MODULUS_BITS, num);
d9f07357 163 } else if (!opt_check_rest_arg(NULL)) {
c27363f5
RS
164 goto opthelp;
165 }
a3fe382e 166
3ad60309
DDO
167 if (!app_RAND_load())
168 goto end;
169
c27363f5 170 private = 1;
d9f07357
DDO
171 if (!opt_cipher(ciphername, &enc))
172 goto end;
7e1b7485 173 if (!app_passwd(NULL, passoutarg, NULL, &passout)) {
0f113f3e 174 BIO_printf(bio_err, "Error getting password\n");
7e1b7485 175 goto end;
0f113f3e 176 }
5270e702 177
bdd58d98 178 out = bio_open_owner(outfile, FORMAT_PEM, private);
7e1b7485
RS
179 if (out == NULL)
180 goto end;
d02b48c6 181
b2ccfd81
CL
182 if (!init_gen_str(&ctx, "RSA", eng, 0, app_get0_libctx(),
183 app_get0_propq()))
8f7e1f68
P
184 goto end;
185
a414fd67
PP
186 if (verbose)
187 EVP_PKEY_CTX_set_cb(ctx, progress_cb);
8f7e1f68
P
188 EVP_PKEY_CTX_set_app_data(ctx, bio_err);
189
190 if (EVP_PKEY_CTX_set_rsa_keygen_bits(ctx, num) <= 0) {
191 BIO_printf(bio_err, "Error setting RSA length\n");
192 goto end;
193 }
194 if (!BN_set_word(bn, f4)) {
195 BIO_printf(bio_err, "Error allocating RSA public exponent\n");
196 goto end;
197 }
3786d748 198 if (EVP_PKEY_CTX_set1_rsa_keygen_pubexp(ctx, bn) <= 0) {
8f7e1f68
P
199 BIO_printf(bio_err, "Error setting RSA public exponent\n");
200 goto end;
201 }
202 if (EVP_PKEY_CTX_set_rsa_keygen_primes(ctx, primes) <= 0) {
203 BIO_printf(bio_err, "Error setting number of primes\n");
204 goto end;
205 }
a7e4ca5b 206 pkey = app_keygen(ctx, "RSA", num, verbose);
8c040c08
BE
207 if (pkey == NULL)
208 goto end;
dc03504d 209
8f7e1f68 210 if (verbose) {
d7e498ac
RL
211 BIGNUM *e = NULL;
212
213 /* Every RSA key has an 'e' */
214 EVP_PKEY_get_bn_param(pkey, "e", &e);
215 if (e == NULL) {
8f7e1f68
P
216 BIO_printf(bio_err, "Error cannot access RSA e\n");
217 goto end;
218 }
219 hexe = BN_bn2hex(e);
220 dece = BN_bn2dec(e);
221 if (hexe && dece) {
222 BIO_printf(bio_err, "e is %s (0x%s)\n", dece, hexe);
223 }
224 OPENSSL_free(hexe);
225 OPENSSL_free(dece);
d7e498ac 226 BN_free(e);
0f113f3e 227 }
10203a34
KR
228 if (traditional) {
229 if (!PEM_write_bio_PrivateKey_traditional(out, pkey, enc, NULL, 0,
230 NULL, passout))
231 goto end;
232 } else {
233 if (!PEM_write_bio_PrivateKey(out, pkey, enc, NULL, 0, NULL, passout))
234 goto end;
235 }
d02b48c6 236
0f113f3e 237 ret = 0;
7e1b7485 238 end:
23a1d5e9
RS
239 BN_free(bn);
240 BN_GENCB_free(cb);
8f7e1f68
P
241 EVP_PKEY_CTX_free(ctx);
242 EVP_PKEY_free(pkey);
606a417f 243 EVP_CIPHER_free(enc);
ca3a82c3 244 BIO_free_all(out);
dd1abd44 245 release_engine(eng);
b548a1f1 246 OPENSSL_free(passout);
0f113f3e
MC
247 if (ret != 0)
248 ERR_print_errors(bio_err);
26a7d938 249 return ret;
0f113f3e 250}
d02b48c6 251