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