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