gendh.c revision 238405
155714Skris/* apps/gendh.c */
259191Skris/* obsoleted by dhparam.c */
355714Skris/* Copyright (C) 1995-1998 Eric Young (eay@cryptsoft.com)
455714Skris * All rights reserved.
555714Skris *
655714Skris * This package is an SSL implementation written
755714Skris * by Eric Young (eay@cryptsoft.com).
855714Skris * The implementation was written so as to conform with Netscapes SSL.
955714Skris *
1055714Skris * This library is free for commercial and non-commercial use as long as
1155714Skris * the following conditions are aheared to.  The following conditions
1255714Skris * apply to all code found in this distribution, be it the RC4, RSA,
1355714Skris * lhash, DES, etc., code; not just the SSL code.  The SSL documentation
1455714Skris * included with this distribution is covered by the same copyright terms
1555714Skris * except that the holder is Tim Hudson (tjh@cryptsoft.com).
1655714Skris *
1755714Skris * Copyright remains Eric Young's, and as such any Copyright notices in
1855714Skris * the code are not to be removed.
1955714Skris * If this package is used in a product, Eric Young should be given attribution
2055714Skris * as the author of the parts of the library used.
2155714Skris * This can be in the form of a textual message at program startup or
2255714Skris * in documentation (online or textual) provided with the package.
2355714Skris *
2455714Skris * Redistribution and use in source and binary forms, with or without
2555714Skris * modification, are permitted provided that the following conditions
2655714Skris * are met:
2755714Skris * 1. Redistributions of source code must retain the copyright
2855714Skris *    notice, this list of conditions and the following disclaimer.
2955714Skris * 2. Redistributions in binary form must reproduce the above copyright
3055714Skris *    notice, this list of conditions and the following disclaimer in the
3155714Skris *    documentation and/or other materials provided with the distribution.
3255714Skris * 3. All advertising materials mentioning features or use of this software
3355714Skris *    must display the following acknowledgement:
3455714Skris *    "This product includes cryptographic software written by
3555714Skris *     Eric Young (eay@cryptsoft.com)"
3655714Skris *    The word 'cryptographic' can be left out if the rouines from the library
3755714Skris *    being used are not cryptographic related :-).
3855714Skris * 4. If you include any Windows specific code (or a derivative thereof) from
3955714Skris *    the apps directory (application code) you must include an acknowledgement:
4055714Skris *    "This product includes software written by Tim Hudson (tjh@cryptsoft.com)"
4155714Skris *
4255714Skris * THIS SOFTWARE IS PROVIDED BY ERIC YOUNG ``AS IS'' AND
4355714Skris * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
4455714Skris * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
4555714Skris * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
4655714Skris * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
4755714Skris * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
4855714Skris * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
4955714Skris * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
5055714Skris * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
5155714Skris * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
5255714Skris * SUCH DAMAGE.
5355714Skris *
5455714Skris * The licence and distribution terms for any publically available version or
5555714Skris * derivative of this code cannot be changed.  i.e. this code cannot simply be
5655714Skris * copied and put under another distribution licence
5755714Skris * [including the GNU Public Licence.]
5855714Skris */
5955714Skris
60160814Ssimon#include <openssl/opensslconf.h>
61160814Ssimon/* Until the key-gen callbacks are modified to use newer prototypes, we allow
62160814Ssimon * deprecated functions for openssl-internal code */
63160814Ssimon#ifdef OPENSSL_NO_DEPRECATED
64160814Ssimon#undef OPENSSL_NO_DEPRECATED
65160814Ssimon#endif
66160814Ssimon
67109998Smarkm#ifndef OPENSSL_NO_DH
6855714Skris#include <stdio.h>
6955714Skris#include <string.h>
7055714Skris#include <sys/types.h>
7155714Skris#include <sys/stat.h>
7255714Skris#include "apps.h"
7355714Skris#include <openssl/bio.h>
7455714Skris#include <openssl/rand.h>
7555714Skris#include <openssl/err.h>
7655714Skris#include <openssl/bn.h>
7755714Skris#include <openssl/dh.h>
7855714Skris#include <openssl/x509.h>
7955714Skris#include <openssl/pem.h>
8055714Skris
8155714Skris#define DEFBITS	512
8255714Skris#undef PROG
8355714Skris#define PROG gendh_main
8455714Skris
85160814Ssimonstatic int MS_CALLBACK dh_cb(int p, int n, BN_GENCB *cb);
8659191Skris
8759191Skrisint MAIN(int, char **);
8859191Skris
8955714Skrisint MAIN(int argc, char **argv)
9055714Skris	{
91160814Ssimon	BN_GENCB cb;
9255714Skris	DH *dh=NULL;
9355714Skris	int ret=1,num=DEFBITS;
9455714Skris	int g=2;
9555714Skris	char *outfile=NULL;
9659191Skris	char *inrand=NULL;
97111147Snectar#ifndef OPENSSL_NO_ENGINE
98109998Smarkm	char *engine=NULL;
99111147Snectar#endif
10055714Skris	BIO *out=NULL;
10155714Skris
10255714Skris	apps_startup();
10355714Skris
104160814Ssimon	BN_GENCB_set(&cb, dh_cb, bio_err);
10555714Skris	if (bio_err == NULL)
10655714Skris		if ((bio_err=BIO_new(BIO_s_file())) != NULL)
10755714Skris			BIO_set_fp(bio_err,stderr,BIO_NOCLOSE|BIO_FP_TEXT);
10855714Skris
109109998Smarkm	if (!load_config(bio_err, NULL))
110109998Smarkm		goto end;
111109998Smarkm
11255714Skris	argv++;
11355714Skris	argc--;
11455714Skris	for (;;)
11555714Skris		{
11655714Skris		if (argc <= 0) break;
11755714Skris		if (strcmp(*argv,"-out") == 0)
11855714Skris			{
11955714Skris			if (--argc < 1) goto bad;
12055714Skris			outfile= *(++argv);
12155714Skris			}
12255714Skris		else if (strcmp(*argv,"-2") == 0)
12355714Skris			g=2;
12455714Skris	/*	else if (strcmp(*argv,"-3") == 0)
12555714Skris			g=3; */
12655714Skris		else if (strcmp(*argv,"-5") == 0)
12755714Skris			g=5;
128111147Snectar#ifndef OPENSSL_NO_ENGINE
129109998Smarkm		else if (strcmp(*argv,"-engine") == 0)
130109998Smarkm			{
131109998Smarkm			if (--argc < 1) goto bad;
132109998Smarkm			engine= *(++argv);
133109998Smarkm			}
134111147Snectar#endif
13555714Skris		else if (strcmp(*argv,"-rand") == 0)
13655714Skris			{
13755714Skris			if (--argc < 1) goto bad;
13855714Skris			inrand= *(++argv);
13955714Skris			}
14055714Skris		else
14155714Skris			break;
14255714Skris		argv++;
14355714Skris		argc--;
14455714Skris		}
14555714Skris	if ((argc >= 1) && ((sscanf(*argv,"%d",&num) == 0) || (num < 0)))
14655714Skris		{
14755714Skrisbad:
14855714Skris		BIO_printf(bio_err,"usage: gendh [args] [numbits]\n");
14955714Skris		BIO_printf(bio_err," -out file - output the key to 'file\n");
150109998Smarkm		BIO_printf(bio_err," -2        - use 2 as the generator value\n");
151109998Smarkm	/*	BIO_printf(bio_err," -3        - use 3 as the generator value\n"); */
152109998Smarkm		BIO_printf(bio_err," -5        - use 5 as the generator value\n");
153111147Snectar#ifndef OPENSSL_NO_ENGINE
154109998Smarkm		BIO_printf(bio_err," -engine e - use engine e, possibly a hardware device.\n");
155111147Snectar#endif
15659191Skris		BIO_printf(bio_err," -rand file%cfile%c...\n", LIST_SEPARATOR_CHAR, LIST_SEPARATOR_CHAR);
15755714Skris		BIO_printf(bio_err,"           - load the file (or the files in the directory) into\n");
15855714Skris		BIO_printf(bio_err,"             the random number generator\n");
15955714Skris		goto end;
16055714Skris		}
16155714Skris
162111147Snectar#ifndef OPENSSL_NO_ENGINE
163215697Ssimon        setup_engine(bio_err, engine, 0);
164111147Snectar#endif
165109998Smarkm
16655714Skris	out=BIO_new(BIO_s_file());
16755714Skris	if (out == NULL)
16855714Skris		{
16955714Skris		ERR_print_errors(bio_err);
17055714Skris		goto end;
17155714Skris		}
17255714Skris
17355714Skris	if (outfile == NULL)
17468651Skris		{
17555714Skris		BIO_set_fp(out,stdout,BIO_NOCLOSE);
176109998Smarkm#ifdef OPENSSL_SYS_VMS
17768651Skris		{
17868651Skris		BIO *tmpbio = BIO_new(BIO_f_linebuffer());
17968651Skris		out = BIO_push(tmpbio, out);
18068651Skris		}
18168651Skris#endif
18268651Skris		}
18355714Skris	else
18455714Skris		{
18555714Skris		if (BIO_write_filename(out,outfile) <= 0)
18655714Skris			{
18755714Skris			perror(outfile);
18855714Skris			goto end;
18955714Skris			}
19055714Skris		}
19155714Skris
19259191Skris	if (!app_RAND_load_file(NULL, bio_err, 1) && inrand == NULL)
19359191Skris		{
19455714Skris		BIO_printf(bio_err,"warning, not much extra random data, consider using the -rand option\n");
19559191Skris		}
19659191Skris	if (inrand != NULL)
19755714Skris		BIO_printf(bio_err,"%ld semi-random bytes loaded\n",
19859191Skris			app_RAND_load_files(inrand));
19955714Skris
20059191Skris	BIO_printf(bio_err,"Generating DH parameters, %d bit long safe prime, generator %d\n",num,g);
20155714Skris	BIO_printf(bio_err,"This is going to take a long time\n");
202160814Ssimon
203160814Ssimon	if(((dh = DH_new()) == NULL) || !DH_generate_parameters_ex(dh, num, g, &cb))
204160814Ssimon		goto end;
20555714Skris
20659191Skris	app_RAND_write_file(NULL, bio_err);
20755714Skris
20855714Skris	if (!PEM_write_bio_DHparams(out,dh))
20955714Skris		goto end;
21055714Skris	ret=0;
21155714Skrisend:
21255714Skris	if (ret != 0)
21355714Skris		ERR_print_errors(bio_err);
21468651Skris	if (out != NULL) BIO_free_all(out);
21555714Skris	if (dh != NULL) DH_free(dh);
216109998Smarkm	apps_shutdown();
217109998Smarkm	OPENSSL_EXIT(ret);
21855714Skris	}
21955714Skris
220160814Ssimonstatic int MS_CALLBACK dh_cb(int p, int n, BN_GENCB *cb)
22155714Skris	{
22255714Skris	char c='*';
22355714Skris
22455714Skris	if (p == 0) c='.';
22555714Skris	if (p == 1) c='+';
22655714Skris	if (p == 2) c='*';
22755714Skris	if (p == 3) c='\n';
228160814Ssimon	BIO_write(cb->arg,&c,1);
229160814Ssimon	(void)BIO_flush(cb->arg);
23055714Skris#ifdef LINT
23155714Skris	p=n;
23255714Skris#endif
233160814Ssimon	return 1;
23455714Skris	}
235238405Sjkim#else /* !OPENSSL_NO_DH */
236238405Sjkim
237238405Sjkim# if PEDANTIC
238238405Sjkimstatic void *dummy=&dummy;
239238405Sjkim# endif
240238405Sjkim
24155714Skris#endif
242