Tor  0.4.7.0-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/microdesc.h"
#include "feature/nodelist/networkstatus.h"
#include "feature/stats/rephist.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 bool entry_has_decrypted_descriptor (const hs_cache_client_descriptor_t *entry)
 
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 void cache_client_desc_free_ (hs_cache_client_descriptor_t *desc)
 
static void cache_client_desc_free_void (void *ptr)
 
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 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_remove_as_client (const ed25519_public_key_t *key)
 
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)
 
size_t hs_cache_get_total_allocation (void)
 
void hs_cache_decrement_allocation (size_t n)
 
void hs_cache_increment_allocation (size_t n)
 

Variables

static size_t hs_cache_total_allocation = 0
 
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:554

Definition at line 548 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 773 of file hs_cache.c.

Referenced by hs_cache_clean_as_client().

◆ 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 232 of file hs_cache.c.

Referenced by hs_cache_clean_as_dir().

◆ 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 361 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 375 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 464 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 632 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 664 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 554 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 565 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 680 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 575 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 541 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 607 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 83 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 96 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 105 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 383 of file hs_cache.c.

Referenced by remove_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 133 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 526 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 533 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 514 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 198 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 689 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 144 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 748 of file hs_cache.c.

Referenced by lookup_v3_desc_as_client().

◆ entry_has_decrypted_descriptor()

static bool entry_has_decrypted_descriptor ( const hs_cache_client_descriptor_t *  entry)
inlinestatic

Helper function: Return true iff the cache entry has a decrypted descriptor.

A NULL desc object in the entry means that we were not able to decrypt the descriptor because we are likely lacking client authorization. It is still a valid entry but some operations can't be done without the decrypted descriptor thus this function MUST be used to safe guard access to the decrypted desc object.

Definition at line 43 of file hs_cache.c.

Referenced by cache_get_client_entry_size(), cache_store_as_client(), hs_cache_lookup_as_client(), and hs_cache_remove_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 942 of file hs_cache.c.

Referenced by clean_caches_callback().

◆ 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 339 of file hs_cache.c.

Referenced by clean_caches_callback().

◆ 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 1001 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 991 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 969 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 1021 of file hs_cache.c.

◆ hs_cache_decrement_allocation()

void hs_cache_decrement_allocation ( size_t  n)

Decrement the total bytes attributed to the rendezvous cache by n.

Definition at line 1164 of file hs_cache.c.

Referenced by remove_v3_desc_as_client().

◆ hs_cache_free_all()

void hs_cache_free_all ( void  )

Cleanup the hidden service cache subsystem.

Definition at line 1141 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 1117 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 1072 of file hs_cache.c.

◆ hs_cache_increment_allocation()

void hs_cache_increment_allocation ( size_t  n)

Increase the total bytes attributed to the rendezvous cache by n.

Definition at line 1181 of file hs_cache.c.

◆ hs_cache_init()

void hs_cache_init ( void  )

Initialize the hidden service cache subsystem.

Definition at line 1126 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 843 of file hs_cache.c.

Referenced by client_desc_has_arrived(), client_get_random_intro(), close_or_reextend_intro_circ(), intro_points_all_timed_out(), 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 318 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 823 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 951 of file hs_cache.c.

◆ hs_cache_remove_as_client()

void hs_cache_remove_as_client ( const ed25519_public_key_t key)

Remove and free a client cache descriptor entry for the given onion service ed25519 public key. If the descriptor is decoded, the intro circuits are closed if any.

This does nothing if no descriptor exists for the given key.

Definition at line 910 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 occurred and the descriptor was not stored.

Definition at line 874 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 281 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 437 of file hs_cache.c.

Referenced by cache_store_as_client(), hs_cache_lookup_as_client(), hs_cache_lookup_encoded_as_client(), and hs_cache_remove_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 72 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 406 of file hs_cache.c.

Referenced by cache_store_as_client(), and hs_cache_remove_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 56 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 416 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 64 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 354 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 349 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 52 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().