Remove -C from dhparam,dsaparam,ecparam
[openssl.git] / apps / dhparam.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 #ifndef OPENSSL_NO_DEPRECATED_3_0
11 /* We need to use some deprecated APIs */
12 # define OPENSSL_SUPPRESS_DEPRECATED
13 #endif
14 #include <openssl/opensslconf.h>
15
16 #include <stdio.h>
17 #include <stdlib.h>
18 #include <time.h>
19 #include <string.h>
20 #include "apps.h"
21 #include "progs.h"
22 #include <openssl/bio.h>
23 #include <openssl/err.h>
24 #include <openssl/bn.h>
25 #include <openssl/dh.h>
26 #include <openssl/x509.h>
27 #include <openssl/pem.h>
28
29 #if !defined(OPENSSL_NO_DSA) && !defined(OPENSSL_NO_DEPRECATED_3_0)
30 # include <openssl/dsa.h>
31 #endif
32
33 #define DEFBITS 2048
34
35 #if !defined(OPENSSL_NO_DSA) && !defined(OPENSSL_NO_DEPRECATED_3_0)
36 static int dh_cb(int p, int n, BN_GENCB *cb);
37 #endif
38 static int gendh_cb(EVP_PKEY_CTX *ctx);
39
40 typedef enum OPTION_choice {
41     OPT_ERR = -1, OPT_EOF = 0, OPT_HELP,
42     OPT_INFORM, OPT_OUTFORM, OPT_IN, OPT_OUT,
43     OPT_ENGINE, OPT_CHECK, OPT_TEXT, OPT_NOOUT,
44     OPT_DSAPARAM, OPT_2, OPT_3, OPT_5,
45     OPT_R_ENUM, OPT_PROV_ENUM
46 } OPTION_CHOICE;
47
48 const OPTIONS dhparam_options[] = {
49     {OPT_HELP_STR, 1, '-', "Usage: %s [options] [numbits]\n"},
50
51     OPT_SECTION("General"),
52     {"help", OPT_HELP, '-', "Display this summary"},
53     {"check", OPT_CHECK, '-', "Check the DH parameters"},
54 #ifndef OPENSSL_NO_DSA
55     {"dsaparam", OPT_DSAPARAM, '-',
56      "Read or generate DSA parameters, convert to DH"},
57 #endif
58 #ifndef OPENSSL_NO_ENGINE
59     {"engine", OPT_ENGINE, 's', "Use engine e, possibly a hardware device"},
60 #endif
61
62     OPT_SECTION("Input"),
63     {"in", OPT_IN, '<', "Input file"},
64     {"inform", OPT_INFORM, 'F', "Input format, DER or PEM"},
65
66     OPT_SECTION("Output"),
67     {"out", OPT_OUT, '>', "Output file"},
68     {"outform", OPT_OUTFORM, 'F', "Output format, DER or PEM"},
69     {"text", OPT_TEXT, '-', "Print a text form of the DH parameters"},
70     {"noout", OPT_NOOUT, '-', "Don't output any DH parameters"},
71     {"2", OPT_2, '-', "Generate parameters using 2 as the generator value"},
72     {"3", OPT_3, '-', "Generate parameters using 3 as the generator value"},
73     {"5", OPT_5, '-', "Generate parameters using 5 as the generator value"},
74
75     OPT_R_OPTIONS,
76     OPT_PROV_OPTIONS,
77
78     OPT_PARAMETERS(),
79     {"numbits", 0, 0, "Number of bits if generating parameters (optional)"},
80     {NULL}
81 };
82
83 int dhparam_main(int argc, char **argv)
84 {
85     BIO *in = NULL, *out = NULL;
86     DH *dh = NULL, *alloc_dh = NULL;
87     EVP_PKEY *pkey = NULL;
88     EVP_PKEY_CTX *ctx = NULL;
89     char *infile = NULL, *outfile = NULL, *prog;
90     ENGINE *e = NULL;
91 #if !defined(OPENSSL_NO_DSA) && !defined(OPENSSL_NO_DEPRECATED_3_0)
92     int dsaparam = 0;
93 #endif
94     int i, text = 0, ret = 1, num = 0, g = 0;
95     int informat = FORMAT_PEM, outformat = FORMAT_PEM, check = 0, noout = 0;
96     OPTION_CHOICE o;
97
98     prog = opt_init(argc, argv, dhparam_options);
99     while ((o = opt_next()) != OPT_EOF) {
100         switch (o) {
101         case OPT_EOF:
102         case OPT_ERR:
103  opthelp:
104             BIO_printf(bio_err, "%s: Use -help for summary.\n", prog);
105             goto end;
106         case OPT_HELP:
107             opt_help(dhparam_options);
108             ret = 0;
109             goto end;
110         case OPT_INFORM:
111             if (!opt_format(opt_arg(), OPT_FMT_PEMDER, &informat))
112                 goto opthelp;
113             break;
114         case OPT_OUTFORM:
115             if (!opt_format(opt_arg(), OPT_FMT_PEMDER, &outformat))
116                 goto opthelp;
117             break;
118         case OPT_IN:
119             infile = opt_arg();
120             break;
121         case OPT_OUT:
122             outfile = opt_arg();
123             break;
124         case OPT_ENGINE:
125             e = setup_engine(opt_arg(), 0);
126             break;
127         case OPT_CHECK:
128             check = 1;
129             break;
130         case OPT_TEXT:
131             text = 1;
132             break;
133         case OPT_DSAPARAM:
134 #ifndef OPENSSL_NO_DSA
135 # ifdef OPENSSL_NO_DEPRECATED_3_0
136             BIO_printf(bio_err, "The dsaparam option is deprecated.\n");
137 # else
138             dsaparam = 1;
139 # endif
140 #endif
141             break;
142         case OPT_2:
143             g = 2;
144             break;
145         case OPT_3:
146             g = 3;
147             break;
148         case OPT_5:
149             g = 5;
150             break;
151         case OPT_NOOUT:
152             noout = 1;
153             break;
154         case OPT_R_CASES:
155             if (!opt_rand(o))
156                 goto end;
157             break;
158         case OPT_PROV_CASES:
159             if (!opt_provider(o))
160                 goto end;
161             break;
162         }
163     }
164     argc = opt_num_rest();
165     argv = opt_rest();
166
167     if (argv[0] != NULL && (!opt_int(argv[0], &num) || num <= 0))
168         goto end;
169
170     if (g && !num)
171         num = DEFBITS;
172
173 #if !defined(OPENSSL_NO_DSA) && !defined(OPENSSL_NO_DEPRECATED_3_0)
174     if (dsaparam && g) {
175         BIO_printf(bio_err,
176                    "Error, generator may not be chosen for DSA parameters\n");
177         goto end;
178     }
179 #endif
180
181     out = bio_open_default(outfile, 'w', outformat);
182     if (out == NULL)
183         goto end;
184
185     /* DH parameters */
186     if (num && !g)
187         g = 2;
188
189     if (num) {
190
191
192 #if !defined(OPENSSL_NO_DSA) && !defined(OPENSSL_NO_DEPRECATED_3_0)
193         if (dsaparam) {
194             DSA *dsa = DSA_new();
195             BN_GENCB *cb  = BN_GENCB_new();
196
197             if (cb == NULL)
198                 goto end;
199
200             BN_GENCB_set(cb, dh_cb, bio_err);
201
202             BIO_printf(bio_err,
203                        "Generating DSA parameters, %d bit long prime\n", num);
204             if (dsa == NULL
205                 || !DSA_generate_parameters_ex(dsa, num, NULL, 0, NULL, NULL,
206                                                cb)) {
207                 DSA_free(dsa);
208                 BN_GENCB_free(cb);
209                 BIO_printf(bio_err, "Error, unable to generate DSA parameters\n");
210                 goto end;
211             }
212
213             dh = alloc_dh = DSA_dup_DH(dsa);
214             DSA_free(dsa);
215             BN_GENCB_free(cb);
216             if (dh == NULL)
217                 goto end;
218         } else
219 #endif
220         {
221             ctx = EVP_PKEY_CTX_new_from_name(NULL, "DH", NULL);
222             if (ctx == NULL) {
223                 BIO_printf(bio_err,
224                            "Error, DH key generation context allocation failed\n");
225                 goto end;
226             }
227             EVP_PKEY_CTX_set_cb(ctx, gendh_cb);
228             EVP_PKEY_CTX_set_app_data(ctx, bio_err);
229             BIO_printf(bio_err,
230                        "Generating DH parameters, %d bit long safe prime, generator %d\n",
231                        num, g);
232             BIO_printf(bio_err, "This is going to take a long time\n");
233             if (!EVP_PKEY_paramgen_init(ctx)) {
234                 BIO_printf(bio_err,
235                            "Error, unable to initialise DH param generation\n");
236                 goto end;
237             }
238
239             if (!EVP_PKEY_CTX_set_dh_paramgen_prime_len(ctx, num)) {
240                 BIO_printf(bio_err, "Error, unable to set DH prime length\n");
241                 goto end;
242             }
243             if (!EVP_PKEY_paramgen(ctx, &pkey)) {
244                 BIO_printf(bio_err, "Error, DH generation failed\n");
245                 goto end;
246             }
247             dh = EVP_PKEY_get0_DH(pkey);
248         }
249     } else {
250         in = bio_open_default(infile, 'r', informat);
251         if (in == NULL)
252             goto end;
253
254 #if !defined(OPENSSL_NO_DSA) && !defined(OPENSSL_NO_DEPRECATED_3_0)
255         if (dsaparam) {
256             DSA *dsa;
257
258             if (informat == FORMAT_ASN1)
259                 dsa = d2i_DSAparams_bio(in, NULL);
260             else                /* informat == FORMAT_PEM */
261                 dsa = PEM_read_bio_DSAparams(in, NULL, NULL, NULL);
262
263             if (dsa == NULL) {
264                 BIO_printf(bio_err, "Error, unable to load DSA parameters\n");
265                 goto end;
266             }
267
268             dh = alloc_dh = DSA_dup_DH(dsa);
269             DSA_free(dsa);
270             if (dh == NULL)
271                 goto end;
272         } else
273 #endif
274         {
275             if (informat == FORMAT_ASN1) {
276                 /*
277                  * We have no PEM header to determine what type of DH params it
278                  * is. We'll just try both.
279                  */
280                 dh = alloc_dh = ASN1_d2i_bio_of(DH, DH_new, d2i_DHparams, in, NULL);
281                 /* BIO_reset() returns 0 for success for file BIOs only!!! */
282                 if (dh == NULL && BIO_reset(in) == 0)
283                     dh = alloc_dh = ASN1_d2i_bio_of(DH, DH_new, d2i_DHxparams, in, NULL);
284             } else {
285                 /* informat == FORMAT_PEM */
286                 dh = alloc_dh = PEM_read_bio_DHparams(in, NULL, NULL, NULL);
287             }
288
289             if (dh == NULL) {
290                 BIO_printf(bio_err, "Error, unable to load DH parameters\n");
291                 goto end;
292             }
293         }
294         /* dh != NULL */
295     }
296
297     if (text)
298         EVP_PKEY_print_params(out, pkey, 4, NULL);
299
300     if (check) {
301         if (!EVP_PKEY_param_check(ctx) /* DH_check(dh, &i) */) {
302             BIO_printf(bio_err, "Error, invalid parameters generated\n");
303             goto end;
304         }
305         BIO_printf(bio_err, "DH parameters appear to be ok.\n");
306         if (num != 0) {
307             /*
308              * We have generated parameters but DH_check() indicates they are
309              * invalid! This should never happen!
310              */
311             BIO_printf(bio_err, "Error, invalid parameters generated\n");
312             goto end;
313         }
314     }
315
316     if (!noout) {
317         const BIGNUM *q;
318         DH_get0_pqg(dh, NULL, &q, NULL);
319         if (outformat == FORMAT_ASN1) {
320             if (q != NULL)
321                 i = ASN1_i2d_bio_of(DH, i2d_DHxparams, out, dh);
322             else
323                 i = ASN1_i2d_bio_of(DH, i2d_DHparams, out, dh);
324         } else if (q != NULL) {
325             i = PEM_write_bio_DHxparams(out, dh);
326         } else {
327             i = PEM_write_bio_DHparams(out, dh);
328         }
329         if (!i) {
330             BIO_printf(bio_err, "Error, unable to write DH parameters\n");
331             goto end;
332         }
333     }
334     ret = 0;
335  end:
336     if (ret != 0)
337         ERR_print_errors(bio_err);
338     DH_free(alloc_dh);
339     BIO_free(in);
340     BIO_free_all(out);
341     EVP_PKEY_free(pkey);
342     EVP_PKEY_CTX_free(ctx);
343     release_engine(e);
344     return ret;
345 }
346
347 static int common_dh_cb(int p, BIO *b)
348 {
349     static const char symbols[] = ".+*\n";
350     char c = (p >= 0 && (size_t)p < sizeof(symbols) - 1) ? symbols[p] : '?';
351
352     BIO_write(b, &c, 1);
353     (void)BIO_flush(b);
354     return 1;
355 }
356
357 #if !defined(OPENSSL_NO_DSA) && !defined(OPENSSL_NO_DEPRECATED_3_0)
358 static int dh_cb(int p, int n, BN_GENCB *cb)
359 {
360     return common_dh_cb(p, BN_GENCB_get_arg(cb));
361 }
362 #endif
363
364 static int gendh_cb(EVP_PKEY_CTX *ctx)
365 {
366     return common_dh_cb(EVP_PKEY_CTX_get_keygen_info(ctx, 0),
367                         EVP_PKEY_CTX_get_app_data(ctx));
368 }