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