1109998Smarkm/* crypto/engine/enginetest.c */ 2296341Sdelphij/* 3296341Sdelphij * Written by Geoff Thorpe (geoff@geoffthorpe.net) for the OpenSSL project 4296341Sdelphij * 2000. 5109998Smarkm */ 6109998Smarkm/* ==================================================================== 7109998Smarkm * Copyright (c) 1999-2001 The OpenSSL Project. All rights reserved. 8109998Smarkm * 9109998Smarkm * Redistribution and use in source and binary forms, with or without 10109998Smarkm * modification, are permitted provided that the following conditions 11109998Smarkm * are met: 12109998Smarkm * 13109998Smarkm * 1. Redistributions of source code must retain the above copyright 14296341Sdelphij * notice, this list of conditions and the following disclaimer. 15109998Smarkm * 16109998Smarkm * 2. Redistributions in binary form must reproduce the above copyright 17109998Smarkm * notice, this list of conditions and the following disclaimer in 18109998Smarkm * the documentation and/or other materials provided with the 19109998Smarkm * distribution. 20109998Smarkm * 21109998Smarkm * 3. All advertising materials mentioning features or use of this 22109998Smarkm * software must display the following acknowledgment: 23109998Smarkm * "This product includes software developed by the OpenSSL Project 24109998Smarkm * for use in the OpenSSL Toolkit. (http://www.OpenSSL.org/)" 25109998Smarkm * 26109998Smarkm * 4. The names "OpenSSL Toolkit" and "OpenSSL Project" must not be used to 27109998Smarkm * endorse or promote products derived from this software without 28109998Smarkm * prior written permission. For written permission, please contact 29109998Smarkm * licensing@OpenSSL.org. 30109998Smarkm * 31109998Smarkm * 5. Products derived from this software may not be called "OpenSSL" 32109998Smarkm * nor may "OpenSSL" appear in their names without prior written 33109998Smarkm * permission of the OpenSSL Project. 34109998Smarkm * 35109998Smarkm * 6. Redistributions of any form whatsoever must retain the following 36109998Smarkm * acknowledgment: 37109998Smarkm * "This product includes software developed by the OpenSSL Project 38109998Smarkm * for use in the OpenSSL Toolkit (http://www.OpenSSL.org/)" 39109998Smarkm * 40109998Smarkm * THIS SOFTWARE IS PROVIDED BY THE OpenSSL PROJECT ``AS IS'' AND ANY 41109998Smarkm * EXPRESSED OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 42109998Smarkm * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR 43109998Smarkm * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE OpenSSL PROJECT OR 44109998Smarkm * ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, 45109998Smarkm * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT 46109998Smarkm * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; 47109998Smarkm * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 48109998Smarkm * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, 49109998Smarkm * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) 50109998Smarkm * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED 51109998Smarkm * OF THE POSSIBILITY OF SUCH DAMAGE. 52109998Smarkm * ==================================================================== 53109998Smarkm * 54109998Smarkm * This product includes cryptographic software written by Eric Young 55109998Smarkm * (eay@cryptsoft.com). This product includes software written by Tim 56109998Smarkm * Hudson (tjh@cryptsoft.com). 57109998Smarkm * 58109998Smarkm */ 59109998Smarkm 60109998Smarkm#include <stdio.h> 61109998Smarkm#include <string.h> 62194206Ssimon#include <openssl/e_os2.h> 63111147Snectar 64111147Snectar#ifdef OPENSSL_NO_ENGINE 65111147Snectarint main(int argc, char *argv[]) 66111147Snectar{ 67111147Snectar printf("No ENGINE support\n"); 68296341Sdelphij return (0); 69111147Snectar} 70111147Snectar#else 71296341Sdelphij# include <openssl/buffer.h> 72296341Sdelphij# include <openssl/crypto.h> 73296341Sdelphij# include <openssl/engine.h> 74296341Sdelphij# include <openssl/err.h> 75109998Smarkm 76160814Ssimonstatic void display_engine_list(void) 77296341Sdelphij{ 78296341Sdelphij ENGINE *h; 79296341Sdelphij int loop; 80109998Smarkm 81296341Sdelphij h = ENGINE_get_first(); 82296341Sdelphij loop = 0; 83296341Sdelphij printf("listing available engine types\n"); 84296341Sdelphij while (h) { 85296341Sdelphij printf("engine %i, id = \"%s\", name = \"%s\"\n", 86296341Sdelphij loop++, ENGINE_get_id(h), ENGINE_get_name(h)); 87296341Sdelphij h = ENGINE_get_next(h); 88296341Sdelphij } 89296341Sdelphij printf("end of list\n"); 90296341Sdelphij /* 91296341Sdelphij * ENGINE_get_first() increases the struct_ref counter, so we must call 92296341Sdelphij * ENGINE_free() to decrease it again 93296341Sdelphij */ 94296341Sdelphij ENGINE_free(h); 95296341Sdelphij} 96109998Smarkm 97109998Smarkmint main(int argc, char *argv[]) 98296341Sdelphij{ 99296341Sdelphij ENGINE *block[512]; 100296341Sdelphij char buf[256]; 101296341Sdelphij const char *id, *name; 102296341Sdelphij ENGINE *ptr; 103296341Sdelphij int loop; 104296341Sdelphij int to_return = 1; 105296341Sdelphij ENGINE *new_h1 = NULL; 106296341Sdelphij ENGINE *new_h2 = NULL; 107296341Sdelphij ENGINE *new_h3 = NULL; 108296341Sdelphij ENGINE *new_h4 = NULL; 109109998Smarkm 110296341Sdelphij /* enable memory leak checking unless explicitly disabled */ 111296341Sdelphij if (!((getenv("OPENSSL_DEBUG_MEMORY") != NULL) 112296341Sdelphij && (0 == strcmp(getenv("OPENSSL_DEBUG_MEMORY"), "off")))) { 113296341Sdelphij CRYPTO_malloc_debug_init(); 114296341Sdelphij CRYPTO_set_mem_debug_options(V_CRYPTO_MDEBUG_ALL); 115296341Sdelphij } else { 116296341Sdelphij /* OPENSSL_DEBUG_MEMORY=off */ 117296341Sdelphij CRYPTO_set_mem_debug_functions(0, 0, 0, 0, 0); 118296341Sdelphij } 119296341Sdelphij CRYPTO_mem_ctrl(CRYPTO_MEM_CHECK_ON); 120296341Sdelphij ERR_load_crypto_strings(); 121109998Smarkm 122296341Sdelphij memset(block, 0, 512 * sizeof(ENGINE *)); 123296341Sdelphij if (((new_h1 = ENGINE_new()) == NULL) || 124296341Sdelphij !ENGINE_set_id(new_h1, "test_id0") || 125296341Sdelphij !ENGINE_set_name(new_h1, "First test item") || 126296341Sdelphij ((new_h2 = ENGINE_new()) == NULL) || 127296341Sdelphij !ENGINE_set_id(new_h2, "test_id1") || 128296341Sdelphij !ENGINE_set_name(new_h2, "Second test item") || 129296341Sdelphij ((new_h3 = ENGINE_new()) == NULL) || 130296341Sdelphij !ENGINE_set_id(new_h3, "test_id2") || 131296341Sdelphij !ENGINE_set_name(new_h3, "Third test item") || 132296341Sdelphij ((new_h4 = ENGINE_new()) == NULL) || 133296341Sdelphij !ENGINE_set_id(new_h4, "test_id3") || 134296341Sdelphij !ENGINE_set_name(new_h4, "Fourth test item")) { 135296341Sdelphij printf("Couldn't set up test ENGINE structures\n"); 136296341Sdelphij goto end; 137296341Sdelphij } 138296341Sdelphij printf("\nenginetest beginning\n\n"); 139296341Sdelphij display_engine_list(); 140296341Sdelphij if (!ENGINE_add(new_h1)) { 141296341Sdelphij printf("Add failed!\n"); 142296341Sdelphij goto end; 143296341Sdelphij } 144296341Sdelphij display_engine_list(); 145296341Sdelphij ptr = ENGINE_get_first(); 146296341Sdelphij if (!ENGINE_remove(ptr)) { 147296341Sdelphij printf("Remove failed!\n"); 148296341Sdelphij goto end; 149296341Sdelphij } 150296341Sdelphij if (ptr) 151296341Sdelphij ENGINE_free(ptr); 152296341Sdelphij display_engine_list(); 153296341Sdelphij if (!ENGINE_add(new_h3) || !ENGINE_add(new_h2)) { 154296341Sdelphij printf("Add failed!\n"); 155296341Sdelphij goto end; 156296341Sdelphij } 157296341Sdelphij display_engine_list(); 158296341Sdelphij if (!ENGINE_remove(new_h2)) { 159296341Sdelphij printf("Remove failed!\n"); 160296341Sdelphij goto end; 161296341Sdelphij } 162296341Sdelphij display_engine_list(); 163296341Sdelphij if (!ENGINE_add(new_h4)) { 164296341Sdelphij printf("Add failed!\n"); 165296341Sdelphij goto end; 166296341Sdelphij } 167296341Sdelphij display_engine_list(); 168296341Sdelphij if (ENGINE_add(new_h3)) { 169296341Sdelphij printf("Add *should* have failed but didn't!\n"); 170296341Sdelphij goto end; 171296341Sdelphij } else 172296341Sdelphij printf("Add that should fail did.\n"); 173296341Sdelphij ERR_clear_error(); 174296341Sdelphij if (ENGINE_remove(new_h2)) { 175296341Sdelphij printf("Remove *should* have failed but didn't!\n"); 176296341Sdelphij goto end; 177296341Sdelphij } else 178296341Sdelphij printf("Remove that should fail did.\n"); 179296341Sdelphij ERR_clear_error(); 180296341Sdelphij if (!ENGINE_remove(new_h3)) { 181296341Sdelphij printf("Remove failed!\n"); 182296341Sdelphij goto end; 183296341Sdelphij } 184296341Sdelphij display_engine_list(); 185296341Sdelphij if (!ENGINE_remove(new_h4)) { 186296341Sdelphij printf("Remove failed!\n"); 187296341Sdelphij goto end; 188296341Sdelphij } 189296341Sdelphij display_engine_list(); 190296341Sdelphij /* 191296341Sdelphij * Depending on whether there's any hardware support compiled in, this 192296341Sdelphij * remove may be destined to fail. 193296341Sdelphij */ 194296341Sdelphij ptr = ENGINE_get_first(); 195296341Sdelphij if (ptr) 196296341Sdelphij if (!ENGINE_remove(ptr)) 197296341Sdelphij printf("Remove failed!i - probably no hardware " 198296341Sdelphij "support present.\n"); 199296341Sdelphij if (ptr) 200296341Sdelphij ENGINE_free(ptr); 201296341Sdelphij display_engine_list(); 202296341Sdelphij if (!ENGINE_add(new_h1) || !ENGINE_remove(new_h1)) { 203296341Sdelphij printf("Couldn't add and remove to an empty list!\n"); 204296341Sdelphij goto end; 205296341Sdelphij } else 206296341Sdelphij printf("Successfully added and removed to an empty list!\n"); 207296341Sdelphij printf("About to beef up the engine-type list\n"); 208296341Sdelphij for (loop = 0; loop < 512; loop++) { 209296341Sdelphij sprintf(buf, "id%i", loop); 210296341Sdelphij id = BUF_strdup(buf); 211296341Sdelphij sprintf(buf, "Fake engine type %i", loop); 212296341Sdelphij name = BUF_strdup(buf); 213296341Sdelphij if (((block[loop] = ENGINE_new()) == NULL) || 214296341Sdelphij !ENGINE_set_id(block[loop], id) || 215296341Sdelphij !ENGINE_set_name(block[loop], name)) { 216296341Sdelphij printf("Couldn't create block of ENGINE structures.\n" 217296341Sdelphij "I'll probably also core-dump now, damn.\n"); 218296341Sdelphij goto end; 219296341Sdelphij } 220296341Sdelphij } 221296341Sdelphij for (loop = 0; loop < 512; loop++) { 222296341Sdelphij if (!ENGINE_add(block[loop])) { 223296341Sdelphij printf("\nAdding stopped at %i, (%s,%s)\n", 224296341Sdelphij loop, ENGINE_get_id(block[loop]), 225296341Sdelphij ENGINE_get_name(block[loop])); 226296341Sdelphij goto cleanup_loop; 227296341Sdelphij } else 228296341Sdelphij printf("."); 229296341Sdelphij fflush(stdout); 230296341Sdelphij } 231296341Sdelphij cleanup_loop: 232296341Sdelphij printf("\nAbout to empty the engine-type list\n"); 233296341Sdelphij while ((ptr = ENGINE_get_first()) != NULL) { 234296341Sdelphij if (!ENGINE_remove(ptr)) { 235296341Sdelphij printf("\nRemove failed!\n"); 236296341Sdelphij goto end; 237296341Sdelphij } 238296341Sdelphij ENGINE_free(ptr); 239296341Sdelphij printf("."); 240296341Sdelphij fflush(stdout); 241296341Sdelphij } 242296341Sdelphij for (loop = 0; loop < 512; loop++) { 243296341Sdelphij OPENSSL_free((void *)ENGINE_get_id(block[loop])); 244296341Sdelphij OPENSSL_free((void *)ENGINE_get_name(block[loop])); 245296341Sdelphij } 246296341Sdelphij printf("\nTests completed happily\n"); 247296341Sdelphij to_return = 0; 248296341Sdelphij end: 249296341Sdelphij if (to_return) 250296341Sdelphij ERR_print_errors_fp(stderr); 251296341Sdelphij if (new_h1) 252296341Sdelphij ENGINE_free(new_h1); 253296341Sdelphij if (new_h2) 254296341Sdelphij ENGINE_free(new_h2); 255296341Sdelphij if (new_h3) 256296341Sdelphij ENGINE_free(new_h3); 257296341Sdelphij if (new_h4) 258296341Sdelphij ENGINE_free(new_h4); 259296341Sdelphij for (loop = 0; loop < 512; loop++) 260296341Sdelphij if (block[loop]) 261296341Sdelphij ENGINE_free(block[loop]); 262296341Sdelphij ENGINE_cleanup(); 263296341Sdelphij CRYPTO_cleanup_all_ex_data(); 264296341Sdelphij ERR_free_strings(); 265296341Sdelphij ERR_remove_thread_state(NULL); 266296341Sdelphij CRYPTO_mem_leaks_fp(stderr); 267296341Sdelphij return to_return; 268296341Sdelphij} 269111147Snectar#endif 270