Tor  0.4.4.0-alpha-dev
hs_client.c
Go to the documentation of this file.
1 /* Copyright (c) 2016-2020, The Tor Project, Inc. */
2 /* See LICENSE for licensing information */
3 
4 /**
5  * \file hs_client.c
6  * \brief Implement next generation hidden service client functionality
7  **/
8 
9 #define HS_CLIENT_PRIVATE
10 
11 #include "core/or/or.h"
12 #include "app/config/config.h"
13 #include "core/crypto/hs_ntor.h"
15 #include "core/or/circuitbuild.h"
16 #include "core/or/circuitlist.h"
17 #include "core/or/circuituse.h"
19 #include "core/or/reasons.h"
20 #include "feature/client/circpathbias.h"
23 #include "feature/hs/hs_cache.h"
24 #include "feature/hs/hs_cell.h"
25 #include "feature/hs/hs_circuit.h"
27 #include "feature/hs/hs_client.h"
28 #include "feature/hs/hs_control.h"
30 #include "feature/hs/hs_ident.h"
39 
43 #include "core/or/extend_info_st.h"
46 
47 /** Client-side authorizations for hidden services; map of service identity
48  * public key to hs_client_service_authorization_t *. */
49 static digest256map_t *client_auths = NULL;
50 
51 #include "trunnel/hs/cell_introduce1.h"
52 
53 /** Return a human-readable string for the client fetch status code. */
54 static const char *
56 {
57  switch (status) {
59  return "Internal error";
61  return "Descriptor fetch launched";
63  return "Already have descriptor";
65  return "No more HSDir available to query";
67  return "Fetching descriptors is not allowed";
69  return "Missing directory information";
71  return "Pending descriptor fetch";
72  default:
73  return "(Unknown client fetch status code)";
74  }
75 }
76 
77 /** Return true iff tor should close the SOCKS request(s) for the descriptor
78  * fetch that ended up with this given status code. */
79 static int
81 {
82  switch (status) {
84  /* No more HSDir to query, we can't complete the SOCKS request(s). */
86  /* The fetch triggered an internal error. */
88  /* Client is not allowed to fetch (FetchHidServDescriptors 0). */
89  goto close;
94  /* The rest doesn't require tor to close the SOCKS request(s). */
95  goto no_close;
96  }
97 
98  no_close:
99  return 0;
100  close:
101  return 1;
102 }
103 
104 /* Return a newly allocated list of all the entry connections that matches the
105  * given service identity pk. If service_identity_pk is NULL, all entry
106  * connections with an hs_ident are returned.
107  *
108  * Caller must free the returned list but does NOT have ownership of the
109  * object inside thus they have to remain untouched. */
110 static smartlist_t *
111 find_entry_conns(const ed25519_public_key_t *service_identity_pk)
112 {
113  time_t now = time(NULL);
114  smartlist_t *conns = NULL, *entry_conns = NULL;
115 
116  entry_conns = smartlist_new();
117 
118  conns = connection_list_by_type_state(CONN_TYPE_AP,
120  SMARTLIST_FOREACH_BEGIN(conns, connection_t *, base_conn) {
121  entry_connection_t *entry_conn = TO_ENTRY_CONN(base_conn);
122  const edge_connection_t *edge_conn = ENTRY_TO_EDGE_CONN(entry_conn);
123 
124  /* Only consider the entry connections that matches the service for which
125  * we just fetched its descriptor. */
126  if (!edge_conn->hs_ident ||
127  (service_identity_pk &&
128  !ed25519_pubkey_eq(service_identity_pk,
129  &edge_conn->hs_ident->identity_pk))) {
130  continue;
131  }
132  assert_connection_ok(base_conn, now);
133 
134  /* Validated! Add the entry connection to the list. */
135  smartlist_add(entry_conns, entry_conn);
136  } SMARTLIST_FOREACH_END(base_conn);
137 
138  /* We don't have ownership of the objects in this list. */
139  smartlist_free(conns);
140  return entry_conns;
141 }
142 
143 /* Cancel all descriptor fetches currently in progress. */
144 static void
145 cancel_descriptor_fetches(void)
146 {
147  smartlist_t *conns =
148  connection_list_by_type_purpose(CONN_TYPE_DIR, DIR_PURPOSE_FETCH_HSDESC);
149  SMARTLIST_FOREACH_BEGIN(conns, connection_t *, conn) {
150  const hs_ident_dir_conn_t *ident = TO_DIR_CONN(conn)->hs_ident;
151  if (BUG(ident == NULL)) {
152  /* A directory connection fetching a service descriptor can't have an
153  * empty hidden service identifier. */
154  continue;
155  }
156  log_debug(LD_REND, "Marking for close a directory connection fetching "
157  "a hidden service descriptor for service %s.",
158  safe_str_client(ed25519_fmt(&ident->identity_pk)));
159  connection_mark_for_close(conn);
160  } SMARTLIST_FOREACH_END(conn);
161 
162  /* No ownership of the objects in this list. */
163  smartlist_free(conns);
164  log_info(LD_REND, "Hidden service client descriptor fetches cancelled.");
165 }
166 
167 /** Get all connections that are waiting on a circuit and flag them back to
168  * waiting for a hidden service descriptor for the given service key
169  * service_identity_pk. */
170 static void
172 {
173  tor_assert(service_identity_pk);
174 
175  smartlist_t *conns =
176  connection_list_by_type_state(CONN_TYPE_AP, AP_CONN_STATE_CIRCUIT_WAIT);
177 
178  SMARTLIST_FOREACH_BEGIN(conns, connection_t *, conn) {
179  edge_connection_t *edge_conn;
180  if (BUG(!CONN_IS_EDGE(conn))) {
181  continue;
182  }
183  edge_conn = TO_EDGE_CONN(conn);
184  if (edge_conn->hs_ident &&
185  ed25519_pubkey_eq(&edge_conn->hs_ident->identity_pk,
186  service_identity_pk)) {
187  connection_ap_mark_as_waiting_for_renddesc(TO_ENTRY_CONN(conn));
188  }
189  } SMARTLIST_FOREACH_END(conn);
190 
191  smartlist_free(conns);
192 }
193 
194 /** Remove tracked HSDir requests from our history for this hidden service
195  * identity public key. */
196 static void
198 {
199  char base64_blinded_pk[ED25519_BASE64_LEN + 1];
200  ed25519_public_key_t blinded_pk;
201 
202  tor_assert(identity_pk);
203 
204  /* Get blinded pubkey of hidden service. It is possible that we just moved
205  * to a new time period meaning that we won't be able to purge the request
206  * from the previous time period. That is fine because they will expire at
207  * some point and we don't care about those anymore. */
208  hs_build_blinded_pubkey(identity_pk, NULL, 0,
209  hs_get_time_period_num(0), &blinded_pk);
210  ed25519_public_to_base64(base64_blinded_pk, &blinded_pk);
211  /* Purge last hidden service request from cache for this blinded key. */
213 }
214 
215 /** Return true iff there is at least one pending directory descriptor request
216  * for the service identity_pk. */
217 static int
219 {
220  int ret = 0;
221  smartlist_t *conns =
222  connection_list_by_type_purpose(CONN_TYPE_DIR, DIR_PURPOSE_FETCH_HSDESC);
223 
224  SMARTLIST_FOREACH_BEGIN(conns, connection_t *, conn) {
225  const hs_ident_dir_conn_t *ident = TO_DIR_CONN(conn)->hs_ident;
226  if (BUG(ident == NULL)) {
227  /* A directory connection fetching a service descriptor can't have an
228  * empty hidden service identifier. */
229  continue;
230  }
231  if (!ed25519_pubkey_eq(identity_pk, &ident->identity_pk)) {
232  continue;
233  }
234  ret = 1;
235  break;
236  } SMARTLIST_FOREACH_END(conn);
237 
238  /* No ownership of the objects in this list. */
239  smartlist_free(conns);
240  return ret;
241 }
242 
243 /** Helper function that changes the state of an entry connection to waiting
244  * for a circuit. For this to work properly, the connection timestamps are set
245  * to now and the connection is then marked as pending for a circuit. */
246 static void
248 {
249  tor_assert(conn);
250 
251  /* Because the connection can now proceed to opening circuit and ultimately
252  * connect to the service, reset those timestamp so the connection is
253  * considered "fresh" and can continue without being closed too early. */
254  conn->timestamp_created = now;
255  conn->timestamp_last_read_allowed = now;
256  conn->timestamp_last_write_allowed = now;
257  /* Change connection's state into waiting for a circuit. */
259 
260  connection_ap_mark_as_pending_circuit(TO_ENTRY_CONN(conn));
261 }
262 
263 /** We failed to fetch a descriptor for the service with <b>identity_pk</b>
264  * because of <b>status</b>. Find all pending SOCKS connections for this
265  * service that are waiting on the descriptor and close them with
266  * <b>reason</b>. */
267 static void
270  int reason)
271 {
272  unsigned int count = 0;
273  smartlist_t *entry_conns = find_entry_conns(identity_pk);
274 
275  SMARTLIST_FOREACH_BEGIN(entry_conns, entry_connection_t *, entry_conn) {
276  /* Unattach the entry connection which will close for the reason. */
277  connection_mark_unattached_ap(entry_conn, reason);
278  count++;
279  } SMARTLIST_FOREACH_END(entry_conn);
280 
281  if (count > 0) {
282  char onion_address[HS_SERVICE_ADDR_LEN_BASE32 + 1];
283  hs_build_address(identity_pk, HS_VERSION_THREE, onion_address);
284  log_notice(LD_REND, "Closed %u streams for service %s.onion "
285  "for reason %s. Fetch status: %s.",
286  count, safe_str_client(onion_address),
288  fetch_status_to_string(status));
289  }
290 
291  /* No ownership of the object(s) in this list. */
292  smartlist_free(entry_conns);
293 }
294 
295 /** Find all pending SOCKS connection waiting for a descriptor and retry them
296  * all. This is called when the directory information changed. */
297 STATIC void
299 {
300  smartlist_t *entry_conns = find_entry_conns(NULL);
301 
302  SMARTLIST_FOREACH_BEGIN(entry_conns, entry_connection_t *, entry_conn) {
304  edge_connection_t *edge_conn = ENTRY_TO_EDGE_CONN(entry_conn);
305  connection_t *base_conn = &edge_conn->base_;
306 
307  /* Ignore non HS or non v3 connection. */
308  if (edge_conn->hs_ident == NULL) {
309  continue;
310  }
311 
312  /* In this loop, we will possibly try to fetch a descriptor for the
313  * pending connections because we just got more directory information.
314  * However, the refetch process can cleanup all SOCKS request to the same
315  * service if an internal error happens. Thus, we can end up with closed
316  * connections in our list. */
317  if (base_conn->marked_for_close) {
318  continue;
319  }
320 
321  /* XXX: There is an optimization we could do which is that for a service
322  * key, we could check if we can fetch and remember that decision. */
323 
324  /* Order a refetch in case it works this time. */
325  status = hs_client_refetch_hsdesc(&edge_conn->hs_ident->identity_pk);
326  if (status == HS_CLIENT_FETCH_HAVE_DESC) {
327  /* This is a rare case where a SOCKS connection is in state waiting for
328  * a descriptor but we do have it in the cache.
329  *
330  * This can happen is tor comes back from suspend where it previously
331  * had the descriptor but the intro points were not usuable. Once it
332  * came back to life, the intro point failure cache was cleaned up and
333  * thus the descriptor became usable again leaving us in this code path.
334  *
335  * We'll mark the connection as waiting for a circuit so the descriptor
336  * can be retried. This is safe because a connection in state waiting
337  * for a descriptor can not be in the entry connection pending list. */
339  continue;
340  }
341  /* In the case of an error, either all SOCKS connections have been
342  * closed or we are still missing directory information. Leave the
343  * connection in renddesc wait state so when we get more info, we'll be
344  * able to try it again. */
345  } SMARTLIST_FOREACH_END(entry_conn);
346 
347  /* We don't have ownership of those objects. */
348  smartlist_free(entry_conns);
349 }
350 
351 /** A v3 HS circuit successfully connected to the hidden service. Update the
352  * stream state at <b>hs_conn_ident</b> appropriately. */
353 static void
355 {
356  tor_assert(hs_conn_ident);
357 
358  /* Remove from the hid serv cache all requests for that service so we can
359  * query the HSDir again later on for various reasons. */
360  purge_hid_serv_request(&hs_conn_ident->identity_pk);
361 
362  /* The v2 subsystem cleans up the intro point time out flag at this stage.
363  * We don't try to do it here because we still need to keep intact the intro
364  * point state for future connections. Even though we are able to connect to
365  * the service, doesn't mean we should reset the timed out intro points.
366  *
367  * It is not possible to have successfully connected to an intro point
368  * present in our cache that was on error or timed out. Every entry in that
369  * cache have a 2 minutes lifetime so ultimately the intro point(s) state
370  * will be reset and thus possible to be retried. */
371 }
372 
373 /** Given the pubkey of a hidden service in <b>onion_identity_pk</b>, fetch its
374  * descriptor by launching a dir connection to <b>hsdir</b>. Return a
375  * hs_client_fetch_status_t status code depending on how it went. */
378  const routerstatus_t *hsdir)
379 {
380  uint64_t current_time_period = hs_get_time_period_num(0);
381  ed25519_public_key_t blinded_pubkey;
382  char base64_blinded_pubkey[ED25519_BASE64_LEN + 1];
383  hs_ident_dir_conn_t hs_conn_dir_ident;
384 
385  tor_assert(hsdir);
386  tor_assert(onion_identity_pk);
387 
388  /* Get blinded pubkey */
389  hs_build_blinded_pubkey(onion_identity_pk, NULL, 0,
390  current_time_period, &blinded_pubkey);
391  /* ...and base64 it. */
392  ed25519_public_to_base64(base64_blinded_pubkey, &blinded_pubkey);
393 
394  /* Copy onion pk to a dir_ident so that we attach it to the dir conn */
395  hs_ident_dir_conn_init(onion_identity_pk, &blinded_pubkey,
396  &hs_conn_dir_ident);
397 
398  /* Setup directory request */
399  directory_request_t *req =
403  directory_request_set_resource(req, base64_blinded_pubkey);
404  directory_request_fetch_set_hs_ident(req, &hs_conn_dir_ident);
406  directory_request_free(req);
407 
408  log_info(LD_REND, "Descriptor fetch request for service %s with blinded "
409  "key %s to directory %s",
410  safe_str_client(ed25519_fmt(onion_identity_pk)),
411  safe_str_client(base64_blinded_pubkey),
412  safe_str_client(routerstatus_describe(hsdir)));
413 
414  /* Fire a REQUESTED event on the control port. */
415  hs_control_desc_event_requested(onion_identity_pk, base64_blinded_pubkey,
416  hsdir);
417 
418  /* Cleanup memory. */
419  memwipe(&blinded_pubkey, 0, sizeof(blinded_pubkey));
420  memwipe(base64_blinded_pubkey, 0, sizeof(base64_blinded_pubkey));
421  memwipe(&hs_conn_dir_ident, 0, sizeof(hs_conn_dir_ident));
422 
424 }
425 
426 /** Return the HSDir we should use to fetch the descriptor of the hidden
427  * service with identity key <b>onion_identity_pk</b>. */
429 pick_hsdir_v3(const ed25519_public_key_t *onion_identity_pk)
430 {
431  char base64_blinded_pubkey[ED25519_BASE64_LEN + 1];
432  uint64_t current_time_period = hs_get_time_period_num(0);
433  smartlist_t *responsible_hsdirs = NULL;
434  ed25519_public_key_t blinded_pubkey;
435  routerstatus_t *hsdir_rs = NULL;
436 
437  tor_assert(onion_identity_pk);
438 
439  /* Get blinded pubkey of hidden service */
440  hs_build_blinded_pubkey(onion_identity_pk, NULL, 0,
441  current_time_period, &blinded_pubkey);
442  /* ...and base64 it. */
443  ed25519_public_to_base64(base64_blinded_pubkey, &blinded_pubkey);
444 
445  /* Get responsible hsdirs of service for this time period */
446  responsible_hsdirs = smartlist_new();
447 
448  hs_get_responsible_hsdirs(&blinded_pubkey, current_time_period,
449  0, 1, responsible_hsdirs);
450 
451  log_debug(LD_REND, "Found %d responsible HSDirs and about to pick one.",
452  smartlist_len(responsible_hsdirs));
453 
454  /* Pick an HSDir from the responsible ones. The ownership of
455  * responsible_hsdirs is given to this function so no need to free it. */
456  hsdir_rs = hs_pick_hsdir(responsible_hsdirs, base64_blinded_pubkey, NULL);
457 
458  return hsdir_rs;
459 }
460 
461 /** Fetch a v3 descriptor using the given <b>onion_identity_pk</b>.
462  *
463  * On success, HS_CLIENT_FETCH_LAUNCHED is returned. Otherwise, an error from
464  * hs_client_fetch_status_t is returned. */
466 fetch_v3_desc, (const ed25519_public_key_t *onion_identity_pk))
467 {
468  routerstatus_t *hsdir_rs =NULL;
469 
470  tor_assert(onion_identity_pk);
471 
472  hsdir_rs = pick_hsdir_v3(onion_identity_pk);
473  if (!hsdir_rs) {
474  log_info(LD_REND, "Couldn't pick a v3 hsdir.");
476  }
477 
478  return directory_launch_v3_desc_fetch(onion_identity_pk, hsdir_rs);
479 }
480 
481 /** With a given <b>onion_identity_pk</b>, fetch its descriptor. If
482  * <b>hsdirs</b> is specified, use the directory servers specified in the list.
483  * Else, use a random server. */
484 void
486  const smartlist_t *hsdirs)
487 {
488  tor_assert(onion_identity_pk);
489 
490  if (hsdirs != NULL) {
491  SMARTLIST_FOREACH_BEGIN(hsdirs, const routerstatus_t *, hsdir) {
492  directory_launch_v3_desc_fetch(onion_identity_pk, hsdir);
493  } SMARTLIST_FOREACH_END(hsdir);
494  } else {
495  fetch_v3_desc(onion_identity_pk);
496  }
497 }
498 
499 /** Make sure that the given v3 origin circuit circ is a valid correct
500  * introduction circuit. This will BUG() on any problems and hard assert if
501  * the anonymity of the circuit is not ok. Return 0 on success else -1 where
502  * the circuit should be mark for closed immediately. */
503 static int
505 {
506  int ret = 0;
507 
508  tor_assert(circ);
509 
510  if (BUG(TO_CIRCUIT(circ)->purpose != CIRCUIT_PURPOSE_C_INTRODUCING &&
512  TO_CIRCUIT(circ)->purpose != CIRCUIT_PURPOSE_C_INTRODUCE_ACKED)) {
513  ret = -1;
514  }
515  if (BUG(circ->hs_ident == NULL)) {
516  ret = -1;
517  }
518  if (BUG(!hs_ident_intro_circ_is_valid(circ->hs_ident))) {
519  ret = -1;
520  }
521 
522  /* This can stop the tor daemon but we want that since if we don't have
523  * anonymity on this circuit, something went really wrong. */
524  assert_circ_anonymity_ok(circ, get_options());
525  return ret;
526 }
527 
528 /** Find a descriptor intro point object that matches the given ident in the
529  * given descriptor desc. Return NULL if not found. */
530 static const hs_desc_intro_point_t *
532  const hs_descriptor_t *desc)
533 {
534  const hs_desc_intro_point_t *intro_point = NULL;
535 
536  tor_assert(ident);
537  tor_assert(desc);
538 
540  const hs_desc_intro_point_t *, ip) {
541  if (ed25519_pubkey_eq(&ident->intro_auth_pk,
542  &ip->auth_key_cert->signed_key)) {
543  intro_point = ip;
544  break;
545  }
546  } SMARTLIST_FOREACH_END(ip);
547 
548  return intro_point;
549 }
550 
551 /** Find a descriptor intro point object from the descriptor object desc that
552  * matches the given legacy identity digest in legacy_id. Return NULL if not
553  * found. */
554 static hs_desc_intro_point_t *
556  const hs_descriptor_t *desc)
557 {
558  hs_desc_intro_point_t *ret_ip = NULL;
559 
560  tor_assert(legacy_id);
561  tor_assert(desc);
562 
563  /* We will go over every intro point and try to find which one is linked to
564  * that circuit. Those lists are small so it's not that expensive. */
566  hs_desc_intro_point_t *, ip) {
567  SMARTLIST_FOREACH_BEGIN(ip->link_specifiers,
568  const link_specifier_t *, lspec) {
569  /* Not all tor node have an ed25519 identity key so we still rely on the
570  * legacy identity digest. */
571  if (link_specifier_get_ls_type(lspec) != LS_LEGACY_ID) {
572  continue;
573  }
574  if (fast_memneq(legacy_id,
575  link_specifier_getconstarray_un_legacy_id(lspec),
576  DIGEST_LEN)) {
577  break;
578  }
579  /* Found it. */
580  ret_ip = ip;
581  goto end;
582  } SMARTLIST_FOREACH_END(lspec);
583  } SMARTLIST_FOREACH_END(ip);
584 
585  end:
586  return ret_ip;
587 }
588 
589 /** Send an INTRODUCE1 cell along the intro circuit and populate the rend
590  * circuit identifier with the needed key material for the e2e encryption.
591  * Return 0 on success, -1 if there is a transient error such that an action
592  * has been taken to recover and -2 if there is a permanent error indicating
593  * that both circuits were closed. */
594 static int
596  origin_circuit_t *rend_circ)
597 {
598  int status;
599  char onion_address[HS_SERVICE_ADDR_LEN_BASE32 + 1];
600  const ed25519_public_key_t *service_identity_pk = NULL;
601  const hs_desc_intro_point_t *ip;
602 
603  tor_assert(rend_circ);
604  if (intro_circ_is_ok(intro_circ) < 0) {
605  goto perm_err;
606  }
607 
608  service_identity_pk = &intro_circ->hs_ident->identity_pk;
609  /* For logging purposes. There will be a time where the hs_ident will have a
610  * version number but for now there is none because it's all v3. */
611  hs_build_address(service_identity_pk, HS_VERSION_THREE, onion_address);
612 
613  log_info(LD_REND, "Sending INTRODUCE1 cell to service %s on circuit %u",
614  safe_str_client(onion_address), TO_CIRCUIT(intro_circ)->n_circ_id);
615 
616  /* 1) Get descriptor from our cache. */
617  const hs_descriptor_t *desc =
618  hs_cache_lookup_as_client(service_identity_pk);
619  if (desc == NULL || !hs_client_any_intro_points_usable(service_identity_pk,
620  desc)) {
621  log_info(LD_REND, "Request to %s %s. Trying to fetch a new descriptor.",
622  safe_str_client(onion_address),
623  (desc) ? "didn't have usable intro points" :
624  "didn't have a descriptor");
625  hs_client_refetch_hsdesc(service_identity_pk);
626  /* We just triggered a refetch, make sure every connections are back
627  * waiting for that descriptor. */
628  flag_all_conn_wait_desc(service_identity_pk);
629  /* We just asked for a refetch so this is a transient error. */
630  goto tran_err;
631  }
632 
633  /* We need to find which intro point in the descriptor we are connected to
634  * on intro_circ. */
635  ip = find_desc_intro_point_by_ident(intro_circ->hs_ident, desc);
636  if (ip == NULL) {
637  /* The following is possible if the descriptor was changed while we had
638  * this introduction circuit open and waiting for the rendezvous circuit to
639  * be ready. Which results in this situation where we can't find the
640  * corresponding intro point within the descriptor of the service. */
641  log_info(LD_REND, "Unable to find introduction point for service %s "
642  "while trying to send an INTRODUCE1 cell.",
643  safe_str_client(onion_address));
644  goto perm_err;
645  }
646 
647  /* Send the INTRODUCE1 cell. */
648  if (hs_circ_send_introduce1(intro_circ, rend_circ, ip,
649  desc->subcredential) < 0) {
650  if (TO_CIRCUIT(intro_circ)->marked_for_close) {
651  /* If the introduction circuit was closed, we were unable to send the
652  * cell for some reasons. In any case, the intro circuit has to be
653  * closed by the above function. We'll return a transient error so tor
654  * can recover and pick a new intro point. To avoid picking that same
655  * intro point, we'll note down the intro point failure so it doesn't
656  * get reused. */
657  hs_cache_client_intro_state_note(service_identity_pk,
658  &intro_circ->hs_ident->intro_auth_pk,
659  INTRO_POINT_FAILURE_GENERIC);
660  }
661  /* It is also possible that the rendezvous circuit was closed due to being
662  * unable to use the rendezvous point node_t so in that case, we also want
663  * to recover and let tor pick a new one. */
664  goto tran_err;
665  }
666 
667  /* Cell has been sent successfully. Copy the introduction point
668  * authentication and encryption key in the rendezvous circuit identifier so
669  * we can compute the ntor keys when we receive the RENDEZVOUS2 cell. */
670  memcpy(&rend_circ->hs_ident->intro_enc_pk, &ip->enc_key,
671  sizeof(rend_circ->hs_ident->intro_enc_pk));
673  &intro_circ->hs_ident->intro_auth_pk);
674 
675  /* Now, we wait for an ACK or NAK on this circuit. */
678  /* Set timestamp_dirty, because circuit_expire_building expects it to
679  * specify when a circuit entered the _C_INTRODUCE_ACK_WAIT state. */
680  TO_CIRCUIT(intro_circ)->timestamp_dirty = time(NULL);
681  pathbias_count_use_attempt(intro_circ);
682 
683  /* Success. */
684  status = 0;
685  goto end;
686 
687  perm_err:
688  /* Permanent error: it is possible that the intro circuit was closed prior
689  * because we weren't able to send the cell. Make sure we don't double close
690  * it which would result in a warning. */
691  if (!TO_CIRCUIT(intro_circ)->marked_for_close) {
692  circuit_mark_for_close(TO_CIRCUIT(intro_circ), END_CIRC_REASON_INTERNAL);
693  }
694  circuit_mark_for_close(TO_CIRCUIT(rend_circ), END_CIRC_REASON_INTERNAL);
695  status = -2;
696  goto end;
697 
698  tran_err:
699  status = -1;
700 
701  end:
702  memwipe(onion_address, 0, sizeof(onion_address));
703  return status;
704 }
705 
706 /** Using the introduction circuit circ, setup the authentication key of the
707  * intro point this circuit has extended to. */
708 static void
710 {
711  const hs_descriptor_t *desc;
712  const hs_desc_intro_point_t *ip;
713 
714  tor_assert(circ);
715 
717  if (desc == NULL) {
718  /* There is a very small race window between the opening of this circuit
719  * and the client descriptor cache that gets purged (NEWNYM) or the
720  * cleaned up because it expired. Mark the circuit for close so a new
721  * descriptor fetch can occur. */
722  circuit_mark_for_close(TO_CIRCUIT(circ), END_CIRC_REASON_INTERNAL);
723  goto end;
724  }
725 
726  /* We will go over every intro point and try to find which one is linked to
727  * that circuit. Those lists are small so it's not that expensive. */
729  circ->build_state->chosen_exit->identity_digest, desc);
730  if (ip) {
731  /* We got it, copy its authentication key to the identifier. */
733  &ip->auth_key_cert->signed_key);
734  goto end;
735  }
736 
737  /* Reaching this point means we didn't find any intro point for this circuit
738  * which is not supposed to happen. */
739  tor_assert_nonfatal_unreached();
740 
741  end:
742  return;
743 }
744 
745 /** Called when an introduction circuit has opened. */
746 static void
748 {
749  tor_assert(circ);
751  log_info(LD_REND, "Introduction circuit %u has opened. Attaching streams.",
752  (unsigned int) TO_CIRCUIT(circ)->n_circ_id);
753 
754  /* This is an introduction circuit so we'll attach the correct
755  * authentication key to the circuit identifier so it can be identified
756  * properly later on. */
758 
760 }
761 
762 /** Called when a rendezvous circuit has opened. */
763 static void
765 {
766  tor_assert(circ);
768 
769  const extend_info_t *rp_ei = circ->build_state->chosen_exit;
770 
771  /* Check that we didn't accidentally choose a node that does not understand
772  * the v3 rendezvous protocol */
773  if (rp_ei) {
774  const node_t *rp_node = node_get_by_id(rp_ei->identity_digest);
775  if (rp_node) {
776  if (BUG(!node_supports_v3_rendezvous_point(rp_node))) {
777  return;
778  }
779  }
780  }
781 
782  log_info(LD_REND, "Rendezvous circuit has opened to %s.",
783  safe_str_client(extend_info_describe(rp_ei)));
784 
785  /* Ignore returned value, nothing we can really do. On failure, the circuit
786  * will be marked for close. */
788 
789  /* Register rend circuit in circuitmap if it's still alive. */
790  if (!TO_CIRCUIT(circ)->marked_for_close) {
792  circ->hs_ident->rendezvous_cookie);
793  }
794 }
795 
796 /** This is an helper function that convert a descriptor intro point object ip
797  * to a newly allocated extend_info_t object fully initialized. Return NULL if
798  * we can't convert it for which chances are that we are missing or malformed
799  * link specifiers. */
802 {
803  extend_info_t *ei;
804 
805  tor_assert(ip);
806 
807  /* Explicitly put the direct connection option to 0 because this is client
808  * side and there is no such thing as a non anonymous client. */
810 
811  return ei;
812 }
813 
814 /** Return true iff the intro point ip for the service service_pk is usable.
815  * This function checks if the intro point is in the client intro state cache
816  * and checks at the failures. It is considered usable if:
817  * - No error happened (INTRO_POINT_FAILURE_GENERIC)
818  * - It is not flagged as timed out (INTRO_POINT_FAILURE_TIMEOUT)
819  * - The unreachable count is lower than
820  * MAX_INTRO_POINT_REACHABILITY_FAILURES (INTRO_POINT_FAILURE_UNREACHABLE)
821  */
822 static int
824  const hs_desc_intro_point_t *ip)
825 {
826  const hs_cache_intro_state_t *state;
827 
828  tor_assert(service_pk);
829  tor_assert(ip);
830 
831  state = hs_cache_client_intro_state_find(service_pk,
832  &ip->auth_key_cert->signed_key);
833  if (state == NULL) {
834  /* This means we've never encountered any problem thus usable. */
835  goto usable;
836  }
837  if (state->error) {
838  log_info(LD_REND, "Intro point with auth key %s had an error. Not usable",
839  safe_str_client(ed25519_fmt(&ip->auth_key_cert->signed_key)));
840  goto not_usable;
841  }
842  if (state->timed_out) {
843  log_info(LD_REND, "Intro point with auth key %s timed out. Not usable",
844  safe_str_client(ed25519_fmt(&ip->auth_key_cert->signed_key)));
845  goto not_usable;
846  }
848  log_info(LD_REND, "Intro point with auth key %s unreachable. Not usable",
849  safe_str_client(ed25519_fmt(&ip->auth_key_cert->signed_key)));
850  goto not_usable;
851  }
852 
853  usable:
854  return 1;
855  not_usable:
856  return 0;
857 }
858 
859 /** Using a descriptor desc, return a newly allocated extend_info_t object of a
860  * randomly picked introduction point from its list. Return NULL if none are
861  * usable. */
864 {
865  extend_info_t *ei = NULL, *ei_excluded = NULL;
866  smartlist_t *usable_ips = NULL;
867  const hs_descriptor_t *desc;
868  const hs_desc_encrypted_data_t *enc_data;
869  const or_options_t *options = get_options();
870  /* Calculate the onion address for logging purposes */
871  char onion_address[HS_SERVICE_ADDR_LEN_BASE32 + 1];
872 
873  tor_assert(service_pk);
874 
875  desc = hs_cache_lookup_as_client(service_pk);
876  /* Assume the service is v3 if the descriptor is missing. This is ok,
877  * because we only use the address in log messages */
878  hs_build_address(service_pk,
879  desc ? desc->plaintext_data.version : HS_VERSION_THREE,
880  onion_address);
881  if (desc == NULL || !hs_client_any_intro_points_usable(service_pk,
882  desc)) {
883  log_info(LD_REND, "Unable to randomly select an introduction point "
884  "for service %s because descriptor %s. We can't connect.",
885  safe_str_client(onion_address),
886  (desc) ? "doesn't have any usable intro points"
887  : "is missing (assuming v3 onion address)");
888  goto end;
889  }
890 
891  enc_data = &desc->encrypted_data;
892  usable_ips = smartlist_new();
893  smartlist_add_all(usable_ips, enc_data->intro_points);
894  while (smartlist_len(usable_ips) != 0) {
895  int idx;
896  const hs_desc_intro_point_t *ip;
897 
898  /* Pick a random intro point and immediately remove it from the usable
899  * list so we don't pick it again if we have to iterate more. */
900  idx = crypto_rand_int(smartlist_len(usable_ips));
901  ip = smartlist_get(usable_ips, idx);
902  smartlist_del(usable_ips, idx);
903 
904  /* We need to make sure we have a usable intro points which is in a good
905  * state in our cache. */
906  if (!intro_point_is_usable(service_pk, ip)) {
907  continue;
908  }
909 
910  /* Generate an extend info object from the intro point object. */
912  if (ei == NULL) {
913  /* We can get here for instance if the intro point is a private address
914  * and we aren't allowed to extend to those. */
915  log_info(LD_REND, "Unable to select introduction point with auth key %s "
916  "for service %s, because we could not extend to it.",
917  safe_str_client(ed25519_fmt(&ip->auth_key_cert->signed_key)),
918  safe_str_client(onion_address));
919  continue;
920  }
921 
922  /* Test the pick against ExcludeNodes. */
923  if (routerset_contains_extendinfo(options->ExcludeNodes, ei)) {
924  /* If this pick is in the ExcludeNodes list, we keep its reference so if
925  * we ever end up not being able to pick anything else and StrictNodes is
926  * unset, we'll use it. */
927  if (ei_excluded) {
928  /* If something was already here free it. After the loop is gone we
929  * will examine the last excluded intro point, and that's fine since
930  * that's random anyway */
931  extend_info_free(ei_excluded);
932  }
933  ei_excluded = ei;
934  continue;
935  }
936 
937  /* Good pick! Let's go with this. */
938  goto end;
939  }
940 
941  /* Reaching this point means a couple of things. Either we can't use any of
942  * the intro point listed because the IP address can't be extended to or it
943  * is listed in the ExcludeNodes list. In the later case, if StrictNodes is
944  * set, we are forced to not use anything. */
945  ei = ei_excluded;
946  if (options->StrictNodes) {
947  log_warn(LD_REND, "Every introduction point for service %s is in the "
948  "ExcludeNodes set and StrictNodes is set. We can't connect.",
949  safe_str_client(onion_address));
950  extend_info_free(ei);
951  ei = NULL;
952  } else {
953  log_fn(LOG_PROTOCOL_WARN, LD_REND, "Every introduction point for service "
954  "%s is unusable or we can't extend to it. We can't connect.",
955  safe_str_client(onion_address));
956  }
957 
958  end:
959  smartlist_free(usable_ips);
960  memwipe(onion_address, 0, sizeof(onion_address));
961  return ei;
962 }
963 
964 /** For this introduction circuit, we'll look at if we have any usable
965  * introduction point left for this service. If so, we'll use the circuit to
966  * re-extend to a new intro point. Else, we'll close the circuit and its
967  * corresponding rendezvous circuit. Return 0 if we are re-extending else -1
968  * if we are closing the circuits.
969  *
970  * This is called when getting an INTRODUCE_ACK cell with a NACK. */
971 static int
973 {
974  int ret = -1;
975  const hs_descriptor_t *desc;
976  origin_circuit_t *rend_circ;
977 
978  tor_assert(intro_circ);
979 
980  desc = hs_cache_lookup_as_client(&intro_circ->hs_ident->identity_pk);
981  if (BUG(desc == NULL)) {
982  /* We can't continue without a descriptor. */
983  goto close;
984  }
985  /* We still have the descriptor, great! Let's try to see if we can
986  * re-extend by looking up if there are any usable intro points. */
988  desc)) {
989  goto close;
990  }
991  /* Try to re-extend now. */
992  if (hs_client_reextend_intro_circuit(intro_circ) < 0) {
993  goto close;
994  }
995  /* Success on re-extending. Don't return an error. */
996  ret = 0;
997  goto end;
998 
999  close:
1000  /* Change the intro circuit purpose before so we don't report an intro point
1001  * failure again triggering an extra descriptor fetch. The circuit can
1002  * already be closed on failure to re-extend. */
1003  if (!TO_CIRCUIT(intro_circ)->marked_for_close) {
1004  circuit_change_purpose(TO_CIRCUIT(intro_circ),
1006  circuit_mark_for_close(TO_CIRCUIT(intro_circ), END_CIRC_REASON_FINISHED);
1007  }
1008  /* Close the related rendezvous circuit. */
1010  intro_circ->hs_ident->rendezvous_cookie);
1011  /* The rendezvous circuit might have collapsed while the INTRODUCE_ACK was
1012  * inflight so we can't expect one every time. */
1013  if (rend_circ) {
1014  circuit_mark_for_close(TO_CIRCUIT(rend_circ), END_CIRC_REASON_FINISHED);
1015  }
1016 
1017  end:
1018  return ret;
1019 }
1020 
1021 /** Called when we get an INTRODUCE_ACK success status code. Do the appropriate
1022  * actions for the rendezvous point and finally close intro_circ. */
1023 static void
1025 {
1026  origin_circuit_t *rend_circ = NULL;
1027 
1028  tor_assert(intro_circ);
1029 
1030  log_info(LD_REND, "Received INTRODUCE_ACK ack! Informing rendezvous");
1031 
1032  /* Get the rendezvous circuit for this rendezvous cookie. */
1033  uint8_t *rendezvous_cookie = intro_circ->hs_ident->rendezvous_cookie;
1034  rend_circ =
1036  if (rend_circ == NULL) {
1037  log_warn(LD_REND, "Can't find any rendezvous circuit. Stopping");
1038  goto end;
1039  }
1040 
1041  assert_circ_anonymity_ok(rend_circ, get_options());
1042 
1043  /* It is possible to get a RENDEZVOUS2 cell before the INTRODUCE_ACK which
1044  * means that the circuit will be joined and already transmitting data. In
1045  * that case, simply skip the purpose change and close the intro circuit
1046  * like it should be. */
1047  if (TO_CIRCUIT(rend_circ)->purpose == CIRCUIT_PURPOSE_C_REND_JOINED) {
1048  goto end;
1049  }
1052  /* Set timestamp_dirty, because circuit_expire_building expects it to
1053  * specify when a circuit entered the
1054  * CIRCUIT_PURPOSE_C_REND_READY_INTRO_ACKED state. */
1055  TO_CIRCUIT(rend_circ)->timestamp_dirty = time(NULL);
1056 
1057  end:
1058  /* We don't need the intro circuit anymore. It did what it had to do! */
1059  circuit_change_purpose(TO_CIRCUIT(intro_circ),
1061  circuit_mark_for_close(TO_CIRCUIT(intro_circ), END_CIRC_REASON_FINISHED);
1062 
1063  /* XXX: Close pending intro circuits we might have in parallel. */
1064  return;
1065 }
1066 
1067 /** Called when we get an INTRODUCE_ACK failure status code. Depending on our
1068  * failure cache status, either close the circuit or re-extend to a new
1069  * introduction point. */
1070 static void
1072 {
1073  tor_assert(circ);
1074 
1075  log_info(LD_REND, "Received INTRODUCE_ACK nack by %s. Reason: %u",
1076  safe_str_client(extend_info_describe(circ->build_state->chosen_exit)),
1077  status);
1078 
1079  /* It's a NAK. The introduction point didn't relay our request. */
1081 
1082  /* Note down this failure in the intro point failure cache. Depending on how
1083  * many times we've tried this intro point, close it or reextend. */
1085  &circ->hs_ident->intro_auth_pk,
1086  INTRO_POINT_FAILURE_GENERIC);
1087 }
1088 
1089 /** Called when we get an INTRODUCE_ACK on the intro circuit circ. The encoded
1090  * cell is in payload of length payload_len. Return 0 on success else a
1091  * negative value. The circuit is either close or reuse to re-extend to a new
1092  * introduction point. */
1093 static int
1094 handle_introduce_ack(origin_circuit_t *circ, const uint8_t *payload,
1095  size_t payload_len)
1096 {
1097  int status, ret = -1;
1098 
1099  tor_assert(circ);
1100  tor_assert(circ->build_state);
1102  assert_circ_anonymity_ok(circ, get_options());
1103  tor_assert(payload);
1104 
1105  status = hs_cell_parse_introduce_ack(payload, payload_len);
1106  switch (status) {
1107  case TRUNNEL_HS_INTRO_ACK_STATUS_SUCCESS:
1108  ret = 0;
1110  goto end;
1111  case TRUNNEL_HS_INTRO_ACK_STATUS_UNKNOWN_ID:
1112  case TRUNNEL_HS_INTRO_ACK_STATUS_BAD_FORMAT:
1113  /* It is possible that the intro point can send us an unknown status code
1114  * for the NACK that we do not know about like a new code for instance.
1115  * Just fallthrough so we can note down the NACK and re-extend. */
1116  default:
1117  handle_introduce_ack_bad(circ, status);
1118  /* We are going to see if we have to close the circuits (IP and RP) or we
1119  * can re-extend to a new intro point. */
1120  ret = close_or_reextend_intro_circ(circ);
1121  break;
1122  }
1123 
1124  end:
1125  return ret;
1126 }
1127 
1128 /** Called when we get a RENDEZVOUS2 cell on the rendezvous circuit circ. The
1129  * encoded cell is in payload of length payload_len. Return 0 on success or a
1130  * negative value on error. On error, the circuit is marked for close. */
1131 STATIC int
1132 handle_rendezvous2(origin_circuit_t *circ, const uint8_t *payload,
1133  size_t payload_len)
1134 {
1135  int ret = -1;
1136  curve25519_public_key_t server_pk;
1137  uint8_t auth_mac[DIGEST256_LEN] = {0};
1138  uint8_t handshake_info[CURVE25519_PUBKEY_LEN + sizeof(auth_mac)] = {0};
1140  const hs_ident_circuit_t *ident;
1141 
1142  tor_assert(circ);
1143  tor_assert(payload);
1144 
1145  /* Make things easier. */
1146  ident = circ->hs_ident;
1147  tor_assert(ident);
1148 
1149  if (hs_cell_parse_rendezvous2(payload, payload_len, handshake_info,
1150  sizeof(handshake_info)) < 0) {
1151  goto err;
1152  }
1153  /* Get from the handshake info the SERVER_PK and AUTH_MAC. */
1154  memcpy(&server_pk, handshake_info, CURVE25519_PUBKEY_LEN);
1155  memcpy(auth_mac, handshake_info + CURVE25519_PUBKEY_LEN, sizeof(auth_mac));
1156 
1157  /* Generate the handshake info. */
1158  if (hs_ntor_client_get_rendezvous1_keys(&ident->intro_auth_pk,
1159  &ident->rendezvous_client_kp,
1160  &ident->intro_enc_pk, &server_pk,
1161  &keys) < 0) {
1162  log_info(LD_REND, "Unable to compute the rendezvous keys.");
1163  goto err;
1164  }
1165 
1166  /* Critical check, make sure that the MAC matches what we got with what we
1167  * computed just above. */
1168  if (!hs_ntor_client_rendezvous2_mac_is_good(&keys, auth_mac)) {
1169  log_info(LD_REND, "Invalid MAC in RENDEZVOUS2. Rejecting cell.");
1170  goto err;
1171  }
1172 
1173  /* Setup the e2e encryption on the circuit and finalize its state. */
1174  if (hs_circuit_setup_e2e_rend_circ(circ, keys.ntor_key_seed,
1175  sizeof(keys.ntor_key_seed), 0) < 0) {
1176  log_info(LD_REND, "Unable to setup the e2e encryption.");
1177  goto err;
1178  }
1179  /* Success. Hidden service connection finalized! */
1180  ret = 0;
1181  goto end;
1182 
1183  err:
1184  circuit_mark_for_close(TO_CIRCUIT(circ), END_CIRC_REASON_TORPROTOCOL);
1185  end:
1186  memwipe(&keys, 0, sizeof(keys));
1187  return ret;
1188 }
1189 
1190 /** Return true iff the client can fetch a descriptor for this service public
1191  * identity key and status_out if not NULL is untouched. If the client can
1192  * _not_ fetch the descriptor and if status_out is not NULL, it is set with
1193  * the fetch status code. */
1194 static unsigned int
1196  hs_client_fetch_status_t *status_out)
1197 {
1198  hs_client_fetch_status_t status;
1199 
1200  tor_assert(identity_pk);
1201 
1202  /* Are we configured to fetch descriptors? */
1203  if (!get_options()->FetchHidServDescriptors) {
1204  log_warn(LD_REND, "We received an onion address for a hidden service "
1205  "descriptor but we are configured to not fetch.");
1206  status = HS_CLIENT_FETCH_NOT_ALLOWED;
1207  goto cannot;
1208  }
1209 
1210  /* Without a live consensus we can't do any client actions. It is needed to
1211  * compute the hashring for a service. */
1213  log_info(LD_REND, "Can't fetch descriptor for service %s because we "
1214  "are missing a live consensus. Stalling connection.",
1215  safe_str_client(ed25519_fmt(identity_pk)));
1217  goto cannot;
1218  }
1219 
1221  log_info(LD_REND, "Can't fetch descriptor for service %s because we "
1222  "dont have enough descriptors. Stalling connection.",
1223  safe_str_client(ed25519_fmt(identity_pk)));
1225  goto cannot;
1226  }
1227 
1228  /* Check if fetching a desc for this HS is useful to us right now */
1229  {
1230  const hs_descriptor_t *cached_desc = NULL;
1231  cached_desc = hs_cache_lookup_as_client(identity_pk);
1232  if (cached_desc && hs_client_any_intro_points_usable(identity_pk,
1233  cached_desc)) {
1234  log_info(LD_GENERAL, "We would fetch a v3 hidden service descriptor "
1235  "but we already have a usable descriptor.");
1236  status = HS_CLIENT_FETCH_HAVE_DESC;
1237  goto cannot;
1238  }
1239  }
1240 
1241  /* Don't try to refetch while we have a pending request for it. */
1242  if (directory_request_is_pending(identity_pk)) {
1243  log_info(LD_REND, "Already a pending directory request. Waiting on it.");
1244  status = HS_CLIENT_FETCH_PENDING;
1245  goto cannot;
1246  }
1247 
1248  /* Yes, client can fetch! */
1249  return 1;
1250  cannot:
1251  if (status_out) {
1252  *status_out = status;
1253  }
1254  return 0;
1255 }
1256 
1257 /** Purge the client authorization cache of all ephemeral entries that is the
1258  * entries that are not flagged with CLIENT_AUTH_FLAG_IS_PERMANENT.
1259  *
1260  * This is called from the hs_client_purge_state() used by a SIGNEWNYM. */
1261 STATIC void
1263 {
1264  DIGEST256MAP_FOREACH_MODIFY(client_auths, key,
1266  /* Cleanup every entry that are _NOT_ permanent that is ephemeral. */
1267  if (!(auth->flags & CLIENT_AUTH_FLAG_IS_PERMANENT)) {
1268  MAP_DEL_CURRENT(key);
1269  client_service_authorization_free(auth);
1270  }
1272 
1273  log_info(LD_REND, "Client onion service ephemeral authorization "
1274  "cache has been purged.");
1275 }
1276 
1277 /** Return the client auth in the map using the service identity public key.
1278  * Return NULL if it does not exist in the map. */
1280 find_client_auth(const ed25519_public_key_t *service_identity_pk)
1281 {
1282  /* If the map is not allocated, we can assume that we do not have any client
1283  * auth information. */
1284  if (!client_auths) {
1285  return NULL;
1286  }
1287  return digest256map_get(client_auths, service_identity_pk->pubkey);
1288 }
1289 
1290 /** This is called when a descriptor has arrived following a fetch request and
1291  * has been stored in the client cache. The given entry connections, matching
1292  * the service identity key, will get attached to the service circuit. */
1293 static void
1295 {
1296  time_t now = time(NULL);
1297 
1298  tor_assert(entry_conns);
1299 
1300  SMARTLIST_FOREACH_BEGIN(entry_conns, entry_connection_t *, entry_conn) {
1301  const hs_descriptor_t *desc;
1302  edge_connection_t *edge_conn = ENTRY_TO_EDGE_CONN(entry_conn);
1303  const ed25519_public_key_t *identity_pk =
1304  &edge_conn->hs_ident->identity_pk;
1305 
1306  /* We were just called because we stored the descriptor for this service
1307  * so not finding a descriptor means we have a bigger problem. */
1308  desc = hs_cache_lookup_as_client(identity_pk);
1309  if (BUG(desc == NULL)) {
1310  goto end;
1311  }
1312 
1313  if (!hs_client_any_intro_points_usable(identity_pk, desc)) {
1314  log_info(LD_REND, "Hidden service descriptor is unusable. "
1315  "Closing streams.");
1316  connection_mark_unattached_ap(entry_conn,
1317  END_STREAM_REASON_RESOLVEFAILED);
1318  /* We are unable to use the descriptor so remove the directory request
1319  * from the cache so the next connection can try again. */
1320  note_connection_attempt_succeeded(edge_conn->hs_ident);
1321  continue;
1322  }
1323 
1324  log_info(LD_REND, "Descriptor has arrived. Launching circuits.");
1325 
1326  /* Mark connection as waiting for a circuit since we do have a usable
1327  * descriptor now. */
1328  mark_conn_as_waiting_for_circuit(&edge_conn->base_, now);
1329  } SMARTLIST_FOREACH_END(entry_conn);
1330 
1331  end:
1332  return;
1333 }
1334 
1335 /** This is called when a descriptor fetch was successful but the descriptor
1336  * couldn't be decrypted due to missing or bad client authorization. */
1337 static void
1339  hs_desc_decode_status_t status)
1340 {
1341  tor_assert(entry_conns);
1342 
1343  SMARTLIST_FOREACH_BEGIN(entry_conns, entry_connection_t *, entry_conn) {
1344  socks5_reply_status_t code;
1345  if (status == HS_DESC_DECODE_BAD_CLIENT_AUTH) {
1346  code = SOCKS5_HS_BAD_CLIENT_AUTH;
1347  } else if (status == HS_DESC_DECODE_NEED_CLIENT_AUTH) {
1348  code = SOCKS5_HS_MISSING_CLIENT_AUTH;
1349  } else {
1350  /* We should not be called with another type of status. Recover by
1351  * sending a generic error. */
1352  tor_assert_nonfatal_unreached();
1353  code = HS_DESC_DECODE_GENERIC_ERROR;
1354  }
1355  entry_conn->socks_request->socks_extended_error_code = code;
1356  connection_mark_unattached_ap(entry_conn, END_STREAM_REASON_MISC);
1357  } SMARTLIST_FOREACH_END(entry_conn);
1358 }
1359 
1360 /** Called when we get a 200 directory fetch status code. */
1361 static void
1363  const smartlist_t *entry_conns, const char *body)
1364 {
1365  hs_desc_decode_status_t decode_status;
1366 
1367  tor_assert(dir_conn);
1368  tor_assert(entry_conns);
1369  tor_assert(body);
1370 
1371  /* We got something: Try storing it in the cache. */
1372  decode_status = hs_cache_store_as_client(body,
1373  &dir_conn->hs_ident->identity_pk);
1374  switch (decode_status) {
1375  case HS_DESC_DECODE_OK:
1376  case HS_DESC_DECODE_NEED_CLIENT_AUTH:
1377  case HS_DESC_DECODE_BAD_CLIENT_AUTH:
1378  log_info(LD_REND, "Stored hidden service descriptor successfully.");
1379  TO_CONN(dir_conn)->purpose = DIR_PURPOSE_HAS_FETCHED_HSDESC;
1380  if (decode_status == HS_DESC_DECODE_OK) {
1381  client_desc_has_arrived(entry_conns);
1382  } else {
1383  /* This handles both client auth decode status. */
1384  client_desc_missing_bad_client_auth(entry_conns, decode_status);
1385  log_info(LD_REND, "Stored hidden service descriptor requires "
1386  "%s client authorization.",
1387  decode_status == HS_DESC_DECODE_NEED_CLIENT_AUTH ? "missing"
1388  : "new");
1389  }
1390  /* Fire control port RECEIVED event. */
1391  hs_control_desc_event_received(dir_conn->hs_ident,
1392  dir_conn->identity_digest);
1393  hs_control_desc_event_content(dir_conn->hs_ident,
1394  dir_conn->identity_digest, body);
1395  break;
1396  case HS_DESC_DECODE_ENCRYPTED_ERROR:
1397  case HS_DESC_DECODE_SUPERENC_ERROR:
1398  case HS_DESC_DECODE_PLAINTEXT_ERROR:
1399  case HS_DESC_DECODE_GENERIC_ERROR:
1400  default:
1401  log_info(LD_REND, "Failed to store hidden service descriptor. "
1402  "Descriptor decoding status: %d", decode_status);
1403  /* Fire control port FAILED event. */
1404  hs_control_desc_event_failed(dir_conn->hs_ident,
1405  dir_conn->identity_digest, "BAD_DESC");
1406  hs_control_desc_event_content(dir_conn->hs_ident,
1407  dir_conn->identity_digest, NULL);
1408  break;
1409  }
1410 }
1411 
1412 /** Called when we get a 404 directory fetch status code. */
1413 static void
1415  const smartlist_t *entry_conns)
1416 {
1417  tor_assert(entry_conns);
1418 
1419  /* Not there. We'll retry when connection_about_to_close_connection() tries
1420  * to clean this conn up. */
1421  log_info(LD_REND, "Fetching hidden service v3 descriptor not found: "
1422  "Retrying at another directory.");
1423  /* Fire control port FAILED event. */
1424  hs_control_desc_event_failed(dir_conn->hs_ident, dir_conn->identity_digest,
1425  "NOT_FOUND");
1426  hs_control_desc_event_content(dir_conn->hs_ident, dir_conn->identity_digest,
1427  NULL);
1428 
1429  /* Flag every entry connections that the descriptor was not found. */
1430  SMARTLIST_FOREACH_BEGIN(entry_conns, entry_connection_t *, entry_conn) {
1432  SOCKS5_HS_NOT_FOUND;
1433  } SMARTLIST_FOREACH_END(entry_conn);
1434 }
1435 
1436 /** Called when we get a 400 directory fetch status code. */
1437 static void
1438 client_dir_fetch_400(dir_connection_t *dir_conn, const char *reason)
1439 {
1440  tor_assert(dir_conn);
1441 
1442  log_warn(LD_REND, "Fetching v3 hidden service descriptor failed: "
1443  "http status 400 (%s). Dirserver didn't like our "
1444  "query? Retrying at another directory.",
1445  escaped(reason));
1446 
1447  /* Fire control port FAILED event. */
1448  hs_control_desc_event_failed(dir_conn->hs_ident, dir_conn->identity_digest,
1449  "QUERY_REJECTED");
1450  hs_control_desc_event_content(dir_conn->hs_ident, dir_conn->identity_digest,
1451  NULL);
1452 }
1453 
1454 /** Called when we get an unexpected directory fetch status code. */
1455 static void
1456 client_dir_fetch_unexpected(dir_connection_t *dir_conn, const char *reason,
1457  const int status_code)
1458 {
1459  tor_assert(dir_conn);
1460 
1461  log_warn(LD_REND, "Fetching v3 hidden service descriptor failed: "
1462  "http status %d (%s) response unexpected from HSDir "
1463  "server '%s:%d'. Retrying at another directory.",
1464  status_code, escaped(reason), TO_CONN(dir_conn)->address,
1465  TO_CONN(dir_conn)->port);
1466  /* Fire control port FAILED event. */
1467  hs_control_desc_event_failed(dir_conn->hs_ident, dir_conn->identity_digest,
1468  "UNEXPECTED");
1469  hs_control_desc_event_content(dir_conn->hs_ident, dir_conn->identity_digest,
1470  NULL);
1471 }
1472 
1473 /** Get the full filename for storing the client auth credentials for the
1474  * service in <b>onion_address</b>. The base directory is <b>dir</b>.
1475  * This function never returns NULL. */
1476 static char *
1477 get_client_auth_creds_filename(const char *onion_address,
1478  const char *dir)
1479 {
1480  char *full_fname = NULL;
1481  char *fname;
1482 
1483  tor_asprintf(&fname, "%s.auth_private", onion_address);
1484  full_fname = hs_path_from_filename(dir, fname);
1485  tor_free(fname);
1486 
1487  return full_fname;
1488 }
1489 
1490 /** Permanently store the credentials in <b>creds</b> to disk.
1491  *
1492  * Return -1 if there was an error while storing the credentials, otherwise
1493  * return 0.
1494  */
1495 static int
1497  const hs_client_service_authorization_t *creds)
1498 {
1499  const or_options_t *options = get_options();
1500  char *full_fname = NULL;
1501  char *file_contents = NULL;
1502  char priv_key_b32[BASE32_NOPAD_LEN(CURVE25519_PUBKEY_LEN)+1];
1503  int retval = -1;
1504 
1505  tor_assert(creds->flags & CLIENT_AUTH_FLAG_IS_PERMANENT);
1506 
1507  /* We need ClientOnionAuthDir to be set, otherwise we can't proceed */
1508  if (!options->ClientOnionAuthDir) {
1509  log_warn(LD_GENERAL, "Can't register permanent client auth credentials "
1510  "for %s without ClientOnionAuthDir option. Discarding.",
1511  creds->onion_address);
1512  goto err;
1513  }
1514 
1515  /* Make sure the directory exists and is private enough. */
1516  if (check_private_dir(options->ClientOnionAuthDir, 0, options->User) < 0) {
1517  goto err;
1518  }
1519 
1520  /* Get filename that we should store the credentials */
1521  full_fname = get_client_auth_creds_filename(creds->onion_address,
1522  options->ClientOnionAuthDir);
1523 
1524  /* Encode client private key */
1525  base32_encode(priv_key_b32, sizeof(priv_key_b32),
1526  (char*)creds->enc_seckey.secret_key,
1527  sizeof(creds->enc_seckey.secret_key));
1528 
1529  /* Get the full file contents and write it to disk! */
1530  tor_asprintf(&file_contents, "%s:descriptor:x25519:%s",
1531  creds->onion_address, priv_key_b32);
1532  if (write_str_to_file(full_fname, file_contents, 0) < 0) {
1533  log_warn(LD_GENERAL, "Failed to write client auth creds file for %s!",
1534  creds->onion_address);
1535  goto err;
1536  }
1537 
1538  retval = 0;
1539 
1540  err:
1541  tor_free(file_contents);
1542  tor_free(full_fname);
1543 
1544  return retval;
1545 }
1546 
1547 /** Register the credential <b>creds</b> as part of the client auth subsystem.
1548  *
1549  * Takes ownership of <b>creds</b>.
1550  **/
1551 hs_client_register_auth_status_t
1553 {
1554  ed25519_public_key_t service_identity_pk;
1555  hs_client_service_authorization_t *old_creds = NULL;
1556  hs_client_register_auth_status_t retval = REGISTER_SUCCESS;
1557 
1558  tor_assert(creds);
1559 
1560  if (!client_auths) {
1561  client_auths = digest256map_new();
1562  }
1563 
1564  if (hs_parse_address(creds->onion_address, &service_identity_pk,
1565  NULL, NULL) < 0) {
1566  client_service_authorization_free(creds);
1567  return REGISTER_FAIL_BAD_ADDRESS;
1568  }
1569 
1570  /* If we reach this point, the credentials will be stored one way or another:
1571  * Make them permanent if the user asked us to. */
1572  if (creds->flags & CLIENT_AUTH_FLAG_IS_PERMANENT) {
1573  if (store_permanent_client_auth_credentials(creds) < 0) {
1574  client_service_authorization_free(creds);
1575  return REGISTER_FAIL_PERMANENT_STORAGE;
1576  }
1577  }
1578 
1579  old_creds = digest256map_get(client_auths, service_identity_pk.pubkey);
1580  if (old_creds) {
1581  digest256map_remove(client_auths, service_identity_pk.pubkey);
1582  client_service_authorization_free(old_creds);
1583  retval = REGISTER_SUCCESS_ALREADY_EXISTS;
1584  }
1585 
1586  digest256map_set(client_auths, service_identity_pk.pubkey, creds);
1587 
1588  /** Now that we set the new credentials, also try to decrypt any cached
1589  * descriptors. */
1590  if (hs_cache_client_new_auth_parse(&service_identity_pk)) {
1591  retval = REGISTER_SUCCESS_AND_DECRYPTED;
1592  }
1593 
1594  return retval;
1595 }
1596 
1597 /** Load a client authorization file with <b>filename</b> that is stored under
1598  * the global client auth directory, and return a newly-allocated credentials
1599  * object if it parsed well. Otherwise, return NULL.
1600  */
1603  const or_options_t *options)
1604 {
1605  hs_client_service_authorization_t *auth = NULL;
1606  char *client_key_file_path = NULL;
1607  char *client_key_str = NULL;
1608 
1609  log_info(LD_REND, "Loading a client authorization key file %s...",
1610  filename);
1611 
1612  if (!auth_key_filename_is_valid(filename)) {
1613  log_notice(LD_REND, "Client authorization unrecognized filename %s. "
1614  "File must end in .auth_private. Ignoring.",
1615  filename);
1616  goto err;
1617  }
1618 
1619  /* Create a full path for a file. */
1620  client_key_file_path = hs_path_from_filename(options->ClientOnionAuthDir,
1621  filename);
1622 
1623  client_key_str = read_file_to_str(client_key_file_path, 0, NULL);
1624  if (!client_key_str) {
1625  log_warn(LD_REND, "The file %s cannot be read.", filename);
1626  goto err;
1627  }
1628 
1629  auth = parse_auth_file_content(client_key_str);
1630  if (!auth) {
1631  goto err;
1632  }
1633 
1634  err:
1635  tor_free(client_key_str);
1636  tor_free(client_key_file_path);
1637 
1638  return auth;
1639 }
1640 
1641 /*
1642  * Remove the file in <b>filename</b> under the global client auth credential
1643  * storage.
1644  */
1645 static void
1646 remove_client_auth_creds_file(const char *filename)
1647 {
1648  char *creds_file_path = NULL;
1649  const or_options_t *options = get_options();
1650 
1651  creds_file_path = hs_path_from_filename(options->ClientOnionAuthDir,
1652  filename);
1653  if (tor_unlink(creds_file_path) != 0) {
1654  log_warn(LD_REND, "Failed to remove client auth file (%s).",
1655  creds_file_path);
1656  goto end;
1657  }
1658 
1659  log_warn(LD_REND, "Successfuly removed client auth file (%s).",
1660  creds_file_path);
1661 
1662  end:
1663  tor_free(creds_file_path);
1664 }
1665 
1666 /**
1667  * Find the filesystem file corresponding to the permanent client auth
1668  * credentials in <b>cred</b> and remove it.
1669  */
1670 static void
1673 {
1674  smartlist_t *file_list = NULL;
1675  const or_options_t *options = get_options();
1676 
1678 
1679  if (!options->ClientOnionAuthDir) {
1680  log_warn(LD_REND, "Found permanent credential but no ClientOnionAuthDir "
1681  "configured. There is no file to be removed.");
1682  goto end;
1683  }
1684 
1685  file_list = tor_listdir(options->ClientOnionAuthDir);
1686  if (file_list == NULL) {
1687  log_warn(LD_REND, "Client authorization key directory %s can't be listed.",
1688  options->ClientOnionAuthDir);
1689  goto end;
1690  }
1691 
1692  SMARTLIST_FOREACH_BEGIN(file_list, const char *, filename) {
1693  hs_client_service_authorization_t *tmp_cred = NULL;
1694 
1695  tmp_cred = get_creds_from_client_auth_filename(filename, options);
1696  if (!tmp_cred) {
1697  continue;
1698  }
1699 
1700  /* Find the right file for this credential */
1701  if (!strcmp(tmp_cred->onion_address, cred->onion_address)) {
1702  /* Found it! Remove the file! */
1703  remove_client_auth_creds_file(filename);
1704  /* cleanup and get out of here */
1705  client_service_authorization_free(tmp_cred);
1706  break;
1707  }
1708 
1709  client_service_authorization_free(tmp_cred);
1710  } SMARTLIST_FOREACH_END(filename);
1711 
1712  end:
1713  if (file_list) {
1714  SMARTLIST_FOREACH(file_list, char *, s, tor_free(s));
1715  smartlist_free(file_list);
1716  }
1717 }
1718 
1719 /** Remove client auth credentials for the service <b>hs_address</b>. */
1720 hs_client_removal_auth_status_t
1722 {
1723  ed25519_public_key_t service_identity_pk;
1724 
1725  if (!client_auths) {
1726  return REMOVAL_SUCCESS_NOT_FOUND;
1727  }
1728 
1729  if (hs_parse_address(hsaddress, &service_identity_pk, NULL, NULL) < 0) {
1730  return REMOVAL_BAD_ADDRESS;
1731  }
1732 
1733  hs_client_service_authorization_t *cred = NULL;
1734  cred = digest256map_remove(client_auths, service_identity_pk.pubkey);
1735 
1736  /* digestmap_remove() returns the previously stored data if there were any */
1737  if (cred) {
1738  if (cred->flags & CLIENT_AUTH_FLAG_IS_PERMANENT) {
1739  /* These creds are stored on disk: remove the corresponding file. */
1741  }
1742 
1743  /* Remove associated descriptor if any. */
1744  hs_cache_remove_as_client(&service_identity_pk);
1745 
1746  client_service_authorization_free(cred);
1747  return REMOVAL_SUCCESS;
1748  }
1749 
1750  return REMOVAL_SUCCESS_NOT_FOUND;
1751 }
1752 
1753 /** Get the HS client auth map. */
1754 digest256map_t *
1756 {
1757  return client_auths;
1758 }
1759 
1760 /* ========== */
1761 /* Public API */
1762 /* ========== */
1763 
1764 /** Called when a circuit was just cleaned up. This is done right before the
1765  * circuit is freed. */
1766 void
1768 {
1769  bool has_timed_out;
1770  rend_intro_point_failure_t failure = INTRO_POINT_FAILURE_GENERIC;
1771  const origin_circuit_t *orig_circ = NULL;
1772 
1773  tor_assert(circ);
1775 
1776  orig_circ = CONST_TO_ORIGIN_CIRCUIT(circ);
1777  tor_assert(orig_circ->hs_ident);
1778 
1779  has_timed_out =
1780  (circ->marked_for_close_orig_reason == END_CIRC_REASON_TIMEOUT);
1781  if (has_timed_out) {
1782  failure = INTRO_POINT_FAILURE_TIMEOUT;
1783  }
1784 
1785  switch (circ->purpose) {
1787  log_info(LD_REND, "Failed v3 intro circ for service %s to intro point %s "
1788  "(awaiting ACK). Failure code: %d",
1789  safe_str_client(ed25519_fmt(&orig_circ->hs_ident->identity_pk)),
1790  safe_str_client(build_state_get_exit_nickname(orig_circ->build_state)),
1791  failure);
1793  &orig_circ->hs_ident->intro_auth_pk,
1794  failure);
1795  break;
1797  if (has_timed_out || !orig_circ->build_state) {
1798  break;
1799  }
1800  failure = INTRO_POINT_FAILURE_UNREACHABLE;
1801  log_info(LD_REND, "Failed v3 intro circ for service %s to intro point %s "
1802  "(while building circuit). Marking as unreachable.",
1803  safe_str_client(ed25519_fmt(&orig_circ->hs_ident->identity_pk)),
1804  safe_str_client(build_state_get_exit_nickname(orig_circ->build_state)));
1806  &orig_circ->hs_ident->intro_auth_pk,
1807  failure);
1808  break;
1809  default:
1810  break;
1811  }
1812 }
1813 
1814 /** A circuit just finished connecting to a hidden service that the stream
1815  * <b>conn</b> has been waiting for. Let the HS subsystem know about this. */
1816 void
1818 {
1820 
1821  if (BUG(conn->rend_data && conn->hs_ident)) {
1822  log_warn(LD_BUG, "Stream had both rend_data and hs_ident..."
1823  "Prioritizing hs_ident");
1824  }
1825 
1826  if (conn->hs_ident) { /* It's v3: pass it to the prop224 handler */
1827  note_connection_attempt_succeeded(conn->hs_ident);
1828  return;
1829  } else if (conn->rend_data) { /* It's v2: pass it to the legacy handler */
1831  return;
1832  }
1833 }
1834 
1835 /** With the given encoded descriptor in desc_str and the service key in
1836  * service_identity_pk, decode the descriptor and set the desc pointer with a
1837  * newly allocated descriptor object.
1838  *
1839  * On success, HS_DESC_DECODE_OK is returned and desc is set to the decoded
1840  * descriptor. On error, desc is set to NULL and a decoding error status is
1841  * returned depending on what was the issue. */
1843 hs_client_decode_descriptor(const char *desc_str,
1844  const ed25519_public_key_t *service_identity_pk,
1845  hs_descriptor_t **desc)
1846 {
1848  uint8_t subcredential[DIGEST256_LEN];
1849  ed25519_public_key_t blinded_pubkey;
1850  hs_client_service_authorization_t *client_auth = NULL;
1851  curve25519_secret_key_t *client_auht_sk = NULL;
1852 
1853  tor_assert(desc_str);
1854  tor_assert(service_identity_pk);
1855  tor_assert(desc);
1856 
1857  /* Check if we have a client authorization for this service in the map. */
1858  client_auth = find_client_auth(service_identity_pk);
1859  if (client_auth) {
1860  client_auht_sk = &client_auth->enc_seckey;
1861  }
1862 
1863  /* Create subcredential for this HS so that we can decrypt */
1864  {
1865  uint64_t current_time_period = hs_get_time_period_num(0);
1866  hs_build_blinded_pubkey(service_identity_pk, NULL, 0, current_time_period,
1867  &blinded_pubkey);
1868  hs_get_subcredential(service_identity_pk, &blinded_pubkey, subcredential);
1869  }
1870 
1871  /* Parse descriptor */
1872  ret = hs_desc_decode_descriptor(desc_str, subcredential,
1873  client_auht_sk, desc);
1874  memwipe(subcredential, 0, sizeof(subcredential));
1875  if (ret != HS_DESC_DECODE_OK) {
1876  goto err;
1877  }
1878 
1879  /* Make sure the descriptor signing key cross certifies with the computed
1880  * blinded key. Without this validation, anyone knowing the subcredential
1881  * and onion address can forge a descriptor. */
1882  tor_cert_t *cert = (*desc)->plaintext_data.signing_key_cert;
1883  if (tor_cert_checksig(cert,
1884  &blinded_pubkey, approx_time()) < 0) {
1885  log_warn(LD_GENERAL, "Descriptor signing key certificate signature "
1886  "doesn't validate with computed blinded key: %s",
1888  ret = HS_DESC_DECODE_GENERIC_ERROR;
1889  goto err;
1890  }
1891 
1892  return HS_DESC_DECODE_OK;
1893  err:
1894  return ret;
1895 }
1896 
1897 /** Return true iff there are at least one usable intro point in the service
1898  * descriptor desc. */
1899 int
1901  const hs_descriptor_t *desc)
1902 {
1903  tor_assert(service_pk);
1904  tor_assert(desc);
1905 
1907  const hs_desc_intro_point_t *, ip) {
1908  if (intro_point_is_usable(service_pk, ip)) {
1909  goto usable;
1910  }
1911  } SMARTLIST_FOREACH_END(ip);
1912 
1913  return 0;
1914  usable:
1915  return 1;
1916 }
1917 
1918 /** Launch a connection to a hidden service directory to fetch a hidden
1919  * service descriptor using <b>identity_pk</b> to get the necessary keys.
1920  *
1921  * A hs_client_fetch_status_t code is returned. */
1922 int
1924 {
1925  hs_client_fetch_status_t status;
1926 
1927  tor_assert(identity_pk);
1928 
1929  if (!can_client_refetch_desc(identity_pk, &status)) {
1930  return status;
1931  }
1932 
1933  /* Try to fetch the desc and if we encounter an unrecoverable error, mark
1934  * the desc as unavailable for now. */
1935  status = fetch_v3_desc(identity_pk);
1936  if (fetch_status_should_close_socks(status)) {
1937  close_all_socks_conns_waiting_for_desc(identity_pk, status,
1938  END_STREAM_REASON_RESOLVEFAILED);
1939  /* Remove HSDir fetch attempts so that we can retry later if the user
1940  * wants us to regardless of if we closed any connections. */
1941  purge_hid_serv_request(identity_pk);
1942  }
1943  return status;
1944 }
1945 
1946 /** This is called when we are trying to attach an AP connection to these
1947  * hidden service circuits from connection_ap_handshake_attach_circuit().
1948  * Return 0 on success, -1 for a transient error that is actions were
1949  * triggered to recover or -2 for a permenent error where both circuits will
1950  * marked for close.
1951  *
1952  * The following supports every hidden service version. */
1953 int
1955  origin_circuit_t *rend_circ)
1956 {
1957  return (intro_circ->hs_ident) ? send_introduce1(intro_circ, rend_circ) :
1958  rend_client_send_introduction(intro_circ,
1959  rend_circ);
1960 }
1961 
1962 /** Called when the client circuit circ has been established. It can be either
1963  * an introduction or rendezvous circuit. This function handles all hidden
1964  * service versions. */
1965 void
1967 {
1968  tor_assert(circ);
1969 
1970  /* Handle both version. v2 uses rend_data and v3 uses the hs circuit
1971  * identifier hs_ident. Can't be both. */
1972  switch (TO_CIRCUIT(circ)->purpose) {
1974  if (circ->hs_ident) {
1976  } else {
1978  }
1979  break;
1981  if (circ->hs_ident) {
1983  } else {
1985  }
1986  break;
1987  default:
1988  tor_assert_nonfatal_unreached();
1989  }
1990 }
1991 
1992 /** Called when we receive a RENDEZVOUS_ESTABLISHED cell. Change the state of
1993  * the circuit to CIRCUIT_PURPOSE_C_REND_READY. Return 0 on success else a
1994  * negative value and the circuit marked for close. */
1995 int
1997  const uint8_t *payload, size_t payload_len)
1998 {
1999  tor_assert(circ);
2000  tor_assert(payload);
2001 
2002  (void) payload_len;
2003 
2004  if (TO_CIRCUIT(circ)->purpose != CIRCUIT_PURPOSE_C_ESTABLISH_REND) {
2005  log_warn(LD_PROTOCOL, "Got a RENDEZVOUS_ESTABLISHED but we were not "
2006  "expecting one. Closing circuit.");
2007  goto err;
2008  }
2009 
2010  log_info(LD_REND, "Received an RENDEZVOUS_ESTABLISHED. This circuit is "
2011  "now ready for rendezvous.");
2013 
2014  /* Set timestamp_dirty, because circuit_expire_building expects it to
2015  * specify when a circuit entered the _C_REND_READY state. */
2016  TO_CIRCUIT(circ)->timestamp_dirty = time(NULL);
2017 
2018  /* From a path bias point of view, this circuit is now successfully used.
2019  * Waiting any longer opens us up to attacks from malicious hidden services.
2020  * They could induce the client to attempt to connect to their hidden
2021  * service and never reply to the client's rend requests */
2023 
2024  /* If we already have the introduction circuit built, make sure we send
2025  * the INTRODUCE cell _now_ */
2027 
2028  return 0;
2029  err:
2030  circuit_mark_for_close(TO_CIRCUIT(circ), END_CIRC_REASON_TORPROTOCOL);
2031  return -1;
2032 }
2033 
2034 void
2035 client_service_authorization_free_(hs_client_service_authorization_t *auth)
2036 {
2037  if (!auth) {
2038  return;
2039  }
2040 
2041  memwipe(auth, 0, sizeof(*auth));
2042  tor_free(auth);
2043 }
2044 
2045 /** Helper for digest256map_free. */
2046 static void
2048 {
2049  client_service_authorization_free_(auth);
2050 }
2051 
2052 static void
2053 client_service_authorization_free_all(void)
2054 {
2055  if (!client_auths) {
2056  return;
2057  }
2059 }
2060 
2061 /** Check if the auth key file name is valid or not. Return 1 if valid,
2062  * otherwise return 0. */
2063 STATIC int
2064 auth_key_filename_is_valid(const char *filename)
2065 {
2066  int ret = 1;
2067  const char *valid_extension = ".auth_private";
2068 
2069  tor_assert(filename);
2070 
2071  /* The length of the filename must be greater than the length of the
2072  * extension and the valid extension must be at the end of filename. */
2073  if (!strcmpend(filename, valid_extension) &&
2074  strlen(filename) != strlen(valid_extension)) {
2075  ret = 1;
2076  } else {
2077  ret = 0;
2078  }
2079 
2080  return ret;
2081 }
2082 
2083 /** Parse the client auth credentials off a string in <b>client_key_str</b>
2084  * based on the file format documented in the "Client side configuration"
2085  * section of rend-spec-v3.txt.
2086  *
2087  * Return NULL if there was an error, otherwise return a newly allocated
2088  * hs_client_service_authorization_t structure.
2089  */
2091 parse_auth_file_content(const char *client_key_str)
2092 {
2093  char *onion_address = NULL;
2094  char *auth_type = NULL;
2095  char *key_type = NULL;
2096  char *seckey_b32 = NULL;
2097  hs_client_service_authorization_t *auth = NULL;
2098  smartlist_t *fields = smartlist_new();
2099 
2100  tor_assert(client_key_str);
2101 
2102  smartlist_split_string(fields, client_key_str, ":",
2103  SPLIT_SKIP_SPACE, 0);
2104  /* Wrong number of fields. */
2105  if (smartlist_len(fields) != 4) {
2106  goto err;
2107  }
2108 
2109  onion_address = smartlist_get(fields, 0);
2110  auth_type = smartlist_get(fields, 1);
2111  key_type = smartlist_get(fields, 2);
2112  seckey_b32 = smartlist_get(fields, 3);
2113 
2114  /* Currently, the only supported auth type is "descriptor" and the only
2115  * supported key type is "x25519". */
2116  if (strcmp(auth_type, "descriptor") || strcmp(key_type, "x25519")) {
2117  goto err;
2118  }
2119 
2120  if (strlen(seckey_b32) != BASE32_NOPAD_LEN(CURVE25519_SECKEY_LEN)) {
2121  log_warn(LD_REND, "Client authorization encoded base32 private key "
2122  "length is invalid: %s", seckey_b32);
2123  goto err;
2124  }
2125 
2126  auth = tor_malloc_zero(sizeof(hs_client_service_authorization_t));
2127  if (base32_decode((char *) auth->enc_seckey.secret_key,
2128  sizeof(auth->enc_seckey.secret_key),
2129  seckey_b32, strlen(seckey_b32)) !=
2130  sizeof(auth->enc_seckey.secret_key)) {
2131  log_warn(LD_REND, "Client authorization encoded base32 private key "
2132  "can't be decoded: %s", seckey_b32);
2133  goto err;
2134  }
2135  strncpy(auth->onion_address, onion_address, HS_SERVICE_ADDR_LEN_BASE32);
2136 
2137  /* We are reading this from the disk, so set the permanent flag anyway. */
2138  auth->flags |= CLIENT_AUTH_FLAG_IS_PERMANENT;
2139 
2140  /* Success. */
2141  goto done;
2142 
2143  err:
2144  client_service_authorization_free(auth);
2145  done:
2146  /* It is also a good idea to wipe the private key. */
2147  if (seckey_b32) {
2148  memwipe(seckey_b32, 0, strlen(seckey_b32));
2149  }
2150  tor_assert(fields);
2151  SMARTLIST_FOREACH(fields, char *, s, tor_free(s));
2152  smartlist_free(fields);
2153  return auth;
2154 }
2155 
2156 /** From a set of <b>options</b>, setup every client authorization detail
2157  * found. Return 0 on success or -1 on failure. If <b>validate_only</b>
2158  * is set, parse, warn and return as normal, but don't actually change
2159  * the configuration. */
2160 int
2162  int validate_only)
2163 {
2164  int ret = -1;
2165  digest256map_t *auths = digest256map_new();
2166  smartlist_t *file_list = NULL;
2167 
2168  tor_assert(options);
2169 
2170  /* There is no client auth configured. We can just silently ignore this
2171  * function. */
2172  if (!options->ClientOnionAuthDir) {
2173  ret = 0;
2174  goto end;
2175  }
2176 
2177  /* Make sure the directory exists and is private enough. */
2178  if (check_private_dir(options->ClientOnionAuthDir, 0, options->User) < 0) {
2179  goto end;
2180  }
2181 
2182  file_list = tor_listdir(options->ClientOnionAuthDir);
2183  if (file_list == NULL) {
2184  log_warn(LD_REND, "Client authorization key directory %s can't be listed.",
2185  options->ClientOnionAuthDir);
2186  goto end;
2187  }
2188 
2189  SMARTLIST_FOREACH_BEGIN(file_list, const char *, filename) {
2190  hs_client_service_authorization_t *auth = NULL;
2191  ed25519_public_key_t identity_pk;
2192 
2193  auth = get_creds_from_client_auth_filename(filename, options);
2194  if (!auth) {
2195  continue;
2196  }
2197 
2198  /* Parse the onion address to get an identity public key and use it
2199  * as a key of global map in the future. */
2200  if (hs_parse_address(auth->onion_address, &identity_pk,
2201  NULL, NULL) < 0) {
2202  log_warn(LD_REND, "The onion address \"%s\" is invalid in "
2203  "file %s", filename, auth->onion_address);
2204  client_service_authorization_free(auth);
2205  continue;
2206  }
2207 
2208  if (digest256map_get(auths, identity_pk.pubkey)) {
2209  log_warn(LD_REND, "Duplicate authorization for the same hidden "
2210  "service address %s.",
2211  safe_str_client_opts(options, auth->onion_address));
2212  client_service_authorization_free(auth);
2213  goto end;
2214  }
2215 
2216  digest256map_set(auths, identity_pk.pubkey, auth);
2217  log_info(LD_REND, "Loaded a client authorization key file %s.",
2218  filename);
2219  } SMARTLIST_FOREACH_END(filename);
2220 
2221  /* Success. */
2222  ret = 0;
2223 
2224  end:
2225  if (file_list) {
2226  SMARTLIST_FOREACH(file_list, char *, s, tor_free(s));
2227  smartlist_free(file_list);
2228  }
2229 
2230  if (!validate_only && ret == 0) {
2231  client_service_authorization_free_all();
2232  client_auths = auths;
2233  } else {
2234  digest256map_free(auths, client_service_authorization_free_void);
2235  }
2236 
2237  return ret;
2238 }
2239 
2240 /** Called when a descriptor directory fetch is done.
2241  *
2242  * Act accordingly on all entry connections depending on the HTTP status code
2243  * we got. In case of an error, the SOCKS error is set (if ExtendedErrors is
2244  * set).
2245  *
2246  * The reason is a human readable string returned by the directory server
2247  * which can describe the status of the request. The body is the response
2248  * content, on 200 code it is the descriptor itself. Finally, the status_code
2249  * is the HTTP code returned by the directory server. */
2250 void
2251 hs_client_dir_fetch_done(dir_connection_t *dir_conn, const char *reason,
2252  const char *body, const int status_code)
2253 {
2254  smartlist_t *entry_conns;
2255 
2256  tor_assert(dir_conn);
2257  tor_assert(body);
2258 
2259  /* Get all related entry connections. */
2260  entry_conns = find_entry_conns(&dir_conn->hs_ident->identity_pk);
2261 
2262  switch (status_code) {
2263  case 200:
2264  client_dir_fetch_200(dir_conn, entry_conns, body);
2265  break;
2266  case 404:
2267  client_dir_fetch_404(dir_conn, entry_conns);
2268  break;
2269  case 400:
2270  client_dir_fetch_400(dir_conn, reason);
2271  break;
2272  default:
2273  client_dir_fetch_unexpected(dir_conn, reason, status_code);
2274  break;
2275  }
2276 
2277  /* We don't have ownership of the objects in this list. */
2278  smartlist_free(entry_conns);
2279 }
2280 
2281 /** Return a newly allocated extend_info_t for a randomly chosen introduction
2282  * point for the given edge connection identifier ident. Return NULL if we
2283  * can't pick any usable introduction points. */
2284 extend_info_t *
2286 {
2287  tor_assert(edge_conn);
2288 
2289  return (edge_conn->hs_ident) ?
2290  client_get_random_intro(&edge_conn->hs_ident->identity_pk) :
2292 }
2293 
2294 /** Called when get an INTRODUCE_ACK cell on the introduction circuit circ.
2295  * Return 0 on success else a negative value is returned. The circuit will be
2296  * closed or reuse to extend again to another intro point. */
2297 int
2299  const uint8_t *payload, size_t payload_len)
2300 {
2301  int ret = -1;
2302 
2303  tor_assert(circ);
2304  tor_assert(payload);
2305 
2306  if (TO_CIRCUIT(circ)->purpose != CIRCUIT_PURPOSE_C_INTRODUCE_ACK_WAIT) {
2307  log_warn(LD_PROTOCOL, "Unexpected INTRODUCE_ACK on circuit %u.",
2308  (unsigned int) TO_CIRCUIT(circ)->n_circ_id);
2309  circuit_mark_for_close(TO_CIRCUIT(circ), END_CIRC_REASON_TORPROTOCOL);
2310  goto end;
2311  }
2312 
2313  ret = (circ->hs_ident) ? handle_introduce_ack(circ, payload, payload_len) :
2314  rend_client_introduction_acked(circ, payload,
2315  payload_len);
2316  /* For path bias: This circuit was used successfully. NACK or ACK counts. */
2318 
2319  end:
2320  return ret;
2321 }
2322 
2323 /** Called when get a RENDEZVOUS2 cell on the rendezvous circuit circ. Return
2324  * 0 on success else a negative value is returned. The circuit will be closed
2325  * on error. */
2326 int
2328  const uint8_t *payload, size_t payload_len)
2329 {
2330  int ret = -1;
2331 
2332  tor_assert(circ);
2333  tor_assert(payload);
2334 
2335  /* Circuit can possibly be in both state because we could receive a
2336  * RENDEZVOUS2 cell before the INTRODUCE_ACK has been received. */
2337  if (TO_CIRCUIT(circ)->purpose != CIRCUIT_PURPOSE_C_REND_READY &&
2339  log_warn(LD_PROTOCOL, "Unexpected RENDEZVOUS2 cell on circuit %u. "
2340  "Closing circuit.",
2341  (unsigned int) TO_CIRCUIT(circ)->n_circ_id);
2342  circuit_mark_for_close(TO_CIRCUIT(circ), END_CIRC_REASON_TORPROTOCOL);
2343  goto end;
2344  }
2345 
2346  log_info(LD_REND, "Got RENDEZVOUS2 cell from hidden service on circuit %u.",
2347  TO_CIRCUIT(circ)->n_circ_id);
2348 
2349  ret = (circ->hs_ident) ? handle_rendezvous2(circ, payload, payload_len) :
2350  rend_client_receive_rendezvous(circ, payload,
2351  payload_len);
2352  end:
2353  return ret;
2354 }
2355 
2356 /** Extend the introduction circuit circ to another valid introduction point
2357  * for the hidden service it is trying to connect to, or mark it and launch a
2358  * new circuit if we can't extend it. Return 0 on success or possible
2359  * success. Return -1 and mark the introduction circuit for close on permanent
2360  * failure.
2361  *
2362  * On failure, the caller is responsible for marking the associated rendezvous
2363  * circuit for close. */
2364 int
2366 {
2367  int ret = -1;
2368  extend_info_t *ei;
2369 
2370  tor_assert(circ);
2371 
2372  ei = (circ->hs_ident) ?
2375  if (ei == NULL) {
2376  log_warn(LD_REND, "No usable introduction points left. Closing.");
2377  circuit_mark_for_close(TO_CIRCUIT(circ), END_CIRC_REASON_INTERNAL);
2378  goto end;
2379  }
2380 
2381  if (circ->remaining_relay_early_cells) {
2382  log_info(LD_REND, "Re-extending circ %u, this time to %s.",
2383  (unsigned int) TO_CIRCUIT(circ)->n_circ_id,
2384  safe_str_client(extend_info_describe(ei)));
2385  ret = circuit_extend_to_new_exit(circ, ei);
2386  if (ret == 0) {
2387  /* We were able to extend so update the timestamp so we avoid expiring
2388  * this circuit too early. The intro circuit is short live so the
2389  * linkability issue is minimized, we just need the circuit to hold a
2390  * bit longer so we can introduce. */
2391  TO_CIRCUIT(circ)->timestamp_dirty = time(NULL);
2392  }
2393  } else {
2394  log_info(LD_REND, "Closing intro circ %u (out of RELAY_EARLY cells).",
2395  (unsigned int) TO_CIRCUIT(circ)->n_circ_id);
2396  circuit_mark_for_close(TO_CIRCUIT(circ), END_CIRC_REASON_FINISHED);
2397  /* connection_ap_handshake_attach_circuit will launch a new intro circ. */
2398  ret = 0;
2399  }
2400 
2401  end:
2402  extend_info_free(ei);
2403  return ret;
2404 }
2405 
2406 /** Close all client introduction circuits related to the given descriptor.
2407  * This is called with a descriptor that is about to get replaced in the
2408  * client cache.
2409  *
2410  * Even though the introduction point might be exactly the same, we'll rebuild
2411  * them if needed but the odds are very low that an existing matching
2412  * introduction circuit exists at that stage. */
2413 void
2415 {
2416  origin_circuit_t *ocirc = NULL;
2417 
2418  tor_assert(desc);
2419 
2420  /* We iterate over all client intro circuits because they aren't kept in the
2421  * HS circuitmap. That is probably something we want to do one day. */
2422  while ((ocirc = circuit_get_next_intro_circ(ocirc, true))) {
2423  if (ocirc->hs_ident == NULL) {
2424  /* Not a v3 circuit, ignore it. */
2425  continue;
2426  }
2427 
2428  /* Does it match any IP in the given descriptor? If not, ignore. */
2429  if (find_desc_intro_point_by_ident(ocirc->hs_ident, desc) == NULL) {
2430  continue;
2431  }
2432 
2433  /* We have a match. Close the circuit as consider it expired. */
2434  circuit_mark_for_close(TO_CIRCUIT(ocirc), END_CIRC_REASON_FINISHED);
2435  }
2436 }
2437 
2438 /** Release all the storage held by the client subsystem. */
2439 void
2441 {
2442  /* Purge the hidden service request cache. */
2444  client_service_authorization_free_all();
2445 }
2446 
2447 /** Purge all potentially remotely-detectable state held in the hidden
2448  * service client code. Called on SIGNAL NEWNYM. */
2449 void
2451 {
2452  /* v2 subsystem. */
2454 
2455  /* Cancel all descriptor fetches. Do this first so once done we are sure
2456  * that our descriptor cache won't modified. */
2457  cancel_descriptor_fetches();
2458  /* Purge the introduction point state cache. */
2460  /* Purge the descriptor cache. */
2462  /* Purge the last hidden service request cache. */
2464  /* Purge ephemeral client authorization. */
2466 
2467  log_info(LD_REND, "Hidden service client state has been purged.");
2468 }
2469 
2470 /** Called when our directory information has changed. */
2471 void
2473 {
2474  /* We have possibly reached the minimum directory information or new
2475  * consensus so retry all pending SOCKS connection in
2476  * AP_CONN_STATE_RENDDESC_WAIT state in order to fetch the descriptor. */
2478 }
2479 
2480 #ifdef TOR_UNIT_TESTS
2481 
2482 STATIC void
2483 set_hs_client_auths_map(digest256map_t *map)
2484 {
2485  client_auths = map;
2486 }
2487 
2488 #endif /* defined(TOR_UNIT_TESTS) */
2489 
uint32_t unreachable_count
Definition: hs_cache.h:37
#define CIRCUIT_PURPOSE_C_INTRODUCING
Definition: circuitlist.h:75
void hs_client_purge_state(void)
Definition: hs_client.c:2450
int rend_client_introduction_acked(origin_circuit_t *circ, const uint8_t *request, size_t request_len)
Definition: rendclient.c:376
static void close_all_socks_conns_waiting_for_desc(const ed25519_public_key_t *identity_pk, hs_client_fetch_status_t status, int reason)
Definition: hs_client.c:268
int hs_cell_parse_rendezvous2(const uint8_t *payload, size_t payload_len, uint8_t *handshake_info, size_t handshake_info_len)
Definition: hs_cell.c:1042
Header file for dirclient.c.
extend_info_t * hs_get_extend_info_from_lspecs(const smartlist_t *lspecs, const curve25519_public_key_t *onion_key, int direct_conn)
Definition: hs_common.c:1690
void hs_client_launch_v3_desc_fetch(const ed25519_public_key_t *onion_identity_pk, const smartlist_t *hsdirs)
Definition: hs_client.c:485
hs_client_register_auth_status_t hs_client_register_auth_credentials(hs_client_service_authorization_t *creds)
Definition: hs_client.c:1552
char identity_digest[DIGEST_LEN]
int routerset_contains_extendinfo(const routerset_t *set, const extend_info_t *ei)
Definition: routerset.c:293
static char * get_client_auth_creds_filename(const char *onion_address, const char *dir)
Definition: hs_client.c:1477
#define CIRCUIT_PURPOSE_C_REND_READY_INTRO_ACKED
Definition: circuitlist.h:88
Common functions for using (pseudo-)random number generators.
Definition: node_st.h:34
#define SMARTLIST_FOREACH_BEGIN(sl, type, var)
int hs_parse_address(const char *address, ed25519_public_key_t *key_out, uint8_t *checksum_out, uint8_t *version_out)
Definition: hs_common.c:914
#define TO_CONN(c)
Definition: or.h:735
int marked_for_close_orig_reason
Definition: circuit_st.h:201
uint8_t rendezvous_cookie[HS_REND_COOKIE_LEN]
Definition: hs_ident.h:60
static int directory_request_is_pending(const ed25519_public_key_t *identity_pk)
Definition: hs_client.c:218
#define MOCK_IMPL(rv, funcname, arglist)
Definition: testsupport.h:133
int check_private_dir(const char *dirname, cpd_check_t check, const char *effective_user)
Definition: dir.c:71
static digest256map_t * client_auths
Definition: hs_client.c:49
static void client_dir_fetch_404(dir_connection_t *dir_conn, const smartlist_t *entry_conns)
Definition: hs_client.c:1414
ed25519_public_key_t signed_key
Definition: torcert.h:30
unsigned int timed_out
Definition: hs_cache.h:34
int tor_cert_checksig(tor_cert_t *cert, const ed25519_public_key_t *pubkey, time_t now)
Definition: torcert.c:244
Header file containing client data for the HS subsytem.
extend_info_t * chosen_exit
void hs_control_desc_event_received(const hs_ident_dir_conn_t *ident, const char *hsdir_id_digest)
Definition: hs_control.c:89
const char * extend_info_describe(const extend_info_t *ei)
Definition: describe.c:204
static int handle_introduce_ack(origin_circuit_t *circ, const uint8_t *payload, size_t payload_len)
Definition: hs_client.c:1094
hs_desc_encrypted_data_t encrypted_data
void hs_ident_dir_conn_init(const ed25519_public_key_t *identity_pk, const ed25519_public_key_t *blinded_pk, hs_ident_dir_conn_t *ident)
Definition: hs_ident.c:69
Header file for connection.c.
int rend_client_send_introduction(origin_circuit_t *introcirc, origin_circuit_t *rendcirc)
Definition: rendclient.c:112
curve25519_keypair_t rendezvous_client_kp
Definition: hs_ident.h:72
int hs_client_send_introduce1(origin_circuit_t *intro_circ, origin_circuit_t *rend_circ)
Definition: hs_client.c:1954
void hs_client_dir_info_changed(void)
Definition: hs_client.c:2472
static void client_desc_has_arrived(const smartlist_t *entry_conns)
Definition: hs_client.c:1294
#define LD_GENERAL
Definition: log.h:62
#define CIRCUIT_IS_ORIGIN(c)
Definition: circuitlist.h:146
static int intro_circ_is_ok(const origin_circuit_t *circ)
Definition: hs_client.c:504
curve25519_secret_key_t enc_seckey
Definition: hs_client.h:69
Header file for describe.c.
Header file for nodelist.c.
void rend_client_note_connection_attempt_ended(const rend_data_t *rend_data)
Definition: rendclient.c:967
uint8_t subcredential[DIGEST256_LEN]
uint8_t purpose
Definition: circuit_st.h:111
int hs_cell_parse_introduce_ack(const uint8_t *payload, size_t payload_len)
Definition: hs_cell.c:1007
Header file for directory.c.
void smartlist_add(smartlist_t *sl, void *element)
int strcmpend(const char *s1, const char *s2)
Definition: util_string.c:242
char * hs_path_from_filename(const char *directory, const char *filename)
Definition: hs_common.c:177
static int close_or_reextend_intro_circ(origin_circuit_t *intro_circ)
Definition: hs_client.c:972
static void client_dir_fetch_400(dir_connection_t *dir_conn, const char *reason)
Definition: hs_client.c:1438
#define ED25519_BASE64_LEN
Definition: x25519_sizes.h:40
#define AP_CONN_STATE_RENDDESC_WAIT
void hs_cache_purge_as_client(void)
Definition: hs_cache.c:899
struct directory_request_t directory_request_t
Definition: dirclient.h:50
int hs_ident_intro_circ_is_valid(const hs_ident_circuit_t *ident)
Definition: hs_ident.c:104
#define TO_CIRCUIT(x)
Definition: or.h:951
#define CIRCUIT_PURPOSE_C_REND_READY
Definition: circuitlist.h:85
Header file for config.c.
const or_options_t * get_options(void)
Definition: config.c:926
#define tor_assert(expr)
Definition: util_bug.h:102
void base32_encode(char *dest, size_t destlen, const char *src, size_t srclen)
Definition: binascii.c:60
const hs_cache_intro_state_t * hs_cache_client_intro_state_find(const ed25519_public_key_t *service_pk, const ed25519_public_key_t *auth_key)
Definition: hs_cache.c:939
smartlist_t * tor_listdir(const char *dirname)
Definition: dir.c:307
unsigned int error
Definition: hs_cache.h:31
static hs_client_service_authorization_t * get_creds_from_client_auth_filename(const char *filename, const or_options_t *options)
Definition: hs_client.c:1602
void ed25519_public_to_base64(char *output, const ed25519_public_key_t *pkey)
int hs_client_reextend_intro_circuit(origin_circuit_t *circ)
Definition: hs_client.c:2365
void connection_ap_mark_as_waiting_for_renddesc(entry_connection_t *entry_conn)
hs_desc_decode_status_t hs_desc_decode_descriptor(const char *encoded, const uint8_t *subcredential, const curve25519_secret_key_t *client_auth_sk, hs_descriptor_t **desc_out)
static int intro_point_is_usable(const ed25519_public_key_t *service_pk, const hs_desc_intro_point_t *ip)
Definition: hs_client.c:823
#define MAP_DEL_CURRENT(keyvar)
Definition: map.h:140
#define tor_free(p)
Definition: malloc.h:52
static const hs_desc_intro_point_t * find_desc_intro_point_by_ident(const hs_ident_circuit_t *ident, const hs_descriptor_t *desc)
Definition: hs_client.c:531
static void handle_introduce_ack_success(origin_circuit_t *intro_circ)
Definition: hs_client.c:1024
networkstatus_t * networkstatus_get_live_consensus(time_t now)
int hs_ntor_client_rendezvous2_mac_is_good(const hs_ntor_rend_cell_keys_t *hs_ntor_rend_cell_keys, const uint8_t *rcvd_mac)
Definition: hs_ntor.c:568
void hs_client_dir_fetch_done(dir_connection_t *dir_conn, const char *reason, const char *body, const int status_code)
Definition: hs_client.c:2251
void memwipe(void *mem, uint8_t byte, size_t sz)
Definition: crypto_util.c:55
void directory_request_set_resource(directory_request_t *req, const char *resource)
Definition: dirclient.c:1039
smartlist_t * smartlist_new(void)
struct routerset_t * ExcludeNodes
Definition: or_options_st.h:89
static int store_permanent_client_auth_credentials(const hs_client_service_authorization_t *creds)
Definition: hs_client.c:1496
void hs_get_responsible_hsdirs(const ed25519_public_key_t *blinded_pk, uint64_t time_period_num, int use_second_hsdir_index, int for_fetching, smartlist_t *responsible_dirs)
Definition: hs_common.c:1317
void hs_purge_last_hid_serv_requests(void)
Definition: hs_common.c:1575
edge_connection_t * TO_EDGE_CONN(connection_t *c)
#define STATIC
Definition: testsupport.h:32
void hs_control_desc_event_failed(const hs_ident_dir_conn_t *ident, const char *hsdir_id_digest, const char *reason)
Definition: hs_control.c:65
int router_have_minimum_dir_info(void)
Definition: nodelist.c:2324
int write_str_to_file(const char *fname, const char *str, int bin)
Definition: files.c:258
void hs_client_close_intro_circuits_from_desc(const hs_descriptor_t *desc)
Definition: hs_client.c:2414
Circuit-build-stse structure.
static void client_desc_missing_bad_client_auth(const smartlist_t *entry_conns, hs_desc_decode_status_t status)
Definition: hs_client.c:1338
void connection_ap_attach_pending(int retry)
#define HS_VERSION_THREE
Definition: hs_common.h:26
Header file for routerset.c.
static int send_introduce1(origin_circuit_t *intro_circ, origin_circuit_t *rend_circ)
Definition: hs_client.c:595
directory_request_t * directory_request_new(uint8_t dir_purpose)
Definition: dirclient.c:942
unsigned int remaining_relay_early_cells
void hs_control_desc_event_content(const hs_ident_dir_conn_t *ident, const char *hsdir_id_digest, const char *body)
Definition: hs_control.c:178
#define DIGEST256_LEN
Definition: digest_sizes.h:23
void hs_client_circuit_cleanup_on_free(const circuit_t *circ)
Definition: hs_client.c:1767
char * ClientOnionAuthDir
int ed25519_pubkey_eq(const ed25519_public_key_t *key1, const ed25519_public_key_t *key2)
extend_info_t * hs_client_get_random_intro_from_edge(const edge_connection_t *edge_conn)
Definition: hs_client.c:2285
int base32_decode(char *dest, size_t destlen, const char *src, size_t srclen)
Definition: binascii.c:90
hs_desc_decode_status_t hs_client_decode_descriptor(const char *desc_str, const ed25519_public_key_t *service_identity_pk, hs_descriptor_t **desc)
Definition: hs_client.c:1843
hs_desc_plaintext_data_t plaintext_data
const char * ed25519_fmt(const ed25519_public_key_t *pkey)
Common functions for cryptographic routines.
int hs_client_receive_introduce_ack(origin_circuit_t *circ, const uint8_t *payload, size_t payload_len)
Definition: hs_client.c:2298
Header for hs_ntor.c.
static void client_service_authorization_free_void(void *auth)
Definition: hs_client.c:2047
static void setup_intro_circ_auth_key(origin_circuit_t *circ)
Definition: hs_client.c:709
void hs_client_free_all(void)
Definition: hs_client.c:2440
Header for crypto_format.c.
int hs_circ_send_establish_rendezvous(origin_circuit_t *circ)
Definition: hs_circuit.c:1171
static void handle_introduce_ack_bad(origin_circuit_t *circ, int status)
Definition: hs_client.c:1071
origin_circuit_t * hs_circuitmap_get_established_rend_circ_client_side(const uint8_t *cookie)
const char * safe_str_client_opts(const or_options_t *options, const char *address)
Definition: config.c:1079
int tor_asprintf(char **strp, const char *fmt,...)
Definition: printf.c:75
curve25519_public_key_t onion_key
origin_circuit_t * circuit_get_next_intro_circ(const origin_circuit_t *start, bool want_client_circ)
Definition: circuitlist.c:1723
dir_connection_t * TO_DIR_CONN(connection_t *c)
Definition: directory.c:85
#define DIGEST_LEN
Definition: digest_sizes.h:20
#define CIRCUIT_PURPOSE_C_INTRODUCE_ACKED
Definition: circuitlist.h:81
Origin circuit structure.
static void purge_hid_serv_request(const ed25519_public_key_t *identity_pk)
Definition: hs_client.c:197
Header file containing cell data for the whole HS subsytem.
void ed25519_pubkey_copy(ed25519_public_key_t *dest, const ed25519_public_key_t *src)
int hs_client_receive_rendezvous2(origin_circuit_t *circ, const uint8_t *payload, size_t payload_len)
Definition: hs_client.c:2327
static void flag_all_conn_wait_desc(const ed25519_public_key_t *service_identity_pk)
Definition: hs_client.c:171
Master header file for Tor-specific functionality.
#define DIR_PURPOSE_FETCH_HSDESC
Definition: directory.h:74
Header file for circuitbuild.c.
void hs_cache_client_intro_state_note(const ed25519_public_key_t *service_pk, const ed25519_public_key_t *auth_key, rend_intro_point_failure_t failure)
Definition: hs_cache.c:917
STATIC extend_info_t * desc_intro_point_to_extend_info(const hs_desc_intro_point_t *ip)
Definition: hs_client.c:801
#define fast_memneq(a, b, c)
Definition: di_ops.h:42
int crypto_rand_int(unsigned int max)
#define AP_CONN_STATE_CIRCUIT_WAIT
const node_t * node_get_by_id(const char *identity_digest)
Definition: nodelist.c:223
Header file containing circuit and connection identifier data for the whole HS subsytem.
STATIC routerstatus_t * pick_hsdir_v3(const ed25519_public_key_t *onion_identity_pk)
Definition: hs_client.c:429
static void client_dir_fetch_200(dir_connection_t *dir_conn, const smartlist_t *entry_conns, const char *body)
Definition: hs_client.c:1362
Entry connection structure.
char onion_address[HS_SERVICE_ADDR_LEN_BASE32+1]
Definition: hs_client.h:72
void hs_get_subcredential(const ed25519_public_key_t *identity_pk, const ed25519_public_key_t *blinded_pk, uint8_t *subcred_out)
Definition: hs_common.c:814
ed25519_public_key_t identity_pk
Definition: hs_ident.h:106
static void client_intro_circ_has_opened(origin_circuit_t *circ)
Definition: hs_client.c:747
const char * routerstatus_describe(const routerstatus_t *rs)
Definition: describe.c:184
static void client_dir_fetch_unexpected(dir_connection_t *dir_conn, const char *reason, const int status_code)
Definition: hs_client.c:1456
Header file for circuituse.c.
ed25519_public_key_t identity_pk
Definition: hs_ident.h:45
ed25519_public_key_t intro_auth_pk
Definition: hs_ident.h:51
Header file for hs_descriptor.c.
Extend-info structure.
socks5_reply_status_t
Definition: socks5_status.h:20
#define CIRCUIT_PURPOSE_C_ESTABLISH_REND
Definition: circuitlist.h:83
STATIC hs_client_fetch_status_t fetch_v3_desc(const ed25519_public_key_t *onion_identity_pk)
Definition: hs_client.c:466
void hs_build_blinded_pubkey(const ed25519_public_key_t *pk, const uint8_t *secret, size_t secret_len, uint64_t time_period_num, ed25519_public_key_t *blinded_pk_out)
Definition: hs_common.c:1021
time_t timestamp_created
static unsigned int can_client_refetch_desc(const ed25519_public_key_t *identity_pk, hs_client_fetch_status_t *status_out)
Definition: hs_client.c:1195
void smartlist_del(smartlist_t *sl, int idx)
Header file for hs_circuitmap.c.
#define LD_REND
Definition: log.h:84
Header file for circuitlist.c.
uint16_t marked_for_close
static hs_desc_intro_point_t * find_desc_intro_point_by_legacy_id(const char *legacy_id, const hs_descriptor_t *desc)
Definition: hs_client.c:555
STATIC void retry_all_socks_conn_waiting_for_desc(void)
Definition: hs_client.c:298
static void note_connection_attempt_succeeded(const hs_ident_edge_conn_t *hs_conn_ident)
Definition: hs_client.c:354
hs_client_removal_auth_status_t hs_client_remove_auth_credentials(const char *hsaddress)
Definition: hs_client.c:1721
void directory_request_fetch_set_hs_ident(directory_request_t *req, const hs_ident_dir_conn_t *ident)
Definition: dirclient.c:1119
const char * tor_cert_describe_signature_status(const tor_cert_t *cert)
Definition: torcert.c:279
static void find_and_remove_client_auth_creds_file(const hs_client_service_authorization_t *cred)
Definition: hs_client.c:1671
static hs_client_service_authorization_t * find_client_auth(const ed25519_public_key_t *service_identity_pk)
Definition: hs_client.c:1280
curve25519_public_key_t enc_key
Client/server directory connection structure.
int connection_edge_is_rendezvous_stream(const edge_connection_t *conn)
static const char * fetch_status_to_string(hs_client_fetch_status_t status)
Definition: hs_client.c:55
#define DIGESTMAP_FOREACH_END
Definition: map.h:168
int hs_circ_send_introduce1(origin_circuit_t *intro_circ, origin_circuit_t *rend_circ, const hs_desc_intro_point_t *ip, const uint8_t *subcredential)
Definition: hs_circuit.c:1092
void hs_build_address(const ed25519_public_key_t *key, uint8_t version, char *addr_out)
Definition: hs_common.c:995
#define CLIENT_AUTH_FLAG_IS_PERMANENT
Definition: hs_client.h:63
int hs_client_receive_rendezvous_acked(origin_circuit_t *circ, const uint8_t *payload, size_t payload_len)
Definition: hs_client.c:1996
#define ENTRY_TO_EDGE_CONN(c)
Header file containing circuit data for the whole HS subsytem.
Header file for connection_edge.c.
rend_data_t * rend_data
origin_circuit_t * hs_circuitmap_get_rend_circ_client_side(const uint8_t *cookie)
void assert_connection_ok(connection_t *conn, time_t now)
Definition: connection.c:5354
Header file containing control port event related code.
void hs_control_desc_event_requested(const ed25519_public_key_t *onion_pk, const char *base64_blinded_pk, const routerstatus_t *hsdir_rs)
Definition: hs_control.c:29
int hs_circuit_setup_e2e_rend_circ(origin_circuit_t *circ, const uint8_t *ntor_key_seed, size_t seed_len, int is_service_side)
Definition: hs_circuit.c:1036
time_t timestamp_last_read_allowed
int node_supports_v3_rendezvous_point(const node_t *node)
Definition: nodelist.c:1210
static void mark_conn_as_waiting_for_circuit(connection_t *conn, time_t now)
Definition: hs_client.c:247
#define SMARTLIST_FOREACH(sl, type, var, cmd)
void hs_purge_hid_serv_from_last_hid_serv_requests(const char *req_key_str)
Definition: hs_common.c:1534
#define MAX_INTRO_POINT_REACHABILITY_FAILURES
Definition: or.h:1065
const char * escaped(const char *s)
Definition: escape.c:126
#define HS_SERVICE_ADDR_LEN_BASE32
Definition: hs_common.h:83
void hs_client_note_connection_attempt_succeeded(const edge_connection_t *conn)
Definition: hs_client.c:1817
#define CIRCUIT_PURPOSE_C_INTRODUCE_ACK_WAIT
Definition: circuitlist.h:78
const char * stream_end_reason_to_string(int reason)
Definition: reasons.c:64
hs_desc_decode_status_t hs_cache_store_as_client(const char *desc_str, const ed25519_public_key_t *identity_pk)
Definition: hs_cache.c:820
cpath_build_state_t * build_state
STATIC void purge_ephemeral_client_auth(void)
Definition: hs_client.c:1262
#define CIRCUIT_PURPOSE_C_REND_JOINED
Definition: circuitlist.h:90
int hs_client_refetch_hsdesc(const ed25519_public_key_t *identity_pk)
Definition: hs_client.c:1923
char identity_digest[DIGEST_LEN]
rend_data_t * rend_data
void hs_client_circuit_has_opened(origin_circuit_t *circ)
Definition: hs_client.c:1966
void directory_initiate_request(directory_request_t *request)
Definition: dirclient.c:1229
#define log_fn(severity, domain, args,...)
Definition: log.h:287
smartlist_t * link_specifiers
void smartlist_add_all(smartlist_t *s1, const smartlist_t *s2)
curve25519_public_key_t intro_enc_pk
Definition: hs_ident.h:55
const hs_descriptor_t * hs_cache_lookup_as_client(const ed25519_public_key_t *key)
Definition: hs_cache.c:789
void hs_cache_remove_as_client(const ed25519_public_key_t *key)
Definition: hs_cache.c:856
smartlist_t * intro_points
time_t approx_time(void)
Definition: approx_time.c:32
STATIC int handle_rendezvous2(origin_circuit_t *circ, const uint8_t *payload, size_t payload_len)
Definition: hs_client.c:1132
#define DIR_PURPOSE_HAS_FETCHED_HSDESC
Definition: directory.h:77
socks5_reply_status_t socks_extended_error_code
void rend_client_purge_state(void)
Definition: rendclient.c:56
int tor_unlink(const char *pathname)
Definition: files.c:154
void directory_request_set_indirection(directory_request_t *req, dir_indirection_t indirection)
Definition: dirclient.c:1026
void pathbias_count_use_attempt(origin_circuit_t *circ)
Definition: circpathbias.c:604
Header file for hs_cache.c.
Client request structure.
void circuit_change_purpose(circuit_t *circ, uint8_t new_purpose)
Definition: circuituse.c:3095
routerstatus_t * hs_pick_hsdir(smartlist_t *responsible_dirs, const char *req_key_str, bool *is_rate_limited_out)
Definition: hs_common.c:1603
void rend_client_rendcirc_has_opened(origin_circuit_t *circ)
Definition: rendclient.c:337
Header file for reasons.c.
#define CONN_TYPE_DIR
Definition: connection.h:53
STATIC int auth_key_filename_is_valid(const char *filename)
Definition: hs_client.c:2064
void hs_circuitmap_register_rend_circ_client_side(origin_circuit_t *or_circ, const uint8_t *cookie)
uint64_t hs_get_time_period_num(time_t now)
Definition: hs_common.c:268
static hs_client_fetch_status_t directory_launch_v3_desc_fetch(const ed25519_public_key_t *onion_identity_pk, const routerstatus_t *hsdir)
Definition: hs_client.c:377
int rend_client_receive_rendezvous(origin_circuit_t *circ, const uint8_t *request, size_t request_len)
Definition: rendclient.c:891
int hs_config_client_authorization(const or_options_t *options, int validate_only)
Definition: hs_client.c:2161
extend_info_t * rend_client_get_random_intro(const rend_data_t *rend_query)
Definition: rendclient.c:1009
struct hs_ident_circuit_t * hs_ident
#define CONN_TYPE_AP
Definition: connection.h:49
static void client_rendezvous_circ_has_opened(origin_circuit_t *circ)
Definition: hs_client.c:764
Header file for rendclient.c.
#define CONN_IS_EDGE(x)
socks_request_t * socks_request
static int fetch_status_should_close_socks(hs_client_fetch_status_t status)
Definition: hs_client.c:80
#define LD_PROTOCOL
Definition: log.h:72
void directory_request_set_routerstatus(directory_request_t *req, const routerstatus_t *status)
Definition: dirclient.c:1156
const char * build_state_get_exit_nickname(cpath_build_state_t *state)
void pathbias_mark_use_success(origin_circuit_t *circ)
Definition: circpathbias.c:661
ed25519_public_key_t identity_pk
Definition: hs_ident.h:90
void hs_cache_client_intro_state_purge(void)
Definition: hs_cache.c:969
digest256map_t * get_hs_client_auths_map(void)
Definition: hs_client.c:1755
STATIC extend_info_t * client_get_random_intro(const ed25519_public_key_t *service_pk)
Definition: hs_client.c:863
time_t timestamp_last_write_allowed
uint8_t state
Definition: connection_st.h:49
hs_desc_decode_status_t
Definition: hs_descriptor.h:73
STATIC hs_client_service_authorization_t * parse_auth_file_content(const char *client_key_str)
Definition: hs_client.c:2091
Header file for networkstatus.c.
#define LD_BUG
Definition: log.h:86
int hs_client_any_intro_points_usable(const ed25519_public_key_t *service_pk, const hs_descriptor_t *desc)
Definition: hs_client.c:1900
#define CURVE25519_SECKEY_LEN
Definition: x25519_sizes.h:22
hs_client_fetch_status_t
Definition: hs_client.h:19
int circuit_extend_to_new_exit(origin_circuit_t *circ, extend_info_t *exit_ei)
#define CURVE25519_PUBKEY_LEN
Definition: x25519_sizes.h:20
void rend_client_introcirc_has_opened(origin_circuit_t *circ)
Definition: rendclient.c:67
tor_cert_t * auth_key_cert
int smartlist_split_string(smartlist_t *sl, const char *str, const char *sep, int flags, int max)