2 * Copyright 1995-2021 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>
19 #include <openssl/bio.h>
20 #include <openssl/err.h>
21 #include <openssl/bn.h>
22 #include <openssl/dsa.h>
23 #include <openssl/x509.h>
24 #include <openssl/pem.h>
26 static int verbose = 0;
28 static int gendsa_cb(EVP_PKEY_CTX *ctx);
30 typedef enum OPTION_choice {
32 OPT_INFORM, OPT_OUTFORM, OPT_IN, OPT_OUT, OPT_TEXT,
33 OPT_NOOUT, OPT_GENKEY, OPT_ENGINE, OPT_VERBOSE,
34 OPT_R_ENUM, OPT_PROV_ENUM
37 const OPTIONS dsaparam_options[] = {
38 {OPT_HELP_STR, 1, '-', "Usage: %s [options] [numbits]\n"},
40 OPT_SECTION("General"),
41 {"help", OPT_HELP, '-', "Display this summary"},
42 #ifndef OPENSSL_NO_ENGINE
43 {"engine", OPT_ENGINE, 's', "Use engine e, possibly a hardware device"},
47 {"in", OPT_IN, '<', "Input file"},
48 {"inform", OPT_INFORM, 'F', "Input format - DER or PEM"},
50 OPT_SECTION("Output"),
51 {"out", OPT_OUT, '>', "Output file"},
52 {"outform", OPT_OUTFORM, 'F', "Output format - DER or PEM"},
53 {"text", OPT_TEXT, '-', "Print as text"},
54 {"noout", OPT_NOOUT, '-', "No output"},
55 {"verbose", OPT_VERBOSE, '-', "Verbose output"},
56 {"genkey", OPT_GENKEY, '-', "Generate a DSA key"},
62 {"numbits", 0, 0, "Number of bits if generating parameters (optional)"},
66 int dsaparam_main(int argc, char **argv)
70 EVP_PKEY *params = NULL, *pkey = NULL;
71 EVP_PKEY_CTX *ctx = NULL;
72 int numbits = -1, num = 0, genkey = 0;
73 int informat = FORMAT_UNDEF, outformat = FORMAT_PEM, noout = 0;
74 int ret = 1, i, text = 0, private = 0;
75 char *infile = NULL, *outfile = NULL, *prog;
78 prog = opt_init(argc, argv, dsaparam_options);
79 while ((o = opt_next()) != OPT_EOF) {
84 BIO_printf(bio_err, "%s: Use -help for summary.\n", prog);
87 opt_help(dsaparam_options);
91 if (!opt_format(opt_arg(), OPT_FMT_PEMDER, &informat))
98 if (!opt_format(opt_arg(), OPT_FMT_PEMDER, &outformat))
105 e = setup_engine(opt_arg(), 0);
118 if (!opt_provider(o))
130 /* Optional arg is bitsize. */
131 argc = opt_num_rest();
134 if (!opt_int(argv[0], &num) || num < 0)
136 } else if (!opt_check_rest_arg(NULL)) {
139 if (!app_RAND_load())
144 private = genkey ? 1 : 0;
146 out = bio_open_owner(outfile, outformat, private);
150 ctx = EVP_PKEY_CTX_new_from_name(NULL, "DSA", NULL);
153 "Error, DSA parameter generation context allocation failed\n");
157 if (numbits > OPENSSL_DSA_MAX_MODULUS_BITS)
159 "Warning: It is not recommended to use more than %d bit for DSA keys.\n"
160 " Your key size is %d! Larger key size may behave not as expected.\n",
161 OPENSSL_DSA_MAX_MODULUS_BITS, numbits);
163 EVP_PKEY_CTX_set_cb(ctx, gendsa_cb);
164 EVP_PKEY_CTX_set_app_data(ctx, bio_err);
166 BIO_printf(bio_err, "Generating DSA parameters, %d bit long prime\n",
168 BIO_printf(bio_err, "This could take some time\n");
170 if (EVP_PKEY_paramgen_init(ctx) <= 0) {
172 "Error, DSA key generation paramgen init failed\n");
175 if (!EVP_PKEY_CTX_set_dsa_paramgen_bits(ctx, num)) {
177 "Error, DSA key generation setting bit length failed\n");
180 params = app_paramgen(ctx, "DSA");
182 params = load_keyparams(infile, informat, 1, "DSA", "DSA parameters");
184 if (params == NULL) {
185 /* Error message should already have been displayed */
190 EVP_PKEY_print_params(out, params, 0, NULL);
193 if (outformat == FORMAT_ASN1 && genkey)
197 if (outformat == FORMAT_ASN1)
198 i = i2d_KeyParams_bio(out, params);
200 i = PEM_write_bio_Parameters(out, params);
202 BIO_printf(bio_err, "Error, unable to write DSA parameters\n");
207 EVP_PKEY_CTX_free(ctx);
208 ctx = EVP_PKEY_CTX_new(params, NULL);
211 "Error, DSA key generation context allocation failed\n");
214 if (EVP_PKEY_keygen_init(ctx) <= 0) {
216 "Error, unable to initialise for key generation\n");
219 pkey = app_keygen(ctx, "DSA", numbits, verbose);
221 if (outformat == FORMAT_ASN1)
222 i = i2d_PrivateKey_bio(out, pkey);
224 i = PEM_write_bio_PrivateKey(out, pkey, NULL, NULL, 0, NULL, NULL);
229 ERR_print_errors(bio_err);
231 EVP_PKEY_CTX_free(ctx);
233 EVP_PKEY_free(params);
238 static int gendsa_cb(EVP_PKEY_CTX *ctx)
240 static const char symbols[] = ".+*\n";
248 b = EVP_PKEY_CTX_get_app_data(ctx);
249 p = EVP_PKEY_CTX_get_keygen_info(ctx, 0);
250 c = (p >= 0 && (size_t)p < sizeof(symbols) - 1) ? symbols[p] : '?';