sl@0: /* crypto/engine/enginetest.c */
sl@0: /* Written by Geoff Thorpe (geoff@geoffthorpe.net) for the OpenSSL
sl@0:  * project 2000.
sl@0:  */
sl@0: /* ====================================================================
sl@0:  * Copyright (c) 1999-2001 The OpenSSL Project.  All rights reserved.
sl@0:  *
sl@0:  * Redistribution and use in source and binary forms, with or without
sl@0:  * modification, are permitted provided that the following conditions
sl@0:  * are met:
sl@0:  *
sl@0:  * 1. Redistributions of source code must retain the above copyright
sl@0:  *    notice, this list of conditions and the following disclaimer. 
sl@0:  *
sl@0:  * 2. Redistributions in binary form must reproduce the above copyright
sl@0:  *    notice, this list of conditions and the following disclaimer in
sl@0:  *    the documentation and/or other materials provided with the
sl@0:  *    distribution.
sl@0:  *
sl@0:  * 3. All advertising materials mentioning features or use of this
sl@0:  *    software must display the following acknowledgment:
sl@0:  *    "This product includes software developed by the OpenSSL Project
sl@0:  *    for use in the OpenSSL Toolkit. (http://www.OpenSSL.org/)"
sl@0:  *
sl@0:  * 4. The names "OpenSSL Toolkit" and "OpenSSL Project" must not be used to
sl@0:  *    endorse or promote products derived from this software without
sl@0:  *    prior written permission. For written permission, please contact
sl@0:  *    licensing@OpenSSL.org.
sl@0:  *
sl@0:  * 5. Products derived from this software may not be called "OpenSSL"
sl@0:  *    nor may "OpenSSL" appear in their names without prior written
sl@0:  *    permission of the OpenSSL Project.
sl@0:  *
sl@0:  * 6. Redistributions of any form whatsoever must retain the following
sl@0:  *    acknowledgment:
sl@0:  *    "This product includes software developed by the OpenSSL Project
sl@0:  *    for use in the OpenSSL Toolkit (http://www.OpenSSL.org/)"
sl@0:  *
sl@0:  * THIS SOFTWARE IS PROVIDED BY THE OpenSSL PROJECT ``AS IS'' AND ANY
sl@0:  * EXPRESSED OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
sl@0:  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
sl@0:  * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE OpenSSL PROJECT OR
sl@0:  * ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
sl@0:  * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
sl@0:  * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
sl@0:  * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
sl@0:  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
sl@0:  * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
sl@0:  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED
sl@0:  * OF THE POSSIBILITY OF SUCH DAMAGE.
sl@0:  * ====================================================================
sl@0:  *	
sl@0:  * This product includes cryptographic software written by Eric Young
sl@0:  * (eay@cryptsoft.com).  This product includes software written by Tim
sl@0:  * Hudson (tjh@cryptsoft.com).
sl@0:  *
sl@0:  */
sl@0: /*
sl@0:  © Portions copyright (c) 2006 Nokia Corporation.  All rights reserved.
sl@0:  */
sl@0: #include <stdio.h>
sl@0: #include <string.h>
sl@0: 
sl@0: #ifdef OPENSSL_NO_ENGINE
sl@0: int main(int argc, char *argv[])
sl@0: {
sl@0:     printf("No ENGINE support\n");
sl@0:     return(0);
sl@0: }
sl@0: #else
sl@0: #include <openssl/e_os2.h>
sl@0: #include <openssl/buffer.h>
sl@0: #include <openssl/crypto.h>
sl@0: #include <openssl/engine.h>
sl@0: #include <openssl/err.h>
sl@0: 
sl@0: #ifdef SYMBIAN
sl@0: #ifdef stdin
sl@0: #undef stdin
sl@0: #endif
sl@0: #ifdef stdout
sl@0: #undef stdout
sl@0: #endif
sl@0: #ifdef stderr
sl@0: #undef stderr
sl@0: #endif
sl@0: 
sl@0: #define stdin fp_stdin
sl@0: #define stdout fp_stdout
sl@0: #define stderr fp_stderr
sl@0: 
sl@0: extern FILE *fp_stdout;
sl@0: extern FILE *fp_stderr;
sl@0: #endif
sl@0: 
sl@0: static void display_engine_list(void)
sl@0: 	{
sl@0: 	ENGINE *h;
sl@0: 	int loop;
sl@0: 
sl@0: 	h = ENGINE_get_first();
sl@0: 	if(errno==ENOMEM)
sl@0:   {
sl@0: 	  return ;
sl@0: 	}
sl@0: 	loop = 0;
sl@0: 	fprintf(stdout,"listing available engine types\n");
sl@0: 	if(errno==ENOMEM)
sl@0:   {
sl@0: 	  return ;
sl@0: 	}
sl@0: 	while(h)
sl@0: 		{
sl@0: 		fprintf(stdout,"engine %i, id = \"%s\", name = \"%s\"\n",
sl@0: 			loop++, ENGINE_get_id(h), ENGINE_get_name(h));
sl@0: 		if(errno==ENOMEM)
sl@0:    	{
sl@0: 	  return ;
sl@0: 	  }	
sl@0: 		h = ENGINE_get_next(h);
sl@0: 		if(errno==ENOMEM)
sl@0:    	{
sl@0: 	  return ;
sl@0: 	  }
sl@0: 		}
sl@0: 	fprintf(stdout,"end of list\n");
sl@0: 	if(errno==ENOMEM)
sl@0:   {
sl@0: 	  return ;
sl@0: 	}
sl@0: 	/* ENGINE_get_first() increases the struct_ref counter, so we 
sl@0:            must call ENGINE_free() to decrease it again */
sl@0: 	ENGINE_free(h);
sl@0: 	if(errno==ENOMEM)
sl@0:   {
sl@0: 	  return ;
sl@0: 	}
sl@0: 	}
sl@0: 
sl@0: #ifndef SYMBIAN
sl@0: int main(int argc, char *argv[])
sl@0: #else
sl@0: int engine_main(int argc, char *argv[])
sl@0: #endif
sl@0: 
sl@0: 	{
sl@0: 	ENGINE *block[512];
sl@0: 	char buf[256];
sl@0: 	const char *id, *name;
sl@0: 	ENGINE *ptr;
sl@0: 	int loop;
sl@0: 	int to_return = 1;
sl@0: 	ENGINE *new_h1 = NULL;
sl@0: 	ENGINE *new_h2 = NULL;
sl@0: 	ENGINE *new_h3 = NULL;
sl@0: 	ENGINE *new_h4 = NULL;
sl@0:     
sl@0: 	/* enable memory leak checking unless explicitly disabled */
sl@0: 
sl@0: 	ERR_load_crypto_strings();
sl@0: 	if(errno==ENOMEM)
sl@0:   {
sl@0: 	  return 1;
sl@0: 	}
sl@0: 
sl@0: 	memset(block, 0, 512 * sizeof(ENGINE *));
sl@0: 	if(errno==ENOMEM)
sl@0:   {
sl@0: 	  return 1;
sl@0: 	}
sl@0: 	if(((new_h1 = ENGINE_new()) == NULL) ||
sl@0: 			!ENGINE_set_id(new_h1, "test_id0") ||
sl@0: 			!ENGINE_set_name(new_h1, "First test item") ||
sl@0: 			((new_h2 = ENGINE_new()) == NULL) ||
sl@0: 			!ENGINE_set_id(new_h2, "test_id1") ||
sl@0: 			!ENGINE_set_name(new_h2, "Second test item") ||
sl@0: 			((new_h3 = ENGINE_new()) == NULL) ||
sl@0: 			!ENGINE_set_id(new_h3, "test_id2") ||
sl@0: 			!ENGINE_set_name(new_h3, "Third test item") ||
sl@0: 			((new_h4 = ENGINE_new()) == NULL) ||
sl@0: 			!ENGINE_set_id(new_h4, "test_id3") ||
sl@0: 			!ENGINE_set_name(new_h4, "Fourth test item"))
sl@0: 		{
sl@0: 		if(errno==ENOMEM)
sl@0:    	{
sl@0: 	  return 1;
sl@0: 	  }	
sl@0: 		fprintf(stdout,"Couldn't set up test ENGINE structures\n");
sl@0: 		goto end;
sl@0: 		}
sl@0: 	fprintf(stdout,"\nenginetest beginning\n\n");
sl@0: 	if(errno==ENOMEM)
sl@0:   {
sl@0: 	  return 1;
sl@0: 	}
sl@0: 	display_engine_list();
sl@0: 	if(!ENGINE_add(new_h1))
sl@0: 		{
sl@0: 		fprintf(stdout,"Add failed!\n");
sl@0: 		goto end;
sl@0: 		}
sl@0: 	display_engine_list();
sl@0: 	if(errno==ENOMEM)
sl@0:   {
sl@0: 	  return 1;
sl@0: 	}
sl@0: 	ptr = ENGINE_get_first();
sl@0: 	if(errno==ENOMEM)
sl@0:   {
sl@0: 	  return 1;
sl@0: 	}
sl@0: 	if(!ENGINE_remove(ptr))
sl@0: 		{
sl@0: 		if(errno==ENOMEM)
sl@0:    	{
sl@0: 	  return 1;
sl@0: 	  }	
sl@0: 		fprintf(stdout,"Remove failed!\n");
sl@0: 		goto end;
sl@0: 		}
sl@0: 	if (ptr)
sl@0: 	{
sl@0: 		ENGINE_free(ptr);
sl@0: 		if(errno==ENOMEM)
sl@0:    	{
sl@0: 	  return 1;
sl@0: 	  }
sl@0: 	}	
sl@0: 	display_engine_list();
sl@0: 	if(errno==ENOMEM)
sl@0:   {
sl@0: 	  return 1;
sl@0: 	}
sl@0: 	if(!ENGINE_add(new_h3) || !ENGINE_add(new_h2))
sl@0: 		{
sl@0: 		if(errno==ENOMEM)
sl@0:    	{
sl@0: 	  return 1;
sl@0: 	  }	
sl@0: 		fprintf(stdout,"Add failed!\n");
sl@0: 		goto end;
sl@0: 		}
sl@0: 	display_engine_list();
sl@0: 	if(errno==ENOMEM)
sl@0:   {
sl@0: 	  return 1;
sl@0: 	}
sl@0: 	if(!ENGINE_remove(new_h2))
sl@0: 		{
sl@0: 		if(errno==ENOMEM)
sl@0:    	{
sl@0: 	  return 1;
sl@0: 	  }	
sl@0: 		fprintf(stdout,"Remove failed!\n");
sl@0: 		goto end;
sl@0: 		}
sl@0: 	display_engine_list();
sl@0: 	if(errno==ENOMEM)
sl@0:   {
sl@0: 	  return 1;
sl@0: 	}
sl@0: 	if(!ENGINE_add(new_h4))
sl@0: 		{
sl@0: 		if(errno==ENOMEM)
sl@0:    	{
sl@0: 	  return 1;
sl@0: 	  }	
sl@0: 		fprintf(stdout,"Add failed!\n");
sl@0: 		goto end;
sl@0: 		}
sl@0: 	display_engine_list();
sl@0: 	if(errno==ENOMEM)
sl@0:   {
sl@0: 	  return 1;
sl@0: 	}
sl@0: 	if(ENGINE_add(new_h3))
sl@0: 		{
sl@0: 		if(errno==ENOMEM)
sl@0:    	{
sl@0: 	  return 1;
sl@0: 	  }	
sl@0: 		fprintf(stdout,"Add *should* have failed but didn't!\n");
sl@0: 		goto end;
sl@0: 		}
sl@0: 	else
sl@0: 		fprintf(stdout,"Add that should fail did.\n");
sl@0: 	ERR_clear_error();
sl@0: 	if(errno==ENOMEM)
sl@0:   {
sl@0: 	  return 1;
sl@0: 	}
sl@0: 	if(ENGINE_remove(new_h2))
sl@0: 		{
sl@0: 		if(errno==ENOMEM)
sl@0:    	{
sl@0: 	  return 1;
sl@0: 	  }	
sl@0: 		fprintf(stdout,"Remove *should* have failed but didn't!\n");
sl@0: 		goto end;
sl@0: 		}
sl@0: 	else
sl@0: 		fprintf(stdout,"Remove that should fail did.\n");
sl@0: 	ERR_clear_error();
sl@0: 	if(errno==ENOMEM)
sl@0:   {
sl@0: 	  return 1;
sl@0: 	}
sl@0: 	if(!ENGINE_remove(new_h3))
sl@0: 		{
sl@0: 		if(errno==ENOMEM)
sl@0:    	{
sl@0: 	  return 1;
sl@0: 	  }	
sl@0: 		fprintf(stdout,"Remove failed!\n");
sl@0: 		goto end;
sl@0: 		}
sl@0: 	display_engine_list();
sl@0: 	if(errno==ENOMEM)
sl@0:   {
sl@0: 	  return 1;
sl@0: 	}
sl@0: 	if(!ENGINE_remove(new_h4))
sl@0: 		{
sl@0: 		if(errno==ENOMEM)
sl@0:    	{
sl@0: 	  return 1;
sl@0: 	  }	
sl@0: 		fprintf(stdout,"Remove failed!\n");
sl@0: 		goto end;
sl@0: 		}
sl@0: 	display_engine_list();
sl@0: 	if(errno==ENOMEM)
sl@0:   {
sl@0: 	  return 1;
sl@0: 	}
sl@0: 	/* Depending on whether there's any hardware support compiled
sl@0: 	 * in, this remove may be destined to fail. */
sl@0: 	ptr = ENGINE_get_first();
sl@0: 	if(errno==ENOMEM)
sl@0:   {
sl@0: 	  return 1;
sl@0: 	}
sl@0: 	if(ptr)
sl@0: 		if(!ENGINE_remove(ptr))
sl@0: 		{
sl@0: 			if(errno==ENOMEM)
sl@0:     	{
sl@0: 	      return 1;
sl@0: 	    }
sl@0: 			fprintf(stdout,"Remove failed!i - probably no hardware "
sl@0: 				"support present.\n");
sl@0: 		}		
sl@0: 	if (ptr)
sl@0: 	{
sl@0: 		ENGINE_free(ptr);
sl@0: 		if(errno==ENOMEM)
sl@0:    	{
sl@0: 	  return 1;
sl@0: 	  }
sl@0: 	}	
sl@0: 	display_engine_list();
sl@0: 	if(errno==ENOMEM)
sl@0:   {
sl@0: 	  return 1;
sl@0: 	}
sl@0: 	if(!ENGINE_add(new_h1) || !ENGINE_remove(new_h1))
sl@0: 		{
sl@0: 		if(errno==ENOMEM)
sl@0:    	{
sl@0: 	  return 1;
sl@0: 	  }	
sl@0: 		fprintf(stdout,"Couldn't add and remove to an empty list!\n");
sl@0: 		goto end;
sl@0: 		}
sl@0: 	else
sl@0: 		fprintf(stdout,"Successfully added and removed to an empty list!\n");
sl@0: 	fprintf(stdout,"About to beef up the engine-type list\n");
sl@0: 	for(loop = 0; loop < 512; loop++)
sl@0: 		{
sl@0: 		sprintf(buf, "id%i", loop);
sl@0: 		id = BUF_strdup(buf);
sl@0: 		if(errno==ENOMEM)
sl@0:    	{
sl@0: 	  return 1;
sl@0: 	  }
sl@0: 		sprintf(buf, "Fake engine type %i", loop);
sl@0: 		name = BUF_strdup(buf);
sl@0: 		if(errno==ENOMEM)
sl@0:    	{
sl@0: 	  return 1;
sl@0: 	  }
sl@0: 		if(((block[loop] = ENGINE_new()) == NULL) ||
sl@0: 				!ENGINE_set_id(block[loop], id) ||
sl@0: 				!ENGINE_set_name(block[loop], name))
sl@0: 			{
sl@0: 			if(errno==ENOMEM)
sl@0:      	{
sl@0: 	      return 1;
sl@0: 	    }	
sl@0: 			fprintf(stdout,"Couldn't create block of ENGINE structures.\n"
sl@0: 				"I'll probably also core-dump now, damn.\n");
sl@0: 			goto end;
sl@0: 			}
sl@0: 		}
sl@0: 	for(loop = 0; loop < 512; loop++)
sl@0: 		{
sl@0: 		if(!ENGINE_add(block[loop]))
sl@0: 			{
sl@0: 			if(errno==ENOMEM)
sl@0:      	{
sl@0: 	     return 1;
sl@0: 	    } 	
sl@0: 			fprintf(stdout,"\nAdding stopped at %i, (%s,%s)\n",
sl@0: 				loop, ENGINE_get_id(block[loop]),
sl@0: 				ENGINE_get_name(block[loop]));
sl@0: 			if(errno==ENOMEM)
sl@0:      	{
sl@0: 	     return 1;
sl@0: 	    } 	
sl@0: 						
sl@0: 			goto cleanup_loop;
sl@0: 			}
sl@0: 		else
sl@0: 			fprintf(stdout,"."); fflush(stdout);
sl@0: 		}
sl@0: cleanup_loop:
sl@0: 	fprintf(stdout,"\nAbout to empty the engine-type list\n");
sl@0: 	while((ptr = ENGINE_get_first()) != NULL)
sl@0: 		{
sl@0: 			if(errno==ENOMEM)
sl@0:    	 {
sl@0: 	    return 1;
sl@0: 	   }
sl@0: 		if(!ENGINE_remove(ptr))
sl@0: 			{
sl@0: 			if(errno==ENOMEM)
sl@0:      	{
sl@0: 	      return 1;
sl@0: 	    }
sl@0: 			fprintf(stdout,"\nRemove failed!\n");
sl@0: 			goto end;
sl@0: 			}
sl@0: 		ENGINE_free(ptr);
sl@0: 		if(errno==ENOMEM)
sl@0:    	{
sl@0: 	  return 1;
sl@0: 	  }
sl@0: 		fprintf(stdout,"."); fflush(stdout);
sl@0: 		}
sl@0: 	for(loop = 0; loop < 512; loop++)
sl@0: 		{
sl@0: 		OPENSSL_free((void *)ENGINE_get_id(block[loop]));
sl@0: 		if(errno==ENOMEM)
sl@0:      	{
sl@0: 	     return 1;
sl@0: 	    } 	
sl@0: 		
sl@0: 		OPENSSL_free((void *)ENGINE_get_name(block[loop]));
sl@0: 			if(errno==ENOMEM)
sl@0:      	{
sl@0: 	     return 1;
sl@0: 	    } 	
sl@0: 		
sl@0: 		}
sl@0: 	fprintf(stdout,"\nTests completed happily\n");
sl@0: 	to_return = 0;
sl@0: end:
sl@0: 	if(to_return)
sl@0: 	{
sl@0: 		ERR_print_errors_fp(stderr);
sl@0: 		if(errno==ENOMEM)
sl@0:    	{
sl@0: 	  return 1;
sl@0: 	  }
sl@0: 	}	
sl@0: 	if(new_h1)
sl@0: 	{ ENGINE_free(new_h1);
sl@0: 		if(errno==ENOMEM)
sl@0:    	{
sl@0: 	  return 1;
sl@0: 	  }
sl@0: 	}  
sl@0: 	if(new_h2) 
sl@0: 	{
sl@0: 		ENGINE_free(new_h2);
sl@0: 		if(errno==ENOMEM)
sl@0:    	{
sl@0: 	  return 1;
sl@0: 	  }
sl@0: 	}  
sl@0: 	if(new_h3)
sl@0: 	{ 
sl@0: 		ENGINE_free(new_h3);
sl@0: 		if(errno==ENOMEM)
sl@0:    	{
sl@0: 	  return 1;
sl@0: 	  }
sl@0: 	}  
sl@0: 	if(new_h4)
sl@0: 	{
sl@0: 		 ENGINE_free(new_h4);
sl@0: 		 if(errno==ENOMEM)
sl@0:    	{
sl@0: 	  return 1;
sl@0: 	  }
sl@0: 	}  
sl@0: 	for(loop = 0; loop < 512; loop++)
sl@0: 		if(block[loop])
sl@0: 		{
sl@0: 			ENGINE_free(block[loop]);
sl@0: 			if(errno==ENOMEM)
sl@0:    	 {
sl@0: 	    return 1;
sl@0: 	   }
sl@0: 		}	
sl@0: 	ENGINE_cleanup();
sl@0: 	if(errno==ENOMEM)
sl@0:   {
sl@0: 	  return 1;
sl@0: 	}
sl@0: 	CRYPTO_cleanup_all_ex_data();
sl@0: 	if(errno==ENOMEM)
sl@0:   {
sl@0: 	  return 1;
sl@0: 	}
sl@0: 	ERR_free_strings();
sl@0: 	if(errno==ENOMEM)
sl@0:  	{
sl@0: 	  return 1;
sl@0: 	}
sl@0: 	ERR_remove_state(0);
sl@0: 	if(errno==ENOMEM)
sl@0:   {
sl@0:   		  return 1;
sl@0: 	}
sl@0: 	CRYPTO_mem_leaks_fp(stderr);
sl@0: 	if(errno==ENOMEM)
sl@0:   {
sl@0: 	  return 1;
sl@0: 	}
sl@0: 	return to_return;
sl@0: 	}
sl@0: #endif