Tor 0.4.9.2-alpha-dev
All Data Structures Files Functions Variables Typedefs Enumerations Enumerator Macros Modules Pages
hs_cache.h
Go to the documentation of this file.
1/* Copyright (c) 2016-2021, The Tor Project, Inc. */
2/* See LICENSE for licensing information */
3
4/**
5 * \file hs_cache.h
6 * \brief Header file for hs_cache.c
7 **/
8
9#ifndef TOR_HS_CACHE_H
10#define TOR_HS_CACHE_H
11
12#include <stdint.h>
13
16#include "feature/hs/hs_ident.h"
19
21
22/** This is the maximum time an introduction point state object can stay in the
23 * client cache in seconds (2 mins or 120 seconds). */
24#define HS_CACHE_CLIENT_INTRO_STATE_MAX_AGE (2 * 60)
25/** How old do we let hidden service descriptors get before discarding
26 * them as too old? */
27#define HS_CACHE_MAX_AGE (2*24*60*60)
28/** How wrong do we assume our clock may be when checking whether hidden
29 * services are too old or too new? */
30#define HS_CACHE_MAX_SKEW (24*60*60)
31/** How old do we keep an intro point failure entry in the failure cache? */
32#define HS_CACHE_FAILURE_MAX_AGE (5*60)
33
34/** Introduction point state. */
35typedef struct hs_cache_intro_state_t {
36 /** When this entry was created and put in the cache. */
37 time_t created_ts;
38
39 /** Did it suffered a generic error? */
40 unsigned int error : 1;
41
42 /** Did it timed out? */
43 unsigned int timed_out : 1;
44
45 /** How many times we tried to reached it and it was unreachable. */
48
50 /** Contains hs_cache_intro_state_t object indexed by introduction point
51 * authentication key. */
52 digest256map_t *intro_points;
54
55/** Descriptor representation on the directory side which is a subset of
56 * information that the HSDir can decode and serve it. */
58 /** This object is indexed using the blinded pubkey located in the plaintext
59 * data which is populated only once the descriptor has been successfully
60 * decoded and validated. This simply points to that pubkey. */
61 const uint8_t *key;
62
63 /** When does this entry has been created. Used to expire entries. */
64 time_t created_ts;
65
66 /** Descriptor plaintext information. Obviously, we can't decrypt the
67 * encrypted part of the descriptor. */
69 /** Encoded descriptor which is basically in text form. It's a NUL terminated
70 * string thus safe to strlen(). */
72 /** How many times this descriptor has been downloaded. We use this as an
73 * heuristic for the OOM cache cleaning. It is very large so we avoid an kind
74 * of possible wrapping. */
75 uint64_t n_downloaded;
77
78/* Public API */
79
80/* Return maximum lifetime in seconds of a cache entry. */
81static inline time_t
82hs_cache_max_entry_lifetime(void)
83{
85}
86
87void hs_cache_init(void);
88void hs_cache_free_all(void);
89void hs_cache_clean_as_dir(time_t now);
90size_t hs_cache_handle_oom(size_t min_remove_bytes);
91
92unsigned int hs_cache_get_max_descriptor_size(void);
93
94/* Store and Lookup function. They are version agnostic that is depending on
95 * the requested version of the descriptor, it will be re-routed to the
96 * right function. */
97int hs_cache_store_as_dir(const char *desc);
98int hs_cache_lookup_as_dir(uint32_t version, const char *query,
99 const char **desc_out);
101
102const hs_descriptor_t *
104const char *
107 const struct ed25519_public_key_t *identity_pk);
108void hs_cache_remove_as_client(const struct ed25519_public_key_t *key);
109void hs_cache_clean_as_client(time_t now);
110void hs_cache_purge_as_client(void);
111
112/* Client failure cache. */
114 const struct ed25519_public_key_t *service_pk,
115 const struct ed25519_public_key_t *auth_key,
116 rend_intro_point_failure_t failure);
118 const struct ed25519_public_key_t *service_pk,
119 const struct ed25519_public_key_t *auth_key);
120void hs_cache_client_intro_state_clean(time_t now);
122
123bool hs_cache_client_new_auth_parse(const ed25519_public_key_t *service_pk);
124
125size_t hs_cache_get_total_allocation(void);
126void hs_cache_decrement_allocation(size_t n);
127void hs_cache_increment_allocation(size_t n);
128
129#ifdef HS_CACHE_PRIVATE
131
132/** Represents a locally cached HS descriptor on a hidden service client. */
133typedef struct hs_cache_client_descriptor_t {
134 /** This object is indexed using the service identity public key */
135 struct ed25519_public_key_t key;
136
137 /** When will this entry expire? We expire cached client descriptors in the
138 * start of the next time period, since that's when clients need to start
139 * using the next blinded key of the service. */
140 time_t expiration_ts;
141
142 /** The cached decoded descriptor, this object is the owner. This can be
143 * NULL if the descriptor couldn't be decoded due to missing or bad client
144 * authorization. It can be decoded later from the encoded_desc object if
145 * the proper client authorization is given tor. */
146 hs_descriptor_t *desc;
147
148 /** Encoded descriptor in string form. Can't be NULL. */
149 char *encoded_desc;
150} hs_cache_client_descriptor_t;
151
152STATIC size_t cache_clean_v3_as_dir(time_t now, time_t global_cutoff);
153STATIC size_t cache_clean_v3_by_downloaded_as_dir(const uint64_t target,
154 const size_t min_remove_bytes,
155 uint64_t *next_lowest);
157
158STATIC hs_cache_client_descriptor_t *
159lookup_v3_desc_as_client(const uint8_t *key);
160
161#ifdef TOR_UNIT_TESTS
162void dir_set_downloaded(const ed25519_public_key_t *pk, uint64_t value);
163#endif /* TOR_UNIT_TESTS */
164
165#endif /* defined(HS_CACHE_PRIVATE) */
166
167#endif /* !defined(TOR_HS_CACHE_H) */
Header for crypto_ed25519.c.
hs_desc_decode_status_t hs_cache_store_as_client(const char *desc_str, const ed25519_public_key_t *identity_pk)
Definition: hs_cache.c:959
void hs_cache_remove_as_client(const ed25519_public_key_t *key)
Definition: hs_cache.c:995
STATIC size_t cache_clean_v3_by_downloaded_as_dir(const uint64_t target, const size_t max_remove_bytes, uint64_t *next_lowest)
Definition: hs_cache.c:239
const char * hs_cache_lookup_encoded_as_client(const ed25519_public_key_t *key)
Definition: hs_cache.c:908
STATIC size_t cache_clean_v3_as_dir(time_t now, time_t global_cutoff)
Definition: hs_cache.c:299
const hs_cache_intro_state_t * hs_cache_client_intro_state_find(const ed25519_public_key_t *service_pk, const ed25519_public_key_t *auth_key)
Definition: hs_cache.c:1076
void hs_cache_client_intro_state_note(const ed25519_public_key_t *service_pk, const ed25519_public_key_t *auth_key, rend_intro_point_failure_t failure)
Definition: hs_cache.c:1054
STATIC hs_cache_dir_descriptor_t * lookup_v3_desc_as_dir(const uint8_t *key)
Definition: hs_cache.c:73
const hs_descriptor_t * hs_cache_lookup_as_client(const ed25519_public_key_t *key)
Definition: hs_cache.c:928
STATIC hs_cache_client_descriptor_t * lookup_v3_desc_as_client(const uint8_t *key)
Definition: hs_cache.c:520
void hs_cache_client_intro_state_clean(time_t now)
Definition: hs_cache.c:1086
void hs_cache_clean_as_client(time_t now)
Definition: hs_cache.c:1027
void hs_cache_client_intro_state_purge(void)
Definition: hs_cache.c:1106
size_t hs_cache_handle_oom(size_t min_remove_bytes)
Definition: hs_cache.c:1157
#define HS_CACHE_MAX_AGE
Definition: hs_cache.h:27
#define HS_CACHE_MAX_SKEW
Definition: hs_cache.h:30
void hs_cache_free_all(void)
Definition: hs_cache.c:1210
int hs_cache_lookup_as_dir(uint32_t version, const char *query, const char **desc_out)
Definition: hs_cache.c:385
void hs_cache_mark_dowloaded_as_dir(const hs_ident_dir_conn_t *ident)
Definition: hs_cache.c:408
void hs_cache_decrement_allocation(size_t n)
Definition: hs_cache.c:1233
int hs_cache_store_as_dir(const char *desc)
Definition: hs_cache.c:348
unsigned int hs_cache_get_max_descriptor_size(void)
Definition: hs_cache.c:1186
void hs_cache_clean_as_dir(time_t now)
Definition: hs_cache.c:422
void hs_cache_purge_as_client(void)
Definition: hs_cache.c:1036
void hs_cache_increment_allocation(size_t n)
Definition: hs_cache.c:1250
void hs_cache_init(void)
Definition: hs_cache.c:1195
Header file containing common data for the whole HS subsystem.
Header file for hs_descriptor.c.
hs_desc_decode_status_t
Definition: hs_descriptor.h:75
Header file containing circuit and connection identifier data for the whole HS subsystem.
Header file for rendcommon.c.
digest256map_t * intro_points
Definition: hs_cache.h:52
hs_desc_plaintext_data_t * plaintext_data
Definition: hs_cache.h:68
const uint8_t * key
Definition: hs_cache.h:61
unsigned int error
Definition: hs_cache.h:40
unsigned int timed_out
Definition: hs_cache.h:43
uint32_t unreachable_count
Definition: hs_cache.h:46
#define STATIC
Definition: testsupport.h:32
Header for torcert.c.