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