test_hs_cache.c 15 KB

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