gendh.c revision 55714
155714Skris/* apps/gendh.c */ 255714Skris/* Copyright (C) 1995-1998 Eric Young (eay@cryptsoft.com) 355714Skris * All rights reserved. 455714Skris * 555714Skris * This package is an SSL implementation written 655714Skris * by Eric Young (eay@cryptsoft.com). 755714Skris * The implementation was written so as to conform with Netscapes SSL. 855714Skris * 955714Skris * This library is free for commercial and non-commercial use as long as 1055714Skris * the following conditions are aheared to. The following conditions 1155714Skris * apply to all code found in this distribution, be it the RC4, RSA, 1255714Skris * lhash, DES, etc., code; not just the SSL code. The SSL documentation 1355714Skris * included with this distribution is covered by the same copyright terms 1455714Skris * except that the holder is Tim Hudson (tjh@cryptsoft.com). 1555714Skris * 1655714Skris * Copyright remains Eric Young's, and as such any Copyright notices in 1755714Skris * the code are not to be removed. 1855714Skris * If this package is used in a product, Eric Young should be given attribution 1955714Skris * as the author of the parts of the library used. 2055714Skris * This can be in the form of a textual message at program startup or 2155714Skris * in documentation (online or textual) provided with the package. 2255714Skris * 2355714Skris * Redistribution and use in source and binary forms, with or without 2455714Skris * modification, are permitted provided that the following conditions 2555714Skris * are met: 2655714Skris * 1. Redistributions of source code must retain the copyright 2755714Skris * notice, this list of conditions and the following disclaimer. 2855714Skris * 2. Redistributions in binary form must reproduce the above copyright 2955714Skris * notice, this list of conditions and the following disclaimer in the 3055714Skris * documentation and/or other materials provided with the distribution. 3155714Skris * 3. All advertising materials mentioning features or use of this software 3255714Skris * must display the following acknowledgement: 3355714Skris * "This product includes cryptographic software written by 3455714Skris * Eric Young (eay@cryptsoft.com)" 3555714Skris * The word 'cryptographic' can be left out if the rouines from the library 3655714Skris * being used are not cryptographic related :-). 3755714Skris * 4. If you include any Windows specific code (or a derivative thereof) from 3855714Skris * the apps directory (application code) you must include an acknowledgement: 3955714Skris * "This product includes software written by Tim Hudson (tjh@cryptsoft.com)" 4055714Skris * 4155714Skris * THIS SOFTWARE IS PROVIDED BY ERIC YOUNG ``AS IS'' AND 4255714Skris * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 4355714Skris * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 4455714Skris * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE 4555714Skris * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 4655714Skris * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 4755714Skris * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 4855714Skris * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 4955714Skris * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 5055714Skris * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 5155714Skris * SUCH DAMAGE. 5255714Skris * 5355714Skris * The licence and distribution terms for any publically available version or 5455714Skris * derivative of this code cannot be changed. i.e. this code cannot simply be 5555714Skris * copied and put under another distribution licence 5655714Skris * [including the GNU Public Licence.] 5755714Skris */ 5855714Skris 5955714Skris#ifndef NO_DH 6055714Skris#include <stdio.h> 6155714Skris#include <string.h> 6255714Skris#include <sys/types.h> 6355714Skris#include <sys/stat.h> 6455714Skris#include "apps.h" 6555714Skris#include <openssl/bio.h> 6655714Skris#include <openssl/rand.h> 6755714Skris#include <openssl/err.h> 6855714Skris#include <openssl/bn.h> 6955714Skris#include <openssl/dh.h> 7055714Skris#include <openssl/x509.h> 7155714Skris#include <openssl/pem.h> 7255714Skris 7355714Skris#define DEFBITS 512 7455714Skris#undef PROG 7555714Skris#define PROG gendh_main 7655714Skris 7755714Skrisstatic void MS_CALLBACK dh_cb(int p, int n, void *arg); 7855714Skrisstatic long dh_load_rand(char *names); 7955714Skrisint MAIN(int argc, char **argv) 8055714Skris { 8155714Skris char buffer[200]; 8255714Skris DH *dh=NULL; 8355714Skris int ret=1,num=DEFBITS; 8455714Skris int g=2; 8555714Skris char *outfile=NULL; 8655714Skris char *inrand=NULL,*randfile; 8755714Skris BIO *out=NULL; 8855714Skris 8955714Skris apps_startup(); 9055714Skris 9155714Skris if (bio_err == NULL) 9255714Skris if ((bio_err=BIO_new(BIO_s_file())) != NULL) 9355714Skris BIO_set_fp(bio_err,stderr,BIO_NOCLOSE|BIO_FP_TEXT); 9455714Skris 9555714Skris argv++; 9655714Skris argc--; 9755714Skris for (;;) 9855714Skris { 9955714Skris if (argc <= 0) break; 10055714Skris if (strcmp(*argv,"-out") == 0) 10155714Skris { 10255714Skris if (--argc < 1) goto bad; 10355714Skris outfile= *(++argv); 10455714Skris } 10555714Skris else if (strcmp(*argv,"-2") == 0) 10655714Skris g=2; 10755714Skris /* else if (strcmp(*argv,"-3") == 0) 10855714Skris g=3; */ 10955714Skris else if (strcmp(*argv,"-5") == 0) 11055714Skris g=5; 11155714Skris else if (strcmp(*argv,"-rand") == 0) 11255714Skris { 11355714Skris if (--argc < 1) goto bad; 11455714Skris inrand= *(++argv); 11555714Skris } 11655714Skris else 11755714Skris break; 11855714Skris argv++; 11955714Skris argc--; 12055714Skris } 12155714Skris if ((argc >= 1) && ((sscanf(*argv,"%d",&num) == 0) || (num < 0))) 12255714Skris { 12355714Skrisbad: 12455714Skris BIO_printf(bio_err,"usage: gendh [args] [numbits]\n"); 12555714Skris BIO_printf(bio_err," -out file - output the key to 'file\n"); 12655714Skris BIO_printf(bio_err," -2 use 2 as the generator value\n"); 12755714Skris /* BIO_printf(bio_err," -3 use 3 as the generator value\n"); */ 12855714Skris BIO_printf(bio_err," -5 use 5 as the generator value\n"); 12955714Skris BIO_printf(bio_err," -rand file:file:...\n"); 13055714Skris BIO_printf(bio_err," - load the file (or the files in the directory) into\n"); 13155714Skris BIO_printf(bio_err," the random number generator\n"); 13255714Skris goto end; 13355714Skris } 13455714Skris 13555714Skris out=BIO_new(BIO_s_file()); 13655714Skris if (out == NULL) 13755714Skris { 13855714Skris ERR_print_errors(bio_err); 13955714Skris goto end; 14055714Skris } 14155714Skris 14255714Skris if (outfile == NULL) 14355714Skris BIO_set_fp(out,stdout,BIO_NOCLOSE); 14455714Skris else 14555714Skris { 14655714Skris if (BIO_write_filename(out,outfile) <= 0) 14755714Skris { 14855714Skris perror(outfile); 14955714Skris goto end; 15055714Skris } 15155714Skris } 15255714Skris 15355714Skris randfile=RAND_file_name(buffer,200); 15455714Skris if ((randfile == NULL)|| !RAND_load_file(randfile,1024L*1024L)) 15555714Skris BIO_printf(bio_err,"unable to load 'random state'\n"); 15655714Skris 15755714Skris if (inrand == NULL) 15855714Skris BIO_printf(bio_err,"warning, not much extra random data, consider using the -rand option\n"); 15955714Skris else 16055714Skris { 16155714Skris BIO_printf(bio_err,"%ld semi-random bytes loaded\n", 16255714Skris dh_load_rand(inrand)); 16355714Skris } 16455714Skris 16555714Skris BIO_printf(bio_err,"Generating DH parameters, %d bit long strong prime, generator of %d\n",num,g); 16655714Skris BIO_printf(bio_err,"This is going to take a long time\n"); 16755714Skris dh=DH_generate_parameters(num,g,dh_cb,bio_err); 16855714Skris 16955714Skris if (dh == NULL) goto end; 17055714Skris 17155714Skris if (randfile == NULL) 17255714Skris BIO_printf(bio_err,"unable to write 'random state'\n"); 17355714Skris else 17455714Skris RAND_write_file(randfile); 17555714Skris 17655714Skris if (!PEM_write_bio_DHparams(out,dh)) 17755714Skris goto end; 17855714Skris ret=0; 17955714Skrisend: 18055714Skris if (ret != 0) 18155714Skris ERR_print_errors(bio_err); 18255714Skris if (out != NULL) BIO_free(out); 18355714Skris if (dh != NULL) DH_free(dh); 18455714Skris EXIT(ret); 18555714Skris } 18655714Skris 18755714Skrisstatic void MS_CALLBACK dh_cb(int p, int n, void *arg) 18855714Skris { 18955714Skris char c='*'; 19055714Skris 19155714Skris if (p == 0) c='.'; 19255714Skris if (p == 1) c='+'; 19355714Skris if (p == 2) c='*'; 19455714Skris if (p == 3) c='\n'; 19555714Skris BIO_write((BIO *)arg,&c,1); 19655714Skris (void)BIO_flush((BIO *)arg); 19755714Skris#ifdef LINT 19855714Skris p=n; 19955714Skris#endif 20055714Skris } 20155714Skris 20255714Skrisstatic long dh_load_rand(char *name) 20355714Skris { 20455714Skris char *p,*n; 20555714Skris int last; 20655714Skris long tot=0; 20755714Skris 20855714Skris for (;;) 20955714Skris { 21055714Skris last=0; 21155714Skris for (p=name; ((*p != '\0') && (*p != LIST_SEPARATOR_CHAR)); p++); 21255714Skris if (*p == '\0') last=1; 21355714Skris *p='\0'; 21455714Skris n=name; 21555714Skris name=p+1; 21655714Skris if (*n == '\0') break; 21755714Skris 21855714Skris tot+=RAND_load_file(n,1); 21955714Skris if (last) break; 22055714Skris } 22155714Skris return(tot); 22255714Skris } 22355714Skris#endif 224