1635165faSspz /*
2*4778aedeSchristos * Copyright 1995-2023 The OpenSSL Project Authors. All Rights Reserved.
3c7da899bSchristos *
4b0d17251Schristos * Licensed under the Apache License 2.0 (the "License"). You may not use
5c7da899bSchristos * this file except in compliance with the License. You can obtain a copy
6c7da899bSchristos * in the file LICENSE in the source distribution or at
7c7da899bSchristos * https://www.openssl.org/source/license.html
8635165faSspz */
9a89c9211Schristos
10c7da899bSchristos #include <openssl/opensslconf.h>
11b0d17251Schristos
12a89c9211Schristos #include <stdio.h>
13a89c9211Schristos #include <stdlib.h>
14b0d17251Schristos #include "apps.h"
15a89c9211Schristos #include <time.h>
16a89c9211Schristos #include <string.h>
17a89c9211Schristos #include "apps.h"
1813d40330Schristos #include "progs.h"
19a89c9211Schristos #include <openssl/bio.h>
20a89c9211Schristos #include <openssl/err.h>
21a89c9211Schristos #include <openssl/bn.h>
22a89c9211Schristos #include <openssl/dsa.h>
23a89c9211Schristos #include <openssl/x509.h>
24a89c9211Schristos #include <openssl/pem.h>
25a89c9211Schristos
26b0d17251Schristos static int verbose = 0;
27b0d17251Schristos
28b0d17251Schristos static int gendsa_cb(EVP_PKEY_CTX *ctx);
29a89c9211Schristos
30c7da899bSchristos typedef enum OPTION_choice {
31b0d17251Schristos OPT_COMMON,
32b0d17251Schristos OPT_INFORM, OPT_OUTFORM, OPT_IN, OPT_OUT, OPT_TEXT,
33b0d17251Schristos OPT_NOOUT, OPT_GENKEY, OPT_ENGINE, OPT_VERBOSE,
34b0d17251Schristos OPT_R_ENUM, OPT_PROV_ENUM
35c7da899bSchristos } OPTION_CHOICE;
36a89c9211Schristos
3713d40330Schristos const OPTIONS dsaparam_options[] = {
38b0d17251Schristos {OPT_HELP_STR, 1, '-', "Usage: %s [options] [numbits]\n"},
39b0d17251Schristos
40b0d17251Schristos OPT_SECTION("General"),
41c7da899bSchristos {"help", OPT_HELP, '-', "Display this summary"},
42c7da899bSchristos #ifndef OPENSSL_NO_ENGINE
43c7da899bSchristos {"engine", OPT_ENGINE, 's', "Use engine e, possibly a hardware device"},
44c7da899bSchristos #endif
45b0d17251Schristos
46b0d17251Schristos OPT_SECTION("Input"),
47b0d17251Schristos {"in", OPT_IN, '<', "Input file"},
48b0d17251Schristos {"inform", OPT_INFORM, 'F', "Input format - DER or PEM"},
49b0d17251Schristos
50b0d17251Schristos OPT_SECTION("Output"),
51b0d17251Schristos {"out", OPT_OUT, '>', "Output file"},
52b0d17251Schristos {"outform", OPT_OUTFORM, 'F', "Output format - DER or PEM"},
53b0d17251Schristos {"text", OPT_TEXT, '-', "Print as text"},
54b0d17251Schristos {"noout", OPT_NOOUT, '-', "No output"},
55b0d17251Schristos {"verbose", OPT_VERBOSE, '-', "Verbose output"},
56b0d17251Schristos {"genkey", OPT_GENKEY, '-', "Generate a DSA key"},
57b0d17251Schristos
58b0d17251Schristos OPT_R_OPTIONS,
59b0d17251Schristos OPT_PROV_OPTIONS,
60b0d17251Schristos
61b0d17251Schristos OPT_PARAMETERS(),
62b0d17251Schristos {"numbits", 0, 0, "Number of bits if generating parameters (optional)"},
63c7da899bSchristos {NULL}
64c7da899bSchristos };
65c7da899bSchristos
dsaparam_main(int argc,char ** argv)66c7da899bSchristos int dsaparam_main(int argc, char **argv)
67a89c9211Schristos {
6834505c60Sspz ENGINE *e = NULL;
69b0d17251Schristos BIO *out = NULL;
70b0d17251Schristos EVP_PKEY *params = NULL, *pkey = NULL;
71b0d17251Schristos EVP_PKEY_CTX *ctx = NULL;
7213d40330Schristos int numbits = -1, num = 0, genkey = 0;
73b0d17251Schristos int informat = FORMAT_UNDEF, outformat = FORMAT_PEM, noout = 0;
74c7da899bSchristos int ret = 1, i, text = 0, private = 0;
7513d40330Schristos char *infile = NULL, *outfile = NULL, *prog;
76c7da899bSchristos OPTION_CHOICE o;
77a89c9211Schristos
78c7da899bSchristos prog = opt_init(argc, argv, dsaparam_options);
79c7da899bSchristos while ((o = opt_next()) != OPT_EOF) {
80c7da899bSchristos switch (o) {
81c7da899bSchristos case OPT_EOF:
82c7da899bSchristos case OPT_ERR:
83c7da899bSchristos opthelp:
84c7da899bSchristos BIO_printf(bio_err, "%s: Use -help for summary.\n", prog);
85a89c9211Schristos goto end;
86c7da899bSchristos case OPT_HELP:
87c7da899bSchristos opt_help(dsaparam_options);
88c7da899bSchristos ret = 0;
89c7da899bSchristos goto end;
90c7da899bSchristos case OPT_INFORM:
91c7da899bSchristos if (!opt_format(opt_arg(), OPT_FMT_PEMDER, &informat))
92c7da899bSchristos goto opthelp;
93c7da899bSchristos break;
94c7da899bSchristos case OPT_IN:
95c7da899bSchristos infile = opt_arg();
96c7da899bSchristos break;
97c7da899bSchristos case OPT_OUTFORM:
98c7da899bSchristos if (!opt_format(opt_arg(), OPT_FMT_PEMDER, &outformat))
99c7da899bSchristos goto opthelp;
100c7da899bSchristos break;
101c7da899bSchristos case OPT_OUT:
102c7da899bSchristos outfile = opt_arg();
103c7da899bSchristos break;
104c7da899bSchristos case OPT_ENGINE:
105c7da899bSchristos e = setup_engine(opt_arg(), 0);
106c7da899bSchristos break;
107c7da899bSchristos case OPT_TEXT:
108a89c9211Schristos text = 1;
109c7da899bSchristos break;
110c7da899bSchristos case OPT_GENKEY:
11113d40330Schristos genkey = 1;
112c7da899bSchristos break;
11313d40330Schristos case OPT_R_CASES:
11413d40330Schristos if (!opt_rand(o))
11513d40330Schristos goto end;
116c7da899bSchristos break;
117b0d17251Schristos case OPT_PROV_CASES:
118b0d17251Schristos if (!opt_provider(o))
119b0d17251Schristos goto end;
120b0d17251Schristos break;
121c7da899bSchristos case OPT_NOOUT:
122a89c9211Schristos noout = 1;
123c7da899bSchristos break;
124b0d17251Schristos case OPT_VERBOSE:
125b0d17251Schristos verbose = 1;
126b0d17251Schristos break;
127c7da899bSchristos }
128c7da899bSchristos }
129b0d17251Schristos
130b0d17251Schristos /* Optional arg is bitsize. */
131c7da899bSchristos argc = opt_num_rest();
132c7da899bSchristos argv = opt_rest();
133c7da899bSchristos if (argc == 1) {
134c7da899bSchristos if (!opt_int(argv[0], &num) || num < 0)
135b0d17251Schristos goto opthelp;
136b0d17251Schristos } else if (argc != 0) {
137b0d17251Schristos goto opthelp;
138b0d17251Schristos }
139b0d17251Schristos if (!app_RAND_load())
140c7da899bSchristos goto end;
141b0d17251Schristos
142a89c9211Schristos /* generate a key */
143a89c9211Schristos numbits = num;
144c7da899bSchristos private = genkey ? 1 : 0;
145a89c9211Schristos
146c7da899bSchristos out = bio_open_owner(outfile, outformat, private);
147c7da899bSchristos if (out == NULL)
148a89c9211Schristos goto end;
149a89c9211Schristos
150b0d17251Schristos ctx = EVP_PKEY_CTX_new_from_name(app_get0_libctx(), "DSA", app_get0_propq());
151b0d17251Schristos if (ctx == NULL) {
152b0d17251Schristos BIO_printf(bio_err,
153b0d17251Schristos "Error, DSA parameter generation context allocation failed\n");
154b0d17251Schristos goto end;
155b0d17251Schristos }
156635165faSspz if (numbits > 0) {
15713d40330Schristos if (numbits > OPENSSL_DSA_MAX_MODULUS_BITS)
15813d40330Schristos BIO_printf(bio_err,
15913d40330Schristos "Warning: It is not recommended to use more than %d bit for DSA keys.\n"
16013d40330Schristos " Your key size is %d! Larger key size may behave not as expected.\n",
16113d40330Schristos OPENSSL_DSA_MAX_MODULUS_BITS, numbits);
16213d40330Schristos
163b0d17251Schristos EVP_PKEY_CTX_set_cb(ctx, gendsa_cb);
164b0d17251Schristos EVP_PKEY_CTX_set_app_data(ctx, bio_err);
165b0d17251Schristos if (verbose) {
166635165faSspz BIO_printf(bio_err, "Generating DSA parameters, %d bit long prime\n",
167635165faSspz num);
168a89c9211Schristos BIO_printf(bio_err, "This could take some time\n");
169b0d17251Schristos }
170b0d17251Schristos if (EVP_PKEY_paramgen_init(ctx) <= 0) {
171b0d17251Schristos BIO_printf(bio_err,
172b0d17251Schristos "Error, DSA key generation paramgen init failed\n");
173a89c9211Schristos goto end;
174a89c9211Schristos }
175b0d17251Schristos if (EVP_PKEY_CTX_set_dsa_paramgen_bits(ctx, num) <= 0) {
176b0d17251Schristos BIO_printf(bio_err,
177b0d17251Schristos "Error, DSA key generation setting bit length failed\n");
178b0d17251Schristos goto end;
17913d40330Schristos }
180b0d17251Schristos params = app_paramgen(ctx, "DSA");
181b0d17251Schristos } else {
182b0d17251Schristos params = load_keyparams(infile, informat, 1, "DSA", "DSA parameters");
183b0d17251Schristos }
184b0d17251Schristos if (params == NULL) {
185b0d17251Schristos /* Error message should already have been displayed */
186a89c9211Schristos goto end;
187a89c9211Schristos }
188a89c9211Schristos
189635165faSspz if (text) {
190b0d17251Schristos EVP_PKEY_print_params(out, params, 0, NULL);
191a89c9211Schristos }
192a89c9211Schristos
19353060421Schristos if (outformat == FORMAT_ASN1 && genkey)
19453060421Schristos noout = 1;
19553060421Schristos
196635165faSspz if (!noout) {
197a89c9211Schristos if (outformat == FORMAT_ASN1)
198b0d17251Schristos i = i2d_KeyParams_bio(out, params);
199c7da899bSchristos else
200b0d17251Schristos i = PEM_write_bio_Parameters(out, params);
201635165faSspz if (!i) {
202b0d17251Schristos BIO_printf(bio_err, "Error, unable to write DSA parameters\n");
203a89c9211Schristos goto end;
204a89c9211Schristos }
205a89c9211Schristos }
206635165faSspz if (genkey) {
207b0d17251Schristos EVP_PKEY_CTX_free(ctx);
208b0d17251Schristos ctx = EVP_PKEY_CTX_new_from_pkey(app_get0_libctx(), params,
209b0d17251Schristos app_get0_propq());
210b0d17251Schristos if (ctx == NULL) {
211b0d17251Schristos BIO_printf(bio_err,
212b0d17251Schristos "Error, DSA key generation context allocation failed\n");
213340218d9Schristos goto end;
214340218d9Schristos }
215b0d17251Schristos if (EVP_PKEY_keygen_init(ctx) <= 0) {
216b0d17251Schristos BIO_printf(bio_err,
217b0d17251Schristos "Error, unable to initialise for key generation\n");
218b0d17251Schristos goto end;
219b0d17251Schristos }
220b0d17251Schristos pkey = app_keygen(ctx, "DSA", numbits, verbose);
221*4778aedeSchristos if (pkey == NULL)
222*4778aedeSchristos goto end;
223c7da899bSchristos assert(private);
224a89c9211Schristos if (outformat == FORMAT_ASN1)
225b0d17251Schristos i = i2d_PrivateKey_bio(out, pkey);
226c7da899bSchristos else
227b0d17251Schristos i = PEM_write_bio_PrivateKey(out, pkey, NULL, NULL, 0, NULL, NULL);
228a89c9211Schristos }
229a89c9211Schristos ret = 0;
230a89c9211Schristos end:
231b0d17251Schristos if (ret != 0)
232b0d17251Schristos ERR_print_errors(bio_err);
233635165faSspz BIO_free_all(out);
234b0d17251Schristos EVP_PKEY_CTX_free(ctx);
235b0d17251Schristos EVP_PKEY_free(pkey);
236b0d17251Schristos EVP_PKEY_free(params);
23734505c60Sspz release_engine(e);
23813d40330Schristos return ret;
239a89c9211Schristos }
240a89c9211Schristos
gendsa_cb(EVP_PKEY_CTX * ctx)241b0d17251Schristos static int gendsa_cb(EVP_PKEY_CTX *ctx)
242a89c9211Schristos {
24313d40330Schristos static const char symbols[] = ".+*\n";
244b0d17251Schristos int p;
245b0d17251Schristos char c;
246b0d17251Schristos BIO *b;
247a89c9211Schristos
248b0d17251Schristos if (!verbose)
249b0d17251Schristos return 1;
250b0d17251Schristos
251b0d17251Schristos b = EVP_PKEY_CTX_get_app_data(ctx);
252b0d17251Schristos p = EVP_PKEY_CTX_get_keygen_info(ctx, 0);
253b0d17251Schristos c = (p >= 0 && (size_t)p < sizeof(symbols) - 1) ? symbols[p] : '?';
254b0d17251Schristos
255b0d17251Schristos BIO_write(b, &c, 1);
256b0d17251Schristos (void)BIO_flush(b);
257a89c9211Schristos return 1;
258a89c9211Schristos }
259