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