Don't compile commands if disabled
[openssl.git] / apps / genrsa.c
1 /*
2  * Copyright 1995-2018 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 /* We need to use the deprecated RSA low level calls */
11 #define OPENSSL_SUPPRESS_DEPRECATED
12
13 #include <openssl/opensslconf.h>
14
15 #include <stdio.h>
16 #include <string.h>
17 #include <sys/types.h>
18 #include <sys/stat.h>
19 #include "apps.h"
20 #include "progs.h"
21 #include <openssl/bio.h>
22 #include <openssl/err.h>
23 #include <openssl/bn.h>
24 #include <openssl/rsa.h>
25 #include <openssl/evp.h>
26 #include <openssl/x509.h>
27 #include <openssl/pem.h>
28 #include <openssl/rand.h>
29
30 #define DEFBITS 2048
31 #define DEFPRIMES 2
32
33 static int verbose = 0;
34
35 static int genrsa_cb(int p, int n, BN_GENCB *cb);
36
37 typedef enum OPTION_choice {
38     OPT_ERR = -1, OPT_EOF = 0, OPT_HELP,
39     OPT_3, 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     {"3", OPT_3, '-', "Use 3 for the E value"},
55     {"F4", OPT_F4, '-', "Use F4 (0x10001) for the E value"},
56     {"f4", OPT_F4, '-', "Use F4 (0x10001) for the E value"},
57
58     OPT_SECTION("Output"),
59     {"out", OPT_OUT, '>', "Output the key to specified file"},
60     {"passout", OPT_PASSOUT, 's', "Output file pass phrase source"},
61     {"primes", OPT_PRIMES, 'p', "Specify number of primes"},
62     {"verbose", OPT_VERBOSE, '-', "Verbose output"},
63     {"", OPT_CIPHER, '-', "Encrypt the output with any supported cipher"},
64
65     OPT_R_OPTIONS,
66     OPT_PROV_OPTIONS,
67
68     OPT_PARAMETERS(),
69     {"numbits", 0, 0, "Size of key in bits"},
70     {NULL}
71 };
72
73 int genrsa_main(int argc, char **argv)
74 {
75     BN_GENCB *cb = BN_GENCB_new();
76     PW_CB_DATA cb_data;
77     ENGINE *eng = NULL;
78     BIGNUM *bn = BN_new();
79     BIO *out = NULL;
80     const BIGNUM *e;
81     RSA *rsa = NULL;
82     const EVP_CIPHER *enc = NULL;
83     int ret = 1, num = DEFBITS, private = 0, primes = DEFPRIMES;
84     unsigned long f4 = RSA_F4;
85     char *outfile = NULL, *passoutarg = NULL, *passout = NULL;
86     char *prog, *hexe, *dece;
87     OPTION_CHOICE o;
88
89     if (bn == NULL || cb == NULL)
90         goto end;
91
92     BN_GENCB_set(cb, genrsa_cb, bio_err);
93
94     prog = opt_init(argc, argv, genrsa_options);
95     while ((o = opt_next()) != OPT_EOF) {
96         switch (o) {
97         case OPT_EOF:
98         case OPT_ERR:
99 opthelp:
100             BIO_printf(bio_err, "%s: Use -help for summary.\n", prog);
101             goto end;
102         case OPT_HELP:
103             ret = 0;
104             opt_help(genrsa_options);
105             goto end;
106         case OPT_3:
107             f4 = 3;
108             break;
109         case OPT_F4:
110             f4 = RSA_F4;
111             break;
112         case OPT_OUT:
113             outfile = opt_arg();
114             break;
115         case OPT_ENGINE:
116             eng = setup_engine(opt_arg(), 0);
117             break;
118         case OPT_R_CASES:
119             if (!opt_rand(o))
120                 goto end;
121             break;
122         case OPT_PROV_CASES:
123             if (!opt_provider(o))
124                 goto end;
125             break;
126         case OPT_PASSOUT:
127             passoutarg = opt_arg();
128             break;
129         case OPT_CIPHER:
130             if (!opt_cipher(opt_unknown(), &enc))
131                 goto end;
132             break;
133         case OPT_PRIMES:
134             if (!opt_int(opt_arg(), &primes))
135                 goto end;
136             break;
137         case OPT_VERBOSE:
138             verbose = 1;
139             break;
140         }
141     }
142     argc = opt_num_rest();
143     argv = opt_rest();
144
145     if (argc == 1) {
146         if (!opt_int(argv[0], &num) || num <= 0)
147             goto end;
148         if (num > OPENSSL_RSA_MAX_MODULUS_BITS)
149             BIO_printf(bio_err,
150                        "Warning: It is not recommended to use more than %d bit for RSA keys.\n"
151                        "         Your key size is %d! Larger key size may behave not as expected.\n",
152                        OPENSSL_RSA_MAX_MODULUS_BITS, num);
153     } else if (argc > 0) {
154         BIO_printf(bio_err, "Extra arguments given.\n");
155         goto opthelp;
156     }
157
158     private = 1;
159     if (!app_passwd(NULL, passoutarg, NULL, &passout)) {
160         BIO_printf(bio_err, "Error getting password\n");
161         goto end;
162     }
163
164     out = bio_open_owner(outfile, FORMAT_PEM, private);
165     if (out == NULL)
166         goto end;
167
168     if (verbose)
169         BIO_printf(bio_err, "Generating RSA private key, %d bit long modulus (%d primes)\n",
170                    num, primes);
171     rsa = eng ? RSA_new_method(eng) : RSA_new();
172     if (rsa == NULL)
173         goto end;
174
175     if (!BN_set_word(bn, f4)
176         || !RSA_generate_multi_prime_key(rsa, num, primes, bn, cb))
177         goto end;
178
179     RSA_get0_key(rsa, NULL, &e, NULL);
180     hexe = BN_bn2hex(e);
181     dece = BN_bn2dec(e);
182     if (hexe && dece && verbose) {
183         BIO_printf(bio_err, "e is %s (0x%s)\n", dece, hexe);
184     }
185     OPENSSL_free(hexe);
186     OPENSSL_free(dece);
187     cb_data.password = passout;
188     cb_data.prompt_info = outfile;
189     assert(private);
190     if (!PEM_write_bio_RSAPrivateKey(out, rsa, enc, NULL, 0,
191                                      (pem_password_cb *)password_callback,
192                                      &cb_data))
193         goto end;
194
195     ret = 0;
196  end:
197     BN_free(bn);
198     BN_GENCB_free(cb);
199     RSA_free(rsa);
200     BIO_free_all(out);
201     release_engine(eng);
202     OPENSSL_free(passout);
203     if (ret != 0)
204         ERR_print_errors(bio_err);
205     return ret;
206 }
207
208 static int genrsa_cb(int p, int n, BN_GENCB *cb)
209 {
210     char c = '*';
211
212     if (!verbose)
213         return 1;
214
215     if (p == 0)
216         c = '.';
217     if (p == 1)
218         c = '+';
219     if (p == 2)
220         c = '*';
221     if (p == 3)
222         c = '\n';
223     BIO_write(BN_GENCB_get_arg(cb), &c, 1);
224     (void)BIO_flush(BN_GENCB_get_arg(cb));
225     return 1;
226 }