1109998Smarkm/* crypto/engine/enginetest.c */
2280304Sjkim/*
3280304Sjkim * Written by Geoff Thorpe (geoff@geoffthorpe.net) for the OpenSSL project
4280304Sjkim * 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
14280304Sjkim *    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");
68280304Sjkim    return (0);
69111147Snectar}
70111147Snectar#else
71280304Sjkim# include <openssl/buffer.h>
72280304Sjkim# include <openssl/crypto.h>
73280304Sjkim# include <openssl/engine.h>
74280304Sjkim# include <openssl/err.h>
75109998Smarkm
76160814Ssimonstatic void display_engine_list(void)
77280304Sjkim{
78280304Sjkim    ENGINE *h;
79280304Sjkim    int loop;
80109998Smarkm
81280304Sjkim    h = ENGINE_get_first();
82280304Sjkim    loop = 0;
83280304Sjkim    printf("listing available engine types\n");
84280304Sjkim    while (h) {
85280304Sjkim        printf("engine %i, id = \"%s\", name = \"%s\"\n",
86280304Sjkim               loop++, ENGINE_get_id(h), ENGINE_get_name(h));
87280304Sjkim        h = ENGINE_get_next(h);
88280304Sjkim    }
89280304Sjkim    printf("end of list\n");
90280304Sjkim    /*
91280304Sjkim     * ENGINE_get_first() increases the struct_ref counter, so we must call
92280304Sjkim     * ENGINE_free() to decrease it again
93280304Sjkim     */
94280304Sjkim    ENGINE_free(h);
95280304Sjkim}
96109998Smarkm
97109998Smarkmint main(int argc, char *argv[])
98280304Sjkim{
99280304Sjkim    ENGINE *block[512];
100280304Sjkim    char buf[256];
101280304Sjkim    const char *id, *name;
102280304Sjkim    ENGINE *ptr;
103280304Sjkim    int loop;
104280304Sjkim    int to_return = 1;
105280304Sjkim    ENGINE *new_h1 = NULL;
106280304Sjkim    ENGINE *new_h2 = NULL;
107280304Sjkim    ENGINE *new_h3 = NULL;
108280304Sjkim    ENGINE *new_h4 = NULL;
109109998Smarkm
110280304Sjkim    /* enable memory leak checking unless explicitly disabled */
111280304Sjkim    if (!((getenv("OPENSSL_DEBUG_MEMORY") != NULL)
112280304Sjkim          && (0 == strcmp(getenv("OPENSSL_DEBUG_MEMORY"), "off")))) {
113280304Sjkim        CRYPTO_malloc_debug_init();
114280304Sjkim        CRYPTO_set_mem_debug_options(V_CRYPTO_MDEBUG_ALL);
115280304Sjkim    } else {
116280304Sjkim        /* OPENSSL_DEBUG_MEMORY=off */
117280304Sjkim        CRYPTO_set_mem_debug_functions(0, 0, 0, 0, 0);
118280304Sjkim    }
119280304Sjkim    CRYPTO_mem_ctrl(CRYPTO_MEM_CHECK_ON);
120280304Sjkim    ERR_load_crypto_strings();
121109998Smarkm
122280304Sjkim    memset(block, 0, 512 * sizeof(ENGINE *));
123280304Sjkim    if (((new_h1 = ENGINE_new()) == NULL) ||
124280304Sjkim        !ENGINE_set_id(new_h1, "test_id0") ||
125280304Sjkim        !ENGINE_set_name(new_h1, "First test item") ||
126280304Sjkim        ((new_h2 = ENGINE_new()) == NULL) ||
127280304Sjkim        !ENGINE_set_id(new_h2, "test_id1") ||
128280304Sjkim        !ENGINE_set_name(new_h2, "Second test item") ||
129280304Sjkim        ((new_h3 = ENGINE_new()) == NULL) ||
130280304Sjkim        !ENGINE_set_id(new_h3, "test_id2") ||
131280304Sjkim        !ENGINE_set_name(new_h3, "Third test item") ||
132280304Sjkim        ((new_h4 = ENGINE_new()) == NULL) ||
133280304Sjkim        !ENGINE_set_id(new_h4, "test_id3") ||
134280304Sjkim        !ENGINE_set_name(new_h4, "Fourth test item")) {
135280304Sjkim        printf("Couldn't set up test ENGINE structures\n");
136280304Sjkim        goto end;
137280304Sjkim    }
138280304Sjkim    printf("\nenginetest beginning\n\n");
139280304Sjkim    display_engine_list();
140280304Sjkim    if (!ENGINE_add(new_h1)) {
141280304Sjkim        printf("Add failed!\n");
142280304Sjkim        goto end;
143280304Sjkim    }
144280304Sjkim    display_engine_list();
145280304Sjkim    ptr = ENGINE_get_first();
146280304Sjkim    if (!ENGINE_remove(ptr)) {
147280304Sjkim        printf("Remove failed!\n");
148280304Sjkim        goto end;
149280304Sjkim    }
150280304Sjkim    if (ptr)
151280304Sjkim        ENGINE_free(ptr);
152280304Sjkim    display_engine_list();
153280304Sjkim    if (!ENGINE_add(new_h3) || !ENGINE_add(new_h2)) {
154280304Sjkim        printf("Add failed!\n");
155280304Sjkim        goto end;
156280304Sjkim    }
157280304Sjkim    display_engine_list();
158280304Sjkim    if (!ENGINE_remove(new_h2)) {
159280304Sjkim        printf("Remove failed!\n");
160280304Sjkim        goto end;
161280304Sjkim    }
162280304Sjkim    display_engine_list();
163280304Sjkim    if (!ENGINE_add(new_h4)) {
164280304Sjkim        printf("Add failed!\n");
165280304Sjkim        goto end;
166280304Sjkim    }
167280304Sjkim    display_engine_list();
168280304Sjkim    if (ENGINE_add(new_h3)) {
169280304Sjkim        printf("Add *should* have failed but didn't!\n");
170280304Sjkim        goto end;
171280304Sjkim    } else
172280304Sjkim        printf("Add that should fail did.\n");
173280304Sjkim    ERR_clear_error();
174280304Sjkim    if (ENGINE_remove(new_h2)) {
175280304Sjkim        printf("Remove *should* have failed but didn't!\n");
176280304Sjkim        goto end;
177280304Sjkim    } else
178280304Sjkim        printf("Remove that should fail did.\n");
179280304Sjkim    ERR_clear_error();
180280304Sjkim    if (!ENGINE_remove(new_h3)) {
181280304Sjkim        printf("Remove failed!\n");
182280304Sjkim        goto end;
183280304Sjkim    }
184280304Sjkim    display_engine_list();
185280304Sjkim    if (!ENGINE_remove(new_h4)) {
186280304Sjkim        printf("Remove failed!\n");
187280304Sjkim        goto end;
188280304Sjkim    }
189280304Sjkim    display_engine_list();
190280304Sjkim    /*
191280304Sjkim     * Depending on whether there's any hardware support compiled in, this
192280304Sjkim     * remove may be destined to fail.
193280304Sjkim     */
194280304Sjkim    ptr = ENGINE_get_first();
195280304Sjkim    if (ptr)
196280304Sjkim        if (!ENGINE_remove(ptr))
197280304Sjkim            printf("Remove failed!i - probably no hardware "
198280304Sjkim                   "support present.\n");
199280304Sjkim    if (ptr)
200280304Sjkim        ENGINE_free(ptr);
201280304Sjkim    display_engine_list();
202280304Sjkim    if (!ENGINE_add(new_h1) || !ENGINE_remove(new_h1)) {
203280304Sjkim        printf("Couldn't add and remove to an empty list!\n");
204280304Sjkim        goto end;
205280304Sjkim    } else
206280304Sjkim        printf("Successfully added and removed to an empty list!\n");
207280304Sjkim    printf("About to beef up the engine-type list\n");
208280304Sjkim    for (loop = 0; loop < 512; loop++) {
209280304Sjkim        sprintf(buf, "id%i", loop);
210280304Sjkim        id = BUF_strdup(buf);
211280304Sjkim        sprintf(buf, "Fake engine type %i", loop);
212280304Sjkim        name = BUF_strdup(buf);
213280304Sjkim        if (((block[loop] = ENGINE_new()) == NULL) ||
214280304Sjkim            !ENGINE_set_id(block[loop], id) ||
215280304Sjkim            !ENGINE_set_name(block[loop], name)) {
216280304Sjkim            printf("Couldn't create block of ENGINE structures.\n"
217280304Sjkim                   "I'll probably also core-dump now, damn.\n");
218280304Sjkim            goto end;
219280304Sjkim        }
220280304Sjkim    }
221280304Sjkim    for (loop = 0; loop < 512; loop++) {
222280304Sjkim        if (!ENGINE_add(block[loop])) {
223280304Sjkim            printf("\nAdding stopped at %i, (%s,%s)\n",
224280304Sjkim                   loop, ENGINE_get_id(block[loop]),
225280304Sjkim                   ENGINE_get_name(block[loop]));
226280304Sjkim            goto cleanup_loop;
227280304Sjkim        } else
228280304Sjkim            printf(".");
229280304Sjkim        fflush(stdout);
230280304Sjkim    }
231280304Sjkim cleanup_loop:
232280304Sjkim    printf("\nAbout to empty the engine-type list\n");
233280304Sjkim    while ((ptr = ENGINE_get_first()) != NULL) {
234280304Sjkim        if (!ENGINE_remove(ptr)) {
235280304Sjkim            printf("\nRemove failed!\n");
236280304Sjkim            goto end;
237280304Sjkim        }
238280304Sjkim        ENGINE_free(ptr);
239280304Sjkim        printf(".");
240280304Sjkim        fflush(stdout);
241280304Sjkim    }
242280304Sjkim    for (loop = 0; loop < 512; loop++) {
243280304Sjkim        OPENSSL_free((void *)ENGINE_get_id(block[loop]));
244280304Sjkim        OPENSSL_free((void *)ENGINE_get_name(block[loop]));
245280304Sjkim    }
246280304Sjkim    printf("\nTests completed happily\n");
247280304Sjkim    to_return = 0;
248280304Sjkim end:
249280304Sjkim    if (to_return)
250280304Sjkim        ERR_print_errors_fp(stderr);
251280304Sjkim    if (new_h1)
252280304Sjkim        ENGINE_free(new_h1);
253280304Sjkim    if (new_h2)
254280304Sjkim        ENGINE_free(new_h2);
255280304Sjkim    if (new_h3)
256280304Sjkim        ENGINE_free(new_h3);
257280304Sjkim    if (new_h4)
258280304Sjkim        ENGINE_free(new_h4);
259280304Sjkim    for (loop = 0; loop < 512; loop++)
260280304Sjkim        if (block[loop])
261280304Sjkim            ENGINE_free(block[loop]);
262280304Sjkim    ENGINE_cleanup();
263280304Sjkim    CRYPTO_cleanup_all_ex_data();
264280304Sjkim    ERR_free_strings();
265280304Sjkim    ERR_remove_thread_state(NULL);
266280304Sjkim    CRYPTO_mem_leaks_fp(stderr);
267280304Sjkim    return to_return;
268280304Sjkim}
269111147Snectar#endif
270