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