hs_cache.c 27 KB

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