enginetest.c 10 KB

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