]> git.ipfire.org Git - thirdparty/openssl.git/blame - apps/dhparam.c
OpenSSL 1.1.1 is now in pre release
[thirdparty/openssl.git] / apps / dhparam.c
CommitLineData
846e33c7 1/*
2234212c 2 * Copyright 1995-2017 The OpenSSL Project Authors. All Rights Reserved.
41918458 3 *
846e33c7
RS
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
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,
3ee1eac2
RS
40 OPT_DSAPARAM, OPT_C, OPT_2, OPT_5,
41 OPT_R_ENUM
7e1b7485
RS
42} OPTION_CHOICE;
43
44c83ebd 44const OPTIONS dhparam_options[] = {
7e1b7485
RS
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"},
16e1b281 54 {"noout", OPT_NOOUT, '-', "Don't output any DH parameters"},
3ee1eac2 55 OPT_R_OPTIONS,
7e1b7485
RS
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"},
7e1b7485
RS
59# ifndef OPENSSL_NO_DSA
60 {"dsaparam", OPT_DSAPARAM, '-',
61 "Read or generate DSA parameters, convert to DH"},
9c3bcfa0
RS
62# endif
63# ifndef OPENSSL_NO_ENGINE
64 {"engine", OPT_ENGINE, 's', "Use engine e, possibly a hardware device"},
0f113f3e 65# endif
7e1b7485
RS
66 {NULL}
67};
68
69int dhparam_main(int argc, char **argv)
70{
71 BIO *in = NULL, *out = NULL;
72 DH *dh = NULL;
3ee1eac2 73 char *infile = NULL, *outfile = NULL, *prog;
dd1abd44 74 ENGINE *e = NULL;
83ae8124
MC
75#ifndef OPENSSL_NO_DSA
76 int dsaparam = 0;
77#endif
78 int i, text = 0, C = 0, ret = 1, num = 0, g = 0;
7e1b7485
RS
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:
dd1abd44 109 e = setup_engine(opt_arg(), 0);
7e1b7485
RS
110 break;
111 case OPT_CHECK:
0f113f3e 112 check = 1;
7e1b7485
RS
113 break;
114 case OPT_TEXT:
0f113f3e 115 text = 1;
7e1b7485
RS
116 break;
117 case OPT_DSAPARAM:
83ae8124 118#ifndef OPENSSL_NO_DSA
0f113f3e 119 dsaparam = 1;
83ae8124 120#endif
7e1b7485
RS
121 break;
122 case OPT_C:
0f113f3e 123 C = 1;
7e1b7485
RS
124 break;
125 case OPT_2:
0f113f3e 126 g = 2;
7e1b7485
RS
127 break;
128 case OPT_5:
0f113f3e 129 g = 5;
7e1b7485
RS
130 break;
131 case OPT_NOOUT:
132 noout = 1;
133 break;
3ee1eac2
RS
134 case OPT_R_CASES:
135 if (!opt_rand(o))
136 goto end;
7e1b7485
RS
137 break;
138 }
0f113f3e 139 }
7e1b7485
RS
140 argc = opt_num_rest();
141 argv = opt_rest();
0f113f3e 142
2234212c 143 if (argv[0] != NULL && (!opt_int(argv[0], &num) || num <= 0))
0f113f3e 144 goto end;
0f113f3e 145
0f113f3e
MC
146 if (g && !num)
147 num = DEFBITS;
148
149# ifndef OPENSSL_NO_DSA
7e1b7485
RS
150 if (dsaparam && g) {
151 BIO_printf(bio_err,
152 "generator may not be chosen for DSA parameters\n");
153 goto end;
0f113f3e 154 }
7e1b7485
RS
155# endif
156 /* DH parameters */
157 if (num && !g)
158 g = 2;
0f113f3e
MC
159
160 if (num) {
161
162 BN_GENCB *cb;
163 cb = BN_GENCB_new();
96487cdd 164 if (cb == NULL) {
0f113f3e
MC
165 ERR_print_errors(bio_err);
166 goto end;
167 }
168
169 BN_GENCB_set(cb, dh_cb, bio_err);
0f113f3e
MC
170
171# ifndef OPENSSL_NO_DSA
172 if (dsaparam) {
173 DSA *dsa = DSA_new();
174
175 BIO_printf(bio_err,
176 "Generating DSA parameters, %d bit long prime\n", num);
96487cdd 177 if (dsa == NULL
0f113f3e
MC
178 || !DSA_generate_parameters_ex(dsa, num, NULL, 0, NULL, NULL,
179 cb)) {
d6407083 180 DSA_free(dsa);
0f113f3e
MC
181 BN_GENCB_free(cb);
182 ERR_print_errors(bio_err);
183 goto end;
184 }
185
186 dh = DSA_dup_DH(dsa);
187 DSA_free(dsa);
188 if (dh == NULL) {
189 BN_GENCB_free(cb);
190 ERR_print_errors(bio_err);
191 goto end;
192 }
193 } else
194# endif
195 {
196 dh = DH_new();
197 BIO_printf(bio_err,
198 "Generating DH parameters, %d bit long safe prime, generator %d\n",
199 num, g);
200 BIO_printf(bio_err, "This is going to take a long time\n");
96487cdd 201 if (dh == NULL || !DH_generate_parameters_ex(dh, num, g, cb)) {
0f113f3e
MC
202 BN_GENCB_free(cb);
203 ERR_print_errors(bio_err);
204 goto end;
205 }
206 }
207
208 BN_GENCB_free(cb);
0f113f3e
MC
209 } else {
210
bdd58d98 211 in = bio_open_default(infile, 'r', informat);
7e1b7485 212 if (in == NULL)
0f113f3e 213 goto end;
0f113f3e 214
0f113f3e
MC
215# ifndef OPENSSL_NO_DSA
216 if (dsaparam) {
217 DSA *dsa;
218
219 if (informat == FORMAT_ASN1)
220 dsa = d2i_DSAparams_bio(in, NULL);
221 else /* informat == FORMAT_PEM */
222 dsa = PEM_read_bio_DSAparams(in, NULL, NULL, NULL);
223
224 if (dsa == NULL) {
225 BIO_printf(bio_err, "unable to load DSA parameters\n");
226 ERR_print_errors(bio_err);
227 goto end;
228 }
229
230 dh = DSA_dup_DH(dsa);
231 DSA_free(dsa);
232 if (dh == NULL) {
233 ERR_print_errors(bio_err);
234 goto end;
235 }
236 } else
237# endif
238 {
18d20b5e
MC
239 if (informat == FORMAT_ASN1) {
240 /*
241 * We have no PEM header to determine what type of DH params it
242 * is. We'll just try both.
243 */
0f113f3e 244 dh = d2i_DHparams_bio(in, NULL);
18d20b5e
MC
245 /* BIO_reset() returns 0 for success for file BIOs only!!! */
246 if (dh == NULL && BIO_reset(in) == 0)
247 dh = d2i_DHxparams_bio(in, NULL);
248 } else {
249 /* informat == FORMAT_PEM */
0f113f3e 250 dh = PEM_read_bio_DHparams(in, NULL, NULL, NULL);
18d20b5e 251 }
0f113f3e
MC
252
253 if (dh == NULL) {
254 BIO_printf(bio_err, "unable to load DH parameters\n");
255 ERR_print_errors(bio_err);
256 goto end;
257 }
258 }
259
260 /* dh != NULL */
261 }
262
bdd58d98 263 out = bio_open_default(outfile, 'w', outformat);
7e1b7485 264 if (out == NULL)
0f113f3e 265 goto end;
0f113f3e
MC
266
267 if (text) {
268 DHparams_print(out, dh);
269 }
270
271 if (check) {
272 if (!DH_check(dh, &i)) {
273 ERR_print_errors(bio_err);
274 goto end;
275 }
276 if (i & DH_CHECK_P_NOT_PRIME)
eeb21772 277 BIO_printf(bio_err, "WARNING: p value is not prime\n");
0f113f3e 278 if (i & DH_CHECK_P_NOT_SAFE_PRIME)
eeb21772
MC
279 BIO_printf(bio_err, "WARNING: p value is not a safe prime\n");
280 if (i & DH_CHECK_Q_NOT_PRIME)
281 BIO_printf(bio_err, "WARNING: q value is not a prime\n");
282 if (i & DH_CHECK_INVALID_Q_VALUE)
283 BIO_printf(bio_err, "WARNING: q value is invalid\n");
284 if (i & DH_CHECK_INVALID_J_VALUE)
285 BIO_printf(bio_err, "WARNING: j value is invalid\n");
0f113f3e 286 if (i & DH_UNABLE_TO_CHECK_GENERATOR)
eeb21772
MC
287 BIO_printf(bio_err,
288 "WARNING: unable to check the generator value\n");
0f113f3e 289 if (i & DH_NOT_SUITABLE_GENERATOR)
eeb21772 290 BIO_printf(bio_err, "WARNING: the g value is not a generator\n");
0f113f3e 291 if (i == 0)
eeb21772
MC
292 BIO_printf(bio_err, "DH parameters appear to be ok.\n");
293 if (num != 0 && i != 0) {
294 /*
295 * We have generated parameters but DH_check() indicates they are
296 * invalid! This should never happen!
297 */
298 BIO_printf(bio_err, "ERROR: Invalid parameters generated\n");
299 goto end;
300 }
0f113f3e
MC
301 }
302 if (C) {
303 unsigned char *data;
7e1b7485 304 int len, bits;
2ac6115d 305 const BIGNUM *pbn, *gbn;
0f113f3e 306
0aeddcfa
MC
307 len = DH_size(dh);
308 bits = DH_bits(dh);
309 DH_get0_pqg(dh, &pbn, NULL, &gbn);
68dc6824 310 data = app_malloc(len, "print a BN");
7e1b7485
RS
311 BIO_printf(out, "#ifndef HEADER_DH_H\n"
312 "# include <openssl/dh.h>\n"
313 "#endif\n"
314 "\n");
315 BIO_printf(out, "DH *get_dh%d()\n{\n", bits);
0aeddcfa
MC
316 print_bignum_var(out, pbn, "dhp", bits, data);
317 print_bignum_var(out, gbn, "dhg", bits, data);
318 BIO_printf(out, " DH *dh = DH_new();\n"
319 " BIGNUM *dhp_bn, *dhg_bn;\n"
7e1b7485
RS
320 "\n"
321 " if (dh == NULL)\n"
322 " return NULL;\n");
cbe29648 323 BIO_printf(out, " dhp_bn = BN_bin2bn(dhp_%d, sizeof(dhp_%d), NULL);\n",
0aeddcfa 324 bits, bits);
cbe29648 325 BIO_printf(out, " dhg_bn = BN_bin2bn(dhg_%d, sizeof(dhg_%d), NULL);\n",
0aeddcfa
MC
326 bits, bits);
327 BIO_printf(out, " if (dhp_bn == NULL || dhg_bn == NULL\n"
328 " || !DH_set0_pqg(dh, dhp_bn, NULL, dhg_bn)) {\n"
7e1b7485 329 " DH_free(dh);\n"
0aeddcfa
MC
330 " BN_free(dhp_bn);\n"
331 " BN_free(dhg_bn);\n"
7e1b7485
RS
332 " return NULL;\n"
333 " }\n");
0aeddcfa 334 if (DH_get_length(dh) > 0)
7e1b7485 335 BIO_printf(out,
0aeddcfa
MC
336 " if (!DH_set_length(dh, %ld)) {\n"
337 " DH_free(dh);\n"
338 " }\n", DH_get_length(dh));
7e1b7485 339 BIO_printf(out, " return dh;\n}\n");
0f113f3e
MC
340 OPENSSL_free(data);
341 }
342
343 if (!noout) {
2ac6115d 344 const BIGNUM *q;
0aeddcfa 345 DH_get0_pqg(dh, NULL, &q, NULL);
18d20b5e
MC
346 if (outformat == FORMAT_ASN1) {
347 if (q != NULL)
348 i = i2d_DHxparams_bio(out, dh);
349 else
350 i = i2d_DHparams_bio(out, dh);
2234212c 351 } else if (q != NULL) {
7e1b7485 352 i = PEM_write_bio_DHxparams(out, dh);
2234212c 353 } else {
7e1b7485 354 i = PEM_write_bio_DHparams(out, dh);
2234212c 355 }
0f113f3e
MC
356 if (!i) {
357 BIO_printf(bio_err, "unable to write DH parameters\n");
358 ERR_print_errors(bio_err);
359 goto end;
360 }
361 }
362 ret = 0;
363 end:
ca3a82c3
RS
364 BIO_free(in);
365 BIO_free_all(out);
d6407083 366 DH_free(dh);
dd1abd44 367 release_engine(e);
26a7d938 368 return ret;
0f113f3e 369}
09483c58 370
6d23cf97 371static int dh_cb(int p, int n, BN_GENCB *cb)
0f113f3e
MC
372{
373 char c = '*';
374
375 if (p == 0)
376 c = '.';
377 if (p == 1)
378 c = '+';
379 if (p == 2)
380 c = '*';
381 if (p == 3)
382 c = '\n';
383 BIO_write(BN_GENCB_get_arg(cb), &c, 1);
384 (void)BIO_flush(BN_GENCB_get_arg(cb));
385 return 1;
386}
09483c58 387#endif