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