2
0

enginetest.c 8.7 KB

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