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