Add "sections" to -help output
[openssl.git] / apps / dhparam.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_DH
12 NON_EMPTY_TRANSLATION_UNIT
13 #else
14
15 # include <stdio.h>
16 # include <stdlib.h>
17 # include <time.h>
18 # include <string.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/dh.h>
25 # include <openssl/x509.h>
26 # include <openssl/pem.h>
27
28 # ifndef OPENSSL_NO_DSA
29 #  include <openssl/dsa.h>
30 # endif
31
32 # define DEFBITS 2048
33
34 static int dh_cb(int p, int n, BN_GENCB *cb);
35
36 typedef enum OPTION_choice {
37     OPT_ERR = -1, OPT_EOF = 0, OPT_HELP,
38     OPT_INFORM, OPT_OUTFORM, OPT_IN, OPT_OUT,
39     OPT_ENGINE, OPT_CHECK, OPT_TEXT, OPT_NOOUT,
40     OPT_DSAPARAM, OPT_C, OPT_2, OPT_3, OPT_5,
41     OPT_R_ENUM
42 } OPTION_CHOICE;
43
44 const OPTIONS dhparam_options[] = {
45     {OPT_HELP_STR, 1, '-', "Usage: %s [flags] [numbits]\n"},
46
47     OPT_SECTION("General"),
48     {"help", OPT_HELP, '-', "Display this summary"},
49     {"check", OPT_CHECK, '-', "Check the DH parameters"},
50 # ifndef OPENSSL_NO_DSA
51     {"dsaparam", OPT_DSAPARAM, '-',
52      "Read or generate DSA parameters, convert to DH"},
53 # endif
54 # ifndef OPENSSL_NO_ENGINE
55     {"engine", OPT_ENGINE, 's', "Use engine e, possibly a hardware device"},
56 # endif
57
58     OPT_SECTION("Input"),
59     {"in", OPT_IN, '<', "Input file"},
60     {"inform", OPT_INFORM, 'F', "Input format, DER or PEM"},
61
62     OPT_SECTION("Output"),
63     {"out", OPT_OUT, '>', "Output file"},
64     {"outform", OPT_OUTFORM, 'F', "Output format, DER or PEM"},
65     {"text", OPT_TEXT, '-', "Print a text form of the DH parameters"},
66     {"noout", OPT_NOOUT, '-', "Don't output any DH parameters"},
67     {"C", OPT_C, '-', "Print C code"},
68     {"2", OPT_2, '-', "Generate parameters using 2 as the generator value"},
69     {"3", OPT_3, '-', "Generate parameters using 3 as the generator value"},
70     {"5", OPT_5, '-', "Generate parameters using 5 as the generator value"},
71
72     OPT_R_OPTIONS,
73     {NULL}
74 };
75
76 int dhparam_main(int argc, char **argv)
77 {
78     BIO *in = NULL, *out = NULL;
79     DH *dh = NULL;
80     char *infile = NULL, *outfile = NULL, *prog;
81     ENGINE *e = NULL;
82 #ifndef OPENSSL_NO_DSA
83     int dsaparam = 0;
84 #endif
85     int i, text = 0, C = 0, ret = 1, num = 0, g = 0;
86     int informat = FORMAT_PEM, outformat = FORMAT_PEM, check = 0, noout = 0;
87     OPTION_CHOICE o;
88
89     prog = opt_init(argc, argv, dhparam_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             opt_help(dhparam_options);
99             ret = 0;
100             goto end;
101         case OPT_INFORM:
102             if (!opt_format(opt_arg(), OPT_FMT_PEMDER, &informat))
103                 goto opthelp;
104             break;
105         case OPT_OUTFORM:
106             if (!opt_format(opt_arg(), OPT_FMT_PEMDER, &outformat))
107                 goto opthelp;
108             break;
109         case OPT_IN:
110             infile = opt_arg();
111             break;
112         case OPT_OUT:
113             outfile = opt_arg();
114             break;
115         case OPT_ENGINE:
116             e = setup_engine(opt_arg(), 0);
117             break;
118         case OPT_CHECK:
119             check = 1;
120             break;
121         case OPT_TEXT:
122             text = 1;
123             break;
124         case OPT_DSAPARAM:
125 #ifndef OPENSSL_NO_DSA
126             dsaparam = 1;
127 #endif
128             break;
129         case OPT_C:
130             C = 1;
131             break;
132         case OPT_2:
133             g = 2;
134             break;
135         case OPT_3:
136             g = 3;
137             break;
138         case OPT_5:
139             g = 5;
140             break;
141         case OPT_NOOUT:
142             noout = 1;
143             break;
144         case OPT_R_CASES:
145             if (!opt_rand(o))
146                 goto end;
147             break;
148         }
149     }
150     argc = opt_num_rest();
151     argv = opt_rest();
152
153     if (argv[0] != NULL && (!opt_int(argv[0], &num) || num <= 0))
154         goto end;
155
156     if (g && !num)
157         num = DEFBITS;
158
159 # ifndef OPENSSL_NO_DSA
160     if (dsaparam && g) {
161         BIO_printf(bio_err,
162                    "generator may not be chosen for DSA parameters\n");
163         goto end;
164     }
165 # endif
166
167     out = bio_open_default(outfile, 'w', outformat);
168     if (out == NULL)
169         goto end;
170
171     /* DH parameters */
172     if (num && !g)
173         g = 2;
174
175     if (num) {
176
177         BN_GENCB *cb;
178         cb = BN_GENCB_new();
179         if (cb == NULL) {
180             ERR_print_errors(bio_err);
181             goto end;
182         }
183
184         BN_GENCB_set(cb, dh_cb, bio_err);
185
186 # ifndef OPENSSL_NO_DSA
187         if (dsaparam) {
188             DSA *dsa = DSA_new();
189
190             BIO_printf(bio_err,
191                        "Generating DSA parameters, %d bit long prime\n", num);
192             if (dsa == NULL
193                 || !DSA_generate_parameters_ex(dsa, num, NULL, 0, NULL, NULL,
194                                                cb)) {
195                 DSA_free(dsa);
196                 BN_GENCB_free(cb);
197                 ERR_print_errors(bio_err);
198                 goto end;
199             }
200
201             dh = DSA_dup_DH(dsa);
202             DSA_free(dsa);
203             if (dh == NULL) {
204                 BN_GENCB_free(cb);
205                 ERR_print_errors(bio_err);
206                 goto end;
207             }
208         } else
209 # endif
210         {
211             dh = DH_new();
212             BIO_printf(bio_err,
213                        "Generating DH parameters, %d bit long safe prime, generator %d\n",
214                        num, g);
215             BIO_printf(bio_err, "This is going to take a long time\n");
216             if (dh == NULL || !DH_generate_parameters_ex(dh, num, g, cb)) {
217                 BN_GENCB_free(cb);
218                 ERR_print_errors(bio_err);
219                 goto end;
220             }
221         }
222
223         BN_GENCB_free(cb);
224     } else {
225
226         in = bio_open_default(infile, 'r', informat);
227         if (in == NULL)
228             goto end;
229
230 # ifndef OPENSSL_NO_DSA
231         if (dsaparam) {
232             DSA *dsa;
233
234             if (informat == FORMAT_ASN1)
235                 dsa = d2i_DSAparams_bio(in, NULL);
236             else                /* informat == FORMAT_PEM */
237                 dsa = PEM_read_bio_DSAparams(in, NULL, NULL, NULL);
238
239             if (dsa == NULL) {
240                 BIO_printf(bio_err, "unable to load DSA parameters\n");
241                 ERR_print_errors(bio_err);
242                 goto end;
243             }
244
245             dh = DSA_dup_DH(dsa);
246             DSA_free(dsa);
247             if (dh == NULL) {
248                 ERR_print_errors(bio_err);
249                 goto end;
250             }
251         } else
252 # endif
253         {
254             if (informat == FORMAT_ASN1) {
255                 /*
256                  * We have no PEM header to determine what type of DH params it
257                  * is. We'll just try both.
258                  */
259                 dh = d2i_DHparams_bio(in, NULL);
260                 /* BIO_reset() returns 0 for success for file BIOs only!!! */
261                 if (dh == NULL && BIO_reset(in) == 0)
262                     dh = d2i_DHxparams_bio(in, NULL);
263             } else {
264                 /* informat == FORMAT_PEM */
265                 dh = PEM_read_bio_DHparams(in, NULL, NULL, NULL);
266             }
267
268             if (dh == NULL) {
269                 BIO_printf(bio_err, "unable to load DH parameters\n");
270                 ERR_print_errors(bio_err);
271                 goto end;
272             }
273         }
274
275         /* dh != NULL */
276     }
277
278     if (text) {
279         DHparams_print(out, dh);
280     }
281
282     if (check) {
283         if (!DH_check(dh, &i)) {
284             ERR_print_errors(bio_err);
285             goto end;
286         }
287         if (i & DH_CHECK_P_NOT_PRIME)
288             BIO_printf(bio_err, "WARNING: p value is not prime\n");
289         if (i & DH_CHECK_P_NOT_SAFE_PRIME)
290             BIO_printf(bio_err, "WARNING: p value is not a safe prime\n");
291         if (i & DH_CHECK_Q_NOT_PRIME)
292             BIO_printf(bio_err, "WARNING: q value is not a prime\n");
293         if (i & DH_CHECK_INVALID_Q_VALUE)
294             BIO_printf(bio_err, "WARNING: q value is invalid\n");
295         if (i & DH_CHECK_INVALID_J_VALUE)
296             BIO_printf(bio_err, "WARNING: j value is invalid\n");
297         if (i & DH_UNABLE_TO_CHECK_GENERATOR)
298             BIO_printf(bio_err,
299                        "WARNING: unable to check the generator value\n");
300         if (i & DH_NOT_SUITABLE_GENERATOR)
301             BIO_printf(bio_err, "WARNING: the g value is not a generator\n");
302         if (i == 0)
303             BIO_printf(bio_err, "DH parameters appear to be ok.\n");
304         if (num != 0 && i != 0) {
305             /*
306              * We have generated parameters but DH_check() indicates they are
307              * invalid! This should never happen!
308              */
309             BIO_printf(bio_err, "ERROR: Invalid parameters generated\n");
310             goto end;
311         }
312     }
313     if (C) {
314         unsigned char *data;
315         int len, bits;
316         const BIGNUM *pbn, *gbn;
317
318         len = DH_size(dh);
319         bits = DH_bits(dh);
320         DH_get0_pqg(dh, &pbn, NULL, &gbn);
321         data = app_malloc(len, "print a BN");
322
323         BIO_printf(out, "static DH *get_dh%d(void)\n{\n", bits);
324         print_bignum_var(out, pbn, "dhp", bits, data);
325         print_bignum_var(out, gbn, "dhg", bits, data);
326         BIO_printf(out, "    DH *dh = DH_new();\n"
327                         "    BIGNUM *p, *g;\n"
328                         "\n"
329                         "    if (dh == NULL)\n"
330                         "        return NULL;\n");
331         BIO_printf(out, "    p = BN_bin2bn(dhp_%d, sizeof(dhp_%d), NULL);\n",
332                    bits, bits);
333         BIO_printf(out, "    g = BN_bin2bn(dhg_%d, sizeof(dhg_%d), NULL);\n",
334                    bits, bits);
335         BIO_printf(out, "    if (p == NULL || g == NULL\n"
336                         "            || !DH_set0_pqg(dh, p, NULL, g)) {\n"
337                         "        DH_free(dh);\n"
338                         "        BN_free(p);\n"
339                         "        BN_free(g);\n"
340                         "        return NULL;\n"
341                         "    }\n");
342         if (DH_get_length(dh) > 0)
343             BIO_printf(out,
344                         "    if (!DH_set_length(dh, %ld)) {\n"
345                         "        DH_free(dh);\n"
346                         "        return NULL;\n"
347                         "    }\n", DH_get_length(dh));
348         BIO_printf(out, "    return dh;\n}\n");
349         OPENSSL_free(data);
350     }
351
352     if (!noout) {
353         const BIGNUM *q;
354         DH_get0_pqg(dh, NULL, &q, NULL);
355         if (outformat == FORMAT_ASN1) {
356             if (q != NULL)
357                 i = i2d_DHxparams_bio(out, dh);
358             else
359                 i = i2d_DHparams_bio(out, dh);
360         } else if (q != NULL) {
361             i = PEM_write_bio_DHxparams(out, dh);
362         } else {
363             i = PEM_write_bio_DHparams(out, dh);
364         }
365         if (!i) {
366             BIO_printf(bio_err, "unable to write DH parameters\n");
367             ERR_print_errors(bio_err);
368             goto end;
369         }
370     }
371     ret = 0;
372  end:
373     BIO_free(in);
374     BIO_free_all(out);
375     DH_free(dh);
376     release_engine(e);
377     return ret;
378 }
379
380 static int dh_cb(int p, int n, BN_GENCB *cb)
381 {
382     static const char symbols[] = ".+*\n";
383     char c = (p >= 0 && (size_t)p < sizeof(symbols) - 1) ? symbols[p] : '?';
384
385     BIO_write(BN_GENCB_get_arg(cb), &c, 1);
386     (void)BIO_flush(BN_GENCB_get_arg(cb));
387     return 1;
388 }
389 #endif