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