]>
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 | ||
0f113f3e | 58 | #include <openssl/opensslconf.h> /* for OPENSSL_NO_DSA */ |
5daec7ea | 59 | |
cf1b7d96 | 60 | #ifndef OPENSSL_NO_DSA |
0f113f3e MC |
61 | # include <assert.h> |
62 | # include <stdio.h> | |
63 | # include <stdlib.h> | |
64 | # include <time.h> | |
65 | # include <string.h> | |
66 | # include "apps.h" | |
67 | # include <openssl/bio.h> | |
68 | # include <openssl/err.h> | |
69 | # include <openssl/bn.h> | |
70 | # include <openssl/dsa.h> | |
71 | # include <openssl/x509.h> | |
72 | # include <openssl/pem.h> | |
73 | ||
0f113f3e | 74 | # ifdef GENCB_TEST |
5daec7ea GT |
75 | |
76 | static int stop_keygen_flag = 0; | |
77 | ||
f92570f0 | 78 | static void timebomb_sigalarm(int foo) |
0f113f3e MC |
79 | { |
80 | stop_keygen_flag = 1; | |
81 | } | |
5daec7ea | 82 | |
0f113f3e | 83 | # endif |
5daec7ea | 84 | |
6d23cf97 | 85 | static int dsa_cb(int p, int n, BN_GENCB *cb); |
667ac4ec | 86 | |
7e1b7485 RS |
87 | typedef enum OPTION_choice { |
88 | OPT_ERR = -1, OPT_EOF = 0, OPT_HELP, | |
89 | OPT_INFORM, OPT_OUTFORM, OPT_IN, OPT_OUT, OPT_TEXT, OPT_C, | |
90 | OPT_NOOUT, OPT_GENKEY, OPT_RAND, OPT_NON_FIPS_ALLOW, OPT_ENGINE, | |
91 | OPT_TIMEBOMB | |
92 | } OPTION_CHOICE; | |
93 | ||
94 | OPTIONS dsaparam_options[] = { | |
95 | {"help", OPT_HELP, '-', "Display this summary"}, | |
96 | {"inform", OPT_INFORM, 'F', "Input format - DER or PEM"}, | |
97 | {"in", OPT_IN, '<', "Input file"}, | |
98 | {"outform", OPT_OUTFORM, 'F', "Output format - DER or PEM"}, | |
99 | {"out", OPT_OUT, '>', "Output file"}, | |
100 | {"text", OPT_TEXT, '-', "Print as text"}, | |
101 | {"C", OPT_C, '-', "Output C code"}, | |
102 | {"noout", OPT_NOOUT, '-', "No output"}, | |
103 | {"genkey", OPT_GENKEY, '-', "Generate a DSA key"}, | |
104 | {"rand", OPT_RAND, 's', "Files to use for random number input"}, | |
105 | {"non-fips-allow", OPT_NON_FIPS_ALLOW, '-'}, | |
106 | # ifndef OPENSSL_NO_ENGINE | |
107 | {"engine", OPT_ENGINE, 's', "Use engine e, possibly a hardware device"}, | |
108 | # endif | |
109 | # ifdef GENCB_TEST | |
110 | {"timebomb", OPT_TIMEBOMB, 'p', "Interrupt keygen after 'pnum' seconds"}, | |
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; |
4c9b0a03 | 120 | int numbits = -1, num = 0, genkey = 0, need_rand = 0, non_fips_allow = 0; |
7e1b7485 RS |
121 | int informat = FORMAT_PEM, outformat = FORMAT_PEM, noout = 0, C = 0, ret = |
122 | 1; | |
123 | int i, text = 0; | |
0f113f3e MC |
124 | # ifdef GENCB_TEST |
125 | int timebomb = 0; | |
126 | # endif | |
333b070e | 127 | char *infile = NULL, *outfile = NULL, *prog, *inrand = NULL; |
7e1b7485 RS |
128 | OPTION_CHOICE o; |
129 | ||
130 | prog = opt_init(argc, argv, dsaparam_options); | |
131 | while ((o = opt_next()) != OPT_EOF) { | |
132 | switch (o) { | |
133 | case OPT_EOF: | |
134 | case OPT_ERR: | |
135 | opthelp: | |
136 | BIO_printf(bio_err, "%s: Use -help for summary.\n", prog); | |
137 | goto end; | |
138 | case OPT_HELP: | |
139 | opt_help(dsaparam_options); | |
140 | ret = 0; | |
141 | goto end; | |
142 | case OPT_INFORM: | |
143 | if (!opt_format(opt_arg(), OPT_FMT_PEMDER, &informat)) | |
144 | goto opthelp; | |
145 | break; | |
146 | case OPT_IN: | |
147 | infile = opt_arg(); | |
148 | break; | |
149 | case OPT_OUTFORM: | |
150 | if (!opt_format(opt_arg(), OPT_FMT_PEMDER, &outformat)) | |
151 | goto opthelp; | |
152 | break; | |
153 | case OPT_OUT: | |
154 | outfile = opt_arg(); | |
155 | break; | |
156 | case OPT_ENGINE: | |
333b070e | 157 | (void)setup_engine(opt_arg(), 0); |
7e1b7485 RS |
158 | break; |
159 | case OPT_TIMEBOMB: | |
0f113f3e | 160 | # ifdef GENCB_TEST |
7e1b7485 RS |
161 | timebomb = atoi(opt_arg()); |
162 | break; | |
0f113f3e | 163 | # endif |
7e1b7485 | 164 | case OPT_TEXT: |
0f113f3e | 165 | text = 1; |
7e1b7485 RS |
166 | break; |
167 | case OPT_C: | |
0f113f3e | 168 | C = 1; |
7e1b7485 RS |
169 | break; |
170 | case OPT_GENKEY: | |
171 | genkey = need_rand = 1; | |
172 | break; | |
173 | case OPT_RAND: | |
174 | inrand = opt_arg(); | |
0f113f3e | 175 | need_rand = 1; |
7e1b7485 RS |
176 | break; |
177 | case OPT_NOOUT: | |
0f113f3e | 178 | noout = 1; |
7e1b7485 RS |
179 | break; |
180 | case OPT_NON_FIPS_ALLOW: | |
0f113f3e | 181 | non_fips_allow = 1; |
0f113f3e MC |
182 | break; |
183 | } | |
0f113f3e | 184 | } |
7e1b7485 RS |
185 | argc = opt_num_rest(); |
186 | argv = opt_rest(); | |
0f113f3e | 187 | |
7e1b7485 RS |
188 | if (argc == 1) { |
189 | if (!opt_int(argv[0], &num)) | |
190 | goto end; | |
191 | /* generate a key */ | |
192 | numbits = num; | |
193 | need_rand = 1; | |
0f113f3e MC |
194 | } |
195 | ||
7e1b7485 RS |
196 | in = bio_open_default(infile, "r"); |
197 | if (in == NULL) | |
198 | goto end; | |
199 | out = bio_open_default(outfile, "w"); | |
200 | if (out == NULL) | |
0f113f3e | 201 | goto end; |
0f113f3e | 202 | |
0f113f3e | 203 | if (need_rand) { |
7e1b7485 | 204 | app_RAND_load_file(NULL, (inrand != NULL)); |
0f113f3e MC |
205 | if (inrand != NULL) |
206 | BIO_printf(bio_err, "%ld semi-random bytes loaded\n", | |
207 | app_RAND_load_files(inrand)); | |
208 | } | |
209 | ||
210 | if (numbits > 0) { | |
211 | cb = BN_GENCB_new(); | |
212 | if (!cb) { | |
213 | BIO_printf(bio_err, "Error allocating BN_GENCB object\n"); | |
214 | goto end; | |
215 | } | |
216 | BN_GENCB_set(cb, dsa_cb, bio_err); | |
217 | assert(need_rand); | |
218 | dsa = DSA_new(); | |
219 | if (!dsa) { | |
220 | BIO_printf(bio_err, "Error allocating DSA object\n"); | |
221 | goto end; | |
222 | } | |
223 | if (non_fips_allow) | |
224 | dsa->flags |= DSA_FLAG_NON_FIPS_ALLOW; | |
225 | BIO_printf(bio_err, "Generating DSA parameters, %d bit long prime\n", | |
226 | num); | |
227 | BIO_printf(bio_err, "This could take some time\n"); | |
228 | # ifdef GENCB_TEST | |
229 | if (timebomb > 0) { | |
230 | struct sigaction act; | |
231 | act.sa_handler = timebomb_sigalarm; | |
232 | act.sa_flags = 0; | |
233 | BIO_printf(bio_err, | |
234 | "(though I'll stop it if not done within %d secs)\n", | |
235 | timebomb); | |
236 | if (sigaction(SIGALRM, &act, NULL) != 0) { | |
237 | BIO_printf(bio_err, "Error, couldn't set SIGALRM handler\n"); | |
238 | goto end; | |
239 | } | |
240 | alarm(timebomb); | |
241 | } | |
242 | # endif | |
243 | if (!DSA_generate_parameters_ex(dsa, num, NULL, 0, NULL, NULL, cb)) { | |
244 | # ifdef GENCB_TEST | |
245 | if (stop_keygen_flag) { | |
246 | BIO_printf(bio_err, "DSA key generation time-stopped\n"); | |
247 | /* This is an asked-for behaviour! */ | |
248 | ret = 0; | |
249 | goto end; | |
250 | } | |
251 | # endif | |
252 | ERR_print_errors(bio_err); | |
253 | BIO_printf(bio_err, "Error, DSA key generation failed\n"); | |
254 | goto end; | |
255 | } | |
256 | } else if (informat == FORMAT_ASN1) | |
257 | dsa = d2i_DSAparams_bio(in, NULL); | |
7e1b7485 | 258 | else |
0f113f3e | 259 | dsa = PEM_read_bio_DSAparams(in, NULL, NULL, NULL); |
0f113f3e MC |
260 | if (dsa == NULL) { |
261 | BIO_printf(bio_err, "unable to load DSA parameters\n"); | |
262 | ERR_print_errors(bio_err); | |
263 | goto end; | |
264 | } | |
265 | ||
266 | if (text) { | |
267 | DSAparams_print(out, dsa); | |
268 | } | |
269 | ||
270 | if (C) { | |
68dc6824 RS |
271 | int len = BN_num_bytes(dsa->p); |
272 | int bits_p = BN_num_bits(dsa->p); | |
273 | unsigned char *data = app_malloc(len + 20, "BN space"); | |
0f113f3e | 274 | |
7e1b7485 RS |
275 | BIO_printf(bio_out, "DSA *get_dsa%d()\n{\n", bits_p); |
276 | print_bignum_var(bio_out, dsa->p, "dsap", len, data); | |
277 | print_bignum_var(bio_out, dsa->q, "dsaq", len, data); | |
278 | print_bignum_var(bio_out, dsa->g, "dsag", len, data); | |
279 | BIO_printf(bio_out, " DSA *dsa = DSA_new();\n" | |
280 | "\n"); | |
281 | BIO_printf(bio_out, " if (dsa == NULL)\n" | |
282 | " return NULL;\n"); | |
283 | BIO_printf(bio_out, " dsa->p = BN_bin2bn(dsap_%d, sizeof (dsap_%d), NULL);\n", | |
0f113f3e | 284 | bits_p, bits_p); |
7e1b7485 | 285 | BIO_printf(bio_out, " dsa->q = BN_bin2bn(dsaq_%d, sizeof (dsaq_%d), NULL);\n", |
0f113f3e | 286 | bits_p, bits_p); |
7e1b7485 | 287 | BIO_printf(bio_out, " dsa->g = BN_bin2bn(dsag_%d, sizeof (dsag_%d), NULL);\n", |
0f113f3e | 288 | bits_p, bits_p); |
7e1b7485 RS |
289 | BIO_printf(bio_out, " if (!dsa->p || !dsa->q || !dsa->g) {\n" |
290 | " DSA_free(dsa);\n" | |
291 | " return NULL;\n" | |
292 | " }\n" | |
293 | " return(dsa);\n}\n"); | |
0f113f3e MC |
294 | } |
295 | ||
296 | if (!noout) { | |
297 | if (outformat == FORMAT_ASN1) | |
298 | i = i2d_DSAparams_bio(out, dsa); | |
7e1b7485 | 299 | else |
0f113f3e | 300 | i = PEM_write_bio_DSAparams(out, dsa); |
0f113f3e MC |
301 | if (!i) { |
302 | BIO_printf(bio_err, "unable to write DSA parameters\n"); | |
303 | ERR_print_errors(bio_err); | |
304 | goto end; | |
305 | } | |
306 | } | |
307 | if (genkey) { | |
308 | DSA *dsakey; | |
309 | ||
310 | assert(need_rand); | |
311 | if ((dsakey = DSAparams_dup(dsa)) == NULL) | |
312 | goto end; | |
313 | if (non_fips_allow) | |
314 | dsakey->flags |= DSA_FLAG_NON_FIPS_ALLOW; | |
315 | if (!DSA_generate_key(dsakey)) { | |
316 | ERR_print_errors(bio_err); | |
317 | DSA_free(dsakey); | |
318 | goto end; | |
319 | } | |
320 | if (outformat == FORMAT_ASN1) | |
321 | i = i2d_DSAPrivateKey_bio(out, dsakey); | |
7e1b7485 | 322 | else |
0f113f3e MC |
323 | i = PEM_write_bio_DSAPrivateKey(out, dsakey, NULL, NULL, 0, NULL, |
324 | NULL); | |
0f113f3e MC |
325 | DSA_free(dsakey); |
326 | } | |
327 | if (need_rand) | |
7e1b7485 | 328 | app_RAND_write_file(NULL); |
0f113f3e MC |
329 | ret = 0; |
330 | end: | |
23a1d5e9 | 331 | BN_GENCB_free(cb); |
ca3a82c3 RS |
332 | BIO_free(in); |
333 | BIO_free_all(out); | |
d6407083 | 334 | DSA_free(dsa); |
7e1b7485 | 335 | return (ret); |
0f113f3e | 336 | } |
d02b48c6 | 337 | |
6d23cf97 | 338 | static int dsa_cb(int p, int n, BN_GENCB *cb) |
0f113f3e MC |
339 | { |
340 | char c = '*'; | |
341 | ||
342 | if (p == 0) | |
343 | c = '.'; | |
344 | if (p == 1) | |
345 | c = '+'; | |
346 | if (p == 2) | |
347 | c = '*'; | |
348 | if (p == 3) | |
349 | c = '\n'; | |
350 | BIO_write(BN_GENCB_get_arg(cb), &c, 1); | |
351 | (void)BIO_flush(BN_GENCB_get_arg(cb)); | |
352 | # ifdef GENCB_TEST | |
353 | if (stop_keygen_flag) | |
354 | return 0; | |
355 | # endif | |
356 | return 1; | |
357 | } | |
358 | #else /* !OPENSSL_NO_DSA */ | |
d4f0339c DSH |
359 | |
360 | # if PEDANTIC | |
0f113f3e | 361 | static void *dummy = &dummy; |
d4f0339c DSH |
362 | # endif |
363 | ||
f5d7a031 | 364 | #endif |