os/ossrv/ssl/tsrc/topenssl/src/dsaparam.c
changeset 0 bde4ae8d615e
     1.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
     1.2 +++ b/os/ossrv/ssl/tsrc/topenssl/src/dsaparam.c	Fri Jun 15 03:10:57 2012 +0200
     1.3 @@ -0,0 +1,484 @@
     1.4 +/* apps/dsaparam.c */
     1.5 +/* Copyright (C) 1995-1998 Eric Young (eay@cryptsoft.com)
     1.6 + * All rights reserved.
     1.7 + *
     1.8 + * This package is an SSL implementation written
     1.9 + * by Eric Young (eay@cryptsoft.com).
    1.10 + * The implementation was written so as to conform with Netscapes SSL.
    1.11 + * 
    1.12 + * This library is free for commercial and non-commercial use as long as
    1.13 + * the following conditions are aheared to.  The following conditions
    1.14 + * apply to all code found in this distribution, be it the RC4, RSA,
    1.15 + * lhash, DES, etc., code; not just the SSL code.  The SSL documentation
    1.16 + * included with this distribution is covered by the same copyright terms
    1.17 + * except that the holder is Tim Hudson (tjh@cryptsoft.com).
    1.18 + * 
    1.19 + * Copyright remains Eric Young's, and as such any Copyright notices in
    1.20 + * the code are not to be removed.
    1.21 + * If this package is used in a product, Eric Young should be given attribution
    1.22 + * as the author of the parts of the library used.
    1.23 + * This can be in the form of a textual message at program startup or
    1.24 + * in documentation (online or textual) provided with the package.
    1.25 + * 
    1.26 + * Redistribution and use in source and binary forms, with or without
    1.27 + * modification, are permitted provided that the following conditions
    1.28 + * are met:
    1.29 + * 1. Redistributions of source code must retain the copyright
    1.30 + *    notice, this list of conditions and the following disclaimer.
    1.31 + * 2. Redistributions in binary form must reproduce the above copyright
    1.32 + *    notice, this list of conditions and the following disclaimer in the
    1.33 + *    documentation and/or other materials provided with the distribution.
    1.34 + * 3. All advertising materials mentioning features or use of this software
    1.35 + *    must display the following acknowledgement:
    1.36 + *    "This product includes cryptographic software written by
    1.37 + *     Eric Young (eay@cryptsoft.com)"
    1.38 + *    The word 'cryptographic' can be left out if the rouines from the library
    1.39 + *    being used are not cryptographic related :-).
    1.40 + * 4. If you include any Windows specific code (or a derivative thereof) from 
    1.41 + *    the apps directory (application code) you must include an acknowledgement:
    1.42 + *    "This product includes software written by Tim Hudson (tjh@cryptsoft.com)"
    1.43 + * 
    1.44 + * THIS SOFTWARE IS PROVIDED BY ERIC YOUNG ``AS IS'' AND
    1.45 + * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
    1.46 + * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
    1.47 + * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
    1.48 + * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
    1.49 + * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
    1.50 + * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
    1.51 + * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
    1.52 + * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
    1.53 + * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
    1.54 + * SUCH DAMAGE.
    1.55 + * 
    1.56 + * The licence and distribution terms for any publically available version or
    1.57 + * derivative of this code cannot be changed.  i.e. this code cannot simply be
    1.58 + * copied and put under another distribution licence
    1.59 + * [including the GNU Public Licence.]
    1.60 + */
    1.61 +
    1.62 +#include <openssl/opensslconf.h>	/* for OPENSSL_NO_DSA */
    1.63 +/* Until the key-gen callbacks are modified to use newer prototypes, we allow
    1.64 + * deprecated functions for openssl-internal code */
    1.65 +#ifdef OPENSSL_NO_DEPRECATED
    1.66 +#undef OPENSSL_NO_DEPRECATED
    1.67 +#endif
    1.68 +
    1.69 +#ifndef OPENSSL_NO_DSA
    1.70 +#include <assert.h>
    1.71 +#include <stdio.h>
    1.72 +#include <stdlib.h>
    1.73 +#include <time.h>
    1.74 +#include <string.h>
    1.75 +#include "apps.h"
    1.76 +#include <openssl/bio.h>
    1.77 +#include <openssl/err.h>
    1.78 +#include <openssl/bn.h>
    1.79 +#include <openssl/dsa.h>
    1.80 +#include <openssl/x509.h>
    1.81 +#include <openssl/pem.h>
    1.82 +
    1.83 +#undef PROG
    1.84 +#define PROG	dsaparam_main
    1.85 +
    1.86 +/* -inform arg	- input format - default PEM (DER or PEM)
    1.87 + * -outform arg - output format - default PEM
    1.88 + * -in arg	- input file - default stdin
    1.89 + * -out arg	- output file - default stdout
    1.90 + * -noout
    1.91 + * -text
    1.92 + * -C
    1.93 + * -noout
    1.94 + * -genkey
    1.95 + *  #ifdef GENCB_TEST
    1.96 + * -timebomb n  - interrupt keygen after <n> seconds
    1.97 + *  #endif
    1.98 + */
    1.99 +
   1.100 +#ifdef GENCB_TEST
   1.101 +
   1.102 +static int stop_keygen_flag = 0;
   1.103 +
   1.104 +static void timebomb_sigalarm(int foo)
   1.105 +	{
   1.106 +	stop_keygen_flag = 1;
   1.107 +	}
   1.108 +
   1.109 +#endif
   1.110 +
   1.111 +static int MS_CALLBACK dsa_cb(int p, int n, BN_GENCB *cb);
   1.112 +
   1.113 +
   1.114 +
   1.115 +int MAIN(int, char **);
   1.116 +
   1.117 +int MAIN(int argc, char **argv)
   1.118 +	{
   1.119 +#ifndef OPENSSL_NO_ENGINE
   1.120 +	ENGINE *e = NULL;
   1.121 +#endif
   1.122 +	DSA *dsa=NULL;
   1.123 +	int i,badops=0,text=0;
   1.124 +	BIO *in=NULL,*out=NULL;
   1.125 +	int informat,outformat,noout=0,C=0,ret=1;
   1.126 +	char *infile,*outfile,*prog,*inrand=NULL;
   1.127 +	int numbits= -1,num,genkey=0;
   1.128 +	int need_rand=0;
   1.129 +#ifndef OPENSSL_NO_ENGINE
   1.130 +	char *engine=NULL;
   1.131 +#endif
   1.132 +#ifdef GENCB_TEST
   1.133 +	int timebomb=0;
   1.134 +#endif
   1.135 +
   1.136 +	apps_startup();
   1.137 +
   1.138 +	if (bio_err == NULL)
   1.139 +		if ((bio_err=BIO_new(BIO_s_file())) != NULL)
   1.140 +			BIO_set_fp(bio_err,stderr,BIO_NOCLOSE|BIO_FP_TEXT);
   1.141 +
   1.142 +
   1.143 +	if (!load_config(bio_err, NULL))
   1.144 +		goto end;
   1.145 +
   1.146 +	infile=NULL;
   1.147 +	outfile=NULL;
   1.148 +	informat=FORMAT_PEM;
   1.149 +	outformat=FORMAT_PEM;
   1.150 +
   1.151 +	prog=argv[0];
   1.152 +	argc--;
   1.153 +	argv++;
   1.154 +	while (argc >= 1)
   1.155 +		{
   1.156 +		if 	(strcmp(*argv,"-inform") == 0)
   1.157 +			{
   1.158 +			if (--argc < 1) goto bad;
   1.159 +			informat=str2fmt(*(++argv));
   1.160 +			}
   1.161 +		else if (strcmp(*argv,"-outform") == 0)
   1.162 +			{
   1.163 +			if (--argc < 1) goto bad;
   1.164 +			outformat=str2fmt(*(++argv));
   1.165 +			}
   1.166 +		else if (strcmp(*argv,"-in") == 0)
   1.167 +			{
   1.168 +			if (--argc < 1) goto bad;
   1.169 +			infile= *(++argv);
   1.170 +			}
   1.171 +		else if (strcmp(*argv,"-out") == 0)
   1.172 +			{
   1.173 +			if (--argc < 1) goto bad;
   1.174 +			outfile= *(++argv);
   1.175 +			}
   1.176 +#ifndef OPENSSL_NO_ENGINE
   1.177 +		else if(strcmp(*argv, "-engine") == 0)
   1.178 +			{
   1.179 +			if (--argc < 1) goto bad;
   1.180 +			engine = *(++argv);
   1.181 +			}
   1.182 +#endif
   1.183 +#ifdef GENCB_TEST
   1.184 +		else if(strcmp(*argv, "-timebomb") == 0)
   1.185 +			{
   1.186 +			if (--argc < 1) goto bad;
   1.187 +			timebomb = atoi(*(++argv));
   1.188 +			}
   1.189 +#endif
   1.190 +		else if (strcmp(*argv,"-text") == 0)
   1.191 +			text=1;
   1.192 +		else if (strcmp(*argv,"-C") == 0)
   1.193 +			C=1;
   1.194 +		else if (strcmp(*argv,"-genkey") == 0)
   1.195 +			{
   1.196 +			genkey=1;
   1.197 +			need_rand=1;
   1.198 +			}
   1.199 +		else if (strcmp(*argv,"-rand") == 0)
   1.200 +			{
   1.201 +			if (--argc < 1) goto bad;
   1.202 +			inrand= *(++argv);
   1.203 +			need_rand=1;
   1.204 +			}
   1.205 +		else if (strcmp(*argv,"-noout") == 0)
   1.206 +			noout=1;
   1.207 +		else if (sscanf(*argv,"%d",&num) == 1)
   1.208 +			{
   1.209 +			/* generate a key */
   1.210 +			numbits=num;
   1.211 +			need_rand=1;
   1.212 +			}
   1.213 +		else
   1.214 +			{
   1.215 +			BIO_printf(bio_err,"unknown option %s\n",*argv);
   1.216 +			badops=1;
   1.217 +			break;
   1.218 +			}
   1.219 +		argc--;
   1.220 +		argv++;
   1.221 +		}
   1.222 +
   1.223 +	if (badops)
   1.224 +		{
   1.225 +bad:
   1.226 +		BIO_printf(bio_err,"%s [options] [bits] <infile >outfile\n",prog);
   1.227 +		BIO_printf(bio_err,"where options are\n");
   1.228 +		BIO_printf(bio_err," -inform arg   input format - DER or PEM\n");
   1.229 +		BIO_printf(bio_err," -outform arg  output format - DER or PEM\n");
   1.230 +		BIO_printf(bio_err," -in arg       input file\n");
   1.231 +		BIO_printf(bio_err," -out arg      output file\n");
   1.232 +		BIO_printf(bio_err," -text         print as text\n");
   1.233 +		BIO_printf(bio_err," -C            Output C code\n");
   1.234 +		BIO_printf(bio_err," -noout        no output\n");
   1.235 +		BIO_printf(bio_err," -genkey       generate a DSA key\n");
   1.236 +		BIO_printf(bio_err," -rand         files to use for random number input\n");
   1.237 +#ifndef OPENSSL_NO_ENGINE
   1.238 +		BIO_printf(bio_err," -engine e     use engine e, possibly a hardware device.\n");
   1.239 +#endif
   1.240 +#ifdef GENCB_TEST
   1.241 +		BIO_printf(bio_err," -timebomb n   interrupt keygen after <n> seconds\n");
   1.242 +#endif
   1.243 +		BIO_printf(bio_err," number        number of bits to use for generating private key\n");
   1.244 +		goto end;
   1.245 +		}
   1.246 +
   1.247 +	ERR_load_crypto_strings();
   1.248 +
   1.249 +	in=BIO_new(BIO_s_file());
   1.250 +	out=BIO_new(BIO_s_file());
   1.251 +	if ((in == NULL) || (out == NULL))
   1.252 +		{
   1.253 +		ERR_print_errors(bio_err);
   1.254 +		goto end;
   1.255 +		}
   1.256 +
   1.257 +	if (infile == NULL)
   1.258 +		BIO_set_fp(in,stdin,BIO_NOCLOSE);
   1.259 +
   1.260 +	else
   1.261 +		{
   1.262 +		if (BIO_read_filename(in,infile) <= 0)
   1.263 +			{
   1.264 +			perror(infile);
   1.265 +			goto end;
   1.266 +			}
   1.267 +		}
   1.268 +	if (outfile == NULL)
   1.269 +		{
   1.270 +		BIO_set_fp(out,stdout,BIO_NOCLOSE);
   1.271 +		
   1.272 +#ifdef OPENSSL_SYS_VMS
   1.273 +		{
   1.274 +		BIO *tmpbio = BIO_new(BIO_f_linebuffer());
   1.275 +		out = BIO_push(tmpbio, out);
   1.276 +		}
   1.277 +#endif
   1.278 +		}
   1.279 +	else
   1.280 +		{
   1.281 +		if (BIO_write_filename(out,outfile) <= 0)
   1.282 +			{
   1.283 +			perror(outfile);
   1.284 +			goto end;
   1.285 +			}
   1.286 +		}
   1.287 +
   1.288 +#ifndef OPENSSL_NO_ENGINE
   1.289 +        e = setup_engine(bio_err, engine, 0);
   1.290 +#endif
   1.291 +
   1.292 +	if (need_rand)
   1.293 +		{
   1.294 +		app_RAND_load_file(NULL, bio_err, (inrand != NULL));
   1.295 +		if (inrand != NULL)
   1.296 +			BIO_printf(bio_err,"%ld semi-random bytes loaded\n",
   1.297 +				app_RAND_load_files(inrand));
   1.298 +		}
   1.299 +
   1.300 +	if (numbits > 0)
   1.301 +		{
   1.302 +		BN_GENCB cb;
   1.303 +		BN_GENCB_set(&cb, dsa_cb, bio_err);
   1.304 +		assert(need_rand);
   1.305 +		dsa = DSA_new();
   1.306 +		if(!dsa)
   1.307 +			{
   1.308 +			BIO_printf(bio_err,"Error allocating DSA object\n");
   1.309 +			goto end;
   1.310 +			}
   1.311 +		BIO_printf(bio_err,"Generating DSA parameters, %d bit long prime\n",num);
   1.312 +	        BIO_printf(bio_err,"This could take some time\n");
   1.313 +#ifdef GENCB_TEST
   1.314 +		if(timebomb > 0)
   1.315 +	{
   1.316 +		struct sigaction act;
   1.317 +		act.sa_handler = timebomb_sigalarm;
   1.318 +		act.sa_flags = 0;
   1.319 +		BIO_printf(bio_err,"(though I'll stop it if not done within %d secs)\n",
   1.320 +				timebomb);
   1.321 +		if(sigaction(SIGALRM, &act, NULL) != 0)
   1.322 +			{
   1.323 +			BIO_printf(bio_err,"Error, couldn't set SIGALRM handler\n");
   1.324 +			goto end;
   1.325 +			}
   1.326 +		alarm(timebomb);
   1.327 +	}
   1.328 +#endif
   1.329 +	        if(!DSA_generate_parameters_ex(dsa,num,NULL,0,NULL,NULL, &cb))
   1.330 +			{
   1.331 +#ifdef GENCB_TEST
   1.332 +			if(stop_keygen_flag)
   1.333 +				{
   1.334 +				BIO_printf(bio_err,"DSA key generation time-stopped\n");
   1.335 +				/* This is an asked-for behaviour! */
   1.336 +				ret = 0;
   1.337 +				goto end;
   1.338 +				}
   1.339 +#endif
   1.340 +			BIO_printf(bio_err,"Error, DSA key generation failed\n");
   1.341 +			goto end;
   1.342 +			}
   1.343 +		}
   1.344 +	else if	(informat == FORMAT_ASN1)
   1.345 +		dsa=d2i_DSAparams_bio(in,NULL);
   1.346 +	else if (informat == FORMAT_PEM)
   1.347 +		dsa=PEM_read_bio_DSAparams(in,NULL,NULL,NULL);
   1.348 +	else
   1.349 +		{
   1.350 +		BIO_printf(bio_err,"bad input format specified\n");
   1.351 +		goto end;
   1.352 +		}
   1.353 +	if (dsa == NULL)
   1.354 +		{
   1.355 +		BIO_printf(bio_err,"unable to load DSA parameters\n");
   1.356 +		ERR_print_errors(bio_err);
   1.357 +		goto end;
   1.358 +		}
   1.359 +
   1.360 +	if (text)
   1.361 +		{
   1.362 +		DSAparams_print(out,dsa);
   1.363 +		}
   1.364 +	
   1.365 +	if (C)
   1.366 +		{
   1.367 +		unsigned char *data;
   1.368 +		int l,len,bits_p,bits_q,bits_g;
   1.369 +
   1.370 +		len=BN_num_bytes(dsa->p);
   1.371 +		bits_p=BN_num_bits(dsa->p);
   1.372 +		bits_q=BN_num_bits(dsa->q);
   1.373 +		bits_g=BN_num_bits(dsa->g);
   1.374 +		data=(unsigned char *)OPENSSL_malloc(len+20);
   1.375 +		if (data == NULL)
   1.376 +			{
   1.377 +			perror("OPENSSL_malloc");
   1.378 +			goto end;
   1.379 +			}
   1.380 +		l=BN_bn2bin(dsa->p,data);
   1.381 +		printf("static unsigned char dsa%d_p[]={",bits_p);
   1.382 +		for (i=0; i<l; i++)
   1.383 +			{
   1.384 +			if ((i%12) == 0) printf("\n\t");
   1.385 +			printf("0x%02X,",data[i]);
   1.386 +			}
   1.387 +		printf("\n\t};\n");
   1.388 +
   1.389 +		l=BN_bn2bin(dsa->q,data);
   1.390 +		printf("static unsigned char dsa%d_q[]={",bits_p);
   1.391 +		for (i=0; i<l; i++)
   1.392 +			{
   1.393 +			if ((i%12) == 0) printf("\n\t");
   1.394 +			printf("0x%02X,",data[i]);
   1.395 +			}
   1.396 +		printf("\n\t};\n");
   1.397 +
   1.398 +		l=BN_bn2bin(dsa->g,data);
   1.399 +		printf("static unsigned char dsa%d_g[]={",bits_p);
   1.400 +		for (i=0; i<l; i++)
   1.401 +			{
   1.402 +			if ((i%12) == 0) printf("\n\t");
   1.403 +			printf("0x%02X,",data[i]);
   1.404 +			}
   1.405 +		printf("\n\t};\n\n");
   1.406 +
   1.407 +		printf("DSA *get_dsa%d()\n\t{\n",bits_p);
   1.408 +		printf("\tDSA *dsa;\n\n");
   1.409 +		printf("\tif ((dsa=DSA_new()) == NULL) return(NULL);\n");
   1.410 +		printf("\tdsa->p=BN_bin2bn(dsa%d_p,sizeof(dsa%d_p),NULL);\n",
   1.411 +			bits_p,bits_p);
   1.412 +		printf("\tdsa->q=BN_bin2bn(dsa%d_q,sizeof(dsa%d_q),NULL);\n",
   1.413 +			bits_p,bits_p);
   1.414 +		printf("\tdsa->g=BN_bin2bn(dsa%d_g,sizeof(dsa%d_g),NULL);\n",
   1.415 +			bits_p,bits_p);
   1.416 +		printf("\tif ((dsa->p == NULL) || (dsa->q == NULL) || (dsa->g == NULL))\n");
   1.417 +		printf("\t\t{ DSA_free(dsa); return(NULL); }\n");
   1.418 +		printf("\treturn(dsa);\n\t}\n");
   1.419 +	    
   1.420 +		}
   1.421 +
   1.422 +
   1.423 +	if (!noout)
   1.424 +		{
   1.425 +		if 	(outformat == FORMAT_ASN1)
   1.426 +			i=i2d_DSAparams_bio(out,dsa);
   1.427 +		else if (outformat == FORMAT_PEM)
   1.428 +			i=PEM_write_bio_DSAparams(out,dsa);
   1.429 +		else	{
   1.430 +			BIO_printf(bio_err,"bad output format specified for outfile\n");
   1.431 +			goto end;
   1.432 +			}
   1.433 +		if (!i)
   1.434 +			{
   1.435 +			BIO_printf(bio_err,"unable to write DSA parameters\n");
   1.436 +			ERR_print_errors(bio_err);
   1.437 +			goto end;
   1.438 +			}
   1.439 +		}
   1.440 +	if (genkey)
   1.441 +		{
   1.442 +		DSA *dsakey;
   1.443 +
   1.444 +		assert(need_rand);
   1.445 +		if ((dsakey=DSAparams_dup(dsa)) == NULL) goto end;
   1.446 +		if (!DSA_generate_key(dsakey)) goto end;
   1.447 +		if 	(outformat == FORMAT_ASN1)
   1.448 +			i=i2d_DSAPrivateKey_bio(out,dsakey);
   1.449 +		else if (outformat == FORMAT_PEM)
   1.450 +			i=PEM_write_bio_DSAPrivateKey(out,dsakey,NULL,NULL,0,NULL,NULL);
   1.451 +		else	{
   1.452 +			BIO_printf(bio_err,"bad output format specified for outfile\n");
   1.453 +			goto end;
   1.454 +			}
   1.455 +		DSA_free(dsakey);
   1.456 +		}
   1.457 +	if (need_rand)
   1.458 +		app_RAND_write_file(NULL, bio_err);
   1.459 +	ret=0;
   1.460 +end:
   1.461 +	if (in != NULL) BIO_free(in);
   1.462 +	if (out != NULL) BIO_free_all(out);
   1.463 +	if (dsa != NULL) DSA_free(dsa);
   1.464 +	apps_shutdown();
   1.465 +	OPENSSL_EXIT(ret);
   1.466 +	}
   1.467 +
   1.468 +static int MS_CALLBACK dsa_cb(int p, int n, BN_GENCB *cb)
   1.469 +	{
   1.470 +	char c='*';
   1.471 +
   1.472 +	if (p == 0) c='.';
   1.473 +	if (p == 1) c='+';
   1.474 +	if (p == 2) c='*';
   1.475 +	if (p == 3) c='\n';
   1.476 +	BIO_write(cb->arg,&c,1);
   1.477 +	(void)BIO_flush(cb->arg);
   1.478 +#ifdef LINT
   1.479 +	p=n;
   1.480 +#endif
   1.481 +#ifdef GENCB_TEST
   1.482 +	if(stop_keygen_flag)
   1.483 +		return 0;
   1.484 +#endif
   1.485 +	return 1;
   1.486 +	}
   1.487 +#endif