Tor  0.4.3.1-alpha-dev
Macros | Functions | Variables
hs_cache.c File Reference

Handle hidden service descriptor caches. More...

#include "core/or/or.h"
#include "app/config/config.h"
#include "lib/crypt_ops/crypto_format.h"
#include "lib/crypt_ops/crypto_util.h"
#include "feature/hs/hs_ident.h"
#include "feature/hs/hs_common.h"
#include "feature/hs/hs_client.h"
#include "feature/hs/hs_descriptor.h"
#include "feature/nodelist/networkstatus.h"
#include "feature/rend/rendcache.h"
#include "feature/hs/hs_cache.h"
#include "feature/nodelist/networkstatus_st.h"

Go to the source code of this file.

Macros

#define HS_CACHE_PRIVATE
 
#define cache_dir_desc_free(val)   FREE_AND_NULL(hs_cache_dir_descriptor_t, cache_dir_desc_free_, (val))
 
#define cache_client_desc_free(val)   FREE_AND_NULL(hs_cache_client_descriptor_t, cache_client_desc_free_, (val))
 
#define cache_intro_state_free(val)   FREE_AND_NULL(hs_cache_intro_state_t, cache_intro_state_free_, (val))
 
#define cache_client_intro_state_free(val)
 

Functions

static int cached_client_descriptor_has_expired (time_t now, const hs_cache_client_descriptor_t *cached_desc)
 
static void remove_v3_desc_as_dir (const hs_cache_dir_descriptor_t *desc)
 
static void store_v3_desc_as_dir (hs_cache_dir_descriptor_t *desc)
 
static hs_cache_dir_descriptor_tlookup_v3_desc_as_dir (const uint8_t *key)
 
static void cache_dir_desc_free_ (hs_cache_dir_descriptor_t *desc)
 
static void cache_dir_desc_free_void (void *ptr)
 
static hs_cache_dir_descriptor_tcache_dir_desc_new (const char *desc)
 
static size_t cache_get_dir_entry_size (const hs_cache_dir_descriptor_t *entry)
 
static int cache_store_v3_as_dir (hs_cache_dir_descriptor_t *desc)
 
static int cache_lookup_v3_as_dir (const char *query, const char **desc_out)
 
STATIC size_t cache_clean_v3_as_dir (time_t now, time_t global_cutoff)
 
int hs_cache_store_as_dir (const char *desc)
 
int hs_cache_lookup_as_dir (uint32_t version, const char *query, const char **desc_out)
 
void hs_cache_clean_as_dir (time_t now)
 
static size_t cache_get_client_entry_size (const hs_cache_client_descriptor_t *entry)
 
static void remove_v3_desc_as_client (const hs_cache_client_descriptor_t *desc)
 
static void store_v3_desc_as_client (hs_cache_client_descriptor_t *desc)
 
STATIC hs_cache_client_descriptor_t * lookup_v3_desc_as_client (const uint8_t *key)
 
static hs_cache_client_descriptor_t * cache_client_desc_new (const char *desc_str, const ed25519_public_key_t *service_identity_pk, hs_desc_decode_status_t *decode_status_out)
 
static void cache_client_desc_free_ (hs_cache_client_descriptor_t *desc)
 
static void cache_client_desc_free_void (void *ptr)
 
static hs_cache_intro_state_tcache_intro_state_new (void)
 
static void cache_intro_state_free_ (hs_cache_intro_state_t *state)
 
static void cache_intro_state_free_void (void *state)
 
static hs_cache_client_intro_state_tcache_client_intro_state_new (void)
 
static void cache_client_intro_state_free_ (hs_cache_client_intro_state_t *cache)
 
static void cache_client_intro_state_free_void (void *entry)
 
static int cache_client_intro_state_lookup (const ed25519_public_key_t *service_pk, const ed25519_public_key_t *auth_key, hs_cache_intro_state_t **entry)
 
static void cache_client_intro_state_note (hs_cache_intro_state_t *state, rend_intro_point_failure_t failure)
 
static void cache_client_intro_state_add (const ed25519_public_key_t *service_pk, const ed25519_public_key_t *auth_key, hs_cache_intro_state_t **state)
 
static void cache_client_intro_state_clean (time_t cutoff, hs_cache_client_intro_state_t *cache)
 
static int cache_client_intro_state_is_empty (const hs_cache_client_intro_state_t *cache)
 
