1 /* * Copyright (c) 2012-2021, The Tor Project, Inc. */
2 /* See LICENSE for licensing information */
7 * \brief A concrete subclass of channel_t using or_connection_t to transfer
8 * cells between Tor instances.
10 * This module fills in the various function pointers in channel_t, to
11 * implement the channel_tls_t channels as used in Tor today. These channels
12 * are created from channel_tls_connect() and
13 * channel_tls_handle_incoming(). Each corresponds 1:1 to or_connection_t
14 * object, as implemented in connection_or.c. These channels transmit cells
15 * to the underlying or_connection_t by calling
16 * connection_or_write_*_cell_to_buf(), and receive cells from the underlying
17 * or_connection_t when connection_or_process_cells_from_inbuf() calls
18 * channel_tls_handle_*_cell().
20 * Here we also implement the server (responder) side of the v3+ Tor link
21 * handshake, which uses CERTS and AUTHENTICATE cell to negotiate versions,
22 * exchange expected and observed IP and time information, and bootstrap a
23 * level of authentication higher than we have gotten on the raw TLS
26 * NOTE: Since there is currently only one type of channel, there are probably
27 * more than a few cases where functionality that is currently in
28 * channeltls.c, connection_or.c, and channel.c ought to be divided up
29 * differently. The right time to do this is probably whenever we introduce
30 * our next channel type.
34 * Define this so channel.h gives us things only channel_t subclasses
37 #define CHANNEL_OBJECT_PRIVATE
39 #define CHANNELTLS_PRIVATE
41 #include "core/or/or.h"
42 #include "core/or/channel.h"
43 #include "core/or/channeltls.h"
44 #include "core/or/circuitmux.h"
45 #include "core/or/circuitmux_ewma.h"
46 #include "core/or/command.h"
47 #include "core/or/dos.h"
48 #include "app/config/config.h"
49 #include "app/config/resolve_addr.h"
50 #include "core/mainloop/connection.h"
51 #include "core/or/connection_or.h"
52 #include "feature/relay/relay_handshake.h"
53 #include "feature/control/control.h"
54 #include "feature/client/entrynodes.h"
55 #include "trunnel/link_handshake.h"
56 #include "core/or/relay.h"
57 #include "feature/stats/rephist.h"
58 #include "feature/stats/geoip_stats.h"
59 #include "feature/relay/router.h"
60 #include "feature/relay/routermode.h"
61 #include "feature/nodelist/dirlist.h"
62 #include "core/or/scheduler.h"
63 #include "feature/nodelist/torcert.h"
64 #include "feature/nodelist/networkstatus.h"
65 #include "trunnel/channelpadding_negotiation.h"
66 #include "trunnel/netinfo.h"
67 #include "core/or/channelpadding.h"
68 #include "core/or/extendinfo.h"
69 #include "core/or/congestion_control_common.h"
71 #include "core/or/cell_st.h"
72 #include "core/or/cell_queue_st.h"
73 #include "core/or/or_connection_st.h"
74 #include "core/or/or_handshake_certs_st.h"
75 #include "core/or/or_handshake_state_st.h"
76 #include "feature/nodelist/routerinfo_st.h"
77 #include "core/or/var_cell_st.h"
78 #include "feature/relay/relay_find_addr.h"
80 #include "lib/tls/tortls.h"
81 #include "lib/tls/x509.h"
83 /** How many CELL_PADDING cells have we received, ever? */
84 uint64_t stats_n_padding_cells_processed
= 0;
85 /** How many CELL_VERSIONS cells have we received, ever? */
86 uint64_t stats_n_versions_cells_processed
= 0;
87 /** How many CELL_NETINFO cells have we received, ever? */
88 uint64_t stats_n_netinfo_cells_processed
= 0;
89 /** How many CELL_VPADDING cells have we received, ever? */
90 uint64_t stats_n_vpadding_cells_processed
= 0;
91 /** How many CELL_CERTS cells have we received, ever? */
92 uint64_t stats_n_certs_cells_processed
= 0;
93 /** How many CELL_AUTH_CHALLENGE cells have we received, ever? */
94 uint64_t stats_n_auth_challenge_cells_processed
= 0;
95 /** How many CELL_AUTHENTICATE cells have we received, ever? */
96 uint64_t stats_n_authenticate_cells_processed
= 0;
97 /** How many CELL_AUTHORIZE cells have we received, ever? */
98 uint64_t stats_n_authorize_cells_processed
= 0;
100 /** Active listener, if any */
101 static channel_listener_t
*channel_tls_listener
= NULL
;
103 /* channel_tls_t method declarations */
105 static void channel_tls_close_method(channel_t
*chan
);
106 static const char * channel_tls_describe_transport_method(channel_t
*chan
);
107 static void channel_tls_free_method(channel_t
*chan
);
108 static double channel_tls_get_overhead_estimate_method(channel_t
*chan
);
109 static int channel_tls_get_remote_addr_method(const channel_t
*chan
,
110 tor_addr_t
*addr_out
);
112 channel_tls_get_transport_name_method(channel_t
*chan
, char **transport_out
);
113 static const char *channel_tls_describe_peer_method(const channel_t
*chan
);
114 static int channel_tls_has_queued_writes_method(channel_t
*chan
);
115 static int channel_tls_is_canonical_method(channel_t
*chan
);
117 channel_tls_matches_extend_info_method(channel_t
*chan
,
118 extend_info_t
*extend_info
);
119 static int channel_tls_matches_target_method(channel_t
*chan
,
120 const tor_addr_t
*target
);
121 static int channel_tls_num_cells_writeable_method(channel_t
*chan
);
122 static size_t channel_tls_num_bytes_queued_method(channel_t
*chan
);
123 static int channel_tls_write_cell_method(channel_t
*chan
,
125 static int channel_tls_write_packed_cell_method(channel_t
*chan
,
126 packed_cell_t
*packed_cell
);
127 static int channel_tls_write_var_cell_method(channel_t
*chan
,
128 var_cell_t
*var_cell
);
130 /* channel_listener_tls_t method declarations */
132 static void channel_tls_listener_close_method(channel_listener_t
*chan_l
);
134 channel_tls_listener_describe_transport_method(channel_listener_t
*chan_l
);
136 /** Handle incoming cells for the handshake stuff here rather than
137 * passing them on up. */
139 static void channel_tls_process_versions_cell(var_cell_t
*cell
,
140 channel_tls_t
*tlschan
);
141 static void channel_tls_process_netinfo_cell(cell_t
*cell
,
142 channel_tls_t
*tlschan
);
143 static int command_allowed_before_handshake(uint8_t command
);
144 static int enter_v3_handshake_with_cell(var_cell_t
*cell
,
145 channel_tls_t
*tlschan
);
146 static void channel_tls_process_padding_negotiate_cell(cell_t
*cell
,
147 channel_tls_t
*chan
);
150 * Do parts of channel_tls_t initialization common to channel_tls_connect()
151 * and channel_tls_handle_incoming().
154 channel_tls_common_init(channel_tls_t
*tlschan
)
160 chan
= &(tlschan
->base_
);
162 chan
->magic
= TLS_CHAN_MAGIC
;
163 chan
->state
= CHANNEL_STATE_OPENING
;
164 chan
->close
= channel_tls_close_method
;
165 chan
->describe_transport
= channel_tls_describe_transport_method
;
166 chan
->free_fn
= channel_tls_free_method
;
167 chan
->get_overhead_estimate
= channel_tls_get_overhead_estimate_method
;
168 chan
->get_remote_addr
= channel_tls_get_remote_addr_method
;
169 chan
->describe_peer
= channel_tls_describe_peer_method
;
170 chan
->get_transport_name
= channel_tls_get_transport_name_method
;
171 chan
->has_queued_writes
= channel_tls_has_queued_writes_method
;
172 chan
->is_canonical
= channel_tls_is_canonical_method
;
173 chan
->matches_extend_info
= channel_tls_matches_extend_info_method
;
174 chan
->matches_target
= channel_tls_matches_target_method
;
175 chan
->num_bytes_queued
= channel_tls_num_bytes_queued_method
;
176 chan
->num_cells_writeable
= channel_tls_num_cells_writeable_method
;
177 chan
->write_cell
= channel_tls_write_cell_method
;
178 chan
->write_packed_cell
= channel_tls_write_packed_cell_method
;
179 chan
->write_var_cell
= channel_tls_write_var_cell_method
;
181 chan
->cmux
= circuitmux_alloc();
182 /* We only have one policy for now so always set it to EWMA. */
183 circuitmux_set_policy(chan
->cmux
, &ewma_policy
);
187 * Start a new TLS channel.
189 * Launch a new OR connection to <b>addr</b>:<b>port</b> and expect to
190 * handshake with an OR with identity digest <b>id_digest</b>, and wrap
191 * it in a channel_tls_t.
194 channel_tls_connect(const tor_addr_t
*addr
, uint16_t port
,
195 const char *id_digest
,
196 const ed25519_public_key_t
*ed_id
)
198 channel_tls_t
*tlschan
= tor_malloc_zero(sizeof(*tlschan
));
199 channel_t
*chan
= &(tlschan
->base_
);
201 channel_tls_common_init(tlschan
);
203 log_debug(LD_CHANNEL
,
204 "In channel_tls_connect() for channel %p "
205 "(global id %"PRIu64
")",
207 (chan
->global_identifier
));
209 if (is_local_to_resolve_addr(addr
)) {
210 log_debug(LD_CHANNEL
,
211 "Marking new outgoing channel %"PRIu64
" at %p as local",
212 (chan
->global_identifier
), chan
);
213 channel_mark_local(chan
);
215 log_debug(LD_CHANNEL
,
216 "Marking new outgoing channel %"PRIu64
" at %p as remote",
217 (chan
->global_identifier
), chan
);
218 channel_mark_remote(chan
);
221 channel_mark_outgoing(chan
);
223 /* Set up or_connection stuff */
224 tlschan
->conn
= connection_or_connect(addr
, port
, id_digest
, ed_id
, tlschan
);
225 /* connection_or_connect() will fill in tlschan->conn */
226 if (!(tlschan
->conn
)) {
227 chan
->reason_for_closing
= CHANNEL_CLOSE_FOR_ERROR
;
228 channel_change_state(chan
, CHANNEL_STATE_ERROR
);
232 log_debug(LD_CHANNEL
,
233 "Got orconn %p for channel with global id %"PRIu64
,
234 tlschan
->conn
, (chan
->global_identifier
));
239 circuitmux_free(chan
->cmux
);
244 /* If we got one, we should register it */
245 if (chan
) channel_register(chan
);
251 * Return the current channel_tls_t listener.
253 * Returns the current channel listener for incoming TLS connections, or
254 * NULL if none has been established
257 channel_tls_get_listener(void)
259 return channel_tls_listener
;
263 * Start a channel_tls_t listener if necessary.
265 * Return the current channel_tls_t listener, or start one if we haven't yet,
269 channel_tls_start_listener(void)
271 channel_listener_t
*listener
;
273 if (!channel_tls_listener
) {
274 listener
= tor_malloc_zero(sizeof(*listener
));
275 channel_init_listener(listener
);
276 listener
->state
= CHANNEL_LISTENER_STATE_LISTENING
;
277 listener
->close
= channel_tls_listener_close_method
;
278 listener
->describe_transport
=
279 channel_tls_listener_describe_transport_method
;
281 channel_tls_listener
= listener
;
283 log_debug(LD_CHANNEL
,
284 "Starting TLS channel listener %p with global id %"PRIu64
,
285 listener
, (listener
->global_identifier
));
287 channel_listener_register(listener
);
288 } else listener
= channel_tls_listener
;
294 * Free everything on shutdown.
296 * Not much to do here, since channel_free_all() takes care of a lot, but let's
297 * get rid of the listener.
300 channel_tls_free_all(void)
302 channel_listener_t
*old_listener
= NULL
;
304 log_debug(LD_CHANNEL
,
305 "Shutting down TLS channels...");
307 if (channel_tls_listener
) {
309 * When we close it, channel_tls_listener will get nulled out, so save
310 * a pointer so we can free it.
312 old_listener
= channel_tls_listener
;
313 log_debug(LD_CHANNEL
,
314 "Closing channel_tls_listener with ID %"PRIu64
316 (old_listener
->global_identifier
),
318 channel_listener_unregister(old_listener
);
319 channel_listener_mark_for_close(old_listener
);
320 channel_listener_free(old_listener
);
321 tor_assert(channel_tls_listener
== NULL
);
324 log_debug(LD_CHANNEL
,
325 "Done shutting down TLS channels");
329 * Create a new channel around an incoming or_connection_t.
332 channel_tls_handle_incoming(or_connection_t
*orconn
)
334 channel_tls_t
*tlschan
= tor_malloc_zero(sizeof(*tlschan
));
335 channel_t
*chan
= &(tlschan
->base_
);
338 tor_assert(!(orconn
->chan
));
340 channel_tls_common_init(tlschan
);
342 /* Link the channel and orconn to each other */
343 tlschan
->conn
= orconn
;
344 orconn
->chan
= tlschan
;
346 if (is_local_to_resolve_addr(&(TO_CONN(orconn
)->addr
))) {
347 log_debug(LD_CHANNEL
,
348 "Marking new incoming channel %"PRIu64
" at %p as local",
349 (chan
->global_identifier
), chan
);
350 channel_mark_local(chan
);
352 log_debug(LD_CHANNEL
,
353 "Marking new incoming channel %"PRIu64
" at %p as remote",
354 (chan
->global_identifier
), chan
);
355 channel_mark_remote(chan
);
358 channel_mark_incoming(chan
);
361 channel_register(chan
);
363 char *transport_name
= NULL
;
364 if (channel_tls_get_transport_name_method(TLS_CHAN_TO_BASE(orconn
->chan
),
365 &transport_name
) < 0) {
366 transport_name
= NULL
;
368 /* Start tracking TLS connections in the DoS subsystem as soon as possible,
369 * so we can protect against attacks that use partially open connections.
371 geoip_note_client_seen(GEOIP_CLIENT_CONNECT
,
372 &TO_CONN(orconn
)->addr
, transport_name
,
374 dos_new_client_conn(orconn
, transport_name
);
375 tor_free(transport_name
);
381 * Set the `potentially_used_for_bootstrapping` flag on the or_connection_t
382 * corresponding to the provided channel.
384 * This flag indicates that if the connection fails, it might be interesting
385 * to the bootstrapping subsystem. (The bootstrapping system only cares about
386 * channels that we have tried to use for our own circuits. Other channels
387 * may have been launched in response to EXTEND cells from somebody else, and
388 * if they fail, it won't necessarily indicate a bootstrapping problem.)
391 channel_mark_as_used_for_origin_circuit(channel_t
*chan
)
395 if (chan
->magic
!= TLS_CHAN_MAGIC
)
397 channel_tls_t
*tlschan
= channel_tls_from_base(chan
);
402 tlschan
->conn
->potentially_used_for_bootstrapping
= 1;
410 * Cast a channel_tls_t to a channel_t.
413 channel_tls_to_base(channel_tls_t
*tlschan
)
415 if (!tlschan
) return NULL
;
417 return &(tlschan
->base_
);
421 * Cast a channel_t to a channel_tls_t, with appropriate type-checking
425 channel_tls_from_base(channel_t
*chan
)
427 if (!chan
) return NULL
;
429 tor_assert(chan
->magic
== TLS_CHAN_MAGIC
);
431 return (channel_tls_t
*)(chan
);
435 * Cast a const channel_tls_t to a const channel_t.
438 channel_tls_to_base_const(const channel_tls_t
*tlschan
)
440 return channel_tls_to_base((channel_tls_t
*) tlschan
);
444 * Cast a const channel_t to a const channel_tls_t, with appropriate
445 * type-checking asserts.
447 const channel_tls_t
*
448 channel_tls_from_base_const(const channel_t
*chan
)
450 return channel_tls_from_base((channel_t
*)chan
);
453 /********************************************
454 * Method implementations for channel_tls_t *
455 *******************************************/
458 * Close a channel_tls_t.
460 * This implements the close method for channel_tls_t.
463 channel_tls_close_method(channel_t
*chan
)
465 channel_tls_t
*tlschan
= BASE_CHAN_TO_TLS(chan
);
469 if (tlschan
->conn
) connection_or_close_normally(tlschan
->conn
, 1);
471 /* Weird - we'll have to change the state ourselves, I guess */
473 "Tried to close channel_tls_t %p with NULL conn",
475 channel_change_state(chan
, CHANNEL_STATE_ERROR
);
480 * Describe the transport for a channel_tls_t.
482 * This returns the string "TLS channel on connection <id>" to the upper
486 channel_tls_describe_transport_method(channel_t
*chan
)
488 static char *buf
= NULL
;
490 channel_tls_t
*tlschan
;
491 const char *rv
= NULL
;
495 tlschan
= BASE_CHAN_TO_TLS(chan
);
498 id
= TO_CONN(tlschan
->conn
)->global_identifier
;
500 if (buf
) tor_free(buf
);
502 "TLS channel (connection %"PRIu64
")",
507 rv
= "TLS channel (no connection)";
514 * Free a channel_tls_t.
516 * This is called by the generic channel layer when freeing a channel_tls_t;
517 * this happens either on a channel which has already reached
518 * CHANNEL_STATE_CLOSED or CHANNEL_STATE_ERROR from channel_run_cleanup() or
519 * on shutdown from channel_free_all(). In the latter case we might still
520 * have an orconn active (which connection_free_all() will get to later),
521 * so we should null out its channel pointer now.
524 channel_tls_free_method(channel_t
*chan
)
526 channel_tls_t
*tlschan
= BASE_CHAN_TO_TLS(chan
);
531 tlschan
->conn
->chan
= NULL
;
532 tlschan
->conn
= NULL
;
537 * Get an estimate of the average TLS overhead for the upper layer.
540 channel_tls_get_overhead_estimate_method(channel_t
*chan
)
542 double overhead
= 1.0;
543 channel_tls_t
*tlschan
= BASE_CHAN_TO_TLS(chan
);
546 tor_assert(tlschan
->conn
);
548 /* Just return 1.0f if we don't have sensible data */
549 if (tlschan
->conn
->bytes_xmitted
> 0 &&
550 tlschan
->conn
->bytes_xmitted_by_tls
>=
551 tlschan
->conn
->bytes_xmitted
) {
552 overhead
= ((double)(tlschan
->conn
->bytes_xmitted_by_tls
)) /
553 ((double)(tlschan
->conn
->bytes_xmitted
));
556 * Never estimate more than 2.0; otherwise we get silly large estimates
557 * at the very start of a new TLS connection.
563 log_debug(LD_CHANNEL
,
564 "Estimated overhead ratio for TLS chan %"PRIu64
" is %f",
565 (chan
->global_identifier
), overhead
);
571 * Get the remote address of a channel_tls_t.
573 * This implements the get_remote_addr method for channel_tls_t; copy the
574 * remote endpoint of the channel to addr_out and return 1. (Always
575 * succeeds if this channel is attached to an OR connection.)
577 * Always returns the real address of the peer, not the canonical address.
580 channel_tls_get_remote_addr_method(const channel_t
*chan
,
581 tor_addr_t
*addr_out
)
583 const channel_tls_t
*tlschan
= CONST_BASE_CHAN_TO_TLS(chan
);
586 tor_assert(addr_out
);
588 if (tlschan
->conn
== NULL
) {
589 tor_addr_make_unspec(addr_out
);
593 /* They want the real address, so give it to them. */
594 tor_addr_copy(addr_out
, &TO_CONN(tlschan
->conn
)->addr
);
600 * Get the name of the pluggable transport used by a channel_tls_t.
602 * This implements the get_transport_name for channel_tls_t. If the
603 * channel uses a pluggable transport, copy its name to
604 * <b>transport_out</b> and return 0. If the channel did not use a
605 * pluggable transport, return -1.
608 channel_tls_get_transport_name_method(channel_t
*chan
, char **transport_out
)
610 channel_tls_t
*tlschan
= BASE_CHAN_TO_TLS(chan
);
613 tor_assert(transport_out
);
614 tor_assert(tlschan
->conn
);
616 if (!tlschan
->conn
->ext_or_transport
)
619 *transport_out
= tor_strdup(tlschan
->conn
->ext_or_transport
);
624 * Get a human-readable endpoint description of a channel_tls_t.
626 * This format is intended for logging, and may change in the future;
627 * nothing should parse or rely on its particular details.
630 channel_tls_describe_peer_method(const channel_t
*chan
)
632 const channel_tls_t
*tlschan
= CONST_BASE_CHAN_TO_TLS(chan
);
636 return connection_describe_peer(TO_CONN(tlschan
->conn
));
638 return "(No connection)";
643 * Tell the upper layer if we have queued writes.
645 * This implements the has_queued_writes method for channel_tls t_; it returns
646 * 1 iff we have queued writes on the outbuf of the underlying or_connection_t.
649 channel_tls_has_queued_writes_method(channel_t
*chan
)
652 channel_tls_t
*tlschan
= BASE_CHAN_TO_TLS(chan
);
655 if (!(tlschan
->conn
)) {
657 "something called has_queued_writes on a tlschan "
658 "(%p with ID %"PRIu64
" but no conn",
659 chan
, (chan
->global_identifier
));
662 outbuf_len
= (tlschan
->conn
!= NULL
) ?
663 connection_get_outbuf_len(TO_CONN(tlschan
->conn
)) :
666 return (outbuf_len
> 0);
670 * Tell the upper layer if we're canonical.
672 * This implements the is_canonical method for channel_tls_t:
673 * it returns whether this is a canonical channel.
676 channel_tls_is_canonical_method(channel_t
*chan
)
679 channel_tls_t
*tlschan
= BASE_CHAN_TO_TLS(chan
);
684 /* If this bit is set to 0, and link_proto is sufficiently old, then we
685 * can't actually _rely_ on this being a non-canonical channel.
686 * Nonetheless, we're going to believe that this is a non-canonical
687 * channel in this case, since nobody should be using these link protocols
689 answer
= tlschan
->conn
->is_canonical
;
696 * Check if we match an extend_info_t.
698 * This implements the matches_extend_info method for channel_tls_t; the upper
699 * layer wants to know if this channel matches an extend_info_t.
701 * NOTE that this function only checks for an address/port match, and should
702 * be used only when no identify is available.
705 channel_tls_matches_extend_info_method(channel_t
*chan
,
706 extend_info_t
*extend_info
)
708 channel_tls_t
*tlschan
= BASE_CHAN_TO_TLS(chan
);
711 tor_assert(extend_info
);
713 /* Never match if we have no conn */
714 if (!(tlschan
->conn
)) {
716 "something called matches_extend_info on a tlschan "
717 "(%p with ID %"PRIu64
" but no conn",
718 chan
, (chan
->global_identifier
));
722 const tor_addr_port_t
*orport
= &tlschan
->conn
->canonical_orport
;
723 // If the canonical address is set, then we'll allow matches based on that.
724 if (! tor_addr_is_unspec(&orport
->addr
)) {
725 if (extend_info_has_orport(extend_info
, &orport
->addr
, orport
->port
)) {
730 // We also want to match if the true address and port are listed in the
732 return extend_info_has_orport(extend_info
,
733 &TO_CONN(tlschan
->conn
)->addr
,
734 TO_CONN(tlschan
->conn
)->port
);
738 * Check if we match a target address; return true iff we do.
740 * This implements the matches_target method for channel_tls t_; the upper
741 * layer wants to know if this channel matches a target address when extending
745 channel_tls_matches_target_method(channel_t
*chan
,
746 const tor_addr_t
*target
)
748 channel_tls_t
*tlschan
= BASE_CHAN_TO_TLS(chan
);
753 /* Never match if we have no conn */
754 if (!(tlschan
->conn
)) {
756 "something called matches_target on a tlschan "
757 "(%p with ID %"PRIu64
" but no conn",
758 chan
, (chan
->global_identifier
));
762 /* addr is the address this connection came from.
763 * canonical_orport is updated by connection_or_init_conn_from_address()
764 * to be the address in the descriptor. It may be tempting to
765 * allow either address to be allowed, but if we did so, it would
766 * enable someone who steals a relay's keys to covertly impersonate/MITM it
767 * from anywhere on the Internet! (Because they could make long-lived
768 * TLS connections from anywhere to all relays, and wait for them to
769 * be used for extends).
771 * An adversary who has stolen a relay's keys could also post a fake relay
772 * descriptor, but that attack is easier to detect.
774 return tor_addr_eq(&TO_CONN(tlschan
->conn
)->addr
, target
);
778 * Tell the upper layer how many bytes we have queued and not yet
782 channel_tls_num_bytes_queued_method(channel_t
*chan
)
784 channel_tls_t
*tlschan
= BASE_CHAN_TO_TLS(chan
);
787 tor_assert(tlschan
->conn
);
789 return connection_get_outbuf_len(TO_CONN(tlschan
->conn
));
793 * Tell the upper layer how many cells we can accept to write.
795 * This implements the num_cells_writeable method for channel_tls_t; it
796 * returns an estimate of the number of cells we can accept with
797 * channel_tls_write_*_cell().
800 channel_tls_num_cells_writeable_method(channel_t
*chan
)
804 channel_tls_t
*tlschan
= BASE_CHAN_TO_TLS(chan
);
805 size_t cell_network_size
;
808 tor_assert(tlschan
->conn
);
810 cell_network_size
= get_cell_network_size(tlschan
->conn
->wide_circ_ids
);
811 outbuf_len
= connection_get_outbuf_len(TO_CONN(tlschan
->conn
));
812 /* Get the number of cells */
813 n
= CEIL_DIV(or_conn_highwatermark() - outbuf_len
, cell_network_size
);
815 #if SIZEOF_SIZE_T > SIZEOF_INT
816 if (n
> INT_MAX
) n
= INT_MAX
;
823 * Write a cell to a channel_tls_t.
825 * This implements the write_cell method for channel_tls_t; given a
826 * channel_tls_t and a cell_t, transmit the cell_t.
829 channel_tls_write_cell_method(channel_t
*chan
, cell_t
*cell
)
831 channel_tls_t
*tlschan
= BASE_CHAN_TO_TLS(chan
);
838 connection_or_write_cell_to_buf(cell
, tlschan
->conn
);
842 "something called write_cell on a tlschan "
843 "(%p with ID %"PRIu64
" but no conn",
844 chan
, (chan
->global_identifier
));
851 * Write a packed cell to a channel_tls_t.
853 * This implements the write_packed_cell method for channel_tls_t; given a
854 * channel_tls_t and a packed_cell_t, transmit the packed_cell_t.
856 * Return 0 on success or negative value on error. The caller must free the
860 channel_tls_write_packed_cell_method(channel_t
*chan
,
861 packed_cell_t
*packed_cell
)
864 channel_tls_t
*tlschan
= BASE_CHAN_TO_TLS(chan
);
865 size_t cell_network_size
= get_cell_network_size(chan
->wide_circ_ids
);
868 tor_assert(packed_cell
);
871 connection_buf_add(packed_cell
->body
, cell_network_size
,
872 TO_CONN(tlschan
->conn
));
875 "something called write_packed_cell on a tlschan "
876 "(%p with ID %"PRIu64
" but no conn",
877 chan
, (chan
->global_identifier
));
885 * Write a variable-length cell to a channel_tls_t.
887 * This implements the write_var_cell method for channel_tls_t; given a
888 * channel_tls_t and a var_cell_t, transmit the var_cell_t.
891 channel_tls_write_var_cell_method(channel_t
*chan
, var_cell_t
*var_cell
)
893 channel_tls_t
*tlschan
= BASE_CHAN_TO_TLS(chan
);
897 tor_assert(var_cell
);
900 connection_or_write_var_cell_to_buf(var_cell
, tlschan
->conn
);
904 "something called write_var_cell on a tlschan "
905 "(%p with ID %"PRIu64
" but no conn",
906 chan
, (chan
->global_identifier
));
912 /*************************************************
913 * Method implementations for channel_listener_t *
914 ************************************************/
917 * Close a channel_listener_t.
919 * This implements the close method for channel_listener_t.
922 channel_tls_listener_close_method(channel_listener_t
*chan_l
)
927 * Listeners we just go ahead and change state through to CLOSED, but
928 * make sure to check if they're channel_tls_listener to NULL it out.
930 if (chan_l
== channel_tls_listener
)
931 channel_tls_listener
= NULL
;
933 if (!(chan_l
->state
== CHANNEL_LISTENER_STATE_CLOSING
||
934 chan_l
->state
== CHANNEL_LISTENER_STATE_CLOSED
||
935 chan_l
->state
== CHANNEL_LISTENER_STATE_ERROR
)) {
936 channel_listener_change_state(chan_l
, CHANNEL_LISTENER_STATE_CLOSING
);
939 if (chan_l
->incoming_list
) {
940 SMARTLIST_FOREACH_BEGIN(chan_l
->incoming_list
,
941 channel_t
*, ichan
) {
942 channel_mark_for_close(ichan
);
943 } SMARTLIST_FOREACH_END(ichan
);
945 smartlist_free(chan_l
->incoming_list
);
946 chan_l
->incoming_list
= NULL
;
949 if (!(chan_l
->state
== CHANNEL_LISTENER_STATE_CLOSED
||
950 chan_l
->state
== CHANNEL_LISTENER_STATE_ERROR
)) {
951 channel_listener_change_state(chan_l
, CHANNEL_LISTENER_STATE_CLOSED
);
956 * Describe the transport for a channel_listener_t.
958 * This returns the string "TLS channel (listening)" to the upper
962 channel_tls_listener_describe_transport_method(channel_listener_t
*chan_l
)
966 return "TLS channel (listening)";
969 /*******************************************************
970 * Functions for handling events on an or_connection_t *
971 ******************************************************/
974 * Handle an orconn state change.
976 * This function will be called by connection_or.c when the or_connection_t
977 * associated with this channel_tls_t changes state.
980 channel_tls_handle_state_change_on_orconn(channel_tls_t
*chan
,
981 or_connection_t
*conn
,
984 channel_t
*base_chan
;
988 tor_assert(conn
->chan
== chan
);
989 tor_assert(chan
->conn
== conn
);
991 base_chan
= TLS_CHAN_TO_BASE(chan
);
993 /* Make sure the base connection state makes sense - shouldn't be error
996 tor_assert(CHANNEL_IS_OPENING(base_chan
) ||
997 CHANNEL_IS_OPEN(base_chan
) ||
998 CHANNEL_IS_MAINT(base_chan
) ||
999 CHANNEL_IS_CLOSING(base_chan
));
1001 /* Did we just go to state open? */
1002 if (state
== OR_CONN_STATE_OPEN
) {
1004 * We can go to CHANNEL_STATE_OPEN from CHANNEL_STATE_OPENING or
1005 * CHANNEL_STATE_MAINT on this.
1007 channel_change_state_open(base_chan
);
1008 /* We might have just become writeable; check and tell the scheduler */
1009 if (connection_or_num_cells_writeable(conn
) > 0) {
1010 scheduler_channel_wants_writes(base_chan
);
1014 * Not open, so from CHANNEL_STATE_OPEN we go to CHANNEL_STATE_MAINT,
1015 * otherwise no change.
1017 if (CHANNEL_IS_OPEN(base_chan
)) {
1018 channel_change_state(base_chan
, CHANNEL_STATE_MAINT
);
1023 #ifdef KEEP_TIMING_STATS
1026 * Timing states wrapper.
1028 * This is a wrapper function around the actual function that processes the
1029 * <b>cell</b> that just arrived on <b>chan</b>. Increment <b>*time</b>
1030 * by the number of microseconds used by the call to <b>*func(cell, chan)</b>.
1033 channel_tls_time_process_cell(cell_t
*cell
, channel_tls_t
*chan
, int *time
,
1034 void (*func
)(cell_t
*, channel_tls_t
*))
1036 struct timeval start
, end
;
1039 tor_gettimeofday(&start
);
1041 (*func
)(cell
, chan
);
1043 tor_gettimeofday(&end
);
1044 time_passed
= tv_udiff(&start
, &end
) ;
1046 if (time_passed
> 10000) { /* more than 10ms */
1047 log_debug(LD_OR
,"That call just took %ld ms.",time_passed
/1000);
1050 if (time_passed
< 0) {
1051 log_info(LD_GENERAL
,"That call took us back in time!");
1055 *time
+= time_passed
;
1057 #endif /* defined(KEEP_TIMING_STATS) */
1059 #ifdef KEEP_TIMING_STATS
1060 #define PROCESS_CELL(tp, cl, cn) STMT_BEGIN { \
1062 channel_tls_time_process_cell(cl, cn, & tp ## time , \
1063 channel_tls_process_ ## tp ## _cell); \
1065 #else /* !defined(KEEP_TIMING_STATS) */
1066 #define PROCESS_CELL(tp, cl, cn) channel_tls_process_ ## tp ## _cell(cl, cn)
1067 #endif /* defined(KEEP_TIMING_STATS) */
1070 * Handle an incoming cell on a channel_tls_t.
1072 * This is called from connection_or.c to handle an arriving cell; it checks
1073 * for cell types specific to the handshake for this transport protocol and
1074 * handles them, and queues all other cells to the channel_t layer, which
1075 * eventually will hand them off to command.c.
1077 * The channel layer itself decides whether the cell should be queued or
1078 * can be handed off immediately to the upper-layer code. It is responsible
1079 * for copying in the case that it queues; we merely pass pointers through
1080 * which we get from connection_or_process_cells_from_inbuf().
1083 channel_tls_handle_cell(cell_t
*cell
, or_connection_t
*conn
)
1085 channel_tls_t
*chan
;
1094 log_warn(LD_CHANNEL
,
1095 "Got a cell_t on an OR connection with no channel");
1099 handshaking
= (TO_CONN(conn
)->state
!= OR_CONN_STATE_OPEN
);
1101 if (conn
->base_
.marked_for_close
)
1104 /* Reject all but VERSIONS and NETINFO when handshaking. */
1105 /* (VERSIONS actually indicates a protocol warning: it's variable-length,
1106 * so if it reaches this function, we're on a v1 connection.) */
1107 if (handshaking
&& cell
->command
!= CELL_VERSIONS
&&
1108 cell
->command
!= CELL_NETINFO
) {
1109 log_fn(LOG_PROTOCOL_WARN
, LD_PROTOCOL
,
1110 "Received unexpected cell command %d in chan state %s / "
1111 "conn state %s; closing the connection.",
1113 channel_state_to_string(TLS_CHAN_TO_BASE(chan
)->state
),
1114 conn_state_to_string(CONN_TYPE_OR
, TO_CONN(conn
)->state
));
1115 connection_or_close_for_error(conn
, 0);
1119 if (conn
->base_
.state
== OR_CONN_STATE_OR_HANDSHAKING_V3
)
1120 or_handshake_state_record_cell(conn
, conn
->handshake_state
, cell
, 1);
1122 /* We note that we're on the internet whenever we read a cell. This is
1123 * a fast operation. */
1124 entry_guards_note_internet_connectivity(get_guard_selection_info());
1125 rep_hist_padding_count_read(PADDING_TYPE_TOTAL
);
1127 if (TLS_CHAN_TO_BASE(chan
)->padding_enabled
)
1128 rep_hist_padding_count_read(PADDING_TYPE_ENABLED_TOTAL
);
1130 switch (cell
->command
) {
1132 rep_hist_padding_count_read(PADDING_TYPE_CELL
);
1133 if (TLS_CHAN_TO_BASE(chan
)->padding_enabled
)
1134 rep_hist_padding_count_read(PADDING_TYPE_ENABLED_CELL
);
1135 ++stats_n_padding_cells_processed
;
1139 /* A VERSIONS cell should always be a variable-length cell, and
1140 * so should never reach this function (which handles constant-sized
1141 * cells). But if the connection is using the (obsolete) v1 link
1142 * protocol, all cells will be treated as constant-sized, and so
1143 * it's possible we'll reach this code.
1145 log_fn(LOG_PROTOCOL_WARN
, LD_CHANNEL
,
1146 "Received unexpected VERSIONS cell on a channel using link "
1147 "protocol %d; ignoring.", conn
->link_proto
);
1150 ++stats_n_netinfo_cells_processed
;
1151 PROCESS_CELL(netinfo
, cell
, chan
);
1153 case CELL_PADDING_NEGOTIATE
:
1154 ++stats_n_netinfo_cells_processed
;
1155 PROCESS_CELL(padding_negotiate
, cell
, chan
);
1158 case CELL_CREATE_FAST
:
1160 case CELL_CREATED_FAST
:
1162 case CELL_RELAY_EARLY
:
1167 * These are all transport independent and we pass them up through the
1168 * channel_t mechanism. They are ultimately handled in command.c.
1170 channel_process_cell(TLS_CHAN_TO_BASE(chan
), cell
);
1173 log_fn(LOG_INFO
, LD_PROTOCOL
,
1174 "Cell of unknown type (%d) received in channeltls.c. "
1182 * Handle an incoming variable-length cell on a channel_tls_t.
1184 * Process a <b>var_cell</b> that was just received on <b>conn</b>. Keep
1185 * internal statistics about how many of each cell we've processed so far
1186 * this second, and the total number of microseconds it took to
1187 * process each type of cell. All the var_cell commands are handshake-
1188 * related and live below the channel_t layer, so no variable-length
1189 * cells ever get delivered in the current implementation, but I've left
1190 * the mechanism in place for future use.
1192 * If we were handing them off to the upper layer, the channel_t queueing
1193 * code would be responsible for memory management, and we'd just be passing
1194 * pointers through from connection_or_process_cells_from_inbuf(). That
1195 * caller always frees them after this function returns, so this function
1196 * should never free var_cell.
1199 channel_tls_handle_var_cell(var_cell_t
*var_cell
, or_connection_t
*conn
)
1201 channel_tls_t
*chan
;
1203 #ifdef KEEP_TIMING_STATS
1204 /* how many of each cell have we seen so far this second? needs better
1206 static int num_versions
= 0, num_certs
= 0;
1207 static time_t current_second
= 0; /* from previous calls to time */
1208 time_t now
= time(NULL
);
1210 if (current_second
== 0) current_second
= now
;
1211 if (now
> current_second
) { /* the second has rolled over */
1214 "At end of second: %d versions (%d ms), %d certs (%d ms)",
1215 num_versions
, versions_time
/ ((now
- current_second
) * 1000),
1216 num_certs
, certs_time
/ ((now
- current_second
) * 1000));
1218 num_versions
= num_certs
= 0;
1219 versions_time
= certs_time
= 0;
1221 /* remember which second it is, for next time */
1222 current_second
= now
;
1224 #endif /* defined(KEEP_TIMING_STATS) */
1226 tor_assert(var_cell
);
1232 log_warn(LD_CHANNEL
,
1233 "Got a var_cell_t on an OR connection with no channel");
1237 if (TO_CONN(conn
)->marked_for_close
)
1240 switch (TO_CONN(conn
)->state
) {
1241 case OR_CONN_STATE_OR_HANDSHAKING_V2
:
1242 if (var_cell
->command
!= CELL_VERSIONS
) {
1243 log_fn(LOG_PROTOCOL_WARN
, LD_PROTOCOL
,
1244 "Received a cell with command %d in unexpected "
1245 "orconn state \"%s\" [%d], channel state \"%s\" [%d]; "
1246 "closing the connection.",
1247 (int)(var_cell
->command
),
1248 conn_state_to_string(CONN_TYPE_OR
, TO_CONN(conn
)->state
),
1249 TO_CONN(conn
)->state
,
1250 channel_state_to_string(TLS_CHAN_TO_BASE(chan
)->state
),
1251 (int)(TLS_CHAN_TO_BASE(chan
)->state
));
1253 * The code in connection_or.c will tell channel_t to close for
1254 * error; it will go to CHANNEL_STATE_CLOSING, and then to
1255 * CHANNEL_STATE_ERROR when conn is closed.
1257 connection_or_close_for_error(conn
, 0);
1261 case OR_CONN_STATE_TLS_HANDSHAKING
:
1262 /* If we're using bufferevents, it's entirely possible for us to
1263 * notice "hey, data arrived!" before we notice "hey, the handshake
1264 * finished!" And we need to be accepting both at once to handle both
1265 * the v2 and v3 handshakes. */
1266 /* But that should be happening any longer've disabled bufferevents. */
1267 tor_assert_nonfatal_unreached_once();
1268 FALLTHROUGH_UNLESS_ALL_BUGS_ARE_FATAL
;
1269 case OR_CONN_STATE_TLS_SERVER_RENEGOTIATING
:
1270 if (!(command_allowed_before_handshake(var_cell
->command
))) {
1271 log_fn(LOG_PROTOCOL_WARN
, LD_PROTOCOL
,
1272 "Received a cell with command %d in unexpected "
1273 "orconn state \"%s\" [%d], channel state \"%s\" [%d]; "
1274 "closing the connection.",
1275 (int)(var_cell
->command
),
1276 conn_state_to_string(CONN_TYPE_OR
, TO_CONN(conn
)->state
),
1277 (int)(TO_CONN(conn
)->state
),
1278 channel_state_to_string(TLS_CHAN_TO_BASE(chan
)->state
),
1279 (int)(TLS_CHAN_TO_BASE(chan
)->state
));
1280 /* see above comment about CHANNEL_STATE_ERROR */
1281 connection_or_close_for_error(conn
, 0);
1284 if (enter_v3_handshake_with_cell(var_cell
, chan
) < 0)
1288 case OR_CONN_STATE_OR_HANDSHAKING_V3
:
1289 if (var_cell
->command
!= CELL_AUTHENTICATE
)
1290 or_handshake_state_record_var_cell(conn
, conn
->handshake_state
,
1292 break; /* Everything is allowed */
1293 case OR_CONN_STATE_OPEN
:
1294 if (conn
->link_proto
< 3) {
1295 log_fn(LOG_PROTOCOL_WARN
, LD_PROTOCOL
,
1296 "Received a variable-length cell with command %d in orconn "
1297 "state %s [%d], channel state %s [%d] with link protocol %d; "
1299 (int)(var_cell
->command
),
1300 conn_state_to_string(CONN_TYPE_OR
, TO_CONN(conn
)->state
),
1301 (int)(TO_CONN(conn
)->state
),
1302 channel_state_to_string(TLS_CHAN_TO_BASE(chan
)->state
),
1303 (int)(TLS_CHAN_TO_BASE(chan
)->state
),
1304 (int)(conn
->link_proto
));
1309 log_fn(LOG_PROTOCOL_WARN
, LD_PROTOCOL
,
1310 "Received var-length cell with command %d in unexpected "
1311 "orconn state \"%s\" [%d], channel state \"%s\" [%d]; "
1313 (int)(var_cell
->command
),
1314 conn_state_to_string(CONN_TYPE_OR
, TO_CONN(conn
)->state
),
1315 (int)(TO_CONN(conn
)->state
),
1316 channel_state_to_string(TLS_CHAN_TO_BASE(chan
)->state
),
1317 (int)(TLS_CHAN_TO_BASE(chan
)->state
));
1321 /* We note that we're on the internet whenever we read a cell. This is
1322 * a fast operation. */
1323 entry_guards_note_internet_connectivity(get_guard_selection_info());
1325 /* Now handle the cell */
1327 switch (var_cell
->command
) {
1329 ++stats_n_versions_cells_processed
;
1330 PROCESS_CELL(versions
, var_cell
, chan
);
1333 ++stats_n_vpadding_cells_processed
;
1337 ++stats_n_certs_cells_processed
;
1338 PROCESS_CELL(certs
, var_cell
, chan
);
1340 case CELL_AUTH_CHALLENGE
:
1341 ++stats_n_auth_challenge_cells_processed
;
1342 PROCESS_CELL(auth_challenge
, var_cell
, chan
);
1344 case CELL_AUTHENTICATE
:
1345 ++stats_n_authenticate_cells_processed
;
1346 PROCESS_CELL(authenticate
, var_cell
, chan
);
1348 case CELL_AUTHORIZE
:
1349 ++stats_n_authorize_cells_processed
;
1350 /* Ignored so far. */
1353 log_fn(LOG_INFO
, LD_PROTOCOL
,
1354 "Variable-length cell of unknown type (%d) received.",
1355 (int)(var_cell
->command
));
1363 * Update channel marks after connection_or.c has changed an address.
1365 * This is called from connection_or_init_conn_from_address() after the
1366 * connection's _base.addr or real_addr fields have potentially been changed
1367 * so we can recalculate the local mark. Notably, this happens when incoming
1368 * connections are reverse-proxied and we only learn the real address of the
1369 * remote router by looking it up in the consensus after we finish the
1370 * handshake and know an authenticated identity digest.
1373 channel_tls_update_marks(or_connection_t
*conn
)
1375 channel_t
*chan
= NULL
;
1378 tor_assert(conn
->chan
);
1380 chan
= TLS_CHAN_TO_BASE(conn
->chan
);
1382 if (is_local_to_resolve_addr(&(TO_CONN(conn
)->addr
))) {
1383 if (!channel_is_local(chan
)) {
1384 log_debug(LD_CHANNEL
,
1385 "Marking channel %"PRIu64
" at %p as local",
1386 (chan
->global_identifier
), chan
);
1387 channel_mark_local(chan
);
1390 if (channel_is_local(chan
)) {
1391 log_debug(LD_CHANNEL
,
1392 "Marking channel %"PRIu64
" at %p as remote",
1393 (chan
->global_identifier
), chan
);
1394 channel_mark_remote(chan
);
1400 * Check if this cell type is allowed before the handshake is finished.
1402 * Return true if <b>command</b> is a cell command that's allowed to start a
1406 command_allowed_before_handshake(uint8_t command
)
1411 case CELL_AUTHORIZE
:
1419 * Start a V3 handshake on an incoming connection.
1421 * Called when we as a server receive an appropriate cell while waiting
1422 * either for a cell or a TLS handshake. Set the connection's state to
1423 * "handshaking_v3', initializes the or_handshake_state field as needed,
1424 * and add the cell to the hash of incoming cells.)
1427 enter_v3_handshake_with_cell(var_cell_t
*cell
, channel_tls_t
*chan
)
1429 int started_here
= 0;
1433 tor_assert(chan
->conn
);
1435 started_here
= connection_or_nonopen_was_started_here(chan
->conn
);
1437 tor_assert(TO_CONN(chan
->conn
)->state
== OR_CONN_STATE_TLS_HANDSHAKING
||
1438 TO_CONN(chan
->conn
)->state
==
1439 OR_CONN_STATE_TLS_SERVER_RENEGOTIATING
);
1442 log_fn(LOG_PROTOCOL_WARN
, LD_OR
,
1443 "Received a cell while TLS-handshaking, not in "
1444 "OR_HANDSHAKING_V3, on a connection we originated.");
1446 connection_or_block_renegotiation(chan
->conn
);
1447 connection_or_change_state(chan
->conn
, OR_CONN_STATE_OR_HANDSHAKING_V3
);
1448 if (connection_init_or_handshake_state(chan
->conn
, started_here
) < 0) {
1449 connection_or_close_for_error(chan
->conn
, 0);
1452 or_handshake_state_record_var_cell(chan
->conn
,
1453 chan
->conn
->handshake_state
, cell
, 1);
1458 * Process a 'versions' cell.
1460 * This function is called to handle an incoming VERSIONS cell; the current
1461 * link protocol version must be 0 to indicate that no version has yet been
1462 * negotiated. We compare the versions in the cell to the list of versions
1463 * we support, pick the highest version we have in common, and continue the
1464 * negotiation from there.
1467 channel_tls_process_versions_cell(var_cell_t
*cell
, channel_tls_t
*chan
)
1469 int highest_supported_version
= 0;
1470 int started_here
= 0;
1474 tor_assert(chan
->conn
);
1476 if ((cell
->payload_len
% 2) == 1) {
1477 log_fn(LOG_PROTOCOL_WARN
, LD_OR
,
1478 "Received a VERSION cell with odd payload length %d; "
1479 "closing connection.",cell
->payload_len
);
1480 connection_or_close_for_error(chan
->conn
, 0);
1484 started_here
= connection_or_nonopen_was_started_here(chan
->conn
);
1486 if (chan
->conn
->link_proto
!= 0 ||
1487 (chan
->conn
->handshake_state
&&
1488 chan
->conn
->handshake_state
->received_versions
)) {
1489 log_fn(LOG_PROTOCOL_WARN
, LD_OR
,
1490 "Received a VERSIONS cell on a connection with its version "
1491 "already set to %d; dropping",
1492 (int)(chan
->conn
->link_proto
));
1495 switch (chan
->conn
->base_
.state
)
1497 case OR_CONN_STATE_OR_HANDSHAKING_V2
:
1498 case OR_CONN_STATE_OR_HANDSHAKING_V3
:
1500 case OR_CONN_STATE_TLS_HANDSHAKING
:
1501 case OR_CONN_STATE_TLS_SERVER_RENEGOTIATING
:
1503 log_fn(LOG_PROTOCOL_WARN
, LD_OR
,
1504 "VERSIONS cell while in unexpected state");
1508 tor_assert(chan
->conn
->handshake_state
);
1512 const uint8_t *cp
= cell
->payload
;
1513 for (i
= 0; i
< cell
->payload_len
/ 2; ++i
, cp
+= 2) {
1514 uint16_t v
= ntohs(get_uint16(cp
));
1515 if (is_or_protocol_version_known(v
) && v
> highest_supported_version
)
1516 highest_supported_version
= v
;
1519 if (!highest_supported_version
) {
1520 log_fn(LOG_PROTOCOL_WARN
, LD_OR
,
1521 "Couldn't find a version in common between my version list and the "
1522 "list in the VERSIONS cell; closing connection.");
1523 connection_or_close_for_error(chan
->conn
, 0);
1525 } else if (highest_supported_version
== 1) {
1526 /* Negotiating version 1 makes no sense, since version 1 has no VERSIONS
1528 log_fn(LOG_PROTOCOL_WARN
, LD_OR
,
1529 "Used version negotiation protocol to negotiate a v1 connection. "
1530 "That's crazily non-compliant. Closing connection.");
1531 connection_or_close_for_error(chan
->conn
, 0);
1533 } else if (highest_supported_version
< 3 &&
1534 chan
->conn
->base_
.state
== OR_CONN_STATE_OR_HANDSHAKING_V3
) {
1535 log_fn(LOG_PROTOCOL_WARN
, LD_OR
,
1536 "Negotiated link protocol 2 or lower after doing a v3 TLS "
1537 "handshake. Closing connection.");
1538 connection_or_close_for_error(chan
->conn
, 0);
1540 } else if (highest_supported_version
!= 2 &&
1541 chan
->conn
->base_
.state
== OR_CONN_STATE_OR_HANDSHAKING_V2
) {
1542 /* XXXX This should eventually be a log_protocol_warn */
1543 log_fn(LOG_WARN
, LD_OR
,
1544 "Negotiated link with non-2 protocol after doing a v2 TLS "
1545 "handshake with %s. Closing connection.",
1546 connection_describe_peer(TO_CONN(chan
->conn
)));
1547 connection_or_close_for_error(chan
->conn
, 0);
1551 rep_hist_note_negotiated_link_proto(highest_supported_version
, started_here
);
1553 chan
->conn
->link_proto
= highest_supported_version
;
1554 chan
->conn
->handshake_state
->received_versions
= 1;
1556 if (chan
->conn
->link_proto
== 2) {
1558 "Negotiated version %d on %s; sending NETINFO.",
1559 highest_supported_version
,
1560 connection_describe(TO_CONN(chan
->conn
)));
1562 if (connection_or_send_netinfo(chan
->conn
) < 0) {
1563 connection_or_close_for_error(chan
->conn
, 0);
1567 const int send_versions
= !started_here
;
1568 /* If we want to authenticate, send a CERTS cell */
1569 const int send_certs
= !started_here
|| public_server_mode(get_options());
1570 /* If we're a host that got a connection, ask for authentication. */
1571 const int send_chall
= !started_here
;
1572 /* If our certs cell will authenticate us, we can send a netinfo cell
1574 const int send_netinfo
= !started_here
;
1575 const int send_any
=
1576 send_versions
|| send_certs
|| send_chall
|| send_netinfo
;
1577 tor_assert(chan
->conn
->link_proto
>= 3);
1580 "Negotiated version %d with on %s; %s%s%s%s%s",
1581 highest_supported_version
,
1582 connection_describe(TO_CONN(chan
->conn
)),
1583 send_any
? "Sending cells:" : "Waiting for CERTS cell",
1584 send_versions
? " VERSIONS" : "",
1585 send_certs
? " CERTS" : "",
1586 send_chall
? " AUTH_CHALLENGE" : "",
1587 send_netinfo
? " NETINFO" : "");
1589 #ifdef DISABLE_V3_LINKPROTO_SERVERSIDE
1591 connection_or_close_normally(chan
->conn
, 1);
1594 #endif /* defined(DISABLE_V3_LINKPROTO_SERVERSIDE) */
1596 if (send_versions
) {
1597 if (connection_or_send_versions(chan
->conn
, 1) < 0) {
1598 log_warn(LD_OR
, "Couldn't send versions cell");
1599 connection_or_close_for_error(chan
->conn
, 0);
1604 /* We set this after sending the versions cell. */
1605 /*XXXXX symbolic const.*/
1606 TLS_CHAN_TO_BASE(chan
)->wide_circ_ids
=
1607 chan
->conn
->link_proto
>= MIN_LINK_PROTO_FOR_WIDE_CIRC_IDS
;
1608 chan
->conn
->wide_circ_ids
= TLS_CHAN_TO_BASE(chan
)->wide_circ_ids
;
1610 TLS_CHAN_TO_BASE(chan
)->padding_enabled
=
1611 chan
->conn
->link_proto
>= MIN_LINK_PROTO_FOR_CHANNEL_PADDING
;
1614 if (connection_or_send_certs_cell(chan
->conn
) < 0) {
1615 log_warn(LD_OR
, "Couldn't send certs cell");
1616 connection_or_close_for_error(chan
->conn
, 0);
1621 if (connection_or_send_auth_challenge_cell(chan
->conn
) < 0) {
1622 log_warn(LD_OR
, "Couldn't send auth_challenge cell");
1623 connection_or_close_for_error(chan
->conn
, 0);
1628 if (connection_or_send_netinfo(chan
->conn
) < 0) {
1629 log_warn(LD_OR
, "Couldn't send netinfo cell");
1630 connection_or_close_for_error(chan
->conn
, 0);
1638 * Process a 'padding_negotiate' cell.
1640 * This function is called to handle an incoming PADDING_NEGOTIATE cell;
1641 * enable or disable padding accordingly, and read and act on its timeout
1645 channel_tls_process_padding_negotiate_cell(cell_t
*cell
, channel_tls_t
*chan
)
1647 channelpadding_negotiate_t
*negotiation
;
1650 tor_assert(chan
->conn
);
1652 if (chan
->conn
->link_proto
< MIN_LINK_PROTO_FOR_CHANNEL_PADDING
) {
1653 log_fn(LOG_PROTOCOL_WARN
, LD_OR
,
1654 "Received a PADDING_NEGOTIATE cell on v%d connection; dropping.",
1655 chan
->conn
->link_proto
);
1659 if (channelpadding_negotiate_parse(&negotiation
, cell
->payload
,
1660 CELL_PAYLOAD_SIZE
) < 0) {
1661 log_fn(LOG_PROTOCOL_WARN
, LD_OR
,
1662 "Received malformed PADDING_NEGOTIATE cell on v%d connection; "
1663 "dropping.", chan
->conn
->link_proto
);
1668 channelpadding_update_padding_for_channel(TLS_CHAN_TO_BASE(chan
),
1671 channelpadding_negotiate_free(negotiation
);
1675 * Convert <b>netinfo_addr</b> into corresponding <b>tor_addr</b>.
1676 * Return 0 on success; on failure, return -1 and log a warning.
1679 tor_addr_from_netinfo_addr(tor_addr_t
*tor_addr
,
1680 const netinfo_addr_t
*netinfo_addr
) {
1681 tor_assert(tor_addr
);
1682 tor_assert(netinfo_addr
);
1684 uint8_t type
= netinfo_addr_get_addr_type(netinfo_addr
);
1685 uint8_t len
= netinfo_addr_get_len(netinfo_addr
);
1687 if (type
== NETINFO_ADDR_TYPE_IPV4
&& len
== 4) {
1688 uint32_t ipv4
= netinfo_addr_get_addr_ipv4(netinfo_addr
);
1689 tor_addr_from_ipv4h(tor_addr
, ipv4
);
1690 } else if (type
== NETINFO_ADDR_TYPE_IPV6
&& len
== 16) {
1691 const uint8_t *ipv6_bytes
= netinfo_addr_getconstarray_addr_ipv6(
1693 tor_addr_from_ipv6_bytes(tor_addr
, ipv6_bytes
);
1695 log_fn(LOG_PROTOCOL_WARN
, LD_OR
, "Cannot read address from NETINFO "
1696 "- wrong type/length.");
1704 * Helper: compute the absolute value of a time_t.
1706 * (we need this because labs() doesn't always work for time_t, since
1707 * long can be shorter than time_t.)
1709 static inline time_t
1710 time_abs(time_t val
)
1712 return (val
< 0) ? -val
: val
;
1715 /** Return true iff the channel can process a NETINFO cell. For this to return
1716 * true, these channel conditions apply:
1718 * 1. Link protocol is version 2 or higher (tor-spec.txt, NETINFO cells
1721 * 2. Underlying OR connection of the channel is either in v2 or v3
1722 * handshaking state.
1725 can_process_netinfo_cell(const channel_tls_t
*chan
)
1727 /* NETINFO cells can only be negotiated on link protocol 2 or higher. */
1728 if (chan
->conn
->link_proto
< 2) {
1729 log_fn(LOG_PROTOCOL_WARN
, LD_OR
,
1730 "Received a NETINFO cell on %s connection; dropping.",
1731 chan
->conn
->link_proto
== 0 ? "non-versioned" : "a v1");
1735 /* Can't process a NETINFO cell if the connection is not handshaking. */
1736 if (chan
->conn
->base_
.state
!= OR_CONN_STATE_OR_HANDSHAKING_V2
&&
1737 chan
->conn
->base_
.state
!= OR_CONN_STATE_OR_HANDSHAKING_V3
) {
1738 log_fn(LOG_PROTOCOL_WARN
, LD_OR
,
1739 "Received a NETINFO cell on non-handshaking connection; dropping.");
1743 /* Make sure we do have handshake state. */
1744 tor_assert(chan
->conn
->handshake_state
);
1745 tor_assert(chan
->conn
->handshake_state
->received_versions
);
1750 /** Mark the given channel endpoint as a client (which means either a tor
1751 * client or a tor bridge).
1753 * This MUST be done on an _unauthenticated_ channel. It is a mistake to mark
1754 * an authenticated channel as a client.
1756 * The following is done on the channel:
1758 * 1. Marked as a client.
1759 * 2. Type of circuit ID type is set.
1760 * 3. The underlying OR connection is initialized with the address of the
1764 mark_channel_tls_endpoint_as_client(channel_tls_t
*chan
)
1766 /* Ending up here for an authenticated link is a mistake. */
1767 if (BUG(chan
->conn
->handshake_state
->authenticated
)) {
1771 tor_assert(tor_digest_is_zero(
1772 (const char*)(chan
->conn
->handshake_state
->
1773 authenticated_rsa_peer_id
)));
1774 tor_assert(fast_mem_is_zero(
1775 (const char*)(chan
->conn
->handshake_state
->
1776 authenticated_ed25519_peer_id
.pubkey
), 32));
1777 /* If the client never authenticated, it's a tor client or bridge
1778 * relay, and we must not use it for EXTEND requests (nor could we, as
1779 * there are no authenticated peer IDs) */
1780 channel_mark_client(TLS_CHAN_TO_BASE(chan
));
1781 channel_set_circid_type(TLS_CHAN_TO_BASE(chan
), NULL
,
1782 chan
->conn
->link_proto
< MIN_LINK_PROTO_FOR_WIDE_CIRC_IDS
);
1784 connection_or_init_conn_from_address(chan
->conn
,
1785 &(chan
->conn
->base_
.addr
),
1786 chan
->conn
->base_
.port
,
1787 /* zero, checked above */
1788 (const char*)(chan
->conn
->handshake_state
->
1789 authenticated_rsa_peer_id
),
1790 NULL
, /* Ed25519 ID: Also checked as zero */
1795 * Process a 'netinfo' cell
1797 * This function is called to handle an incoming NETINFO cell; read and act
1798 * on its contents, and set the connection state to "open".
1801 channel_tls_process_netinfo_cell(cell_t
*cell
, channel_tls_t
*chan
)
1804 uint8_t my_addr_type
;
1805 uint8_t my_addr_len
;
1806 uint8_t n_other_addrs
;
1807 time_t now
= time(NULL
);
1808 const routerinfo_t
*me
= router_get_my_routerinfo();
1810 time_t apparent_skew
= 0;
1811 tor_addr_t my_apparent_addr
= TOR_ADDR_NULL
;
1812 int started_here
= 0;
1813 const char *identity_digest
= NULL
;
1817 tor_assert(chan
->conn
);
1819 /* Make sure we can process a NETINFO cell. Link protocol and state
1820 * validation is done to make sure of it. */
1821 if (!can_process_netinfo_cell(chan
)) {
1825 started_here
= connection_or_nonopen_was_started_here(chan
->conn
);
1826 identity_digest
= chan
->conn
->identity_digest
;
1828 if (chan
->conn
->base_
.state
== OR_CONN_STATE_OR_HANDSHAKING_V3
) {
1829 tor_assert(chan
->conn
->link_proto
>= 3);
1831 if (!(chan
->conn
->handshake_state
->authenticated
)) {
1832 log_fn(LOG_PROTOCOL_WARN
, LD_OR
,
1833 "Got a NETINFO cell from server, "
1834 "but no authentication. Closing the connection.");
1835 connection_or_close_for_error(chan
->conn
, 0);
1839 /* We're the server. If the client never authenticated, we have some
1840 * housekeeping to do.
1842 * It's a tor client or bridge relay, and we must not use it for EXTEND
1843 * requests (nor could we, as there are no authenticated peer IDs) */
1844 if (!(chan
->conn
->handshake_state
->authenticated
)) {
1845 mark_channel_tls_endpoint_as_client(chan
);
1850 /* Decode the cell. */
1851 netinfo_cell_t
*netinfo_cell
= NULL
;
1853 ssize_t parsed
= netinfo_cell_parse(&netinfo_cell
, cell
->payload
,
1857 log_fn(LOG_PROTOCOL_WARN
, LD_OR
,
1858 "Failed to parse NETINFO cell - closing connection.");
1859 connection_or_close_for_error(chan
->conn
, 0);
1863 timestamp
= netinfo_cell_get_timestamp(netinfo_cell
);
1865 const netinfo_addr_t
*my_addr
=
1866 netinfo_cell_getconst_other_addr(netinfo_cell
);
1868 my_addr_type
= netinfo_addr_get_addr_type(my_addr
);
1869 my_addr_len
= netinfo_addr_get_len(my_addr
);
1871 if ((now
- chan
->conn
->handshake_state
->sent_versions_at
) < 180) {
1872 apparent_skew
= now
- timestamp
;
1874 /* We used to check:
1875 * if (my_addr_len >= CELL_PAYLOAD_SIZE - 6) {
1877 * This is actually never going to happen, since my_addr_len is at most 255,
1878 * and CELL_PAYLOAD_LEN - 6 is 503. So we know that cp is < end. */
1880 if (tor_addr_from_netinfo_addr(&my_apparent_addr
, my_addr
) == -1) {
1881 connection_or_close_for_error(chan
->conn
, 0);
1882 netinfo_cell_free(netinfo_cell
);
1886 if (my_addr_type
== NETINFO_ADDR_TYPE_IPV4
&& my_addr_len
== 4) {
1887 if (!get_options()->BridgeRelay
&& me
&&
1888 tor_addr_eq(&my_apparent_addr
, &me
->ipv4_addr
)) {
1889 TLS_CHAN_TO_BASE(chan
)->is_canonical_to_peer
= 1;
1891 } else if (my_addr_type
== NETINFO_ADDR_TYPE_IPV6
&&
1892 my_addr_len
== 16) {
1893 if (!get_options()->BridgeRelay
&& me
&&
1894 !tor_addr_is_null(&me
->ipv6_addr
) &&
1895 tor_addr_eq(&my_apparent_addr
, &me
->ipv6_addr
)) {
1896 TLS_CHAN_TO_BASE(chan
)->is_canonical_to_peer
= 1;
1901 /* We have a descriptor, so we are a relay: record the address that the
1902 * other side said we had. */
1903 tor_addr_copy(&TLS_CHAN_TO_BASE(chan
)->addr_according_to_peer
,
1907 n_other_addrs
= netinfo_cell_get_n_my_addrs(netinfo_cell
);
1908 for (uint8_t i
= 0; i
< n_other_addrs
; i
++) {
1909 /* Consider all the other addresses; if any matches, this connection is
1912 const netinfo_addr_t
*netinfo_addr
=
1913 netinfo_cell_getconst_my_addrs(netinfo_cell
, i
);
1917 if (tor_addr_from_netinfo_addr(&addr
, netinfo_addr
) == -1) {
1918 log_fn(LOG_PROTOCOL_WARN
, LD_OR
,
1919 "Bad address in netinfo cell; Skipping.");
1922 /* A relay can connect from anywhere and be canonical, so
1923 * long as it tells you from where it came. This may sound a bit
1924 * concerning... but that's what "canonical" means: that the
1925 * address is one that the relay itself has claimed. The relay
1926 * might be doing something funny, but nobody else is doing a MITM
1927 * on the relay's TCP.
1929 if (tor_addr_eq(&addr
, &TO_CONN(chan
->conn
)->addr
)) {
1930 connection_or_set_canonical(chan
->conn
, 1);
1935 netinfo_cell_free(netinfo_cell
);
1937 if (me
&& !TLS_CHAN_TO_BASE(chan
)->is_canonical_to_peer
&&
1938 channel_is_canonical(TLS_CHAN_TO_BASE(chan
))) {
1939 const char *descr
= channel_describe_peer(
1940 TLS_CHAN_TO_BASE(chan
));
1942 "We made a connection to a relay at %s (fp=%s) but we think "
1943 "they will not consider this connection canonical. They "
1944 "think we are at %s, but we think its %s.",
1946 safe_str(hex_str(identity_digest
, DIGEST_LEN
)),
1947 safe_str(tor_addr_is_null(&my_apparent_addr
) ?
1948 "<none>" : fmt_and_decorate_addr(&my_apparent_addr
)),
1949 safe_str(fmt_addr(&me
->ipv4_addr
)));
1952 /* Act on apparent skew. */
1953 /** Warn when we get a netinfo skew with at least this value. */
1954 #define NETINFO_NOTICE_SKEW 3600
1955 if (time_abs(apparent_skew
) > NETINFO_NOTICE_SKEW
&&
1957 connection_or_digest_is_known_relay(chan
->conn
->identity_digest
))) {
1958 int trusted
= router_digest_is_trusted_dir(chan
->conn
->identity_digest
);
1959 clock_skew_warning(TO_CONN(chan
->conn
), apparent_skew
, trusted
, LD_GENERAL
,
1960 "NETINFO cell", "OR");
1963 /* Consider our apparent address as a possible suggestion for our address if
1964 * we were unable to resolve it previously. The endpoint address is passed
1965 * in order to make sure to never consider an address that is the same as
1967 relay_address_new_suggestion(&my_apparent_addr
, &TO_CONN(chan
->conn
)->addr
,
1970 if (! chan
->conn
->handshake_state
->sent_netinfo
) {
1971 /* If we were prepared to authenticate, but we never got an AUTH_CHALLENGE
1972 * cell, then we would not previously have sent a NETINFO cell. Do so
1974 if (connection_or_send_netinfo(chan
->conn
) < 0) {
1975 connection_or_close_for_error(chan
->conn
, 0);
1980 if (connection_or_set_state_open(chan
->conn
) < 0) {
1981 log_fn(LOG_PROTOCOL_WARN
, LD_OR
,
1982 "Got good NETINFO cell on %s; but "
1983 "was unable to make the OR connection become open.",
1984 connection_describe(TO_CONN(chan
->conn
)));
1985 connection_or_close_for_error(chan
->conn
, 0);
1988 "Got good NETINFO cell on %s; OR connection is now "
1989 "open, using protocol version %d. Its ID digest is %s. "
1990 "Our address is apparently %s.",
1991 connection_describe(TO_CONN(chan
->conn
)),
1992 (int)(chan
->conn
->link_proto
),
1993 hex_str(identity_digest
, DIGEST_LEN
),
1994 tor_addr_is_null(&my_apparent_addr
) ?
1996 safe_str_client(fmt_and_decorate_addr(&my_apparent_addr
)));
1998 assert_connection_ok(TO_CONN(chan
->conn
),time(NULL
));
2001 /** Types of certificates that we know how to parse from CERTS cells. Each
2002 * type corresponds to a different encoding format. */
2003 typedef enum cert_encoding_t
{
2004 CERT_ENCODING_UNKNOWN
, /**< We don't recognize this. */
2005 CERT_ENCODING_X509
, /**< It's an RSA key, signed with RSA, encoded in x509.
2006 * (Actually, it might not be RSA. We test that later.) */
2007 CERT_ENCODING_ED25519
, /**< It's something signed with an Ed25519 key,
2008 * encoded asa a tor_cert_t.*/
2009 CERT_ENCODING_RSA_CROSSCERT
, /**< It's an Ed key signed with an RSA key. */
2013 * Given one of the certificate type codes used in a CERTS cell,
2014 * return the corresponding cert_encoding_t that we should use to parse
2017 static cert_encoding_t
2018 certs_cell_typenum_to_cert_type(int typenum
)
2021 case CERTTYPE_RSA1024_ID_LINK
:
2022 case CERTTYPE_RSA1024_ID_ID
:
2023 case CERTTYPE_RSA1024_ID_AUTH
:
2024 return CERT_ENCODING_X509
;
2025 case CERTTYPE_ED_ID_SIGN
:
2026 case CERTTYPE_ED_SIGN_LINK
:
2027 case CERTTYPE_ED_SIGN_AUTH
:
2028 return CERT_ENCODING_ED25519
;
2029 case CERTTYPE_RSA1024_ID_EDID
:
2030 return CERT_ENCODING_RSA_CROSSCERT
;
2032 return CERT_ENCODING_UNKNOWN
;
2037 * Process a CERTS cell from a channel.
2039 * This function is called to process an incoming CERTS cell on a
2042 * If the other side should not have sent us a CERTS cell, or the cell is
2043 * malformed, or it is supposed to authenticate the TLS key but it doesn't,
2044 * then mark the connection.
2046 * If the cell has a good cert chain and we're doing a v3 handshake, then
2047 * store the certificates in or_handshake_state. If this is the client side
2048 * of the connection, we then authenticate the server or mark the connection.
2049 * If it's the server side, wait for an AUTHENTICATE cell.
2052 channel_tls_process_certs_cell(var_cell_t
*cell
, channel_tls_t
*chan
)
2054 #define MAX_CERT_TYPE_WANTED CERTTYPE_RSA1024_ID_EDID
2055 /* These arrays will be sparse, since a cert type can be at most one
2057 tor_x509_cert_t
*x509_certs
[MAX_CERT_TYPE_WANTED
+ 1];
2058 tor_cert_t
*ed_certs
[MAX_CERT_TYPE_WANTED
+ 1];
2059 uint8_t *rsa_ed_cc_cert
= NULL
;
2060 size_t rsa_ed_cc_cert_len
= 0;
2063 certs_cell_t
*cc
= NULL
;
2065 int send_netinfo
= 0, started_here
= 0;
2067 memset(x509_certs
, 0, sizeof(x509_certs
));
2068 memset(ed_certs
, 0, sizeof(ed_certs
));
2071 tor_assert(chan
->conn
);
2075 log_fn(LOG_PROTOCOL_WARN, LD_PROTOCOL, \
2076 "Received a bad CERTS cell on %s: %s", \
2077 connection_describe(TO_CONN(chan->conn)), \
2079 connection_or_close_for_error(chan->conn, 0); \
2083 /* Can't use connection_or_nonopen_was_started_here(); its conn->tls
2084 * check looks like it breaks
2085 * test_link_handshake_recv_certs_ok_server(). */
2086 started_here
= chan
->conn
->handshake_state
->started_here
;
2088 if (chan
->conn
->base_
.state
!= OR_CONN_STATE_OR_HANDSHAKING_V3
)
2089 ERR("We're not doing a v3 handshake!");
2090 if (chan
->conn
->link_proto
< 3)
2091 ERR("We're not using link protocol >= 3");
2092 if (chan
->conn
->handshake_state
->received_certs_cell
)
2093 ERR("We already got one");
2094 if (chan
->conn
->handshake_state
->authenticated
) {
2095 /* Should be unreachable, but let's make sure. */
2096 ERR("We're already authenticated!");
2098 if (cell
->payload_len
< 1)
2099 ERR("It had no body");
2101 ERR("It had a nonzero circuit ID");
2103 if (certs_cell_parse(&cc
, cell
->payload
, cell
->payload_len
) < 0)
2104 ERR("It couldn't be parsed.");
2106 n_certs
= cc
->n_certs
;
2108 for (i
= 0; i
< n_certs
; ++i
) {
2109 certs_cell_cert_t
*c
= certs_cell_get_certs(cc
, i
);
2111 uint16_t cert_type
= c
->cert_type
;
2112 uint16_t cert_len
= c
->cert_len
;
2113 uint8_t *cert_body
= certs_cell_cert_getarray_body(c
);
2115 if (cert_type
> MAX_CERT_TYPE_WANTED
)
2117 const cert_encoding_t ct
= certs_cell_typenum_to_cert_type(cert_type
);
2120 case CERT_ENCODING_UNKNOWN
:
2122 case CERT_ENCODING_X509
: {
2123 tor_x509_cert_t
*x509_cert
= tor_x509_cert_decode(cert_body
, cert_len
);
2125 log_fn(LOG_PROTOCOL_WARN
, LD_PROTOCOL
,
2126 "Received undecodable certificate in CERTS cell on %s",
2127 connection_describe(TO_CONN(chan
->conn
)));
2129 if (x509_certs
[cert_type
]) {
2130 tor_x509_cert_free(x509_cert
);
2131 ERR("Duplicate x509 certificate");
2133 x509_certs
[cert_type
] = x509_cert
;
2138 case CERT_ENCODING_ED25519
: {
2139 tor_cert_t
*ed_cert
= tor_cert_parse(cert_body
, cert_len
);
2141 log_fn(LOG_PROTOCOL_WARN
, LD_PROTOCOL
,
2142 "Received undecodable Ed certificate "
2143 "in CERTS cell on %s",
2144 connection_describe(TO_CONN(chan
->conn
)));
2146 if (ed_certs
[cert_type
]) {
2147 tor_cert_free(ed_cert
);
2148 ERR("Duplicate Ed25519 certificate");
2150 ed_certs
[cert_type
] = ed_cert
;
2156 case CERT_ENCODING_RSA_CROSSCERT
: {
2157 if (rsa_ed_cc_cert
) {
2158 ERR("Duplicate RSA->Ed25519 crosscert");
2160 rsa_ed_cc_cert
= tor_memdup(cert_body
, cert_len
);
2161 rsa_ed_cc_cert_len
= cert_len
;
2168 /* Move the certificates we (might) want into the handshake_state->certs
2170 tor_x509_cert_t
*id_cert
= x509_certs
[CERTTYPE_RSA1024_ID_ID
];
2171 tor_x509_cert_t
*auth_cert
= x509_certs
[CERTTYPE_RSA1024_ID_AUTH
];
2172 tor_x509_cert_t
*link_cert
= x509_certs
[CERTTYPE_RSA1024_ID_LINK
];
2173 chan
->conn
->handshake_state
->certs
->auth_cert
= auth_cert
;
2174 chan
->conn
->handshake_state
->certs
->link_cert
= link_cert
;
2175 chan
->conn
->handshake_state
->certs
->id_cert
= id_cert
;
2176 x509_certs
[CERTTYPE_RSA1024_ID_ID
] =
2177 x509_certs
[CERTTYPE_RSA1024_ID_AUTH
] =
2178 x509_certs
[CERTTYPE_RSA1024_ID_LINK
] = NULL
;
2180 tor_cert_t
*ed_id_sign
= ed_certs
[CERTTYPE_ED_ID_SIGN
];
2181 tor_cert_t
*ed_sign_link
= ed_certs
[CERTTYPE_ED_SIGN_LINK
];
2182 tor_cert_t
*ed_sign_auth
= ed_certs
[CERTTYPE_ED_SIGN_AUTH
];
2183 chan
->conn
->handshake_state
->certs
->ed_id_sign
= ed_id_sign
;
2184 chan
->conn
->handshake_state
->certs
->ed_sign_link
= ed_sign_link
;
2185 chan
->conn
->handshake_state
->certs
->ed_sign_auth
= ed_sign_auth
;
2186 ed_certs
[CERTTYPE_ED_ID_SIGN
] =
2187 ed_certs
[CERTTYPE_ED_SIGN_LINK
] =
2188 ed_certs
[CERTTYPE_ED_SIGN_AUTH
] = NULL
;
2190 chan
->conn
->handshake_state
->certs
->ed_rsa_crosscert
= rsa_ed_cc_cert
;
2191 chan
->conn
->handshake_state
->certs
->ed_rsa_crosscert_len
=
2193 rsa_ed_cc_cert
= NULL
;
2196 /* Note that this warns more loudly about time and validity if we were
2197 * _trying_ to connect to an authority, not necessarily if we _did_ connect
2200 router_digest_is_trusted_dir(TLS_CHAN_TO_BASE(chan
)->identity_digest
))
2201 severity
= LOG_WARN
;
2203 severity
= LOG_PROTOCOL_WARN
;
2205 const ed25519_public_key_t
*checked_ed_id
= NULL
;
2206 const common_digests_t
*checked_rsa_id
= NULL
;
2207 or_handshake_certs_check_both(severity
,
2208 chan
->conn
->handshake_state
->certs
,
2214 if (!checked_rsa_id
)
2215 ERR("Invalid certificate chain!");
2218 /* No more information is needed. */
2220 chan
->conn
->handshake_state
->authenticated
= 1;
2221 chan
->conn
->handshake_state
->authenticated_rsa
= 1;
2223 const common_digests_t
*id_digests
= checked_rsa_id
;
2224 crypto_pk_t
*identity_rcvd
;
2226 ERR("Couldn't compute digests for key in ID cert");
2228 identity_rcvd
= tor_tls_cert_get_key(id_cert
);
2229 if (!identity_rcvd
) {
2230 ERR("Couldn't get RSA key from ID cert.");
2232 memcpy(chan
->conn
->handshake_state
->authenticated_rsa_peer_id
,
2233 id_digests
->d
[DIGEST_SHA1
], DIGEST_LEN
);
2234 channel_set_circid_type(TLS_CHAN_TO_BASE(chan
), identity_rcvd
,
2235 chan
->conn
->link_proto
< MIN_LINK_PROTO_FOR_WIDE_CIRC_IDS
);
2236 crypto_pk_free(identity_rcvd
);
2239 if (checked_ed_id
) {
2240 chan
->conn
->handshake_state
->authenticated_ed25519
= 1;
2241 memcpy(&chan
->conn
->handshake_state
->authenticated_ed25519_peer_id
,
2242 checked_ed_id
, sizeof(ed25519_public_key_t
));
2245 log_debug(LD_HANDSHAKE
, "calling client_learned_peer_id from "
2246 "process_certs_cell");
2248 if (connection_or_client_learned_peer_id(chan
->conn
,
2249 chan
->conn
->handshake_state
->authenticated_rsa_peer_id
,
2251 ERR("Problem setting or checking peer id");
2253 log_info(LD_HANDSHAKE
,
2254 "Got some good certificates on %s: Authenticated it with "
2256 connection_describe(TO_CONN(chan
->conn
)),
2257 checked_ed_id
? " and Ed25519" : "");
2259 if (!public_server_mode(get_options())) {
2260 /* If we initiated the connection and we are not a public server, we
2261 * aren't planning to authenticate at all. At this point we know who we
2262 * are talking to, so we can just send a netinfo now. */
2266 /* We can't call it authenticated till we see an AUTHENTICATE cell. */
2268 "Got some good RSA%s certificates on %s. "
2269 "Waiting for AUTHENTICATE.",
2270 checked_ed_id
? " and Ed25519" : "",
2271 connection_describe(TO_CONN(chan
->conn
)));
2272 /* XXXX check more stuff? */
2275 chan
->conn
->handshake_state
->received_certs_cell
= 1;
2278 if (connection_or_send_netinfo(chan
->conn
) < 0) {
2279 log_warn(LD_OR
, "Couldn't send netinfo cell");
2280 connection_or_close_for_error(chan
->conn
, 0);
2286 for (unsigned u
= 0; u
< ARRAY_LENGTH(x509_certs
); ++u
) {
2287 tor_x509_cert_free(x509_certs
[u
]);
2289 for (unsigned u
= 0; u
< ARRAY_LENGTH(ed_certs
); ++u
) {
2290 tor_cert_free(ed_certs
[u
]);
2292 tor_free(rsa_ed_cc_cert
);
2293 certs_cell_free(cc
);
2298 * Process an AUTH_CHALLENGE cell from a channel_tls_t.
2300 * This function is called to handle an incoming AUTH_CHALLENGE cell on a
2301 * channel_tls_t; if we weren't supposed to get one (for example, because we're
2302 * not the originator of the channel), or it's ill-formed, or we aren't doing
2303 * a v3 handshake, mark the channel. If the cell is well-formed but we don't
2304 * want to authenticate, just drop it. If the cell is well-formed *and* we
2305 * want to authenticate, send an AUTHENTICATE cell and then a NETINFO cell.
2308 channel_tls_process_auth_challenge_cell(var_cell_t
*cell
, channel_tls_t
*chan
)
2310 int n_types
, i
, use_type
= -1;
2311 auth_challenge_cell_t
*ac
= NULL
;
2315 tor_assert(chan
->conn
);
2319 log_fn(LOG_PROTOCOL_WARN, LD_PROTOCOL, \
2320 "Received a bad AUTH_CHALLENGE cell on %s: %s", \
2321 connection_describe(TO_CONN(chan->conn)), \
2323 connection_or_close_for_error(chan->conn, 0); \
2327 if (chan
->conn
->base_
.state
!= OR_CONN_STATE_OR_HANDSHAKING_V3
)
2328 ERR("We're not currently doing a v3 handshake");
2329 if (chan
->conn
->link_proto
< 3)
2330 ERR("We're not using link protocol >= 3");
2331 if (!(chan
->conn
->handshake_state
->started_here
))
2332 ERR("We didn't originate this connection");
2333 if (chan
->conn
->handshake_state
->received_auth_challenge
)
2334 ERR("We already received one");
2335 if (!(chan
->conn
->handshake_state
->received_certs_cell
))
2336 ERR("We haven't gotten a CERTS cell yet");
2338 ERR("It had a nonzero circuit ID");
2340 if (auth_challenge_cell_parse(&ac
, cell
->payload
, cell
->payload_len
) < 0)
2341 ERR("It was not well-formed.");
2343 n_types
= ac
->n_methods
;
2345 /* Now see if there is an authentication type we can use */
2346 for (i
= 0; i
< n_types
; ++i
) {
2347 uint16_t authtype
= auth_challenge_cell_get_methods(ac
, i
);
2348 if (authchallenge_type_is_supported(authtype
)) {
2349 if (use_type
== -1 ||
2350 authchallenge_type_is_better(authtype
, use_type
)) {
2351 use_type
= authtype
;
2356 chan
->conn
->handshake_state
->received_auth_challenge
= 1;
2358 if (! public_server_mode(get_options())) {
2359 /* If we're not a public server then we don't want to authenticate on a
2360 connection we originated, and we already sent a NETINFO cell when we
2361 got the CERTS cell. We have nothing more to do. */
2365 if (use_type
>= 0) {
2367 "Got an AUTH_CHALLENGE cell on %s: Sending "
2368 "authentication type %d",
2369 connection_describe(TO_CONN(chan
->conn
)),
2372 if (connection_or_send_authenticate_cell(chan
->conn
, use_type
) < 0) {
2374 "Couldn't send authenticate cell");
2375 connection_or_close_for_error(chan
->conn
, 0);
2380 "Got an AUTH_CHALLENGE cell on %s, but we don't "
2381 "know any of its authentication types. Not authenticating.",
2382 connection_describe(TO_CONN(chan
->conn
)));
2385 if (connection_or_send_netinfo(chan
->conn
) < 0) {
2386 log_warn(LD_OR
, "Couldn't send netinfo cell");
2387 connection_or_close_for_error(chan
->conn
, 0);
2392 auth_challenge_cell_free(ac
);
2398 * Process an AUTHENTICATE cell from a channel_tls_t.
2400 * If it's ill-formed or we weren't supposed to get one or we're not doing a
2401 * v3 handshake, then mark the connection. If it does not authenticate the
2402 * other side of the connection successfully (because it isn't signed right,
2403 * we didn't get a CERTS cell, etc) mark the connection. Otherwise, accept
2404 * the identity of the router on the other side of the connection.
2407 channel_tls_process_authenticate_cell(var_cell_t
*cell
, channel_tls_t
*chan
)
2409 var_cell_t
*expected_cell
= NULL
;
2410 const uint8_t *auth
;
2417 tor_assert(chan
->conn
);
2421 log_fn(LOG_PROTOCOL_WARN, LD_PROTOCOL, \
2422 "Received a bad AUTHENTICATE cell on %s: %s", \
2423 connection_describe(TO_CONN(chan->conn)), \
2425 connection_or_close_for_error(chan->conn, 0); \
2426 var_cell_free(expected_cell); \
2430 if (chan
->conn
->base_
.state
!= OR_CONN_STATE_OR_HANDSHAKING_V3
)
2431 ERR("We're not doing a v3 handshake");
2432 if (chan
->conn
->link_proto
< 3)
2433 ERR("We're not using link protocol >= 3");
2434 if (chan
->conn
->handshake_state
->started_here
)
2435 ERR("We originated this connection");
2436 if (chan
->conn
->handshake_state
->received_authenticate
)
2437 ERR("We already got one!");
2438 if (chan
->conn
->handshake_state
->authenticated
) {
2439 /* Should be impossible given other checks */
2440 ERR("The peer is already authenticated");
2442 if (!(chan
->conn
->handshake_state
->received_certs_cell
))
2443 ERR("We never got a certs cell");
2444 if (chan
->conn
->handshake_state
->certs
->id_cert
== NULL
)
2445 ERR("We never got an identity certificate");
2446 if (cell
->payload_len
< 4)
2447 ERR("Cell was way too short");
2449 auth
= cell
->payload
;
2451 uint16_t type
= ntohs(get_uint16(auth
));
2452 uint16_t len
= ntohs(get_uint16(auth
+2));
2453 if (4 + len
> cell
->payload_len
)
2454 ERR("Authenticator was truncated");
2456 if (! authchallenge_type_is_supported(type
))
2457 ERR("Authenticator type was not recognized");
2464 if (authlen
< V3_AUTH_BODY_LEN
+ 1)
2465 ERR("Authenticator was too short");
2467 expected_cell
= connection_or_compute_authenticate_cell_body(
2468 chan
->conn
, authtype
, NULL
, NULL
, 1);
2469 if (! expected_cell
)
2470 ERR("Couldn't compute expected AUTHENTICATE cell body");
2473 if (authtype
== AUTHTYPE_RSA_SHA256_TLSSECRET
||
2474 authtype
== AUTHTYPE_RSA_SHA256_RFC5705
) {
2475 bodylen
= V3_AUTH_BODY_LEN
;
2478 tor_assert(authtype
== AUTHTYPE_ED25519_SHA256_RFC5705
);
2479 /* Our earlier check had better have made sure we had room
2480 * for an ed25519 sig (inadvertently) */
2481 tor_assert(V3_AUTH_BODY_LEN
> ED25519_SIG_LEN
);
2482 bodylen
= authlen
- ED25519_SIG_LEN
;
2485 if (expected_cell
->payload_len
!= bodylen
+4) {
2486 ERR("Expected AUTHENTICATE cell body len not as expected.");
2489 /* Length of random part. */
2490 if (BUG(bodylen
< 24)) {
2492 ERR("Bodylen is somehow less than 24, which should really be impossible");
2496 if (tor_memneq(expected_cell
->payload
+4, auth
, bodylen
-24))
2497 ERR("Some field in the AUTHENTICATE cell body was not as expected");
2500 if (chan
->conn
->handshake_state
->certs
->ed_id_sign
!= NULL
)
2501 ERR("RSA-signed AUTHENTICATE response provided with an ED25519 cert");
2503 if (chan
->conn
->handshake_state
->certs
->auth_cert
== NULL
)
2504 ERR("We never got an RSA authentication certificate");
2506 crypto_pk_t
*pk
= tor_tls_cert_get_key(
2507 chan
->conn
->handshake_state
->certs
->auth_cert
);
2508 char d
[DIGEST256_LEN
];
2514 ERR("Couldn't get RSA key from AUTH cert.");
2516 crypto_digest256(d
, (char*)auth
, V3_AUTH_BODY_LEN
, DIGEST_SHA256
);
2518 keysize
= crypto_pk_keysize(pk
);
2519 signed_data
= tor_malloc(keysize
);
2520 signed_len
= crypto_pk_public_checksig(pk
, signed_data
, keysize
,
2521 (char*)auth
+ V3_AUTH_BODY_LEN
,
2522 authlen
- V3_AUTH_BODY_LEN
);
2524 if (signed_len
< 0) {
2525 tor_free(signed_data
);
2526 ERR("RSA signature wasn't valid");
2528 if (signed_len
< DIGEST256_LEN
) {
2529 tor_free(signed_data
);
2530 ERR("Not enough data was signed");
2532 /* Note that we deliberately allow *more* than DIGEST256_LEN bytes here,
2533 * in case they're later used to hold a SHA3 digest or something. */
2534 if (tor_memneq(signed_data
, d
, DIGEST256_LEN
)) {
2535 tor_free(signed_data
);
2536 ERR("Signature did not match data to be signed.");
2538 tor_free(signed_data
);
2540 if (chan
->conn
->handshake_state
->certs
->ed_id_sign
== NULL
)
2541 ERR("We never got an Ed25519 identity certificate.");
2542 if (chan
->conn
->handshake_state
->certs
->ed_sign_auth
== NULL
)
2543 ERR("We never got an Ed25519 authentication certificate.");
2545 const ed25519_public_key_t
*authkey
=
2546 &chan
->conn
->handshake_state
->certs
->ed_sign_auth
->signed_key
;
2547 ed25519_signature_t sig
;
2548 tor_assert(authlen
> ED25519_SIG_LEN
);
2549 memcpy(&sig
.sig
, auth
+ authlen
- ED25519_SIG_LEN
, ED25519_SIG_LEN
);
2550 if (ed25519_checksig(&sig
, auth
, authlen
- ED25519_SIG_LEN
, authkey
)<0) {
2551 ERR("Ed25519 signature wasn't valid.");
2555 /* Okay, we are authenticated. */
2556 chan
->conn
->handshake_state
->received_authenticate
= 1;
2557 chan
->conn
->handshake_state
->authenticated
= 1;
2558 chan
->conn
->handshake_state
->authenticated_rsa
= 1;
2559 chan
->conn
->handshake_state
->digest_received_data
= 0;
2561 tor_x509_cert_t
*id_cert
= chan
->conn
->handshake_state
->certs
->id_cert
;
2562 crypto_pk_t
*identity_rcvd
= tor_tls_cert_get_key(id_cert
);
2563 const common_digests_t
*id_digests
= tor_x509_cert_get_id_digests(id_cert
);
2564 const ed25519_public_key_t
*ed_identity_received
= NULL
;
2567 chan
->conn
->handshake_state
->authenticated_ed25519
= 1;
2568 ed_identity_received
=
2569 &chan
->conn
->handshake_state
->certs
->ed_id_sign
->signing_key
;
2570 memcpy(&chan
->conn
->handshake_state
->authenticated_ed25519_peer_id
,
2571 ed_identity_received
, sizeof(ed25519_public_key_t
));
2574 /* This must exist; we checked key type when reading the cert. */
2575 tor_assert(id_digests
);
2577 memcpy(chan
->conn
->handshake_state
->authenticated_rsa_peer_id
,
2578 id_digests
->d
[DIGEST_SHA1
], DIGEST_LEN
);
2580 channel_set_circid_type(TLS_CHAN_TO_BASE(chan
), identity_rcvd
,
2581 chan
->conn
->link_proto
< MIN_LINK_PROTO_FOR_WIDE_CIRC_IDS
);
2582 crypto_pk_free(identity_rcvd
);
2584 log_debug(LD_HANDSHAKE
,
2585 "Calling connection_or_init_conn_from_address on %s "
2586 " from %s, with%s ed25519 id.",
2587 connection_describe(TO_CONN(chan
->conn
)),
2589 ed_identity_received
? "" : "out");
2591 connection_or_init_conn_from_address(chan
->conn
,
2592 &(chan
->conn
->base_
.addr
),
2593 chan
->conn
->base_
.port
,
2594 (const char*)(chan
->conn
->handshake_state
->
2595 authenticated_rsa_peer_id
),
2596 ed_identity_received
,
2599 log_debug(LD_HANDSHAKE
,
2600 "Got an AUTHENTICATE cell on %s, type %d: Looks good.",
2601 connection_describe(TO_CONN(chan
->conn
)),
2605 var_cell_free(expected_cell
);