test_hs_cache.c 15 KB

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