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