Add "sections" to -help output
[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 #include <openssl/opensslconf.h>
11 #ifdef OPENSSL_NO_RSA
12 NON_EMPTY_TRANSLATION_UNIT
13 #else
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
42 } OPTION_CHOICE;
43
44 const OPTIONS genrsa_options[] = {
45
46     OPT_SECTION("General"),
47     {"help", OPT_HELP, '-', "Display this summary"},
48 # ifndef OPENSSL_NO_ENGINE
49     {"engine", OPT_ENGINE, 's', "Use engine, possibly a hardware device"},
50 # endif
51
52     OPT_SECTION("Input"),
53     {"3", OPT_3, '-', "Use 3 for the E value"},
54     {"F4", OPT_F4, '-', "Use F4 (0x10001) for the E value"},
55     {"f4", OPT_F4, '-', "Use F4 (0x10001) for the E value"},
56
57     OPT_SECTION("Output"),
58     {"out", OPT_OUT, '>', "Output the key to specified file"},
59     {"passout", OPT_PASSOUT, 's', "Output file pass phrase source"},
60     {"primes", OPT_PRIMES, 'p', "Specify number of primes"},
61     {"verbose", OPT_VERBOSE, '-', "Verbose output"},
62     {"", OPT_CIPHER, '-', "Encrypt the output with any supported cipher"},
63
64     OPT_R_OPTIONS,
65     {NULL}
66 };
67
68 int genrsa_main(int argc, char **argv)
69 {
70     BN_GENCB *cb = BN_GENCB_new();
71     PW_CB_DATA cb_data;
72     ENGINE *eng = NULL;
73     BIGNUM *bn = BN_new();
74     BIO *out = NULL;
75     const BIGNUM *e;
76     RSA *rsa = NULL;
77     const EVP_CIPHER *enc = NULL;
78     int ret = 1, num = DEFBITS, private = 0, primes = DEFPRIMES;
79     unsigned long f4 = RSA_F4;
80     char *outfile = NULL, *passoutarg = NULL, *passout = NULL;
81     char *prog, *hexe, *dece;
82     OPTION_CHOICE o;
83
84     if (bn == NULL || cb == NULL)
85         goto end;
86
87     BN_GENCB_set(cb, genrsa_cb, bio_err);
88
89     prog = opt_init(argc, argv, genrsa_options);
90     while ((o = opt_next()) != OPT_EOF) {
91         switch (o) {
92         case OPT_EOF:
93         case OPT_ERR:
94 opthelp:
95             BIO_printf(bio_err, "%s: Use -help for summary.\n", prog);
96             goto end;
97         case OPT_HELP:
98             ret = 0;
99             opt_help(genrsa_options);
100             goto end;
101         case OPT_3:
102             f4 = 3;
103             break;
104         case OPT_F4:
105             f4 = RSA_F4;
106             break;
107         case OPT_OUT:
108             outfile = opt_arg();
109             break;
110         case OPT_ENGINE:
111             eng = setup_engine(opt_arg(), 0);
112             break;
113         case OPT_R_CASES:
114             if (!opt_rand(o))
115                 goto end;
116             break;
117         case OPT_PASSOUT:
118             passoutarg = opt_arg();
119             break;
120         case OPT_CIPHER:
121             if (!opt_cipher(opt_unknown(), &enc))
122                 goto end;
123             break;
124         case OPT_PRIMES:
125             if (!opt_int(opt_arg(), &primes))
126                 goto end;
127             break;
128         case OPT_VERBOSE:
129             verbose = 1;
130             break;
131         }
132     }
133     argc = opt_num_rest();
134     argv = opt_rest();
135
136     if (argc == 1) {
137         if (!opt_int(argv[0], &num) || num <= 0)
138             goto end;
139         if (num > OPENSSL_RSA_MAX_MODULUS_BITS)
140             BIO_printf(bio_err,
141                        "Warning: It is not recommended to use more than %d bit for RSA keys.\n"
142                        "         Your key size is %d! Larger key size may behave not as expected.\n",
143                        OPENSSL_RSA_MAX_MODULUS_BITS, num);
144     } else if (argc > 0) {
145         BIO_printf(bio_err, "Extra arguments given.\n");
146         goto opthelp;
147     }
148
149     private = 1;
150     if (!app_passwd(NULL, passoutarg, NULL, &passout)) {
151         BIO_printf(bio_err, "Error getting password\n");
152         goto end;
153     }
154
155     out = bio_open_owner(outfile, FORMAT_PEM, private);
156     if (out == NULL)
157         goto end;
158
159     if (verbose)
160         BIO_printf(bio_err, "Generating RSA private key, %d bit long modulus (%d primes)\n",
161                    num, primes);
162     rsa = eng ? RSA_new_method(eng) : RSA_new();
163     if (rsa == NULL)
164         goto end;
165
166     if (!BN_set_word(bn, f4)
167         || !RSA_generate_multi_prime_key(rsa, num, primes, bn, cb))
168         goto end;
169
170     RSA_get0_key(rsa, NULL, &e, NULL);
171     hexe = BN_bn2hex(e);
172     dece = BN_bn2dec(e);
173     if (hexe && dece && verbose) {
174         BIO_printf(bio_err, "e is %s (0x%s)\n", dece, hexe);
175     }
176     OPENSSL_free(hexe);
177     OPENSSL_free(dece);
178     cb_data.password = passout;
179     cb_data.prompt_info = outfile;
180     assert(private);
181     if (!PEM_write_bio_RSAPrivateKey(out, rsa, enc, NULL, 0,
182                                      (pem_password_cb *)password_callback,
183                                      &cb_data))
184         goto end;
185
186     ret = 0;
187  end:
188     BN_free(bn);
189     BN_GENCB_free(cb);
190     RSA_free(rsa);
191     BIO_free_all(out);
192     release_engine(eng);
193     OPENSSL_free(passout);
194     if (ret != 0)
195         ERR_print_errors(bio_err);
196     return ret;
197 }
198
199 static int genrsa_cb(int p, int n, BN_GENCB *cb)
200 {
201     char c = '*';
202
203     if (!verbose)
204         return 1;
205
206     if (p == 0)
207         c = '.';
208     if (p == 1)
209         c = '+';
210     if (p == 2)
211         c = '*';
212     if (p == 3)
213         c = '\n';
214     BIO_write(BN_GENCB_get_arg(cb), &c, 1);
215     (void)BIO_flush(BN_GENCB_get_arg(cb));
216     return 1;
217 }
218 #endif