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 #include <stdint.h>
60 #include <stdio.h>
61 #include <string.h>
62 #include <openssl/e_os2.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/buffer.h>
72 #include <openssl/crypto.h>
73 #include <openssl/engine.h>
74 #include <openssl/err.h>
75 
76 static void display_engine_list(void)
77 	{
78 	ENGINE *h;
79 	int loop;
80 
81 	h = ENGINE_get_first();
82 	loop = 0;
83 	printf("listing available engine types\n");
84 	while(h)
85 		{
86 		printf("engine %i, id = \"%s\", name = \"%s\"\n",
87 			loop++, ENGINE_get_id(h), ENGINE_get_name(h));
88 		h = ENGINE_get_next(h);
89 		}
90 	printf("end of list\n");
91 	/* ENGINE_get_first() increases the struct_ref counter, so we
92            must call ENGINE_free() to decrease it again */
93 	ENGINE_free(h);
94 	}
95 
96 int main(int argc, char *argv[])
97 	{
98 	ENGINE *block[512];
99 	char buf[256];
100 	const char *id, *name;
101 	ENGINE *ptr;
102 	int loop;
103 	int to_return = 1;
104 	ENGINE *new_h1 = NULL;
105 	ENGINE *new_h2 = NULL;
106 	ENGINE *new_h3 = NULL;
107 	ENGINE *new_h4 = NULL;
108 
109 	/* enable memory leak checking unless explicitly disabled */
110 	if (!((getenv("OPENSSL_DEBUG_MEMORY") != NULL) && (0 == strcmp(getenv("OPENSSL_DEBUG_MEMORY"), "off"))))
111 		{
112 		CRYPTO_malloc_debug_init();
113 		CRYPTO_set_mem_debug_options(V_CRYPTO_MDEBUG_ALL);
114 		}
115 	else
116 		{
117 		/* OPENSSL_DEBUG_MEMORY=off */
118 		CRYPTO_set_mem_debug_functions(0, 0, 0, 0, 0);
119 		}
120 	CRYPTO_mem_ctrl(CRYPTO_MEM_CHECK_ON);
121 	ERR_load_crypto_strings();
122 
123 	memset(block, 0, 512 * sizeof(ENGINE *));
124 	if(((new_h1 = ENGINE_new()) == NULL) ||
125 			!ENGINE_set_id(new_h1, "test_id0") ||
126 			!ENGINE_set_name(new_h1, "First test item") ||
127 			((new_h2 = ENGINE_new()) == NULL) ||
128 			!ENGINE_set_id(new_h2, "test_id1") ||
129 			!ENGINE_set_name(new_h2, "Second test item") ||
130 			((new_h3 = ENGINE_new()) == NULL) ||
131 			!ENGINE_set_id(new_h3, "test_id2") ||
132 			!ENGINE_set_name(new_h3, "Third test item") ||
133 			((new_h4 = ENGINE_new()) == NULL) ||
134 			!ENGINE_set_id(new_h4, "test_id3") ||
135 			!ENGINE_set_name(new_h4, "Fourth test item"))
136 		{
137 		printf("Couldn't set up test ENGINE structures\n");
138 		goto end;
139 		}
140 	printf("\nenginetest beginning\n\n");
141 	display_engine_list();
142 	if(!ENGINE_add(new_h1))
143 		{
144 		printf("Add failed!\n");
145 		goto end;
146 		}
147 	display_engine_list();
148 	ptr = ENGINE_get_first();
149 	if(!ENGINE_remove(ptr))
150 		{
151 		printf("Remove failed!\n");
152 		goto end;
153 		}
154 	if (ptr)
155 		ENGINE_free(ptr);
156 	display_engine_list();
157 	if(!ENGINE_add(new_h3) || !ENGINE_add(new_h2))
158 		{
159 		printf("Add failed!\n");
160 		goto end;
161 		}
162 	display_engine_list();
163 	if(!ENGINE_remove(new_h2))
164 		{
165 		printf("Remove failed!\n");
166 		goto end;
167 		}
168 	display_engine_list();
169 	if(!ENGINE_add(new_h4))
170 		{
171 		printf("Add failed!\n");
172 		goto end;
173 		}
174 	display_engine_list();
175 	if(ENGINE_add(new_h3))
176 		{
177 		printf("Add *should* have failed but didn't!\n");
178 		goto end;
179 		}
180 	else
181 		printf("Add that should fail did.\n");
182 	ERR_clear_error();
183 	if(ENGINE_remove(new_h2))
184 		{
185 		printf("Remove *should* have failed but didn't!\n");
186 		goto end;
187 		}
188 	else
189 		printf("Remove that should fail did.\n");
190 	ERR_clear_error();
191 	if(!ENGINE_remove(new_h3))
192 		{
193 		printf("Remove failed!\n");
194 		goto end;
195 		}
196 	display_engine_list();
197 	if(!ENGINE_remove(new_h4))
198 		{
199 		printf("Remove failed!\n");
200 		goto end;
201 		}
202 	display_engine_list();
203 	/* Depending on whether there's any hardware support compiled
204 	 * in, this remove may be destined to fail. */
205 	ptr = ENGINE_get_first();
206 	if(ptr)
207 		if(!ENGINE_remove(ptr))
208 			printf("Remove failed!i - probably no hardware "
209 				"support present.\n");
210 	if (ptr)
211 		ENGINE_free(ptr);
212 	display_engine_list();
213 	if(!ENGINE_add(new_h1) || !ENGINE_remove(new_h1))
214 		{
215 		printf("Couldn't add and remove to an empty list!\n");
216 		goto end;
217 		}
218 	else
219 		printf("Successfully added and removed to an empty list!\n");
220 	printf("About to beef up the engine-type list\n");
221 	for(loop = 0; loop < 512; loop++)
222 		{
223 		sprintf(buf, "id%i", loop);
224 		id = BUF_strdup(buf);
225 		sprintf(buf, "Fake engine type %i", loop);
226 		name = BUF_strdup(buf);
227 		if(((block[loop] = ENGINE_new()) == NULL) ||
228 				!ENGINE_set_id(block[loop], id) ||
229 				!ENGINE_set_name(block[loop], name))
230 			{
231 			printf("Couldn't create block of ENGINE structures.\n"
232 				"I'll probably also core-dump now, damn.\n");
233 			goto end;
234 			}
235 		}
236 	for(loop = 0; loop < 512; loop++)
237 		{
238 		if(!ENGINE_add(block[loop]))
239 			{
240 			printf("\nAdding stopped at %i, (%s,%s)\n",
241 				loop, ENGINE_get_id(block[loop]),
242 				ENGINE_get_name(block[loop]));
243 			goto cleanup_loop;
244 			}
245 		else
246 			printf("."); fflush(stdout);
247 		}
248 cleanup_loop:
249 	printf("\nAbout to empty the engine-type list\n");
250 	while((ptr = ENGINE_get_first()) != NULL)
251 		{
252 		if(!ENGINE_remove(ptr))
253 			{
254 			printf("\nRemove failed!\n");
255 			goto end;
256 			}
257 		ENGINE_free(ptr);
258 		printf("."); fflush(stdout);
259 		}
260 	for(loop = 0; loop < 512; loop++)
261 		{
262 		OPENSSL_free((void *)(intptr_t)ENGINE_get_id(block[loop]));
263 		OPENSSL_free((void *)(intptr_t)ENGINE_get_name(block[loop]));
264 		}
265 	printf("\nTests completed happily\n");
266 	to_return = 0;
267 end:
268 	if(to_return)
269 		ERR_print_errors_fp(stderr);
270 	if(new_h1) ENGINE_free(new_h1);
271 	if(new_h2) ENGINE_free(new_h2);
272 	if(new_h3) ENGINE_free(new_h3);
273 	if(new_h4) ENGINE_free(new_h4);
274 	for(loop = 0; loop < 512; loop++)
275 		if(block[loop])
276 			ENGINE_free(block[loop]);
277 	ENGINE_cleanup();
278 	CRYPTO_cleanup_all_ex_data();
279 	ERR_free_strings();
280 	ERR_remove_thread_state(NULL);
281 	CRYPTO_mem_leaks_fp(stderr);
282 	return to_return;
283 	}
284 #endif
285