2 * Copyright 1995-2018 The OpenSSL Project Authors. All Rights Reserved.
4 * Licensed under the OpenSSL license (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>
12 NON_EMPTY_TRANSLATION_UNIT
17 # include <sys/types.h>
18 # include <sys/stat.h>
21 # include <openssl/bio.h>
22 # include <openssl/err.h>
23 # include <openssl/bn.h>
24 # include <openssl/dsa.h>
25 # include <openssl/x509.h>
26 # include <openssl/pem.h>
28 typedef enum OPTION_choice {
29 OPT_ERR = -1, OPT_EOF = 0, OPT_HELP,
30 OPT_OUT, OPT_PASSOUT, OPT_ENGINE, OPT_CIPHER,
34 const OPTIONS gendsa_options[] = {
35 {OPT_HELP_STR, 1, '-', "Usage: %s [args] dsaparam-file\n"},
36 {OPT_HELP_STR, 1, '-', "Valid options are:\n"},
37 {"help", OPT_HELP, '-', "Display this summary"},
38 {"out", OPT_OUT, '>', "Output the key to the specified file"},
39 {"passout", OPT_PASSOUT, 's', "Output file pass phrase source"},
41 {"", OPT_CIPHER, '-', "Encrypt the output with any supported cipher"},
42 # ifndef OPENSSL_NO_ENGINE
43 {"engine", OPT_ENGINE, 's', "Use engine, possibly a hardware device"},
48 int gendsa_main(int argc, char **argv)
51 BIO *out = NULL, *in = NULL;
53 const EVP_CIPHER *enc = NULL;
54 char *dsaparams = NULL;
55 char *outfile = NULL, *passoutarg = NULL, *passout = NULL, *prog;
57 int ret = 1, private = 0;
58 const BIGNUM *p = NULL;
60 prog = opt_init(argc, argv, gendsa_options);
61 while ((o = opt_next()) != OPT_EOF) {
66 BIO_printf(bio_err, "%s: Use -help for summary.\n", prog);
70 opt_help(gendsa_options);
76 passoutarg = opt_arg();
79 e = setup_engine(opt_arg(), 0);
86 if (!opt_cipher(opt_unknown(), &enc))
91 argc = opt_num_rest();
99 if (!app_passwd(NULL, passoutarg, NULL, &passout)) {
100 BIO_printf(bio_err, "Error getting password\n");
104 in = bio_open_default(dsaparams, 'r', FORMAT_PEM);
108 if ((dsa = PEM_read_bio_DSAparams(in, NULL, NULL, NULL)) == NULL) {
109 BIO_printf(bio_err, "unable to load DSA parameter file\n");
115 out = bio_open_owner(outfile, FORMAT_PEM, private);
119 DSA_get0_pqg(dsa, &p, NULL, NULL);
120 BIO_printf(bio_err, "Generating DSA key, %d bits\n", BN_num_bits(p));
121 if (!DSA_generate_key(dsa))
125 if (!PEM_write_bio_DSAPrivateKey(out, dsa, enc, NULL, 0, NULL, passout))
130 ERR_print_errors(bio_err);
136 OPENSSL_free(passout);