1 /* * Copyright (c) 2012-2013, The Tor Project, Inc. */
2 /* See LICENSE for licensing information */
6 * \brief channel_t concrete subclass using or_connection_t
10 * Define this so channel.h gives us things only channel_t subclasses
14 #define TOR_CHANNEL_INTERNAL_
18 #include "channeltls.h"
19 #include "circuitmux.h"
20 #include "circuitmux_ewma.h"
22 #include "connection.h"
23 #include "connection_or.h"
27 #include "routerlist.h"
29 /** How many CELL_PADDING cells have we received, ever? */
30 uint64_t stats_n_padding_cells_processed
= 0;
31 /** How many CELL_VERSIONS cells have we received, ever? */
32 uint64_t stats_n_versions_cells_processed
= 0;
33 /** How many CELL_NETINFO cells have we received, ever? */
34 uint64_t stats_n_netinfo_cells_processed
= 0;
35 /** How many CELL_VPADDING cells have we received, ever? */
36 uint64_t stats_n_vpadding_cells_processed
= 0;
37 /** How many CELL_CERTS cells have we received, ever? */
38 uint64_t stats_n_certs_cells_processed
= 0;
39 /** How many CELL_AUTH_CHALLENGE cells have we received, ever? */
40 uint64_t stats_n_auth_challenge_cells_processed
= 0;
41 /** How many CELL_AUTHENTICATE cells have we received, ever? */
42 uint64_t stats_n_authenticate_cells_processed
= 0;
43 /** How many CELL_AUTHORIZE cells have we received, ever? */
44 uint64_t stats_n_authorize_cells_processed
= 0;
46 /** Active listener, if any */
47 channel_listener_t
*channel_tls_listener
= NULL
;
49 /* Utility function declarations */
50 static void channel_tls_common_init(channel_tls_t
*tlschan
);
52 /* channel_tls_t method declarations */
54 static void channel_tls_close_method(channel_t
*chan
);
55 static const char * channel_tls_describe_transport_method(channel_t
*chan
);
56 static void channel_tls_free_method(channel_t
*chan
);
58 channel_tls_get_remote_addr_method(channel_t
*chan
, tor_addr_t
*addr_out
);
60 channel_tls_get_transport_name_method(channel_t
*chan
, char **transport_out
);
62 channel_tls_get_remote_descr_method(channel_t
*chan
, int flags
);
63 static int channel_tls_has_queued_writes_method(channel_t
*chan
);
64 static int channel_tls_is_canonical_method(channel_t
*chan
, int req
);
66 channel_tls_matches_extend_info_method(channel_t
*chan
,
67 extend_info_t
*extend_info
);
68 static int channel_tls_matches_target_method(channel_t
*chan
,
69 const tor_addr_t
*target
);
70 static int channel_tls_write_cell_method(channel_t
*chan
,
72 static int channel_tls_write_packed_cell_method(channel_t
*chan
,
73 packed_cell_t
*packed_cell
);
74 static int channel_tls_write_var_cell_method(channel_t
*chan
,
75 var_cell_t
*var_cell
);
77 /* channel_listener_tls_t method declarations */
79 static void channel_tls_listener_close_method(channel_listener_t
*chan_l
);
81 channel_tls_listener_describe_transport_method(channel_listener_t
*chan_l
);
83 /** Handle incoming cells for the handshake stuff here rather than
84 * passing them on up. */
86 static void channel_tls_process_versions_cell(var_cell_t
*cell
,
87 channel_tls_t
*tlschan
);
88 static void channel_tls_process_netinfo_cell(cell_t
*cell
,
89 channel_tls_t
*tlschan
);
90 static void channel_tls_process_certs_cell(var_cell_t
*cell
,
91 channel_tls_t
*tlschan
);
92 static void channel_tls_process_auth_challenge_cell(var_cell_t
*cell
,
93 channel_tls_t
*tlschan
);
94 static void channel_tls_process_authenticate_cell(var_cell_t
*cell
,
95 channel_tls_t
*tlschan
);
96 static int command_allowed_before_handshake(uint8_t command
);
97 static int enter_v3_handshake_with_cell(var_cell_t
*cell
,
98 channel_tls_t
*tlschan
);
101 * Do parts of channel_tls_t initialization common to channel_tls_connect()
102 * and channel_tls_handle_incoming().
106 channel_tls_common_init(channel_tls_t
*tlschan
)
112 chan
= &(tlschan
->base_
);
114 chan
->magic
= TLS_CHAN_MAGIC
;
115 chan
->state
= CHANNEL_STATE_OPENING
;
116 chan
->close
= channel_tls_close_method
;
117 chan
->describe_transport
= channel_tls_describe_transport_method
;
118 chan
->free
= channel_tls_free_method
;
119 chan
->get_remote_addr
= channel_tls_get_remote_addr_method
;
120 chan
->get_remote_descr
= channel_tls_get_remote_descr_method
;
121 chan
->get_transport_name
= channel_tls_get_transport_name_method
;
122 chan
->has_queued_writes
= channel_tls_has_queued_writes_method
;
123 chan
->is_canonical
= channel_tls_is_canonical_method
;
124 chan
->matches_extend_info
= channel_tls_matches_extend_info_method
;
125 chan
->matches_target
= channel_tls_matches_target_method
;
126 chan
->write_cell
= channel_tls_write_cell_method
;
127 chan
->write_packed_cell
= channel_tls_write_packed_cell_method
;
128 chan
->write_var_cell
= channel_tls_write_var_cell_method
;
130 chan
->cmux
= circuitmux_alloc();
131 if (cell_ewma_enabled()) {
132 circuitmux_set_policy(chan
->cmux
, &ewma_policy
);
137 * Start a new TLS channel
139 * Launch a new OR connection to <b>addr</b>:<b>port</b> and expect to
140 * handshake with an OR with identity digest <b>id_digest</b>, and wrap
141 * it in a channel_tls_t.
145 channel_tls_connect(const tor_addr_t
*addr
, uint16_t port
,
146 const char *id_digest
)
148 channel_tls_t
*tlschan
= tor_malloc_zero(sizeof(*tlschan
));
149 channel_t
*chan
= &(tlschan
->base_
);
151 channel_tls_common_init(tlschan
);
153 log_debug(LD_CHANNEL
,
154 "In channel_tls_connect() for channel %p "
155 "(global id " U64_FORMAT
")",
157 U64_PRINTF_ARG(chan
->global_identifier
));
159 if (is_local_addr(addr
)) {
160 log_debug(LD_CHANNEL
,
161 "Marking new outgoing channel " U64_FORMAT
" at %p as local",
162 U64_PRINTF_ARG(chan
->global_identifier
), chan
);
163 channel_mark_local(chan
);
165 log_debug(LD_CHANNEL
,
166 "Marking new outgoing channel " U64_FORMAT
" at %p as remote",
167 U64_PRINTF_ARG(chan
->global_identifier
), chan
);
168 channel_mark_remote(chan
);
171 channel_mark_outgoing(chan
);
173 /* Set up or_connection stuff */
174 tlschan
->conn
= connection_or_connect(addr
, port
, id_digest
, tlschan
);
175 /* connection_or_connect() will fill in tlschan->conn */
176 if (!(tlschan
->conn
)) {
177 chan
->reason_for_closing
= CHANNEL_CLOSE_FOR_ERROR
;
178 channel_change_state(chan
, CHANNEL_STATE_ERROR
);
182 log_debug(LD_CHANNEL
,
183 "Got orconn %p for channel with global id " U64_FORMAT
,
184 tlschan
->conn
, U64_PRINTF_ARG(chan
->global_identifier
));
189 circuitmux_free(chan
->cmux
);
194 /* If we got one, we should register it */
195 if (chan
) channel_register(chan
);
201 * Return the current channel_tls_t listener
203 * Returns the current channel listener for incoming TLS connections, or
204 * NULL if none has been established
208 channel_tls_get_listener(void)
210 return channel_tls_listener
;
214 * Start a channel_tls_t listener if necessary
216 * Return the current channel_tls_t listener, or start one if we haven't yet,
221 channel_tls_start_listener(void)
223 channel_listener_t
*listener
;
225 if (!channel_tls_listener
) {
226 listener
= tor_malloc_zero(sizeof(*listener
));
227 channel_init_listener(listener
);
228 listener
->state
= CHANNEL_LISTENER_STATE_LISTENING
;
229 listener
->close
= channel_tls_listener_close_method
;
230 listener
->describe_transport
=
231 channel_tls_listener_describe_transport_method
;
233 channel_tls_listener
= listener
;
235 log_debug(LD_CHANNEL
,
236 "Starting TLS channel listener %p with global id " U64_FORMAT
,
237 listener
, U64_PRINTF_ARG(listener
->global_identifier
));
239 channel_listener_register(listener
);
240 } else listener
= channel_tls_listener
;
246 * Free everything on shutdown
248 * Not much to do here, since channel_free_all() takes care of a lot, but let's
249 * get rid of the listener.
253 channel_tls_free_all(void)
255 channel_listener_t
*old_listener
= NULL
;
257 log_debug(LD_CHANNEL
,
258 "Shutting down TLS channels...");
260 if (channel_tls_listener
) {
262 * When we close it, channel_tls_listener will get nulled out, so save
263 * a pointer so we can free it.
265 old_listener
= channel_tls_listener
;
266 log_debug(LD_CHANNEL
,
267 "Closing channel_tls_listener with ID " U64_FORMAT
269 U64_PRINTF_ARG(old_listener
->global_identifier
),
271 channel_listener_unregister(old_listener
);
272 channel_listener_mark_for_close(old_listener
);
273 channel_listener_free(old_listener
);
274 tor_assert(channel_tls_listener
== NULL
);
277 log_debug(LD_CHANNEL
,
278 "Done shutting down TLS channels");
282 * Create a new channel around an incoming or_connection_t
286 channel_tls_handle_incoming(or_connection_t
*orconn
)
288 channel_tls_t
*tlschan
= tor_malloc_zero(sizeof(*tlschan
));
289 channel_t
*chan
= &(tlschan
->base_
);
292 tor_assert(!(orconn
->chan
));
294 channel_tls_common_init(tlschan
);
296 /* Link the channel and orconn to each other */
297 tlschan
->conn
= orconn
;
298 orconn
->chan
= tlschan
;
300 if (is_local_addr(&(TO_CONN(orconn
)->addr
))) {
301 log_debug(LD_CHANNEL
,
302 "Marking new incoming channel " U64_FORMAT
" at %p as local",
303 U64_PRINTF_ARG(chan
->global_identifier
), chan
);
304 channel_mark_local(chan
);
306 log_debug(LD_CHANNEL
,
307 "Marking new incoming channel " U64_FORMAT
" at %p as remote",
308 U64_PRINTF_ARG(chan
->global_identifier
), chan
);
309 channel_mark_remote(chan
);
312 channel_mark_incoming(chan
);
315 channel_register(chan
);
325 * Cast a channel_tls_t to a channel_t.
329 channel_tls_to_base(channel_tls_t
*tlschan
)
331 if (!tlschan
) return NULL
;
333 return &(tlschan
->base_
);
337 * Cast a channel_t to a channel_tls_t, with appropriate type-checking
342 channel_tls_from_base(channel_t
*chan
)
344 if (!chan
) return NULL
;
346 tor_assert(chan
->magic
== TLS_CHAN_MAGIC
);
348 return (channel_tls_t
*)(chan
);
351 /********************************************
352 * Method implementations for channel_tls_t *
353 *******************************************/
356 * Close a channel_tls_t
358 * This implements the close method for channel_tls_t
362 channel_tls_close_method(channel_t
*chan
)
364 channel_tls_t
*tlschan
= BASE_CHAN_TO_TLS(chan
);
368 if (tlschan
->conn
) connection_or_close_normally(tlschan
->conn
, 1);
370 /* Weird - we'll have to change the state ourselves, I guess */
372 "Tried to close channel_tls_t %p with NULL conn",
374 channel_change_state(chan
, CHANNEL_STATE_ERROR
);
379 * Describe the transport for a channel_tls_t
381 * This returns the string "TLS channel on connection <id>" to the upper
386 channel_tls_describe_transport_method(channel_t
*chan
)
388 static char *buf
= NULL
;
390 channel_tls_t
*tlschan
;
391 const char *rv
= NULL
;
395 tlschan
= BASE_CHAN_TO_TLS(chan
);
398 id
= TO_CONN(tlschan
->conn
)->global_identifier
;
400 if (buf
) tor_free(buf
);
402 "TLS channel (connection " U64_FORMAT
")",
407 rv
= "TLS channel (no connection)";
414 * Free a channel_tls_t
416 * This is called by the generic channel layer when freeing a channel_tls_t;
417 * this happens either on a channel which has already reached
418 * CHANNEL_STATE_CLOSED or CHANNEL_STATE_ERROR from channel_run_cleanup() or
419 * on shutdown from channel_free_all(). In the latter case we might still
420 * have an orconn active (which connection_free_all() will get to later),
421 * so we should null out its channel pointer now.
425 channel_tls_free_method(channel_t
*chan
)
427 channel_tls_t
*tlschan
= BASE_CHAN_TO_TLS(chan
);
432 tlschan
->conn
->chan
= NULL
;
433 tlschan
->conn
= NULL
;
438 * Get the remote address of a channel_tls_t
440 * This implements the get_remote_addr method for channel_tls_t; copy the
441 * remote endpoint of the channel to addr_out and return 1 (always
442 * succeeds for this transport).
446 channel_tls_get_remote_addr_method(channel_t
*chan
, tor_addr_t
*addr_out
)
449 channel_tls_t
*tlschan
= BASE_CHAN_TO_TLS(chan
);
452 tor_assert(addr_out
);
455 tor_addr_copy(addr_out
, &(TO_CONN(tlschan
->conn
)->addr
));
457 } else tor_addr_make_unspec(addr_out
);
463 * Get the name of the pluggable transport used by a channel_tls_t.
465 * This implements the get_transport_name for channel_tls_t. If the
466 * channel uses a pluggable transport, copy its name to
467 * <b>transport_out</b> and return 0. If the channel did not use a
468 * pluggable transport, return -1. */
471 channel_tls_get_transport_name_method(channel_t
*chan
, char **transport_out
)
473 channel_tls_t
*tlschan
= BASE_CHAN_TO_TLS(chan
);
476 tor_assert(transport_out
);
477 tor_assert(tlschan
->conn
);
479 if (!tlschan
->conn
->ext_or_transport
)
482 *transport_out
= tor_strdup(tlschan
->conn
->ext_or_transport
);
487 * Get endpoint description of a channel_tls_t
489 * This implements the get_remote_descr method for channel_tls_t; it returns
490 * a text description of the remote endpoint of the channel suitable for use
491 * in log messages. The req parameter is 0 for the canonical address or 1 for
492 * the actual address seen.
496 channel_tls_get_remote_descr_method(channel_t
*chan
, int flags
)
498 #define MAX_DESCR_LEN 32
500 static char buf
[MAX_DESCR_LEN
+ 1];
501 channel_tls_t
*tlschan
= BASE_CHAN_TO_TLS(chan
);
503 const char *answer
= NULL
;
509 conn
= TO_CONN(tlschan
->conn
);
512 /* Canonical address with port*/
513 tor_snprintf(buf
, MAX_DESCR_LEN
+ 1,
514 "%s:%u", conn
->address
, conn
->port
);
517 case GRD_FLAG_ORIGINAL
:
518 /* Actual address with port */
519 addr_str
= tor_dup_addr(&(tlschan
->conn
->real_addr
));
520 tor_snprintf(buf
, MAX_DESCR_LEN
+ 1,
521 "%s:%u", addr_str
, conn
->port
);
525 case GRD_FLAG_ADDR_ONLY
:
526 /* Canonical address, no port */
527 strlcpy(buf
, conn
->address
, sizeof(buf
));
530 case GRD_FLAG_ORIGINAL
|GRD_FLAG_ADDR_ONLY
:
531 /* Actual address, no port */
532 addr_str
= tor_dup_addr(&(tlschan
->conn
->real_addr
));
533 strlcpy(buf
, addr_str
, sizeof(buf
));
538 /* Something's broken in channel.c */
542 strlcpy(buf
, "(No connection)", sizeof(buf
));
550 * Tell the upper layer if we have queued writes
552 * This implements the has_queued_writes method for channel_tls t_; it returns
553 * 1 iff we have queued writes on the outbuf of the underlying or_connection_t.
557 channel_tls_has_queued_writes_method(channel_t
*chan
)
560 channel_tls_t
*tlschan
= BASE_CHAN_TO_TLS(chan
);
563 if (!(tlschan
->conn
)) {
565 "something called has_queued_writes on a tlschan "
566 "(%p with ID " U64_FORMAT
" but no conn",
567 chan
, U64_PRINTF_ARG(chan
->global_identifier
));
570 outbuf_len
= (tlschan
->conn
!= NULL
) ?
571 connection_get_outbuf_len(TO_CONN(tlschan
->conn
)) :
574 return (outbuf_len
> 0);
578 * Tell the upper layer if we're canonical
580 * This implements the is_canonical method for channel_tls_t; if req is zero,
581 * it returns whether this is a canonical channel, and if it is one it returns
582 * whether that can be relied upon.
586 channel_tls_is_canonical_method(channel_t
*chan
, int req
)
589 channel_tls_t
*tlschan
= BASE_CHAN_TO_TLS(chan
);
596 answer
= tlschan
->conn
->is_canonical
;
600 * Is the is_canonical bit reliable? In protocols version 2 and up
601 * we get the canonical address from a NETINFO cell, but in older
602 * versions it might be based on an obsolete descriptor.
604 answer
= (tlschan
->conn
->link_proto
>= 2);
607 /* This shouldn't happen; channel.c is broken if it does */
611 /* else return 0 for tlschan->conn == NULL */
617 * Check if we match an extend_info_t
619 * This implements the matches_extend_info method for channel_tls_t; the upper
620 * layer wants to know if this channel matches an extend_info_t.
624 channel_tls_matches_extend_info_method(channel_t
*chan
,
625 extend_info_t
*extend_info
)
627 channel_tls_t
*tlschan
= BASE_CHAN_TO_TLS(chan
);
630 tor_assert(extend_info
);
632 /* Never match if we have no conn */
633 if (!(tlschan
->conn
)) {
635 "something called matches_extend_info on a tlschan "
636 "(%p with ID " U64_FORMAT
" but no conn",
637 chan
, U64_PRINTF_ARG(chan
->global_identifier
));
641 return (tor_addr_eq(&(extend_info
->addr
),
642 &(TO_CONN(tlschan
->conn
)->addr
)) &&
643 (extend_info
->port
== TO_CONN(tlschan
->conn
)->port
));
647 * Check if we match a target address; return true iff we do.
649 * This implements the matches_target method for channel_tls t_; the upper
650 * layer wants to know if this channel matches a target address when extending
655 channel_tls_matches_target_method(channel_t
*chan
,
656 const tor_addr_t
*target
)
658 channel_tls_t
*tlschan
= BASE_CHAN_TO_TLS(chan
);
663 /* Never match if we have no conn */
664 if (!(tlschan
->conn
)) {
666 "something called matches_target on a tlschan "
667 "(%p with ID " U64_FORMAT
" but no conn",
668 chan
, U64_PRINTF_ARG(chan
->global_identifier
));
672 return tor_addr_eq(&(tlschan
->conn
->real_addr
), target
);
676 * Write a cell to a channel_tls_t
678 * This implements the write_cell method for channel_tls_t; given a
679 * channel_tls_t and a cell_t, transmit the cell_t.
683 channel_tls_write_cell_method(channel_t
*chan
, cell_t
*cell
)
685 channel_tls_t
*tlschan
= BASE_CHAN_TO_TLS(chan
);
692 connection_or_write_cell_to_buf(cell
, tlschan
->conn
);
696 "something called write_cell on a tlschan "
697 "(%p with ID " U64_FORMAT
" but no conn",
698 chan
, U64_PRINTF_ARG(chan
->global_identifier
));
705 * Write a packed cell to a channel_tls_t
707 * This implements the write_packed_cell method for channel_tls_t; given a
708 * channel_tls_t and a packed_cell_t, transmit the packed_cell_t.
712 channel_tls_write_packed_cell_method(channel_t
*chan
,
713 packed_cell_t
*packed_cell
)
715 channel_tls_t
*tlschan
= BASE_CHAN_TO_TLS(chan
);
716 size_t cell_network_size
= get_cell_network_size(chan
->wide_circ_ids
);
720 tor_assert(packed_cell
);
723 connection_write_to_buf(packed_cell
->body
, cell_network_size
,
724 TO_CONN(tlschan
->conn
));
726 /* This is where the cell is finished; used to be done from relay.c */
727 packed_cell_free(packed_cell
);
731 "something called write_packed_cell on a tlschan "
732 "(%p with ID " U64_FORMAT
" but no conn",
733 chan
, U64_PRINTF_ARG(chan
->global_identifier
));
740 * Write a variable-length cell to a channel_tls_t
742 * This implements the write_var_cell method for channel_tls_t; given a
743 * channel_tls_t and a var_cell_t, transmit the var_cell_t.
747 channel_tls_write_var_cell_method(channel_t
*chan
, var_cell_t
*var_cell
)
749 channel_tls_t
*tlschan
= BASE_CHAN_TO_TLS(chan
);
753 tor_assert(var_cell
);
756 connection_or_write_var_cell_to_buf(var_cell
, tlschan
->conn
);
760 "something called write_var_cell on a tlschan "
761 "(%p with ID " U64_FORMAT
" but no conn",
762 chan
, U64_PRINTF_ARG(chan
->global_identifier
));
768 /*************************************************
769 * Method implementations for channel_listener_t *
770 ************************************************/
773 * Close a channel_listener_t
775 * This implements the close method for channel_listener_t
779 channel_tls_listener_close_method(channel_listener_t
*chan_l
)
784 * Listeners we just go ahead and change state through to CLOSED, but
785 * make sure to check if they're channel_tls_listener to NULL it out.
787 if (chan_l
== channel_tls_listener
)
788 channel_tls_listener
= NULL
;
790 if (!(chan_l
->state
== CHANNEL_LISTENER_STATE_CLOSING
||
791 chan_l
->state
== CHANNEL_LISTENER_STATE_CLOSED
||
792 chan_l
->state
== CHANNEL_LISTENER_STATE_ERROR
)) {
793 channel_listener_change_state(chan_l
, CHANNEL_LISTENER_STATE_CLOSING
);
796 if (chan_l
->incoming_list
) {
797 SMARTLIST_FOREACH_BEGIN(chan_l
->incoming_list
,
798 channel_t
*, ichan
) {
799 channel_mark_for_close(ichan
);
800 } SMARTLIST_FOREACH_END(ichan
);
802 smartlist_free(chan_l
->incoming_list
);
803 chan_l
->incoming_list
= NULL
;
806 if (!(chan_l
->state
== CHANNEL_LISTENER_STATE_CLOSED
||
807 chan_l
->state
== CHANNEL_LISTENER_STATE_ERROR
)) {
808 channel_listener_change_state(chan_l
, CHANNEL_LISTENER_STATE_CLOSED
);
813 * Describe the transport for a channel_listener_t
815 * This returns the string "TLS channel (listening)" to the upper
820 channel_tls_listener_describe_transport_method(channel_listener_t
*chan_l
)
824 return "TLS channel (listening)";
827 /*******************************************************
828 * Functions for handling events on an or_connection_t *
829 ******************************************************/
832 * Handle an orconn state change
834 * This function will be called by connection_or.c when the or_connection_t
835 * associated with this channel_tls_t changes state.
839 channel_tls_handle_state_change_on_orconn(channel_tls_t
*chan
,
840 or_connection_t
*conn
,
844 channel_t
*base_chan
;
848 tor_assert(conn
->chan
== chan
);
849 tor_assert(chan
->conn
== conn
);
850 /* -Werror appeasement */
851 tor_assert(old_state
== old_state
);
853 base_chan
= TLS_CHAN_TO_BASE(chan
);
855 /* Make sure the base connection state makes sense - shouldn't be error,
856 * closed or listening. */
858 tor_assert(base_chan
->state
== CHANNEL_STATE_OPENING
||
859 base_chan
->state
== CHANNEL_STATE_OPEN
||
860 base_chan
->state
== CHANNEL_STATE_MAINT
||
861 base_chan
->state
== CHANNEL_STATE_CLOSING
);
863 /* Did we just go to state open? */
864 if (state
== OR_CONN_STATE_OPEN
) {
866 * We can go to CHANNEL_STATE_OPEN from CHANNEL_STATE_OPENING or
867 * CHANNEL_STATE_MAINT on this.
869 channel_change_state(base_chan
, CHANNEL_STATE_OPEN
);
872 * Not open, so from CHANNEL_STATE_OPEN we go to CHANNEL_STATE_MAINT,
873 * otherwise no change.
875 if (base_chan
->state
== CHANNEL_STATE_OPEN
) {
876 channel_change_state(base_chan
, CHANNEL_STATE_MAINT
);
882 * Flush cells from a channel_tls_t
884 * Try to flush up to about num_cells cells, and return how many we flushed.
888 channel_tls_flush_some_cells(channel_tls_t
*chan
, ssize_t num_cells
)
894 if (flushed
>= num_cells
) goto done
;
897 * If channel_tls_t ever buffers anything below the channel_t layer, flush
901 flushed
+= channel_flush_some_cells(TLS_CHAN_TO_BASE(chan
),
902 num_cells
- flushed
);
905 * If channel_tls_t ever buffers anything below the channel_t layer, check
906 * how much we actually got and push it on down here.
914 * Check if a channel_tls_t has anything to flush
916 * Return true if there is any more to flush on this channel (cells in queue
917 * or active circuits).
921 channel_tls_more_to_flush(channel_tls_t
*chan
)
926 * If channel_tls_t ever buffers anything below channel_t, the
927 * check for that should go here first.
930 return channel_more_to_flush(TLS_CHAN_TO_BASE(chan
));
933 #ifdef KEEP_TIMING_STATS
936 * Timing states wrapper
938 * This is a wrapper function around the actual function that processes the
939 * <b>cell</b> that just arrived on <b>chan</b>. Increment <b>*time</b>
940 * by the number of microseconds used by the call to <b>*func(cell, chan)</b>.
944 channel_tls_time_process_cell(cell_t
*cell
, channel_tls_t
*chan
, int *time
,
945 void (*func
)(cell_t
*, channel_tls_t
*))
947 struct timeval start
, end
;
950 tor_gettimeofday(&start
);
954 tor_gettimeofday(&end
);
955 time_passed
= tv_udiff(&start
, &end
) ;
957 if (time_passed
> 10000) { /* more than 10ms */
958 log_debug(LD_OR
,"That call just took %ld ms.",time_passed
/1000);
961 if (time_passed
< 0) {
962 log_info(LD_GENERAL
,"That call took us back in time!");
966 *time
+= time_passed
;
971 * Handle an incoming cell on a channel_tls_t
973 * This is called from connection_or.c to handle an arriving cell; it checks
974 * for cell types specific to the handshake for this transport protocol and
975 * handles them, and queues all other cells to the channel_t layer, which
976 * eventually will hand them off to command.c.
980 channel_tls_handle_cell(cell_t
*cell
, or_connection_t
*conn
)
985 #ifdef KEEP_TIMING_STATS
986 #define PROCESS_CELL(tp, cl, cn) STMT_BEGIN { \
988 channel_tls_time_process_cell(cl, cn, & tp ## time , \
989 channel_tls_process_ ## tp ## _cell); \
992 #define PROCESS_CELL(tp, cl, cn) channel_tls_process_ ## tp ## _cell(cl, cn)
1001 log_warn(LD_CHANNEL
,
1002 "Got a cell_t on an OR connection with no channel");
1006 handshaking
= (TO_CONN(conn
)->state
!= OR_CONN_STATE_OPEN
);
1008 if (conn
->base_
.marked_for_close
)
1011 /* Reject all but VERSIONS and NETINFO when handshaking. */
1012 /* (VERSIONS should actually be impossible; it's variable-length.) */
1013 if (handshaking
&& cell
->command
!= CELL_VERSIONS
&&
1014 cell
->command
!= CELL_NETINFO
) {
1015 log_fn(LOG_PROTOCOL_WARN
, LD_PROTOCOL
,
1016 "Received unexpected cell command %d in chan state %s / "
1017 "conn state %s; closing the connection.",
1019 channel_state_to_string(TLS_CHAN_TO_BASE(chan
)->state
),
1020 conn_state_to_string(CONN_TYPE_OR
, TO_CONN(conn
)->state
));
1021 connection_or_close_for_error(conn
, 0);
1025 if (conn
->base_
.state
== OR_CONN_STATE_OR_HANDSHAKING_V3
)
1026 or_handshake_state_record_cell(conn
, conn
->handshake_state
, cell
, 1);
1028 switch (cell
->command
) {
1030 ++stats_n_padding_cells_processed
;
1034 tor_fragile_assert();
1037 ++stats_n_netinfo_cells_processed
;
1038 PROCESS_CELL(netinfo
, cell
, chan
);
1041 case CELL_CREATE_FAST
:
1043 case CELL_CREATED_FAST
:
1045 case CELL_RELAY_EARLY
:
1050 * These are all transport independent and we pass them up through the
1051 * channel_t mechanism. They are ultimately handled in command.c.
1053 channel_queue_cell(TLS_CHAN_TO_BASE(chan
), cell
);
1056 log_fn(LOG_INFO
, LD_PROTOCOL
,
1057 "Cell of unknown type (%d) received in channeltls.c. "
1065 * Handle an incoming variable-length cell on a channel_tls_t
1067 * Process a <b>var_cell</b> that was just received on <b>conn</b>. Keep
1068 * internal statistics about how many of each cell we've processed so far
1069 * this second, and the total number of microseconds it took to
1070 * process each type of cell. All the var_cell commands are handshake-
1071 * related and live below the channel_t layer, so no variable-length
1072 * cells ever get delivered in the current implementation, but I've left
1073 * the mechanism in place for future use.
1077 channel_tls_handle_var_cell(var_cell_t
*var_cell
, or_connection_t
*conn
)
1079 channel_tls_t
*chan
;
1081 #ifdef KEEP_TIMING_STATS
1082 /* how many of each cell have we seen so far this second? needs better
1084 static int num_versions
= 0, num_certs
= 0;
1085 static time_t current_second
= 0; /* from previous calls to time */
1086 time_t now
= time(NULL
);
1088 if (current_second
== 0) current_second
= now
;
1089 if (now
> current_second
) { /* the second has rolled over */
1092 "At end of second: %d versions (%d ms), %d certs (%d ms)",
1093 num_versions
, versions_time
/ ((now
- current_second
) * 1000),
1094 num_certs
, certs_time
/ ((now
- current_second
) * 1000));
1096 num_versions
= num_certs
= 0;
1097 versions_time
= certs_time
= 0;
1099 /* remember which second it is, for next time */
1100 current_second
= now
;
1104 tor_assert(var_cell
);
1110 log_warn(LD_CHANNEL
,
1111 "Got a var_cell_t on an OR connection with no channel");
1115 if (TO_CONN(conn
)->marked_for_close
)
1118 switch (TO_CONN(conn
)->state
) {
1119 case OR_CONN_STATE_OR_HANDSHAKING_V2
:
1120 if (var_cell
->command
!= CELL_VERSIONS
) {
1121 log_fn(LOG_PROTOCOL_WARN
, LD_PROTOCOL
,
1122 "Received a cell with command %d in unexpected "
1123 "orconn state \"%s\" [%d], channel state \"%s\" [%d]; "
1124 "closing the connection.",
1125 (int)(var_cell
->command
),
1126 conn_state_to_string(CONN_TYPE_OR
, TO_CONN(conn
)->state
),
1127 TO_CONN(conn
)->state
,
1128 channel_state_to_string(TLS_CHAN_TO_BASE(chan
)->state
),
1129 (int)(TLS_CHAN_TO_BASE(chan
)->state
));
1131 * The code in connection_or.c will tell channel_t to close for
1132 * error; it will go to CHANNEL_STATE_CLOSING, and then to
1133 * CHANNEL_STATE_ERROR when conn is closed.
1135 connection_or_close_for_error(conn
, 0);
1139 case OR_CONN_STATE_TLS_HANDSHAKING
:
1140 /* If we're using bufferevents, it's entirely possible for us to
1141 * notice "hey, data arrived!" before we notice "hey, the handshake
1142 * finished!" And we need to be accepting both at once to handle both
1143 * the v2 and v3 handshakes. */
1146 case OR_CONN_STATE_TLS_SERVER_RENEGOTIATING
:
1147 if (!(command_allowed_before_handshake(var_cell
->command
))) {
1148 log_fn(LOG_PROTOCOL_WARN
, LD_PROTOCOL
,
1149 "Received a cell with command %d in unexpected "
1150 "orconn state \"%s\" [%d], channel state \"%s\" [%d]; "
1151 "closing the connection.",
1152 (int)(var_cell
->command
),
1153 conn_state_to_string(CONN_TYPE_OR
, TO_CONN(conn
)->state
),
1154 (int)(TO_CONN(conn
)->state
),
1155 channel_state_to_string(TLS_CHAN_TO_BASE(chan
)->state
),
1156 (int)(TLS_CHAN_TO_BASE(chan
)->state
));
1157 /* see above comment about CHANNEL_STATE_ERROR */
1158 connection_or_close_for_error(conn
, 0);
1161 if (enter_v3_handshake_with_cell(var_cell
, chan
) < 0)
1165 case OR_CONN_STATE_OR_HANDSHAKING_V3
:
1166 if (var_cell
->command
!= CELL_AUTHENTICATE
)
1167 or_handshake_state_record_var_cell(conn
, conn
->handshake_state
,
1169 break; /* Everything is allowed */
1170 case OR_CONN_STATE_OPEN
:
1171 if (conn
->link_proto
< 3) {
1172 log_fn(LOG_PROTOCOL_WARN
, LD_PROTOCOL
,
1173 "Received a variable-length cell with command %d in orconn "
1174 "state %s [%d], channel state %s [%d] with link protocol %d; "
1176 (int)(var_cell
->command
),
1177 conn_state_to_string(CONN_TYPE_OR
, TO_CONN(conn
)->state
),
1178 (int)(TO_CONN(conn
)->state
),
1179 channel_state_to_string(TLS_CHAN_TO_BASE(chan
)->state
),
1180 (int)(TLS_CHAN_TO_BASE(chan
)->state
),
1181 (int)(conn
->link_proto
));
1186 log_fn(LOG_PROTOCOL_WARN
, LD_PROTOCOL
,
1187 "Received var-length cell with command %d in unexpected "
1188 "orconn state \"%s\" [%d], channel state \"%s\" [%d]; "
1190 (int)(var_cell
->command
),
1191 conn_state_to_string(CONN_TYPE_OR
, TO_CONN(conn
)->state
),
1192 (int)(TO_CONN(conn
)->state
),
1193 channel_state_to_string(TLS_CHAN_TO_BASE(chan
)->state
),
1194 (int)(TLS_CHAN_TO_BASE(chan
)->state
));
1198 /* Now handle the cell */
1200 switch (var_cell
->command
) {
1202 ++stats_n_versions_cells_processed
;
1203 PROCESS_CELL(versions
, var_cell
, chan
);
1206 ++stats_n_vpadding_cells_processed
;
1210 ++stats_n_certs_cells_processed
;
1211 PROCESS_CELL(certs
, var_cell
, chan
);
1213 case CELL_AUTH_CHALLENGE
:
1214 ++stats_n_auth_challenge_cells_processed
;
1215 PROCESS_CELL(auth_challenge
, var_cell
, chan
);
1217 case CELL_AUTHENTICATE
:
1218 ++stats_n_authenticate_cells_processed
;
1219 PROCESS_CELL(authenticate
, var_cell
, chan
);
1221 case CELL_AUTHORIZE
:
1222 ++stats_n_authorize_cells_processed
;
1223 /* Ignored so far. */
1226 log_fn(LOG_INFO
, LD_PROTOCOL
,
1227 "Variable-length cell of unknown type (%d) received.",
1228 (int)(var_cell
->command
));
1234 * Update channel marks after connection_or.c has changed an address
1236 * This is called from connection_or_init_conn_from_address() after the
1237 * connection's _base.addr or real_addr fields have potentially been changed
1238 * so we can recalculate the local mark. Notably, this happens when incoming
1239 * connections are reverse-proxied and we only learn the real address of the
1240 * remote router by looking it up in the consensus after we finish the
1241 * handshake and know an authenticated identity digest.
1245 channel_tls_update_marks(or_connection_t
*conn
)
1247 channel_t
*chan
= NULL
;
1250 tor_assert(conn
->chan
);
1252 chan
= TLS_CHAN_TO_BASE(conn
->chan
);
1254 if (is_local_addr(&(TO_CONN(conn
)->addr
))) {
1255 if (!channel_is_local(chan
)) {
1256 log_debug(LD_CHANNEL
,
1257 "Marking channel " U64_FORMAT
" at %p as local",
1258 U64_PRINTF_ARG(chan
->global_identifier
), chan
);
1259 channel_mark_local(chan
);
1262 if (channel_is_local(chan
)) {
1263 log_debug(LD_CHANNEL
,
1264 "Marking channel " U64_FORMAT
" at %p as remote",
1265 U64_PRINTF_ARG(chan
->global_identifier
), chan
);
1266 channel_mark_remote(chan
);
1272 * Check if this cell type is allowed before the handshake is finished
1274 * Return true if <b>command</b> is a cell command that's allowed to start a
1279 command_allowed_before_handshake(uint8_t command
)
1284 case CELL_AUTHORIZE
:
1292 * Start a V3 handshake on an incoming connection
1294 * Called when we as a server receive an appropriate cell while waiting
1295 * either for a cell or a TLS handshake. Set the connection's state to
1296 * "handshaking_v3', initializes the or_handshake_state field as needed,
1297 * and add the cell to the hash of incoming cells.)
1301 enter_v3_handshake_with_cell(var_cell_t
*cell
, channel_tls_t
*chan
)
1303 int started_here
= 0;
1307 tor_assert(chan
->conn
);
1309 started_here
= connection_or_nonopen_was_started_here(chan
->conn
);
1311 tor_assert(TO_CONN(chan
->conn
)->state
== OR_CONN_STATE_TLS_HANDSHAKING
||
1312 TO_CONN(chan
->conn
)->state
==
1313 OR_CONN_STATE_TLS_SERVER_RENEGOTIATING
);
1316 log_fn(LOG_PROTOCOL_WARN
, LD_OR
,
1317 "Received a cell while TLS-handshaking, not in "
1318 "OR_HANDSHAKING_V3, on a connection we originated.");
1320 connection_or_block_renegotiation(chan
->conn
);
1321 chan
->conn
->base_
.state
= OR_CONN_STATE_OR_HANDSHAKING_V3
;
1322 if (connection_init_or_handshake_state(chan
->conn
, started_here
) < 0) {
1323 connection_or_close_for_error(chan
->conn
, 0);
1326 or_handshake_state_record_var_cell(chan
->conn
,
1327 chan
->conn
->handshake_state
, cell
, 1);
1332 * Process a 'versions' cell.
1334 * This function is called to handle an incoming VERSIONS cell; the current
1335 * link protocol version must be 0 to indicate that no version has yet been
1336 * negotiated. We compare the versions in the cell to the list of versions
1337 * we support, pick the highest version we have in common, and continue the
1338 * negotiation from there.
1342 channel_tls_process_versions_cell(var_cell_t
*cell
, channel_tls_t
*chan
)
1344 int highest_supported_version
= 0;
1345 int started_here
= 0;
1349 tor_assert(chan
->conn
);
1351 if ((cell
->payload_len
% 2) == 1) {
1352 log_fn(LOG_PROTOCOL_WARN
, LD_OR
,
1353 "Received a VERSION cell with odd payload length %d; "
1354 "closing connection.",cell
->payload_len
);
1355 connection_or_close_for_error(chan
->conn
, 0);
1359 started_here
= connection_or_nonopen_was_started_here(chan
->conn
);
1361 if (chan
->conn
->link_proto
!= 0 ||
1362 (chan
->conn
->handshake_state
&&
1363 chan
->conn
->handshake_state
->received_versions
)) {
1364 log_fn(LOG_PROTOCOL_WARN
, LD_OR
,
1365 "Received a VERSIONS cell on a connection with its version "
1366 "already set to %d; dropping",
1367 (int)(chan
->conn
->link_proto
));
1370 switch (chan
->conn
->base_
.state
)
1372 case OR_CONN_STATE_OR_HANDSHAKING_V2
:
1373 case OR_CONN_STATE_OR_HANDSHAKING_V3
:
1375 case OR_CONN_STATE_TLS_HANDSHAKING
:
1376 case OR_CONN_STATE_TLS_SERVER_RENEGOTIATING
:
1378 log_fn(LOG_PROTOCOL_WARN
, LD_OR
,
1379 "VERSIONS cell while in unexpected state");
1383 tor_assert(chan
->conn
->handshake_state
);
1387 const uint8_t *cp
= cell
->payload
;
1388 for (i
= 0; i
< cell
->payload_len
/ 2; ++i
, cp
+= 2) {
1389 uint16_t v
= ntohs(get_uint16(cp
));
1390 if (is_or_protocol_version_known(v
) && v
> highest_supported_version
)
1391 highest_supported_version
= v
;
1394 if (!highest_supported_version
) {
1395 log_fn(LOG_PROTOCOL_WARN
, LD_OR
,
1396 "Couldn't find a version in common between my version list and the "
1397 "list in the VERSIONS cell; closing connection.");
1398 connection_or_close_for_error(chan
->conn
, 0);
1400 } else if (highest_supported_version
== 1) {
1401 /* Negotiating version 1 makes no sense, since version 1 has no VERSIONS
1403 log_fn(LOG_PROTOCOL_WARN
, LD_OR
,
1404 "Used version negotiation protocol to negotiate a v1 connection. "
1405 "That's crazily non-compliant. Closing connection.");
1406 connection_or_close_for_error(chan
->conn
, 0);
1408 } else if (highest_supported_version
< 3 &&
1409 chan
->conn
->base_
.state
== OR_CONN_STATE_OR_HANDSHAKING_V3
) {
1410 log_fn(LOG_PROTOCOL_WARN
, LD_OR
,
1411 "Negotiated link protocol 2 or lower after doing a v3 TLS "
1412 "handshake. Closing connection.");
1413 connection_or_close_for_error(chan
->conn
, 0);
1415 } else if (highest_supported_version
!= 2 &&
1416 chan
->conn
->base_
.state
== OR_CONN_STATE_OR_HANDSHAKING_V2
) {
1417 /* XXXX This should eventually be a log_protocol_warn */
1418 log_fn(LOG_WARN
, LD_OR
,
1419 "Negotiated link with non-2 protocol after doing a v2 TLS "
1420 "handshake with %s. Closing connection.",
1421 fmt_addr(&chan
->conn
->base_
.addr
));
1422 connection_or_close_for_error(chan
->conn
, 0);
1426 chan
->conn
->link_proto
= highest_supported_version
;
1427 chan
->conn
->handshake_state
->received_versions
= 1;
1429 if (chan
->conn
->link_proto
== 2) {
1431 "Negotiated version %d with %s:%d; sending NETINFO.",
1432 highest_supported_version
,
1433 safe_str_client(chan
->conn
->base_
.address
),
1434 chan
->conn
->base_
.port
);
1436 if (connection_or_send_netinfo(chan
->conn
) < 0) {
1437 connection_or_close_for_error(chan
->conn
, 0);
1441 const int send_versions
= !started_here
;
1442 /* If we want to authenticate, send a CERTS cell */
1443 const int send_certs
= !started_here
|| public_server_mode(get_options());
1444 /* If we're a host that got a connection, ask for authentication. */
1445 const int send_chall
= !started_here
;
1446 /* If our certs cell will authenticate us, we can send a netinfo cell
1448 const int send_netinfo
= !started_here
;
1449 const int send_any
=
1450 send_versions
|| send_certs
|| send_chall
|| send_netinfo
;
1451 tor_assert(chan
->conn
->link_proto
>= 3);
1454 "Negotiated version %d with %s:%d; %s%s%s%s%s",
1455 highest_supported_version
,
1456 safe_str_client(chan
->conn
->base_
.address
),
1457 chan
->conn
->base_
.port
,
1458 send_any
? "Sending cells:" : "Waiting for CERTS cell",
1459 send_versions
? " VERSIONS" : "",
1460 send_certs
? " CERTS" : "",
1461 send_chall
? " AUTH_CHALLENGE" : "",
1462 send_netinfo
? " NETINFO" : "");
1464 #ifdef DISABLE_V3_LINKPROTO_SERVERSIDE
1466 connection_or_close_normally(chan
->conn
, 1);
1471 if (send_versions
) {
1472 if (connection_or_send_versions(chan
->conn
, 1) < 0) {
1473 log_warn(LD_OR
, "Couldn't send versions cell");
1474 connection_or_close_for_error(chan
->conn
, 0);
1479 /* We set this after sending the verions cell. */
1480 /*XXXXX symbolic const.*/
1481 chan
->base_
.wide_circ_ids
=
1482 chan
->conn
->link_proto
>= MIN_LINK_PROTO_FOR_WIDE_CIRC_IDS
;
1483 chan
->conn
->wide_circ_ids
= chan
->base_
.wide_circ_ids
;
1486 if (connection_or_send_certs_cell(chan
->conn
) < 0) {
1487 log_warn(LD_OR
, "Couldn't send certs cell");
1488 connection_or_close_for_error(chan
->conn
, 0);
1493 if (connection_or_send_auth_challenge_cell(chan
->conn
) < 0) {
1494 log_warn(LD_OR
, "Couldn't send auth_challenge cell");
1495 connection_or_close_for_error(chan
->conn
, 0);
1500 if (connection_or_send_netinfo(chan
->conn
) < 0) {
1501 log_warn(LD_OR
, "Couldn't send netinfo cell");
1502 connection_or_close_for_error(chan
->conn
, 0);
1510 * Process a 'netinfo' cell
1512 * This function is called to handle an incoming NETINFO cell; read and act
1513 * on its contents, and set the connection state to "open".
1517 channel_tls_process_netinfo_cell(cell_t
*cell
, channel_tls_t
*chan
)
1520 uint8_t my_addr_type
;
1521 uint8_t my_addr_len
;
1522 const uint8_t *my_addr_ptr
;
1523 const uint8_t *cp
, *end
;
1524 uint8_t n_other_addrs
;
1525 time_t now
= time(NULL
);
1527 long apparent_skew
= 0;
1528 tor_addr_t my_apparent_addr
= TOR_ADDR_NULL
;
1532 tor_assert(chan
->conn
);
1534 if (chan
->conn
->link_proto
< 2) {
1535 log_fn(LOG_PROTOCOL_WARN
, LD_OR
,
1536 "Received a NETINFO cell on %s connection; dropping.",
1537 chan
->conn
->link_proto
== 0 ? "non-versioned" : "a v1");
1540 if (chan
->conn
->base_
.state
!= OR_CONN_STATE_OR_HANDSHAKING_V2
&&
1541 chan
->conn
->base_
.state
!= OR_CONN_STATE_OR_HANDSHAKING_V3
) {
1542 log_fn(LOG_PROTOCOL_WARN
, LD_OR
,
1543 "Received a NETINFO cell on non-handshaking connection; dropping.");
1546 tor_assert(chan
->conn
->handshake_state
&&
1547 chan
->conn
->handshake_state
->received_versions
);
1549 if (chan
->conn
->base_
.state
== OR_CONN_STATE_OR_HANDSHAKING_V3
) {
1550 tor_assert(chan
->conn
->link_proto
>= 3);
1551 if (chan
->conn
->handshake_state
->started_here
) {
1552 if (!(chan
->conn
->handshake_state
->authenticated
)) {
1553 log_fn(LOG_PROTOCOL_WARN
, LD_OR
,
1554 "Got a NETINFO cell from server, "
1555 "but no authentication. Closing the connection.");
1556 connection_or_close_for_error(chan
->conn
, 0);
1560 /* we're the server. If the client never authenticated, we have
1561 some housekeeping to do.*/
1562 if (!(chan
->conn
->handshake_state
->authenticated
)) {
1563 tor_assert(tor_digest_is_zero(
1564 (const char*)(chan
->conn
->handshake_state
->
1565 authenticated_peer_id
)));
1566 channel_set_circid_type(TLS_CHAN_TO_BASE(chan
), NULL
,
1567 chan
->conn
->link_proto
< MIN_LINK_PROTO_FOR_WIDE_CIRC_IDS
);
1569 connection_or_init_conn_from_address(chan
->conn
,
1570 &(chan
->conn
->base_
.addr
),
1571 chan
->conn
->base_
.port
,
1572 (const char*)(chan
->conn
->handshake_state
->
1573 authenticated_peer_id
),
1579 /* Decode the cell. */
1580 timestamp
= ntohl(get_uint32(cell
->payload
));
1581 if (labs(now
- chan
->conn
->handshake_state
->sent_versions_at
) < 180) {
1582 apparent_skew
= now
- timestamp
;
1585 my_addr_type
= (uint8_t) cell
->payload
[4];
1586 my_addr_len
= (uint8_t) cell
->payload
[5];
1587 my_addr_ptr
= (uint8_t*) cell
->payload
+ 6;
1588 end
= cell
->payload
+ CELL_PAYLOAD_SIZE
;
1589 cp
= cell
->payload
+ 6 + my_addr_len
;
1591 /* We used to check:
1592 * if (my_addr_len >= CELL_PAYLOAD_SIZE - 6) {
1594 * This is actually never going to happen, since my_addr_len is at most 255,
1595 * and CELL_PAYLOAD_LEN - 6 is 503. So we know that cp is < end. */
1597 if (my_addr_type
== RESOLVED_TYPE_IPV4
&& my_addr_len
== 4) {
1598 tor_addr_from_ipv4n(&my_apparent_addr
, get_uint32(my_addr_ptr
));
1599 } else if (my_addr_type
== RESOLVED_TYPE_IPV6
&& my_addr_len
== 16) {
1600 tor_addr_from_ipv6_bytes(&my_apparent_addr
, (const char *) my_addr_ptr
);
1603 n_other_addrs
= (uint8_t) *cp
++;
1604 while (n_other_addrs
&& cp
< end
-2) {
1605 /* Consider all the other addresses; if any matches, this connection is
1608 const uint8_t *next
=
1609 decode_address_from_payload(&addr
, cp
, (int)(end
-cp
));
1611 log_fn(LOG_PROTOCOL_WARN
, LD_OR
,
1612 "Bad address in netinfo cell; closing connection.");
1613 connection_or_close_for_error(chan
->conn
, 0);
1616 if (tor_addr_eq(&addr
, &(chan
->conn
->real_addr
))) {
1617 connection_or_set_canonical(chan
->conn
, 1);
1624 /* Act on apparent skew. */
1625 /** Warn when we get a netinfo skew with at least this value. */
1626 #define NETINFO_NOTICE_SKEW 3600
1627 if (labs(apparent_skew
) > NETINFO_NOTICE_SKEW
&&
1628 router_get_by_id_digest(chan
->conn
->identity_digest
)) {
1631 /*XXXX be smarter about when everybody says we are skewed. */
1632 if (router_digest_is_trusted_dir(chan
->conn
->identity_digest
))
1633 severity
= LOG_WARN
;
1635 severity
= LOG_INFO
;
1636 format_time_interval(dbuf
, sizeof(dbuf
), apparent_skew
);
1637 log_fn(severity
, LD_GENERAL
,
1638 "Received NETINFO cell with skewed time from "
1639 "server at %s:%d. It seems that our clock is %s by %s, or "
1640 "that theirs is %s. Tor requires an accurate clock to work: "
1641 "please check your time and date settings.",
1642 chan
->conn
->base_
.address
,
1643 (int)(chan
->conn
->base_
.port
),
1644 apparent_skew
> 0 ? "ahead" : "behind",
1646 apparent_skew
> 0 ? "behind" : "ahead");
1647 if (severity
== LOG_WARN
) /* only tell the controller if an authority */
1648 control_event_general_status(LOG_WARN
,
1649 "CLOCK_SKEW SKEW=%ld SOURCE=OR:%s:%d",
1651 chan
->conn
->base_
.address
,
1652 chan
->conn
->base_
.port
);
1655 /* XXX maybe act on my_apparent_addr, if the source is sufficiently
1658 if (! chan
->conn
->handshake_state
->sent_netinfo
) {
1659 /* If we were prepared to authenticate, but we never got an AUTH_CHALLENGE
1660 * cell, then we would not previously have sent a NETINFO cell. Do so
1662 if (connection_or_send_netinfo(chan
->conn
) < 0) {
1663 connection_or_close_for_error(chan
->conn
, 0);
1668 if (connection_or_set_state_open(chan
->conn
) < 0) {
1669 log_fn(LOG_PROTOCOL_WARN
, LD_OR
,
1670 "Got good NETINFO cell from %s:%d; but "
1671 "was unable to make the OR connection become open.",
1672 safe_str_client(chan
->conn
->base_
.address
),
1673 chan
->conn
->base_
.port
);
1674 connection_or_close_for_error(chan
->conn
, 0);
1677 "Got good NETINFO cell from %s:%d; OR connection is now "
1678 "open, using protocol version %d. Its ID digest is %s. "
1679 "Our address is apparently %s.",
1680 safe_str_client(chan
->conn
->base_
.address
),
1681 chan
->conn
->base_
.port
,
1682 (int)(chan
->conn
->link_proto
),
1683 hex_str(TLS_CHAN_TO_BASE(chan
)->identity_digest
,
1685 tor_addr_is_null(&my_apparent_addr
) ?
1686 "<none>" : fmt_and_decorate_addr(&my_apparent_addr
));
1688 assert_connection_ok(TO_CONN(chan
->conn
),time(NULL
));
1692 * Process a CERTS cell from a channel.
1694 * This function is called to process an incoming CERTS cell on a
1697 * If the other side should not have sent us a CERTS cell, or the cell is
1698 * malformed, or it is supposed to authenticate the TLS key but it doesn't,
1699 * then mark the connection.
1701 * If the cell has a good cert chain and we're doing a v3 handshake, then
1702 * store the certificates in or_handshake_state. If this is the client side
1703 * of the connection, we then authenticate the server or mark the connection.
1704 * If it's the server side, wait for an AUTHENTICATE cell.
1708 channel_tls_process_certs_cell(var_cell_t
*cell
, channel_tls_t
*chan
)
1710 tor_cert_t
*link_cert
= NULL
;
1711 tor_cert_t
*id_cert
= NULL
;
1712 tor_cert_t
*auth_cert
= NULL
;
1715 int send_netinfo
= 0;
1719 tor_assert(chan
->conn
);
1723 log_fn(LOG_PROTOCOL_WARN, LD_PROTOCOL, \
1724 "Received a bad CERTS cell from %s:%d: %s", \
1725 safe_str(chan->conn->base_.address), \
1726 chan->conn->base_.port, (s)); \
1727 connection_or_close_for_error(chan->conn, 0); \
1731 if (chan
->conn
->base_
.state
!= OR_CONN_STATE_OR_HANDSHAKING_V3
)
1732 ERR("We're not doing a v3 handshake!");
1733 if (chan
->conn
->link_proto
< 3)
1734 ERR("We're not using link protocol >= 3");
1735 if (chan
->conn
->handshake_state
->received_certs_cell
)
1736 ERR("We already got one");
1737 if (chan
->conn
->handshake_state
->authenticated
) {
1738 /* Should be unreachable, but let's make sure. */
1739 ERR("We're already authenticated!");
1741 if (cell
->payload_len
< 1)
1742 ERR("It had no body");
1744 ERR("It had a nonzero circuit ID");
1746 n_certs
= cell
->payload
[0];
1747 ptr
= cell
->payload
+ 1;
1748 for (i
= 0; i
< n_certs
; ++i
) {
1751 if (cell
->payload_len
< 3)
1753 if (ptr
> cell
->payload
+ cell
->payload_len
- 3) {
1757 cert_len
= ntohs(get_uint16(ptr
+1));
1758 if (cell
->payload_len
< 3 + cert_len
)
1760 if (ptr
> cell
->payload
+ cell
->payload_len
- cert_len
- 3) {
1763 if (cert_type
== OR_CERT_TYPE_TLS_LINK
||
1764 cert_type
== OR_CERT_TYPE_ID_1024
||
1765 cert_type
== OR_CERT_TYPE_AUTH_1024
) {
1766 tor_cert_t
*cert
= tor_cert_decode(ptr
+ 3, cert_len
);
1768 log_fn(LOG_PROTOCOL_WARN
, LD_PROTOCOL
,
1769 "Received undecodable certificate in CERTS cell from %s:%d",
1770 safe_str(chan
->conn
->base_
.address
),
1771 chan
->conn
->base_
.port
);
1773 if (cert_type
== OR_CERT_TYPE_TLS_LINK
) {
1775 tor_cert_free(cert
);
1776 ERR("Too many TLS_LINK certificates");
1779 } else if (cert_type
== OR_CERT_TYPE_ID_1024
) {
1781 tor_cert_free(cert
);
1782 ERR("Too many ID_1024 certificates");
1785 } else if (cert_type
== OR_CERT_TYPE_AUTH_1024
) {
1787 tor_cert_free(cert
);
1788 ERR("Too many AUTH_1024 certificates");
1792 tor_cert_free(cert
);
1796 ptr
+= 3 + cert_len
;
1800 ERR("It ends in the middle of a certificate");
1803 if (chan
->conn
->handshake_state
->started_here
) {
1805 if (! (id_cert
&& link_cert
))
1806 ERR("The certs we wanted were missing");
1807 /* Okay. We should be able to check the certificates now. */
1808 if (! tor_tls_cert_matches_key(chan
->conn
->tls
, link_cert
)) {
1809 ERR("The link certificate didn't match the TLS public key");
1811 /* Note that this warns more loudly about time and validity if we were
1812 * _trying_ to connect to an authority, not necessarily if we _did_ connect
1814 if (router_digest_is_trusted_dir(
1815 TLS_CHAN_TO_BASE(chan
)->identity_digest
))
1816 severity
= LOG_WARN
;
1818 severity
= LOG_PROTOCOL_WARN
;
1820 if (! tor_tls_cert_is_valid(severity
, link_cert
, id_cert
, 0))
1821 ERR("The link certificate was not valid");
1822 if (! tor_tls_cert_is_valid(severity
, id_cert
, id_cert
, 1))
1823 ERR("The ID certificate was not valid");
1825 chan
->conn
->handshake_state
->authenticated
= 1;
1827 const digests_t
*id_digests
= tor_cert_get_id_digests(id_cert
);
1828 crypto_pk_t
*identity_rcvd
;
1830 ERR("Couldn't compute digests for key in ID cert");
1832 identity_rcvd
= tor_tls_cert_get_key(id_cert
);
1834 ERR("Internal error: Couldn't get RSA key from ID cert.");
1835 memcpy(chan
->conn
->handshake_state
->authenticated_peer_id
,
1836 id_digests
->d
[DIGEST_SHA1
], DIGEST_LEN
);
1837 channel_set_circid_type(TLS_CHAN_TO_BASE(chan
), identity_rcvd
,
1838 chan
->conn
->link_proto
< MIN_LINK_PROTO_FOR_WIDE_CIRC_IDS
);
1839 crypto_pk_free(identity_rcvd
);
1842 if (connection_or_client_learned_peer_id(chan
->conn
,
1843 chan
->conn
->handshake_state
->authenticated_peer_id
) < 0)
1844 ERR("Problem setting or checking peer id");
1847 "Got some good certificates from %s:%d: Authenticated it.",
1848 safe_str(chan
->conn
->base_
.address
), chan
->conn
->base_
.port
);
1850 chan
->conn
->handshake_state
->id_cert
= id_cert
;
1853 if (!public_server_mode(get_options())) {
1854 /* If we initiated the connection and we are not a public server, we
1855 * aren't planning to authenticate at all. At this point we know who we
1856 * are talking to, so we can just send a netinfo now. */
1860 if (! (id_cert
&& auth_cert
))
1861 ERR("The certs we wanted were missing");
1863 /* Remember these certificates so we can check an AUTHENTICATE cell */
1864 if (! tor_tls_cert_is_valid(LOG_PROTOCOL_WARN
, auth_cert
, id_cert
, 1))
1865 ERR("The authentication certificate was not valid");
1866 if (! tor_tls_cert_is_valid(LOG_PROTOCOL_WARN
, id_cert
, id_cert
, 1))
1867 ERR("The ID certificate was not valid");
1870 "Got some good certificates from %s:%d: "
1871 "Waiting for AUTHENTICATE.",
1872 safe_str(chan
->conn
->base_
.address
),
1873 chan
->conn
->base_
.port
);
1874 /* XXXX check more stuff? */
1876 chan
->conn
->handshake_state
->id_cert
= id_cert
;
1877 chan
->conn
->handshake_state
->auth_cert
= auth_cert
;
1878 id_cert
= auth_cert
= NULL
;
1881 chan
->conn
->handshake_state
->received_certs_cell
= 1;
1884 if (connection_or_send_netinfo(chan
->conn
) < 0) {
1885 log_warn(LD_OR
, "Couldn't send netinfo cell");
1886 connection_or_close_for_error(chan
->conn
, 0);
1892 tor_cert_free(id_cert
);
1893 tor_cert_free(link_cert
);
1894 tor_cert_free(auth_cert
);
1899 * Process an AUTH_CHALLENGE cell from a channel_tls_t
1901 * This function is called to handle an incoming AUTH_CHALLENGE cell on a
1902 * channel_tls_t; if we weren't supposed to get one (for example, because we're
1903 * not the originator of the channel), or it's ill-formed, or we aren't doing
1904 * a v3 handshake, mark the channel. If the cell is well-formed but we don't
1905 * want to authenticate, just drop it. If the cell is well-formed *and* we
1906 * want to authenticate, send an AUTHENTICATE cell and then a NETINFO cell.
1910 channel_tls_process_auth_challenge_cell(var_cell_t
*cell
, channel_tls_t
*chan
)
1912 int n_types
, i
, use_type
= -1;
1917 tor_assert(chan
->conn
);
1921 log_fn(LOG_PROTOCOL_WARN, LD_PROTOCOL, \
1922 "Received a bad AUTH_CHALLENGE cell from %s:%d: %s", \
1923 safe_str(chan->conn->base_.address), \
1924 chan->conn->base_.port, (s)); \
1925 connection_or_close_for_error(chan->conn, 0); \
1929 if (chan
->conn
->base_
.state
!= OR_CONN_STATE_OR_HANDSHAKING_V3
)
1930 ERR("We're not currently doing a v3 handshake");
1931 if (chan
->conn
->link_proto
< 3)
1932 ERR("We're not using link protocol >= 3");
1933 if (!(chan
->conn
->handshake_state
->started_here
))
1934 ERR("We didn't originate this connection");
1935 if (chan
->conn
->handshake_state
->received_auth_challenge
)
1936 ERR("We already received one");
1937 if (!(chan
->conn
->handshake_state
->received_certs_cell
))
1938 ERR("We haven't gotten a CERTS cell yet");
1939 if (cell
->payload_len
< OR_AUTH_CHALLENGE_LEN
+ 2)
1940 ERR("It was too short");
1942 ERR("It had a nonzero circuit ID");
1944 n_types
= ntohs(get_uint16(cell
->payload
+ OR_AUTH_CHALLENGE_LEN
));
1945 if (cell
->payload_len
< OR_AUTH_CHALLENGE_LEN
+ 2 + 2*n_types
)
1946 ERR("It looks truncated");
1948 /* Now see if there is an authentication type we can use */
1949 cp
= cell
->payload
+OR_AUTH_CHALLENGE_LEN
+ 2;
1950 for (i
= 0; i
< n_types
; ++i
, cp
+= 2) {
1951 uint16_t authtype
= ntohs(get_uint16(cp
));
1952 if (authtype
== AUTHTYPE_RSA_SHA256_TLSSECRET
)
1953 use_type
= authtype
;
1956 chan
->conn
->handshake_state
->received_auth_challenge
= 1;
1958 if (! public_server_mode(get_options())) {
1959 /* If we're not a public server then we don't want to authenticate on a
1960 connection we originated, and we already sent a NETINFO cell when we
1961 got the CERTS cell. We have nothing more to do. */
1965 if (use_type
>= 0) {
1967 "Got an AUTH_CHALLENGE cell from %s:%d: Sending "
1969 safe_str(chan
->conn
->base_
.address
),
1970 chan
->conn
->base_
.port
);
1972 if (connection_or_send_authenticate_cell(chan
->conn
, use_type
) < 0) {
1974 "Couldn't send authenticate cell");
1975 connection_or_close_for_error(chan
->conn
, 0);
1980 "Got an AUTH_CHALLENGE cell from %s:%d, but we don't "
1981 "know any of its authentication types. Not authenticating.",
1982 safe_str(chan
->conn
->base_
.address
),
1983 chan
->conn
->base_
.port
);
1986 if (connection_or_send_netinfo(chan
->conn
) < 0) {
1987 log_warn(LD_OR
, "Couldn't send netinfo cell");
1988 connection_or_close_for_error(chan
->conn
, 0);
1996 * Process an AUTHENTICATE cell from a channel_tls_t
1998 * If it's ill-formed or we weren't supposed to get one or we're not doing a
1999 * v3 handshake, then mark the connection. If it does not authenticate the
2000 * other side of the connection successfully (because it isn't signed right,
2001 * we didn't get a CERTS cell, etc) mark the connection. Otherwise, accept
2002 * the identity of the router on the other side of the connection.
2006 channel_tls_process_authenticate_cell(var_cell_t
*cell
, channel_tls_t
*chan
)
2008 uint8_t expected
[V3_AUTH_FIXED_PART_LEN
];
2009 const uint8_t *auth
;
2014 tor_assert(chan
->conn
);
2018 log_fn(LOG_PROTOCOL_WARN, LD_PROTOCOL, \
2019 "Received a bad AUTHENTICATE cell from %s:%d: %s", \
2020 safe_str(chan->conn->base_.address), \
2021 chan->conn->base_.port, (s)); \
2022 connection_or_close_for_error(chan->conn, 0); \
2026 if (chan
->conn
->base_
.state
!= OR_CONN_STATE_OR_HANDSHAKING_V3
)
2027 ERR("We're not doing a v3 handshake");
2028 if (chan
->conn
->link_proto
< 3)
2029 ERR("We're not using link protocol >= 3");
2030 if (chan
->conn
->handshake_state
->started_here
)
2031 ERR("We originated this connection");
2032 if (chan
->conn
->handshake_state
->received_authenticate
)
2033 ERR("We already got one!");
2034 if (chan
->conn
->handshake_state
->authenticated
) {
2035 /* Should be impossible given other checks */
2036 ERR("The peer is already authenticated");
2038 if (!(chan
->conn
->handshake_state
->received_certs_cell
))
2039 ERR("We never got a certs cell");
2040 if (chan
->conn
->handshake_state
->auth_cert
== NULL
)
2041 ERR("We never got an authentication certificate");
2042 if (chan
->conn
->handshake_state
->id_cert
== NULL
)
2043 ERR("We never got an identity certificate");
2044 if (cell
->payload_len
< 4)
2045 ERR("Cell was way too short");
2047 auth
= cell
->payload
;
2049 uint16_t type
= ntohs(get_uint16(auth
));
2050 uint16_t len
= ntohs(get_uint16(auth
+2));
2051 if (4 + len
> cell
->payload_len
)
2052 ERR("Authenticator was truncated");
2054 if (type
!= AUTHTYPE_RSA_SHA256_TLSSECRET
)
2055 ERR("Authenticator type was not recognized");
2061 if (authlen
< V3_AUTH_BODY_LEN
+ 1)
2062 ERR("Authenticator was too short");
2064 if (connection_or_compute_authenticate_cell_body(
2065 chan
->conn
, expected
, sizeof(expected
), NULL
, 1) < 0)
2066 ERR("Couldn't compute expected AUTHENTICATE cell body");
2068 if (tor_memneq(expected
, auth
, sizeof(expected
)))
2069 ERR("Some field in the AUTHENTICATE cell body was not as expected");
2072 crypto_pk_t
*pk
= tor_tls_cert_get_key(
2073 chan
->conn
->handshake_state
->auth_cert
);
2074 char d
[DIGEST256_LEN
];
2080 ERR("Internal error: couldn't get RSA key from AUTH cert.");
2081 crypto_digest256(d
, (char*)auth
, V3_AUTH_BODY_LEN
, DIGEST_SHA256
);
2083 keysize
= crypto_pk_keysize(pk
);
2084 signed_data
= tor_malloc(keysize
);
2085 signed_len
= crypto_pk_public_checksig(pk
, signed_data
, keysize
,
2086 (char*)auth
+ V3_AUTH_BODY_LEN
,
2087 authlen
- V3_AUTH_BODY_LEN
);
2089 if (signed_len
< 0) {
2090 tor_free(signed_data
);
2091 ERR("Signature wasn't valid");
2093 if (signed_len
< DIGEST256_LEN
) {
2094 tor_free(signed_data
);
2095 ERR("Not enough data was signed");
2097 /* Note that we deliberately allow *more* than DIGEST256_LEN bytes here,
2098 * in case they're later used to hold a SHA3 digest or something. */
2099 if (tor_memneq(signed_data
, d
, DIGEST256_LEN
)) {
2100 tor_free(signed_data
);
2101 ERR("Signature did not match data to be signed.");
2103 tor_free(signed_data
);
2106 /* Okay, we are authenticated. */
2107 chan
->conn
->handshake_state
->received_authenticate
= 1;
2108 chan
->conn
->handshake_state
->authenticated
= 1;
2109 chan
->conn
->handshake_state
->digest_received_data
= 0;
2111 crypto_pk_t
*identity_rcvd
=
2112 tor_tls_cert_get_key(chan
->conn
->handshake_state
->id_cert
);
2113 const digests_t
*id_digests
=
2114 tor_cert_get_id_digests(chan
->conn
->handshake_state
->id_cert
);
2116 /* This must exist; we checked key type when reading the cert. */
2117 tor_assert(id_digests
);
2119 memcpy(chan
->conn
->handshake_state
->authenticated_peer_id
,
2120 id_digests
->d
[DIGEST_SHA1
], DIGEST_LEN
);
2122 channel_set_circid_type(TLS_CHAN_TO_BASE(chan
), identity_rcvd
,
2123 chan
->conn
->link_proto
< MIN_LINK_PROTO_FOR_WIDE_CIRC_IDS
);
2124 crypto_pk_free(identity_rcvd
);
2126 connection_or_init_conn_from_address(chan
->conn
,
2127 &(chan
->conn
->base_
.addr
),
2128 chan
->conn
->base_
.port
,
2129 (const char*)(chan
->conn
->handshake_state
->
2130 authenticated_peer_id
),
2134 "Got an AUTHENTICATE cell from %s:%d: Looks good.",
2135 safe_str(chan
->conn
->base_
.address
),
2136 chan
->conn
->base_
.port
);