hs_cache.c 30 KB

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