|
@@ -9,25 +9,62 @@
|
|
|
#define HS_SERVICE_PRIVATE
|
|
|
|
|
|
#include "or.h"
|
|
|
+#include "circpathbias.h"
|
|
|
+#include "circuitbuild.h"
|
|
|
#include "circuitlist.h"
|
|
|
+#include "circuituse.h"
|
|
|
#include "config.h"
|
|
|
+#include "directory.h"
|
|
|
+#include "main.h"
|
|
|
+#include "networkstatus.h"
|
|
|
+#include "nodelist.h"
|
|
|
#include "relay.h"
|
|
|
#include "rendservice.h"
|
|
|
#include "router.h"
|
|
|
#include "routerkeys.h"
|
|
|
+#include "routerlist.h"
|
|
|
+#include "statefile.h"
|
|
|
|
|
|
+#include "hs_circuit.h"
|
|
|
#include "hs_common.h"
|
|
|
#include "hs_config.h"
|
|
|
+#include "hs_circuit.h"
|
|
|
+#include "hs_descriptor.h"
|
|
|
+#include "hs_ident.h"
|
|
|
#include "hs_intropoint.h"
|
|
|
#include "hs_service.h"
|
|
|
|
|
|
-#include "hs/cell_establish_intro.h"
|
|
|
+/* Trunnel */
|
|
|
+#include "ed25519_cert.h"
|
|
|
#include "hs/cell_common.h"
|
|
|
+#include "hs/cell_establish_intro.h"
|
|
|
+
|
|
|
+/* Helper macro. Iterate over every service in the global map. The var is the
|
|
|
+ * name of the service pointer. */
|
|
|
+#define FOR_EACH_SERVICE_BEGIN(var) \
|
|
|
+ STMT_BEGIN \
|
|
|
+ hs_service_t **var##_iter, *var; \
|
|
|
+ HT_FOREACH(var##_iter, hs_service_ht, hs_service_map) { \
|
|
|
+ var = *var##_iter;
|
|
|
+#define FOR_EACH_SERVICE_END } STMT_END ;
|
|
|
+
|
|
|
+/* Helper macro. Iterate over both current and previous descriptor of a
|
|
|
+ * service. The var is the name of the descriptor pointer. This macro skips
|
|
|
+ * any descriptor object of the service that is NULL. */
|
|
|
+#define FOR_EACH_DESCRIPTOR_BEGIN(service, var) \
|
|
|
+ STMT_BEGIN \
|
|
|
+ hs_service_descriptor_t *var; \
|
|
|
+ for (int var ## _loop_idx = 0; var ## _loop_idx < 2; \
|
|
|
+ ++var ## _loop_idx) { \
|
|
|
+ (var ## _loop_idx == 0) ? (var = service->desc_current) : \
|
|
|
+ (var = service->desc_next); \
|
|
|
+ if (var == NULL) continue;
|
|
|
+#define FOR_EACH_DESCRIPTOR_END } STMT_END ;
|
|
|
|
|
|
/* Onion service directory file names. */
|
|
|
-static const char *fname_keyfile_prefix = "hs_ed25519";
|
|
|
-static const char *fname_hostname = "hostname";
|
|
|
-static const char *address_tld = "onion";
|
|
|
+static const char fname_keyfile_prefix[] = "hs_ed25519";
|
|
|
+static const char fname_hostname[] = "hostname";
|
|
|
+static const char address_tld[] = "onion";
|
|
|
|
|
|
/* Staging list of service object. When configuring service, we add them to
|
|
|
* this list considered a staging area and they will get added to our global
|
|
@@ -35,6 +72,8 @@ static const char *address_tld = "onion";
|
|
|
* loading keys requires that we are an actual running tor process. */
|
|
|
static smartlist_t *hs_service_staging_list;
|
|
|
|
|
|
+static void set_descriptor_revision_counter(hs_descriptor_t *hs_desc);
|
|
|
+
|
|
|
/* Helper: Function to compare two objects in the service map. Return 1 if the
|
|
|
* two service have the same master public identity key. */
|
|
|
static inline int
|
|
@@ -137,10 +176,10 @@ static void
|
|
|
set_service_default_config(hs_service_config_t *c,
|
|
|
const or_options_t *options)
|
|
|
{
|
|
|
+ (void) options;
|
|
|
tor_assert(c);
|
|
|
c->ports = smartlist_new();
|
|
|
c->directory_path = NULL;
|
|
|
- c->descriptor_post_period = options->RendPostPeriod;
|
|
|
c->max_streams_per_rdv_circuit = 0;
|
|
|
c->max_streams_close_circuit = 0;
|
|
|
c->num_intro_points = NUM_INTRO_POINTS_DEFAULT;
|
|
@@ -167,6 +206,77 @@ service_clear_config(hs_service_config_t *config)
|
|
|
memset(config, 0, sizeof(*config));
|
|
|
}
|
|
|
|
|
|
+/* Return the lower bound of maximum INTRODUCE2 cells per circuit before we
|
|
|
+ * rotate intro point (defined by a consensus parameter or the default
|
|
|
+ * value). */
|
|
|
+static int32_t
|
|
|
+get_intro_point_min_introduce2(void)
|
|
|
+{
|
|
|
+ /* The [0, 2147483647] range is quite large to accomodate anything we decide
|
|
|
+ * in the future. */
|
|
|
+ return networkstatus_get_param(NULL, "hs_intro_min_introduce2",
|
|
|
+ INTRO_POINT_MIN_LIFETIME_INTRODUCTIONS,
|
|
|
+ 0, INT32_MAX);
|
|
|
+}
|
|
|
+
|
|
|
+/* Return the upper bound of maximum INTRODUCE2 cells per circuit before we
|
|
|
+ * rotate intro point (defined by a consensus parameter or the default
|
|
|
+ * value). */
|
|
|
+static int32_t
|
|
|
+get_intro_point_max_introduce2(void)
|
|
|
+{
|
|
|
+ /* The [0, 2147483647] range is quite large to accomodate anything we decide
|
|
|
+ * in the future. */
|
|
|
+ return networkstatus_get_param(NULL, "hs_intro_max_introduce2",
|
|
|
+ INTRO_POINT_MAX_LIFETIME_INTRODUCTIONS,
|
|
|
+ 0, INT32_MAX);
|
|
|
+}
|
|
|
+
|
|
|
+/* Return the minimum lifetime in seconds of an introduction point defined by a
|
|
|
+ * consensus parameter or the default value. */
|
|
|
+static int32_t
|
|
|
+get_intro_point_min_lifetime(void)
|
|
|
+{
|
|
|
+#define MIN_INTRO_POINT_LIFETIME_TESTING 10
|
|
|
+ if (get_options()->TestingTorNetwork) {
|
|
|
+ return MIN_INTRO_POINT_LIFETIME_TESTING;
|
|
|
+ }
|
|
|
+
|
|
|
+ /* The [0, 2147483647] range is quite large to accomodate anything we decide
|
|
|
+ * in the future. */
|
|
|
+ return networkstatus_get_param(NULL, "hs_intro_min_lifetime",
|
|
|
+ INTRO_POINT_LIFETIME_MIN_SECONDS,
|
|
|
+ 0, INT32_MAX);
|
|
|
+}
|
|
|
+
|
|
|
+/* Return the maximum lifetime in seconds of an introduction point defined by a
|
|
|
+ * consensus parameter or the default value. */
|
|
|
+static int32_t
|
|
|
+get_intro_point_max_lifetime(void)
|
|
|
+{
|
|
|
+#define MAX_INTRO_POINT_LIFETIME_TESTING 30
|
|
|
+ if (get_options()->TestingTorNetwork) {
|
|
|
+ return MAX_INTRO_POINT_LIFETIME_TESTING;
|
|
|
+ }
|
|
|
+
|
|
|
+ /* The [0, 2147483647] range is quite large to accomodate anything we decide
|
|
|
+ * in the future. */
|
|
|
+ return networkstatus_get_param(NULL, "hs_intro_max_lifetime",
|
|
|
+ INTRO_POINT_LIFETIME_MAX_SECONDS,
|
|
|
+ 0, INT32_MAX);
|
|
|
+}
|
|
|
+
|
|
|
+/* Return the number of extra introduction point defined by a consensus
|
|
|
+ * parameter or the default value. */
|
|
|
+static int32_t
|
|
|
+get_intro_point_num_extra(void)
|
|
|
+{
|
|
|
+ /* The [0, 128] range bounds the number of extra introduction point allowed.
|
|
|
+ * Above 128 intro points, it's getting a bit crazy. */
|
|
|
+ return networkstatus_get_param(NULL, "hs_intro_num_extra",
|
|
|
+ NUM_INTRO_POINTS_EXTRA, 0, 128);
|
|
|
+}
|
|
|
+
|
|
|
/* Helper: Function that needs to return 1 for the HT for each loop which
|
|
|
* frees every service in an hash map. */
|
|
|
static int
|
|
@@ -201,13 +311,357 @@ service_free_all(void)
|
|
|
}
|
|
|
}
|
|
|
|
|
|
+/* Free a given service intro point object. */
|
|
|
+STATIC void
|
|
|
+service_intro_point_free(hs_service_intro_point_t *ip)
|
|
|
+{
|
|
|
+ if (!ip) {
|
|
|
+ return;
|
|
|
+ }
|
|
|
+ memwipe(&ip->auth_key_kp, 0, sizeof(ip->auth_key_kp));
|
|
|
+ memwipe(&ip->enc_key_kp, 0, sizeof(ip->enc_key_kp));
|
|
|
+ crypto_pk_free(ip->legacy_key);
|
|
|
+ replaycache_free(ip->replay_cache);
|
|
|
+ hs_intropoint_clear(&ip->base);
|
|
|
+ tor_free(ip);
|
|
|
+}
|
|
|
+
|
|
|
+/* Helper: free an hs_service_intro_point_t object. This function is used by
|
|
|
+ * digest256map_free() which requires a void * pointer. */
|
|
|
+static void
|
|
|
+service_intro_point_free_(void *obj)
|
|
|
+{
|
|
|
+ service_intro_point_free(obj);
|
|
|
+}
|
|
|
+
|
|
|
+/* Return a newly allocated service intro point and fully initialized from the
|
|
|
+ * given extend_info_t ei if non NULL. If is_legacy is true, we also generate
|
|
|
+ * the legacy key. On error, NULL is returned. */
|
|
|
+STATIC hs_service_intro_point_t *
|
|
|
+service_intro_point_new(const extend_info_t *ei, unsigned int is_legacy)
|
|
|
+{
|
|
|
+ hs_desc_link_specifier_t *ls;
|
|
|
+ hs_service_intro_point_t *ip;
|
|
|
+
|
|
|
+ ip = tor_malloc_zero(sizeof(*ip));
|
|
|
+ /* We'll create the key material. No need for extra strong, those are short
|
|
|
+ * term keys. */
|
|
|
+ ed25519_keypair_generate(&ip->auth_key_kp, 0);
|
|
|
+
|
|
|
+ { /* Set introduce2 max cells limit */
|
|
|
+ int32_t min_introduce2_cells = get_intro_point_min_introduce2();
|
|
|
+ int32_t max_introduce2_cells = get_intro_point_max_introduce2();
|
|
|
+ if (BUG(max_introduce2_cells < min_introduce2_cells)) {
|
|
|
+ goto err;
|
|
|
+ }
|
|
|
+ ip->introduce2_max = crypto_rand_int_range(min_introduce2_cells,
|
|
|
+ max_introduce2_cells);
|
|
|
+ }
|
|
|
+ { /* Set intro point lifetime */
|
|
|
+ int32_t intro_point_min_lifetime = get_intro_point_min_lifetime();
|
|
|
+ int32_t intro_point_max_lifetime = get_intro_point_max_lifetime();
|
|
|
+ if (BUG(intro_point_max_lifetime < intro_point_min_lifetime)) {
|
|
|
+ goto err;
|
|
|
+ }
|
|
|
+ ip->time_to_expire = time(NULL) +
|
|
|
+ crypto_rand_int_range(intro_point_min_lifetime,intro_point_max_lifetime);
|
|
|
+ }
|
|
|
+
|
|
|
+ ip->replay_cache = replaycache_new(0, 0);
|
|
|
+
|
|
|
+ /* Initialize the base object. We don't need the certificate object. */
|
|
|
+ ip->base.link_specifiers = smartlist_new();
|
|
|
+
|
|
|
+ /* Generate the encryption key for this intro point. */
|
|
|
+ curve25519_keypair_generate(&ip->enc_key_kp, 0);
|
|
|
+ /* Figure out if this chosen node supports v3 or is legacy only. */
|
|
|
+ if (is_legacy) {
|
|
|
+ ip->base.is_only_legacy = 1;
|
|
|
+ /* Legacy mode that is doesn't support v3+ with ed25519 auth key. */
|
|
|
+ ip->legacy_key = crypto_pk_new();
|
|
|
+ if (crypto_pk_generate_key(ip->legacy_key) < 0) {
|
|
|
+ goto err;
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+ if (ei == NULL) {
|
|
|
+ goto done;
|
|
|
+ }
|
|
|
+
|
|
|
+ /* We'll try to add all link specifier. Legacy, IPv4 and ed25519 are
|
|
|
+ * mandatory. */
|
|
|
+ ls = hs_desc_link_specifier_new(ei, LS_IPV4);
|
|
|
+ /* It is impossible to have an extend info object without a v4. */
|
|
|
+ if (BUG(!ls)) {
|
|
|
+ goto err;
|
|
|
+ }
|
|
|
+ smartlist_add(ip->base.link_specifiers, ls);
|
|
|
+
|
|
|
+ ls = hs_desc_link_specifier_new(ei, LS_LEGACY_ID);
|
|
|
+ /* It is impossible to have an extend info object without an identity
|
|
|
+ * digest. */
|
|
|
+ if (BUG(!ls)) {
|
|
|
+ goto err;
|
|
|
+ }
|
|
|
+ smartlist_add(ip->base.link_specifiers, ls);
|
|
|
+
|
|
|
+ /* ed25519 identity key is optional for intro points */
|
|
|
+ ls = hs_desc_link_specifier_new(ei, LS_ED25519_ID);
|
|
|
+ if (ls) {
|
|
|
+ smartlist_add(ip->base.link_specifiers, ls);
|
|
|
+ }
|
|
|
+
|
|
|
+ /* IPv6 is optional. */
|
|
|
+ ls = hs_desc_link_specifier_new(ei, LS_IPV6);
|
|
|
+ if (ls) {
|
|
|
+ smartlist_add(ip->base.link_specifiers, ls);
|
|
|
+ }
|
|
|
+
|
|
|
+ /* Finally, copy onion key from the extend_info_t object. */
|
|
|
+ memcpy(&ip->onion_key, &ei->curve25519_onion_key, sizeof(ip->onion_key));
|
|
|
+
|
|
|
+ done:
|
|
|
+ return ip;
|
|
|
+ err:
|
|
|
+ service_intro_point_free(ip);
|
|
|
+ return NULL;
|
|
|
+}
|
|
|
+
|
|
|
+/* Add the given intro point object to the given intro point map. The intro
|
|
|
+ * point MUST have its RSA encryption key set if this is a legacy type or the
|
|
|
+ * authentication key set otherwise. */
|
|
|
+STATIC void
|
|
|
+service_intro_point_add(digest256map_t *map, hs_service_intro_point_t *ip)
|
|
|
+{
|
|
|
+ hs_service_intro_point_t *old_ip_entry;
|
|
|
+
|
|
|
+ tor_assert(map);
|
|
|
+ tor_assert(ip);
|
|
|
+
|
|
|
+ old_ip_entry = digest256map_set(map, ip->auth_key_kp.pubkey.pubkey, ip);
|
|
|
+ /* Make sure we didn't just try to double-add an intro point */
|
|
|
+ tor_assert_nonfatal(!old_ip_entry);
|
|
|
+}
|
|
|
+
|
|
|
+/* For a given service, remove the intro point from that service's descriptors
|
|
|
+ * (check both current and next descriptor) */
|
|
|
+STATIC void
|
|
|
+service_intro_point_remove(const hs_service_t *service,
|
|
|
+ const hs_service_intro_point_t *ip)
|
|
|
+{
|
|
|
+ tor_assert(service);
|
|
|
+ tor_assert(ip);
|
|
|
+
|
|
|
+ /* Trying all descriptors. */
|
|
|
+ FOR_EACH_DESCRIPTOR_BEGIN(service, desc) {
|
|
|
+ /* We'll try to remove the descriptor on both descriptors which is not
|
|
|
+ * very expensive to do instead of doing loopup + remove. */
|
|
|
+ digest256map_remove(desc->intro_points.map,
|
|
|
+ ip->auth_key_kp.pubkey.pubkey);
|
|
|
+ } FOR_EACH_DESCRIPTOR_END;
|
|
|
+}
|
|
|
+
|
|
|
+/* For a given service and authentication key, return the intro point or NULL
|
|
|
+ * if not found. This will check both descriptors in the service. */
|
|
|
+STATIC hs_service_intro_point_t *
|
|
|
+service_intro_point_find(const hs_service_t *service,
|
|
|
+ const ed25519_public_key_t *auth_key)
|
|
|
+{
|
|
|
+ hs_service_intro_point_t *ip = NULL;
|
|
|
+
|
|
|
+ tor_assert(service);
|
|
|
+ tor_assert(auth_key);
|
|
|
+
|
|
|
+ /* Trying all descriptors to find the right intro point.
|
|
|
+ *
|
|
|
+ * Even if we use the same node as intro point in both descriptors, the node
|
|
|
+ * will have a different intro auth key for each descriptor since we generate
|
|
|
+ * a new one everytime we pick an intro point.
|
|
|
+ *
|
|
|
+ * After #22893 gets implemented, intro points will be moved to be
|
|
|
+ * per-service instead of per-descriptor so this function will need to
|
|
|
+ * change.
|
|
|
+ */
|
|
|
+ FOR_EACH_DESCRIPTOR_BEGIN(service, desc) {
|
|
|
+ if ((ip = digest256map_get(desc->intro_points.map,
|
|
|
+ auth_key->pubkey)) != NULL) {
|
|
|
+ break;
|
|
|
+ }
|
|
|
+ } FOR_EACH_DESCRIPTOR_END;
|
|
|
+
|
|
|
+ return ip;
|
|
|
+}
|
|
|
+
|
|
|
+/* For a given service and intro point, return the descriptor for which the
|
|
|
+ * intro point is assigned to. NULL is returned if not found. */
|
|
|
+STATIC hs_service_descriptor_t *
|
|
|
+service_desc_find_by_intro(const hs_service_t *service,
|
|
|
+ const hs_service_intro_point_t *ip)
|
|
|
+{
|
|
|
+ hs_service_descriptor_t *descp = NULL;
|
|
|
+
|
|
|
+ tor_assert(service);
|
|
|
+ tor_assert(ip);
|
|
|
+
|
|
|
+ FOR_EACH_DESCRIPTOR_BEGIN(service, desc) {
|
|
|
+ if (digest256map_get(desc->intro_points.map,
|
|
|
+ ip->auth_key_kp.pubkey.pubkey)) {
|
|
|
+ descp = desc;
|
|
|
+ break;
|
|
|
+ }
|
|
|
+ } FOR_EACH_DESCRIPTOR_END;
|
|
|
+
|
|
|
+ return descp;
|
|
|
+}
|
|
|
+
|
|
|
+/* From a circuit identifier, get all the possible objects associated with the
|
|
|
+ * ident. If not NULL, service, ip or desc are set if the object can be found.
|
|
|
+ * They are untouched if they can't be found.
|
|
|
+ *
|
|
|
+ * This is an helper function because we do those lookups often so it's more
|
|
|
+ * convenient to simply call this functions to get all the things at once. */
|
|
|
+STATIC void
|
|
|
+get_objects_from_ident(const hs_ident_circuit_t *ident,
|
|
|
+ hs_service_t **service, hs_service_intro_point_t **ip,
|
|
|
+ hs_service_descriptor_t **desc)
|
|
|
+{
|
|
|
+ hs_service_t *s;
|
|
|
+
|
|
|
+ tor_assert(ident);
|
|
|
+
|
|
|
+ /* Get service object from the circuit identifier. */
|
|
|
+ s = find_service(hs_service_map, &ident->identity_pk);
|
|
|
+ if (s && service) {
|
|
|
+ *service = s;
|
|
|
+ }
|
|
|
+
|
|
|
+ /* From the service object, get the intro point object of that circuit. The
|
|
|
+ * following will query both descriptors intro points list. */
|
|
|
+ if (s && ip) {
|
|
|
+ *ip = service_intro_point_find(s, &ident->intro_auth_pk);
|
|
|
+ }
|
|
|
+
|
|
|
+ /* Get the descriptor for this introduction point and service. */
|
|
|
+ if (s && ip && *ip && desc) {
|
|
|
+ *desc = service_desc_find_by_intro(s, *ip);
|
|
|
+ }
|
|
|
+}
|
|
|
+
|
|
|
+/* From a given intro point, return the first link specifier of type
|
|
|
+ * encountered in the link specifier list. Return NULL if it can't be found.
|
|
|
+ *
|
|
|
+ * The caller does NOT have ownership of the object, the intro point does. */
|
|
|
+static hs_desc_link_specifier_t *
|
|
|
+get_link_spec_by_type(const hs_service_intro_point_t *ip, uint8_t type)
|
|
|
+{
|
|
|
+ hs_desc_link_specifier_t *lnk_spec = NULL;
|
|
|
+
|
|
|
+ tor_assert(ip);
|
|
|
+
|
|
|
+ SMARTLIST_FOREACH_BEGIN(ip->base.link_specifiers,
|
|
|
+ hs_desc_link_specifier_t *, ls) {
|
|
|
+ if (ls->type == type) {
|
|
|
+ lnk_spec = ls;
|
|
|
+ goto end;
|
|
|
+ }
|
|
|
+ } SMARTLIST_FOREACH_END(ls);
|
|
|
+
|
|
|
+ end:
|
|
|
+ return lnk_spec;
|
|
|
+}
|
|
|
+
|
|
|
+/* Given a service intro point, return the node_t associated to it. This can
|
|
|
+ * return NULL if the given intro point has no legacy ID or if the node can't
|
|
|
+ * be found in the consensus. */
|
|
|
+STATIC const node_t *
|
|
|
+get_node_from_intro_point(const hs_service_intro_point_t *ip)
|
|
|
+{
|
|
|
+ const hs_desc_link_specifier_t *ls;
|
|
|
+
|
|
|
+ tor_assert(ip);
|
|
|
+
|
|
|
+ ls = get_link_spec_by_type(ip, LS_LEGACY_ID);
|
|
|
+ if (BUG(!ls)) {
|
|
|
+ return NULL;
|
|
|
+ }
|
|
|
+ /* XXX In the future, we want to only use the ed25519 ID (#22173). */
|
|
|
+ return node_get_by_id((const char *) ls->u.legacy_id);
|
|
|
+}
|
|
|
+
|
|
|
+/* Given a service intro point, return the extend_info_t for it. This can
|
|
|
+ * return NULL if the node can't be found for the intro point or the extend
|
|
|
+ * info can't be created for the found node. If direct_conn is set, the extend
|
|
|
+ * info is validated on if we can connect directly. */
|
|
|
+static extend_info_t *
|
|
|
+get_extend_info_from_intro_point(const hs_service_intro_point_t *ip,
|
|
|
+ unsigned int direct_conn)
|
|
|
+{
|
|
|
+ extend_info_t *info = NULL;
|
|
|
+ const node_t *node;
|
|
|
+
|
|
|
+ tor_assert(ip);
|
|
|
+
|
|
|
+ node = get_node_from_intro_point(ip);
|
|
|
+ if (node == NULL) {
|
|
|
+ /* This can happen if the relay serving as intro point has been removed
|
|
|
+ * from the consensus. In that case, the intro point will be removed from
|
|
|
+ * the descriptor during the scheduled events. */
|
|
|
+ goto end;
|
|
|
+ }
|
|
|
+
|
|
|
+ /* In the case of a direct connection (single onion service), it is possible
|
|
|
+ * our firewall policy won't allow it so this can return a NULL value. */
|
|
|
+ info = extend_info_from_node(node, direct_conn);
|
|
|
+
|
|
|
+ end:
|
|
|
+ return info;
|
|
|
+}
|
|
|
+
|
|
|
+/* Return the number of introduction points that are established for the
|
|
|
+ * given descriptor. */
|
|
|
+static unsigned int
|
|
|
+count_desc_circuit_established(const hs_service_descriptor_t *desc)
|
|
|
+{
|
|
|
+ unsigned int count = 0;
|
|
|
+
|
|
|
+ tor_assert(desc);
|
|
|
+
|
|
|
+ DIGEST256MAP_FOREACH(desc->intro_points.map, key,
|
|
|
+ const hs_service_intro_point_t *, ip) {
|
|
|
+ count += ip->circuit_established;
|
|
|
+ } DIGEST256MAP_FOREACH_END;
|
|
|
+
|
|
|
+ return count;
|
|
|
+}
|
|
|
+
|
|
|
/* Close all rendezvous circuits for the given service. */
|
|
|
static void
|
|
|
close_service_rp_circuits(hs_service_t *service)
|
|
|
{
|
|
|
+ origin_circuit_t *ocirc = NULL;
|
|
|
+
|
|
|
tor_assert(service);
|
|
|
- /* XXX: To implement. */
|
|
|
- return;
|
|
|
+
|
|
|
+ /* The reason we go over all circuit instead of using the circuitmap API is
|
|
|
+ * because most hidden service circuits are rendezvous circuits so there is
|
|
|
+ * no real improvement at getting all rendezvous circuits from the
|
|
|
+ * circuitmap and then going over them all to find the right ones.
|
|
|
+ * Furthermore, another option would have been to keep a list of RP cookies
|
|
|
+ * for a service but it creates an engineering complexity since we don't
|
|
|
+ * have a "RP circuit closed" event to clean it up properly so we avoid a
|
|
|
+ * memory DoS possibility. */
|
|
|
+
|
|
|
+ while ((ocirc = circuit_get_next_service_rp_circ(ocirc))) {
|
|
|
+ /* Only close circuits that are v3 and for this service. */
|
|
|
+ if (ocirc->hs_ident != NULL &&
|
|
|
+ ed25519_pubkey_eq(ô->hs_ident->identity_pk,
|
|
|
+ &service->keys.identity_pk)) {
|
|
|
+ /* Reason is FINISHED because service has been removed and thus the
|
|
|
+ * circuit is considered old/uneeded. When freed, it is removed from the
|
|
|
+ * hs circuitmap. */
|
|
|
+ circuit_mark_for_close(TO_CIRCUIT(ocirc), END_CIRC_REASON_FINISHED);
|
|
|
+ }
|
|
|
+ }
|
|
|
}
|
|
|
|
|
|
/* Close the circuit(s) for the given map of introduction points. */
|
|
@@ -218,13 +672,11 @@ close_intro_circuits(hs_service_intropoints_t *intro_points)
|
|
|
|
|
|
DIGEST256MAP_FOREACH(intro_points->map, key,
|
|
|
const hs_service_intro_point_t *, ip) {
|
|
|
- origin_circuit_t *ocirc =
|
|
|
- hs_circuitmap_get_intro_circ_v3_service_side(
|
|
|
- &ip->auth_key_kp.pubkey);
|
|
|
+ origin_circuit_t *ocirc = hs_circ_service_get_intro_circ(ip);
|
|
|
if (ocirc) {
|
|
|
- hs_circuitmap_remove_circuit(TO_CIRCUIT(ocirc));
|
|
|
/* Reason is FINISHED because service has been removed and thus the
|
|
|
- * circuit is considered old/uneeded. */
|
|
|
+ * circuit is considered old/uneeded. When freed, the circuit is removed
|
|
|
+ * from the HS circuitmap. */
|
|
|
circuit_mark_for_close(TO_CIRCUIT(ocirc), END_CIRC_REASON_FINISHED);
|
|
|
}
|
|
|
} DIGEST256MAP_FOREACH_END;
|
|
@@ -236,12 +688,9 @@ close_service_intro_circuits(hs_service_t *service)
|
|
|
{
|
|
|
tor_assert(service);
|
|
|
|
|
|
- if (service->desc_current) {
|
|
|
- close_intro_circuits(&service->desc_current->intro_points);
|
|
|
- }
|
|
|
- if (service->desc_next) {
|
|
|
- close_intro_circuits(&service->desc_next->intro_points);
|
|
|
- }
|
|
|
+ FOR_EACH_DESCRIPTOR_BEGIN(service, desc) {
|
|
|
+ close_intro_circuits(&desc->intro_points);
|
|
|
+ } FOR_EACH_DESCRIPTOR_END;
|
|
|
}
|
|
|
|
|
|
/* Close any circuits related to the given service. */
|
|
@@ -269,7 +718,7 @@ move_descriptor_intro_points(hs_service_descriptor_t *src,
|
|
|
tor_assert(src);
|
|
|
tor_assert(dst);
|
|
|
|
|
|
- /* XXX: Free dst introduction points. */
|
|
|
+ digest256map_free(dst->intro_points.map, service_intro_point_free_);
|
|
|
dst->intro_points.map = src->intro_points.map;
|
|
|
/* Nullify the source. */
|
|
|
src->intro_points.map = NULL;
|
|
@@ -283,7 +732,6 @@ move_intro_points(hs_service_t *src, hs_service_t *dst)
|
|
|
tor_assert(src);
|
|
|
tor_assert(dst);
|
|
|
|
|
|
- /* Cleanup destination. */
|
|
|
if (src->desc_current && dst->desc_current) {
|
|
|
move_descriptor_intro_points(src->desc_current, dst->desc_current);
|
|
|
}
|
|
@@ -339,7 +787,6 @@ static void
|
|
|
register_all_services(void)
|
|
|
{
|
|
|
struct hs_service_ht *new_service_map;
|
|
|
- hs_service_t *s, **iter;
|
|
|
|
|
|
tor_assert(hs_service_staging_list);
|
|
|
|
|
@@ -359,6 +806,8 @@ register_all_services(void)
|
|
|
move_ephemeral_services(hs_service_map, new_service_map);
|
|
|
|
|
|
SMARTLIST_FOREACH_BEGIN(hs_service_staging_list, hs_service_t *, snew) {
|
|
|
+ hs_service_t *s;
|
|
|
+
|
|
|
/* Check if that service is already in our global map and if so, we'll
|
|
|
* transfer the intro points to it. */
|
|
|
s = find_service(hs_service_map, &snew->keys.identity_pk);
|
|
@@ -386,9 +835,9 @@ register_all_services(void)
|
|
|
|
|
|
/* Close any circuits associated with the non surviving services. Every
|
|
|
* service in the current global map are roaming. */
|
|
|
- HT_FOREACH(iter, hs_service_ht, hs_service_map) {
|
|
|
- close_service_circuits(*iter);
|
|
|
- }
|
|
|
+ FOR_EACH_SERVICE_BEGIN(service) {
|
|
|
+ close_service_circuits(service);
|
|
|
+ } FOR_EACH_SERVICE_END;
|
|
|
|
|
|
/* Time to make the switch. We'll clear the staging list because its content
|
|
|
* has now changed ownership to the map. */
|
|
@@ -399,25 +848,22 @@ register_all_services(void)
|
|
|
|
|
|
/* Write the onion address of a given service to the given filename fname_ in
|
|
|
* the service directory. Return 0 on success else -1 on error. */
|
|
|
-static int
|
|
|
+STATIC int
|
|
|
write_address_to_file(const hs_service_t *service, const char *fname_)
|
|
|
{
|
|
|
int ret = -1;
|
|
|
char *fname = NULL;
|
|
|
- /* Length of an address plus the sizeof the address tld (onion) which counts
|
|
|
- * the NUL terminated byte so we keep it for the "." and the newline. */
|
|
|
- char buf[HS_SERVICE_ADDR_LEN_BASE32 + sizeof(address_tld) + 1];
|
|
|
+ char *addr_buf = NULL;
|
|
|
|
|
|
tor_assert(service);
|
|
|
tor_assert(fname_);
|
|
|
|
|
|
/* Construct the full address with the onion tld and write the hostname file
|
|
|
* to disk. */
|
|
|
- tor_snprintf(buf, sizeof(buf), "%s.%s\n", service->onion_address,
|
|
|
- address_tld);
|
|
|
+ tor_asprintf(&addr_buf, "%s.%s\n", service->onion_address, address_tld);
|
|
|
/* Notice here that we use the given "fname_". */
|
|
|
fname = hs_path_from_filename(service->config.directory_path, fname_);
|
|
|
- if (write_str_to_file(fname, buf, 0) < 0) {
|
|
|
+ if (write_str_to_file(fname, addr_buf, 0) < 0) {
|
|
|
log_warn(LD_REND, "Could not write onion address to hostname file %s",
|
|
|
escaped(fname));
|
|
|
goto end;
|
|
@@ -437,6 +883,7 @@ write_address_to_file(const hs_service_t *service, const char *fname_)
|
|
|
ret = 0;
|
|
|
end:
|
|
|
tor_free(fname);
|
|
|
+ tor_free(addr_buf);
|
|
|
return ret;
|
|
|
}
|
|
|
|
|
@@ -510,291 +957,2099 @@ load_service_keys(hs_service_t *service)
|
|
|
return ret;
|
|
|
}
|
|
|
|
|
|
-/* Load and/or generate keys for all onion services including the client
|
|
|
- * authorization if any. Return 0 on success, -1 on failure. */
|
|
|
-int
|
|
|
-hs_service_load_all_keys(void)
|
|
|
+/* Free a given service descriptor object and all key material is wiped. */
|
|
|
+STATIC void
|
|
|
+service_descriptor_free(hs_service_descriptor_t *desc)
|
|
|
{
|
|
|
- /* Load v2 service keys if we have v2. */
|
|
|
- if (num_rend_services() != 0) {
|
|
|
- if (rend_service_load_all_keys(NULL) < 0) {
|
|
|
- goto err;
|
|
|
- }
|
|
|
+ if (!desc) {
|
|
|
+ return;
|
|
|
}
|
|
|
+ hs_descriptor_free(desc->desc);
|
|
|
+ memwipe(&desc->signing_kp, 0, sizeof(desc->signing_kp));
|
|
|
+ memwipe(&desc->blinded_kp, 0, sizeof(desc->blinded_kp));
|
|
|
+ SMARTLIST_FOREACH(desc->hsdir_missing_info, char *, id, tor_free(id));
|
|
|
+ smartlist_free(desc->hsdir_missing_info);
|
|
|
+ /* Cleanup all intro points. */
|
|
|
+ digest256map_free(desc->intro_points.map, service_intro_point_free_);
|
|
|
+ digestmap_free(desc->intro_points.failed_id, tor_free_);
|
|
|
+ tor_free(desc);
|
|
|
+}
|
|
|
|
|
|
- /* Load or/and generate them for v3+. */
|
|
|
- SMARTLIST_FOREACH_BEGIN(hs_service_staging_list, hs_service_t *, service) {
|
|
|
- /* Ignore ephemeral service, they already have their keys set. */
|
|
|
- if (service->config.is_ephemeral) {
|
|
|
- continue;
|
|
|
- }
|
|
|
- log_info(LD_REND, "Loading v3 onion service keys from %s",
|
|
|
- service_escaped_dir(service));
|
|
|
- if (load_service_keys(service) < 0) {
|
|
|
- goto err;
|
|
|
- }
|
|
|
- /* XXX: Load/Generate client authorization keys. (#20700) */
|
|
|
- } SMARTLIST_FOREACH_END(service);
|
|
|
+/* Return a newly allocated service descriptor object. */
|
|
|
+STATIC hs_service_descriptor_t *
|
|
|
+service_descriptor_new(void)
|
|
|
+{
|
|
|
+ hs_service_descriptor_t *sdesc = tor_malloc_zero(sizeof(*sdesc));
|
|
|
+ sdesc->desc = tor_malloc_zero(sizeof(hs_descriptor_t));
|
|
|
+ /* Initialize the intro points map. */
|
|
|
+ sdesc->intro_points.map = digest256map_new();
|
|
|
+ sdesc->intro_points.failed_id = digestmap_new();
|
|
|
+ sdesc->hsdir_missing_info = smartlist_new();
|
|
|
+ return sdesc;
|
|
|
+}
|
|
|
|
|
|
- /* Final step, the staging list contains service in a quiescent state that
|
|
|
- * is ready to be used. Register them to the global map. Once this is over,
|
|
|
- * the staging list will be cleaned up. */
|
|
|
- register_all_services();
|
|
|
+/* From the given service, remove all expired failing intro points for each
|
|
|
+ * descriptor. */
|
|
|
+static void
|
|
|
+remove_expired_failing_intro(hs_service_t *service, time_t now)
|
|
|
+{
|
|
|
+ tor_assert(service);
|
|
|
|
|
|
- /* All keys have been loaded successfully. */
|
|
|
- return 0;
|
|
|
- err:
|
|
|
- return -1;
|
|
|
+ /* For both descriptors, cleanup the failing intro points list. */
|
|
|
+ FOR_EACH_DESCRIPTOR_BEGIN(service, desc) {
|
|
|
+ DIGESTMAP_FOREACH_MODIFY(desc->intro_points.failed_id, key, time_t *, t) {
|
|
|
+ time_t failure_time = *t;
|
|
|
+ if ((failure_time + INTRO_CIRC_RETRY_PERIOD) <= now) {
|
|
|
+ MAP_DEL_CURRENT(key);
|
|
|
+ tor_free(t);
|
|
|
+ }
|
|
|
+ } DIGESTMAP_FOREACH_END;
|
|
|
+ } FOR_EACH_DESCRIPTOR_END;
|
|
|
}
|
|
|
|
|
|
-/* Put all service object in the given service list. After this, the caller
|
|
|
- * looses ownership of every elements in the list and responsible to free the
|
|
|
- * list pointer. */
|
|
|
-void
|
|
|
-hs_service_stage_services(const smartlist_t *service_list)
|
|
|
+/* For the given descriptor desc, put all node_t object found from its failing
|
|
|
+ * intro point list and put them in the given node_list. */
|
|
|
+static void
|
|
|
+setup_intro_point_exclude_list(const hs_service_descriptor_t *desc,
|
|
|
+ smartlist_t *node_list)
|
|
|
{
|
|
|
- tor_assert(service_list);
|
|
|
- /* This list is freed at registration time but this function can be called
|
|
|
- * multiple time. */
|
|
|
- if (hs_service_staging_list == NULL) {
|
|
|
- hs_service_staging_list = smartlist_new();
|
|
|
- }
|
|
|
- /* Add all service object to our staging list. Caller is responsible for
|
|
|
- * freeing the service_list. */
|
|
|
- smartlist_add_all(hs_service_staging_list, service_list);
|
|
|
+ tor_assert(desc);
|
|
|
+ tor_assert(node_list);
|
|
|
+
|
|
|
+ DIGESTMAP_FOREACH(desc->intro_points.failed_id, key, time_t *, t) {
|
|
|
+ (void) t; /* Make gcc happy. */
|
|
|
+ const node_t *node = node_get_by_id(key);
|
|
|
+ if (node) {
|
|
|
+ smartlist_add(node_list, (void *) node);
|
|
|
+ }
|
|
|
+ } DIGESTMAP_FOREACH_END;
|
|
|
}
|
|
|
|
|
|
-/* Allocate and initilize a service object. The service configuration will
|
|
|
- * contain the default values. Return the newly allocated object pointer. This
|
|
|
- * function can't fail. */
|
|
|
-hs_service_t *
|
|
|
-hs_service_new(const or_options_t *options)
|
|
|
+/* For the given failing intro point ip, we add its time of failure to the
|
|
|
+ * failed map and index it by identity digest (legacy ID) in the descriptor
|
|
|
+ * desc failed id map. */
|
|
|
+static void
|
|
|
+remember_failing_intro_point(const hs_service_intro_point_t *ip,
|
|
|
+ hs_service_descriptor_t *desc, time_t now)
|
|
|
{
|
|
|
- hs_service_t *service = tor_malloc_zero(sizeof(hs_service_t));
|
|
|
- /* Set default configuration value. */
|
|
|
- set_service_default_config(&service->config, options);
|
|
|
- /* Set the default service version. */
|
|
|
- service->config.version = HS_SERVICE_DEFAULT_VERSION;
|
|
|
- return service;
|
|
|
+ time_t *time_of_failure, *prev_ptr;
|
|
|
+ const hs_desc_link_specifier_t *legacy_ls;
|
|
|
+
|
|
|
+ tor_assert(ip);
|
|
|
+ tor_assert(desc);
|
|
|
+
|
|
|
+ time_of_failure = tor_malloc_zero(sizeof(time_t));
|
|
|
+ *time_of_failure = now;
|
|
|
+ legacy_ls = get_link_spec_by_type(ip, LS_LEGACY_ID);
|
|
|
+ tor_assert(legacy_ls);
|
|
|
+ prev_ptr = digestmap_set(desc->intro_points.failed_id,
|
|
|
+ (const char *) legacy_ls->u.legacy_id,
|
|
|
+ time_of_failure);
|
|
|
+ tor_free(prev_ptr);
|
|
|
}
|
|
|
|
|
|
-/* Free the given <b>service</b> object and all its content. This function
|
|
|
- * also takes care of wiping service keys from memory. It is safe to pass a
|
|
|
- * NULL pointer. */
|
|
|
-void
|
|
|
-hs_service_free(hs_service_t *service)
|
|
|
+/* Copy the descriptor link specifier object from src to dst. */
|
|
|
+static void
|
|
|
+link_specifier_copy(hs_desc_link_specifier_t *dst,
|
|
|
+ const hs_desc_link_specifier_t *src)
|
|
|
{
|
|
|
- if (service == NULL) {
|
|
|
- return;
|
|
|
- }
|
|
|
+ tor_assert(dst);
|
|
|
+ tor_assert(src);
|
|
|
+ memcpy(dst, src, sizeof(hs_desc_link_specifier_t));
|
|
|
+}
|
|
|
|
|
|
- /* Free descriptors. */
|
|
|
- if (service->desc_current) {
|
|
|
- hs_descriptor_free(service->desc_current->desc);
|
|
|
- /* Wipe keys. */
|
|
|
- memwipe(&service->desc_current->signing_kp, 0,
|
|
|
- sizeof(service->desc_current->signing_kp));
|
|
|
- memwipe(&service->desc_current->blinded_kp, 0,
|
|
|
- sizeof(service->desc_current->blinded_kp));
|
|
|
- /* XXX: Free intro points. */
|
|
|
- tor_free(service->desc_current);
|
|
|
- }
|
|
|
- if (service->desc_next) {
|
|
|
- hs_descriptor_free(service->desc_next->desc);
|
|
|
- /* Wipe keys. */
|
|
|
- memwipe(&service->desc_next->signing_kp, 0,
|
|
|
- sizeof(service->desc_next->signing_kp));
|
|
|
- memwipe(&service->desc_next->blinded_kp, 0,
|
|
|
- sizeof(service->desc_next->blinded_kp));
|
|
|
- /* XXX: Free intro points. */
|
|
|
- tor_free(service->desc_next);
|
|
|
+/* Using a given descriptor signing keypair signing_kp, a service intro point
|
|
|
+ * object ip and the time now, setup the content of an already allocated
|
|
|
+ * descriptor intro desc_ip.
|
|
|
+ *
|
|
|
+ * Return 0 on success else a negative value. */
|
|
|
+static int
|
|
|
+setup_desc_intro_point(const ed25519_keypair_t *signing_kp,
|
|
|
+ const hs_service_intro_point_t *ip,
|
|
|
+ time_t now, hs_desc_intro_point_t *desc_ip)
|
|
|
+{
|
|
|
+ int ret = -1;
|
|
|
+ time_t nearest_hour = now - (now % 3600);
|
|
|
+
|
|
|
+ tor_assert(signing_kp);
|
|
|
+ tor_assert(ip);
|
|
|
+ tor_assert(desc_ip);
|
|
|
+
|
|
|
+ /* Copy the onion key. */
|
|
|
+ memcpy(&desc_ip->onion_key, &ip->onion_key, sizeof(desc_ip->onion_key));
|
|
|
+
|
|
|
+ /* Key and certificate material. */
|
|
|
+ desc_ip->auth_key_cert = tor_cert_create(signing_kp,
|
|
|
+ CERT_TYPE_AUTH_HS_IP_KEY,
|
|
|
+ &ip->auth_key_kp.pubkey,
|
|
|
+ nearest_hour,
|
|
|
+ HS_DESC_CERT_LIFETIME,
|
|
|
+ CERT_FLAG_INCLUDE_SIGNING_KEY);
|
|
|
+ if (desc_ip->auth_key_cert == NULL) {
|
|
|
+ log_warn(LD_REND, "Unable to create intro point auth-key certificate");
|
|
|
+ goto done;
|
|
|
}
|
|
|
|
|
|
- /* Free service configuration. */
|
|
|
- service_clear_config(&service->config);
|
|
|
+ /* Copy link specifier(s). */
|
|
|
+ SMARTLIST_FOREACH_BEGIN(ip->base.link_specifiers,
|
|
|
+ const hs_desc_link_specifier_t *, ls) {
|
|
|
+ hs_desc_link_specifier_t *dup = tor_malloc_zero(sizeof(*dup));
|
|
|
+ link_specifier_copy(dup, ls);
|
|
|
+ smartlist_add(desc_ip->link_specifiers, dup);
|
|
|
+ } SMARTLIST_FOREACH_END(ls);
|
|
|
+
|
|
|
+ /* For a legacy intro point, we'll use an RSA/ed cross certificate. */
|
|
|
+ if (ip->base.is_only_legacy) {
|
|
|
+ desc_ip->legacy.key = crypto_pk_dup_key(ip->legacy_key);
|
|
|
+ /* Create cross certification cert. */
|
|
|
+ ssize_t cert_len = tor_make_rsa_ed25519_crosscert(
|
|
|
+ &signing_kp->pubkey,
|
|
|
+ desc_ip->legacy.key,
|
|
|
+ nearest_hour + HS_DESC_CERT_LIFETIME,
|
|
|
+ &desc_ip->legacy.cert.encoded);
|
|
|
+ if (cert_len < 0) {
|
|
|
+ log_warn(LD_REND, "Unable to create enc key legacy cross cert.");
|
|
|
+ goto done;
|
|
|
+ }
|
|
|
+ desc_ip->legacy.cert.len = cert_len;
|
|
|
+ }
|
|
|
|
|
|
- /* Wipe service keys. */
|
|
|
- memwipe(&service->keys.identity_sk, 0, sizeof(service->keys.identity_sk));
|
|
|
+ /* Encryption key and its cross certificate. */
|
|
|
+ {
|
|
|
+ ed25519_public_key_t ed25519_pubkey;
|
|
|
+
|
|
|
+ /* Use the public curve25519 key. */
|
|
|
+ memcpy(&desc_ip->enc_key, &ip->enc_key_kp.pubkey,
|
|
|
+ sizeof(desc_ip->enc_key));
|
|
|
+ /* The following can't fail. */
|
|
|
+ ed25519_public_key_from_curve25519_public_key(&ed25519_pubkey,
|
|
|
+ &ip->enc_key_kp.pubkey,
|
|
|
+ 0);
|
|
|
+ desc_ip->enc_key_cert = tor_cert_create(signing_kp,
|
|
|
+ CERT_TYPE_CROSS_HS_IP_KEYS,
|
|
|
+ &ed25519_pubkey, nearest_hour,
|
|
|
+ HS_DESC_CERT_LIFETIME,
|
|
|
+ CERT_FLAG_INCLUDE_SIGNING_KEY);
|
|
|
+ if (desc_ip->enc_key_cert == NULL) {
|
|
|
+ log_warn(LD_REND, "Unable to create enc key curve25519 cross cert.");
|
|
|
+ goto done;
|
|
|
+ }
|
|
|
+ }
|
|
|
+ /* Success. */
|
|
|
+ ret = 0;
|
|
|
|
|
|
- tor_free(service);
|
|
|
+ done:
|
|
|
+ return ret;
|
|
|
}
|
|
|
|
|
|
-/* Initialize the service HS subsystem. */
|
|
|
-void
|
|
|
-hs_service_init(void)
|
|
|
+/* Using the given descriptor from the given service, build the descriptor
|
|
|
+ * intro point list so we can then encode the descriptor for publication. This
|
|
|
+ * function does not pick intro points, they have to be in the descriptor
|
|
|
+ * current map. Cryptographic material (keys) must be initialized in the
|
|
|
+ * descriptor for this function to make sense. */
|
|
|
+static void
|
|
|
+build_desc_intro_points(const hs_service_t *service,
|
|
|
+ hs_service_descriptor_t *desc, time_t now)
|
|
|
{
|
|
|
- /* Should never be called twice. */
|
|
|
- tor_assert(!hs_service_map);
|
|
|
- tor_assert(!hs_service_staging_list);
|
|
|
+ hs_desc_encrypted_data_t *encrypted;
|
|
|
|
|
|
- /* v2 specific. */
|
|
|
- rend_service_init();
|
|
|
+ tor_assert(service);
|
|
|
+ tor_assert(desc);
|
|
|
|
|
|
- hs_service_map = tor_malloc_zero(sizeof(struct hs_service_ht));
|
|
|
- HT_INIT(hs_service_ht, hs_service_map);
|
|
|
+ /* Ease our life. */
|
|
|
+ encrypted = &desc->desc->encrypted_data;
|
|
|
+ /* Cleanup intro points, we are about to set them from scratch. */
|
|
|
+ hs_descriptor_clear_intro_points(desc->desc);
|
|
|
|
|
|
- hs_service_staging_list = smartlist_new();
|
|
|
+ DIGEST256MAP_FOREACH(desc->intro_points.map, key,
|
|
|
+ const hs_service_intro_point_t *, ip) {
|
|
|
+ hs_desc_intro_point_t *desc_ip = hs_desc_intro_point_new();
|
|
|
+ if (setup_desc_intro_point(&desc->signing_kp, ip, now, desc_ip) < 0) {
|
|
|
+ hs_desc_intro_point_free(desc_ip);
|
|
|
+ continue;
|
|
|
+ }
|
|
|
+ /* We have a valid descriptor intro point. Add it to the list. */
|
|
|
+ smartlist_add(encrypted->intro_points, desc_ip);
|
|
|
+ } DIGEST256MAP_FOREACH_END;
|
|
|
}
|
|
|
|
|
|
-/* Release all global storage of the hidden service subsystem. */
|
|
|
-void
|
|
|
-hs_service_free_all(void)
|
|
|
+/* Populate the descriptor encrypted section fomr the given service object.
|
|
|
+ * This will generate a valid list of introduction points that can be used
|
|
|
+ * after for circuit creation. Return 0 on success else -1 on error. */
|
|
|
+static int
|
|
|
+build_service_desc_encrypted(const hs_service_t *service,
|
|
|
+ hs_service_descriptor_t *desc)
|
|
|
{
|
|
|
- rend_service_free_all();
|
|
|
- service_free_all();
|
|
|
-}
|
|
|
+ hs_desc_encrypted_data_t *encrypted;
|
|
|
+
|
|
|
+ tor_assert(service);
|
|
|
+ tor_assert(desc);
|
|
|
+
|
|
|
+ encrypted = &desc->desc->encrypted_data;
|
|
|
|
|
|
-/* XXX We don't currently use these functions, apart from generating unittest
|
|
|
- data. When we start implementing the service-side support for prop224 we
|
|
|
- should revisit these functions and use them. */
|
|
|
+ encrypted->create2_ntor = 1;
|
|
|
+ encrypted->single_onion_service = service->config.is_single_onion;
|
|
|
|
|
|
-/** Given an ESTABLISH_INTRO <b>cell</b>, encode it and place its payload in
|
|
|
- * <b>buf_out</b> which has size <b>buf_out_len</b>. Return the number of
|
|
|
- * bytes written, or a negative integer if there was an error. */
|
|
|
-ssize_t
|
|
|
-get_establish_intro_payload(uint8_t *buf_out, size_t buf_out_len,
|
|
|
- const trn_cell_establish_intro_t *cell)
|
|
|
+ /* Setup introduction points from what we have in the service. */
|
|
|
+ if (encrypted->intro_points == NULL) {
|
|
|
+ encrypted->intro_points = smartlist_new();
|
|
|
+ }
|
|
|
+ /* We do NOT build introduction point yet, we only do that once the circuit
|
|
|
+ * have been opened. Until we have the right number of introduction points,
|
|
|
+ * we do not encode anything in the descriptor. */
|
|
|
+
|
|
|
+ /* XXX: Support client authorization (#20700). */
|
|
|
+ encrypted->intro_auth_types = NULL;
|
|
|
+ return 0;
|
|
|
+}
|
|
|
+
|
|
|
+/* Populare the descriptor plaintext section from the given service object.
|
|
|
+ * The caller must make sure that the keys in the descriptors are valid that
|
|
|
+ * is are non-zero. Return 0 on success else -1 on error. */
|
|
|
+static int
|
|
|
+build_service_desc_plaintext(const hs_service_t *service,
|
|
|
+ hs_service_descriptor_t *desc, time_t now)
|
|
|
{
|
|
|
- ssize_t bytes_used = 0;
|
|
|
+ int ret = -1;
|
|
|
+ hs_desc_plaintext_data_t *plaintext;
|
|
|
|
|
|
- if (buf_out_len < RELAY_PAYLOAD_SIZE) {
|
|
|
- return -1;
|
|
|
+ tor_assert(service);
|
|
|
+ tor_assert(desc);
|
|
|
+ /* XXX: Use a "assert_desc_ok()" ? */
|
|
|
+ tor_assert(!tor_mem_is_zero((char *) &desc->blinded_kp,
|
|
|
+ sizeof(desc->blinded_kp)));
|
|
|
+ tor_assert(!tor_mem_is_zero((char *) &desc->signing_kp,
|
|
|
+ sizeof(desc->signing_kp)));
|
|
|
+
|
|
|
+ /* Set the subcredential. */
|
|
|
+ hs_get_subcredential(&service->keys.identity_pk, &desc->blinded_kp.pubkey,
|
|
|
+ desc->desc->subcredential);
|
|
|
+
|
|
|
+ plaintext = &desc->desc->plaintext_data;
|
|
|
+
|
|
|
+ plaintext->version = service->config.version;
|
|
|
+ plaintext->lifetime_sec = HS_DESC_DEFAULT_LIFETIME;
|
|
|
+ plaintext->signing_key_cert =
|
|
|
+ tor_cert_create(&desc->blinded_kp, CERT_TYPE_SIGNING_HS_DESC,
|
|
|
+ &desc->signing_kp.pubkey, now, HS_DESC_CERT_LIFETIME,
|
|
|
+ CERT_FLAG_INCLUDE_SIGNING_KEY);
|
|
|
+ if (plaintext->signing_key_cert == NULL) {
|
|
|
+ log_warn(LD_REND, "Unable to create descriptor signing certificate for "
|
|
|
+ "service %s",
|
|
|
+ safe_str_client(service->onion_address));
|
|
|
+ goto end;
|
|
|
+ }
|
|
|
+ /* Copy public key material to go in the descriptor. */
|
|
|
+ ed25519_pubkey_copy(&plaintext->signing_pubkey, &desc->signing_kp.pubkey);
|
|
|
+ ed25519_pubkey_copy(&plaintext->blinded_pubkey, &desc->blinded_kp.pubkey);
|
|
|
+ /* Success. */
|
|
|
+ ret = 0;
|
|
|
+
|
|
|
+ end:
|
|
|
+ return ret;
|
|
|
+}
|
|
|
+
|
|
|
+/* For the given service and descriptor object, create the key material which
|
|
|
+ * is the blinded keypair and the descriptor signing keypair. Return 0 on
|
|
|
+ * success else -1 on error where the generated keys MUST be ignored. */
|
|
|
+static int
|
|
|
+build_service_desc_keys(const hs_service_t *service,
|
|
|
+ hs_service_descriptor_t *desc,
|
|
|
+ uint64_t time_period_num)
|
|
|
+{
|
|
|
+ int ret = 0;
|
|
|
+ ed25519_keypair_t kp;
|
|
|
+
|
|
|
+ tor_assert(desc);
|
|
|
+ tor_assert(!tor_mem_is_zero((char *) &service->keys.identity_pk,
|
|
|
+ ED25519_PUBKEY_LEN));
|
|
|
+
|
|
|
+ /* XXX: Support offline key feature (#18098). */
|
|
|
+
|
|
|
+ /* Copy the identity keys to the keypair so we can use it to create the
|
|
|
+ * blinded key. */
|
|
|
+ memcpy(&kp.pubkey, &service->keys.identity_pk, sizeof(kp.pubkey));
|
|
|
+ memcpy(&kp.seckey, &service->keys.identity_sk, sizeof(kp.seckey));
|
|
|
+ /* Build blinded keypair for this time period. */
|
|
|
+ hs_build_blinded_keypair(&kp, NULL, 0, time_period_num, &desc->blinded_kp);
|
|
|
+ /* Let's not keep too much traces of our keys in memory. */
|
|
|
+ memwipe(&kp, 0, sizeof(kp));
|
|
|
+
|
|
|
+ /* No need for extra strong, this is a temporary key only for this
|
|
|
+ * descriptor. Nothing long term. */
|
|
|
+ if (ed25519_keypair_generate(&desc->signing_kp, 0) < 0) {
|
|
|
+ log_warn(LD_REND, "Can't generate descriptor signing keypair for "
|
|
|
+ "service %s",
|
|
|
+ safe_str_client(service->onion_address));
|
|
|
+ ret = -1;
|
|
|
}
|
|
|
|
|
|
- bytes_used = trn_cell_establish_intro_encode(buf_out, buf_out_len,
|
|
|
- cell);
|
|
|
- return bytes_used;
|
|
|
+ return ret;
|
|
|
}
|
|
|
|
|
|
-/* Set the cell extensions of <b>cell</b>. */
|
|
|
+/* Given a service and the current time, build a descriptor for the service.
|
|
|
+ * This function does not pick introduction point, this needs to be done by
|
|
|
+ * the update function. On success, desc_out will point to the newly allocated
|
|
|
+ * descriptor object.
|
|
|
+ *
|
|
|
+ * This can error if we are unable to create keys or certificate. */
|
|
|
static void
|
|
|
-set_trn_cell_extensions(trn_cell_establish_intro_t *cell)
|
|
|
+build_service_descriptor(hs_service_t *service, time_t now,
|
|
|
+ uint64_t time_period_num,
|
|
|
+ hs_service_descriptor_t **desc_out)
|
|
|
{
|
|
|
- trn_cell_extension_t *trn_cell_extensions = trn_cell_extension_new();
|
|
|
+ char *encoded_desc;
|
|
|
+ hs_service_descriptor_t *desc;
|
|
|
+
|
|
|
+ tor_assert(service);
|
|
|
+ tor_assert(desc_out);
|
|
|
+
|
|
|
+ desc = service_descriptor_new();
|
|
|
+ desc->time_period_num = time_period_num;
|
|
|
+
|
|
|
+ /* Create the needed keys so we can setup the descriptor content. */
|
|
|
+ if (build_service_desc_keys(service, desc, time_period_num) < 0) {
|
|
|
+ goto err;
|
|
|
+ }
|
|
|
+ /* Setup plaintext descriptor content. */
|
|
|
+ if (build_service_desc_plaintext(service, desc, now) < 0) {
|
|
|
+ goto err;
|
|
|
+ }
|
|
|
+ /* Setup encrypted descriptor content. */
|
|
|
+ if (build_service_desc_encrypted(service, desc) < 0) {
|
|
|
+ goto err;
|
|
|
+ }
|
|
|
+
|
|
|
+ /* Set the revision counter for this descriptor */
|
|
|
+ set_descriptor_revision_counter(desc->desc);
|
|
|
|
|
|
- /* For now, we don't use extensions at all. */
|
|
|
- trn_cell_extensions->num = 0; /* It's already zeroed, but be explicit. */
|
|
|
- trn_cell_establish_intro_set_extensions(cell, trn_cell_extensions);
|
|
|
+ /* Let's make sure that we've created a descriptor that can actually be
|
|
|
+ * encoded properly. This function also checks if the encoded output is
|
|
|
+ * decodable after. */
|
|
|
+ if (BUG(hs_desc_encode_descriptor(desc->desc, &desc->signing_kp,
|
|
|
+ &encoded_desc) < 0)) {
|
|
|
+ goto err;
|
|
|
+ }
|
|
|
+ tor_free(encoded_desc);
|
|
|
+
|
|
|
+ /* Assign newly built descriptor to the next slot. */
|
|
|
+ *desc_out = desc;
|
|
|
+ return;
|
|
|
+
|
|
|
+ err:
|
|
|
+ service_descriptor_free(desc);
|
|
|
+}
|
|
|
+
|
|
|
+/* Build descriptors for each service if needed. There are conditions to build
|
|
|
+ * a descriptor which are details in the function. */
|
|
|
+STATIC void
|
|
|
+build_all_descriptors(time_t now)
|
|
|
+{
|
|
|
+ FOR_EACH_SERVICE_BEGIN(service) {
|
|
|
+ if (service->desc_current == NULL) {
|
|
|
+ /* This means we just booted up because else this descriptor will never
|
|
|
+ * be NULL as it should always point to the descriptor that was in
|
|
|
+ * desc_next after rotation. */
|
|
|
+ build_service_descriptor(service, now, hs_get_time_period_num(now),
|
|
|
+ &service->desc_current);
|
|
|
+
|
|
|
+ log_info(LD_REND, "Hidden service %s current descriptor successfully "
|
|
|
+ "built. Now scheduled for upload.",
|
|
|
+ safe_str_client(service->onion_address));
|
|
|
+ }
|
|
|
+ /* A next descriptor to NULL indicate that we need to build a fresh one if
|
|
|
+ * we are in the overlap period for the _next_ time period since it means
|
|
|
+ * we either just booted or we just rotated our descriptors. */
|
|
|
+ if (hs_overlap_mode_is_active(NULL, now) && service->desc_next == NULL) {
|
|
|
+ build_service_descriptor(service, now, hs_get_next_time_period_num(now),
|
|
|
+ &service->desc_next);
|
|
|
+ log_info(LD_REND, "Hidden service %s next descriptor successfully "
|
|
|
+ "built. Now scheduled for upload.",
|
|
|
+ safe_str_client(service->onion_address));
|
|
|
+ }
|
|
|
+ } FOR_EACH_DESCRIPTOR_END;
|
|
|
}
|
|
|
|
|
|
-/** Given the circuit handshake info in <b>circuit_key_material</b>, create and
|
|
|
- * return an ESTABLISH_INTRO cell. Return NULL if something went wrong. The
|
|
|
- * returned cell is allocated on the heap and it's the responsibility of the
|
|
|
- * caller to free it. */
|
|
|
-trn_cell_establish_intro_t *
|
|
|
-generate_establish_intro_cell(const uint8_t *circuit_key_material,
|
|
|
- size_t circuit_key_material_len)
|
|
|
+/* Randomly pick a node to become an introduction point but not present in the
|
|
|
+ * given exclude_nodes list. The chosen node is put in the exclude list
|
|
|
+ * regardless of success or not because in case of failure, the node is simply
|
|
|
+ * unsusable from that point on. If direct_conn is set, try to pick a node
|
|
|
+ * that our local firewall/policy allows to directly connect to and if not,
|
|
|
+ * fallback to a normal 3-hop node. Return a newly allocated service intro
|
|
|
+ * point ready to be used for encoding. NULL on error. */
|
|
|
+static hs_service_intro_point_t *
|
|
|
+pick_intro_point(unsigned int direct_conn, smartlist_t *exclude_nodes)
|
|
|
{
|
|
|
- trn_cell_establish_intro_t *cell = NULL;
|
|
|
- ssize_t encoded_len;
|
|
|
+ const node_t *node;
|
|
|
+ extend_info_t *info = NULL;
|
|
|
+ hs_service_intro_point_t *ip = NULL;
|
|
|
+ /* Normal 3-hop introduction point flags. */
|
|
|
+ router_crn_flags_t flags = CRN_NEED_UPTIME | CRN_NEED_DESC;
|
|
|
+ /* Single onion flags. */
|
|
|
+ router_crn_flags_t direct_flags = flags | CRN_PREF_ADDR | CRN_DIRECT_CONN;
|
|
|
+
|
|
|
+ node = router_choose_random_node(exclude_nodes, get_options()->ExcludeNodes,
|
|
|
+ direct_conn ? direct_flags : flags);
|
|
|
+ if (node == NULL && direct_conn) {
|
|
|
+ /* Unable to find a node for direct connection, let's fall back to a
|
|
|
+ * normal 3-hop node. */
|
|
|
+ node = router_choose_random_node(exclude_nodes,
|
|
|
+ get_options()->ExcludeNodes, flags);
|
|
|
+ }
|
|
|
+ if (!node) {
|
|
|
+ goto err;
|
|
|
+ }
|
|
|
+
|
|
|
+ /* We have a suitable node, add it to the exclude list. We do this *before*
|
|
|
+ * we can validate the extend information because even in case of failure,
|
|
|
+ * we don't want to use that node anymore. */
|
|
|
+ smartlist_add(exclude_nodes, (void *) node);
|
|
|
|
|
|
- log_warn(LD_GENERAL,
|
|
|
- "Generating ESTABLISH_INTRO cell (key_material_len: %u)",
|
|
|
- (unsigned) circuit_key_material_len);
|
|
|
+ /* We do this to ease our life but also this call makes appropriate checks
|
|
|
+ * of the node object such as validating ntor support for instance. */
|
|
|
+ info = extend_info_from_node(node, direct_conn);
|
|
|
+ if (BUG(info == NULL)) {
|
|
|
+ goto err;
|
|
|
+ }
|
|
|
+
|
|
|
+ /* Let's do a basic sanity check here so that we don't end up advertising the
|
|
|
+ * ed25519 identity key of relays that don't actually support the link
|
|
|
+ * protocol */
|
|
|
+ if (!node_supports_ed25519_link_authentication(node)) {
|
|
|
+ tor_assert_nonfatal(ed25519_public_key_is_zero(&info->ed_identity));
|
|
|
+ }
|
|
|
|
|
|
- /* Generate short-term keypair for use in ESTABLISH_INTRO */
|
|
|
- ed25519_keypair_t key_struct;
|
|
|
- if (ed25519_keypair_generate(&key_struct, 0) < 0) {
|
|
|
+ /* Create our objects and populate them with the node information. */
|
|
|
+ ip = service_intro_point_new(info, !node_supports_ed25519_hs_intro(node));
|
|
|
+ if (ip == NULL) {
|
|
|
goto err;
|
|
|
}
|
|
|
+ extend_info_free(info);
|
|
|
+ return ip;
|
|
|
+ err:
|
|
|
+ service_intro_point_free(ip);
|
|
|
+ extend_info_free(info);
|
|
|
+ return NULL;
|
|
|
+}
|
|
|
+
|
|
|
+/* For a given descriptor from the given service, pick any needed intro points
|
|
|
+ * and update the current map with those newly picked intro points. Return the
|
|
|
+ * number node that might have been added to the descriptor current map. */
|
|
|
+static unsigned int
|
|
|
+pick_needed_intro_points(hs_service_t *service,
|
|
|
+ hs_service_descriptor_t *desc)
|
|
|
+{
|
|
|
+ int i = 0, num_needed_ip;
|
|
|
+ smartlist_t *exclude_nodes = smartlist_new();
|
|
|
+
|
|
|
+ tor_assert(service);
|
|
|
+ tor_assert(desc);
|
|
|
+
|
|
|
+ /* Compute how many intro points we actually need to open. */
|
|
|
+ num_needed_ip = service->config.num_intro_points -
|
|
|
+ digest256map_size(desc->intro_points.map);
|
|
|
+ if (BUG(num_needed_ip < 0)) {
|
|
|
+ /* Let's not make tor freak out here and just skip this. */
|
|
|
+ goto done;
|
|
|
+ }
|
|
|
+
|
|
|
+ /* We want to end up with config.num_intro_points intro points, but if we
|
|
|
+ * have no intro points at all (chances are they all cycled or we are
|
|
|
+ * starting up), we launch get_intro_point_num_extra() extra circuits and
|
|
|
+ * use the first config.num_intro_points that complete. See proposal #155,
|
|
|
+ * section 4 for the rationale of this which is purely for performance.
|
|
|
+ *
|
|
|
+ * The ones after the first config.num_intro_points will be converted to
|
|
|
+ * 'General' internal circuits and then we'll drop them from the list of
|
|
|
+ * intro points. */
|
|
|
+ if (digest256map_size(desc->intro_points.map) == 0) {
|
|
|
+ num_needed_ip += get_intro_point_num_extra();
|
|
|
+ }
|
|
|
+
|
|
|
+ /* Build an exclude list of nodes of our intro point(s). The expiring intro
|
|
|
+ * points are OK to pick again because this is afterall a concept of round
|
|
|
+ * robin so they are considered valid nodes to pick again. */
|
|
|
+ DIGEST256MAP_FOREACH(desc->intro_points.map, key,
|
|
|
+ hs_service_intro_point_t *, ip) {
|
|
|
+ const node_t *intro_node = get_node_from_intro_point(ip);
|
|
|
+ if (intro_node) {
|
|
|
+ smartlist_add(exclude_nodes, (void*)intro_node);
|
|
|
+ }
|
|
|
+ } DIGEST256MAP_FOREACH_END;
|
|
|
+ /* Also, add the failing intro points that our descriptor encounteered in
|
|
|
+ * the exclude node list. */
|
|
|
+ setup_intro_point_exclude_list(desc, exclude_nodes);
|
|
|
+
|
|
|
+ for (i = 0; i < num_needed_ip; i++) {
|
|
|
+ hs_service_intro_point_t *ip;
|
|
|
+
|
|
|
+ /* This function will add the picked intro point node to the exclude nodes
|
|
|
+ * list so we don't pick the same one at the next iteration. */
|
|
|
+ ip = pick_intro_point(service->config.is_single_onion, exclude_nodes);
|
|
|
+ if (ip == NULL) {
|
|
|
+ /* If we end up unable to pick an introduction point it is because we
|
|
|
+ * can't find suitable node and calling this again is highly unlikely to
|
|
|
+ * give us a valid node all of the sudden. */
|
|
|
+ log_info(LD_REND, "Unable to find a suitable node to be an "
|
|
|
+ "introduction point for service %s.",
|
|
|
+ safe_str_client(service->onion_address));
|
|
|
+ goto done;
|
|
|
+ }
|
|
|
+ /* Valid intro point object, add it to the descriptor current map. */
|
|
|
+ service_intro_point_add(desc->intro_points.map, ip);
|
|
|
+ }
|
|
|
+ /* We've successfully picked all our needed intro points thus none are
|
|
|
+ * missing which will tell our upload process to expect the number of
|
|
|
+ * circuits to be the number of configured intro points circuits and not the
|
|
|
+ * number of intro points object that we have. */
|
|
|
+ desc->missing_intro_points = 0;
|
|
|
+
|
|
|
+ /* Success. */
|
|
|
+ done:
|
|
|
+ /* We don't have ownership of the node_t object in this list. */
|
|
|
+ smartlist_free(exclude_nodes);
|
|
|
+ return i;
|
|
|
+}
|
|
|
+
|
|
|
+/* Update the given descriptor from the given service. The possible update
|
|
|
+ * actions includes:
|
|
|
+ * - Picking missing intro points if needed.
|
|
|
+ * - Incrementing the revision counter if needed.
|
|
|
+ */
|
|
|
+static void
|
|
|
+update_service_descriptor(hs_service_t *service,
|
|
|
+ hs_service_descriptor_t *desc, time_t now)
|
|
|
+{
|
|
|
+ unsigned int num_intro_points;
|
|
|
+
|
|
|
+ tor_assert(service);
|
|
|
+ tor_assert(desc);
|
|
|
+ tor_assert(desc->desc);
|
|
|
+
|
|
|
+ num_intro_points = digest256map_size(desc->intro_points.map);
|
|
|
+
|
|
|
+ /* Pick any missing introduction point(s). */
|
|
|
+ if (num_intro_points < service->config.num_intro_points) {
|
|
|
+ unsigned int num_new_intro_points = pick_needed_intro_points(service,
|
|
|
+ desc);
|
|
|
+ if (num_new_intro_points != 0) {
|
|
|
+ log_info(LD_REND, "Service %s just picked %u intro points and wanted "
|
|
|
+ "%u. It currently has %d intro points. "
|
|
|
+ "Launching ESTABLISH_INTRO circuit shortly.",
|
|
|
+ safe_str_client(service->onion_address),
|
|
|
+ num_new_intro_points,
|
|
|
+ service->config.num_intro_points - num_intro_points,
|
|
|
+ num_intro_points);
|
|
|
+ /* We'll build those introduction point into the descriptor once we have
|
|
|
+ * confirmation that the circuits are opened and ready. However,
|
|
|
+ * indicate that this descriptor should be uploaded from now on. */
|
|
|
+ desc->next_upload_time = now;
|
|
|
+ }
|
|
|
+ /* Were we able to pick all the intro points we needed? If not, we'll
|
|
|
+ * flag the descriptor that it's missing intro points because it
|
|
|
+ * couldn't pick enough which will trigger a descriptor upload. */
|
|
|
+ if ((num_new_intro_points + num_intro_points) <
|
|
|
+ service->config.num_intro_points) {
|
|
|
+ desc->missing_intro_points = 1;
|
|
|
+ }
|
|
|
+ }
|
|
|
+}
|
|
|
+
|
|
|
+/* Update descriptors for each service if needed. */
|
|
|
+STATIC void
|
|
|
+update_all_descriptors(time_t now)
|
|
|
+{
|
|
|
+ FOR_EACH_SERVICE_BEGIN(service) {
|
|
|
+ /* We'll try to update each descriptor that is if certain conditions apply
|
|
|
+ * in order for the descriptor to be updated. */
|
|
|
+ FOR_EACH_DESCRIPTOR_BEGIN(service, desc) {
|
|
|
+ update_service_descriptor(service, desc, now);
|
|
|
+ } FOR_EACH_DESCRIPTOR_END;
|
|
|
+ } FOR_EACH_SERVICE_END;
|
|
|
+}
|
|
|
+
|
|
|
+/* Return true iff the given intro point has expired that is it has been used
|
|
|
+ * for too long or we've reached our max seen INTRODUCE2 cell. */
|
|
|
+STATIC int
|
|
|
+intro_point_should_expire(const hs_service_intro_point_t *ip,
|
|
|
+ time_t now)
|
|
|
+{
|
|
|
+ tor_assert(ip);
|
|
|
+
|
|
|
+ if (ip->introduce2_count >= ip->introduce2_max) {
|
|
|
+ goto expired;
|
|
|
+ }
|
|
|
+
|
|
|
+ if (ip->time_to_expire <= now) {
|
|
|
+ goto expired;
|
|
|
+ }
|
|
|
+
|
|
|
+ /* Not expiring. */
|
|
|
+ return 0;
|
|
|
+ expired:
|
|
|
+ return 1;
|
|
|
+}
|
|
|
+
|
|
|
+/* Go over the given set of intro points for each service and remove any
|
|
|
+ * invalid ones. The conditions for removal are:
|
|
|
+ *
|
|
|
+ * - The node doesn't exists anymore (not in consensus)
|
|
|
+ * OR
|
|
|
+ * - The intro point maximum circuit retry count has been reached and no
|
|
|
+ * circuit can be found associated with it.
|
|
|
+ * OR
|
|
|
+ * - The intro point has expired and we should pick a new one.
|
|
|
+ *
|
|
|
+ * If an intro point is removed, the circuit (if any) is immediately close.
|
|
|
+ * If a circuit can't be found, the intro point is kept if it hasn't reached
|
|
|
+ * its maximum circuit retry value and thus should be retried. */
|
|
|
+static void
|
|
|
+cleanup_intro_points(hs_service_t *service, time_t now)
|
|
|
+{
|
|
|
+ tor_assert(service);
|
|
|
+
|
|
|
+ /* For both descriptors, cleanup the intro points. */
|
|
|
+ FOR_EACH_DESCRIPTOR_BEGIN(service, desc) {
|
|
|
+ /* Go over the current intro points we have, make sure they are still
|
|
|
+ * valid and remove any of them that aren't. */
|
|
|
+ DIGEST256MAP_FOREACH_MODIFY(desc->intro_points.map, key,
|
|
|
+ hs_service_intro_point_t *, ip) {
|
|
|
+ const node_t *node = get_node_from_intro_point(ip);
|
|
|
+ origin_circuit_t *ocirc = hs_circ_service_get_intro_circ(ip);
|
|
|
+ int has_expired = intro_point_should_expire(ip, now);
|
|
|
+
|
|
|
+ /* We cleanup an intro point if it has expired or if we do not know the
|
|
|
+ * node_t anymore (removed from our latest consensus) or if we've
|
|
|
+ * reached the maximum number of retry with a non existing circuit. */
|
|
|
+ if (has_expired || node == NULL ||
|
|
|
+ ip->circuit_retries > MAX_INTRO_POINT_CIRCUIT_RETRIES) {
|
|
|
+ /* Remove intro point from descriptor map. We'll add it to the failed
|
|
|
+ * map if we retried it too many times. */
|
|
|
+ MAP_DEL_CURRENT(key);
|
|
|
+ service_intro_point_free(ip);
|
|
|
+
|
|
|
+ /* XXX: Legacy code does NOT do that, it keeps the circuit open until
|
|
|
+ * a new descriptor is uploaded and then closed all expiring intro
|
|
|
+ * point circuit. Here, we close immediately and because we just
|
|
|
+ * discarded the intro point, a new one will be selected, a new
|
|
|
+ * descriptor created and uploaded. There is no difference to an
|
|
|
+ * attacker between the timing of a new consensus and intro point
|
|
|
+ * rotation (possibly?). */
|
|
|
+ if (ocirc && !TO_CIRCUIT(ocirc)->marked_for_close) {
|
|
|
+ /* After this, no new cells will be handled on the circuit. */
|
|
|
+ circuit_mark_for_close(TO_CIRCUIT(ocirc), END_CIRC_REASON_FINISHED);
|
|
|
+ }
|
|
|
+ continue;
|
|
|
+ }
|
|
|
+ } DIGEST256MAP_FOREACH_END;
|
|
|
+ } FOR_EACH_DESCRIPTOR_END;
|
|
|
+}
|
|
|
+
|
|
|
+/** We just entered overlap period and we need to rotate our <b>service</b>
|
|
|
+ * descriptors */
|
|
|
+static void
|
|
|
+rotate_service_descriptors(hs_service_t *service)
|
|
|
+{
|
|
|
+ if (service->desc_current) {
|
|
|
+ /* Close all IP circuits for the descriptor. */
|
|
|
+ close_intro_circuits(&service->desc_current->intro_points);
|
|
|
+ /* We don't need this one anymore, we won't serve any clients coming with
|
|
|
+ * this service descriptor. */
|
|
|
+ service_descriptor_free(service->desc_current);
|
|
|
+ }
|
|
|
+ /* The next one become the current one and emptying the next will trigger
|
|
|
+ * a descriptor creation for it. */
|
|
|
+ service->desc_current = service->desc_next;
|
|
|
+ service->desc_next = NULL;
|
|
|
+}
|
|
|
+
|
|
|
+/* Rotate descriptors for each service if needed. If we are just entering
|
|
|
+ * the overlap period, rotate them that is point the previous descriptor to
|
|
|
+ * the current and cleanup the previous one. A non existing current
|
|
|
+ * descriptor will trigger a descriptor build for the next time period. */
|
|
|
+STATIC void
|
|
|
+rotate_all_descriptors(time_t now)
|
|
|
+{
|
|
|
+ /* XXX We rotate all our service descriptors at once. In the future it might
|
|
|
+ * be wise, to rotate service descriptors independently to hide that all
|
|
|
+ * those descriptors are on the same tor instance */
|
|
|
+
|
|
|
+ FOR_EACH_SERVICE_BEGIN(service) {
|
|
|
+ /* We are _not_ in the overlap period so skip rotation. */
|
|
|
+ if (!hs_overlap_mode_is_active(NULL, now)) {
|
|
|
+ service->state.in_overlap_period = 0;
|
|
|
+ continue;
|
|
|
+ }
|
|
|
+ /* We've entered the overlap period already so skip rotation. */
|
|
|
+ if (service->state.in_overlap_period) {
|
|
|
+ continue;
|
|
|
+ }
|
|
|
+ /* It's the first time the service encounters the overlap period so flag
|
|
|
+ * it in order to make sure we don't rotate at next check. */
|
|
|
+ service->state.in_overlap_period = 1;
|
|
|
+
|
|
|
+ /* If we have a next descriptor lined up, rotate the descriptors so that it
|
|
|
+ * becomes current. */
|
|
|
+ if (service->desc_next) {
|
|
|
+ rotate_service_descriptors(service);
|
|
|
+ }
|
|
|
+ log_info(LD_REND, "We've just entered the overlap period. Service %s "
|
|
|
+ "descriptors have been rotated!",
|
|
|
+ safe_str_client(service->onion_address));
|
|
|
+ } FOR_EACH_SERVICE_END;
|
|
|
+}
|
|
|
+
|
|
|
+/* Scheduled event run from the main loop. Make sure all our services are up
|
|
|
+ * to date and ready for the other scheduled events. This includes looking at
|
|
|
+ * the introduction points status and descriptor rotation time. */
|
|
|
+STATIC void
|
|
|
+run_housekeeping_event(time_t now)
|
|
|
+{
|
|
|
+ /* Note that nothing here opens circuit(s) nor uploads descriptor(s). We are
|
|
|
+ * simply moving things around or removing uneeded elements. */
|
|
|
+
|
|
|
+ FOR_EACH_SERVICE_BEGIN(service) {
|
|
|
+ /* Cleanup invalid intro points from the service descriptor. */
|
|
|
+ cleanup_intro_points(service, now);
|
|
|
+
|
|
|
+ /* Remove expired failing intro point from the descriptor failed list. We
|
|
|
+ * reset them at each INTRO_CIRC_RETRY_PERIOD. */
|
|
|
+ remove_expired_failing_intro(service, now);
|
|
|
+
|
|
|
+ /* At this point, the service is now ready to go through the scheduled
|
|
|
+ * events guaranteeing a valid state. Intro points might be missing from
|
|
|
+ * the descriptors after the cleanup but the update/build process will
|
|
|
+ * make sure we pick those missing ones. */
|
|
|
+ } FOR_EACH_SERVICE_END;
|
|
|
+}
|
|
|
+
|
|
|
+/* Scheduled event run from the main loop. Make sure all descriptors are up to
|
|
|
+ * date. Once this returns, each service descriptor needs to be considered for
|
|
|
+ * new introduction circuits and then for upload. */
|
|
|
+static void
|
|
|
+run_build_descriptor_event(time_t now)
|
|
|
+{
|
|
|
+ /* For v2 services, this step happens in the upload event. */
|
|
|
+
|
|
|
+ /* Run v3+ events. */
|
|
|
+ /* We start by rotating the descriptors only if needed. */
|
|
|
+ rotate_all_descriptors(now);
|
|
|
+
|
|
|
+ /* Then, we'll try to build new descriptors that we might need. The
|
|
|
+ * condition is that the next descriptor is non existing because it has
|
|
|
+ * been rotated or we just started up. */
|
|
|
+ build_all_descriptors(now);
|
|
|
+
|
|
|
+ /* Finally, we'll check if we should update the descriptors. Missing
|
|
|
+ * introduction points will be picked in this function which is useful for
|
|
|
+ * newly built descriptors. */
|
|
|
+ update_all_descriptors(now);
|
|
|
+}
|
|
|
+
|
|
|
+/* For the given service, launch any intro point circuits that could be
|
|
|
+ * needed. This considers every descriptor of the service. */
|
|
|
+static void
|
|
|
+launch_intro_point_circuits(hs_service_t *service)
|
|
|
+{
|
|
|
+ tor_assert(service);
|
|
|
+
|
|
|
+ /* For both descriptors, try to launch any missing introduction point
|
|
|
+ * circuits using the current map. */
|
|
|
+ FOR_EACH_DESCRIPTOR_BEGIN(service, desc) {
|
|
|
+ /* Keep a ref on if we need a direct connection. We use this often. */
|
|
|
+ unsigned int direct_conn = service->config.is_single_onion;
|
|
|
+
|
|
|
+ DIGEST256MAP_FOREACH_MODIFY(desc->intro_points.map, key,
|
|
|
+ hs_service_intro_point_t *, ip) {
|
|
|
+ extend_info_t *ei;
|
|
|
+
|
|
|
+ /* Skip the intro point that already has an existing circuit
|
|
|
+ * (established or not). */
|
|
|
+ if (hs_circ_service_get_intro_circ(ip)) {
|
|
|
+ continue;
|
|
|
+ }
|
|
|
+
|
|
|
+ ei = get_extend_info_from_intro_point(ip, direct_conn);
|
|
|
+ if (ei == NULL) {
|
|
|
+ if (!direct_conn) {
|
|
|
+ /* In case of a multi-hop connection, it should never happen that we
|
|
|
+ * can't get the extend info from the node. Avoid connection and
|
|
|
+ * remove intro point from descriptor in order to recover from this
|
|
|
+ * potential bug. */
|
|
|
+ tor_assert_nonfatal(ei);
|
|
|
+ }
|
|
|
+ MAP_DEL_CURRENT(key);
|
|
|
+ service_intro_point_free(ip);
|
|
|
+ continue;
|
|
|
+ }
|
|
|
+
|
|
|
+ /* Launch a circuit to the intro point. */
|
|
|
+ ip->circuit_retries++;
|
|
|
+ if (hs_circ_launch_intro_point(service, ip, ei) < 0) {
|
|
|
+ log_warn(LD_REND, "Unable to launch intro circuit to node %s "
|
|
|
+ "for service %s.",
|
|
|
+ safe_str_client(extend_info_describe(ei)),
|
|
|
+ safe_str_client(service->onion_address));
|
|
|
+ /* Intro point will be retried if possible after this. */
|
|
|
+ }
|
|
|
+ extend_info_free(ei);
|
|
|
+ } DIGEST256MAP_FOREACH_END;
|
|
|
+ } FOR_EACH_DESCRIPTOR_END;
|
|
|
+}
|
|
|
+
|
|
|
+/* Don't try to build more than this many circuits before giving up for a
|
|
|
+ * while. Dynamically calculated based on the configured number of intro
|
|
|
+ * points for the given service and how many descriptor exists. The default
|
|
|
+ * use case of 3 introduction points and two descriptors will allow 28
|
|
|
+ * circuits for a retry period (((3 + 2) + (3 * 3)) * 2). */
|
|
|
+static unsigned int
|
|
|
+get_max_intro_circ_per_period(const hs_service_t *service)
|
|
|
+{
|
|
|
+ unsigned int count = 0;
|
|
|
+ unsigned int multiplier = 0;
|
|
|
+ unsigned int num_wanted_ip;
|
|
|
+
|
|
|
+ tor_assert(service);
|
|
|
+ tor_assert(service->config.num_intro_points <=
|
|
|
+ HS_CONFIG_V3_MAX_INTRO_POINTS);
|
|
|
+
|
|
|
+/* For a testing network, allow to do it for the maximum amount so circuit
|
|
|
+ * creation and rotation and so on can actually be tested without limit. */
|
|
|
+#define MAX_INTRO_POINT_CIRCUIT_RETRIES_TESTING -1
|
|
|
+ if (get_options()->TestingTorNetwork) {
|
|
|
+ return MAX_INTRO_POINT_CIRCUIT_RETRIES_TESTING;
|
|
|
+ }
|
|
|
|
|
|
- cell = trn_cell_establish_intro_new();
|
|
|
+ num_wanted_ip = service->config.num_intro_points;
|
|
|
|
|
|
- /* Set AUTH_KEY_TYPE: 2 means ed25519 */
|
|
|
- trn_cell_establish_intro_set_auth_key_type(cell,
|
|
|
- HS_INTRO_AUTH_KEY_TYPE_ED25519);
|
|
|
+ /* The calculation is as follow. We have a number of intro points that we
|
|
|
+ * want configured as a torrc option (num_intro_points). We then add an
|
|
|
+ * extra value so we can launch multiple circuits at once and pick the
|
|
|
+ * quickest ones. For instance, we want 3 intros, we add 2 extra so we'll
|
|
|
+ * pick 5 intros and launch 5 circuits. */
|
|
|
+ count += (num_wanted_ip + get_intro_point_num_extra());
|
|
|
|
|
|
- /* Set AUTH_KEY_LEN field */
|
|
|
- /* Must also set byte-length of AUTH_KEY to match */
|
|
|
- int auth_key_len = ED25519_PUBKEY_LEN;
|
|
|
- trn_cell_establish_intro_set_auth_key_len(cell, auth_key_len);
|
|
|
- trn_cell_establish_intro_setlen_auth_key(cell, auth_key_len);
|
|
|
+ /* Then we add the number of retries that is possible to do for each intro
|
|
|
+ * point. If we want 3 intros, we'll allow 3 times the number of possible
|
|
|
+ * retry. */
|
|
|
+ count += (num_wanted_ip * MAX_INTRO_POINT_CIRCUIT_RETRIES);
|
|
|
|
|
|
- /* Set AUTH_KEY field */
|
|
|
- uint8_t *auth_key_ptr = trn_cell_establish_intro_getarray_auth_key(cell);
|
|
|
- memcpy(auth_key_ptr, key_struct.pubkey.pubkey, auth_key_len);
|
|
|
+ /* Then, we multiply by a factor of 2 if we have both descriptor or 0 if we
|
|
|
+ * have none. */
|
|
|
+ multiplier += (service->desc_current) ? 1 : 0;
|
|
|
+ multiplier += (service->desc_next) ? 1 : 0;
|
|
|
|
|
|
- /* No cell extensions needed */
|
|
|
- set_trn_cell_extensions(cell);
|
|
|
+ return (count * multiplier);
|
|
|
+}
|
|
|
|
|
|
- /* Set signature size.
|
|
|
- We need to do this up here, because _encode() needs it and we need to call
|
|
|
- _encode() to calculate the MAC and signature.
|
|
|
- */
|
|
|
- int sig_len = ED25519_SIG_LEN;
|
|
|
- trn_cell_establish_intro_set_sig_len(cell, sig_len);
|
|
|
- trn_cell_establish_intro_setlen_sig(cell, sig_len);
|
|
|
+/* For the given service, return 1 if the service is allowed to launch more
|
|
|
+ * introduction circuits else 0 if the maximum has been reached for the retry
|
|
|
+ * period of INTRO_CIRC_RETRY_PERIOD. */
|
|
|
+STATIC int
|
|
|
+can_service_launch_intro_circuit(hs_service_t *service, time_t now)
|
|
|
+{
|
|
|
+ tor_assert(service);
|
|
|
|
|
|
- /* XXX How to make this process easier and nicer? */
|
|
|
+ /* Consider the intro circuit retry period of the service. */
|
|
|
+ if (now > (service->state.intro_circ_retry_started_time +
|
|
|
+ INTRO_CIRC_RETRY_PERIOD)) {
|
|
|
+ service->state.intro_circ_retry_started_time = now;
|
|
|
+ service->state.num_intro_circ_launched = 0;
|
|
|
+ goto allow;
|
|
|
+ }
|
|
|
+ /* Check if we can still launch more circuits in this period. */
|
|
|
+ if (service->state.num_intro_circ_launched <=
|
|
|
+ get_max_intro_circ_per_period(service)) {
|
|
|
+ goto allow;
|
|
|
+ }
|
|
|
|
|
|
- /* Calculate the cell MAC (aka HANDSHAKE_AUTH). */
|
|
|
+ /* Rate limit log that we've reached our circuit creation limit. */
|
|
|
{
|
|
|
- /* To calculate HANDSHAKE_AUTH, we dump the cell in bytes, and then derive
|
|
|
- the MAC from it. */
|
|
|
- uint8_t cell_bytes_tmp[RELAY_PAYLOAD_SIZE] = {0};
|
|
|
- uint8_t mac[TRUNNEL_SHA3_256_LEN];
|
|
|
-
|
|
|
- encoded_len = trn_cell_establish_intro_encode(cell_bytes_tmp,
|
|
|
- sizeof(cell_bytes_tmp),
|
|
|
- cell);
|
|
|
- if (encoded_len < 0) {
|
|
|
- log_warn(LD_OR, "Unable to pre-encode ESTABLISH_INTRO cell.");
|
|
|
- goto err;
|
|
|
+ char *msg;
|
|
|
+ time_t elapsed_time = now - service->state.intro_circ_retry_started_time;
|
|
|
+ static ratelim_t rlimit = RATELIM_INIT(INTRO_CIRC_RETRY_PERIOD);
|
|
|
+ if ((msg = rate_limit_log(&rlimit, now))) {
|
|
|
+ log_info(LD_REND, "Hidden service %s exceeded its circuit launch limit "
|
|
|
+ "of %u per %d seconds. It launched %u circuits in "
|
|
|
+ "the last %ld seconds. Will retry in %ld seconds.",
|
|
|
+ safe_str_client(service->onion_address),
|
|
|
+ get_max_intro_circ_per_period(service),
|
|
|
+ INTRO_CIRC_RETRY_PERIOD,
|
|
|
+ service->state.num_intro_circ_launched, elapsed_time,
|
|
|
+ INTRO_CIRC_RETRY_PERIOD - elapsed_time);
|
|
|
+ tor_free(msg);
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+ /* Not allow. */
|
|
|
+ return 0;
|
|
|
+ allow:
|
|
|
+ return 1;
|
|
|
+}
|
|
|
+
|
|
|
+/* Scheduled event run from the main loop. Make sure we have all the circuits
|
|
|
+ * we need for each service. */
|
|
|
+static void
|
|
|
+run_build_circuit_event(time_t now)
|
|
|
+{
|
|
|
+ /* Make sure we can actually have enough information or able to build
|
|
|
+ * internal circuits as required by services. */
|
|
|
+ if (router_have_consensus_path() == CONSENSUS_PATH_UNKNOWN ||
|
|
|
+ !have_completed_a_circuit()) {
|
|
|
+ return;
|
|
|
+ }
|
|
|
+
|
|
|
+ /* Run v2 check. */
|
|
|
+ if (rend_num_services() > 0) {
|
|
|
+ rend_consider_services_intro_points(now);
|
|
|
+ }
|
|
|
+
|
|
|
+ /* Run v3+ check. */
|
|
|
+ FOR_EACH_SERVICE_BEGIN(service) {
|
|
|
+ /* For introduction circuit, we need to make sure we don't stress too much
|
|
|
+ * circuit creation so make sure this service is respecting that limit. */
|
|
|
+ if (can_service_launch_intro_circuit(service, now)) {
|
|
|
+ /* Launch intro point circuits if needed. */
|
|
|
+ launch_intro_point_circuits(service);
|
|
|
+ /* Once the circuits have opened, we'll make sure to update the
|
|
|
+ * descriptor intro point list and cleanup any extraneous. */
|
|
|
}
|
|
|
+ } FOR_EACH_SERVICE_END;
|
|
|
+}
|
|
|
|
|
|
- /* sanity check */
|
|
|
- tor_assert(encoded_len > ED25519_SIG_LEN + 2 + TRUNNEL_SHA3_256_LEN);
|
|
|
+/* Encode and sign the service descriptor desc and upload it to the given
|
|
|
+ * hidden service directory. This does nothing if PublishHidServDescriptors
|
|
|
+ * is false. */
|
|
|
+static void
|
|
|
+upload_descriptor_to_hsdir(const hs_service_t *service,
|
|
|
+ hs_service_descriptor_t *desc, const node_t *hsdir)
|
|
|
+{
|
|
|
+ char version_str[4] = {0}, *encoded_desc = NULL;
|
|
|
+ directory_request_t *dir_req;
|
|
|
+ hs_ident_dir_conn_t ident;
|
|
|
+
|
|
|
+ tor_assert(service);
|
|
|
+ tor_assert(desc);
|
|
|
+ tor_assert(hsdir);
|
|
|
+
|
|
|
+ memset(&ident, 0, sizeof(ident));
|
|
|
+
|
|
|
+ /* Let's avoid doing that if tor is configured to not publish. */
|
|
|
+ if (!get_options()->PublishHidServDescriptors) {
|
|
|
+ log_info(LD_REND, "Service %s not publishing descriptor. "
|
|
|
+ "PublishHidServDescriptors is set to 1.",
|
|
|
+ safe_str_client(service->onion_address));
|
|
|
+ goto end;
|
|
|
+ }
|
|
|
|
|
|
- /* Calculate MAC of all fields before HANDSHAKE_AUTH */
|
|
|
- crypto_mac_sha3_256(mac, sizeof(mac),
|
|
|
- circuit_key_material, circuit_key_material_len,
|
|
|
- cell_bytes_tmp,
|
|
|
- encoded_len -
|
|
|
- (ED25519_SIG_LEN + 2 + TRUNNEL_SHA3_256_LEN));
|
|
|
- /* Write the MAC to the cell */
|
|
|
- uint8_t *handshake_ptr =
|
|
|
- trn_cell_establish_intro_getarray_handshake_mac(cell);
|
|
|
- memcpy(handshake_ptr, mac, sizeof(mac));
|
|
|
+ /* First of all, we'll encode the descriptor. This should NEVER fail but
|
|
|
+ * just in case, let's make sure we have an actual usable descriptor. */
|
|
|
+ if (BUG(hs_desc_encode_descriptor(desc->desc, &desc->signing_kp,
|
|
|
+ &encoded_desc) < 0)) {
|
|
|
+ goto end;
|
|
|
}
|
|
|
|
|
|
- /* Calculate the cell signature */
|
|
|
+ /* Setup the connection identifier. */
|
|
|
+ ed25519_pubkey_copy(&ident.identity_pk, &service->keys.identity_pk);
|
|
|
+ /* This is our resource when uploading which is used to construct the URL
|
|
|
+ * with the version number: "/tor/hs/<version>/publish". */
|
|
|
+ tor_snprintf(version_str, sizeof(version_str), "%u",
|
|
|
+ service->config.version);
|
|
|
+
|
|
|
+ /* Build the directory request for this HSDir. */
|
|
|
+ dir_req = directory_request_new(DIR_PURPOSE_UPLOAD_HSDESC);
|
|
|
+ directory_request_set_routerstatus(dir_req, hsdir->rs);
|
|
|
+ directory_request_set_indirection(dir_req, DIRIND_ANONYMOUS);
|
|
|
+ directory_request_set_resource(dir_req, version_str);
|
|
|
+ directory_request_set_payload(dir_req, encoded_desc,
|
|
|
+ strlen(encoded_desc));
|
|
|
+ /* The ident object is copied over the directory connection object once
|
|
|
+ * the directory request is initiated. */
|
|
|
+ directory_request_upload_set_hs_ident(dir_req, &ident);
|
|
|
+
|
|
|
+ /* Initiate the directory request to the hsdir.*/
|
|
|
+ directory_initiate_request(dir_req);
|
|
|
+ directory_request_free(dir_req);
|
|
|
+
|
|
|
+ /* Logging so we know where it was sent. */
|
|
|
{
|
|
|
- /* To calculate the sig we follow the same procedure as above. We first
|
|
|
- dump the cell up to the sig, and then calculate the sig */
|
|
|
- uint8_t cell_bytes_tmp[RELAY_PAYLOAD_SIZE] = {0};
|
|
|
- ed25519_signature_t sig;
|
|
|
-
|
|
|
- encoded_len = trn_cell_establish_intro_encode(cell_bytes_tmp,
|
|
|
- sizeof(cell_bytes_tmp),
|
|
|
- cell);
|
|
|
- if (encoded_len < 0) {
|
|
|
- log_warn(LD_OR, "Unable to pre-encode ESTABLISH_INTRO cell (2).");
|
|
|
- goto err;
|
|
|
+ int is_next_desc = (service->desc_next == desc);
|
|
|
+ const uint8_t *index = (is_next_desc) ? hsdir->hsdir_index->next :
|
|
|
+ hsdir->hsdir_index->current;
|
|
|
+ log_info(LD_REND, "Service %s %s descriptor of revision %" PRIu64
|
|
|
+ " initiated upload request to %s with index %s",
|
|
|
+ safe_str_client(service->onion_address),
|
|
|
+ (is_next_desc) ? "next" : "current",
|
|
|
+ desc->desc->plaintext_data.revision_counter,
|
|
|
+ safe_str_client(node_describe(hsdir)),
|
|
|
+ safe_str_client(hex_str((const char *) index, 32)));
|
|
|
+ }
|
|
|
+
|
|
|
+ /* XXX: Inform control port of the upload event (#20699). */
|
|
|
+ end:
|
|
|
+ tor_free(encoded_desc);
|
|
|
+ return;
|
|
|
+}
|
|
|
+
|
|
|
+/** Return a newly-allocated string for our state file which contains revision
|
|
|
+ * counter information for <b>desc</b>. The format is:
|
|
|
+ *
|
|
|
+ * HidServRevCounter <blinded_pubkey> <rev_counter>
|
|
|
+ */
|
|
|
+STATIC char *
|
|
|
+encode_desc_rev_counter_for_state(const hs_service_descriptor_t *desc)
|
|
|
+{
|
|
|
+ char *state_str = NULL;
|
|
|
+ char blinded_pubkey_b64[ED25519_BASE64_LEN+1];
|
|
|
+ uint64_t rev_counter = desc->desc->plaintext_data.revision_counter;
|
|
|
+ const ed25519_public_key_t *blinded_pubkey = &desc->blinded_kp.pubkey;
|
|
|
+
|
|
|
+ /* Turn the blinded key into b64 so that we save it on state */
|
|
|
+ tor_assert(blinded_pubkey);
|
|
|
+ if (ed25519_public_to_base64(blinded_pubkey_b64, blinded_pubkey) < 0) {
|
|
|
+ goto done;
|
|
|
+ }
|
|
|
+
|
|
|
+ /* Format is: <blinded key> <rev counter> */
|
|
|
+ tor_asprintf(&state_str, "%s %" PRIu64, blinded_pubkey_b64, rev_counter);
|
|
|
+
|
|
|
+ log_info(LD_GENERAL, "[!] Adding rev counter %" PRIu64 " for %s!",
|
|
|
+ rev_counter, blinded_pubkey_b64);
|
|
|
+
|
|
|
+ done:
|
|
|
+ return state_str;
|
|
|
+}
|
|
|
+
|
|
|
+/** Update HS descriptor revision counters in our state by removing the old
|
|
|
+ * ones and writing down the ones that are currently active. */
|
|
|
+static void
|
|
|
+update_revision_counters_in_state(void)
|
|
|
+{
|
|
|
+ config_line_t *lines = NULL;
|
|
|
+ config_line_t **nextline = &lines;
|
|
|
+ or_state_t *state = get_or_state();
|
|
|
+
|
|
|
+ /* Prepare our state structure with the rev counters */
|
|
|
+ FOR_EACH_SERVICE_BEGIN(service) {
|
|
|
+ FOR_EACH_DESCRIPTOR_BEGIN(service, desc) {
|
|
|
+ /* We don't want to save zero counters */
|
|
|
+ if (desc->desc->plaintext_data.revision_counter == 0) {
|
|
|
+ continue;
|
|
|
+ }
|
|
|
+
|
|
|
+ *nextline = tor_malloc_zero(sizeof(config_line_t));
|
|
|
+ (*nextline)->key = tor_strdup("HidServRevCounter");
|
|
|
+ (*nextline)->value = encode_desc_rev_counter_for_state(desc);
|
|
|
+ nextline = &(*nextline)->next;
|
|
|
+ } FOR_EACH_DESCRIPTOR_END;
|
|
|
+ } FOR_EACH_SERVICE_END;
|
|
|
+
|
|
|
+ /* Remove the old rev counters, and replace them with the new ones */
|
|
|
+ config_free_lines(state->HidServRevCounter);
|
|
|
+ state->HidServRevCounter = lines;
|
|
|
+
|
|
|
+ /* Set the state as dirty since we just edited it */
|
|
|
+ if (!get_options()->AvoidDiskWrites) {
|
|
|
+ or_state_mark_dirty(state, 0);
|
|
|
+ }
|
|
|
+}
|
|
|
+
|
|
|
+/** Scan the string <b>state_line</b> for the revision counter of the service
|
|
|
+ * with <b>blinded_pubkey</b>. Set <b>service_found_out</b> to True if the
|
|
|
+ * line is relevant to this service, and return the cached revision
|
|
|
+ * counter. Else set <b>service_found_out</b> to False. */
|
|
|
+STATIC uint64_t
|
|
|
+check_state_line_for_service_rev_counter(const char *state_line,
|
|
|
+ const ed25519_public_key_t *blinded_pubkey,
|
|
|
+ int *service_found_out)
|
|
|
+{
|
|
|
+ smartlist_t *items = NULL;
|
|
|
+ int ok;
|
|
|
+ ed25519_public_key_t pubkey_in_state;
|
|
|
+ uint64_t rev_counter = 0;
|
|
|
+
|
|
|
+ tor_assert(service_found_out);
|
|
|
+ tor_assert(state_line);
|
|
|
+ tor_assert(blinded_pubkey);
|
|
|
+
|
|
|
+ /* Assume that the line is not for this service */
|
|
|
+ *service_found_out = 0;
|
|
|
+
|
|
|
+ /* Start parsing the state line */
|
|
|
+ items = smartlist_new();
|
|
|
+ smartlist_split_string(items, state_line, NULL,
|
|
|
+ SPLIT_SKIP_SPACE|SPLIT_IGNORE_BLANK, -1);
|
|
|
+ if (smartlist_len(items) < 2) {
|
|
|
+ log_warn(LD_GENERAL, "Incomplete rev counter line. Ignoring.");
|
|
|
+ goto done;
|
|
|
+ }
|
|
|
+
|
|
|
+ char *b64_key_str = smartlist_get(items, 0);
|
|
|
+ char *saved_rev_counter_str = smartlist_get(items, 1);
|
|
|
+
|
|
|
+ /* Parse blinded key to check if it's for this hidden service */
|
|
|
+ if (ed25519_public_from_base64(&pubkey_in_state, b64_key_str) < 0) {
|
|
|
+ log_warn(LD_GENERAL, "Unable to base64 key in revcount line. Ignoring.");
|
|
|
+ goto done;
|
|
|
+ }
|
|
|
+ /* State line not for this hidden service */
|
|
|
+ if (!ed25519_pubkey_eq(&pubkey_in_state, blinded_pubkey)) {
|
|
|
+ goto done;
|
|
|
+ }
|
|
|
+
|
|
|
+ rev_counter = tor_parse_uint64(saved_rev_counter_str,
|
|
|
+ 10, 0, UINT64_MAX, &ok, NULL);
|
|
|
+ if (!ok) {
|
|
|
+ log_warn(LD_GENERAL, "Unable to parse rev counter. Ignoring.");
|
|
|
+ goto done;
|
|
|
+ }
|
|
|
+
|
|
|
+ /* Since we got this far, the line was for this service */
|
|
|
+ *service_found_out = 1;
|
|
|
+
|
|
|
+ log_info(LD_GENERAL, "Found rev counter for %s: %" PRIu64,
|
|
|
+ b64_key_str, rev_counter);
|
|
|
+
|
|
|
+ done:
|
|
|
+ if (items) {
|
|
|
+ SMARTLIST_FOREACH(items, char*, s, tor_free(s));
|
|
|
+ smartlist_free(items);
|
|
|
+ }
|
|
|
+
|
|
|
+ return rev_counter;
|
|
|
+}
|
|
|
+
|
|
|
+/** Dig into our state file and find the current revision counter for the
|
|
|
+ * service with blinded key <b>blinded_pubkey</b>. If no revision counter is
|
|
|
+ * found, return 0. */
|
|
|
+static uint64_t
|
|
|
+get_rev_counter_for_service(const ed25519_public_key_t *blinded_pubkey)
|
|
|
+{
|
|
|
+ or_state_t *state = get_or_state();
|
|
|
+ config_line_t *line;
|
|
|
+
|
|
|
+ /* Set default value for rev counters (if not found) to 0 */
|
|
|
+ uint64_t final_rev_counter = 0;
|
|
|
+
|
|
|
+ for (line = state->HidServRevCounter ; line ; line = line->next) {
|
|
|
+ int service_found = 0;
|
|
|
+ uint64_t rev_counter = 0;
|
|
|
+
|
|
|
+ tor_assert(!strcmp(line->key, "HidServRevCounter"));
|
|
|
+
|
|
|
+ /* Scan all the HidServRevCounter lines till we find the line for this
|
|
|
+ service: */
|
|
|
+ rev_counter = check_state_line_for_service_rev_counter(line->value,
|
|
|
+ blinded_pubkey,
|
|
|
+ &service_found);
|
|
|
+ if (service_found) {
|
|
|
+ final_rev_counter = rev_counter;
|
|
|
+ goto done;
|
|
|
}
|
|
|
+ }
|
|
|
|
|
|
- tor_assert(encoded_len > ED25519_SIG_LEN);
|
|
|
+ done:
|
|
|
+ return final_rev_counter;
|
|
|
+}
|
|
|
|
|
|
- if (ed25519_sign_prefixed(&sig,
|
|
|
- cell_bytes_tmp,
|
|
|
- encoded_len -
|
|
|
- (ED25519_SIG_LEN + sizeof(cell->sig_len)),
|
|
|
- ESTABLISH_INTRO_SIG_PREFIX,
|
|
|
- &key_struct)) {
|
|
|
- log_warn(LD_BUG, "Unable to gen signature for ESTABLISH_INTRO cell.");
|
|
|
- goto err;
|
|
|
+/** Update the value of the revision counter for <b>hs_desc</b> and save it on
|
|
|
+ our state file. */
|
|
|
+static void
|
|
|
+increment_descriptor_revision_counter(hs_descriptor_t *hs_desc)
|
|
|
+{
|
|
|
+ /* Find stored rev counter if it exists */
|
|
|
+ uint64_t rev_counter =
|
|
|
+ get_rev_counter_for_service(&hs_desc->plaintext_data.blinded_pubkey);
|
|
|
+
|
|
|
+ /* Increment the revision counter of <b>hs_desc</b> so the next update (which
|
|
|
+ * will trigger an upload) will have the right value. We do this at this
|
|
|
+ * stage to only do it once because a descriptor can have many updates before
|
|
|
+ * being uploaded. By doing it at upload, we are sure to only increment by 1
|
|
|
+ * and thus avoid leaking how many operations we made on the descriptor from
|
|
|
+ * the previous one before uploading. */
|
|
|
+ rev_counter++;
|
|
|
+ hs_desc->plaintext_data.revision_counter = rev_counter;
|
|
|
+
|
|
|
+ update_revision_counters_in_state();
|
|
|
+}
|
|
|
+
|
|
|
+/** Set the revision counter in <b>hs_desc</b>, using the state file to find
|
|
|
+ * the current counter value if it exists. */
|
|
|
+static void
|
|
|
+set_descriptor_revision_counter(hs_descriptor_t *hs_desc)
|
|
|
+{
|
|
|
+ /* Find stored rev counter if it exists */
|
|
|
+ uint64_t rev_counter =
|
|
|
+ get_rev_counter_for_service(&hs_desc->plaintext_data.blinded_pubkey);
|
|
|
+
|
|
|
+ hs_desc->plaintext_data.revision_counter = rev_counter;
|
|
|
+}
|
|
|
+
|
|
|
+/* Encode and sign the service descriptor desc and upload it to the
|
|
|
+ * responsible hidden service directories. If for_next_period is true, the set
|
|
|
+ * of directories are selected using the next hsdir_index. This does nothing
|
|
|
+ * if PublishHidServDescriptors is false. */
|
|
|
+static void
|
|
|
+upload_descriptor_to_all(const hs_service_t *service,
|
|
|
+ hs_service_descriptor_t *desc, int for_next_period)
|
|
|
+{
|
|
|
+ smartlist_t *responsible_dirs = NULL;
|
|
|
+
|
|
|
+ tor_assert(service);
|
|
|
+ tor_assert(desc);
|
|
|
+
|
|
|
+ /* Get our list of responsible HSDir. */
|
|
|
+ responsible_dirs = smartlist_new();
|
|
|
+ /* The parameter 0 means that we aren't a client so tell the function to use
|
|
|
+ * the spread store consensus paremeter. */
|
|
|
+ hs_get_responsible_hsdirs(&desc->blinded_kp.pubkey, desc->time_period_num,
|
|
|
+ for_next_period, 0, responsible_dirs);
|
|
|
+
|
|
|
+ /* For each responsible HSDir we have, initiate an upload command. */
|
|
|
+ SMARTLIST_FOREACH_BEGIN(responsible_dirs, const routerstatus_t *,
|
|
|
+ hsdir_rs) {
|
|
|
+ const node_t *hsdir_node = node_get_by_id(hsdir_rs->identity_digest);
|
|
|
+ /* Getting responsible hsdir implies that the node_t object exists for the
|
|
|
+ * routerstatus_t found in the consensus else we have a problem. */
|
|
|
+ tor_assert(hsdir_node);
|
|
|
+ /* Do not upload to an HSDir we don't have a descriptor for. */
|
|
|
+ if (!node_has_descriptor(hsdir_node)) {
|
|
|
+ log_info(LD_REND, "Missing descriptor for HSDir %s. Not uploading "
|
|
|
+ "descriptor. We'll try later once we have it.",
|
|
|
+ safe_str_client(node_describe(hsdir_node)));
|
|
|
+ /* Once we get new directory information, this HSDir will be retried if
|
|
|
+ * we ever get the descriptor. */
|
|
|
+ smartlist_add(desc->hsdir_missing_info,
|
|
|
+ tor_memdup(hsdir_rs->identity_digest, DIGEST_LEN));
|
|
|
+ continue;
|
|
|
}
|
|
|
|
|
|
- /* And write the signature to the cell */
|
|
|
- uint8_t *sig_ptr = trn_cell_establish_intro_getarray_sig(cell);
|
|
|
- memcpy(sig_ptr, sig.sig, sig_len);
|
|
|
+ /* Upload this descriptor to the chosen directory. */
|
|
|
+ upload_descriptor_to_hsdir(service, desc, hsdir_node);
|
|
|
+ } SMARTLIST_FOREACH_END(hsdir_rs);
|
|
|
+
|
|
|
+ /* Set the next upload time for this descriptor. Even if we are configured
|
|
|
+ * to not upload, we still want to follow the right cycle of life for this
|
|
|
+ * descriptor. */
|
|
|
+ desc->next_upload_time =
|
|
|
+ (time(NULL) + crypto_rand_int_range(HS_SERVICE_NEXT_UPLOAD_TIME_MIN,
|
|
|
+ HS_SERVICE_NEXT_UPLOAD_TIME_MAX));
|
|
|
+ {
|
|
|
+ char fmt_next_time[ISO_TIME_LEN+1];
|
|
|
+ format_local_iso_time(fmt_next_time, desc->next_upload_time);
|
|
|
+ log_debug(LD_REND, "Service %s set to upload a descriptor at %s",
|
|
|
+ safe_str_client(service->onion_address), fmt_next_time);
|
|
|
}
|
|
|
|
|
|
- /* We are done! Return the cell! */
|
|
|
- return cell;
|
|
|
+ /* Update the revision counter of this descriptor */
|
|
|
+ increment_descriptor_revision_counter(desc->desc);
|
|
|
|
|
|
- err:
|
|
|
- trn_cell_establish_intro_free(cell);
|
|
|
- return NULL;
|
|
|
+ smartlist_free(responsible_dirs);
|
|
|
+ return;
|
|
|
+}
|
|
|
+
|
|
|
+/* Return 1 if the given descriptor from the given service can be uploaded
|
|
|
+ * else return 0 if it can not. */
|
|
|
+static int
|
|
|
+should_service_upload_descriptor(const hs_service_t *service,
|
|
|
+ const hs_service_descriptor_t *desc, time_t now)
|
|
|
+{
|
|
|
+ unsigned int num_intro_points;
|
|
|
+
|
|
|
+ tor_assert(service);
|
|
|
+ tor_assert(desc);
|
|
|
+
|
|
|
+ /* If this descriptors has missing intro points that is that it couldn't get
|
|
|
+ * them all when it was time to pick them, it means that we should upload
|
|
|
+ * instead of waiting an arbitrary amount of time breaking the service.
|
|
|
+ * Else, if we have no missing intro points, we use the value taken from the
|
|
|
+ * service configuration. */
|
|
|
+ if (desc->missing_intro_points) {
|
|
|
+ num_intro_points = digest256map_size(desc->intro_points.map);
|
|
|
+ } else {
|
|
|
+ num_intro_points = service->config.num_intro_points;
|
|
|
+ }
|
|
|
+
|
|
|
+ /* This means we tried to pick intro points but couldn't get any so do not
|
|
|
+ * upload descriptor in this case. We need at least one for the service to
|
|
|
+ * be reachable. */
|
|
|
+ if (desc->missing_intro_points && num_intro_points == 0) {
|
|
|
+ goto cannot;
|
|
|
+ }
|
|
|
+
|
|
|
+ /* Check if all our introduction circuit have been established for all the
|
|
|
+ * intro points we have selected. */
|
|
|
+ if (count_desc_circuit_established(desc) != num_intro_points) {
|
|
|
+ goto cannot;
|
|
|
+ }
|
|
|
+
|
|
|
+ /* Is it the right time to upload? */
|
|
|
+ if (desc->next_upload_time > now) {
|
|
|
+ goto cannot;
|
|
|
+ }
|
|
|
+
|
|
|
+ /* Can upload! */
|
|
|
+ return 1;
|
|
|
+ cannot:
|
|
|
+ return 0;
|
|
|
+}
|
|
|
+
|
|
|
+/* Scheduled event run from the main loop. Try to upload the descriptor for
|
|
|
+ * each service. */
|
|
|
+STATIC void
|
|
|
+run_upload_descriptor_event(time_t now)
|
|
|
+{
|
|
|
+ /* v2 services use the same function for descriptor creation and upload so
|
|
|
+ * we do everything here because the intro circuits were checked before. */
|
|
|
+ if (rend_num_services() > 0) {
|
|
|
+ rend_consider_services_upload(now);
|
|
|
+ rend_consider_descriptor_republication();
|
|
|
+ }
|
|
|
+
|
|
|
+ /* Run v3+ check. */
|
|
|
+ FOR_EACH_SERVICE_BEGIN(service) {
|
|
|
+ FOR_EACH_DESCRIPTOR_BEGIN(service, desc) {
|
|
|
+ int for_next_period = 0;
|
|
|
+
|
|
|
+ /* Can this descriptor be uploaed? */
|
|
|
+ if (!should_service_upload_descriptor(service, desc, now)) {
|
|
|
+ continue;
|
|
|
+ }
|
|
|
+
|
|
|
+ log_info(LD_REND, "Initiating upload for hidden service %s descriptor "
|
|
|
+ "for service %s with %u/%u introduction points%s.",
|
|
|
+ (desc == service->desc_current) ? "current" : "next",
|
|
|
+ safe_str_client(service->onion_address),
|
|
|
+ digest256map_size(desc->intro_points.map),
|
|
|
+ service->config.num_intro_points,
|
|
|
+ (desc->missing_intro_points) ? " (couldn't pick more)" : "");
|
|
|
+
|
|
|
+ /* At this point, we have to upload the descriptor so start by building
|
|
|
+ * the intro points descriptor section which we are now sure to be
|
|
|
+ * accurate because all circuits have been established. */
|
|
|
+ build_desc_intro_points(service, desc, now);
|
|
|
+
|
|
|
+ /* If the service is in the overlap period and this descriptor is the
|
|
|
+ * next one, it has to be uploaded for the next time period meaning
|
|
|
+ * we'll use the next node_t hsdir_index to pick the HSDirs. */
|
|
|
+ if (desc == service->desc_next) {
|
|
|
+ for_next_period = 1;
|
|
|
+ }
|
|
|
+ upload_descriptor_to_all(service, desc, for_next_period);
|
|
|
+ } FOR_EACH_DESCRIPTOR_END;
|
|
|
+ } FOR_EACH_SERVICE_END;
|
|
|
+}
|
|
|
+
|
|
|
+/* Called when the introduction point circuit is done building and ready to be
|
|
|
+ * used. */
|
|
|
+static void
|
|
|
+service_intro_circ_has_opened(origin_circuit_t *circ)
|
|
|
+{
|
|
|
+ hs_service_t *service = NULL;
|
|
|
+ hs_service_intro_point_t *ip = NULL;
|
|
|
+ hs_service_descriptor_t *desc = NULL;
|
|
|
+
|
|
|
+ tor_assert(circ);
|
|
|
+
|
|
|
+ /* Let's do some basic sanity checking of the circ state */
|
|
|
+ if (BUG(!circ->cpath)) {
|
|
|
+ return;
|
|
|
+ }
|
|
|
+ if (BUG(TO_CIRCUIT(circ)->purpose != CIRCUIT_PURPOSE_S_ESTABLISH_INTRO)) {
|
|
|
+ return;
|
|
|
+ }
|
|
|
+ if (BUG(!circ->hs_ident)) {
|
|
|
+ return;
|
|
|
+ }
|
|
|
+
|
|
|
+ /* Get the corresponding service and intro point. */
|
|
|
+ get_objects_from_ident(circ->hs_ident, &service, &ip, &desc);
|
|
|
+
|
|
|
+ if (service == NULL) {
|
|
|
+ log_warn(LD_REND, "Unknown service identity key %s on the introduction "
|
|
|
+ "circuit %u. Can't find onion service.",
|
|
|
+ safe_str_client(ed25519_fmt(&circ->hs_ident->identity_pk)),
|
|
|
+ TO_CIRCUIT(circ)->n_circ_id);
|
|
|
+ goto err;
|
|
|
+ }
|
|
|
+ if (ip == NULL) {
|
|
|
+ log_warn(LD_REND, "Unknown introduction point auth key on circuit %u "
|
|
|
+ "for service %s",
|
|
|
+ TO_CIRCUIT(circ)->n_circ_id,
|
|
|
+ safe_str_client(service->onion_address));
|
|
|
+ goto err;
|
|
|
+ }
|
|
|
+ /* We can't have an IP object without a descriptor. */
|
|
|
+ tor_assert(desc);
|
|
|
+
|
|
|
+ if (hs_circ_service_intro_has_opened(service, ip, desc, circ)) {
|
|
|
+ /* Getting here means that the circuit has been re-purposed because we
|
|
|
+ * have enough intro circuit opened. Remove the IP from the service. */
|
|
|
+ service_intro_point_remove(service, ip);
|
|
|
+ service_intro_point_free(ip);
|
|
|
+ }
|
|
|
+
|
|
|
+ goto done;
|
|
|
+
|
|
|
+ err:
|
|
|
+ /* Close circuit, we can't use it. */
|
|
|
+ circuit_mark_for_close(TO_CIRCUIT(circ), END_CIRC_REASON_NOSUCHSERVICE);
|
|
|
+ done:
|
|
|
+ return;
|
|
|
+}
|
|
|
+
|
|
|
+/* Called when a rendezvous circuit is done building and ready to be used. */
|
|
|
+static void
|
|
|
+service_rendezvous_circ_has_opened(origin_circuit_t *circ)
|
|
|
+{
|
|
|
+ hs_service_t *service = NULL;
|
|
|
+
|
|
|
+ tor_assert(circ);
|
|
|
+ tor_assert(circ->cpath);
|
|
|
+ /* Getting here means this is a v3 rendezvous circuit. */
|
|
|
+ tor_assert(circ->hs_ident);
|
|
|
+ tor_assert(TO_CIRCUIT(circ)->purpose == CIRCUIT_PURPOSE_S_CONNECT_REND);
|
|
|
+
|
|
|
+ /* Declare the circuit dirty to avoid reuse, and for path-bias */
|
|
|
+ if (!TO_CIRCUIT(circ)->timestamp_dirty)
|
|
|
+ TO_CIRCUIT(circ)->timestamp_dirty = time(NULL);
|
|
|
+ pathbias_count_use_attempt(circ);
|
|
|
+
|
|
|
+ /* Get the corresponding service and intro point. */
|
|
|
+ get_objects_from_ident(circ->hs_ident, &service, NULL, NULL);
|
|
|
+ if (service == NULL) {
|
|
|
+ log_warn(LD_REND, "Unknown service identity key %s on the rendezvous "
|
|
|
+ "circuit %u with cookie %s. Can't find onion service.",
|
|
|
+ safe_str_client(ed25519_fmt(&circ->hs_ident->identity_pk)),
|
|
|
+ TO_CIRCUIT(circ)->n_circ_id,
|
|
|
+ hex_str((const char *) circ->hs_ident->rendezvous_cookie,
|
|
|
+ REND_COOKIE_LEN));
|
|
|
+ goto err;
|
|
|
+ }
|
|
|
+
|
|
|
+ /* If the cell can't be sent, the circuit will be closed within this
|
|
|
+ * function. */
|
|
|
+ hs_circ_service_rp_has_opened(service, circ);
|
|
|
+ goto done;
|
|
|
+
|
|
|
+ err:
|
|
|
+ circuit_mark_for_close(TO_CIRCUIT(circ), END_CIRC_REASON_NOSUCHSERVICE);
|
|
|
+ done:
|
|
|
+ return;
|
|
|
+}
|
|
|
+
|
|
|
+/* We've been expecting an INTRO_ESTABLISHED cell on this circuit and it just
|
|
|
+ * arrived. Handle the INTRO_ESTABLISHED cell arriving on the given
|
|
|
+ * introduction circuit. Return 0 on success else a negative value. */
|
|
|
+static int
|
|
|
+service_handle_intro_established(origin_circuit_t *circ,
|
|
|
+ const uint8_t *payload,
|
|
|
+ size_t payload_len)
|
|
|
+{
|
|
|
+ hs_service_t *service = NULL;
|
|
|
+ hs_service_intro_point_t *ip = NULL;
|
|
|
+
|
|
|
+ tor_assert(circ);
|
|
|
+ tor_assert(payload);
|
|
|
+ tor_assert(TO_CIRCUIT(circ)->purpose == CIRCUIT_PURPOSE_S_ESTABLISH_INTRO);
|
|
|
+
|
|
|
+ /* We need the service and intro point for this cell. */
|
|
|
+ get_objects_from_ident(circ->hs_ident, &service, &ip, NULL);
|
|
|
+
|
|
|
+ /* Get service object from the circuit identifier. */
|
|
|
+ if (service == NULL) {
|
|
|
+ log_warn(LD_REND, "Unknown service identity key %s on the introduction "
|
|
|
+ "circuit %u. Can't find onion service.",
|
|
|
+ safe_str_client(ed25519_fmt(&circ->hs_ident->identity_pk)),
|
|
|
+ TO_CIRCUIT(circ)->n_circ_id);
|
|
|
+ goto err;
|
|
|
+ }
|
|
|
+ if (ip == NULL) {
|
|
|
+ /* We don't recognize the key. */
|
|
|
+ log_warn(LD_REND, "Introduction circuit established without an intro "
|
|
|
+ "point object on circuit %u for service %s",
|
|
|
+ TO_CIRCUIT(circ)->n_circ_id,
|
|
|
+ safe_str_client(service->onion_address));
|
|
|
+ goto err;
|
|
|
+ }
|
|
|
+
|
|
|
+ /* Try to parse the payload into a cell making sure we do actually have a
|
|
|
+ * valid cell. On success, the ip object and circuit purpose is updated to
|
|
|
+ * reflect the fact that the introduction circuit is established. */
|
|
|
+ if (hs_circ_handle_intro_established(service, ip, circ, payload,
|
|
|
+ payload_len) < 0) {
|
|
|
+ goto err;
|
|
|
+ }
|
|
|
+
|
|
|
+ /* Flag that we have an established circuit for this intro point. This value
|
|
|
+ * is what indicates the upload scheduled event if we are ready to build the
|
|
|
+ * intro point into the descriptor and upload. */
|
|
|
+ ip->circuit_established = 1;
|
|
|
+
|
|
|
+ log_info(LD_REND, "Successfully received an INTRO_ESTABLISHED cell "
|
|
|
+ "on circuit %u for service %s",
|
|
|
+ TO_CIRCUIT(circ)->n_circ_id,
|
|
|
+ safe_str_client(service->onion_address));
|
|
|
+ return 0;
|
|
|
+
|
|
|
+ err:
|
|
|
+ return -1;
|
|
|
+}
|
|
|
+
|
|
|
+/* We just received an INTRODUCE2 cell on the established introduction circuit
|
|
|
+ * circ. Handle the cell and return 0 on success else a negative value. */
|
|
|
+static int
|
|
|
+service_handle_introduce2(origin_circuit_t *circ, const uint8_t *payload,
|
|
|
+ size_t payload_len)
|
|
|
+{
|
|
|
+ hs_service_t *service = NULL;
|
|
|
+ hs_service_intro_point_t *ip = NULL;
|
|
|
+ hs_service_descriptor_t *desc = NULL;
|
|
|
+
|
|
|
+ tor_assert(circ);
|
|
|
+ tor_assert(payload);
|
|
|
+ tor_assert(TO_CIRCUIT(circ)->purpose == CIRCUIT_PURPOSE_S_INTRO);
|
|
|
+
|
|
|
+ /* We'll need every object associated with this circuit. */
|
|
|
+ get_objects_from_ident(circ->hs_ident, &service, &ip, &desc);
|
|
|
+
|
|
|
+ /* Get service object from the circuit identifier. */
|
|
|
+ if (service == NULL) {
|
|
|
+ log_warn(LD_BUG, "Unknown service identity key %s when handling "
|
|
|
+ "an INTRODUCE2 cell on circuit %u",
|
|
|
+ safe_str_client(ed25519_fmt(&circ->hs_ident->identity_pk)),
|
|
|
+ TO_CIRCUIT(circ)->n_circ_id);
|
|
|
+ goto err;
|
|
|
+ }
|
|
|
+ if (ip == NULL) {
|
|
|
+ /* We don't recognize the key. */
|
|
|
+ log_warn(LD_BUG, "Unknown introduction auth key when handling "
|
|
|
+ "an INTRODUCE2 cell on circuit %u for service %s",
|
|
|
+ TO_CIRCUIT(circ)->n_circ_id,
|
|
|
+ safe_str_client(service->onion_address));
|
|
|
+ goto err;
|
|
|
+ }
|
|
|
+ /* If we have an IP object, we MUST have a descriptor object. */
|
|
|
+ tor_assert(desc);
|
|
|
+
|
|
|
+ /* The following will parse, decode and launch the rendezvous point circuit.
|
|
|
+ * Both current and legacy cells are handled. */
|
|
|
+ if (hs_circ_handle_introduce2(service, circ, ip, desc->desc->subcredential,
|
|
|
+ payload, payload_len) < 0) {
|
|
|
+ goto err;
|
|
|
+ }
|
|
|
+
|
|
|
+ return 0;
|
|
|
+ err:
|
|
|
+ return -1;
|
|
|
+}
|
|
|
+
|
|
|
+/* For a given service and a descriptor of that service, consider retrying to
|
|
|
+ * upload the descriptor to any directories from which we had missing
|
|
|
+ * information when originally tried to be uploaded. This is called when our
|
|
|
+ * directory information has changed. */
|
|
|
+static void
|
|
|
+consider_hsdir_upload_retry(const hs_service_t *service,
|
|
|
+ hs_service_descriptor_t *desc)
|
|
|
+{
|
|
|
+ smartlist_t *responsible_dirs = NULL;
|
|
|
+ smartlist_t *still_missing_dirs = NULL;
|
|
|
+
|
|
|
+ tor_assert(service);
|
|
|
+ tor_assert(desc);
|
|
|
+
|
|
|
+ responsible_dirs = smartlist_new();
|
|
|
+ still_missing_dirs = smartlist_new();
|
|
|
+
|
|
|
+ /* We first need to get responsible directories from the latest consensus so
|
|
|
+ * we can then make sure that the node that we were missing information for
|
|
|
+ * is still responsible for this descriptor. */
|
|
|
+ hs_get_responsible_hsdirs(&desc->blinded_kp.pubkey, desc->time_period_num,
|
|
|
+ service->desc_next == desc, 0, responsible_dirs);
|
|
|
+
|
|
|
+ SMARTLIST_FOREACH_BEGIN(responsible_dirs, const routerstatus_t *, rs) {
|
|
|
+ const node_t *node;
|
|
|
+ const char *id = rs->identity_digest;
|
|
|
+ if (!smartlist_contains_digest(desc->hsdir_missing_info, id)) {
|
|
|
+ continue;
|
|
|
+ }
|
|
|
+ /* We do need a node_t object and descriptor to perform an upload. If
|
|
|
+ * found, we remove the id from the missing dir list else we add it to the
|
|
|
+ * still missing dir list to keep track of id that are still missing. */
|
|
|
+ node = node_get_by_id(id);
|
|
|
+ if (node && node_has_descriptor(node)) {
|
|
|
+ upload_descriptor_to_hsdir(service, desc, node);
|
|
|
+ smartlist_remove(desc->hsdir_missing_info, id);
|
|
|
+ } else {
|
|
|
+ smartlist_add(still_missing_dirs, tor_memdup(id, DIGEST_LEN));
|
|
|
+ }
|
|
|
+ } SMARTLIST_FOREACH_END(rs);
|
|
|
+
|
|
|
+ /* Switch the still missing dir list with the current missing dir list in
|
|
|
+ * the descriptor. It is possible that the list ends up empty which is what
|
|
|
+ * we want if we have no more missing dir. */
|
|
|
+ SMARTLIST_FOREACH(desc->hsdir_missing_info, char *, id, tor_free(id));
|
|
|
+ smartlist_free(desc->hsdir_missing_info);
|
|
|
+ desc->hsdir_missing_info = still_missing_dirs;
|
|
|
+
|
|
|
+ /* No ownership of the routerstatus_t object in this list. */
|
|
|
+ smartlist_free(responsible_dirs);
|
|
|
+}
|
|
|
+
|
|
|
+/* Add to list every filename used by service. This is used by the sandbox
|
|
|
+ * subsystem. */
|
|
|
+static void
|
|
|
+service_add_fnames_to_list(const hs_service_t *service, smartlist_t *list)
|
|
|
+{
|
|
|
+ const char *s_dir;
|
|
|
+ char fname[128] = {0};
|
|
|
+
|
|
|
+ tor_assert(service);
|
|
|
+ tor_assert(list);
|
|
|
+
|
|
|
+ /* Ease our life. */
|
|
|
+ s_dir = service->config.directory_path;
|
|
|
+ /* The hostname file. */
|
|
|
+ smartlist_add(list, hs_path_from_filename(s_dir, fname_hostname));
|
|
|
+ /* The key files splitted in two. */
|
|
|
+ tor_snprintf(fname, sizeof(fname), "%s_secret_key", fname_keyfile_prefix);
|
|
|
+ smartlist_add(list, hs_path_from_filename(s_dir, fname));
|
|
|
+ tor_snprintf(fname, sizeof(fname), "%s_public_key", fname_keyfile_prefix);
|
|
|
+ smartlist_add(list, hs_path_from_filename(s_dir, fname));
|
|
|
+}
|
|
|
+
|
|
|
+/* ========== */
|
|
|
+/* Public API */
|
|
|
+/* ========== */
|
|
|
+
|
|
|
+/* Return the number of service we have configured and usable. */
|
|
|
+unsigned int
|
|
|
+hs_service_get_num_services(void)
|
|
|
+{
|
|
|
+ if (hs_service_map == NULL) {
|
|
|
+ return 0;
|
|
|
+ }
|
|
|
+ return HT_SIZE(hs_service_map);
|
|
|
+}
|
|
|
+
|
|
|
+/* Called once an introduction circuit is closed. If the circuit doesn't have
|
|
|
+ * a v3 identifier, it is ignored. */
|
|
|
+void
|
|
|
+hs_service_intro_circ_has_closed(origin_circuit_t *circ)
|
|
|
+{
|
|
|
+ hs_service_t *service = NULL;
|
|
|
+ hs_service_intro_point_t *ip = NULL;
|
|
|
+ hs_service_descriptor_t *desc = NULL;
|
|
|
+
|
|
|
+ tor_assert(circ);
|
|
|
+
|
|
|
+ if (circ->hs_ident == NULL) {
|
|
|
+ /* This is not a v3 circuit, ignore. */
|
|
|
+ goto end;
|
|
|
+ }
|
|
|
+
|
|
|
+ get_objects_from_ident(circ->hs_ident, &service, &ip, &desc);
|
|
|
+ if (service == NULL) {
|
|
|
+ log_warn(LD_REND, "Unable to find any hidden service associated "
|
|
|
+ "identity key %s on intro circuit %u.",
|
|
|
+ ed25519_fmt(&circ->hs_ident->identity_pk),
|
|
|
+ TO_CIRCUIT(circ)->n_circ_id);
|
|
|
+ goto end;
|
|
|
+ }
|
|
|
+ if (ip == NULL) {
|
|
|
+ /* The introduction point object has already been removed probably by our
|
|
|
+ * cleanup process so ignore. */
|
|
|
+ goto end;
|
|
|
+ }
|
|
|
+ /* Can't have an intro point object without a descriptor. */
|
|
|
+ tor_assert(desc);
|
|
|
+
|
|
|
+ /* Circuit disappeared so make sure the intro point is updated. By
|
|
|
+ * keeping the object in the descriptor, we'll be able to retry. */
|
|
|
+ ip->circuit_established = 0;
|
|
|
+
|
|
|
+ /* We've retried too many times, remember it as a failed intro point so we
|
|
|
+ * don't pick it up again. It will be retried in INTRO_CIRC_RETRY_PERIOD
|
|
|
+ * seconds. */
|
|
|
+ if (ip->circuit_retries > MAX_INTRO_POINT_CIRCUIT_RETRIES) {
|
|
|
+ remember_failing_intro_point(ip, desc, approx_time());
|
|
|
+ service_intro_point_remove(service, ip);
|
|
|
+ service_intro_point_free(ip);
|
|
|
+ }
|
|
|
+
|
|
|
+ end:
|
|
|
+ return;
|
|
|
+}
|
|
|
+
|
|
|
+/* Given conn, a rendezvous edge connection acting as an exit stream, look up
|
|
|
+ * the hidden service for the circuit circ, and look up the port and address
|
|
|
+ * based on the connection port. Assign the actual connection address.
|
|
|
+ *
|
|
|
+ * Return 0 on success. Return -1 on failure and the caller should NOT close
|
|
|
+ * the circuit. Return -2 on failure and the caller MUST close the circuit for
|
|
|
+ * security reasons. */
|
|
|
+int
|
|
|
+hs_service_set_conn_addr_port(const origin_circuit_t *circ,
|
|
|
+ edge_connection_t *conn)
|
|
|
+{
|
|
|
+ hs_service_t *service = NULL;
|
|
|
+
|
|
|
+ tor_assert(circ);
|
|
|
+ tor_assert(conn);
|
|
|
+ tor_assert(TO_CIRCUIT(circ)->purpose == CIRCUIT_PURPOSE_S_REND_JOINED);
|
|
|
+ tor_assert(circ->hs_ident);
|
|
|
+
|
|
|
+ get_objects_from_ident(circ->hs_ident, &service, NULL, NULL);
|
|
|
+
|
|
|
+ if (service == NULL) {
|
|
|
+ log_warn(LD_REND, "Unable to find any hidden service associated "
|
|
|
+ "identity key %s on rendezvous circuit %u.",
|
|
|
+ ed25519_fmt(&circ->hs_ident->identity_pk),
|
|
|
+ TO_CIRCUIT(circ)->n_circ_id);
|
|
|
+ /* We want the caller to close the circuit because it's not a valid
|
|
|
+ * service so no danger. Attempting to bruteforce the entire key space by
|
|
|
+ * opening circuits to learn which service is being hosted here is
|
|
|
+ * impractical. */
|
|
|
+ goto err_close;
|
|
|
+ }
|
|
|
+
|
|
|
+ /* Enforce the streams-per-circuit limit, and refuse to provide a mapping if
|
|
|
+ * this circuit will exceed the limit. */
|
|
|
+ if (service->config.max_streams_per_rdv_circuit > 0 &&
|
|
|
+ (circ->hs_ident->num_rdv_streams >=
|
|
|
+ service->config.max_streams_per_rdv_circuit)) {
|
|
|
+#define MAX_STREAM_WARN_INTERVAL 600
|
|
|
+ static struct ratelim_t stream_ratelim =
|
|
|
+ RATELIM_INIT(MAX_STREAM_WARN_INTERVAL);
|
|
|
+ log_fn_ratelim(&stream_ratelim, LOG_WARN, LD_REND,
|
|
|
+ "Maximum streams per circuit limit reached on "
|
|
|
+ "rendezvous circuit %u for service %s. Circuit has "
|
|
|
+ "%" PRIu64 " out of %" PRIu64 " streams. %s.",
|
|
|
+ TO_CIRCUIT(circ)->n_circ_id,
|
|
|
+ service->onion_address,
|
|
|
+ circ->hs_ident->num_rdv_streams,
|
|
|
+ service->config.max_streams_per_rdv_circuit,
|
|
|
+ service->config.max_streams_close_circuit ?
|
|
|
+ "Closing circuit" : "Ignoring open stream request");
|
|
|
+ if (service->config.max_streams_close_circuit) {
|
|
|
+ /* Service explicitly configured to close immediately. */
|
|
|
+ goto err_close;
|
|
|
+ }
|
|
|
+ /* Exceeding the limit makes tor silently ignore the stream creation
|
|
|
+ * request and keep the circuit open. */
|
|
|
+ goto err_no_close;
|
|
|
+ }
|
|
|
+
|
|
|
+ /* Find a virtual port of that service mathcing the one in the connection if
|
|
|
+ * succesful, set the address in the connection. */
|
|
|
+ if (hs_set_conn_addr_port(service->config.ports, conn) < 0) {
|
|
|
+ log_info(LD_REND, "No virtual port mapping exists for port %d for "
|
|
|
+ "hidden service %s.",
|
|
|
+ TO_CONN(conn)->port, service->onion_address);
|
|
|
+ if (service->config.allow_unknown_ports) {
|
|
|
+ /* Service explicitly allow connection to unknown ports so close right
|
|
|
+ * away because we do not care about port mapping. */
|
|
|
+ goto err_close;
|
|
|
+ }
|
|
|
+ /* If the service didn't explicitly allow it, we do NOT close the circuit
|
|
|
+ * here to raise the bar in terms of performance for port mapping. */
|
|
|
+ goto err_no_close;
|
|
|
+ }
|
|
|
+
|
|
|
+ /* Success. */
|
|
|
+ return 0;
|
|
|
+ err_close:
|
|
|
+ /* Indicate the caller that the circuit should be closed. */
|
|
|
+ return -2;
|
|
|
+ err_no_close:
|
|
|
+ /* Indicate the caller to NOT close the circuit. */
|
|
|
+ return -1;
|
|
|
+}
|
|
|
+
|
|
|
+/* Add to file_list every filename used by a configured hidden service, and to
|
|
|
+ * dir_list every directory path used by a configured hidden service. This is
|
|
|
+ * used by the sandbox subsystem to whitelist those. */
|
|
|
+void
|
|
|
+hs_service_lists_fnames_for_sandbox(smartlist_t *file_list,
|
|
|
+ smartlist_t *dir_list)
|
|
|
+{
|
|
|
+ tor_assert(file_list);
|
|
|
+ tor_assert(dir_list);
|
|
|
+
|
|
|
+ /* Add files and dirs for legacy services. */
|
|
|
+ rend_services_add_filenames_to_lists(file_list, dir_list);
|
|
|
+
|
|
|
+ /* Add files and dirs for v3+. */
|
|
|
+ FOR_EACH_SERVICE_BEGIN(service) {
|
|
|
+ /* Skip ephemeral service, they don't touch the disk. */
|
|
|
+ if (service->config.is_ephemeral) {
|
|
|
+ continue;
|
|
|
+ }
|
|
|
+ service_add_fnames_to_list(service, file_list);
|
|
|
+ smartlist_add_strdup(dir_list, service->config.directory_path);
|
|
|
+ } FOR_EACH_DESCRIPTOR_END;
|
|
|
+}
|
|
|
+
|
|
|
+/* Called when our internal view of the directory has changed. We might have
|
|
|
+ * new descriptors for hidden service directories that we didn't have before
|
|
|
+ * so try them if it's the case. */
|
|
|
+void
|
|
|
+hs_service_dir_info_changed(void)
|
|
|
+{
|
|
|
+ /* For each service we have, check every descriptor and consider retrying to
|
|
|
+ * upload it to directories that we might have had missing information
|
|
|
+ * previously that is missing a router descriptor. */
|
|
|
+ FOR_EACH_SERVICE_BEGIN(service) {
|
|
|
+ FOR_EACH_DESCRIPTOR_BEGIN(service, desc) {
|
|
|
+ /* This cleans up the descriptor missing hsdir information list if a
|
|
|
+ * successful upload is made or if any of the directory aren't
|
|
|
+ * responsible anymore for the service descriptor. */
|
|
|
+ consider_hsdir_upload_retry(service, desc);
|
|
|
+ } FOR_EACH_DESCRIPTOR_END;
|
|
|
+ } FOR_EACH_SERVICE_END;
|
|
|
+}
|
|
|
+
|
|
|
+/* Called when we get an INTRODUCE2 cell on the circ. Respond to the cell and
|
|
|
+ * launch a circuit to the rendezvous point. */
|
|
|
+int
|
|
|
+hs_service_receive_introduce2(origin_circuit_t *circ, const uint8_t *payload,
|
|
|
+ size_t payload_len)
|
|
|
+{
|
|
|
+ int ret = -1;
|
|
|
+
|
|
|
+ tor_assert(circ);
|
|
|
+ tor_assert(payload);
|
|
|
+
|
|
|
+ /* Do some initial validation and logging before we parse the cell */
|
|
|
+ if (TO_CIRCUIT(circ)->purpose != CIRCUIT_PURPOSE_S_INTRO) {
|
|
|
+ log_warn(LD_PROTOCOL, "Received an INTRODUCE2 cell on a "
|
|
|
+ "non introduction circuit of purpose %d",
|
|
|
+ TO_CIRCUIT(circ)->purpose);
|
|
|
+ goto done;
|
|
|
+ }
|
|
|
+
|
|
|
+ if (circ->hs_ident) {
|
|
|
+ ret = service_handle_introduce2(circ, payload, payload_len);
|
|
|
+ } else {
|
|
|
+ ret = rend_service_receive_introduction(circ, payload, payload_len);
|
|
|
+ }
|
|
|
+
|
|
|
+ done:
|
|
|
+ return ret;
|
|
|
+}
|
|
|
+
|
|
|
+/* Called when we get an INTRO_ESTABLISHED cell. Mark the circuit as an
|
|
|
+ * established introduction point. Return 0 on success else a negative value
|
|
|
+ * and the circuit is closed. */
|
|
|
+int
|
|
|
+hs_service_receive_intro_established(origin_circuit_t *circ,
|
|
|
+ const uint8_t *payload,
|
|
|
+ size_t payload_len)
|
|
|
+{
|
|
|
+ int ret = -1;
|
|
|
+
|
|
|
+ tor_assert(circ);
|
|
|
+ tor_assert(payload);
|
|
|
+
|
|
|
+ if (TO_CIRCUIT(circ)->purpose != CIRCUIT_PURPOSE_S_ESTABLISH_INTRO) {
|
|
|
+ log_warn(LD_PROTOCOL, "Received an INTRO_ESTABLISHED cell on a "
|
|
|
+ "non introduction circuit of purpose %d",
|
|
|
+ TO_CIRCUIT(circ)->purpose);
|
|
|
+ goto err;
|
|
|
+ }
|
|
|
+
|
|
|
+ /* Handle both version. v2 uses rend_data and v3 uses the hs circuit
|
|
|
+ * identifier hs_ident. Can't be both. */
|
|
|
+ if (circ->hs_ident) {
|
|
|
+ ret = service_handle_intro_established(circ, payload, payload_len);
|
|
|
+ } else {
|
|
|
+ ret = rend_service_intro_established(circ, payload, payload_len);
|
|
|
+ }
|
|
|
+
|
|
|
+ if (ret < 0) {
|
|
|
+ goto err;
|
|
|
+ }
|
|
|
+ return 0;
|
|
|
+ err:
|
|
|
+ circuit_mark_for_close(TO_CIRCUIT(circ), END_CIRC_REASON_TORPROTOCOL);
|
|
|
+ return -1;
|
|
|
+}
|
|
|
+
|
|
|
+/* Called when any kind of hidden service circuit is done building thus
|
|
|
+ * opened. This is the entry point from the circuit subsystem. */
|
|
|
+void
|
|
|
+hs_service_circuit_has_opened(origin_circuit_t *circ)
|
|
|
+{
|
|
|
+ tor_assert(circ);
|
|
|
+
|
|
|
+ /* Handle both version. v2 uses rend_data and v3 uses the hs circuit
|
|
|
+ * identifier hs_ident. Can't be both. */
|
|
|
+ switch (TO_CIRCUIT(circ)->purpose) {
|
|
|
+ case CIRCUIT_PURPOSE_S_ESTABLISH_INTRO:
|
|
|
+ if (circ->hs_ident) {
|
|
|
+ service_intro_circ_has_opened(circ);
|
|
|
+ } else {
|
|
|
+ rend_service_intro_has_opened(circ);
|
|
|
+ }
|
|
|
+ break;
|
|
|
+ case CIRCUIT_PURPOSE_S_CONNECT_REND:
|
|
|
+ if (circ->hs_ident) {
|
|
|
+ service_rendezvous_circ_has_opened(circ);
|
|
|
+ } else {
|
|
|
+ rend_service_rendezvous_has_opened(circ);
|
|
|
+ }
|
|
|
+ break;
|
|
|
+ default:
|
|
|
+ tor_assert(0);
|
|
|
+ }
|
|
|
+}
|
|
|
+
|
|
|
+/* Load and/or generate keys for all onion services including the client
|
|
|
+ * authorization if any. Return 0 on success, -1 on failure. */
|
|
|
+int
|
|
|
+hs_service_load_all_keys(void)
|
|
|
+{
|
|
|
+ /* Load v2 service keys if we have v2. */
|
|
|
+ if (rend_num_services() != 0) {
|
|
|
+ if (rend_service_load_all_keys(NULL) < 0) {
|
|
|
+ goto err;
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+ /* Load or/and generate them for v3+. */
|
|
|
+ SMARTLIST_FOREACH_BEGIN(hs_service_staging_list, hs_service_t *, service) {
|
|
|
+ /* Ignore ephemeral service, they already have their keys set. */
|
|
|
+ if (service->config.is_ephemeral) {
|
|
|
+ continue;
|
|
|
+ }
|
|
|
+ log_info(LD_REND, "Loading v3 onion service keys from %s",
|
|
|
+ service_escaped_dir(service));
|
|
|
+ if (load_service_keys(service) < 0) {
|
|
|
+ goto err;
|
|
|
+ }
|
|
|
+ /* XXX: Load/Generate client authorization keys. (#20700) */
|
|
|
+ } SMARTLIST_FOREACH_END(service);
|
|
|
+
|
|
|
+ /* Final step, the staging list contains service in a quiescent state that
|
|
|
+ * is ready to be used. Register them to the global map. Once this is over,
|
|
|
+ * the staging list will be cleaned up. */
|
|
|
+ register_all_services();
|
|
|
+
|
|
|
+ /* All keys have been loaded successfully. */
|
|
|
+ return 0;
|
|
|
+ err:
|
|
|
+ return -1;
|
|
|
+}
|
|
|
+
|
|
|
+/* Put all service object in the given service list. After this, the caller
|
|
|
+ * looses ownership of every elements in the list and responsible to free the
|
|
|
+ * list pointer. */
|
|
|
+void
|
|
|
+hs_service_stage_services(const smartlist_t *service_list)
|
|
|
+{
|
|
|
+ tor_assert(service_list);
|
|
|
+ /* This list is freed at registration time but this function can be called
|
|
|
+ * multiple time. */
|
|
|
+ if (hs_service_staging_list == NULL) {
|
|
|
+ hs_service_staging_list = smartlist_new();
|
|
|
+ }
|
|
|
+ /* Add all service object to our staging list. Caller is responsible for
|
|
|
+ * freeing the service_list. */
|
|
|
+ smartlist_add_all(hs_service_staging_list, service_list);
|
|
|
+}
|
|
|
+
|
|
|
+/* Allocate and initilize a service object. The service configuration will
|
|
|
+ * contain the default values. Return the newly allocated object pointer. This
|
|
|
+ * function can't fail. */
|
|
|
+hs_service_t *
|
|
|
+hs_service_new(const or_options_t *options)
|
|
|
+{
|
|
|
+ hs_service_t *service = tor_malloc_zero(sizeof(hs_service_t));
|
|
|
+ /* Set default configuration value. */
|
|
|
+ set_service_default_config(&service->config, options);
|
|
|
+ /* Set the default service version. */
|
|
|
+ service->config.version = HS_SERVICE_DEFAULT_VERSION;
|
|
|
+ /* Allocate the CLIENT_PK replay cache in service state. */
|
|
|
+ service->state.replay_cache_rend_cookie =
|
|
|
+ replaycache_new(REND_REPLAY_TIME_INTERVAL, REND_REPLAY_TIME_INTERVAL);
|
|
|
+ return service;
|
|
|
+}
|
|
|
+
|
|
|
+/* Free the given <b>service</b> object and all its content. This function
|
|
|
+ * also takes care of wiping service keys from memory. It is safe to pass a
|
|
|
+ * NULL pointer. */
|
|
|
+void
|
|
|
+hs_service_free(hs_service_t *service)
|
|
|
+{
|
|
|
+ if (service == NULL) {
|
|
|
+ return;
|
|
|
+ }
|
|
|
+
|
|
|
+ /* Free descriptors. Go over both descriptor with this loop. */
|
|
|
+ FOR_EACH_DESCRIPTOR_BEGIN(service, desc) {
|
|
|
+ service_descriptor_free(desc);
|
|
|
+ } FOR_EACH_DESCRIPTOR_END;
|
|
|
+
|
|
|
+ /* Free service configuration. */
|
|
|
+ service_clear_config(&service->config);
|
|
|
+
|
|
|
+ /* Free replay cache from state. */
|
|
|
+ if (service->state.replay_cache_rend_cookie) {
|
|
|
+ replaycache_free(service->state.replay_cache_rend_cookie);
|
|
|
+ }
|
|
|
+
|
|
|
+ /* Wipe service keys. */
|
|
|
+ memwipe(&service->keys.identity_sk, 0, sizeof(service->keys.identity_sk));
|
|
|
+
|
|
|
+ tor_free(service);
|
|
|
+}
|
|
|
+
|
|
|
+/* Periodic callback. Entry point from the main loop to the HS service
|
|
|
+ * subsystem. This is call every second. This is skipped if tor can't build a
|
|
|
+ * circuit or the network is disabled. */
|
|
|
+void
|
|
|
+hs_service_run_scheduled_events(time_t now)
|
|
|
+{
|
|
|
+ /* First thing we'll do here is to make sure our services are in a
|
|
|
+ * quiescent state for the scheduled events. */
|
|
|
+ run_housekeeping_event(now);
|
|
|
+
|
|
|
+ /* Order matters here. We first make sure the descriptor object for each
|
|
|
+ * service contains the latest data. Once done, we check if we need to open
|
|
|
+ * new introduction circuit. Finally, we try to upload the descriptor for
|
|
|
+ * each service. */
|
|
|
+
|
|
|
+ /* Make sure descriptors are up to date. */
|
|
|
+ run_build_descriptor_event(now);
|
|
|
+ /* Make sure services have enough circuits. */
|
|
|
+ run_build_circuit_event(now);
|
|
|
+ /* Upload the descriptors if needed/possible. */
|
|
|
+ run_upload_descriptor_event(now);
|
|
|
+}
|
|
|
+
|
|
|
+/* Initialize the service HS subsystem. */
|
|
|
+void
|
|
|
+hs_service_init(void)
|
|
|
+{
|
|
|
+ /* Should never be called twice. */
|
|
|
+ tor_assert(!hs_service_map);
|
|
|
+ tor_assert(!hs_service_staging_list);
|
|
|
+
|
|
|
+ /* v2 specific. */
|
|
|
+ rend_service_init();
|
|
|
+
|
|
|
+ hs_service_map = tor_malloc_zero(sizeof(struct hs_service_ht));
|
|
|
+ HT_INIT(hs_service_ht, hs_service_map);
|
|
|
+
|
|
|
+ hs_service_staging_list = smartlist_new();
|
|
|
+}
|
|
|
+
|
|
|
+/* Release all global storage of the hidden service subsystem. */
|
|
|
+void
|
|
|
+hs_service_free_all(void)
|
|
|
+{
|
|
|
+ rend_service_free_all();
|
|
|
+ service_free_all();
|
|
|
}
|
|
|
|
|
|
#ifdef TOR_UNIT_TESTS
|