static int cache_store_as_client (hs_cache_client_descriptor_t *client_desc)
 
static size_t cache_clean_v3_as_client (time_t now)
 
const char * hs_cache_lookup_encoded_as_client (const ed25519_public_key_t *key)
 
const hs_descriptor_ths_cache_lookup_as_client (const ed25519_public_key_t *key)
 
hs_desc_decode_status_t hs_cache_store_as_client (const char *desc_str, const ed25519_public_key_t *identity_pk)
 
void hs_cache_clean_as_client (time_t now)
 
void hs_cache_purge_as_client (void)
 
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)
 
const hs_cache_intro_state_ths_cache_client_intro_state_find (const ed25519_public_key_t *service_pk, const ed25519_public_key_t *auth_key)
 
void hs_cache_client_intro_state_clean (time_t now)
 
void hs_cache_client_intro_state_purge (void)
 
bool hs_cache_client_new_auth_parse (const ed25519_public_key_t *service_pk)
 
size_t hs_cache_handle_oom (time_t now, size_t min_remove_bytes)
 
unsigned int hs_cache_get_max_descriptor_size (void)
 
void hs_cache_init (void)
 
void hs_cache_free_all (void)
 

Variables

static digest256map_t * hs_cache_v3_dir
 
static digest256map_t * hs_cache_v3_client
 
static digest256map_t * hs_cache_client_intro_state
 

Detailed Description

Handle hidden service descriptor caches.

Definition in file hs_cache.c.

Macro Definition Documentation

◆ cache_client_intro_state_free

#define cache_client_intro_state_free (   val)
Value:
static void cache_client_intro_state_free_(hs_cache_client_intro_state_t *cache)
Definition: hs_cache.c:512

Definition at line 506 of file hs_cache.c.

Function Documentation

◆ cache_clean_v3_as_client()

static size_t cache_clean_v3_as_client ( time_t  now)
static

clean the client cache using now as the current time. Return the total size of removed bytes from the cache.

Definition at line 723 of file hs_cache.c.

◆ cache_clean_v3_as_dir()

STATIC size_t cache_clean_v3_as_dir ( time_t  now,
time_t  global_cutoff 
)

Clean the v3 cache by removing any entry that has expired using the global_cutoff value. If global_cutoff is 0, the cleaning process will use the lifetime found in the plaintext data section. Return the number of bytes cleaned.

Definition at line 210 of file hs_cache.c.

◆ cache_client_desc_free_()

static void cache_client_desc_free_ ( hs_cache_client_descriptor_t *  desc)
static

Free memory allocated by desc.

Definition at line 450 of file hs_cache.c.

◆ cache_client_desc_free_void()

static void cache_client_desc_free_void ( void *  ptr)
static

Helper function: Use by the free all function to clear the client cache

Definition at line 464 of file hs_cache.c.

◆ cache_client_desc_new()

static hs_cache_client_descriptor_t* cache_client_desc_new ( const char *  desc_str,
const ed25519_public_key_t service_identity_pk,
hs_desc_decode_status_t decode_status_out 
)
static

Parse the encoded descriptor in desc_str using service_identity_pk to decrypt it first.

If everything goes well, allocate and return a new hs_cache_client_descriptor_t object. In case of error, return NULL.

Definition at line 397 of file hs_cache.c.

Referenced by hs_cache_store_as_client().

◆ cache_client_intro_state_add()

static void cache_client_intro_state_add ( const ed25519_public_key_t service_pk,
const ed25519_public_key_t auth_key,
hs_cache_intro_state_t **  state 
)
static

For the given service identity key service_pk and an introduction authentication key auth_key, add an entry in the client intro state cache If no entry exists for the service, it will create one. If state is non NULL, it will point to the new intro state entry.

Definition at line 590 of file hs_cache.c.

Referenced by hs_cache_client_intro_state_note().

◆ cache_client_intro_state_clean()

static void cache_client_intro_state_clean ( time_t  cutoff,
hs_cache_client_intro_state_t cache 
)
static

Remove every intro point state entry from cache that has been created before or at the cutoff.

Definition at line 622 of file hs_cache.c.

◆ cache_client_intro_state_free_()

static void cache_client_intro_state_free_ ( hs_cache_client_intro_state_t cache)
static

Free a cache_client_intro_state object.

