1c7da899bSchristos /*
2*4778aedeSchristos * Copyright 1995-2023 The OpenSSL Project Authors. All Rights Reserved.
3a89c9211Schristos *
4b0d17251Schristos * Licensed under the Apache License 2.0 (the "License"). You may not use
5c7da899bSchristos * this file except in compliance with the License. You can obtain a copy
6c7da899bSchristos * in the file LICENSE in the source distribution or at
7c7da899bSchristos * https://www.openssl.org/source/license.html
8a89c9211Schristos */
9a89c9211Schristos
10c7da899bSchristos #include <openssl/opensslconf.h>
11b0d17251Schristos
12a89c9211Schristos #include <stdio.h>
13a89c9211Schristos #include <stdlib.h>
14a89c9211Schristos #include <time.h>
15a89c9211Schristos #include <string.h>
16a89c9211Schristos #include "apps.h"
1713d40330Schristos #include "progs.h"
18a89c9211Schristos #include <openssl/bio.h>
19a89c9211Schristos #include <openssl/err.h>
20a89c9211Schristos #include <openssl/bn.h>
21b0d17251Schristos #include <openssl/dsa.h>
22a89c9211Schristos #include <openssl/dh.h>
23a89c9211Schristos #include <openssl/x509.h>
24a89c9211Schristos #include <openssl/pem.h>
25b0d17251Schristos #include <openssl/core_names.h>
26b0d17251Schristos #include <openssl/core_dispatch.h>
27b0d17251Schristos #include <openssl/param_build.h>
28b0d17251Schristos #include <openssl/encoder.h>
29b0d17251Schristos #include <openssl/decoder.h>
30a89c9211Schristos
319cae6e10Schristos #define DEFBITS 2048
32a89c9211Schristos
33b0d17251Schristos static EVP_PKEY *dsa_to_dh(EVP_PKEY *dh);
34b0d17251Schristos static int gendh_cb(EVP_PKEY_CTX *ctx);
35a89c9211Schristos
36c7da899bSchristos typedef enum OPTION_choice {
37b0d17251Schristos OPT_COMMON,
38c7da899bSchristos OPT_INFORM, OPT_OUTFORM, OPT_IN, OPT_OUT,
39c7da899bSchristos OPT_ENGINE, OPT_CHECK, OPT_TEXT, OPT_NOOUT,
40b0d17251Schristos OPT_DSAPARAM, OPT_2, OPT_3, OPT_5,
41b0d17251Schristos OPT_R_ENUM, OPT_PROV_ENUM
42c7da899bSchristos } OPTION_CHOICE;
43a89c9211Schristos
4413d40330Schristos const OPTIONS dhparam_options[] = {
45b0d17251Schristos {OPT_HELP_STR, 1, '-', "Usage: %s [options] [numbits]\n"},
46b0d17251Schristos
47b0d17251Schristos OPT_SECTION("General"),
48c7da899bSchristos {"help", OPT_HELP, '-', "Display this summary"},
49c7da899bSchristos {"check", OPT_CHECK, '-', "Check the DH parameters"},
50b0d17251Schristos #if !defined(OPENSSL_NO_DSA) || !defined(OPENSSL_NO_DEPRECATED_3_0)
51c7da899bSchristos {"dsaparam", OPT_DSAPARAM, '-',
52c7da899bSchristos "Read or generate DSA parameters, convert to DH"},
53c7da899bSchristos #endif
54c7da899bSchristos #ifndef OPENSSL_NO_ENGINE
55c7da899bSchristos {"engine", OPT_ENGINE, 's', "Use engine e, possibly a hardware device"},
56c7da899bSchristos #endif
57b0d17251Schristos
58b0d17251Schristos OPT_SECTION("Input"),
59b0d17251Schristos {"in", OPT_IN, '<', "Input file"},
60b0d17251Schristos {"inform", OPT_INFORM, 'F', "Input format, DER or PEM"},
61b0d17251Schristos
62b0d17251Schristos OPT_SECTION("Output"),
63b0d17251Schristos {"out", OPT_OUT, '>', "Output file"},
64b0d17251Schristos {"outform", OPT_OUTFORM, 'F', "Output format, DER or PEM"},
65b0d17251Schristos {"text", OPT_TEXT, '-', "Print a text form of the DH parameters"},
66b0d17251Schristos {"noout", OPT_NOOUT, '-', "Don't output any DH parameters"},
67b0d17251Schristos {"2", OPT_2, '-', "Generate parameters using 2 as the generator value"},
68b0d17251Schristos {"3", OPT_3, '-', "Generate parameters using 3 as the generator value"},
69b0d17251Schristos {"5", OPT_5, '-', "Generate parameters using 5 as the generator value"},
70b0d17251Schristos
71b0d17251Schristos OPT_R_OPTIONS,
72b0d17251Schristos OPT_PROV_OPTIONS,
73b0d17251Schristos
74b0d17251Schristos OPT_PARAMETERS(),
75b0d17251Schristos {"numbits", 0, 0, "Number of bits if generating parameters (optional)"},
76c7da899bSchristos {NULL}
77c7da899bSchristos };
78a89c9211Schristos
dhparam_main(int argc,char ** argv)79c7da899bSchristos int dhparam_main(int argc, char **argv)
80a89c9211Schristos {
81c7da899bSchristos BIO *in = NULL, *out = NULL;
82b0d17251Schristos EVP_PKEY *pkey = NULL, *tmppkey = NULL;
83b0d17251Schristos EVP_PKEY_CTX *ctx = NULL;
8413d40330Schristos char *infile = NULL, *outfile = NULL, *prog;
85c7da899bSchristos ENGINE *e = NULL;
86a89c9211Schristos int dsaparam = 0;
87b0d17251Schristos int text = 0, ret = 1, num = 0, g = 0;
88c7da899bSchristos int informat = FORMAT_PEM, outformat = FORMAT_PEM, check = 0, noout = 0;
89c7da899bSchristos OPTION_CHOICE o;
90a89c9211Schristos
91c7da899bSchristos prog = opt_init(argc, argv, dhparam_options);
92c7da899bSchristos while ((o = opt_next()) != OPT_EOF) {
93c7da899bSchristos switch (o) {
94c7da899bSchristos case OPT_EOF:
95c7da899bSchristos case OPT_ERR:
96c7da899bSchristos opthelp:
97c7da899bSchristos BIO_printf(bio_err, "%s: Use -help for summary.\n", prog);
98a89c9211Schristos goto end;
99c7da899bSchristos case OPT_HELP:
100c7da899bSchristos opt_help(dhparam_options);
101c7da899bSchristos ret = 0;
102c7da899bSchristos goto end;
103c7da899bSchristos case OPT_INFORM:
104c7da899bSchristos if (!opt_format(opt_arg(), OPT_FMT_PEMDER, &informat))
105c7da899bSchristos goto opthelp;
106c7da899bSchristos break;
107c7da899bSchristos case OPT_OUTFORM:
108c7da899bSchristos if (!opt_format(opt_arg(), OPT_FMT_PEMDER, &outformat))
109c7da899bSchristos goto opthelp;
110c7da899bSchristos break;
111c7da899bSchristos case OPT_IN:
112c7da899bSchristos infile = opt_arg();
113c7da899bSchristos break;
114c7da899bSchristos case OPT_OUT:
115c7da899bSchristos outfile = opt_arg();
116c7da899bSchristos break;
117c7da899bSchristos case OPT_ENGINE:
118c7da899bSchristos e = setup_engine(opt_arg(), 0);
119c7da899bSchristos break;
120c7da899bSchristos case OPT_CHECK:
121a89c9211Schristos check = 1;
122c7da899bSchristos break;
123c7da899bSchristos case OPT_TEXT:
124a89c9211Schristos text = 1;
125c7da899bSchristos break;
126c7da899bSchristos case OPT_DSAPARAM:
127a89c9211Schristos dsaparam = 1;
128c7da899bSchristos break;
129c7da899bSchristos case OPT_2:
130a89c9211Schristos g = 2;
131c7da899bSchristos break;
132b0d17251Schristos case OPT_3:
133b0d17251Schristos g = 3;
134b0d17251Schristos break;
135c7da899bSchristos case OPT_5:
136a89c9211Schristos g = 5;
137c7da899bSchristos break;
138c7da899bSchristos case OPT_NOOUT:
139c7da899bSchristos noout = 1;
140c7da899bSchristos break;
14113d40330Schristos case OPT_R_CASES:
14213d40330Schristos if (!opt_rand(o))
14313d40330Schristos goto end;
144c7da899bSchristos break;
145b0d17251Schristos case OPT_PROV_CASES:
146b0d17251Schristos if (!opt_provider(o))
147b0d17251Schristos goto end;
148b0d17251Schristos break;
149a89c9211Schristos }
150c7da899bSchristos }
151b0d17251Schristos
152b0d17251Schristos /* One optional argument, bitsize to generate. */
153c7da899bSchristos argc = opt_num_rest();
154c7da899bSchristos argv = opt_rest();
155b0d17251Schristos if (argc == 1) {
156b0d17251Schristos if (!opt_int(argv[0], &num) || num <= 0)
157b0d17251Schristos goto opthelp;
158b0d17251Schristos } else if (argc != 0) {
159b0d17251Schristos goto opthelp;
160b0d17251Schristos }
161b0d17251Schristos if (!app_RAND_load())
162a89c9211Schristos goto end;
163a89c9211Schristos
164a89c9211Schristos if (g && !num)
165a89c9211Schristos num = DEFBITS;
166a89c9211Schristos
167c7da899bSchristos if (dsaparam && g) {
168635165faSspz BIO_printf(bio_err,
169b0d17251Schristos "Error, generator may not be chosen for DSA parameters\n");
170a89c9211Schristos goto end;
171a89c9211Schristos }
172132cc1c4Schristos
173132cc1c4Schristos out = bio_open_default(outfile, 'w', outformat);
174132cc1c4Schristos if (out == NULL)
175132cc1c4Schristos goto end;
176132cc1c4Schristos
177a89c9211Schristos /* DH parameters */
178a89c9211Schristos if (num && !g)
179a89c9211Schristos g = 2;
180a89c9211Schristos
181a89c9211Schristos if (num) {
182b0d17251Schristos const char *alg = dsaparam ? "DSA" : "DH";
183a89c9211Schristos
184b0d17251Schristos if (infile != NULL) {
185b0d17251Schristos BIO_printf(bio_err, "Warning, input file %s ignored\n", infile);
186b0d17251Schristos }
187b0d17251Schristos
188b0d17251Schristos ctx = EVP_PKEY_CTX_new_from_name(app_get0_libctx(), alg, app_get0_propq());
189b0d17251Schristos if (ctx == NULL) {
190b0d17251Schristos BIO_printf(bio_err,
191b0d17251Schristos "Error, %s param generation context allocation failed\n",
192b0d17251Schristos alg);
193b0d17251Schristos goto end;
194b0d17251Schristos }
195b0d17251Schristos EVP_PKEY_CTX_set_cb(ctx, gendh_cb);
196b0d17251Schristos EVP_PKEY_CTX_set_app_data(ctx, bio_err);
197b0d17251Schristos BIO_printf(bio_err,
198b0d17251Schristos "Generating %s parameters, %d bit long %sprime\n",
199b0d17251Schristos alg, num, dsaparam ? "" : "safe ");
200b0d17251Schristos
201b0d17251Schristos if (EVP_PKEY_paramgen_init(ctx) <= 0) {
202b0d17251Schristos BIO_printf(bio_err,
203b0d17251Schristos "Error, unable to initialise %s parameters\n",
204b0d17251Schristos alg);
205c7da899bSchristos goto end;
206c7da899bSchristos }
207c7da899bSchristos
208635165faSspz if (dsaparam) {
209b0d17251Schristos if (EVP_PKEY_CTX_set_dsa_paramgen_bits(ctx, num) <= 0) {
210b0d17251Schristos BIO_printf(bio_err, "Error, unable to set DSA prime length\n");
211a89c9211Schristos goto end;
212a89c9211Schristos }
213a89c9211Schristos } else {
214b0d17251Schristos if (EVP_PKEY_CTX_set_dh_paramgen_prime_len(ctx, num) <= 0) {
215b0d17251Schristos BIO_printf(bio_err, "Error, unable to set DH prime length\n");
216b0d17251Schristos goto end;
217b0d17251Schristos }
218b0d17251Schristos if (EVP_PKEY_CTX_set_dh_paramgen_generator(ctx, g) <= 0) {
219b0d17251Schristos BIO_printf(bio_err, "Error, unable to set generator\n");
220b0d17251Schristos goto end;
221b0d17251Schristos }
222b0d17251Schristos }
223b0d17251Schristos
224b0d17251Schristos tmppkey = app_paramgen(ctx, alg);
225*4778aedeSchristos if (tmppkey == NULL)
226*4778aedeSchristos goto end;
227b0d17251Schristos EVP_PKEY_CTX_free(ctx);
228b0d17251Schristos ctx = NULL;
229b0d17251Schristos if (dsaparam) {
230b0d17251Schristos pkey = dsa_to_dh(tmppkey);
231b0d17251Schristos if (pkey == NULL)
232b0d17251Schristos goto end;
233b0d17251Schristos EVP_PKEY_free(tmppkey);
234b0d17251Schristos } else {
235b0d17251Schristos pkey = tmppkey;
236b0d17251Schristos }
237b0d17251Schristos tmppkey = NULL;
238b0d17251Schristos } else {
239b0d17251Schristos OSSL_DECODER_CTX *decoderctx = NULL;
240b0d17251Schristos const char *keytype = "DH";
241b0d17251Schristos int done;
242a89c9211Schristos
243c7da899bSchristos in = bio_open_default(infile, 'r', informat);
244c7da899bSchristos if (in == NULL)
245a89c9211Schristos goto end;
246a89c9211Schristos
247b0d17251Schristos do {
248c7da899bSchristos /*
249b0d17251Schristos * We assume we're done unless we explicitly want to retry and set
250b0d17251Schristos * this to 0 below.
251c7da899bSchristos */
252b0d17251Schristos done = 1;
253b0d17251Schristos /*
254b0d17251Schristos * We set NULL for the keytype to allow any key type. We don't know
255b0d17251Schristos * if we're going to get DH or DHX (or DSA in the event of dsaparam).
256b0d17251Schristos * We check that we got one of those key types afterwards.
257b0d17251Schristos */
258b0d17251Schristos decoderctx
259b0d17251Schristos = OSSL_DECODER_CTX_new_for_pkey(&tmppkey,
260b0d17251Schristos (informat == FORMAT_ASN1)
261b0d17251Schristos ? "DER" : "PEM",
262b0d17251Schristos NULL,
263b0d17251Schristos (informat == FORMAT_ASN1)
264b0d17251Schristos ? keytype : NULL,
265b0d17251Schristos OSSL_KEYMGMT_SELECT_DOMAIN_PARAMETERS,
266b0d17251Schristos NULL, NULL);
267a89c9211Schristos
268b0d17251Schristos if (decoderctx != NULL
269b0d17251Schristos && !OSSL_DECODER_from_bio(decoderctx, in)
270b0d17251Schristos && informat == FORMAT_ASN1
271b0d17251Schristos && strcmp(keytype, "DH") == 0) {
272b0d17251Schristos /*
273b0d17251Schristos * When reading DER we explicitly state the expected keytype
274b0d17251Schristos * because, unlike PEM, there is no header to declare what
275b0d17251Schristos * the contents of the DER file are. The decoders just try
276b0d17251Schristos * and guess. Unfortunately with DHX key types they may guess
277b0d17251Schristos * wrong and think we have a DSA keytype. Therefore we try
278b0d17251Schristos * both DH and DHX sequentially.
279b0d17251Schristos */
280b0d17251Schristos keytype = "DHX";
281b0d17251Schristos /*
282b0d17251Schristos * BIO_reset() returns 0 for success for file BIOs only!!!
283b0d17251Schristos * This won't work for stdin (and never has done)
284b0d17251Schristos */
285b0d17251Schristos if (BIO_reset(in) == 0)
286b0d17251Schristos done = 0;
287b0d17251Schristos }
288b0d17251Schristos OSSL_DECODER_CTX_free(decoderctx);
289b0d17251Schristos } while (!done);
290b0d17251Schristos if (tmppkey == NULL) {
291b0d17251Schristos BIO_printf(bio_err, "Error, unable to load parameters\n");
292a89c9211Schristos goto end;
293a89c9211Schristos }
294b0d17251Schristos
295b0d17251Schristos if (dsaparam) {
296b0d17251Schristos if (!EVP_PKEY_is_a(tmppkey, "DSA")) {
297b0d17251Schristos BIO_printf(bio_err, "Error, unable to load DSA parameters\n");
298b0d17251Schristos goto end;
299b0d17251Schristos }
300b0d17251Schristos pkey = dsa_to_dh(tmppkey);
301b0d17251Schristos if (pkey == NULL)
302b0d17251Schristos goto end;
303b0d17251Schristos } else {
304b0d17251Schristos if (!EVP_PKEY_is_a(tmppkey, "DH")
305b0d17251Schristos && !EVP_PKEY_is_a(tmppkey, "DHX")) {
306b0d17251Schristos BIO_printf(bio_err, "Error, unable to load DH parameters\n");
307b0d17251Schristos goto end;
308b0d17251Schristos }
309b0d17251Schristos pkey = tmppkey;
310b0d17251Schristos tmppkey = NULL;
311b0d17251Schristos }
312a89c9211Schristos }
313a89c9211Schristos
314b0d17251Schristos if (text)
315b0d17251Schristos EVP_PKEY_print_params(out, pkey, 4, NULL);
316a89c9211Schristos
317635165faSspz if (check) {
318b0d17251Schristos ctx = EVP_PKEY_CTX_new_from_pkey(app_get0_libctx(), pkey, app_get0_propq());
319b0d17251Schristos if (ctx == NULL) {
320b0d17251Schristos BIO_printf(bio_err, "Error, failed to check DH parameters\n");
321a89c9211Schristos goto end;
322a89c9211Schristos }
323b0d17251Schristos if (EVP_PKEY_param_check(ctx) <= 0) {
324b0d17251Schristos BIO_printf(bio_err, "Error, invalid parameters generated\n");
325b0d17251Schristos goto end;
326b0d17251Schristos }
327c7da899bSchristos BIO_printf(bio_err, "DH parameters appear to be ok.\n");
328a89c9211Schristos }
329a89c9211Schristos
330635165faSspz if (!noout) {
331b0d17251Schristos OSSL_ENCODER_CTX *ectx =
332b0d17251Schristos OSSL_ENCODER_CTX_new_for_pkey(pkey,
333b0d17251Schristos OSSL_KEYMGMT_SELECT_DOMAIN_PARAMETERS,
334b0d17251Schristos outformat == FORMAT_ASN1
335b0d17251Schristos ? "DER" : "PEM",
336b0d17251Schristos NULL, NULL);
337b0d17251Schristos
338b0d17251Schristos if (ectx == NULL || !OSSL_ENCODER_to_bio(ectx, out)) {
339b0d17251Schristos OSSL_ENCODER_CTX_free(ectx);
340b0d17251Schristos BIO_printf(bio_err, "Error, unable to write DH parameters\n");
341a89c9211Schristos goto end;
342a89c9211Schristos }
343b0d17251Schristos OSSL_ENCODER_CTX_free(ectx);
344a89c9211Schristos }
345a89c9211Schristos ret = 0;
346a89c9211Schristos end:
347b0d17251Schristos if (ret != 0)
348b0d17251Schristos ERR_print_errors(bio_err);
349635165faSspz BIO_free(in);
350635165faSspz BIO_free_all(out);
351b0d17251Schristos EVP_PKEY_free(pkey);
352b0d17251Schristos EVP_PKEY_free(tmppkey);
353b0d17251Schristos EVP_PKEY_CTX_free(ctx);
35434505c60Sspz release_engine(e);
35513d40330Schristos return ret;
356a89c9211Schristos }
357a89c9211Schristos
358b0d17251Schristos /*
359b0d17251Schristos * Historically we had the low level call DSA_dup_DH() to do this.
360b0d17251Schristos * That is now deprecated with no replacement. Since we still need to do this
361b0d17251Schristos * for backwards compatibility reasons, we do it "manually".
362b0d17251Schristos */
dsa_to_dh(EVP_PKEY * dh)363b0d17251Schristos static EVP_PKEY *dsa_to_dh(EVP_PKEY *dh)
364a89c9211Schristos {
365b0d17251Schristos OSSL_PARAM_BLD *tmpl = NULL;
366b0d17251Schristos OSSL_PARAM *params = NULL;
367b0d17251Schristos BIGNUM *bn_p = NULL, *bn_q = NULL, *bn_g = NULL;
368b0d17251Schristos EVP_PKEY_CTX *ctx = NULL;
369b0d17251Schristos EVP_PKEY *pkey = NULL;
370b0d17251Schristos
371b0d17251Schristos if (!EVP_PKEY_get_bn_param(dh, OSSL_PKEY_PARAM_FFC_P, &bn_p)
372b0d17251Schristos || !EVP_PKEY_get_bn_param(dh, OSSL_PKEY_PARAM_FFC_Q, &bn_q)
373b0d17251Schristos || !EVP_PKEY_get_bn_param(dh, OSSL_PKEY_PARAM_FFC_G, &bn_g)) {
374b0d17251Schristos BIO_printf(bio_err, "Error, failed to set DH parameters\n");
375b0d17251Schristos goto err;
376b0d17251Schristos }
377b0d17251Schristos
378b0d17251Schristos if ((tmpl = OSSL_PARAM_BLD_new()) == NULL
379b0d17251Schristos || !OSSL_PARAM_BLD_push_BN(tmpl, OSSL_PKEY_PARAM_FFC_P,
380b0d17251Schristos bn_p)
381b0d17251Schristos || !OSSL_PARAM_BLD_push_BN(tmpl, OSSL_PKEY_PARAM_FFC_Q,
382b0d17251Schristos bn_q)
383b0d17251Schristos || !OSSL_PARAM_BLD_push_BN(tmpl, OSSL_PKEY_PARAM_FFC_G,
384b0d17251Schristos bn_g)
385b0d17251Schristos || (params = OSSL_PARAM_BLD_to_param(tmpl)) == NULL) {
386b0d17251Schristos BIO_printf(bio_err, "Error, failed to set DH parameters\n");
387b0d17251Schristos goto err;
388b0d17251Schristos }
389b0d17251Schristos
390b0d17251Schristos ctx = EVP_PKEY_CTX_new_from_name(app_get0_libctx(), "DHX", app_get0_propq());
391b0d17251Schristos if (ctx == NULL
392b0d17251Schristos || EVP_PKEY_fromdata_init(ctx) <= 0
393b0d17251Schristos || EVP_PKEY_fromdata(ctx, &pkey, EVP_PKEY_KEY_PARAMETERS, params) <= 0) {
394b0d17251Schristos BIO_printf(bio_err, "Error, failed to set DH parameters\n");
395b0d17251Schristos goto err;
396b0d17251Schristos }
397b0d17251Schristos
398b0d17251Schristos err:
399b0d17251Schristos EVP_PKEY_CTX_free(ctx);
400b0d17251Schristos OSSL_PARAM_free(params);
401b0d17251Schristos OSSL_PARAM_BLD_free(tmpl);
402b0d17251Schristos BN_free(bn_p);
403b0d17251Schristos BN_free(bn_q);
404b0d17251Schristos BN_free(bn_g);
405b0d17251Schristos return pkey;
406b0d17251Schristos }
407b0d17251Schristos
gendh_cb(EVP_PKEY_CTX * ctx)408b0d17251Schristos static int gendh_cb(EVP_PKEY_CTX *ctx)
409b0d17251Schristos {
410b0d17251Schristos int p = EVP_PKEY_CTX_get_keygen_info(ctx, 0);
411b0d17251Schristos BIO *b = EVP_PKEY_CTX_get_app_data(ctx);
41213d40330Schristos static const char symbols[] = ".+*\n";
41313d40330Schristos char c = (p >= 0 && (size_t)p < sizeof(symbols) - 1) ? symbols[p] : '?';
414a89c9211Schristos
415b0d17251Schristos BIO_write(b, &c, 1);
416b0d17251Schristos (void)BIO_flush(b);
417a89c9211Schristos return 1;
418a89c9211Schristos }
419