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