enginetest.c 8.0 KB

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