2 * Copyright 1995-2018 The OpenSSL Project Authors. All Rights Reserved.
4 * Licensed under the OpenSSL license (the "License"). You may not use
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
10 #include <openssl/opensslconf.h>
12 NON_EMPTY_TRANSLATION_UNIT
20 # include <openssl/bio.h>
21 # include <openssl/err.h>
22 # include <openssl/bn.h>
23 # include <openssl/dh.h>
24 # include <openssl/x509.h>
25 # include <openssl/pem.h>
27 # ifndef OPENSSL_NO_DSA
28 # include <openssl/dsa.h>
33 static int dh_cb(int p, int n, BN_GENCB *cb);
35 typedef enum OPTION_choice {
36 OPT_ERR = -1, OPT_EOF = 0, OPT_HELP,
37 OPT_INFORM, OPT_OUTFORM, OPT_IN, OPT_OUT,
38 OPT_ENGINE, OPT_CHECK, OPT_TEXT, OPT_NOOUT,
39 OPT_RAND, OPT_DSAPARAM, OPT_C, OPT_2, OPT_5
42 OPTIONS dhparam_options[] = {
43 {OPT_HELP_STR, 1, '-', "Usage: %s [flags] [numbits]\n"},
44 {OPT_HELP_STR, 1, '-', "Valid options are:\n"},
45 {"help", OPT_HELP, '-', "Display this summary"},
46 {"in", OPT_IN, '<', "Input file"},
47 {"inform", OPT_INFORM, 'F', "Input format, DER or PEM"},
48 {"outform", OPT_OUTFORM, 'F', "Output format, DER or PEM"},
49 {"out", OPT_OUT, '>', "Output file"},
50 {"check", OPT_CHECK, '-', "Check the DH parameters"},
51 {"text", OPT_TEXT, '-', "Print a text form of the DH parameters"},
52 {"noout", OPT_NOOUT, '-', "Don't output any DH parameters"},
53 {"rand", OPT_RAND, 's',
54 "Load the file(s) into the random number generator"},
55 {"C", OPT_C, '-', "Print C code"},
56 {"2", OPT_2, '-', "Generate parameters using 2 as the generator value"},
57 {"5", OPT_5, '-', "Generate parameters using 5 as the generator value"},
58 # ifndef OPENSSL_NO_DSA
59 {"dsaparam", OPT_DSAPARAM, '-',
60 "Read or generate DSA parameters, convert to DH"},
62 # ifndef OPENSSL_NO_ENGINE
63 {"engine", OPT_ENGINE, 's', "Use engine e, possibly a hardware device"},
68 int dhparam_main(int argc, char **argv)
70 BIO *in = NULL, *out = NULL;
72 char *infile = NULL, *outfile = NULL, *prog, *inrand = NULL;
74 #ifndef OPENSSL_NO_DSA
77 int i, text = 0, C = 0, ret = 1, num = 0, g = 0;
78 int informat = FORMAT_PEM, outformat = FORMAT_PEM, check = 0, noout = 0;
81 prog = opt_init(argc, argv, dhparam_options);
82 while ((o = opt_next()) != OPT_EOF) {
87 BIO_printf(bio_err, "%s: Use -help for summary.\n", prog);
90 opt_help(dhparam_options);
94 if (!opt_format(opt_arg(), OPT_FMT_PEMDER, &informat))
98 if (!opt_format(opt_arg(), OPT_FMT_PEMDER, &outformat))
108 e = setup_engine(opt_arg(), 0);
117 #ifndef OPENSSL_NO_DSA
138 argc = opt_num_rest();
141 if (argv[0] && (!opt_int(argv[0], &num) || num <= 0))
147 # ifndef OPENSSL_NO_DSA
150 "generator may not be chosen for DSA parameters\n");
155 out = bio_open_default(outfile, 'w', outformat);
168 ERR_print_errors(bio_err);
172 BN_GENCB_set(cb, dh_cb, bio_err);
173 if (!app_RAND_load_file(NULL, 1) && inrand == NULL) {
175 "warning, not much extra random data, consider using the -rand option\n");
178 BIO_printf(bio_err, "%ld semi-random bytes loaded\n",
179 app_RAND_load_files(inrand));
181 # ifndef OPENSSL_NO_DSA
183 DSA *dsa = DSA_new();
186 "Generating DSA parameters, %d bit long prime\n", num);
188 || !DSA_generate_parameters_ex(dsa, num, NULL, 0, NULL, NULL,
192 ERR_print_errors(bio_err);
196 dh = DSA_dup_DH(dsa);
200 ERR_print_errors(bio_err);
208 "Generating DH parameters, %d bit long safe prime, generator %d\n",
210 BIO_printf(bio_err, "This is going to take a long time\n");
211 if (dh == NULL || !DH_generate_parameters_ex(dh, num, g, cb)) {
213 ERR_print_errors(bio_err);
219 app_RAND_write_file(NULL);
222 in = bio_open_default(infile, 'r', informat);
226 # ifndef OPENSSL_NO_DSA
230 if (informat == FORMAT_ASN1)
231 dsa = d2i_DSAparams_bio(in, NULL);
232 else /* informat == FORMAT_PEM */
233 dsa = PEM_read_bio_DSAparams(in, NULL, NULL, NULL);
236 BIO_printf(bio_err, "unable to load DSA parameters\n");
237 ERR_print_errors(bio_err);
241 dh = DSA_dup_DH(dsa);
244 ERR_print_errors(bio_err);
250 if (informat == FORMAT_ASN1) {
252 * We have no PEM header to determine what type of DH params it
253 * is. We'll just try both.
255 dh = d2i_DHparams_bio(in, NULL);
256 /* BIO_reset() returns 0 for success for file BIOs only!!! */
257 if (dh == NULL && BIO_reset(in) == 0)
258 dh = d2i_DHxparams_bio(in, NULL);
260 /* informat == FORMAT_PEM */
261 dh = PEM_read_bio_DHparams(in, NULL, NULL, NULL);
265 BIO_printf(bio_err, "unable to load DH parameters\n");
266 ERR_print_errors(bio_err);
275 DHparams_print(out, dh);
279 if (!DH_check(dh, &i)) {
280 ERR_print_errors(bio_err);
283 if (i & DH_CHECK_P_NOT_PRIME)
284 BIO_printf(bio_err, "WARNING: p value is not prime\n");
285 if (i & DH_CHECK_P_NOT_SAFE_PRIME)
286 BIO_printf(bio_err, "WARNING: p value is not a safe prime\n");
287 if (i & DH_CHECK_Q_NOT_PRIME)
288 BIO_printf(bio_err, "WARNING: q value is not a prime\n");
289 if (i & DH_CHECK_INVALID_Q_VALUE)
290 BIO_printf(bio_err, "WARNING: q value is invalid\n");
291 if (i & DH_CHECK_INVALID_J_VALUE)
292 BIO_printf(bio_err, "WARNING: j value is invalid\n");
293 if (i & DH_UNABLE_TO_CHECK_GENERATOR)
295 "WARNING: unable to check the generator value\n");
296 if (i & DH_NOT_SUITABLE_GENERATOR)
297 BIO_printf(bio_err, "WARNING: the g value is not a generator\n");
299 BIO_printf(bio_err, "DH parameters appear to be ok.\n");
300 if (num != 0 && i != 0) {
302 * We have generated parameters but DH_check() indicates they are
303 * invalid! This should never happen!
305 BIO_printf(bio_err, "ERROR: Invalid parameters generated\n");
312 const BIGNUM *pbn, *gbn;
316 DH_get0_pqg(dh, &pbn, NULL, &gbn);
317 data = app_malloc(len, "print a BN");
318 BIO_printf(out, "#ifndef HEADER_DH_H\n"
319 "# include <openssl/dh.h>\n"
322 BIO_printf(out, "DH *get_dh%d()\n{\n", bits);
323 print_bignum_var(out, pbn, "dhp", bits, data);
324 print_bignum_var(out, gbn, "dhg", bits, data);
325 BIO_printf(out, " DH *dh = DH_new();\n"
326 " BIGNUM *dhp_bn, *dhg_bn;\n"
330 BIO_printf(out, " dhp_bn = BN_bin2bn(dhp_%d, sizeof(dhp_%d), NULL);\n",
332 BIO_printf(out, " dhg_bn = BN_bin2bn(dhg_%d, sizeof(dhg_%d), NULL);\n",
334 BIO_printf(out, " if (dhp_bn == NULL || dhg_bn == NULL\n"
335 " || !DH_set0_pqg(dh, dhp_bn, NULL, dhg_bn)) {\n"
337 " BN_free(dhp_bn);\n"
338 " BN_free(dhg_bn);\n"
341 if (DH_get_length(dh) > 0)
343 " if (!DH_set_length(dh, %ld)) {\n"
345 " }\n", DH_get_length(dh));
346 BIO_printf(out, " return dh;\n}\n");
352 DH_get0_pqg(dh, NULL, &q, NULL);
353 if (outformat == FORMAT_ASN1) {
355 i = i2d_DHxparams_bio(out, dh);
357 i = i2d_DHparams_bio(out, dh);
358 } else if (q != NULL)
359 i = PEM_write_bio_DHxparams(out, dh);
361 i = PEM_write_bio_DHparams(out, dh);
363 BIO_printf(bio_err, "unable to write DH parameters\n");
364 ERR_print_errors(bio_err);
377 static int dh_cb(int p, int n, BN_GENCB *cb)
389 BIO_write(BN_GENCB_get_arg(cb), &c, 1);
390 (void)BIO_flush(BN_GENCB_get_arg(cb));