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