ssl/tsrc/topenssl/src/openssl.c
changeset 31 ce057bb09d0b
child 45 4b03adbd26ca
equal deleted inserted replaced
30:e20de85af2ee 31:ce057bb09d0b
       
     1 /* apps/openssl.c */
       
     2 /* Copyright (C) 1995-1998 Eric Young (eay@cryptsoft.com)
       
     3  * All rights reserved.
       
     4  *
       
     5  * This package is an SSL implementation written
       
     6  * by Eric Young (eay@cryptsoft.com).
       
     7  * The implementation was written so as to conform with Netscapes SSL.
       
     8  * 
       
     9  * This library is free for commercial and non-commercial use as long as
       
    10  * the following conditions are aheared to.  The following conditions
       
    11  * apply to all code found in this distribution, be it the RC4, RSA,
       
    12  * lhash, DES, etc., code; not just the SSL code.  The SSL documentation
       
    13  * included with this distribution is covered by the same copyright terms
       
    14  * except that the holder is Tim Hudson (tjh@cryptsoft.com).
       
    15  * 
       
    16  * Copyright remains Eric Young's, and as such any Copyright notices in
       
    17  * the code are not to be removed.
       
    18  * If this package is used in a product, Eric Young should be given attribution
       
    19  * as the author of the parts of the library used.
       
    20  * This can be in the form of a textual message at program startup or
       
    21  * in documentation (online or textual) provided with the package.
       
    22  * 
       
    23  * Redistribution and use in source and binary forms, with or without
       
    24  * modification, are permitted provided that the following conditions
       
    25  * are met:
       
    26  * 1. Redistributions of source code must retain the copyright
       
    27  *    notice, this list of conditions and the following disclaimer.
       
    28  * 2. Redistributions in binary form must reproduce the above copyright
       
    29  *    notice, this list of conditions and the following disclaimer in the
       
    30  *    documentation and/or other materials provided with the distribution.
       
    31  * 3. All advertising materials mentioning features or use of this software
       
    32  *    must display the following acknowledgement:
       
    33  *    "This product includes cryptographic software written by
       
    34  *     Eric Young (eay@cryptsoft.com)"
       
    35  *    The word 'cryptographic' can be left out if the rouines from the library
       
    36  *    being used are not cryptographic related :-).
       
    37  * 4. If you include any Windows specific code (or a derivative thereof) from 
       
    38  *    the apps directory (application code) you must include an acknowledgement:
       
    39  *    "This product includes software written by Tim Hudson (tjh@cryptsoft.com)"
       
    40  * 
       
    41  * THIS SOFTWARE IS PROVIDED BY ERIC YOUNG ``AS IS'' AND
       
    42  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
       
    43  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
       
    44  * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
       
    45  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
       
    46  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
       
    47  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
       
    48  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
       
    49  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
       
    50  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
       
    51  * SUCH DAMAGE.
       
    52  * 
       
    53  * The licence and distribution terms for any publically available version or
       
    54  * derivative of this code cannot be changed.  i.e. this code cannot simply be
       
    55  * copied and put under another distribution licence
       
    56  * [including the GNU Public Licence.]
       
    57  */
       
    58 /* ====================================================================
       
    59  * Copyright (c) 1998-2001 The OpenSSL Project.  All rights reserved.
       
    60  *
       
    61  * Redistribution and use in source and binary forms, with or without
       
    62  * modification, are permitted provided that the following conditions
       
    63  * are met:
       
    64  *
       
    65  * 1. Redistributions of source code must retain the above copyright
       
    66  *    notice, this list of conditions and the following disclaimer. 
       
    67  *
       
    68  * 2. Redistributions in binary form must reproduce the above copyright
       
    69  *    notice, this list of conditions and the following disclaimer in
       
    70  *    the documentation and/or other materials provided with the
       
    71  *    distribution.
       
    72  *
       
    73  * 3. All advertising materials mentioning features or use of this
       
    74  *    software must display the following acknowledgment:
       
    75  *    "This product includes software developed by the OpenSSL Project
       
    76  *    for use in the OpenSSL Toolkit. (http://www.openssl.org/)"
       
    77  *
       
    78  * 4. The names "OpenSSL Toolkit" and "OpenSSL Project" must not be used to
       
    79  *    endorse or promote products derived from this software without
       
    80  *    prior written permission. For written permission, please contact
       
    81  *    openssl-core@openssl.org.
       
    82  *
       
    83  * 5. Products derived from this software may not be called "OpenSSL"
       
    84  *    nor may "OpenSSL" appear in their names without prior written
       
    85  *    permission of the OpenSSL Project.
       
    86  *
       
    87  * 6. Redistributions of any form whatsoever must retain the following
       
    88  *    acknowledgment:
       
    89  *    "This product includes software developed by the OpenSSL Project
       
    90  *    for use in the OpenSSL Toolkit (http://www.openssl.org/)"
       
    91  *
       
    92  * THIS SOFTWARE IS PROVIDED BY THE OpenSSL PROJECT ``AS IS'' AND ANY
       
    93  * EXPRESSED OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
       
    94  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
       
    95  * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE OpenSSL PROJECT OR
       
    96  * ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
       
    97  * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
       
    98  * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
       
    99  * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
       
   100  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
       
   101  * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
       
   102  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED
       
   103  * OF THE POSSIBILITY OF SUCH DAMAGE.
       
   104  * ====================================================================
       
   105  *
       
   106  * This product includes cryptographic software written by Eric Young
       
   107  * (eay@cryptsoft.com).  This product includes software written by Tim
       
   108  * Hudson (tjh@cryptsoft.com).
       
   109  *
       
   110  */
       
   111 
       
   112 
       
   113 #include <stdio.h>
       
   114 #include <string.h>
       
   115 #include <stdlib.h>
       
   116 #include <dirent.h>
       
   117 #define OPENSSL_C /* tells apps.h to use complete apps_startup() */
       
   118 #include "apps.h"
       
   119 #include <openssl/bio.h>
       
   120 #include <openssl/crypto.h>
       
   121 #include <openssl/lhash.h>
       
   122 #include <openssl/conf.h>
       
   123 #include <openssl/x509.h>
       
   124 #include <openssl/pem.h>
       
   125 #include <openssl/ssl.h>
       
   126 #include <sys/stat.h>
       
   127 #ifndef OPENSSL_NO_ENGINE
       
   128 #include <openssl/engine.h>
       
   129 #endif
       
   130 #define USE_SOCKETS /* needed for the _O_BINARY defs in the MS world */
       
   131 #include "progs.h"
       
   132 #include "s_apps.h"
       
   133 #include <openssl/err.h>
       
   134 #ifdef SYMBIAN
       
   135 #include "topenssl.h"
       
   136 #endif
       
   137 
       
   138 /* The LHASH callbacks ("hash" & "cmp") have been replaced by functions with the
       
   139  * base prototypes (we cast each variable inside the function to the required
       
   140  * type of "FUNCTION*"). This removes the necessity for macro-generated wrapper
       
   141  * functions. */
       
   142 
       
   143 /* static unsigned long MS_CALLBACK hash(FUNCTION *a); */
       
   144 static unsigned long MS_CALLBACK hash(const void *a_void);
       
   145 /* static int MS_CALLBACK cmp(FUNCTION *a,FUNCTION *b); */
       
   146 static int MS_CALLBACK cmp(const void *a_void,const void *b_void);
       
   147 static LHASH *prog_init(void );
       
   148 static int do_cmd(LHASH *prog,int argc,char *argv[]);
       
   149 char *default_config_file=NULL;
       
   150 
       
   151 /* Make sure there is only one when MONOLITH is defined */
       
   152 #ifdef MONOLITH
       
   153 CONF *config=NULL;
       
   154 BIO *bio_err=NULL;
       
   155 #endif
       
   156 
       
   157 #ifdef SYMBIAN
       
   158 FILE *fp_stdout;
       
   159 FILE *fp_stderr;
       
   160 FILE *fp_stdin;
       
   161 #endif
       
   162 
       
   163 
       
   164 static void lock_dbg_cb(int mode, int type, const char *file, int line)
       
   165 	{
       
   166 	static int modes[CRYPTO_NUM_LOCKS]; /* = {0, 0, ... } */
       
   167 	const char *errstr = NULL;
       
   168 	int rw;
       
   169 	
       
   170 	rw = mode & (CRYPTO_READ|CRYPTO_WRITE);
       
   171 	if (!((rw == CRYPTO_READ) || (rw == CRYPTO_WRITE)))
       
   172 		{
       
   173 		errstr = "invalid mode";
       
   174 		goto err;
       
   175 		}
       
   176 
       
   177 	if (type < 0 || type >= CRYPTO_NUM_LOCKS)
       
   178 		{
       
   179 		errstr = "type out of bounds";
       
   180 		goto err;
       
   181 		}
       
   182 
       
   183 	if (mode & CRYPTO_LOCK)
       
   184 		{
       
   185 		if (modes[type])
       
   186 			{
       
   187 			errstr = "already locked";
       
   188 			/* must not happen in a single-threaded program
       
   189 			 * (would deadlock) */
       
   190 			goto err;
       
   191 			}
       
   192 
       
   193 		modes[type] = rw;
       
   194 		}
       
   195 	else if (mode & CRYPTO_UNLOCK)
       
   196 		{
       
   197 		if (!modes[type])
       
   198 			{
       
   199 			errstr = "not locked";
       
   200 			goto err;
       
   201 			}
       
   202 		
       
   203 		if (modes[type] != rw)
       
   204 			{
       
   205 			errstr = (rw == CRYPTO_READ) ?
       
   206 				"CRYPTO_r_unlock on write lock" :
       
   207 				"CRYPTO_w_unlock on read lock";
       
   208 			}
       
   209 
       
   210 		modes[type] = 0;
       
   211 		}
       
   212 	else
       
   213 		{
       
   214 		errstr = "invalid mode";
       
   215 		goto err;
       
   216 		}
       
   217 
       
   218  err:
       
   219 	if (errstr)
       
   220 		{
       
   221 		/* we cannot use bio_err here */
       
   222 #ifndef SYMBIAN		
       
   223 		fprintf(stderr, "openssl (lock_dbg_cb): %s (mode=%d, type=%d) at %s:%d\n",
       
   224 			errstr, mode, type, file, line);
       
   225 #else
       
   226         fprintf(fp_stderr, "openssl (lock_dbg_cb): %s (mode=%d, type=%d) at %s:%d\n",
       
   227 			errstr, mode, type, file, line);
       
   228         fclose(fp_stderr)			;
       
   229 #endif			
       
   230 		}
       
   231 	}
       
   232 
       
   233 
       
   234 
       
   235 #ifdef SYMBIAN
       
   236 void testResultXml(char *filename,int retval)
       
   237 {
       
   238 	char time_buf[50];
       
   239 	
       
   240 	char result[10];
       
   241 	
       
   242 	char xmlfilename[256];
       
   243 		
       
   244 	time_t t = time(NULL);
       
   245 	
       
   246 	struct tm *tm1 = localtime(&t);
       
   247 	
       
   248 	char *atsinitmsg 	= 	"<test-report>\n\t<test-batch>";
       
   249 	
       
   250 	char *atsbatchinit1	=   \
       
   251 							"\n\t\t<batch-init>\
       
   252 							\n\t\t\t<description></description>\
       
   253 							\n\t\t\t<date>";						
       
   254 							
       
   255 	char *atsbatchinit2 =	"</date>\
       
   256 							\n\t\t\t<factory>NA</factory>\
       
   257 							\n\t\t\t<component>\
       
   258 							\n\t\t\t\t<name>NA</name>\
       
   259 							\n\t\t\t\t<version>NA</version>\
       
   260 							\n\t\t\t</component>\
       
   261 							\n\t\t</batch-init>";
       
   262 							
       
   263 	char *atsbatchresult=	\
       
   264 							"\n\t\t<batch-result>\
       
   265 							\n\t\t\t<run-time>00:00:00</run-time>\
       
   266 							\n\t\t</batch-result>";
       
   267 							
       
   268 	char *atsclosemsg	=   \
       
   269 							"\n\t</test-batch>\
       
   270 							\n</test-report>\n ";
       
   271 
       
   272 	char *atstestinit	=	"\n\t\t<test-case time-stamp=\"00:00:00\">";
       
   273 
       
   274 	
       
   275 	char *atscaseinit1	=	\
       
   276 							"\n\t\t\t<case-init>\
       
   277 							\n\t\t\t\t<version></version>\
       
   278 							\n\t\t\t\t<id>";
       
   279 												
       
   280 	char *atscaseinit2 = 	"</id>\
       
   281 							\n\t\t\t\t<expected-result description=\"\">0</expected-result>\
       
   282 							\n\t\t\t</case-init>";
       
   283 							
       
   284 	char *atscaseresult1=	\
       
   285 							"\n\t\t\t<case-result status=\"";
       
   286 							
       
   287 	char *atscaseresult2=	"\">\
       
   288 							\n\t\t\t\t<actual-result>0</actual-result>\
       
   289 							\n\t\t\t\t<run-time>00:00:00</run-time>\
       
   290 							\n\t\t\t</case-result>";
       
   291 
       
   292 	char *atstestclose	=	"\n\t\t</test-case>";
       
   293 	
       
   294 	DIR *dir;
       
   295 	FILE *fp;
       
   296 	
       
   297 	dir = opendir("c:\\spd_logs");
       
   298 	if(!dir)
       
   299 		mkdir("c:\\spd_logs",0777);
       
   300 	
       
   301 	dir = opendir("c:\\spd_logs\\xml");
       
   302 	if(!dir)
       
   303 		mkdir("c:\\spd_logs\\xml",0777);
       
   304 	
       
   305 	// create the xml file name
       
   306 	strcpy(xmlfilename,"c:/spd_logs/xml/");
       
   307 	strcat(xmlfilename,filename);
       
   308 	strcat(xmlfilename,".xml");
       
   309 	
       
   310 	strftime(time_buf,50,"%c",tm1);
       
   311 
       
   312 	if(retval)
       
   313 		strcpy(result,"FAILED");
       
   314 	else
       
   315 		strcpy(result,"PASSED");
       
   316 	
       
   317 	fp = fopen(xmlfilename,"w");
       
   318 	
       
   319 	if(fp)
       
   320 	{
       
   321 		fprintf(fp,"%s%s%s%s%s%s%s%s%s%s%s%s%s%s",atsinitmsg,atsbatchinit1,time_buf,atsbatchinit2,atstestinit,
       
   322 			atscaseinit1,filename,atscaseinit2,atscaseresult1,result,atscaseresult2,
       
   323 			atstestclose,atsbatchresult,atsclosemsg);
       
   324 			
       
   325 		fclose(fp);	
       
   326 	}
       
   327 }
       
   328 
       
   329 
       
   330 int openssl_init(void)
       
   331 {
       
   332 	
       
   333 	fp_stdout = freopen(LOG_STDOUT,"w+",stdout);
       
   334 	if(!fp_stdout)
       
   335 	return 1;
       
   336 	fp_stderr = freopen(LOG_STDERR,"w+",stderr);
       
   337 	if(!fp_stderr)
       
   338 	return 1;
       
   339 	fp_stdin = freopen(LOG_STDIN,"w+",stdin);
       
   340 	if(!fp_stdin)
       
   341 	return 1;
       
   342 	return 0;
       
   343 }
       
   344 
       
   345 void openssl_deinit(void)
       
   346 {
       
   347     fclose(fp_stdout);
       
   348     fclose(fp_stderr);
       
   349     fclose(fp_stdin);
       
   350     fp_stdout=NULL;
       
   351     fp_stdin=NULL;
       
   352     fp_stderr=NULL;
       
   353     
       
   354 }
       
   355 
       
   356 int append(char *file1,char *file2, char *dst_file )
       
   357 {
       
   358    FILE *fp1 = NULL;
       
   359    FILE *fp2 = NULL;
       
   360    FILE *fp3 = NULL;
       
   361 				
       
   362    long file1_size;
       
   363    long file2_size;
       
   364    char *file1_data = NULL;
       
   365    char *file2_data = NULL;
       
   366 	
       
   367    int ret;				
       
   368 	
       
   369    if(!(fp1 = fopen(file1,"r+")))
       
   370    {
       
   371 	 fprintf(stdout,"Faliure opening file %s.",file1);
       
   372      ret = -1;
       
   373 	 goto err;
       
   374    }
       
   375 					
       
   376    if(!(fp2 = fopen(file2,"r+")))
       
   377    {
       
   378 	 fprintf(stdout,"Faliure opening file %s.",file2);
       
   379 	 ret = -1;
       
   380 	 goto err;
       
   381    }
       
   382  
       
   383    if(!(fp3= fopen(dst_file,"w+")))
       
   384    {
       
   385 	 fprintf(stdout,"Faliure opening file %s.",dst_file);
       
   386 	 ret = -1;
       
   387 	 goto err;
       
   388    }
       
   389 
       
   390    if((ret = fseek(fp1, 0, SEEK_END)) < 0)
       
   391    {
       
   392    	  ret = -1;
       
   393 	  goto err;
       
   394    }
       
   395    
       
   396 			
       
   397    if((ret = fseek(fp2, 0, SEEK_END)) < 0)
       
   398    {
       
   399    	 ret = -1;
       
   400 	 goto err;
       
   401    }
       
   402    
       
   403 			
       
   404    if((file1_size = ftell(fp1)) < 0)
       
   405    {	
       
   406    	 ret = -1;
       
   407 	 goto err;
       
   408    }
       
   409 	
       
   410    if((file2_size = ftell(fp2)) < 0)
       
   411    {
       
   412      ret = -1;
       
   413 	 goto err;
       
   414    }
       
   415 									
       
   416    if((ret = fseek(fp1, 0, SEEK_SET)) < 0)
       
   417    {
       
   418    	 ret = -1;
       
   419 	 goto err;
       
   420    }
       
   421 				
       
   422    if((ret = fseek(fp2, 0, SEEK_SET)) < 0)
       
   423    {
       
   424    	 ret = -1;
       
   425 	 goto err;
       
   426    }
       
   427    				
       
   428    file1_data = (char *)malloc(file1_size);
       
   429    file2_data = (char *)malloc(file2_size);
       
   430 				
       
   431    if(!file1_data||!file2_data)
       
   432    {
       
   433    	 ret = -1;
       
   434 	 goto err;
       
   435    }
       
   436    memset(file1_data,0,file1_size);
       
   437    
       
   438    memset(file2_data,0,file2_size);
       
   439 				
       
   440    ret = fread(file1_data,sizeof(char),file1_size,fp1);
       
   441    if(ret < file1_size)
       
   442    {
       
   443 	fprintf(stdout,"Failed reading file %s.", file1);
       
   444 	ret = -1;
       
   445     goto err;
       
   446    }
       
   447 
       
   448   ret = fread(file2_data,sizeof(char),file2_size,fp2);
       
   449   if(ret < file2_size)
       
   450   {
       
   451 	fprintf(stdout,"Failed reading file %s.",file2);
       
   452 	ret = -1;
       
   453 	goto err;
       
   454   }
       
   455   
       
   456   ret = fwrite(file1_data,sizeof(char),file1_size,fp3);
       
   457   if(ret < file1_size)
       
   458   {
       
   459 	fprintf(stdout,"Failed wrting from %s file to file %s.",file1,dst_file);
       
   460 	ret = -1;
       
   461 	goto err;
       
   462   }
       
   463   ret = fwrite(file2_data,sizeof(char),file2_size,fp3);
       
   464   if(ret < file2_size)
       
   465   {
       
   466 	fprintf(stdout,"Failed wrting from %s file to file %s.",file2,dst_file);
       
   467 	ret = -1;
       
   468 	goto err;
       
   469   }
       
   470   ret=0;
       
   471 
       
   472 err:  if(file1_data)
       
   473   free(file1_data);
       
   474   if(file2_data)
       
   475   free(file2_data);				
       
   476   fclose(fp1);
       
   477   fclose(fp2);
       
   478   fclose(fp3);
       
   479   return ret;
       
   480  				
       
   481 }
       
   482 void print_logs(int argc, char *argv[])
       
   483 {
       
   484 	int i=0;
       
   485 	for(i=0;i<argc-3;i++)
       
   486     {
       
   487     	fprintf(stdout,"%s ",argv[i+3]);
       
   488     }
       
   489    fprintf(stdout,"\n");
       
   490 	
       
   491 }
       
   492 
       
   493 char** create_params(char *ip_str,int *argc)
       
   494 {
       
   495    char *paramets[50];
       
   496    char **cmd_line=NULL;
       
   497    char *str=NULL,*ptr=NULL;
       
   498    int len=0,cnt=0,i=0;
       
   499   
       
   500   
       
   501    
       
   502    str=ip_str;
       
   503     
       
   504    while(str)
       
   505    {
       
   506       ptr=strchr(str,' ');
       
   507       if(ptr)
       
   508       {
       
   509         len = ptr-str;
       
   510         paramets[cnt]=(char *)malloc(sizeof(char)*len+1);
       
   511         if(paramets[cnt])
       
   512         {
       
   513           strncpy(paramets[cnt],str,len);
       
   514       	  paramets[cnt][len]='\0';
       
   515       	}
       
   516       	cnt++;
       
   517       	str+= len + 1;
       
   518       }
       
   519       else
       
   520       {
       
   521         len = strlen(str);
       
   522         paramets[cnt]=(char *)malloc(sizeof(char)*len+1);
       
   523         if(paramets[cnt])
       
   524        	strcpy(paramets[cnt],str);
       
   525         cnt++;
       
   526       	break;
       
   527       }
       
   528    	
       
   529    }
       
   530    
       
   531    if(cnt)
       
   532    cmd_line=(char **)malloc(cnt*sizeof(char *));
       
   533    for(i=0;i<cnt;i++) 
       
   534    cmd_line[i]=paramets[i];
       
   535    *argc=cnt;
       
   536    return cmd_line;
       
   537  
       
   538  	
       
   539 }
       
   540 
       
   541 
       
   542 
       
   543 int compare_files(const char *file1, const char *file2)
       
   544 {
       
   545 			
       
   546    			FILE *fp1 = NULL;
       
   547 			FILE *fp2 = NULL;
       
   548 				
       
   549 				long file1_size;
       
   550 				long file2_size;
       
   551 				
       
   552 				char *file1_data = NULL;
       
   553 				char *file2_data = NULL;
       
   554 				
       
   555 				int ret;				
       
   556 				int argc=0;
       
   557    			char** argv = NULL;
       
   558    		
       
   559    			
       
   560 				if(!(fp1 = fopen(file1,"r+"))){
       
   561 					fprintf(stdout,"Faliure opening file %s.",file1);
       
   562 					ret = -1;
       
   563 					goto err;
       
   564 				}
       
   565 					
       
   566 				if(!(fp2 = fopen(file2,"r+"))){
       
   567 					fprintf(stdout,"Faliure opening file %s.",file2);
       
   568 					ret = -1;
       
   569 					goto err;
       
   570 				}
       
   571 				
       
   572 				if((ret = fseek(fp1, 0, SEEK_END)) < 0)
       
   573 					goto err;
       
   574 				
       
   575 				if((ret = fseek(fp2, 0, SEEK_END)) < 0)
       
   576 					goto err;
       
   577 								
       
   578  				if((file1_size = ftell(fp1)) < 0)
       
   579 				{	
       
   580 					ret = file1_size;
       
   581 					goto err;
       
   582 				}
       
   583 				if((file2_size = ftell(fp1)) < 0)
       
   584 					{
       
   585 						ret = file2_size;
       
   586 						goto err;
       
   587 					}
       
   588 									
       
   589 				if(file1_size != file2_size)
       
   590 				{
       
   591 					fprintf(stdout,"File size of  %s is not same as that of %s.",file1,file2);
       
   592 					ret = -1;
       
   593 					goto err;
       
   594 				}
       
   595 							
       
   596                 if((ret = fseek(fp1, 0, SEEK_SET)) < 0)
       
   597 					goto err;
       
   598 				
       
   599 				if((ret = fseek(fp2, 0, SEEK_SET)) < 0)
       
   600 					goto err;							
       
   601 				
       
   602 				file1_data = (char *)malloc(file1_size);
       
   603 				file2_data = (char *)malloc(file2_size);
       
   604 				
       
   605 				memset(file1_data,0,file1_size);
       
   606 				memset(file2_data,0,file2_size);
       
   607 				
       
   608 				ret = fread(file1_data,sizeof(char),file1_size,fp1);
       
   609 				if(ret < file1_size)
       
   610 				{
       
   611 				    ret = ferror(fp1);
       
   612 					fprintf(stdout,"Failed reading file %s.", file1);
       
   613 					ret = -1;
       
   614 					goto err1;
       
   615 				}
       
   616 				
       
   617 				ret = fread(file2_data,sizeof(char),file2_size,fp2);
       
   618 				if(ret < file2_size)
       
   619 				{
       
   620 					fprintf(stdout,"Failed reading file %s.",file2);
       
   621 					ret = -1;
       
   622 					goto err2;
       
   623 				}
       
   624 				
       
   625 				if(memcmp(file1_data,file2_data,file1_size))
       
   626 				{
       
   627 						
       
   628 				   fprintf(stdout,"File file1 is not similar to file2.");
       
   629 				   ret = -1;
       
   630 				   goto err2;
       
   631 				}
       
   632 				else
       
   633 				ret=0; //file are smae
       
   634 		
       
   635 err2:		
       
   636 				fclose(fp2);
       
   637 err1:
       
   638 				fclose(fp1);
       
   639 err:
       
   640 
       
   641 				if(file1_data)
       
   642 					free(file1_data);
       
   643 					
       
   644 				if(file2_data)
       
   645 					free(file2_data);
       
   646 					
       
   647 				return ret;
       
   648 }
       
   649 
       
   650 void delete_file(char *file)
       
   651 {
       
   652 	unlink(file);
       
   653 }
       
   654 
       
   655 void delete_params(char ** cmd_line,int argc)
       
   656 {
       
   657 	int i;
       
   658 	for(i=0;i<argc;i++) 
       
   659 	if(cmd_line[i])	free(cmd_line[i]);
       
   660 	if(cmd_line)
       
   661 	free(cmd_line);
       
   662 	cmd_line=NULL;
       
   663 }
       
   664 
       
   665 void strip(char *str)
       
   666 {
       
   667 	int len=0;
       
   668 	char *ptr=NULL;
       
   669 	char *ptr1=NULL;
       
   670 	len=strlen(str);
       
   671 	ptr=strchr(str,'\r');
       
   672 	ptr1=strchr(str,'\n');
       
   673 	if((ptr) || (ptr1))
       
   674 	{
       
   675 		if(ptr)
       
   676 		*ptr='\0'; //strip \r and \n	
       
   677 		else
       
   678 		*ptr1='\0';
       
   679 	}
       
   680 	return;
       
   681 }
       
   682 
       
   683 
       
   684 int main(int argc_m, char *argv_m[])
       
   685 {
       
   686 	
       
   687    int ret,argc=0;
       
   688    char argstr[200];
       
   689    int total_cases,cur_case=0,fail=0;
       
   690    char **argv=NULL;
       
   691    char openssltestnum[20];
       
   692    FILE *arg_fp;
       
   693    
       
   694    	
       
   695   if(openssl_init())
       
   696   {
       
   697     return 1;	
       
   698   }
       
   699     
       
   700     if(argc_m > 1)
       
   701     	arg_fp = fopen(argv_m[1],"r+");
       
   702     else
       
   703     	return 1; // filename needed as argument.
       
   704     
       
   705   
       
   706     if(!arg_fp)
       
   707     {
       
   708       return 1;	
       
   709     }
       
   710   
       
   711     while(!feof(arg_fp)&&!fail)
       
   712     {
       
   713   	  memset(argstr,0,200);
       
   714       fgets(argstr, 200, arg_fp);
       
   715       strip(argstr);
       
   716       argv = create_params(argstr,&argc);
       
   717 	  strcpy(openssltestnum,argv[1]);
       
   718 	
       
   719 	  if(!strcmp(argv[2],"test_log"))
       
   720       {
       
   721         print_logs(argc,argv);
       
   722       }
       
   723       else if(!strcmp(argv[2],"compare"))
       
   724       {
       
   725         ret = compare_files(argv[3],argv[4]);	
       
   726         if(ret)
       
   727         {
       
   728         testResultXml(openssltestnum, ret);
       
   729         fail=1;
       
   730         }
       
   731         
       
   732       }
       
   733       else if(!strcmp(argv[2],"append"))
       
   734       {
       
   735         ret = append(argv[3],argv[4],argv[5]);	
       
   736         if(ret)
       
   737         {
       
   738          testResultXml(openssltestnum, ret);
       
   739          fail=1;
       
   740         }
       
   741         
       
   742       }
       
   743        else if(!strcmp(argv[2],"delete"))
       
   744       {
       
   745         delete_file(argv[3]);	
       
   746     
       
   747       }
       
   748       else
       
   749       {
       
   750         ret = openssl_main(argc-2,argv+2);
       
   751         if(ret)
       
   752         {
       
   753         testResultXml(openssltestnum, ret);
       
   754         fail=1;	
       
   755         }
       
   756         
       
   757       }   
       
   758     
       
   759      //delete_params(argv,argc);  
       
   760    }//while
       
   761    
       
   762    if(!fail&&!ret)
       
   763    testResultXml(openssltestnum, fail);
       
   764    fclose(arg_fp);
       
   765  
       
   766   
       
   767  openssl_deinit(); 
       
   768  return ret;
       
   769 }
       
   770 
       
   771 #endif  /* IF_DEF SYMBIAN */
       
   772 #ifndef SYMBIAN
       
   773 int main(int Argc, char *Argv[])
       
   774 #else
       
   775 int openssl_main(int Argc, char *Argv[])
       
   776 #endif
       
   777 	{
       
   778 	ARGS arg;
       
   779 #define PROG_NAME_SIZE	39
       
   780 	char pname[PROG_NAME_SIZE+1];
       
   781 	FUNCTION f,*fp;
       
   782 	MS_STATIC const char *prompt;
       
   783 	MS_STATIC char buf[1024];
       
   784 	char *to_free=NULL;
       
   785 	int n,i,ret=0;
       
   786 	int argc;
       
   787 	char **argv,*p;
       
   788 	LHASH *prog=NULL;
       
   789 	long errline;
       
   790  
       
   791 	arg.data=NULL;
       
   792 	arg.count=0;
       
   793 	
       
   794 	if (bio_err == NULL)
       
   795 		if ((bio_err=BIO_new(BIO_s_file())) != NULL)
       
   796 #ifdef SYMBIAN		
       
   797         BIO_set_fp(bio_err,fp_stderr,BIO_NOCLOSE|BIO_FP_TEXT);
       
   798 #else
       
   799         BIO_set_fp(bio_err,stderr,BIO_NOCLOSE|BIO_FP_TEXT);
       
   800 #endif			
       
   801 	if (getenv("OPENSSL_DEBUG_MEMORY") != NULL) /* if not defined, use compiled-in library defaults */
       
   802 		{
       
   803 		if (!(0 == strcmp(getenv("OPENSSL_DEBUG_MEMORY"), "off")))
       
   804 			{
       
   805 			CRYPTO_malloc_debug_init();
       
   806 			CRYPTO_set_mem_debug_options(V_CRYPTO_MDEBUG_ALL);
       
   807 			}
       
   808 		else
       
   809 			{
       
   810 			/* OPENSSL_DEBUG_MEMORY=off */
       
   811 			CRYPTO_set_mem_debug_functions(0, 0, 0, 0, 0);
       
   812 			}
       
   813 		}
       
   814 	CRYPTO_mem_ctrl(CRYPTO_MEM_CHECK_ON);
       
   815 
       
   816 #if 0
       
   817 	if (getenv("OPENSSL_DEBUG_LOCKING") != NULL)
       
   818 #endif
       
   819 		{
       
   820 		CRYPTO_set_locking_callback(lock_dbg_cb);
       
   821 		}
       
   822 
       
   823 	apps_startup();
       
   824 
       
   825 	/* Lets load up our environment a little */
       
   826 	p=getenv("OPENSSL_CONF");
       
   827 	if (p == NULL)
       
   828 		p=getenv("SSLEAY_CONF");
       
   829 	if (p == NULL)
       
   830 		p=to_free=make_config_name();
       
   831 
       
   832 	default_config_file=p;
       
   833 
       
   834 	config=NCONF_new(NULL);
       
   835 	i=NCONF_load(config,p,&errline);
       
   836 	if (i == 0)
       
   837 		{
       
   838 		NCONF_free(config);
       
   839 		config = NULL;
       
   840 		ERR_clear_error();
       
   841 		}
       
   842 
       
   843 	prog=prog_init();
       
   844 
       
   845 	/* first check the program name */
       
   846 	program_name(Argv[0],pname,sizeof pname);
       
   847 
       
   848 	f.name=pname;
       
   849 	fp=(FUNCTION *)lh_retrieve(prog,&f);
       
   850 	if (fp != NULL)
       
   851 		{
       
   852 		Argv[0]=pname;
       
   853 		ret=fp->func(Argc,Argv);
       
   854 		goto end;
       
   855 		}
       
   856 
       
   857 	/* ok, now check that there are not arguments, if there are,
       
   858 	 * run with them, shifting the ssleay off the front */
       
   859 	if (Argc != 1)
       
   860 		{
       
   861 		Argc--;
       
   862 		Argv++;
       
   863 		ret=do_cmd(prog,Argc,Argv);
       
   864 		if (ret < 0) ret=0;
       
   865 		goto end;
       
   866 		}
       
   867 
       
   868 	/* ok, lets enter the old 'OpenSSL>' mode */
       
   869 	
       
   870 	for (;;)
       
   871 		{
       
   872 		ret=0;
       
   873 		p=buf;
       
   874 		n=sizeof buf;
       
   875 		i=0;
       
   876 		for (;;)
       
   877 			{
       
   878 			p[0]='\0';
       
   879 			if (i++)
       
   880 				prompt=">";
       
   881 			else	prompt="OpenSSL> ";
       
   882 #ifndef SYMBIAN			
       
   883 			fputs(prompt,stdout);
       
   884 			fflush(stdout);
       
   885 			fgets(p,n,stdin);
       
   886 #else
       
   887             fputs(prompt,stdout);
       
   888 			fflush(stdout);
       
   889 			fgets(p,n,stdin);
       
   890 
       
   891 #endif			
       
   892 			if (p[0] == '\0') goto end;
       
   893 			i=strlen(p);
       
   894 			if (i <= 1) break;
       
   895 			if (p[i-2] != '\\') break;
       
   896 			i-=2;
       
   897 			p+=i;
       
   898 			n-=i;
       
   899 			}
       
   900 		if (!chopup_args(&arg,buf,&argc,&argv)) break;
       
   901 
       
   902 		ret=do_cmd(prog,argc,argv);
       
   903 		if (ret < 0)
       
   904 			{
       
   905 			ret=0;
       
   906 			goto end;
       
   907 			}
       
   908 		if (ret != 0)
       
   909 			BIO_printf(bio_err,"error in %s\n",argv[0]);
       
   910 		(void)BIO_flush(bio_err);
       
   911 		}
       
   912 	BIO_printf(bio_err,"bad exit\n");
       
   913 	ret=1;
       
   914 end:
       
   915 	if (to_free)
       
   916 		OPENSSL_free(to_free);
       
   917 	if (config != NULL)
       
   918 		{
       
   919 		NCONF_free(config);
       
   920 		config=NULL;
       
   921 		}
       
   922 	if (prog != NULL) lh_free(prog);
       
   923 	if (arg.data != NULL) OPENSSL_free(arg.data);
       
   924 
       
   925 	apps_shutdown();
       
   926 
       
   927 	CRYPTO_mem_leaks(bio_err);
       
   928 	if (bio_err != NULL)
       
   929 		{
       
   930 		BIO_free(bio_err);
       
   931 		bio_err=NULL;
       
   932 		}
       
   933 	
       
   934 	return ret;
       
   935 //	OPENSSL_EXIT(ret);
       
   936 	}
       
   937 
       
   938 #define LIST_STANDARD_COMMANDS "list-standard-commands"
       
   939 #define LIST_MESSAGE_DIGEST_COMMANDS "list-message-digest-commands"
       
   940 #define LIST_CIPHER_COMMANDS "list-cipher-commands"
       
   941 
       
   942 static int do_cmd(LHASH *prog, int argc, char *argv[])
       
   943 	{
       
   944 	FUNCTION f,*fp;
       
   945 	int i,ret=1,tp,nl;
       
   946 
       
   947 	if ((argc <= 0) || (argv[0] == NULL))
       
   948 		{ ret=0; goto end; }
       
   949 	f.name=argv[0];
       
   950 	fp=(FUNCTION *)lh_retrieve(prog,&f);
       
   951 	if (fp != NULL)
       
   952 		{
       
   953 		ret=fp->func(argc,argv);
       
   954 		}
       
   955 	else if ((strncmp(argv[0],"no-",3)) == 0)
       
   956 		{
       
   957 
       
   958 		BIO *bio_stdout = BIO_new_fp(stdout,BIO_NOCLOSE);
       
   959 #ifdef OPENSSL_SYS_VMS
       
   960 		{
       
   961 		BIO *tmpbio = BIO_new(BIO_f_linebuffer());
       
   962 		bio_stdout = BIO_push(tmpbio, bio_stdout);
       
   963 		}
       
   964 #endif
       
   965 		f.name=argv[0]+3;
       
   966 		ret = (lh_retrieve(prog,&f) != NULL);
       
   967 		if (!ret)
       
   968 			BIO_printf(bio_stdout, "%s\n", argv[0]);
       
   969 		else
       
   970 			BIO_printf(bio_stdout, "%s\n", argv[0]+3);
       
   971 		BIO_free_all(bio_stdout);
       
   972 		goto end;
       
   973 		}
       
   974 	else if ((strcmp(argv[0],"quit") == 0) ||
       
   975 		(strcmp(argv[0],"q") == 0) ||
       
   976 		(strcmp(argv[0],"exit") == 0) ||
       
   977 		(strcmp(argv[0],"bye") == 0))
       
   978 		{
       
   979 		ret= -1;
       
   980 		goto end;
       
   981 		}
       
   982 	else if ((strcmp(argv[0],LIST_STANDARD_COMMANDS) == 0) ||
       
   983 		(strcmp(argv[0],LIST_MESSAGE_DIGEST_COMMANDS) == 0) ||
       
   984 		(strcmp(argv[0],LIST_CIPHER_COMMANDS) == 0))
       
   985 		{
       
   986 		int list_type;
       
   987 		BIO *bio_stdout;
       
   988 
       
   989 		if (strcmp(argv[0],LIST_STANDARD_COMMANDS) == 0)
       
   990 			list_type = FUNC_TYPE_GENERAL;
       
   991 		else if (strcmp(argv[0],LIST_MESSAGE_DIGEST_COMMANDS) == 0)
       
   992 			list_type = FUNC_TYPE_MD;
       
   993 		else /* strcmp(argv[0],LIST_CIPHER_COMMANDS) == 0 */
       
   994 			list_type = FUNC_TYPE_CIPHER;
       
   995 
       
   996 		bio_stdout = BIO_new_fp(stdout,BIO_NOCLOSE);
       
   997 #ifdef OPENSSL_SYS_VMS
       
   998 		{
       
   999 		BIO *tmpbio = BIO_new(BIO_f_linebuffer());
       
  1000 		bio_stdout = BIO_push(tmpbio, bio_stdout);
       
  1001 		}
       
  1002 #endif
       
  1003 		
       
  1004 		for (fp=functions; fp->name != NULL; fp++)
       
  1005 			if (fp->type == list_type)
       
  1006 				BIO_printf(bio_stdout, "%s\n", fp->name);
       
  1007 		BIO_free_all(bio_stdout);
       
  1008 		ret=0;
       
  1009 		goto end;
       
  1010 		}
       
  1011 	else
       
  1012 		{
       
  1013 		BIO_printf(bio_err,"openssl:Error: '%s' is an invalid command.\n",
       
  1014 			argv[0]);
       
  1015 		BIO_printf(bio_err, "\nStandard commands");
       
  1016 		i=0;
       
  1017 		tp=0;
       
  1018 		for (fp=functions; fp->name != NULL; fp++)
       
  1019 			{
       
  1020 			nl=0;
       
  1021 			if (((i++) % 5) == 0)
       
  1022 				{
       
  1023 				BIO_printf(bio_err,"\n");
       
  1024 				nl=1;
       
  1025 				}
       
  1026 			if (fp->type != tp)
       
  1027 				{
       
  1028 				tp=fp->type;
       
  1029 				if (!nl) BIO_printf(bio_err,"\n");
       
  1030 				if (tp == FUNC_TYPE_MD)
       
  1031 					{
       
  1032 					i=1;
       
  1033 					BIO_printf(bio_err,
       
  1034 						"\nMessage Digest commands (see the `dgst' command for more details)\n");
       
  1035 					}
       
  1036 				else if (tp == FUNC_TYPE_CIPHER)
       
  1037 					{
       
  1038 					i=1;
       
  1039 					BIO_printf(bio_err,"\nCipher commands (see the `enc' command for more details)\n");
       
  1040 					}
       
  1041 				}
       
  1042 			BIO_printf(bio_err,"%-15s",fp->name);
       
  1043 			}
       
  1044 		BIO_printf(bio_err,"\n\n");
       
  1045 		ret=0;
       
  1046 		}
       
  1047 end:
       
  1048 	return(ret);
       
  1049 	}
       
  1050 
       
  1051 static int SortFnByName(const void *_f1,const void *_f2)
       
  1052     {
       
  1053     const FUNCTION *f1=_f1;
       
  1054     const FUNCTION *f2=_f2;
       
  1055 
       
  1056     if(f1->type != f2->type)
       
  1057 	return f1->type-f2->type;
       
  1058     return strcmp(f1->name,f2->name);
       
  1059     }
       
  1060 
       
  1061 static LHASH *prog_init(void)
       
  1062 	{
       
  1063 	LHASH *ret;
       
  1064 	FUNCTION *f;
       
  1065 	size_t i;
       
  1066 
       
  1067 	/* Purely so it looks nice when the user hits ? */
       
  1068 	for(i=0,f=functions ; f->name != NULL ; ++f,++i)
       
  1069 	    ;
       
  1070 	qsort(functions,i,sizeof *functions,SortFnByName);
       
  1071 
       
  1072 	if ((ret=lh_new(hash, cmp)) == NULL)
       
  1073 		return(NULL);
       
  1074 
       
  1075 	for (f=functions; f->name != NULL; f++)
       
  1076 		lh_insert(ret,f);
       
  1077 	return(ret);
       
  1078 	}
       
  1079 
       
  1080 /* static int MS_CALLBACK cmp(FUNCTION *a, FUNCTION *b) */
       
  1081 static int MS_CALLBACK cmp(const void *a_void, const void *b_void)
       
  1082 	{
       
  1083 	return(strncmp(((const FUNCTION *)a_void)->name,
       
  1084 			((const FUNCTION *)b_void)->name,8));
       
  1085 	}
       
  1086 
       
  1087 /* static unsigned long MS_CALLBACK hash(FUNCTION *a) */
       
  1088 static unsigned long MS_CALLBACK hash(const void *a_void)
       
  1089 	{
       
  1090 	return(lh_strhash(((const FUNCTION *)a_void)->name));
       
  1091 	}