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_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 {"2", OPT_2, '-', "Generate parameters using 2 as the generator value"},
72 {"3", OPT_3, '-', "Generate parameters using 3 as the generator value"},
73 {"5", OPT_5, '-', "Generate parameters using 5 as the generator value"},
79 {"numbits", 0, 0, "Number of bits if generating parameters (optional)"},
83 int dhparam_main(int argc, char **argv)
85 BIO *in = NULL, *out = NULL;
86 DH *dh = NULL, *alloc_dh = NULL;
87 EVP_PKEY *pkey = NULL;
88 EVP_PKEY_CTX *ctx = NULL;
89 char *infile = NULL, *outfile = NULL, *prog;
91 #if !defined(OPENSSL_NO_DSA) && !defined(OPENSSL_NO_DEPRECATED_3_0)
94 int i, text = 0, ret = 1, num = 0, g = 0;
95 int informat = FORMAT_PEM, outformat = FORMAT_PEM, check = 0, noout = 0;
98 prog = opt_init(argc, argv, dhparam_options);
99 while ((o = opt_next()) != OPT_EOF) {
104 BIO_printf(bio_err, "%s: Use -help for summary.\n", prog);
107 opt_help(dhparam_options);
111 if (!opt_format(opt_arg(), OPT_FMT_PEMDER, &informat))
115 if (!opt_format(opt_arg(), OPT_FMT_PEMDER, &outformat))
125 e = setup_engine(opt_arg(), 0);
134 #ifndef OPENSSL_NO_DSA
135 # ifdef OPENSSL_NO_DEPRECATED_3_0
136 BIO_printf(bio_err, "The dsaparam option is deprecated.\n");
159 if (!opt_provider(o))
164 argc = opt_num_rest();
167 if (argv[0] != NULL && (!opt_int(argv[0], &num) || num <= 0))
173 #if !defined(OPENSSL_NO_DSA) && !defined(OPENSSL_NO_DEPRECATED_3_0)
176 "Error, generator may not be chosen for DSA parameters\n");
181 out = bio_open_default(outfile, 'w', outformat);
192 #if !defined(OPENSSL_NO_DSA) && !defined(OPENSSL_NO_DEPRECATED_3_0)
194 DSA *dsa = DSA_new();
195 BN_GENCB *cb = BN_GENCB_new();
200 BN_GENCB_set(cb, dh_cb, bio_err);
203 "Generating DSA parameters, %d bit long prime\n", num);
205 || !DSA_generate_parameters_ex(dsa, num, NULL, 0, NULL, NULL,
209 BIO_printf(bio_err, "Error, unable to generate DSA parameters\n");
213 dh = alloc_dh = DSA_dup_DH(dsa);
221 ctx = EVP_PKEY_CTX_new_from_name(NULL, "DH", NULL);
224 "Error, DH key generation context allocation failed\n");
227 EVP_PKEY_CTX_set_cb(ctx, gendh_cb);
228 EVP_PKEY_CTX_set_app_data(ctx, bio_err);
230 "Generating DH parameters, %d bit long safe prime, generator %d\n",
232 BIO_printf(bio_err, "This is going to take a long time\n");
233 if (!EVP_PKEY_paramgen_init(ctx)) {
235 "Error, unable to initialise DH param generation\n");
239 if (!EVP_PKEY_CTX_set_dh_paramgen_prime_len(ctx, num)) {
240 BIO_printf(bio_err, "Error, unable to set DH prime length\n");
243 if (!EVP_PKEY_paramgen(ctx, &pkey)) {
244 BIO_printf(bio_err, "Error, DH generation failed\n");
247 dh = EVP_PKEY_get0_DH(pkey);
250 in = bio_open_default(infile, 'r', informat);
254 #if !defined(OPENSSL_NO_DSA) && !defined(OPENSSL_NO_DEPRECATED_3_0)
258 if (informat == FORMAT_ASN1)
259 dsa = d2i_DSAparams_bio(in, NULL);
260 else /* informat == FORMAT_PEM */
261 dsa = PEM_read_bio_DSAparams(in, NULL, NULL, NULL);
264 BIO_printf(bio_err, "Error, unable to load DSA parameters\n");
268 dh = alloc_dh = DSA_dup_DH(dsa);
275 if (informat == FORMAT_ASN1) {
277 * We have no PEM header to determine what type of DH params it
278 * is. We'll just try both.
280 dh = alloc_dh = ASN1_d2i_bio_of(DH, DH_new, d2i_DHparams, in, NULL);
281 /* BIO_reset() returns 0 for success for file BIOs only!!! */
282 if (dh == NULL && BIO_reset(in) == 0)
283 dh = alloc_dh = ASN1_d2i_bio_of(DH, DH_new, d2i_DHxparams, in, NULL);
285 /* informat == FORMAT_PEM */
286 dh = alloc_dh = PEM_read_bio_DHparams(in, NULL, NULL, NULL);
290 BIO_printf(bio_err, "Error, unable to load DH parameters\n");
298 EVP_PKEY_print_params(out, pkey, 4, NULL);
301 if (!EVP_PKEY_param_check(ctx) /* DH_check(dh, &i) */) {
302 BIO_printf(bio_err, "Error, invalid parameters generated\n");
305 BIO_printf(bio_err, "DH parameters appear to be ok.\n");
308 * We have generated parameters but DH_check() indicates they are
309 * invalid! This should never happen!
311 BIO_printf(bio_err, "Error, invalid parameters generated\n");
318 DH_get0_pqg(dh, NULL, &q, NULL);
319 if (outformat == FORMAT_ASN1) {
321 i = ASN1_i2d_bio_of(DH, i2d_DHxparams, out, dh);
323 i = ASN1_i2d_bio_of(DH, i2d_DHparams, out, dh);
324 } else if (q != NULL) {
325 i = PEM_write_bio_DHxparams(out, dh);
327 i = PEM_write_bio_DHparams(out, dh);
330 BIO_printf(bio_err, "Error, unable to write DH parameters\n");
337 ERR_print_errors(bio_err);
342 EVP_PKEY_CTX_free(ctx);
347 static int common_dh_cb(int p, BIO *b)
349 static const char symbols[] = ".+*\n";
350 char c = (p >= 0 && (size_t)p < sizeof(symbols) - 1) ? symbols[p] : '?';
357 #if !defined(OPENSSL_NO_DSA) && !defined(OPENSSL_NO_DEPRECATED_3_0)
358 static int dh_cb(int p, int n, BN_GENCB *cb)
360 return common_dh_cb(p, BN_GENCB_get_arg(cb));
364 static int gendh_cb(EVP_PKEY_CTX *ctx)
366 return common_dh_cb(EVP_PKEY_CTX_get_keygen_info(ctx, 0),
367 EVP_PKEY_CTX_get_app_data(ctx));