2
0

enginetest.c 7.9 KB

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