2 * Copyright 1995-2016 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>
20 # include <openssl/bio.h>
21 # include <openssl/err.h>
22 # include <openssl/bn.h>
23 # include <openssl/dsa.h>
24 # include <openssl/x509.h>
25 # include <openssl/pem.h>
27 typedef enum OPTION_choice
{
28 OPT_ERR
= -1, OPT_EOF
= 0, OPT_HELP
,
29 OPT_OUT
, OPT_PASSOUT
, OPT_ENGINE
, OPT_RAND
, OPT_CIPHER
32 OPTIONS gendsa_options
[] = {
33 {OPT_HELP_STR
, 1, '-', "Usage: %s [args] dsaparam-file\n"},
34 {OPT_HELP_STR
, 1, '-', "Valid options are:\n"},
35 {"help", OPT_HELP
, '-', "Display this summary"},
36 {"out", OPT_OUT
, '>', "Output the key to the specified file"},
37 {"passout", OPT_PASSOUT
, 's'},
38 {"rand", OPT_RAND
, 's',
39 "Load the file(s) into the random number generator"},
40 {"", OPT_CIPHER
, '-', "Encrypt the output with any supported cipher"},
41 # ifndef OPENSSL_NO_ENGINE
42 {"engine", OPT_ENGINE
, 's', "Use engine, possibly a hardware device"},
47 int gendsa_main(int argc
, char **argv
)
49 BIO
*out
= NULL
, *in
= NULL
;
51 const EVP_CIPHER
*enc
= NULL
;
52 char *inrand
= NULL
, *dsaparams
= NULL
;
53 char *outfile
= NULL
, *passoutarg
= NULL
, *passout
= NULL
, *prog
;
55 int ret
= 1, private = 0;
58 prog
= opt_init(argc
, argv
, gendsa_options
);
59 while ((o
= opt_next()) != OPT_EOF
) {
64 BIO_printf(bio_err
, "%s: Use -help for summary.\n", prog
);
68 opt_help(gendsa_options
);
74 passoutarg
= opt_arg();
77 (void)setup_engine(opt_arg(), 0);
83 if (!opt_cipher(opt_unknown(), &enc
))
88 argc
= opt_num_rest();
96 if (!app_passwd(NULL
, passoutarg
, NULL
, &passout
)) {
97 BIO_printf(bio_err
, "Error getting password\n");
101 in
= bio_open_default(dsaparams
, 'r', FORMAT_PEM
);
105 if ((dsa
= PEM_read_bio_DSAparams(in
, NULL
, NULL
, NULL
)) == NULL
) {
106 BIO_printf(bio_err
, "unable to load DSA parameter file\n");
112 out
= bio_open_owner(outfile
, FORMAT_PEM
, private);
116 if (!app_RAND_load_file(NULL
, 1) && inrand
== NULL
) {
118 "warning, not much extra random data, consider using the -rand option\n");
121 BIO_printf(bio_err
, "%ld semi-random bytes loaded\n",
122 app_RAND_load_files(inrand
));
124 DSA_get0_pqg(dsa
, &p
, NULL
, NULL
);
125 BIO_printf(bio_err
, "Generating DSA key, %d bits\n", BN_num_bits(p
));
126 if (!DSA_generate_key(dsa
))
129 app_RAND_write_file(NULL
);
132 if (!PEM_write_bio_DSAPrivateKey(out
, dsa
, enc
, NULL
, 0, NULL
, passout
))
137 ERR_print_errors(bio_err
);
142 OPENSSL_free(passout
);