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>
14 #include <sys/types.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 typedef enum OPTION_choice {
27 OPT_OUT, OPT_PASSOUT, OPT_ENGINE, OPT_CIPHER, OPT_VERBOSE, OPT_QUIET,
28 OPT_R_ENUM, OPT_PROV_ENUM
31 const OPTIONS gendsa_options[] = {
32 {OPT_HELP_STR, 1, '-', "Usage: %s [options] dsaparam-file\n"},
34 OPT_SECTION("General"),
35 {"help", OPT_HELP, '-', "Display this summary"},
36 #ifndef OPENSSL_NO_ENGINE
37 {"engine", OPT_ENGINE, 's', "Use engine, possibly a hardware device"},
40 OPT_SECTION("Output"),
41 {"out", OPT_OUT, '>', "Output the key to the specified file"},
42 {"passout", OPT_PASSOUT, 's', "Output file pass phrase source"},
45 {"", OPT_CIPHER, '-', "Encrypt the output with any supported cipher"},
46 {"verbose", OPT_VERBOSE, '-', "Verbose output"},
47 {"quiet", OPT_QUIET, '-', "Terse output"},
50 {"dsaparam-file", 0, 0, "File containing DSA parameters"},
54 int gendsa_main(int argc, char **argv)
57 BIO *out = NULL, *in = NULL;
58 EVP_PKEY *pkey = NULL;
59 EVP_PKEY_CTX *ctx = NULL;
60 EVP_CIPHER *enc = NULL;
61 char *dsaparams = NULL, *ciphername = NULL;
62 char *outfile = NULL, *passoutarg = NULL, *passout = NULL, *prog;
64 int ret = 1, private = 0, verbose = 0, nbits;
66 opt_set_unknown_name("cipher");
67 prog = opt_init(argc, argv, gendsa_options);
68 while ((o = opt_next()) != OPT_EOF) {
73 BIO_printf(bio_err, "%s: Use -help for summary.\n", prog);
77 opt_help(gendsa_options);
83 passoutarg = opt_arg();
86 e = setup_engine(opt_arg(), 0);
97 ciphername = opt_unknown();
108 /* One argument, the params file. */
109 if (!opt_check_rest_arg("params file"))
114 if (!app_RAND_load())
117 if (!opt_cipher(ciphername, &enc))
121 if (!app_passwd(NULL, passoutarg, NULL, &passout)) {
122 BIO_printf(bio_err, "Error getting password\n");
126 pkey = load_keyparams(dsaparams, FORMAT_UNDEF, 1, "DSA", "DSA parameters");
128 out = bio_open_owner(outfile, FORMAT_PEM, private);
132 nbits = EVP_PKEY_get_bits(pkey);
133 if (nbits > OPENSSL_DSA_MAX_MODULUS_BITS)
135 "Warning: It is not recommended to use more than %d bit for DSA keys.\n"
136 " Your key size is %d! Larger key size may behave not as expected.\n",
137 OPENSSL_DSA_MAX_MODULUS_BITS, EVP_PKEY_get_bits(pkey));
139 ctx = EVP_PKEY_CTX_new_from_pkey(app_get0_libctx(), pkey, app_get0_propq());
141 BIO_printf(bio_err, "unable to create PKEY context\n");
146 if (EVP_PKEY_keygen_init(ctx) <= 0) {
147 BIO_printf(bio_err, "unable to set up for key generation\n");
150 pkey = app_keygen(ctx, "DSA", nbits, verbose);
155 if (!PEM_write_bio_PrivateKey(out, pkey, enc, NULL, 0, NULL, passout)) {
156 BIO_printf(bio_err, "unable to output generated key\n");
162 ERR_print_errors(bio_err);
167 EVP_PKEY_CTX_free(ctx);
168 EVP_CIPHER_free(enc);
170 OPENSSL_free(passout);