test_hs_cache.c 16 KB

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