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 #include <openssl/opensslconf.h>
18 #include <openssl/bio.h>
19 #include <openssl/err.h>
20 #include <openssl/bn.h>
21 #include <openssl/x509.h>
22 #include <openssl/pem.h>
23 #include <openssl/core_names.h>
24 #include <openssl/param_build.h>
25 #include <openssl/decoder.h>
29 static EVP_PKEY *dsa_to_dh(EVP_PKEY *dh);
30 static int gendh_cb(EVP_PKEY_CTX *ctx);
32 typedef enum OPTION_choice {
33 OPT_ERR = -1, OPT_EOF = 0, OPT_HELP,
34 OPT_INFORM, OPT_OUTFORM, OPT_IN, OPT_OUT,
35 OPT_ENGINE, OPT_CHECK, OPT_TEXT, OPT_NOOUT,
36 OPT_DSAPARAM, OPT_2, OPT_3, OPT_5,
37 OPT_R_ENUM, OPT_PROV_ENUM
40 const OPTIONS dhparam_options[] = {
41 {OPT_HELP_STR, 1, '-', "Usage: %s [options] [numbits]\n"},
43 OPT_SECTION("General"),
44 {"help", OPT_HELP, '-', "Display this summary"},
45 {"check", OPT_CHECK, '-', "Check the DH parameters"},
46 #ifndef OPENSSL_NO_DSA
47 {"dsaparam", OPT_DSAPARAM, '-',
48 "Read or generate DSA parameters, convert to DH"},
50 #ifndef OPENSSL_NO_ENGINE
51 {"engine", OPT_ENGINE, 's', "Use engine e, possibly a hardware device"},
55 {"in", OPT_IN, '<', "Input file"},
56 {"inform", OPT_INFORM, 'F', "Input format, DER or PEM"},
58 OPT_SECTION("Output"),
59 {"out", OPT_OUT, '>', "Output file"},
60 {"outform", OPT_OUTFORM, 'F', "Output format, DER or PEM"},
61 {"text", OPT_TEXT, '-', "Print a text form of the DH parameters"},
62 {"noout", OPT_NOOUT, '-', "Don't output any DH parameters"},
63 {"2", OPT_2, '-', "Generate parameters using 2 as the generator value"},
64 {"3", OPT_3, '-', "Generate parameters using 3 as the generator value"},
65 {"5", OPT_5, '-', "Generate parameters using 5 as the generator value"},
71 {"numbits", 0, 0, "Number of bits if generating parameters (optional)"},
75 int dhparam_main(int argc, char **argv)
77 BIO *in = NULL, *out = NULL;
78 DH *dh = NULL, *alloc_dh = NULL;
79 EVP_PKEY *pkey = NULL, *tmppkey = NULL;
80 EVP_PKEY_CTX *ctx = NULL;
81 char *infile = NULL, *outfile = NULL, *prog;
83 #if !defined(OPENSSL_NO_DSA) && !defined(OPENSSL_NO_DEPRECATED_3_0)
86 int i, text = 0, ret = 1, num = 0, g = 0;
87 int informat = FORMAT_PEM, outformat = FORMAT_PEM, check = 0, noout = 0;
90 prog = opt_init(argc, argv, dhparam_options);
91 while ((o = opt_next()) != OPT_EOF) {
96 BIO_printf(bio_err, "%s: Use -help for summary.\n", prog);
99 opt_help(dhparam_options);
103 if (!opt_format(opt_arg(), OPT_FMT_PEMDER, &informat))
107 if (!opt_format(opt_arg(), OPT_FMT_PEMDER, &outformat))
117 e = setup_engine(opt_arg(), 0);
126 # ifdef OPENSSL_NO_DEPRECATED_3_0
127 BIO_printf(bio_err, "The dsaparam option is deprecated.\n");
149 if (!opt_provider(o))
154 argc = opt_num_rest();
157 if (argv[0] != NULL && (!opt_int(argv[0], &num) || num <= 0))
165 "Error, generator may not be chosen for DSA parameters\n");
169 out = bio_open_default(outfile, 'w', outformat);
178 const char *alg = dsaparam ? "DSA" : "DH";
180 ctx = EVP_PKEY_CTX_new_from_name(NULL, alg, NULL);
183 "Error, %s param generation context allocation failed\n",
187 EVP_PKEY_CTX_set_cb(ctx, gendh_cb);
188 EVP_PKEY_CTX_set_app_data(ctx, bio_err);
190 "Generating %s parameters, %d bit long %sprime\n",
191 alg, num, dsaparam ? "" : "safe ");
193 if (!EVP_PKEY_paramgen_init(ctx)) {
195 "Error, unable to initialise %s parameters\n",
201 if (!EVP_PKEY_CTX_set_dsa_paramgen_bits(ctx, num)) {
202 BIO_printf(bio_err, "Error, unable to set DSA prime length\n");
206 if (!EVP_PKEY_CTX_set_dh_paramgen_prime_len(ctx, num)) {
207 BIO_printf(bio_err, "Error, unable to set DH prime length\n");
210 if (!EVP_PKEY_CTX_set_dh_paramgen_generator(ctx, g)) {
211 BIO_printf(bio_err, "Error, unable to set generator\n");
216 if (!EVP_PKEY_paramgen(ctx, &tmppkey)) {
217 BIO_printf(bio_err, "Error, %s generation failed\n", alg);
221 EVP_PKEY_CTX_free(ctx);
224 pkey = dsa_to_dh(tmppkey);
227 EVP_PKEY_free(tmppkey);
233 OSSL_DECODER_CTX *decoderctx = NULL;
234 const char *keytype = "DH";
237 in = bio_open_default(infile, 'r', informat);
243 * We assume we're done unless we explicitly want to retry and set
248 * We set NULL for the keytype to allow any key type. We don't know
249 * if we're going to get DH or DHX (or DSA in the event of dsaparam).
250 * We check that we got one of those key types afterwards.
253 = OSSL_DECODER_CTX_new_by_EVP_PKEY(&tmppkey,
254 (informat == FORMAT_ASN1)
256 (informat == FORMAT_ASN1)
260 if (decoderctx != NULL
261 && !OSSL_DECODER_from_bio(decoderctx, in)
262 && informat == FORMAT_ASN1
263 && strcmp(keytype, "DH") == 0) {
265 * When reading DER we explicitly state the expected keytype
266 * because, unlike PEM, there is no header to declare what
267 * the contents of the DER file are. The decoders just try
268 * and guess. Unfortunately with DHX key types they may guess
269 * wrong and think we have a DSA keytype. Therefore we try
270 * both DH and DHX sequentially.
274 * BIO_reset() returns 0 for success for file BIOs only!!!
275 * This won't work for stdin (and never has done)
276 * TODO: We should fix this at some point
278 if (BIO_reset(in) == 0)
281 OSSL_DECODER_CTX_free(decoderctx);
283 if (tmppkey == NULL) {
284 BIO_printf(bio_err, "Error, unable to load parameters\n");
289 if (!EVP_PKEY_is_a(tmppkey, "DSA")) {
290 BIO_printf(bio_err, "Error, unable to load DSA parameters\n");
293 pkey = dsa_to_dh(tmppkey);
297 if (!EVP_PKEY_is_a(tmppkey, "DH")
298 && !EVP_PKEY_is_a(tmppkey, "DHX")) {
299 BIO_printf(bio_err, "Error, unable to load DH parameters\n");
308 EVP_PKEY_print_params(out, pkey, 4, NULL);
311 ctx = EVP_PKEY_CTX_new_from_name(NULL, "DH", NULL);
313 BIO_printf(bio_err, "Error, failed to check DH parameters\n");
316 if (!EVP_PKEY_param_check(ctx) /* DH_check(dh, &i) */) {
317 BIO_printf(bio_err, "Error, invalid parameters generated\n");
320 BIO_printf(bio_err, "DH parameters appear to be ok.\n");
323 * We have generated parameters but DH_check() indicates they are
324 * invalid! This should never happen!
326 BIO_printf(bio_err, "Error, invalid parameters generated\n");
333 /* Temporary: FIXME */
334 dh = EVP_PKEY_get0_DH(pkey);
335 DH_get0_pqg(dh, NULL, &q, NULL);
336 if (outformat == FORMAT_ASN1) {
338 i = ASN1_i2d_bio_of(DH, i2d_DHxparams, out, dh);
340 i = ASN1_i2d_bio_of(DH, i2d_DHparams, out, dh);
341 } else if (q != NULL) {
342 i = PEM_write_bio_DHxparams(out, dh);
344 i = PEM_write_bio_DHparams(out, dh);
347 BIO_printf(bio_err, "Error, unable to write DH parameters\n");
354 ERR_print_errors(bio_err);
359 EVP_PKEY_free(tmppkey);
360 EVP_PKEY_CTX_free(ctx);
366 * Historically we had the low level call DSA_dup_DH() to do this.
367 * That is now deprecated with no replacement. Since we still need to do this
368 * for backwards compatibility reasons, we do it "manually".
370 static EVP_PKEY *dsa_to_dh(EVP_PKEY *dh)
372 OSSL_PARAM_BLD *tmpl = NULL;
373 OSSL_PARAM *params = NULL;
374 BIGNUM *bn_p = NULL, *bn_q = NULL, *bn_g = NULL;
375 EVP_PKEY_CTX *ctx = NULL;
376 EVP_PKEY *pkey = NULL;
378 if (!EVP_PKEY_get_bn_param(dh, OSSL_PKEY_PARAM_FFC_P, &bn_p)
379 || !EVP_PKEY_get_bn_param(dh, OSSL_PKEY_PARAM_FFC_Q, &bn_q)
380 || !EVP_PKEY_get_bn_param(dh, OSSL_PKEY_PARAM_FFC_G, &bn_g)) {
381 BIO_printf(bio_err, "Error, failed to set DH parameters\n");
385 if ((tmpl = OSSL_PARAM_BLD_new()) == NULL
386 || !OSSL_PARAM_BLD_push_BN(tmpl, OSSL_PKEY_PARAM_FFC_P,
388 || !OSSL_PARAM_BLD_push_BN(tmpl, OSSL_PKEY_PARAM_FFC_Q,
390 || !OSSL_PARAM_BLD_push_BN(tmpl, OSSL_PKEY_PARAM_FFC_G,
392 || (params = OSSL_PARAM_BLD_to_param(tmpl)) == NULL) {
393 BIO_printf(bio_err, "Error, failed to set DH parameters\n");
397 ctx = EVP_PKEY_CTX_new_from_name(NULL, "DHX", NULL);
399 || !EVP_PKEY_param_fromdata_init(ctx)
400 || !EVP_PKEY_fromdata(ctx, &pkey, params)) {
401 BIO_printf(bio_err, "Error, failed to set DH parameters\n");
406 EVP_PKEY_CTX_free(ctx);
407 OSSL_PARAM_BLD_free_params(params);
408 OSSL_PARAM_BLD_free(tmpl);
415 static int gendh_cb(EVP_PKEY_CTX *ctx)
417 int p = EVP_PKEY_CTX_get_keygen_info(ctx, 0);
418 BIO *b = EVP_PKEY_CTX_get_app_data(ctx);
419 static const char symbols[] = ".+*\n";
420 char c = (p >= 0 && (size_t)p < sizeof(symbols) - 1) ? symbols[p] : '?';