test_hs_cache.c 16 KB

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