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