]>
Commit | Line | Data |
---|---|---|
846e33c7 | 1 | /* |
6738bf14 | 2 | * Copyright 1995-2018 The OpenSSL Project Authors. All Rights Reserved. |
d02b48c6 | 3 | * |
dffa7520 | 4 | * Licensed under the Apache License 2.0 (the "License"). You may not use |
846e33c7 RS |
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 | |
d02b48c6 RE |
8 | */ |
9 | ||
effaf4de RS |
10 | #include <openssl/opensslconf.h> |
11 | #ifdef OPENSSL_NO_DSA | |
12 | NON_EMPTY_TRANSLATION_UNIT | |
13 | #else | |
14 | ||
0f113f3e MC |
15 | # include <stdio.h> |
16 | # include <string.h> | |
17 | # include <sys/types.h> | |
18 | # include <sys/stat.h> | |
19 | # include "apps.h" | |
dab2cd68 | 20 | # include "progs.h" |
0f113f3e MC |
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> | |
d02b48c6 | 27 | |
7e1b7485 RS |
28 | typedef enum OPTION_choice { |
29 | OPT_ERR = -1, OPT_EOF = 0, OPT_HELP, | |
b6a07f67 | 30 | OPT_OUT, OPT_PASSOUT, OPT_ENGINE, OPT_CIPHER, OPT_VERBOSE, |
3ee1eac2 | 31 | OPT_R_ENUM |
7e1b7485 RS |
32 | } OPTION_CHOICE; |
33 | ||
44c83ebd | 34 | const OPTIONS gendsa_options[] = { |
7e1b7485 RS |
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"}, | |
12d56b29 | 39 | {"passout", OPT_PASSOUT, 's', "Output file pass phrase source"}, |
3ee1eac2 | 40 | OPT_R_OPTIONS, |
9c3bcfa0 | 41 | {"", OPT_CIPHER, '-', "Encrypt the output with any supported cipher"}, |
7e1b7485 RS |
42 | # ifndef OPENSSL_NO_ENGINE |
43 | {"engine", OPT_ENGINE, 's', "Use engine, possibly a hardware device"}, | |
44 | # endif | |
b6a07f67 | 45 | {"verbose", OPT_VERBOSE, '-', "Verbose output"}, |
7e1b7485 RS |
46 | {NULL} |
47 | }; | |
667ac4ec | 48 | |
7e1b7485 | 49 | int gendsa_main(int argc, char **argv) |
0f113f3e | 50 | { |
dd1abd44 | 51 | ENGINE *e = NULL; |
0f113f3e | 52 | BIO *out = NULL, *in = NULL; |
7e1b7485 | 53 | DSA *dsa = NULL; |
0f113f3e | 54 | const EVP_CIPHER *enc = NULL; |
3ee1eac2 | 55 | char *dsaparams = NULL; |
7e1b7485 RS |
56 | char *outfile = NULL, *passoutarg = NULL, *passout = NULL, *prog; |
57 | OPTION_CHOICE o; | |
b6a07f67 | 58 | int ret = 1, private = 0, verbose = 0; |
2ac6115d | 59 | const BIGNUM *p = NULL; |
d02b48c6 | 60 | |
7e1b7485 RS |
61 | prog = opt_init(argc, argv, gendsa_options); |
62 | while ((o = opt_next()) != OPT_EOF) { | |
63 | switch (o) { | |
64 | case OPT_EOF: | |
65 | case OPT_ERR: | |
66 | opthelp: | |
67 | BIO_printf(bio_err, "%s: Use -help for summary.\n", prog); | |
68 | goto end; | |
69 | case OPT_HELP: | |
70 | ret = 0; | |
71 | opt_help(gendsa_options); | |
72 | goto end; | |
73 | case OPT_OUT: | |
74 | outfile = opt_arg(); | |
75 | break; | |
76 | case OPT_PASSOUT: | |
77 | passoutarg = opt_arg(); | |
78 | break; | |
79 | case OPT_ENGINE: | |
dd1abd44 | 80 | e = setup_engine(opt_arg(), 0); |
7e1b7485 | 81 | break; |
3ee1eac2 RS |
82 | case OPT_R_CASES: |
83 | if (!opt_rand(o)) | |
84 | goto end; | |
7e1b7485 RS |
85 | break; |
86 | case OPT_CIPHER: | |
87 | if (!opt_cipher(opt_unknown(), &enc)) | |
88 | goto end; | |
0f113f3e | 89 | break; |
b6a07f67 PP |
90 | case OPT_VERBOSE: |
91 | verbose = 1; | |
92 | break; | |
0f113f3e | 93 | } |
0f113f3e | 94 | } |
7e1b7485 RS |
95 | argc = opt_num_rest(); |
96 | argv = opt_rest(); | |
3b061a00 | 97 | private = 1; |
7e1b7485 RS |
98 | |
99 | if (argc != 1) | |
100 | goto opthelp; | |
101 | dsaparams = *argv; | |
d02b48c6 | 102 | |
7e1b7485 | 103 | if (!app_passwd(NULL, passoutarg, NULL, &passout)) { |
0f113f3e MC |
104 | BIO_printf(bio_err, "Error getting password\n"); |
105 | goto end; | |
106 | } | |
a3fe382e | 107 | |
bdd58d98 | 108 | in = bio_open_default(dsaparams, 'r', FORMAT_PEM); |
7e1b7485 RS |
109 | if (in == NULL) |
110 | goto end2; | |
a3fe382e | 111 | |
0f113f3e MC |
112 | if ((dsa = PEM_read_bio_DSAparams(in, NULL, NULL, NULL)) == NULL) { |
113 | BIO_printf(bio_err, "unable to load DSA parameter file\n"); | |
114 | goto end; | |
115 | } | |
116 | BIO_free(in); | |
117 | in = NULL; | |
d02b48c6 | 118 | |
bdd58d98 | 119 | out = bio_open_owner(outfile, FORMAT_PEM, private); |
0f113f3e | 120 | if (out == NULL) |
7e1b7485 | 121 | goto end2; |
d02b48c6 | 122 | |
6e9fa57c | 123 | DSA_get0_pqg(dsa, &p, NULL, NULL); |
0336df2f GS |
124 | |
125 | if (BN_num_bits(p) > OPENSSL_DSA_MAX_MODULUS_BITS) | |
126 | BIO_printf(bio_err, | |
127 | "Warning: It is not recommended to use more than %d bit for DSA keys.\n" | |
128 | " Your key size is %d! Larger key size may behave not as expected.\n", | |
129 | OPENSSL_DSA_MAX_MODULUS_BITS, BN_num_bits(p)); | |
130 | ||
b6a07f67 PP |
131 | if (verbose) |
132 | BIO_printf(bio_err, "Generating DSA key, %d bits\n", BN_num_bits(p)); | |
0f113f3e MC |
133 | if (!DSA_generate_key(dsa)) |
134 | goto end; | |
d02b48c6 | 135 | |
3b061a00 | 136 | assert(private); |
0f113f3e MC |
137 | if (!PEM_write_bio_DSAPrivateKey(out, dsa, enc, NULL, 0, NULL, passout)) |
138 | goto end; | |
139 | ret = 0; | |
140 | end: | |
141 | if (ret != 0) | |
142 | ERR_print_errors(bio_err); | |
7e1b7485 | 143 | end2: |
ca3a82c3 RS |
144 | BIO_free(in); |
145 | BIO_free_all(out); | |
d6407083 | 146 | DSA_free(dsa); |
dd1abd44 | 147 | release_engine(e); |
b548a1f1 | 148 | OPENSSL_free(passout); |
26a7d938 | 149 | return ret; |
0f113f3e | 150 | } |
f5d7a031 | 151 | #endif |