2 * Copyright 1995-2018 The OpenSSL Project Authors. All Rights Reserved.
4 * Licensed under the Apache License 2.0 (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 /* We need to use some deprecated APIs */
11 #define OPENSSL_SUPPRESS_DEPRECATED
13 #include <openssl/opensslconf.h>
21 #include <openssl/bio.h>
22 #include <openssl/err.h>
23 #include <openssl/bn.h>
24 #include <openssl/dh.h>
25 #include <openssl/x509.h>
26 #include <openssl/pem.h>
28 #ifndef OPENSSL_NO_DSA
29 # include <openssl/dsa.h>
34 static int dh_cb(int p, int n, BN_GENCB *cb);
36 typedef enum OPTION_choice {
37 OPT_ERR = -1, OPT_EOF = 0, OPT_HELP,
38 OPT_INFORM, OPT_OUTFORM, OPT_IN, OPT_OUT,
39 OPT_ENGINE, OPT_CHECK, OPT_TEXT, OPT_NOOUT,
40 OPT_DSAPARAM, OPT_C, OPT_2, OPT_3, OPT_5,
41 OPT_R_ENUM, OPT_PROV_ENUM
44 const OPTIONS dhparam_options[] = {
45 {OPT_HELP_STR, 1, '-', "Usage: %s [options] [numbits]\n"},
47 OPT_SECTION("General"),
48 {"help", OPT_HELP, '-', "Display this summary"},
49 {"check", OPT_CHECK, '-', "Check the DH parameters"},
50 #ifndef OPENSSL_NO_DSA
51 {"dsaparam", OPT_DSAPARAM, '-',
52 "Read or generate DSA parameters, convert to DH"},
54 #ifndef OPENSSL_NO_ENGINE
55 {"engine", OPT_ENGINE, 's', "Use engine e, possibly a hardware device"},
59 {"in", OPT_IN, '<', "Input file"},
60 {"inform", OPT_INFORM, 'F', "Input format, DER or PEM"},
62 OPT_SECTION("Output"),
63 {"out", OPT_OUT, '>', "Output file"},
64 {"outform", OPT_OUTFORM, 'F', "Output format, DER or PEM"},
65 {"text", OPT_TEXT, '-', "Print a text form of the DH parameters"},
66 {"noout", OPT_NOOUT, '-', "Don't output any DH parameters"},
67 {"C", OPT_C, '-', "Print C code"},
68 {"2", OPT_2, '-', "Generate parameters using 2 as the generator value"},
69 {"3", OPT_3, '-', "Generate parameters using 3 as the generator value"},
70 {"5", OPT_5, '-', "Generate parameters using 5 as the generator value"},
76 {"numbits", 0, 0, "Number of bits if generating parameters (optional)"},
80 int dhparam_main(int argc, char **argv)
82 BIO *in = NULL, *out = NULL;
84 char *infile = NULL, *outfile = NULL, *prog;
86 #ifndef OPENSSL_NO_DSA
89 int i, text = 0, C = 0, ret = 1, num = 0, g = 0;
90 int informat = FORMAT_PEM, outformat = FORMAT_PEM, check = 0, noout = 0;
93 prog = opt_init(argc, argv, dhparam_options);
94 while ((o = opt_next()) != OPT_EOF) {
99 BIO_printf(bio_err, "%s: Use -help for summary.\n", prog);
102 opt_help(dhparam_options);
106 if (!opt_format(opt_arg(), OPT_FMT_PEMDER, &informat))
110 if (!opt_format(opt_arg(), OPT_FMT_PEMDER, &outformat))
120 e = setup_engine(opt_arg(), 0);
129 #ifndef OPENSSL_NO_DSA
153 if (!opt_provider(o))
158 argc = opt_num_rest();
161 if (argv[0] != NULL && (!opt_int(argv[0], &num) || num <= 0))
167 #ifndef OPENSSL_NO_DSA
170 "generator may not be chosen for DSA parameters\n");
175 out = bio_open_default(outfile, 'w', outformat);
188 ERR_print_errors(bio_err);
192 BN_GENCB_set(cb, dh_cb, bio_err);
194 #ifndef OPENSSL_NO_DSA
196 DSA *dsa = DSA_new();
199 "Generating DSA parameters, %d bit long prime\n", num);
201 || !DSA_generate_parameters_ex(dsa, num, NULL, 0, NULL, NULL,
205 ERR_print_errors(bio_err);
209 dh = DSA_dup_DH(dsa);
213 ERR_print_errors(bio_err);
221 "Generating DH parameters, %d bit long safe prime, generator %d\n",
223 BIO_printf(bio_err, "This is going to take a long time\n");
224 if (dh == NULL || !DH_generate_parameters_ex(dh, num, g, cb)) {
226 ERR_print_errors(bio_err);
234 in = bio_open_default(infile, 'r', informat);
238 #ifndef OPENSSL_NO_DSA
242 if (informat == FORMAT_ASN1)
243 dsa = d2i_DSAparams_bio(in, NULL);
244 else /* informat == FORMAT_PEM */
245 dsa = PEM_read_bio_DSAparams(in, NULL, NULL, NULL);
248 BIO_printf(bio_err, "unable to load DSA parameters\n");
249 ERR_print_errors(bio_err);
253 dh = DSA_dup_DH(dsa);
256 ERR_print_errors(bio_err);
262 if (informat == FORMAT_ASN1) {
264 * We have no PEM header to determine what type of DH params it
265 * is. We'll just try both.
267 dh = d2i_DHparams_bio(in, NULL);
268 /* BIO_reset() returns 0 for success for file BIOs only!!! */
269 if (dh == NULL && BIO_reset(in) == 0)
270 dh = d2i_DHxparams_bio(in, NULL);
272 /* informat == FORMAT_PEM */
273 dh = PEM_read_bio_DHparams(in, NULL, NULL, NULL);
277 BIO_printf(bio_err, "unable to load DH parameters\n");
278 ERR_print_errors(bio_err);
287 DHparams_print(out, dh);
291 if (!DH_check(dh, &i)) {
292 ERR_print_errors(bio_err);
295 if (i & DH_CHECK_P_NOT_PRIME)
296 BIO_printf(bio_err, "WARNING: p value is not prime\n");
297 if (i & DH_CHECK_P_NOT_SAFE_PRIME)
298 BIO_printf(bio_err, "WARNING: p value is not a safe prime\n");
299 if (i & DH_CHECK_Q_NOT_PRIME)
300 BIO_printf(bio_err, "WARNING: q value is not a prime\n");
301 if (i & DH_CHECK_INVALID_Q_VALUE)
302 BIO_printf(bio_err, "WARNING: q value is invalid\n");
303 if (i & DH_CHECK_INVALID_J_VALUE)
304 BIO_printf(bio_err, "WARNING: j value is invalid\n");
305 if (i & DH_UNABLE_TO_CHECK_GENERATOR)
307 "WARNING: unable to check the generator value\n");
308 if (i & DH_NOT_SUITABLE_GENERATOR)
309 BIO_printf(bio_err, "WARNING: the g value is not a generator\n");
311 BIO_printf(bio_err, "DH parameters appear to be ok.\n");
312 if (num != 0 && i != 0) {
314 * We have generated parameters but DH_check() indicates they are
315 * invalid! This should never happen!
317 BIO_printf(bio_err, "ERROR: Invalid parameters generated\n");
324 const BIGNUM *pbn, *gbn;
328 DH_get0_pqg(dh, &pbn, NULL, &gbn);
329 data = app_malloc(len, "print a BN");
331 BIO_printf(out, "static DH *get_dh%d(void)\n{\n", bits);
332 print_bignum_var(out, pbn, "dhp", bits, data);
333 print_bignum_var(out, gbn, "dhg", bits, data);
334 BIO_printf(out, " DH *dh = DH_new();\n"
339 BIO_printf(out, " p = BN_bin2bn(dhp_%d, sizeof(dhp_%d), NULL);\n",
341 BIO_printf(out, " g = BN_bin2bn(dhg_%d, sizeof(dhg_%d), NULL);\n",
343 BIO_printf(out, " if (p == NULL || g == NULL\n"
344 " || !DH_set0_pqg(dh, p, NULL, g)) {\n"
350 if (DH_get_length(dh) > 0)
352 " if (!DH_set_length(dh, %ld)) {\n"
355 " }\n", DH_get_length(dh));
356 BIO_printf(out, " return dh;\n}\n");
362 DH_get0_pqg(dh, NULL, &q, NULL);
363 if (outformat == FORMAT_ASN1) {
365 i = i2d_DHxparams_bio(out, dh);
367 i = i2d_DHparams_bio(out, dh);
368 } else if (q != NULL) {
369 i = PEM_write_bio_DHxparams(out, dh);
371 i = PEM_write_bio_DHparams(out, dh);
374 BIO_printf(bio_err, "unable to write DH parameters\n");
375 ERR_print_errors(bio_err);
388 static int dh_cb(int p, int n, BN_GENCB *cb)
390 static const char symbols[] = ".+*\n";
391 char c = (p >= 0 && (size_t)p < sizeof(symbols) - 1) ? symbols[p] : '?';
393 BIO_write(BN_GENCB_get_arg(cb), &c, 1);
394 (void)BIO_flush(BN_GENCB_get_arg(cb));