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. 9296341Sdelphij * 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). 16296341Sdelphij * 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. 23296341Sdelphij * 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 :-). 38296341Sdelphij * 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)" 41296341Sdelphij * 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. 53296341Sdelphij * 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> 61296341Sdelphij/* 62296341Sdelphij * Until the key-gen callbacks are modified to use newer prototypes, we allow 63296341Sdelphij * deprecated functions for openssl-internal code 64296341Sdelphij */ 65160814Ssimon#ifdef OPENSSL_NO_DEPRECATED 66296341Sdelphij# undef OPENSSL_NO_DEPRECATED 67160814Ssimon#endif 68160814Ssimon 69109998Smarkm#ifndef OPENSSL_NO_DH 70296341Sdelphij# include <stdio.h> 71296341Sdelphij# include <string.h> 72296341Sdelphij# include <sys/types.h> 73296341Sdelphij# include <sys/stat.h> 74296341Sdelphij# include "apps.h" 75296341Sdelphij# include <openssl/bio.h> 76296341Sdelphij# include <openssl/rand.h> 77296341Sdelphij# include <openssl/err.h> 78296341Sdelphij# include <openssl/bn.h> 79296341Sdelphij# include <openssl/dh.h> 80296341Sdelphij# include <openssl/x509.h> 81296341Sdelphij# include <openssl/pem.h> 8255714Skris 83296341Sdelphij# define DEFBITS 2048 84296341Sdelphij# undef PROG 85296341Sdelphij# 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) 92296341Sdelphij{ 93296341Sdelphij BN_GENCB cb; 94296341Sdelphij DH *dh = NULL; 95296341Sdelphij int ret = 1, num = DEFBITS; 96296341Sdelphij int g = 2; 97296341Sdelphij char *outfile = NULL; 98296341Sdelphij char *inrand = NULL; 99296341Sdelphij# ifndef OPENSSL_NO_ENGINE 100296341Sdelphij char *engine = NULL; 101296341Sdelphij# endif 102296341Sdelphij BIO *out = NULL; 10355714Skris 104296341Sdelphij apps_startup(); 10555714Skris 106296341Sdelphij BN_GENCB_set(&cb, dh_cb, bio_err); 107296341Sdelphij if (bio_err == NULL) 108296341Sdelphij if ((bio_err = BIO_new(BIO_s_file())) != NULL) 109296341Sdelphij BIO_set_fp(bio_err, stderr, BIO_NOCLOSE | BIO_FP_TEXT); 11055714Skris 111296341Sdelphij if (!load_config(bio_err, NULL)) 112296341Sdelphij goto end; 113109998Smarkm 114296341Sdelphij argv++; 115296341Sdelphij argc--; 116296341Sdelphij for (;;) { 117296341Sdelphij if (argc <= 0) 118296341Sdelphij break; 119296341Sdelphij if (strcmp(*argv, "-out") == 0) { 120296341Sdelphij if (--argc < 1) 121296341Sdelphij goto bad; 122296341Sdelphij outfile = *(++argv); 123296341Sdelphij } else if (strcmp(*argv, "-2") == 0) 124296341Sdelphij g = 2; 125296341Sdelphij/*- else if (strcmp(*argv,"-3") == 0) 126296341Sdelphij g=3; */ 127296341Sdelphij else if (strcmp(*argv, "-5") == 0) 128296341Sdelphij g = 5; 129296341Sdelphij# ifndef OPENSSL_NO_ENGINE 130296341Sdelphij else if (strcmp(*argv, "-engine") == 0) { 131296341Sdelphij if (--argc < 1) 132296341Sdelphij goto bad; 133296341Sdelphij engine = *(++argv); 134296341Sdelphij } 135296341Sdelphij# endif 136296341Sdelphij else if (strcmp(*argv, "-rand") == 0) { 137296341Sdelphij if (--argc < 1) 138296341Sdelphij goto bad; 139296341Sdelphij inrand = *(++argv); 140296341Sdelphij } else 141296341Sdelphij break; 142296341Sdelphij argv++; 143296341Sdelphij argc--; 144296341Sdelphij } 145296341Sdelphij if ((argc >= 1) && ((sscanf(*argv, "%d", &num) == 0) || (num < 0))) { 146296341Sdelphij bad: 147296341Sdelphij BIO_printf(bio_err, "usage: gendh [args] [numbits]\n"); 148296341Sdelphij BIO_printf(bio_err, " -out file - output the key to 'file\n"); 149296341Sdelphij BIO_printf(bio_err, " -2 - use 2 as the generator value\n"); 150296341Sdelphij /* 151296341Sdelphij * BIO_printf(bio_err," -3 - use 3 as the generator value\n"); 152296341Sdelphij */ 153296341Sdelphij BIO_printf(bio_err, " -5 - use 5 as the generator value\n"); 154296341Sdelphij# ifndef OPENSSL_NO_ENGINE 155296341Sdelphij BIO_printf(bio_err, 156296341Sdelphij " -engine e - use engine e, possibly a hardware device.\n"); 157296341Sdelphij# endif 158296341Sdelphij BIO_printf(bio_err, " -rand file%cfile%c...\n", LIST_SEPARATOR_CHAR, 159296341Sdelphij LIST_SEPARATOR_CHAR); 160296341Sdelphij BIO_printf(bio_err, 161296341Sdelphij " - load the file (or the files in the directory) into\n"); 162296341Sdelphij BIO_printf(bio_err, " the random number generator\n"); 163296341Sdelphij goto end; 164296341Sdelphij } 165296341Sdelphij# ifndef OPENSSL_NO_ENGINE 166296341Sdelphij setup_engine(bio_err, engine, 0); 167296341Sdelphij# endif 168109998Smarkm 169296341Sdelphij out = BIO_new(BIO_s_file()); 170296341Sdelphij if (out == NULL) { 171296341Sdelphij ERR_print_errors(bio_err); 172296341Sdelphij goto end; 173296341Sdelphij } 17455714Skris 175296341Sdelphij if (outfile == NULL) { 176296341Sdelphij BIO_set_fp(out, stdout, BIO_NOCLOSE); 177296341Sdelphij# ifdef OPENSSL_SYS_VMS 178296341Sdelphij { 179296341Sdelphij BIO *tmpbio = BIO_new(BIO_f_linebuffer()); 180296341Sdelphij out = BIO_push(tmpbio, out); 181296341Sdelphij } 182296341Sdelphij# endif 183296341Sdelphij } else { 184296341Sdelphij if (BIO_write_filename(out, outfile) <= 0) { 185296341Sdelphij perror(outfile); 186296341Sdelphij goto end; 187296341Sdelphij } 188296341Sdelphij } 18955714Skris 190296341Sdelphij if (!app_RAND_load_file(NULL, bio_err, 1) && inrand == NULL) { 191296341Sdelphij BIO_printf(bio_err, 192296341Sdelphij "warning, not much extra random data, consider using the -rand option\n"); 193296341Sdelphij } 194296341Sdelphij if (inrand != NULL) 195296341Sdelphij BIO_printf(bio_err, "%ld semi-random bytes loaded\n", 196296341Sdelphij app_RAND_load_files(inrand)); 19755714Skris 198296341Sdelphij BIO_printf(bio_err, 199296341Sdelphij "Generating DH parameters, %d bit long safe prime, generator %d\n", 200296341Sdelphij num, g); 201296341Sdelphij BIO_printf(bio_err, "This is going to take a long time\n"); 202160814Ssimon 203296341Sdelphij if (((dh = DH_new()) == NULL) 204296341Sdelphij || !DH_generate_parameters_ex(dh, num, g, &cb)) 205296341Sdelphij goto end; 20655714Skris 207296341Sdelphij app_RAND_write_file(NULL, bio_err); 20855714Skris 209296341Sdelphij if (!PEM_write_bio_DHparams(out, dh)) 210296341Sdelphij goto end; 211296341Sdelphij ret = 0; 212296341Sdelphij end: 213296341Sdelphij if (ret != 0) 214296341Sdelphij ERR_print_errors(bio_err); 215296341Sdelphij if (out != NULL) 216296341Sdelphij BIO_free_all(out); 217296341Sdelphij if (dh != NULL) 218296341Sdelphij DH_free(dh); 219296341Sdelphij apps_shutdown(); 220296341Sdelphij OPENSSL_EXIT(ret); 221296341Sdelphij} 222296341Sdelphij 223160814Ssimonstatic int MS_CALLBACK dh_cb(int p, int n, BN_GENCB *cb) 224296341Sdelphij{ 225296341Sdelphij char c = '*'; 22655714Skris 227296341Sdelphij if (p == 0) 228296341Sdelphij c = '.'; 229296341Sdelphij if (p == 1) 230296341Sdelphij c = '+'; 231296341Sdelphij if (p == 2) 232296341Sdelphij c = '*'; 233296341Sdelphij if (p == 3) 234296341Sdelphij c = '\n'; 235296341Sdelphij BIO_write(cb->arg, &c, 1); 236296341Sdelphij (void)BIO_flush(cb->arg); 237296341Sdelphij# ifdef LINT 238296341Sdelphij p = n; 239296341Sdelphij# endif 240296341Sdelphij return 1; 241296341Sdelphij} 242296341Sdelphij#else /* !OPENSSL_NO_DH */ 243238405Sjkim 244238405Sjkim# if PEDANTIC 245296341Sdelphijstatic void *dummy = &dummy; 246238405Sjkim# endif 247238405Sjkim 24855714Skris#endif 249