]>
Commit | Line | Data |
---|---|---|
58964a49 | 1 | /* Copyright (C) 1995-1998 Eric Young (eay@cryptsoft.com) |
d02b48c6 RE |
2 | * All rights reserved. |
3 | * | |
4 | * This package is an SSL implementation written | |
5 | * by Eric Young (eay@cryptsoft.com). | |
6 | * The implementation was written so as to conform with Netscapes SSL. | |
0f113f3e | 7 | * |
d02b48c6 RE |
8 | * This library is free for commercial and non-commercial use as long as |
9 | * the following conditions are aheared to. The following conditions | |
10 | * apply to all code found in this distribution, be it the RC4, RSA, | |
11 | * lhash, DES, etc., code; not just the SSL code. The SSL documentation | |
12 | * included with this distribution is covered by the same copyright terms | |
13 | * except that the holder is Tim Hudson (tjh@cryptsoft.com). | |
0f113f3e | 14 | * |
d02b48c6 RE |
15 | * Copyright remains Eric Young's, and as such any Copyright notices in |
16 | * the code are not to be removed. | |
17 | * If this package is used in a product, Eric Young should be given attribution | |
18 | * as the author of the parts of the library used. | |
19 | * This can be in the form of a textual message at program startup or | |
20 | * in documentation (online or textual) provided with the package. | |
0f113f3e | 21 | * |
d02b48c6 RE |
22 | * Redistribution and use in source and binary forms, with or without |
23 | * modification, are permitted provided that the following conditions | |
24 | * are met: | |
25 | * 1. Redistributions of source code must retain the copyright | |
26 | * notice, this list of conditions and the following disclaimer. | |
27 | * 2. Redistributions in binary form must reproduce the above copyright | |
28 | * notice, this list of conditions and the following disclaimer in the | |
29 | * documentation and/or other materials provided with the distribution. | |
30 | * 3. All advertising materials mentioning features or use of this software | |
31 | * must display the following acknowledgement: | |
32 | * "This product includes cryptographic software written by | |
33 | * Eric Young (eay@cryptsoft.com)" | |
34 | * The word 'cryptographic' can be left out if the rouines from the library | |
35 | * being used are not cryptographic related :-). | |
0f113f3e | 36 | * 4. If you include any Windows specific code (or a derivative thereof) from |
d02b48c6 RE |
37 | * the apps directory (application code) you must include an acknowledgement: |
38 | * "This product includes software written by Tim Hudson (tjh@cryptsoft.com)" | |
0f113f3e | 39 | * |
d02b48c6 RE |
40 | * THIS SOFTWARE IS PROVIDED BY ERIC YOUNG ``AS IS'' AND |
41 | * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE | |
42 | * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE | |
43 | * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE | |
44 | * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL | |
45 | * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS | |
46 | * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) | |
47 | * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT | |
48 | * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY | |
49 | * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF | |
50 | * SUCH DAMAGE. | |
0f113f3e | 51 | * |
d02b48c6 RE |
52 | * The licence and distribution terms for any publically available version or |
53 | * derivative of this code cannot be changed. i.e. this code cannot simply be | |
54 | * copied and put under another distribution licence | |
55 | * [including the GNU Public Licence.] | |
56 | */ | |
57 | ||
effaf4de RS |
58 | #include <openssl/opensslconf.h> |
59 | #ifdef OPENSSL_NO_DSA | |
60 | NON_EMPTY_TRANSLATION_UNIT | |
61 | #else | |
5daec7ea | 62 | |
0f113f3e MC |
63 | # include <stdio.h> |
64 | # include <stdlib.h> | |
65 | # include <time.h> | |
66 | # include <string.h> | |
67 | # include "apps.h" | |
68 | # include <openssl/bio.h> | |
69 | # include <openssl/err.h> | |
70 | # include <openssl/bn.h> | |
71 | # include <openssl/dsa.h> | |
72 | # include <openssl/x509.h> | |
73 | # include <openssl/pem.h> | |
74 | ||
0f113f3e | 75 | # ifdef GENCB_TEST |
5daec7ea GT |
76 | |
77 | static int stop_keygen_flag = 0; | |
78 | ||
f92570f0 | 79 | static void timebomb_sigalarm(int foo) |
0f113f3e MC |
80 | { |
81 | stop_keygen_flag = 1; | |
82 | } | |
5daec7ea | 83 | |
0f113f3e | 84 | # endif |
5daec7ea | 85 | |
6d23cf97 | 86 | static int dsa_cb(int p, int n, BN_GENCB *cb); |
667ac4ec | 87 | |
7e1b7485 RS |
88 | typedef enum OPTION_choice { |
89 | OPT_ERR = -1, OPT_EOF = 0, OPT_HELP, | |
90 | OPT_INFORM, OPT_OUTFORM, OPT_IN, OPT_OUT, OPT_TEXT, OPT_C, | |
700b4a4a | 91 | OPT_NOOUT, OPT_GENKEY, OPT_RAND, OPT_ENGINE, |
7e1b7485 RS |
92 | OPT_TIMEBOMB |
93 | } OPTION_CHOICE; | |
94 | ||
95 | OPTIONS dsaparam_options[] = { | |
96 | {"help", OPT_HELP, '-', "Display this summary"}, | |
97 | {"inform", OPT_INFORM, 'F', "Input format - DER or PEM"}, | |
98 | {"in", OPT_IN, '<', "Input file"}, | |
99 | {"outform", OPT_OUTFORM, 'F', "Output format - DER or PEM"}, | |
100 | {"out", OPT_OUT, '>', "Output file"}, | |
101 | {"text", OPT_TEXT, '-', "Print as text"}, | |
102 | {"C", OPT_C, '-', "Output C code"}, | |
103 | {"noout", OPT_NOOUT, '-', "No output"}, | |
104 | {"genkey", OPT_GENKEY, '-', "Generate a DSA key"}, | |
105 | {"rand", OPT_RAND, 's', "Files to use for random number input"}, | |
7e1b7485 RS |
106 | # ifdef GENCB_TEST |
107 | {"timebomb", OPT_TIMEBOMB, 'p', "Interrupt keygen after 'pnum' seconds"}, | |
9c3bcfa0 RS |
108 | # endif |
109 | # ifndef OPENSSL_NO_ENGINE | |
110 | {"engine", OPT_ENGINE, 's', "Use engine e, possibly a hardware device"}, | |
7e1b7485 RS |
111 | # endif |
112 | {NULL} | |
113 | }; | |
667ac4ec | 114 | |
7e1b7485 | 115 | int dsaparam_main(int argc, char **argv) |
0f113f3e MC |
116 | { |
117 | DSA *dsa = NULL; | |
0f113f3e | 118 | BIO *in = NULL, *out = NULL; |
0f113f3e | 119 | BN_GENCB *cb = NULL; |
700b4a4a | 120 | int numbits = -1, num = 0, genkey = 0, need_rand = 0; |
3b061a00 RS |
121 | int informat = FORMAT_PEM, outformat = FORMAT_PEM, noout = 0, C = 0; |
122 | int ret = 1, i, text = 0, private = 0; | |
0f113f3e MC |
123 | # ifdef GENCB_TEST |
124 | int timebomb = 0; | |
125 | # endif | |
333b070e | 126 | char *infile = NULL, *outfile = NULL, *prog, *inrand = NULL; |
7e1b7485 RS |
127 | OPTION_CHOICE o; |
128 | ||
129 | prog = opt_init(argc, argv, dsaparam_options); | |
130 | while ((o = opt_next()) != OPT_EOF) { | |
131 | switch (o) { | |
132 | case OPT_EOF: | |
133 | case OPT_ERR: | |
134 | opthelp: | |
135 | BIO_printf(bio_err, "%s: Use -help for summary.\n", prog); | |
136 | goto end; | |
137 | case OPT_HELP: | |
138 | opt_help(dsaparam_options); | |
139 | ret = 0; | |
140 | goto end; | |
141 | case OPT_INFORM: | |
142 | if (!opt_format(opt_arg(), OPT_FMT_PEMDER, &informat)) | |
143 | goto opthelp; | |
144 | break; | |
145 | case OPT_IN: | |
146 | infile = opt_arg(); | |
147 | break; | |
148 | case OPT_OUTFORM: | |
149 | if (!opt_format(opt_arg(), OPT_FMT_PEMDER, &outformat)) | |
150 | goto opthelp; | |
151 | break; | |
152 | case OPT_OUT: | |
153 | outfile = opt_arg(); | |
154 | break; | |
155 | case OPT_ENGINE: | |
333b070e | 156 | (void)setup_engine(opt_arg(), 0); |
7e1b7485 RS |
157 | break; |
158 | case OPT_TIMEBOMB: | |
0f113f3e | 159 | # ifdef GENCB_TEST |
7e1b7485 RS |
160 | timebomb = atoi(opt_arg()); |
161 | break; | |
0f113f3e | 162 | # endif |
7e1b7485 | 163 | case OPT_TEXT: |
0f113f3e | 164 | text = 1; |
7e1b7485 RS |
165 | break; |
166 | case OPT_C: | |
0f113f3e | 167 | C = 1; |
7e1b7485 RS |
168 | break; |
169 | case OPT_GENKEY: | |
170 | genkey = need_rand = 1; | |
171 | break; | |
172 | case OPT_RAND: | |
173 | inrand = opt_arg(); | |
0f113f3e | 174 | need_rand = 1; |
7e1b7485 RS |
175 | break; |
176 | case OPT_NOOUT: | |
0f113f3e | 177 | noout = 1; |
7e1b7485 | 178 | break; |
0f113f3e | 179 | } |
0f113f3e | 180 | } |
7e1b7485 RS |
181 | argc = opt_num_rest(); |
182 | argv = opt_rest(); | |
0f113f3e | 183 | |
7e1b7485 | 184 | if (argc == 1) { |
bd4850df | 185 | if (!opt_int(argv[0], &num) || num < 0) |
7e1b7485 RS |
186 | goto end; |
187 | /* generate a key */ | |
188 | numbits = num; | |
189 | need_rand = 1; | |
0f113f3e | 190 | } |
3b061a00 | 191 | private = genkey ? 1 : 0; |
0f113f3e | 192 | |
bdd58d98 | 193 | in = bio_open_default(infile, 'r', informat); |
7e1b7485 RS |
194 | if (in == NULL) |
195 | goto end; | |
bdd58d98 | 196 | out = bio_open_owner(outfile, outformat, private); |
7e1b7485 | 197 | if (out == NULL) |
0f113f3e | 198 | goto end; |
0f113f3e | 199 | |
0f113f3e | 200 | if (need_rand) { |
7e1b7485 | 201 | app_RAND_load_file(NULL, (inrand != NULL)); |
0f113f3e MC |
202 | if (inrand != NULL) |
203 | BIO_printf(bio_err, "%ld semi-random bytes loaded\n", | |
204 | app_RAND_load_files(inrand)); | |
205 | } | |
206 | ||
207 | if (numbits > 0) { | |
208 | cb = BN_GENCB_new(); | |
96487cdd | 209 | if (cb == NULL) { |
0f113f3e MC |
210 | BIO_printf(bio_err, "Error allocating BN_GENCB object\n"); |
211 | goto end; | |
212 | } | |
213 | BN_GENCB_set(cb, dsa_cb, bio_err); | |
214 | assert(need_rand); | |
215 | dsa = DSA_new(); | |
96487cdd | 216 | if (dsa == NULL) { |
0f113f3e MC |
217 | BIO_printf(bio_err, "Error allocating DSA object\n"); |
218 | goto end; | |
219 | } | |
0f113f3e MC |
220 | BIO_printf(bio_err, "Generating DSA parameters, %d bit long prime\n", |
221 | num); | |
222 | BIO_printf(bio_err, "This could take some time\n"); | |
223 | # ifdef GENCB_TEST | |
224 | if (timebomb > 0) { | |
225 | struct sigaction act; | |
226 | act.sa_handler = timebomb_sigalarm; | |
227 | act.sa_flags = 0; | |
228 | BIO_printf(bio_err, | |
229 | "(though I'll stop it if not done within %d secs)\n", | |
230 | timebomb); | |
231 | if (sigaction(SIGALRM, &act, NULL) != 0) { | |
232 | BIO_printf(bio_err, "Error, couldn't set SIGALRM handler\n"); | |
233 | goto end; | |
234 | } | |
235 | alarm(timebomb); | |
236 | } | |
237 | # endif | |
238 | if (!DSA_generate_parameters_ex(dsa, num, NULL, 0, NULL, NULL, cb)) { | |
239 | # ifdef GENCB_TEST | |
240 | if (stop_keygen_flag) { | |
241 | BIO_printf(bio_err, "DSA key generation time-stopped\n"); | |
242 | /* This is an asked-for behaviour! */ | |
243 | ret = 0; | |
244 | goto end; | |
245 | } | |
246 | # endif | |
247 | ERR_print_errors(bio_err); | |
248 | BIO_printf(bio_err, "Error, DSA key generation failed\n"); | |
249 | goto end; | |
250 | } | |
251 | } else if (informat == FORMAT_ASN1) | |
252 | dsa = d2i_DSAparams_bio(in, NULL); | |
7e1b7485 | 253 | else |
0f113f3e | 254 | dsa = PEM_read_bio_DSAparams(in, NULL, NULL, NULL); |
0f113f3e MC |
255 | if (dsa == NULL) { |
256 | BIO_printf(bio_err, "unable to load DSA parameters\n"); | |
257 | ERR_print_errors(bio_err); | |
258 | goto end; | |
259 | } | |
260 | ||
261 | if (text) { | |
262 | DSAparams_print(out, dsa); | |
263 | } | |
264 | ||
265 | if (C) { | |
68dc6824 RS |
266 | int len = BN_num_bytes(dsa->p); |
267 | int bits_p = BN_num_bits(dsa->p); | |
268 | unsigned char *data = app_malloc(len + 20, "BN space"); | |
0f113f3e | 269 | |
7e1b7485 RS |
270 | BIO_printf(bio_out, "DSA *get_dsa%d()\n{\n", bits_p); |
271 | print_bignum_var(bio_out, dsa->p, "dsap", len, data); | |
272 | print_bignum_var(bio_out, dsa->q, "dsaq", len, data); | |
273 | print_bignum_var(bio_out, dsa->g, "dsag", len, data); | |
274 | BIO_printf(bio_out, " DSA *dsa = DSA_new();\n" | |
275 | "\n"); | |
276 | BIO_printf(bio_out, " if (dsa == NULL)\n" | |
277 | " return NULL;\n"); | |
278 | BIO_printf(bio_out, " dsa->p = BN_bin2bn(dsap_%d, sizeof (dsap_%d), NULL);\n", | |
0f113f3e | 279 | bits_p, bits_p); |
7e1b7485 | 280 | BIO_printf(bio_out, " dsa->q = BN_bin2bn(dsaq_%d, sizeof (dsaq_%d), NULL);\n", |
0f113f3e | 281 | bits_p, bits_p); |
7e1b7485 | 282 | BIO_printf(bio_out, " dsa->g = BN_bin2bn(dsag_%d, sizeof (dsag_%d), NULL);\n", |
0f113f3e | 283 | bits_p, bits_p); |
7e1b7485 RS |
284 | BIO_printf(bio_out, " if (!dsa->p || !dsa->q || !dsa->g) {\n" |
285 | " DSA_free(dsa);\n" | |
286 | " return NULL;\n" | |
287 | " }\n" | |
288 | " return(dsa);\n}\n"); | |
0f113f3e MC |
289 | } |
290 | ||
291 | if (!noout) { | |
292 | if (outformat == FORMAT_ASN1) | |
293 | i = i2d_DSAparams_bio(out, dsa); | |
7e1b7485 | 294 | else |
0f113f3e | 295 | i = PEM_write_bio_DSAparams(out, dsa); |
0f113f3e MC |
296 | if (!i) { |
297 | BIO_printf(bio_err, "unable to write DSA parameters\n"); | |
298 | ERR_print_errors(bio_err); | |
299 | goto end; | |
300 | } | |
301 | } | |
302 | if (genkey) { | |
303 | DSA *dsakey; | |
304 | ||
305 | assert(need_rand); | |
306 | if ((dsakey = DSAparams_dup(dsa)) == NULL) | |
307 | goto end; | |
0f113f3e MC |
308 | if (!DSA_generate_key(dsakey)) { |
309 | ERR_print_errors(bio_err); | |
310 | DSA_free(dsakey); | |
311 | goto end; | |
312 | } | |
3b061a00 | 313 | assert(private); |
0f113f3e MC |
314 | if (outformat == FORMAT_ASN1) |
315 | i = i2d_DSAPrivateKey_bio(out, dsakey); | |
7e1b7485 | 316 | else |
0f113f3e MC |
317 | i = PEM_write_bio_DSAPrivateKey(out, dsakey, NULL, NULL, 0, NULL, |
318 | NULL); | |
0f113f3e MC |
319 | DSA_free(dsakey); |
320 | } | |
321 | if (need_rand) | |
7e1b7485 | 322 | app_RAND_write_file(NULL); |
0f113f3e MC |
323 | ret = 0; |
324 | end: | |
23a1d5e9 | 325 | BN_GENCB_free(cb); |
ca3a82c3 RS |
326 | BIO_free(in); |
327 | BIO_free_all(out); | |
d6407083 | 328 | DSA_free(dsa); |
7e1b7485 | 329 | return (ret); |
0f113f3e | 330 | } |
d02b48c6 | 331 | |
6d23cf97 | 332 | static int dsa_cb(int p, int n, BN_GENCB *cb) |
0f113f3e MC |
333 | { |
334 | char c = '*'; | |
335 | ||
336 | if (p == 0) | |
337 | c = '.'; | |
338 | if (p == 1) | |
339 | c = '+'; | |
340 | if (p == 2) | |
341 | c = '*'; | |
342 | if (p == 3) | |
343 | c = '\n'; | |
344 | BIO_write(BN_GENCB_get_arg(cb), &c, 1); | |
345 | (void)BIO_flush(BN_GENCB_get_arg(cb)); | |
346 | # ifdef GENCB_TEST | |
347 | if (stop_keygen_flag) | |
348 | return 0; | |
349 | # endif | |
350 | return 1; | |
351 | } | |
f5d7a031 | 352 | #endif |