2 * Copyright 1995-2023 The OpenSSL Project Authors. All Rights Reserved.
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
10 #include <openssl/opensslconf.h>
18 #include <openssl/bio.h>
19 #include <openssl/err.h>
20 #include <openssl/bn.h>
21 #include <openssl/dsa.h>
22 #include <openssl/x509.h>
23 #include <openssl/pem.h>
25 static int verbose = 0;
27 typedef enum OPTION_choice {
29 OPT_INFORM, OPT_OUTFORM, OPT_IN, OPT_OUT, OPT_TEXT,
30 OPT_NOOUT, OPT_GENKEY, OPT_ENGINE, OPT_VERBOSE, OPT_QUIET,
31 OPT_R_ENUM, OPT_PROV_ENUM
34 const OPTIONS dsaparam_options[] = {
35 {OPT_HELP_STR, 1, '-', "Usage: %s [options] [numbits] [numqbits]\n"},
37 OPT_SECTION("General"),
38 {"help", OPT_HELP, '-', "Display this summary"},
39 #ifndef OPENSSL_NO_ENGINE
40 {"engine", OPT_ENGINE, 's', "Use engine e, possibly a hardware device"},
44 {"in", OPT_IN, '<', "Input file"},
45 {"inform", OPT_INFORM, 'F', "Input format - DER or PEM"},
47 OPT_SECTION("Output"),
48 {"out", OPT_OUT, '>', "Output file"},
49 {"outform", OPT_OUTFORM, 'F', "Output format - DER or PEM"},
50 {"text", OPT_TEXT, '-', "Print as text"},
51 {"noout", OPT_NOOUT, '-', "No output"},
52 {"verbose", OPT_VERBOSE, '-', "Verbose output"},
53 {"quiet", OPT_QUIET, '-', "Terse output"},
54 {"genkey", OPT_GENKEY, '-', "Generate a DSA key"},
60 {"numbits", 0, 0, "Number of bits if generating parameters or key (optional)"},
61 {"numqbits", 0, 0, "Number of bits in the subprime parameter q if generating parameters or key (optional)"},
65 int dsaparam_main(int argc, char **argv)
69 EVP_PKEY *params = NULL, *pkey = NULL;
70 EVP_PKEY_CTX *ctx = NULL;
71 int numbits = -1, numqbits = -1, num = 0, genkey = 0;
72 int informat = FORMAT_UNDEF, outformat = FORMAT_PEM, noout = 0;
73 int ret = 1, i, text = 0, private = 0;
74 char *infile = NULL, *outfile = NULL, *prog;
77 prog = opt_init(argc, argv, dsaparam_options);
78 while ((o = opt_next()) != OPT_EOF) {
83 BIO_printf(bio_err, "%s: Use -help for summary.\n", prog);
86 opt_help(dsaparam_options);
90 if (!opt_format(opt_arg(), OPT_FMT_PEMDER, &informat))
97 if (!opt_format(opt_arg(), OPT_FMT_PEMDER, &outformat))
104 e = setup_engine(opt_arg(), 0);
117 if (!opt_provider(o))
132 /* Optional args are bitsize and q bitsize. */
133 argc = opt_num_rest();
136 if (!opt_int(argv[0], &num) || num < 0)
138 if (!opt_int(argv[1], &numqbits) || numqbits < 0)
140 } else if (argc == 1) {
141 if (!opt_int(argv[0], &num) || num < 0)
143 } else if (!opt_check_rest_arg(NULL)) {
146 if (!app_RAND_load())
151 private = genkey ? 1 : 0;
153 out = bio_open_owner(outfile, outformat, private);
157 ctx = EVP_PKEY_CTX_new_from_name(app_get0_libctx(), "DSA", app_get0_propq());
160 "Error, DSA parameter generation context allocation failed\n");
164 if (numbits > OPENSSL_DSA_MAX_MODULUS_BITS)
166 "Warning: It is not recommended to use more than %d bit for DSA keys.\n"
167 " Your key size is %d! Larger key size may behave not as expected.\n",
168 OPENSSL_DSA_MAX_MODULUS_BITS, numbits);
170 EVP_PKEY_CTX_set_app_data(ctx, bio_err);
172 EVP_PKEY_CTX_set_cb(ctx, progress_cb);
173 BIO_printf(bio_err, "Generating DSA parameters, %d bit long prime\n",
175 BIO_printf(bio_err, "This could take some time\n");
177 if (EVP_PKEY_paramgen_init(ctx) <= 0) {
179 "Error, DSA key generation paramgen init failed\n");
182 if (EVP_PKEY_CTX_set_dsa_paramgen_bits(ctx, num) <= 0) {
184 "Error, DSA key generation setting bit length failed\n");
188 if (EVP_PKEY_CTX_set_dsa_paramgen_q_bits(ctx, numqbits) <= 0) {
190 "Error, DSA key generation setting subprime bit length failed\n");
194 params = app_paramgen(ctx, "DSA");
196 params = load_keyparams(infile, informat, 1, "DSA", "DSA parameters");
198 if (params == NULL) {
199 /* Error message should already have been displayed */
204 EVP_PKEY_print_params(out, params, 0, NULL);
207 if (outformat == FORMAT_ASN1 && genkey)
211 if (outformat == FORMAT_ASN1)
212 i = i2d_KeyParams_bio(out, params);
214 i = PEM_write_bio_Parameters(out, params);
216 BIO_printf(bio_err, "Error, unable to write DSA parameters\n");
221 EVP_PKEY_CTX_free(ctx);
222 ctx = EVP_PKEY_CTX_new_from_pkey(app_get0_libctx(), params,
226 "Error, DSA key generation context allocation failed\n");
229 if (EVP_PKEY_keygen_init(ctx) <= 0) {
231 "Error, unable to initialise for key generation\n");
234 pkey = app_keygen(ctx, "DSA", numbits, verbose);
236 if (outformat == FORMAT_ASN1)
237 i = i2d_PrivateKey_bio(out, pkey);
239 i = PEM_write_bio_PrivateKey(out, pkey, NULL, NULL, 0, NULL, NULL);
244 ERR_print_errors(bio_err);
246 EVP_PKEY_CTX_free(ctx);
248 EVP_PKEY_free(params);