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