gendh.c revision 280297
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. 9280297Sjkim * 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). 16280297Sjkim * 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. 23280297Sjkim * 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 :-). 38280297Sjkim * 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)" 41280297Sjkim * 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. 53280297Sjkim * 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> 61280297Sjkim/* 62280297Sjkim * Until the key-gen callbacks are modified to use newer prototypes, we allow 63280297Sjkim * deprecated functions for openssl-internal code 64280297Sjkim */ 65160814Ssimon#ifdef OPENSSL_NO_DEPRECATED 66280297Sjkim# undef OPENSSL_NO_DEPRECATED 67160814Ssimon#endif 68160814Ssimon 69109998Smarkm#ifndef OPENSSL_NO_DH 70280297Sjkim# include <stdio.h> 71280297Sjkim# include <string.h> 72280297Sjkim# include <sys/types.h> 73280297Sjkim# include <sys/stat.h> 74280297Sjkim# include "apps.h" 75280297Sjkim# include <openssl/bio.h> 76280297Sjkim# include <openssl/rand.h> 77280297Sjkim# include <openssl/err.h> 78280297Sjkim# include <openssl/bn.h> 79280297Sjkim# include <openssl/dh.h> 80280297Sjkim# include <openssl/x509.h> 81280297Sjkim# include <openssl/pem.h> 8255714Skris 83280297Sjkim# define DEFBITS 512 84280297Sjkim# undef PROG 85280297Sjkim# define PROG gendh_main 8655714Skris 87160814Ssimonstatic int MS_CALLBACK dh_cb(int p, int n, BN_GENCB *cb); 8859191Skris 8959191Skrisint MAIN(int, char **); 9059191Skris 9155714Skrisint MAIN(int argc, char **argv) 92280297Sjkim{ 93280297Sjkim BN_GENCB cb; 94280297Sjkim DH *dh = NULL; 95280297Sjkim int ret = 1, num = DEFBITS; 96280297Sjkim int g = 2; 97280297Sjkim char *outfile = NULL; 98280297Sjkim char *inrand = NULL; 99280297Sjkim# ifndef OPENSSL_NO_ENGINE 100280297Sjkim char *engine = NULL; 101280297Sjkim# endif 102280297Sjkim BIO *out = NULL; 10355714Skris 104280297Sjkim apps_startup(); 10555714Skris 106280297Sjkim BN_GENCB_set(&cb, dh_cb, bio_err); 107280297Sjkim if (bio_err == NULL) 108280297Sjkim if ((bio_err = BIO_new(BIO_s_file())) != NULL) 109280297Sjkim BIO_set_fp(bio_err, stderr, BIO_NOCLOSE | BIO_FP_TEXT); 11055714Skris 111280297Sjkim if (!load_config(bio_err, NULL)) 112280297Sjkim goto end; 113109998Smarkm 114280297Sjkim argv++; 115280297Sjkim argc--; 116280297Sjkim for (;;) { 117280297Sjkim if (argc <= 0) 118280297Sjkim break; 119280297Sjkim if (strcmp(*argv, "-out") == 0) { 120280297Sjkim if (--argc < 1) 121280297Sjkim goto bad; 122280297Sjkim outfile = *(++argv); 123280297Sjkim } else if (strcmp(*argv, "-2") == 0) 124280297Sjkim g = 2; 125280297Sjkim/*- else if (strcmp(*argv,"-3") == 0) 126280297Sjkim g=3; */ 127280297Sjkim else if (strcmp(*argv, "-5") == 0) 128280297Sjkim g = 5; 129280297Sjkim# ifndef OPENSSL_NO_ENGINE 130280297Sjkim else if (strcmp(*argv, "-engine") == 0) { 131280297Sjkim if (--argc < 1) 132280297Sjkim goto bad; 133280297Sjkim engine = *(++argv); 134280297Sjkim } 135280297Sjkim# endif 136280297Sjkim else if (strcmp(*argv, "-rand") == 0) { 137280297Sjkim if (--argc < 1) 138280297Sjkim goto bad; 139280297Sjkim inrand = *(++argv); 140280297Sjkim } else 141280297Sjkim break; 142280297Sjkim argv++; 143280297Sjkim argc--; 144280297Sjkim } 145280297Sjkim if ((argc >= 1) && ((sscanf(*argv, "%d", &num) == 0) || (num < 0))) { 146280297Sjkim bad: 147280297Sjkim BIO_printf(bio_err, "usage: gendh [args] [numbits]\n"); 148280297Sjkim BIO_printf(bio_err, " -out file - output the key to 'file\n"); 149280297Sjkim BIO_printf(bio_err, " -2 - use 2 as the generator value\n"); 150280297Sjkim /* 151280297Sjkim * BIO_printf(bio_err," -3 - use 3 as the generator value\n"); 152280297Sjkim */ 153280297Sjkim BIO_printf(bio_err, " -5 - use 5 as the generator value\n"); 154280297Sjkim# ifndef OPENSSL_NO_ENGINE 155280297Sjkim BIO_printf(bio_err, 156280297Sjkim " -engine e - use engine e, possibly a hardware device.\n"); 157280297Sjkim# endif 158280297Sjkim BIO_printf(bio_err, " -rand file%cfile%c...\n", LIST_SEPARATOR_CHAR, 159280297Sjkim LIST_SEPARATOR_CHAR); 160280297Sjkim BIO_printf(bio_err, 161280297Sjkim " - load the file (or the files in the directory) into\n"); 162280297Sjkim BIO_printf(bio_err, " the random number generator\n"); 163280297Sjkim goto end; 164280297Sjkim } 165280297Sjkim# ifndef OPENSSL_NO_ENGINE 166280297Sjkim setup_engine(bio_err, engine, 0); 167280297Sjkim# endif 168109998Smarkm 169280297Sjkim out = BIO_new(BIO_s_file()); 170280297Sjkim if (out == NULL) { 171280297Sjkim ERR_print_errors(bio_err); 172280297Sjkim goto end; 173280297Sjkim } 17455714Skris 175280297Sjkim if (outfile == NULL) { 176280297Sjkim BIO_set_fp(out, stdout, BIO_NOCLOSE); 177280297Sjkim# ifdef OPENSSL_SYS_VMS 178280297Sjkim { 179280297Sjkim BIO *tmpbio = BIO_new(BIO_f_linebuffer()); 180280297Sjkim out = BIO_push(tmpbio, out); 181280297Sjkim } 182280297Sjkim# endif 183280297Sjkim } else { 184280297Sjkim if (BIO_write_filename(out, outfile) <= 0) { 185280297Sjkim perror(outfile); 186280297Sjkim goto end; 187280297Sjkim } 188280297Sjkim } 18955714Skris 190280297Sjkim if (!app_RAND_load_file(NULL, bio_err, 1) && inrand == NULL) { 191280297Sjkim BIO_printf(bio_err, 192280297Sjkim "warning, not much extra random data, consider using the -rand option\n"); 193280297Sjkim } 194280297Sjkim if (inrand != NULL) 195280297Sjkim BIO_printf(bio_err, "%ld semi-random bytes loaded\n", 196280297Sjkim app_RAND_load_files(inrand)); 19755714Skris 198280297Sjkim BIO_printf(bio_err, 199280297Sjkim "Generating DH parameters, %d bit long safe prime, generator %d\n", 200280297Sjkim num, g); 201280297Sjkim BIO_printf(bio_err, "This is going to take a long time\n"); 202160814Ssimon 203280297Sjkim if (((dh = DH_new()) == NULL) 204280297Sjkim || !DH_generate_parameters_ex(dh, num, g, &cb)) 205280297Sjkim goto end; 20655714Skris 207280297Sjkim app_RAND_write_file(NULL, bio_err); 20855714Skris 209280297Sjkim if (!PEM_write_bio_DHparams(out, dh)) 210280297Sjkim goto end; 211280297Sjkim ret = 0; 212280297Sjkim end: 213280297Sjkim if (ret != 0) 214280297Sjkim ERR_print_errors(bio_err); 215280297Sjkim if (out != NULL) 216280297Sjkim BIO_free_all(out); 217280297Sjkim if (dh != NULL) 218280297Sjkim DH_free(dh); 219280297Sjkim apps_shutdown(); 220280297Sjkim OPENSSL_EXIT(ret); 221280297Sjkim} 222280297Sjkim 223160814Ssimonstatic int MS_CALLBACK dh_cb(int p, int n, BN_GENCB *cb) 224280297Sjkim{ 225280297Sjkim char c = '*'; 22655714Skris 227280297Sjkim if (p == 0) 228280297Sjkim c = '.'; 229280297Sjkim if (p == 1) 230280297Sjkim c = '+'; 231280297Sjkim if (p == 2) 232280297Sjkim c = '*'; 233280297Sjkim if (p == 3) 234280297Sjkim c = '\n'; 235280297Sjkim BIO_write(cb->arg, &c, 1); 236280297Sjkim (void)BIO_flush(cb->arg); 237280297Sjkim# ifdef LINT 238280297Sjkim p = n; 239280297Sjkim# endif 240280297Sjkim return 1; 241280297Sjkim} 242280297Sjkim#else /* !OPENSSL_NO_DH */ 243238405Sjkim 244238405Sjkim# if PEDANTIC 245280297Sjkimstatic void *dummy = &dummy; 246238405Sjkim# endif 247238405Sjkim 24855714Skris#endif 249