test_hs_cache.c 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479
  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 *desc1_str;
  118. const char *desc_out;
  119. hs_descriptor_t *desc1;
  120. (void) arg;
  121. init_test();
  122. /* Generate a valid descriptor with normal values. */
  123. desc1 = helper_build_hs_desc(42, 3 * 60 * 60, NULL);
  124. tt_assert(desc1);
  125. ret = hs_desc_encode_descriptor(desc1, &desc1_str);
  126. tt_int_op(ret, OP_EQ, 0);
  127. /* Very first basic test, should be able to be stored, survive a
  128. * clean, found with a lookup and then cleaned by our OOM. */
  129. {
  130. ret = hs_cache_store_as_dir(desc1_str);
  131. tt_int_op(ret, OP_EQ, 0);
  132. /* Re-add, it should fail since we already have it. */
  133. ret = hs_cache_store_as_dir(desc1_str);
  134. tt_int_op(ret, OP_EQ, -1);
  135. /* Try to clean now which should be fine, there is at worst few seconds
  136. * between the store and this call. */
  137. hs_cache_clean_as_dir(time(NULL));
  138. /* We should find it in our cache. */
  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, desc1_str);
  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. /* We should NOT find our zero lifetime desc in our cache. */
  168. ret = hs_cache_lookup_as_dir(3,
  169. helper_get_hsdir_query(desc_zero_lifetime),
  170. NULL);
  171. tt_int_op(ret, OP_EQ, 0);
  172. /* Cleanup our entire cache. */
  173. oom_size = hs_cache_handle_oom(time(NULL), 1);
  174. tt_int_op(oom_size, >=, 1);
  175. }
  176. /* Throw junk at it. */
  177. {
  178. ret = hs_cache_store_as_dir("blah");
  179. tt_int_op(ret, OP_EQ, -1);
  180. /* Poor attempt at tricking the decoding. */
  181. ret = hs_cache_store_as_dir("hs-descriptor 3\nJUNK");
  182. tt_int_op(ret, OP_EQ, -1);
  183. /* Undecodable base64 query. */
  184. ret = hs_cache_lookup_as_dir(3, "blah", NULL);
  185. tt_int_op(ret, OP_EQ, -1);
  186. /* Decodable base64 query but wrong ed25519 size. */
  187. ret = hs_cache_lookup_as_dir(3, "dW5pY29ybg==", NULL);
  188. tt_int_op(ret, OP_EQ, -1);
  189. }
  190. /* Test descriptor replacement with revision counter. */
  191. {
  192. char *new_desc_str;
  193. /* Add a descriptor. */
  194. ret = hs_cache_store_as_dir(desc1_str);
  195. tt_int_op(ret, OP_EQ, 0);
  196. ret = hs_cache_lookup_as_dir(3, helper_get_hsdir_query(desc1), &desc_out);
  197. tt_int_op(ret, OP_EQ, 1);
  198. /* Bump revision counter. */
  199. desc1->plaintext_data.revision_counter++;
  200. ret = hs_desc_encode_descriptor(desc1, &new_desc_str);
  201. tt_int_op(ret, OP_EQ, 0);
  202. ret = hs_cache_store_as_dir(new_desc_str);
  203. tt_int_op(ret, OP_EQ, 0);
  204. /* Look it up, it should have been replaced. */
  205. ret = hs_cache_lookup_as_dir(3, helper_get_hsdir_query(desc1), &desc_out);
  206. tt_int_op(ret, OP_EQ, 1);
  207. tt_str_op(desc_out, OP_EQ, new_desc_str);
  208. tor_free(new_desc_str);
  209. }
  210. done:
  211. ;
  212. }
  213. static void
  214. test_clean_as_dir(void *arg)
  215. {
  216. size_t ret;
  217. char *desc1_str = NULL;
  218. time_t now = time(NULL);
  219. hs_descriptor_t *desc1 = NULL;
  220. (void) arg;
  221. init_test();
  222. /* Generate a valid descriptor with values. */
  223. desc1 = helper_build_hs_desc(42, 3 * 60 * 60, NULL);
  224. tt_assert(desc1);
  225. ret = hs_desc_encode_descriptor(desc1, &desc1_str);
  226. tt_int_op(ret, OP_EQ, 0);
  227. ret = hs_cache_store_as_dir(desc1_str);
  228. tt_int_op(ret, OP_EQ, 0);
  229. /* With the lifetime being 3 hours, a cleanup shouldn't remove it. */
  230. ret = cache_clean_v3_as_dir(now, 0);
  231. tt_int_op(ret, ==, 0);
  232. /* Should be present after clean up. */
  233. ret = hs_cache_lookup_as_dir(3, helper_get_hsdir_query(desc1), NULL);
  234. tt_int_op(ret, OP_EQ, 1);
  235. /* Set a cutoff 100 seconds in the past. It should not remove the entry
  236. * since the entry is still recent enough. */
  237. ret = cache_clean_v3_as_dir(now, now - 100);
  238. tt_int_op(ret, ==, 0);
  239. /* Should be present after clean up. */
  240. ret = hs_cache_lookup_as_dir(3, helper_get_hsdir_query(desc1), NULL);
  241. tt_int_op(ret, OP_EQ, 1);
  242. /* Set a cutoff of 100 seconds in the future. It should remove the entry
  243. * that we've just added since it's not too old for the cutoff. */
  244. ret = cache_clean_v3_as_dir(now, now + 100);
  245. tt_int_op(ret, >, 0);
  246. /* Shouldn't be present after clean up. */
  247. ret = hs_cache_lookup_as_dir(3, helper_get_hsdir_query(desc1), NULL);
  248. tt_int_op(ret, OP_EQ, 0);
  249. done:
  250. hs_descriptor_free(desc1);
  251. tor_free(desc1_str);
  252. }
  253. /* Test helper: Fetch an HS descriptor from an HSDir (for the hidden service
  254. with <b>blinded_key</b>. Return the received descriptor string. */
  255. static char *
  256. helper_fetch_desc_from_hsdir(const ed25519_public_key_t *blinded_key)
  257. {
  258. int retval;
  259. char *received_desc = NULL;
  260. char *hsdir_query_str = NULL;
  261. /* The dir conn we are going to simulate */
  262. dir_connection_t *conn = NULL;
  263. tor_addr_t mock_tor_addr;
  264. /* First extract the blinded public key that we are going to use in our
  265. query, and then build the actual query string. */
  266. {
  267. char hsdir_cache_key[ED25519_BASE64_LEN+1];
  268. retval = ed25519_public_to_base64(hsdir_cache_key,
  269. blinded_key);
  270. tt_int_op(retval, ==, 0);
  271. tor_asprintf(&hsdir_query_str, GET("/tor/hs/3/%s"), hsdir_cache_key);
  272. }
  273. /* Simulate an HTTP GET request to the HSDir */
  274. conn = dir_connection_new(tor_addr_family(&mock_tor_addr));
  275. TO_CONN(conn)->linked = 1;/* Pretend the conn is encrypted :) */
  276. retval = directory_handle_command_get(conn, hsdir_query_str,
  277. NULL, 0);
  278. tt_int_op(retval, OP_EQ, 0);
  279. /* Read the descriptor that the HSDir just served us */
  280. {
  281. char *headers = NULL;
  282. size_t body_used = 0;
  283. fetch_from_buf_http(TO_CONN(conn)->outbuf, &headers, MAX_HEADERS_SIZE,
  284. &received_desc, &body_used, 10000, 0);
  285. }
  286. done:
  287. tor_free(hsdir_query_str);
  288. return received_desc;
  289. }
  290. /* Publish a descriptor to the HSDir, then fetch it. Check that the received
  291. descriptor matches the published one. */
  292. static void
  293. test_upload_and_download_hs_desc(void *arg)
  294. {
  295. int retval;
  296. hs_descriptor_t *published_desc;
  297. char *published_desc_str = NULL;
  298. char *received_desc_str = NULL;
  299. (void) arg;
  300. /* Initialize HSDir cache subsystem */
  301. init_test();
  302. /* Generate a valid descriptor with normal values. */
  303. {
  304. published_desc = helper_build_hs_desc(42, 3 * 60 * 60, NULL);
  305. tt_assert(published_desc);
  306. retval = hs_desc_encode_descriptor(published_desc, &published_desc_str);
  307. tt_int_op(retval, OP_EQ, 0);
  308. }
  309. /* Publish descriptor to the HSDir */
  310. {
  311. retval = handle_post_hs_descriptor("/tor/hs/3/publish",published_desc_str);
  312. tt_int_op(retval, ==, 200);
  313. }
  314. /* Simulate a fetch of the previously published descriptor */
  315. {
  316. const ed25519_public_key_t *blinded_key;
  317. blinded_key = &published_desc->plaintext_data.blinded_kp.pubkey;
  318. received_desc_str = helper_fetch_desc_from_hsdir(blinded_key);
  319. }
  320. /* Verify we received the exact same descriptor we published earlier */
  321. tt_str_op(received_desc_str, OP_EQ, published_desc_str);
  322. done:
  323. tor_free(received_desc_str);
  324. tor_free(published_desc_str);
  325. }
  326. /* Test that HSDirs reject outdated descriptors based on their revision
  327. * counter. Also test that HSDirs correctly replace old descriptors with newer
  328. * descriptors. */
  329. static void
  330. test_hsdir_revision_counter_check(void *arg)
  331. {
  332. int retval;
  333. hs_descriptor_t *published_desc;
  334. char *published_desc_str = NULL;
  335. char *received_desc_str = NULL;
  336. hs_descriptor_t *received_desc = NULL;
  337. (void) arg;
  338. /* Initialize HSDir cache subsystem */
  339. init_test();
  340. /* Generate a valid descriptor with normal values. */
  341. {
  342. published_desc = helper_build_hs_desc(1312, 3 * 60 * 60, NULL);
  343. tt_assert(published_desc);
  344. retval = hs_desc_encode_descriptor(published_desc, &published_desc_str);
  345. tt_int_op(retval, OP_EQ, 0);
  346. }
  347. /* Publish descriptor to the HSDir */
  348. {
  349. retval = handle_post_hs_descriptor("/tor/hs/3/publish",published_desc_str);
  350. tt_int_op(retval, ==, 200);
  351. }
  352. /* Try publishing again with the same revision counter: Should fail. */
  353. {
  354. retval = handle_post_hs_descriptor("/tor/hs/3/publish",published_desc_str);
  355. tt_int_op(retval, ==, 400);
  356. }
  357. /* Fetch the published descriptor and validate the revision counter. */
  358. {
  359. const ed25519_public_key_t *blinded_key;
  360. blinded_key = &published_desc->plaintext_data.blinded_kp.pubkey;
  361. received_desc_str = helper_fetch_desc_from_hsdir(blinded_key);
  362. retval = hs_desc_decode_descriptor(received_desc_str,NULL, &received_desc);
  363. tt_int_op(retval, ==, 0);
  364. tt_assert(received_desc);
  365. /* Check that the revision counter is correct */
  366. tt_int_op(received_desc->plaintext_data.revision_counter, ==, 1312);
  367. }
  368. /* Increment the revision counter and try again. Should work. */
  369. {
  370. published_desc->plaintext_data.revision_counter = 1313;
  371. tor_free(published_desc_str);
  372. retval = hs_desc_encode_descriptor(published_desc, &published_desc_str);
  373. tt_int_op(retval, OP_EQ, 0);
  374. retval = handle_post_hs_descriptor("/tor/hs/3/publish",published_desc_str);
  375. tt_int_op(retval, ==, 200);
  376. }
  377. /* Again, fetch the published descriptor and perform the revision counter
  378. validation. The revision counter must have changed. */
  379. {
  380. const ed25519_public_key_t *blinded_key;
  381. blinded_key = &published_desc->plaintext_data.blinded_kp.pubkey;
  382. received_desc_str = helper_fetch_desc_from_hsdir(blinded_key);
  383. retval = hs_desc_decode_descriptor(received_desc_str,NULL, &received_desc);
  384. tt_int_op(retval, ==, 0);
  385. tt_assert(received_desc);
  386. /* Check that the revision counter is the latest */
  387. tt_int_op(received_desc->plaintext_data.revision_counter, ==, 1313);
  388. }
  389. done:
  390. hs_descriptor_free(published_desc);
  391. hs_descriptor_free(received_desc);
  392. tor_free(received_desc_str);
  393. tor_free(published_desc_str);
  394. }
  395. struct testcase_t hs_cache[] = {
  396. /* Encoding tests. */
  397. { "directory", test_directory, TT_FORK,
  398. NULL, NULL },
  399. { "clean_as_dir", test_clean_as_dir, TT_FORK,
  400. NULL, NULL },
  401. { "hsdir_revision_counter_check", test_hsdir_revision_counter_check, TT_FORK,
  402. NULL, NULL },
  403. { "upload_and_download_hs_desc", test_upload_and_download_hs_desc, TT_FORK,
  404. NULL, NULL },
  405. END_OF_TESTCASES
  406. };