#include <openssl/opensslconf.h>
#ifndef OPENSSL_NO_DH
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <time.h>
#include "apps.h"
#include <openssl/bio.h>
#include <openssl/bn.h>
#include <openssl/err.h>
#include <openssl/dh.h>
#include <openssl/pem.h>
#include <openssl/x509.h>
#include <openssl/dsa.h>
#define DEFBITS 2048
static struct {
int check;
int dsaparam;
int g;
char *infile;
int informat;
int noout;
char *outfile;
int outformat;
int text;
} cfg;
static const struct option dhparam_options[] = {
{
.name = "2",
.desc = "Generate DH parameters with a generator value of 2 "
"(default)",
.type = OPTION_VALUE,
.opt.value = &cfg.g,
.value = 2,
},
{
.name = "5",
.desc = "Generate DH parameters with a generator value of 5",
.type = OPTION_VALUE,
.opt.value = &cfg.g,
.value = 5,
},
{
.name = "check",
.desc = "Check the DH parameters",
.type = OPTION_FLAG,
.opt.flag = &cfg.check,
},
{
.name = "dsaparam",
.desc = "Read or generate DSA parameters and convert to DH",
.type = OPTION_FLAG,
.opt.flag = &cfg.dsaparam,
},
{
.name = "in",
.argname = "file",
.desc = "Input file (default stdin)",
.type = OPTION_ARG,
.opt.arg = &cfg.infile,
},
{
.name = "inform",
.argname = "format",
.desc = "Input format (DER or PEM (default))",
.type = OPTION_ARG_FORMAT,
.opt.value = &cfg.informat,
},
{
.name = "noout",
.desc = "Do not output encoded version of DH parameters",
.type = OPTION_FLAG,
.opt.flag = &cfg.noout,
},
{
.name = "out",
.argname = "file",
.desc = "Output file (default stdout)",
.type = OPTION_ARG,
.opt.arg = &cfg.outfile,
},
{
.name = "outform",
.argname = "format",
.desc = "Output format (DER or PEM (default))",
.type = OPTION_ARG_FORMAT,
.opt.value = &cfg.outformat,
},
{
.name = "text",
.desc = "Print DH parameters in plain text",
.type = OPTION_FLAG,
.opt.flag = &cfg.text,
},
{ NULL },
};
static void
dhparam_usage(void)
{
fprintf(stderr,
"usage: dhparam [-2 | -5] [-check] [-dsaparam]\n"
" [-in file] [-inform DER | PEM] [-noout] [-out file]\n"
" [-outform DER | PEM] [-text] [numbits]\n\n");
options_usage(dhparam_options);
}
static int dh_cb(int p, int n, BN_GENCB *cb);
int
dhparam_main(int argc, char **argv)
{
BIO *in = NULL, *out = NULL;
BN_GENCB *cb = NULL;
char *num_bits = NULL;
DH *dh = NULL;
int num = 0;
int ret = 1;
int i;
if (pledge("stdio cpath wpath rpath", NULL) == -1) {
perror("pledge");
exit(1);
}
memset(&cfg, 0, sizeof(cfg));
cfg.informat = FORMAT_PEM;
cfg.outformat = FORMAT_PEM;
if (options_parse(argc, argv, dhparam_options, &num_bits, NULL) != 0) {
dhparam_usage();
return (1);
}
if (num_bits != NULL) {
if(sscanf(num_bits, "%d", &num) == 0 || num <= 0) {
BIO_printf(bio_err, "invalid number of bits: %s\n",
num_bits);
return (1);
}
}
if (cfg.g && !num)
num = DEFBITS;
if (cfg.dsaparam) {
if (cfg.g) {
BIO_printf(bio_err, "generator may not be chosen for DSA parameters\n");
goto end;
}
} else {
if (num && !cfg.g)
cfg.g = 2;
}
if (num) {
if ((cb = BN_GENCB_new()) == NULL) {
BIO_printf(bio_err,
"Error allocating BN_GENCB object\n");
goto end;
}
BN_GENCB_set(cb, dh_cb, bio_err);
if (cfg.dsaparam) {
DSA *dsa = DSA_new();
BIO_printf(bio_err, "Generating DSA parameters, %d bit long prime\n", num);
if (!dsa || !DSA_generate_parameters_ex(dsa, num,
NULL, 0, NULL, NULL, cb)) {
DSA_free(dsa);
ERR_print_errors(bio_err);
goto end;
}
dh = DSA_dup_DH(dsa);
DSA_free(dsa);
if (dh == NULL) {
ERR_print_errors(bio_err);
goto end;
}
} else {
dh = DH_new();
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");
if (!dh || !DH_generate_parameters_ex(dh, num, cfg.g, cb)) {
ERR_print_errors(bio_err);
goto end;
}
}
} else {
in = BIO_new(BIO_s_file());
if (in == NULL) {
ERR_print_errors(bio_err);
goto end;
}
if (cfg.infile == NULL)
BIO_set_fp(in, stdin, BIO_NOCLOSE);
else {
if (BIO_read_filename(in, cfg.infile) <= 0) {
perror(cfg.infile);
goto end;
}
}
if (cfg.informat != FORMAT_ASN1 &&
cfg.informat != FORMAT_PEM) {
BIO_printf(bio_err, "bad input format specified\n");
goto end;
}
if (cfg.dsaparam) {
DSA *dsa;
if (cfg.informat == FORMAT_ASN1)
dsa = d2i_DSAparams_bio(in, NULL);
else
dsa = PEM_read_bio_DSAparams(in, NULL, NULL, NULL);
if (dsa == NULL) {
BIO_printf(bio_err, "unable to load DSA parameters\n");
ERR_print_errors(bio_err);
goto end;
}
dh = DSA_dup_DH(dsa);
DSA_free(dsa);
if (dh == NULL) {
ERR_print_errors(bio_err);
goto end;
}
} else
{
if (cfg.informat == FORMAT_ASN1)
dh = d2i_DHparams_bio(in, NULL);
else
dh = PEM_read_bio_DHparams(in, NULL, NULL, NULL);
if (dh == NULL) {
BIO_printf(bio_err, "unable to load DH parameters\n");
ERR_print_errors(bio_err);
goto end;
}
}
}
out = BIO_new(BIO_s_file());
if (out == NULL) {
ERR_print_errors(bio_err);
goto end;
}
if (cfg.outfile == NULL) {
BIO_set_fp(out, stdout, BIO_NOCLOSE);
} else {
if (BIO_write_filename(out, cfg.outfile) <= 0) {
perror(cfg.outfile);
goto end;
}
}
if (cfg.text) {
DHparams_print(out, dh);
}
if (cfg.check) {
if (!DH_check(dh, &i)) {
ERR_print_errors(bio_err);
goto end;
}
if (i & DH_CHECK_P_NOT_PRIME)
printf("p value is not prime\n");
if (i & DH_CHECK_P_NOT_SAFE_PRIME)
printf("p value is not a safe prime\n");
if (i & DH_UNABLE_TO_CHECK_GENERATOR)
printf("unable to check the generator value\n");
if (i & DH_NOT_SUITABLE_GENERATOR)
printf("the g value is not a generator\n");
if (i == 0)
printf("DH parameters appear to be ok.\n");
}
if (!cfg.noout) {
if (cfg.outformat == FORMAT_ASN1)
i = i2d_DHparams_bio(out, dh);
else if (cfg.outformat == FORMAT_PEM)
i = PEM_write_bio_DHparams(out, dh);
else {
BIO_printf(bio_err, "bad output format specified for outfile\n");
goto end;
}
if (!i) {
BIO_printf(bio_err, "unable to write DH parameters\n");
ERR_print_errors(bio_err);
goto end;
}
}
ret = 0;
end:
BIO_free(in);
BIO_free_all(out);
BN_GENCB_free(cb);
DH_free(dh);
return (ret);
}
static int
dh_cb(int p, int n, BN_GENCB *cb)
{
char c = '*';
if (p == 0)
c = '.';
if (p == 1)
c = '+';
if (p == 2)
c = '*';
if (p == 3)
c = '\n';
BIO_write(BN_GENCB_get_arg(cb), &c, 1);
(void) BIO_flush(BN_GENCB_get_arg(cb));
return 1;
}
#endif