2
0

enginetest.c 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358
  1. /*
  2. * Copyright 2000-2017 The OpenSSL Project Authors. All Rights Reserved.
  3. *
  4. * Licensed under the OpenSSL license (the "License"). You may not use
  5. * this file except in compliance with the License. You can obtain a copy
  6. * in the file LICENSE in the source distribution or at
  7. * https://www.openssl.org/source/license.html
  8. */
  9. #include <stdio.h>
  10. #include <string.h>
  11. #include <stdlib.h>
  12. #include <openssl/e_os2.h>
  13. # include "testutil.h"
  14. #ifndef OPENSSL_NO_ENGINE
  15. # include <openssl/buffer.h>
  16. # include <openssl/crypto.h>
  17. # include <openssl/engine.h>
  18. # include <openssl/rsa.h>
  19. # include <openssl/err.h>
  20. static void display_engine_list(void)
  21. {
  22. ENGINE *h;
  23. int loop;
  24. loop = 0;
  25. for (h = ENGINE_get_first(); h != NULL; h = ENGINE_get_next(h)) {
  26. TEST_info("#%d: id = \"%s\", name = \"%s\"",
  27. loop++, ENGINE_get_id(h), ENGINE_get_name(h));
  28. }
  29. /*
  30. * ENGINE_get_first() increases the struct_ref counter, so we must call
  31. * ENGINE_free() to decrease it again
  32. */
  33. ENGINE_free(h);
  34. }
  35. #define NUMTOADD 512
  36. static int test_engines(void)
  37. {
  38. ENGINE *block[NUMTOADD];
  39. char buf[256];
  40. const char *id, *name;
  41. ENGINE *ptr;
  42. int loop;
  43. int to_return = 0;
  44. ENGINE *new_h1 = NULL;
  45. ENGINE *new_h2 = NULL;
  46. ENGINE *new_h3 = NULL;
  47. ENGINE *new_h4 = NULL;
  48. memset(block, 0, sizeof(block));
  49. if (!TEST_ptr(new_h1 = ENGINE_new())
  50. || !TEST_true(ENGINE_set_id(new_h1, "test_id0"))
  51. || !TEST_true(ENGINE_set_name(new_h1, "First test item"))
  52. || !TEST_ptr(new_h2 = ENGINE_new())
  53. || !TEST_true(ENGINE_set_id(new_h2, "test_id1"))
  54. || !TEST_true(ENGINE_set_name(new_h2, "Second test item"))
  55. || !TEST_ptr(new_h3 = ENGINE_new())
  56. || !TEST_true(ENGINE_set_id(new_h3, "test_id2"))
  57. || !TEST_true(ENGINE_set_name(new_h3, "Third test item"))
  58. || !TEST_ptr(new_h4 = ENGINE_new())
  59. || !TEST_true(ENGINE_set_id(new_h4, "test_id3"))
  60. || !TEST_true(ENGINE_set_name(new_h4, "Fourth test item")))
  61. goto end;
  62. TEST_info("Engines:");
  63. display_engine_list();
  64. if (!TEST_true(ENGINE_add(new_h1)))
  65. goto end;
  66. TEST_info("Engines:");
  67. display_engine_list();
  68. ptr = ENGINE_get_first();
  69. if (!TEST_true(ENGINE_remove(ptr)))
  70. goto end;
  71. ENGINE_free(ptr);
  72. TEST_info("Engines:");
  73. display_engine_list();
  74. if (!TEST_true(ENGINE_add(new_h3))
  75. || !TEST_true(ENGINE_add(new_h2)))
  76. goto end;
  77. TEST_info("Engines:");
  78. display_engine_list();
  79. if (!TEST_true(ENGINE_remove(new_h2)))
  80. goto end;
  81. TEST_info("Engines:");
  82. display_engine_list();
  83. if (!TEST_true(ENGINE_add(new_h4)))
  84. goto end;
  85. TEST_info("Engines:");
  86. display_engine_list();
  87. /* Should fail. */
  88. if (!TEST_false(ENGINE_add(new_h3)))
  89. goto end;
  90. ERR_clear_error();
  91. /* Should fail. */
  92. if (!TEST_false(ENGINE_remove(new_h2)))
  93. goto end;
  94. ERR_clear_error();
  95. if (!TEST_true(ENGINE_remove(new_h3)))
  96. goto end;
  97. TEST_info("Engines:");
  98. display_engine_list();
  99. if (!TEST_true(ENGINE_remove(new_h4)))
  100. goto end;
  101. TEST_info("Engines:");
  102. display_engine_list();
  103. /*
  104. * Depending on whether there's any hardware support compiled in, this
  105. * remove may be destined to fail.
  106. */
  107. if ((ptr = ENGINE_get_first()) != NULL) {
  108. if (!ENGINE_remove(ptr))
  109. TEST_info("Remove failed - probably no hardware support present");
  110. }
  111. ENGINE_free(ptr);
  112. TEST_info("Engines:");
  113. display_engine_list();
  114. if (!TEST_true(ENGINE_add(new_h1))
  115. || !TEST_true(ENGINE_remove(new_h1)))
  116. goto end;
  117. TEST_info("About to beef up the engine-type list");
  118. for (loop = 0; loop < NUMTOADD; loop++) {
  119. sprintf(buf, "id%d", loop);
  120. id = OPENSSL_strdup(buf);
  121. sprintf(buf, "Fake engine type %d", loop);
  122. name = OPENSSL_strdup(buf);
  123. if (!TEST_ptr(block[loop] = ENGINE_new())
  124. || !TEST_true(ENGINE_set_id(block[loop], id))
  125. || !TEST_true(ENGINE_set_name(block[loop], name)))
  126. goto end;
  127. }
  128. for (loop = 0; loop < NUMTOADD; loop++) {
  129. if (!TEST_true(ENGINE_add(block[loop]))) {
  130. test_note("Adding stopped at %d, (%s,%s)",
  131. loop, ENGINE_get_id(block[loop]),
  132. ENGINE_get_name(block[loop]));
  133. goto cleanup_loop;
  134. }
  135. }
  136. cleanup_loop:
  137. TEST_info("About to empty the engine-type list");
  138. while ((ptr = ENGINE_get_first()) != NULL) {
  139. if (!TEST_true(ENGINE_remove(ptr)))
  140. goto end;
  141. ENGINE_free(ptr);
  142. }
  143. for (loop = 0; loop < NUMTOADD; loop++) {
  144. OPENSSL_free((void *)ENGINE_get_id(block[loop]));
  145. OPENSSL_free((void *)ENGINE_get_name(block[loop]));
  146. }
  147. to_return = 1;
  148. end:
  149. ENGINE_free(new_h1);
  150. ENGINE_free(new_h2);
  151. ENGINE_free(new_h3);
  152. ENGINE_free(new_h4);
  153. for (loop = 0; loop < NUMTOADD; loop++)
  154. ENGINE_free(block[loop]);
  155. return to_return;
  156. }
  157. /* Test EVP_PKEY method */
  158. static EVP_PKEY_METHOD *test_rsa = NULL;
  159. static int called_encrypt = 0;
  160. /* Test function to check operation has been redirected */
  161. static int test_encrypt(EVP_PKEY_CTX *ctx, unsigned char *sig,
  162. size_t *siglen, const unsigned char *tbs, size_t tbslen)
  163. {
  164. called_encrypt = 1;
  165. return 1;
  166. }
  167. static int test_pkey_meths(ENGINE *e, EVP_PKEY_METHOD **pmeth,
  168. const int **pnids, int nid)
  169. {
  170. static const int rnid = EVP_PKEY_RSA;
  171. if (pmeth == NULL) {
  172. *pnids = &rnid;
  173. return 1;
  174. }
  175. if (nid == EVP_PKEY_RSA) {
  176. *pmeth = test_rsa;
  177. return 1;
  178. }
  179. *pmeth = NULL;
  180. return 0;
  181. }
  182. /* Return a test EVP_PKEY value */
  183. static EVP_PKEY *get_test_pkey(void)
  184. {
  185. static unsigned char n[] =
  186. "\x00\xAA\x36\xAB\xCE\x88\xAC\xFD\xFF\x55\x52\x3C\x7F\xC4\x52\x3F"
  187. "\x90\xEF\xA0\x0D\xF3\x77\x4A\x25\x9F\x2E\x62\xB4\xC5\xD9\x9C\xB5"
  188. "\xAD\xB3\x00\xA0\x28\x5E\x53\x01\x93\x0E\x0C\x70\xFB\x68\x76\x93"
  189. "\x9C\xE6\x16\xCE\x62\x4A\x11\xE0\x08\x6D\x34\x1E\xBC\xAC\xA0\xA1"
  190. "\xF5";
  191. static unsigned char e[] = "\x11";
  192. RSA *rsa = RSA_new();
  193. EVP_PKEY *pk = EVP_PKEY_new();
  194. if (rsa == NULL || pk == NULL || !EVP_PKEY_assign_RSA(pk, rsa)) {
  195. RSA_free(rsa);
  196. EVP_PKEY_free(pk);
  197. return NULL;
  198. }
  199. if (!RSA_set0_key(rsa, BN_bin2bn(n, sizeof(n)-1, NULL),
  200. BN_bin2bn(e, sizeof(e)-1, NULL), NULL)) {
  201. EVP_PKEY_free(pk);
  202. return NULL;
  203. }
  204. return pk;
  205. }
  206. static int test_redirect(void)
  207. {
  208. const unsigned char pt[] = "Hello World\n";
  209. unsigned char *tmp = NULL;
  210. size_t len;
  211. EVP_PKEY_CTX *ctx = NULL;
  212. ENGINE *e = NULL;
  213. EVP_PKEY *pkey = NULL;
  214. int to_return = 0;
  215. if (!TEST_ptr(pkey = get_test_pkey()))
  216. goto err;
  217. len = EVP_PKEY_size(pkey);
  218. if (!TEST_ptr(tmp = OPENSSL_malloc(len)))
  219. goto err;
  220. if (!TEST_ptr(ctx = EVP_PKEY_CTX_new(pkey, NULL)))
  221. goto err;
  222. TEST_info("EVP_PKEY_encrypt test: no redirection");
  223. /* Encrypt some data: should succeed but not be redirected */
  224. if (!TEST_int_gt(EVP_PKEY_encrypt_init(ctx), 0)
  225. || !TEST_int_gt(EVP_PKEY_encrypt(ctx, tmp, &len, pt, sizeof(pt)), 0)
  226. || !TEST_false(called_encrypt))
  227. goto err;
  228. EVP_PKEY_CTX_free(ctx);
  229. ctx = NULL;
  230. /* Create a test ENGINE */
  231. if (!TEST_ptr(e = ENGINE_new())
  232. || !TEST_true(ENGINE_set_id(e, "Test redirect engine"))
  233. || !TEST_true(ENGINE_set_name(e, "Test redirect engine")))
  234. goto err;
  235. /*
  236. * Try to create a context for this engine and test key.
  237. * Try setting test key engine. Both should fail because the
  238. * engine has no public key methods.
  239. */
  240. if (!TEST_ptr_null(EVP_PKEY_CTX_new(pkey, e))
  241. || !TEST_int_le(EVP_PKEY_set1_engine(pkey, e), 0))
  242. goto err;
  243. /* Setup an empty test EVP_PKEY_METHOD and set callback to return it */
  244. if (!TEST_ptr(test_rsa = EVP_PKEY_meth_new(EVP_PKEY_RSA, 0)))
  245. goto err;
  246. ENGINE_set_pkey_meths(e, test_pkey_meths);
  247. /* Getting a context for test ENGINE should now succeed */
  248. if (!TEST_ptr(ctx = EVP_PKEY_CTX_new(pkey, e)))
  249. goto err;
  250. /* Encrypt should fail because operation is not supported */
  251. if (!TEST_int_le(EVP_PKEY_encrypt_init(ctx), 0))
  252. goto err;
  253. EVP_PKEY_CTX_free(ctx);
  254. ctx = NULL;
  255. /* Add test encrypt operation to method */
  256. EVP_PKEY_meth_set_encrypt(test_rsa, 0, test_encrypt);
  257. TEST_info("EVP_PKEY_encrypt test: redirection via EVP_PKEY_CTX_new()");
  258. if (!TEST_ptr(ctx = EVP_PKEY_CTX_new(pkey, e)))
  259. goto err;
  260. /* Encrypt some data: should succeed and be redirected */
  261. if (!TEST_int_gt(EVP_PKEY_encrypt_init(ctx), 0)
  262. || !TEST_int_gt(EVP_PKEY_encrypt(ctx, tmp, &len, pt, sizeof(pt)), 0)
  263. || !TEST_true(called_encrypt))
  264. goto err;
  265. EVP_PKEY_CTX_free(ctx);
  266. ctx = NULL;
  267. called_encrypt = 0;
  268. /* Create context with default engine: should not be redirected */
  269. if (!TEST_ptr(ctx = EVP_PKEY_CTX_new(pkey, NULL))
  270. || !TEST_int_gt(EVP_PKEY_encrypt_init(ctx), 0)
  271. || !TEST_int_gt(EVP_PKEY_encrypt(ctx, tmp, &len, pt, sizeof(pt)), 0)
  272. || !TEST_false(called_encrypt))
  273. goto err;
  274. EVP_PKEY_CTX_free(ctx);
  275. ctx = NULL;
  276. /* Set engine explicitly for test key */
  277. if (!TEST_true(EVP_PKEY_set1_engine(pkey, e)))
  278. goto err;
  279. TEST_info("EVP_PKEY_encrypt test: redirection via EVP_PKEY_set1_engine()");
  280. /* Create context with default engine: should be redirected now */
  281. if (!TEST_ptr(ctx = EVP_PKEY_CTX_new(pkey, NULL))
  282. || !TEST_int_gt(EVP_PKEY_encrypt_init(ctx), 0)
  283. || !TEST_int_gt(EVP_PKEY_encrypt(ctx, tmp, &len, pt, sizeof(pt)), 0)
  284. || !TEST_true(called_encrypt))
  285. goto err;
  286. to_return = 1;
  287. err:
  288. EVP_PKEY_CTX_free(ctx);
  289. EVP_PKEY_free(pkey);
  290. ENGINE_free(e);
  291. OPENSSL_free(tmp);
  292. return to_return;
  293. }
  294. #endif
  295. int setup_tests(void)
  296. {
  297. #ifdef OPENSSL_NO_ENGINE
  298. TEST_note("No ENGINE support");
  299. #else
  300. ADD_TEST(test_engines);
  301. ADD_TEST(test_redirect);
  302. #endif
  303. return 1;
  304. }