ssl/tsrc/crypto_test/src/enginetest.c
changeset 31 ce057bb09d0b
parent 0 e4d67989cc36
equal deleted inserted replaced
30:e20de85af2ee 31:ce057bb09d0b
       
     1 /* crypto/engine/enginetest.c */
       
     2 /* Written by Geoff Thorpe (geoff@geoffthorpe.net) for the OpenSSL
       
     3  * project 2000.
       
     4  */
       
     5 /* ====================================================================
       
     6  * Copyright (c) 1999-2001 The OpenSSL Project.  All rights reserved.
       
     7  *
       
     8  * Redistribution and use in source and binary forms, with or without
       
     9  * modification, are permitted provided that the following conditions
       
    10  * are met:
       
    11  *
       
    12  * 1. Redistributions of source code must retain the above copyright
       
    13  *    notice, this list of conditions and the following disclaimer. 
       
    14  *
       
    15  * 2. Redistributions in binary form must reproduce the above copyright
       
    16  *    notice, this list of conditions and the following disclaimer in
       
    17  *    the documentation and/or other materials provided with the
       
    18  *    distribution.
       
    19  *
       
    20  * 3. All advertising materials mentioning features or use of this
       
    21  *    software must display the following acknowledgment:
       
    22  *    "This product includes software developed by the OpenSSL Project
       
    23  *    for use in the OpenSSL Toolkit. (http://www.OpenSSL.org/)"
       
    24  *
       
    25  * 4. The names "OpenSSL Toolkit" and "OpenSSL Project" must not be used to
       
    26  *    endorse or promote products derived from this software without
       
    27  *    prior written permission. For written permission, please contact
       
    28  *    licensing@OpenSSL.org.
       
    29  *
       
    30  * 5. Products derived from this software may not be called "OpenSSL"
       
    31  *    nor may "OpenSSL" appear in their names without prior written
       
    32  *    permission of the OpenSSL Project.
       
    33  *
       
    34  * 6. Redistributions of any form whatsoever must retain the following
       
    35  *    acknowledgment:
       
    36  *    "This product includes software developed by the OpenSSL Project
       
    37  *    for use in the OpenSSL Toolkit (http://www.OpenSSL.org/)"
       
    38  *
       
    39  * THIS SOFTWARE IS PROVIDED BY THE OpenSSL PROJECT ``AS IS'' AND ANY
       
    40  * EXPRESSED OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
       
    41  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
       
    42  * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE OpenSSL PROJECT OR
       
    43  * ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
       
    44  * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
       
    45  * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
       
    46  * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
       
    47  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
       
    48  * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
       
    49  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED
       
    50  * OF THE POSSIBILITY OF SUCH DAMAGE.
       
    51  * ====================================================================
       
    52  *	
       
    53  * This product includes cryptographic software written by Eric Young
       
    54  * (eay@cryptsoft.com).  This product includes software written by Tim
       
    55  * Hudson (tjh@cryptsoft.com).
       
    56  *
       
    57  */
       
    58 /*
       
    59  © Portions copyright (c) 2006 Nokia Corporation.  All rights reserved.
       
    60  */
       
    61 #include <stdio.h>
       
    62 #include <string.h>
       
    63 
       
    64 #ifdef OPENSSL_NO_ENGINE
       
    65 int main(int argc, char *argv[])
       
    66 {
       
    67     printf("No ENGINE support\n");
       
    68     return(0);
       
    69 }
       
    70 #else
       
    71 #include <openssl/e_os2.h>
       
    72 #include <openssl/buffer.h>
       
    73 #include <openssl/crypto.h>
       
    74 #include <openssl/engine.h>
       
    75 #include <openssl/err.h>
       
    76 
       
    77 #ifdef SYMBIAN
       
    78 #ifdef stdin
       
    79 #undef stdin
       
    80 #endif
       
    81 #ifdef stdout
       
    82 #undef stdout
       
    83 #endif
       
    84 #ifdef stderr
       
    85 #undef stderr
       
    86 #endif
       
    87 
       
    88 #define stdin fp_stdin
       
    89 #define stdout fp_stdout
       
    90 #define stderr fp_stderr
       
    91 
       
    92 extern FILE *fp_stdout;
       
    93 extern FILE *fp_stderr;
       
    94 #endif
       
    95 
       
    96 static void display_engine_list(void)
       
    97 	{
       
    98 	ENGINE *h;
       
    99 	int loop;
       
   100 
       
   101 	h = ENGINE_get_first();
       
   102 	if(errno==ENOMEM)
       
   103   {
       
   104 	  return ;
       
   105 	}
       
   106 	loop = 0;
       
   107 	fprintf(stdout,"listing available engine types\n");
       
   108 	if(errno==ENOMEM)
       
   109   {
       
   110 	  return ;
       
   111 	}
       
   112 	while(h)
       
   113 		{
       
   114 		fprintf(stdout,"engine %i, id = \"%s\", name = \"%s\"\n",
       
   115 			loop++, ENGINE_get_id(h), ENGINE_get_name(h));
       
   116 		if(errno==ENOMEM)
       
   117    	{
       
   118 	  return ;
       
   119 	  }	
       
   120 		h = ENGINE_get_next(h);
       
   121 		if(errno==ENOMEM)
       
   122    	{
       
   123 	  return ;
       
   124 	  }
       
   125 		}
       
   126 	fprintf(stdout,"end of list\n");
       
   127 	if(errno==ENOMEM)
       
   128   {
       
   129 	  return ;
       
   130 	}
       
   131 	/* ENGINE_get_first() increases the struct_ref counter, so we 
       
   132            must call ENGINE_free() to decrease it again */
       
   133 	ENGINE_free(h);
       
   134 	if(errno==ENOMEM)
       
   135   {
       
   136 	  return ;
       
   137 	}
       
   138 	}
       
   139 
       
   140 #ifndef SYMBIAN
       
   141 int main(int argc, char *argv[])
       
   142 #else
       
   143 int engine_main(int argc, char *argv[])
       
   144 #endif
       
   145 
       
   146 	{
       
   147 	ENGINE *block[512];
       
   148 	char buf[256];
       
   149 	const char *id, *name;
       
   150 	ENGINE *ptr;
       
   151 	int loop;
       
   152 	int to_return = 1;
       
   153 	ENGINE *new_h1 = NULL;
       
   154 	ENGINE *new_h2 = NULL;
       
   155 	ENGINE *new_h3 = NULL;
       
   156 	ENGINE *new_h4 = NULL;
       
   157     
       
   158 	/* enable memory leak checking unless explicitly disabled */
       
   159 
       
   160 	ERR_load_crypto_strings();
       
   161 	if(errno==ENOMEM)
       
   162   {
       
   163 	  return 1;
       
   164 	}
       
   165 
       
   166 	memset(block, 0, 512 * sizeof(ENGINE *));
       
   167 	if(errno==ENOMEM)
       
   168   {
       
   169 	  return 1;
       
   170 	}
       
   171 	if(((new_h1 = ENGINE_new()) == NULL) ||
       
   172 			!ENGINE_set_id(new_h1, "test_id0") ||
       
   173 			!ENGINE_set_name(new_h1, "First test item") ||
       
   174 			((new_h2 = ENGINE_new()) == NULL) ||
       
   175 			!ENGINE_set_id(new_h2, "test_id1") ||
       
   176 			!ENGINE_set_name(new_h2, "Second test item") ||
       
   177 			((new_h3 = ENGINE_new()) == NULL) ||
       
   178 			!ENGINE_set_id(new_h3, "test_id2") ||
       
   179 			!ENGINE_set_name(new_h3, "Third test item") ||
       
   180 			((new_h4 = ENGINE_new()) == NULL) ||
       
   181 			!ENGINE_set_id(new_h4, "test_id3") ||
       
   182 			!ENGINE_set_name(new_h4, "Fourth test item"))
       
   183 		{
       
   184 		if(errno==ENOMEM)
       
   185    	{
       
   186 	  return 1;
       
   187 	  }	
       
   188 		fprintf(stdout,"Couldn't set up test ENGINE structures\n");
       
   189 		goto end;
       
   190 		}
       
   191 	fprintf(stdout,"\nenginetest beginning\n\n");
       
   192 	if(errno==ENOMEM)
       
   193   {
       
   194 	  return 1;
       
   195 	}
       
   196 	display_engine_list();
       
   197 	if(!ENGINE_add(new_h1))
       
   198 		{
       
   199 		fprintf(stdout,"Add failed!\n");
       
   200 		goto end;
       
   201 		}
       
   202 	display_engine_list();
       
   203 	if(errno==ENOMEM)
       
   204   {
       
   205 	  return 1;
       
   206 	}
       
   207 	ptr = ENGINE_get_first();
       
   208 	if(errno==ENOMEM)
       
   209   {
       
   210 	  return 1;
       
   211 	}
       
   212 	if(!ENGINE_remove(ptr))
       
   213 		{
       
   214 		if(errno==ENOMEM)
       
   215    	{
       
   216 	  return 1;
       
   217 	  }	
       
   218 		fprintf(stdout,"Remove failed!\n");
       
   219 		goto end;
       
   220 		}
       
   221 	if (ptr)
       
   222 	{
       
   223 		ENGINE_free(ptr);
       
   224 		if(errno==ENOMEM)
       
   225    	{
       
   226 	  return 1;
       
   227 	  }
       
   228 	}	
       
   229 	display_engine_list();
       
   230 	if(errno==ENOMEM)
       
   231   {
       
   232 	  return 1;
       
   233 	}
       
   234 	if(!ENGINE_add(new_h3) || !ENGINE_add(new_h2))
       
   235 		{
       
   236 		if(errno==ENOMEM)
       
   237    	{
       
   238 	  return 1;
       
   239 	  }	
       
   240 		fprintf(stdout,"Add failed!\n");
       
   241 		goto end;
       
   242 		}
       
   243 	display_engine_list();
       
   244 	if(errno==ENOMEM)
       
   245   {
       
   246 	  return 1;
       
   247 	}
       
   248 	if(!ENGINE_remove(new_h2))
       
   249 		{
       
   250 		if(errno==ENOMEM)
       
   251    	{
       
   252 	  return 1;
       
   253 	  }	
       
   254 		fprintf(stdout,"Remove failed!\n");
       
   255 		goto end;
       
   256 		}
       
   257 	display_engine_list();
       
   258 	if(errno==ENOMEM)
       
   259   {
       
   260 	  return 1;
       
   261 	}
       
   262 	if(!ENGINE_add(new_h4))
       
   263 		{
       
   264 		if(errno==ENOMEM)
       
   265    	{
       
   266 	  return 1;
       
   267 	  }	
       
   268 		fprintf(stdout,"Add failed!\n");
       
   269 		goto end;
       
   270 		}
       
   271 	display_engine_list();
       
   272 	if(errno==ENOMEM)
       
   273   {
       
   274 	  return 1;
       
   275 	}
       
   276 	if(ENGINE_add(new_h3))
       
   277 		{
       
   278 		if(errno==ENOMEM)
       
   279    	{
       
   280 	  return 1;
       
   281 	  }	
       
   282 		fprintf(stdout,"Add *should* have failed but didn't!\n");
       
   283 		goto end;
       
   284 		}
       
   285 	else
       
   286 		fprintf(stdout,"Add that should fail did.\n");
       
   287 	ERR_clear_error();
       
   288 	if(errno==ENOMEM)
       
   289   {
       
   290 	  return 1;
       
   291 	}
       
   292 	if(ENGINE_remove(new_h2))
       
   293 		{
       
   294 		if(errno==ENOMEM)
       
   295    	{
       
   296 	  return 1;
       
   297 	  }	
       
   298 		fprintf(stdout,"Remove *should* have failed but didn't!\n");
       
   299 		goto end;
       
   300 		}
       
   301 	else
       
   302 		fprintf(stdout,"Remove that should fail did.\n");
       
   303 	ERR_clear_error();
       
   304 	if(errno==ENOMEM)
       
   305   {
       
   306 	  return 1;
       
   307 	}
       
   308 	if(!ENGINE_remove(new_h3))
       
   309 		{
       
   310 		if(errno==ENOMEM)
       
   311    	{
       
   312 	  return 1;
       
   313 	  }	
       
   314 		fprintf(stdout,"Remove failed!\n");
       
   315 		goto end;
       
   316 		}
       
   317 	display_engine_list();
       
   318 	if(errno==ENOMEM)
       
   319   {
       
   320 	  return 1;
       
   321 	}
       
   322 	if(!ENGINE_remove(new_h4))
       
   323 		{
       
   324 		if(errno==ENOMEM)
       
   325    	{
       
   326 	  return 1;
       
   327 	  }	
       
   328 		fprintf(stdout,"Remove failed!\n");
       
   329 		goto end;
       
   330 		}
       
   331 	display_engine_list();
       
   332 	if(errno==ENOMEM)
       
   333   {
       
   334 	  return 1;
       
   335 	}
       
   336 	/* Depending on whether there's any hardware support compiled
       
   337 	 * in, this remove may be destined to fail. */
       
   338 	ptr = ENGINE_get_first();
       
   339 	if(errno==ENOMEM)
       
   340   {
       
   341 	  return 1;
       
   342 	}
       
   343 	if(ptr)
       
   344 		if(!ENGINE_remove(ptr))
       
   345 		{
       
   346 			if(errno==ENOMEM)
       
   347     	{
       
   348 	      return 1;
       
   349 	    }
       
   350 			fprintf(stdout,"Remove failed!i - probably no hardware "
       
   351 				"support present.\n");
       
   352 		}		
       
   353 	if (ptr)
       
   354 	{
       
   355 		ENGINE_free(ptr);
       
   356 		if(errno==ENOMEM)
       
   357    	{
       
   358 	  return 1;
       
   359 	  }
       
   360 	}	
       
   361 	display_engine_list();
       
   362 	if(errno==ENOMEM)
       
   363   {
       
   364 	  return 1;
       
   365 	}
       
   366 	if(!ENGINE_add(new_h1) || !ENGINE_remove(new_h1))
       
   367 		{
       
   368 		if(errno==ENOMEM)
       
   369    	{
       
   370 	  return 1;
       
   371 	  }	
       
   372 		fprintf(stdout,"Couldn't add and remove to an empty list!\n");
       
   373 		goto end;
       
   374 		}
       
   375 	else
       
   376 		fprintf(stdout,"Successfully added and removed to an empty list!\n");
       
   377 	fprintf(stdout,"About to beef up the engine-type list\n");
       
   378 	for(loop = 0; loop < 512; loop++)
       
   379 		{
       
   380 		sprintf(buf, "id%i", loop);
       
   381 		id = BUF_strdup(buf);
       
   382 		if(errno==ENOMEM)
       
   383    	{
       
   384 	  return 1;
       
   385 	  }
       
   386 		sprintf(buf, "Fake engine type %i", loop);
       
   387 		name = BUF_strdup(buf);
       
   388 		if(errno==ENOMEM)
       
   389    	{
       
   390 	  return 1;
       
   391 	  }
       
   392 		if(((block[loop] = ENGINE_new()) == NULL) ||
       
   393 				!ENGINE_set_id(block[loop], id) ||
       
   394 				!ENGINE_set_name(block[loop], name))
       
   395 			{
       
   396 			if(errno==ENOMEM)
       
   397      	{
       
   398 	      return 1;
       
   399 	    }	
       
   400 			fprintf(stdout,"Couldn't create block of ENGINE structures.\n"
       
   401 				"I'll probably also core-dump now, damn.\n");
       
   402 			goto end;
       
   403 			}
       
   404 		}
       
   405 	for(loop = 0; loop < 512; loop++)
       
   406 		{
       
   407 		if(!ENGINE_add(block[loop]))
       
   408 			{
       
   409 			if(errno==ENOMEM)
       
   410      	{
       
   411 	     return 1;
       
   412 	    } 	
       
   413 			fprintf(stdout,"\nAdding stopped at %i, (%s,%s)\n",
       
   414 				loop, ENGINE_get_id(block[loop]),
       
   415 				ENGINE_get_name(block[loop]));
       
   416 			if(errno==ENOMEM)
       
   417      	{
       
   418 	     return 1;
       
   419 	    } 	
       
   420 						
       
   421 			goto cleanup_loop;
       
   422 			}
       
   423 		else
       
   424 			fprintf(stdout,"."); fflush(stdout);
       
   425 		}
       
   426 cleanup_loop:
       
   427 	fprintf(stdout,"\nAbout to empty the engine-type list\n");
       
   428 	while((ptr = ENGINE_get_first()) != NULL)
       
   429 		{
       
   430 			if(errno==ENOMEM)
       
   431    	 {
       
   432 	    return 1;
       
   433 	   }
       
   434 		if(!ENGINE_remove(ptr))
       
   435 			{
       
   436 			if(errno==ENOMEM)
       
   437      	{
       
   438 	      return 1;
       
   439 	    }
       
   440 			fprintf(stdout,"\nRemove failed!\n");
       
   441 			goto end;
       
   442 			}
       
   443 		ENGINE_free(ptr);
       
   444 		if(errno==ENOMEM)
       
   445    	{
       
   446 	  return 1;
       
   447 	  }
       
   448 		fprintf(stdout,"."); fflush(stdout);
       
   449 		}
       
   450 	for(loop = 0; loop < 512; loop++)
       
   451 		{
       
   452 		OPENSSL_free((void *)ENGINE_get_id(block[loop]));
       
   453 		if(errno==ENOMEM)
       
   454      	{
       
   455 	     return 1;
       
   456 	    } 	
       
   457 		
       
   458 		OPENSSL_free((void *)ENGINE_get_name(block[loop]));
       
   459 			if(errno==ENOMEM)
       
   460      	{
       
   461 	     return 1;
       
   462 	    } 	
       
   463 		
       
   464 		}
       
   465 	fprintf(stdout,"\nTests completed happily\n");
       
   466 	to_return = 0;
       
   467 end:
       
   468 	if(to_return)
       
   469 	{
       
   470 		ERR_print_errors_fp(stderr);
       
   471 		if(errno==ENOMEM)
       
   472    	{
       
   473 	  return 1;
       
   474 	  }
       
   475 	}	
       
   476 	if(new_h1)
       
   477 	{ ENGINE_free(new_h1);
       
   478 		if(errno==ENOMEM)
       
   479    	{
       
   480 	  return 1;
       
   481 	  }
       
   482 	}  
       
   483 	if(new_h2) 
       
   484 	{
       
   485 		ENGINE_free(new_h2);
       
   486 		if(errno==ENOMEM)
       
   487    	{
       
   488 	  return 1;
       
   489 	  }
       
   490 	}  
       
   491 	if(new_h3)
       
   492 	{ 
       
   493 		ENGINE_free(new_h3);
       
   494 		if(errno==ENOMEM)
       
   495    	{
       
   496 	  return 1;
       
   497 	  }
       
   498 	}  
       
   499 	if(new_h4)
       
   500 	{
       
   501 		 ENGINE_free(new_h4);
       
   502 		 if(errno==ENOMEM)
       
   503    	{
       
   504 	  return 1;
       
   505 	  }
       
   506 	}  
       
   507 	for(loop = 0; loop < 512; loop++)
       
   508 		if(block[loop])
       
   509 		{
       
   510 			ENGINE_free(block[loop]);
       
   511 			if(errno==ENOMEM)
       
   512    	 {
       
   513 	    return 1;
       
   514 	   }
       
   515 		}	
       
   516 	ENGINE_cleanup();
       
   517 	if(errno==ENOMEM)
       
   518   {
       
   519 	  return 1;
       
   520 	}
       
   521 	CRYPTO_cleanup_all_ex_data();
       
   522 	if(errno==ENOMEM)
       
   523   {
       
   524 	  return 1;
       
   525 	}
       
   526 	ERR_free_strings();
       
   527 	if(errno==ENOMEM)
       
   528  	{
       
   529 	  return 1;
       
   530 	}
       
   531 	ERR_remove_state(0);
       
   532 	if(errno==ENOMEM)
       
   533   {
       
   534   		  return 1;
       
   535 	}
       
   536 	CRYPTO_mem_leaks_fp(stderr);
       
   537 	if(errno==ENOMEM)
       
   538   {
       
   539 	  return 1;
       
   540 	}
       
   541 	return to_return;
       
   542 	}
       
   543 #endif