1.1 --- /dev/null Thu Jan 01 00:00:00 1970 +0000
1.2 +++ b/os/ossrv/ssl/tsrc/topenssl/src/gendh.c Fri Jun 15 03:10:57 2012 +0200
1.3 @@ -0,0 +1,240 @@
1.4 +/* apps/gendh.c */
1.5 +/* obsoleted by dhparam.c */
1.6 +/* Copyright (C) 1995-1998 Eric Young (eay@cryptsoft.com)
1.7 + * All rights reserved.
1.8 + *
1.9 + * This package is an SSL implementation written
1.10 + * by Eric Young (eay@cryptsoft.com).
1.11 + * The implementation was written so as to conform with Netscapes SSL.
1.12 + *
1.13 + * This library is free for commercial and non-commercial use as long as
1.14 + * the following conditions are aheared to. The following conditions
1.15 + * apply to all code found in this distribution, be it the RC4, RSA,
1.16 + * lhash, DES, etc., code; not just the SSL code. The SSL documentation
1.17 + * included with this distribution is covered by the same copyright terms
1.18 + * except that the holder is Tim Hudson (tjh@cryptsoft.com).
1.19 + *
1.20 + * Copyright remains Eric Young's, and as such any Copyright notices in
1.21 + * the code are not to be removed.
1.22 + * If this package is used in a product, Eric Young should be given attribution
1.23 + * as the author of the parts of the library used.
1.24 + * This can be in the form of a textual message at program startup or
1.25 + * in documentation (online or textual) provided with the package.
1.26 + *
1.27 + * Redistribution and use in source and binary forms, with or without
1.28 + * modification, are permitted provided that the following conditions
1.29 + * are met:
1.30 + * 1. Redistributions of source code must retain the copyright
1.31 + * notice, this list of conditions and the following disclaimer.
1.32 + * 2. Redistributions in binary form must reproduce the above copyright
1.33 + * notice, this list of conditions and the following disclaimer in the
1.34 + * documentation and/or other materials provided with the distribution.
1.35 + * 3. All advertising materials mentioning features or use of this software
1.36 + * must display the following acknowledgement:
1.37 + * "This product includes cryptographic software written by
1.38 + * Eric Young (eay@cryptsoft.com)"
1.39 + * The word 'cryptographic' can be left out if the rouines from the library
1.40 + * being used are not cryptographic related :-).
1.41 + * 4. If you include any Windows specific code (or a derivative thereof) from
1.42 + * the apps directory (application code) you must include an acknowledgement:
1.43 + * "This product includes software written by Tim Hudson (tjh@cryptsoft.com)"
1.44 + *
1.45 + * THIS SOFTWARE IS PROVIDED BY ERIC YOUNG ``AS IS'' AND
1.46 + * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
1.47 + * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
1.48 + * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
1.49 + * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
1.50 + * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
1.51 + * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
1.52 + * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
1.53 + * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
1.54 + * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
1.55 + * SUCH DAMAGE.
1.56 + *
1.57 + * The licence and distribution terms for any publically available version or
1.58 + * derivative of this code cannot be changed. i.e. this code cannot simply be
1.59 + * copied and put under another distribution licence
1.60 + * [including the GNU Public Licence.]
1.61 + */
1.62 +
1.63 +#include <openssl/opensslconf.h>
1.64 +/* Until the key-gen callbacks are modified to use newer prototypes, we allow
1.65 + * deprecated functions for openssl-internal code */
1.66 +#ifdef OPENSSL_NO_DEPRECATED
1.67 +#undef OPENSSL_NO_DEPRECATED
1.68 +#endif
1.69 +
1.70 +#ifndef OPENSSL_NO_DH
1.71 +#include <stdio.h>
1.72 +#include <string.h>
1.73 +#include <sys/types.h>
1.74 +#include <sys/stat.h>
1.75 +#include "apps.h"
1.76 +#include <openssl/bio.h>
1.77 +#include <openssl/rand.h>
1.78 +#include <openssl/err.h>
1.79 +#include <openssl/bn.h>
1.80 +#include <openssl/dh.h>
1.81 +#include <openssl/x509.h>
1.82 +#include <openssl/pem.h>
1.83 +
1.84 +#define DEFBITS 512
1.85 +#undef PROG
1.86 +#define PROG gendh_main
1.87 +
1.88 +static int MS_CALLBACK dh_cb(int p, int n, BN_GENCB *cb);
1.89 +
1.90 +
1.91 +int MAIN(int, char **);
1.92 +
1.93 +int MAIN(int argc, char **argv)
1.94 + {
1.95 + BN_GENCB cb;
1.96 +#ifndef OPENSSL_NO_ENGINE
1.97 + ENGINE *e = NULL;
1.98 +#endif
1.99 + DH *dh=NULL;
1.100 + int ret=1,num=DEFBITS;
1.101 + int g=2;
1.102 + char *outfile=NULL;
1.103 + char *inrand=NULL;
1.104 +#ifndef OPENSSL_NO_ENGINE
1.105 + char *engine=NULL;
1.106 +#endif
1.107 + BIO *out=NULL;
1.108 +
1.109 + apps_startup();
1.110 +
1.111 + BN_GENCB_set(&cb, dh_cb, bio_err);
1.112 + if (bio_err == NULL)
1.113 + if ((bio_err=BIO_new(BIO_s_file())) != NULL)
1.114 + BIO_set_fp(bio_err,stderr,BIO_NOCLOSE|BIO_FP_TEXT);
1.115 +
1.116 + if (!load_config(bio_err, NULL))
1.117 + goto end;
1.118 +
1.119 + argv++;
1.120 + argc--;
1.121 + for (;;)
1.122 + {
1.123 + if (argc <= 0) break;
1.124 + if (strcmp(*argv,"-out") == 0)
1.125 + {
1.126 + if (--argc < 1) goto bad;
1.127 + outfile= *(++argv);
1.128 + }
1.129 + else if (strcmp(*argv,"-2") == 0)
1.130 + g=2;
1.131 + /* else if (strcmp(*argv,"-3") == 0)
1.132 + g=3; */
1.133 + else if (strcmp(*argv,"-5") == 0)
1.134 + g=5;
1.135 +#ifndef OPENSSL_NO_ENGINE
1.136 + else if (strcmp(*argv,"-engine") == 0)
1.137 + {
1.138 + if (--argc < 1) goto bad;
1.139 + engine= *(++argv);
1.140 + }
1.141 +#endif
1.142 + else if (strcmp(*argv,"-rand") == 0)
1.143 + {
1.144 + if (--argc < 1) goto bad;
1.145 + inrand= *(++argv);
1.146 + }
1.147 + else
1.148 + break;
1.149 + argv++;
1.150 + argc--;
1.151 + }
1.152 + if ((argc >= 1) && ((sscanf(*argv,"%d",&num) == 0) || (num < 0)))
1.153 + {
1.154 +bad:
1.155 + BIO_printf(bio_err,"usage: gendh [args] [numbits]\n");
1.156 + BIO_printf(bio_err," -out file - output the key to 'file\n");
1.157 + BIO_printf(bio_err," -2 - use 2 as the generator value\n");
1.158 + /* BIO_printf(bio_err," -3 - use 3 as the generator value\n"); */
1.159 + BIO_printf(bio_err," -5 - use 5 as the generator value\n");
1.160 +#ifndef OPENSSL_NO_ENGINE
1.161 + BIO_printf(bio_err," -engine e - use engine e, possibly a hardware device.\n");
1.162 +#endif
1.163 + BIO_printf(bio_err," -rand file%cfile%c...\n", LIST_SEPARATOR_CHAR, LIST_SEPARATOR_CHAR);
1.164 + BIO_printf(bio_err," - load the file (or the files in the directory) into\n");
1.165 + BIO_printf(bio_err," the random number generator\n");
1.166 + goto end;
1.167 + }
1.168 +
1.169 +#ifndef OPENSSL_NO_ENGINE
1.170 + e = setup_engine(bio_err, engine, 0);
1.171 +#endif
1.172 +
1.173 + out=BIO_new(BIO_s_file());
1.174 + if (out == NULL)
1.175 + {
1.176 + ERR_print_errors(bio_err);
1.177 + goto end;
1.178 + }
1.179 +
1.180 + if (outfile == NULL)
1.181 + {
1.182 + BIO_set_fp(out,stdout,BIO_NOCLOSE);
1.183 +
1.184 +#ifdef OPENSSL_SYS_VMS
1.185 + {
1.186 + BIO *tmpbio = BIO_new(BIO_f_linebuffer());
1.187 + out = BIO_push(tmpbio, out);
1.188 + }
1.189 +#endif
1.190 + }
1.191 + else
1.192 + {
1.193 + if (BIO_write_filename(out,outfile) <= 0)
1.194 + {
1.195 + perror(outfile);
1.196 + goto end;
1.197 + }
1.198 + }
1.199 +
1.200 + if (!app_RAND_load_file(NULL, bio_err, 1) && inrand == NULL)
1.201 + {
1.202 + BIO_printf(bio_err,"warning, not much extra random data, consider using the -rand option\n");
1.203 + }
1.204 + if (inrand != NULL)
1.205 + BIO_printf(bio_err,"%ld semi-random bytes loaded\n",
1.206 + app_RAND_load_files(inrand));
1.207 +
1.208 + BIO_printf(bio_err,"Generating DH parameters, %d bit long safe prime, generator %d\n",num,g);
1.209 + BIO_printf(bio_err,"This is going to take a long time\n");
1.210 +
1.211 + if(((dh = DH_new()) == NULL) || !DH_generate_parameters_ex(dh, num, g, &cb))
1.212 + goto end;
1.213 +
1.214 + app_RAND_write_file(NULL, bio_err);
1.215 +
1.216 + if (!PEM_write_bio_DHparams(out,dh))
1.217 + goto end;
1.218 + ret=0;
1.219 +end:
1.220 + if (ret != 0)
1.221 + ERR_print_errors(bio_err);
1.222 + if (out != NULL) BIO_free_all(out);
1.223 + if (dh != NULL) DH_free(dh);
1.224 + apps_shutdown();
1.225 + OPENSSL_EXIT(ret);
1.226 + }
1.227 +
1.228 +static int MS_CALLBACK dh_cb(int p, int n, BN_GENCB *cb)
1.229 + {
1.230 + char c='*';
1.231 +
1.232 + if (p == 0) c='.';
1.233 + if (p == 1) c='+';
1.234 + if (p == 2) c='*';
1.235 + if (p == 3) c='\n';
1.236 + BIO_write(cb->arg,&c,1);
1.237 + (void)BIO_flush(cb->arg);
1.238 +#ifdef LINT
1.239 + p=n;
1.240 +#endif
1.241 + return 1;
1.242 + }
1.243 +#endif