16f9291ceSJung-uk Kim /*
2*ad991e4cSEd Maste * Copyright 1995-2023 The OpenSSL Project Authors. All Rights Reserved.
3e71b7053SJung-uk Kim *
4b077aed3SPierre Pronchery * Licensed under the Apache License 2.0 (the "License"). You may not use
5e71b7053SJung-uk Kim * this file except in compliance with the License. You can obtain a copy
6e71b7053SJung-uk Kim * in the file LICENSE in the source distribution or at
7e71b7053SJung-uk Kim * https://www.openssl.org/source/license.html
86f9291ceSJung-uk Kim */
93b4e3dcbSSimon L. B. Nielsen
10e71b7053SJung-uk Kim #include <openssl/opensslconf.h>
11b077aed3SPierre Pronchery
1274664626SKris Kennaway #include <stdio.h>
1374664626SKris Kennaway #include <stdlib.h>
14b077aed3SPierre Pronchery #include "apps.h"
1574664626SKris Kennaway #include <time.h>
1674664626SKris Kennaway #include <string.h>
1774664626SKris Kennaway #include "apps.h"
18e71b7053SJung-uk Kim #include "progs.h"
1974664626SKris Kennaway #include <openssl/bio.h>
2074664626SKris Kennaway #include <openssl/err.h>
2174664626SKris Kennaway #include <openssl/bn.h>
2274664626SKris Kennaway #include <openssl/dsa.h>
2374664626SKris Kennaway #include <openssl/x509.h>
2474664626SKris Kennaway #include <openssl/pem.h>
2574664626SKris Kennaway
26b077aed3SPierre Pronchery static int verbose = 0;
27b077aed3SPierre Pronchery
28b077aed3SPierre Pronchery static int gendsa_cb(EVP_PKEY_CTX *ctx);
2974664626SKris Kennaway
30e71b7053SJung-uk Kim typedef enum OPTION_choice {
31b077aed3SPierre Pronchery OPT_COMMON,
32b077aed3SPierre Pronchery OPT_INFORM, OPT_OUTFORM, OPT_IN, OPT_OUT, OPT_TEXT,
33b077aed3SPierre Pronchery OPT_NOOUT, OPT_GENKEY, OPT_ENGINE, OPT_VERBOSE,
34b077aed3SPierre Pronchery OPT_R_ENUM, OPT_PROV_ENUM
35e71b7053SJung-uk Kim } OPTION_CHOICE;
3674664626SKris Kennaway
37e71b7053SJung-uk Kim const OPTIONS dsaparam_options[] = {
38b077aed3SPierre Pronchery {OPT_HELP_STR, 1, '-', "Usage: %s [options] [numbits]\n"},
39b077aed3SPierre Pronchery
40b077aed3SPierre Pronchery OPT_SECTION("General"),
41e71b7053SJung-uk Kim {"help", OPT_HELP, '-', "Display this summary"},
42fceca8a3SJacques Vidrine #ifndef OPENSSL_NO_ENGINE
43e71b7053SJung-uk Kim {"engine", OPT_ENGINE, 's', "Use engine e, possibly a hardware device"},
44fceca8a3SJacques Vidrine #endif
45b077aed3SPierre Pronchery
46b077aed3SPierre Pronchery OPT_SECTION("Input"),
47b077aed3SPierre Pronchery {"in", OPT_IN, '<', "Input file"},
48b077aed3SPierre Pronchery {"inform", OPT_INFORM, 'F', "Input format - DER or PEM"},
49b077aed3SPierre Pronchery
50b077aed3SPierre Pronchery OPT_SECTION("Output"),
51b077aed3SPierre Pronchery {"out", OPT_OUT, '>', "Output file"},
52b077aed3SPierre Pronchery {"outform", OPT_OUTFORM, 'F', "Output format - DER or PEM"},
53b077aed3SPierre Pronchery {"text", OPT_TEXT, '-', "Print as text"},
54b077aed3SPierre Pronchery {"noout", OPT_NOOUT, '-', "No output"},
55b077aed3SPierre Pronchery {"verbose", OPT_VERBOSE, '-', "Verbose output"},
56b077aed3SPierre Pronchery {"genkey", OPT_GENKEY, '-', "Generate a DSA key"},
57b077aed3SPierre Pronchery
58b077aed3SPierre Pronchery OPT_R_OPTIONS,
59b077aed3SPierre Pronchery OPT_PROV_OPTIONS,
60b077aed3SPierre Pronchery
61b077aed3SPierre Pronchery OPT_PARAMETERS(),
62b077aed3SPierre Pronchery {"numbits", 0, 0, "Number of bits if generating parameters (optional)"},
63e71b7053SJung-uk Kim {NULL}
64e71b7053SJung-uk Kim };
65e71b7053SJung-uk Kim
dsaparam_main(int argc,char ** argv)66e71b7053SJung-uk Kim int dsaparam_main(int argc, char **argv)
67e71b7053SJung-uk Kim {
68e71b7053SJung-uk Kim ENGINE *e = NULL;
69b077aed3SPierre Pronchery BIO *out = NULL;
70b077aed3SPierre Pronchery EVP_PKEY *params = NULL, *pkey = NULL;
71b077aed3SPierre Pronchery EVP_PKEY_CTX *ctx = NULL;
72e71b7053SJung-uk Kim int numbits = -1, num = 0, genkey = 0;
73b077aed3SPierre Pronchery int informat = FORMAT_UNDEF, outformat = FORMAT_PEM, noout = 0;
74e71b7053SJung-uk Kim int ret = 1, i, text = 0, private = 0;
75e71b7053SJung-uk Kim char *infile = NULL, *outfile = NULL, *prog;
76e71b7053SJung-uk Kim OPTION_CHOICE o;
77e71b7053SJung-uk Kim
78e71b7053SJung-uk Kim prog = opt_init(argc, argv, dsaparam_options);
79e71b7053SJung-uk Kim while ((o = opt_next()) != OPT_EOF) {
80e71b7053SJung-uk Kim switch (o) {
81e71b7053SJung-uk Kim case OPT_EOF:
82e71b7053SJung-uk Kim case OPT_ERR:
83e71b7053SJung-uk Kim opthelp:
84e71b7053SJung-uk Kim BIO_printf(bio_err, "%s: Use -help for summary.\n", prog);
85e71b7053SJung-uk Kim goto end;
86e71b7053SJung-uk Kim case OPT_HELP:
87e71b7053SJung-uk Kim opt_help(dsaparam_options);
88e71b7053SJung-uk Kim ret = 0;
89e71b7053SJung-uk Kim goto end;
90e71b7053SJung-uk Kim case OPT_INFORM:
91e71b7053SJung-uk Kim if (!opt_format(opt_arg(), OPT_FMT_PEMDER, &informat))
92e71b7053SJung-uk Kim goto opthelp;
93e71b7053SJung-uk Kim break;
94e71b7053SJung-uk Kim case OPT_IN:
95e71b7053SJung-uk Kim infile = opt_arg();
96e71b7053SJung-uk Kim break;
97e71b7053SJung-uk Kim case OPT_OUTFORM:
98e71b7053SJung-uk Kim if (!opt_format(opt_arg(), OPT_FMT_PEMDER, &outformat))
99e71b7053SJung-uk Kim goto opthelp;
100e71b7053SJung-uk Kim break;
101e71b7053SJung-uk Kim case OPT_OUT:
102e71b7053SJung-uk Kim outfile = opt_arg();
103e71b7053SJung-uk Kim break;
104e71b7053SJung-uk Kim case OPT_ENGINE:
105e71b7053SJung-uk Kim e = setup_engine(opt_arg(), 0);
106e71b7053SJung-uk Kim break;
107e71b7053SJung-uk Kim case OPT_TEXT:
10874664626SKris Kennaway text = 1;
109e71b7053SJung-uk Kim break;
110e71b7053SJung-uk Kim case OPT_GENKEY:
11174664626SKris Kennaway genkey = 1;
112e71b7053SJung-uk Kim break;
113e71b7053SJung-uk Kim case OPT_R_CASES:
114e71b7053SJung-uk Kim if (!opt_rand(o))
115e71b7053SJung-uk Kim goto end;
116e71b7053SJung-uk Kim break;
117b077aed3SPierre Pronchery case OPT_PROV_CASES:
118b077aed3SPierre Pronchery if (!opt_provider(o))
119b077aed3SPierre Pronchery goto end;
120b077aed3SPierre Pronchery break;
121e71b7053SJung-uk Kim case OPT_NOOUT:
12274664626SKris Kennaway noout = 1;
12374664626SKris Kennaway break;
124b077aed3SPierre Pronchery case OPT_VERBOSE:
125b077aed3SPierre Pronchery verbose = 1;
126b077aed3SPierre Pronchery break;
12774664626SKris Kennaway }
12874664626SKris Kennaway }
129b077aed3SPierre Pronchery
130b077aed3SPierre Pronchery /* Optional arg is bitsize. */
131e71b7053SJung-uk Kim argc = opt_num_rest();
132e71b7053SJung-uk Kim argv = opt_rest();
133e71b7053SJung-uk Kim if (argc == 1) {
134e71b7053SJung-uk Kim if (!opt_int(argv[0], &num) || num < 0)
135b077aed3SPierre Pronchery goto opthelp;
136b077aed3SPierre Pronchery } else if (argc != 0) {
137b077aed3SPierre Pronchery goto opthelp;
138b077aed3SPierre Pronchery }
139b077aed3SPierre Pronchery if (!app_RAND_load())
14074664626SKris Kennaway goto end;
141b077aed3SPierre Pronchery
142e71b7053SJung-uk Kim /* generate a key */
143e71b7053SJung-uk Kim numbits = num;
144e71b7053SJung-uk Kim private = genkey ? 1 : 0;
14574664626SKris Kennaway
146e71b7053SJung-uk Kim out = bio_open_owner(outfile, outformat, private);
147e71b7053SJung-uk Kim if (out == NULL)
14874664626SKris Kennaway goto end;
149f579bf8eSKris Kennaway
150b077aed3SPierre Pronchery ctx = EVP_PKEY_CTX_new_from_name(app_get0_libctx(), "DSA", app_get0_propq());
151b077aed3SPierre Pronchery if (ctx == NULL) {
152b077aed3SPierre Pronchery BIO_printf(bio_err,
153b077aed3SPierre Pronchery "Error, DSA parameter generation context allocation failed\n");
154b077aed3SPierre Pronchery goto end;
155b077aed3SPierre Pronchery }
1566f9291ceSJung-uk Kim if (numbits > 0) {
157e71b7053SJung-uk Kim if (numbits > OPENSSL_DSA_MAX_MODULUS_BITS)
158e71b7053SJung-uk Kim BIO_printf(bio_err,
159e71b7053SJung-uk Kim "Warning: It is not recommended to use more than %d bit for DSA keys.\n"
160e71b7053SJung-uk Kim " Your key size is %d! Larger key size may behave not as expected.\n",
161e71b7053SJung-uk Kim OPENSSL_DSA_MAX_MODULUS_BITS, numbits);
162e71b7053SJung-uk Kim
163b077aed3SPierre Pronchery EVP_PKEY_CTX_set_cb(ctx, gendsa_cb);
164b077aed3SPierre Pronchery EVP_PKEY_CTX_set_app_data(ctx, bio_err);
165b077aed3SPierre Pronchery if (verbose) {
1666f9291ceSJung-uk Kim BIO_printf(bio_err, "Generating DSA parameters, %d bit long prime\n",
1676f9291ceSJung-uk Kim num);
16874664626SKris Kennaway BIO_printf(bio_err, "This could take some time\n");
169b077aed3SPierre Pronchery }
170b077aed3SPierre Pronchery if (EVP_PKEY_paramgen_init(ctx) <= 0) {
171b077aed3SPierre Pronchery BIO_printf(bio_err,
172b077aed3SPierre Pronchery "Error, DSA key generation paramgen init failed\n");
1733b4e3dcbSSimon L. B. Nielsen goto end;
1743b4e3dcbSSimon L. B. Nielsen }
175b077aed3SPierre Pronchery if (EVP_PKEY_CTX_set_dsa_paramgen_bits(ctx, num) <= 0) {
176b077aed3SPierre Pronchery BIO_printf(bio_err,
177b077aed3SPierre Pronchery "Error, DSA key generation setting bit length failed\n");
178b077aed3SPierre Pronchery goto end;
17974664626SKris Kennaway }
180b077aed3SPierre Pronchery params = app_paramgen(ctx, "DSA");
181b077aed3SPierre Pronchery } else {
182b077aed3SPierre Pronchery params = load_keyparams(infile, informat, 1, "DSA", "DSA parameters");
183b077aed3SPierre Pronchery }
184b077aed3SPierre Pronchery if (params == NULL) {
185b077aed3SPierre Pronchery /* Error message should already have been displayed */
18674664626SKris Kennaway goto end;
18774664626SKris Kennaway }
18874664626SKris Kennaway
1896f9291ceSJung-uk Kim if (text) {
190b077aed3SPierre Pronchery EVP_PKEY_print_params(out, params, 0, NULL);
19174664626SKris Kennaway }
19274664626SKris Kennaway
193dee36b4fSJung-uk Kim if (outformat == FORMAT_ASN1 && genkey)
194dee36b4fSJung-uk Kim noout = 1;
195dee36b4fSJung-uk Kim
1966f9291ceSJung-uk Kim if (!noout) {
19774664626SKris Kennaway if (outformat == FORMAT_ASN1)
198b077aed3SPierre Pronchery i = i2d_KeyParams_bio(out, params);
199e71b7053SJung-uk Kim else
200b077aed3SPierre Pronchery i = PEM_write_bio_Parameters(out, params);
2016f9291ceSJung-uk Kim if (!i) {
202b077aed3SPierre Pronchery BIO_printf(bio_err, "Error, unable to write DSA parameters\n");
20374664626SKris Kennaway goto end;
20474664626SKris Kennaway }
20574664626SKris Kennaway }
2066f9291ceSJung-uk Kim if (genkey) {
207b077aed3SPierre Pronchery EVP_PKEY_CTX_free(ctx);
208b077aed3SPierre Pronchery ctx = EVP_PKEY_CTX_new_from_pkey(app_get0_libctx(), params,
209b077aed3SPierre Pronchery app_get0_propq());
210b077aed3SPierre Pronchery if (ctx == NULL) {
211b077aed3SPierre Pronchery BIO_printf(bio_err,
212b077aed3SPierre Pronchery "Error, DSA key generation context allocation failed\n");
21309286989SJung-uk Kim goto end;
21409286989SJung-uk Kim }
215b077aed3SPierre Pronchery if (EVP_PKEY_keygen_init(ctx) <= 0) {
216b077aed3SPierre Pronchery BIO_printf(bio_err,
217b077aed3SPierre Pronchery "Error, unable to initialise for key generation\n");
218b077aed3SPierre Pronchery goto end;
219b077aed3SPierre Pronchery }
220b077aed3SPierre Pronchery pkey = app_keygen(ctx, "DSA", numbits, verbose);
221*ad991e4cSEd Maste if (pkey == NULL)
222*ad991e4cSEd Maste goto end;
223e71b7053SJung-uk Kim assert(private);
22474664626SKris Kennaway if (outformat == FORMAT_ASN1)
225b077aed3SPierre Pronchery i = i2d_PrivateKey_bio(out, pkey);
226e71b7053SJung-uk Kim else
227b077aed3SPierre Pronchery i = PEM_write_bio_PrivateKey(out, pkey, NULL, NULL, 0, NULL, NULL);
22874664626SKris Kennaway }
22974664626SKris Kennaway ret = 0;
23074664626SKris Kennaway end:
231b077aed3SPierre Pronchery if (ret != 0)
232b077aed3SPierre Pronchery ERR_print_errors(bio_err);
2336f9291ceSJung-uk Kim BIO_free_all(out);
234b077aed3SPierre Pronchery EVP_PKEY_CTX_free(ctx);
235b077aed3SPierre Pronchery EVP_PKEY_free(pkey);
236b077aed3SPierre Pronchery EVP_PKEY_free(params);
2376cf8931aSJung-uk Kim release_engine(e);
238e71b7053SJung-uk Kim return ret;
23974664626SKris Kennaway }
24074664626SKris Kennaway
gendsa_cb(EVP_PKEY_CTX * ctx)241b077aed3SPierre Pronchery static int gendsa_cb(EVP_PKEY_CTX *ctx)
24274664626SKris Kennaway {
243e71b7053SJung-uk Kim static const char symbols[] = ".+*\n";
244b077aed3SPierre Pronchery int p;
245b077aed3SPierre Pronchery char c;
246b077aed3SPierre Pronchery BIO *b;
24774664626SKris Kennaway
248b077aed3SPierre Pronchery if (!verbose)
249b077aed3SPierre Pronchery return 1;
250b077aed3SPierre Pronchery
251b077aed3SPierre Pronchery b = EVP_PKEY_CTX_get_app_data(ctx);
252b077aed3SPierre Pronchery p = EVP_PKEY_CTX_get_keygen_info(ctx, 0);
253b077aed3SPierre Pronchery c = (p >= 0 && (size_t)p < sizeof(symbols) - 1) ? symbols[p] : '?';
254b077aed3SPierre Pronchery
255b077aed3SPierre Pronchery BIO_write(b, &c, 1);
256b077aed3SPierre Pronchery (void)BIO_flush(b);
2573b4e3dcbSSimon L. B. Nielsen return 1;
25874664626SKris Kennaway }
259