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