hs_cache.c 30 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976
  1. /* Copyright (c) 2016-2017, The Tor Project, Inc. */
  2. /* See LICENSE for licensing information */
  3. /**
  4. * \file hs_cache.c
  5. * \brief Handle hidden service descriptor caches.
  6. **/
  7. /* For unit tests.*/
  8. #define HS_CACHE_PRIVATE
  9. #include "or.h"
  10. #include "config.h"
  11. #include "hs_ident.h"
  12. #include "hs_common.h"
  13. #include "hs_client.h"
  14. #include "hs_descriptor.h"
  15. #include "networkstatus.h"
  16. #include "rendcache.h"
  17. #include "hs_cache.h"
  18. static int cached_client_descriptor_has_expired(time_t now,
  19. const hs_cache_client_descriptor_t *cached_desc);
  20. /********************** Directory HS cache ******************/
  21. /* Directory descriptor cache. Map indexed by blinded key. */
  22. static digest256map_t *hs_cache_v3_dir;
  23. /* Remove a given descriptor from our cache. */
  24. static void
  25. remove_v3_desc_as_dir(const hs_cache_dir_descriptor_t *desc)
  26. {
  27. tor_assert(desc);
  28. digest256map_remove(hs_cache_v3_dir, desc->key);
  29. }
  30. /* Store a given descriptor in our cache. */
  31. static void
  32. store_v3_desc_as_dir(hs_cache_dir_descriptor_t *desc)
  33. {
  34. tor_assert(desc);
  35. digest256map_set(hs_cache_v3_dir, desc->key, desc);
  36. }
  37. /* Query our cache and return the entry or NULL if not found. */
  38. static hs_cache_dir_descriptor_t *
  39. lookup_v3_desc_as_dir(const uint8_t *key)
  40. {
  41. tor_assert(key);
  42. return digest256map_get(hs_cache_v3_dir, key);
  43. }
  44. #define cache_dir_desc_free(val) \
  45. FREE_AND_NULL(hs_cache_dir_descriptor_t, cache_dir_desc_free_, (val))
  46. /* Free a directory descriptor object. */
  47. static void
  48. cache_dir_desc_free_(hs_cache_dir_descriptor_t *desc)
  49. {
  50. if (desc == NULL) {
  51. return;
  52. }
  53. hs_desc_plaintext_data_free(desc->plaintext_data);
  54. tor_free(desc->encoded_desc);
  55. tor_free(desc);
  56. }
  57. /* Helper function: Use by the free all function using the digest256map
  58. * interface to cache entries. */
  59. static void
  60. cache_dir_desc_free_void(void *ptr)
  61. {
  62. cache_dir_desc_free_(ptr);
  63. }
  64. /* Create a new directory cache descriptor object from a encoded descriptor.
  65. * On success, return the heap-allocated cache object, otherwise return NULL if
  66. * we can't decode the descriptor. */
  67. static hs_cache_dir_descriptor_t *
  68. cache_dir_desc_new(const char *desc)
  69. {
  70. hs_cache_dir_descriptor_t *dir_desc;
  71. tor_assert(desc);
  72. dir_desc = tor_malloc_zero(sizeof(hs_cache_dir_descriptor_t));
  73. dir_desc->plaintext_data =
  74. tor_malloc_zero(sizeof(hs_desc_plaintext_data_t));
  75. dir_desc->encoded_desc = tor_strdup(desc);
  76. if (hs_desc_decode_plaintext(desc, dir_desc->plaintext_data) < 0) {
  77. log_debug(LD_DIR, "Unable to decode descriptor. Rejecting.");
  78. goto err;
  79. }
  80. /* The blinded pubkey is the indexed key. */
  81. dir_desc->key = dir_desc->plaintext_data->blinded_pubkey.pubkey;
  82. dir_desc->created_ts = time(NULL);
  83. return dir_desc;
  84. err:
  85. cache_dir_desc_free(dir_desc);
  86. return NULL;
  87. }
  88. /* Return the size of a cache entry in bytes. */
  89. static size_t
  90. cache_get_dir_entry_size(const hs_cache_dir_descriptor_t *entry)
  91. {
  92. return (sizeof(*entry) + hs_desc_plaintext_obj_size(entry->plaintext_data)
  93. + strlen(entry->encoded_desc));
  94. }
  95. /* Try to store a valid version 3 descriptor in the directory cache. Return 0
  96. * on success else a negative value is returned indicating that we have a
  97. * newer version in our cache. On error, caller is responsible to free the
  98. * given descriptor desc. */
  99. static int
  100. cache_store_v3_as_dir(hs_cache_dir_descriptor_t *desc)
  101. {
  102. hs_cache_dir_descriptor_t *cache_entry;
  103. tor_assert(desc);
  104. /* Verify if we have an entry in the cache for that key and if yes, check
  105. * if we should replace it? */
  106. cache_entry = lookup_v3_desc_as_dir(desc->key);
  107. if (cache_entry != NULL) {
  108. /* Only replace descriptor if revision-counter is greater than the one
  109. * in our cache */
  110. if (cache_entry->plaintext_data->revision_counter >=
  111. desc->plaintext_data->revision_counter) {
  112. log_info(LD_REND, "Descriptor revision counter in our cache is "
  113. "greater or equal than the one we received (%d/%d). "
  114. "Rejecting!",
  115. (int)cache_entry->plaintext_data->revision_counter,
  116. (int)desc->plaintext_data->revision_counter);
  117. goto err;
  118. }
  119. /* We now know that the descriptor we just received is a new one so
  120. * remove the entry we currently have from our cache so we can then
  121. * store the new one. */
  122. remove_v3_desc_as_dir(cache_entry);
  123. rend_cache_decrement_allocation(cache_get_dir_entry_size(cache_entry));
  124. cache_dir_desc_free(cache_entry);
  125. }
  126. /* Store the descriptor we just got. We are sure here that either we
  127. * don't have the entry or we have a newer descriptor and the old one
  128. * has been removed from the cache. */
  129. store_v3_desc_as_dir(desc);
  130. /* Update our total cache size with this entry for the OOM. This uses the
  131. * old HS protocol cache subsystem for which we are tied with. */
  132. rend_cache_increment_allocation(cache_get_dir_entry_size(desc));
  133. /* XXX: Update HS statistics. We should have specific stats for v3. */
  134. return 0;
  135. err:
  136. return -1;
  137. }
  138. /* Using the query which is the base64 encoded blinded key of a version 3
  139. * descriptor, lookup in our directory cache the entry. If found, 1 is
  140. * returned and desc_out is populated with a newly allocated string being the
  141. * encoded descriptor. If not found, 0 is returned and desc_out is untouched.
  142. * On error, a negative value is returned and desc_out is untouched. */
  143. static int
  144. cache_lookup_v3_as_dir(const char *query, const char **desc_out)
  145. {
  146. int found = 0;
  147. ed25519_public_key_t blinded_key;
  148. const hs_cache_dir_descriptor_t *entry;
  149. tor_assert(query);
  150. /* Decode blinded key using the given query value. */
  151. if (ed25519_public_from_base64(&blinded_key, query) < 0) {
  152. log_info(LD_REND, "Unable to decode the v3 HSDir query %s.",
  153. safe_str_client(query));
  154. goto err;
  155. }
  156. entry = lookup_v3_desc_as_dir(blinded_key.pubkey);
  157. if (entry != NULL) {
  158. found = 1;
  159. if (desc_out) {
  160. *desc_out = entry->encoded_desc;
  161. }
  162. }
  163. return found;
  164. err:
  165. return -1;
  166. }
  167. /* Clean the v3 cache by removing any entry that has expired using the
  168. * <b>global_cutoff</b> value. If <b>global_cutoff</b> is 0, the cleaning
  169. * process will use the lifetime found in the plaintext data section. Return
  170. * the number of bytes cleaned. */
  171. STATIC size_t
  172. cache_clean_v3_as_dir(time_t now, time_t global_cutoff)
  173. {
  174. size_t bytes_removed = 0;
  175. /* Code flow error if this ever happens. */
  176. tor_assert(global_cutoff >= 0);
  177. if (!hs_cache_v3_dir) { /* No cache to clean. Just return. */
  178. return 0;
  179. }
  180. DIGEST256MAP_FOREACH_MODIFY(hs_cache_v3_dir, key,
  181. hs_cache_dir_descriptor_t *, entry) {
  182. size_t entry_size;
  183. time_t cutoff = global_cutoff;
  184. if (!cutoff) {
  185. /* Cutoff is the lifetime of the entry found in the descriptor. */
  186. cutoff = now - entry->plaintext_data->lifetime_sec;
  187. }
  188. /* If the entry has been created _after_ the cutoff, not expired so
  189. * continue to the next entry in our v3 cache. */
  190. if (entry->created_ts > cutoff) {
  191. continue;
  192. }
  193. /* Here, our entry has expired, remove and free. */
  194. MAP_DEL_CURRENT(key);
  195. entry_size = cache_get_dir_entry_size(entry);
  196. bytes_removed += entry_size;
  197. /* Entry is not in the cache anymore, destroy it. */
  198. cache_dir_desc_free(entry);
  199. /* Update our cache entry allocation size for the OOM. */
  200. rend_cache_decrement_allocation(entry_size);
  201. /* Logging. */
  202. {
  203. char key_b64[BASE64_DIGEST256_LEN + 1];
  204. digest256_to_base64(key_b64, (const char *) key);
  205. log_info(LD_REND, "Removing v3 descriptor '%s' from HSDir cache",
  206. safe_str_client(key_b64));
  207. }
  208. } DIGEST256MAP_FOREACH_END;
  209. return bytes_removed;
  210. }
  211. /* Given an encoded descriptor, store it in the directory cache depending on
  212. * which version it is. Return a negative value on error. On success, 0 is
  213. * returned. */
  214. int
  215. hs_cache_store_as_dir(const char *desc)
  216. {
  217. hs_cache_dir_descriptor_t *dir_desc = NULL;
  218. tor_assert(desc);
  219. /* Create a new cache object. This can fail if the descriptor plaintext data
  220. * is unparseable which in this case a log message will be triggered. */
  221. dir_desc = cache_dir_desc_new(desc);
  222. if (dir_desc == NULL) {
  223. goto err;
  224. }
  225. /* Call the right function against the descriptor version. At this point,
  226. * we are sure that the descriptor's version is supported else the
  227. * decoding would have failed. */
  228. switch (dir_desc->plaintext_data->version) {
  229. case HS_VERSION_THREE:
  230. default:
  231. if (cache_store_v3_as_dir(dir_desc) < 0) {
  232. goto err;
  233. }
  234. break;
  235. }
  236. return 0;
  237. err:
  238. cache_dir_desc_free(dir_desc);
  239. return -1;
  240. }
  241. /* Using the query, lookup in our directory cache the entry. If found, 1 is
  242. * returned and desc_out is populated with a newly allocated string being
  243. * the encoded descriptor. If not found, 0 is returned and desc_out is
  244. * untouched. On error, a negative value is returned and desc_out is
  245. * untouched. */
  246. int
  247. hs_cache_lookup_as_dir(uint32_t version, const char *query,
  248. const char **desc_out)
  249. {
  250. int found;
  251. tor_assert(query);
  252. /* This should never be called with an unsupported version. */
  253. tor_assert(hs_desc_is_supported_version(version));
  254. switch (version) {
  255. case HS_VERSION_THREE:
  256. default:
  257. found = cache_lookup_v3_as_dir(query, desc_out);
  258. break;
  259. }
  260. return found;
  261. }
  262. /* Clean all directory caches using the current time now. */
  263. void
  264. hs_cache_clean_as_dir(time_t now)
  265. {
  266. time_t cutoff;
  267. /* Start with v2 cache cleaning. */
  268. cutoff = now - rend_cache_max_entry_lifetime();
  269. rend_cache_clean_v2_descs_as_dir(cutoff);
  270. /* Now, clean the v3 cache. Set the cutoff to 0 telling the cleanup function
  271. * to compute the cutoff by itself using the lifetime value. */
  272. cache_clean_v3_as_dir(now, 0);
  273. }
  274. /********************** Client-side HS cache ******************/
  275. /* Client-side HS descriptor cache. Map indexed by service identity key. */
  276. static digest256map_t *hs_cache_v3_client;
  277. /* Client-side introduction point state cache. Map indexed by service public
  278. * identity key (onion address). It contains hs_cache_client_intro_state_t
  279. * objects all related to a specific service. */
  280. static digest256map_t *hs_cache_client_intro_state;
  281. /* Return the size of a client cache entry in bytes. */
  282. static size_t
  283. cache_get_client_entry_size(const hs_cache_client_descriptor_t *entry)
  284. {
  285. return sizeof(*entry) +
  286. strlen(entry->encoded_desc) + hs_desc_obj_size(entry->desc);
  287. }
  288. /* Remove a given descriptor from our cache. */
  289. static void
  290. remove_v3_desc_as_client(const hs_cache_client_descriptor_t *desc)
  291. {
  292. tor_assert(desc);
  293. digest256map_remove(hs_cache_v3_client, desc->key.pubkey);
  294. /* Update cache size with this entry for the OOM handler. */
  295. rend_cache_decrement_allocation(cache_get_client_entry_size(desc));
  296. }
  297. /* Store a given descriptor in our cache. */
  298. static void
  299. store_v3_desc_as_client(hs_cache_client_descriptor_t *desc)
  300. {
  301. tor_assert(desc);
  302. digest256map_set(hs_cache_v3_client, desc->key.pubkey, desc);
  303. /* Update cache size with this entry for the OOM handler. */
  304. rend_cache_increment_allocation(cache_get_client_entry_size(desc));
  305. }
  306. /* Query our cache and return the entry or NULL if not found or if expired. */
  307. STATIC hs_cache_client_descriptor_t *
  308. lookup_v3_desc_as_client(const uint8_t *key)
  309. {
  310. time_t now = approx_time();
  311. hs_cache_client_descriptor_t *cached_desc;
  312. tor_assert(key);
  313. /* Do the lookup */
  314. cached_desc = digest256map_get(hs_cache_v3_client, key);
  315. if (!cached_desc) {
  316. return NULL;
  317. }
  318. /* Don't return expired entries */
  319. if (cached_client_descriptor_has_expired(now, cached_desc)) {
  320. return NULL;
  321. }
  322. return cached_desc;
  323. }
  324. /* Parse the encoded descriptor in <b>desc_str</b> using
  325. * <b>service_identity_pk<b> to decrypt it first.
  326. *
  327. * If everything goes well, allocate and return a new
  328. * hs_cache_client_descriptor_t object. In case of error, return NULL. */
  329. static hs_cache_client_descriptor_t *
  330. cache_client_desc_new(const char *desc_str,
  331. const ed25519_public_key_t *service_identity_pk)
  332. {
  333. hs_descriptor_t *desc = NULL;
  334. hs_cache_client_descriptor_t *client_desc = NULL;
  335. tor_assert(desc_str);
  336. tor_assert(service_identity_pk);
  337. /* Decode the descriptor we just fetched. */
  338. if (hs_client_decode_descriptor(desc_str, service_identity_pk, &desc) < 0) {
  339. goto end;
  340. }
  341. tor_assert(desc);
  342. /* All is good: make a cache object for this descriptor */
  343. client_desc = tor_malloc_zero(sizeof(hs_cache_client_descriptor_t));
  344. ed25519_pubkey_copy(&client_desc->key, service_identity_pk);
  345. /* Set expiration time for this cached descriptor to be the start of the next
  346. * time period since that's when clients need to start using the next blinded
  347. * pk of the service (and hence will need its next descriptor). */
  348. client_desc->expiration_ts = hs_get_start_time_of_next_time_period(0);
  349. client_desc->desc = desc;
  350. client_desc->encoded_desc = tor_strdup(desc_str);
  351. end:
  352. return client_desc;
  353. }
  354. #define cache_client_desc_free(val) \
  355. FREE_AND_NULL(hs_cache_client_descriptor_t, cache_client_desc_free_, (val))
  356. /** Free memory allocated by <b>desc</b>. */
  357. static void
  358. cache_client_desc_free_(hs_cache_client_descriptor_t *desc)
  359. {
  360. if (desc == NULL) {
  361. return;
  362. }
  363. hs_descriptor_free(desc->desc);
  364. memwipe(&desc->key, 0, sizeof(desc->key));
  365. memwipe(desc->encoded_desc, 0, strlen(desc->encoded_desc));
  366. tor_free(desc->encoded_desc);
  367. tor_free(desc);
  368. }
  369. /** Helper function: Use by the free all function to clear the client cache */
  370. static void
  371. cache_client_desc_free_void(void *ptr)
  372. {
  373. hs_cache_client_descriptor_t *desc = ptr;
  374. cache_client_desc_free(desc);
  375. }
  376. /* Return a newly allocated and initialized hs_cache_intro_state_t object. */
  377. static hs_cache_intro_state_t *
  378. cache_intro_state_new(void)
  379. {
  380. hs_cache_intro_state_t *state = tor_malloc_zero(sizeof(*state));
  381. state->created_ts = approx_time();
  382. return state;
  383. }
  384. #define cache_intro_state_free(val) \
  385. FREE_AND_NULL(hs_cache_intro_state_t, cache_intro_state_free_, (val))
  386. /* Free an hs_cache_intro_state_t object. */
  387. static void
  388. cache_intro_state_free_(hs_cache_intro_state_t *state)
  389. {
  390. tor_free(state);
  391. }
  392. /* Helper function: use by the free all function. */
  393. static void
  394. cache_intro_state_free_void(void *state)
  395. {
  396. cache_intro_state_free_(state);
  397. }
  398. /* Return a newly allocated and initialized hs_cache_client_intro_state_t
  399. * object. */
  400. static hs_cache_client_intro_state_t *
  401. cache_client_intro_state_new(void)
  402. {
  403. hs_cache_client_intro_state_t *cache = tor_malloc_zero(sizeof(*cache));
  404. cache->intro_points = digest256map_new();
  405. return cache;
  406. }
  407. #define cache_client_intro_state_free(val) \
  408. FREE_AND_NULL(hs_cache_client_intro_state_t, \
  409. cache_client_intro_state_free_, (val))
  410. /* Free a cache client intro state object. */
  411. static void
  412. cache_client_intro_state_free_(hs_cache_client_intro_state_t *cache)
  413. {
  414. if (cache == NULL) {
  415. return;
  416. }
  417. digest256map_free(cache->intro_points, cache_intro_state_free_void);
  418. tor_free(cache);
  419. }
  420. /* Helper function: use by the free all function. */
  421. static void
  422. cache_client_intro_state_free_void(void *entry)
  423. {
  424. cache_client_intro_state_free_(entry);
  425. }
  426. /* For the given service identity key service_pk and an introduction
  427. * authentication key auth_key, lookup the intro state object. Return 1 if
  428. * found and put it in entry if not NULL. Return 0 if not found and entry is
  429. * untouched. */
  430. static int
  431. cache_client_intro_state_lookup(const ed25519_public_key_t *service_pk,
  432. const ed25519_public_key_t *auth_key,
  433. hs_cache_intro_state_t **entry)
  434. {
  435. hs_cache_intro_state_t *state;
  436. hs_cache_client_intro_state_t *cache;
  437. tor_assert(service_pk);
  438. tor_assert(auth_key);
  439. /* Lookup the intro state cache for this service key. */
  440. cache = digest256map_get(hs_cache_client_intro_state, service_pk->pubkey);
  441. if (cache == NULL) {
  442. goto not_found;
  443. }
  444. /* From the cache we just found for the service, lookup in the introduction
  445. * points map for the given authentication key. */
  446. state = digest256map_get(cache->intro_points, auth_key->pubkey);
  447. if (state == NULL) {
  448. goto not_found;
  449. }
  450. if (entry) {
  451. *entry = state;
  452. }
  453. return 1;
  454. not_found:
  455. return 0;
  456. }
  457. /* Note the given failure in state. */
  458. static void
  459. cache_client_intro_state_note(hs_cache_intro_state_t *state,
  460. rend_intro_point_failure_t failure)
  461. {
  462. tor_assert(state);
  463. switch (failure) {
  464. case INTRO_POINT_FAILURE_GENERIC:
  465. state->error = 1;
  466. break;
  467. case INTRO_POINT_FAILURE_TIMEOUT:
  468. state->timed_out = 1;
  469. break;
  470. case INTRO_POINT_FAILURE_UNREACHABLE:
  471. state->unreachable_count++;
  472. break;
  473. default:
  474. tor_assert_nonfatal_unreached();
  475. return;
  476. }
  477. }
  478. /* For the given service identity key service_pk and an introduction
  479. * authentication key auth_key, add an entry in the client intro state cache
  480. * If no entry exists for the service, it will create one. If state is non
  481. * NULL, it will point to the new intro state entry. */
  482. static void
  483. cache_client_intro_state_add(const ed25519_public_key_t *service_pk,
  484. const ed25519_public_key_t *auth_key,
  485. hs_cache_intro_state_t **state)
  486. {
  487. hs_cache_intro_state_t *entry, *old_entry;
  488. hs_cache_client_intro_state_t *cache;
  489. tor_assert(service_pk);
  490. tor_assert(auth_key);
  491. /* Lookup the state cache for this service key. */
  492. cache = digest256map_get(hs_cache_client_intro_state, service_pk->pubkey);
  493. if (cache == NULL) {
  494. cache = cache_client_intro_state_new();
  495. digest256map_set(hs_cache_client_intro_state, service_pk->pubkey, cache);
  496. }
  497. entry = cache_intro_state_new();
  498. old_entry = digest256map_set(cache->intro_points, auth_key->pubkey, entry);
  499. /* This should never happened because the code flow is to lookup the entry
  500. * before adding it. But, just in case, non fatal assert and free it. */
  501. tor_assert_nonfatal(old_entry == NULL);
  502. tor_free(old_entry);
  503. if (state) {
  504. *state = entry;
  505. }
  506. }
  507. /* Remove every intro point state entry from cache that has been created
  508. * before or at the cutoff. */
  509. static void
  510. cache_client_intro_state_clean(time_t cutoff,
  511. hs_cache_client_intro_state_t *cache)
  512. {
  513. tor_assert(cache);
  514. DIGEST256MAP_FOREACH_MODIFY(cache->intro_points, key,
  515. hs_cache_intro_state_t *, entry) {
  516. if (entry->created_ts <= cutoff) {
  517. cache_intro_state_free(entry);
  518. MAP_DEL_CURRENT(key);
  519. }
  520. } DIGEST256MAP_FOREACH_END;
  521. }
  522. /* Return true iff no intro points are in this cache. */
  523. static int
  524. cache_client_intro_state_is_empty(const hs_cache_client_intro_state_t *cache)
  525. {
  526. return digest256map_isempty(cache->intro_points);
  527. }
  528. /** Check whether <b>client_desc</b> is useful for us, and store it in the
  529. * client-side HS cache if so. The client_desc is freed if we already have a
  530. * fresher (higher revision counter count) in the cache. */
  531. static int
  532. cache_store_as_client(hs_cache_client_descriptor_t *client_desc)
  533. {
  534. hs_cache_client_descriptor_t *cache_entry;
  535. /* TODO: Heavy code duplication with cache_store_as_dir(). Consider
  536. * refactoring and uniting! */
  537. tor_assert(client_desc);
  538. /* Check if we already have a descriptor from this HS in cache. If we do,
  539. * check if this descriptor is newer than the cached one */
  540. cache_entry = lookup_v3_desc_as_client(client_desc->key.pubkey);
  541. if (cache_entry != NULL) {
  542. /* If we have an entry in our cache that has a revision counter greater
  543. * than the one we just fetched, discard the one we fetched. */
  544. if (cache_entry->desc->plaintext_data.revision_counter >
  545. client_desc->desc->plaintext_data.revision_counter) {
  546. cache_client_desc_free(client_desc);
  547. goto done;
  548. }
  549. /* Remove old entry. Make space for the new one! */
  550. remove_v3_desc_as_client(cache_entry);
  551. cache_client_desc_free(cache_entry);
  552. }
  553. /* Store descriptor in cache */
  554. store_v3_desc_as_client(client_desc);
  555. done:
  556. return 0;
  557. }
  558. /* Return true iff the cached client descriptor at <b>cached_desc</b has
  559. * expired. */
  560. static int
  561. cached_client_descriptor_has_expired(time_t now,
  562. const hs_cache_client_descriptor_t *cached_desc)
  563. {
  564. /* We use the current consensus time to see if we should expire this
  565. * descriptor since we use consensus time for all other parts of the protocol
  566. * as well (e.g. to build the blinded key and compute time periods). */
  567. const networkstatus_t *ns = networkstatus_get_live_consensus(now);
  568. /* If we don't have a recent consensus, consider this entry expired since we
  569. * will want to fetch a new HS desc when we get a live consensus. */
  570. if (!ns) {
  571. return 1;
  572. }
  573. if (cached_desc->expiration_ts <= ns->valid_after) {
  574. return 1;
  575. }
  576. return 0;
  577. }
  578. /* clean the client cache using now as the current time. Return the total size
  579. * of removed bytes from the cache. */
  580. static size_t
  581. cache_clean_v3_as_client(time_t now)
  582. {
  583. size_t bytes_removed = 0;
  584. if (!hs_cache_v3_client) { /* No cache to clean. Just return. */
  585. return 0;
  586. }
  587. DIGEST256MAP_FOREACH_MODIFY(hs_cache_v3_client, key,
  588. hs_cache_client_descriptor_t *, entry) {
  589. size_t entry_size;
  590. /* If the entry has not expired, continue to the next cached entry */
  591. if (!cached_client_descriptor_has_expired(now, entry)) {
  592. continue;
  593. }
  594. /* Here, our entry has expired, remove and free. */
  595. MAP_DEL_CURRENT(key);
  596. entry_size = cache_get_client_entry_size(entry);
  597. bytes_removed += entry_size;
  598. /* Entry is not in the cache anymore, destroy it. */
  599. cache_client_desc_free(entry);
  600. /* Update our OOM. We didn't use the remove() function because we are in
  601. * a loop so we have to explicitey decrement. */
  602. rend_cache_decrement_allocation(entry_size);
  603. /* Logging. */
  604. {
  605. char key_b64[BASE64_DIGEST256_LEN + 1];
  606. digest256_to_base64(key_b64, (const char *) key);
  607. log_info(LD_REND, "Removing hidden service v3 descriptor '%s' "
  608. "from client cache",
  609. safe_str_client(key_b64));
  610. }
  611. } DIGEST256MAP_FOREACH_END;
  612. return bytes_removed;
  613. }
  614. /** Public API: Given the HS ed25519 identity public key in <b>key</b>, return
  615. * its HS encoded descriptor if it's stored in our cache, or NULL if not. */
  616. const char *
  617. hs_cache_lookup_encoded_as_client(const ed25519_public_key_t *key)
  618. {
  619. hs_cache_client_descriptor_t *cached_desc = NULL;
  620. tor_assert(key);
  621. cached_desc = lookup_v3_desc_as_client(key->pubkey);
  622. if (cached_desc) {
  623. tor_assert(cached_desc->encoded_desc);
  624. return cached_desc->encoded_desc;
  625. }
  626. return NULL;
  627. }
  628. /** Public API: Given the HS ed25519 identity public key in <b>key</b>, return
  629. * its HS descriptor if it's stored in our cache, or NULL if not. */
  630. const hs_descriptor_t *
  631. hs_cache_lookup_as_client(const ed25519_public_key_t *key)
  632. {
  633. hs_cache_client_descriptor_t *cached_desc = NULL;
  634. tor_assert(key);
  635. cached_desc = lookup_v3_desc_as_client(key->pubkey);
  636. if (cached_desc) {
  637. tor_assert(cached_desc->desc);
  638. return cached_desc->desc;
  639. }
  640. return NULL;
  641. }
  642. /** Public API: Given an encoded descriptor, store it in the client HS
  643. * cache. Return -1 on error, 0 on success .*/
  644. int
  645. hs_cache_store_as_client(const char *desc_str,
  646. const ed25519_public_key_t *identity_pk)
  647. {
  648. hs_cache_client_descriptor_t *client_desc = NULL;
  649. tor_assert(desc_str);
  650. tor_assert(identity_pk);
  651. /* Create client cache descriptor object */
  652. client_desc = cache_client_desc_new(desc_str, identity_pk);
  653. if (!client_desc) {
  654. log_warn(LD_GENERAL, "Failed to parse received descriptor %s.",
  655. escaped(desc_str));
  656. goto err;
  657. }
  658. /* Push it to the cache */
  659. if (cache_store_as_client(client_desc) < 0) {
  660. goto err;
  661. }
  662. return 0;
  663. err:
  664. cache_client_desc_free(client_desc);
  665. return -1;
  666. }
  667. /* Clean all client caches using the current time now. */
  668. void
  669. hs_cache_clean_as_client(time_t now)
  670. {
  671. /* Start with v2 cache cleaning. */
  672. rend_cache_clean(now, REND_CACHE_TYPE_CLIENT);
  673. /* Now, clean the v3 cache. Set the cutoff to 0 telling the cleanup function
  674. * to compute the cutoff by itself using the lifetime value. */
  675. cache_clean_v3_as_client(now);
  676. }
  677. /* Purge the client descriptor cache. */
  678. void
  679. hs_cache_purge_as_client(void)
  680. {
  681. DIGEST256MAP_FOREACH_MODIFY(hs_cache_v3_client, key,
  682. hs_cache_client_descriptor_t *, entry) {
  683. size_t entry_size = cache_get_client_entry_size(entry);
  684. MAP_DEL_CURRENT(key);
  685. cache_client_desc_free(entry);
  686. /* Update our OOM. We didn't use the remove() function because we are in
  687. * a loop so we have to explicitly decrement. */
  688. rend_cache_decrement_allocation(entry_size);
  689. } DIGEST256MAP_FOREACH_END;
  690. log_info(LD_REND, "Hidden service client descriptor cache purged.");
  691. }
  692. /* For a given service identity public key and an introduction authentication
  693. * key, note the given failure in the client intro state cache. */
  694. void
  695. hs_cache_client_intro_state_note(const ed25519_public_key_t *service_pk,
  696. const ed25519_public_key_t *auth_key,
  697. rend_intro_point_failure_t failure)
  698. {
  699. int found;
  700. hs_cache_intro_state_t *entry;
  701. tor_assert(service_pk);
  702. tor_assert(auth_key);
  703. found = cache_client_intro_state_lookup(service_pk, auth_key, &entry);
  704. if (!found) {
  705. /* Create a new entry and add it to the cache. */
  706. cache_client_intro_state_add(service_pk, auth_key, &entry);
  707. }
  708. /* Note down the entry. */
  709. cache_client_intro_state_note(entry, failure);
  710. }
  711. /* For a given service identity public key and an introduction authentication
  712. * key, return true iff it is present in the failure cache. */
  713. const hs_cache_intro_state_t *
  714. hs_cache_client_intro_state_find(const ed25519_public_key_t *service_pk,
  715. const ed25519_public_key_t *auth_key)
  716. {
  717. hs_cache_intro_state_t *state = NULL;
  718. cache_client_intro_state_lookup(service_pk, auth_key, &state);
  719. return state;
  720. }
  721. /* Cleanup the client introduction state cache. */
  722. void
  723. hs_cache_client_intro_state_clean(time_t now)
  724. {
  725. time_t cutoff = now - HS_CACHE_CLIENT_INTRO_STATE_MAX_AGE;
  726. DIGEST256MAP_FOREACH_MODIFY(hs_cache_client_intro_state, key,
  727. hs_cache_client_intro_state_t *, cache) {
  728. /* Cleanup intro points failure. */
  729. cache_client_intro_state_clean(cutoff, cache);
  730. /* Is this cache empty for this service key? If yes, remove it from the
  731. * cache. Else keep it. */
  732. if (cache_client_intro_state_is_empty(cache)) {
  733. cache_client_intro_state_free(cache);
  734. MAP_DEL_CURRENT(key);
  735. }
  736. } DIGEST256MAP_FOREACH_END;
  737. }
  738. /* Purge the client introduction state cache. */
  739. void
  740. hs_cache_client_intro_state_purge(void)
  741. {
  742. DIGEST256MAP_FOREACH_MODIFY(hs_cache_client_intro_state, key,
  743. hs_cache_client_intro_state_t *, cache) {
  744. MAP_DEL_CURRENT(key);
  745. cache_client_intro_state_free(cache);
  746. } DIGEST256MAP_FOREACH_END;
  747. log_info(LD_REND, "Hidden service client introduction point state "
  748. "cache purged.");
  749. }
  750. /**************** Generics *********************************/
  751. /* Do a round of OOM cleanup on all directory caches. Return the amount of
  752. * removed bytes. It is possible that the returned value is lower than
  753. * min_remove_bytes if the caches get emptied out so the caller should be
  754. * aware of this. */
  755. size_t
  756. hs_cache_handle_oom(time_t now, size_t min_remove_bytes)
  757. {
  758. time_t k;
  759. size_t bytes_removed = 0;
  760. /* Our OOM handler called with 0 bytes to remove is a code flow error. */
  761. tor_assert(min_remove_bytes != 0);
  762. /* The algorithm is as follow. K is the oldest expected descriptor age.
  763. *
  764. * 1) Deallocate all entries from v2 cache that are older than K hours.
  765. * 1.1) If the amount of remove bytes has been reached, stop.
  766. * 2) Deallocate all entries from v3 cache that are older than K hours
  767. * 2.1) If the amount of remove bytes has been reached, stop.
  768. * 3) Set K = K - RendPostPeriod and repeat process until K is < 0.
  769. *
  770. * This ends up being O(Kn).
  771. */
  772. /* Set K to the oldest expected age in seconds which is the maximum
  773. * lifetime of a cache entry. We'll use the v2 lifetime because it's much
  774. * bigger than the v3 thus leading to cleaning older descriptors. */
  775. k = rend_cache_max_entry_lifetime();
  776. do {
  777. time_t cutoff;
  778. /* If K becomes negative, it means we've empty the caches so stop and
  779. * return what we were able to cleanup. */
  780. if (k < 0) {
  781. break;
  782. }
  783. /* Compute a cutoff value with K and the current time. */
  784. cutoff = now - k;
  785. /* Start by cleaning the v2 cache with that cutoff. */
  786. bytes_removed += rend_cache_clean_v2_descs_as_dir(cutoff);
  787. if (bytes_removed < min_remove_bytes) {
  788. /* We haven't remove enough bytes so clean v3 cache. */
  789. bytes_removed += cache_clean_v3_as_dir(now, cutoff);
  790. /* Decrement K by a post period to shorten the cutoff. */
  791. k -= get_options()->RendPostPeriod;
  792. }
  793. } while (bytes_removed < min_remove_bytes);
  794. return bytes_removed;
  795. }
  796. /* Return the maximum size of a v3 HS descriptor. */
  797. unsigned int
  798. hs_cache_get_max_descriptor_size(void)
  799. {
  800. return (unsigned) networkstatus_get_param(NULL,
  801. "HSV3MaxDescriptorSize",
  802. HS_DESC_MAX_LEN, 1, INT32_MAX);
  803. }
  804. /* Initialize the hidden service cache subsystem. */
  805. void
  806. hs_cache_init(void)
  807. {
  808. /* Calling this twice is very wrong code flow. */
  809. tor_assert(!hs_cache_v3_dir);
  810. hs_cache_v3_dir = digest256map_new();
  811. tor_assert(!hs_cache_v3_client);
  812. hs_cache_v3_client = digest256map_new();
  813. tor_assert(!hs_cache_client_intro_state);
  814. hs_cache_client_intro_state = digest256map_new();
  815. }
  816. /* Cleanup the hidden service cache subsystem. */
  817. void
  818. hs_cache_free_all(void)
  819. {
  820. digest256map_free(hs_cache_v3_dir, cache_dir_desc_free_void);
  821. hs_cache_v3_dir = NULL;
  822. digest256map_free(hs_cache_v3_client, cache_client_desc_free_void);
  823. hs_cache_v3_client = NULL;
  824. digest256map_free(hs_cache_client_intro_state,
  825. cache_client_intro_state_free_void);
  826. hs_cache_client_intro_state = NULL;
  827. }