2 * Copyright 1995-2016 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 const 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
;
73 #ifndef OPENSSL_NO_DSA
76 int i
, text
= 0, C
= 0, ret
= 1, num
= 0, g
= 0;
77 int informat
= FORMAT_PEM
, outformat
= FORMAT_PEM
, check
= 0, noout
= 0;
80 prog
= opt_init(argc
, argv
, dhparam_options
);
81 while ((o
= opt_next()) != OPT_EOF
) {
86 BIO_printf(bio_err
, "%s: Use -help for summary.\n", prog
);
89 opt_help(dhparam_options
);
93 if (!opt_format(opt_arg(), OPT_FMT_PEMDER
, &informat
))
97 if (!opt_format(opt_arg(), OPT_FMT_PEMDER
, &outformat
))
107 (void)setup_engine(opt_arg(), 0);
116 #ifndef OPENSSL_NO_DSA
137 argc
= opt_num_rest();
140 if (argv
[0] && (!opt_int(argv
[0], &num
) || num
<= 0))
146 # ifndef OPENSSL_NO_DSA
149 "generator may not be chosen for DSA parameters\n");
162 ERR_print_errors(bio_err
);
166 BN_GENCB_set(cb
, dh_cb
, bio_err
);
167 if (!app_RAND_load_file(NULL
, 1) && inrand
== NULL
) {
169 "warning, not much extra random data, consider using the -rand option\n");
172 BIO_printf(bio_err
, "%ld semi-random bytes loaded\n",
173 app_RAND_load_files(inrand
));
175 # ifndef OPENSSL_NO_DSA
177 DSA
*dsa
= DSA_new();
180 "Generating DSA parameters, %d bit long prime\n", num
);
182 || !DSA_generate_parameters_ex(dsa
, num
, NULL
, 0, NULL
, NULL
,
186 ERR_print_errors(bio_err
);
190 dh
= DSA_dup_DH(dsa
);
194 ERR_print_errors(bio_err
);
202 "Generating DH parameters, %d bit long safe prime, generator %d\n",
204 BIO_printf(bio_err
, "This is going to take a long time\n");
205 if (dh
== NULL
|| !DH_generate_parameters_ex(dh
, num
, g
, cb
)) {
207 ERR_print_errors(bio_err
);
213 app_RAND_write_file(NULL
);
216 in
= bio_open_default(infile
, 'r', informat
);
220 # ifndef OPENSSL_NO_DSA
224 if (informat
== FORMAT_ASN1
)
225 dsa
= d2i_DSAparams_bio(in
, NULL
);
226 else /* informat == FORMAT_PEM */
227 dsa
= PEM_read_bio_DSAparams(in
, NULL
, NULL
, NULL
);
230 BIO_printf(bio_err
, "unable to load DSA parameters\n");
231 ERR_print_errors(bio_err
);
235 dh
= DSA_dup_DH(dsa
);
238 ERR_print_errors(bio_err
);
244 if (informat
== FORMAT_ASN1
)
245 dh
= d2i_DHparams_bio(in
, NULL
);
246 else /* informat == FORMAT_PEM */
247 dh
= PEM_read_bio_DHparams(in
, NULL
, NULL
, NULL
);
250 BIO_printf(bio_err
, "unable to load DH parameters\n");
251 ERR_print_errors(bio_err
);
259 out
= bio_open_default(outfile
, 'w', outformat
);
264 DHparams_print(out
, dh
);
268 if (!DH_check(dh
, &i
)) {
269 ERR_print_errors(bio_err
);
272 if (i
& DH_CHECK_P_NOT_PRIME
)
273 BIO_printf(bio_err
, "WARNING: p value is not prime\n");
274 if (i
& DH_CHECK_P_NOT_SAFE_PRIME
)
275 BIO_printf(bio_err
, "WARNING: p value is not a safe prime\n");
276 if (i
& DH_CHECK_Q_NOT_PRIME
)
277 BIO_printf(bio_err
, "WARNING: q value is not a prime\n");
278 if (i
& DH_CHECK_INVALID_Q_VALUE
)
279 BIO_printf(bio_err
, "WARNING: q value is invalid\n");
280 if (i
& DH_CHECK_INVALID_J_VALUE
)
281 BIO_printf(bio_err
, "WARNING: j value is invalid\n");
282 if (i
& DH_UNABLE_TO_CHECK_GENERATOR
)
284 "WARNING: unable to check the generator value\n");
285 if (i
& DH_NOT_SUITABLE_GENERATOR
)
286 BIO_printf(bio_err
, "WARNING: the g value is not a generator\n");
288 BIO_printf(bio_err
, "DH parameters appear to be ok.\n");
289 if (num
!= 0 && i
!= 0) {
291 * We have generated parameters but DH_check() indicates they are
292 * invalid! This should never happen!
294 BIO_printf(bio_err
, "ERROR: Invalid parameters generated\n");
301 const BIGNUM
*pbn
, *gbn
;
305 DH_get0_pqg(dh
, &pbn
, NULL
, &gbn
);
306 data
= app_malloc(len
, "print a BN");
307 BIO_printf(out
, "#ifndef HEADER_DH_H\n"
308 "# include <openssl/dh.h>\n"
311 BIO_printf(out
, "DH *get_dh%d()\n{\n", bits
);
312 print_bignum_var(out
, pbn
, "dhp", bits
, data
);
313 print_bignum_var(out
, gbn
, "dhg", bits
, data
);
314 BIO_printf(out
, " DH *dh = DH_new();\n"
315 " BIGNUM *dhp_bn, *dhg_bn;\n"
319 BIO_printf(out
, " dhp_bn = BN_bin2bn(dhp_%d, sizeof (dhp_%d), NULL);\n",
321 BIO_printf(out
, " dhg_bn = BN_bin2bn(dhg_%d, sizeof (dhg_%d), NULL);\n",
323 BIO_printf(out
, " if (dhp_bn == NULL || dhg_bn == NULL\n"
324 " || !DH_set0_pqg(dh, dhp_bn, NULL, dhg_bn)) {\n"
326 " BN_free(dhp_bn);\n"
327 " BN_free(dhg_bn);\n"
330 if (DH_get_length(dh
) > 0)
332 " if (!DH_set_length(dh, %ld)) {\n"
334 " }\n", DH_get_length(dh
));
335 BIO_printf(out
, " return dh;\n}\n");
341 DH_get0_pqg(dh
, NULL
, &q
, NULL
);
342 if (outformat
== FORMAT_ASN1
)
343 i
= i2d_DHparams_bio(out
, dh
);
345 i
= PEM_write_bio_DHxparams(out
, dh
);
347 i
= PEM_write_bio_DHparams(out
, dh
);
349 BIO_printf(bio_err
, "unable to write DH parameters\n");
350 ERR_print_errors(bio_err
);
362 static int dh_cb(int p
, int n
, BN_GENCB
*cb
)
374 BIO_write(BN_GENCB_get_arg(cb
), &c
, 1);
375 (void)BIO_flush(BN_GENCB_get_arg(cb
));