hs_cache.c 30 KB

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