]>
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 | |
5daec7ea | 14 | |
0f113f3e MC |
15 | # include <stdio.h> |
16 | # include <stdlib.h> | |
17 | # include <time.h> | |
18 | # include <string.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> | |
27 | ||
b6a07f67 PP |
28 | static int verbose = 0; |
29 | ||
6d23cf97 | 30 | static int dsa_cb(int p, int n, BN_GENCB *cb); |
667ac4ec | 31 | |
7e1b7485 RS |
32 | typedef enum OPTION_choice { |
33 | OPT_ERR = -1, OPT_EOF = 0, OPT_HELP, | |
34 | OPT_INFORM, OPT_OUTFORM, OPT_IN, OPT_OUT, OPT_TEXT, OPT_C, | |
b6a07f67 PP |
35 | OPT_NOOUT, OPT_GENKEY, OPT_ENGINE, OPT_VERBOSE, |
36 | OPT_R_ENUM | |
7e1b7485 RS |
37 | } OPTION_CHOICE; |
38 | ||
44c83ebd | 39 | const OPTIONS dsaparam_options[] = { |
92de469f RS |
40 | {OPT_HELP_STR, 1, '-', "Usage: %s [options] [numbits]\n"}, |
41 | ||
5388f986 | 42 | OPT_SECTION("General"), |
7e1b7485 | 43 | {"help", OPT_HELP, '-', "Display this summary"}, |
5388f986 RS |
44 | # ifndef OPENSSL_NO_ENGINE |
45 | {"engine", OPT_ENGINE, 's', "Use engine e, possibly a hardware device"}, | |
46 | # endif | |
47 | ||
48 | OPT_SECTION("Input"), | |
7e1b7485 | 49 | {"in", OPT_IN, '<', "Input file"}, |
5388f986 RS |
50 | {"inform", OPT_INFORM, 'F', "Input format - DER or PEM"}, |
51 | ||
52 | OPT_SECTION("Output"), | |
7e1b7485 | 53 | {"out", OPT_OUT, '>', "Output file"}, |
5388f986 | 54 | {"outform", OPT_OUTFORM, 'F', "Output format - DER or PEM"}, |
7e1b7485 RS |
55 | {"text", OPT_TEXT, '-', "Print as text"}, |
56 | {"C", OPT_C, '-', "Output C code"}, | |
57 | {"noout", OPT_NOOUT, '-', "No output"}, | |
5388f986 | 58 | {"verbose", OPT_VERBOSE, '-', "Verbose output"}, |
7e1b7485 | 59 | {"genkey", OPT_GENKEY, '-', "Generate a DSA key"}, |
5388f986 | 60 | |
3ee1eac2 | 61 | OPT_R_OPTIONS, |
92de469f RS |
62 | |
63 | OPT_PARAMETERS(), | |
64 | {"numbits", 0, 0, "Number of bits if generating parameters (optional)"}, | |
7e1b7485 RS |
65 | {NULL} |
66 | }; | |
667ac4ec | 67 | |
7e1b7485 | 68 | int dsaparam_main(int argc, char **argv) |
0f113f3e | 69 | { |
dd1abd44 | 70 | ENGINE *e = NULL; |
0f113f3e | 71 | DSA *dsa = NULL; |
0f113f3e | 72 | BIO *in = NULL, *out = NULL; |
0f113f3e | 73 | BN_GENCB *cb = NULL; |
3ee1eac2 | 74 | int numbits = -1, num = 0, genkey = 0; |
3b061a00 RS |
75 | int informat = FORMAT_PEM, outformat = FORMAT_PEM, noout = 0, C = 0; |
76 | int ret = 1, i, text = 0, private = 0; | |
3ee1eac2 | 77 | char *infile = NULL, *outfile = NULL, *prog; |
7e1b7485 RS |
78 | OPTION_CHOICE o; |
79 | ||
80 | prog = opt_init(argc, argv, dsaparam_options); | |
81 | while ((o = opt_next()) != OPT_EOF) { | |
82 | switch (o) { | |
83 | case OPT_EOF: | |
84 | case OPT_ERR: | |
85 | opthelp: | |
86 | BIO_printf(bio_err, "%s: Use -help for summary.\n", prog); | |
87 | goto end; | |
88 | case OPT_HELP: | |
89 | opt_help(dsaparam_options); | |
90 | ret = 0; | |
91 | goto end; | |
92 | case OPT_INFORM: | |
93 | if (!opt_format(opt_arg(), OPT_FMT_PEMDER, &informat)) | |
94 | goto opthelp; | |
95 | break; | |
96 | case OPT_IN: | |
97 | infile = opt_arg(); | |
98 | break; | |
99 | case OPT_OUTFORM: | |
100 | if (!opt_format(opt_arg(), OPT_FMT_PEMDER, &outformat)) | |
101 | goto opthelp; | |
102 | break; | |
103 | case OPT_OUT: | |
104 | outfile = opt_arg(); | |
105 | break; | |
106 | case OPT_ENGINE: | |
dd1abd44 | 107 | e = setup_engine(opt_arg(), 0); |
7e1b7485 | 108 | break; |
7e1b7485 | 109 | case OPT_TEXT: |
0f113f3e | 110 | text = 1; |
7e1b7485 RS |
111 | break; |
112 | case OPT_C: | |
0f113f3e | 113 | C = 1; |
7e1b7485 RS |
114 | break; |
115 | case OPT_GENKEY: | |
3ee1eac2 | 116 | genkey = 1; |
7e1b7485 | 117 | break; |
3ee1eac2 RS |
118 | case OPT_R_CASES: |
119 | if (!opt_rand(o)) | |
120 | goto end; | |
7e1b7485 RS |
121 | break; |
122 | case OPT_NOOUT: | |
0f113f3e | 123 | noout = 1; |
7e1b7485 | 124 | break; |
b6a07f67 PP |
125 | case OPT_VERBOSE: |
126 | verbose = 1; | |
127 | break; | |
0f113f3e | 128 | } |
0f113f3e | 129 | } |
7e1b7485 RS |
130 | argc = opt_num_rest(); |
131 | argv = opt_rest(); | |
0f113f3e | 132 | |
7e1b7485 | 133 | if (argc == 1) { |
bd4850df | 134 | if (!opt_int(argv[0], &num) || num < 0) |
7e1b7485 RS |
135 | goto end; |
136 | /* generate a key */ | |
137 | numbits = num; | |
0f113f3e | 138 | } |
3b061a00 | 139 | private = genkey ? 1 : 0; |
0f113f3e | 140 | |
bdd58d98 | 141 | in = bio_open_default(infile, 'r', informat); |
7e1b7485 RS |
142 | if (in == NULL) |
143 | goto end; | |
bdd58d98 | 144 | out = bio_open_owner(outfile, outformat, private); |
7e1b7485 | 145 | if (out == NULL) |
0f113f3e | 146 | goto end; |
0f113f3e | 147 | |
0f113f3e | 148 | if (numbits > 0) { |
0336df2f GS |
149 | if (numbits > OPENSSL_DSA_MAX_MODULUS_BITS) |
150 | BIO_printf(bio_err, | |
151 | "Warning: It is not recommended to use more than %d bit for DSA keys.\n" | |
152 | " Your key size is %d! Larger key size may behave not as expected.\n", | |
153 | OPENSSL_DSA_MAX_MODULUS_BITS, numbits); | |
154 | ||
0f113f3e | 155 | cb = BN_GENCB_new(); |
96487cdd | 156 | if (cb == NULL) { |
0f113f3e MC |
157 | BIO_printf(bio_err, "Error allocating BN_GENCB object\n"); |
158 | goto end; | |
159 | } | |
160 | BN_GENCB_set(cb, dsa_cb, bio_err); | |
0f113f3e | 161 | dsa = DSA_new(); |
96487cdd | 162 | if (dsa == NULL) { |
0f113f3e MC |
163 | BIO_printf(bio_err, "Error allocating DSA object\n"); |
164 | goto end; | |
165 | } | |
b6a07f67 PP |
166 | if (verbose) { |
167 | BIO_printf(bio_err, "Generating DSA parameters, %d bit long prime\n", | |
168 | num); | |
169 | BIO_printf(bio_err, "This could take some time\n"); | |
170 | } | |
0f113f3e | 171 | if (!DSA_generate_parameters_ex(dsa, num, NULL, 0, NULL, NULL, cb)) { |
0f113f3e MC |
172 | ERR_print_errors(bio_err); |
173 | BIO_printf(bio_err, "Error, DSA key generation failed\n"); | |
174 | goto end; | |
175 | } | |
2234212c | 176 | } else if (informat == FORMAT_ASN1) { |
0f113f3e | 177 | dsa = d2i_DSAparams_bio(in, NULL); |
2234212c | 178 | } else { |
0f113f3e | 179 | dsa = PEM_read_bio_DSAparams(in, NULL, NULL, NULL); |
2234212c | 180 | } |
0f113f3e MC |
181 | if (dsa == NULL) { |
182 | BIO_printf(bio_err, "unable to load DSA parameters\n"); | |
183 | ERR_print_errors(bio_err); | |
184 | goto end; | |
185 | } | |
186 | ||
187 | if (text) { | |
188 | DSAparams_print(out, dsa); | |
189 | } | |
190 | ||
191 | if (C) { | |
2ac6115d | 192 | const BIGNUM *p = NULL, *q = NULL, *g = NULL; |
ae6c553e | 193 | unsigned char *data; |
6e9fa57c MC |
194 | int len, bits_p; |
195 | ||
196 | DSA_get0_pqg(dsa, &p, &q, &g); | |
197 | len = BN_num_bytes(p); | |
198 | bits_p = BN_num_bits(p); | |
199 | ||
ae6c553e | 200 | data = app_malloc(len + 20, "BN space"); |
0f113f3e | 201 | |
d6b50b6e | 202 | BIO_printf(bio_out, "static DSA *get_dsa%d(void)\n{\n", bits_p); |
708c28f2 AP |
203 | print_bignum_var(bio_out, p, "dsap", bits_p, data); |
204 | print_bignum_var(bio_out, q, "dsaq", bits_p, data); | |
205 | print_bignum_var(bio_out, g, "dsag", bits_p, data); | |
7e1b7485 | 206 | BIO_printf(bio_out, " DSA *dsa = DSA_new();\n" |
708c28f2 | 207 | " BIGNUM *p, *q, *g;\n" |
7e1b7485 RS |
208 | "\n"); |
209 | BIO_printf(bio_out, " if (dsa == NULL)\n" | |
210 | " return NULL;\n"); | |
708c28f2 AP |
211 | BIO_printf(bio_out, " if (!DSA_set0_pqg(dsa, p = BN_bin2bn(dsap_%d, sizeof(dsap_%d), NULL),\n", |
212 | bits_p, bits_p); | |
213 | BIO_printf(bio_out, " q = BN_bin2bn(dsaq_%d, sizeof(dsaq_%d), NULL),\n", | |
214 | bits_p, bits_p); | |
215 | BIO_printf(bio_out, " g = BN_bin2bn(dsag_%d, sizeof(dsag_%d), NULL))) {\n", | |
216 | bits_p, bits_p); | |
217 | BIO_printf(bio_out, " DSA_free(dsa);\n" | |
218 | " BN_free(p);\n" | |
219 | " BN_free(q);\n" | |
220 | " BN_free(g);\n" | |
7e1b7485 RS |
221 | " return NULL;\n" |
222 | " }\n" | |
708c28f2 | 223 | " return dsa;\n}\n"); |
a855d1a1 | 224 | OPENSSL_free(data); |
0f113f3e MC |
225 | } |
226 | ||
52814352 BE |
227 | if (outformat == FORMAT_ASN1 && genkey) |
228 | noout = 1; | |
229 | ||
0f113f3e MC |
230 | if (!noout) { |
231 | if (outformat == FORMAT_ASN1) | |
232 | i = i2d_DSAparams_bio(out, dsa); | |
7e1b7485 | 233 | else |
0f113f3e | 234 | i = PEM_write_bio_DSAparams(out, dsa); |
0f113f3e MC |
235 | if (!i) { |
236 | BIO_printf(bio_err, "unable to write DSA parameters\n"); | |
237 | ERR_print_errors(bio_err); | |
238 | goto end; | |
239 | } | |
240 | } | |
241 | if (genkey) { | |
242 | DSA *dsakey; | |
243 | ||
0f113f3e MC |
244 | if ((dsakey = DSAparams_dup(dsa)) == NULL) |
245 | goto end; | |
0f113f3e MC |
246 | if (!DSA_generate_key(dsakey)) { |
247 | ERR_print_errors(bio_err); | |
248 | DSA_free(dsakey); | |
249 | goto end; | |
250 | } | |
3b061a00 | 251 | assert(private); |
0f113f3e MC |
252 | if (outformat == FORMAT_ASN1) |
253 | i = i2d_DSAPrivateKey_bio(out, dsakey); | |
7e1b7485 | 254 | else |
0f113f3e MC |
255 | i = PEM_write_bio_DSAPrivateKey(out, dsakey, NULL, NULL, 0, NULL, |
256 | NULL); | |
0f113f3e MC |
257 | DSA_free(dsakey); |
258 | } | |
0f113f3e MC |
259 | ret = 0; |
260 | end: | |
23a1d5e9 | 261 | BN_GENCB_free(cb); |
ca3a82c3 RS |
262 | BIO_free(in); |
263 | BIO_free_all(out); | |
d6407083 | 264 | DSA_free(dsa); |
dd1abd44 | 265 | release_engine(e); |
26a7d938 | 266 | return ret; |
0f113f3e | 267 | } |
d02b48c6 | 268 | |
6d23cf97 | 269 | static int dsa_cb(int p, int n, BN_GENCB *cb) |
0f113f3e | 270 | { |
201b305a BB |
271 | static const char symbols[] = ".+*\n"; |
272 | char c = (p >= 0 && (size_t)p < sizeof(symbols) - 1) ? symbols[p] : '?'; | |
0f113f3e | 273 | |
b6a07f67 PP |
274 | if (!verbose) |
275 | return 1; | |
276 | ||
0f113f3e MC |
277 | BIO_write(BN_GENCB_get_arg(cb), &c, 1); |
278 | (void)BIO_flush(BN_GENCB_get_arg(cb)); | |
0f113f3e MC |
279 | return 1; |
280 | } | |
f5d7a031 | 281 | #endif |