10Sstevel@tonic-gate /* apps/gendh.c */
20Sstevel@tonic-gate /* obsoleted by dhparam.c */
30Sstevel@tonic-gate /* Copyright (C) 1995-1998 Eric Young (eay@cryptsoft.com)
40Sstevel@tonic-gate * All rights reserved.
50Sstevel@tonic-gate *
60Sstevel@tonic-gate * This package is an SSL implementation written
70Sstevel@tonic-gate * by Eric Young (eay@cryptsoft.com).
80Sstevel@tonic-gate * The implementation was written so as to conform with Netscapes SSL.
90Sstevel@tonic-gate *
100Sstevel@tonic-gate * This library is free for commercial and non-commercial use as long as
110Sstevel@tonic-gate * the following conditions are aheared to. The following conditions
120Sstevel@tonic-gate * apply to all code found in this distribution, be it the RC4, RSA,
130Sstevel@tonic-gate * lhash, DES, etc., code; not just the SSL code. The SSL documentation
140Sstevel@tonic-gate * included with this distribution is covered by the same copyright terms
150Sstevel@tonic-gate * except that the holder is Tim Hudson (tjh@cryptsoft.com).
160Sstevel@tonic-gate *
170Sstevel@tonic-gate * Copyright remains Eric Young's, and as such any Copyright notices in
180Sstevel@tonic-gate * the code are not to be removed.
190Sstevel@tonic-gate * If this package is used in a product, Eric Young should be given attribution
200Sstevel@tonic-gate * as the author of the parts of the library used.
210Sstevel@tonic-gate * This can be in the form of a textual message at program startup or
220Sstevel@tonic-gate * in documentation (online or textual) provided with the package.
230Sstevel@tonic-gate *
240Sstevel@tonic-gate * Redistribution and use in source and binary forms, with or without
250Sstevel@tonic-gate * modification, are permitted provided that the following conditions
260Sstevel@tonic-gate * are met:
270Sstevel@tonic-gate * 1. Redistributions of source code must retain the copyright
280Sstevel@tonic-gate * notice, this list of conditions and the following disclaimer.
290Sstevel@tonic-gate * 2. Redistributions in binary form must reproduce the above copyright
300Sstevel@tonic-gate * notice, this list of conditions and the following disclaimer in the
310Sstevel@tonic-gate * documentation and/or other materials provided with the distribution.
320Sstevel@tonic-gate * 3. All advertising materials mentioning features or use of this software
330Sstevel@tonic-gate * must display the following acknowledgement:
340Sstevel@tonic-gate * "This product includes cryptographic software written by
350Sstevel@tonic-gate * Eric Young (eay@cryptsoft.com)"
360Sstevel@tonic-gate * The word 'cryptographic' can be left out if the rouines from the library
370Sstevel@tonic-gate * being used are not cryptographic related :-).
380Sstevel@tonic-gate * 4. If you include any Windows specific code (or a derivative thereof) from
390Sstevel@tonic-gate * the apps directory (application code) you must include an acknowledgement:
400Sstevel@tonic-gate * "This product includes software written by Tim Hudson (tjh@cryptsoft.com)"
410Sstevel@tonic-gate *
420Sstevel@tonic-gate * THIS SOFTWARE IS PROVIDED BY ERIC YOUNG ``AS IS'' AND
430Sstevel@tonic-gate * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
440Sstevel@tonic-gate * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
450Sstevel@tonic-gate * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
460Sstevel@tonic-gate * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
470Sstevel@tonic-gate * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
480Sstevel@tonic-gate * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
490Sstevel@tonic-gate * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
500Sstevel@tonic-gate * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
510Sstevel@tonic-gate * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
520Sstevel@tonic-gate * SUCH DAMAGE.
530Sstevel@tonic-gate *
540Sstevel@tonic-gate * The licence and distribution terms for any publically available version or
550Sstevel@tonic-gate * derivative of this code cannot be changed. i.e. this code cannot simply be
560Sstevel@tonic-gate * copied and put under another distribution licence
570Sstevel@tonic-gate * [including the GNU Public Licence.]
580Sstevel@tonic-gate */
590Sstevel@tonic-gate
60*2139Sjp161948 #include <openssl/opensslconf.h>
61*2139Sjp161948 /* Until the key-gen callbacks are modified to use newer prototypes, we allow
62*2139Sjp161948 * deprecated functions for openssl-internal code */
63*2139Sjp161948 #ifdef OPENSSL_NO_DEPRECATED
64*2139Sjp161948 #undef OPENSSL_NO_DEPRECATED
65*2139Sjp161948 #endif
66*2139Sjp161948
670Sstevel@tonic-gate #ifndef OPENSSL_NO_DH
680Sstevel@tonic-gate #include <stdio.h>
690Sstevel@tonic-gate #include <string.h>
700Sstevel@tonic-gate #include <sys/types.h>
710Sstevel@tonic-gate #include <sys/stat.h>
720Sstevel@tonic-gate #include "apps.h"
730Sstevel@tonic-gate #include <openssl/bio.h>
740Sstevel@tonic-gate #include <openssl/rand.h>
750Sstevel@tonic-gate #include <openssl/err.h>
760Sstevel@tonic-gate #include <openssl/bn.h>
770Sstevel@tonic-gate #include <openssl/dh.h>
780Sstevel@tonic-gate #include <openssl/x509.h>
790Sstevel@tonic-gate #include <openssl/pem.h>
800Sstevel@tonic-gate
810Sstevel@tonic-gate #define DEFBITS 512
820Sstevel@tonic-gate #undef PROG
830Sstevel@tonic-gate #define PROG gendh_main
840Sstevel@tonic-gate
85*2139Sjp161948 static int MS_CALLBACK dh_cb(int p, int n, BN_GENCB *cb);
860Sstevel@tonic-gate
870Sstevel@tonic-gate int MAIN(int, char **);
880Sstevel@tonic-gate
MAIN(int argc,char ** argv)890Sstevel@tonic-gate int MAIN(int argc, char **argv)
900Sstevel@tonic-gate {
91*2139Sjp161948 BN_GENCB cb;
920Sstevel@tonic-gate #ifndef OPENSSL_NO_ENGINE
930Sstevel@tonic-gate ENGINE *e = NULL;
940Sstevel@tonic-gate #endif
950Sstevel@tonic-gate DH *dh=NULL;
960Sstevel@tonic-gate int ret=1,num=DEFBITS;
970Sstevel@tonic-gate int g=2;
980Sstevel@tonic-gate char *outfile=NULL;
990Sstevel@tonic-gate char *inrand=NULL;
1000Sstevel@tonic-gate #ifndef OPENSSL_NO_ENGINE
1010Sstevel@tonic-gate char *engine=NULL;
1020Sstevel@tonic-gate #endif
1030Sstevel@tonic-gate BIO *out=NULL;
1040Sstevel@tonic-gate
1050Sstevel@tonic-gate apps_startup();
1060Sstevel@tonic-gate
107*2139Sjp161948 BN_GENCB_set(&cb, dh_cb, bio_err);
1080Sstevel@tonic-gate if (bio_err == NULL)
1090Sstevel@tonic-gate if ((bio_err=BIO_new(BIO_s_file())) != NULL)
1100Sstevel@tonic-gate BIO_set_fp(bio_err,stderr,BIO_NOCLOSE|BIO_FP_TEXT);
1110Sstevel@tonic-gate
1120Sstevel@tonic-gate if (!load_config(bio_err, NULL))
1130Sstevel@tonic-gate goto end;
1140Sstevel@tonic-gate
1150Sstevel@tonic-gate argv++;
1160Sstevel@tonic-gate argc--;
1170Sstevel@tonic-gate for (;;)
1180Sstevel@tonic-gate {
1190Sstevel@tonic-gate if (argc <= 0) break;
1200Sstevel@tonic-gate if (strcmp(*argv,"-out") == 0)
1210Sstevel@tonic-gate {
1220Sstevel@tonic-gate if (--argc < 1) goto bad;
1230Sstevel@tonic-gate outfile= *(++argv);
1240Sstevel@tonic-gate }
1250Sstevel@tonic-gate else if (strcmp(*argv,"-2") == 0)
1260Sstevel@tonic-gate g=2;
1270Sstevel@tonic-gate /* else if (strcmp(*argv,"-3") == 0)
1280Sstevel@tonic-gate g=3; */
1290Sstevel@tonic-gate else if (strcmp(*argv,"-5") == 0)
1300Sstevel@tonic-gate g=5;
1310Sstevel@tonic-gate #ifndef OPENSSL_NO_ENGINE
1320Sstevel@tonic-gate else if (strcmp(*argv,"-engine") == 0)
1330Sstevel@tonic-gate {
1340Sstevel@tonic-gate if (--argc < 1) goto bad;
1350Sstevel@tonic-gate engine= *(++argv);
1360Sstevel@tonic-gate }
1370Sstevel@tonic-gate #endif
1380Sstevel@tonic-gate else if (strcmp(*argv,"-rand") == 0)
1390Sstevel@tonic-gate {
1400Sstevel@tonic-gate if (--argc < 1) goto bad;
1410Sstevel@tonic-gate inrand= *(++argv);
1420Sstevel@tonic-gate }
1430Sstevel@tonic-gate else
1440Sstevel@tonic-gate break;
1450Sstevel@tonic-gate argv++;
1460Sstevel@tonic-gate argc--;
1470Sstevel@tonic-gate }
1480Sstevel@tonic-gate if ((argc >= 1) && ((sscanf(*argv,"%d",&num) == 0) || (num < 0)))
1490Sstevel@tonic-gate {
1500Sstevel@tonic-gate bad:
1510Sstevel@tonic-gate BIO_printf(bio_err,"usage: gendh [args] [numbits]\n");
1520Sstevel@tonic-gate BIO_printf(bio_err," -out file - output the key to 'file\n");
1530Sstevel@tonic-gate BIO_printf(bio_err," -2 - use 2 as the generator value\n");
1540Sstevel@tonic-gate /* BIO_printf(bio_err," -3 - use 3 as the generator value\n"); */
1550Sstevel@tonic-gate BIO_printf(bio_err," -5 - use 5 as the generator value\n");
1560Sstevel@tonic-gate #ifndef OPENSSL_NO_ENGINE
1570Sstevel@tonic-gate BIO_printf(bio_err," -engine e - use engine e, possibly a hardware device.\n");
1580Sstevel@tonic-gate #endif
1590Sstevel@tonic-gate BIO_printf(bio_err," -rand file%cfile%c...\n", LIST_SEPARATOR_CHAR, LIST_SEPARATOR_CHAR);
1600Sstevel@tonic-gate BIO_printf(bio_err," - load the file (or the files in the directory) into\n");
1610Sstevel@tonic-gate BIO_printf(bio_err," the random number generator\n");
1620Sstevel@tonic-gate goto end;
1630Sstevel@tonic-gate }
1640Sstevel@tonic-gate
1650Sstevel@tonic-gate #ifndef OPENSSL_NO_ENGINE
1660Sstevel@tonic-gate e = setup_engine(bio_err, engine, 0);
1670Sstevel@tonic-gate #endif
1680Sstevel@tonic-gate
1690Sstevel@tonic-gate out=BIO_new(BIO_s_file());
1700Sstevel@tonic-gate if (out == NULL)
1710Sstevel@tonic-gate {
1720Sstevel@tonic-gate ERR_print_errors(bio_err);
1730Sstevel@tonic-gate goto end;
1740Sstevel@tonic-gate }
1750Sstevel@tonic-gate
1760Sstevel@tonic-gate if (outfile == NULL)
1770Sstevel@tonic-gate {
1780Sstevel@tonic-gate BIO_set_fp(out,stdout,BIO_NOCLOSE);
1790Sstevel@tonic-gate #ifdef OPENSSL_SYS_VMS
1800Sstevel@tonic-gate {
1810Sstevel@tonic-gate BIO *tmpbio = BIO_new(BIO_f_linebuffer());
1820Sstevel@tonic-gate out = BIO_push(tmpbio, out);
1830Sstevel@tonic-gate }
1840Sstevel@tonic-gate #endif
1850Sstevel@tonic-gate }
1860Sstevel@tonic-gate else
1870Sstevel@tonic-gate {
1880Sstevel@tonic-gate if (BIO_write_filename(out,outfile) <= 0)
1890Sstevel@tonic-gate {
1900Sstevel@tonic-gate perror(outfile);
1910Sstevel@tonic-gate goto end;
1920Sstevel@tonic-gate }
1930Sstevel@tonic-gate }
1940Sstevel@tonic-gate
1950Sstevel@tonic-gate if (!app_RAND_load_file(NULL, bio_err, 1) && inrand == NULL)
1960Sstevel@tonic-gate {
1970Sstevel@tonic-gate BIO_printf(bio_err,"warning, not much extra random data, consider using the -rand option\n");
1980Sstevel@tonic-gate }
1990Sstevel@tonic-gate if (inrand != NULL)
2000Sstevel@tonic-gate BIO_printf(bio_err,"%ld semi-random bytes loaded\n",
2010Sstevel@tonic-gate app_RAND_load_files(inrand));
2020Sstevel@tonic-gate
2030Sstevel@tonic-gate BIO_printf(bio_err,"Generating DH parameters, %d bit long safe prime, generator %d\n",num,g);
2040Sstevel@tonic-gate BIO_printf(bio_err,"This is going to take a long time\n");
205*2139Sjp161948
206*2139Sjp161948 if(((dh = DH_new()) == NULL) || !DH_generate_parameters_ex(dh, num, g, &cb))
207*2139Sjp161948 goto end;
2080Sstevel@tonic-gate
2090Sstevel@tonic-gate app_RAND_write_file(NULL, bio_err);
2100Sstevel@tonic-gate
2110Sstevel@tonic-gate if (!PEM_write_bio_DHparams(out,dh))
2120Sstevel@tonic-gate goto end;
2130Sstevel@tonic-gate ret=0;
2140Sstevel@tonic-gate end:
2150Sstevel@tonic-gate if (ret != 0)
2160Sstevel@tonic-gate ERR_print_errors(bio_err);
2170Sstevel@tonic-gate if (out != NULL) BIO_free_all(out);
2180Sstevel@tonic-gate if (dh != NULL) DH_free(dh);
2190Sstevel@tonic-gate apps_shutdown();
2200Sstevel@tonic-gate OPENSSL_EXIT(ret);
2210Sstevel@tonic-gate }
2220Sstevel@tonic-gate
dh_cb(int p,int n,BN_GENCB * cb)223*2139Sjp161948 static int MS_CALLBACK dh_cb(int p, int n, BN_GENCB *cb)
2240Sstevel@tonic-gate {
2250Sstevel@tonic-gate char c='*';
2260Sstevel@tonic-gate
2270Sstevel@tonic-gate if (p == 0) c='.';
2280Sstevel@tonic-gate if (p == 1) c='+';
2290Sstevel@tonic-gate if (p == 2) c='*';
2300Sstevel@tonic-gate if (p == 3) c='\n';
231*2139Sjp161948 BIO_write(cb->arg,&c,1);
232*2139Sjp161948 (void)BIO_flush(cb->arg);
2330Sstevel@tonic-gate #ifdef LINT
2340Sstevel@tonic-gate p=n;
2350Sstevel@tonic-gate #endif
236*2139Sjp161948 return 1;
2370Sstevel@tonic-gate }
2380Sstevel@tonic-gate #endif
239