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