gendh.c revision 109998
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 60109998Smarkm#ifndef OPENSSL_NO_DH 6155714Skris#include <stdio.h> 6255714Skris#include <string.h> 6355714Skris#include <sys/types.h> 6455714Skris#include <sys/stat.h> 6555714Skris#include "apps.h" 6655714Skris#include <openssl/bio.h> 6755714Skris#include <openssl/rand.h> 6855714Skris#include <openssl/err.h> 6955714Skris#include <openssl/bn.h> 7055714Skris#include <openssl/dh.h> 7155714Skris#include <openssl/x509.h> 7255714Skris#include <openssl/pem.h> 7355714Skris 7455714Skris#define DEFBITS 512 7555714Skris#undef PROG 7655714Skris#define PROG gendh_main 7755714Skris 7855714Skrisstatic void MS_CALLBACK dh_cb(int p, int n, void *arg); 7959191Skris 8059191Skrisint MAIN(int, char **); 8159191Skris 8255714Skrisint MAIN(int argc, char **argv) 8355714Skris { 84109998Smarkm ENGINE *e = NULL; 8555714Skris DH *dh=NULL; 8655714Skris int ret=1,num=DEFBITS; 8755714Skris int g=2; 8855714Skris char *outfile=NULL; 8959191Skris char *inrand=NULL; 90109998Smarkm char *engine=NULL; 9155714Skris BIO *out=NULL; 9255714Skris 9355714Skris apps_startup(); 9455714Skris 9555714Skris if (bio_err == NULL) 9655714Skris if ((bio_err=BIO_new(BIO_s_file())) != NULL) 9755714Skris BIO_set_fp(bio_err,stderr,BIO_NOCLOSE|BIO_FP_TEXT); 9855714Skris 99109998Smarkm if (!load_config(bio_err, NULL)) 100109998Smarkm goto end; 101109998Smarkm 10255714Skris argv++; 10355714Skris argc--; 10455714Skris for (;;) 10555714Skris { 10655714Skris if (argc <= 0) break; 10755714Skris if (strcmp(*argv,"-out") == 0) 10855714Skris { 10955714Skris if (--argc < 1) goto bad; 11055714Skris outfile= *(++argv); 11155714Skris } 11255714Skris else if (strcmp(*argv,"-2") == 0) 11355714Skris g=2; 11455714Skris /* else if (strcmp(*argv,"-3") == 0) 11555714Skris g=3; */ 11655714Skris else if (strcmp(*argv,"-5") == 0) 11755714Skris g=5; 118109998Smarkm else if (strcmp(*argv,"-engine") == 0) 119109998Smarkm { 120109998Smarkm if (--argc < 1) goto bad; 121109998Smarkm engine= *(++argv); 122109998Smarkm } 12355714Skris else if (strcmp(*argv,"-rand") == 0) 12455714Skris { 12555714Skris if (--argc < 1) goto bad; 12655714Skris inrand= *(++argv); 12755714Skris } 12855714Skris else 12955714Skris break; 13055714Skris argv++; 13155714Skris argc--; 13255714Skris } 13355714Skris if ((argc >= 1) && ((sscanf(*argv,"%d",&num) == 0) || (num < 0))) 13455714Skris { 13555714Skrisbad: 13655714Skris BIO_printf(bio_err,"usage: gendh [args] [numbits]\n"); 13755714Skris BIO_printf(bio_err," -out file - output the key to 'file\n"); 138109998Smarkm BIO_printf(bio_err," -2 - use 2 as the generator value\n"); 139109998Smarkm /* BIO_printf(bio_err," -3 - use 3 as the generator value\n"); */ 140109998Smarkm BIO_printf(bio_err," -5 - use 5 as the generator value\n"); 141109998Smarkm BIO_printf(bio_err," -engine e - use engine e, possibly a hardware device.\n"); 14259191Skris BIO_printf(bio_err," -rand file%cfile%c...\n", LIST_SEPARATOR_CHAR, LIST_SEPARATOR_CHAR); 14355714Skris BIO_printf(bio_err," - load the file (or the files in the directory) into\n"); 14455714Skris BIO_printf(bio_err," the random number generator\n"); 14555714Skris goto end; 14655714Skris } 14755714Skris 148109998Smarkm e = setup_engine(bio_err, engine, 0); 149109998Smarkm 15055714Skris out=BIO_new(BIO_s_file()); 15155714Skris if (out == NULL) 15255714Skris { 15355714Skris ERR_print_errors(bio_err); 15455714Skris goto end; 15555714Skris } 15655714Skris 15755714Skris if (outfile == NULL) 15868651Skris { 15955714Skris BIO_set_fp(out,stdout,BIO_NOCLOSE); 160109998Smarkm#ifdef OPENSSL_SYS_VMS 16168651Skris { 16268651Skris BIO *tmpbio = BIO_new(BIO_f_linebuffer()); 16368651Skris out = BIO_push(tmpbio, out); 16468651Skris } 16568651Skris#endif 16668651Skris } 16755714Skris else 16855714Skris { 16955714Skris if (BIO_write_filename(out,outfile) <= 0) 17055714Skris { 17155714Skris perror(outfile); 17255714Skris goto end; 17355714Skris } 17455714Skris } 17555714Skris 17659191Skris if (!app_RAND_load_file(NULL, bio_err, 1) && inrand == NULL) 17759191Skris { 17855714Skris BIO_printf(bio_err,"warning, not much extra random data, consider using the -rand option\n"); 17959191Skris } 18059191Skris if (inrand != NULL) 18155714Skris BIO_printf(bio_err,"%ld semi-random bytes loaded\n", 18259191Skris app_RAND_load_files(inrand)); 18355714Skris 18459191Skris BIO_printf(bio_err,"Generating DH parameters, %d bit long safe prime, generator %d\n",num,g); 18555714Skris BIO_printf(bio_err,"This is going to take a long time\n"); 18655714Skris dh=DH_generate_parameters(num,g,dh_cb,bio_err); 18755714Skris 18855714Skris if (dh == NULL) goto end; 18955714Skris 19059191Skris app_RAND_write_file(NULL, bio_err); 19155714Skris 19255714Skris if (!PEM_write_bio_DHparams(out,dh)) 19355714Skris goto end; 19455714Skris ret=0; 19555714Skrisend: 19655714Skris if (ret != 0) 19755714Skris ERR_print_errors(bio_err); 19868651Skris if (out != NULL) BIO_free_all(out); 19955714Skris if (dh != NULL) DH_free(dh); 200109998Smarkm apps_shutdown(); 201109998Smarkm OPENSSL_EXIT(ret); 20255714Skris } 20355714Skris 20455714Skrisstatic void MS_CALLBACK dh_cb(int p, int n, void *arg) 20555714Skris { 20655714Skris char c='*'; 20755714Skris 20855714Skris if (p == 0) c='.'; 20955714Skris if (p == 1) c='+'; 21055714Skris if (p == 2) c='*'; 21155714Skris if (p == 3) c='\n'; 21255714Skris BIO_write((BIO *)arg,&c,1); 21355714Skris (void)BIO_flush((BIO *)arg); 21455714Skris#ifdef LINT 21555714Skris p=n; 21655714Skris#endif 21755714Skris } 21855714Skris#endif 219