xref: /onnv-gate/usr/src/common/openssl/apps/dhparam.c (revision 2139:6243c3338933)
10Sstevel@tonic-gate /* apps/dhparam.c */
20Sstevel@tonic-gate /* Copyright (C) 1995-1998 Eric Young (eay@cryptsoft.com)
30Sstevel@tonic-gate  * All rights reserved.
40Sstevel@tonic-gate  *
50Sstevel@tonic-gate  * This package is an SSL implementation written
60Sstevel@tonic-gate  * by Eric Young (eay@cryptsoft.com).
70Sstevel@tonic-gate  * The implementation was written so as to conform with Netscapes SSL.
80Sstevel@tonic-gate  *
90Sstevel@tonic-gate  * This library is free for commercial and non-commercial use as long as
100Sstevel@tonic-gate  * the following conditions are aheared to.  The following conditions
110Sstevel@tonic-gate  * apply to all code found in this distribution, be it the RC4, RSA,
120Sstevel@tonic-gate  * lhash, DES, etc., code; not just the SSL code.  The SSL documentation
130Sstevel@tonic-gate  * included with this distribution is covered by the same copyright terms
140Sstevel@tonic-gate  * except that the holder is Tim Hudson (tjh@cryptsoft.com).
150Sstevel@tonic-gate  *
160Sstevel@tonic-gate  * Copyright remains Eric Young's, and as such any Copyright notices in
170Sstevel@tonic-gate  * the code are not to be removed.
180Sstevel@tonic-gate  * If this package is used in a product, Eric Young should be given attribution
190Sstevel@tonic-gate  * as the author of the parts of the library used.
200Sstevel@tonic-gate  * This can be in the form of a textual message at program startup or
210Sstevel@tonic-gate  * in documentation (online or textual) provided with the package.
220Sstevel@tonic-gate  *
230Sstevel@tonic-gate  * Redistribution and use in source and binary forms, with or without
240Sstevel@tonic-gate  * modification, are permitted provided that the following conditions
250Sstevel@tonic-gate  * are met:
260Sstevel@tonic-gate  * 1. Redistributions of source code must retain the copyright
270Sstevel@tonic-gate  *    notice, this list of conditions and the following disclaimer.
280Sstevel@tonic-gate  * 2. Redistributions in binary form must reproduce the above copyright
290Sstevel@tonic-gate  *    notice, this list of conditions and the following disclaimer in the
300Sstevel@tonic-gate  *    documentation and/or other materials provided with the distribution.
310Sstevel@tonic-gate  * 3. All advertising materials mentioning features or use of this software
320Sstevel@tonic-gate  *    must display the following acknowledgement:
330Sstevel@tonic-gate  *    "This product includes cryptographic software written by
340Sstevel@tonic-gate  *     Eric Young (eay@cryptsoft.com)"
350Sstevel@tonic-gate  *    The word 'cryptographic' can be left out if the rouines from the library
360Sstevel@tonic-gate  *    being used are not cryptographic related :-).
370Sstevel@tonic-gate  * 4. If you include any Windows specific code (or a derivative thereof) from
380Sstevel@tonic-gate  *    the apps directory (application code) you must include an acknowledgement:
390Sstevel@tonic-gate  *    "This product includes software written by Tim Hudson (tjh@cryptsoft.com)"
400Sstevel@tonic-gate  *
410Sstevel@tonic-gate  * THIS SOFTWARE IS PROVIDED BY ERIC YOUNG ``AS IS'' AND
420Sstevel@tonic-gate  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
430Sstevel@tonic-gate  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
440Sstevel@tonic-gate  * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
450Sstevel@tonic-gate  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
460Sstevel@tonic-gate  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
470Sstevel@tonic-gate  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
480Sstevel@tonic-gate  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
490Sstevel@tonic-gate  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
500Sstevel@tonic-gate  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
510Sstevel@tonic-gate  * SUCH DAMAGE.
520Sstevel@tonic-gate  *
530Sstevel@tonic-gate  * The licence and distribution terms for any publically available version or
540Sstevel@tonic-gate  * derivative of this code cannot be changed.  i.e. this code cannot simply be
550Sstevel@tonic-gate  * copied and put under another distribution licence
560Sstevel@tonic-gate  * [including the GNU Public Licence.]
570Sstevel@tonic-gate  */
580Sstevel@tonic-gate /* ====================================================================
590Sstevel@tonic-gate  * Copyright (c) 1998-2000 The OpenSSL Project.  All rights reserved.
600Sstevel@tonic-gate  *
610Sstevel@tonic-gate  * Redistribution and use in source and binary forms, with or without
620Sstevel@tonic-gate  * modification, are permitted provided that the following conditions
630Sstevel@tonic-gate  * are met:
640Sstevel@tonic-gate  *
650Sstevel@tonic-gate  * 1. Redistributions of source code must retain the above copyright
660Sstevel@tonic-gate  *    notice, this list of conditions and the following disclaimer.
670Sstevel@tonic-gate  *
680Sstevel@tonic-gate  * 2. Redistributions in binary form must reproduce the above copyright
690Sstevel@tonic-gate  *    notice, this list of conditions and the following disclaimer in
700Sstevel@tonic-gate  *    the documentation and/or other materials provided with the
710Sstevel@tonic-gate  *    distribution.
720Sstevel@tonic-gate  *
730Sstevel@tonic-gate  * 3. All advertising materials mentioning features or use of this
740Sstevel@tonic-gate  *    software must display the following acknowledgment:
750Sstevel@tonic-gate  *    "This product includes software developed by the OpenSSL Project
760Sstevel@tonic-gate  *    for use in the OpenSSL Toolkit. (http://www.openssl.org/)"
770Sstevel@tonic-gate  *
780Sstevel@tonic-gate  * 4. The names "OpenSSL Toolkit" and "OpenSSL Project" must not be used to
790Sstevel@tonic-gate  *    endorse or promote products derived from this software without
800Sstevel@tonic-gate  *    prior written permission. For written permission, please contact
810Sstevel@tonic-gate  *    openssl-core@openssl.org.
820Sstevel@tonic-gate  *
830Sstevel@tonic-gate  * 5. Products derived from this software may not be called "OpenSSL"
840Sstevel@tonic-gate  *    nor may "OpenSSL" appear in their names without prior written
850Sstevel@tonic-gate  *    permission of the OpenSSL Project.
860Sstevel@tonic-gate  *
870Sstevel@tonic-gate  * 6. Redistributions of any form whatsoever must retain the following
880Sstevel@tonic-gate  *    acknowledgment:
890Sstevel@tonic-gate  *    "This product includes software developed by the OpenSSL Project
900Sstevel@tonic-gate  *    for use in the OpenSSL Toolkit (http://www.openssl.org/)"
910Sstevel@tonic-gate  *
920Sstevel@tonic-gate  * THIS SOFTWARE IS PROVIDED BY THE OpenSSL PROJECT ``AS IS'' AND ANY
930Sstevel@tonic-gate  * EXPRESSED OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
940Sstevel@tonic-gate  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
950Sstevel@tonic-gate  * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE OpenSSL PROJECT OR
960Sstevel@tonic-gate  * ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
970Sstevel@tonic-gate  * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
980Sstevel@tonic-gate  * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
990Sstevel@tonic-gate  * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
1000Sstevel@tonic-gate  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
1010Sstevel@tonic-gate  * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
1020Sstevel@tonic-gate  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED
1030Sstevel@tonic-gate  * OF THE POSSIBILITY OF SUCH DAMAGE.
1040Sstevel@tonic-gate  * ====================================================================
1050Sstevel@tonic-gate  *
1060Sstevel@tonic-gate  * This product includes cryptographic software written by Eric Young
1070Sstevel@tonic-gate  * (eay@cryptsoft.com).  This product includes software written by Tim
1080Sstevel@tonic-gate  * Hudson (tjh@cryptsoft.com).
1090Sstevel@tonic-gate  *
1100Sstevel@tonic-gate  */
1110Sstevel@tonic-gate 
112*2139Sjp161948 #include <openssl/opensslconf.h>	/* for OPENSSL_NO_DH */
1130Sstevel@tonic-gate #ifndef OPENSSL_NO_DH
1140Sstevel@tonic-gate #include <stdio.h>
1150Sstevel@tonic-gate #include <stdlib.h>
1160Sstevel@tonic-gate #include <time.h>
1170Sstevel@tonic-gate #include <string.h>
1180Sstevel@tonic-gate #include "apps.h"
1190Sstevel@tonic-gate #include <openssl/bio.h>
1200Sstevel@tonic-gate #include <openssl/err.h>
1210Sstevel@tonic-gate #include <openssl/bn.h>
1220Sstevel@tonic-gate #include <openssl/dh.h>
1230Sstevel@tonic-gate #include <openssl/x509.h>
1240Sstevel@tonic-gate #include <openssl/pem.h>
1250Sstevel@tonic-gate 
1260Sstevel@tonic-gate #ifndef OPENSSL_NO_DSA
1270Sstevel@tonic-gate #include <openssl/dsa.h>
1280Sstevel@tonic-gate #endif
1290Sstevel@tonic-gate 
1300Sstevel@tonic-gate #undef PROG
1310Sstevel@tonic-gate #define PROG	dhparam_main
1320Sstevel@tonic-gate 
1330Sstevel@tonic-gate #define DEFBITS	512
1340Sstevel@tonic-gate 
1350Sstevel@tonic-gate /* -inform arg	- input format - default PEM (DER or PEM)
1360Sstevel@tonic-gate  * -outform arg - output format - default PEM
1370Sstevel@tonic-gate  * -in arg	- input file - default stdin
1380Sstevel@tonic-gate  * -out arg	- output file - default stdout
1390Sstevel@tonic-gate  * -dsaparam  - read or generate DSA parameters, convert to DH
1400Sstevel@tonic-gate  * -check	- check the parameters are ok
1410Sstevel@tonic-gate  * -noout
1420Sstevel@tonic-gate  * -text
1430Sstevel@tonic-gate  * -C
1440Sstevel@tonic-gate  */
1450Sstevel@tonic-gate 
146*2139Sjp161948 static int MS_CALLBACK dh_cb(int p, int n, BN_GENCB *cb);
1470Sstevel@tonic-gate 
1480Sstevel@tonic-gate int MAIN(int, char **);
1490Sstevel@tonic-gate 
MAIN(int argc,char ** argv)1500Sstevel@tonic-gate int MAIN(int argc, char **argv)
1510Sstevel@tonic-gate 	{
1520Sstevel@tonic-gate #ifndef OPENSSL_NO_ENGINE
1530Sstevel@tonic-gate 	ENGINE *e = NULL;
1540Sstevel@tonic-gate #endif
1550Sstevel@tonic-gate 	DH *dh=NULL;
1560Sstevel@tonic-gate 	int i,badops=0,text=0;
1570Sstevel@tonic-gate #ifndef OPENSSL_NO_DSA
1580Sstevel@tonic-gate 	int dsaparam=0;
1590Sstevel@tonic-gate #endif
1600Sstevel@tonic-gate 	BIO *in=NULL,*out=NULL;
1610Sstevel@tonic-gate 	int informat,outformat,check=0,noout=0,C=0,ret=1;
1620Sstevel@tonic-gate 	char *infile,*outfile,*prog;
1630Sstevel@tonic-gate 	char *inrand=NULL;
1640Sstevel@tonic-gate #ifndef OPENSSL_NO_ENGINE
1650Sstevel@tonic-gate 	char *engine=NULL;
1660Sstevel@tonic-gate #endif
1670Sstevel@tonic-gate 	int num = 0, g = 0;
1680Sstevel@tonic-gate 
1690Sstevel@tonic-gate 	apps_startup();
1700Sstevel@tonic-gate 
1710Sstevel@tonic-gate 	if (bio_err == NULL)
1720Sstevel@tonic-gate 		if ((bio_err=BIO_new(BIO_s_file())) != NULL)
1730Sstevel@tonic-gate 			BIO_set_fp(bio_err,stderr,BIO_NOCLOSE|BIO_FP_TEXT);
1740Sstevel@tonic-gate 
1750Sstevel@tonic-gate 	if (!load_config(bio_err, NULL))
1760Sstevel@tonic-gate 		goto end;
1770Sstevel@tonic-gate 
1780Sstevel@tonic-gate 	infile=NULL;
1790Sstevel@tonic-gate 	outfile=NULL;
1800Sstevel@tonic-gate 	informat=FORMAT_PEM;
1810Sstevel@tonic-gate 	outformat=FORMAT_PEM;
1820Sstevel@tonic-gate 
1830Sstevel@tonic-gate 	prog=argv[0];
1840Sstevel@tonic-gate 	argc--;
1850Sstevel@tonic-gate 	argv++;
1860Sstevel@tonic-gate 	while (argc >= 1)
1870Sstevel@tonic-gate 		{
1880Sstevel@tonic-gate 		if 	(strcmp(*argv,"-inform") == 0)
1890Sstevel@tonic-gate 			{
1900Sstevel@tonic-gate 			if (--argc < 1) goto bad;
1910Sstevel@tonic-gate 			informat=str2fmt(*(++argv));
1920Sstevel@tonic-gate 			}
1930Sstevel@tonic-gate 		else if (strcmp(*argv,"-outform") == 0)
1940Sstevel@tonic-gate 			{
1950Sstevel@tonic-gate 			if (--argc < 1) goto bad;
1960Sstevel@tonic-gate 			outformat=str2fmt(*(++argv));
1970Sstevel@tonic-gate 			}
1980Sstevel@tonic-gate 		else if (strcmp(*argv,"-in") == 0)
1990Sstevel@tonic-gate 			{
2000Sstevel@tonic-gate 			if (--argc < 1) goto bad;
2010Sstevel@tonic-gate 			infile= *(++argv);
2020Sstevel@tonic-gate 			}
2030Sstevel@tonic-gate 		else if (strcmp(*argv,"-out") == 0)
2040Sstevel@tonic-gate 			{
2050Sstevel@tonic-gate 			if (--argc < 1) goto bad;
2060Sstevel@tonic-gate 			outfile= *(++argv);
2070Sstevel@tonic-gate 			}
2080Sstevel@tonic-gate #ifndef OPENSSL_NO_ENGINE
2090Sstevel@tonic-gate 		else if (strcmp(*argv,"-engine") == 0)
2100Sstevel@tonic-gate 			{
2110Sstevel@tonic-gate 			if (--argc < 1) goto bad;
2120Sstevel@tonic-gate 			engine= *(++argv);
2130Sstevel@tonic-gate 			}
2140Sstevel@tonic-gate #endif
2150Sstevel@tonic-gate 		else if (strcmp(*argv,"-check") == 0)
2160Sstevel@tonic-gate 			check=1;
2170Sstevel@tonic-gate 		else if (strcmp(*argv,"-text") == 0)
2180Sstevel@tonic-gate 			text=1;
2190Sstevel@tonic-gate #ifndef OPENSSL_NO_DSA
2200Sstevel@tonic-gate 		else if (strcmp(*argv,"-dsaparam") == 0)
2210Sstevel@tonic-gate 			dsaparam=1;
2220Sstevel@tonic-gate #endif
2230Sstevel@tonic-gate 		else if (strcmp(*argv,"-C") == 0)
2240Sstevel@tonic-gate 			C=1;
2250Sstevel@tonic-gate 		else if (strcmp(*argv,"-noout") == 0)
2260Sstevel@tonic-gate 			noout=1;
2270Sstevel@tonic-gate 		else if (strcmp(*argv,"-2") == 0)
2280Sstevel@tonic-gate 			g=2;
2290Sstevel@tonic-gate 		else if (strcmp(*argv,"-5") == 0)
2300Sstevel@tonic-gate 			g=5;
2310Sstevel@tonic-gate 		else if (strcmp(*argv,"-rand") == 0)
2320Sstevel@tonic-gate 			{
2330Sstevel@tonic-gate 			if (--argc < 1) goto bad;
2340Sstevel@tonic-gate 			inrand= *(++argv);
2350Sstevel@tonic-gate 			}
2360Sstevel@tonic-gate 		else if (((sscanf(*argv,"%d",&num) == 0) || (num <= 0)))
2370Sstevel@tonic-gate 			goto bad;
2380Sstevel@tonic-gate 		argv++;
2390Sstevel@tonic-gate 		argc--;
2400Sstevel@tonic-gate 		}
2410Sstevel@tonic-gate 
2420Sstevel@tonic-gate 	if (badops)
2430Sstevel@tonic-gate 		{
2440Sstevel@tonic-gate bad:
2450Sstevel@tonic-gate 		BIO_printf(bio_err,"%s [options] [numbits]\n",prog);
2460Sstevel@tonic-gate 		BIO_printf(bio_err,"where options are\n");
2470Sstevel@tonic-gate 		BIO_printf(bio_err," -inform arg   input format - one of DER PEM\n");
2480Sstevel@tonic-gate 		BIO_printf(bio_err," -outform arg  output format - one of DER PEM\n");
2490Sstevel@tonic-gate 		BIO_printf(bio_err," -in arg       input file\n");
2500Sstevel@tonic-gate 		BIO_printf(bio_err," -out arg      output file\n");
2510Sstevel@tonic-gate #ifndef OPENSSL_NO_DSA
2520Sstevel@tonic-gate 		BIO_printf(bio_err," -dsaparam     read or generate DSA parameters, convert to DH\n");
2530Sstevel@tonic-gate #endif
2540Sstevel@tonic-gate 		BIO_printf(bio_err," -check        check the DH parameters\n");
2550Sstevel@tonic-gate 		BIO_printf(bio_err," -text         print a text form of the DH parameters\n");
2560Sstevel@tonic-gate 		BIO_printf(bio_err," -C            Output C code\n");
2570Sstevel@tonic-gate 		BIO_printf(bio_err," -2            generate parameters using  2 as the generator value\n");
2580Sstevel@tonic-gate 		BIO_printf(bio_err," -5            generate parameters using  5 as the generator value\n");
2590Sstevel@tonic-gate 		BIO_printf(bio_err," numbits       number of bits in to generate (default 512)\n");
2600Sstevel@tonic-gate #ifndef OPENSSL_NO_ENGINE
2610Sstevel@tonic-gate 		BIO_printf(bio_err," -engine e     use engine e, possibly a hardware device.\n");
2620Sstevel@tonic-gate #endif
2630Sstevel@tonic-gate 		BIO_printf(bio_err," -rand file%cfile%c...\n", LIST_SEPARATOR_CHAR, LIST_SEPARATOR_CHAR);
2640Sstevel@tonic-gate 		BIO_printf(bio_err,"               - load the file (or the files in the directory) into\n");
2650Sstevel@tonic-gate 		BIO_printf(bio_err,"               the random number generator\n");
2660Sstevel@tonic-gate 		BIO_printf(bio_err," -noout        no output\n");
2670Sstevel@tonic-gate 		goto end;
2680Sstevel@tonic-gate 		}
2690Sstevel@tonic-gate 
2700Sstevel@tonic-gate 	ERR_load_crypto_strings();
2710Sstevel@tonic-gate 
2720Sstevel@tonic-gate #ifndef OPENSSL_NO_ENGINE
2730Sstevel@tonic-gate         e = setup_engine(bio_err, engine, 0);
2740Sstevel@tonic-gate #endif
2750Sstevel@tonic-gate 
2760Sstevel@tonic-gate 	if (g && !num)
2770Sstevel@tonic-gate 		num = DEFBITS;
2780Sstevel@tonic-gate 
2790Sstevel@tonic-gate #ifndef OPENSSL_NO_DSA
2800Sstevel@tonic-gate 	if (dsaparam)
2810Sstevel@tonic-gate 		{
2820Sstevel@tonic-gate 		if (g)
2830Sstevel@tonic-gate 			{
2840Sstevel@tonic-gate 			BIO_printf(bio_err, "generator may not be chosen for DSA parameters\n");
2850Sstevel@tonic-gate 			goto end;
2860Sstevel@tonic-gate 			}
2870Sstevel@tonic-gate 		}
2880Sstevel@tonic-gate 	else
2890Sstevel@tonic-gate #endif
2900Sstevel@tonic-gate 		{
2910Sstevel@tonic-gate 		/* DH parameters */
2920Sstevel@tonic-gate 		if (num && !g)
2930Sstevel@tonic-gate 			g = 2;
2940Sstevel@tonic-gate 		}
2950Sstevel@tonic-gate 
2960Sstevel@tonic-gate 	if(num) {
2970Sstevel@tonic-gate 
298*2139Sjp161948 		BN_GENCB cb;
299*2139Sjp161948 		BN_GENCB_set(&cb, dh_cb, bio_err);
3000Sstevel@tonic-gate 		if (!app_RAND_load_file(NULL, bio_err, 1) && inrand == NULL)
3010Sstevel@tonic-gate 			{
3020Sstevel@tonic-gate 			BIO_printf(bio_err,"warning, not much extra random data, consider using the -rand option\n");
3030Sstevel@tonic-gate 			}
3040Sstevel@tonic-gate 		if (inrand != NULL)
3050Sstevel@tonic-gate 			BIO_printf(bio_err,"%ld semi-random bytes loaded\n",
3060Sstevel@tonic-gate 				app_RAND_load_files(inrand));
3070Sstevel@tonic-gate 
3080Sstevel@tonic-gate #ifndef OPENSSL_NO_DSA
3090Sstevel@tonic-gate 		if (dsaparam)
3100Sstevel@tonic-gate 			{
311*2139Sjp161948 			DSA *dsa = DSA_new();
3120Sstevel@tonic-gate 
3130Sstevel@tonic-gate 			BIO_printf(bio_err,"Generating DSA parameters, %d bit long prime\n",num);
314*2139Sjp161948 			if(!dsa || !DSA_generate_parameters_ex(dsa, num,
315*2139Sjp161948 						NULL, 0, NULL, NULL, &cb))
3160Sstevel@tonic-gate 				{
317*2139Sjp161948 				if(dsa) DSA_free(dsa);
3180Sstevel@tonic-gate 				ERR_print_errors(bio_err);
3190Sstevel@tonic-gate 				goto end;
3200Sstevel@tonic-gate 				}
3210Sstevel@tonic-gate 
3220Sstevel@tonic-gate 			dh = DSA_dup_DH(dsa);
3230Sstevel@tonic-gate 			DSA_free(dsa);
3240Sstevel@tonic-gate 			if (dh == NULL)
3250Sstevel@tonic-gate 				{
3260Sstevel@tonic-gate 				ERR_print_errors(bio_err);
3270Sstevel@tonic-gate 				goto end;
3280Sstevel@tonic-gate 				}
3290Sstevel@tonic-gate 			}
3300Sstevel@tonic-gate 		else
3310Sstevel@tonic-gate #endif
3320Sstevel@tonic-gate 			{
333*2139Sjp161948 			dh = DH_new();
3340Sstevel@tonic-gate 			BIO_printf(bio_err,"Generating DH parameters, %d bit long safe prime, generator %d\n",num,g);
3350Sstevel@tonic-gate 			BIO_printf(bio_err,"This is going to take a long time\n");
336*2139Sjp161948 			if(!dh || !DH_generate_parameters_ex(dh, num, g, &cb))
3370Sstevel@tonic-gate 				{
338*2139Sjp161948 				if(dh) DH_free(dh);
3390Sstevel@tonic-gate 				ERR_print_errors(bio_err);
3400Sstevel@tonic-gate 				goto end;
3410Sstevel@tonic-gate 				}
3420Sstevel@tonic-gate 			}
3430Sstevel@tonic-gate 
3440Sstevel@tonic-gate 		app_RAND_write_file(NULL, bio_err);
3450Sstevel@tonic-gate 	} else {
3460Sstevel@tonic-gate 
3470Sstevel@tonic-gate 		in=BIO_new(BIO_s_file());
3480Sstevel@tonic-gate 		if (in == NULL)
3490Sstevel@tonic-gate 			{
3500Sstevel@tonic-gate 			ERR_print_errors(bio_err);
3510Sstevel@tonic-gate 			goto end;
3520Sstevel@tonic-gate 			}
3530Sstevel@tonic-gate 		if (infile == NULL)
3540Sstevel@tonic-gate 			BIO_set_fp(in,stdin,BIO_NOCLOSE);
3550Sstevel@tonic-gate 		else
3560Sstevel@tonic-gate 			{
3570Sstevel@tonic-gate 			if (BIO_read_filename(in,infile) <= 0)
3580Sstevel@tonic-gate 				{
3590Sstevel@tonic-gate 				perror(infile);
3600Sstevel@tonic-gate 				goto end;
3610Sstevel@tonic-gate 				}
3620Sstevel@tonic-gate 			}
3630Sstevel@tonic-gate 
3640Sstevel@tonic-gate 		if	(informat != FORMAT_ASN1 && informat != FORMAT_PEM)
3650Sstevel@tonic-gate 			{
3660Sstevel@tonic-gate 			BIO_printf(bio_err,"bad input format specified\n");
3670Sstevel@tonic-gate 			goto end;
3680Sstevel@tonic-gate 			}
3690Sstevel@tonic-gate 
3700Sstevel@tonic-gate #ifndef OPENSSL_NO_DSA
3710Sstevel@tonic-gate 		if (dsaparam)
3720Sstevel@tonic-gate 			{
3730Sstevel@tonic-gate 			DSA *dsa;
3740Sstevel@tonic-gate 
3750Sstevel@tonic-gate 			if (informat == FORMAT_ASN1)
3760Sstevel@tonic-gate 				dsa=d2i_DSAparams_bio(in,NULL);
3770Sstevel@tonic-gate 			else /* informat == FORMAT_PEM */
3780Sstevel@tonic-gate 				dsa=PEM_read_bio_DSAparams(in,NULL,NULL,NULL);
3790Sstevel@tonic-gate 
3800Sstevel@tonic-gate 			if (dsa == NULL)
3810Sstevel@tonic-gate 				{
3820Sstevel@tonic-gate 				BIO_printf(bio_err,"unable to load DSA parameters\n");
3830Sstevel@tonic-gate 				ERR_print_errors(bio_err);
3840Sstevel@tonic-gate 				goto end;
3850Sstevel@tonic-gate 				}
3860Sstevel@tonic-gate 
3870Sstevel@tonic-gate 			dh = DSA_dup_DH(dsa);
3880Sstevel@tonic-gate 			DSA_free(dsa);
3890Sstevel@tonic-gate 			if (dh == NULL)
3900Sstevel@tonic-gate 				{
3910Sstevel@tonic-gate 				ERR_print_errors(bio_err);
3920Sstevel@tonic-gate 				goto end;
3930Sstevel@tonic-gate 				}
3940Sstevel@tonic-gate 			}
3950Sstevel@tonic-gate 		else
3960Sstevel@tonic-gate #endif
3970Sstevel@tonic-gate 			{
3980Sstevel@tonic-gate 			if (informat == FORMAT_ASN1)
3990Sstevel@tonic-gate 				dh=d2i_DHparams_bio(in,NULL);
4000Sstevel@tonic-gate 			else /* informat == FORMAT_PEM */
4010Sstevel@tonic-gate 				dh=PEM_read_bio_DHparams(in,NULL,NULL,NULL);
4020Sstevel@tonic-gate 
4030Sstevel@tonic-gate 			if (dh == NULL)
4040Sstevel@tonic-gate 				{
4050Sstevel@tonic-gate 				BIO_printf(bio_err,"unable to load DH parameters\n");
4060Sstevel@tonic-gate 				ERR_print_errors(bio_err);
4070Sstevel@tonic-gate 				goto end;
4080Sstevel@tonic-gate 				}
4090Sstevel@tonic-gate 			}
4100Sstevel@tonic-gate 
4110Sstevel@tonic-gate 		/* dh != NULL */
4120Sstevel@tonic-gate 	}
4130Sstevel@tonic-gate 
4140Sstevel@tonic-gate 	out=BIO_new(BIO_s_file());
4150Sstevel@tonic-gate 	if (out == NULL)
4160Sstevel@tonic-gate 		{
4170Sstevel@tonic-gate 		ERR_print_errors(bio_err);
4180Sstevel@tonic-gate 		goto end;
4190Sstevel@tonic-gate 		}
4200Sstevel@tonic-gate 	if (outfile == NULL)
4210Sstevel@tonic-gate 		{
4220Sstevel@tonic-gate 		BIO_set_fp(out,stdout,BIO_NOCLOSE);
4230Sstevel@tonic-gate #ifdef OPENSSL_SYS_VMS
4240Sstevel@tonic-gate 		{
4250Sstevel@tonic-gate 		BIO *tmpbio = BIO_new(BIO_f_linebuffer());
4260Sstevel@tonic-gate 		out = BIO_push(tmpbio, out);
4270Sstevel@tonic-gate 		}
4280Sstevel@tonic-gate #endif
4290Sstevel@tonic-gate 		}
4300Sstevel@tonic-gate 	else
4310Sstevel@tonic-gate 		{
4320Sstevel@tonic-gate 		if (BIO_write_filename(out,outfile) <= 0)
4330Sstevel@tonic-gate 			{
4340Sstevel@tonic-gate 			perror(outfile);
4350Sstevel@tonic-gate 			goto end;
4360Sstevel@tonic-gate 			}
4370Sstevel@tonic-gate 		}
4380Sstevel@tonic-gate 
4390Sstevel@tonic-gate 
4400Sstevel@tonic-gate 	if (text)
4410Sstevel@tonic-gate 		{
4420Sstevel@tonic-gate 		DHparams_print(out,dh);
4430Sstevel@tonic-gate 		}
4440Sstevel@tonic-gate 
4450Sstevel@tonic-gate 	if (check)
4460Sstevel@tonic-gate 		{
4470Sstevel@tonic-gate 		if (!DH_check(dh,&i))
4480Sstevel@tonic-gate 			{
4490Sstevel@tonic-gate 			ERR_print_errors(bio_err);
4500Sstevel@tonic-gate 			goto end;
4510Sstevel@tonic-gate 			}
4520Sstevel@tonic-gate 		if (i & DH_CHECK_P_NOT_PRIME)
4530Sstevel@tonic-gate 			printf("p value is not prime\n");
4540Sstevel@tonic-gate 		if (i & DH_CHECK_P_NOT_SAFE_PRIME)
4550Sstevel@tonic-gate 			printf("p value is not a safe prime\n");
4560Sstevel@tonic-gate 		if (i & DH_UNABLE_TO_CHECK_GENERATOR)
4570Sstevel@tonic-gate 			printf("unable to check the generator value\n");
4580Sstevel@tonic-gate 		if (i & DH_NOT_SUITABLE_GENERATOR)
4590Sstevel@tonic-gate 			printf("the g value is not a generator\n");
4600Sstevel@tonic-gate 		if (i == 0)
4610Sstevel@tonic-gate 			printf("DH parameters appear to be ok.\n");
4620Sstevel@tonic-gate 		}
4630Sstevel@tonic-gate 	if (C)
4640Sstevel@tonic-gate 		{
4650Sstevel@tonic-gate 		unsigned char *data;
4660Sstevel@tonic-gate 		int len,l,bits;
4670Sstevel@tonic-gate 
4680Sstevel@tonic-gate 		len=BN_num_bytes(dh->p);
4690Sstevel@tonic-gate 		bits=BN_num_bits(dh->p);
4700Sstevel@tonic-gate 		data=(unsigned char *)OPENSSL_malloc(len);
4710Sstevel@tonic-gate 		if (data == NULL)
4720Sstevel@tonic-gate 			{
4730Sstevel@tonic-gate 			perror("OPENSSL_malloc");
4740Sstevel@tonic-gate 			goto end;
4750Sstevel@tonic-gate 			}
4760Sstevel@tonic-gate 		printf("#ifndef HEADER_DH_H\n"
4770Sstevel@tonic-gate 		       "#include <openssl/dh.h>\n"
4780Sstevel@tonic-gate 		       "#endif\n");
4790Sstevel@tonic-gate 		printf("DH *get_dh%d()\n\t{\n",bits);
4800Sstevel@tonic-gate 
4810Sstevel@tonic-gate 		l=BN_bn2bin(dh->p,data);
4820Sstevel@tonic-gate 		printf("\tstatic unsigned char dh%d_p[]={",bits);
4830Sstevel@tonic-gate 		for (i=0; i<l; i++)
4840Sstevel@tonic-gate 			{
4850Sstevel@tonic-gate 			if ((i%12) == 0) printf("\n\t\t");
4860Sstevel@tonic-gate 			printf("0x%02X,",data[i]);
4870Sstevel@tonic-gate 			}
4880Sstevel@tonic-gate 		printf("\n\t\t};\n");
4890Sstevel@tonic-gate 
4900Sstevel@tonic-gate 		l=BN_bn2bin(dh->g,data);
4910Sstevel@tonic-gate 		printf("\tstatic unsigned char dh%d_g[]={",bits);
4920Sstevel@tonic-gate 		for (i=0; i<l; i++)
4930Sstevel@tonic-gate 			{
4940Sstevel@tonic-gate 			if ((i%12) == 0) printf("\n\t\t");
4950Sstevel@tonic-gate 			printf("0x%02X,",data[i]);
4960Sstevel@tonic-gate 			}
4970Sstevel@tonic-gate 		printf("\n\t\t};\n");
4980Sstevel@tonic-gate 
4990Sstevel@tonic-gate 		printf("\tDH *dh;\n\n");
5000Sstevel@tonic-gate 		printf("\tif ((dh=DH_new()) == NULL) return(NULL);\n");
5010Sstevel@tonic-gate 		printf("\tdh->p=BN_bin2bn(dh%d_p,sizeof(dh%d_p),NULL);\n",
5020Sstevel@tonic-gate 			bits,bits);
5030Sstevel@tonic-gate 		printf("\tdh->g=BN_bin2bn(dh%d_g,sizeof(dh%d_g),NULL);\n",
5040Sstevel@tonic-gate 			bits,bits);
5050Sstevel@tonic-gate 		printf("\tif ((dh->p == NULL) || (dh->g == NULL))\n");
5060Sstevel@tonic-gate 		printf("\t\t{ DH_free(dh); return(NULL); }\n");
5070Sstevel@tonic-gate 		if (dh->length)
5080Sstevel@tonic-gate 			printf("\tdh->length = %ld;\n", dh->length);
5090Sstevel@tonic-gate 		printf("\treturn(dh);\n\t}\n");
5100Sstevel@tonic-gate 		OPENSSL_free(data);
5110Sstevel@tonic-gate 		}
5120Sstevel@tonic-gate 
5130Sstevel@tonic-gate 
5140Sstevel@tonic-gate 	if (!noout)
5150Sstevel@tonic-gate 		{
5160Sstevel@tonic-gate 		if 	(outformat == FORMAT_ASN1)
5170Sstevel@tonic-gate 			i=i2d_DHparams_bio(out,dh);
5180Sstevel@tonic-gate 		else if (outformat == FORMAT_PEM)
5190Sstevel@tonic-gate 			i=PEM_write_bio_DHparams(out,dh);
5200Sstevel@tonic-gate 		else	{
5210Sstevel@tonic-gate 			BIO_printf(bio_err,"bad output format specified for outfile\n");
5220Sstevel@tonic-gate 			goto end;
5230Sstevel@tonic-gate 			}
5240Sstevel@tonic-gate 		if (!i)
5250Sstevel@tonic-gate 			{
5260Sstevel@tonic-gate 			BIO_printf(bio_err,"unable to write DH parameters\n");
5270Sstevel@tonic-gate 			ERR_print_errors(bio_err);
5280Sstevel@tonic-gate 			goto end;
5290Sstevel@tonic-gate 			}
5300Sstevel@tonic-gate 		}
5310Sstevel@tonic-gate 	ret=0;
5320Sstevel@tonic-gate end:
5330Sstevel@tonic-gate 	if (in != NULL) BIO_free(in);
5340Sstevel@tonic-gate 	if (out != NULL) BIO_free_all(out);
5350Sstevel@tonic-gate 	if (dh != NULL) DH_free(dh);
5360Sstevel@tonic-gate 	apps_shutdown();
5370Sstevel@tonic-gate 	OPENSSL_EXIT(ret);
5380Sstevel@tonic-gate 	}
5390Sstevel@tonic-gate 
5400Sstevel@tonic-gate /* dh_cb is identical to dsa_cb in apps/dsaparam.c */
dh_cb(int p,int n,BN_GENCB * cb)541*2139Sjp161948 static int MS_CALLBACK dh_cb(int p, int n, BN_GENCB *cb)
5420Sstevel@tonic-gate 	{
5430Sstevel@tonic-gate 	char c='*';
5440Sstevel@tonic-gate 
5450Sstevel@tonic-gate 	if (p == 0) c='.';
5460Sstevel@tonic-gate 	if (p == 1) c='+';
5470Sstevel@tonic-gate 	if (p == 2) c='*';
5480Sstevel@tonic-gate 	if (p == 3) c='\n';
549*2139Sjp161948 	BIO_write(cb->arg,&c,1);
550*2139Sjp161948 	(void)BIO_flush(cb->arg);
5510Sstevel@tonic-gate #ifdef LINT
5520Sstevel@tonic-gate 	p=n;
5530Sstevel@tonic-gate #endif
554*2139Sjp161948 	return 1;
5550Sstevel@tonic-gate 	}
5560Sstevel@tonic-gate 
5570Sstevel@tonic-gate #endif
558