enginetest.c 11 KB

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