2 * Copyright 1995-2020 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 #ifndef OPENSSL_NO_DEPRECATED_3_0
11 /* We need to use some deprecated APIs */
12 # define OPENSSL_SUPPRESS_DEPRECATED
14 #include <openssl/opensslconf.h>
22 #include <openssl/bio.h>
23 #include <openssl/err.h>
24 #include <openssl/bn.h>
25 #include <openssl/dh.h>
26 #include <openssl/x509.h>
27 #include <openssl/pem.h>
29 #if !defined(OPENSSL_NO_DSA) && !defined(OPENSSL_NO_DEPRECATED_3_0)
30 # include <openssl/dsa.h>
35 #if !defined(OPENSSL_NO_DSA) && !defined(OPENSSL_NO_DEPRECATED_3_0)
36 static int dh_cb(int p, int n, BN_GENCB *cb);
38 static int gendh_cb(EVP_PKEY_CTX *ctx);
40 typedef enum OPTION_choice {
41 OPT_ERR = -1, OPT_EOF = 0, OPT_HELP,
42 OPT_INFORM, OPT_OUTFORM, OPT_IN, OPT_OUT,
43 OPT_ENGINE, OPT_CHECK, OPT_TEXT, OPT_NOOUT,
44 OPT_DSAPARAM, OPT_C, OPT_2, OPT_3, OPT_5,
45 OPT_R_ENUM, OPT_PROV_ENUM
48 const OPTIONS dhparam_options[] = {
49 {OPT_HELP_STR, 1, '-', "Usage: %s [options] [numbits]\n"},
51 OPT_SECTION("General"),
52 {"help", OPT_HELP, '-', "Display this summary"},
53 {"check", OPT_CHECK, '-', "Check the DH parameters"},
54 #ifndef OPENSSL_NO_DSA
55 {"dsaparam", OPT_DSAPARAM, '-',
56 "Read or generate DSA parameters, convert to DH"},
58 #ifndef OPENSSL_NO_ENGINE
59 {"engine", OPT_ENGINE, 's', "Use engine e, possibly a hardware device"},
63 {"in", OPT_IN, '<', "Input file"},
64 {"inform", OPT_INFORM, 'F', "Input format, DER or PEM"},
66 OPT_SECTION("Output"),
67 {"out", OPT_OUT, '>', "Output file"},
68 {"outform", OPT_OUTFORM, 'F', "Output format, DER or PEM"},
69 {"text", OPT_TEXT, '-', "Print a text form of the DH parameters"},
70 {"noout", OPT_NOOUT, '-', "Don't output any DH parameters"},
71 {"C", OPT_C, '-', "Print C code"},
72 {"2", OPT_2, '-', "Generate parameters using 2 as the generator value"},
73 {"3", OPT_3, '-', "Generate parameters using 3 as the generator value"},
74 {"5", OPT_5, '-', "Generate parameters using 5 as the generator value"},
80 {"numbits", 0, 0, "Number of bits if generating parameters (optional)"},
84 int dhparam_main(int argc, char **argv)
86 BIO *in = NULL, *out = NULL;
88 EVP_PKEY *pkey = NULL;
89 EVP_PKEY_CTX *ctx = NULL;
90 char *infile = NULL, *outfile = NULL, *prog;
92 #if !defined(OPENSSL_NO_DSA) && !defined(OPENSSL_NO_DEPRECATED_3_0)
95 int i, text = 0, C = 0, ret = 1, num = 0, g = 0;
96 int informat = FORMAT_PEM, outformat = FORMAT_PEM, check = 0, noout = 0;
99 prog = opt_init(argc, argv, dhparam_options);
100 while ((o = opt_next()) != OPT_EOF) {
105 BIO_printf(bio_err, "%s: Use -help for summary.\n", prog);
108 opt_help(dhparam_options);
112 if (!opt_format(opt_arg(), OPT_FMT_PEMDER, &informat))
116 if (!opt_format(opt_arg(), OPT_FMT_PEMDER, &outformat))
126 e = setup_engine(opt_arg(), 0);
135 #ifndef OPENSSL_NO_DSA
136 # ifdef OPENSSL_NO_DEPRECATED_3_0
137 BIO_printf(bio_err, "The dsaparam option is deprecated.\n");
163 if (!opt_provider(o))
168 argc = opt_num_rest();
171 if (argv[0] != NULL && (!opt_int(argv[0], &num) || num <= 0))
177 #if !defined(OPENSSL_NO_DSA) && !defined(OPENSSL_NO_DEPRECATED_3_0)
180 "generator may not be chosen for DSA parameters\n");
185 out = bio_open_default(outfile, 'w', outformat);
196 #if !defined(OPENSSL_NO_DSA) && !defined(OPENSSL_NO_DEPRECATED_3_0)
198 DSA *dsa = DSA_new();
199 BN_GENCB *cb = BN_GENCB_new();
202 ERR_print_errors(bio_err);
206 BN_GENCB_set(cb, dh_cb, bio_err);
209 "Generating DSA parameters, %d bit long prime\n", num);
211 || !DSA_generate_parameters_ex(dsa, num, NULL, 0, NULL, NULL,
215 ERR_print_errors(bio_err);
219 dh = DSA_dup_DH(dsa);
223 ERR_print_errors(bio_err);
229 ctx = EVP_PKEY_CTX_new_from_name(NULL, "DH", NULL);
231 ERR_print_errors(bio_err);
233 "Error, DH key generation context allocation failed\n");
236 EVP_PKEY_CTX_set_cb(ctx, gendh_cb);
237 EVP_PKEY_CTX_set_app_data(ctx, bio_err);
239 "Generating DH parameters, %d bit long safe prime, generator %d\n",
241 BIO_printf(bio_err, "This is going to take a long time\n");
242 if (!EVP_PKEY_paramgen_init(ctx)) {
244 "Error, unable to initialise DH param generation\n");
245 ERR_print_errors(bio_err);
249 if (!EVP_PKEY_CTX_set_dh_paramgen_prime_len(ctx, num)) {
250 BIO_printf(bio_err, "Error, unable to set DH prime length\n");
251 ERR_print_errors(bio_err);
254 if (!EVP_PKEY_paramgen(ctx, &pkey)) {
255 BIO_printf(bio_err, "Error, DH generation failed\n");
256 ERR_print_errors(bio_err);
261 in = bio_open_default(infile, 'r', informat);
265 #if !defined(OPENSSL_NO_DSA) && !defined(OPENSSL_NO_DEPRECATED_3_0)
269 if (informat == FORMAT_ASN1)
270 dsa = d2i_DSAparams_bio(in, NULL);
271 else /* informat == FORMAT_PEM */
272 dsa = PEM_read_bio_DSAparams(in, NULL, NULL, NULL);
275 BIO_printf(bio_err, "unable to load DSA parameters\n");
276 ERR_print_errors(bio_err);
280 dh = DSA_dup_DH(dsa);
283 ERR_print_errors(bio_err);
289 if (informat == FORMAT_ASN1) {
291 * We have no PEM header to determine what type of DH params it
292 * is. We'll just try both.
294 dh = ASN1_d2i_bio_of(DH, DH_new, d2i_DHparams, in, NULL);
295 /* BIO_reset() returns 0 for success for file BIOs only!!! */
296 if (dh == NULL && BIO_reset(in) == 0)
297 dh = ASN1_d2i_bio_of(DH, DH_new, d2i_DHxparams, in, NULL);
299 /* informat == FORMAT_PEM */
300 dh = PEM_read_bio_DHparams(in, NULL, NULL, NULL);
304 BIO_printf(bio_err, "unable to load DH parameters\n");
305 ERR_print_errors(bio_err);
313 EVP_PKEY_print_params(out, pkey, 4, NULL);
316 if (!EVP_PKEY_param_check(ctx) /* DH_check(dh, &i) */) {
317 ERR_print_errors(bio_err);
318 BIO_printf(bio_err, "ERROR: Invalid parameters generated\n");
321 BIO_printf(bio_err, "DH parameters appear to be ok.\n");
324 * We have generated parameters but DH_check() indicates they are
325 * invalid! This should never happen!
327 BIO_printf(bio_err, "ERROR: Invalid parameters generated\n");
334 const BIGNUM *pbn, *gbn;
336 dh = EVP_PKEY_get0_DH(pkey);
337 len = EVP_PKEY_size(pkey);
338 bits = EVP_PKEY_size(pkey);
339 DH_get0_pqg(dh, &pbn, NULL, &gbn);
340 data = app_malloc(len, "print a BN");
342 BIO_printf(out, "static DH *get_dh%d(void)\n{\n", bits);
343 print_bignum_var(out, pbn, "dhp", bits, data);
344 print_bignum_var(out, gbn, "dhg", bits, data);
345 BIO_printf(out, " DH *dh = DH_new();\n"
350 BIO_printf(out, " p = BN_bin2bn(dhp_%d, sizeof(dhp_%d), NULL);\n",
352 BIO_printf(out, " g = BN_bin2bn(dhg_%d, sizeof(dhg_%d), NULL);\n",
354 BIO_printf(out, " if (p == NULL || g == NULL\n"
355 " || !DH_set0_pqg(dh, p, NULL, g)) {\n"
361 if (DH_get_length(dh) > 0)
363 " if (!DH_set_length(dh, %ld)) {\n"
366 " }\n", DH_get_length(dh));
367 BIO_printf(out, " return dh;\n}\n");
373 DH_get0_pqg(dh, NULL, &q, NULL);
374 if (outformat == FORMAT_ASN1) {
376 i = ASN1_i2d_bio_of(DH, i2d_DHxparams, out, dh);
378 i = ASN1_i2d_bio_of(DH, i2d_DHparams, out, dh);
379 } else if (q != NULL) {
380 i = PEM_write_bio_DHxparams(out, dh);
382 i = PEM_write_bio_DHparams(out, dh);
385 BIO_printf(bio_err, "unable to write DH parameters\n");
386 ERR_print_errors(bio_err);
395 EVP_PKEY_CTX_free(ctx);
400 static int common_dh_cb(int p, BIO *b)
402 static const char symbols[] = ".+*\n";
403 char c = (p >= 0 && (size_t)p < sizeof(symbols) - 1) ? symbols[p] : '?';
410 #if !defined(OPENSSL_NO_DSA) && !defined(OPENSSL_NO_DEPRECATED_3_0)
411 static int dh_cb(int p, int n, BN_GENCB *cb)
413 return common_dh_cb(p, BN_GENCB_get_arg(cb));
417 static int gendh_cb(EVP_PKEY_CTX *ctx)
419 return common_dh_cb(EVP_PKEY_CTX_get_keygen_info(ctx, 0),
420 EVP_PKEY_CTX_get_app_data(ctx));