xref: /onnv-gate/usr/src/common/openssl/crypto/engine/enginetest.c (revision 0:68f95e015346)
1*0Sstevel@tonic-gate /* crypto/engine/enginetest.c */
2*0Sstevel@tonic-gate /* Written by Geoff Thorpe (geoff@geoffthorpe.net) for the OpenSSL
3*0Sstevel@tonic-gate  * project 2000.
4*0Sstevel@tonic-gate  */
5*0Sstevel@tonic-gate /* ====================================================================
6*0Sstevel@tonic-gate  * Copyright (c) 1999-2001 The OpenSSL Project.  All rights reserved.
7*0Sstevel@tonic-gate  *
8*0Sstevel@tonic-gate  * Redistribution and use in source and binary forms, with or without
9*0Sstevel@tonic-gate  * modification, are permitted provided that the following conditions
10*0Sstevel@tonic-gate  * are met:
11*0Sstevel@tonic-gate  *
12*0Sstevel@tonic-gate  * 1. Redistributions of source code must retain the above copyright
13*0Sstevel@tonic-gate  *    notice, this list of conditions and the following disclaimer.
14*0Sstevel@tonic-gate  *
15*0Sstevel@tonic-gate  * 2. Redistributions in binary form must reproduce the above copyright
16*0Sstevel@tonic-gate  *    notice, this list of conditions and the following disclaimer in
17*0Sstevel@tonic-gate  *    the documentation and/or other materials provided with the
18*0Sstevel@tonic-gate  *    distribution.
19*0Sstevel@tonic-gate  *
20*0Sstevel@tonic-gate  * 3. All advertising materials mentioning features or use of this
21*0Sstevel@tonic-gate  *    software must display the following acknowledgment:
22*0Sstevel@tonic-gate  *    "This product includes software developed by the OpenSSL Project
23*0Sstevel@tonic-gate  *    for use in the OpenSSL Toolkit. (http://www.OpenSSL.org/)"
24*0Sstevel@tonic-gate  *
25*0Sstevel@tonic-gate  * 4. The names "OpenSSL Toolkit" and "OpenSSL Project" must not be used to
26*0Sstevel@tonic-gate  *    endorse or promote products derived from this software without
27*0Sstevel@tonic-gate  *    prior written permission. For written permission, please contact
28*0Sstevel@tonic-gate  *    licensing@OpenSSL.org.
29*0Sstevel@tonic-gate  *
30*0Sstevel@tonic-gate  * 5. Products derived from this software may not be called "OpenSSL"
31*0Sstevel@tonic-gate  *    nor may "OpenSSL" appear in their names without prior written
32*0Sstevel@tonic-gate  *    permission of the OpenSSL Project.
33*0Sstevel@tonic-gate  *
34*0Sstevel@tonic-gate  * 6. Redistributions of any form whatsoever must retain the following
35*0Sstevel@tonic-gate  *    acknowledgment:
36*0Sstevel@tonic-gate  *    "This product includes software developed by the OpenSSL Project
37*0Sstevel@tonic-gate  *    for use in the OpenSSL Toolkit (http://www.OpenSSL.org/)"
38*0Sstevel@tonic-gate  *
39*0Sstevel@tonic-gate  * THIS SOFTWARE IS PROVIDED BY THE OpenSSL PROJECT ``AS IS'' AND ANY
40*0Sstevel@tonic-gate  * EXPRESSED OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
41*0Sstevel@tonic-gate  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
42*0Sstevel@tonic-gate  * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE OpenSSL PROJECT OR
43*0Sstevel@tonic-gate  * ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
44*0Sstevel@tonic-gate  * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
45*0Sstevel@tonic-gate  * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
46*0Sstevel@tonic-gate  * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
47*0Sstevel@tonic-gate  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
48*0Sstevel@tonic-gate  * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
49*0Sstevel@tonic-gate  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED
50*0Sstevel@tonic-gate  * OF THE POSSIBILITY OF SUCH DAMAGE.
51*0Sstevel@tonic-gate  * ====================================================================
52*0Sstevel@tonic-gate  *
53*0Sstevel@tonic-gate  * This product includes cryptographic software written by Eric Young
54*0Sstevel@tonic-gate  * (eay@cryptsoft.com).  This product includes software written by Tim
55*0Sstevel@tonic-gate  * Hudson (tjh@cryptsoft.com).
56*0Sstevel@tonic-gate  *
57*0Sstevel@tonic-gate  */
58*0Sstevel@tonic-gate 
59*0Sstevel@tonic-gate #include <stdio.h>
60*0Sstevel@tonic-gate #include <string.h>
61*0Sstevel@tonic-gate 
62*0Sstevel@tonic-gate #ifdef OPENSSL_NO_ENGINE
63*0Sstevel@tonic-gate int main(int argc, char *argv[])
64*0Sstevel@tonic-gate {
65*0Sstevel@tonic-gate     printf("No ENGINE support\n");
66*0Sstevel@tonic-gate     return(0);
67*0Sstevel@tonic-gate }
68*0Sstevel@tonic-gate #else
69*0Sstevel@tonic-gate #include <openssl/e_os2.h>
70*0Sstevel@tonic-gate #include <openssl/buffer.h>
71*0Sstevel@tonic-gate #include <openssl/crypto.h>
72*0Sstevel@tonic-gate #include <openssl/engine.h>
73*0Sstevel@tonic-gate #include <openssl/err.h>
74*0Sstevel@tonic-gate 
75*0Sstevel@tonic-gate static void display_engine_list()
76*0Sstevel@tonic-gate 	{
77*0Sstevel@tonic-gate 	ENGINE *h;
78*0Sstevel@tonic-gate 	int loop;
79*0Sstevel@tonic-gate 
80*0Sstevel@tonic-gate 	h = ENGINE_get_first();
81*0Sstevel@tonic-gate 	loop = 0;
82*0Sstevel@tonic-gate 	printf("listing available engine types\n");
83*0Sstevel@tonic-gate 	while(h)
84*0Sstevel@tonic-gate 		{
85*0Sstevel@tonic-gate 		printf("engine %i, id = \"%s\", name = \"%s\"\n",
86*0Sstevel@tonic-gate 			loop++, ENGINE_get_id(h), ENGINE_get_name(h));
87*0Sstevel@tonic-gate 		h = ENGINE_get_next(h);
88*0Sstevel@tonic-gate 		}
89*0Sstevel@tonic-gate 	printf("end of list\n");
90*0Sstevel@tonic-gate 	/* ENGINE_get_first() increases the struct_ref counter, so we
91*0Sstevel@tonic-gate            must call ENGINE_free() to decrease it again */
92*0Sstevel@tonic-gate 	ENGINE_free(h);
93*0Sstevel@tonic-gate 	}
94*0Sstevel@tonic-gate 
95*0Sstevel@tonic-gate int main(int argc, char *argv[])
96*0Sstevel@tonic-gate 	{
97*0Sstevel@tonic-gate 	ENGINE *block[512];
98*0Sstevel@tonic-gate 	char buf[256];
99*0Sstevel@tonic-gate 	const char *id, *name;
100*0Sstevel@tonic-gate 	ENGINE *ptr;
101*0Sstevel@tonic-gate 	int loop;
102*0Sstevel@tonic-gate 	int to_return = 1;
103*0Sstevel@tonic-gate 	ENGINE *new_h1 = NULL;
104*0Sstevel@tonic-gate 	ENGINE *new_h2 = NULL;
105*0Sstevel@tonic-gate 	ENGINE *new_h3 = NULL;
106*0Sstevel@tonic-gate 	ENGINE *new_h4 = NULL;
107*0Sstevel@tonic-gate 
108*0Sstevel@tonic-gate 	/* enable memory leak checking unless explicitly disabled */
109*0Sstevel@tonic-gate 	if (!((getenv("OPENSSL_DEBUG_MEMORY") != NULL) && (0 == strcmp(getenv("OPENSSL_DEBUG_MEMORY"), "off"))))
110*0Sstevel@tonic-gate 		{
111*0Sstevel@tonic-gate 		CRYPTO_malloc_debug_init();
112*0Sstevel@tonic-gate 		CRYPTO_set_mem_debug_options(V_CRYPTO_MDEBUG_ALL);
113*0Sstevel@tonic-gate 		}
114*0Sstevel@tonic-gate 	else
115*0Sstevel@tonic-gate 		{
116*0Sstevel@tonic-gate 		/* OPENSSL_DEBUG_MEMORY=off */
117*0Sstevel@tonic-gate 		CRYPTO_set_mem_debug_functions(0, 0, 0, 0, 0);
118*0Sstevel@tonic-gate 		}
119*0Sstevel@tonic-gate 	CRYPTO_mem_ctrl(CRYPTO_MEM_CHECK_ON);
120*0Sstevel@tonic-gate 	ERR_load_crypto_strings();
121*0Sstevel@tonic-gate 
122*0Sstevel@tonic-gate 	memset(block, 0, 512 * sizeof(ENGINE *));
123*0Sstevel@tonic-gate 	if(((new_h1 = ENGINE_new()) == NULL) ||
124*0Sstevel@tonic-gate 			!ENGINE_set_id(new_h1, "test_id0") ||
125*0Sstevel@tonic-gate 			!ENGINE_set_name(new_h1, "First test item") ||
126*0Sstevel@tonic-gate 			((new_h2 = ENGINE_new()) == NULL) ||
127*0Sstevel@tonic-gate 			!ENGINE_set_id(new_h2, "test_id1") ||
128*0Sstevel@tonic-gate 			!ENGINE_set_name(new_h2, "Second test item") ||
129*0Sstevel@tonic-gate 			((new_h3 = ENGINE_new()) == NULL) ||
130*0Sstevel@tonic-gate 			!ENGINE_set_id(new_h3, "test_id2") ||
131*0Sstevel@tonic-gate 			!ENGINE_set_name(new_h3, "Third test item") ||
132*0Sstevel@tonic-gate 			((new_h4 = ENGINE_new()) == NULL) ||
133*0Sstevel@tonic-gate 			!ENGINE_set_id(new_h4, "test_id3") ||
134*0Sstevel@tonic-gate 			!ENGINE_set_name(new_h4, "Fourth test item"))
135*0Sstevel@tonic-gate 		{
136*0Sstevel@tonic-gate 		printf("Couldn't set up test ENGINE structures\n");
137*0Sstevel@tonic-gate 		goto end;
138*0Sstevel@tonic-gate 		}
139*0Sstevel@tonic-gate 	printf("\nenginetest beginning\n\n");
140*0Sstevel@tonic-gate 	display_engine_list();
141*0Sstevel@tonic-gate 	if(!ENGINE_add(new_h1))
142*0Sstevel@tonic-gate 		{
143*0Sstevel@tonic-gate 		printf("Add failed!\n");
144*0Sstevel@tonic-gate 		goto end;
145*0Sstevel@tonic-gate 		}
146*0Sstevel@tonic-gate 	display_engine_list();
147*0Sstevel@tonic-gate 	ptr = ENGINE_get_first();
148*0Sstevel@tonic-gate 	if(!ENGINE_remove(ptr))
149*0Sstevel@tonic-gate 		{
150*0Sstevel@tonic-gate 		printf("Remove failed!\n");
151*0Sstevel@tonic-gate 		goto end;
152*0Sstevel@tonic-gate 		}
153*0Sstevel@tonic-gate 	if (ptr)
154*0Sstevel@tonic-gate 		ENGINE_free(ptr);
155*0Sstevel@tonic-gate 	display_engine_list();
156*0Sstevel@tonic-gate 	if(!ENGINE_add(new_h3) || !ENGINE_add(new_h2))
157*0Sstevel@tonic-gate 		{
158*0Sstevel@tonic-gate 		printf("Add failed!\n");
159*0Sstevel@tonic-gate 		goto end;
160*0Sstevel@tonic-gate 		}
161*0Sstevel@tonic-gate 	display_engine_list();
162*0Sstevel@tonic-gate 	if(!ENGINE_remove(new_h2))
163*0Sstevel@tonic-gate 		{
164*0Sstevel@tonic-gate 		printf("Remove failed!\n");
165*0Sstevel@tonic-gate 		goto end;
166*0Sstevel@tonic-gate 		}
167*0Sstevel@tonic-gate 	display_engine_list();
168*0Sstevel@tonic-gate 	if(!ENGINE_add(new_h4))
169*0Sstevel@tonic-gate 		{
170*0Sstevel@tonic-gate 		printf("Add failed!\n");
171*0Sstevel@tonic-gate 		goto end;
172*0Sstevel@tonic-gate 		}
173*0Sstevel@tonic-gate 	display_engine_list();
174*0Sstevel@tonic-gate 	if(ENGINE_add(new_h3))
175*0Sstevel@tonic-gate 		{
176*0Sstevel@tonic-gate 		printf("Add *should* have failed but didn't!\n");
177*0Sstevel@tonic-gate 		goto end;
178*0Sstevel@tonic-gate 		}
179*0Sstevel@tonic-gate 	else
180*0Sstevel@tonic-gate 		printf("Add that should fail did.\n");
181*0Sstevel@tonic-gate 	ERR_clear_error();
182*0Sstevel@tonic-gate 	if(ENGINE_remove(new_h2))
183*0Sstevel@tonic-gate 		{
184*0Sstevel@tonic-gate 		printf("Remove *should* have failed but didn't!\n");
185*0Sstevel@tonic-gate 		goto end;
186*0Sstevel@tonic-gate 		}
187*0Sstevel@tonic-gate 	else
188*0Sstevel@tonic-gate 		printf("Remove that should fail did.\n");
189*0Sstevel@tonic-gate 	ERR_clear_error();
190*0Sstevel@tonic-gate 	if(!ENGINE_remove(new_h3))
191*0Sstevel@tonic-gate 		{
192*0Sstevel@tonic-gate 		printf("Remove failed!\n");
193*0Sstevel@tonic-gate 		goto end;
194*0Sstevel@tonic-gate 		}
195*0Sstevel@tonic-gate 	display_engine_list();
196*0Sstevel@tonic-gate 	if(!ENGINE_remove(new_h4))
197*0Sstevel@tonic-gate 		{
198*0Sstevel@tonic-gate 		printf("Remove failed!\n");
199*0Sstevel@tonic-gate 		goto end;
200*0Sstevel@tonic-gate 		}
201*0Sstevel@tonic-gate 	display_engine_list();
202*0Sstevel@tonic-gate 	/* Depending on whether there's any hardware support compiled
203*0Sstevel@tonic-gate 	 * in, this remove may be destined to fail. */
204*0Sstevel@tonic-gate 	ptr = ENGINE_get_first();
205*0Sstevel@tonic-gate 	if(ptr)
206*0Sstevel@tonic-gate 		if(!ENGINE_remove(ptr))
207*0Sstevel@tonic-gate 			printf("Remove failed!i - probably no hardware "
208*0Sstevel@tonic-gate 				"support present.\n");
209*0Sstevel@tonic-gate 	if (ptr)
210*0Sstevel@tonic-gate 		ENGINE_free(ptr);
211*0Sstevel@tonic-gate 	display_engine_list();
212*0Sstevel@tonic-gate 	if(!ENGINE_add(new_h1) || !ENGINE_remove(new_h1))
213*0Sstevel@tonic-gate 		{
214*0Sstevel@tonic-gate 		printf("Couldn't add and remove to an empty list!\n");
215*0Sstevel@tonic-gate 		goto end;
216*0Sstevel@tonic-gate 		}
217*0Sstevel@tonic-gate 	else
218*0Sstevel@tonic-gate 		printf("Successfully added and removed to an empty list!\n");
219*0Sstevel@tonic-gate 	printf("About to beef up the engine-type list\n");
220*0Sstevel@tonic-gate 	for(loop = 0; loop < 512; loop++)
221*0Sstevel@tonic-gate 		{
222*0Sstevel@tonic-gate 		sprintf(buf, "id%i", loop);
223*0Sstevel@tonic-gate 		id = BUF_strdup(buf);
224*0Sstevel@tonic-gate 		sprintf(buf, "Fake engine type %i", loop);
225*0Sstevel@tonic-gate 		name = BUF_strdup(buf);
226*0Sstevel@tonic-gate 		if(((block[loop] = ENGINE_new()) == NULL) ||
227*0Sstevel@tonic-gate 				!ENGINE_set_id(block[loop], id) ||
228*0Sstevel@tonic-gate 				!ENGINE_set_name(block[loop], name))
229*0Sstevel@tonic-gate 			{
230*0Sstevel@tonic-gate 			printf("Couldn't create block of ENGINE structures.\n"
231*0Sstevel@tonic-gate 				"I'll probably also core-dump now, damn.\n");
232*0Sstevel@tonic-gate 			goto end;
233*0Sstevel@tonic-gate 			}
234*0Sstevel@tonic-gate 		}
235*0Sstevel@tonic-gate 	for(loop = 0; loop < 512; loop++)
236*0Sstevel@tonic-gate 		{
237*0Sstevel@tonic-gate 		if(!ENGINE_add(block[loop]))
238*0Sstevel@tonic-gate 			{
239*0Sstevel@tonic-gate 			printf("\nAdding stopped at %i, (%s,%s)\n",
240*0Sstevel@tonic-gate 				loop, ENGINE_get_id(block[loop]),
241*0Sstevel@tonic-gate 				ENGINE_get_name(block[loop]));
242*0Sstevel@tonic-gate 			goto cleanup_loop;
243*0Sstevel@tonic-gate 			}
244*0Sstevel@tonic-gate 		else
245*0Sstevel@tonic-gate 			printf("."); fflush(stdout);
246*0Sstevel@tonic-gate 		}
247*0Sstevel@tonic-gate cleanup_loop:
248*0Sstevel@tonic-gate 	printf("\nAbout to empty the engine-type list\n");
249*0Sstevel@tonic-gate 	while((ptr = ENGINE_get_first()) != NULL)
250*0Sstevel@tonic-gate 		{
251*0Sstevel@tonic-gate 		if(!ENGINE_remove(ptr))
252*0Sstevel@tonic-gate 			{
253*0Sstevel@tonic-gate 			printf("\nRemove failed!\n");
254*0Sstevel@tonic-gate 			goto end;
255*0Sstevel@tonic-gate 			}
256*0Sstevel@tonic-gate 		ENGINE_free(ptr);
257*0Sstevel@tonic-gate 		printf("."); fflush(stdout);
258*0Sstevel@tonic-gate 		}
259*0Sstevel@tonic-gate 	for(loop = 0; loop < 512; loop++)
260*0Sstevel@tonic-gate 		{
261*0Sstevel@tonic-gate 		OPENSSL_free((void *)ENGINE_get_id(block[loop]));
262*0Sstevel@tonic-gate 		OPENSSL_free((void *)ENGINE_get_name(block[loop]));
263*0Sstevel@tonic-gate 		}
264*0Sstevel@tonic-gate 	printf("\nTests completed happily\n");
265*0Sstevel@tonic-gate 	to_return = 0;
266*0Sstevel@tonic-gate end:
267*0Sstevel@tonic-gate 	if(to_return)
268*0Sstevel@tonic-gate 		ERR_print_errors_fp(stderr);
269*0Sstevel@tonic-gate 	if(new_h1) ENGINE_free(new_h1);
270*0Sstevel@tonic-gate 	if(new_h2) ENGINE_free(new_h2);
271*0Sstevel@tonic-gate 	if(new_h3) ENGINE_free(new_h3);
272*0Sstevel@tonic-gate 	if(new_h4) ENGINE_free(new_h4);
273*0Sstevel@tonic-gate 	for(loop = 0; loop < 512; loop++)
274*0Sstevel@tonic-gate 		if(block[loop])
275*0Sstevel@tonic-gate 			ENGINE_free(block[loop]);
276*0Sstevel@tonic-gate 	ENGINE_cleanup();
277*0Sstevel@tonic-gate 	CRYPTO_cleanup_all_ex_data();
278*0Sstevel@tonic-gate 	ERR_free_strings();
279*0Sstevel@tonic-gate 	ERR_remove_state(0);
280*0Sstevel@tonic-gate 	CRYPTO_mem_leaks_fp(stderr);
281*0Sstevel@tonic-gate 	return to_return;
282*0Sstevel@tonic-gate 	}
283*0Sstevel@tonic-gate #endif
284