Definition at line 512 of file hs_cache.c.

Referenced by cache_client_intro_state_free_void().

◆ cache_client_intro_state_free_void()

static void cache_client_intro_state_free_void ( void *  entry)
static

Helper function: used by the free all function.

Definition at line 523 of file hs_cache.c.

◆ cache_client_intro_state_is_empty()

static int cache_client_intro_state_is_empty ( const hs_cache_client_intro_state_t cache)
static

Return true iff no intro points are in this cache.

Definition at line 638 of file hs_cache.c.

◆ cache_client_intro_state_lookup()

static int cache_client_intro_state_lookup ( const ed25519_public_key_t service_pk,
const ed25519_public_key_t auth_key,
hs_cache_intro_state_t **  entry 
)
static

For the given service identity key service_pk and an introduction authentication key auth_key, lookup the intro state object. Return 1 if found and put it in entry if not NULL. Return 0 if not found and entry is untouched.

Definition at line 533 of file hs_cache.c.

Referenced by hs_cache_client_intro_state_find(), and hs_cache_client_intro_state_note().

◆ cache_client_intro_state_new()

static hs_cache_client_intro_state_t* cache_client_intro_state_new ( void  )
static

Return a newly allocated and initialized hs_cache_client_intro_state_t object.

Definition at line 499 of file hs_cache.c.

Referenced by cache_client_intro_state_add().

◆ cache_client_intro_state_note()

static void cache_client_intro_state_note ( hs_cache_intro_state_t state,
rend_intro_point_failure_t  failure 
)
static

Note the given failure in state.

Definition at line 565 of file hs_cache.c.

Referenced by hs_cache_client_intro_state_note().

◆ cache_dir_desc_free_()

static void cache_dir_desc_free_ ( hs_cache_dir_descriptor_t desc)
static

Free a directory descriptor object.

Definition at line 64 of file hs_cache.c.

Referenced by cache_dir_desc_free_void().

◆ cache_dir_desc_free_void()

static void cache_dir_desc_free_void ( void *  ptr)
static

Helper function: Use by the free all function using the digest256map interface to cache entries.

Definition at line 77 of file hs_cache.c.

◆ cache_dir_desc_new()

static hs_cache_dir_descriptor_t* cache_dir_desc_new ( const char *  desc)
static

Create a new directory cache descriptor object from a encoded descriptor. On success, return the heap-allocated cache object, otherwise return NULL if we can't decode the descriptor.

Definition at line 86 of file hs_cache.c.

Referenced by hs_cache_store_as_dir().

◆ cache_get_client_entry_size()

static size_t cache_get_client_entry_size ( const hs_cache_client_descriptor_t *  entry)
static

Return the size of a client cache entry in bytes.

Definition at line 342 of file hs_cache.c.

Referenced by remove_v3_desc_as_client(), and store_v3_desc_as_client().

◆ cache_get_dir_entry_size()

static size_t cache_get_dir_entry_size ( const hs_cache_dir_descriptor_t entry)
static

Return the size of a cache entry in bytes.

Definition at line 114 of file hs_cache.c.

◆ cache_intro_state_free_()

static void cache_intro_state_free_ ( hs_cache_intro_state_t state)
static

Free an hs_cache_intro_state_t object.

Definition at line 484 of file hs_cache.c.

Referenced by cache_intro_state_free_void().

◆ cache_intro_state_free_void()

static void cache_intro_state_free_void ( void *  state)
static

Helper function: used by the free all function.

Definition at line 491 of file hs_cache.c.

◆ cache_intro_state_new()

static hs_cache_intro_state_t* cache_intro_state_new ( void  )
static

Return a newly allocated and initialized hs_cache_intro_state_t object.

Definition at line 472 of file hs_cache.c.

Referenced by cache_client_intro_state_add().

◆ cache_lookup_v3_as_dir()

static int cache_lookup_v3_as_dir ( const char *  query,
const char **  desc_out 
)
static

Using the query which is the base64 encoded blinded key of a version 3 descriptor, lookup in our directory cache the entry. If found, 1 is returned and desc_out is populated with a newly allocated string being the encoded descriptor. If not found, 0 is returned and desc_out is untouched. On error, a negative value is returned and desc_out is untouched.

Definition at line 176 of file hs_cache.c.

Referenced by hs_cache_lookup_as_dir().

◆ cache_store_as_client()

