1.1 --- /dev/null Thu Jan 01 00:00:00 1970 +0000
1.2 +++ b/os/ossrv/ssl/tsrc/BC/libcrypto/topenssl/src/dh.c Fri Jun 15 03:10:57 2012 +0200
1.3 @@ -0,0 +1,355 @@
1.4 +/* apps/dh.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> /* for OPENSSL_NO_DH */
1.64 +#ifndef OPENSSL_NO_DH
1.65 +#include <stdio.h>
1.66 +#include <stdlib.h>
1.67 +#include <time.h>
1.68 +#include <string.h>
1.69 +#include "apps.h"
1.70 +#include <openssl/bio.h>
1.71 +#include <openssl/err.h>
1.72 +#include <openssl/bn.h>
1.73 +#include <openssl/dh.h>
1.74 +#include <openssl/x509.h>
1.75 +#include <openssl/pem.h>
1.76 +
1.77 +#undef PROG
1.78 +#define PROG dh_main
1.79 +
1.80 +/* -inform arg - input format - default PEM (DER or PEM)
1.81 + * -outform arg - output format - default PEM
1.82 + * -in arg - input file - default stdin
1.83 + * -out arg - output file - default stdout
1.84 + * -check - check the parameters are ok
1.85 + * -noout
1.86 + * -text
1.87 + * -C
1.88 + */
1.89 +
1.90 +
1.91 +int MAIN(int, char **);
1.92 +
1.93 +int MAIN(int argc, char **argv)
1.94 + {
1.95 +#ifndef OPENSSL_NO_ENGINE
1.96 + ENGINE *e = NULL;
1.97 +#endif
1.98 + DH *dh=NULL;
1.99 + int i,badops=0,text=0;
1.100 + BIO *in=NULL,*out=NULL;
1.101 + int informat,outformat,check=0,noout=0,C=0,ret=1;
1.102 + char *infile,*outfile,*prog;
1.103 +#ifndef OPENSSL_NO_ENGINE
1.104 + char *engine;
1.105 +#endif
1.106 +
1.107 + apps_startup();
1.108 +
1.109 + if (bio_err == NULL)
1.110 + if ((bio_err=BIO_new(BIO_s_file())) != NULL)
1.111 + BIO_set_fp(bio_err,stderr,BIO_NOCLOSE|BIO_FP_TEXT);
1.112 +
1.113 + if (!load_config(bio_err, NULL))
1.114 + goto end;
1.115 +
1.116 +#ifndef OPENSSL_NO_ENGINE
1.117 + engine=NULL;
1.118 +#endif
1.119 + infile=NULL;
1.120 + outfile=NULL;
1.121 + informat=FORMAT_PEM;
1.122 + outformat=FORMAT_PEM;
1.123 +
1.124 + prog=argv[0];
1.125 + argc--;
1.126 + argv++;
1.127 + while (argc >= 1)
1.128 + {
1.129 + if (strcmp(*argv,"-inform") == 0)
1.130 + {
1.131 + if (--argc < 1) goto bad;
1.132 + informat=str2fmt(*(++argv));
1.133 + }
1.134 + else if (strcmp(*argv,"-outform") == 0)
1.135 + {
1.136 + if (--argc < 1) goto bad;
1.137 + outformat=str2fmt(*(++argv));
1.138 + }
1.139 + else if (strcmp(*argv,"-in") == 0)
1.140 + {
1.141 + if (--argc < 1) goto bad;
1.142 + infile= *(++argv);
1.143 + }
1.144 + else if (strcmp(*argv,"-out") == 0)
1.145 + {
1.146 + if (--argc < 1) goto bad;
1.147 + outfile= *(++argv);
1.148 + }
1.149 +#ifndef OPENSSL_NO_ENGINE
1.150 + else if (strcmp(*argv,"-engine") == 0)
1.151 + {
1.152 + if (--argc < 1) goto bad;
1.153 + engine= *(++argv);
1.154 + }
1.155 +#endif
1.156 + else if (strcmp(*argv,"-check") == 0)
1.157 + check=1;
1.158 + else if (strcmp(*argv,"-text") == 0)
1.159 + text=1;
1.160 + else if (strcmp(*argv,"-C") == 0)
1.161 + C=1;
1.162 + else if (strcmp(*argv,"-noout") == 0)
1.163 + noout=1;
1.164 + else
1.165 + {
1.166 + BIO_printf(bio_err,"unknown option %s\n",*argv);
1.167 + badops=1;
1.168 + break;
1.169 + }
1.170 + argc--;
1.171 + argv++;
1.172 + }
1.173 +
1.174 + if (badops)
1.175 + {
1.176 +bad:
1.177 + BIO_printf(bio_err,"%s [options] <infile >outfile\n",prog);
1.178 + BIO_printf(bio_err,"where options are\n");
1.179 + BIO_printf(bio_err," -inform arg input format - one of DER PEM\n");
1.180 + BIO_printf(bio_err," -outform arg output format - one of DER PEM\n");
1.181 + BIO_printf(bio_err," -in arg input file\n");
1.182 + BIO_printf(bio_err," -out arg output file\n");
1.183 + BIO_printf(bio_err," -check check the DH parameters\n");
1.184 + BIO_printf(bio_err," -text print a text form of the DH parameters\n");
1.185 + BIO_printf(bio_err," -C Output C code\n");
1.186 + BIO_printf(bio_err," -noout no output\n");
1.187 +#ifndef OPENSSL_NO_ENGINE
1.188 + BIO_printf(bio_err," -engine e use engine e, possibly a hardware device.\n");
1.189 +#endif
1.190 + goto end;
1.191 + }
1.192 +
1.193 + ERR_load_crypto_strings();
1.194 +
1.195 +#ifndef OPENSSL_NO_ENGINE
1.196 + e = setup_engine(bio_err, engine, 0);
1.197 +#endif
1.198 +
1.199 + in=BIO_new(BIO_s_file());
1.200 + out=BIO_new(BIO_s_file());
1.201 + if ((in == NULL) || (out == NULL))
1.202 + {
1.203 + ERR_print_errors(bio_err);
1.204 + goto end;
1.205 + }
1.206 +
1.207 + if (infile == NULL)
1.208 + BIO_set_fp(in,stdin,BIO_NOCLOSE);
1.209 +
1.210 + else
1.211 + {
1.212 + if (BIO_read_filename(in,infile) <= 0)
1.213 + {
1.214 + perror(infile);
1.215 + goto end;
1.216 + }
1.217 + }
1.218 + if (outfile == NULL)
1.219 + {
1.220 + BIO_set_fp(out,stdout,BIO_NOCLOSE);
1.221 +#ifdef OPENSSL_SYS_VMS
1.222 + {
1.223 + BIO *tmpbio = BIO_new(BIO_f_linebuffer());
1.224 + out = BIO_push(tmpbio, out);
1.225 + }
1.226 +#endif
1.227 + }
1.228 + else
1.229 + {
1.230 + if (BIO_write_filename(out,outfile) <= 0)
1.231 + {
1.232 + perror(outfile);
1.233 + goto end;
1.234 + }
1.235 + }
1.236 +
1.237 + if (informat == FORMAT_ASN1)
1.238 + dh=d2i_DHparams_bio(in,NULL);
1.239 + else if (informat == FORMAT_PEM)
1.240 + dh=PEM_read_bio_DHparams(in,NULL,NULL,NULL);
1.241 + else
1.242 + {
1.243 + BIO_printf(bio_err,"bad input format specified\n");
1.244 + goto end;
1.245 + }
1.246 + if (dh == NULL)
1.247 + {
1.248 + BIO_printf(bio_err,"unable to load DH parameters\n");
1.249 + ERR_print_errors(bio_err);
1.250 + goto end;
1.251 + }
1.252 +
1.253 +
1.254 +
1.255 + if (text)
1.256 + {
1.257 + DHparams_print(out,dh);
1.258 +#ifdef undef
1.259 + printf("p=");
1.260 + BN_print(stdout,dh->p);
1.261 + printf("\ng=");
1.262 + BN_print(stdout,dh->g);
1.263 + printf("\n");
1.264 + if (dh->length != 0)
1.265 + printf("recommended private length=%ld\n",dh->length);
1.266 +#endif
1.267 + }
1.268 +
1.269 + if (check)
1.270 + {
1.271 + if (!DH_check(dh,&i))
1.272 + {
1.273 + ERR_print_errors(bio_err);
1.274 + goto end;
1.275 + }
1.276 + if (i & DH_CHECK_P_NOT_PRIME)
1.277 + printf("p value is not prime\n");
1.278 + if (i & DH_CHECK_P_NOT_SAFE_PRIME)
1.279 + printf("p value is not a safe prime\n");
1.280 + if (i & DH_UNABLE_TO_CHECK_GENERATOR)
1.281 + printf("unable to check the generator value\n");
1.282 + if (i & DH_NOT_SUITABLE_GENERATOR)
1.283 + printf("the g value is not a generator\n");
1.284 + if (i == 0)
1.285 + printf("DH parameters appear to be ok.\n");
1.286 +
1.287 + }
1.288 + if (C)
1.289 + {
1.290 + unsigned char *data;
1.291 + int len,l,bits;
1.292 +
1.293 + len=BN_num_bytes(dh->p);
1.294 + bits=BN_num_bits(dh->p);
1.295 + data=(unsigned char *)OPENSSL_malloc(len);
1.296 + if (data == NULL)
1.297 + {
1.298 + perror("OPENSSL_malloc");
1.299 + goto end;
1.300 + }
1.301 + l=BN_bn2bin(dh->p,data);
1.302 + printf("static unsigned char dh%d_p[]={",bits);
1.303 + for (i=0; i<l; i++)
1.304 + {
1.305 + if ((i%12) == 0) printf("\n\t");
1.306 + printf("0x%02X,",data[i]);
1.307 + }
1.308 + printf("\n\t};\n");
1.309 +
1.310 + l=BN_bn2bin(dh->g,data);
1.311 + printf("static unsigned char dh%d_g[]={",bits);
1.312 + for (i=0; i<l; i++)
1.313 + {
1.314 + if ((i%12) == 0) printf("\n\t");
1.315 + printf("0x%02X,",data[i]);
1.316 + }
1.317 + printf("\n\t};\n\n");
1.318 +
1.319 + printf("DH *get_dh%d()\n\t{\n",bits);
1.320 + printf("\tDH *dh;\n\n");
1.321 + printf("\tif ((dh=DH_new()) == NULL) return(NULL);\n");
1.322 + printf("\tdh->p=BN_bin2bn(dh%d_p,sizeof(dh%d_p),NULL);\n",
1.323 + bits,bits);
1.324 + printf("\tdh->g=BN_bin2bn(dh%d_g,sizeof(dh%d_g),NULL);\n",
1.325 + bits,bits);
1.326 + printf("\tif ((dh->p == NULL) || (dh->g == NULL))\n");
1.327 + printf("\t\treturn(NULL);\n");
1.328 + printf("\treturn(dh);\n\t}\n");
1.329 + OPENSSL_free(data);
1.330 + }
1.331 +
1.332 +
1.333 + if (!noout)
1.334 + {
1.335 + if (outformat == FORMAT_ASN1)
1.336 + i=i2d_DHparams_bio(out,dh);
1.337 + else if (outformat == FORMAT_PEM)
1.338 + i=PEM_write_bio_DHparams(out,dh);
1.339 + else {
1.340 + BIO_printf(bio_err,"bad output format specified for outfile\n");
1.341 + goto end;
1.342 + }
1.343 + if (!i)
1.344 + {
1.345 + BIO_printf(bio_err,"unable to write DH parameters\n");
1.346 + ERR_print_errors(bio_err);
1.347 + goto end;
1.348 + }
1.349 + }
1.350 + ret=0;
1.351 +end:
1.352 + if (in != NULL) BIO_free(in);
1.353 + if (out != NULL) BIO_free_all(out);
1.354 + if (dh != NULL) DH_free(dh);
1.355 + apps_shutdown();
1.356 + OPENSSL_EXIT(ret);
1.357 + }
1.358 +#endif