include source root directory via -I for libnonfips.a
[oweals/openssl.git] / apps / genrsa.c
1 /*
2  * Copyright 1995-2020 The OpenSSL Project Authors. All Rights Reserved.
3  *
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
8  */
9
10 #include <openssl/opensslconf.h>
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
29
30 static int verbose = 0;
31
32 static int genrsa_cb(EVP_PKEY_CTX *ctx);
33
34 typedef enum OPTION_choice {
35     OPT_ERR = -1, OPT_EOF = 0, OPT_HELP,
36 #ifndef OPENSSL_NO_DEPRECATED_3_0
37     OPT_3,
38 #endif
39     OPT_F4, OPT_ENGINE,
40     OPT_OUT, OPT_PASSOUT, OPT_CIPHER, OPT_PRIMES, OPT_VERBOSE,
41     OPT_R_ENUM, OPT_PROV_ENUM
42 } OPTION_CHOICE;
43
44 const OPTIONS genrsa_options[] = {
45     {OPT_HELP_STR, 1, '-', "Usage: %s [options] numbits\n"},
46
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"},
51 #endif
52
53     OPT_SECTION("Input"),
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"},
59
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     {"", OPT_CIPHER, '-', "Encrypt the output with any supported cipher"},
66
67     OPT_R_OPTIONS,
68     OPT_PROV_OPTIONS,
69
70     OPT_PARAMETERS(),
71     {"numbits", 0, 0, "Size of key in bits"},
72     {NULL}
73 };
74
75 int genrsa_main(int argc, char **argv)
76 {
77     BN_GENCB *cb = BN_GENCB_new();
78     ENGINE *eng = NULL;
79     BIGNUM *bn = BN_new();
80     RSA *rsa;
81     BIO *out = NULL;
82     const BIGNUM *e;
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;
90     OPTION_CHOICE o;
91     unsigned char *ebuf = NULL;
92
93     if (bn == NULL || cb == NULL)
94         goto end;
95
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:
101 opthelp:
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;
108 #ifndef OPENSSL_NO_DEPRECATED_3_0
109         case OPT_3:
110             f4 = RSA_3;
111             break;
112 #endif
113         case OPT_F4:
114             f4 = RSA_F4;
115             break;
116         case OPT_OUT:
117             outfile = opt_arg();
118             break;
119         case OPT_ENGINE:
120             eng = setup_engine(opt_arg(), 0);
121             break;
122         case OPT_R_CASES:
123             if (!opt_rand(o))
124                 goto end;
125             break;
126         case OPT_PROV_CASES:
127             if (!opt_provider(o))
128                 goto end;
129             break;
130         case OPT_PASSOUT:
131             passoutarg = opt_arg();
132             break;
133         case OPT_CIPHER:
134             if (!opt_cipher(opt_unknown(), &enc))
135                 goto end;
136             break;
137         case OPT_PRIMES:
138             if (!opt_int(opt_arg(), &primes))
139                 goto end;
140             break;
141         case OPT_VERBOSE:
142             verbose = 1;
143             break;
144         }
145     }
146     argc = opt_num_rest();
147     argv = opt_rest();
148
149     if (argc == 1) {
150         if (!opt_int(argv[0], &num) || num <= 0)
151             goto end;
152         if (num > OPENSSL_RSA_MAX_MODULUS_BITS)
153             BIO_printf(bio_err,
154                        "Warning: It is not recommended to use more than %d bit for RSA keys.\n"
155                        "         Your key size is %d! Larger key size may behave not as expected.\n",
156                        OPENSSL_RSA_MAX_MODULUS_BITS, num);
157     } else if (argc > 0) {
158         BIO_printf(bio_err, "Extra arguments given.\n");
159         goto opthelp;
160     }
161
162     private = 1;
163     if (!app_passwd(NULL, passoutarg, NULL, &passout)) {
164         BIO_printf(bio_err, "Error getting password\n");
165         goto end;
166     }
167
168     out = bio_open_owner(outfile, FORMAT_PEM, private);
169     if (out == NULL)
170         goto end;
171
172     if (!init_gen_str(&ctx, "RSA", eng, 0))
173         goto end;
174
175     EVP_PKEY_CTX_set_cb(ctx, genrsa_cb);
176     EVP_PKEY_CTX_set_app_data(ctx, bio_err);
177
178     if (EVP_PKEY_CTX_set_rsa_keygen_bits(ctx, num) <= 0) {
179         BIO_printf(bio_err, "Error setting RSA length\n");
180         goto end;
181     }
182     if (!BN_set_word(bn, f4)) {
183         BIO_printf(bio_err, "Error allocating RSA public exponent\n");
184         goto end;
185     }
186     if (EVP_PKEY_CTX_set_rsa_keygen_pubexp(ctx, bn) <= 0) {
187         BIO_printf(bio_err, "Error setting RSA public exponent\n");
188         goto end;
189     }
190     if (EVP_PKEY_CTX_set_rsa_keygen_primes(ctx, primes) <= 0) {
191         BIO_printf(bio_err, "Error setting number of primes\n");
192         goto end;
193     }
194     if (verbose)
195         BIO_printf(bio_err, "Generating RSA private key, %d bit long modulus (%d primes)\n",
196                    num, primes);
197     if (!EVP_PKEY_keygen(ctx, &pkey)) {
198         BIO_printf(bio_err, "Error generating RSA key\n");
199         goto end;
200     }
201
202     if (verbose) {
203         if ((rsa = EVP_PKEY_get0_RSA(pkey)) != NULL) {
204             RSA_get0_key(rsa, NULL, &e, NULL);
205         } else {
206             BIO_printf(bio_err, "Error cannot access RSA e\n");
207             goto end;
208         }
209         hexe = BN_bn2hex(e);
210         dece = BN_bn2dec(e);
211         if (hexe && dece) {
212             BIO_printf(bio_err, "e is %s (0x%s)\n", dece, hexe);
213         }
214         OPENSSL_free(hexe);
215         OPENSSL_free(dece);
216     }
217     if (!PEM_write_bio_PrivateKey(out, pkey, enc, NULL, 0, NULL, passout))
218         goto end;
219
220     ret = 0;
221  end:
222     BN_free(bn);
223     BN_GENCB_free(cb);
224     EVP_PKEY_CTX_free(ctx);
225     EVP_PKEY_free(pkey);
226     BIO_free_all(out);
227     release_engine(eng);
228     OPENSSL_free(passout);
229     OPENSSL_free(ebuf);
230     if (ret != 0)
231         ERR_print_errors(bio_err);
232     return ret;
233 }
234
235 static int genrsa_cb(EVP_PKEY_CTX *ctx)
236 {
237     char c = '*';
238     BIO *b = EVP_PKEY_CTX_get_app_data(ctx);
239     int p = EVP_PKEY_CTX_get_keygen_info(ctx, 0);
240
241     if (!verbose)
242         return 1;
243
244     if (p == 0)
245         c = '.';
246     if (p == 1)
247         c = '+';
248     if (p == 2)
249         c = '*';
250     if (p == 3)
251         c = '\n';
252     BIO_write(b, &c, 1);
253     (void)BIO_flush(b);
254     return 1;
255 }