xref: /minix3/crypto/external/bsd/openssl/dist/apps/dsaparam.c (revision 0a6a1f1d05b60e214de2f05a7310ddd1f0e590e7)
1ebfedea0SLionel Sambuc /* apps/dsaparam.c */
2ebfedea0SLionel Sambuc /* Copyright (C) 1995-1998 Eric Young (eay@cryptsoft.com)
3ebfedea0SLionel Sambuc  * All rights reserved.
4ebfedea0SLionel Sambuc  *
5ebfedea0SLionel Sambuc  * This package is an SSL implementation written
6ebfedea0SLionel Sambuc  * by Eric Young (eay@cryptsoft.com).
7ebfedea0SLionel Sambuc  * The implementation was written so as to conform with Netscapes SSL.
8ebfedea0SLionel Sambuc  *
9ebfedea0SLionel Sambuc  * This library is free for commercial and non-commercial use as long as
10ebfedea0SLionel Sambuc  * the following conditions are aheared to.  The following conditions
11ebfedea0SLionel Sambuc  * apply to all code found in this distribution, be it the RC4, RSA,
12ebfedea0SLionel Sambuc  * lhash, DES, etc., code; not just the SSL code.  The SSL documentation
13ebfedea0SLionel Sambuc  * included with this distribution is covered by the same copyright terms
14ebfedea0SLionel Sambuc  * except that the holder is Tim Hudson (tjh@cryptsoft.com).
15ebfedea0SLionel Sambuc  *
16ebfedea0SLionel Sambuc  * Copyright remains Eric Young's, and as such any Copyright notices in
17ebfedea0SLionel Sambuc  * the code are not to be removed.
18ebfedea0SLionel Sambuc  * If this package is used in a product, Eric Young should be given attribution
19ebfedea0SLionel Sambuc  * as the author of the parts of the library used.
20ebfedea0SLionel Sambuc  * This can be in the form of a textual message at program startup or
21ebfedea0SLionel Sambuc  * in documentation (online or textual) provided with the package.
22ebfedea0SLionel Sambuc  *
23ebfedea0SLionel Sambuc  * Redistribution and use in source and binary forms, with or without
24ebfedea0SLionel Sambuc  * modification, are permitted provided that the following conditions
25ebfedea0SLionel Sambuc  * are met:
26ebfedea0SLionel Sambuc  * 1. Redistributions of source code must retain the copyright
27ebfedea0SLionel Sambuc  *    notice, this list of conditions and the following disclaimer.
28ebfedea0SLionel Sambuc  * 2. Redistributions in binary form must reproduce the above copyright
29ebfedea0SLionel Sambuc  *    notice, this list of conditions and the following disclaimer in the
30ebfedea0SLionel Sambuc  *    documentation and/or other materials provided with the distribution.
31ebfedea0SLionel Sambuc  * 3. All advertising materials mentioning features or use of this software
32ebfedea0SLionel Sambuc  *    must display the following acknowledgement:
33ebfedea0SLionel Sambuc  *    "This product includes cryptographic software written by
34ebfedea0SLionel Sambuc  *     Eric Young (eay@cryptsoft.com)"
35ebfedea0SLionel Sambuc  *    The word 'cryptographic' can be left out if the rouines from the library
36ebfedea0SLionel Sambuc  *    being used are not cryptographic related :-).
37ebfedea0SLionel Sambuc  * 4. If you include any Windows specific code (or a derivative thereof) from
38ebfedea0SLionel Sambuc  *    the apps directory (application code) you must include an acknowledgement:
39ebfedea0SLionel Sambuc  *    "This product includes software written by Tim Hudson (tjh@cryptsoft.com)"
40ebfedea0SLionel Sambuc  *
41ebfedea0SLionel Sambuc  * THIS SOFTWARE IS PROVIDED BY ERIC YOUNG ``AS IS'' AND
42ebfedea0SLionel Sambuc  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
43ebfedea0SLionel Sambuc  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
44ebfedea0SLionel Sambuc  * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
45ebfedea0SLionel Sambuc  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
46ebfedea0SLionel Sambuc  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
47ebfedea0SLionel Sambuc  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
48ebfedea0SLionel Sambuc  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
49ebfedea0SLionel Sambuc  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
50ebfedea0SLionel Sambuc  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
51ebfedea0SLionel Sambuc  * SUCH DAMAGE.
52ebfedea0SLionel Sambuc  *
53ebfedea0SLionel Sambuc  * The licence and distribution terms for any publically available version or
54ebfedea0SLionel Sambuc  * derivative of this code cannot be changed.  i.e. this code cannot simply be
55ebfedea0SLionel Sambuc  * copied and put under another distribution licence
56ebfedea0SLionel Sambuc  * [including the GNU Public Licence.]
57ebfedea0SLionel Sambuc  */
58ebfedea0SLionel Sambuc 
59ebfedea0SLionel Sambuc #include <openssl/opensslconf.h> /* for OPENSSL_NO_DSA */
60*0a6a1f1dSLionel Sambuc /*
61*0a6a1f1dSLionel Sambuc  * Until the key-gen callbacks are modified to use newer prototypes, we allow
62*0a6a1f1dSLionel Sambuc  * deprecated functions for openssl-internal code
63*0a6a1f1dSLionel Sambuc  */
64ebfedea0SLionel Sambuc #ifdef OPENSSL_NO_DEPRECATED
65ebfedea0SLionel Sambuc # undef OPENSSL_NO_DEPRECATED
66ebfedea0SLionel Sambuc #endif
67ebfedea0SLionel Sambuc 
68ebfedea0SLionel Sambuc #ifndef OPENSSL_NO_DSA
69ebfedea0SLionel Sambuc # include <assert.h>
70ebfedea0SLionel Sambuc # include <stdio.h>
71ebfedea0SLionel Sambuc # include <stdlib.h>
72ebfedea0SLionel Sambuc # include <time.h>
73ebfedea0SLionel Sambuc # include <string.h>
74ebfedea0SLionel Sambuc # include "apps.h"
75ebfedea0SLionel Sambuc # include <openssl/bio.h>
76ebfedea0SLionel Sambuc # include <openssl/err.h>
77ebfedea0SLionel Sambuc # include <openssl/bn.h>
78ebfedea0SLionel Sambuc # include <openssl/dsa.h>
79ebfedea0SLionel Sambuc # include <openssl/x509.h>
80ebfedea0SLionel Sambuc # include <openssl/pem.h>
81ebfedea0SLionel Sambuc 
82ebfedea0SLionel Sambuc # undef PROG
83ebfedea0SLionel Sambuc # define PROG    dsaparam_main
84ebfedea0SLionel Sambuc 
85*0a6a1f1dSLionel Sambuc /*-
86*0a6a1f1dSLionel Sambuc  * -inform arg  - input format - default PEM (DER or PEM)
87ebfedea0SLionel Sambuc  * -outform arg - output format - default PEM
88ebfedea0SLionel Sambuc  * -in arg      - input file - default stdin
89ebfedea0SLionel Sambuc  * -out arg     - output file - default stdout
90ebfedea0SLionel Sambuc  * -noout
91ebfedea0SLionel Sambuc  * -text
92ebfedea0SLionel Sambuc  * -C
93ebfedea0SLionel Sambuc  * -noout
94ebfedea0SLionel Sambuc  * -genkey
95ebfedea0SLionel Sambuc  *  #ifdef GENCB_TEST
96ebfedea0SLionel Sambuc  * -timebomb n  - interrupt keygen after <n> seconds
97ebfedea0SLionel Sambuc  *  #endif
98ebfedea0SLionel Sambuc  */
99ebfedea0SLionel Sambuc 
100ebfedea0SLionel Sambuc # ifdef GENCB_TEST
101ebfedea0SLionel Sambuc 
102ebfedea0SLionel Sambuc static int stop_keygen_flag = 0;
103ebfedea0SLionel Sambuc 
timebomb_sigalarm(int foo)104ebfedea0SLionel Sambuc static void timebomb_sigalarm(int foo)
105ebfedea0SLionel Sambuc {
106ebfedea0SLionel Sambuc     stop_keygen_flag = 1;
107ebfedea0SLionel Sambuc }
108ebfedea0SLionel Sambuc 
109ebfedea0SLionel Sambuc # endif
110ebfedea0SLionel Sambuc 
111ebfedea0SLionel Sambuc static int MS_CALLBACK dsa_cb(int p, int n, BN_GENCB *cb);
112ebfedea0SLionel Sambuc 
113ebfedea0SLionel Sambuc int MAIN(int, char **);
114ebfedea0SLionel Sambuc 
MAIN(int argc,char ** argv)115ebfedea0SLionel Sambuc int MAIN(int argc, char **argv)
116ebfedea0SLionel Sambuc {
117ebfedea0SLionel Sambuc     DSA *dsa = NULL;
118ebfedea0SLionel Sambuc     int i, badops = 0, text = 0;
119ebfedea0SLionel Sambuc     BIO *in = NULL, *out = NULL;
120ebfedea0SLionel Sambuc     int informat, outformat, noout = 0, C = 0, ret = 1;
121ebfedea0SLionel Sambuc     char *infile, *outfile, *prog, *inrand = NULL;
122ebfedea0SLionel Sambuc     int numbits = -1, num, genkey = 0;
123ebfedea0SLionel Sambuc     int need_rand = 0;
124ebfedea0SLionel Sambuc # ifndef OPENSSL_NO_ENGINE
125ebfedea0SLionel Sambuc     char *engine = NULL;
126ebfedea0SLionel Sambuc # endif
127ebfedea0SLionel Sambuc # ifdef GENCB_TEST
128ebfedea0SLionel Sambuc     int timebomb = 0;
129ebfedea0SLionel Sambuc # endif
130ebfedea0SLionel Sambuc 
131ebfedea0SLionel Sambuc     apps_startup();
132ebfedea0SLionel Sambuc 
133ebfedea0SLionel Sambuc     if (bio_err == NULL)
134ebfedea0SLionel Sambuc         if ((bio_err = BIO_new(BIO_s_file())) != NULL)
135ebfedea0SLionel Sambuc             BIO_set_fp(bio_err, stderr, BIO_NOCLOSE | BIO_FP_TEXT);
136ebfedea0SLionel Sambuc 
137ebfedea0SLionel Sambuc     if (!load_config(bio_err, NULL))
138ebfedea0SLionel Sambuc         goto end;
139ebfedea0SLionel Sambuc 
140ebfedea0SLionel Sambuc     infile = NULL;
141ebfedea0SLionel Sambuc     outfile = NULL;
142ebfedea0SLionel Sambuc     informat = FORMAT_PEM;
143ebfedea0SLionel Sambuc     outformat = FORMAT_PEM;
144ebfedea0SLionel Sambuc 
145ebfedea0SLionel Sambuc     prog = argv[0];
146ebfedea0SLionel Sambuc     argc--;
147ebfedea0SLionel Sambuc     argv++;
148*0a6a1f1dSLionel Sambuc     while (argc >= 1) {
149*0a6a1f1dSLionel Sambuc         if (strcmp(*argv, "-inform") == 0) {
150*0a6a1f1dSLionel Sambuc             if (--argc < 1)
151*0a6a1f1dSLionel Sambuc                 goto bad;
152ebfedea0SLionel Sambuc             informat = str2fmt(*(++argv));
153*0a6a1f1dSLionel Sambuc         } else if (strcmp(*argv, "-outform") == 0) {
154*0a6a1f1dSLionel Sambuc             if (--argc < 1)
155*0a6a1f1dSLionel Sambuc                 goto bad;
156ebfedea0SLionel Sambuc             outformat = str2fmt(*(++argv));
157*0a6a1f1dSLionel Sambuc         } else if (strcmp(*argv, "-in") == 0) {
158*0a6a1f1dSLionel Sambuc             if (--argc < 1)
159*0a6a1f1dSLionel Sambuc                 goto bad;
160ebfedea0SLionel Sambuc             infile = *(++argv);
161*0a6a1f1dSLionel Sambuc         } else if (strcmp(*argv, "-out") == 0) {
162*0a6a1f1dSLionel Sambuc             if (--argc < 1)
163*0a6a1f1dSLionel Sambuc                 goto bad;
164ebfedea0SLionel Sambuc             outfile = *(++argv);
165ebfedea0SLionel Sambuc         }
166ebfedea0SLionel Sambuc # ifndef OPENSSL_NO_ENGINE
167*0a6a1f1dSLionel Sambuc         else if (strcmp(*argv, "-engine") == 0) {
168*0a6a1f1dSLionel Sambuc             if (--argc < 1)
169*0a6a1f1dSLionel Sambuc                 goto bad;
170ebfedea0SLionel Sambuc             engine = *(++argv);
171ebfedea0SLionel Sambuc         }
172ebfedea0SLionel Sambuc # endif
173ebfedea0SLionel Sambuc # ifdef GENCB_TEST
174*0a6a1f1dSLionel Sambuc         else if (strcmp(*argv, "-timebomb") == 0) {
175*0a6a1f1dSLionel Sambuc             if (--argc < 1)
176*0a6a1f1dSLionel Sambuc                 goto bad;
177ebfedea0SLionel Sambuc             timebomb = atoi(*(++argv));
178ebfedea0SLionel Sambuc         }
179ebfedea0SLionel Sambuc # endif
180ebfedea0SLionel Sambuc         else if (strcmp(*argv, "-text") == 0)
181ebfedea0SLionel Sambuc             text = 1;
182ebfedea0SLionel Sambuc         else if (strcmp(*argv, "-C") == 0)
183ebfedea0SLionel Sambuc             C = 1;
184*0a6a1f1dSLionel Sambuc         else if (strcmp(*argv, "-genkey") == 0) {
185ebfedea0SLionel Sambuc             genkey = 1;
186ebfedea0SLionel Sambuc             need_rand = 1;
187*0a6a1f1dSLionel Sambuc         } else if (strcmp(*argv, "-rand") == 0) {
188*0a6a1f1dSLionel Sambuc             if (--argc < 1)
189*0a6a1f1dSLionel Sambuc                 goto bad;
190ebfedea0SLionel Sambuc             inrand = *(++argv);
191ebfedea0SLionel Sambuc             need_rand = 1;
192*0a6a1f1dSLionel Sambuc         } else if (strcmp(*argv, "-noout") == 0)
193ebfedea0SLionel Sambuc             noout = 1;
194*0a6a1f1dSLionel Sambuc         else if (sscanf(*argv, "%d", &num) == 1) {
195ebfedea0SLionel Sambuc             /* generate a key */
196ebfedea0SLionel Sambuc             numbits = num;
197ebfedea0SLionel Sambuc             need_rand = 1;
198*0a6a1f1dSLionel Sambuc         } else {
199ebfedea0SLionel Sambuc             BIO_printf(bio_err, "unknown option %s\n", *argv);
200ebfedea0SLionel Sambuc             badops = 1;
201ebfedea0SLionel Sambuc             break;
202ebfedea0SLionel Sambuc         }
203ebfedea0SLionel Sambuc         argc--;
204ebfedea0SLionel Sambuc         argv++;
205ebfedea0SLionel Sambuc     }
206ebfedea0SLionel Sambuc 
207*0a6a1f1dSLionel Sambuc     if (badops) {
208ebfedea0SLionel Sambuc  bad:
209ebfedea0SLionel Sambuc         BIO_printf(bio_err, "%s [options] [bits] <infile >outfile\n", prog);
210ebfedea0SLionel Sambuc         BIO_printf(bio_err, "where options are\n");
211ebfedea0SLionel Sambuc         BIO_printf(bio_err, " -inform arg   input format - DER or PEM\n");
212ebfedea0SLionel Sambuc         BIO_printf(bio_err, " -outform arg  output format - DER or PEM\n");
213ebfedea0SLionel Sambuc         BIO_printf(bio_err, " -in arg       input file\n");
214ebfedea0SLionel Sambuc         BIO_printf(bio_err, " -out arg      output file\n");
215ebfedea0SLionel Sambuc         BIO_printf(bio_err, " -text         print as text\n");
216ebfedea0SLionel Sambuc         BIO_printf(bio_err, " -C            Output C code\n");
217ebfedea0SLionel Sambuc         BIO_printf(bio_err, " -noout        no output\n");
218ebfedea0SLionel Sambuc         BIO_printf(bio_err, " -genkey       generate a DSA key\n");
219*0a6a1f1dSLionel Sambuc         BIO_printf(bio_err,
220*0a6a1f1dSLionel Sambuc                    " -rand         files to use for random number input\n");
221ebfedea0SLionel Sambuc # ifndef OPENSSL_NO_ENGINE
222*0a6a1f1dSLionel Sambuc         BIO_printf(bio_err,
223*0a6a1f1dSLionel Sambuc                    " -engine e     use engine e, possibly a hardware device.\n");
224ebfedea0SLionel Sambuc # endif
225ebfedea0SLionel Sambuc # ifdef GENCB_TEST
226*0a6a1f1dSLionel Sambuc         BIO_printf(bio_err,
227*0a6a1f1dSLionel Sambuc                    " -timebomb n   interrupt keygen after <n> seconds\n");
228ebfedea0SLionel Sambuc # endif
229*0a6a1f1dSLionel Sambuc         BIO_printf(bio_err,
230*0a6a1f1dSLionel Sambuc                    " number        number of bits to use for generating private key\n");
231ebfedea0SLionel Sambuc         goto end;
232ebfedea0SLionel Sambuc     }
233ebfedea0SLionel Sambuc 
234ebfedea0SLionel Sambuc     ERR_load_crypto_strings();
235ebfedea0SLionel Sambuc 
236ebfedea0SLionel Sambuc     in = BIO_new(BIO_s_file());
237ebfedea0SLionel Sambuc     out = BIO_new(BIO_s_file());
238*0a6a1f1dSLionel Sambuc     if ((in == NULL) || (out == NULL)) {
239ebfedea0SLionel Sambuc         ERR_print_errors(bio_err);
240ebfedea0SLionel Sambuc         goto end;
241ebfedea0SLionel Sambuc     }
242ebfedea0SLionel Sambuc 
243ebfedea0SLionel Sambuc     if (infile == NULL)
244ebfedea0SLionel Sambuc         BIO_set_fp(in, stdin, BIO_NOCLOSE);
245*0a6a1f1dSLionel Sambuc     else {
246*0a6a1f1dSLionel Sambuc         if (BIO_read_filename(in, infile) <= 0) {
247ebfedea0SLionel Sambuc             perror(infile);
248ebfedea0SLionel Sambuc             goto end;
249ebfedea0SLionel Sambuc         }
250ebfedea0SLionel Sambuc     }
251*0a6a1f1dSLionel Sambuc     if (outfile == NULL) {
252ebfedea0SLionel Sambuc         BIO_set_fp(out, stdout, BIO_NOCLOSE);
253ebfedea0SLionel Sambuc # ifdef OPENSSL_SYS_VMS
254ebfedea0SLionel Sambuc         {
255ebfedea0SLionel Sambuc             BIO *tmpbio = BIO_new(BIO_f_linebuffer());
256ebfedea0SLionel Sambuc             out = BIO_push(tmpbio, out);
257ebfedea0SLionel Sambuc         }
258ebfedea0SLionel Sambuc # endif
259*0a6a1f1dSLionel Sambuc     } else {
260*0a6a1f1dSLionel Sambuc         if (BIO_write_filename(out, outfile) <= 0) {
261ebfedea0SLionel Sambuc             perror(outfile);
262ebfedea0SLionel Sambuc             goto end;
263ebfedea0SLionel Sambuc         }
264ebfedea0SLionel Sambuc     }
265ebfedea0SLionel Sambuc 
266ebfedea0SLionel Sambuc # ifndef OPENSSL_NO_ENGINE
267ebfedea0SLionel Sambuc     setup_engine(bio_err, engine, 0);
268ebfedea0SLionel Sambuc # endif
269ebfedea0SLionel Sambuc 
270*0a6a1f1dSLionel Sambuc     if (need_rand) {
271ebfedea0SLionel Sambuc         app_RAND_load_file(NULL, bio_err, (inrand != NULL));
272ebfedea0SLionel Sambuc         if (inrand != NULL)
273ebfedea0SLionel Sambuc             BIO_printf(bio_err, "%ld semi-random bytes loaded\n",
274ebfedea0SLionel Sambuc                        app_RAND_load_files(inrand));
275ebfedea0SLionel Sambuc     }
276ebfedea0SLionel Sambuc 
277*0a6a1f1dSLionel Sambuc     if (numbits > 0) {
278ebfedea0SLionel Sambuc         BN_GENCB cb;
279ebfedea0SLionel Sambuc         BN_GENCB_set(&cb, dsa_cb, bio_err);
280ebfedea0SLionel Sambuc         assert(need_rand);
281ebfedea0SLionel Sambuc         dsa = DSA_new();
282*0a6a1f1dSLionel Sambuc         if (!dsa) {
283ebfedea0SLionel Sambuc             BIO_printf(bio_err, "Error allocating DSA object\n");
284ebfedea0SLionel Sambuc             goto end;
285ebfedea0SLionel Sambuc         }
286*0a6a1f1dSLionel Sambuc         BIO_printf(bio_err, "Generating DSA parameters, %d bit long prime\n",
287*0a6a1f1dSLionel Sambuc                    num);
288ebfedea0SLionel Sambuc         BIO_printf(bio_err, "This could take some time\n");
289ebfedea0SLionel Sambuc # ifdef GENCB_TEST
290*0a6a1f1dSLionel Sambuc         if (timebomb > 0) {
291ebfedea0SLionel Sambuc             struct sigaction act;
292ebfedea0SLionel Sambuc             act.sa_handler = timebomb_sigalarm;
293ebfedea0SLionel Sambuc             act.sa_flags = 0;
294*0a6a1f1dSLionel Sambuc             BIO_printf(bio_err,
295*0a6a1f1dSLionel Sambuc                        "(though I'll stop it if not done within %d secs)\n",
296ebfedea0SLionel Sambuc                        timebomb);
297*0a6a1f1dSLionel Sambuc             if (sigaction(SIGALRM, &act, NULL) != 0) {
298ebfedea0SLionel Sambuc                 BIO_printf(bio_err, "Error, couldn't set SIGALRM handler\n");
299ebfedea0SLionel Sambuc                 goto end;
300ebfedea0SLionel Sambuc             }
301ebfedea0SLionel Sambuc             alarm(timebomb);
302ebfedea0SLionel Sambuc         }
303ebfedea0SLionel Sambuc # endif
304*0a6a1f1dSLionel Sambuc         if (!DSA_generate_parameters_ex(dsa, num, NULL, 0, NULL, NULL, &cb)) {
305ebfedea0SLionel Sambuc # ifdef GENCB_TEST
306*0a6a1f1dSLionel Sambuc             if (stop_keygen_flag) {
307ebfedea0SLionel Sambuc                 BIO_printf(bio_err, "DSA key generation time-stopped\n");
308ebfedea0SLionel Sambuc                 /* This is an asked-for behaviour! */
309ebfedea0SLionel Sambuc                 ret = 0;
310ebfedea0SLionel Sambuc                 goto end;
311ebfedea0SLionel Sambuc             }
312ebfedea0SLionel Sambuc # endif
313ebfedea0SLionel Sambuc             ERR_print_errors(bio_err);
314ebfedea0SLionel Sambuc             BIO_printf(bio_err, "Error, DSA key generation failed\n");
315ebfedea0SLionel Sambuc             goto end;
316ebfedea0SLionel Sambuc         }
317*0a6a1f1dSLionel Sambuc     } else if (informat == FORMAT_ASN1)
318ebfedea0SLionel Sambuc         dsa = d2i_DSAparams_bio(in, NULL);
319ebfedea0SLionel Sambuc     else if (informat == FORMAT_PEM)
320ebfedea0SLionel Sambuc         dsa = PEM_read_bio_DSAparams(in, NULL, NULL, NULL);
321*0a6a1f1dSLionel Sambuc     else {
322ebfedea0SLionel Sambuc         BIO_printf(bio_err, "bad input format specified\n");
323ebfedea0SLionel Sambuc         goto end;
324ebfedea0SLionel Sambuc     }
325*0a6a1f1dSLionel Sambuc     if (dsa == NULL) {
326ebfedea0SLionel Sambuc         BIO_printf(bio_err, "unable to load DSA parameters\n");
327ebfedea0SLionel Sambuc         ERR_print_errors(bio_err);
328ebfedea0SLionel Sambuc         goto end;
329ebfedea0SLionel Sambuc     }
330ebfedea0SLionel Sambuc 
331*0a6a1f1dSLionel Sambuc     if (text) {
332ebfedea0SLionel Sambuc         DSAparams_print(out, dsa);
333ebfedea0SLionel Sambuc     }
334ebfedea0SLionel Sambuc 
335*0a6a1f1dSLionel Sambuc     if (C) {
336ebfedea0SLionel Sambuc         unsigned char *data;
337ebfedea0SLionel Sambuc         int l, len, bits_p;
338ebfedea0SLionel Sambuc 
339ebfedea0SLionel Sambuc         len = BN_num_bytes(dsa->p);
340ebfedea0SLionel Sambuc         bits_p = BN_num_bits(dsa->p);
341ebfedea0SLionel Sambuc         data = (unsigned char *)OPENSSL_malloc(len + 20);
342*0a6a1f1dSLionel Sambuc         if (data == NULL) {
343ebfedea0SLionel Sambuc             perror("OPENSSL_malloc");
344ebfedea0SLionel Sambuc             goto end;
345ebfedea0SLionel Sambuc         }
346ebfedea0SLionel Sambuc         l = BN_bn2bin(dsa->p, data);
347ebfedea0SLionel Sambuc         printf("static unsigned char dsa%d_p[]={", bits_p);
348*0a6a1f1dSLionel Sambuc         for (i = 0; i < l; i++) {
349*0a6a1f1dSLionel Sambuc             if ((i % 12) == 0)
350*0a6a1f1dSLionel Sambuc                 printf("\n\t");
351ebfedea0SLionel Sambuc             printf("0x%02X,", data[i]);
352ebfedea0SLionel Sambuc         }
353ebfedea0SLionel Sambuc         printf("\n\t};\n");
354ebfedea0SLionel Sambuc 
355ebfedea0SLionel Sambuc         l = BN_bn2bin(dsa->q, data);
356ebfedea0SLionel Sambuc         printf("static unsigned char dsa%d_q[]={", bits_p);
357*0a6a1f1dSLionel Sambuc         for (i = 0; i < l; i++) {
358*0a6a1f1dSLionel Sambuc             if ((i % 12) == 0)
359*0a6a1f1dSLionel Sambuc                 printf("\n\t");
360ebfedea0SLionel Sambuc             printf("0x%02X,", data[i]);
361ebfedea0SLionel Sambuc         }
362ebfedea0SLionel Sambuc         printf("\n\t};\n");
363ebfedea0SLionel Sambuc 
364ebfedea0SLionel Sambuc         l = BN_bn2bin(dsa->g, data);
365ebfedea0SLionel Sambuc         printf("static unsigned char dsa%d_g[]={", bits_p);
366*0a6a1f1dSLionel Sambuc         for (i = 0; i < l; i++) {
367*0a6a1f1dSLionel Sambuc             if ((i % 12) == 0)
368*0a6a1f1dSLionel Sambuc                 printf("\n\t");
369ebfedea0SLionel Sambuc             printf("0x%02X,", data[i]);
370ebfedea0SLionel Sambuc         }
371ebfedea0SLionel Sambuc         printf("\n\t};\n\n");
372ebfedea0SLionel Sambuc 
373ebfedea0SLionel Sambuc         printf("DSA *get_dsa%d()\n\t{\n", bits_p);
374ebfedea0SLionel Sambuc         printf("\tDSA *dsa;\n\n");
375ebfedea0SLionel Sambuc         printf("\tif ((dsa=DSA_new()) == NULL) return(NULL);\n");
376ebfedea0SLionel Sambuc         printf("\tdsa->p=BN_bin2bn(dsa%d_p,sizeof(dsa%d_p),NULL);\n",
377ebfedea0SLionel Sambuc                bits_p, bits_p);
378ebfedea0SLionel Sambuc         printf("\tdsa->q=BN_bin2bn(dsa%d_q,sizeof(dsa%d_q),NULL);\n",
379ebfedea0SLionel Sambuc                bits_p, bits_p);
380ebfedea0SLionel Sambuc         printf("\tdsa->g=BN_bin2bn(dsa%d_g,sizeof(dsa%d_g),NULL);\n",
381ebfedea0SLionel Sambuc                bits_p, bits_p);
382*0a6a1f1dSLionel Sambuc         printf
383*0a6a1f1dSLionel Sambuc             ("\tif ((dsa->p == NULL) || (dsa->q == NULL) || (dsa->g == NULL))\n");
384ebfedea0SLionel Sambuc         printf("\t\t{ DSA_free(dsa); return(NULL); }\n");
385ebfedea0SLionel Sambuc         printf("\treturn(dsa);\n\t}\n");
386ebfedea0SLionel Sambuc     }
387ebfedea0SLionel Sambuc 
388*0a6a1f1dSLionel Sambuc     if (!noout) {
389ebfedea0SLionel Sambuc         if (outformat == FORMAT_ASN1)
390ebfedea0SLionel Sambuc             i = i2d_DSAparams_bio(out, dsa);
391ebfedea0SLionel Sambuc         else if (outformat == FORMAT_PEM)
392ebfedea0SLionel Sambuc             i = PEM_write_bio_DSAparams(out, dsa);
393ebfedea0SLionel Sambuc         else {
394ebfedea0SLionel Sambuc             BIO_printf(bio_err, "bad output format specified for outfile\n");
395ebfedea0SLionel Sambuc             goto end;
396ebfedea0SLionel Sambuc         }
397*0a6a1f1dSLionel Sambuc         if (!i) {
398ebfedea0SLionel Sambuc             BIO_printf(bio_err, "unable to write DSA parameters\n");
399ebfedea0SLionel Sambuc             ERR_print_errors(bio_err);
400ebfedea0SLionel Sambuc             goto end;
401ebfedea0SLionel Sambuc         }
402ebfedea0SLionel Sambuc     }
403*0a6a1f1dSLionel Sambuc     if (genkey) {
404ebfedea0SLionel Sambuc         DSA *dsakey;
405ebfedea0SLionel Sambuc 
406ebfedea0SLionel Sambuc         assert(need_rand);
407*0a6a1f1dSLionel Sambuc         if ((dsakey = DSAparams_dup(dsa)) == NULL)
408*0a6a1f1dSLionel Sambuc             goto end;
409*0a6a1f1dSLionel Sambuc         if (!DSA_generate_key(dsakey)) {
410ebfedea0SLionel Sambuc             ERR_print_errors(bio_err);
411ebfedea0SLionel Sambuc             DSA_free(dsakey);
412ebfedea0SLionel Sambuc             goto end;
413ebfedea0SLionel Sambuc         }
414ebfedea0SLionel Sambuc         if (outformat == FORMAT_ASN1)
415ebfedea0SLionel Sambuc             i = i2d_DSAPrivateKey_bio(out, dsakey);
416ebfedea0SLionel Sambuc         else if (outformat == FORMAT_PEM)
417*0a6a1f1dSLionel Sambuc             i = PEM_write_bio_DSAPrivateKey(out, dsakey, NULL, NULL, 0, NULL,
418*0a6a1f1dSLionel Sambuc                                             NULL);
419ebfedea0SLionel Sambuc         else {
420ebfedea0SLionel Sambuc             BIO_printf(bio_err, "bad output format specified for outfile\n");
421ebfedea0SLionel Sambuc             DSA_free(dsakey);
422ebfedea0SLionel Sambuc             goto end;
423ebfedea0SLionel Sambuc         }
424ebfedea0SLionel Sambuc         DSA_free(dsakey);
425ebfedea0SLionel Sambuc     }
426ebfedea0SLionel Sambuc     if (need_rand)
427ebfedea0SLionel Sambuc         app_RAND_write_file(NULL, bio_err);
428ebfedea0SLionel Sambuc     ret = 0;
429ebfedea0SLionel Sambuc  end:
430*0a6a1f1dSLionel Sambuc     if (in != NULL)
431*0a6a1f1dSLionel Sambuc         BIO_free(in);
432*0a6a1f1dSLionel Sambuc     if (out != NULL)
433*0a6a1f1dSLionel Sambuc         BIO_free_all(out);
434*0a6a1f1dSLionel Sambuc     if (dsa != NULL)
435*0a6a1f1dSLionel Sambuc         DSA_free(dsa);
436ebfedea0SLionel Sambuc     apps_shutdown();
437ebfedea0SLionel Sambuc     OPENSSL_EXIT(ret);
438ebfedea0SLionel Sambuc }
439ebfedea0SLionel Sambuc 
dsa_cb(int p,int n,BN_GENCB * cb)440ebfedea0SLionel Sambuc static int MS_CALLBACK dsa_cb(int p, int n, BN_GENCB *cb)
441ebfedea0SLionel Sambuc {
442ebfedea0SLionel Sambuc     char c = '*';
443ebfedea0SLionel Sambuc 
444*0a6a1f1dSLionel Sambuc     if (p == 0)
445*0a6a1f1dSLionel Sambuc         c = '.';
446*0a6a1f1dSLionel Sambuc     if (p == 1)
447*0a6a1f1dSLionel Sambuc         c = '+';
448*0a6a1f1dSLionel Sambuc     if (p == 2)
449*0a6a1f1dSLionel Sambuc         c = '*';
450*0a6a1f1dSLionel Sambuc     if (p == 3)
451*0a6a1f1dSLionel Sambuc         c = '\n';
452ebfedea0SLionel Sambuc     BIO_write(cb->arg, &c, 1);
453ebfedea0SLionel Sambuc     (void)BIO_flush(cb->arg);
454ebfedea0SLionel Sambuc # ifdef LINT
455ebfedea0SLionel Sambuc     p = n;
456ebfedea0SLionel Sambuc # endif
457ebfedea0SLionel Sambuc # ifdef GENCB_TEST
458ebfedea0SLionel Sambuc     if (stop_keygen_flag)
459ebfedea0SLionel Sambuc         return 0;
460ebfedea0SLionel Sambuc # endif
461ebfedea0SLionel Sambuc     return 1;
462ebfedea0SLionel Sambuc }
463ebfedea0SLionel Sambuc #else                           /* !OPENSSL_NO_DSA */
464ebfedea0SLionel Sambuc 
465ebfedea0SLionel Sambuc # if PEDANTIC
466ebfedea0SLionel Sambuc static void *dummy = &dummy;
467ebfedea0SLionel Sambuc # endif
468ebfedea0SLionel Sambuc 
469ebfedea0SLionel Sambuc #endif
470