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