diff options
author | jsing <> | 2015-07-11 15:04:56 +0000 |
---|---|---|
committer | jsing <> | 2015-07-11 15:04:56 +0000 |
commit | c72c50e028bb7f0bdfac41e425b6cee5393458d9 (patch) | |
tree | 1edd26583f3df5c454163392ba9da883e3e95020 | |
parent | cfcd8c19e095b37942124c85b7d96fb4b1aca7f5 (diff) | |
download | openbsd-c72c50e028bb7f0bdfac41e425b6cee5393458d9.tar.gz openbsd-c72c50e028bb7f0bdfac41e425b6cee5393458d9.tar.bz2 openbsd-c72c50e028bb7f0bdfac41e425b6cee5393458d9.zip |
Convert openssl(1) dhparam to new option handling.
ok doug@
Diffstat (limited to '')
-rw-r--r-- | src/usr.bin/openssl/dhparam.c | 281 |
1 files changed, 160 insertions, 121 deletions
diff --git a/src/usr.bin/openssl/dhparam.c b/src/usr.bin/openssl/dhparam.c index 005ed25877..6f471d65e1 100644 --- a/src/usr.bin/openssl/dhparam.c +++ b/src/usr.bin/openssl/dhparam.c | |||
@@ -1,4 +1,4 @@ | |||
1 | /* $OpenBSD: dhparam.c,v 1.3 2015/07/08 16:37:25 sthen Exp $ */ | 1 | /* $OpenBSD: dhparam.c,v 1.4 2015/07/11 15:04:56 jsing Exp $ */ |
2 | /* Copyright (C) 1995-1998 Eric Young (eay@cryptsoft.com) | 2 | /* Copyright (C) 1995-1998 Eric Young (eay@cryptsoft.com) |
3 | * All rights reserved. | 3 | * All rights reserved. |
4 | * | 4 | * |
@@ -131,16 +131,117 @@ | |||
131 | 131 | ||
132 | #define DEFBITS 2048 | 132 | #define DEFBITS 2048 |
133 | 133 | ||
134 | /* -inform arg - input format - default PEM (DER or PEM) | 134 | struct { |
135 | * -outform arg - output format - default PEM | 135 | int C; |
136 | * -in arg - input file - default stdin | 136 | int check; |
137 | * -out arg - output file - default stdout | 137 | int dsaparam; |
138 | * -dsaparam - read or generate DSA parameters, convert to DH | 138 | #ifndef OPENSSL_NO_ENGINE |
139 | * -check - check the parameters are ok | 139 | char *engine; |
140 | * -noout | 140 | #endif |
141 | * -text | 141 | int g; |
142 | * -C | 142 | char *infile; |
143 | */ | 143 | int informat; |
144 | int noout; | ||
145 | char *outfile; | ||
146 | int outformat; | ||
147 | int text; | ||
148 | } dhparam_config; | ||
149 | |||
150 | struct option dhparam_options[] = { | ||
151 | { | ||
152 | .name = "2", | ||
153 | .desc = "Generate DH parameters with a generator value of 2 " | ||
154 | "(default)", | ||
155 | .type = OPTION_VALUE, | ||
156 | .opt.value = &dhparam_config.g, | ||
157 | .value = 2, | ||
158 | }, | ||
159 | { | ||
160 | .name = "5", | ||
161 | .desc = "Generate DH parameters with a generator value of 5", | ||
162 | .type = OPTION_VALUE, | ||
163 | .opt.value = &dhparam_config.g, | ||
164 | .value = 5, | ||
165 | }, | ||
166 | { | ||
167 | .name = "C", | ||
168 | .desc = "Convert DH parameters into C code", | ||
169 | .type = OPTION_FLAG, | ||
170 | .opt.flag = &dhparam_config.C, | ||
171 | }, | ||
172 | { | ||
173 | .name = "check", | ||
174 | .desc = "Check the DH parameters", | ||
175 | .type = OPTION_FLAG, | ||
176 | .opt.flag = &dhparam_config.check, | ||
177 | }, | ||
178 | { | ||
179 | .name = "dsaparam", | ||
180 | .desc = "Read or generate DSA parameters and convert to DH", | ||
181 | .type = OPTION_FLAG, | ||
182 | .opt.flag = &dhparam_config.dsaparam, | ||
183 | }, | ||
184 | #ifndef OPENSSL_NO_ENGINE | ||
185 | { | ||
186 | .name = "engine", | ||
187 | .argname = "id", | ||
188 | .desc = "Use the engine specified by the given identifier", | ||
189 | .type = OPTION_ARG, | ||
190 | .opt.arg = &dhparam_config.engine, | ||
191 | }, | ||
192 | #endif | ||
193 | { | ||
194 | .name = "in", | ||
195 | .argname = "file", | ||
196 | .desc = "Input file (default stdin)", | ||
197 | .type = OPTION_ARG, | ||
198 | .opt.arg = &dhparam_config.infile, | ||
199 | }, | ||
200 | { | ||
201 | .name = "inform", | ||
202 | .argname = "format", | ||
203 | .desc = "Input format (DER or PEM (default))", | ||
204 | .type = OPTION_ARG_FORMAT, | ||
205 | .opt.value = &dhparam_config.informat, | ||
206 | }, | ||
207 | { | ||
208 | .name = "noout", | ||
209 | .desc = "Do not output encoded version of DH parameters", | ||
210 | .type = OPTION_FLAG, | ||
211 | .opt.flag = &dhparam_config.noout, | ||
212 | }, | ||
213 | { | ||
214 | .name = "out", | ||
215 | .argname = "file", | ||
216 | .desc = "Output file (default stdout)", | ||
217 | .type = OPTION_ARG, | ||
218 | .opt.arg = &dhparam_config.outfile, | ||
219 | }, | ||
220 | { | ||
221 | .name = "outform", | ||
222 | .argname = "format", | ||
223 | .desc = "Output format (DER or PEM (default))", | ||
224 | .type = OPTION_ARG_FORMAT, | ||
225 | .opt.value = &dhparam_config.outformat, | ||
226 | }, | ||
227 | { | ||
228 | .name = "text", | ||
229 | .desc = "Print DH parameters in plain text", | ||
230 | .type = OPTION_FLAG, | ||
231 | .opt.flag = &dhparam_config.text, | ||
232 | }, | ||
233 | { NULL }, | ||
234 | }; | ||
235 | |||
236 | static void | ||
237 | dhparam_usage() | ||
238 | { | ||
239 | fprintf(stderr, | ||
240 | "usage: dhparam [-2 | -5] [-C] [-check] [-dsaparam] [-engine id]\n" | ||
241 | " [-in file] [-inform DER | PEM] [-noout] [-out file]\n" | ||
242 | " [-outform DER | PEM] [-text] [numbits]\n\n"); | ||
243 | options_usage(dhparam_options); | ||
244 | } | ||
144 | 245 | ||
145 | static int dh_cb(int p, int n, BN_GENCB * cb); | 246 | static int dh_cb(int p, int n, BN_GENCB * cb); |
146 | 247 | ||
@@ -149,116 +250,54 @@ int dhparam_main(int, char **); | |||
149 | int | 250 | int |
150 | dhparam_main(int argc, char **argv) | 251 | dhparam_main(int argc, char **argv) |
151 | { | 252 | { |
152 | DH *dh = NULL; | ||
153 | int i, badops = 0, text = 0; | ||
154 | int dsaparam = 0; | ||
155 | BIO *in = NULL, *out = NULL; | 253 | BIO *in = NULL, *out = NULL; |
156 | int informat, outformat, check = 0, noout = 0, C = 0, ret = 1; | 254 | char *num_bits = NULL; |
157 | char *infile, *outfile, *prog; | 255 | DH *dh = NULL; |
158 | #ifndef OPENSSL_NO_ENGINE | 256 | int num = 0; |
159 | char *engine = NULL; | 257 | int ret = 1; |
160 | #endif | 258 | int i; |
161 | int num = 0, g = 0; | 259 | |
162 | 260 | memset(&dhparam_config, 0, sizeof(dhparam_config)); | |
163 | infile = NULL; | 261 | |
164 | outfile = NULL; | 262 | dhparam_config.informat = FORMAT_PEM; |
165 | informat = FORMAT_PEM; | 263 | dhparam_config.outformat = FORMAT_PEM; |
166 | outformat = FORMAT_PEM; | 264 | |
167 | 265 | if (options_parse(argc, argv, dhparam_options, &num_bits, NULL) != 0) { | |
168 | prog = argv[0]; | 266 | dhparam_usage(); |
169 | argc--; | 267 | return (1); |
170 | argv++; | ||
171 | while (argc >= 1) { | ||
172 | if (strcmp(*argv, "-inform") == 0) { | ||
173 | if (--argc < 1) | ||
174 | goto bad; | ||
175 | informat = str2fmt(*(++argv)); | ||
176 | } else if (strcmp(*argv, "-outform") == 0) { | ||
177 | if (--argc < 1) | ||
178 | goto bad; | ||
179 | outformat = str2fmt(*(++argv)); | ||
180 | } else if (strcmp(*argv, "-in") == 0) { | ||
181 | if (--argc < 1) | ||
182 | goto bad; | ||
183 | infile = *(++argv); | ||
184 | } else if (strcmp(*argv, "-out") == 0) { | ||
185 | if (--argc < 1) | ||
186 | goto bad; | ||
187 | outfile = *(++argv); | ||
188 | } | ||
189 | #ifndef OPENSSL_NO_ENGINE | ||
190 | else if (strcmp(*argv, "-engine") == 0) { | ||
191 | if (--argc < 1) | ||
192 | goto bad; | ||
193 | engine = *(++argv); | ||
194 | } | ||
195 | #endif | ||
196 | else if (strcmp(*argv, "-check") == 0) | ||
197 | check = 1; | ||
198 | else if (strcmp(*argv, "-text") == 0) | ||
199 | text = 1; | ||
200 | else if (strcmp(*argv, "-dsaparam") == 0) | ||
201 | dsaparam = 1; | ||
202 | else if (strcmp(*argv, "-C") == 0) | ||
203 | C = 1; | ||
204 | else if (strcmp(*argv, "-noout") == 0) | ||
205 | noout = 1; | ||
206 | else if (strcmp(*argv, "-2") == 0) | ||
207 | g = 2; | ||
208 | else if (strcmp(*argv, "-5") == 0) | ||
209 | g = 5; | ||
210 | else if (((sscanf(*argv, "%d", &num) == 0) || (num <= 0))) | ||
211 | goto bad; | ||
212 | argv++; | ||
213 | argc--; | ||
214 | } | 268 | } |
215 | 269 | ||
216 | if (badops) { | 270 | if (num_bits != NULL) { |
217 | bad: | 271 | if(sscanf(num_bits, "%d", &num) == 0 || num <= 0) { |
218 | BIO_printf(bio_err, "%s [options] [numbits]\n", prog); | 272 | BIO_printf(bio_err, "invalid number of bits: %s\n", |
219 | BIO_printf(bio_err, "where options are\n"); | 273 | num_bits); |
220 | BIO_printf(bio_err, " -inform arg input format - one of DER PEM\n"); | 274 | return (1); |
221 | BIO_printf(bio_err, " -outform arg output format - one of DER PEM\n"); | 275 | } |
222 | BIO_printf(bio_err, " -in arg input file\n"); | ||
223 | BIO_printf(bio_err, " -out arg output file\n"); | ||
224 | BIO_printf(bio_err, " -dsaparam read or generate DSA parameters, convert to DH\n"); | ||
225 | BIO_printf(bio_err, " -check check the DH parameters\n"); | ||
226 | BIO_printf(bio_err, " -text print a text form of the DH parameters\n"); | ||
227 | BIO_printf(bio_err, " -C Output C code\n"); | ||
228 | BIO_printf(bio_err, " -2 generate parameters using 2 as the generator value\n"); | ||
229 | BIO_printf(bio_err, " -5 generate parameters using 5 as the generator value\n"); | ||
230 | BIO_printf(bio_err, " numbits number of bits in to generate (default 2048)\n"); | ||
231 | #ifndef OPENSSL_NO_ENGINE | ||
232 | BIO_printf(bio_err, " -engine e use engine e, possibly a hardware device.\n"); | ||
233 | #endif | ||
234 | BIO_printf(bio_err, " -noout no output\n"); | ||
235 | goto end; | ||
236 | } | 276 | } |
237 | 277 | ||
238 | #ifndef OPENSSL_NO_ENGINE | 278 | #ifndef OPENSSL_NO_ENGINE |
239 | setup_engine(bio_err, engine, 0); | 279 | setup_engine(bio_err, dhparam_config.engine, 0); |
240 | #endif | 280 | #endif |
241 | 281 | ||
242 | if (g && !num) | 282 | if (dhparam_config.g && !num) |
243 | num = DEFBITS; | 283 | num = DEFBITS; |
244 | 284 | ||
245 | if (dsaparam) { | 285 | if (dhparam_config.dsaparam) { |
246 | if (g) { | 286 | if (dhparam_config.g) { |
247 | BIO_printf(bio_err, "generator may not be chosen for DSA parameters\n"); | 287 | BIO_printf(bio_err, "generator may not be chosen for DSA parameters\n"); |
248 | goto end; | 288 | goto end; |
249 | } | 289 | } |
250 | } else | 290 | } else { |
251 | { | ||
252 | /* DH parameters */ | 291 | /* DH parameters */ |
253 | if (num && !g) | 292 | if (num && !dhparam_config.g) |
254 | g = 2; | 293 | dhparam_config.g = 2; |
255 | } | 294 | } |
256 | 295 | ||
257 | if (num) { | 296 | if (num) { |
258 | 297 | ||
259 | BN_GENCB cb; | 298 | BN_GENCB cb; |
260 | BN_GENCB_set(&cb, dh_cb, bio_err); | 299 | BN_GENCB_set(&cb, dh_cb, bio_err); |
261 | if (dsaparam) { | 300 | if (dhparam_config.dsaparam) { |
262 | DSA *dsa = DSA_new(); | 301 | DSA *dsa = DSA_new(); |
263 | 302 | ||
264 | BIO_printf(bio_err, "Generating DSA parameters, %d bit long prime\n", num); | 303 | BIO_printf(bio_err, "Generating DSA parameters, %d bit long prime\n", num); |
@@ -275,12 +314,11 @@ bad: | |||
275 | ERR_print_errors(bio_err); | 314 | ERR_print_errors(bio_err); |
276 | goto end; | 315 | goto end; |
277 | } | 316 | } |
278 | } else | 317 | } else { |
279 | { | ||
280 | dh = DH_new(); | 318 | dh = DH_new(); |
281 | BIO_printf(bio_err, "Generating DH parameters, %d bit long safe prime, generator %d\n", num, g); | 319 | BIO_printf(bio_err, "Generating DH parameters, %d bit long safe prime, generator %d\n", num, dhparam_config.g); |
282 | BIO_printf(bio_err, "This is going to take a long time\n"); | 320 | BIO_printf(bio_err, "This is going to take a long time\n"); |
283 | if (!dh || !DH_generate_parameters_ex(dh, num, g, &cb)) { | 321 | if (!dh || !DH_generate_parameters_ex(dh, num, dhparam_config.g, &cb)) { |
284 | ERR_print_errors(bio_err); | 322 | ERR_print_errors(bio_err); |
285 | goto end; | 323 | goto end; |
286 | } | 324 | } |
@@ -292,23 +330,24 @@ bad: | |||
292 | ERR_print_errors(bio_err); | 330 | ERR_print_errors(bio_err); |
293 | goto end; | 331 | goto end; |
294 | } | 332 | } |
295 | if (infile == NULL) | 333 | if (dhparam_config.infile == NULL) |
296 | BIO_set_fp(in, stdin, BIO_NOCLOSE); | 334 | BIO_set_fp(in, stdin, BIO_NOCLOSE); |
297 | else { | 335 | else { |
298 | if (BIO_read_filename(in, infile) <= 0) { | 336 | if (BIO_read_filename(in, dhparam_config.infile) <= 0) { |
299 | perror(infile); | 337 | perror(dhparam_config.infile); |
300 | goto end; | 338 | goto end; |
301 | } | 339 | } |
302 | } | 340 | } |
303 | 341 | ||
304 | if (informat != FORMAT_ASN1 && informat != FORMAT_PEM) { | 342 | if (dhparam_config.informat != FORMAT_ASN1 && |
343 | dhparam_config.informat != FORMAT_PEM) { | ||
305 | BIO_printf(bio_err, "bad input format specified\n"); | 344 | BIO_printf(bio_err, "bad input format specified\n"); |
306 | goto end; | 345 | goto end; |
307 | } | 346 | } |
308 | if (dsaparam) { | 347 | if (dhparam_config.dsaparam) { |
309 | DSA *dsa; | 348 | DSA *dsa; |
310 | 349 | ||
311 | if (informat == FORMAT_ASN1) | 350 | if (dhparam_config.informat == FORMAT_ASN1) |
312 | dsa = d2i_DSAparams_bio(in, NULL); | 351 | dsa = d2i_DSAparams_bio(in, NULL); |
313 | else /* informat == FORMAT_PEM */ | 352 | else /* informat == FORMAT_PEM */ |
314 | dsa = PEM_read_bio_DSAparams(in, NULL, NULL, NULL); | 353 | dsa = PEM_read_bio_DSAparams(in, NULL, NULL, NULL); |
@@ -326,7 +365,7 @@ bad: | |||
326 | } | 365 | } |
327 | } else | 366 | } else |
328 | { | 367 | { |
329 | if (informat == FORMAT_ASN1) | 368 | if (dhparam_config.informat == FORMAT_ASN1) |
330 | dh = d2i_DHparams_bio(in, NULL); | 369 | dh = d2i_DHparams_bio(in, NULL); |
331 | else /* informat == FORMAT_PEM */ | 370 | else /* informat == FORMAT_PEM */ |
332 | dh = PEM_read_bio_DHparams(in, NULL, NULL, NULL); | 371 | dh = PEM_read_bio_DHparams(in, NULL, NULL, NULL); |
@@ -346,20 +385,20 @@ bad: | |||
346 | ERR_print_errors(bio_err); | 385 | ERR_print_errors(bio_err); |
347 | goto end; | 386 | goto end; |
348 | } | 387 | } |
349 | if (outfile == NULL) { | 388 | if (dhparam_config.outfile == NULL) { |
350 | BIO_set_fp(out, stdout, BIO_NOCLOSE); | 389 | BIO_set_fp(out, stdout, BIO_NOCLOSE); |
351 | } else { | 390 | } else { |
352 | if (BIO_write_filename(out, outfile) <= 0) { | 391 | if (BIO_write_filename(out, dhparam_config.outfile) <= 0) { |
353 | perror(outfile); | 392 | perror(dhparam_config.outfile); |
354 | goto end; | 393 | goto end; |
355 | } | 394 | } |
356 | } | 395 | } |
357 | 396 | ||
358 | 397 | ||
359 | if (text) { | 398 | if (dhparam_config.text) { |
360 | DHparams_print(out, dh); | 399 | DHparams_print(out, dh); |
361 | } | 400 | } |
362 | if (check) { | 401 | if (dhparam_config.check) { |
363 | if (!DH_check(dh, &i)) { | 402 | if (!DH_check(dh, &i)) { |
364 | ERR_print_errors(bio_err); | 403 | ERR_print_errors(bio_err); |
365 | goto end; | 404 | goto end; |
@@ -375,7 +414,7 @@ bad: | |||
375 | if (i == 0) | 414 | if (i == 0) |
376 | printf("DH parameters appear to be ok.\n"); | 415 | printf("DH parameters appear to be ok.\n"); |
377 | } | 416 | } |
378 | if (C) { | 417 | if (dhparam_config.C) { |
379 | unsigned char *data; | 418 | unsigned char *data; |
380 | int len, l, bits; | 419 | int len, l, bits; |
381 | 420 | ||
@@ -422,10 +461,10 @@ bad: | |||
422 | printf("\treturn(dh);\n\t}\n"); | 461 | printf("\treturn(dh);\n\t}\n"); |
423 | free(data); | 462 | free(data); |
424 | } | 463 | } |
425 | if (!noout) { | 464 | if (!dhparam_config.noout) { |
426 | if (outformat == FORMAT_ASN1) | 465 | if (dhparam_config.outformat == FORMAT_ASN1) |
427 | i = i2d_DHparams_bio(out, dh); | 466 | i = i2d_DHparams_bio(out, dh); |
428 | else if (outformat == FORMAT_PEM) | 467 | else if (dhparam_config.outformat == FORMAT_PEM) |
429 | i = PEM_write_bio_DHparams(out, dh); | 468 | i = PEM_write_bio_DHparams(out, dh); |
430 | else { | 469 | else { |
431 | BIO_printf(bio_err, "bad output format specified for outfile\n"); | 470 | BIO_printf(bio_err, "bad output format specified for outfile\n"); |