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