static int cache_store_as_client ( hs_cache_client_descriptor_t *  client_desc)
static

Check whether client_desc is useful for us, and store it in the client-side HS cache if so. The client_desc is freed if we already have a fresher (higher revision counter count) in the cache.

Definition at line 647 of file hs_cache.c.

◆ cache_store_v3_as_dir()

static int cache_store_v3_as_dir ( hs_cache_dir_descriptor_t desc)
static

Try to store a valid version 3 descriptor in the directory cache. Return 0 on success else a negative value is returned indicating that we have a newer version in our cache. On error, caller is responsible to free the given descriptor desc.

Definition at line 125 of file hs_cache.c.

Referenced by hs_cache_store_as_dir().

◆ cached_client_descriptor_has_expired()

static int cached_client_descriptor_has_expired ( time_t  now,
const hs_cache_client_descriptor_t *  cached_desc 
)
static

Return true iff the cached client descriptor at cached_desc has expired.

Definition at line 700 of file hs_cache.c.

Referenced by lookup_v3_desc_as_client().

◆ hs_cache_clean_as_client()

void hs_cache_clean_as_client ( time_t  now)

Clean all client caches using the current time now.

Definition at line 852 of file hs_cache.c.

◆ hs_cache_clean_as_dir()

void hs_cache_clean_as_dir ( time_t  now)

Clean all directory caches using the current time now.

Definition at line 317 of file hs_cache.c.

◆ hs_cache_client_intro_state_clean()

void hs_cache_client_intro_state_clean ( time_t  now)

Cleanup the client introduction state cache.

Definition at line 913 of file hs_cache.c.

Referenced by rend_cache_failure_clean_callback().

◆ hs_cache_client_intro_state_find()

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 
)

For a given service identity public key and an introduction authentication key, return true iff it is present in the failure cache.

Definition at line 903 of file hs_cache.c.

Referenced by intro_point_is_usable().

◆ hs_cache_client_intro_state_note()

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 
)

For a given service identity public key and an introduction authentication key, note the given failure in the client intro state cache.

Definition at line 881 of file hs_cache.c.

◆ hs_cache_client_intro_state_purge()

void hs_cache_client_intro_state_purge ( void  )

Purge the client introduction state cache.

Definition at line 933 of file hs_cache.c.

◆ hs_cache_free_all()

void hs_cache_free_all ( void  )

Cleanup the hidden service cache subsystem.

Definition at line 1059 of file hs_cache.c.

Referenced by hs_free_all().

◆ hs_cache_get_max_descriptor_size()

unsigned int hs_cache_get_max_descriptor_size ( void  )

Return the maximum size of a v3 HS descriptor.

Definition at line 1035 of file hs_cache.c.

◆ hs_cache_handle_oom()

size_t hs_cache_handle_oom ( time_t  now,
size_t  min_remove_bytes 
)

Do a round of OOM cleanup on all directory caches. Return the amount of removed bytes. It is possible that the returned value is lower than min_remove_bytes if the caches get emptied out so the caller should be aware of this.

Definition at line 984 of file hs_cache.c.

◆ hs_cache_init()

void hs_cache_init ( void  )

Initialize the hidden service cache subsystem.

Definition at line 1044 of file hs_cache.c.

Referenced by hs_init().

◆ hs_cache_lookup_as_client()

const hs_descriptor_t* hs_cache_lookup_as_client ( const ed25519_public_key_t key)

Public API: Given the HS ed25519 identity public key in key, return its HS descriptor if it's stored in our cache, or NULL if not or if the descriptor was never decrypted. The later can happen if we are waiting for client authorization to be added.

Definition at line 789 of file hs_cache.c.

Referenced by client_desc_has_arrived(), client_get_random_intro(), close_or_reextend_intro_circ(), and setup_intro_circ_auth_key().

◆ hs_cache_lookup_as_dir()

int hs_cache_lookup_as_dir ( uint32_t  version,
const char *  query,
const char **  desc_out 
)

Using the query, lookup in our directory cache the entry. If found, 1 is returned and desc_out is populated with a newly allocated string being the encoded descriptor. If not found, 0 is returned and desc_out is untouched. On error, a negative value is returned and desc_out is untouched.

Definition at line 296 of file hs_cache.c.

Referenced by handle_get_hs_descriptor_v3().

◆ hs_cache_lookup_encoded_as_client()

