os/ossrv/ssl/tsrc/BC/libcrypto/topenssl/src/openssl.c
changeset 0 bde4ae8d615e
     1.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
     1.2 +++ b/os/ossrv/ssl/tsrc/BC/libcrypto/topenssl/src/openssl.c	Fri Jun 15 03:10:57 2012 +0200
     1.3 @@ -0,0 +1,1092 @@
     1.4 +/* apps/openssl.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 + * Copyright (c) 1998-2001 The OpenSSL Project.  All rights reserved.
    1.63 + *
    1.64 + * Redistribution and use in source and binary forms, with or without
    1.65 + * modification, are permitted provided that the following conditions
    1.66 + * are met:
    1.67 + *
    1.68 + * 1. Redistributions of source code must retain the above copyright
    1.69 + *    notice, this list of conditions and the following disclaimer.
    1.70 + *
    1.71 + * 2. Redistributions in binary form must reproduce the above copyright
    1.72 + *    notice, this list of conditions and the following disclaimer in
    1.73 + *    the documentation and/or other materials provided with the
    1.74 + *    distribution.
    1.75 + *
    1.76 + * 3. All advertising materials mentioning features or use of this
    1.77 + *    software must display the following acknowledgment:
    1.78 + *    "This product includes software developed by the OpenSSL Project
    1.79 + *    for use in the OpenSSL Toolkit. (http://www.openssl.org/)"
    1.80 + *
    1.81 + * 4. The names "OpenSSL Toolkit" and "OpenSSL Project" must not be used to
    1.82 + *    endorse or promote products derived from this software without
    1.83 + *    prior written permission. For written permission, please contact
    1.84 + *    openssl-core@openssl.org.
    1.85 + *
    1.86 + * 5. Products derived from this software may not be called "OpenSSL"
    1.87 + *    nor may "OpenSSL" appear in their names without prior written
    1.88 + *    permission of the OpenSSL Project.
    1.89 + *
    1.90 + * 6. Redistributions of any form whatsoever must retain the following
    1.91 + *    acknowledgment:
    1.92 + *    "This product includes software developed by the OpenSSL Project
    1.93 + *    for use in the OpenSSL Toolkit (http://www.openssl.org/)"
    1.94 + *
    1.95 + * THIS SOFTWARE IS PROVIDED BY THE OpenSSL PROJECT ``AS IS'' AND ANY
    1.96 + * EXPRESSED OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
    1.97 + * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
    1.98 + * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE OpenSSL PROJECT OR
    1.99 + * ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
   1.100 + * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
   1.101 + * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
   1.102 + * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
   1.103 + * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
   1.104 + * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
   1.105 + * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED
   1.106 + * OF THE POSSIBILITY OF SUCH DAMAGE.
   1.107 + * ====================================================================
   1.108 + *
   1.109 + * This product includes cryptographic software written by Eric Young
   1.110 + * (eay@cryptsoft.com).  This product includes software written by Tim
   1.111 + * Hudson (tjh@cryptsoft.com).
   1.112 + *
   1.113 + */
   1.114 +
   1.115 +
   1.116 +#include <stdio.h>
   1.117 +#include <string.h>
   1.118 +#include <stdlib.h>
   1.119 +#include <dirent.h>
   1.120 +#define OPENSSL_C /* tells apps.h to use complete apps_startup() */
   1.121 +#include "apps.h"
   1.122 +#include <openssl/bio.h>
   1.123 +#include <openssl/crypto.h>
   1.124 +#include <openssl/lhash.h>
   1.125 +#include <openssl/conf.h>
   1.126 +#include <openssl/x509.h>
   1.127 +#include <openssl/pem.h>
   1.128 +#include <openssl/ssl.h>
   1.129 +#ifndef OPENSSL_NO_ENGINE
   1.130 +#include <openssl/engine.h>
   1.131 +#endif
   1.132 +#define USE_SOCKETS /* needed for the _O_BINARY defs in the MS world */
   1.133 +#include "progs.h"
   1.134 +#include "s_apps.h"
   1.135 +#include <openssl/err.h>
   1.136 +#ifdef SYMBIAN
   1.137 +#include "topenssl.h"
   1.138 +#endif
   1.139 +
   1.140 +/* The LHASH callbacks ("hash" & "cmp") have been replaced by functions with the
   1.141 + * base prototypes (we cast each variable inside the function to the required
   1.142 + * type of "FUNCTION*"). This removes the necessity for macro-generated wrapper
   1.143 + * functions. */
   1.144 +
   1.145 +/* static unsigned long MS_CALLBACK hash(FUNCTION *a); */
   1.146 +static unsigned long MS_CALLBACK hash(const void *a_void);
   1.147 +/* static int MS_CALLBACK cmp(FUNCTION *a,FUNCTION *b); */
   1.148 +static int MS_CALLBACK cmp(const void *a_void,const void *b_void);
   1.149 +static LHASH *prog_init(void );
   1.150 +static int do_cmd(LHASH *prog,int argc,char *argv[]);
   1.151 +char *default_config_file=NULL;
   1.152 +
   1.153 +/* Make sure there is only one when MONOLITH is defined */
   1.154 +#ifdef MONOLITH
   1.155 +CONF *config=NULL;
   1.156 +BIO *bio_err=NULL;
   1.157 +#endif
   1.158 +
   1.159 +#ifdef SYMBIAN
   1.160 +FILE *fp_stdout;
   1.161 +FILE *fp_stderr;
   1.162 +FILE *fp_stdin;
   1.163 +#endif
   1.164 +
   1.165 +
   1.166 +static void lock_dbg_cb(int mode, int type, const char *file, int line)
   1.167 +	{
   1.168 +	static int modes[CRYPTO_NUM_LOCKS]; /* = {0, 0, ... } */
   1.169 +	const char *errstr = NULL;
   1.170 +	int rw;
   1.171 +
   1.172 +	rw = mode & (CRYPTO_READ|CRYPTO_WRITE);
   1.173 +	if (!((rw == CRYPTO_READ) || (rw == CRYPTO_WRITE)))
   1.174 +		{
   1.175 +		errstr = "invalid mode";
   1.176 +		goto err;
   1.177 +		}
   1.178 +
   1.179 +	if (type < 0 || type >= CRYPTO_NUM_LOCKS)
   1.180 +		{
   1.181 +		errstr = "type out of bounds";
   1.182 +		goto err;
   1.183 +		}
   1.184 +
   1.185 +	if (mode & CRYPTO_LOCK)
   1.186 +		{
   1.187 +		if (modes[type])
   1.188 +			{
   1.189 +			errstr = "already locked";
   1.190 +			/* must not happen in a single-threaded program
   1.191 +			 * (would deadlock) */
   1.192 +			goto err;
   1.193 +			}
   1.194 +
   1.195 +		modes[type] = rw;
   1.196 +		}
   1.197 +	else if (mode & CRYPTO_UNLOCK)
   1.198 +		{
   1.199 +		if (!modes[type])
   1.200 +			{
   1.201 +			errstr = "not locked";
   1.202 +			goto err;
   1.203 +			}
   1.204 +
   1.205 +		if (modes[type] != rw)
   1.206 +			{
   1.207 +			errstr = (rw == CRYPTO_READ) ?
   1.208 +				"CRYPTO_r_unlock on write lock" :
   1.209 +				"CRYPTO_w_unlock on read lock";
   1.210 +			}
   1.211 +
   1.212 +		modes[type] = 0;
   1.213 +		}
   1.214 +	else
   1.215 +		{
   1.216 +		errstr = "invalid mode";
   1.217 +		goto err;
   1.218 +		}
   1.219 +
   1.220 + err:
   1.221 +	if (errstr)
   1.222 +		{
   1.223 +		/* we cannot use bio_err here */
   1.224 +#ifndef SYMBIAN
   1.225 +		fprintf(stderr, "openssl (lock_dbg_cb): %s (mode=%d, type=%d) at %s:%d\n",
   1.226 +			errstr, mode, type, file, line);
   1.227 +#else
   1.228 +        fprintf(fp_stderr, "openssl (lock_dbg_cb): %s (mode=%d, type=%d) at %s:%d\n",
   1.229 +			errstr, mode, type, file, line);
   1.230 +        fclose(fp_stderr)			;
   1.231 +#endif
   1.232 +		}
   1.233 +	}
   1.234 +
   1.235 +
   1.236 +
   1.237 +#ifdef SYMBIAN
   1.238 +void testResultXml(char *filename,int retval)
   1.239 +{
   1.240 +	char time_buf[50];
   1.241 +
   1.242 +	char result[10];
   1.243 +
   1.244 +	char xmlfilename[256];
   1.245 +
   1.246 +	time_t t = time(NULL);
   1.247 +
   1.248 +	struct tm *tm1 = localtime(&t);
   1.249 +
   1.250 +	char *atsinitmsg 	= 	"<test-report>\n\t<test-batch>";
   1.251 +
   1.252 +	char *atsbatchinit1	=   \
   1.253 +							"\n\t\t<batch-init>\
   1.254 +							\n\t\t\t<description></description>\
   1.255 +							\n\t\t\t<date>";
   1.256 +
   1.257 +	char *atsbatchinit2 =	"</date>\
   1.258 +							\n\t\t\t<factory>NA</factory>\
   1.259 +							\n\t\t\t<component>\
   1.260 +							\n\t\t\t\t<name>NA</name>\
   1.261 +							\n\t\t\t\t<version>NA</version>\
   1.262 +							\n\t\t\t</component>\
   1.263 +							\n\t\t</batch-init>";
   1.264 +
   1.265 +	char *atsbatchresult=	\
   1.266 +							"\n\t\t<batch-result>\
   1.267 +							\n\t\t\t<run-time>00:00:00</run-time>\
   1.268 +							\n\t\t</batch-result>";
   1.269 +
   1.270 +	char *atsclosemsg	=   \
   1.271 +							"\n\t</test-batch>\
   1.272 +							\n</test-report>\n ";
   1.273 +
   1.274 +	char *atstestinit	=	"\n\t\t<test-case time-stamp=\"00:00:00\">";
   1.275 +
   1.276 +
   1.277 +	char *atscaseinit1	=	\
   1.278 +							"\n\t\t\t<case-init>\
   1.279 +							\n\t\t\t\t<version></version>\
   1.280 +							\n\t\t\t\t<id>";
   1.281 +
   1.282 +	char *atscaseinit2 = 	"</id>\
   1.283 +							\n\t\t\t\t<expected-result description=\"\">0</expected-result>\
   1.284 +							\n\t\t\t</case-init>";
   1.285 +
   1.286 +	char *atscaseresult1=	\
   1.287 +							"\n\t\t\t<case-result status=\"";
   1.288 +
   1.289 +	char *atscaseresult2=	"\">\
   1.290 +							\n\t\t\t\t<actual-result>0</actual-result>\
   1.291 +							\n\t\t\t\t<run-time>00:00:00</run-time>\
   1.292 +							\n\t\t\t</case-result>";
   1.293 +
   1.294 +	char *atstestclose	=	"\n\t\t</test-case>";
   1.295 +
   1.296 +	DIR *dir;
   1.297 +	FILE *fp;
   1.298 +
   1.299 +	dir = opendir("c:\\spd_logs");
   1.300 +	if(!dir)
   1.301 +		mkdir("c:\\spd_logs",0777);
   1.302 +
   1.303 +	dir = opendir("c:\\spd_logs\\xml");
   1.304 +	if(!dir)
   1.305 +		mkdir("c:\\spd_logs\\xml",0777);
   1.306 +
   1.307 +	// create the xml file name
   1.308 +	strcpy(xmlfilename,"c:/spd_logs/xml/");
   1.309 +	strcat(xmlfilename,filename);
   1.310 +	strcat(xmlfilename,".xml");
   1.311 +
   1.312 +	strftime(time_buf,50,"%c",tm1);
   1.313 +
   1.314 +	if(retval)
   1.315 +		strcpy(result,"FAILED");
   1.316 +	else
   1.317 +		strcpy(result,"PASSED");
   1.318 +
   1.319 +	fp = fopen(xmlfilename,"w");
   1.320 +
   1.321 +	if(fp)
   1.322 +	{
   1.323 +		fprintf(fp,"%s%s%s%s%s%s%s%s%s%s%s%s%s%s",atsinitmsg,atsbatchinit1,time_buf,atsbatchinit2,atstestinit,
   1.324 +			atscaseinit1,filename,atscaseinit2,atscaseresult1,result,atscaseresult2,
   1.325 +			atstestclose,atsbatchresult,atsclosemsg);
   1.326 +
   1.327 +		fclose(fp);
   1.328 +	}
   1.329 +}
   1.330 +
   1.331 +
   1.332 +int openssl_init(void)
   1.333 +{
   1.334 +
   1.335 +	fp_stdout = freopen(LOG_STDOUT,"w+",stdout);
   1.336 +	if(!fp_stdout)
   1.337 +	return 1;
   1.338 +	fp_stderr = freopen(LOG_STDERR,"w+",stderr);
   1.339 +	if(!fp_stderr)
   1.340 +	return 1;
   1.341 +	fp_stdin = freopen(LOG_STDIN,"w+",stdin);
   1.342 +	if(!fp_stdin)
   1.343 +	return 1;
   1.344 +	return 0;
   1.345 +}
   1.346 +
   1.347 +void openssl_deinit(void)
   1.348 +{
   1.349 +    fclose(fp_stdout);
   1.350 +    fclose(fp_stderr);
   1.351 +    fclose(fp_stdin);
   1.352 +    fp_stdout=NULL;
   1.353 +    fp_stdin=NULL;
   1.354 +    fp_stderr=NULL;
   1.355 +
   1.356 +}
   1.357 +
   1.358 +int append(char *file1,char *file2, char *dst_file )
   1.359 +{
   1.360 +   FILE *fp1 = NULL;
   1.361 +   FILE *fp2 = NULL;
   1.362 +   FILE *fp3 = NULL;
   1.363 +
   1.364 +   long file1_size;
   1.365 +   long file2_size;
   1.366 +   char *file1_data = NULL;
   1.367 +   char *file2_data = NULL;
   1.368 +
   1.369 +   int ret;
   1.370 +
   1.371 +   if(!(fp1 = fopen(file1,"r+")))
   1.372 +   {
   1.373 +	 fprintf(stdout,"Faliure opening file %s.",file1);
   1.374 +     ret = -1;
   1.375 +	 goto err;
   1.376 +   }
   1.377 +
   1.378 +   if(!(fp2 = fopen(file2,"r+")))
   1.379 +   {
   1.380 +	 fprintf(stdout,"Faliure opening file %s.",file2);
   1.381 +	 ret = -1;
   1.382 +	 goto err;
   1.383 +   }
   1.384 +
   1.385 +   if(!(fp3= fopen(dst_file,"w+")))
   1.386 +   {
   1.387 +	 fprintf(stdout,"Faliure opening file %s.",dst_file);
   1.388 +	 ret = -1;
   1.389 +	 goto err;
   1.390 +   }
   1.391 +
   1.392 +   if((ret = fseek(fp1, 0, SEEK_END)) < 0)
   1.393 +   {
   1.394 +   	  ret = -1;
   1.395 +	  goto err;
   1.396 +   }
   1.397 +
   1.398 +
   1.399 +   if((ret = fseek(fp2, 0, SEEK_END)) < 0)
   1.400 +   {
   1.401 +   	 ret = -1;
   1.402 +	 goto err;
   1.403 +   }
   1.404 +
   1.405 +
   1.406 +   if((file1_size = ftell(fp1)) < 0)
   1.407 +   {
   1.408 +   	 ret = -1;
   1.409 +	 goto err;
   1.410 +   }
   1.411 +
   1.412 +   if((file2_size = ftell(fp2)) < 0)
   1.413 +   {
   1.414 +     ret = -1;
   1.415 +	 goto err;
   1.416 +   }
   1.417 +
   1.418 +   if((ret = fseek(fp1, 0, SEEK_SET)) < 0)
   1.419 +   {
   1.420 +   	 ret = -1;
   1.421 +	 goto err;
   1.422 +   }
   1.423 +
   1.424 +   if((ret = fseek(fp2, 0, SEEK_SET)) < 0)
   1.425 +   {
   1.426 +   	 ret = -1;
   1.427 +	 goto err;
   1.428 +   }
   1.429 +
   1.430 +   file1_data = (char *)malloc(file1_size);
   1.431 +   file2_data = (char *)malloc(file2_size);
   1.432 +
   1.433 +   if(!file1_data||!file2_data)
   1.434 +   {
   1.435 +   	 ret = -1;
   1.436 +	 goto err;
   1.437 +   }
   1.438 +   memset(file1_data,0,file1_size);
   1.439 +
   1.440 +   memset(file2_data,0,file2_size);
   1.441 +
   1.442 +   ret = fread(file1_data,sizeof(char),file1_size,fp1);
   1.443 +   if(ret < file1_size)
   1.444 +   {
   1.445 +	fprintf(stdout,"Failed reading file %s.", file1);
   1.446 +	ret = -1;
   1.447 +    goto err;
   1.448 +   }
   1.449 +
   1.450 +  ret = fread(file2_data,sizeof(char),file2_size,fp2);
   1.451 +  if(ret < file2_size)
   1.452 +  {
   1.453 +	fprintf(stdout,"Failed reading file %s.",file2);
   1.454 +	ret = -1;
   1.455 +	goto err;
   1.456 +  }
   1.457 +
   1.458 +  ret = fwrite(file1_data,sizeof(char),file1_size,fp3);
   1.459 +  if(ret < file1_size)
   1.460 +  {
   1.461 +	fprintf(stdout,"Failed wrting from %s file to file %s.",file1,dst_file);
   1.462 +	ret = -1;
   1.463 +	goto err;
   1.464 +  }
   1.465 +  ret = fwrite(file2_data,sizeof(char),file2_size,fp3);
   1.466 +  if(ret < file2_size)
   1.467 +  {
   1.468 +	fprintf(stdout,"Failed wrting from %s file to file %s.",file2,dst_file);
   1.469 +	ret = -1;
   1.470 +	goto err;
   1.471 +  }
   1.472 +  ret=0;
   1.473 +
   1.474 +err:  if(file1_data)
   1.475 +  free(file1_data);
   1.476 +  if(file2_data)
   1.477 +  free(file2_data);
   1.478 +  fclose(fp1);
   1.479 +  fclose(fp2);
   1.480 +  fclose(fp3);
   1.481 +  return ret;
   1.482 +
   1.483 +}
   1.484 +void print_logs(int argc, char *argv[])
   1.485 +{
   1.486 +	int i=0;
   1.487 +	for(i=0;i<argc-3;i++)
   1.488 +    {
   1.489 +    	fprintf(stdout,"%s ",argv[i+3]);
   1.490 +    }
   1.491 +   fprintf(stdout,"\n");
   1.492 +
   1.493 +}
   1.494 +
   1.495 +char** create_params(char *ip_str,int *argc)
   1.496 +{
   1.497 +   char *paramets[50];
   1.498 +   char **cmd_line=NULL;
   1.499 +   char *str=NULL,*ptr=NULL;
   1.500 +   int len=0,cnt=0,i=0;
   1.501 +
   1.502 +
   1.503 +
   1.504 +   str=ip_str;
   1.505 +
   1.506 +   while(str)
   1.507 +   {
   1.508 +      ptr=strchr(str,' ');
   1.509 +      if(ptr)
   1.510 +      {
   1.511 +        len = ptr-str;
   1.512 +        paramets[cnt]=(char *)malloc(sizeof(char)*len+1);
   1.513 +        if(paramets[cnt])
   1.514 +        {
   1.515 +          strncpy(paramets[cnt],str,len);
   1.516 +      	  paramets[cnt][len]='\0';
   1.517 +      	}
   1.518 +      	cnt++;
   1.519 +      	str+= len + 1;
   1.520 +      }
   1.521 +      else
   1.522 +      {
   1.523 +        len = strlen(str);
   1.524 +        paramets[cnt]=(char *)malloc(sizeof(char)*len+1);
   1.525 +        if(paramets[cnt])
   1.526 +       	strcpy(paramets[cnt],str);
   1.527 +        cnt++;
   1.528 +      	break;
   1.529 +      }
   1.530 +
   1.531 +   }
   1.532 +
   1.533 +   if(cnt)
   1.534 +   cmd_line=(char **)malloc(cnt*sizeof(char *));
   1.535 +   for(i=0;i<cnt;i++)
   1.536 +   cmd_line[i]=paramets[i];
   1.537 +   *argc=cnt;
   1.538 +   return cmd_line;
   1.539 +
   1.540 +
   1.541 +}
   1.542 +
   1.543 +
   1.544 +
   1.545 +int compare_files(const char *file1, const char *file2)
   1.546 +{
   1.547 +
   1.548 +   			FILE *fp1 = NULL;
   1.549 +			FILE *fp2 = NULL;
   1.550 +
   1.551 +				long file1_size;
   1.552 +				long file2_size;
   1.553 +
   1.554 +				char *file1_data = NULL;
   1.555 +				char *file2_data = NULL;
   1.556 +
   1.557 +				int ret;
   1.558 +				int argc=0;
   1.559 +   			char** argv = NULL;
   1.560 +
   1.561 +
   1.562 +				if(!(fp1 = fopen(file1,"r+"))){
   1.563 +					fprintf(stdout,"Faliure opening file %s.",file1);
   1.564 +					ret = -1;
   1.565 +					goto err;
   1.566 +				}
   1.567 +
   1.568 +				if(!(fp2 = fopen(file2,"r+"))){
   1.569 +					fprintf(stdout,"Faliure opening file %s.",file2);
   1.570 +					ret = -1;
   1.571 +					goto err;
   1.572 +				}
   1.573 +
   1.574 +				if((ret = fseek(fp1, 0, SEEK_END)) < 0)
   1.575 +					goto err;
   1.576 +
   1.577 +				if((ret = fseek(fp2, 0, SEEK_END)) < 0)
   1.578 +					goto err;
   1.579 +
   1.580 + 				if((file1_size = ftell(fp1)) < 0)
   1.581 +				{
   1.582 +					ret = file1_size;
   1.583 +					goto err;
   1.584 +				}
   1.585 +				if((file2_size = ftell(fp1)) < 0)
   1.586 +					{
   1.587 +						ret = file2_size;
   1.588 +						goto err;
   1.589 +					}
   1.590 +
   1.591 +				if(file1_size != file2_size)
   1.592 +				{
   1.593 +					fprintf(stdout,"File size of  %s is not same as that of %s.",file1,file2);
   1.594 +					ret = -1;
   1.595 +					goto err;
   1.596 +				}
   1.597 +
   1.598 +                if((ret = fseek(fp1, 0, SEEK_SET)) < 0)
   1.599 +					goto err;
   1.600 +
   1.601 +				if((ret = fseek(fp2, 0, SEEK_SET)) < 0)
   1.602 +					goto err;
   1.603 +
   1.604 +				file1_data = (char *)malloc(file1_size);
   1.605 +				file2_data = (char *)malloc(file2_size);
   1.606 +
   1.607 +				memset(file1_data,0,file1_size);
   1.608 +				memset(file2_data,0,file2_size);
   1.609 +
   1.610 +				ret = fread(file1_data,sizeof(char),file1_size,fp1);
   1.611 +				if(ret < file1_size)
   1.612 +				{
   1.613 +				    ret = ferror(fp1);
   1.614 +					fprintf(stdout,"Failed reading file %s.", file1);
   1.615 +					ret = -1;
   1.616 +					goto err1;
   1.617 +				}
   1.618 +
   1.619 +				ret = fread(file2_data,sizeof(char),file2_size,fp2);
   1.620 +				if(ret < file2_size)
   1.621 +				{
   1.622 +					fprintf(stdout,"Failed reading file %s.",file2);
   1.623 +					ret = -1;
   1.624 +					goto err2;
   1.625 +				}
   1.626 +
   1.627 +				if(memcmp(file1_data,file2_data,file1_size))
   1.628 +				{
   1.629 +
   1.630 +				   fprintf(stdout,"File file1 is not similar to file2.");
   1.631 +				   ret = -1;
   1.632 +				   goto err2;
   1.633 +				}
   1.634 +				else
   1.635 +				ret=0; //file are smae
   1.636 +
   1.637 +err2:
   1.638 +				fclose(fp2);
   1.639 +err1:
   1.640 +				fclose(fp1);
   1.641 +err:
   1.642 +
   1.643 +				if(file1_data)
   1.644 +					free(file1_data);
   1.645 +
   1.646 +				if(file2_data)
   1.647 +					free(file2_data);
   1.648 +
   1.649 +				return ret;
   1.650 +}
   1.651 +
   1.652 +void delete_file(char *file)
   1.653 +{
   1.654 +	unlink(file);
   1.655 +}
   1.656 +
   1.657 +void delete_params(char ** cmd_line,int argc)
   1.658 +{
   1.659 +	int i;
   1.660 +	for(i=0;i<argc;i++)
   1.661 +	if(cmd_line[i])	free(cmd_line[i]);
   1.662 +	if(cmd_line)
   1.663 +	free(cmd_line);
   1.664 +	cmd_line=NULL;
   1.665 +}
   1.666 +
   1.667 +void strip(char *str)
   1.668 +{
   1.669 +	int len=0;
   1.670 +	char *ptr=NULL;
   1.671 +	char *ptr1=NULL;
   1.672 +	len=strlen(str);
   1.673 +	ptr=strchr(str,'\r');
   1.674 +	ptr1=strchr(str,'\n');
   1.675 +	if((ptr) || (ptr1))
   1.676 +	{
   1.677 +		if(ptr)
   1.678 +		*ptr='\0'; //strip \r and \n
   1.679 +		else
   1.680 +		*ptr1='\0';
   1.681 +	}
   1.682 +	return;
   1.683 +}
   1.684 +
   1.685 +
   1.686 +int main(int argc_m, char *argv_m[])
   1.687 +{
   1.688 +
   1.689 +   int ret,argc=0;
   1.690 +   char argstr[200];
   1.691 +   int total_cases,cur_case=0,fail=0;
   1.692 +   char **argv=NULL;
   1.693 +   char openssltestnum[20];
   1.694 +   FILE *arg_fp;
   1.695 +
   1.696 +
   1.697 +  if(openssl_init())
   1.698 +  {
   1.699 +    return 1;
   1.700 +  }
   1.701 +
   1.702 +
   1.703 +
   1.704 +    if(argc_m > 1)
   1.705 +    	arg_fp = fopen(argv_m[1],"r+");
   1.706 +    else
   1.707 +    	return 1; // filename needed as argument.
   1.708 +
   1.709 +
   1.710 +    if(!arg_fp)
   1.711 +    {
   1.712 +      return 1;
   1.713 +    }
   1.714 +
   1.715 +    while(!feof(arg_fp)&&!fail)
   1.716 +    {
   1.717 +  	  memset(argstr,0,200);
   1.718 +      fgets(argstr, 200, arg_fp);
   1.719 +      strip(argstr);
   1.720 +      argv = create_params(argstr,&argc);
   1.721 +	  strcpy(openssltestnum,argv[1]);
   1.722 +
   1.723 +	  if(!strcmp(argv[2],"test_log"))
   1.724 +      {
   1.725 +        print_logs(argc,argv);
   1.726 +      }
   1.727 +      else if(!strcmp(argv[2],"compare"))
   1.728 +      {
   1.729 +        ret = compare_files(argv[3],argv[4]);
   1.730 +        if(ret)
   1.731 +        {
   1.732 +        testResultXml(openssltestnum, ret);
   1.733 +        fail=1;
   1.734 +        }
   1.735 +
   1.736 +      }
   1.737 +      else if(!strcmp(argv[2],"append"))
   1.738 +      {
   1.739 +        ret = append(argv[3],argv[4],argv[5]);
   1.740 +        if(ret)
   1.741 +        {
   1.742 +         testResultXml(openssltestnum, ret);
   1.743 +         fail=1;
   1.744 +        }
   1.745 +
   1.746 +      }
   1.747 +       else if(!strcmp(argv[2],"delete"))
   1.748 +      {
   1.749 +        delete_file(argv[3]);
   1.750 +
   1.751 +      }
   1.752 +      else
   1.753 +      {
   1.754 +        ret = openssl_main(argc-2,argv+2);
   1.755 +        if(ret)
   1.756 +        {
   1.757 +        testResultXml(openssltestnum, ret);
   1.758 +        fail=1;
   1.759 +        }
   1.760 +
   1.761 +      }
   1.762 +
   1.763 +     //delete_params(argv,argc);
   1.764 +   }//while
   1.765 +
   1.766 +   if(!fail&&!ret)
   1.767 +   testResultXml(openssltestnum, fail);
   1.768 +   fclose(arg_fp);
   1.769 +
   1.770 +
   1.771 + openssl_deinit();
   1.772 + return ret;
   1.773 +}
   1.774 +
   1.775 +#endif  /* IF_DEF SYMBIAN */
   1.776 +#ifndef SYMBIAN
   1.777 +int main(int Argc, char *Argv[])
   1.778 +#else
   1.779 +int openssl_main(int Argc, char *Argv[])
   1.780 +#endif
   1.781 +	{
   1.782 +	ARGS arg;
   1.783 +#define PROG_NAME_SIZE	39
   1.784 +	char pname[PROG_NAME_SIZE+1];
   1.785 +	FUNCTION f,*fp;
   1.786 +	MS_STATIC const char *prompt;
   1.787 +	MS_STATIC char buf[1024];
   1.788 +	char *to_free=NULL;
   1.789 +	int n,i,ret=0;
   1.790 +	int argc;
   1.791 +	char **argv,*p;
   1.792 +	LHASH *prog=NULL;
   1.793 +	long errline;
   1.794 +
   1.795 +	arg.data=NULL;
   1.796 +	arg.count=0;
   1.797 +
   1.798 +	if (bio_err == NULL)
   1.799 +		if ((bio_err=BIO_new(BIO_s_file())) != NULL)
   1.800 +#ifdef SYMBIAN
   1.801 +        BIO_set_fp(bio_err,fp_stderr,BIO_NOCLOSE|BIO_FP_TEXT);
   1.802 +#else
   1.803 +        BIO_set_fp(bio_err,stderr,BIO_NOCLOSE|BIO_FP_TEXT);
   1.804 +#endif
   1.805 +	if (getenv("OPENSSL_DEBUG_MEMORY") != NULL) /* if not defined, use compiled-in library defaults */
   1.806 +		{
   1.807 +		if (!(0 == strcmp(getenv("OPENSSL_DEBUG_MEMORY"), "off")))
   1.808 +			{
   1.809 +			CRYPTO_malloc_debug_init();
   1.810 +			CRYPTO_set_mem_debug_options(V_CRYPTO_MDEBUG_ALL);
   1.811 +			}
   1.812 +		else
   1.813 +			{
   1.814 +			/* OPENSSL_DEBUG_MEMORY=off */
   1.815 +			CRYPTO_set_mem_debug_functions(0, 0, 0, 0, 0);
   1.816 +			}
   1.817 +		}
   1.818 +	CRYPTO_mem_ctrl(CRYPTO_MEM_CHECK_ON);
   1.819 +
   1.820 +#if 0
   1.821 +	if (getenv("OPENSSL_DEBUG_LOCKING") != NULL)
   1.822 +#endif
   1.823 +		{
   1.824 +		CRYPTO_set_locking_callback(lock_dbg_cb);
   1.825 +		}
   1.826 +
   1.827 +	apps_startup();
   1.828 +
   1.829 +	/* Lets load up our environment a little */
   1.830 +	p=getenv("OPENSSL_CONF");
   1.831 +	if (p == NULL)
   1.832 +		p=getenv("SSLEAY_CONF");
   1.833 +	if (p == NULL)
   1.834 +		p=to_free=make_config_name();
   1.835 +
   1.836 +	default_config_file=p;
   1.837 +
   1.838 +	config=NCONF_new(NULL);
   1.839 +	i=NCONF_load(config,p,&errline);
   1.840 +	if (i == 0)
   1.841 +		{
   1.842 +		NCONF_free(config);
   1.843 +		config = NULL;
   1.844 +		ERR_clear_error();
   1.845 +		}
   1.846 +
   1.847 +	prog=prog_init();
   1.848 +
   1.849 +	/* first check the program name */
   1.850 +	program_name(Argv[0],pname,sizeof pname);
   1.851 +
   1.852 +	f.name=pname;
   1.853 +	fp=(FUNCTION *)lh_retrieve(prog,&f);
   1.854 +	if (fp != NULL)
   1.855 +		{
   1.856 +		Argv[0]=pname;
   1.857 +		ret=fp->func(Argc,Argv);
   1.858 +		goto end;
   1.859 +		}
   1.860 +
   1.861 +	/* ok, now check that there are not arguments, if there are,
   1.862 +	 * run with them, shifting the ssleay off the front */
   1.863 +	if (Argc != 1)
   1.864 +		{
   1.865 +		Argc--;
   1.866 +		Argv++;
   1.867 +		ret=do_cmd(prog,Argc,Argv);
   1.868 +		if (ret < 0) ret=0;
   1.869 +		goto end;
   1.870 +		}
   1.871 +
   1.872 +	/* ok, lets enter the old 'OpenSSL>' mode */
   1.873 +
   1.874 +	for (;;)
   1.875 +		{
   1.876 +		ret=0;
   1.877 +		p=buf;
   1.878 +		n=sizeof buf;
   1.879 +		i=0;
   1.880 +		for (;;)
   1.881 +			{
   1.882 +			p[0]='\0';
   1.883 +			if (i++)
   1.884 +				prompt=">";
   1.885 +			else	prompt="OpenSSL> ";
   1.886 +#ifndef SYMBIAN
   1.887 +			fputs(prompt,stdout);
   1.888 +			fflush(stdout);
   1.889 +			fgets(p,n,stdin);
   1.890 +#else
   1.891 +            fputs(prompt,stdout);
   1.892 +			fflush(stdout);
   1.893 +			fgets(p,n,stdin);
   1.894 +
   1.895 +#endif
   1.896 +			if (p[0] == '\0') goto end;
   1.897 +			i=strlen(p);
   1.898 +			if (i <= 1) break;
   1.899 +			if (p[i-2] != '\\') break;
   1.900 +			i-=2;
   1.901 +			p+=i;
   1.902 +			n-=i;
   1.903 +			}
   1.904 +		if (!chopup_args(&arg,buf,&argc,&argv)) break;
   1.905 +
   1.906 +		ret=do_cmd(prog,argc,argv);
   1.907 +		if (ret < 0)
   1.908 +			{
   1.909 +			ret=0;
   1.910 +			goto end;
   1.911 +			}
   1.912 +		if (ret != 0)
   1.913 +			BIO_printf(bio_err,"error in %s\n",argv[0]);
   1.914 +		(void)BIO_flush(bio_err);
   1.915 +		}
   1.916 +	BIO_printf(bio_err,"bad exit\n");
   1.917 +	ret=1;
   1.918 +end:
   1.919 +	if (to_free)
   1.920 +		OPENSSL_free(to_free);
   1.921 +	if (config != NULL)
   1.922 +		{
   1.923 +		NCONF_free(config);
   1.924 +		config=NULL;
   1.925 +		}
   1.926 +	if (prog != NULL) lh_free(prog);
   1.927 +	if (arg.data != NULL) OPENSSL_free(arg.data);
   1.928 +
   1.929 +	apps_shutdown();
   1.930 +
   1.931 +	CRYPTO_mem_leaks(bio_err);
   1.932 +	if (bio_err != NULL)
   1.933 +		{
   1.934 +		BIO_free(bio_err);
   1.935 +		bio_err=NULL;
   1.936 +		}
   1.937 +
   1.938 +	return ret;
   1.939 +//	OPENSSL_EXIT(ret);
   1.940 +	}
   1.941 +
   1.942 +#define LIST_STANDARD_COMMANDS "list-standard-commands"
   1.943 +#define LIST_MESSAGE_DIGEST_COMMANDS "list-message-digest-commands"
   1.944 +#define LIST_CIPHER_COMMANDS "list-cipher-commands"
   1.945 +
   1.946 +static int do_cmd(LHASH *prog, int argc, char *argv[])
   1.947 +	{
   1.948 +	FUNCTION f,*fp;
   1.949 +	int i,ret=1,tp,nl;
   1.950 +
   1.951 +	if ((argc <= 0) || (argv[0] == NULL))
   1.952 +		{ ret=0; goto end; }
   1.953 +	f.name=argv[0];
   1.954 +	fp=(FUNCTION *)lh_retrieve(prog,&f);
   1.955 +	if (fp != NULL)
   1.956 +		{
   1.957 +		ret=fp->func(argc,argv);
   1.958 +		}
   1.959 +	else if ((strncmp(argv[0],"no-",3)) == 0)
   1.960 +		{
   1.961 +
   1.962 +		BIO *bio_stdout = BIO_new_fp(stdout,BIO_NOCLOSE);
   1.963 +#ifdef OPENSSL_SYS_VMS
   1.964 +		{
   1.965 +		BIO *tmpbio = BIO_new(BIO_f_linebuffer());
   1.966 +		bio_stdout = BIO_push(tmpbio, bio_stdout);
   1.967 +		}
   1.968 +#endif
   1.969 +		f.name=argv[0]+3;
   1.970 +		ret = (lh_retrieve(prog,&f) != NULL);
   1.971 +		if (!ret)
   1.972 +			BIO_printf(bio_stdout, "%s\n", argv[0]);
   1.973 +		else
   1.974 +			BIO_printf(bio_stdout, "%s\n", argv[0]+3);
   1.975 +		BIO_free_all(bio_stdout);
   1.976 +		goto end;
   1.977 +		}
   1.978 +	else if ((strcmp(argv[0],"quit") == 0) ||
   1.979 +		(strcmp(argv[0],"q") == 0) ||
   1.980 +		(strcmp(argv[0],"exit") == 0) ||
   1.981 +		(strcmp(argv[0],"bye") == 0))
   1.982 +		{
   1.983 +		ret= -1;
   1.984 +		goto end;
   1.985 +		}
   1.986 +	else if ((strcmp(argv[0],LIST_STANDARD_COMMANDS) == 0) ||
   1.987 +		(strcmp(argv[0],LIST_MESSAGE_DIGEST_COMMANDS) == 0) ||
   1.988 +		(strcmp(argv[0],LIST_CIPHER_COMMANDS) == 0))
   1.989 +		{
   1.990 +		int list_type;
   1.991 +		BIO *bio_stdout;
   1.992 +
   1.993 +		if (strcmp(argv[0],LIST_STANDARD_COMMANDS) == 0)
   1.994 +			list_type = FUNC_TYPE_GENERAL;
   1.995 +		else if (strcmp(argv[0],LIST_MESSAGE_DIGEST_COMMANDS) == 0)
   1.996 +			list_type = FUNC_TYPE_MD;
   1.997 +		else /* strcmp(argv[0],LIST_CIPHER_COMMANDS) == 0 */
   1.998 +			list_type = FUNC_TYPE_CIPHER;
   1.999 +
  1.1000 +		bio_stdout = BIO_new_fp(stdout,BIO_NOCLOSE);
  1.1001 +#ifdef OPENSSL_SYS_VMS
  1.1002 +		{
  1.1003 +		BIO *tmpbio = BIO_new(BIO_f_linebuffer());
  1.1004 +		bio_stdout = BIO_push(tmpbio, bio_stdout);
  1.1005 +		}
  1.1006 +#endif
  1.1007 +
  1.1008 +		for (fp=functions; fp->name != NULL; fp++)
  1.1009 +			if (fp->type == list_type)
  1.1010 +				BIO_printf(bio_stdout, "%s\n", fp->name);
  1.1011 +		BIO_free_all(bio_stdout);
  1.1012 +		ret=0;
  1.1013 +		goto end;
  1.1014 +		}
  1.1015 +	else
  1.1016 +		{
  1.1017 +		BIO_printf(bio_err,"openssl:Error: '%s' is an invalid command.\n",
  1.1018 +			argv[0]);
  1.1019 +		BIO_printf(bio_err, "\nStandard commands");
  1.1020 +		i=0;
  1.1021 +		tp=0;
  1.1022 +		for (fp=functions; fp->name != NULL; fp++)
  1.1023 +			{
  1.1024 +			nl=0;
  1.1025 +			if (((i++) % 5) == 0)
  1.1026 +				{
  1.1027 +				BIO_printf(bio_err,"\n");
  1.1028 +				nl=1;
  1.1029 +				}
  1.1030 +			if (fp->type != tp)
  1.1031 +				{
  1.1032 +				tp=fp->type;
  1.1033 +				if (!nl) BIO_printf(bio_err,"\n");
  1.1034 +				if (tp == FUNC_TYPE_MD)
  1.1035 +					{
  1.1036 +					i=1;
  1.1037 +					BIO_printf(bio_err,
  1.1038 +						"\nMessage Digest commands (see the `dgst' command for more details)\n");
  1.1039 +					}
  1.1040 +				else if (tp == FUNC_TYPE_CIPHER)
  1.1041 +					{
  1.1042 +					i=1;
  1.1043 +					BIO_printf(bio_err,"\nCipher commands (see the `enc' command for more details)\n");
  1.1044 +					}
  1.1045 +				}
  1.1046 +			BIO_printf(bio_err,"%-15s",fp->name);
  1.1047 +			}
  1.1048 +		BIO_printf(bio_err,"\n\n");
  1.1049 +		ret=0;
  1.1050 +		}
  1.1051 +end:
  1.1052 +	return(ret);
  1.1053 +	}
  1.1054 +
  1.1055 +static int SortFnByName(const void *_f1,const void *_f2)
  1.1056 +    {
  1.1057 +    const FUNCTION *f1=_f1;
  1.1058 +    const FUNCTION *f2=_f2;
  1.1059 +
  1.1060 +    if(f1->type != f2->type)
  1.1061 +	return f1->type-f2->type;
  1.1062 +    return strcmp(f1->name,f2->name);
  1.1063 +    }
  1.1064 +
  1.1065 +static LHASH *prog_init(void)
  1.1066 +	{
  1.1067 +	LHASH *ret;
  1.1068 +	FUNCTION *f;
  1.1069 +	size_t i;
  1.1070 +
  1.1071 +	/* Purely so it looks nice when the user hits ? */
  1.1072 +	for(i=0,f=functions ; f->name != NULL ; ++f,++i)
  1.1073 +	    ;
  1.1074 +	qsort(functions,i,sizeof *functions,SortFnByName);
  1.1075 +
  1.1076 +	if ((ret=lh_new(hash, cmp)) == NULL)
  1.1077 +		return(NULL);
  1.1078 +
  1.1079 +	for (f=functions; f->name != NULL; f++)
  1.1080 +		lh_insert(ret,f);
  1.1081 +	return(ret);
  1.1082 +	}
  1.1083 +
  1.1084 +/* static int MS_CALLBACK cmp(FUNCTION *a, FUNCTION *b) */
  1.1085 +static int MS_CALLBACK cmp(const void *a_void, const void *b_void)
  1.1086 +	{
  1.1087 +	return(strncmp(((const FUNCTION *)a_void)->name,
  1.1088 +			((const FUNCTION *)b_void)->name,8));
  1.1089 +	}
  1.1090 +
  1.1091 +/* static unsigned long MS_CALLBACK hash(FUNCTION *a) */
  1.1092 +static unsigned long MS_CALLBACK hash(const void *a_void)
  1.1093 +	{
  1.1094 +	return(lh_strhash(((const FUNCTION *)a_void)->name));
  1.1095 +	}