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