10Sstevel@tonic-gate /* crypto/engine/enginetest.c */
20Sstevel@tonic-gate /* Written by Geoff Thorpe (geoff@geoffthorpe.net) for the OpenSSL
30Sstevel@tonic-gate * project 2000.
40Sstevel@tonic-gate */
50Sstevel@tonic-gate /* ====================================================================
60Sstevel@tonic-gate * Copyright (c) 1999-2001 The OpenSSL Project. All rights reserved.
70Sstevel@tonic-gate *
80Sstevel@tonic-gate * Redistribution and use in source and binary forms, with or without
90Sstevel@tonic-gate * modification, are permitted provided that the following conditions
100Sstevel@tonic-gate * are met:
110Sstevel@tonic-gate *
120Sstevel@tonic-gate * 1. Redistributions of source code must retain the above copyright
130Sstevel@tonic-gate * notice, this list of conditions and the following disclaimer.
140Sstevel@tonic-gate *
150Sstevel@tonic-gate * 2. Redistributions in binary form must reproduce the above copyright
160Sstevel@tonic-gate * notice, this list of conditions and the following disclaimer in
170Sstevel@tonic-gate * the documentation and/or other materials provided with the
180Sstevel@tonic-gate * distribution.
190Sstevel@tonic-gate *
200Sstevel@tonic-gate * 3. All advertising materials mentioning features or use of this
210Sstevel@tonic-gate * software must display the following acknowledgment:
220Sstevel@tonic-gate * "This product includes software developed by the OpenSSL Project
230Sstevel@tonic-gate * for use in the OpenSSL Toolkit. (http://www.OpenSSL.org/)"
240Sstevel@tonic-gate *
250Sstevel@tonic-gate * 4. The names "OpenSSL Toolkit" and "OpenSSL Project" must not be used to
260Sstevel@tonic-gate * endorse or promote products derived from this software without
270Sstevel@tonic-gate * prior written permission. For written permission, please contact
280Sstevel@tonic-gate * licensing@OpenSSL.org.
290Sstevel@tonic-gate *
300Sstevel@tonic-gate * 5. Products derived from this software may not be called "OpenSSL"
310Sstevel@tonic-gate * nor may "OpenSSL" appear in their names without prior written
320Sstevel@tonic-gate * permission of the OpenSSL Project.
330Sstevel@tonic-gate *
340Sstevel@tonic-gate * 6. Redistributions of any form whatsoever must retain the following
350Sstevel@tonic-gate * acknowledgment:
360Sstevel@tonic-gate * "This product includes software developed by the OpenSSL Project
370Sstevel@tonic-gate * for use in the OpenSSL Toolkit (http://www.OpenSSL.org/)"
380Sstevel@tonic-gate *
390Sstevel@tonic-gate * THIS SOFTWARE IS PROVIDED BY THE OpenSSL PROJECT ``AS IS'' AND ANY
400Sstevel@tonic-gate * EXPRESSED OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
410Sstevel@tonic-gate * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
420Sstevel@tonic-gate * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE OpenSSL PROJECT OR
430Sstevel@tonic-gate * ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
440Sstevel@tonic-gate * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
450Sstevel@tonic-gate * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
460Sstevel@tonic-gate * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
470Sstevel@tonic-gate * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
480Sstevel@tonic-gate * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
490Sstevel@tonic-gate * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED
500Sstevel@tonic-gate * OF THE POSSIBILITY OF SUCH DAMAGE.
510Sstevel@tonic-gate * ====================================================================
520Sstevel@tonic-gate *
530Sstevel@tonic-gate * This product includes cryptographic software written by Eric Young
540Sstevel@tonic-gate * (eay@cryptsoft.com). This product includes software written by Tim
550Sstevel@tonic-gate * Hudson (tjh@cryptsoft.com).
560Sstevel@tonic-gate *
570Sstevel@tonic-gate */
580Sstevel@tonic-gate
590Sstevel@tonic-gate #include <stdio.h>
600Sstevel@tonic-gate #include <string.h>
610Sstevel@tonic-gate
620Sstevel@tonic-gate #ifdef OPENSSL_NO_ENGINE
main(int argc,char * argv[])630Sstevel@tonic-gate int main(int argc, char *argv[])
640Sstevel@tonic-gate {
650Sstevel@tonic-gate printf("No ENGINE support\n");
660Sstevel@tonic-gate return(0);
670Sstevel@tonic-gate }
680Sstevel@tonic-gate #else
690Sstevel@tonic-gate #include <openssl/e_os2.h>
700Sstevel@tonic-gate #include <openssl/buffer.h>
710Sstevel@tonic-gate #include <openssl/crypto.h>
720Sstevel@tonic-gate #include <openssl/engine.h>
730Sstevel@tonic-gate #include <openssl/err.h>
740Sstevel@tonic-gate
display_engine_list(void)75*2139Sjp161948 static void display_engine_list(void)
760Sstevel@tonic-gate {
770Sstevel@tonic-gate ENGINE *h;
780Sstevel@tonic-gate int loop;
790Sstevel@tonic-gate
800Sstevel@tonic-gate h = ENGINE_get_first();
810Sstevel@tonic-gate loop = 0;
820Sstevel@tonic-gate printf("listing available engine types\n");
830Sstevel@tonic-gate while(h)
840Sstevel@tonic-gate {
850Sstevel@tonic-gate printf("engine %i, id = \"%s\", name = \"%s\"\n",
860Sstevel@tonic-gate loop++, ENGINE_get_id(h), ENGINE_get_name(h));
870Sstevel@tonic-gate h = ENGINE_get_next(h);
880Sstevel@tonic-gate }
890Sstevel@tonic-gate printf("end of list\n");
900Sstevel@tonic-gate /* ENGINE_get_first() increases the struct_ref counter, so we
910Sstevel@tonic-gate must call ENGINE_free() to decrease it again */
920Sstevel@tonic-gate ENGINE_free(h);
930Sstevel@tonic-gate }
940Sstevel@tonic-gate
main(int argc,char * argv[])950Sstevel@tonic-gate int main(int argc, char *argv[])
960Sstevel@tonic-gate {
970Sstevel@tonic-gate ENGINE *block[512];
980Sstevel@tonic-gate char buf[256];
990Sstevel@tonic-gate const char *id, *name;
1000Sstevel@tonic-gate ENGINE *ptr;
1010Sstevel@tonic-gate int loop;
1020Sstevel@tonic-gate int to_return = 1;
1030Sstevel@tonic-gate ENGINE *new_h1 = NULL;
1040Sstevel@tonic-gate ENGINE *new_h2 = NULL;
1050Sstevel@tonic-gate ENGINE *new_h3 = NULL;
1060Sstevel@tonic-gate ENGINE *new_h4 = NULL;
1070Sstevel@tonic-gate
1080Sstevel@tonic-gate /* enable memory leak checking unless explicitly disabled */
1090Sstevel@tonic-gate if (!((getenv("OPENSSL_DEBUG_MEMORY") != NULL) && (0 == strcmp(getenv("OPENSSL_DEBUG_MEMORY"), "off"))))
1100Sstevel@tonic-gate {
1110Sstevel@tonic-gate CRYPTO_malloc_debug_init();
1120Sstevel@tonic-gate CRYPTO_set_mem_debug_options(V_CRYPTO_MDEBUG_ALL);
1130Sstevel@tonic-gate }
1140Sstevel@tonic-gate else
1150Sstevel@tonic-gate {
1160Sstevel@tonic-gate /* OPENSSL_DEBUG_MEMORY=off */
1170Sstevel@tonic-gate CRYPTO_set_mem_debug_functions(0, 0, 0, 0, 0);
1180Sstevel@tonic-gate }
1190Sstevel@tonic-gate CRYPTO_mem_ctrl(CRYPTO_MEM_CHECK_ON);
1200Sstevel@tonic-gate ERR_load_crypto_strings();
1210Sstevel@tonic-gate
1220Sstevel@tonic-gate memset(block, 0, 512 * sizeof(ENGINE *));
1230Sstevel@tonic-gate if(((new_h1 = ENGINE_new()) == NULL) ||
1240Sstevel@tonic-gate !ENGINE_set_id(new_h1, "test_id0") ||
1250Sstevel@tonic-gate !ENGINE_set_name(new_h1, "First test item") ||
1260Sstevel@tonic-gate ((new_h2 = ENGINE_new()) == NULL) ||
1270Sstevel@tonic-gate !ENGINE_set_id(new_h2, "test_id1") ||
1280Sstevel@tonic-gate !ENGINE_set_name(new_h2, "Second test item") ||
1290Sstevel@tonic-gate ((new_h3 = ENGINE_new()) == NULL) ||
1300Sstevel@tonic-gate !ENGINE_set_id(new_h3, "test_id2") ||
1310Sstevel@tonic-gate !ENGINE_set_name(new_h3, "Third test item") ||
1320Sstevel@tonic-gate ((new_h4 = ENGINE_new()) == NULL) ||
1330Sstevel@tonic-gate !ENGINE_set_id(new_h4, "test_id3") ||
1340Sstevel@tonic-gate !ENGINE_set_name(new_h4, "Fourth test item"))
1350Sstevel@tonic-gate {
1360Sstevel@tonic-gate printf("Couldn't set up test ENGINE structures\n");
1370Sstevel@tonic-gate goto end;
1380Sstevel@tonic-gate }
1390Sstevel@tonic-gate printf("\nenginetest beginning\n\n");
1400Sstevel@tonic-gate display_engine_list();
1410Sstevel@tonic-gate if(!ENGINE_add(new_h1))
1420Sstevel@tonic-gate {
1430Sstevel@tonic-gate printf("Add failed!\n");
1440Sstevel@tonic-gate goto end;
1450Sstevel@tonic-gate }
1460Sstevel@tonic-gate display_engine_list();
1470Sstevel@tonic-gate ptr = ENGINE_get_first();
1480Sstevel@tonic-gate if(!ENGINE_remove(ptr))
1490Sstevel@tonic-gate {
1500Sstevel@tonic-gate printf("Remove failed!\n");
1510Sstevel@tonic-gate goto end;
1520Sstevel@tonic-gate }
1530Sstevel@tonic-gate if (ptr)
1540Sstevel@tonic-gate ENGINE_free(ptr);
1550Sstevel@tonic-gate display_engine_list();
1560Sstevel@tonic-gate if(!ENGINE_add(new_h3) || !ENGINE_add(new_h2))
1570Sstevel@tonic-gate {
1580Sstevel@tonic-gate printf("Add failed!\n");
1590Sstevel@tonic-gate goto end;
1600Sstevel@tonic-gate }
1610Sstevel@tonic-gate display_engine_list();
1620Sstevel@tonic-gate if(!ENGINE_remove(new_h2))
1630Sstevel@tonic-gate {
1640Sstevel@tonic-gate printf("Remove failed!\n");
1650Sstevel@tonic-gate goto end;
1660Sstevel@tonic-gate }
1670Sstevel@tonic-gate display_engine_list();
1680Sstevel@tonic-gate if(!ENGINE_add(new_h4))
1690Sstevel@tonic-gate {
1700Sstevel@tonic-gate printf("Add failed!\n");
1710Sstevel@tonic-gate goto end;
1720Sstevel@tonic-gate }
1730Sstevel@tonic-gate display_engine_list();
1740Sstevel@tonic-gate if(ENGINE_add(new_h3))
1750Sstevel@tonic-gate {
1760Sstevel@tonic-gate printf("Add *should* have failed but didn't!\n");
1770Sstevel@tonic-gate goto end;
1780Sstevel@tonic-gate }
1790Sstevel@tonic-gate else
1800Sstevel@tonic-gate printf("Add that should fail did.\n");
1810Sstevel@tonic-gate ERR_clear_error();
1820Sstevel@tonic-gate if(ENGINE_remove(new_h2))
1830Sstevel@tonic-gate {
1840Sstevel@tonic-gate printf("Remove *should* have failed but didn't!\n");
1850Sstevel@tonic-gate goto end;
1860Sstevel@tonic-gate }
1870Sstevel@tonic-gate else
1880Sstevel@tonic-gate printf("Remove that should fail did.\n");
1890Sstevel@tonic-gate ERR_clear_error();
1900Sstevel@tonic-gate if(!ENGINE_remove(new_h3))
1910Sstevel@tonic-gate {
1920Sstevel@tonic-gate printf("Remove failed!\n");
1930Sstevel@tonic-gate goto end;
1940Sstevel@tonic-gate }
1950Sstevel@tonic-gate display_engine_list();
1960Sstevel@tonic-gate if(!ENGINE_remove(new_h4))
1970Sstevel@tonic-gate {
1980Sstevel@tonic-gate printf("Remove failed!\n");
1990Sstevel@tonic-gate goto end;
2000Sstevel@tonic-gate }
2010Sstevel@tonic-gate display_engine_list();
2020Sstevel@tonic-gate /* Depending on whether there's any hardware support compiled
2030Sstevel@tonic-gate * in, this remove may be destined to fail. */
2040Sstevel@tonic-gate ptr = ENGINE_get_first();
2050Sstevel@tonic-gate if(ptr)
2060Sstevel@tonic-gate if(!ENGINE_remove(ptr))
2070Sstevel@tonic-gate printf("Remove failed!i - probably no hardware "
2080Sstevel@tonic-gate "support present.\n");
2090Sstevel@tonic-gate if (ptr)
2100Sstevel@tonic-gate ENGINE_free(ptr);
2110Sstevel@tonic-gate display_engine_list();
2120Sstevel@tonic-gate if(!ENGINE_add(new_h1) || !ENGINE_remove(new_h1))
2130Sstevel@tonic-gate {
2140Sstevel@tonic-gate printf("Couldn't add and remove to an empty list!\n");
2150Sstevel@tonic-gate goto end;
2160Sstevel@tonic-gate }
2170Sstevel@tonic-gate else
2180Sstevel@tonic-gate printf("Successfully added and removed to an empty list!\n");
2190Sstevel@tonic-gate printf("About to beef up the engine-type list\n");
2200Sstevel@tonic-gate for(loop = 0; loop < 512; loop++)
2210Sstevel@tonic-gate {
2220Sstevel@tonic-gate sprintf(buf, "id%i", loop);
2230Sstevel@tonic-gate id = BUF_strdup(buf);
2240Sstevel@tonic-gate sprintf(buf, "Fake engine type %i", loop);
2250Sstevel@tonic-gate name = BUF_strdup(buf);
2260Sstevel@tonic-gate if(((block[loop] = ENGINE_new()) == NULL) ||
2270Sstevel@tonic-gate !ENGINE_set_id(block[loop], id) ||
2280Sstevel@tonic-gate !ENGINE_set_name(block[loop], name))
2290Sstevel@tonic-gate {
2300Sstevel@tonic-gate printf("Couldn't create block of ENGINE structures.\n"
2310Sstevel@tonic-gate "I'll probably also core-dump now, damn.\n");
2320Sstevel@tonic-gate goto end;
2330Sstevel@tonic-gate }
2340Sstevel@tonic-gate }
2350Sstevel@tonic-gate for(loop = 0; loop < 512; loop++)
2360Sstevel@tonic-gate {
2370Sstevel@tonic-gate if(!ENGINE_add(block[loop]))
2380Sstevel@tonic-gate {
2390Sstevel@tonic-gate printf("\nAdding stopped at %i, (%s,%s)\n",
2400Sstevel@tonic-gate loop, ENGINE_get_id(block[loop]),
2410Sstevel@tonic-gate ENGINE_get_name(block[loop]));
2420Sstevel@tonic-gate goto cleanup_loop;
2430Sstevel@tonic-gate }
2440Sstevel@tonic-gate else
2450Sstevel@tonic-gate printf("."); fflush(stdout);
2460Sstevel@tonic-gate }
2470Sstevel@tonic-gate cleanup_loop:
2480Sstevel@tonic-gate printf("\nAbout to empty the engine-type list\n");
2490Sstevel@tonic-gate while((ptr = ENGINE_get_first()) != NULL)
2500Sstevel@tonic-gate {
2510Sstevel@tonic-gate if(!ENGINE_remove(ptr))
2520Sstevel@tonic-gate {
2530Sstevel@tonic-gate printf("\nRemove failed!\n");
2540Sstevel@tonic-gate goto end;
2550Sstevel@tonic-gate }
2560Sstevel@tonic-gate ENGINE_free(ptr);
2570Sstevel@tonic-gate printf("."); fflush(stdout);
2580Sstevel@tonic-gate }
2590Sstevel@tonic-gate for(loop = 0; loop < 512; loop++)
2600Sstevel@tonic-gate {
2610Sstevel@tonic-gate OPENSSL_free((void *)ENGINE_get_id(block[loop]));
2620Sstevel@tonic-gate OPENSSL_free((void *)ENGINE_get_name(block[loop]));
2630Sstevel@tonic-gate }
2640Sstevel@tonic-gate printf("\nTests completed happily\n");
2650Sstevel@tonic-gate to_return = 0;
2660Sstevel@tonic-gate end:
2670Sstevel@tonic-gate if(to_return)
2680Sstevel@tonic-gate ERR_print_errors_fp(stderr);
2690Sstevel@tonic-gate if(new_h1) ENGINE_free(new_h1);
2700Sstevel@tonic-gate if(new_h2) ENGINE_free(new_h2);
2710Sstevel@tonic-gate if(new_h3) ENGINE_free(new_h3);
2720Sstevel@tonic-gate if(new_h4) ENGINE_free(new_h4);
2730Sstevel@tonic-gate for(loop = 0; loop < 512; loop++)
2740Sstevel@tonic-gate if(block[loop])
2750Sstevel@tonic-gate ENGINE_free(block[loop]);
2760Sstevel@tonic-gate ENGINE_cleanup();
2770Sstevel@tonic-gate CRYPTO_cleanup_all_ex_data();
2780Sstevel@tonic-gate ERR_free_strings();
2790Sstevel@tonic-gate ERR_remove_state(0);
2800Sstevel@tonic-gate CRYPTO_mem_leaks_fp(stderr);
2810Sstevel@tonic-gate return to_return;
2820Sstevel@tonic-gate }
2830Sstevel@tonic-gate #endif
284