version 1.16, 2023/03/05 13:12:53 |
version 1.17, 2023/03/06 14:32:06 |
|
|
char *outfile; |
char *outfile; |
int outformat; |
int outformat; |
int text; |
int text; |
} dhparam_config; |
} cfg; |
|
|
static const struct option dhparam_options[] = { |
static const struct option dhparam_options[] = { |
{ |
{ |
|
|
.desc = "Generate DH parameters with a generator value of 2 " |
.desc = "Generate DH parameters with a generator value of 2 " |
"(default)", |
"(default)", |
.type = OPTION_VALUE, |
.type = OPTION_VALUE, |
.opt.value = &dhparam_config.g, |
.opt.value = &cfg.g, |
.value = 2, |
.value = 2, |
}, |
}, |
{ |
{ |
.name = "5", |
.name = "5", |
.desc = "Generate DH parameters with a generator value of 5", |
.desc = "Generate DH parameters with a generator value of 5", |
.type = OPTION_VALUE, |
.type = OPTION_VALUE, |
.opt.value = &dhparam_config.g, |
.opt.value = &cfg.g, |
.value = 5, |
.value = 5, |
}, |
}, |
{ |
{ |
.name = "C", |
.name = "C", |
.desc = "Convert DH parameters into C code", |
.desc = "Convert DH parameters into C code", |
.type = OPTION_FLAG, |
.type = OPTION_FLAG, |
.opt.flag = &dhparam_config.C, |
.opt.flag = &cfg.C, |
}, |
}, |
{ |
{ |
.name = "check", |
.name = "check", |
.desc = "Check the DH parameters", |
.desc = "Check the DH parameters", |
.type = OPTION_FLAG, |
.type = OPTION_FLAG, |
.opt.flag = &dhparam_config.check, |
.opt.flag = &cfg.check, |
}, |
}, |
{ |
{ |
.name = "dsaparam", |
.name = "dsaparam", |
.desc = "Read or generate DSA parameters and convert to DH", |
.desc = "Read or generate DSA parameters and convert to DH", |
.type = OPTION_FLAG, |
.type = OPTION_FLAG, |
.opt.flag = &dhparam_config.dsaparam, |
.opt.flag = &cfg.dsaparam, |
}, |
}, |
{ |
{ |
.name = "in", |
.name = "in", |
.argname = "file", |
.argname = "file", |
.desc = "Input file (default stdin)", |
.desc = "Input file (default stdin)", |
.type = OPTION_ARG, |
.type = OPTION_ARG, |
.opt.arg = &dhparam_config.infile, |
.opt.arg = &cfg.infile, |
}, |
}, |
{ |
{ |
.name = "inform", |
.name = "inform", |
.argname = "format", |
.argname = "format", |
.desc = "Input format (DER or PEM (default))", |
.desc = "Input format (DER or PEM (default))", |
.type = OPTION_ARG_FORMAT, |
.type = OPTION_ARG_FORMAT, |
.opt.value = &dhparam_config.informat, |
.opt.value = &cfg.informat, |
}, |
}, |
{ |
{ |
.name = "noout", |
.name = "noout", |
.desc = "Do not output encoded version of DH parameters", |
.desc = "Do not output encoded version of DH parameters", |
.type = OPTION_FLAG, |
.type = OPTION_FLAG, |
.opt.flag = &dhparam_config.noout, |
.opt.flag = &cfg.noout, |
}, |
}, |
{ |
{ |
.name = "out", |
.name = "out", |
.argname = "file", |
.argname = "file", |
.desc = "Output file (default stdout)", |
.desc = "Output file (default stdout)", |
.type = OPTION_ARG, |
.type = OPTION_ARG, |
.opt.arg = &dhparam_config.outfile, |
.opt.arg = &cfg.outfile, |
}, |
}, |
{ |
{ |
.name = "outform", |
.name = "outform", |
.argname = "format", |
.argname = "format", |
.desc = "Output format (DER or PEM (default))", |
.desc = "Output format (DER or PEM (default))", |
.type = OPTION_ARG_FORMAT, |
.type = OPTION_ARG_FORMAT, |
.opt.value = &dhparam_config.outformat, |
.opt.value = &cfg.outformat, |
}, |
}, |
{ |
{ |
.name = "text", |
.name = "text", |
.desc = "Print DH parameters in plain text", |
.desc = "Print DH parameters in plain text", |
.type = OPTION_FLAG, |
.type = OPTION_FLAG, |
.opt.flag = &dhparam_config.text, |
.opt.flag = &cfg.text, |
}, |
}, |
{ NULL }, |
{ NULL }, |
}; |
}; |
|
|
exit(1); |
exit(1); |
} |
} |
|
|
memset(&dhparam_config, 0, sizeof(dhparam_config)); |
memset(&cfg, 0, sizeof(cfg)); |
|
|
dhparam_config.informat = FORMAT_PEM; |
cfg.informat = FORMAT_PEM; |
dhparam_config.outformat = FORMAT_PEM; |
cfg.outformat = FORMAT_PEM; |
|
|
if (options_parse(argc, argv, dhparam_options, &num_bits, NULL) != 0) { |
if (options_parse(argc, argv, dhparam_options, &num_bits, NULL) != 0) { |
dhparam_usage(); |
dhparam_usage(); |
|
|
} |
} |
} |
} |
|
|
if (dhparam_config.g && !num) |
if (cfg.g && !num) |
num = DEFBITS; |
num = DEFBITS; |
|
|
if (dhparam_config.dsaparam) { |
if (cfg.dsaparam) { |
if (dhparam_config.g) { |
if (cfg.g) { |
BIO_printf(bio_err, "generator may not be chosen for DSA parameters\n"); |
BIO_printf(bio_err, "generator may not be chosen for DSA parameters\n"); |
goto end; |
goto end; |
} |
} |
} else { |
} else { |
/* DH parameters */ |
/* DH parameters */ |
if (num && !dhparam_config.g) |
if (num && !cfg.g) |
dhparam_config.g = 2; |
cfg.g = 2; |
} |
} |
|
|
if (num) { |
if (num) { |
|
|
} |
} |
|
|
BN_GENCB_set(cb, dh_cb, bio_err); |
BN_GENCB_set(cb, dh_cb, bio_err); |
if (dhparam_config.dsaparam) { |
if (cfg.dsaparam) { |
DSA *dsa = DSA_new(); |
DSA *dsa = DSA_new(); |
|
|
BIO_printf(bio_err, "Generating DSA parameters, %d bit long prime\n", num); |
BIO_printf(bio_err, "Generating DSA parameters, %d bit long prime\n", num); |
|
|
} |
} |
} else { |
} else { |
dh = DH_new(); |
dh = DH_new(); |
BIO_printf(bio_err, "Generating DH parameters, %d bit long safe prime, generator %d\n", num, dhparam_config.g); |
BIO_printf(bio_err, "Generating DH parameters, %d bit long safe prime, generator %d\n", num, cfg.g); |
BIO_printf(bio_err, "This is going to take a long time\n"); |
BIO_printf(bio_err, "This is going to take a long time\n"); |
if (!dh || !DH_generate_parameters_ex(dh, num, dhparam_config.g, cb)) { |
if (!dh || !DH_generate_parameters_ex(dh, num, cfg.g, cb)) { |
ERR_print_errors(bio_err); |
ERR_print_errors(bio_err); |
goto end; |
goto end; |
} |
} |
|
|
ERR_print_errors(bio_err); |
ERR_print_errors(bio_err); |
goto end; |
goto end; |
} |
} |
if (dhparam_config.infile == NULL) |
if (cfg.infile == NULL) |
BIO_set_fp(in, stdin, BIO_NOCLOSE); |
BIO_set_fp(in, stdin, BIO_NOCLOSE); |
else { |
else { |
if (BIO_read_filename(in, dhparam_config.infile) <= 0) { |
if (BIO_read_filename(in, cfg.infile) <= 0) { |
perror(dhparam_config.infile); |
perror(cfg.infile); |
goto end; |
goto end; |
} |
} |
} |
} |
|
|
if (dhparam_config.informat != FORMAT_ASN1 && |
if (cfg.informat != FORMAT_ASN1 && |
dhparam_config.informat != FORMAT_PEM) { |
cfg.informat != FORMAT_PEM) { |
BIO_printf(bio_err, "bad input format specified\n"); |
BIO_printf(bio_err, "bad input format specified\n"); |
goto end; |
goto end; |
} |
} |
if (dhparam_config.dsaparam) { |
if (cfg.dsaparam) { |
DSA *dsa; |
DSA *dsa; |
|
|
if (dhparam_config.informat == FORMAT_ASN1) |
if (cfg.informat == FORMAT_ASN1) |
dsa = d2i_DSAparams_bio(in, NULL); |
dsa = d2i_DSAparams_bio(in, NULL); |
else /* informat == FORMAT_PEM */ |
else /* informat == FORMAT_PEM */ |
dsa = PEM_read_bio_DSAparams(in, NULL, NULL, NULL); |
dsa = PEM_read_bio_DSAparams(in, NULL, NULL, NULL); |
|
|
} |
} |
} else |
} else |
{ |
{ |
if (dhparam_config.informat == FORMAT_ASN1) |
if (cfg.informat == FORMAT_ASN1) |
dh = d2i_DHparams_bio(in, NULL); |
dh = d2i_DHparams_bio(in, NULL); |
else /* informat == FORMAT_PEM */ |
else /* informat == FORMAT_PEM */ |
dh = PEM_read_bio_DHparams(in, NULL, NULL, NULL); |
dh = PEM_read_bio_DHparams(in, NULL, NULL, NULL); |
|
|
ERR_print_errors(bio_err); |
ERR_print_errors(bio_err); |
goto end; |
goto end; |
} |
} |
if (dhparam_config.outfile == NULL) { |
if (cfg.outfile == NULL) { |
BIO_set_fp(out, stdout, BIO_NOCLOSE); |
BIO_set_fp(out, stdout, BIO_NOCLOSE); |
} else { |
} else { |
if (BIO_write_filename(out, dhparam_config.outfile) <= 0) { |
if (BIO_write_filename(out, cfg.outfile) <= 0) { |
perror(dhparam_config.outfile); |
perror(cfg.outfile); |
goto end; |
goto end; |
} |
} |
} |
} |
|
|
|
|
if (dhparam_config.text) { |
if (cfg.text) { |
DHparams_print(out, dh); |
DHparams_print(out, dh); |
} |
} |
if (dhparam_config.check) { |
if (cfg.check) { |
if (!DH_check(dh, &i)) { |
if (!DH_check(dh, &i)) { |
ERR_print_errors(bio_err); |
ERR_print_errors(bio_err); |
goto end; |
goto end; |
|
|
if (i == 0) |
if (i == 0) |
printf("DH parameters appear to be ok.\n"); |
printf("DH parameters appear to be ok.\n"); |
} |
} |
if (dhparam_config.C) { |
if (cfg.C) { |
unsigned char *data; |
unsigned char *data; |
int len, l, bits; |
int len, l, bits; |
|
|
|
|
printf("\treturn(dh);\n\t}\n"); |
printf("\treturn(dh);\n\t}\n"); |
free(data); |
free(data); |
} |
} |
if (!dhparam_config.noout) { |
if (!cfg.noout) { |
if (dhparam_config.outformat == FORMAT_ASN1) |
if (cfg.outformat == FORMAT_ASN1) |
i = i2d_DHparams_bio(out, dh); |
i = i2d_DHparams_bio(out, dh); |
else if (dhparam_config.outformat == FORMAT_PEM) |
else if (cfg.outformat == FORMAT_PEM) |
i = PEM_write_bio_DHparams(out, dh); |
i = PEM_write_bio_DHparams(out, dh); |
else { |
else { |
BIO_printf(bio_err, "bad output format specified for outfile\n"); |
BIO_printf(bio_err, "bad output format specified for outfile\n"); |