]> git.ipfire.org Git - thirdparty/openssl.git/blame - apps/dhparam.c
Add "sections" to -help output
[thirdparty/openssl.git] / apps / dhparam.c
CommitLineData
846e33c7 1/*
6738bf14 2 * Copyright 1995-2018 The OpenSSL Project Authors. All Rights Reserved.
41918458 3 *
dffa7520 4 * Licensed under the Apache License 2.0 (the "License"). You may not use
846e33c7
RS
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
41918458 8 */
09483c58 9
effaf4de
RS
10#include <openssl/opensslconf.h>
11#ifdef OPENSSL_NO_DH
12NON_EMPTY_TRANSLATION_UNIT
13#else
14
0f113f3e
MC
15# include <stdio.h>
16# include <stdlib.h>
17# include <time.h>
18# include <string.h>
19# include "apps.h"
dab2cd68 20# include "progs.h"
0f113f3e
MC
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
41918458 31
0f113f3e 32# define DEFBITS 2048
09483c58 33
6d23cf97 34static int dh_cb(int p, int n, BN_GENCB *cb);
09483c58 35
7e1b7485
RS
36typedef 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,
a38c878c 40 OPT_DSAPARAM, OPT_C, OPT_2, OPT_3, OPT_5,
3ee1eac2 41 OPT_R_ENUM
7e1b7485
RS
42} OPTION_CHOICE;
43
44c83ebd 44const OPTIONS dhparam_options[] = {
7e1b7485 45 {OPT_HELP_STR, 1, '-', "Usage: %s [flags] [numbits]\n"},
5388f986
RS
46
47 OPT_SECTION("General"),
7e1b7485 48 {"help", OPT_HELP, '-', "Display this summary"},
5388f986
RS
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"),
7e1b7485
RS
59 {"in", OPT_IN, '<', "Input file"},
60 {"inform", OPT_INFORM, 'F', "Input format, DER or PEM"},
5388f986
RS
61
62 OPT_SECTION("Output"),
7e1b7485 63 {"out", OPT_OUT, '>', "Output file"},
5388f986 64 {"outform", OPT_OUTFORM, 'F', "Output format, DER or PEM"},
7e1b7485 65 {"text", OPT_TEXT, '-', "Print a text form of the DH parameters"},
16e1b281 66 {"noout", OPT_NOOUT, '-', "Don't output any DH parameters"},
7e1b7485
RS
67 {"C", OPT_C, '-', "Print C code"},
68 {"2", OPT_2, '-', "Generate parameters using 2 as the generator value"},
a38c878c 69 {"3", OPT_3, '-', "Generate parameters using 3 as the generator value"},
7e1b7485 70 {"5", OPT_5, '-', "Generate parameters using 5 as the generator value"},
5388f986
RS
71
72 OPT_R_OPTIONS,
7e1b7485
RS
73 {NULL}
74};
75
76int dhparam_main(int argc, char **argv)
77{
78 BIO *in = NULL, *out = NULL;
79 DH *dh = NULL;
3ee1eac2 80 char *infile = NULL, *outfile = NULL, *prog;
dd1abd44 81 ENGINE *e = NULL;
83ae8124
MC
82#ifndef OPENSSL_NO_DSA
83 int dsaparam = 0;
84#endif
85 int i, text = 0, C = 0, ret = 1, num = 0, g = 0;
7e1b7485
RS
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:
dd1abd44 116 e = setup_engine(opt_arg(), 0);
7e1b7485
RS
117 break;
118 case OPT_CHECK:
0f113f3e 119 check = 1;
7e1b7485
RS
120 break;
121 case OPT_TEXT:
0f113f3e 122 text = 1;
7e1b7485
RS
123 break;
124 case OPT_DSAPARAM:
83ae8124 125#ifndef OPENSSL_NO_DSA
0f113f3e 126 dsaparam = 1;
83ae8124 127#endif
7e1b7485
RS
128 break;
129 case OPT_C:
0f113f3e 130 C = 1;
7e1b7485
RS
131 break;
132 case OPT_2:
0f113f3e 133 g = 2;
7e1b7485 134 break;
a38c878c
BE
135 case OPT_3:
136 g = 3;
137 break;
7e1b7485 138 case OPT_5:
0f113f3e 139 g = 5;
7e1b7485
RS
140 break;
141 case OPT_NOOUT:
142 noout = 1;
143 break;
3ee1eac2
RS
144 case OPT_R_CASES:
145 if (!opt_rand(o))
146 goto end;
7e1b7485
RS
147 break;
148 }
0f113f3e 149 }
7e1b7485
RS
150 argc = opt_num_rest();
151 argv = opt_rest();
0f113f3e 152
2234212c 153 if (argv[0] != NULL && (!opt_int(argv[0], &num) || num <= 0))
0f113f3e 154 goto end;
0f113f3e 155
0f113f3e
MC
156 if (g && !num)
157 num = DEFBITS;
158
159# ifndef OPENSSL_NO_DSA
7e1b7485
RS
160 if (dsaparam && g) {
161 BIO_printf(bio_err,
162 "generator may not be chosen for DSA parameters\n");
163 goto end;
0f113f3e 164 }
7e1b7485 165# endif
10b37541
RL
166
167 out = bio_open_default(outfile, 'w', outformat);
168 if (out == NULL)
169 goto end;
170
7e1b7485
RS
171 /* DH parameters */
172 if (num && !g)
173 g = 2;
0f113f3e
MC
174
175 if (num) {
176
177 BN_GENCB *cb;
178 cb = BN_GENCB_new();
96487cdd 179 if (cb == NULL) {
0f113f3e
MC
180 ERR_print_errors(bio_err);
181 goto end;
182 }
183
184 BN_GENCB_set(cb, dh_cb, bio_err);
0f113f3e
MC
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);
96487cdd 192 if (dsa == NULL
0f113f3e
MC
193 || !DSA_generate_parameters_ex(dsa, num, NULL, 0, NULL, NULL,
194 cb)) {
d6407083 195 DSA_free(dsa);
0f113f3e
MC
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");
96487cdd 216 if (dh == NULL || !DH_generate_parameters_ex(dh, num, g, cb)) {
0f113f3e
MC
217 BN_GENCB_free(cb);
218 ERR_print_errors(bio_err);
219 goto end;
220 }
221 }
222
223 BN_GENCB_free(cb);
0f113f3e
MC
224 } else {
225
bdd58d98 226 in = bio_open_default(infile, 'r', informat);
7e1b7485 227 if (in == NULL)
0f113f3e 228 goto end;
0f113f3e 229
0f113f3e
MC
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 {
18d20b5e
MC
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 */
0f113f3e 259 dh = d2i_DHparams_bio(in, NULL);
18d20b5e
MC
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 */
0f113f3e 265 dh = PEM_read_bio_DHparams(in, NULL, NULL, NULL);
18d20b5e 266 }
0f113f3e
MC
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
0f113f3e
MC
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)
eeb21772 288 BIO_printf(bio_err, "WARNING: p value is not prime\n");
0f113f3e 289 if (i & DH_CHECK_P_NOT_SAFE_PRIME)
eeb21772
MC
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");
0f113f3e 297 if (i & DH_UNABLE_TO_CHECK_GENERATOR)
eeb21772
MC
298 BIO_printf(bio_err,
299 "WARNING: unable to check the generator value\n");
0f113f3e 300 if (i & DH_NOT_SUITABLE_GENERATOR)
eeb21772 301 BIO_printf(bio_err, "WARNING: the g value is not a generator\n");
0f113f3e 302 if (i == 0)
eeb21772
MC
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 }
0f113f3e
MC
312 }
313 if (C) {
314 unsigned char *data;
7e1b7485 315 int len, bits;
2ac6115d 316 const BIGNUM *pbn, *gbn;
0f113f3e 317
0aeddcfa
MC
318 len = DH_size(dh);
319 bits = DH_bits(dh);
320 DH_get0_pqg(dh, &pbn, NULL, &gbn);
68dc6824 321 data = app_malloc(len, "print a BN");
201b305a
BB
322
323 BIO_printf(out, "static DH *get_dh%d(void)\n{\n", bits);
0aeddcfa
MC
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"
201b305a 327 " BIGNUM *p, *g;\n"
7e1b7485
RS
328 "\n"
329 " if (dh == NULL)\n"
330 " return NULL;\n");
201b305a 331 BIO_printf(out, " p = BN_bin2bn(dhp_%d, sizeof(dhp_%d), NULL);\n",
0aeddcfa 332 bits, bits);
201b305a 333 BIO_printf(out, " g = BN_bin2bn(dhg_%d, sizeof(dhg_%d), NULL);\n",
0aeddcfa 334 bits, bits);
201b305a
BB
335 BIO_printf(out, " if (p == NULL || g == NULL\n"
336 " || !DH_set0_pqg(dh, p, NULL, g)) {\n"
7e1b7485 337 " DH_free(dh);\n"
201b305a
BB
338 " BN_free(p);\n"
339 " BN_free(g);\n"
7e1b7485
RS
340 " return NULL;\n"
341 " }\n");
0aeddcfa 342 if (DH_get_length(dh) > 0)
7e1b7485 343 BIO_printf(out,
0aeddcfa
MC
344 " if (!DH_set_length(dh, %ld)) {\n"
345 " DH_free(dh);\n"
201b305a 346 " return NULL;\n"
0aeddcfa 347 " }\n", DH_get_length(dh));
7e1b7485 348 BIO_printf(out, " return dh;\n}\n");
0f113f3e
MC
349 OPENSSL_free(data);
350 }
351
352 if (!noout) {
2ac6115d 353 const BIGNUM *q;
0aeddcfa 354 DH_get0_pqg(dh, NULL, &q, NULL);
18d20b5e
MC
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);
2234212c 360 } else if (q != NULL) {
7e1b7485 361 i = PEM_write_bio_DHxparams(out, dh);
2234212c 362 } else {
7e1b7485 363 i = PEM_write_bio_DHparams(out, dh);
2234212c 364 }
0f113f3e
MC
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:
ca3a82c3
RS
373 BIO_free(in);
374 BIO_free_all(out);
d6407083 375 DH_free(dh);
dd1abd44 376 release_engine(e);
26a7d938 377 return ret;
0f113f3e 378}
09483c58 379
6d23cf97 380static int dh_cb(int p, int n, BN_GENCB *cb)
0f113f3e 381{
201b305a
BB
382 static const char symbols[] = ".+*\n";
383 char c = (p >= 0 && (size_t)p < sizeof(symbols) - 1) ? symbols[p] : '?';
384
0f113f3e
MC
385 BIO_write(BN_GENCB_get_arg(cb), &c, 1);
386 (void)BIO_flush(BN_GENCB_get_arg(cb));
387 return 1;
388}
09483c58 389#endif