xref: /onnv-gate/usr/src/common/openssl/apps/dhparam.c (revision 0:68f95e015346)
1*0Sstevel@tonic-gate /* apps/dhparam.c */
2*0Sstevel@tonic-gate /* Copyright (C) 1995-1998 Eric Young (eay@cryptsoft.com)
3*0Sstevel@tonic-gate  * All rights reserved.
4*0Sstevel@tonic-gate  *
5*0Sstevel@tonic-gate  * This package is an SSL implementation written
6*0Sstevel@tonic-gate  * by Eric Young (eay@cryptsoft.com).
7*0Sstevel@tonic-gate  * The implementation was written so as to conform with Netscapes SSL.
8*0Sstevel@tonic-gate  *
9*0Sstevel@tonic-gate  * This library is free for commercial and non-commercial use as long as
10*0Sstevel@tonic-gate  * the following conditions are aheared to.  The following conditions
11*0Sstevel@tonic-gate  * apply to all code found in this distribution, be it the RC4, RSA,
12*0Sstevel@tonic-gate  * lhash, DES, etc., code; not just the SSL code.  The SSL documentation
13*0Sstevel@tonic-gate  * included with this distribution is covered by the same copyright terms
14*0Sstevel@tonic-gate  * except that the holder is Tim Hudson (tjh@cryptsoft.com).
15*0Sstevel@tonic-gate  *
16*0Sstevel@tonic-gate  * Copyright remains Eric Young's, and as such any Copyright notices in
17*0Sstevel@tonic-gate  * the code are not to be removed.
18*0Sstevel@tonic-gate  * If this package is used in a product, Eric Young should be given attribution
19*0Sstevel@tonic-gate  * as the author of the parts of the library used.
20*0Sstevel@tonic-gate  * This can be in the form of a textual message at program startup or
21*0Sstevel@tonic-gate  * in documentation (online or textual) provided with the package.
22*0Sstevel@tonic-gate  *
23*0Sstevel@tonic-gate  * Redistribution and use in source and binary forms, with or without
24*0Sstevel@tonic-gate  * modification, are permitted provided that the following conditions
25*0Sstevel@tonic-gate  * are met:
26*0Sstevel@tonic-gate  * 1. Redistributions of source code must retain the copyright
27*0Sstevel@tonic-gate  *    notice, this list of conditions and the following disclaimer.
28*0Sstevel@tonic-gate  * 2. Redistributions in binary form must reproduce the above copyright
29*0Sstevel@tonic-gate  *    notice, this list of conditions and the following disclaimer in the
30*0Sstevel@tonic-gate  *    documentation and/or other materials provided with the distribution.
31*0Sstevel@tonic-gate  * 3. All advertising materials mentioning features or use of this software
32*0Sstevel@tonic-gate  *    must display the following acknowledgement:
33*0Sstevel@tonic-gate  *    "This product includes cryptographic software written by
34*0Sstevel@tonic-gate  *     Eric Young (eay@cryptsoft.com)"
35*0Sstevel@tonic-gate  *    The word 'cryptographic' can be left out if the rouines from the library
36*0Sstevel@tonic-gate  *    being used are not cryptographic related :-).
37*0Sstevel@tonic-gate  * 4. If you include any Windows specific code (or a derivative thereof) from
38*0Sstevel@tonic-gate  *    the apps directory (application code) you must include an acknowledgement:
39*0Sstevel@tonic-gate  *    "This product includes software written by Tim Hudson (tjh@cryptsoft.com)"
40*0Sstevel@tonic-gate  *
41*0Sstevel@tonic-gate  * THIS SOFTWARE IS PROVIDED BY ERIC YOUNG ``AS IS'' AND
42*0Sstevel@tonic-gate  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
43*0Sstevel@tonic-gate  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
44*0Sstevel@tonic-gate  * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
45*0Sstevel@tonic-gate  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
46*0Sstevel@tonic-gate  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
47*0Sstevel@tonic-gate  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
48*0Sstevel@tonic-gate  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
49*0Sstevel@tonic-gate  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
50*0Sstevel@tonic-gate  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
51*0Sstevel@tonic-gate  * SUCH DAMAGE.
52*0Sstevel@tonic-gate  *
53*0Sstevel@tonic-gate  * The licence and distribution terms for any publically available version or
54*0Sstevel@tonic-gate  * derivative of this code cannot be changed.  i.e. this code cannot simply be
55*0Sstevel@tonic-gate  * copied and put under another distribution licence
56*0Sstevel@tonic-gate  * [including the GNU Public Licence.]
57*0Sstevel@tonic-gate  */
58*0Sstevel@tonic-gate /* ====================================================================
59*0Sstevel@tonic-gate  * Copyright (c) 1998-2000 The OpenSSL Project.  All rights reserved.
60*0Sstevel@tonic-gate  *
61*0Sstevel@tonic-gate  * Redistribution and use in source and binary forms, with or without
62*0Sstevel@tonic-gate  * modification, are permitted provided that the following conditions
63*0Sstevel@tonic-gate  * are met:
64*0Sstevel@tonic-gate  *
65*0Sstevel@tonic-gate  * 1. Redistributions of source code must retain the above copyright
66*0Sstevel@tonic-gate  *    notice, this list of conditions and the following disclaimer.
67*0Sstevel@tonic-gate  *
68*0Sstevel@tonic-gate  * 2. Redistributions in binary form must reproduce the above copyright
69*0Sstevel@tonic-gate  *    notice, this list of conditions and the following disclaimer in
70*0Sstevel@tonic-gate  *    the documentation and/or other materials provided with the
71*0Sstevel@tonic-gate  *    distribution.
72*0Sstevel@tonic-gate  *
73*0Sstevel@tonic-gate  * 3. All advertising materials mentioning features or use of this
74*0Sstevel@tonic-gate  *    software must display the following acknowledgment:
75*0Sstevel@tonic-gate  *    "This product includes software developed by the OpenSSL Project
76*0Sstevel@tonic-gate  *    for use in the OpenSSL Toolkit. (http://www.openssl.org/)"
77*0Sstevel@tonic-gate  *
78*0Sstevel@tonic-gate  * 4. The names "OpenSSL Toolkit" and "OpenSSL Project" must not be used to
79*0Sstevel@tonic-gate  *    endorse or promote products derived from this software without
80*0Sstevel@tonic-gate  *    prior written permission. For written permission, please contact
81*0Sstevel@tonic-gate  *    openssl-core@openssl.org.
82*0Sstevel@tonic-gate  *
83*0Sstevel@tonic-gate  * 5. Products derived from this software may not be called "OpenSSL"
84*0Sstevel@tonic-gate  *    nor may "OpenSSL" appear in their names without prior written
85*0Sstevel@tonic-gate  *    permission of the OpenSSL Project.
86*0Sstevel@tonic-gate  *
87*0Sstevel@tonic-gate  * 6. Redistributions of any form whatsoever must retain the following
88*0Sstevel@tonic-gate  *    acknowledgment:
89*0Sstevel@tonic-gate  *    "This product includes software developed by the OpenSSL Project
90*0Sstevel@tonic-gate  *    for use in the OpenSSL Toolkit (http://www.openssl.org/)"
91*0Sstevel@tonic-gate  *
92*0Sstevel@tonic-gate  * THIS SOFTWARE IS PROVIDED BY THE OpenSSL PROJECT ``AS IS'' AND ANY
93*0Sstevel@tonic-gate  * EXPRESSED OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
94*0Sstevel@tonic-gate  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
95*0Sstevel@tonic-gate  * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE OpenSSL PROJECT OR
96*0Sstevel@tonic-gate  * ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
97*0Sstevel@tonic-gate  * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
98*0Sstevel@tonic-gate  * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
99*0Sstevel@tonic-gate  * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
100*0Sstevel@tonic-gate  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
101*0Sstevel@tonic-gate  * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
102*0Sstevel@tonic-gate  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED
103*0Sstevel@tonic-gate  * OF THE POSSIBILITY OF SUCH DAMAGE.
104*0Sstevel@tonic-gate  * ====================================================================
105*0Sstevel@tonic-gate  *
106*0Sstevel@tonic-gate  * This product includes cryptographic software written by Eric Young
107*0Sstevel@tonic-gate  * (eay@cryptsoft.com).  This product includes software written by Tim
108*0Sstevel@tonic-gate  * Hudson (tjh@cryptsoft.com).
109*0Sstevel@tonic-gate  *
110*0Sstevel@tonic-gate  */
111*0Sstevel@tonic-gate 
112*0Sstevel@tonic-gate #ifndef OPENSSL_NO_DH
113*0Sstevel@tonic-gate #include <stdio.h>
114*0Sstevel@tonic-gate #include <stdlib.h>
115*0Sstevel@tonic-gate #include <time.h>
116*0Sstevel@tonic-gate #include <string.h>
117*0Sstevel@tonic-gate #include "apps.h"
118*0Sstevel@tonic-gate #include <openssl/bio.h>
119*0Sstevel@tonic-gate #include <openssl/err.h>
120*0Sstevel@tonic-gate #include <openssl/bn.h>
121*0Sstevel@tonic-gate #include <openssl/dh.h>
122*0Sstevel@tonic-gate #include <openssl/x509.h>
123*0Sstevel@tonic-gate #include <openssl/pem.h>
124*0Sstevel@tonic-gate 
125*0Sstevel@tonic-gate #ifndef OPENSSL_NO_DSA
126*0Sstevel@tonic-gate #include <openssl/dsa.h>
127*0Sstevel@tonic-gate #endif
128*0Sstevel@tonic-gate 
129*0Sstevel@tonic-gate #undef PROG
130*0Sstevel@tonic-gate #define PROG	dhparam_main
131*0Sstevel@tonic-gate 
132*0Sstevel@tonic-gate #define DEFBITS	512
133*0Sstevel@tonic-gate 
134*0Sstevel@tonic-gate /* -inform arg	- input format - default PEM (DER or PEM)
135*0Sstevel@tonic-gate  * -outform arg - output format - default PEM
136*0Sstevel@tonic-gate  * -in arg	- input file - default stdin
137*0Sstevel@tonic-gate  * -out arg	- output file - default stdout
138*0Sstevel@tonic-gate  * -dsaparam  - read or generate DSA parameters, convert to DH
139*0Sstevel@tonic-gate  * -check	- check the parameters are ok
140*0Sstevel@tonic-gate  * -noout
141*0Sstevel@tonic-gate  * -text
142*0Sstevel@tonic-gate  * -C
143*0Sstevel@tonic-gate  */
144*0Sstevel@tonic-gate 
145*0Sstevel@tonic-gate static void MS_CALLBACK dh_cb(int p, int n, void *arg);
146*0Sstevel@tonic-gate 
147*0Sstevel@tonic-gate int MAIN(int, char **);
148*0Sstevel@tonic-gate 
149*0Sstevel@tonic-gate int MAIN(int argc, char **argv)
150*0Sstevel@tonic-gate 	{
151*0Sstevel@tonic-gate #ifndef OPENSSL_NO_ENGINE
152*0Sstevel@tonic-gate 	ENGINE *e = NULL;
153*0Sstevel@tonic-gate #endif
154*0Sstevel@tonic-gate 	DH *dh=NULL;
155*0Sstevel@tonic-gate 	int i,badops=0,text=0;
156*0Sstevel@tonic-gate #ifndef OPENSSL_NO_DSA
157*0Sstevel@tonic-gate 	int dsaparam=0;
158*0Sstevel@tonic-gate #endif
159*0Sstevel@tonic-gate 	BIO *in=NULL,*out=NULL;
160*0Sstevel@tonic-gate 	int informat,outformat,check=0,noout=0,C=0,ret=1;
161*0Sstevel@tonic-gate 	char *infile,*outfile,*prog;
162*0Sstevel@tonic-gate 	char *inrand=NULL;
163*0Sstevel@tonic-gate #ifndef OPENSSL_NO_ENGINE
164*0Sstevel@tonic-gate 	char *engine=NULL;
165*0Sstevel@tonic-gate #endif
166*0Sstevel@tonic-gate 	int num = 0, g = 0;
167*0Sstevel@tonic-gate 
168*0Sstevel@tonic-gate 	apps_startup();
169*0Sstevel@tonic-gate 
170*0Sstevel@tonic-gate 	if (bio_err == NULL)
171*0Sstevel@tonic-gate 		if ((bio_err=BIO_new(BIO_s_file())) != NULL)
172*0Sstevel@tonic-gate 			BIO_set_fp(bio_err,stderr,BIO_NOCLOSE|BIO_FP_TEXT);
173*0Sstevel@tonic-gate 
174*0Sstevel@tonic-gate 	if (!load_config(bio_err, NULL))
175*0Sstevel@tonic-gate 		goto end;
176*0Sstevel@tonic-gate 
177*0Sstevel@tonic-gate 	infile=NULL;
178*0Sstevel@tonic-gate 	outfile=NULL;
179*0Sstevel@tonic-gate 	informat=FORMAT_PEM;
180*0Sstevel@tonic-gate 	outformat=FORMAT_PEM;
181*0Sstevel@tonic-gate 
182*0Sstevel@tonic-gate 	prog=argv[0];
183*0Sstevel@tonic-gate 	argc--;
184*0Sstevel@tonic-gate 	argv++;
185*0Sstevel@tonic-gate 	while (argc >= 1)
186*0Sstevel@tonic-gate 		{
187*0Sstevel@tonic-gate 		if 	(strcmp(*argv,"-inform") == 0)
188*0Sstevel@tonic-gate 			{
189*0Sstevel@tonic-gate 			if (--argc < 1) goto bad;
190*0Sstevel@tonic-gate 			informat=str2fmt(*(++argv));
191*0Sstevel@tonic-gate 			}
192*0Sstevel@tonic-gate 		else if (strcmp(*argv,"-outform") == 0)
193*0Sstevel@tonic-gate 			{
194*0Sstevel@tonic-gate 			if (--argc < 1) goto bad;
195*0Sstevel@tonic-gate 			outformat=str2fmt(*(++argv));
196*0Sstevel@tonic-gate 			}
197*0Sstevel@tonic-gate 		else if (strcmp(*argv,"-in") == 0)
198*0Sstevel@tonic-gate 			{
199*0Sstevel@tonic-gate 			if (--argc < 1) goto bad;
200*0Sstevel@tonic-gate 			infile= *(++argv);
201*0Sstevel@tonic-gate 			}
202*0Sstevel@tonic-gate 		else if (strcmp(*argv,"-out") == 0)
203*0Sstevel@tonic-gate 			{
204*0Sstevel@tonic-gate 			if (--argc < 1) goto bad;
205*0Sstevel@tonic-gate 			outfile= *(++argv);
206*0Sstevel@tonic-gate 			}
207*0Sstevel@tonic-gate #ifndef OPENSSL_NO_ENGINE
208*0Sstevel@tonic-gate 		else if (strcmp(*argv,"-engine") == 0)
209*0Sstevel@tonic-gate 			{
210*0Sstevel@tonic-gate 			if (--argc < 1) goto bad;
211*0Sstevel@tonic-gate 			engine= *(++argv);
212*0Sstevel@tonic-gate 			}
213*0Sstevel@tonic-gate #endif
214*0Sstevel@tonic-gate 		else if (strcmp(*argv,"-check") == 0)
215*0Sstevel@tonic-gate 			check=1;
216*0Sstevel@tonic-gate 		else if (strcmp(*argv,"-text") == 0)
217*0Sstevel@tonic-gate 			text=1;
218*0Sstevel@tonic-gate #ifndef OPENSSL_NO_DSA
219*0Sstevel@tonic-gate 		else if (strcmp(*argv,"-dsaparam") == 0)
220*0Sstevel@tonic-gate 			dsaparam=1;
221*0Sstevel@tonic-gate #endif
222*0Sstevel@tonic-gate 		else if (strcmp(*argv,"-C") == 0)
223*0Sstevel@tonic-gate 			C=1;
224*0Sstevel@tonic-gate 		else if (strcmp(*argv,"-noout") == 0)
225*0Sstevel@tonic-gate 			noout=1;
226*0Sstevel@tonic-gate 		else if (strcmp(*argv,"-2") == 0)
227*0Sstevel@tonic-gate 			g=2;
228*0Sstevel@tonic-gate 		else if (strcmp(*argv,"-5") == 0)
229*0Sstevel@tonic-gate 			g=5;
230*0Sstevel@tonic-gate 		else if (strcmp(*argv,"-rand") == 0)
231*0Sstevel@tonic-gate 			{
232*0Sstevel@tonic-gate 			if (--argc < 1) goto bad;
233*0Sstevel@tonic-gate 			inrand= *(++argv);
234*0Sstevel@tonic-gate 			}
235*0Sstevel@tonic-gate 		else if (((sscanf(*argv,"%d",&num) == 0) || (num <= 0)))
236*0Sstevel@tonic-gate 			goto bad;
237*0Sstevel@tonic-gate 		argv++;
238*0Sstevel@tonic-gate 		argc--;
239*0Sstevel@tonic-gate 		}
240*0Sstevel@tonic-gate 
241*0Sstevel@tonic-gate 	if (badops)
242*0Sstevel@tonic-gate 		{
243*0Sstevel@tonic-gate bad:
244*0Sstevel@tonic-gate 		BIO_printf(bio_err,"%s [options] [numbits]\n",prog);
245*0Sstevel@tonic-gate 		BIO_printf(bio_err,"where options are\n");
246*0Sstevel@tonic-gate 		BIO_printf(bio_err," -inform arg   input format - one of DER PEM\n");
247*0Sstevel@tonic-gate 		BIO_printf(bio_err," -outform arg  output format - one of DER PEM\n");
248*0Sstevel@tonic-gate 		BIO_printf(bio_err," -in arg       input file\n");
249*0Sstevel@tonic-gate 		BIO_printf(bio_err," -out arg      output file\n");
250*0Sstevel@tonic-gate #ifndef OPENSSL_NO_DSA
251*0Sstevel@tonic-gate 		BIO_printf(bio_err," -dsaparam     read or generate DSA parameters, convert to DH\n");
252*0Sstevel@tonic-gate #endif
253*0Sstevel@tonic-gate 		BIO_printf(bio_err," -check        check the DH parameters\n");
254*0Sstevel@tonic-gate 		BIO_printf(bio_err," -text         print a text form of the DH parameters\n");
255*0Sstevel@tonic-gate 		BIO_printf(bio_err," -C            Output C code\n");
256*0Sstevel@tonic-gate 		BIO_printf(bio_err," -2            generate parameters using  2 as the generator value\n");
257*0Sstevel@tonic-gate 		BIO_printf(bio_err," -5            generate parameters using  5 as the generator value\n");
258*0Sstevel@tonic-gate 		BIO_printf(bio_err," numbits       number of bits in to generate (default 512)\n");
259*0Sstevel@tonic-gate #ifndef OPENSSL_NO_ENGINE
260*0Sstevel@tonic-gate 		BIO_printf(bio_err," -engine e     use engine e, possibly a hardware device.\n");
261*0Sstevel@tonic-gate #endif
262*0Sstevel@tonic-gate 		BIO_printf(bio_err," -rand file%cfile%c...\n", LIST_SEPARATOR_CHAR, LIST_SEPARATOR_CHAR);
263*0Sstevel@tonic-gate 		BIO_printf(bio_err,"               - load the file (or the files in the directory) into\n");
264*0Sstevel@tonic-gate 		BIO_printf(bio_err,"               the random number generator\n");
265*0Sstevel@tonic-gate 		BIO_printf(bio_err," -noout        no output\n");
266*0Sstevel@tonic-gate 		goto end;
267*0Sstevel@tonic-gate 		}
268*0Sstevel@tonic-gate 
269*0Sstevel@tonic-gate 	ERR_load_crypto_strings();
270*0Sstevel@tonic-gate 
271*0Sstevel@tonic-gate #ifndef OPENSSL_NO_ENGINE
272*0Sstevel@tonic-gate         e = setup_engine(bio_err, engine, 0);
273*0Sstevel@tonic-gate #endif
274*0Sstevel@tonic-gate 
275*0Sstevel@tonic-gate 	if (g && !num)
276*0Sstevel@tonic-gate 		num = DEFBITS;
277*0Sstevel@tonic-gate 
278*0Sstevel@tonic-gate #ifndef OPENSSL_NO_DSA
279*0Sstevel@tonic-gate 	if (dsaparam)
280*0Sstevel@tonic-gate 		{
281*0Sstevel@tonic-gate 		if (g)
282*0Sstevel@tonic-gate 			{
283*0Sstevel@tonic-gate 			BIO_printf(bio_err, "generator may not be chosen for DSA parameters\n");
284*0Sstevel@tonic-gate 			goto end;
285*0Sstevel@tonic-gate 			}
286*0Sstevel@tonic-gate 		}
287*0Sstevel@tonic-gate 	else
288*0Sstevel@tonic-gate #endif
289*0Sstevel@tonic-gate 		{
290*0Sstevel@tonic-gate 		/* DH parameters */
291*0Sstevel@tonic-gate 		if (num && !g)
292*0Sstevel@tonic-gate 			g = 2;
293*0Sstevel@tonic-gate 		}
294*0Sstevel@tonic-gate 
295*0Sstevel@tonic-gate 	if(num) {
296*0Sstevel@tonic-gate 
297*0Sstevel@tonic-gate 		if (!app_RAND_load_file(NULL, bio_err, 1) && inrand == NULL)
298*0Sstevel@tonic-gate 			{
299*0Sstevel@tonic-gate 			BIO_printf(bio_err,"warning, not much extra random data, consider using the -rand option\n");
300*0Sstevel@tonic-gate 			}
301*0Sstevel@tonic-gate 		if (inrand != NULL)
302*0Sstevel@tonic-gate 			BIO_printf(bio_err,"%ld semi-random bytes loaded\n",
303*0Sstevel@tonic-gate 				app_RAND_load_files(inrand));
304*0Sstevel@tonic-gate 
305*0Sstevel@tonic-gate #ifndef OPENSSL_NO_DSA
306*0Sstevel@tonic-gate 		if (dsaparam)
307*0Sstevel@tonic-gate 			{
308*0Sstevel@tonic-gate 			DSA *dsa;
309*0Sstevel@tonic-gate 
310*0Sstevel@tonic-gate 			BIO_printf(bio_err,"Generating DSA parameters, %d bit long prime\n",num);
311*0Sstevel@tonic-gate 			dsa = DSA_generate_parameters(num, NULL, 0, NULL, NULL, dh_cb, bio_err);
312*0Sstevel@tonic-gate 			if (dsa == NULL)
313*0Sstevel@tonic-gate 				{
314*0Sstevel@tonic-gate 				ERR_print_errors(bio_err);
315*0Sstevel@tonic-gate 				goto end;
316*0Sstevel@tonic-gate 				}
317*0Sstevel@tonic-gate 
318*0Sstevel@tonic-gate 			dh = DSA_dup_DH(dsa);
319*0Sstevel@tonic-gate 			DSA_free(dsa);
320*0Sstevel@tonic-gate 			if (dh == NULL)
321*0Sstevel@tonic-gate 				{
322*0Sstevel@tonic-gate 				ERR_print_errors(bio_err);
323*0Sstevel@tonic-gate 				goto end;
324*0Sstevel@tonic-gate 				}
325*0Sstevel@tonic-gate 			}
326*0Sstevel@tonic-gate 		else
327*0Sstevel@tonic-gate #endif
328*0Sstevel@tonic-gate 			{
329*0Sstevel@tonic-gate 			BIO_printf(bio_err,"Generating DH parameters, %d bit long safe prime, generator %d\n",num,g);
330*0Sstevel@tonic-gate 			BIO_printf(bio_err,"This is going to take a long time\n");
331*0Sstevel@tonic-gate 			dh=DH_generate_parameters(num,g,dh_cb,bio_err);
332*0Sstevel@tonic-gate 
333*0Sstevel@tonic-gate 			if (dh == NULL)
334*0Sstevel@tonic-gate 				{
335*0Sstevel@tonic-gate 				ERR_print_errors(bio_err);
336*0Sstevel@tonic-gate 				goto end;
337*0Sstevel@tonic-gate 				}
338*0Sstevel@tonic-gate 			}
339*0Sstevel@tonic-gate 
340*0Sstevel@tonic-gate 		app_RAND_write_file(NULL, bio_err);
341*0Sstevel@tonic-gate 	} else {
342*0Sstevel@tonic-gate 
343*0Sstevel@tonic-gate 		in=BIO_new(BIO_s_file());
344*0Sstevel@tonic-gate 		if (in == NULL)
345*0Sstevel@tonic-gate 			{
346*0Sstevel@tonic-gate 			ERR_print_errors(bio_err);
347*0Sstevel@tonic-gate 			goto end;
348*0Sstevel@tonic-gate 			}
349*0Sstevel@tonic-gate 		if (infile == NULL)
350*0Sstevel@tonic-gate 			BIO_set_fp(in,stdin,BIO_NOCLOSE);
351*0Sstevel@tonic-gate 		else
352*0Sstevel@tonic-gate 			{
353*0Sstevel@tonic-gate 			if (BIO_read_filename(in,infile) <= 0)
354*0Sstevel@tonic-gate 				{
355*0Sstevel@tonic-gate 				perror(infile);
356*0Sstevel@tonic-gate 				goto end;
357*0Sstevel@tonic-gate 				}
358*0Sstevel@tonic-gate 			}
359*0Sstevel@tonic-gate 
360*0Sstevel@tonic-gate 		if	(informat != FORMAT_ASN1 && informat != FORMAT_PEM)
361*0Sstevel@tonic-gate 			{
362*0Sstevel@tonic-gate 			BIO_printf(bio_err,"bad input format specified\n");
363*0Sstevel@tonic-gate 			goto end;
364*0Sstevel@tonic-gate 			}
365*0Sstevel@tonic-gate 
366*0Sstevel@tonic-gate #ifndef OPENSSL_NO_DSA
367*0Sstevel@tonic-gate 		if (dsaparam)
368*0Sstevel@tonic-gate 			{
369*0Sstevel@tonic-gate 			DSA *dsa;
370*0Sstevel@tonic-gate 
371*0Sstevel@tonic-gate 			if (informat == FORMAT_ASN1)
372*0Sstevel@tonic-gate 				dsa=d2i_DSAparams_bio(in,NULL);
373*0Sstevel@tonic-gate 			else /* informat == FORMAT_PEM */
374*0Sstevel@tonic-gate 				dsa=PEM_read_bio_DSAparams(in,NULL,NULL,NULL);
375*0Sstevel@tonic-gate 
376*0Sstevel@tonic-gate 			if (dsa == NULL)
377*0Sstevel@tonic-gate 				{
378*0Sstevel@tonic-gate 				BIO_printf(bio_err,"unable to load DSA parameters\n");
379*0Sstevel@tonic-gate 				ERR_print_errors(bio_err);
380*0Sstevel@tonic-gate 				goto end;
381*0Sstevel@tonic-gate 				}
382*0Sstevel@tonic-gate 
383*0Sstevel@tonic-gate 			dh = DSA_dup_DH(dsa);
384*0Sstevel@tonic-gate 			DSA_free(dsa);
385*0Sstevel@tonic-gate 			if (dh == NULL)
386*0Sstevel@tonic-gate 				{
387*0Sstevel@tonic-gate 				ERR_print_errors(bio_err);
388*0Sstevel@tonic-gate 				goto end;
389*0Sstevel@tonic-gate 				}
390*0Sstevel@tonic-gate 			}
391*0Sstevel@tonic-gate 		else
392*0Sstevel@tonic-gate #endif
393*0Sstevel@tonic-gate 			{
394*0Sstevel@tonic-gate 			if (informat == FORMAT_ASN1)
395*0Sstevel@tonic-gate 				dh=d2i_DHparams_bio(in,NULL);
396*0Sstevel@tonic-gate 			else /* informat == FORMAT_PEM */
397*0Sstevel@tonic-gate 				dh=PEM_read_bio_DHparams(in,NULL,NULL,NULL);
398*0Sstevel@tonic-gate 
399*0Sstevel@tonic-gate 			if (dh == NULL)
400*0Sstevel@tonic-gate 				{
401*0Sstevel@tonic-gate 				BIO_printf(bio_err,"unable to load DH parameters\n");
402*0Sstevel@tonic-gate 				ERR_print_errors(bio_err);
403*0Sstevel@tonic-gate 				goto end;
404*0Sstevel@tonic-gate 				}
405*0Sstevel@tonic-gate 			}
406*0Sstevel@tonic-gate 
407*0Sstevel@tonic-gate 		/* dh != NULL */
408*0Sstevel@tonic-gate 	}
409*0Sstevel@tonic-gate 
410*0Sstevel@tonic-gate 	out=BIO_new(BIO_s_file());
411*0Sstevel@tonic-gate 	if (out == NULL)
412*0Sstevel@tonic-gate 		{
413*0Sstevel@tonic-gate 		ERR_print_errors(bio_err);
414*0Sstevel@tonic-gate 		goto end;
415*0Sstevel@tonic-gate 		}
416*0Sstevel@tonic-gate 	if (outfile == NULL)
417*0Sstevel@tonic-gate 		{
418*0Sstevel@tonic-gate 		BIO_set_fp(out,stdout,BIO_NOCLOSE);
419*0Sstevel@tonic-gate #ifdef OPENSSL_SYS_VMS
420*0Sstevel@tonic-gate 		{
421*0Sstevel@tonic-gate 		BIO *tmpbio = BIO_new(BIO_f_linebuffer());
422*0Sstevel@tonic-gate 		out = BIO_push(tmpbio, out);
423*0Sstevel@tonic-gate 		}
424*0Sstevel@tonic-gate #endif
425*0Sstevel@tonic-gate 		}
426*0Sstevel@tonic-gate 	else
427*0Sstevel@tonic-gate 		{
428*0Sstevel@tonic-gate 		if (BIO_write_filename(out,outfile) <= 0)
429*0Sstevel@tonic-gate 			{
430*0Sstevel@tonic-gate 			perror(outfile);
431*0Sstevel@tonic-gate 			goto end;
432*0Sstevel@tonic-gate 			}
433*0Sstevel@tonic-gate 		}
434*0Sstevel@tonic-gate 
435*0Sstevel@tonic-gate 
436*0Sstevel@tonic-gate 	if (text)
437*0Sstevel@tonic-gate 		{
438*0Sstevel@tonic-gate 		DHparams_print(out,dh);
439*0Sstevel@tonic-gate 		}
440*0Sstevel@tonic-gate 
441*0Sstevel@tonic-gate 	if (check)
442*0Sstevel@tonic-gate 		{
443*0Sstevel@tonic-gate 		if (!DH_check(dh,&i))
444*0Sstevel@tonic-gate 			{
445*0Sstevel@tonic-gate 			ERR_print_errors(bio_err);
446*0Sstevel@tonic-gate 			goto end;
447*0Sstevel@tonic-gate 			}
448*0Sstevel@tonic-gate 		if (i & DH_CHECK_P_NOT_PRIME)
449*0Sstevel@tonic-gate 			printf("p value is not prime\n");
450*0Sstevel@tonic-gate 		if (i & DH_CHECK_P_NOT_SAFE_PRIME)
451*0Sstevel@tonic-gate 			printf("p value is not a safe prime\n");
452*0Sstevel@tonic-gate 		if (i & DH_UNABLE_TO_CHECK_GENERATOR)
453*0Sstevel@tonic-gate 			printf("unable to check the generator value\n");
454*0Sstevel@tonic-gate 		if (i & DH_NOT_SUITABLE_GENERATOR)
455*0Sstevel@tonic-gate 			printf("the g value is not a generator\n");
456*0Sstevel@tonic-gate 		if (i == 0)
457*0Sstevel@tonic-gate 			printf("DH parameters appear to be ok.\n");
458*0Sstevel@tonic-gate 		}
459*0Sstevel@tonic-gate 	if (C)
460*0Sstevel@tonic-gate 		{
461*0Sstevel@tonic-gate 		unsigned char *data;
462*0Sstevel@tonic-gate 		int len,l,bits;
463*0Sstevel@tonic-gate 
464*0Sstevel@tonic-gate 		len=BN_num_bytes(dh->p);
465*0Sstevel@tonic-gate 		bits=BN_num_bits(dh->p);
466*0Sstevel@tonic-gate 		data=(unsigned char *)OPENSSL_malloc(len);
467*0Sstevel@tonic-gate 		if (data == NULL)
468*0Sstevel@tonic-gate 			{
469*0Sstevel@tonic-gate 			perror("OPENSSL_malloc");
470*0Sstevel@tonic-gate 			goto end;
471*0Sstevel@tonic-gate 			}
472*0Sstevel@tonic-gate 		printf("#ifndef HEADER_DH_H\n"
473*0Sstevel@tonic-gate 		       "#include <openssl/dh.h>\n"
474*0Sstevel@tonic-gate 		       "#endif\n");
475*0Sstevel@tonic-gate 		printf("DH *get_dh%d()\n\t{\n",bits);
476*0Sstevel@tonic-gate 
477*0Sstevel@tonic-gate 		l=BN_bn2bin(dh->p,data);
478*0Sstevel@tonic-gate 		printf("\tstatic unsigned char dh%d_p[]={",bits);
479*0Sstevel@tonic-gate 		for (i=0; i<l; i++)
480*0Sstevel@tonic-gate 			{
481*0Sstevel@tonic-gate 			if ((i%12) == 0) printf("\n\t\t");
482*0Sstevel@tonic-gate 			printf("0x%02X,",data[i]);
483*0Sstevel@tonic-gate 			}
484*0Sstevel@tonic-gate 		printf("\n\t\t};\n");
485*0Sstevel@tonic-gate 
486*0Sstevel@tonic-gate 		l=BN_bn2bin(dh->g,data);
487*0Sstevel@tonic-gate 		printf("\tstatic unsigned char dh%d_g[]={",bits);
488*0Sstevel@tonic-gate 		for (i=0; i<l; i++)
489*0Sstevel@tonic-gate 			{
490*0Sstevel@tonic-gate 			if ((i%12) == 0) printf("\n\t\t");
491*0Sstevel@tonic-gate 			printf("0x%02X,",data[i]);
492*0Sstevel@tonic-gate 			}
493*0Sstevel@tonic-gate 		printf("\n\t\t};\n");
494*0Sstevel@tonic-gate 
495*0Sstevel@tonic-gate 		printf("\tDH *dh;\n\n");
496*0Sstevel@tonic-gate 		printf("\tif ((dh=DH_new()) == NULL) return(NULL);\n");
497*0Sstevel@tonic-gate 		printf("\tdh->p=BN_bin2bn(dh%d_p,sizeof(dh%d_p),NULL);\n",
498*0Sstevel@tonic-gate 			bits,bits);
499*0Sstevel@tonic-gate 		printf("\tdh->g=BN_bin2bn(dh%d_g,sizeof(dh%d_g),NULL);\n",
500*0Sstevel@tonic-gate 			bits,bits);
501*0Sstevel@tonic-gate 		printf("\tif ((dh->p == NULL) || (dh->g == NULL))\n");
502*0Sstevel@tonic-gate 		printf("\t\t{ DH_free(dh); return(NULL); }\n");
503*0Sstevel@tonic-gate 		if (dh->length)
504*0Sstevel@tonic-gate 			printf("\tdh->length = %ld;\n", dh->length);
505*0Sstevel@tonic-gate 		printf("\treturn(dh);\n\t}\n");
506*0Sstevel@tonic-gate 		OPENSSL_free(data);
507*0Sstevel@tonic-gate 		}
508*0Sstevel@tonic-gate 
509*0Sstevel@tonic-gate 
510*0Sstevel@tonic-gate 	if (!noout)
511*0Sstevel@tonic-gate 		{
512*0Sstevel@tonic-gate 		if 	(outformat == FORMAT_ASN1)
513*0Sstevel@tonic-gate 			i=i2d_DHparams_bio(out,dh);
514*0Sstevel@tonic-gate 		else if (outformat == FORMAT_PEM)
515*0Sstevel@tonic-gate 			i=PEM_write_bio_DHparams(out,dh);
516*0Sstevel@tonic-gate 		else	{
517*0Sstevel@tonic-gate 			BIO_printf(bio_err,"bad output format specified for outfile\n");
518*0Sstevel@tonic-gate 			goto end;
519*0Sstevel@tonic-gate 			}
520*0Sstevel@tonic-gate 		if (!i)
521*0Sstevel@tonic-gate 			{
522*0Sstevel@tonic-gate 			BIO_printf(bio_err,"unable to write DH parameters\n");
523*0Sstevel@tonic-gate 			ERR_print_errors(bio_err);
524*0Sstevel@tonic-gate 			goto end;
525*0Sstevel@tonic-gate 			}
526*0Sstevel@tonic-gate 		}
527*0Sstevel@tonic-gate 	ret=0;
528*0Sstevel@tonic-gate end:
529*0Sstevel@tonic-gate 	if (in != NULL) BIO_free(in);
530*0Sstevel@tonic-gate 	if (out != NULL) BIO_free_all(out);
531*0Sstevel@tonic-gate 	if (dh != NULL) DH_free(dh);
532*0Sstevel@tonic-gate 	apps_shutdown();
533*0Sstevel@tonic-gate 	OPENSSL_EXIT(ret);
534*0Sstevel@tonic-gate 	}
535*0Sstevel@tonic-gate 
536*0Sstevel@tonic-gate /* dh_cb is identical to dsa_cb in apps/dsaparam.c */
537*0Sstevel@tonic-gate static void MS_CALLBACK dh_cb(int p, int n, void *arg)
538*0Sstevel@tonic-gate 	{
539*0Sstevel@tonic-gate 	char c='*';
540*0Sstevel@tonic-gate 
541*0Sstevel@tonic-gate 	if (p == 0) c='.';
542*0Sstevel@tonic-gate 	if (p == 1) c='+';
543*0Sstevel@tonic-gate 	if (p == 2) c='*';
544*0Sstevel@tonic-gate 	if (p == 3) c='\n';
545*0Sstevel@tonic-gate 	BIO_write((BIO *)arg,&c,1);
546*0Sstevel@tonic-gate 	(void)BIO_flush((BIO *)arg);
547*0Sstevel@tonic-gate #ifdef LINT
548*0Sstevel@tonic-gate 	p=n;
549*0Sstevel@tonic-gate #endif
550*0Sstevel@tonic-gate 	}
551*0Sstevel@tonic-gate 
552*0Sstevel@tonic-gate #endif
553