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