1 /* Copyright (c) 2015-2016, The Tor Project, Inc. */
2 /* See LICENSE for licensing information */
6 * \brief Hidden service descriptor cache.
9 #define RENDCACHE_PRIVATE
10 #include "rendcache.h"
14 #include "routerlist.h"
15 #include "routerparse.h"
16 #include "rendcommon.h"
18 /** Map from service id (as generated by rend_get_service_id) to
19 * rend_cache_entry_t. */
20 STATIC strmap_t
*rend_cache
= NULL
;
22 /** Map from service id to rend_cache_entry_t; only for hidden services. */
23 static strmap_t
*rend_cache_local_service
= NULL
;
25 /** Map from descriptor id to rend_cache_entry_t; only for hidden service
27 STATIC digestmap_t
*rend_cache_v2_dir
= NULL
;
29 /** (Client side only) Map from service id to rend_cache_failure_t. This
30 * cache is used to track intro point(IP) failures so we know when to keep
31 * or discard a new descriptor we just fetched. Here is a description of the
34 * Everytime tor discards an IP (ex: receives a NACK), we add an entry to
35 * this cache noting the identity digest of the IP and it's failure type for
36 * the service ID. The reason we indexed this cache by service ID is to
37 * differentiate errors that can occur only for a specific service like a
38 * NACK for instance. It applies for one but maybe not for the others.
40 * Once a service descriptor is fetched and considered valid, each IP is
41 * looked up in this cache and if present, it is discarded from the fetched
42 * descriptor. At the end, all IP(s) in the cache, for a specific service
43 * ID, that were NOT present in the descriptor are removed from this cache.
44 * Which means that if at least one IP was not in this cache, thus usuable,
45 * it's considered a new descriptor so we keep it. Else, if all IPs were in
46 * this cache, we discard the descriptor as it's considered unsuable.
48 * Once a descriptor is removed from the rend cache or expires, the entry
49 * in this cache is also removed for the service ID.
51 * This scheme allows us to not realy on the descriptor's timestamp (which
52 * is rounded down to the hour) to know if we have a newer descriptor. We
53 * only rely on the usability of intro points from an internal state. */
54 STATIC strmap_t
*rend_cache_failure
= NULL
;
57 STATIC
size_t rend_cache_total_allocation
= 0;
59 /** Initializes the service descriptor cache.
64 rend_cache
= strmap_new();
65 rend_cache_v2_dir
= digestmap_new();
66 rend_cache_local_service
= strmap_new();
67 rend_cache_failure
= strmap_new();
70 /** Return the approximate number of bytes needed to hold <b>e</b>. */
72 rend_cache_entry_allocation(const rend_cache_entry_t
*e
)
77 /* This doesn't count intro_nodes or key size */
78 return sizeof(*e
) + e
->len
+ sizeof(*e
->parsed
);
83 rend_cache_get_total_allocation(void)
85 return rend_cache_total_allocation
;
88 /** Decrement the total bytes attributed to the rendezvous cache by n. */
90 rend_cache_decrement_allocation(size_t n
)
92 static int have_underflowed
= 0;
94 if (rend_cache_total_allocation
>= n
) {
95 rend_cache_total_allocation
-= n
;
97 rend_cache_total_allocation
= 0;
98 if (! have_underflowed
) {
100 log_warn(LD_BUG
, "Underflow in rend_cache_decrement_allocation");
105 /** Increase the total bytes attributed to the rendezvous cache by n. */
107 rend_cache_increment_allocation(size_t n
)
109 static int have_overflowed
= 0;
110 if (rend_cache_total_allocation
<= SIZE_MAX
- n
) {
111 rend_cache_total_allocation
+= n
;
113 rend_cache_total_allocation
= SIZE_MAX
;
114 if (! have_overflowed
) {
116 log_warn(LD_BUG
, "Overflow in rend_cache_increment_allocation");
121 /** Helper: free a rend cache failure intro object. */
123 rend_cache_failure_intro_entry_free(rend_cache_failure_intro_t
*entry
)
132 rend_cache_failure_intro_entry_free_(void *entry
)
134 rend_cache_failure_intro_entry_free(entry
);
137 /** Allocate a rend cache failure intro object and return it. <b>failure</b>
138 * is set into the object. This function can not fail. */
139 STATIC rend_cache_failure_intro_t
*
140 rend_cache_failure_intro_entry_new(rend_intro_point_failure_t failure
)
142 rend_cache_failure_intro_t
*entry
= tor_malloc(sizeof(*entry
));
143 entry
->failure_type
= failure
;
144 entry
->created_ts
= time(NULL
);
148 /** Helper: free a rend cache failure object. */
150 rend_cache_failure_entry_free(rend_cache_failure_t
*entry
)
156 /* Free and remove every intro failure object. */
157 digestmap_free(entry
->intro_failures
,
158 rend_cache_failure_intro_entry_free_
);
163 /** Helper: deallocate a rend_cache_failure_t. (Used with strmap_free(),
164 * which requires a function pointer whose argument is void*). */
166 rend_cache_failure_entry_free_(void *entry
)
168 rend_cache_failure_entry_free(entry
);
171 /** Allocate a rend cache failure object and return it. This function can
173 STATIC rend_cache_failure_t
*
174 rend_cache_failure_entry_new(void)
176 rend_cache_failure_t
*entry
= tor_malloc(sizeof(*entry
));
177 entry
->intro_failures
= digestmap_new();
181 /** Remove failure cache entry for the service ID in the given descriptor
184 rend_cache_failure_remove(rend_service_descriptor_t
*desc
)
186 char service_id
[REND_SERVICE_ID_LEN_BASE32
+ 1];
187 rend_cache_failure_t
*entry
;
192 if (rend_get_service_id(desc
->pk
, service_id
) < 0) {
195 entry
= strmap_get_lc(rend_cache_failure
, service_id
);
197 strmap_remove_lc(rend_cache_failure
, service_id
);
198 rend_cache_failure_entry_free(entry
);
202 /** Helper: free storage held by a single service descriptor cache entry. */
204 rend_cache_entry_free(rend_cache_entry_t
*e
)
208 rend_cache_decrement_allocation(rend_cache_entry_allocation(e
));
209 /* We are about to remove a descriptor from the cache so remove the entry
210 * in the failure cache. */
211 rend_cache_failure_remove(e
->parsed
);
212 rend_service_descriptor_free(e
->parsed
);
217 /** Helper: deallocate a rend_cache_entry_t. (Used with strmap_free(), which
218 * requires a function pointer whose argument is void*). */
220 rend_cache_entry_free_(void *p
)
222 rend_cache_entry_free(p
);
225 /** Free all storage held by the service descriptor cache. */
227 rend_cache_free_all(void)
229 strmap_free(rend_cache
, rend_cache_entry_free_
);
230 digestmap_free(rend_cache_v2_dir
, rend_cache_entry_free_
);
231 strmap_free(rend_cache_local_service
, rend_cache_entry_free_
);
232 strmap_free(rend_cache_failure
, rend_cache_failure_entry_free_
);
234 rend_cache_v2_dir
= NULL
;
235 rend_cache_local_service
= NULL
;
236 rend_cache_failure
= NULL
;
237 rend_cache_total_allocation
= 0;
240 /** Remove all entries that re REND_CACHE_FAILURE_MAX_AGE old. This is
241 * called every second.
243 * We have to clean these regurlarly else if for whatever reasons an hidden
244 * service goes offline and a client tries to connect to it during that
245 * time, a failure entry is created and the client will be unable to connect
246 * for a while even though the service has return online. */
248 rend_cache_failure_clean(time_t now
)
250 time_t cutoff
= now
- REND_CACHE_FAILURE_MAX_AGE
;
251 STRMAP_FOREACH_MODIFY(rend_cache_failure
, key
,
252 rend_cache_failure_t
*, ent
) {
253 /* Free and remove every intro failure object that match the cutoff. */
254 DIGESTMAP_FOREACH_MODIFY(ent
->intro_failures
, ip_key
,
255 rend_cache_failure_intro_t
*, ip_ent
) {
256 if (ip_ent
->created_ts
< cutoff
) {
257 rend_cache_failure_intro_entry_free(ip_ent
);
258 MAP_DEL_CURRENT(ip_key
);
260 } DIGESTMAP_FOREACH_END
;
261 /* If the entry is now empty of intro point failures, remove it. */
262 if (digestmap_isempty(ent
->intro_failures
)) {
263 rend_cache_failure_entry_free(ent
);
264 MAP_DEL_CURRENT(key
);
266 } STRMAP_FOREACH_END
;
269 /** Removes all old entries from the client or service descriptor cache.
272 rend_cache_clean(time_t now
, rend_cache_type_t cache_type
)
277 rend_cache_entry_t
*ent
;
278 time_t cutoff
= now
- REND_CACHE_MAX_AGE
- REND_CACHE_MAX_SKEW
;
279 strmap_t
*cache
= NULL
;
281 if (cache_type
== REND_CACHE_TYPE_CLIENT
) {
283 } else if (cache_type
== REND_CACHE_TYPE_SERVICE
) {
284 cache
= rend_cache_local_service
;
288 for (iter
= strmap_iter_init(cache
); !strmap_iter_done(iter
); ) {
289 strmap_iter_get(iter
, &key
, &val
);
290 ent
= (rend_cache_entry_t
*)val
;
291 if (ent
->parsed
->timestamp
< cutoff
) {
292 iter
= strmap_iter_next_rmv(cache
, iter
);
293 rend_cache_entry_free(ent
);
295 iter
= strmap_iter_next(cache
, iter
);
300 /** Remove ALL entries from the rendezvous service descriptor cache.
303 rend_cache_purge(void)
306 log_info(LD_REND
, "Purging HS descriptor cache");
307 strmap_free(rend_cache
, rend_cache_entry_free_
);
309 rend_cache
= strmap_new();
312 /** Remove ALL entries from the failure cache. This is also called when a
313 * NEWNYM signal is received. */
315 rend_cache_failure_purge(void)
317 if (rend_cache_failure
) {
318 log_info(LD_REND
, "Purging HS failure cache");
319 strmap_free(rend_cache_failure
, rend_cache_failure_entry_free_
);
321 rend_cache_failure
= strmap_new();
324 /** Lookup the rend failure cache using a relay identity digest in
325 * <b>identity</b> which has DIGEST_LEN bytes and service ID <b>service_id</b>
326 * which is a null-terminated string. If found, the intro failure is set in
327 * <b>intro_entry</b> else it stays untouched. Return 1 iff found else 0. */
329 cache_failure_intro_lookup(const uint8_t *identity
, const char *service_id
,
330 rend_cache_failure_intro_t
**intro_entry
)
332 rend_cache_failure_t
*elem
;
333 rend_cache_failure_intro_t
*intro_elem
;
335 tor_assert(rend_cache_failure
);
341 /* Lookup descriptor and return it. */
342 elem
= strmap_get_lc(rend_cache_failure
, service_id
);
346 intro_elem
= digestmap_get(elem
->intro_failures
, (char *) identity
);
347 if (intro_elem
== NULL
) {
351 *intro_entry
= intro_elem
;
358 /** Allocate a new cache failure intro object and copy the content from
359 * <b>entry</b> to this newly allocated object. Return it. */
360 static rend_cache_failure_intro_t
*
361 cache_failure_intro_dup(const rend_cache_failure_intro_t
*entry
)
363 rend_cache_failure_intro_t
*ent_dup
=
364 rend_cache_failure_intro_entry_new(entry
->failure_type
);
365 ent_dup
->created_ts
= entry
->created_ts
;
369 /** Add an intro point failure to the failure cache using the relay
370 * <b>identity</b> and service ID <b>service_id</b>. Record the
371 * <b>failure</b> in that object. */
373 cache_failure_intro_add(const uint8_t *identity
, const char *service_id
,
374 rend_intro_point_failure_t failure
)
376 rend_cache_failure_t
*fail_entry
;
377 rend_cache_failure_intro_t
*entry
, *old_entry
;
379 /* Make sure we have a failure object for this service ID and if not,
380 * create it with this new intro failure entry. */
381 fail_entry
= strmap_get_lc(rend_cache_failure
, service_id
);
382 if (fail_entry
== NULL
) {
383 fail_entry
= rend_cache_failure_entry_new();
384 /* Add failure entry to global rend failure cache. */
385 strmap_set_lc(rend_cache_failure
, service_id
, fail_entry
);
387 entry
= rend_cache_failure_intro_entry_new(failure
);
388 old_entry
= digestmap_set(fail_entry
->intro_failures
,
389 (char *) identity
, entry
);
390 /* This _should_ be NULL, but in case it isn't, free it. */
391 rend_cache_failure_intro_entry_free(old_entry
);
394 /** Using a parsed descriptor <b>desc</b>, check if the introduction points
395 * are present in the failure cache and if so they are removed from the
396 * descriptor and kept into the failure cache. Then, each intro points that
397 * are NOT in the descriptor but in the failure cache for the given
398 * <b>service_id</b> are removed from the failure cache. */
400 validate_intro_point_failure(const rend_service_descriptor_t
*desc
,
401 const char *service_id
)
403 rend_cache_failure_t
*new_entry
, *cur_entry
;
404 /* New entry for the service ID that will be replacing the one in the
405 * failure cache since we have a new descriptor. In the case where all
406 * intro points are removed, we are assured that the new entry is the same
407 * as the current one. */
408 new_entry
= tor_malloc(sizeof(*new_entry
));
409 new_entry
->intro_failures
= digestmap_new();
413 SMARTLIST_FOREACH_BEGIN(desc
->intro_nodes
, rend_intro_point_t
*, intro
) {
415 rend_cache_failure_intro_t
*entry
;
416 const uint8_t *identity
=
417 (uint8_t *) intro
->extend_info
->identity_digest
;
419 found
= cache_failure_intro_lookup(identity
, service_id
, &entry
);
421 /* Dup here since it will be freed at the end when removing the
422 * original entry in the cache. */
423 rend_cache_failure_intro_t
*ent_dup
= cache_failure_intro_dup(entry
);
424 /* This intro point is in our cache, discard it from the descriptor
425 * because chances are that it's unusable. */
426 SMARTLIST_DEL_CURRENT(desc
->intro_nodes
, intro
);
427 /* Keep it for our new entry. */
428 digestmap_set(new_entry
->intro_failures
, (char *) identity
, ent_dup
);
429 /* Only free it when we're done looking at it. */
430 rend_intro_point_free(intro
);
433 } SMARTLIST_FOREACH_END(intro
);
435 /* Swap the failure entry in the cache and free the current one. */
436 cur_entry
= strmap_get_lc(rend_cache_failure
, service_id
);
437 if (cur_entry
!= NULL
) {
438 rend_cache_failure_entry_free(cur_entry
);
440 strmap_set_lc(rend_cache_failure
, service_id
, new_entry
);
443 /** Note down an intro failure in the rend failure cache using the type of
444 * failure in <b>failure</b> for the relay identity digest in
445 * <b>identity</b> and service ID <b>service_id</b>. If an entry already
446 * exists in the cache, the failure type is changed with <b>failure</b>. */
448 rend_cache_intro_failure_note(rend_intro_point_failure_t failure
,
449 const uint8_t *identity
,
450 const char *service_id
)
453 rend_cache_failure_intro_t
*entry
;
455 found
= cache_failure_intro_lookup(identity
, service_id
, &entry
);
457 cache_failure_intro_add(identity
, service_id
, failure
);
459 /* Replace introduction point failure with this one. */
460 entry
->failure_type
= failure
;
464 /** Remove all old v2 descriptors and those for which this hidden service
465 * directory is not responsible for any more.
467 * If at all possible, remove at least <b>force_remove</b> bytes of data.
470 rend_cache_clean_v2_descs_as_dir(time_t now
, size_t force_remove
)
472 digestmap_iter_t
*iter
;
473 time_t cutoff
= now
- REND_CACHE_MAX_AGE
- REND_CACHE_MAX_SKEW
;
474 const int LAST_SERVED_CUTOFF_STEP
= 1800;
475 time_t last_served_cutoff
= cutoff
;
476 size_t bytes_removed
= 0;
478 for (iter
= digestmap_iter_init(rend_cache_v2_dir
);
479 !digestmap_iter_done(iter
); ) {
482 rend_cache_entry_t
*ent
;
483 digestmap_iter_get(iter
, &key
, &val
);
485 if (ent
->parsed
->timestamp
< cutoff
||
486 ent
->last_served
< last_served_cutoff
) {
487 char key_base32
[REND_DESC_ID_V2_LEN_BASE32
+ 1];
488 base32_encode(key_base32
, sizeof(key_base32
), key
, DIGEST_LEN
);
489 log_info(LD_REND
, "Removing descriptor with ID '%s' from cache",
490 safe_str_client(key_base32
));
491 bytes_removed
+= rend_cache_entry_allocation(ent
);
492 iter
= digestmap_iter_next_rmv(rend_cache_v2_dir
, iter
);
493 rend_cache_entry_free(ent
);
495 iter
= digestmap_iter_next(rend_cache_v2_dir
, iter
);
499 /* In case we didn't remove enough bytes, advance the cutoff a little. */
500 last_served_cutoff
+= LAST_SERVED_CUTOFF_STEP
;
501 if (last_served_cutoff
> now
)
503 } while (bytes_removed
< force_remove
);
506 /** Lookup in the client cache the given service ID <b>query</b> for
509 * Return 0 if found and if <b>e</b> is non NULL, set it with the entry
510 * found. Else, a negative value is returned and <b>e</b> is untouched.
511 * -EINVAL means that <b>query</b> is not a valid service id.
512 * -ENOENT means that no entry in the cache was found. */
514 rend_cache_lookup_entry(const char *query
, int version
, rend_cache_entry_t
**e
)
517 char key
[REND_SERVICE_ID_LEN_BASE32
+ 2]; /* <version><query>\0 */
518 rend_cache_entry_t
*entry
= NULL
;
519 static const int default_version
= 2;
521 tor_assert(rend_cache
);
524 if (!rend_valid_service_id(query
)) {
531 log_warn(LD_REND
, "Cache lookup of a v0 renddesc is deprecated.");
534 /* Default is version 2. */
536 tor_snprintf(key
, sizeof(key
), "%d%s", default_version
, query
);
537 entry
= strmap_get_lc(rend_cache
, key
);
544 tor_assert(entry
->parsed
&& entry
->parsed
->intro_nodes
);
555 * Lookup the v2 service descriptor with the service ID <b>query</b> in the
556 * local service descriptor cache. Return 0 if found and if <b>e</b> is
557 * non NULL, set it with the entry found. Else, a negative value is returned
558 * and <b>e</b> is untouched.
559 * -EINVAL means that <b>query</b> is not a valid service id.
560 * -ENOENT means that no entry in the cache was found. */
562 rend_cache_lookup_v2_desc_as_service(const char *query
, rend_cache_entry_t
**e
)
565 rend_cache_entry_t
*entry
= NULL
;
567 tor_assert(rend_cache_local_service
);
570 if (!rend_valid_service_id(query
)) {
575 /* Lookup descriptor and return. */
576 entry
= strmap_get_lc(rend_cache_local_service
, query
);
590 /** Lookup the v2 service descriptor with base32-encoded <b>desc_id</b> and
591 * copy the pointer to it to *<b>desc</b>. Return 1 on success, 0 on
592 * well-formed-but-not-found, and -1 on failure.
595 rend_cache_lookup_v2_desc_as_dir(const char *desc_id
, const char **desc
)
597 rend_cache_entry_t
*e
;
598 char desc_id_digest
[DIGEST_LEN
];
599 tor_assert(rend_cache_v2_dir
);
600 if (base32_decode(desc_id_digest
, DIGEST_LEN
,
601 desc_id
, REND_DESC_ID_V2_LEN_BASE32
) < 0) {
602 log_fn(LOG_PROTOCOL_WARN
, LD_REND
,
603 "Rejecting v2 rendezvous descriptor request -- descriptor ID "
604 "contains illegal characters: %s",
608 /* Lookup descriptor and return. */
609 e
= digestmap_get(rend_cache_v2_dir
, desc_id_digest
);
612 e
->last_served
= approx_time();
618 /** Parse the v2 service descriptor(s) in <b>desc</b> and store it/them to the
619 * local rend cache. Don't attempt to decrypt the included list of introduction
620 * points (as we don't have a descriptor cookie for it).
622 * If we have a newer descriptor with the same ID, ignore this one.
623 * If we have an older descriptor with the same ID, replace it.
625 * Return an appropriate rend_cache_store_status_t.
627 rend_cache_store_status_t
628 rend_cache_store_v2_desc_as_dir(const char *desc
)
630 const or_options_t
*options
= get_options();
631 rend_service_descriptor_t
*parsed
;
632 char desc_id
[DIGEST_LEN
];
636 char desc_id_base32
[REND_DESC_ID_V2_LEN_BASE32
+ 1];
637 int number_parsed
= 0, number_stored
= 0;
638 const char *current_desc
= desc
;
639 const char *next_desc
;
640 rend_cache_entry_t
*e
;
641 time_t now
= time(NULL
);
642 tor_assert(rend_cache_v2_dir
);
644 while (rend_parse_v2_service_descriptor(&parsed
, desc_id
, &intro_content
,
645 &intro_size
, &encoded_size
,
646 &next_desc
, current_desc
, 1) >= 0) {
648 /* We don't care about the introduction points. */
649 tor_free(intro_content
);
650 /* For pretty log statements. */
651 base32_encode(desc_id_base32
, sizeof(desc_id_base32
),
652 desc_id
, DIGEST_LEN
);
653 /* Is descriptor too old? */
654 if (parsed
->timestamp
< now
- REND_CACHE_MAX_AGE
-REND_CACHE_MAX_SKEW
) {
655 log_info(LD_REND
, "Service descriptor with desc ID %s is too old.",
656 safe_str(desc_id_base32
));
659 /* Is descriptor too far in the future? */
660 if (parsed
->timestamp
> now
+ REND_CACHE_MAX_SKEW
) {
661 log_info(LD_REND
, "Service descriptor with desc ID %s is too far in the "
663 safe_str(desc_id_base32
));
666 /* Do we already have a newer descriptor? */
667 e
= digestmap_get(rend_cache_v2_dir
, desc_id
);
668 if (e
&& e
->parsed
->timestamp
> parsed
->timestamp
) {
669 log_info(LD_REND
, "We already have a newer service descriptor with the "
670 "same desc ID %s and version.",
671 safe_str(desc_id_base32
));
674 /* Do we already have this descriptor? */
675 if (e
&& !strcmp(desc
, e
->desc
)) {
676 log_info(LD_REND
, "We already have this service descriptor with desc "
677 "ID %s.", safe_str(desc_id_base32
));
680 /* Store received descriptor. */
682 e
= tor_malloc_zero(sizeof(rend_cache_entry_t
));
683 digestmap_set(rend_cache_v2_dir
, desc_id
, e
);
684 /* Treat something just uploaded as having been served a little
685 * while ago, so that flooding with new descriptors doesn't help
688 e
->last_served
= approx_time() - 3600;
690 rend_cache_decrement_allocation(rend_cache_entry_allocation(e
));
691 rend_service_descriptor_free(e
->parsed
);
695 e
->desc
= tor_strndup(current_desc
, encoded_size
);
696 e
->len
= encoded_size
;
697 rend_cache_increment_allocation(rend_cache_entry_allocation(e
));
698 log_info(LD_REND
, "Successfully stored service descriptor with desc ID "
700 safe_str(desc_id_base32
), (int)encoded_size
);
701 /* Statistics: Note down this potentially new HS. */
702 if (options
->HiddenServiceStatistics
) {
703 rep_hist_stored_maybe_new_hs(e
->parsed
->pk
);
709 rend_service_descriptor_free(parsed
);
711 /* advance to next descriptor, if available. */
712 current_desc
= next_desc
;
713 /* check if there is a next descriptor. */
715 strcmpstart(current_desc
, "rendezvous-service-descriptor "))
718 if (!number_parsed
) {
719 log_info(LD_REND
, "Could not parse any descriptor.");
722 log_info(LD_REND
, "Parsed %d and added %d descriptor%s.",
723 number_parsed
, number_stored
, number_stored
!= 1 ? "s" : "");
727 /** Parse the v2 service descriptor in <b>desc</b> and store it to the
728 * local service rend cache. Don't attempt to decrypt the included list of
729 * introduction points.
731 * If we have a newer descriptor with the same ID, ignore this one.
732 * If we have an older descriptor with the same ID, replace it.
734 * Return an appropriate rend_cache_store_status_t.
736 rend_cache_store_status_t
737 rend_cache_store_v2_desc_as_service(const char *desc
)
739 rend_service_descriptor_t
*parsed
= NULL
;
740 char desc_id
[DIGEST_LEN
];
741 char *intro_content
= NULL
;
744 const char *next_desc
;
745 char service_id
[REND_SERVICE_ID_LEN_BASE32
+1];
746 rend_cache_entry_t
*e
;
747 rend_cache_store_status_t retval
= RCS_BADDESC
;
748 tor_assert(rend_cache_local_service
);
751 /* Parse the descriptor. */
752 if (rend_parse_v2_service_descriptor(&parsed
, desc_id
, &intro_content
,
753 &intro_size
, &encoded_size
,
754 &next_desc
, desc
, 0) < 0) {
755 log_warn(LD_REND
, "Could not parse descriptor.");
758 /* Compute service ID from public key. */
759 if (rend_get_service_id(parsed
->pk
, service_id
)<0) {
760 log_warn(LD_REND
, "Couldn't compute service ID.");
764 /* Do we already have a newer descriptor? Allow new descriptors with a
765 rounded timestamp equal to or newer than the current descriptor */
766 e
= (rend_cache_entry_t
*) strmap_get_lc(rend_cache_local_service
,
768 if (e
&& e
->parsed
->timestamp
> parsed
->timestamp
) {
769 log_info(LD_REND
, "We already have a newer service descriptor for "
770 "service ID %s.", safe_str_client(service_id
));
773 /* We don't care about the introduction points. */
774 tor_free(intro_content
);
776 e
= tor_malloc_zero(sizeof(rend_cache_entry_t
));
777 strmap_set_lc(rend_cache_local_service
, service_id
, e
);
779 rend_cache_decrement_allocation(rend_cache_entry_allocation(e
));
780 rend_service_descriptor_free(e
->parsed
);
784 e
->desc
= tor_malloc_zero(encoded_size
+ 1);
785 strlcpy(e
->desc
, desc
, encoded_size
+ 1);
786 e
->len
= encoded_size
;
787 rend_cache_increment_allocation(rend_cache_entry_allocation(e
));
788 log_debug(LD_REND
,"Successfully stored rend desc '%s', len %d.",
789 safe_str_client(service_id
), (int)encoded_size
);
796 rend_service_descriptor_free(parsed
);
797 tor_free(intro_content
);
801 /** Parse the v2 service descriptor in <b>desc</b>, decrypt the included list
802 * of introduction points with <b>descriptor_cookie</b> (which may also be
803 * <b>NULL</b> if decryption is not necessary), and store the descriptor to
804 * the local cache under its version and service id.
806 * If we have a newer v2 descriptor with the same ID, ignore this one.
807 * If we have an older descriptor with the same ID, replace it.
808 * If the descriptor's service ID does not match
809 * <b>rend_query</b>-\>onion_address, reject it.
811 * If the descriptor's descriptor ID doesn't match <b>desc_id_base32</b>,
814 * Return an appropriate rend_cache_store_status_t. If entry is not NULL,
815 * set it with the cache entry pointer of the descriptor.
817 rend_cache_store_status_t
818 rend_cache_store_v2_desc_as_client(const char *desc
,
819 const char *desc_id_base32
,
820 const rend_data_t
*rend_query
,
821 rend_cache_entry_t
**entry
)
823 /*XXXX this seems to have a bit of duplicate code with
824 * rend_cache_store_v2_desc_as_dir(). Fix that. */
825 /* Though having similar elements, both functions were separated on
827 * - dirs don't care about encoded/encrypted introduction points, clients
829 * - dirs store descriptors in a separate cache by descriptor ID, whereas
830 * clients store them by service ID; both caches are different data
831 * structures and have different access methods.
832 * - dirs store a descriptor only if they are responsible for its ID,
833 * clients do so in every way (because they have requested it before).
834 * - dirs can process multiple concatenated descriptors which is required
835 * for replication, whereas clients only accept a single descriptor.
836 * Thus, combining both methods would result in a lot of if statements
837 * which probably would not improve, but worsen code readability. -KL */
838 rend_service_descriptor_t
*parsed
= NULL
;
839 char desc_id
[DIGEST_LEN
];
840 char *intro_content
= NULL
;
843 const char *next_desc
;
844 time_t now
= time(NULL
);
845 char key
[REND_SERVICE_ID_LEN_BASE32
+2];
846 char service_id
[REND_SERVICE_ID_LEN_BASE32
+1];
847 char want_desc_id
[DIGEST_LEN
];
848 rend_cache_entry_t
*e
;
849 rend_cache_store_status_t retval
= RCS_BADDESC
;
850 tor_assert(rend_cache
);
852 tor_assert(desc_id_base32
);
853 memset(want_desc_id
, 0, sizeof(want_desc_id
));
857 if (base32_decode(want_desc_id
, sizeof(want_desc_id
),
858 desc_id_base32
, strlen(desc_id_base32
)) != 0) {
859 log_warn(LD_BUG
, "Couldn't decode base32 %s for descriptor id.",
860 escaped_safe_str_client(desc_id_base32
));
863 /* Parse the descriptor. */
864 if (rend_parse_v2_service_descriptor(&parsed
, desc_id
, &intro_content
,
865 &intro_size
, &encoded_size
,
866 &next_desc
, desc
, 0) < 0) {
867 log_warn(LD_REND
, "Could not parse descriptor.");
870 /* Compute service ID from public key. */
871 if (rend_get_service_id(parsed
->pk
, service_id
)<0) {
872 log_warn(LD_REND
, "Couldn't compute service ID.");
875 if (rend_query
->onion_address
[0] != '\0' &&
876 strcmp(rend_query
->onion_address
, service_id
)) {
877 log_warn(LD_REND
, "Received service descriptor for service ID %s; "
878 "expected descriptor for service ID %s.",
879 service_id
, safe_str(rend_query
->onion_address
));
882 if (tor_memneq(desc_id
, want_desc_id
, DIGEST_LEN
)) {
883 log_warn(LD_REND
, "Received service descriptor for %s with incorrect "
884 "descriptor ID.", service_id
);
888 /* Decode/decrypt introduction points. */
889 if (intro_content
&& intro_size
> 0) {
891 if (rend_query
->auth_type
!= REND_NO_AUTH
&&
892 !tor_mem_is_zero(rend_query
->descriptor_cookie
,
893 sizeof(rend_query
->descriptor_cookie
))) {
894 char *ipos_decrypted
= NULL
;
895 size_t ipos_decrypted_size
;
896 if (rend_decrypt_introduction_points(&ipos_decrypted
,
897 &ipos_decrypted_size
,
898 rend_query
->descriptor_cookie
,
901 log_warn(LD_REND
, "Failed to decrypt introduction points. We are "
902 "probably unable to parse the encoded introduction points.");
904 /* Replace encrypted with decrypted introduction points. */
905 log_info(LD_REND
, "Successfully decrypted introduction points.");
906 tor_free(intro_content
);
907 intro_content
= ipos_decrypted
;
908 intro_size
= ipos_decrypted_size
;
911 n_intro_points
= rend_parse_introduction_points(parsed
, intro_content
,
913 if (n_intro_points
<= 0) {
914 log_warn(LD_REND
, "Failed to parse introduction points. Either the "
915 "service has published a corrupt descriptor or you have "
916 "provided invalid authorization data.");
918 } else if (n_intro_points
> MAX_INTRO_POINTS
) {
919 log_warn(LD_REND
, "Found too many introduction points on a hidden "
920 "service descriptor for %s. This is probably a (misguided) "
921 "attempt to improve reliability, but it could also be an "
922 "attempt to do a guard enumeration attack. Rejecting.",
923 safe_str_client(service_id
));
928 log_info(LD_REND
, "Descriptor does not contain any introduction points.");
929 parsed
->intro_nodes
= smartlist_new();
931 /* We don't need the encoded/encrypted introduction points any longer. */
932 tor_free(intro_content
);
933 /* Is descriptor too old? */
934 if (parsed
->timestamp
< now
- REND_CACHE_MAX_AGE
-REND_CACHE_MAX_SKEW
) {
935 log_warn(LD_REND
, "Service descriptor with service ID %s is too old.",
936 safe_str_client(service_id
));
939 /* Is descriptor too far in the future? */
940 if (parsed
->timestamp
> now
+ REND_CACHE_MAX_SKEW
) {
941 log_warn(LD_REND
, "Service descriptor with service ID %s is too far in "
942 "the future.", safe_str_client(service_id
));
945 /* Do we have the same exact copy already in our cache? */
946 tor_snprintf(key
, sizeof(key
), "2%s", service_id
);
947 e
= (rend_cache_entry_t
*) strmap_get_lc(rend_cache
, key
);
948 if (e
&& !strcmp(desc
, e
->desc
)) {
949 log_info(LD_REND
,"We already have this service descriptor %s.",
950 safe_str_client(service_id
));
953 /* Verify that we are not replacing an older descriptor. It's important to
954 * avoid an evil HSDir serving old descriptor. We validate if the
955 * timestamp is greater than and not equal because it's a rounded down
956 * timestamp to the hour so if the descriptor changed in the same hour,
957 * the rend cache failure will tells us if we have a new descriptor. */
958 if (e
&& e
->parsed
->timestamp
> parsed
->timestamp
) {
959 log_info(LD_REND
, "We already have a new enough service descriptor for "
960 "service ID %s with the same desc ID and version.",
961 safe_str_client(service_id
));
964 /* Lookup our failure cache for intro point that might be unsuable. */
965 validate_intro_point_failure(parsed
, service_id
);
966 /* It's now possible that our intro point list is empty, this means that
967 * this descriptor is useless to us because intro points have all failed
968 * somehow before. Discard the descriptor. */
969 if (smartlist_len(parsed
->intro_nodes
) == 0) {
970 log_info(LD_REND
, "Service descriptor with service ID %s, every "
971 "intro points are unusable. Discarding it.",
972 safe_str_client(service_id
));
975 /* Now either purge the current one and replace it's content or create a
976 * new one and add it to the rend cache. */
978 e
= tor_malloc_zero(sizeof(rend_cache_entry_t
));
979 strmap_set_lc(rend_cache
, key
, e
);
981 rend_cache_decrement_allocation(rend_cache_entry_allocation(e
));
982 rend_cache_failure_remove(e
->parsed
);
983 rend_service_descriptor_free(e
->parsed
);
987 e
->desc
= tor_malloc_zero(encoded_size
+ 1);
988 strlcpy(e
->desc
, desc
, encoded_size
+ 1);
989 e
->len
= encoded_size
;
990 rend_cache_increment_allocation(rend_cache_entry_allocation(e
));
991 log_debug(LD_REND
,"Successfully stored rend desc '%s', len %d.",
992 safe_str_client(service_id
), (int)encoded_size
);
1005 rend_service_descriptor_free(parsed
);
1006 tor_free(intro_content
);