tor  0.4.2.1-alpha-dev
channeltls.c
Go to the documentation of this file.
1 /* * Copyright (c) 2012-2019, The Tor Project, Inc. */
2 /* See LICENSE for licensing information */
3 
33 /*
34  * Define this so channel.h gives us things only channel_t subclasses
35  * should touch.
36  */
37 #define TOR_CHANNEL_INTERNAL_
38 
39 #define CHANNELTLS_PRIVATE
40 
41 #include "core/or/or.h"
42 #include "core/or/channel.h"
43 #include "core/or/channeltls.h"
44 #include "core/or/circuitmux.h"
46 #include "core/or/command.h"
47 #include "app/config/config.h"
49 #include "core/or/connection_or.h"
52 #include "trunnel/link_handshake.h"
53 #include "core/or/relay.h"
54 #include "feature/stats/rephist.h"
55 #include "feature/relay/router.h"
58 #include "core/or/scheduler.h"
59 #include "feature/nodelist/torcert.h"
61 #include "trunnel/channelpadding_negotiation.h"
62 #include "trunnel/netinfo.h"
63 #include "core/or/channelpadding.h"
64 
65 #include "core/or/cell_st.h"
66 #include "core/or/cell_queue_st.h"
67 #include "core/or/extend_info_st.h"
68 #include "core/or/or_connection_st.h"
69 #include "core/or/or_handshake_certs_st.h"
70 #include "core/or/or_handshake_state_st.h"
71 #include "feature/nodelist/routerinfo_st.h"
72 #include "core/or/var_cell_st.h"
73 
74 #include "lib/tls/tortls.h"
75 #include "lib/tls/x509.h"
76 
93 
96 
97 /* channel_tls_t method declarations */
98 
99 static void channel_tls_close_method(channel_t *chan);
100 static const char * channel_tls_describe_transport_method(channel_t *chan);
101 static void channel_tls_free_method(channel_t *chan);
103 static int
105 static int
106 channel_tls_get_transport_name_method(channel_t *chan, char **transport_out);
107 static const char *
110 static int channel_tls_is_canonical_method(channel_t *chan, int req);
111 static int
113  extend_info_t *extend_info);
115  const tor_addr_t *target);
119  cell_t *cell);
121  packed_cell_t *packed_cell);
123  var_cell_t *var_cell);
124 
125 /* channel_listener_tls_t method declarations */
126 
128 static const char *
130 
135  channel_tls_t *tlschan);
136 static void channel_tls_process_netinfo_cell(cell_t *cell,
137  channel_tls_t *tlschan);
138 static int command_allowed_before_handshake(uint8_t command);
140  channel_tls_t *tlschan);
142  channel_tls_t *chan);
143 
148 STATIC void
149 channel_tls_common_init(channel_tls_t *tlschan)
150 {
151  channel_t *chan;
152 
153  tor_assert(tlschan);
154 
155  chan = &(tlschan->base_);
156  channel_init(chan);
157  chan->magic = TLS_CHAN_MAGIC;
158  chan->state = CHANNEL_STATE_OPENING;
163  chan->get_remote_addr = channel_tls_get_remote_addr_method;
165  chan->get_transport_name = channel_tls_get_transport_name_method;
170  chan->num_bytes_queued = channel_tls_num_bytes_queued_method;
171  chan->num_cells_writeable = channel_tls_num_cells_writeable_method;
172  chan->write_cell = channel_tls_write_cell_method;
175 
176  chan->cmux = circuitmux_alloc();
177  /* We only have one policy for now so always set it to EWMA. */
178  circuitmux_set_policy(chan->cmux, &ewma_policy);
179 }
180 
188 channel_t *
189 channel_tls_connect(const tor_addr_t *addr, uint16_t port,
190  const char *id_digest,
191  const ed25519_public_key_t *ed_id)
192 {
193  channel_tls_t *tlschan = tor_malloc_zero(sizeof(*tlschan));
194  channel_t *chan = &(tlschan->base_);
195 
196  channel_tls_common_init(tlschan);
197 
198  log_debug(LD_CHANNEL,
199  "In channel_tls_connect() for channel %p "
200  "(global id %"PRIu64 ")",
201  tlschan,
202  (chan->global_identifier));
203 
204  if (is_local_addr(addr)) {
205  log_debug(LD_CHANNEL,
206  "Marking new outgoing channel %"PRIu64 " at %p as local",
207  (chan->global_identifier), chan);
208  channel_mark_local(chan);
209  } else {
210  log_debug(LD_CHANNEL,
211  "Marking new outgoing channel %"PRIu64 " at %p as remote",
212  (chan->global_identifier), chan);
213  channel_mark_remote(chan);
214  }
215 
216  channel_mark_outgoing(chan);
217 
218  /* Set up or_connection stuff */
219  tlschan->conn = connection_or_connect(addr, port, id_digest, ed_id, tlschan);
220  /* connection_or_connect() will fill in tlschan->conn */
221  if (!(tlschan->conn)) {
222  chan->reason_for_closing = CHANNEL_CLOSE_FOR_ERROR;
223  channel_change_state(chan, CHANNEL_STATE_ERROR);
224  goto err;
225  }
226 
227  log_debug(LD_CHANNEL,
228  "Got orconn %p for channel with global id %"PRIu64,
229  tlschan->conn, (chan->global_identifier));
230 
231  goto done;
232 
233  err:
234  circuitmux_free(chan->cmux);
235  tor_free(tlschan);
236  chan = NULL;
237 
238  done:
239  /* If we got one, we should register it */
240  if (chan) channel_register(chan);
241 
242  return chan;
243 }
244 
253 {
254  return channel_tls_listener;
255 }
256 
265 {
266  channel_listener_t *listener;
267 
268  if (!channel_tls_listener) {
269  listener = tor_malloc_zero(sizeof(*listener));
270  channel_init_listener(listener);
271  listener->state = CHANNEL_LISTENER_STATE_LISTENING;
273  listener->describe_transport =
275 
276  channel_tls_listener = listener;
277 
278  log_debug(LD_CHANNEL,
279  "Starting TLS channel listener %p with global id %"PRIu64,
280  listener, (listener->global_identifier));
281 
282  channel_listener_register(listener);
283  } else listener = channel_tls_listener;
284 
285  return listener;
286 }
287 
294 void
296 {
297  channel_listener_t *old_listener = NULL;
298 
299  log_debug(LD_CHANNEL,
300  "Shutting down TLS channels...");
301 
302  if (channel_tls_listener) {
303  /*
304  * When we close it, channel_tls_listener will get nulled out, so save
305  * a pointer so we can free it.
306  */
307  old_listener = channel_tls_listener;
308  log_debug(LD_CHANNEL,
309  "Closing channel_tls_listener with ID %"PRIu64
310  " at %p.",
311  (old_listener->global_identifier),
312  old_listener);
313  channel_listener_unregister(old_listener);
314  channel_listener_mark_for_close(old_listener);
315  channel_listener_free(old_listener);
317  }
318 
319  log_debug(LD_CHANNEL,
320  "Done shutting down TLS channels");
321 }
322 
326 channel_t *
328 {
329  channel_tls_t *tlschan = tor_malloc_zero(sizeof(*tlschan));
330  channel_t *chan = &(tlschan->base_);
331 
332  tor_assert(orconn);
333  tor_assert(!(orconn->chan));
334 
335  channel_tls_common_init(tlschan);
336 
337  /* Link the channel and orconn to each other */
338  tlschan->conn = orconn;
339  orconn->chan = tlschan;
340 
341  if (is_local_addr(&(TO_CONN(orconn)->addr))) {
342  log_debug(LD_CHANNEL,
343  "Marking new incoming channel %"PRIu64 " at %p as local",
344  (chan->global_identifier), chan);
345  channel_mark_local(chan);
346  } else {
347  log_debug(LD_CHANNEL,
348  "Marking new incoming channel %"PRIu64 " at %p as remote",
349  (chan->global_identifier), chan);
350  channel_mark_remote(chan);
351  }
352 
353  channel_mark_incoming(chan);
354 
355  /* Register it */
356  channel_register(chan);
357 
358  return chan;
359 }
360 
361 /*********
362  * Casts *
363  ********/
364 
368 channel_t *
369 channel_tls_to_base(channel_tls_t *tlschan)
370 {
371  if (!tlschan) return NULL;
372 
373  return &(tlschan->base_);
374 }
375 
380 channel_tls_t *
382 {
383  if (!chan) return NULL;
384 
385  tor_assert(chan->magic == TLS_CHAN_MAGIC);
386 
387  return (channel_tls_t *)(chan);
388 }
389 
390 /********************************************
391  * Method implementations for channel_tls_t *
392  *******************************************/
393 
399 static void
401 {
402  channel_tls_t *tlschan = BASE_CHAN_TO_TLS(chan);
403 
404  tor_assert(tlschan);
405 
406  if (tlschan->conn) connection_or_close_normally(tlschan->conn, 1);
407  else {
408  /* Weird - we'll have to change the state ourselves, I guess */
409  log_info(LD_CHANNEL,
410  "Tried to close channel_tls_t %p with NULL conn",
411  tlschan);
412  channel_change_state(chan, CHANNEL_STATE_ERROR);
413  }
414 }
415 
422 static const char *
424 {
425  static char *buf = NULL;
426  uint64_t id;
427  channel_tls_t *tlschan;
428  const char *rv = NULL;
429 
430  tor_assert(chan);
431 
432  tlschan = BASE_CHAN_TO_TLS(chan);
433 
434  if (tlschan->conn) {
435  id = TO_CONN(tlschan->conn)->global_identifier;
436 
437  if (buf) tor_free(buf);
438  tor_asprintf(&buf,
439  "TLS channel (connection %"PRIu64 ")",
440  (id));
441 
442  rv = buf;
443  } else {
444  rv = "TLS channel (no connection)";
445  }
446 
447  return rv;
448 }
449 
460 static void
462 {
463  channel_tls_t *tlschan = BASE_CHAN_TO_TLS(chan);
464 
465  tor_assert(tlschan);
466 
467  if (tlschan->conn) {
468  tlschan->conn->chan = NULL;
469  tlschan->conn = NULL;
470  }
471 }
472 
476 static double
478 {
479  double overhead = 1.0;
480  channel_tls_t *tlschan = BASE_CHAN_TO_TLS(chan);
481 
482  tor_assert(tlschan);
483  tor_assert(tlschan->conn);
484 
485  /* Just return 1.0f if we don't have sensible data */
486  if (tlschan->conn->bytes_xmitted > 0 &&
487  tlschan->conn->bytes_xmitted_by_tls >=
488  tlschan->conn->bytes_xmitted) {
489  overhead = ((double)(tlschan->conn->bytes_xmitted_by_tls)) /
490  ((double)(tlschan->conn->bytes_xmitted));
491 
492  /*
493  * Never estimate more than 2.0; otherwise we get silly large estimates
494  * at the very start of a new TLS connection.
495  */
496  if (overhead > 2.0)
497  overhead = 2.0;
498  }
499 
500  log_debug(LD_CHANNEL,
501  "Estimated overhead ratio for TLS chan %"PRIu64 " is %f",
502  (chan->global_identifier), overhead);
503 
504  return overhead;
505 }
506 
514 static int
516 {
517  int rv = 0;
518  channel_tls_t *tlschan = BASE_CHAN_TO_TLS(chan);
519 
520  tor_assert(tlschan);
521  tor_assert(addr_out);
522 
523  if (tlschan->conn) {
524  tor_addr_copy(addr_out, &(tlschan->conn->real_addr));
525  rv = 1;
526  } else tor_addr_make_unspec(addr_out);
527 
528  return rv;
529 }
530 
539 static int
541 {
542  channel_tls_t *tlschan = BASE_CHAN_TO_TLS(chan);
543 
544  tor_assert(tlschan);
545  tor_assert(transport_out);
546  tor_assert(tlschan->conn);
547 
548  if (!tlschan->conn->ext_or_transport)
549  return -1;
550 
551  *transport_out = tor_strdup(tlschan->conn->ext_or_transport);
552  return 0;
553 }
554 
563 static const char *
565 {
566 #define MAX_DESCR_LEN 32
567 
568  static char buf[MAX_DESCR_LEN + 1];
569  channel_tls_t *tlschan = BASE_CHAN_TO_TLS(chan);
570  connection_t *conn;
571  const char *answer = NULL;
572  char *addr_str;
573 
574  tor_assert(tlschan);
575 
576  if (tlschan->conn) {
577  conn = TO_CONN(tlschan->conn);
578  switch (flags) {
579  case 0:
580  /* Canonical address with port*/
581  tor_snprintf(buf, MAX_DESCR_LEN + 1,
582  "%s:%u", conn->address, conn->port);
583  answer = buf;
584  break;
585  case GRD_FLAG_ORIGINAL:
586  /* Actual address with port */
587  addr_str = tor_addr_to_str_dup(&(tlschan->conn->real_addr));
588  tor_snprintf(buf, MAX_DESCR_LEN + 1,
589  "%s:%u", addr_str, conn->port);
590  tor_free(addr_str);
591  answer = buf;
592  break;
593  case GRD_FLAG_ADDR_ONLY:
594  /* Canonical address, no port */
595  strlcpy(buf, conn->address, sizeof(buf));
596  answer = buf;
597  break;
598  case GRD_FLAG_ORIGINAL|GRD_FLAG_ADDR_ONLY:
599  /* Actual address, no port */
600  addr_str = tor_addr_to_str_dup(&(tlschan->conn->real_addr));
601  strlcpy(buf, addr_str, sizeof(buf));
602  tor_free(addr_str);
603  answer = buf;
604  break;
605  default:
606  /* Something's broken in channel.c */
607  tor_assert_nonfatal_unreached_once();
608  }
609  } else {
610  strlcpy(buf, "(No connection)", sizeof(buf));
611  answer = buf;
612  }
613 
614  return answer;
615 }
616 
623 static int
625 {
626  size_t outbuf_len;
627  channel_tls_t *tlschan = BASE_CHAN_TO_TLS(chan);
628 
629  tor_assert(tlschan);
630  if (!(tlschan->conn)) {
631  log_info(LD_CHANNEL,
632  "something called has_queued_writes on a tlschan "
633  "(%p with ID %"PRIu64 " but no conn",
634  chan, (chan->global_identifier));
635  }
636 
637  outbuf_len = (tlschan->conn != NULL) ?
638  connection_get_outbuf_len(TO_CONN(tlschan->conn)) :
639  0;
640 
641  return (outbuf_len > 0);
642 }
643 
651 static int
653 {
654  int answer = 0;
655  channel_tls_t *tlschan = BASE_CHAN_TO_TLS(chan);
656 
657  tor_assert(tlschan);
658 
659  if (tlschan->conn) {
660  switch (req) {
661  case 0:
662  answer = tlschan->conn->is_canonical;
663  break;
664  case 1:
665  /*
666  * Is the is_canonical bit reliable? In protocols version 2 and up
667  * we get the canonical address from a NETINFO cell, but in older
668  * versions it might be based on an obsolete descriptor.
669  */
670  answer = (tlschan->conn->link_proto >= 2);
671  break;
672  default:
673  /* This shouldn't happen; channel.c is broken if it does */
674  tor_assert_nonfatal_unreached_once();
675  }
676  }
677  /* else return 0 for tlschan->conn == NULL */
678 
679  return answer;
680 }
681 
688 static int
690  extend_info_t *extend_info)
691 {
692  channel_tls_t *tlschan = BASE_CHAN_TO_TLS(chan);
693 
694  tor_assert(tlschan);
695  tor_assert(extend_info);
696 
697  /* Never match if we have no conn */
698  if (!(tlschan->conn)) {
699  log_info(LD_CHANNEL,
700  "something called matches_extend_info on a tlschan "
701  "(%p with ID %"PRIu64 " but no conn",
702  chan, (chan->global_identifier));
703  return 0;
704  }
705 
706  return (tor_addr_eq(&(extend_info->addr),
707  &(TO_CONN(tlschan->conn)->addr)) &&
708  (extend_info->port == TO_CONN(tlschan->conn)->port));
709 }
710 
718 static int
720  const tor_addr_t *target)
721 {
722  channel_tls_t *tlschan = BASE_CHAN_TO_TLS(chan);
723 
724  tor_assert(tlschan);
725  tor_assert(target);
726 
727  /* Never match if we have no conn */
728  if (!(tlschan->conn)) {
729  log_info(LD_CHANNEL,
730  "something called matches_target on a tlschan "
731  "(%p with ID %"PRIu64 " but no conn",
732  chan, (chan->global_identifier));
733  return 0;
734  }
735 
736  /* real_addr is the address this connection came from.
737  * base_.addr is updated by connection_or_init_conn_from_address()
738  * to be the address in the descriptor. It may be tempting to
739  * allow either address to be allowed, but if we did so, it would
740  * enable someone who steals a relay's keys to impersonate/MITM it
741  * from anywhere on the Internet! (Because they could make long-lived
742  * TLS connections from anywhere to all relays, and wait for them to
743  * be used for extends).
744  */
745  return tor_addr_eq(&(tlschan->conn->real_addr), target);
746 }
747 
752 static size_t
754 {
755  channel_tls_t *tlschan = BASE_CHAN_TO_TLS(chan);
756 
757  tor_assert(tlschan);
758  tor_assert(tlschan->conn);
759 
760  return connection_get_outbuf_len(TO_CONN(tlschan->conn));
761 }
762 
770 static int
772 {
773  size_t outbuf_len;
774  ssize_t n;
775  channel_tls_t *tlschan = BASE_CHAN_TO_TLS(chan);
776  size_t cell_network_size;
777 
778  tor_assert(tlschan);
779  tor_assert(tlschan->conn);
780 
781  cell_network_size = get_cell_network_size(tlschan->conn->wide_circ_ids);
782  outbuf_len = connection_get_outbuf_len(TO_CONN(tlschan->conn));
783  /* Get the number of cells */
784  n = CEIL_DIV(OR_CONN_HIGHWATER - outbuf_len, cell_network_size);
785  if (n < 0) n = 0;
786 #if SIZEOF_SIZE_T > SIZEOF_INT
787  if (n > INT_MAX) n = INT_MAX;
788 #endif
789 
790  return (int)n;
791 }
792 
799 static int
801 {
802  channel_tls_t *tlschan = BASE_CHAN_TO_TLS(chan);
803  int written = 0;
804 
805  tor_assert(tlschan);
806  tor_assert(cell);
807 
808  if (tlschan->conn) {
809  connection_or_write_cell_to_buf(cell, tlschan->conn);
810  ++written;
811  } else {
812  log_info(LD_CHANNEL,
813  "something called write_cell on a tlschan "
814  "(%p with ID %"PRIu64 " but no conn",
815  chan, (chan->global_identifier));
816  }
817 
818  return written;
819 }
820 
830 static int
832  packed_cell_t *packed_cell)
833 {
834  tor_assert(chan);
835  channel_tls_t *tlschan = BASE_CHAN_TO_TLS(chan);
836  size_t cell_network_size = get_cell_network_size(chan->wide_circ_ids);
837 
838  tor_assert(tlschan);
839  tor_assert(packed_cell);
840 
841  if (tlschan->conn) {
842  connection_buf_add(packed_cell->body, cell_network_size,
843  TO_CONN(tlschan->conn));
844  } else {
845  log_info(LD_CHANNEL,
846  "something called write_packed_cell on a tlschan "
847  "(%p with ID %"PRIu64 " but no conn",
848  chan, (chan->global_identifier));
849  return -1;
850  }
851 
852  return 0;
853 }
854 
861 static int
863 {
864  channel_tls_t *tlschan = BASE_CHAN_TO_TLS(chan);
865  int written = 0;
866 
867  tor_assert(tlschan);
868  tor_assert(var_cell);
869 
870  if (tlschan->conn) {
871  connection_or_write_var_cell_to_buf(var_cell, tlschan->conn);
872  ++written;
873  } else {
874  log_info(LD_CHANNEL,
875  "something called write_var_cell on a tlschan "
876  "(%p with ID %"PRIu64 " but no conn",
877  chan, (chan->global_identifier));
878  }
879 
880  return written;
881 }
882 
883 /*************************************************
884  * Method implementations for channel_listener_t *
885  ************************************************/
886 
892 static void
894 {
895  tor_assert(chan_l);
896 
897  /*
898  * Listeners we just go ahead and change state through to CLOSED, but
899  * make sure to check if they're channel_tls_listener to NULL it out.
900  */
901  if (chan_l == channel_tls_listener)
902  channel_tls_listener = NULL;
903 
904  if (!(chan_l->state == CHANNEL_LISTENER_STATE_CLOSING ||
905  chan_l->state == CHANNEL_LISTENER_STATE_CLOSED ||
906  chan_l->state == CHANNEL_LISTENER_STATE_ERROR)) {
907  channel_listener_change_state(chan_l, CHANNEL_LISTENER_STATE_CLOSING);
908  }
909 
910  if (chan_l->incoming_list) {
912  channel_t *, ichan) {
913  channel_mark_for_close(ichan);
914  } SMARTLIST_FOREACH_END(ichan);
915 
916  smartlist_free(chan_l->incoming_list);
917  chan_l->incoming_list = NULL;
918  }
919 
920  if (!(chan_l->state == CHANNEL_LISTENER_STATE_CLOSED ||
921  chan_l->state == CHANNEL_LISTENER_STATE_ERROR)) {
922  channel_listener_change_state(chan_l, CHANNEL_LISTENER_STATE_CLOSED);
923  }
924 }
925 
932 static const char *
934 {
935  tor_assert(chan_l);
936 
937  return "TLS channel (listening)";
938 }
939 
940 /*******************************************************
941  * Functions for handling events on an or_connection_t *
942  ******************************************************/
943 
950 void
952  or_connection_t *conn,
953  uint8_t state)
954 {
955  channel_t *base_chan;
956 
957  tor_assert(chan);
958  tor_assert(conn);
959  tor_assert(conn->chan == chan);
960  tor_assert(chan->conn == conn);
961 
962  base_chan = TLS_CHAN_TO_BASE(chan);
963 
964  /* Make sure the base connection state makes sense - shouldn't be error
965  * or closed. */
966 
967  tor_assert(CHANNEL_IS_OPENING(base_chan) ||
968  CHANNEL_IS_OPEN(base_chan) ||
969  CHANNEL_IS_MAINT(base_chan) ||
970  CHANNEL_IS_CLOSING(base_chan));
971 
972  /* Did we just go to state open? */
973  if (state == OR_CONN_STATE_OPEN) {
974  /*
975  * We can go to CHANNEL_STATE_OPEN from CHANNEL_STATE_OPENING or
976  * CHANNEL_STATE_MAINT on this.
977  */
978  channel_change_state_open(base_chan);
979  /* We might have just become writeable; check and tell the scheduler */
980  if (connection_or_num_cells_writeable(conn) > 0) {
982  }
983  } else {
984  /*
985  * Not open, so from CHANNEL_STATE_OPEN we go to CHANNEL_STATE_MAINT,
986  * otherwise no change.
987  */
988  if (CHANNEL_IS_OPEN(base_chan)) {
989  channel_change_state(base_chan, CHANNEL_STATE_MAINT);
990  }
991  }
992 }
993 
994 #ifdef KEEP_TIMING_STATS
995 
1003 static void
1004 channel_tls_time_process_cell(cell_t *cell, channel_tls_t *chan, int *time,
1005  void (*func)(cell_t *, channel_tls_t *))
1006 {
1007  struct timeval start, end;
1008  long time_passed;
1009 
1010  tor_gettimeofday(&start);
1011 
1012  (*func)(cell, chan);
1013 
1014  tor_gettimeofday(&end);
1015  time_passed = tv_udiff(&start, &end) ;
1016 
1017  if (time_passed > 10000) { /* more than 10ms */
1018  log_debug(LD_OR,"That call just took %ld ms.",time_passed/1000);
1019  }
1020 
1021  if (time_passed < 0) {
1022  log_info(LD_GENERAL,"That call took us back in time!");
1023  time_passed = 0;
1024  }
1025 
1026  *time += time_passed;
1027 }
1028 #endif /* defined(KEEP_TIMING_STATS) */
1029 
1043 void
1045 {
1046  channel_tls_t *chan;
1047  int handshaking;
1048 
1049 #ifdef KEEP_TIMING_STATS
1050 #define PROCESS_CELL(tp, cl, cn) STMT_BEGIN { \
1051  ++num ## tp; \
1052  channel_tls_time_process_cell(cl, cn, & tp ## time , \
1053  channel_tls_process_ ## tp ## _cell); \
1054  } STMT_END
1055 #else /* !(defined(KEEP_TIMING_STATS)) */
1056 #define PROCESS_CELL(tp, cl, cn) channel_tls_process_ ## tp ## _cell(cl, cn)
1057 #endif /* defined(KEEP_TIMING_STATS) */
1058 
1059  tor_assert(cell);
1060  tor_assert(conn);
1061 
1062  chan = conn->chan;
1063 
1064  if (!chan) {
1065  log_warn(LD_CHANNEL,
1066  "Got a cell_t on an OR connection with no channel");
1067  return;
1068  }
1069 
1070  handshaking = (TO_CONN(conn)->state != OR_CONN_STATE_OPEN);
1071 
1072  if (conn->base_.marked_for_close)
1073  return;
1074 
1075  /* Reject all but VERSIONS and NETINFO when handshaking. */
1076  /* (VERSIONS should actually be impossible; it's variable-length.) */
1077  if (handshaking && cell->command != CELL_VERSIONS &&
1078  cell->command != CELL_NETINFO) {
1079  log_fn(LOG_PROTOCOL_WARN, LD_PROTOCOL,
1080  "Received unexpected cell command %d in chan state %s / "
1081  "conn state %s; closing the connection.",
1082  (int)cell->command,
1083  channel_state_to_string(TLS_CHAN_TO_BASE(chan)->state),
1084  conn_state_to_string(CONN_TYPE_OR, TO_CONN(conn)->state));
1085  connection_or_close_for_error(conn, 0);
1086  return;
1087  }
1088 
1089  if (conn->base_.state == OR_CONN_STATE_OR_HANDSHAKING_V3)
1090  or_handshake_state_record_cell(conn, conn->handshake_state, cell, 1);
1091 
1092  /* We note that we're on the internet whenever we read a cell. This is
1093  * a fast operation. */
1096 
1097  if (TLS_CHAN_TO_BASE(chan)->padding_enabled)
1099 
1100  switch (cell->command) {
1101  case CELL_PADDING:
1103  if (TLS_CHAN_TO_BASE(chan)->padding_enabled)
1106  /* do nothing */
1107  break;
1108  case CELL_VERSIONS:
1110  break;
1111  case CELL_NETINFO:
1113  PROCESS_CELL(netinfo, cell, chan);
1114  break;
1115  case CELL_PADDING_NEGOTIATE:
1117  PROCESS_CELL(padding_negotiate, cell, chan);
1118  break;
1119  case CELL_CREATE:
1120  case CELL_CREATE_FAST:
1121  case CELL_CREATED:
1122  case CELL_CREATED_FAST:
1123  case CELL_RELAY:
1124  case CELL_RELAY_EARLY:
1125  case CELL_DESTROY:
1126  case CELL_CREATE2:
1127  case CELL_CREATED2:
1128  /*
1129  * These are all transport independent and we pass them up through the
1130  * channel_t mechanism. They are ultimately handled in command.c.
1131  */
1132  channel_process_cell(TLS_CHAN_TO_BASE(chan), cell);
1133  break;
1134  default:
1136  "Cell of unknown type (%d) received in channeltls.c. "
1137  "Dropping.",
1138  cell->command);
1139  break;
1140  }
1141 }
1142 
1160 void
1162 {
1163  channel_tls_t *chan;
1164 
1165 #ifdef KEEP_TIMING_STATS
1166  /* how many of each cell have we seen so far this second? needs better
1167  * name. */
1168  static int num_versions = 0, num_certs = 0;
1169  static time_t current_second = 0; /* from previous calls to time */
1170  time_t now = time(NULL);
1171 
1172  if (current_second == 0) current_second = now;
1173  if (now > current_second) { /* the second has rolled over */
1174  /* print stats */
1175  log_info(LD_OR,
1176  "At end of second: %d versions (%d ms), %d certs (%d ms)",
1177  num_versions, versions_time / ((now - current_second) * 1000),
1178  num_certs, certs_time / ((now - current_second) * 1000));
1179 
1180  num_versions = num_certs = 0;
1181  versions_time = certs_time = 0;
1182 
1183  /* remember which second it is, for next time */
1184  current_second = now;
1185  }
1186 #endif /* defined(KEEP_TIMING_STATS) */
1187 
1188  tor_assert(var_cell);
1189  tor_assert(conn);
1190 
1191  chan = conn->chan;
1192 
1193  if (!chan) {
1194  log_warn(LD_CHANNEL,
1195  "Got a var_cell_t on an OR connection with no channel");
1196  return;
1197  }
1198 
1199  if (TO_CONN(conn)->marked_for_close)
1200  return;
1201 
1202  switch (TO_CONN(conn)->state) {
1204  if (var_cell->command != CELL_VERSIONS) {
1205  log_fn(LOG_PROTOCOL_WARN, LD_PROTOCOL,
1206  "Received a cell with command %d in unexpected "
1207  "orconn state \"%s\" [%d], channel state \"%s\" [%d]; "
1208  "closing the connection.",
1209  (int)(var_cell->command),
1210  conn_state_to_string(CONN_TYPE_OR, TO_CONN(conn)->state),
1211  TO_CONN(conn)->state,
1212  channel_state_to_string(TLS_CHAN_TO_BASE(chan)->state),
1213  (int)(TLS_CHAN_TO_BASE(chan)->state));
1214  /*
1215  * The code in connection_or.c will tell channel_t to close for
1216  * error; it will go to CHANNEL_STATE_CLOSING, and then to
1217  * CHANNEL_STATE_ERROR when conn is closed.
1218  */
1219  connection_or_close_for_error(conn, 0);
1220  return;
1221  }
1222  break;
1224  /* If we're using bufferevents, it's entirely possible for us to
1225  * notice "hey, data arrived!" before we notice "hey, the handshake
1226  * finished!" And we need to be accepting both at once to handle both
1227  * the v2 and v3 handshakes. */
1228  /* But that should be happening any longer've disabled bufferevents. */
1229  tor_assert_nonfatal_unreached_once();
1230 
1231  /* fall through */
1233  if (!(command_allowed_before_handshake(var_cell->command))) {
1234  log_fn(LOG_PROTOCOL_WARN, LD_PROTOCOL,
1235  "Received a cell with command %d in unexpected "
1236  "orconn state \"%s\" [%d], channel state \"%s\" [%d]; "
1237  "closing the connection.",
1238  (int)(var_cell->command),
1239  conn_state_to_string(CONN_TYPE_OR, TO_CONN(conn)->state),
1240  (int)(TO_CONN(conn)->state),
1241  channel_state_to_string(TLS_CHAN_TO_BASE(chan)->state),
1242  (int)(TLS_CHAN_TO_BASE(chan)->state));
1243  /* see above comment about CHANNEL_STATE_ERROR */
1244  connection_or_close_for_error(conn, 0);
1245  return;
1246  } else {
1247  if (enter_v3_handshake_with_cell(var_cell, chan) < 0)
1248  return;
1249  }
1250  break;
1252  if (var_cell->command != CELL_AUTHENTICATE)
1254  var_cell, 1);
1255  break; /* Everything is allowed */
1256  case OR_CONN_STATE_OPEN:
1257  if (conn->link_proto < 3) {
1258  log_fn(LOG_PROTOCOL_WARN, LD_PROTOCOL,
1259  "Received a variable-length cell with command %d in orconn "
1260  "state %s [%d], channel state %s [%d] with link protocol %d; "
1261  "ignoring it.",
1262  (int)(var_cell->command),
1263  conn_state_to_string(CONN_TYPE_OR, TO_CONN(conn)->state),
1264  (int)(TO_CONN(conn)->state),
1265  channel_state_to_string(TLS_CHAN_TO_BASE(chan)->state),
1266  (int)(TLS_CHAN_TO_BASE(chan)->state),
1267  (int)(conn->link_proto));
1268  return;
1269  }
1270  break;
1271  default:
1272  log_fn(LOG_PROTOCOL_WARN, LD_PROTOCOL,
1273  "Received var-length cell with command %d in unexpected "
1274  "orconn state \"%s\" [%d], channel state \"%s\" [%d]; "
1275  "ignoring it.",
1276  (int)(var_cell->command),
1277  conn_state_to_string(CONN_TYPE_OR, TO_CONN(conn)->state),
1278  (int)(TO_CONN(conn)->state),
1279  channel_state_to_string(TLS_CHAN_TO_BASE(chan)->state),
1280  (int)(TLS_CHAN_TO_BASE(chan)->state));
1281  return;
1282  }
1283 
1284  /* We note that we're on the internet whenever we read a cell. This is
1285  * a fast operation. */
1287 
1288  /* Now handle the cell */
1289 
1290  switch (var_cell->command) {
1291  case CELL_VERSIONS:
1293  PROCESS_CELL(versions, var_cell, chan);
1294  break;
1295  case CELL_VPADDING:
1297  /* Do nothing */
1298  break;
1299  case CELL_CERTS:
1301  PROCESS_CELL(certs, var_cell, chan);
1302  break;
1303  case CELL_AUTH_CHALLENGE:
1305  PROCESS_CELL(auth_challenge, var_cell, chan);
1306  break;
1307  case CELL_AUTHENTICATE:
1309  PROCESS_CELL(authenticate, var_cell, chan);
1310  break;
1311  case CELL_AUTHORIZE:
1313  /* Ignored so far. */
1314  break;
1315  default:
1317  "Variable-length cell of unknown type (%d) received.",
1318  (int)(var_cell->command));
1319  break;
1320  }
1321 }
1322 
1333 void
1335 {
1336  channel_t *chan = NULL;
1337 
1338  tor_assert(conn);
1339  tor_assert(conn->chan);
1340 
1341  chan = TLS_CHAN_TO_BASE(conn->chan);
1342 
1343  if (is_local_addr(&(TO_CONN(conn)->addr))) {
1344  if (!channel_is_local(chan)) {
1345  log_debug(LD_CHANNEL,
1346  "Marking channel %"PRIu64 " at %p as local",
1347  (chan->global_identifier), chan);
1348  channel_mark_local(chan);
1349  }
1350  } else {
1351  if (channel_is_local(chan)) {
1352  log_debug(LD_CHANNEL,
1353  "Marking channel %"PRIu64 " at %p as remote",
1354  (chan->global_identifier), chan);
1355  channel_mark_remote(chan);
1356  }
1357  }
1358 }
1359 
1366 static int
1368 {
1369  switch (command) {
1370  case CELL_VERSIONS:
1371  case CELL_VPADDING:
1372  case CELL_AUTHORIZE:
1373  return 1;
1374  default:
1375  return 0;
1376  }
1377 }
1378 
1387 static int
1388 enter_v3_handshake_with_cell(var_cell_t *cell, channel_tls_t *chan)
1389 {
1390  int started_here = 0;
1391 
1392  tor_assert(cell);
1393  tor_assert(chan);
1394  tor_assert(chan->conn);
1395 
1396  started_here = connection_or_nonopen_was_started_here(chan->conn);
1397 
1398  tor_assert(TO_CONN(chan->conn)->state == OR_CONN_STATE_TLS_HANDSHAKING ||
1399  TO_CONN(chan->conn)->state ==
1401 
1402  if (started_here) {
1403  log_fn(LOG_PROTOCOL_WARN, LD_OR,
1404  "Received a cell while TLS-handshaking, not in "
1405  "OR_HANDSHAKING_V3, on a connection we originated.");
1406  }
1408  chan->conn->base_.state = OR_CONN_STATE_OR_HANDSHAKING_V3;
1409  if (connection_init_or_handshake_state(chan->conn, started_here) < 0) {
1410  connection_or_close_for_error(chan->conn, 0);
1411  return -1;
1412  }
1414  chan->conn->handshake_state, cell, 1);
1415  return 0;
1416 }
1417 
1427 static void
1429 {
1430  int highest_supported_version = 0;
1431  int started_here = 0;
1432 
1433  tor_assert(cell);
1434  tor_assert(chan);
1435  tor_assert(chan->conn);
1436 
1437  if ((cell->payload_len % 2) == 1) {
1438  log_fn(LOG_PROTOCOL_WARN, LD_OR,
1439  "Received a VERSION cell with odd payload length %d; "
1440  "closing connection.",cell->payload_len);
1441  connection_or_close_for_error(chan->conn, 0);
1442  return;
1443  }
1444 
1445  started_here = connection_or_nonopen_was_started_here(chan->conn);
1446 
1447  if (chan->conn->link_proto != 0 ||
1448  (chan->conn->handshake_state &&
1449  chan->conn->handshake_state->received_versions)) {
1450  log_fn(LOG_PROTOCOL_WARN, LD_OR,
1451  "Received a VERSIONS cell on a connection with its version "
1452  "already set to %d; dropping",
1453  (int)(chan->conn->link_proto));
1454  return;
1455  }
1456  switch (chan->conn->base_.state)
1457  {
1460  break;
1463  default:
1464  log_fn(LOG_PROTOCOL_WARN, LD_OR,
1465  "VERSIONS cell while in unexpected state");
1466  return;
1467  }
1468 
1469  tor_assert(chan->conn->handshake_state);
1470 
1471  {
1472  int i;
1473  const uint8_t *cp = cell->payload;
1474  for (i = 0; i < cell->payload_len / 2; ++i, cp += 2) {
1475  uint16_t v = ntohs(get_uint16(cp));
1476  if (is_or_protocol_version_known(v) && v > highest_supported_version)
1477  highest_supported_version = v;
1478  }
1479  }
1480  if (!highest_supported_version) {
1481  log_fn(LOG_PROTOCOL_WARN, LD_OR,
1482  "Couldn't find a version in common between my version list and the "
1483  "list in the VERSIONS cell; closing connection.");
1484  connection_or_close_for_error(chan->conn, 0);
1485  return;
1486  } else if (highest_supported_version == 1) {
1487  /* Negotiating version 1 makes no sense, since version 1 has no VERSIONS
1488  * cells. */
1489  log_fn(LOG_PROTOCOL_WARN, LD_OR,
1490  "Used version negotiation protocol to negotiate a v1 connection. "
1491  "That's crazily non-compliant. Closing connection.");
1492  connection_or_close_for_error(chan->conn, 0);
1493  return;
1494  } else if (highest_supported_version < 3 &&
1495  chan->conn->base_.state == OR_CONN_STATE_OR_HANDSHAKING_V3) {
1496  log_fn(LOG_PROTOCOL_WARN, LD_OR,
1497  "Negotiated link protocol 2 or lower after doing a v3 TLS "
1498  "handshake. Closing connection.");
1499  connection_or_close_for_error(chan->conn, 0);
1500  return;
1501  } else if (highest_supported_version != 2 &&
1502  chan->conn->base_.state == OR_CONN_STATE_OR_HANDSHAKING_V2) {
1503  /* XXXX This should eventually be a log_protocol_warn */
1505  "Negotiated link with non-2 protocol after doing a v2 TLS "
1506  "handshake with %s. Closing connection.",
1507  fmt_addr(&chan->conn->base_.addr));
1508  connection_or_close_for_error(chan->conn, 0);
1509  return;
1510  }
1511 
1512  rep_hist_note_negotiated_link_proto(highest_supported_version, started_here);
1513 
1514  chan->conn->link_proto = highest_supported_version;
1515  chan->conn->handshake_state->received_versions = 1;
1516 
1517  if (chan->conn->link_proto == 2) {
1518  log_info(LD_OR,
1519  "Negotiated version %d with %s:%d; sending NETINFO.",
1520  highest_supported_version,
1521  safe_str_client(chan->conn->base_.address),
1522  chan->conn->base_.port);
1523 
1524  if (connection_or_send_netinfo(chan->conn) < 0) {
1525  connection_or_close_for_error(chan->conn, 0);
1526  return;
1527  }
1528  } else {
1529  const int send_versions = !started_here;
1530  /* If we want to authenticate, send a CERTS cell */
1531  const int send_certs = !started_here || public_server_mode(get_options());
1532  /* If we're a host that got a connection, ask for authentication. */
1533  const int send_chall = !started_here;
1534  /* If our certs cell will authenticate us, we can send a netinfo cell
1535  * right now. */
1536  const int send_netinfo = !started_here;
1537  const int send_any =
1538  send_versions || send_certs || send_chall || send_netinfo;
1539  tor_assert(chan->conn->link_proto >= 3);
1540 
1541  log_info(LD_OR,
1542  "Negotiated version %d with %s:%d; %s%s%s%s%s",
1543  highest_supported_version,
1544  safe_str_client(chan->conn->base_.address),
1545  chan->conn->base_.port,
1546  send_any ? "Sending cells:" : "Waiting for CERTS cell",
1547  send_versions ? " VERSIONS" : "",
1548  send_certs ? " CERTS" : "",
1549  send_chall ? " AUTH_CHALLENGE" : "",
1550  send_netinfo ? " NETINFO" : "");
1551 
1552 #ifdef DISABLE_V3_LINKPROTO_SERVERSIDE
1553  if (1) {
1554  connection_or_close_normally(chan->conn, 1);
1555  return;
1556  }
1557 #endif /* defined(DISABLE_V3_LINKPROTO_SERVERSIDE) */
1558 
1559  if (send_versions) {
1560  if (connection_or_send_versions(chan->conn, 1) < 0) {
1561  log_warn(LD_OR, "Couldn't send versions cell");
1562  connection_or_close_for_error(chan->conn, 0);
1563  return;
1564  }
1565  }
1566 
1567  /* We set this after sending the versions cell. */
1568  /*XXXXX symbolic const.*/
1569  TLS_CHAN_TO_BASE(chan)->wide_circ_ids =
1570  chan->conn->link_proto >= MIN_LINK_PROTO_FOR_WIDE_CIRC_IDS;
1571  chan->conn->wide_circ_ids = TLS_CHAN_TO_BASE(chan)->wide_circ_ids;
1572 
1573  TLS_CHAN_TO_BASE(chan)->padding_enabled =
1574  chan->conn->link_proto >= MIN_LINK_PROTO_FOR_CHANNEL_PADDING;
1575 
1576  if (send_certs) {
1577  if (connection_or_send_certs_cell(chan->conn) < 0) {
1578  log_warn(LD_OR, "Couldn't send certs cell");
1579  connection_or_close_for_error(chan->conn, 0);
1580  return;
1581  }
1582  }
1583  if (send_chall) {
1584  if (connection_or_send_auth_challenge_cell(chan->conn) < 0) {
1585  log_warn(LD_OR, "Couldn't send auth_challenge cell");
1586  connection_or_close_for_error(chan->conn, 0);
1587  return;
1588  }
1589  }
1590  if (send_netinfo) {
1591  if (connection_or_send_netinfo(chan->conn) < 0) {
1592  log_warn(LD_OR, "Couldn't send netinfo cell");
1593  connection_or_close_for_error(chan->conn, 0);
1594  return;
1595  }
1596  }
1597  }
1598 }
1599 
1607 static void
1609 {
1610  channelpadding_negotiate_t *negotiation;
1611  tor_assert(cell);
1612  tor_assert(chan);
1613  tor_assert(chan->conn);
1614 
1615  if (chan->conn->link_proto < MIN_LINK_PROTO_FOR_CHANNEL_PADDING) {
1616  log_fn(LOG_PROTOCOL_WARN, LD_OR,
1617  "Received a PADDING_NEGOTIATE cell on v%d connection; dropping.",
1618  chan->conn->link_proto);
1619  return;
1620  }
1621 
1622  if (channelpadding_negotiate_parse(&negotiation, cell->payload,
1623  CELL_PAYLOAD_SIZE) < 0) {
1624  log_fn(LOG_PROTOCOL_WARN, LD_OR,
1625  "Received malformed PADDING_NEGOTIATE cell on v%d connection; "
1626  "dropping.", chan->conn->link_proto);
1627 
1628  return;
1629  }
1630 
1631  channelpadding_update_padding_for_channel(TLS_CHAN_TO_BASE(chan),
1632  negotiation);
1633 
1634  channelpadding_negotiate_free(negotiation);
1635 }
1636 
1641 static int
1643  const netinfo_addr_t *netinfo_addr) {
1644  tor_assert(tor_addr);
1645  tor_assert(netinfo_addr);
1646 
1647  uint8_t type = netinfo_addr_get_addr_type(netinfo_addr);
1648  uint8_t len = netinfo_addr_get_len(netinfo_addr);
1649 
1650  if (type == NETINFO_ADDR_TYPE_IPV4 && len == 4) {
1651  uint32_t ipv4 = netinfo_addr_get_addr_ipv4(netinfo_addr);
1652  tor_addr_from_ipv4h(tor_addr, ipv4);
1653  } else if (type == NETINFO_ADDR_TYPE_IPV6 && len == 16) {
1654  const uint8_t *ipv6_bytes = netinfo_addr_getconstarray_addr_ipv6(
1655  netinfo_addr);
1656  tor_addr_from_ipv6_bytes(tor_addr, (const char *)ipv6_bytes);
1657  } else {
1658  log_fn(LOG_PROTOCOL_WARN, LD_OR, "Cannot read address from NETINFO "
1659  "- wrong type/length.");
1660  return -1;
1661  }
1662 
1663  return 0;
1664 }
1665 
1672 static inline time_t
1673 time_abs(time_t val)
1674 {
1675  return (val < 0) ? -val : val;
1676 }
1677 
1684 static void
1685 channel_tls_process_netinfo_cell(cell_t *cell, channel_tls_t *chan)
1686 {
1687  time_t timestamp;
1688  uint8_t my_addr_type;
1689  uint8_t my_addr_len;
1690  uint8_t n_other_addrs;
1691  time_t now = time(NULL);
1692  const routerinfo_t *me = router_get_my_routerinfo();
1693 
1694  time_t apparent_skew = 0;
1695  tor_addr_t my_apparent_addr = TOR_ADDR_NULL;
1696  int started_here = 0;
1697  const char *identity_digest = NULL;
1698 
1699  tor_assert(cell);
1700  tor_assert(chan);
1701  tor_assert(chan->conn);
1702 
1703  if (chan->conn->link_proto < 2) {
1704  log_fn(LOG_PROTOCOL_WARN, LD_OR,
1705  "Received a NETINFO cell on %s connection; dropping.",
1706  chan->conn->link_proto == 0 ? "non-versioned" : "a v1");
1707  return;
1708  }
1709  if (chan->conn->base_.state != OR_CONN_STATE_OR_HANDSHAKING_V2 &&
1710  chan->conn->base_.state != OR_CONN_STATE_OR_HANDSHAKING_V3) {
1711  log_fn(LOG_PROTOCOL_WARN, LD_OR,
1712  "Received a NETINFO cell on non-handshaking connection; dropping.");
1713  return;
1714  }
1715  tor_assert(chan->conn->handshake_state &&
1716  chan->conn->handshake_state->received_versions);
1717  started_here = connection_or_nonopen_was_started_here(chan->conn);
1718  identity_digest = chan->conn->identity_digest;
1719 
1720  if (chan->conn->base_.state == OR_CONN_STATE_OR_HANDSHAKING_V3) {
1721  tor_assert(chan->conn->link_proto >= 3);
1722  if (started_here) {
1723  if (!(chan->conn->handshake_state->authenticated)) {
1724  log_fn(LOG_PROTOCOL_WARN, LD_OR,
1725  "Got a NETINFO cell from server, "
1726  "but no authentication. Closing the connection.");
1727  connection_or_close_for_error(chan->conn, 0);
1728  return;
1729  }
1730  } else {
1731  /* we're the server. If the client never authenticated, we have
1732  some housekeeping to do.*/
1733  if (!(chan->conn->handshake_state->authenticated)) {
1735  (const char*)(chan->conn->handshake_state->
1736  authenticated_rsa_peer_id)));
1738  (const char*)(chan->conn->handshake_state->
1739  authenticated_ed25519_peer_id.pubkey), 32));
1740  /* If the client never authenticated, it's a tor client or bridge
1741  * relay, and we must not use it for EXTEND requests (nor could we, as
1742  * there are no authenticated peer IDs) */
1743  channel_mark_client(TLS_CHAN_TO_BASE(chan));
1744  channel_set_circid_type(TLS_CHAN_TO_BASE(chan), NULL,
1745  chan->conn->link_proto < MIN_LINK_PROTO_FOR_WIDE_CIRC_IDS);
1746 
1748  &(chan->conn->base_.addr),
1749  chan->conn->base_.port,
1750  /* zero, checked above */
1751  (const char*)(chan->conn->handshake_state->
1752  authenticated_rsa_peer_id),
1753  NULL, /* Ed25519 ID: Also checked as zero */
1754  0);
1755  }
1756  }
1757  }
1758 
1759  /* Decode the cell. */
1760  netinfo_cell_t *netinfo_cell = NULL;
1761 
1762  ssize_t parsed = netinfo_cell_parse(&netinfo_cell, cell->payload,
1764 
1765  if (parsed < 0) {
1766  log_fn(LOG_PROTOCOL_WARN, LD_OR,
1767  "Failed to parse NETINFO cell - closing connection.");
1768  connection_or_close_for_error(chan->conn, 0);
1769  return;
1770  }
1771 
1772  timestamp = netinfo_cell_get_timestamp(netinfo_cell);
1773 
1774  const netinfo_addr_t *my_addr =
1775  netinfo_cell_getconst_other_addr(netinfo_cell);
1776 
1777  my_addr_type = netinfo_addr_get_addr_type(my_addr);
1778  my_addr_len = netinfo_addr_get_len(my_addr);
1779 
1780  if ((now - chan->conn->handshake_state->sent_versions_at) < 180) {
1781  apparent_skew = now - timestamp;
1782  }
1783  /* We used to check:
1784  * if (my_addr_len >= CELL_PAYLOAD_SIZE - 6) {
1785  *
1786  * This is actually never going to happen, since my_addr_len is at most 255,
1787  * and CELL_PAYLOAD_LEN - 6 is 503. So we know that cp is < end. */
1788 
1789  if (tor_addr_from_netinfo_addr(&my_apparent_addr, my_addr) == -1) {
1790  connection_or_close_for_error(chan->conn, 0);
1791  netinfo_cell_free(netinfo_cell);
1792  return;
1793  }
1794 
1795  if (my_addr_type == NETINFO_ADDR_TYPE_IPV4 && my_addr_len == 4) {
1796  if (!get_options()->BridgeRelay && me &&
1797  tor_addr_eq_ipv4h(&my_apparent_addr, me->addr)) {
1798  TLS_CHAN_TO_BASE(chan)->is_canonical_to_peer = 1;
1799  }
1800  } else if (my_addr_type == NETINFO_ADDR_TYPE_IPV6 &&
1801  my_addr_len == 16) {
1802  if (!get_options()->BridgeRelay && me &&
1803  !tor_addr_is_null(&me->ipv6_addr) &&
1804  tor_addr_eq(&my_apparent_addr, &me->ipv6_addr)) {
1805  TLS_CHAN_TO_BASE(chan)->is_canonical_to_peer = 1;
1806  }
1807  }
1808 
1809  n_other_addrs = netinfo_cell_get_n_my_addrs(netinfo_cell);
1810  for (uint8_t i = 0; i < n_other_addrs; i++) {
1811  /* Consider all the other addresses; if any matches, this connection is
1812  * "canonical." */
1813 
1814  const netinfo_addr_t *netinfo_addr =
1815  netinfo_cell_getconst_my_addrs(netinfo_cell, i);
1816 
1817  tor_addr_t addr;
1818 
1819  if (tor_addr_from_netinfo_addr(&addr, netinfo_addr) == -1) {
1820  log_fn(LOG_PROTOCOL_WARN, LD_OR,
1821  "Bad address in netinfo cell; Skipping.");
1822  continue;
1823  }
1824  /* A relay can connect from anywhere and be canonical, so
1825  * long as it tells you from where it came. This may sound a bit
1826  * concerning... but that's what "canonical" means: that the
1827  * address is one that the relay itself has claimed. The relay
1828  * might be doing something funny, but nobody else is doing a MITM
1829  * on the relay's TCP.
1830  */
1831  if (tor_addr_eq(&addr, &(chan->conn->real_addr))) {
1832  connection_or_set_canonical(chan->conn, 1);
1833  break;
1834  }
1835  }
1836 
1837  netinfo_cell_free(netinfo_cell);
1838 
1839  if (me && !TLS_CHAN_TO_BASE(chan)->is_canonical_to_peer &&
1840  channel_is_canonical(TLS_CHAN_TO_BASE(chan))) {
1841  const char *descr =
1842  TLS_CHAN_TO_BASE(chan)->get_remote_descr(TLS_CHAN_TO_BASE(chan), 0);
1843  log_info(LD_OR,
1844  "We made a connection to a relay at %s (fp=%s) but we think "
1845  "they will not consider this connection canonical. They "
1846  "think we are at %s, but we think its %s.",
1847  safe_str(descr),
1848  safe_str(hex_str(identity_digest, DIGEST_LEN)),
1849  safe_str(tor_addr_is_null(&my_apparent_addr) ?
1850  "<none>" : fmt_and_decorate_addr(&my_apparent_addr)),
1851  safe_str(fmt_addr32(me->addr)));
1852  }
1853 
1854  /* Act on apparent skew. */
1856 #define NETINFO_NOTICE_SKEW 3600
1857  if (time_abs(apparent_skew) > NETINFO_NOTICE_SKEW &&
1858  (started_here ||
1859  connection_or_digest_is_known_relay(chan->conn->identity_digest))) {
1860  int trusted = router_digest_is_trusted_dir(chan->conn->identity_digest);
1861  clock_skew_warning(TO_CONN(chan->conn), apparent_skew, trusted, LD_GENERAL,
1862  "NETINFO cell", "OR");
1863  }
1864 
1865  /* XXX maybe act on my_apparent_addr, if the source is sufficiently
1866  * trustworthy. */
1867 
1868  if (! chan->conn->handshake_state->sent_netinfo) {
1869  /* If we were prepared to authenticate, but we never got an AUTH_CHALLENGE
1870  * cell, then we would not previously have sent a NETINFO cell. Do so
1871  * now. */
1872  if (connection_or_send_netinfo(chan->conn) < 0) {
1873  connection_or_close_for_error(chan->conn, 0);
1874  return;
1875  }
1876  }
1877 
1878  if (connection_or_set_state_open(chan->conn) < 0) {
1879  log_fn(LOG_PROTOCOL_WARN, LD_OR,
1880  "Got good NETINFO cell from %s:%d; but "
1881  "was unable to make the OR connection become open.",
1882  safe_str_client(chan->conn->base_.address),
1883  chan->conn->base_.port);
1884  connection_or_close_for_error(chan->conn, 0);
1885  } else {
1886  log_info(LD_OR,
1887  "Got good NETINFO cell from %s:%d; OR connection is now "
1888  "open, using protocol version %d. Its ID digest is %s. "
1889  "Our address is apparently %s.",
1890  safe_str_client(chan->conn->base_.address),
1891  chan->conn->base_.port,
1892  (int)(chan->conn->link_proto),
1893  hex_str(identity_digest, DIGEST_LEN),
1894  tor_addr_is_null(&my_apparent_addr) ?
1895  "<none>" :
1896  safe_str_client(fmt_and_decorate_addr(&my_apparent_addr)));
1897  }
1898  assert_connection_ok(TO_CONN(chan->conn),time(NULL));
1899 }
1900 
1903 typedef enum cert_encoding_t {
1910 } cert_encoding_t;
1911 
1917 static cert_encoding_t
1919 {
1920  switch (typenum) {
1921  case CERTTYPE_RSA1024_ID_LINK:
1922  case CERTTYPE_RSA1024_ID_ID:
1923  case CERTTYPE_RSA1024_ID_AUTH:
1924  return CERT_ENCODING_X509;
1925  case CERTTYPE_ED_ID_SIGN:
1926  case CERTTYPE_ED_SIGN_LINK:
1927  case CERTTYPE_ED_SIGN_AUTH:
1928  return CERT_ENCODING_ED25519;
1929  case CERTTYPE_RSA1024_ID_EDID:
1931  default:
1932  return CERT_ENCODING_UNKNOWN;
1933  }
1934 }
1935 
1951 STATIC void
1952 channel_tls_process_certs_cell(var_cell_t *cell, channel_tls_t *chan)
1953 {
1954 #define MAX_CERT_TYPE_WANTED CERTTYPE_RSA1024_ID_EDID
1955  /* These arrays will be sparse, since a cert type can be at most one
1956  * of ed/x509 */
1957  tor_x509_cert_t *x509_certs[MAX_CERT_TYPE_WANTED + 1];
1958  tor_cert_t *ed_certs[MAX_CERT_TYPE_WANTED + 1];
1959  uint8_t *rsa_ed_cc_cert = NULL;
1960  size_t rsa_ed_cc_cert_len = 0;
1961 
1962  int n_certs, i;
1963  certs_cell_t *cc = NULL;
1964 
1965  int send_netinfo = 0, started_here = 0;
1966 
1967  memset(x509_certs, 0, sizeof(x509_certs));
1968  memset(ed_certs, 0, sizeof(ed_certs));
1969  tor_assert(cell);
1970  tor_assert(chan);
1971  tor_assert(chan->conn);
1972 
1973 #define ERR(s) \
1974  do { \
1975  log_fn(LOG_PROTOCOL_WARN, LD_PROTOCOL, \
1976  "Received a bad CERTS cell from %s:%d: %s", \
1977  safe_str(chan->conn->base_.address), \
1978  chan->conn->base_.port, (s)); \
1979  connection_or_close_for_error(chan->conn, 0); \
1980  goto err; \
1981  } while (0)
1982 
1983  /* Can't use connection_or_nonopen_was_started_here(); its conn->tls
1984  * check looks like it breaks
1985  * test_link_handshake_recv_certs_ok_server(). */
1986  started_here = chan->conn->handshake_state->started_here;
1987 
1988  if (chan->conn->base_.state != OR_CONN_STATE_OR_HANDSHAKING_V3)
1989  ERR("We're not doing a v3 handshake!");
1990  if (chan->conn->link_proto < 3)
1991  ERR("We're not using link protocol >= 3");
1992  if (chan->conn->handshake_state->received_certs_cell)
1993  ERR("We already got one");
1994  if (chan->conn->handshake_state->authenticated) {
1995  /* Should be unreachable, but let's make sure. */
1996  ERR("We're already authenticated!");
1997  }
1998  if (cell->payload_len < 1)
1999  ERR("It had no body");
2000  if (cell->circ_id)
2001  ERR("It had a nonzero circuit ID");
2002 
2003  if (certs_cell_parse(&cc, cell->payload, cell->payload_len) < 0)
2004  ERR("It couldn't be parsed.");
2005 
2006  n_certs = cc->n_certs;
2007 
2008  for (i = 0; i < n_certs; ++i) {
2009  certs_cell_cert_t *c = certs_cell_get_certs(cc, i);
2010 
2011  uint16_t cert_type = c->cert_type;
2012  uint16_t cert_len = c->cert_len;
2013  uint8_t *cert_body = certs_cell_cert_getarray_body(c);
2014 
2015  if (cert_type > MAX_CERT_TYPE_WANTED)
2016  continue;
2017  const cert_encoding_t ct = certs_cell_typenum_to_cert_type(cert_type);
2018  switch (ct) {
2019  default:
2020  case CERT_ENCODING_UNKNOWN:
2021  break;
2022  case CERT_ENCODING_X509: {
2023  tor_x509_cert_t *x509_cert = tor_x509_cert_decode(cert_body, cert_len);
2024  if (!x509_cert) {
2025  log_fn(LOG_PROTOCOL_WARN, LD_PROTOCOL,
2026  "Received undecodable certificate in CERTS cell from %s:%d",
2027  safe_str(chan->conn->base_.address),
2028  chan->conn->base_.port);
2029  } else {
2030  if (x509_certs[cert_type]) {
2031  tor_x509_cert_free(x509_cert);
2032  ERR("Duplicate x509 certificate");
2033  } else {
2034  x509_certs[cert_type] = x509_cert;
2035  }
2036  }
2037  break;
2038  }
2039  case CERT_ENCODING_ED25519: {
2040  tor_cert_t *ed_cert = tor_cert_parse(cert_body, cert_len);
2041  if (!ed_cert) {
2042  log_fn(LOG_PROTOCOL_WARN, LD_PROTOCOL,
2043  "Received undecodable Ed certificate "
2044  "in CERTS cell from %s:%d",
2045  safe_str(chan->conn->base_.address),
2046  chan->conn->base_.port);
2047  } else {
2048  if (ed_certs[cert_type]) {
2049  tor_cert_free(ed_cert);
2050  ERR("Duplicate Ed25519 certificate");
2051  } else {
2052  ed_certs[cert_type] = ed_cert;
2053  }
2054  }
2055  break;
2056  }
2057 
2059  if (rsa_ed_cc_cert) {
2060  ERR("Duplicate RSA->Ed25519 crosscert");
2061  } else {
2062  rsa_ed_cc_cert = tor_memdup(cert_body, cert_len);
2063  rsa_ed_cc_cert_len = cert_len;
2064  }
2065  break;
2066  }
2067  }
2068  }
2069 
2070  /* Move the certificates we (might) want into the handshake_state->certs
2071  * structure. */
2072  tor_x509_cert_t *id_cert = x509_certs[CERTTYPE_RSA1024_ID_ID];
2073  tor_x509_cert_t *auth_cert = x509_certs[CERTTYPE_RSA1024_ID_AUTH];
2074  tor_x509_cert_t *link_cert = x509_certs[CERTTYPE_RSA1024_ID_LINK];
2075  chan->conn->handshake_state->certs->auth_cert = auth_cert;
2076  chan->conn->handshake_state->certs->link_cert = link_cert;
2077  chan->conn->handshake_state->certs->id_cert = id_cert;
2078  x509_certs[CERTTYPE_RSA1024_ID_ID] =
2079  x509_certs[CERTTYPE_RSA1024_ID_AUTH] =
2080  x509_certs[CERTTYPE_RSA1024_ID_LINK] = NULL;
2081 
2082  tor_cert_t *ed_id_sign = ed_certs[CERTTYPE_ED_ID_SIGN];
2083  tor_cert_t *ed_sign_link = ed_certs[CERTTYPE_ED_SIGN_LINK];
2084  tor_cert_t *ed_sign_auth = ed_certs[CERTTYPE_ED_SIGN_AUTH];
2085  chan->conn->handshake_state->certs->ed_id_sign = ed_id_sign;
2086  chan->conn->handshake_state->certs->ed_sign_link = ed_sign_link;
2087  chan->conn->handshake_state->certs->ed_sign_auth = ed_sign_auth;
2088  ed_certs[CERTTYPE_ED_ID_SIGN] =
2089  ed_certs[CERTTYPE_ED_SIGN_LINK] =
2090  ed_certs[CERTTYPE_ED_SIGN_AUTH] = NULL;
2091 
2092  chan->conn->handshake_state->certs->ed_rsa_crosscert = rsa_ed_cc_cert;
2093  chan->conn->handshake_state->certs->ed_rsa_crosscert_len =
2094  rsa_ed_cc_cert_len;
2095  rsa_ed_cc_cert = NULL;
2096 
2097  int severity;
2098  /* Note that this warns more loudly about time and validity if we were
2099  * _trying_ to connect to an authority, not necessarily if we _did_ connect
2100  * to one. */
2101  if (started_here &&
2102  router_digest_is_trusted_dir(TLS_CHAN_TO_BASE(chan)->identity_digest))
2103  severity = LOG_WARN;
2104  else
2105  severity = LOG_PROTOCOL_WARN;
2106 
2107  const ed25519_public_key_t *checked_ed_id = NULL;
2108  const common_digests_t *checked_rsa_id = NULL;
2110  chan->conn->handshake_state->certs,
2111  chan->conn->tls,
2112  time(NULL),
2113  &checked_ed_id,
2114  &checked_rsa_id);
2115 
2116  if (!checked_rsa_id)
2117  ERR("Invalid certificate chain!");
2118 
2119  if (started_here) {
2120  /* No more information is needed. */
2121 
2122  chan->conn->handshake_state->authenticated = 1;
2123  chan->conn->handshake_state->authenticated_rsa = 1;
2124  {
2125  const common_digests_t *id_digests = checked_rsa_id;
2126  crypto_pk_t *identity_rcvd;
2127  if (!id_digests)
2128  ERR("Couldn't compute digests for key in ID cert");
2129 
2130  identity_rcvd = tor_tls_cert_get_key(id_cert);
2131  if (!identity_rcvd) {
2132  ERR("Couldn't get RSA key from ID cert.");
2133  }
2134  memcpy(chan->conn->handshake_state->authenticated_rsa_peer_id,
2135  id_digests->d[DIGEST_SHA1], DIGEST_LEN);
2136  channel_set_circid_type(TLS_CHAN_TO_BASE(chan), identity_rcvd,
2137  chan->conn->link_proto < MIN_LINK_PROTO_FOR_WIDE_CIRC_IDS);
2138  crypto_pk_free(identity_rcvd);
2139  }
2140 
2141  if (checked_ed_id) {
2142  chan->conn->handshake_state->authenticated_ed25519 = 1;
2143  memcpy(&chan->conn->handshake_state->authenticated_ed25519_peer_id,
2144  checked_ed_id, sizeof(ed25519_public_key_t));
2145  }
2146 
2147  log_debug(LD_HANDSHAKE, "calling client_learned_peer_id from "
2148  "process_certs_cell");
2149 
2150  if (connection_or_client_learned_peer_id(chan->conn,
2151  chan->conn->handshake_state->authenticated_rsa_peer_id,
2152  checked_ed_id) < 0)
2153  ERR("Problem setting or checking peer id");
2154 
2155  log_info(LD_HANDSHAKE,
2156  "Got some good certificates from %s:%d: Authenticated it with "
2157  "RSA%s",
2158  safe_str(chan->conn->base_.address), chan->conn->base_.port,
2159  checked_ed_id ? " and Ed25519" : "");
2160 
2161  if (!public_server_mode(get_options())) {
2162  /* If we initiated the connection and we are not a public server, we
2163  * aren't planning to authenticate at all. At this point we know who we
2164  * are talking to, so we can just send a netinfo now. */
2165  send_netinfo = 1;
2166  }
2167  } else {
2168  /* We can't call it authenticated till we see an AUTHENTICATE cell. */
2169  log_info(LD_OR,
2170  "Got some good RSA%s certificates from %s:%d. "
2171  "Waiting for AUTHENTICATE.",
2172  checked_ed_id ? " and Ed25519" : "",
2173  safe_str(chan->conn->base_.address),
2174  chan->conn->base_.port);
2175  /* XXXX check more stuff? */
2176  }
2177 
2178  chan->conn->handshake_state->received_certs_cell = 1;
2179 
2180  if (send_netinfo) {
2181  if (connection_or_send_netinfo(chan->conn) < 0) {
2182  log_warn(LD_OR, "Couldn't send netinfo cell");
2183  connection_or_close_for_error(chan->conn, 0);
2184  goto err;
2185  }
2186  }
2187 
2188  err:
2189  for (unsigned u = 0; u < ARRAY_LENGTH(x509_certs); ++u) {
2190  tor_x509_cert_free(x509_certs[u]);
2191  }
2192  for (unsigned u = 0; u < ARRAY_LENGTH(ed_certs); ++u) {
2193  tor_cert_free(ed_certs[u]);
2194  }
2195  tor_free(rsa_ed_cc_cert);
2196  certs_cell_free(cc);
2197 #undef ERR
2198 }
2199 
2210 STATIC void
2212 {
2213  int n_types, i, use_type = -1;
2214  auth_challenge_cell_t *ac = NULL;
2215 
2216  tor_assert(cell);
2217  tor_assert(chan);
2218  tor_assert(chan->conn);
2219 
2220 #define ERR(s) \
2221  do { \
2222  log_fn(LOG_PROTOCOL_WARN, LD_PROTOCOL, \
2223  "Received a bad AUTH_CHALLENGE cell from %s:%d: %s", \
2224  safe_str(chan->conn->base_.address), \
2225  chan->conn->base_.port, (s)); \
2226  connection_or_close_for_error(chan->conn, 0); \
2227  goto done; \
2228  } while (0)
2229 
2230  if (chan->conn->base_.state != OR_CONN_STATE_OR_HANDSHAKING_V3)
2231  ERR("We're not currently doing a v3 handshake");
2232  if (chan->conn->link_proto < 3)
2233  ERR("We're not using link protocol >= 3");
2234  if (!(chan->conn->handshake_state->started_here))
2235  ERR("We didn't originate this connection");
2236  if (chan->conn->handshake_state->received_auth_challenge)
2237  ERR("We already received one");
2238  if (!(chan->conn->handshake_state->received_certs_cell))
2239  ERR("We haven't gotten a CERTS cell yet");
2240  if (cell->circ_id)
2241  ERR("It had a nonzero circuit ID");
2242 
2243  if (auth_challenge_cell_parse(&ac, cell->payload, cell->payload_len) < 0)
2244  ERR("It was not well-formed.");
2245 
2246  n_types = ac->n_methods;
2247 
2248  /* Now see if there is an authentication type we can use */
2249  for (i = 0; i < n_types; ++i) {
2250  uint16_t authtype = auth_challenge_cell_get_methods(ac, i);
2251  if (authchallenge_type_is_supported(authtype)) {
2252  if (use_type == -1 ||
2253  authchallenge_type_is_better(authtype, use_type)) {
2254  use_type = authtype;
2255  }
2256  }
2257  }
2258 
2259  chan->conn->handshake_state->received_auth_challenge = 1;
2260 
2261  if (! public_server_mode(get_options())) {
2262  /* If we're not a public server then we don't want to authenticate on a
2263  connection we originated, and we already sent a NETINFO cell when we
2264  got the CERTS cell. We have nothing more to do. */
2265  goto done;
2266  }
2267 
2268  if (use_type >= 0) {
2269  log_info(LD_OR,
2270  "Got an AUTH_CHALLENGE cell from %s:%d: Sending "
2271  "authentication type %d",
2272  safe_str(chan->conn->base_.address),
2273  chan->conn->base_.port,
2274  use_type);
2275 
2276  if (connection_or_send_authenticate_cell(chan->conn, use_type) < 0) {
2277  log_warn(LD_OR,
2278  "Couldn't send authenticate cell");
2279  connection_or_close_for_error(chan->conn, 0);
2280  goto done;
2281  }
2282  } else {
2283  log_info(LD_OR,
2284  "Got an AUTH_CHALLENGE cell from %s:%d, but we don't "
2285  "know any of its authentication types. Not authenticating.",
2286  safe_str(chan->conn->base_.address),
2287  chan->conn->base_.port);
2288  }
2289 
2290  if (connection_or_send_netinfo(chan->conn) < 0) {
2291  log_warn(LD_OR, "Couldn't send netinfo cell");
2292  connection_or_close_for_error(chan->conn, 0);
2293  goto done;
2294  }
2295 
2296  done:
2297  auth_challenge_cell_free(ac);
2298 
2299 #undef ERR
2300 }
2301 
2311 STATIC void
2313 {
2314  var_cell_t *expected_cell = NULL;
2315  const uint8_t *auth;
2316  int authlen;
2317  int authtype;
2318  int bodylen;
2319 
2320  tor_assert(cell);
2321  tor_assert(chan);
2322  tor_assert(chan->conn);
2323 
2324 #define ERR(s) \
2325  do { \
2326  log_fn(LOG_PROTOCOL_WARN, LD_PROTOCOL, \
2327  "Received a bad AUTHENTICATE cell from %s:%d: %s", \
2328  safe_str(chan->conn->base_.address), \
2329  chan->conn->base_.port, (s)); \
2330  connection_or_close_for_error(chan->conn, 0); \
2331  var_cell_free(expected_cell); \
2332  return; \
2333  } while (0)
2334 
2335  if (chan->conn->base_.state != OR_CONN_STATE_OR_HANDSHAKING_V3)
2336  ERR("We're not doing a v3 handshake");
2337  if (chan->conn->link_proto < 3)
2338  ERR("We're not using link protocol >= 3");
2339  if (chan->conn->handshake_state->started_here)
2340  ERR("We originated this connection");
2341  if (chan->conn->handshake_state->received_authenticate)
2342  ERR("We already got one!");
2343  if (chan->conn->handshake_state->authenticated) {
2344  /* Should be impossible given other checks */
2345  ERR("The peer is already authenticated");
2346  }
2347  if (!(chan->conn->handshake_state->received_certs_cell))
2348  ERR("We never got a certs cell");
2349  if (chan->conn->handshake_state->certs->id_cert == NULL)
2350  ERR("We never got an identity certificate");
2351  if (cell->payload_len < 4)
2352  ERR("Cell was way too short");
2353 
2354  auth = cell->payload;
2355  {
2356  uint16_t type = ntohs(get_uint16(auth));
2357  uint16_t len = ntohs(get_uint16(auth+2));
2358  if (4 + len > cell->payload_len)
2359  ERR("Authenticator was truncated");
2360 
2361  if (! authchallenge_type_is_supported(type))
2362  ERR("Authenticator type was not recognized");
2363  authtype = type;
2364 
2365  auth += 4;
2366  authlen = len;
2367  }
2368 
2369  if (authlen < V3_AUTH_BODY_LEN + 1)
2370  ERR("Authenticator was too short");
2371 
2373  chan->conn, authtype, NULL, NULL, 1);
2374  if (! expected_cell)
2375  ERR("Couldn't compute expected AUTHENTICATE cell body");
2376 
2377  int sig_is_rsa;
2378  if (authtype == AUTHTYPE_RSA_SHA256_TLSSECRET ||
2379  authtype == AUTHTYPE_RSA_SHA256_RFC5705) {
2380  bodylen = V3_AUTH_BODY_LEN;
2381  sig_is_rsa = 1;
2382  } else {
2384  /* Our earlier check had better have made sure we had room
2385  * for an ed25519 sig (inadvertently) */
2386  tor_assert(V3_AUTH_BODY_LEN > ED25519_SIG_LEN);
2387  bodylen = authlen - ED25519_SIG_LEN;
2388  sig_is_rsa = 0;
2389  }
2390  if (expected_cell->payload_len != bodylen+4) {
2391  ERR("Expected AUTHENTICATE cell body len not as expected.");
2392  }
2393 
2394  /* Length of random part. */
2395  if (BUG(bodylen < 24)) {
2396  // LCOV_EXCL_START
2397  ERR("Bodylen is somehow less than 24, which should really be impossible");
2398  // LCOV_EXCL_STOP
2399  }
2400 
2401  if (tor_memneq(expected_cell->payload+4, auth, bodylen-24))
2402  ERR("Some field in the AUTHENTICATE cell body was not as expected");
2403 
2404  if (sig_is_rsa) {
2405  if (chan->conn->handshake_state->certs->ed_id_sign != NULL)
2406  ERR("RSA-signed AUTHENTICATE response provided with an ED25519 cert");
2407 
2408  if (chan->conn->handshake_state->certs->auth_cert == NULL)
2409  ERR("We never got an RSA authentication certificate");
2410 
2412  chan->conn->handshake_state->certs->auth_cert);
2413  char d[DIGEST256_LEN];
2414  char *signed_data;
2415  size_t keysize;
2416  int signed_len;
2417 
2418  if (! pk) {
2419  ERR("Couldn't get RSA key from AUTH cert.");
2420  }
2421  crypto_digest256(d, (char*)auth, V3_AUTH_BODY_LEN, DIGEST_SHA256);
2422 
2423  keysize = crypto_pk_keysize(pk);
2424  signed_data = tor_malloc(keysize);
2425  signed_len = crypto_pk_public_checksig(pk, signed_data, keysize,
2426  (char*)auth + V3_AUTH_BODY_LEN,
2427  authlen - V3_AUTH_BODY_LEN);
2428  crypto_pk_free(pk);
2429  if (signed_len < 0) {
2430  tor_free(signed_data);
2431  ERR("RSA signature wasn't valid");
2432  }
2433  if (signed_len < DIGEST256_LEN) {
2434  tor_free(signed_data);
2435  ERR("Not enough data was signed");
2436  }
2437  /* Note that we deliberately allow *more* than DIGEST256_LEN bytes here,
2438  * in case they're later used to hold a SHA3 digest or something. */
2439  if (tor_memneq(signed_data, d, DIGEST256_LEN)) {
2440  tor_free(signed_data);
2441  ERR("Signature did not match data to be signed.");
2442  }
2443  tor_free(signed_data);
2444  } else {
2445  if (chan->conn->handshake_state->certs->ed_id_sign == NULL)
2446  ERR("We never got an Ed25519 identity certificate.");
2447  if (chan->conn->handshake_state->certs->ed_sign_auth == NULL)
2448  ERR("We never got an Ed25519 authentication certificate.");
2449 
2450  const ed25519_public_key_t *authkey =
2451  &chan->conn->handshake_state->certs->ed_sign_auth->signed_key;
2452  ed25519_signature_t sig;
2453  tor_assert(authlen > ED25519_SIG_LEN);
2454  memcpy(&sig.sig, auth + authlen - ED25519_SIG_LEN, ED25519_SIG_LEN);
2455  if (ed25519_checksig(&sig, auth, authlen - ED25519_SIG_LEN, authkey)<0) {
2456  ERR("Ed25519 signature wasn't valid.");
2457  }
2458  }
2459 
2460  /* Okay, we are authenticated. */
2461  chan->conn->handshake_state->received_authenticate = 1;
2462  chan->conn->handshake_state->authenticated = 1;
2463  chan->conn->handshake_state->authenticated_rsa = 1;
2464  chan->conn->handshake_state->digest_received_data = 0;
2465  {
2466  tor_x509_cert_t *id_cert = chan->conn->handshake_state->certs->id_cert;
2467  crypto_pk_t *identity_rcvd = tor_tls_cert_get_key(id_cert);
2468  const common_digests_t *id_digests = tor_x509_cert_get_id_digests(id_cert);
2469  const ed25519_public_key_t *ed_identity_received = NULL;
2470 
2471  if (! sig_is_rsa) {
2472  chan->conn->handshake_state->authenticated_ed25519 = 1;
2473  ed_identity_received =
2474  &chan->conn->handshake_state->certs->ed_id_sign->signing_key;
2475  memcpy(&chan->conn->handshake_state->authenticated_ed25519_peer_id,
2476  ed_identity_received, sizeof(ed25519_public_key_t));
2477  }
2478 
2479  /* This must exist; we checked key type when reading the cert. */
2480  tor_assert(id_digests);
2481 
2482  memcpy(chan->conn->handshake_state->authenticated_rsa_peer_id,
2483  id_digests->d[DIGEST_SHA1], DIGEST_LEN);
2484 
2485  channel_set_circid_type(TLS_CHAN_TO_BASE(chan), identity_rcvd,
2486  chan->conn->link_proto < MIN_LINK_PROTO_FOR_WIDE_CIRC_IDS);
2487  crypto_pk_free(identity_rcvd);
2488 
2489  log_debug(LD_HANDSHAKE,
2490  "Calling connection_or_init_conn_from_address for %s "
2491  " from %s, with%s ed25519 id.",
2492  safe_str(chan->conn->base_.address),
2493  __func__,
2494  ed_identity_received ? "" : "out");
2495 
2497  &(chan->conn->base_.addr),
2498  chan->conn->base_.port,
2499  (const char*)(chan->conn->handshake_state->
2500  authenticated_rsa_peer_id),
2501  ed_identity_received,
2502  0);
2503 
2504  log_debug(LD_HANDSHAKE,
2505  "Got an AUTHENTICATE cell from %s:%d, type %d: Looks good.",
2506  safe_str(chan->conn->base_.address),
2507  chan->conn->base_.port,
2508  authtype);
2509  }
2510 
2511  var_cell_free(expected_cell);
2512 
2513 #undef ERR
2514 }
channel_state_t state
Definition: channel.h:192
void(* close)(channel_t *)
Definition: channel.h:315
const char *(* describe_transport)(channel_listener_t *)
Definition: channel.h:498
Header file for channeltls.c.
void connection_or_close_normally(or_connection_t *orconn, int flush)
int(* has_queued_writes)(channel_t *)
Definition: channel.h:352
uint64_t stats_n_authorize_cells_processed
Definition: channeltls.c:92
Header file for circuitbuild.c.
uint8_t payload[CELL_PAYLOAD_SIZE]
Definition: cell_st.h:16
uint8_t payload[FLEXIBLE_ARRAY_MEMBER]
Definition: var_cell_st.h:19
void channel_change_state(channel_t *chan, channel_state_t to_state)
Definition: channel.c:1609
#define SMARTLIST_FOREACH_BEGIN(sl, type, var)
#define TO_CONN(c)
Definition: or.h:735
#define AUTHTYPE_ED25519_SHA256_RFC5705
Definition: or.h:691
cert_encoding_t
Definition: channeltls.c:1903
Header file for command.c.
void tor_addr_make_unspec(tor_addr_t *a)
Definition: address.c:225
static void channel_tls_process_padding_negotiate_cell(cell_t *cell, channel_tls_t *chan)
Definition: channeltls.c:1608
or_handshake_state_t * handshake_state
void channel_tls_handle_cell(cell_t *cell, or_connection_t *conn)
Definition: channeltls.c:1044
static int channel_tls_write_var_cell_method(channel_t *chan, var_cell_t *var_cell)
Definition: channeltls.c:862
void channel_change_state_open(channel_t *chan)
Definition: channel.c:1619
uint64_t stats_n_auth_challenge_cells_processed
Definition: channeltls.c:88
Header file for connection.c.
#define OR_CONN_HIGHWATER
Definition: or.h:720
channel_listener_t * channel_tls_get_listener(void)
Definition: channeltls.c:252
void channel_tls_handle_state_change_on_orconn(channel_tls_t *chan, or_connection_t *conn, uint8_t state)
Definition: channeltls.c:951
Definition: cell_st.h:12
#define LD_GENERAL
Definition: log.h:60
void connection_or_block_renegotiation(or_connection_t *conn)
void rep_hist_note_negotiated_link_proto(unsigned link_proto, int started_here)
Definition: rephist.c:2701
uint8_t state
Definition: connection_st.h:44
void or_handshake_certs_check_both(int severity, or_handshake_certs_t *certs, tor_tls_t *tls, time_t now, const ed25519_public_key_t **ed_id_out, const common_digests_t **rsa_id_out)
Definition: torcert.c:685
static int tor_addr_eq_ipv4h(const tor_addr_t *a, uint32_t u)
Definition: address.h:193
#define LOG_INFO
Definition: log.h:43
static size_t channel_tls_num_bytes_queued_method(channel_t *chan)
Definition: channeltls.c:753
const common_digests_t * tor_x509_cert_get_id_digests(const tor_x509_cert_t *cert)
Definition: x509.c:58
channel_tls_t * channel_tls_from_base(channel_t *chan)
Definition: channeltls.c:381
#define OR_CONN_STATE_TLS_HANDSHAKING
Definition: orconn_event.h:36
void channel_tls_update_marks(or_connection_t *conn)
Definition: channeltls.c:1334
int authchallenge_type_is_supported(uint16_t challenge_type)
int connection_or_digest_is_known_relay(const char *id_digest)
#define OR_CONN_STATE_TLS_SERVER_RENEGOTIATING
Definition: orconn_event.h:43
static double channel_tls_get_overhead_estimate_method(channel_t *chan)
Definition: channeltls.c:477
Header file for config.c.
#define V3_AUTH_BODY_LEN
Definition: or.h:706
#define fmt_and_decorate_addr(a)
Definition: address.h:214
#define CONN_TYPE_OR
Definition: connection.h:24
int(* write_packed_cell)(channel_t *, packed_cell_t *)
Definition: channel.h:371
int channel_is_canonical(channel_t *chan)
Definition: channel.c:2964
int fast_mem_is_zero(const char *mem, size_t len)
Definition: util_string.c:74
guard_selection_t * get_guard_selection_info(void)
Definition: entrynodes.c:303
static void channel_tls_close_method(channel_t *chan)
Definition: channeltls.c:400
uint64_t stats_n_authenticate_cells_processed
Definition: channeltls.c:90
const char * channel_state_to_string(channel_state_t state)
Definition: channel.c:315
uint16_t port
crypto_pk_t * tor_tls_cert_get_key(tor_x509_cert_t *cert)
Definition: x509_nss.c:285
static int channel_tls_has_queued_writes_method(channel_t *chan)
Definition: channeltls.c:624
#define tor_free(p)
Definition: malloc.h:52
int channel_is_local(channel_t *chan)
Definition: channel.c:3025
channel_t * channel_tls_connect(const tor_addr_t *addr, uint16_t port, const char *id_digest, const ed25519_public_key_t *ed_id)
Definition: channeltls.c:189
void channel_mark_for_close(channel_t *chan)
Definition: channel.c:1134
#define tor_fragile_assert()
Definition: util_bug.h:241
STATIC void channel_tls_process_authenticate_cell(var_cell_t *cell, channel_tls_t *chan)
Definition: channeltls.c:2312
static cert_encoding_t certs_cell_typenum_to_cert_type(int typenum)
Definition: channeltls.c:1918
const char * conn_state_to_string(int type, int state)
Definition: connection.c:275
int(* matches_extend_info)(channel_t *, extend_info_t *)
Definition: channel.h:361
uint64_t stats_n_versions_cells_processed
Definition: channeltls.c:80
circuitmux_t * cmux
Definition: channel.h:403
static int tor_addr_from_netinfo_addr(tor_addr_t *tor_addr, const netinfo_addr_t *netinfo_addr)
Definition: channeltls.c:1642
static int channel_tls_write_cell_method(channel_t *chan, cell_t *cell)
Definition: channeltls.c:800
static int command_allowed_before_handshake(uint8_t command)
Definition: channeltls.c:1367
char body[CELL_MAX_NETWORK_SIZE]
Definition: cell_queue_st.h:16
void connection_or_init_conn_from_address(or_connection_t *conn, const tor_addr_t *addr, uint16_t port, const char *id_digest, const ed25519_public_key_t *ed_id, int started_here)
Header file for scheduler*.c.
#define AUTHTYPE_RSA_SHA256_TLSSECRET
Definition: or.h:680
void rep_hist_padding_count_read(padding_type_t type)
Definition: rephist.c:2759
const char *(* get_remote_descr)(channel_t *, int)
Definition: channel.h:350
#define DIGEST256_LEN
Definition: digest_sizes.h:23
void channel_process_cell(channel_t *chan, cell_t *cell)
Definition: channel.c:1973
void channel_mark_incoming(channel_t *chan)
Definition: channel.c:3008
Header file for channel.c.
const char *(* describe_transport)(channel_t *)
Definition: channel.h:317
tor_assert(buffer)
#define AUTHTYPE_RSA_SHA256_RFC5705
Definition: or.h:688
static void channel_tls_process_netinfo_cell(cell_t *cell, channel_tls_t *tlschan)
Definition: channeltls.c:1685
static int channel_tls_get_transport_name_method(channel_t *chan, char **transport_out)
Definition: channeltls.c:540
tor_cert_t * tor_cert_parse(const uint8_t *encoded, const size_t len)
Definition: torcert.c:159
int connection_or_set_state_open(or_connection_t *conn)
#define tor_addr_from_ipv4h(dest, v4addr)
Definition: address.h:287
uint32_t magic
Definition: channel.h:183
static channel_listener_t * channel_tls_listener
Definition: channeltls.c:95
uint64_t stats_n_netinfo_cells_processed
Definition: channeltls.c:82
Header file for routermode.c.
int tor_asprintf(char **strp, const char *fmt,...)
Definition: printf.c:75
uint64_t stats_n_certs_cells_processed
Definition: channeltls.c:86
#define LD_CHANNEL
Definition: log.h:103
#define DIGEST_LEN
Definition: digest_sizes.h:20
void tor_addr_from_ipv6_bytes(tor_addr_t *dest, const char *ipv6_bytes)
Definition: address.c:885
static const char * channel_tls_get_remote_descr_method(channel_t *chan, int flags)
Definition: channeltls.c:564
static int channel_tls_matches_target_method(channel_t *chan, const tor_addr_t *target)
Definition: channeltls.c:719
var_cell_t * connection_or_compute_authenticate_cell_body(or_connection_t *conn, const int authtype, crypto_pk_t *signing_key, const ed25519_keypair_t *ed_signing_key, int server)
Master header file for Tor-specific functionality.
static int channel_tls_write_packed_cell_method(channel_t *chan, packed_cell_t *packed_cell)
Definition: channeltls.c:831
const char * hex_str(const char *from, size_t fromlen)
Definition: binascii.c:34
static int channel_tls_num_cells_writeable_method(channel_t *chan)
Definition: channeltls.c:771
uint64_t stats_n_padding_cells_processed
Definition: channeltls.c:78
STATIC void channel_tls_process_auth_challenge_cell(var_cell_t *cell, channel_tls_t *chan)
Definition: channeltls.c:2211
static time_t time_abs(time_t val)
Definition: channeltls.c:1673
#define OR_CONN_STATE_OPEN
Definition: orconn_event.h:53
uint16_t marked_for_close
void circuitmux_set_policy(circuitmux_t *cmux, const circuitmux_policy_t *pol)
Definition: circuitmux.c:477
#define LD_HANDSHAKE
Definition: log.h:99
static void channel_tls_process_versions_cell(var_cell_t *cell, channel_tls_t *tlschan)
Definition: channeltls.c:1428
static int channel_tls_get_remote_addr_method(channel_t *chan, tor_addr_t *addr_out)
Definition: channeltls.c:515
Header file for rephist.c.
const char * fmt_addr32(uint32_t addr)
Definition: address.c:1181
static uint16_t get_uint16(const void *cp)
Definition: bytes.h:37
void channel_mark_remote(channel_t *chan)
Definition: channel.c:3056
#define LOG_WARN
Definition: log.h:51
void channel_listener_change_state(channel_listener_t *chan_l, channel_listener_state_t to_state)
Definition: channel.c:1636
int tor_addr_is_null(const tor_addr_t *addr)
Definition: address.c:770
void connection_or_write_cell_to_buf(const cell_t *cell, or_connection_t *conn)
Header file for circuitmux_ewma.c.
void channel_listener_mark_for_close(channel_listener_t *chan_l)
Definition: channel.c:1173
#define LD_OR
Definition: log.h:90
int tor_digest_is_zero(const char *digest)
Definition: util_string.c:96
Headers for tortls.c.
STATIC void channel_tls_common_init(channel_tls_t *tlschan)
Definition: channeltls.c:149
int connection_or_nonopen_was_started_here(or_connection_t *conn)
#define OR_CONN_STATE_OR_HANDSHAKING_V2
Definition: orconn_event.h:47
void channel_init_listener(channel_listener_t *chan_l)
Definition: channel.c:860
static const char * channel_tls_describe_transport_method(channel_t *chan)
Definition: channeltls.c:423
void channel_register(channel_t *chan)
Definition: channel.c:386
void channel_mark_local(channel_t *chan)
Definition: channel.c:3040
static void channel_tls_listener_close_method(channel_listener_t *chan_l)
Definition: channeltls.c:893
int connection_or_client_learned_peer_id(or_connection_t *conn, const uint8_t *rsa_peer_id, const ed25519_public_key_t *ed_peer_id)
void or_handshake_state_record_cell(or_connection_t *conn, or_handshake_state_t *state, const cell_t *cell, int incoming)
double(* get_overhead_estimate)(channel_t *)
Definition: channel.h:331
void entry_guards_note_internet_connectivity(guard_selection_t *gs)
Definition: entrynodes.c:2031
int(* is_canonical)(channel_t *, int)
Definition: channel.h:359
void assert_connection_ok(connection_t *conn, time_t now)
Definition: connection.c:5251
channel_listener_t * channel_tls_start_listener(void)
Definition: channeltls.c:264
void channel_init(channel_t *chan)
Definition: channel.c:823
STATIC void channel_tls_process_certs_cell(var_cell_t *cell, channel_tls_t *chan)
Definition: channeltls.c:1952
int tor_snprintf(char *str, size_t size, const char *format,...)
Definition: printf.c:27
char * tor_addr_to_str_dup(const tor_addr_t *addr)
Definition: address.c:1134
void channel_mark_outgoing(channel_t *chan)
Definition: channel.c:3085
#define CELL_PAYLOAD_SIZE
Definition: or.h:576
int(* write_var_cell)(channel_t *, var_cell_t *)
Definition: channel.h:373
int connection_or_send_certs_cell(or_connection_t *conn)
ssize_t connection_or_num_cells_writeable(or_connection_t *conn)
static void channel_tls_free_method(channel_t *chan)
Definition: channeltls.c:461
void or_handshake_state_record_var_cell(or_connection_t *conn, or_handshake_state_t *state, const var_cell_t *cell, int incoming)
static int channel_tls_matches_extend_info_method(channel_t *chan, extend_info_t *extend_info)
Definition: channeltls.c:689
Header file for relay.c.
Header file for circuitmux.c.
int connection_or_send_auth_challenge_cell(or_connection_t *conn)
tor_x509_cert_t * tor_x509_cert_decode(const uint8_t *certificate, size_t certificate_len)
Definition: x509_nss.c:269
enum channel_s::@9 reason_for_closing
Header file for router.c.
#define fmt_addr(a)
Definition: address.h:211
smartlist_t * incoming_list
Definition: channel.h:506
void channel_listener_unregister(channel_listener_t *chan_l)
Definition: channel.c:524
#define ARRAY_LENGTH(x)
int connection_init_or_handshake_state(or_connection_t *conn, int started_here)
#define log_fn(severity, domain, args,...)
Definition: log.h:274
static int enter_v3_handshake_with_cell(var_cell_t *cell, channel_tls_t *tlschan)
Definition: channeltls.c:1388
channel_tls_t * chan
int(* matches_target)(channel_t *, const tor_addr_t *)
Definition: channel.h:363
long tv_udiff(const struct timeval *start, const struct timeval *end)
Definition: tvdiff.c:53
size_t crypto_pk_keysize(const crypto_pk_t *env)
Headers for tortls.c.
channel_t * channel_tls_handle_incoming(or_connection_t *orconn)
Definition: channeltls.c:327
Header file for control.c.
#define tor_addr_eq(a, b)
Definition: address.h:244
circid_t circ_id
Definition: var_cell_st.h:15
void channel_tls_free_all(void)
Definition: channeltls.c:295
#define OR_CONN_STATE_OR_HANDSHAKING_V3
Definition: orconn_event.h:51
void scheduler_channel_wants_writes(channel_t *chan)
Definition: scheduler.c:669
int authchallenge_type_is_better(uint16_t challenge_type_a, uint16_t challenge_type_b)
Header file for dirlist.c.
void(* free_fn)(channel_t *)
Definition: channel.h:313
int connection_or_send_versions(or_connection_t *conn, int v3_plus)
uint32_t addr
Definition: routerinfo_st.h:19
uint8_t command
Definition: var_cell_st.h:13
uint8_t command
Definition: cell_st.h:14
Header file for connection_or.c.
int crypto_digest256(char *digest, const char *m, size_t len, digest_algorithm_t algorithm)
int is_or_protocol_version_known(uint16_t v)
uint16_t payload_len
Definition: var_cell_st.h:17
channel_t * channel_tls_to_base(channel_tls_t *tlschan)
Definition: channeltls.c:369
void channel_tls_handle_var_cell(var_cell_t *var_cell, or_connection_t *conn)
Definition: channeltls.c:1161
tor_addr_t addr
tor_addr_t ipv6_addr
Definition: routerinfo_st.h:27
void tor_addr_copy(tor_addr_t *dest, const tor_addr_t *src)
Definition: address.c:904
uint64_t stats_n_vpadding_cells_processed
Definition: channeltls.c:84
#define LD_PROTOCOL
Definition: log.h:70
void(* close)(channel_listener_t *)
Definition: channel.h:496
void channel_mark_client(channel_t *chan)
Definition: channel.c:2937
static time_t current_second
Definition: mainloop.c:2163
Header file for networkstatus.c.
static const char * channel_tls_listener_describe_transport_method(channel_listener_t *chan_l)
Definition: channeltls.c:933
static int channel_tls_is_canonical_method(channel_t *chan, int req)
Definition: channeltls.c:652
uint64_t global_identifier
Definition: channel.h:197
void channel_listener_register(channel_listener_t *chan_l)
Definition: channel.c:483