const char* hs_cache_lookup_encoded_as_client ( const ed25519_public_key_t key)

Public API: Given the HS ed25519 identity public key in key, return its HS encoded descriptor if it's stored in our cache, or NULL if not.

Definition at line 769 of file hs_cache.c.

◆ hs_cache_purge_as_client()

void hs_cache_purge_as_client ( void  )

Purge the client descriptor cache.

Definition at line 863 of file hs_cache.c.

◆ hs_cache_store_as_client()

hs_desc_decode_status_t hs_cache_store_as_client ( const char *  desc_str,
const ed25519_public_key_t identity_pk 
)

Public API: Given an encoded descriptor, store it in the client HS cache. Return a decode status which changes how we handle the SOCKS connection depending on its value:

HS_DESC_DECODE_OK: Returned on success. Descriptor was properly decoded and is now stored.

HS_DESC_DECODE_NEED_CLIENT_AUTH: Client authorization is needed but the descriptor was still stored.

HS_DESC_DECODE_BAD_CLIENT_AUTH: Client authorization for this descriptor was not usable but the descriptor was still stored.

Any other codes means indicate where the error occured and the descriptor was not stored.

Definition at line 820 of file hs_cache.c.

Referenced by client_dir_fetch_200().

◆ hs_cache_store_as_dir()

int hs_cache_store_as_dir ( const char *  desc)

Given an encoded descriptor, store it in the directory cache depending on which version it is. Return a negative value on error. On success, 0 is returned.

Definition at line 259 of file hs_cache.c.

◆ lookup_v3_desc_as_client()

STATIC hs_cache_client_descriptor_t* lookup_v3_desc_as_client ( const uint8_t *  key)

Query our cache and return the entry or NULL if not found or if expired.

Definition at line 370 of file hs_cache.c.

Referenced by cache_store_as_client(), hs_cache_lookup_as_client(), and hs_cache_lookup_encoded_as_client().

◆ lookup_v3_desc_as_dir()

static hs_cache_dir_descriptor_t* lookup_v3_desc_as_dir ( const uint8_t *  key)
static

Query our cache and return the entry or NULL if not found.

Definition at line 53 of file hs_cache.c.

Referenced by cache_store_v3_as_dir().

◆ remove_v3_desc_as_client()

static void remove_v3_desc_as_client ( const hs_cache_client_descriptor_t *  desc)
static

Remove a given descriptor from our cache.

Definition at line 350 of file hs_cache.c.

Referenced by cache_store_as_client().

◆ remove_v3_desc_as_dir()

static void remove_v3_desc_as_dir ( const hs_cache_dir_descriptor_t desc)
static

Remove a given descriptor from our cache.

Definition at line 37 of file hs_cache.c.

◆ store_v3_desc_as_client()

static void store_v3_desc_as_client ( hs_cache_client_descriptor_t *  desc)
static

Store a given descriptor in our cache.

Definition at line 360 of file hs_cache.c.

◆ store_v3_desc_as_dir()

static void store_v3_desc_as_dir ( hs_cache_dir_descriptor_t desc)
static

Store a given descriptor in our cache.

Definition at line 45 of file hs_cache.c.

Variable Documentation

◆ hs_cache_client_intro_state

digest256map_t* hs_cache_client_intro_state
static

Client-side introduction point state cache. Map indexed by service public identity key (onion address). It contains hs_cache_client_intro_state_t objects all related to a specific service.

Definition at line 338 of file hs_cache.c.

Referenced by cache_client_intro_state_add(), cache_client_intro_state_lookup(), and hs_cache_init().

◆ hs_cache_v3_client

digest256map_t* hs_cache_v3_client
static

Client-side HS descriptor cache. Map indexed by service identity key.

Definition at line 333 of file hs_cache.c.

Referenced by cache_clean_v3_as_client(), hs_cache_init(), lookup_v3_desc_as_client(), remove_v3_desc_as_client(), and store_v3_desc_as_client().

◆ hs_cache_v3_dir

digest256map_t* hs_cache_v3_dir
static

Directory descriptor cache. Map indexed by blinded key.

Definition at line 33 of file hs_cache.c.

Referenced by cache_clean_v3_as_dir(), hs_cache_init(), lookup_v3_desc_as_dir(), remove_v3_desc_as_dir(), and store_v3_desc_as_dir().