test_hs_cache.c 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510
  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_public_key_t *signing_pubkey)
  61. {
  62. int ret;
  63. ed25519_keypair_t blinded_kp;
  64. hs_descriptor_t *descp = NULL, *desc = tor_malloc_zero(sizeof(*desc));
  65. desc->plaintext_data.version = HS_DESC_SUPPORTED_FORMAT_VERSION_MAX;
  66. /* Copy only the public key into the descriptor. */
  67. memcpy(&desc->plaintext_data.signing_pubkey, signing_pubkey,
  68. sizeof(ed25519_public_key_t));
  69. ret = ed25519_keypair_generate(&blinded_kp, 0);
  70. tt_int_op(ret, ==, 0);
  71. /* Copy only the public key into the descriptor. */
  72. memcpy(&desc->plaintext_data.blinded_pubkey, &blinded_kp.pubkey,
  73. sizeof(ed25519_public_key_t));
  74. desc->plaintext_data.signing_key_cert =
  75. tor_cert_create(&blinded_kp, CERT_TYPE_SIGNING_HS_DESC, signing_pubkey,
  76. time(NULL), 3600, CERT_FLAG_INCLUDE_SIGNING_KEY);
  77. tt_assert(desc->plaintext_data.signing_key_cert);
  78. desc->plaintext_data.revision_counter = revision_counter;
  79. desc->plaintext_data.lifetime_sec = lifetime;
  80. /* Setup encrypted data section. */
  81. desc->encrypted_data.create2_ntor = 1;
  82. desc->encrypted_data.auth_types = smartlist_new();
  83. smartlist_add(desc->encrypted_data.auth_types, tor_strdup("ed25519"));
  84. desc->encrypted_data.intro_points = smartlist_new();
  85. /* Add an intro point. */
  86. smartlist_add(desc->encrypted_data.intro_points,
  87. helper_build_intro_point(&blinded_kp, "1.2.3.4"));
  88. descp = desc;
  89. done:
  90. return descp;
  91. }
  92. /* Static variable used to encoded the HSDir query. */
  93. static char query_b64[256];
  94. /* Build an HSDir query using a ed25519 public key. */
  95. static const char *
  96. helper_get_hsdir_query(const hs_descriptor_t *desc)
  97. {
  98. ed25519_public_to_base64(query_b64, &desc->plaintext_data.blinded_pubkey);
  99. return query_b64;
  100. }
  101. static void
  102. init_test(void)
  103. {
  104. /* Always needed. Initialize the subsystem. */
  105. hs_cache_init();
  106. /* We need the v2 cache since our OOM and cache cleanup does poke at it. */
  107. rend_cache_init();
  108. }
  109. static void
  110. test_directory(void *arg)
  111. {
  112. int ret;
  113. size_t oom_size;
  114. char *desc1_str = NULL;
  115. const char *desc_out;
  116. ed25519_keypair_t signing_kp1;
  117. hs_descriptor_t *desc1 = NULL;
  118. (void) arg;
  119. init_test();
  120. /* Generate a valid descriptor with normal values. */
  121. ret = ed25519_keypair_generate(&signing_kp1, 0);
  122. tt_int_op(ret, ==, 0);
  123. desc1 = helper_build_hs_desc(42, 3 * 60 * 60, &signing_kp1.pubkey);
  124. tt_assert(desc1);
  125. ret = hs_desc_encode_descriptor(desc1, &signing_kp1, &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. ed25519_keypair_t signing_kp_zero;
  152. ret = ed25519_keypair_generate(&signing_kp_zero, 0);
  153. tt_int_op(ret, ==, 0);
  154. hs_descriptor_t *desc_zero_lifetime;
  155. desc_zero_lifetime = helper_build_hs_desc(1, 0, &signing_kp_zero.pubkey);
  156. tt_assert(desc_zero_lifetime);
  157. char *desc_zero_lifetime_str;
  158. ret = hs_desc_encode_descriptor(desc_zero_lifetime, &signing_kp_zero,
  159. &desc_zero_lifetime_str);
  160. tt_int_op(ret, OP_EQ, 0);
  161. ret = hs_cache_store_as_dir(desc1_str);
  162. tt_int_op(ret, OP_EQ, 0);
  163. ret = hs_cache_store_as_dir(desc_zero_lifetime_str);
  164. tt_int_op(ret, OP_EQ, 0);
  165. /* This one should clear out our zero lifetime desc. */
  166. hs_cache_clean_as_dir(time(NULL));
  167. /* We should find desc1 in our cache. */
  168. ret = hs_cache_lookup_as_dir(3, helper_get_hsdir_query(desc1), &desc_out);
  169. tt_int_op(ret, OP_EQ, 1);
  170. tt_str_op(desc_out, OP_EQ, desc1_str);
  171. /* We should NOT find our zero lifetime desc in our cache. */
  172. ret = hs_cache_lookup_as_dir(3,
  173. helper_get_hsdir_query(desc_zero_lifetime),
  174. NULL);
  175. tt_int_op(ret, OP_EQ, 0);
  176. /* Cleanup our entire cache. */
  177. oom_size = hs_cache_handle_oom(time(NULL), 1);
  178. tt_int_op(oom_size, >=, 1);
  179. hs_descriptor_free(desc_zero_lifetime);
  180. tor_free(desc_zero_lifetime_str);
  181. }
  182. /* Throw junk at it. */
  183. {
  184. ret = hs_cache_store_as_dir("blah");
  185. tt_int_op(ret, OP_EQ, -1);
  186. /* Poor attempt at tricking the decoding. */
  187. ret = hs_cache_store_as_dir("hs-descriptor 3\nJUNK");
  188. tt_int_op(ret, OP_EQ, -1);
  189. /* Undecodable base64 query. */
  190. ret = hs_cache_lookup_as_dir(3, "blah", NULL);
  191. tt_int_op(ret, OP_EQ, -1);
  192. /* Decodable base64 query but wrong ed25519 size. */
  193. ret = hs_cache_lookup_as_dir(3, "dW5pY29ybg==", NULL);
  194. tt_int_op(ret, OP_EQ, -1);
  195. }
  196. /* Test descriptor replacement with revision counter. */
  197. {
  198. char *new_desc_str;
  199. /* Add a descriptor. */
  200. ret = hs_cache_store_as_dir(desc1_str);
  201. tt_int_op(ret, OP_EQ, 0);
  202. ret = hs_cache_lookup_as_dir(3, helper_get_hsdir_query(desc1), &desc_out);
  203. tt_int_op(ret, OP_EQ, 1);
  204. /* Bump revision counter. */
  205. desc1->plaintext_data.revision_counter++;
  206. ret = hs_desc_encode_descriptor(desc1, &signing_kp1, &new_desc_str);
  207. tt_int_op(ret, OP_EQ, 0);
  208. ret = hs_cache_store_as_dir(new_desc_str);
  209. tt_int_op(ret, OP_EQ, 0);
  210. /* Look it up, it should have been replaced. */
  211. ret = hs_cache_lookup_as_dir(3, helper_get_hsdir_query(desc1), &desc_out);
  212. tt_int_op(ret, OP_EQ, 1);
  213. tt_str_op(desc_out, OP_EQ, new_desc_str);
  214. tor_free(new_desc_str);
  215. }
  216. done:
  217. hs_descriptor_free(desc1);
  218. tor_free(desc1_str);
  219. }
  220. static void
  221. test_clean_as_dir(void *arg)
  222. {
  223. size_t ret;
  224. char *desc1_str = NULL;
  225. time_t now = time(NULL);
  226. hs_descriptor_t *desc1 = NULL;
  227. ed25519_keypair_t signing_kp1;
  228. (void) arg;
  229. init_test();
  230. /* Generate a valid descriptor with values. */
  231. ret = ed25519_keypair_generate(&signing_kp1, 0);
  232. tt_int_op(ret, ==, 0);
  233. desc1 = helper_build_hs_desc(42, 3 * 60 * 60, &signing_kp1.pubkey);
  234. tt_assert(desc1);
  235. ret = hs_desc_encode_descriptor(desc1, &signing_kp1, &desc1_str);
  236. tt_int_op(ret, OP_EQ, 0);
  237. ret = hs_cache_store_as_dir(desc1_str);
  238. tt_int_op(ret, OP_EQ, 0);
  239. /* With the lifetime being 3 hours, a cleanup shouldn't remove it. */
  240. ret = cache_clean_v3_as_dir(now, 0);
  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 100 seconds in the past. It should not remove the entry
  246. * since the entry is still recent enough. */
  247. ret = cache_clean_v3_as_dir(now, now - 100);
  248. tt_int_op(ret, ==, 0);
  249. /* Should 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, 1);
  252. /* Set a cutoff of 100 seconds in the future. It should remove the entry
  253. * that we've just added since it's not too old for the cutoff. */
  254. ret = cache_clean_v3_as_dir(now, now + 100);
  255. tt_int_op(ret, >, 0);
  256. /* Shouldn't be present after clean up. */
  257. ret = hs_cache_lookup_as_dir(3, helper_get_hsdir_query(desc1), NULL);
  258. tt_int_op(ret, OP_EQ, 0);
  259. done:
  260. hs_descriptor_free(desc1);
  261. tor_free(desc1_str);
  262. }
  263. /* Test helper: Fetch an HS descriptor from an HSDir (for the hidden service
  264. with <b>blinded_key</b>. Return the received descriptor string. */
  265. static char *
  266. helper_fetch_desc_from_hsdir(const ed25519_public_key_t *blinded_key)
  267. {
  268. int retval;
  269. char *received_desc = NULL;
  270. char *hsdir_query_str = NULL;
  271. /* The dir conn we are going to simulate */
  272. dir_connection_t *conn = NULL;
  273. /* First extract the blinded public key that we are going to use in our
  274. query, and then build the actual query string. */
  275. {
  276. char hsdir_cache_key[ED25519_BASE64_LEN+1];
  277. retval = ed25519_public_to_base64(hsdir_cache_key,
  278. blinded_key);
  279. tt_int_op(retval, ==, 0);
  280. tor_asprintf(&hsdir_query_str, GET("/tor/hs/3/%s"), hsdir_cache_key);
  281. }
  282. /* Simulate an HTTP GET request to the HSDir */
  283. conn = dir_connection_new(AF_INET);
  284. tor_addr_from_ipv4h(&conn->base_.addr, 0x7f000001);
  285. TO_CONN(conn)->linked = 1;/* Pretend the conn is encrypted :) */
  286. retval = directory_handle_command_get(conn, hsdir_query_str,
  287. NULL, 0);
  288. tt_int_op(retval, OP_EQ, 0);
  289. /* Read the descriptor that the HSDir just served us */
  290. {
  291. char *headers = NULL;
  292. size_t body_used = 0;
  293. fetch_from_buf_http(TO_CONN(conn)->outbuf, &headers, MAX_HEADERS_SIZE,
  294. &received_desc, &body_used, 10000, 0);
  295. tor_free(headers);
  296. }
  297. done:
  298. tor_free(hsdir_query_str);
  299. if (conn)
  300. connection_free_(TO_CONN(conn));
  301. return received_desc;
  302. }
  303. /* Publish a descriptor to the HSDir, then fetch it. Check that the received
  304. descriptor matches the published one. */
  305. static void
  306. test_upload_and_download_hs_desc(void *arg)
  307. {
  308. int retval;
  309. hs_descriptor_t *published_desc = NULL;
  310. char *published_desc_str = NULL;
  311. char *received_desc_str = NULL;
  312. (void) arg;
  313. /* Initialize HSDir cache subsystem */
  314. init_test();
  315. /* Generate a valid descriptor with normal values. */
  316. {
  317. ed25519_keypair_t signing_kp;
  318. retval = ed25519_keypair_generate(&signing_kp, 0);
  319. tt_int_op(retval, ==, 0);
  320. published_desc = helper_build_hs_desc(42, 3 * 60 * 60, &signing_kp.pubkey);
  321. tt_assert(published_desc);
  322. retval = hs_desc_encode_descriptor(published_desc, &signing_kp,
  323. &published_desc_str);
  324. tt_int_op(retval, OP_EQ, 0);
  325. }
  326. /* Publish descriptor to the HSDir */
  327. {
  328. retval = handle_post_hs_descriptor("/tor/hs/3/publish",published_desc_str);
  329. tt_int_op(retval, ==, 200);
  330. }
  331. /* Simulate a fetch of the previously published descriptor */
  332. {
  333. const ed25519_public_key_t *blinded_key;
  334. blinded_key = &published_desc->plaintext_data.blinded_pubkey;
  335. received_desc_str = helper_fetch_desc_from_hsdir(blinded_key);
  336. }
  337. /* Verify we received the exact same descriptor we published earlier */
  338. tt_str_op(received_desc_str, OP_EQ, published_desc_str);
  339. done:
  340. tor_free(received_desc_str);
  341. tor_free(published_desc_str);
  342. hs_descriptor_free(published_desc);
  343. }
  344. /* Test that HSDirs reject outdated descriptors based on their revision
  345. * counter. Also test that HSDirs correctly replace old descriptors with newer
  346. * descriptors. */
  347. static void
  348. test_hsdir_revision_counter_check(void *arg)
  349. {
  350. int retval;
  351. ed25519_keypair_t signing_kp;
  352. hs_descriptor_t *published_desc = NULL;
  353. char *published_desc_str = NULL;
  354. char *received_desc_str = NULL;
  355. hs_descriptor_t *received_desc = NULL;
  356. (void) arg;
  357. /* Initialize HSDir cache subsystem */
  358. init_test();
  359. /* Generate a valid descriptor with normal values. */
  360. {
  361. retval = ed25519_keypair_generate(&signing_kp, 0);
  362. tt_int_op(retval, ==, 0);
  363. published_desc = helper_build_hs_desc(1312, 3 * 60 * 60,
  364. &signing_kp.pubkey);
  365. tt_assert(published_desc);
  366. retval = hs_desc_encode_descriptor(published_desc, &signing_kp,
  367. &published_desc_str);
  368. tt_int_op(retval, OP_EQ, 0);
  369. }
  370. /* Publish descriptor to the HSDir */
  371. {
  372. retval = handle_post_hs_descriptor("/tor/hs/3/publish",published_desc_str);
  373. tt_int_op(retval, ==, 200);
  374. }
  375. /* Try publishing again with the same revision counter: Should fail. */
  376. {
  377. retval = handle_post_hs_descriptor("/tor/hs/3/publish",published_desc_str);
  378. tt_int_op(retval, ==, 400);
  379. }
  380. /* Fetch the published descriptor and validate the revision counter. */
  381. {
  382. const ed25519_public_key_t *blinded_key;
  383. blinded_key = &published_desc->plaintext_data.blinded_pubkey;
  384. received_desc_str = helper_fetch_desc_from_hsdir(blinded_key);
  385. retval = hs_desc_decode_descriptor(received_desc_str,NULL, &received_desc);
  386. tt_int_op(retval, ==, 0);
  387. tt_assert(received_desc);
  388. /* Check that the revision counter is correct */
  389. tt_u64_op(received_desc->plaintext_data.revision_counter, ==, 1312);
  390. hs_descriptor_free(received_desc);
  391. received_desc = NULL;
  392. tor_free(received_desc_str);
  393. }
  394. /* Increment the revision counter and try again. Should work. */
  395. {
  396. published_desc->plaintext_data.revision_counter = 1313;
  397. tor_free(published_desc_str);
  398. retval = hs_desc_encode_descriptor(published_desc, &signing_kp,
  399. &published_desc_str);
  400. tt_int_op(retval, OP_EQ, 0);
  401. retval = handle_post_hs_descriptor("/tor/hs/3/publish",published_desc_str);
  402. tt_int_op(retval, ==, 200);
  403. }
  404. /* Again, fetch the published descriptor and perform the revision counter
  405. validation. The revision counter must have changed. */
  406. {
  407. const ed25519_public_key_t *blinded_key;
  408. blinded_key = &published_desc->plaintext_data.blinded_pubkey;
  409. received_desc_str = helper_fetch_desc_from_hsdir(blinded_key);
  410. retval = hs_desc_decode_descriptor(received_desc_str,NULL, &received_desc);
  411. tt_int_op(retval, ==, 0);
  412. tt_assert(received_desc);
  413. /* Check that the revision counter is the latest */
  414. tt_u64_op(received_desc->plaintext_data.revision_counter, ==, 1313);
  415. }
  416. done:
  417. hs_descriptor_free(published_desc);
  418. hs_descriptor_free(received_desc);
  419. tor_free(received_desc_str);
  420. tor_free(published_desc_str);
  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. END_OF_TESTCASES
  433. };