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 "apps.h"
15e1051a39Sopenharmony_ci#include <time.h>
16e1051a39Sopenharmony_ci#include <string.h>
17e1051a39Sopenharmony_ci#include "apps.h"
18e1051a39Sopenharmony_ci#include "progs.h"
19e1051a39Sopenharmony_ci#include <openssl/bio.h>
20e1051a39Sopenharmony_ci#include <openssl/err.h>
21e1051a39Sopenharmony_ci#include <openssl/bn.h>
22e1051a39Sopenharmony_ci#include <openssl/dsa.h>
23e1051a39Sopenharmony_ci#include <openssl/x509.h>
24e1051a39Sopenharmony_ci#include <openssl/pem.h>
25e1051a39Sopenharmony_ci
26e1051a39Sopenharmony_cistatic int verbose = 0;
27e1051a39Sopenharmony_ci
28e1051a39Sopenharmony_cistatic int gendsa_cb(EVP_PKEY_CTX *ctx);
29e1051a39Sopenharmony_ci
30e1051a39Sopenharmony_citypedef enum OPTION_choice {
31e1051a39Sopenharmony_ci    OPT_COMMON,
32e1051a39Sopenharmony_ci    OPT_INFORM, OPT_OUTFORM, OPT_IN, OPT_OUT, OPT_TEXT,
33e1051a39Sopenharmony_ci    OPT_NOOUT, OPT_GENKEY, OPT_ENGINE, OPT_VERBOSE,
34e1051a39Sopenharmony_ci    OPT_R_ENUM, OPT_PROV_ENUM
35e1051a39Sopenharmony_ci} OPTION_CHOICE;
36e1051a39Sopenharmony_ci
37e1051a39Sopenharmony_ciconst OPTIONS dsaparam_options[] = {
38e1051a39Sopenharmony_ci    {OPT_HELP_STR, 1, '-', "Usage: %s [options] [numbits]\n"},
39e1051a39Sopenharmony_ci
40e1051a39Sopenharmony_ci    OPT_SECTION("General"),
41e1051a39Sopenharmony_ci    {"help", OPT_HELP, '-', "Display this summary"},
42e1051a39Sopenharmony_ci#ifndef OPENSSL_NO_ENGINE
43e1051a39Sopenharmony_ci    {"engine", OPT_ENGINE, 's', "Use engine e, possibly a hardware device"},
44e1051a39Sopenharmony_ci#endif
45e1051a39Sopenharmony_ci
46e1051a39Sopenharmony_ci    OPT_SECTION("Input"),
47e1051a39Sopenharmony_ci    {"in", OPT_IN, '<', "Input file"},
48e1051a39Sopenharmony_ci    {"inform", OPT_INFORM, 'F', "Input format - DER or PEM"},
49e1051a39Sopenharmony_ci
50e1051a39Sopenharmony_ci    OPT_SECTION("Output"),
51e1051a39Sopenharmony_ci    {"out", OPT_OUT, '>', "Output file"},
52e1051a39Sopenharmony_ci    {"outform", OPT_OUTFORM, 'F', "Output format - DER or PEM"},
53e1051a39Sopenharmony_ci    {"text", OPT_TEXT, '-', "Print as text"},
54e1051a39Sopenharmony_ci    {"noout", OPT_NOOUT, '-', "No output"},
55e1051a39Sopenharmony_ci    {"verbose", OPT_VERBOSE, '-', "Verbose output"},
56e1051a39Sopenharmony_ci    {"genkey", OPT_GENKEY, '-', "Generate a DSA key"},
57e1051a39Sopenharmony_ci
58e1051a39Sopenharmony_ci    OPT_R_OPTIONS,
59e1051a39Sopenharmony_ci    OPT_PROV_OPTIONS,
60e1051a39Sopenharmony_ci
61e1051a39Sopenharmony_ci    OPT_PARAMETERS(),
62e1051a39Sopenharmony_ci    {"numbits", 0, 0, "Number of bits if generating parameters (optional)"},
63e1051a39Sopenharmony_ci    {NULL}
64e1051a39Sopenharmony_ci};
65e1051a39Sopenharmony_ci
66e1051a39Sopenharmony_ciint dsaparam_main(int argc, char **argv)
67e1051a39Sopenharmony_ci{
68e1051a39Sopenharmony_ci    ENGINE *e = NULL;
69e1051a39Sopenharmony_ci    BIO *out = NULL;
70e1051a39Sopenharmony_ci    EVP_PKEY *params = NULL, *pkey = NULL;
71e1051a39Sopenharmony_ci    EVP_PKEY_CTX *ctx = NULL;
72e1051a39Sopenharmony_ci    int numbits = -1, num = 0, genkey = 0;
73e1051a39Sopenharmony_ci    int informat = FORMAT_UNDEF, outformat = FORMAT_PEM, noout = 0;
74e1051a39Sopenharmony_ci    int ret = 1, i, text = 0, private = 0;
75e1051a39Sopenharmony_ci    char *infile = NULL, *outfile = NULL, *prog;
76e1051a39Sopenharmony_ci    OPTION_CHOICE o;
77e1051a39Sopenharmony_ci
78e1051a39Sopenharmony_ci    prog = opt_init(argc, argv, dsaparam_options);
79e1051a39Sopenharmony_ci    while ((o = opt_next()) != OPT_EOF) {
80e1051a39Sopenharmony_ci        switch (o) {
81e1051a39Sopenharmony_ci        case OPT_EOF:
82e1051a39Sopenharmony_ci        case OPT_ERR:
83e1051a39Sopenharmony_ci opthelp:
84e1051a39Sopenharmony_ci            BIO_printf(bio_err, "%s: Use -help for summary.\n", prog);
85e1051a39Sopenharmony_ci            goto end;
86e1051a39Sopenharmony_ci        case OPT_HELP:
87e1051a39Sopenharmony_ci            opt_help(dsaparam_options);
88e1051a39Sopenharmony_ci            ret = 0;
89e1051a39Sopenharmony_ci            goto end;
90e1051a39Sopenharmony_ci        case OPT_INFORM:
91e1051a39Sopenharmony_ci            if (!opt_format(opt_arg(), OPT_FMT_PEMDER, &informat))
92e1051a39Sopenharmony_ci                goto opthelp;
93e1051a39Sopenharmony_ci            break;
94e1051a39Sopenharmony_ci        case OPT_IN:
95e1051a39Sopenharmony_ci            infile = opt_arg();
96e1051a39Sopenharmony_ci            break;
97e1051a39Sopenharmony_ci        case OPT_OUTFORM:
98e1051a39Sopenharmony_ci            if (!opt_format(opt_arg(), OPT_FMT_PEMDER, &outformat))
99e1051a39Sopenharmony_ci                goto opthelp;
100e1051a39Sopenharmony_ci            break;
101e1051a39Sopenharmony_ci        case OPT_OUT:
102e1051a39Sopenharmony_ci            outfile = opt_arg();
103e1051a39Sopenharmony_ci            break;
104e1051a39Sopenharmony_ci        case OPT_ENGINE:
105e1051a39Sopenharmony_ci            e = setup_engine(opt_arg(), 0);
106e1051a39Sopenharmony_ci            break;
107e1051a39Sopenharmony_ci        case OPT_TEXT:
108e1051a39Sopenharmony_ci            text = 1;
109e1051a39Sopenharmony_ci            break;
110e1051a39Sopenharmony_ci        case OPT_GENKEY:
111e1051a39Sopenharmony_ci            genkey = 1;
112e1051a39Sopenharmony_ci            break;
113e1051a39Sopenharmony_ci        case OPT_R_CASES:
114e1051a39Sopenharmony_ci            if (!opt_rand(o))
115e1051a39Sopenharmony_ci                goto end;
116e1051a39Sopenharmony_ci            break;
117e1051a39Sopenharmony_ci        case OPT_PROV_CASES:
118e1051a39Sopenharmony_ci            if (!opt_provider(o))
119e1051a39Sopenharmony_ci                goto end;
120e1051a39Sopenharmony_ci            break;
121e1051a39Sopenharmony_ci        case OPT_NOOUT:
122e1051a39Sopenharmony_ci            noout = 1;
123e1051a39Sopenharmony_ci            break;
124e1051a39Sopenharmony_ci        case OPT_VERBOSE:
125e1051a39Sopenharmony_ci            verbose = 1;
126e1051a39Sopenharmony_ci            break;
127e1051a39Sopenharmony_ci        }
128e1051a39Sopenharmony_ci    }
129e1051a39Sopenharmony_ci
130e1051a39Sopenharmony_ci    /* Optional arg is bitsize. */
131e1051a39Sopenharmony_ci    argc = opt_num_rest();
132e1051a39Sopenharmony_ci    argv = opt_rest();
133e1051a39Sopenharmony_ci    if (argc == 1) {
134e1051a39Sopenharmony_ci        if (!opt_int(argv[0], &num) || num < 0)
135e1051a39Sopenharmony_ci            goto opthelp;
136e1051a39Sopenharmony_ci    } else if (argc != 0) {
137e1051a39Sopenharmony_ci        goto opthelp;
138e1051a39Sopenharmony_ci    }
139e1051a39Sopenharmony_ci    if (!app_RAND_load())
140e1051a39Sopenharmony_ci        goto end;
141e1051a39Sopenharmony_ci
142e1051a39Sopenharmony_ci    /* generate a key */
143e1051a39Sopenharmony_ci    numbits = num;
144e1051a39Sopenharmony_ci    private = genkey ? 1 : 0;
145e1051a39Sopenharmony_ci
146e1051a39Sopenharmony_ci    out = bio_open_owner(outfile, outformat, private);
147e1051a39Sopenharmony_ci    if (out == NULL)
148e1051a39Sopenharmony_ci        goto end;
149e1051a39Sopenharmony_ci
150e1051a39Sopenharmony_ci    ctx = EVP_PKEY_CTX_new_from_name(app_get0_libctx(), "DSA", app_get0_propq());
151e1051a39Sopenharmony_ci    if (ctx == NULL) {
152e1051a39Sopenharmony_ci        BIO_printf(bio_err,
153e1051a39Sopenharmony_ci                   "Error, DSA parameter generation context allocation failed\n");
154e1051a39Sopenharmony_ci        goto end;
155e1051a39Sopenharmony_ci    }
156e1051a39Sopenharmony_ci    if (numbits > 0) {
157e1051a39Sopenharmony_ci        if (numbits > OPENSSL_DSA_MAX_MODULUS_BITS)
158e1051a39Sopenharmony_ci            BIO_printf(bio_err,
159e1051a39Sopenharmony_ci                       "Warning: It is not recommended to use more than %d bit for DSA keys.\n"
160e1051a39Sopenharmony_ci                       "         Your key size is %d! Larger key size may behave not as expected.\n",
161e1051a39Sopenharmony_ci                       OPENSSL_DSA_MAX_MODULUS_BITS, numbits);
162e1051a39Sopenharmony_ci
163e1051a39Sopenharmony_ci        EVP_PKEY_CTX_set_cb(ctx, gendsa_cb);
164e1051a39Sopenharmony_ci        EVP_PKEY_CTX_set_app_data(ctx, bio_err);
165e1051a39Sopenharmony_ci        if (verbose) {
166e1051a39Sopenharmony_ci            BIO_printf(bio_err, "Generating DSA parameters, %d bit long prime\n",
167e1051a39Sopenharmony_ci                       num);
168e1051a39Sopenharmony_ci            BIO_printf(bio_err, "This could take some time\n");
169e1051a39Sopenharmony_ci        }
170e1051a39Sopenharmony_ci        if (EVP_PKEY_paramgen_init(ctx) <= 0) {
171e1051a39Sopenharmony_ci            BIO_printf(bio_err,
172e1051a39Sopenharmony_ci                       "Error, DSA key generation paramgen init failed\n");
173e1051a39Sopenharmony_ci            goto end;
174e1051a39Sopenharmony_ci        }
175e1051a39Sopenharmony_ci        if (EVP_PKEY_CTX_set_dsa_paramgen_bits(ctx, num) <= 0) {
176e1051a39Sopenharmony_ci            BIO_printf(bio_err,
177e1051a39Sopenharmony_ci                       "Error, DSA key generation setting bit length failed\n");
178e1051a39Sopenharmony_ci            goto end;
179e1051a39Sopenharmony_ci        }
180e1051a39Sopenharmony_ci        params = app_paramgen(ctx, "DSA");
181e1051a39Sopenharmony_ci    } else {
182e1051a39Sopenharmony_ci        params = load_keyparams(infile, informat, 1, "DSA", "DSA parameters");
183e1051a39Sopenharmony_ci    }
184e1051a39Sopenharmony_ci    if (params == NULL) {
185e1051a39Sopenharmony_ci        /* Error message should already have been displayed */
186e1051a39Sopenharmony_ci        goto end;
187e1051a39Sopenharmony_ci    }
188e1051a39Sopenharmony_ci
189e1051a39Sopenharmony_ci    if (text) {
190e1051a39Sopenharmony_ci        EVP_PKEY_print_params(out, params, 0, NULL);
191e1051a39Sopenharmony_ci    }
192e1051a39Sopenharmony_ci
193e1051a39Sopenharmony_ci    if (outformat == FORMAT_ASN1 && genkey)
194e1051a39Sopenharmony_ci        noout = 1;
195e1051a39Sopenharmony_ci
196e1051a39Sopenharmony_ci    if (!noout) {
197e1051a39Sopenharmony_ci        if (outformat == FORMAT_ASN1)
198e1051a39Sopenharmony_ci            i = i2d_KeyParams_bio(out, params);
199e1051a39Sopenharmony_ci        else
200e1051a39Sopenharmony_ci            i = PEM_write_bio_Parameters(out, params);
201e1051a39Sopenharmony_ci        if (!i) {
202e1051a39Sopenharmony_ci            BIO_printf(bio_err, "Error, unable to write DSA parameters\n");
203e1051a39Sopenharmony_ci            goto end;
204e1051a39Sopenharmony_ci        }
205e1051a39Sopenharmony_ci    }
206e1051a39Sopenharmony_ci    if (genkey) {
207e1051a39Sopenharmony_ci        EVP_PKEY_CTX_free(ctx);
208e1051a39Sopenharmony_ci        ctx = EVP_PKEY_CTX_new_from_pkey(app_get0_libctx(), params,
209e1051a39Sopenharmony_ci                app_get0_propq());
210e1051a39Sopenharmony_ci        if (ctx == NULL) {
211e1051a39Sopenharmony_ci            BIO_printf(bio_err,
212e1051a39Sopenharmony_ci                       "Error, DSA key generation context allocation failed\n");
213e1051a39Sopenharmony_ci            goto end;
214e1051a39Sopenharmony_ci        }
215e1051a39Sopenharmony_ci        if (EVP_PKEY_keygen_init(ctx) <= 0) {
216e1051a39Sopenharmony_ci            BIO_printf(bio_err,
217e1051a39Sopenharmony_ci                       "Error, unable to initialise for key generation\n");
218e1051a39Sopenharmony_ci            goto end;
219e1051a39Sopenharmony_ci        }
220e1051a39Sopenharmony_ci        pkey = app_keygen(ctx, "DSA", numbits, verbose);
221e1051a39Sopenharmony_ci        if (pkey == NULL)
222e1051a39Sopenharmony_ci            goto end;
223e1051a39Sopenharmony_ci        assert(private);
224e1051a39Sopenharmony_ci        if (outformat == FORMAT_ASN1)
225e1051a39Sopenharmony_ci            i = i2d_PrivateKey_bio(out, pkey);
226e1051a39Sopenharmony_ci        else
227e1051a39Sopenharmony_ci            i = PEM_write_bio_PrivateKey(out, pkey, NULL, NULL, 0, NULL, NULL);
228e1051a39Sopenharmony_ci    }
229e1051a39Sopenharmony_ci    ret = 0;
230e1051a39Sopenharmony_ci end:
231e1051a39Sopenharmony_ci    if (ret != 0)
232e1051a39Sopenharmony_ci        ERR_print_errors(bio_err);
233e1051a39Sopenharmony_ci    BIO_free_all(out);
234e1051a39Sopenharmony_ci    EVP_PKEY_CTX_free(ctx);
235e1051a39Sopenharmony_ci    EVP_PKEY_free(pkey);
236e1051a39Sopenharmony_ci    EVP_PKEY_free(params);
237e1051a39Sopenharmony_ci    release_engine(e);
238e1051a39Sopenharmony_ci    return ret;
239e1051a39Sopenharmony_ci}
240e1051a39Sopenharmony_ci
241e1051a39Sopenharmony_cistatic int gendsa_cb(EVP_PKEY_CTX *ctx)
242e1051a39Sopenharmony_ci{
243e1051a39Sopenharmony_ci    static const char symbols[] = ".+*\n";
244e1051a39Sopenharmony_ci    int p;
245e1051a39Sopenharmony_ci    char c;
246e1051a39Sopenharmony_ci    BIO *b;
247e1051a39Sopenharmony_ci
248e1051a39Sopenharmony_ci    if (!verbose)
249e1051a39Sopenharmony_ci        return 1;
250e1051a39Sopenharmony_ci
251e1051a39Sopenharmony_ci    b = EVP_PKEY_CTX_get_app_data(ctx);
252e1051a39Sopenharmony_ci    p = EVP_PKEY_CTX_get_keygen_info(ctx, 0);
253e1051a39Sopenharmony_ci    c = (p >= 0 && (size_t)p < sizeof(symbols) - 1) ? symbols[p] : '?';
254e1051a39Sopenharmony_ci
255e1051a39Sopenharmony_ci    BIO_write(b, &c, 1);
256e1051a39Sopenharmony_ci    (void)BIO_flush(b);
257e1051a39Sopenharmony_ci    return 1;
258e1051a39Sopenharmony_ci}
259