test_hs_cache.c 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447
  1. /* Copyright (c) 2016-2017, The Tor Project, Inc. */
  2. /* See LICENSE for licensing information */
  3. /**
  4. * \file test_hs_cache.c
  5. * \brief Test hidden service caches.
  6. */
  7. #define CONNECTION_PRIVATE
  8. #define HS_CACHE_PRIVATE
  9. #include "ed25519_cert.h"
  10. #include "hs_cache.h"
  11. #include "rendcache.h"
  12. #include "directory.h"
  13. #include "connection.h"
  14. #include "hs_test_helpers.h"
  15. #include "test_helpers.h"
  16. #include "test.h"
  17. /* Static variable used to encoded the HSDir query. */
  18. static char query_b64[256];
  19. /* Build an HSDir query using a ed25519 public key. */
  20. static const char *
  21. helper_get_hsdir_query(const hs_descriptor_t *desc)
  22. {
  23. ed25519_public_to_base64(query_b64, &desc->plaintext_data.blinded_pubkey);
  24. return query_b64;
  25. }
  26. static void
  27. init_test(void)
  28. {
  29. /* Always needed. Initialize the subsystem. */
  30. hs_cache_init();
  31. /* We need the v2 cache since our OOM and cache cleanup does poke at it. */
  32. rend_cache_init();
  33. }
  34. static void
  35. test_directory(void *arg)
  36. {
  37. int ret;
  38. size_t oom_size;
  39. char *desc1_str = NULL;
  40. const char *desc_out;
  41. ed25519_keypair_t signing_kp1;
  42. hs_descriptor_t *desc1 = NULL;
  43. (void) arg;
  44. init_test();
  45. /* Generate a valid descriptor with normal values. */
  46. ret = ed25519_keypair_generate(&signing_kp1, 0);
  47. tt_int_op(ret, ==, 0);
  48. desc1 = hs_helper_build_hs_desc_with_ip(&signing_kp1);
  49. tt_assert(desc1);
  50. ret = hs_desc_encode_descriptor(desc1, &signing_kp1, &desc1_str);
  51. tt_int_op(ret, OP_EQ, 0);
  52. /* Very first basic test, should be able to be stored, survive a
  53. * clean, found with a lookup and then cleaned by our OOM. */
  54. {
  55. ret = hs_cache_store_as_dir(desc1_str);
  56. tt_int_op(ret, OP_EQ, 0);
  57. /* Re-add, it should fail since we already have it. */
  58. ret = hs_cache_store_as_dir(desc1_str);
  59. tt_int_op(ret, OP_EQ, -1);
  60. /* Try to clean now which should be fine, there is at worst few seconds
  61. * between the store and this call. */
  62. hs_cache_clean_as_dir(time(NULL));
  63. /* We should find it in our cache. */
  64. ret = hs_cache_lookup_as_dir(3, helper_get_hsdir_query(desc1), &desc_out);
  65. tt_int_op(ret, OP_EQ, 1);
  66. tt_str_op(desc_out, OP_EQ, desc1_str);
  67. /* Tell our OOM to run and to at least remove a byte which will result in
  68. * removing the descriptor from our cache. */
  69. oom_size = hs_cache_handle_oom(time(NULL), 1);
  70. tt_int_op(oom_size, >=, 1);
  71. ret = hs_cache_lookup_as_dir(3, helper_get_hsdir_query(desc1), NULL);
  72. tt_int_op(ret, OP_EQ, 0);
  73. }
  74. /* Store two descriptors and remove the expiring one only. */
  75. {
  76. ed25519_keypair_t signing_kp_zero;
  77. ret = ed25519_keypair_generate(&signing_kp_zero, 0);
  78. tt_int_op(ret, ==, 0);
  79. hs_descriptor_t *desc_zero_lifetime;
  80. desc_zero_lifetime = hs_helper_build_hs_desc_with_ip(&signing_kp_zero);
  81. tt_assert(desc_zero_lifetime);
  82. desc_zero_lifetime->plaintext_data.revision_counter = 1;
  83. desc_zero_lifetime->plaintext_data.lifetime_sec = 0;
  84. char *desc_zero_lifetime_str;
  85. ret = hs_desc_encode_descriptor(desc_zero_lifetime, &signing_kp_zero,
  86. &desc_zero_lifetime_str);
  87. tt_int_op(ret, OP_EQ, 0);
  88. ret = hs_cache_store_as_dir(desc1_str);
  89. tt_int_op(ret, OP_EQ, 0);
  90. ret = hs_cache_store_as_dir(desc_zero_lifetime_str);
  91. tt_int_op(ret, OP_EQ, 0);
  92. /* This one should clear out our zero lifetime desc. */
  93. hs_cache_clean_as_dir(time(NULL));
  94. /* We should find desc1 in our cache. */
  95. ret = hs_cache_lookup_as_dir(3, helper_get_hsdir_query(desc1), &desc_out);
  96. tt_int_op(ret, OP_EQ, 1);
  97. tt_str_op(desc_out, OP_EQ, desc1_str);
  98. /* We should NOT find our zero lifetime desc in our cache. */
  99. ret = hs_cache_lookup_as_dir(3,
  100. helper_get_hsdir_query(desc_zero_lifetime),
  101. NULL);
  102. tt_int_op(ret, OP_EQ, 0);
  103. /* Cleanup our entire cache. */
  104. oom_size = hs_cache_handle_oom(time(NULL), 1);
  105. tt_int_op(oom_size, >=, 1);
  106. hs_descriptor_free(desc_zero_lifetime);
  107. tor_free(desc_zero_lifetime_str);
  108. }
  109. /* Throw junk at it. */
  110. {
  111. ret = hs_cache_store_as_dir("blah");
  112. tt_int_op(ret, OP_EQ, -1);
  113. /* Poor attempt at tricking the decoding. */
  114. ret = hs_cache_store_as_dir("hs-descriptor 3\nJUNK");
  115. tt_int_op(ret, OP_EQ, -1);
  116. /* Undecodable base64 query. */
  117. ret = hs_cache_lookup_as_dir(3, "blah", NULL);
  118. tt_int_op(ret, OP_EQ, -1);
  119. /* Decodable base64 query but wrong ed25519 size. */
  120. ret = hs_cache_lookup_as_dir(3, "dW5pY29ybg==", NULL);
  121. tt_int_op(ret, OP_EQ, -1);
  122. }
  123. /* Test descriptor replacement with revision counter. */
  124. {
  125. char *new_desc_str;
  126. /* Add a descriptor. */
  127. ret = hs_cache_store_as_dir(desc1_str);
  128. tt_int_op(ret, OP_EQ, 0);
  129. ret = hs_cache_lookup_as_dir(3, helper_get_hsdir_query(desc1), &desc_out);
  130. tt_int_op(ret, OP_EQ, 1);
  131. /* Bump revision counter. */
  132. desc1->plaintext_data.revision_counter++;
  133. ret = hs_desc_encode_descriptor(desc1, &signing_kp1, &new_desc_str);
  134. tt_int_op(ret, OP_EQ, 0);
  135. ret = hs_cache_store_as_dir(new_desc_str);
  136. tt_int_op(ret, OP_EQ, 0);
  137. /* Look it up, it should have been replaced. */
  138. ret = hs_cache_lookup_as_dir(3, helper_get_hsdir_query(desc1), &desc_out);
  139. tt_int_op(ret, OP_EQ, 1);
  140. tt_str_op(desc_out, OP_EQ, new_desc_str);
  141. tor_free(new_desc_str);
  142. }
  143. done:
  144. hs_descriptor_free(desc1);
  145. tor_free(desc1_str);
  146. }
  147. static void
  148. test_clean_as_dir(void *arg)
  149. {
  150. size_t ret;
  151. char *desc1_str = NULL;
  152. time_t now = time(NULL);
  153. hs_descriptor_t *desc1 = NULL;
  154. ed25519_keypair_t signing_kp1;
  155. (void) arg;
  156. init_test();
  157. /* Generate a valid descriptor with values. */
  158. ret = ed25519_keypair_generate(&signing_kp1, 0);
  159. tt_int_op(ret, ==, 0);
  160. desc1 = hs_helper_build_hs_desc_with_ip(&signing_kp1);
  161. tt_assert(desc1);
  162. ret = hs_desc_encode_descriptor(desc1, &signing_kp1, &desc1_str);
  163. tt_int_op(ret, OP_EQ, 0);
  164. ret = hs_cache_store_as_dir(desc1_str);
  165. tt_int_op(ret, OP_EQ, 0);
  166. /* With the lifetime being 3 hours, a cleanup shouldn't remove it. */
  167. ret = cache_clean_v3_as_dir(now, 0);
  168. tt_int_op(ret, ==, 0);
  169. /* Should be present after clean up. */
  170. ret = hs_cache_lookup_as_dir(3, helper_get_hsdir_query(desc1), NULL);
  171. tt_int_op(ret, OP_EQ, 1);
  172. /* Set a cutoff 100 seconds in the past. It should not remove the entry
  173. * since the entry is still recent enough. */
  174. ret = cache_clean_v3_as_dir(now, now - 100);
  175. tt_int_op(ret, ==, 0);
  176. /* Should be present after clean up. */
  177. ret = hs_cache_lookup_as_dir(3, helper_get_hsdir_query(desc1), NULL);
  178. tt_int_op(ret, OP_EQ, 1);
  179. /* Set a cutoff of 100 seconds in the future. It should remove the entry
  180. * that we've just added since it's not too old for the cutoff. */
  181. ret = cache_clean_v3_as_dir(now, now + 100);
  182. tt_int_op(ret, >, 0);
  183. /* Shouldn't be present after clean up. */
  184. ret = hs_cache_lookup_as_dir(3, helper_get_hsdir_query(desc1), NULL);
  185. tt_int_op(ret, OP_EQ, 0);
  186. done:
  187. hs_descriptor_free(desc1);
  188. tor_free(desc1_str);
  189. }
  190. /* Test helper: Fetch an HS descriptor from an HSDir (for the hidden service
  191. with <b>blinded_key</b>. Return the received descriptor string. */
  192. static char *
  193. helper_fetch_desc_from_hsdir(const ed25519_public_key_t *blinded_key)
  194. {
  195. int retval;
  196. char *received_desc = NULL;
  197. char *hsdir_query_str = NULL;
  198. /* The dir conn we are going to simulate */
  199. dir_connection_t *conn = NULL;
  200. /* First extract the blinded public key that we are going to use in our
  201. query, and then build the actual query string. */
  202. {
  203. char hsdir_cache_key[ED25519_BASE64_LEN+1];
  204. retval = ed25519_public_to_base64(hsdir_cache_key,
  205. blinded_key);
  206. tt_int_op(retval, ==, 0);
  207. tor_asprintf(&hsdir_query_str, GET("/tor/hs/3/%s"), hsdir_cache_key);
  208. }
  209. /* Simulate an HTTP GET request to the HSDir */
  210. conn = dir_connection_new(AF_INET);
  211. tor_addr_from_ipv4h(&conn->base_.addr, 0x7f000001);
  212. TO_CONN(conn)->linked = 1;/* Pretend the conn is encrypted :) */
  213. retval = directory_handle_command_get(conn, hsdir_query_str,
  214. NULL, 0);
  215. tt_int_op(retval, OP_EQ, 0);
  216. /* Read the descriptor that the HSDir just served us */
  217. {
  218. char *headers = NULL;
  219. size_t body_used = 0;
  220. fetch_from_buf_http(TO_CONN(conn)->outbuf, &headers, MAX_HEADERS_SIZE,
  221. &received_desc, &body_used, HS_DESC_MAX_LEN, 0);
  222. tor_free(headers);
  223. }
  224. done:
  225. tor_free(hsdir_query_str);
  226. if (conn)
  227. connection_free_(TO_CONN(conn));
  228. return received_desc;
  229. }
  230. /* Publish a descriptor to the HSDir, then fetch it. Check that the received
  231. descriptor matches the published one. */
  232. static void
  233. test_upload_and_download_hs_desc(void *arg)
  234. {
  235. int retval;
  236. hs_descriptor_t *published_desc = NULL;
  237. char *published_desc_str = NULL;
  238. char *received_desc_str = NULL;
  239. (void) arg;
  240. /* Initialize HSDir cache subsystem */
  241. init_test();
  242. /* Test a descriptor not found in the directory cache. */
  243. {
  244. ed25519_public_key_t blinded_key;
  245. memset(&blinded_key.pubkey, 'A', sizeof(blinded_key.pubkey));
  246. received_desc_str = helper_fetch_desc_from_hsdir(&blinded_key);
  247. tt_int_op(strlen(received_desc_str), OP_EQ, 0);
  248. tor_free(received_desc_str);
  249. }
  250. /* Generate a valid descriptor with normal values. */
  251. {
  252. ed25519_keypair_t signing_kp;
  253. retval = ed25519_keypair_generate(&signing_kp, 0);
  254. tt_int_op(retval, ==, 0);
  255. published_desc = hs_helper_build_hs_desc_with_ip(&signing_kp);
  256. tt_assert(published_desc);
  257. retval = hs_desc_encode_descriptor(published_desc, &signing_kp,
  258. &published_desc_str);
  259. tt_int_op(retval, OP_EQ, 0);
  260. }
  261. /* Publish descriptor to the HSDir */
  262. {
  263. retval = handle_post_hs_descriptor("/tor/hs/3/publish",published_desc_str);
  264. tt_int_op(retval, ==, 200);
  265. }
  266. /* Simulate a fetch of the previously published descriptor */
  267. {
  268. const ed25519_public_key_t *blinded_key;
  269. blinded_key = &published_desc->plaintext_data.blinded_pubkey;
  270. received_desc_str = helper_fetch_desc_from_hsdir(blinded_key);
  271. }
  272. /* Verify we received the exact same descriptor we published earlier */
  273. tt_str_op(received_desc_str, OP_EQ, published_desc_str);
  274. tor_free(received_desc_str);
  275. /* With a valid descriptor in the directory cache, try again an invalid. */
  276. {
  277. ed25519_public_key_t blinded_key;
  278. memset(&blinded_key.pubkey, 'A', sizeof(blinded_key.pubkey));
  279. received_desc_str = helper_fetch_desc_from_hsdir(&blinded_key);
  280. tt_int_op(strlen(received_desc_str), OP_EQ, 0);
  281. }
  282. done:
  283. tor_free(received_desc_str);
  284. tor_free(published_desc_str);
  285. hs_descriptor_free(published_desc);
  286. }
  287. /* Test that HSDirs reject outdated descriptors based on their revision
  288. * counter. Also test that HSDirs correctly replace old descriptors with newer
  289. * descriptors. */
  290. static void
  291. test_hsdir_revision_counter_check(void *arg)
  292. {
  293. int retval;
  294. ed25519_keypair_t signing_kp;
  295. hs_descriptor_t *published_desc = NULL;
  296. char *published_desc_str = NULL;
  297. uint8_t subcredential[DIGEST256_LEN];
  298. char *received_desc_str = NULL;
  299. hs_descriptor_t *received_desc = NULL;
  300. (void) arg;
  301. /* Initialize HSDir cache subsystem */
  302. init_test();
  303. /* Generate a valid descriptor with normal values. */
  304. {
  305. retval = ed25519_keypair_generate(&signing_kp, 0);
  306. tt_int_op(retval, ==, 0);
  307. published_desc = hs_helper_build_hs_desc_with_ip(&signing_kp);
  308. tt_assert(published_desc);
  309. retval = hs_desc_encode_descriptor(published_desc, &signing_kp,
  310. &published_desc_str);
  311. tt_int_op(retval, OP_EQ, 0);
  312. }
  313. /* Publish descriptor to the HSDir */
  314. {
  315. retval = handle_post_hs_descriptor("/tor/hs/3/publish",published_desc_str);
  316. tt_int_op(retval, ==, 200);
  317. }
  318. /* Try publishing again with the same revision counter: Should fail. */
  319. {
  320. retval = handle_post_hs_descriptor("/tor/hs/3/publish",published_desc_str);
  321. tt_int_op(retval, ==, 400);
  322. }
  323. /* Fetch the published descriptor and validate the revision counter. */
  324. {
  325. const ed25519_public_key_t *blinded_key;
  326. blinded_key = &published_desc->plaintext_data.blinded_pubkey;
  327. hs_get_subcredential(&signing_kp.pubkey, blinded_key, subcredential);
  328. received_desc_str = helper_fetch_desc_from_hsdir(blinded_key);
  329. retval = hs_desc_decode_descriptor(received_desc_str,
  330. subcredential, &received_desc);
  331. tt_int_op(retval, ==, 0);
  332. tt_assert(received_desc);
  333. /* Check that the revision counter is correct */
  334. tt_u64_op(received_desc->plaintext_data.revision_counter, ==, 42);
  335. hs_descriptor_free(received_desc);
  336. received_desc = NULL;
  337. tor_free(received_desc_str);
  338. }
  339. /* Increment the revision counter and try again. Should work. */
  340. {
  341. published_desc->plaintext_data.revision_counter = 1313;
  342. tor_free(published_desc_str);
  343. retval = hs_desc_encode_descriptor(published_desc, &signing_kp,
  344. &published_desc_str);
  345. tt_int_op(retval, OP_EQ, 0);
  346. retval = handle_post_hs_descriptor("/tor/hs/3/publish",published_desc_str);
  347. tt_int_op(retval, ==, 200);
  348. }
  349. /* Again, fetch the published descriptor and perform the revision counter
  350. validation. The revision counter must have changed. */
  351. {
  352. const ed25519_public_key_t *blinded_key;
  353. blinded_key = &published_desc->plaintext_data.blinded_pubkey;
  354. received_desc_str = helper_fetch_desc_from_hsdir(blinded_key);
  355. retval = hs_desc_decode_descriptor(received_desc_str,
  356. subcredential, &received_desc);
  357. tt_int_op(retval, ==, 0);
  358. tt_assert(received_desc);
  359. /* Check that the revision counter is the latest */
  360. tt_u64_op(received_desc->plaintext_data.revision_counter, ==, 1313);
  361. }
  362. done:
  363. hs_descriptor_free(published_desc);
  364. hs_descriptor_free(received_desc);
  365. tor_free(received_desc_str);
  366. tor_free(published_desc_str);
  367. }
  368. struct testcase_t hs_cache[] = {
  369. /* Encoding tests. */
  370. { "directory", test_directory, TT_FORK,
  371. NULL, NULL },
  372. { "clean_as_dir", test_clean_as_dir, TT_FORK,
  373. NULL, NULL },
  374. { "hsdir_revision_counter_check", test_hsdir_revision_counter_check, TT_FORK,
  375. NULL, NULL },
  376. { "upload_and_download_hs_desc", test_upload_and_download_hs_desc, TT_FORK,
  377. NULL, NULL },
  378. END_OF_TESTCASES
  379. };