Tor 0.4.9.13
Loading...
Searching...
No Matches
channeltls.c
Go to the documentation of this file.
1/* * Copyright (c) 2012-2021, The Tor Project, Inc. */
2/* See LICENSE for licensing information */
3
4/**
5 * \file channeltls.c
6 *
7 * \brief A concrete subclass of channel_t using or_connection_t to transfer
8 * cells between Tor instances.
9 *
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().
19 *
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
24 * handshake.
25 *
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.
31 **/
32
33/*
34 * Define this so channel.h gives us things only channel_t subclasses
35 * should touch.
36 */
37#define CHANNEL_OBJECT_PRIVATE
38
39#define CHANNELTLS_PRIVATE
40
41#include "core/or/or.h"
42#include "core/or/channel.h"
43#include "core/or/channeltls.h"
44#include "core/or/circuitmux.h"
46#include "core/or/command.h"
47#include "core/or/dos.h"
48#include "app/config/config.h"
55#include "trunnel/link_handshake.h"
56#include "core/or/relay.h"
62#include "core/or/scheduler.h"
65#include "trunnel/channelpadding_negotiation.h"
66#include "trunnel/netinfo.h"
67#include "core/or/channelpadding.h"
68#include "core/or/extendinfo.h"
70
71#include "core/or/cell_st.h"
77#include "core/or/var_cell_st.h"
79
80#include "lib/tls/tortls.h"
81#include "lib/tls/x509.h"
82
83/** How many CELL_PADDING cells have we received, ever? */
85/** How many CELL_VERSIONS cells have we received, ever? */
87/** How many CELL_NETINFO cells have we received, ever? */
89/** How many CELL_VPADDING cells have we received, ever? */
91/** How many CELL_CERTS cells have we received, ever? */
93/** How many CELL_AUTH_CHALLENGE cells have we received, ever? */
95/** How many CELL_AUTHENTICATE cells have we received, ever? */
97/** How many CELL_AUTHORIZE cells have we received, ever? */
99
100/** Active listener, if any */
102
103/* channel_tls_t method declarations */
104
105static void channel_tls_close_method(channel_t *chan);
106static const char * channel_tls_describe_transport_method(channel_t *chan);
107static void channel_tls_free_method(channel_t *chan);
109static int channel_tls_get_remote_addr_method(const channel_t *chan,
110 tor_addr_t *addr_out);
111static int
112channel_tls_get_transport_name_method(channel_t *chan, char **transport_out);
113static const char *channel_tls_describe_peer_method(const channel_t *chan);
116static int
118 extend_info_t *extend_info);
120 const tor_addr_t *target);
124 cell_t *cell);
126 packed_cell_t *packed_cell);
128 var_cell_t *var_cell);
129
130/* channel_listener_tls_t method declarations */
131
133static const char *
135
136/** Handle incoming cells for the handshake stuff here rather than
137 * passing them on up. */
138
140 channel_tls_t *tlschan);
142 channel_tls_t *tlschan);
143static int command_allowed_before_handshake(uint8_t command);
145 channel_tls_t *tlschan);
147 channel_tls_t *chan);
148
149/**
150 * Do parts of channel_tls_t initialization common to channel_tls_connect()
151 * and channel_tls_handle_incoming().
152 */
153STATIC void
154channel_tls_common_init(channel_tls_t *tlschan)
155{
156 channel_t *chan;
157
158 tor_assert(tlschan);
159
160 chan = &(tlschan->base_);
161 channel_init(chan);
162 chan->magic = TLS_CHAN_MAGIC;
168 chan->get_remote_addr = channel_tls_get_remote_addr_method;
170 chan->get_transport_name = channel_tls_get_transport_name_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;
180
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);
184}
185
186/**
187 * Start a new TLS channel.
188 *
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.
192 */
193channel_t *
194channel_tls_connect(const tor_addr_t *addr, uint16_t port,
195 const char *id_digest,
196 const ed25519_public_key_t *ed_id,
197 const struct circuit_guard_state_t *guard_state,
198 bool for_origin_circ)
199{
200 channel_tls_t *tlschan = tor_malloc_zero(sizeof(*tlschan));
201 channel_t *chan = &(tlschan->base_);
202
204
205 log_debug(LD_CHANNEL,
206 "In channel_tls_connect() for channel %p "
207 "(global id %"PRIu64 ")",
208 tlschan,
209 (chan->global_identifier));
210
211 if (is_local_to_resolve_addr(addr)) {
212 log_debug(LD_CHANNEL,
213 "Marking new outgoing channel %"PRIu64 " at %p as local",
214 (chan->global_identifier), chan);
215 channel_mark_local(chan);
216 } else {
217 log_debug(LD_CHANNEL,
218 "Marking new outgoing channel %"PRIu64 " at %p as remote",
219 (chan->global_identifier), chan);
221 }
222
224 /* guard_state is borrowed for this synchronous launch only. */
226
227 /* Set up or_connection stuff */
228 or_connection_t *conn =
229 connection_or_connect(addr, port, id_digest, ed_id, tlschan,
230 for_origin_circ);
231 /* connection_or_connect() sets both conn->chan and tlschan->conn. If
232 * nonblocking connect() succeeds immediately, it also starts proxy/TLS
233 * setup before returning. That setup can fail (e.g., SOCKS4 with an IPv6
234 * target), leaving the OR connection marked for deferred close while
235 * returning NULL. Keeping that return value separate preserves tlschan->conn
236 * so the error path below can detach the surviving OR connection.
237 *
238 * Before freeing tlschan at err, this function clears the surviving OR
239 * connection's conn->chan, then tlschan->conn. Otherwise later
240 * connection_or_about_to_close() would notify a freed channel. The OR
241 * connection itself remains for normal deferred cleanup. Earlier failures
242 * that already freed it have already cleared tlschan->conn. */
243 if (!conn) {
244 if (tlschan->conn) {
245 tlschan->conn->chan = NULL;
246 tlschan->conn = NULL;
247 }
248 chan->reason_for_closing = CHANNEL_CLOSE_FOR_ERROR;
250 goto err;
251 }
252 tlschan->conn = conn;
253
254 log_debug(LD_CHANNEL,
255 "Got orconn %p for channel with global id %"PRIu64,
256 tlschan->conn, (chan->global_identifier));
257
258 goto done;
259
260 err:
261 /* The only current path here already clears the handle by transitioning to
262 * ERROR. This defensive release also covers future error paths at this
263 * raw-free boundary, which bypasses channel_free_().
264 * Clearing an already-consumed handle is harmless. */
266 circuitmux_free(chan->cmux);
267 tor_free(tlschan);
268 chan = NULL;
269
270 done:
271 /* If we got one, we should register it */
272 if (chan) channel_register(chan);
273
274 return chan;
275}
276
277/**
278 * Return the current channel_tls_t listener.
279 *
280 * Returns the current channel listener for incoming TLS connections, or
281 * NULL if none has been established
282 */
288
289/**
290 * Start a channel_tls_t listener if necessary.
291 *
292 * Return the current channel_tls_t listener, or start one if we haven't yet,
293 * and return that.
294 */
297{
298 channel_listener_t *listener;
299
301 listener = tor_malloc_zero(sizeof(*listener));
302 channel_init_listener(listener);
305 listener->describe_transport =
307
308 channel_tls_listener = listener;
309
310 log_debug(LD_CHANNEL,
311 "Starting TLS channel listener %p with global id %"PRIu64,
312 listener, (listener->global_identifier));
313
315 } else listener = channel_tls_listener;
316
317 return listener;
318}
319
320/**
321 * Free everything on shutdown.
322 *
323 * Not much to do here, since channel_free_all() takes care of a lot, but let's
324 * get rid of the listener.
325 */
326void
328{
329 channel_listener_t *old_listener = NULL;
330
331 log_debug(LD_CHANNEL,
332 "Shutting down TLS channels...");
333
335 /*
336 * When we close it, channel_tls_listener will get nulled out, so save
337 * a pointer so we can free it.
338 */
339 old_listener = channel_tls_listener;
340 log_debug(LD_CHANNEL,
341 "Closing channel_tls_listener with ID %"PRIu64
342 " at %p.",
343 (old_listener->global_identifier),
344 old_listener);
345 channel_listener_unregister(old_listener);
347 channel_listener_free(old_listener);
349 }
350
351 log_debug(LD_CHANNEL,
352 "Done shutting down TLS channels");
353}
354
355/**
356 * Create a new channel around an incoming or_connection_t.
357 */
358channel_t *
360{
361 channel_tls_t *tlschan = tor_malloc_zero(sizeof(*tlschan));
362 channel_t *chan = &(tlschan->base_);
363
364 tor_assert(orconn);
365 tor_assert(!(orconn->chan));
366
368
369 /* Link the channel and orconn to each other */
370 tlschan->conn = orconn;
371 orconn->chan = tlschan;
372
373 if (is_local_to_resolve_addr(&(TO_CONN(orconn)->addr))) {
374 log_debug(LD_CHANNEL,
375 "Marking new incoming channel %"PRIu64 " at %p as local",
376 (chan->global_identifier), chan);
377 channel_mark_local(chan);
378 } else {
379 log_debug(LD_CHANNEL,
380 "Marking new incoming channel %"PRIu64 " at %p as remote",
381 (chan->global_identifier), chan);
383 }
384
386
387 /* Register it */
388 channel_register(chan);
389
390 char *transport_name = NULL;
391 if (channel_tls_get_transport_name_method(TLS_CHAN_TO_BASE(orconn->chan),
392 &transport_name) < 0) {
393 transport_name = NULL;
394 }
395 /* Start tracking TLS connections in the DoS subsystem as soon as possible,
396 * so we can protect against attacks that use partially open connections.
397 */
398 geoip_note_client_seen(GEOIP_CLIENT_CONNECT,
399 &TO_CONN(orconn)->addr, transport_name,
400 time(NULL));
401 dos_new_client_conn(orconn, transport_name);
402 tor_free(transport_name);
403
404 return chan;
405}
406
407/**
408 * Set the `potentially_used_for_bootstrapping` flag on the or_connection_t
409 * corresponding to the provided channel.
410 *
411 * This flag indicates that if the connection fails, it might be interesting
412 * to the bootstrapping subsystem. (The bootstrapping system only cares about
413 * channels that we have tried to use for our own circuits. Other channels
414 * may have been launched in response to EXTEND cells from somebody else, and
415 * if they fail, it won't necessarily indicate a bootstrapping problem.)
416 **/
417void
419{
420 if (BUG(!chan))
421 return;
422 if (chan->magic != TLS_CHAN_MAGIC)
423 return;
424 channel_tls_t *tlschan = channel_tls_from_base(chan);
425 if (BUG(!tlschan))
426 return;
427
428 if (tlschan->conn)
429 tlschan->conn->potentially_used_for_bootstrapping = 1;
430}
431
432/*********
433 * Casts *
434 ********/
435
436/**
437 * Cast a channel_tls_t to a channel_t.
438 */
439channel_t *
440channel_tls_to_base(channel_tls_t *tlschan)
441{
442 if (!tlschan) return NULL;
443
444 return &(tlschan->base_);
445}
446
447/**
448 * Cast a channel_t to a channel_tls_t, with appropriate type-checking
449 * asserts.
450 */
451channel_tls_t *
453{
454 if (!chan) return NULL;
455
456 tor_assert(chan->magic == TLS_CHAN_MAGIC);
457
458 return (channel_tls_t *)(chan);
459}
460
461/**
462 * Cast a const channel_tls_t to a const channel_t.
463 */
464const channel_t *
465channel_tls_to_base_const(const channel_tls_t *tlschan)
466{
467 return channel_tls_to_base((channel_tls_t*) tlschan);
468}
469
470/**
471 * Cast a const channel_t to a const channel_tls_t, with appropriate
472 * type-checking asserts.
473 */
474const channel_tls_t *
476{
477 return channel_tls_from_base((channel_t *)chan);
478}
479
480/********************************************
481 * Method implementations for channel_tls_t *
482 *******************************************/
483
484/**
485 * Close a channel_tls_t.
486 *
487 * This implements the close method for channel_tls_t.
488 */
489static void
491{
492 channel_tls_t *tlschan = BASE_CHAN_TO_TLS(chan);
493
494 tor_assert(tlschan);
495
496 if (tlschan->conn) connection_or_close_normally(tlschan->conn, 1);
497 else {
498 /* Weird - we'll have to change the state ourselves, I guess */
499 log_info(LD_CHANNEL,
500 "Tried to close channel_tls_t %p with NULL conn",
501 tlschan);
503 }
504}
505
506/**
507 * Describe the transport for a channel_tls_t.
508 *
509 * This returns the string "TLS channel on connection <id>" to the upper
510 * layer.
511 */
512static const char *
514{
515 static char *buf = NULL;
516 uint64_t id;
517 channel_tls_t *tlschan;
518 const char *rv = NULL;
519
520 tor_assert(chan);
521
522 tlschan = BASE_CHAN_TO_TLS(chan);
523
524 if (tlschan->conn) {
525 id = TO_CONN(tlschan->conn)->global_identifier;
526
527 if (buf) tor_free(buf);
528 tor_asprintf(&buf,
529 "TLS channel (connection %"PRIu64 ")",
530 (id));
531
532 rv = buf;
533 } else {
534 rv = "TLS channel (no connection)";
535 }
536
537 return rv;
538}
539
540/**
541 * Free a channel_tls_t.
542 *
543 * This is called by the generic channel layer when freeing a channel_tls_t;
544 * this happens either on a channel which has already reached
545 * CHANNEL_STATE_CLOSED or CHANNEL_STATE_ERROR from channel_run_cleanup() or
546 * on shutdown from channel_free_all(). In the latter case we might still
547 * have an orconn active (which connection_free_all() will get to later),
548 * so we should null out its channel pointer now.
549 */
550static void
552{
553 channel_tls_t *tlschan = BASE_CHAN_TO_TLS(chan);
554
555 tor_assert(tlschan);
556
557 if (tlschan->conn) {
558 tlschan->conn->chan = NULL;
559 tlschan->conn = NULL;
560 }
561}
562
563/**
564 * Get an estimate of the average TLS overhead for the upper layer.
565 */
566static double
568{
569 double overhead = 1.0;
570 channel_tls_t *tlschan = BASE_CHAN_TO_TLS(chan);
571
572 tor_assert(tlschan);
573 tor_assert(tlschan->conn);
574
575 /* Just return 1.0f if we don't have sensible data */
576 if (tlschan->conn->bytes_xmitted > 0 &&
577 tlschan->conn->bytes_xmitted_by_tls >=
578 tlschan->conn->bytes_xmitted) {
579 overhead = ((double)(tlschan->conn->bytes_xmitted_by_tls)) /
580 ((double)(tlschan->conn->bytes_xmitted));
581
582 /*
583 * Never estimate more than 2.0; otherwise we get silly large estimates
584 * at the very start of a new TLS connection.
585 */
586 if (overhead > 2.0)
587 overhead = 2.0;
588 }
589
590 log_debug(LD_CHANNEL,
591 "Estimated overhead ratio for TLS chan %"PRIu64 " is %f",
592 (chan->global_identifier), overhead);
593
594 return overhead;
595}
596
597/**
598 * Get the remote address of a channel_tls_t.
599 *
600 * This implements the get_remote_addr method for channel_tls_t; copy the
601 * remote endpoint of the channel to addr_out and return 1. (Always
602 * succeeds if this channel is attached to an OR connection.)
603 *
604 * Always returns the real address of the peer, not the canonical address.
605 */
606static int
608 tor_addr_t *addr_out)
609{
610 const channel_tls_t *tlschan = CONST_BASE_CHAN_TO_TLS(chan);
611
612 tor_assert(tlschan);
613 tor_assert(addr_out);
614
615 if (tlschan->conn == NULL) {
616 tor_addr_make_unspec(addr_out);
617 return 0;
618 }
619
620 /* They want the real address, so give it to them. */
621 tor_addr_copy(addr_out, &TO_CONN(tlschan->conn)->addr);
622
623 return 1;
624}
625
626/**
627 * Get the name of the pluggable transport used by a channel_tls_t.
628 *
629 * This implements the get_transport_name for channel_tls_t. If the
630 * channel uses a pluggable transport, copy its name to
631 * <b>transport_out</b> and return 0. If the channel did not use a
632 * pluggable transport, return -1.
633 */
634static int
636{
637 channel_tls_t *tlschan = BASE_CHAN_TO_TLS(chan);
638
639 tor_assert(tlschan);
640 tor_assert(transport_out);
641 tor_assert(tlschan->conn);
642
643 if (!tlschan->conn->ext_or_transport)
644 return -1;
645
646 *transport_out = tor_strdup(tlschan->conn->ext_or_transport);
647 return 0;
648}
649
650/**
651 * Get a human-readable endpoint description of a channel_tls_t.
652 *
653 * This format is intended for logging, and may change in the future;
654 * nothing should parse or rely on its particular details.
655 */
656static const char *
658{
659 const channel_tls_t *tlschan = CONST_BASE_CHAN_TO_TLS(chan);
660 tor_assert(tlschan);
661
662 if (tlschan->conn) {
663 return connection_describe_peer(TO_CONN(tlschan->conn));
664 } else {
665 return "(No connection)";
666 }
667}
668
669/**
670 * Tell the upper layer if we have queued writes.
671 *
672 * This implements the has_queued_writes method for channel_tls t_; it returns
673 * 1 iff we have queued writes on the outbuf of the underlying or_connection_t.
674 */
675static int
677{
678 size_t outbuf_len;
679 channel_tls_t *tlschan = BASE_CHAN_TO_TLS(chan);
680
681 tor_assert(tlschan);
682 if (!(tlschan->conn)) {
683 log_info(LD_CHANNEL,
684 "something called has_queued_writes on a tlschan "
685 "(%p with ID %"PRIu64 " but no conn",
686 chan, (chan->global_identifier));
687 }
688
689 outbuf_len = (tlschan->conn != NULL) ?
690 connection_get_outbuf_len(TO_CONN(tlschan->conn)) :
691 0;
692
693 return (outbuf_len > 0);
694}
695
696/**
697 * Tell the upper layer if we're canonical.
698 *
699 * This implements the is_canonical method for channel_tls_t:
700 * it returns whether this is a canonical channel.
701 */
702static int
704{
705 int answer = 0;
706 channel_tls_t *tlschan = BASE_CHAN_TO_TLS(chan);
707
708 tor_assert(tlschan);
709
710 if (tlschan->conn) {
711 /* If this bit is set to 0, and link_proto is sufficiently old, then we
712 * can't actually _rely_ on this being a non-canonical channel.
713 * Nonetheless, we're going to believe that this is a non-canonical
714 * channel in this case, since nobody should be using these link protocols
715 * any more. */
716 answer = tlschan->conn->is_canonical;
717 }
718
719 return answer;
720}
721
722/**
723 * Check if we match an extend_info_t.
724 *
725 * This implements the matches_extend_info method for channel_tls_t; the upper
726 * layer wants to know if this channel matches an extend_info_t.
727 *
728 * NOTE that this function only checks for an address/port match, and should
729 * be used only when no identify is available.
730 */
731static int
733 extend_info_t *extend_info)
734{
735 channel_tls_t *tlschan = BASE_CHAN_TO_TLS(chan);
736
737 tor_assert(tlschan);
738 tor_assert(extend_info);
739
740 /* Never match if we have no conn */
741 if (!(tlschan->conn)) {
742 log_info(LD_CHANNEL,
743 "something called matches_extend_info on a tlschan "
744 "(%p with ID %"PRIu64 " but no conn",
745 chan, (chan->global_identifier));
746 return 0;
747 }
748
749 const tor_addr_port_t *orport = &tlschan->conn->canonical_orport;
750 // If the canonical address is set, then we'll allow matches based on that.
751 if (! tor_addr_is_unspec(&orport->addr)) {
752 if (extend_info_has_orport(extend_info, &orport->addr, orport->port)) {
753 return 1;
754 }
755 }
756
757 // We also want to match if the true address and port are listed in the
758 // extend info.
759 return extend_info_has_orport(extend_info,
760 &TO_CONN(tlschan->conn)->addr,
761 TO_CONN(tlschan->conn)->port);
762}
763
764/**
765 * Check if we match a target address; return true iff we do.
766 *
767 * This implements the matches_target method for channel_tls t_; the upper
768 * layer wants to know if this channel matches a target address when extending
769 * a circuit.
770 */
771static int
773 const tor_addr_t *target)
774{
775 channel_tls_t *tlschan = BASE_CHAN_TO_TLS(chan);
776
777 tor_assert(tlschan);
778 tor_assert(target);
779
780 /* Never match if we have no conn */
781 if (!(tlschan->conn)) {
782 log_info(LD_CHANNEL,
783 "something called matches_target on a tlschan "
784 "(%p with ID %"PRIu64 " but no conn",
785 chan, (chan->global_identifier));
786 return 0;
787 }
788
789 /* addr is the address this connection came from.
790 * canonical_orport is updated by connection_or_init_conn_from_address()
791 * to be the address in the descriptor. It may be tempting to
792 * allow either address to be allowed, but if we did so, it would
793 * enable someone who steals a relay's keys to covertly impersonate/MITM it
794 * from anywhere on the Internet! (Because they could make long-lived
795 * TLS connections from anywhere to all relays, and wait for them to
796 * be used for extends).
797 *
798 * An adversary who has stolen a relay's keys could also post a fake relay
799 * descriptor, but that attack is easier to detect.
800 */
801 return tor_addr_eq(&TO_CONN(tlschan->conn)->addr, target);
802}
803
804/**
805 * Tell the upper layer how many bytes we have queued and not yet
806 * sent.
807 */
808static size_t
810{
811 channel_tls_t *tlschan = BASE_CHAN_TO_TLS(chan);
812
813 tor_assert(tlschan);
814 tor_assert(tlschan->conn);
815
816 return connection_get_outbuf_len(TO_CONN(tlschan->conn));
817}
818
819/**
820 * Tell the upper layer how many cells we can accept to write.
821 *
822 * This implements the num_cells_writeable method for channel_tls_t; it
823 * returns an estimate of the number of cells we can accept with
824 * channel_tls_write_*_cell().
825 */
826static int
828{
829 size_t outbuf_len;
830 ssize_t n;
831 channel_tls_t *tlschan = BASE_CHAN_TO_TLS(chan);
832 size_t cell_network_size;
833
834 tor_assert(tlschan);
835 tor_assert(tlschan->conn);
836
837 cell_network_size = get_cell_network_size(tlschan->conn->wide_circ_ids);
838 outbuf_len = connection_get_outbuf_len(TO_CONN(tlschan->conn));
839 /* Get the number of cells */
840 n = CEIL_DIV(or_conn_highwatermark() - outbuf_len, cell_network_size);
841 if (n < 0) n = 0;
842#if SIZEOF_SIZE_T > SIZEOF_INT
843 if (n > INT_MAX) n = INT_MAX;
844#endif
845
846 return (int)n;
847}
848
849/**
850 * Write a cell to a channel_tls_t.
851 *
852 * This implements the write_cell method for channel_tls_t; given a
853 * channel_tls_t and a cell_t, transmit the cell_t.
854 */
855static int
857{
858 channel_tls_t *tlschan = BASE_CHAN_TO_TLS(chan);
859 int written = 0;
860
861 tor_assert(tlschan);
862 tor_assert(cell);
863
864 if (tlschan->conn) {
865 connection_or_write_cell_to_buf(cell, tlschan->conn);
866 ++written;
867 } else {
868 log_info(LD_CHANNEL,
869 "something called write_cell on a tlschan "
870 "(%p with ID %"PRIu64 " but no conn",
871 chan, (chan->global_identifier));
872 }
873
874 return written;
875}
876
877/**
878 * Write a packed cell to a channel_tls_t.
879 *
880 * This implements the write_packed_cell method for channel_tls_t; given a
881 * channel_tls_t and a packed_cell_t, transmit the packed_cell_t.
882 *
883 * Return 0 on success or negative value on error. The caller must free the
884 * packed cell.
885 */
886static int
888 packed_cell_t *packed_cell)
889{
890 tor_assert(chan);
891 channel_tls_t *tlschan = BASE_CHAN_TO_TLS(chan);
892 size_t cell_network_size = get_cell_network_size(chan->wide_circ_ids);
893
894 tor_assert(tlschan);
895 tor_assert(packed_cell);
896
897 if (tlschan->conn) {
898 connection_buf_add(packed_cell->body, cell_network_size,
899 TO_CONN(tlschan->conn));
900 } else {
901 log_info(LD_CHANNEL,
902 "something called write_packed_cell on a tlschan "
903 "(%p with ID %"PRIu64 " but no conn",
904 chan, (chan->global_identifier));
905 return -1;
906 }
907
908 return 0;
909}
910
911/**
912 * Write a variable-length cell to a channel_tls_t.
913 *
914 * This implements the write_var_cell method for channel_tls_t; given a
915 * channel_tls_t and a var_cell_t, transmit the var_cell_t.
916 */
917static int
919{
920 channel_tls_t *tlschan = BASE_CHAN_TO_TLS(chan);
921 int written = 0;
922
923 tor_assert(tlschan);
924 tor_assert(var_cell);
925
926 if (tlschan->conn) {
927 connection_or_write_var_cell_to_buf(var_cell, tlschan->conn);
928 ++written;
929 } else {
930 log_info(LD_CHANNEL,
931 "something called write_var_cell on a tlschan "
932 "(%p with ID %"PRIu64 " but no conn",
933 chan, (chan->global_identifier));
934 }
935
936 return written;
937}
938
939/*************************************************
940 * Method implementations for channel_listener_t *
941 ************************************************/
942
943/**
944 * Close a channel_listener_t.
945 *
946 * This implements the close method for channel_listener_t.
947 */
948static void
950{
951 tor_assert(chan_l);
952
953 /*
954 * Listeners we just go ahead and change state through to CLOSED, but
955 * make sure to check if they're channel_tls_listener to NULL it out.
956 */
957 if (chan_l == channel_tls_listener)
959
960 if (!(chan_l->state == CHANNEL_LISTENER_STATE_CLOSING ||
964 }
965
966 if (chan_l->incoming_list) {
968 channel_t *, ichan) {
970 } SMARTLIST_FOREACH_END(ichan);
971
972 smartlist_free(chan_l->incoming_list);
973 chan_l->incoming_list = NULL;
974 }
975
976 if (!(chan_l->state == CHANNEL_LISTENER_STATE_CLOSED ||
979 }
980}
981
982/**
983 * Describe the transport for a channel_listener_t.
984 *
985 * This returns the string "TLS channel (listening)" to the upper
986 * layer.
987 */
988static const char *
990{
991 tor_assert(chan_l);
992
993 return "TLS channel (listening)";
994}
995
996/*******************************************************
997 * Functions for handling events on an or_connection_t *
998 ******************************************************/
999
1000/**
1001 * Handle an orconn state change.
1002 *
1003 * This function will be called by connection_or.c when the or_connection_t
1004 * associated with this channel_tls_t changes state.
1005 */
1006void
1008 or_connection_t *conn,
1009 uint8_t state)
1010{
1011 channel_t *base_chan;
1012
1013 tor_assert(chan);
1014 tor_assert(conn);
1015 tor_assert(conn->chan == chan);
1016 tor_assert(chan->conn == conn);
1017
1018 base_chan = TLS_CHAN_TO_BASE(chan);
1019
1020 /* Make sure the base connection state makes sense - shouldn't be error
1021 * or closed. */
1022
1023 tor_assert(CHANNEL_IS_OPENING(base_chan) ||
1024 CHANNEL_IS_OPEN(base_chan) ||
1025 CHANNEL_IS_MAINT(base_chan) ||
1026 CHANNEL_IS_CLOSING(base_chan));
1027
1028 /* Did we just go to state open? */
1029 if (state == OR_CONN_STATE_OPEN) {
1030 /*
1031 * We can go to CHANNEL_STATE_OPEN from CHANNEL_STATE_OPENING or
1032 * CHANNEL_STATE_MAINT on this.
1033 */
1034 channel_change_state_open(base_chan);
1035 /* We might have just become writeable; check and tell the scheduler */
1036 if (connection_or_num_cells_writeable(conn) > 0) {
1038 }
1039 } else {
1040 /*
1041 * Not open, so from CHANNEL_STATE_OPEN we go to CHANNEL_STATE_MAINT,
1042 * otherwise no change.
1043 */
1044 if (CHANNEL_IS_OPEN(base_chan)) {
1046 }
1047 }
1048}
1049
1050#ifdef KEEP_TIMING_STATS
1051
1052/**
1053 * Timing states wrapper.
1054 *
1055 * This is a wrapper function around the actual function that processes the
1056 * <b>cell</b> that just arrived on <b>chan</b>. Increment <b>*time</b>
1057 * by the number of microseconds used by the call to <b>*func(cell, chan)</b>.
1058 */
1059static void
1060channel_tls_time_process_cell(cell_t *cell, channel_tls_t *chan, int *time,
1061 void (*func)(cell_t *, channel_tls_t *))
1062{
1063 struct timeval start, end;
1064 long time_passed;
1065
1066 tor_gettimeofday(&start);
1067
1068 (*func)(cell, chan);
1069
1070 tor_gettimeofday(&end);
1071 time_passed = tv_udiff(&start, &end) ;
1072
1073 if (time_passed > 10000) { /* more than 10ms */
1074 log_debug(LD_OR,"That call just took %ld ms.",time_passed/1000);
1075 }
1076
1077 if (time_passed < 0) {
1078 log_info(LD_GENERAL,"That call took us back in time!");
1079 time_passed = 0;
1080 }
1081
1082 *time += time_passed;
1083}
1084#endif /* defined(KEEP_TIMING_STATS) */
1085
1086#ifdef KEEP_TIMING_STATS
1087#define PROCESS_CELL(tp, cl, cn) STMT_BEGIN { \
1088 ++num ## tp; \
1089 channel_tls_time_process_cell(cl, cn, & tp ## time , \
1090 channel_tls_process_ ## tp ## _cell); \
1091 } STMT_END
1092#else /* !defined(KEEP_TIMING_STATS) */
1093#define PROCESS_CELL(tp, cl, cn) channel_tls_process_ ## tp ## _cell(cl, cn)
1094#endif /* defined(KEEP_TIMING_STATS) */
1095
1096/**
1097 * Handle an incoming cell on a channel_tls_t.
1098 *
1099 * This is called from connection_or.c to handle an arriving cell; it checks
1100 * for cell types specific to the handshake for this transport protocol and
1101 * handles them, and queues all other cells to the channel_t layer, which
1102 * eventually will hand them off to command.c.
1103 *
1104 * The channel layer itself decides whether the cell should be queued or
1105 * can be handed off immediately to the upper-layer code. It is responsible
1106 * for copying in the case that it queues; we merely pass pointers through
1107 * which we get from connection_or_process_cells_from_inbuf().
1108 */
1109void
1111{
1112 channel_tls_t *chan;
1113 int handshaking;
1114
1115 tor_assert(cell);
1116 tor_assert(conn);
1117
1118 chan = conn->chan;
1119
1120 if (!chan) {
1121 log_warn(LD_CHANNEL,
1122 "Got a cell_t on an OR connection with no channel");
1123 return;
1124 }
1125
1126 handshaking = (TO_CONN(conn)->state != OR_CONN_STATE_OPEN);
1127
1128 if (conn->base_.marked_for_close)
1129 return;
1130
1131 /* Reject all but VERSIONS and NETINFO when handshaking. */
1132 /* (VERSIONS actually indicates a protocol warning: it's variable-length,
1133 * so if it reaches this function, we're on a v1 connection.) */
1134 if (handshaking && cell->command != CELL_VERSIONS &&
1135 cell->command != CELL_NETINFO) {
1136 log_fn(LOG_PROTOCOL_WARN, LD_PROTOCOL,
1137 "Received unexpected cell command %d in chan state %s / "
1138 "conn state %s; closing the connection.",
1139 (int)cell->command,
1140 channel_state_to_string(TLS_CHAN_TO_BASE(chan)->state),
1143 return;
1144 }
1145
1146 if (conn->base_.state == OR_CONN_STATE_OR_HANDSHAKING_V3) {
1147 /* Defense in depth considering bug #41420. */
1148 if (BUG(!chan->conn->handshake_state)) {
1149 log_fn(LOG_PROTOCOL_WARN, LD_PROTOCOL,
1150 "Received a cell but handshake state is missing on %s",
1151 connection_describe(TO_CONN(chan->conn)));
1152 connection_or_close_for_error(chan->conn, 0);
1153 return;
1154 }
1155 or_handshake_state_record_cell(conn, conn->handshake_state, cell, 1);
1156 }
1157
1158 /* We note that we're on the internet whenever we read a cell. This is
1159 * a fast operation. */
1162
1163 if (TLS_CHAN_TO_BASE(chan)->padding_enabled)
1165
1166 switch (cell->command) {
1167 case CELL_PADDING:
1169 if (TLS_CHAN_TO_BASE(chan)->padding_enabled)
1172 /* do nothing */
1173 break;
1174 case CELL_VERSIONS:
1175 /* A VERSIONS cell should always be a variable-length cell, and
1176 * so should never reach this function (which handles constant-sized
1177 * cells). But if the connection is using the (obsolete) v1 link
1178 * protocol, all cells will be treated as constant-sized, and so
1179 * it's possible we'll reach this code.
1180 */
1181 log_fn(LOG_PROTOCOL_WARN, LD_CHANNEL,
1182 "Received unexpected VERSIONS cell on a channel using link "
1183 "protocol %d; ignoring.", conn->link_proto);
1184 break;
1185 case CELL_NETINFO:
1187 PROCESS_CELL(netinfo, cell, chan);
1188 break;
1189 case CELL_PADDING_NEGOTIATE:
1191 PROCESS_CELL(padding_negotiate, cell, chan);
1192 break;
1193 case CELL_CREATE:
1194 case CELL_CREATE_FAST:
1195 case CELL_CREATED:
1196 case CELL_CREATED_FAST:
1197 case CELL_RELAY:
1198 case CELL_RELAY_EARLY:
1199 case CELL_DESTROY:
1200 case CELL_CREATE2:
1201 case CELL_CREATED2:
1202 /*
1203 * These are all transport independent and we pass them up through the
1204 * channel_t mechanism. They are ultimately handled in command.c.
1205 */
1206 channel_process_cell(TLS_CHAN_TO_BASE(chan), cell);
1207 break;
1208 default:
1210 "Cell of unknown type (%d) received in channeltls.c. "
1211 "Dropping.",
1212 cell->command);
1213 break;
1214 }
1215}
1216
1217/**
1218 * Handle an incoming variable-length cell on a channel_tls_t.
1219 *
1220 * Process a <b>var_cell</b> that was just received on <b>conn</b>. Keep
1221 * internal statistics about how many of each cell we've processed so far
1222 * this second, and the total number of microseconds it took to
1223 * process each type of cell. All the var_cell commands are handshake-
1224 * related and live below the channel_t layer, so no variable-length
1225 * cells ever get delivered in the current implementation, but I've left
1226 * the mechanism in place for future use.
1227 *
1228 * If we were handing them off to the upper layer, the channel_t queueing
1229 * code would be responsible for memory management, and we'd just be passing
1230 * pointers through from connection_or_process_cells_from_inbuf(). That
1231 * caller always frees them after this function returns, so this function
1232 * should never free var_cell.
1233 */
1234void
1236{
1237 channel_tls_t *chan;
1238
1239#ifdef KEEP_TIMING_STATS
1240 /* how many of each cell have we seen so far this second? needs better
1241 * name. */
1242 static int num_versions = 0, num_certs = 0;
1243 static time_t current_second = 0; /* from previous calls to time */
1244 time_t now = time(NULL);
1245
1246 if (current_second == 0) current_second = now;
1247 if (now > current_second) { /* the second has rolled over */
1248 /* print stats */
1249 log_info(LD_OR,
1250 "At end of second: %d versions (%d ms), %d certs (%d ms)",
1251 num_versions, versions_time / ((now - current_second) * 1000),
1252 num_certs, certs_time / ((now - current_second) * 1000));
1253
1254 num_versions = num_certs = 0;
1255 versions_time = certs_time = 0;
1256
1257 /* remember which second it is, for next time */
1258 current_second = now;
1259 }
1260#endif /* defined(KEEP_TIMING_STATS) */
1261
1262 tor_assert(var_cell);
1263 tor_assert(conn);
1264
1265 chan = conn->chan;
1266
1267 if (!chan) {
1268 log_warn(LD_CHANNEL,
1269 "Got a var_cell_t on an OR connection with no channel");
1270 return;
1271 }
1272
1273 if (TO_CONN(conn)->marked_for_close)
1274 return;
1275
1276 switch (TO_CONN(conn)->state) {
1278 /* If we're using bufferevents, it's entirely possible for us to
1279 * notice "hey, data arrived!" before we notice "hey, the handshake
1280 * finished!" And we need to be accepting both at once to handle both
1281 * the v2 and v3 handshakes. */
1282 /* But that should be happening any longer've disabled bufferevents. */
1283 tor_assert_nonfatal_unreached_once();
1286 if (!(command_allowed_before_handshake(var_cell->command))) {
1287 log_fn(LOG_PROTOCOL_WARN, LD_PROTOCOL,
1288 "Received a cell with command %d in unexpected "
1289 "orconn state \"%s\" [%d], channel state \"%s\" [%d]; "
1290 "closing the connection.",
1291 (int)(var_cell->command),
1293 (int)(TO_CONN(conn)->state),
1294 channel_state_to_string(TLS_CHAN_TO_BASE(chan)->state),
1295 (int)(TLS_CHAN_TO_BASE(chan)->state));
1296 /* see above comment about CHANNEL_STATE_ERROR */
1298 return;
1299 } else {
1300 if (enter_v3_handshake_with_cell(var_cell, chan) < 0)
1301 return;
1302 }
1303 break;
1305 if (var_cell->command != CELL_AUTHENTICATE) {
1306 /* Defense in depth considering bug #41420. */
1307 if (BUG(!chan->conn->handshake_state)) {
1308 log_fn(LOG_PROTOCOL_WARN, LD_PROTOCOL,
1309 "Received a var cell but handshake state is missing on %s",
1310 connection_describe(TO_CONN(chan->conn)));
1311 connection_or_close_for_error(chan->conn, 0);
1312 return;
1313 }
1315 var_cell, 1);
1316 }
1317 break; /* Everything is allowed */
1318 case OR_CONN_STATE_OPEN:
1319 if (conn->link_proto < 3) {
1320 log_fn(LOG_PROTOCOL_WARN, LD_PROTOCOL,
1321 "Received a variable-length cell with command %d in orconn "
1322 "state %s [%d], channel state %s [%d] with link protocol %d; "
1323 "ignoring it.",
1324 (int)(var_cell->command),
1326 (int)(TO_CONN(conn)->state),
1327 channel_state_to_string(TLS_CHAN_TO_BASE(chan)->state),
1328 (int)(TLS_CHAN_TO_BASE(chan)->state),
1329 (int)(conn->link_proto));
1330 return;
1331 }
1332 break;
1333 default:
1334 log_fn(LOG_PROTOCOL_WARN, LD_PROTOCOL,
1335 "Received var-length cell with command %d in unexpected "
1336 "orconn state \"%s\" [%d], channel state \"%s\" [%d]; "
1337 "ignoring it.",
1338 (int)(var_cell->command),
1340 (int)(TO_CONN(conn)->state),
1341 channel_state_to_string(TLS_CHAN_TO_BASE(chan)->state),
1342 (int)(TLS_CHAN_TO_BASE(chan)->state));
1343 return;
1344 }
1345
1346 /* We note that we're on the internet whenever we read a cell. This is
1347 * a fast operation. */
1349
1350 /* Now handle the cell */
1351
1352 switch (var_cell->command) {
1353 case CELL_VERSIONS:
1355 PROCESS_CELL(versions, var_cell, chan);
1356 break;
1357 case CELL_VPADDING:
1359 /* Do nothing */
1360 break;
1361 case CELL_CERTS:
1363 PROCESS_CELL(certs, var_cell, chan);
1364 break;
1365 case CELL_AUTH_CHALLENGE:
1367 PROCESS_CELL(auth_challenge, var_cell, chan);
1368 break;
1369 case CELL_AUTHENTICATE:
1371 PROCESS_CELL(authenticate, var_cell, chan);
1372 break;
1373 case CELL_AUTHORIZE:
1375 /* Ignored so far. */
1376 break;
1377 default:
1379 "Variable-length cell of unknown type (%d) received.",
1380 (int)(var_cell->command));
1381 break;
1382 }
1383}
1384
1385#undef PROCESS_CELL
1386
1387/**
1388 * Update channel marks after connection_or.c has changed an address.
1389 *
1390 * This is called from connection_or_init_conn_from_address() after the
1391 * connection's _base.addr or real_addr fields have potentially been changed
1392 * so we can recalculate the local mark. Notably, this happens when incoming
1393 * connections are reverse-proxied and we only learn the real address of the
1394 * remote router by looking it up in the consensus after we finish the
1395 * handshake and know an authenticated identity digest.
1396 */
1397void
1399{
1400 channel_t *chan = NULL;
1401
1402 tor_assert(conn);
1403 tor_assert(conn->chan);
1404
1405 chan = TLS_CHAN_TO_BASE(conn->chan);
1406
1407 if (is_local_to_resolve_addr(&(TO_CONN(conn)->addr))) {
1408 if (!channel_is_local(chan)) {
1409 log_debug(LD_CHANNEL,
1410 "Marking channel %"PRIu64 " at %p as local",
1411 (chan->global_identifier), chan);
1412 channel_mark_local(chan);
1413 }
1414 } else {
1415 if (channel_is_local(chan)) {
1416 log_debug(LD_CHANNEL,
1417 "Marking channel %"PRIu64 " at %p as remote",
1418 (chan->global_identifier), chan);
1419 channel_mark_remote(chan);
1420 }
1421 }
1422}
1423
1424/**
1425 * Check if this cell type is allowed before the handshake is finished.
1426 *
1427 * Return true if <b>command</b> is a cell command that's allowed to start a
1428 * V3 handshake.
1429 */
1430static int
1432{
1433 switch (command) {
1434 case CELL_VERSIONS:
1435 case CELL_VPADDING:
1436 case CELL_AUTHORIZE:
1437 return 1;
1438 default:
1439 return 0;
1440 }
1441}
1442
1443/**
1444 * Start a V3 handshake on an incoming connection.
1445 *
1446 * Called when we as a server receive an appropriate cell while waiting
1447 * either for a cell or a TLS handshake. Set the connection's state to
1448 * "handshaking_v3', initializes the or_handshake_state field as needed,
1449 * and add the cell to the hash of incoming cells.)
1450 */
1451static int
1452enter_v3_handshake_with_cell(var_cell_t *cell, channel_tls_t *chan)
1453{
1454 int started_here = 0;
1455
1456 tor_assert(cell);
1457 tor_assert(chan);
1458 tor_assert(chan->conn);
1459
1460 started_here = connection_or_nonopen_was_started_here(chan->conn);
1461
1462 tor_assert(TO_CONN(chan->conn)->state == OR_CONN_STATE_TLS_HANDSHAKING ||
1463 TO_CONN(chan->conn)->state ==
1465
1466 if (started_here) {
1467 log_fn(LOG_PROTOCOL_WARN, LD_OR,
1468 "Received a cell while TLS-handshaking, not in "
1469 "OR_HANDSHAKING_V3, on a connection we originated.");
1470 }
1472 if (connection_init_or_handshake_state(chan->conn, started_here) < 0) {
1473 connection_or_close_for_error(chan->conn, 0);
1474 return -1;
1475 }
1477 chan->conn->handshake_state, cell, 1);
1478 return 0;
1479}
1480
1481/**
1482 * Process a 'versions' cell.
1483 *
1484 * This function is called to handle an incoming VERSIONS cell; the current
1485 * link protocol version must be 0 to indicate that no version has yet been
1486 * negotiated. We compare the versions in the cell to the list of versions
1487 * we support, pick the highest version we have in common, and continue the
1488 * negotiation from there.
1489 */
1490static void
1492{
1493 int highest_supported_version = 0;
1494 int started_here = 0;
1495
1496 tor_assert(cell);
1497 tor_assert(chan);
1498 tor_assert(chan->conn);
1499
1500 if ((cell->payload_len % 2) == 1) {
1501 log_fn(LOG_PROTOCOL_WARN, LD_OR,
1502 "Received a VERSION cell with odd payload length %d; "
1503 "closing connection.",cell->payload_len);
1504 connection_or_close_for_error(chan->conn, 0);
1505 return;
1506 }
1507
1508 started_here = connection_or_nonopen_was_started_here(chan->conn);
1509
1510 if (chan->conn->link_proto != 0 ||
1511 (chan->conn->handshake_state &&
1512 chan->conn->handshake_state->received_versions)) {
1513 log_fn(LOG_PROTOCOL_WARN, LD_OR,
1514 "Received a VERSIONS cell on a connection with its version "
1515 "already set to %d; dropping",
1516 (int)(chan->conn->link_proto));
1517 return;
1518 }
1519 switch (chan->conn->base_.state)
1520 {
1522 break;
1525 default:
1526 log_fn(LOG_PROTOCOL_WARN, LD_OR,
1527 "VERSIONS cell while in unexpected state");
1528 return;
1529 }
1530
1531 tor_assert(chan->conn->handshake_state);
1532
1533 {
1534 int i;
1535 const uint8_t *cp = cell->payload;
1536 for (i = 0; i < cell->payload_len / 2; ++i, cp += 2) {
1537 uint16_t v = ntohs(get_uint16(cp));
1538 if (is_or_protocol_version_known(v) && v > highest_supported_version)
1539 highest_supported_version = v;
1540 }
1541 }
1542 if (!highest_supported_version) {
1543 log_fn(LOG_PROTOCOL_WARN, LD_OR,
1544 "Couldn't find a version in common between my version list and the "
1545 "list in the VERSIONS cell; closing connection.");
1546 connection_or_close_for_error(chan->conn, 0);
1547 return;
1548 } else if (highest_supported_version == 1) {
1549 /* Negotiating version 1 makes no sense, since version 1 has no VERSIONS
1550 * cells. */
1551 log_fn(LOG_PROTOCOL_WARN, LD_OR,
1552 "Used version negotiation protocol to negotiate a v1 connection. "
1553 "That's crazily non-compliant. Closing connection.");
1554 connection_or_close_for_error(chan->conn, 0);
1555 return;
1556 } else if (highest_supported_version < 3 &&
1557 chan->conn->base_.state == OR_CONN_STATE_OR_HANDSHAKING_V3) {
1558 log_fn(LOG_PROTOCOL_WARN, LD_OR,
1559 "Negotiated link protocol 2 or lower after doing a v3 TLS "
1560 "handshake. Closing connection.");
1561 connection_or_close_for_error(chan->conn, 0);
1562 return;
1563 }
1564
1565 rep_hist_note_negotiated_link_proto(highest_supported_version, started_here);
1566
1567 chan->conn->link_proto = highest_supported_version;
1568 chan->conn->handshake_state->received_versions = 1;
1569
1570 if (chan->conn->link_proto == 2) {
1571 log_info(LD_OR,
1572 "Negotiated version %d on %s; sending NETINFO.",
1573 highest_supported_version,
1574 connection_describe(TO_CONN(chan->conn)));
1575
1576 if (connection_or_send_netinfo(chan->conn) < 0) {
1577 connection_or_close_for_error(chan->conn, 0);
1578 return;
1579 }
1580 } else {
1581 const int send_versions = !started_here;
1582 /* If we want to authenticate, send a CERTS cell */
1583 const int send_certs = !started_here || public_server_mode(get_options());
1584 /* If we're a host that got a connection, ask for authentication. */
1585 const int send_chall = !started_here;
1586 /* If our certs cell will authenticate us, we can send a netinfo cell
1587 * right now. */
1588 const int send_netinfo = !started_here;
1589 const int send_any =
1590 send_versions || send_certs || send_chall || send_netinfo;
1591 tor_assert(chan->conn->link_proto >= 3);
1592
1593 log_info(LD_OR,
1594 "Negotiated version %d with on %s; %s%s%s%s%s",
1595 highest_supported_version,
1596 connection_describe(TO_CONN(chan->conn)),
1597 send_any ? "Sending cells:" : "Waiting for CERTS cell",
1598 send_versions ? " VERSIONS" : "",
1599 send_certs ? " CERTS" : "",
1600 send_chall ? " AUTH_CHALLENGE" : "",
1601 send_netinfo ? " NETINFO" : "");
1602
1603#ifdef DISABLE_V3_LINKPROTO_SERVERSIDE
1604 if (1) {
1605 connection_or_close_normally(chan->conn, 1);
1606 return;
1607 }
1608#endif /* defined(DISABLE_V3_LINKPROTO_SERVERSIDE) */
1609
1610 if (send_versions) {
1611 if (connection_or_send_versions(chan->conn, 1) < 0) {
1612 log_warn(LD_OR, "Couldn't send versions cell");
1613 connection_or_close_for_error(chan->conn, 0);
1614 return;
1615 }
1616 }
1617
1618 /* We set this after sending the versions cell. */
1619 /*XXXXX symbolic const.*/
1620 TLS_CHAN_TO_BASE(chan)->wide_circ_ids =
1621 chan->conn->link_proto >= MIN_LINK_PROTO_FOR_WIDE_CIRC_IDS;
1622 chan->conn->wide_circ_ids = TLS_CHAN_TO_BASE(chan)->wide_circ_ids;
1623
1624 TLS_CHAN_TO_BASE(chan)->padding_enabled =
1625 chan->conn->link_proto >= MIN_LINK_PROTO_FOR_CHANNEL_PADDING;
1626
1627 if (send_certs) {
1628 if (connection_or_send_certs_cell(chan->conn) < 0) {
1629 log_warn(LD_OR, "Couldn't send certs cell");
1630 connection_or_close_for_error(chan->conn, 0);
1631 return;
1632 }
1633 }
1634 if (send_chall) {
1635 if (connection_or_send_auth_challenge_cell(chan->conn) < 0) {
1636 log_warn(LD_OR, "Couldn't send auth_challenge cell");
1637 connection_or_close_for_error(chan->conn, 0);
1638 return;
1639 }
1640 }
1641 if (send_netinfo) {
1642 if (connection_or_send_netinfo(chan->conn) < 0) {
1643 log_warn(LD_OR, "Couldn't send netinfo cell");
1644 connection_or_close_for_error(chan->conn, 0);
1645 return;
1646 }
1647 }
1648 }
1649}
1650
1651/**
1652 * Process a 'padding_negotiate' cell.
1653 *
1654 * This function is called to handle an incoming PADDING_NEGOTIATE cell;
1655 * enable or disable padding accordingly, and read and act on its timeout
1656 * value contents.
1657 */
1658static void
1660{
1661 channelpadding_negotiate_t *negotiation;
1662 tor_assert(cell);
1663 tor_assert(chan);
1664 tor_assert(chan->conn);
1665
1666 if (chan->conn->link_proto < MIN_LINK_PROTO_FOR_CHANNEL_PADDING) {
1667 log_fn(LOG_PROTOCOL_WARN, LD_OR,
1668 "Received a PADDING_NEGOTIATE cell on v%d connection; dropping.",
1669 chan->conn->link_proto);
1670 return;
1671 }
1672
1673 if (channelpadding_negotiate_parse(&negotiation, cell->payload,
1674 CELL_PAYLOAD_SIZE) < 0) {
1675 log_fn(LOG_PROTOCOL_WARN, LD_OR,
1676 "Received malformed PADDING_NEGOTIATE cell on v%d connection; "
1677 "dropping.", chan->conn->link_proto);
1678
1679 return;
1680 }
1681
1682 channelpadding_update_padding_for_channel(TLS_CHAN_TO_BASE(chan),
1683 negotiation);
1684
1685 channelpadding_negotiate_free(negotiation);
1686}
1687
1688/**
1689 * Convert <b>netinfo_addr</b> into corresponding <b>tor_addr</b>.
1690 * Return 0 on success; on failure, return -1 and log a warning.
1691 */
1692static int
1694 const netinfo_addr_t *netinfo_addr) {
1695 tor_assert(tor_addr);
1696 tor_assert(netinfo_addr);
1697
1698 uint8_t type = netinfo_addr_get_addr_type(netinfo_addr);
1699 uint8_t len = netinfo_addr_get_len(netinfo_addr);
1700
1701 if (type == NETINFO_ADDR_TYPE_IPV4 && len == 4) {
1702 uint32_t ipv4 = netinfo_addr_get_addr_ipv4(netinfo_addr);
1703 tor_addr_from_ipv4h(tor_addr, ipv4);
1704 } else if (type == NETINFO_ADDR_TYPE_IPV6 && len == 16) {
1705 const uint8_t *ipv6_bytes = netinfo_addr_getconstarray_addr_ipv6(
1706 netinfo_addr);
1707 tor_addr_from_ipv6_bytes(tor_addr, ipv6_bytes);
1708 } else {
1709 log_fn(LOG_PROTOCOL_WARN, LD_OR, "Cannot read address from NETINFO "
1710 "- wrong type/length.");
1711 return -1;
1712 }
1713
1714 return 0;
1715}
1716
1717/**
1718 * Helper: compute the absolute value of a time_t.
1719 *
1720 * (we need this because labs() doesn't always work for time_t, since
1721 * long can be shorter than time_t.)
1722 */
1723static inline time_t
1724time_abs(time_t val)
1725{
1726 return (val < 0) ? -val : val;
1727}
1728
1729/** Return true iff the channel can process a NETINFO cell. For this to return
1730 * true, these channel conditions apply:
1731 *
1732 * 1. Link protocol is version 2 or higher (tor-spec.txt, NETINFO cells
1733 * section).
1734 *
1735 * 2. Underlying OR connection of the channel is either in v2 or v3
1736 * handshaking state.
1737 */
1738static bool
1739can_process_netinfo_cell(const channel_tls_t *chan)
1740{
1741 /* NETINFO cells can only be negotiated on link protocol 2 or higher. */
1742 if (chan->conn->link_proto < 2) {
1743 log_fn(LOG_PROTOCOL_WARN, LD_OR,
1744 "Received a NETINFO cell on %s connection; dropping.",
1745 chan->conn->link_proto == 0 ? "non-versioned" : "a v1");
1746 return false;
1747 }
1748
1749 /* Can't process a NETINFO cell if the connection is not handshaking. */
1750 if (chan->conn->base_.state != OR_CONN_STATE_OR_HANDSHAKING_V3) {
1751 log_fn(LOG_PROTOCOL_WARN, LD_OR,
1752 "Received a NETINFO cell on non-handshaking connection; dropping.");
1753 return false;
1754 }
1755
1756 /* Make sure we do have handshake state. */
1757 tor_assert(chan->conn->handshake_state);
1758 tor_assert(chan->conn->handshake_state->received_versions);
1759
1760 return true;
1761}
1762
1763/** Mark the given channel endpoint as a client (which means either a tor
1764 * client or a tor bridge).
1765 *
1766 * This MUST be done on an _unauthenticated_ channel. It is a mistake to mark
1767 * an authenticated channel as a client.
1768 *
1769 * The following is done on the channel:
1770 *
1771 * 1. Marked as a client.
1772 * 2. Type of circuit ID type is set.
1773 * 3. The underlying OR connection is initialized with the address of the
1774 * endpoint.
1775 */
1776static void
1778{
1779 /* Ending up here for an authenticated link is a mistake. */
1780 if (BUG(chan->conn->handshake_state->authenticated)) {
1781 return;
1782 }
1783
1785 (const char*)(chan->conn->handshake_state->
1786 authenticated_rsa_peer_id)));
1788 (const char*)(chan->conn->handshake_state->
1789 authenticated_ed25519_peer_id.pubkey), 32));
1790 /* If the client never authenticated, it's a tor client or bridge
1791 * relay, and we must not use it for EXTEND requests (nor could we, as
1792 * there are no authenticated peer IDs) */
1793 channel_mark_client(TLS_CHAN_TO_BASE(chan));
1794 channel_set_circid_type(TLS_CHAN_TO_BASE(chan), NULL,
1795 chan->conn->link_proto < MIN_LINK_PROTO_FOR_WIDE_CIRC_IDS);
1796
1798 &(chan->conn->base_.addr),
1799 chan->conn->base_.port,
1800 /* zero, checked above */
1801 (const char*)(chan->conn->handshake_state->
1802 authenticated_rsa_peer_id),
1803 NULL, /* Ed25519 ID: Also checked as zero */
1804 0);
1805}
1806
1807/**
1808 * Process a 'netinfo' cell
1809 *
1810 * This function is called to handle an incoming NETINFO cell; read and act
1811 * on its contents, and set the connection state to "open".
1812 */
1813static void
1814channel_tls_process_netinfo_cell(cell_t *cell, channel_tls_t *chan)
1815{
1816 time_t timestamp;
1817 uint8_t my_addr_type;
1818 uint8_t my_addr_len;
1819 uint8_t n_other_addrs;
1820 time_t now = time(NULL);
1822
1823 time_t apparent_skew = 0;
1824 tor_addr_t my_apparent_addr = TOR_ADDR_NULL;
1825 int started_here = 0;
1826 const char *identity_digest = NULL;
1827
1828 tor_assert(cell);
1829 tor_assert(chan);
1830 tor_assert(chan->conn);
1831
1832 /* Make sure we can process a NETINFO cell. Link protocol and state
1833 * validation is done to make sure of it. */
1834 if (!can_process_netinfo_cell(chan)) {
1835 return;
1836 }
1837
1838 started_here = connection_or_nonopen_was_started_here(chan->conn);
1839 identity_digest = chan->conn->identity_digest;
1840
1841 if (chan->conn->base_.state == OR_CONN_STATE_OR_HANDSHAKING_V3) {
1842 tor_assert(chan->conn->link_proto >= 3);
1843 if (started_here) {
1844 if (!(chan->conn->handshake_state->authenticated)) {
1845 log_fn(LOG_PROTOCOL_WARN, LD_OR,
1846 "Got a NETINFO cell from server, "
1847 "but no authentication. Closing the connection.");
1848 connection_or_close_for_error(chan->conn, 0);
1849 return;
1850 }
1851 } else {
1852 /* We're the server. If the client never authenticated, we have some
1853 * housekeeping to do.
1854 *
1855 * It's a tor client or bridge relay, and we must not use it for EXTEND
1856 * requests (nor could we, as there are no authenticated peer IDs) */
1857 if (!(chan->conn->handshake_state->authenticated)) {
1859 }
1860 }
1861 }
1862
1863 /* Decode the cell. */
1864 netinfo_cell_t *netinfo_cell = NULL;
1865
1866 ssize_t parsed = netinfo_cell_parse(&netinfo_cell, cell->payload,
1868
1869 if (parsed < 0) {
1870 log_fn(LOG_PROTOCOL_WARN, LD_OR,
1871 "Failed to parse NETINFO cell - closing connection.");
1872 connection_or_close_for_error(chan->conn, 0);
1873 return;
1874 }
1875
1876 timestamp = netinfo_cell_get_timestamp(netinfo_cell);
1877
1878 const netinfo_addr_t *my_addr =
1879 netinfo_cell_getconst_other_addr(netinfo_cell);
1880
1881 my_addr_type = netinfo_addr_get_addr_type(my_addr);
1882 my_addr_len = netinfo_addr_get_len(my_addr);
1883
1884 if ((now - chan->conn->handshake_state->sent_versions_at) < 180) {
1885 apparent_skew = now - timestamp;
1886 }
1887 /* We used to check:
1888 * if (my_addr_len >= CELL_PAYLOAD_SIZE - 6) {
1889 *
1890 * This is actually never going to happen, since my_addr_len is at most 255,
1891 * and CELL_PAYLOAD_LEN - 6 is 503. So we know that cp is < end. */
1892
1893 if (tor_addr_from_netinfo_addr(&my_apparent_addr, my_addr) == -1) {
1894 connection_or_close_for_error(chan->conn, 0);
1895 netinfo_cell_free(netinfo_cell);
1896 return;
1897 }
1898
1899 if (my_addr_type == NETINFO_ADDR_TYPE_IPV4 && my_addr_len == 4) {
1900 if (!get_options()->BridgeRelay && me &&
1901 tor_addr_eq(&my_apparent_addr, &me->ipv4_addr)) {
1902 TLS_CHAN_TO_BASE(chan)->is_canonical_to_peer = 1;
1903 }
1904 } else if (my_addr_type == NETINFO_ADDR_TYPE_IPV6 &&
1905 my_addr_len == 16) {
1906 if (!get_options()->BridgeRelay && me &&
1907 !tor_addr_is_null(&me->ipv6_addr) &&
1908 tor_addr_eq(&my_apparent_addr, &me->ipv6_addr)) {
1909 TLS_CHAN_TO_BASE(chan)->is_canonical_to_peer = 1;
1910 }
1911 }
1912
1913 if (me) {
1914 /* We have a descriptor, so we are a relay: record the address that the
1915 * other side said we had. */
1916 tor_addr_copy(&TLS_CHAN_TO_BASE(chan)->addr_according_to_peer,
1917 &my_apparent_addr);
1918 }
1919
1920 n_other_addrs = netinfo_cell_get_n_my_addrs(netinfo_cell);
1921 for (uint8_t i = 0; i < n_other_addrs; i++) {
1922 /* Consider all the other addresses; if any matches, this connection is
1923 * "canonical." */
1924
1925 const netinfo_addr_t *netinfo_addr =
1926 netinfo_cell_getconst_my_addrs(netinfo_cell, i);
1927
1928 tor_addr_t addr;
1929
1930 if (tor_addr_from_netinfo_addr(&addr, netinfo_addr) == -1) {
1931 log_fn(LOG_PROTOCOL_WARN, LD_OR,
1932 "Bad address in netinfo cell; Skipping.");
1933 continue;
1934 }
1935 /* A relay can connect from anywhere and be canonical, so
1936 * long as it tells you from where it came. This may sound a bit
1937 * concerning... but that's what "canonical" means: that the
1938 * address is one that the relay itself has claimed. The relay
1939 * might be doing something funny, but nobody else is doing a MITM
1940 * on the relay's TCP.
1941 */
1942 if (tor_addr_eq(&addr, &TO_CONN(chan->conn)->addr)) {
1943 connection_or_set_canonical(chan->conn, 1);
1944 break;
1945 }
1946 }
1947
1948 netinfo_cell_free(netinfo_cell);
1949
1950 if (me && !TLS_CHAN_TO_BASE(chan)->is_canonical_to_peer &&
1951 channel_is_canonical(TLS_CHAN_TO_BASE(chan))) {
1952 const char *descr = channel_describe_peer(
1953 TLS_CHAN_TO_BASE(chan));
1954 log_info(LD_OR,
1955 "We made a connection to a relay at %s (fp=%s) but we think "
1956 "they will not consider this connection canonical. They "
1957 "think we are at %s, but we think its %s.",
1958 safe_str(descr),
1959 safe_str(hex_str(identity_digest, DIGEST_LEN)),
1960 safe_str(tor_addr_is_null(&my_apparent_addr) ?
1961 "<none>" : fmt_and_decorate_addr(&my_apparent_addr)),
1962 safe_str(fmt_addr(&me->ipv4_addr)));
1963 }
1964
1965 /* Act on apparent skew. */
1966 /** Warn when we get a netinfo skew with at least this value. */
1967#define NETINFO_NOTICE_SKEW 3600
1968 if (time_abs(apparent_skew) > NETINFO_NOTICE_SKEW &&
1969 (started_here ||
1970 connection_or_digest_is_known_relay(chan->conn->identity_digest))) {
1971 int trusted = router_digest_is_trusted_dir(chan->conn->identity_digest);
1972 clock_skew_warning(TO_CONN(chan->conn), apparent_skew, trusted, LD_GENERAL,
1973 "NETINFO cell", "OR");
1974 }
1975
1976 /* Consider our apparent address as a possible suggestion for our address if
1977 * we were unable to resolve it previously. The endpoint address is passed
1978 * in order to make sure to never consider an address that is the same as
1979 * our endpoint. */
1980 relay_address_new_suggestion(&my_apparent_addr, &TO_CONN(chan->conn)->addr,
1981 identity_digest);
1982
1983 if (! chan->conn->handshake_state->sent_netinfo) {
1984 /* If we were prepared to authenticate, but we never got an AUTH_CHALLENGE
1985 * cell, then we would not previously have sent a NETINFO cell. Do so
1986 * now. */
1987 if (connection_or_send_netinfo(chan->conn) < 0) {
1988 connection_or_close_for_error(chan->conn, 0);
1989 return;
1990 }
1991 }
1992
1993 if (connection_or_set_state_open(chan->conn) < 0) {
1994 log_fn(LOG_PROTOCOL_WARN, LD_OR,
1995 "Got good NETINFO cell on %s; but "
1996 "was unable to make the OR connection become open.",
1997 connection_describe(TO_CONN(chan->conn)));
1998 connection_or_close_for_error(chan->conn, 0);
1999 } else {
2000 log_info(LD_OR,
2001 "Got good NETINFO cell on %s; OR connection is now "
2002 "open, using protocol version %d. Its ID digest is %s. "
2003 "Our address is apparently %s.",
2004 connection_describe(TO_CONN(chan->conn)),
2005 (int)(chan->conn->link_proto),
2006 hex_str(identity_digest, DIGEST_LEN),
2007 tor_addr_is_null(&my_apparent_addr) ?
2008 "<none>" :
2009 safe_str_client(fmt_and_decorate_addr(&my_apparent_addr)));
2010 }
2011 assert_connection_ok(TO_CONN(chan->conn),time(NULL));
2012}
2013
2014/** Types of certificates that we know how to parse from CERTS cells. Each
2015 * type corresponds to a different encoding format. */
2016typedef enum cert_encoding_t {
2017 CERT_ENCODING_UNKNOWN, /**< We don't recognize this. */
2018 CERT_ENCODING_X509, /**< It's an RSA key, signed with RSA, encoded in x509.
2019 * (Actually, it might not be RSA. We test that later.) */
2020 CERT_ENCODING_ED25519, /**< It's something signed with an Ed25519 key,
2021 * encoded asa a tor_cert_t.*/
2022 CERT_ENCODING_RSA_CROSSCERT, /**< It's an Ed key signed with an RSA key. */
2024
2025/**
2026 * Given one of the certificate type codes used in a CERTS cell,
2027 * return the corresponding cert_encoding_t that we should use to parse
2028 * the certificate.
2029 */
2030static cert_encoding_t
2032{
2033 switch (typenum) {
2034 case CERTTYPE_RSA1024_ID_LINK:
2035 case CERTTYPE_RSA1024_ID_ID:
2036 case CERTTYPE_RSA1024_ID_AUTH:
2037 return CERT_ENCODING_X509;
2038 case CERTTYPE_ED_ID_SIGN:
2039 case CERTTYPE_ED_SIGN_LINK:
2040 case CERTTYPE_ED_SIGN_AUTH:
2041 return CERT_ENCODING_ED25519;
2042 case CERTTYPE_RSA1024_ID_EDID:
2044 default:
2045 return CERT_ENCODING_UNKNOWN;
2046 }
2047}
2048
2049/**
2050 * Process a CERTS cell from a channel.
2051 *
2052 * This function is called to process an incoming CERTS cell on a
2053 * channel_tls_t:
2054 *
2055 * If the other side should not have sent us a CERTS cell, or the cell is
2056 * malformed, or it is supposed to authenticate the TLS key but it doesn't,
2057 * then mark the connection.
2058 *
2059 * If the cell has a good cert chain and we're doing a v3 handshake, then
2060 * store the certificates in or_handshake_state. If this is the client side
2061 * of the connection, we then authenticate the server or mark the connection.
2062 * If it's the server side, wait for an AUTHENTICATE cell.
2063 */
2064STATIC void
2066{
2067#define MAX_CERT_TYPE_WANTED CERTTYPE_RSA1024_ID_EDID
2068 /* These arrays will be sparse, since a cert type can be at most one
2069 * of ed/x509 */
2070 tor_x509_cert_t *x509_certs[MAX_CERT_TYPE_WANTED + 1];
2071 tor_cert_t *ed_certs[MAX_CERT_TYPE_WANTED + 1];
2072 uint8_t *rsa_ed_cc_cert = NULL;
2073 size_t rsa_ed_cc_cert_len = 0;
2074
2075 int n_certs, i;
2076 certs_cell_t *cc = NULL;
2077
2078 int send_netinfo = 0, started_here = 0;
2079
2080 memset(x509_certs, 0, sizeof(x509_certs));
2081 memset(ed_certs, 0, sizeof(ed_certs));
2082 tor_assert(cell);
2083 tor_assert(chan);
2084 tor_assert(chan->conn);
2085
2086#define ERR(s) \
2087 do { \
2088 log_fn(LOG_PROTOCOL_WARN, LD_PROTOCOL, \
2089 "Received a bad CERTS cell on %s: %s", \
2090 connection_describe(TO_CONN(chan->conn)), \
2091 (s)); \
2092 connection_or_close_for_error(chan->conn, 0); \
2093 goto err; \
2094 } while (0)
2095
2096 if (chan->conn->base_.state != OR_CONN_STATE_OR_HANDSHAKING_V3) {
2097 ERR("We're not doing a v3 handshake!");
2098 }
2099 if (BUG(!chan->conn->handshake_state)) {
2100 ERR("Handshake state not present.");
2101 }
2102
2103 /* Can't use connection_or_nonopen_was_started_here(); its conn->tls
2104 * check looks like it breaks
2105 * test_link_handshake_recv_certs_ok_server(). */
2106 started_here = chan->conn->handshake_state->started_here;
2107
2108 if (chan->conn->link_proto < 3)
2109 ERR("We're not using link protocol >= 3");
2110 if (chan->conn->handshake_state->received_certs_cell)
2111 ERR("We already got one");
2112 if (chan->conn->handshake_state->authenticated) {
2113 /* Should be unreachable, but let's make sure. */
2114 ERR("We're already authenticated!");
2115 }
2116 if (cell->payload_len < 1)
2117 ERR("It had no body");
2118 if (cell->circ_id)
2119 ERR("It had a nonzero circuit ID");
2120
2121 if (certs_cell_parse(&cc, cell->payload, cell->payload_len) < 0)
2122 ERR("It couldn't be parsed.");
2123
2124 n_certs = cc->n_certs;
2125
2126 for (i = 0; i < n_certs; ++i) {
2127 certs_cell_cert_t *c = certs_cell_get_certs(cc, i);
2128
2129 uint16_t cert_type = c->cert_type;
2130 uint16_t cert_len = c->cert_len;
2131 uint8_t *cert_body = certs_cell_cert_getarray_body(c);
2132
2133 if (cert_type > MAX_CERT_TYPE_WANTED)
2134 continue;
2136 switch (ct) {
2137 default:
2139 break;
2140 case CERT_ENCODING_X509: {
2141 tor_x509_cert_t *x509_cert = tor_x509_cert_decode(cert_body, cert_len);
2142 if (!x509_cert) {
2143 log_fn(LOG_PROTOCOL_WARN, LD_PROTOCOL,
2144 "Received undecodable certificate in CERTS cell on %s",
2145 connection_describe(TO_CONN(chan->conn)));
2146 } else {
2147 if (x509_certs[cert_type]) {
2148 tor_x509_cert_free(x509_cert);
2149 ERR("Duplicate x509 certificate");
2150 } else {
2151 x509_certs[cert_type] = x509_cert;
2152 }
2153 }
2154 break;
2155 }
2156 case CERT_ENCODING_ED25519: {
2157 tor_cert_t *ed_cert = tor_cert_parse(cert_body, cert_len);
2158 if (!ed_cert) {
2159 log_fn(LOG_PROTOCOL_WARN, LD_PROTOCOL,
2160 "Received undecodable Ed certificate "
2161 "in CERTS cell on %s",
2162 connection_describe(TO_CONN(chan->conn)));
2163 } else {
2164 if (ed_certs[cert_type]) {
2165 tor_cert_free(ed_cert);
2166 ERR("Duplicate Ed25519 certificate");
2167 } else {
2168 ed_certs[cert_type] = ed_cert;
2169 }
2170 }
2171 break;
2172 }
2173
2175 if (rsa_ed_cc_cert) {
2176 ERR("Duplicate RSA->Ed25519 crosscert");
2177 } else {
2178 rsa_ed_cc_cert = tor_memdup(cert_body, cert_len);
2179 rsa_ed_cc_cert_len = cert_len;
2180 }
2181 break;
2182 }
2183 }
2184 }
2185
2186 /* Move the certificates we (might) want into the handshake_state->certs
2187 * structure. */
2188 tor_x509_cert_t *id_cert = x509_certs[CERTTYPE_RSA1024_ID_ID];
2189 tor_x509_cert_t *auth_cert = x509_certs[CERTTYPE_RSA1024_ID_AUTH];
2190 tor_x509_cert_t *link_cert = x509_certs[CERTTYPE_RSA1024_ID_LINK];
2191 chan->conn->handshake_state->certs->auth_cert = auth_cert;
2192 chan->conn->handshake_state->certs->link_cert = link_cert;
2193 chan->conn->handshake_state->certs->id_cert = id_cert;
2194 x509_certs[CERTTYPE_RSA1024_ID_ID] =
2195 x509_certs[CERTTYPE_RSA1024_ID_AUTH] =
2196 x509_certs[CERTTYPE_RSA1024_ID_LINK] = NULL;
2197
2198 tor_cert_t *ed_id_sign = ed_certs[CERTTYPE_ED_ID_SIGN];
2199 tor_cert_t *ed_sign_link = ed_certs[CERTTYPE_ED_SIGN_LINK];
2200 tor_cert_t *ed_sign_auth = ed_certs[CERTTYPE_ED_SIGN_AUTH];
2201 chan->conn->handshake_state->certs->ed_id_sign = ed_id_sign;
2202 chan->conn->handshake_state->certs->ed_sign_link = ed_sign_link;
2203 chan->conn->handshake_state->certs->ed_sign_auth = ed_sign_auth;
2204 ed_certs[CERTTYPE_ED_ID_SIGN] =
2205 ed_certs[CERTTYPE_ED_SIGN_LINK] =
2206 ed_certs[CERTTYPE_ED_SIGN_AUTH] = NULL;
2207
2208 chan->conn->handshake_state->certs->ed_rsa_crosscert = rsa_ed_cc_cert;
2209 chan->conn->handshake_state->certs->ed_rsa_crosscert_len =
2210 rsa_ed_cc_cert_len;
2211 rsa_ed_cc_cert = NULL;
2212
2213 int severity;
2214 /* Note that this warns more loudly about time and validity if we were
2215 * _trying_ to connect to an authority, not necessarily if we _did_ connect
2216 * to one. */
2217 if (started_here &&
2218 router_digest_is_trusted_dir(TLS_CHAN_TO_BASE(chan)->identity_digest))
2219 severity = LOG_WARN;
2220 else
2221 severity = LOG_PROTOCOL_WARN;
2222
2223 const ed25519_public_key_t *checked_ed_id = NULL;
2224 const common_digests_t *checked_rsa_id = NULL;
2226 chan->conn->handshake_state->certs,
2227 chan->conn->tls,
2228 time(NULL),
2229 &checked_ed_id,
2230 &checked_rsa_id);
2231
2232 if (!checked_rsa_id)
2233 ERR("Invalid certificate chain!");
2234
2235 if (started_here) {
2236 /* No more information is needed. */
2237
2238 chan->conn->handshake_state->authenticated = 1;
2239 chan->conn->handshake_state->authenticated_rsa = 1;
2240 {
2241 const common_digests_t *id_digests = checked_rsa_id;
2242 crypto_pk_t *identity_rcvd;
2243 if (!id_digests)
2244 ERR("Couldn't compute digests for key in ID cert");
2245
2246 identity_rcvd = tor_tls_cert_get_key(id_cert);
2247 if (!identity_rcvd) {
2248 ERR("Couldn't get RSA key from ID cert.");
2249 }
2250 memcpy(chan->conn->handshake_state->authenticated_rsa_peer_id,
2251 id_digests->d[DIGEST_SHA1], DIGEST_LEN);
2252 channel_set_circid_type(TLS_CHAN_TO_BASE(chan), identity_rcvd,
2253 chan->conn->link_proto < MIN_LINK_PROTO_FOR_WIDE_CIRC_IDS);
2254 crypto_pk_free(identity_rcvd);
2255 }
2256
2257 if (checked_ed_id) {
2258 chan->conn->handshake_state->authenticated_ed25519 = 1;
2259 memcpy(&chan->conn->handshake_state->authenticated_ed25519_peer_id,
2260 checked_ed_id, sizeof(ed25519_public_key_t));
2261 }
2262
2263 log_debug(LD_HANDSHAKE, "calling client_learned_peer_id from "
2264 "process_certs_cell");
2265
2267 chan->conn->handshake_state->authenticated_rsa_peer_id,
2268 checked_ed_id) < 0)
2269 ERR("Problem setting or checking peer id");
2270
2271 log_info(LD_HANDSHAKE,
2272 "Got some good certificates on %s: Authenticated it with "
2273 "RSA%s",
2274 connection_describe(TO_CONN(chan->conn)),
2275 checked_ed_id ? " and Ed25519" : "");
2276
2277 if (!public_server_mode(get_options())) {
2278 /* If we initiated the connection and we are not a public server, we
2279 * aren't planning to authenticate at all. At this point we know who we
2280 * are talking to, so we can just send a netinfo now. */
2281 send_netinfo = 1;
2282 }
2283 } else {
2284 /* We can't call it authenticated till we see an AUTHENTICATE cell. */
2285 log_info(LD_OR,
2286 "Got some good RSA%s certificates on %s. "
2287 "Waiting for AUTHENTICATE.",
2288 checked_ed_id ? " and Ed25519" : "",
2289 connection_describe(TO_CONN(chan->conn)));
2290 /* XXXX check more stuff? */
2291 }
2292
2293 chan->conn->handshake_state->received_certs_cell = 1;
2294
2295 if (send_netinfo) {
2296 if (connection_or_send_netinfo(chan->conn) < 0) {
2297 log_warn(LD_OR, "Couldn't send netinfo cell");
2298 connection_or_close_for_error(chan->conn, 0);
2299 goto err;
2300 }
2301 }
2302
2303 err:
2304 for (unsigned u = 0; u < ARRAY_LENGTH(x509_certs); ++u) {
2305 tor_x509_cert_free(x509_certs[u]);
2306 }
2307 for (unsigned u = 0; u < ARRAY_LENGTH(ed_certs); ++u) {
2308 tor_cert_free(ed_certs[u]);
2309 }
2310 tor_free(rsa_ed_cc_cert);
2311 certs_cell_free(cc);
2312#undef ERR
2313}
2314
2315/**
2316 * Process an AUTH_CHALLENGE cell from a channel_tls_t.
2317 *
2318 * This function is called to handle an incoming AUTH_CHALLENGE cell on a
2319 * channel_tls_t; if we weren't supposed to get one (for example, because we're
2320 * not the originator of the channel), or it's ill-formed, or we aren't doing
2321 * a v3 handshake, mark the channel. If the cell is well-formed but we don't
2322 * want to authenticate, just drop it. If the cell is well-formed *and* we
2323 * want to authenticate, send an AUTHENTICATE cell and then a NETINFO cell.
2324 */
2325STATIC void
2327{
2328 int n_types, i, use_type = -1;
2329 auth_challenge_cell_t *ac = NULL;
2330
2331 tor_assert(cell);
2332 tor_assert(chan);
2333 tor_assert(chan->conn);
2334
2335#define ERR(s) \
2336 do { \
2337 log_fn(LOG_PROTOCOL_WARN, LD_PROTOCOL, \
2338 "Received a bad AUTH_CHALLENGE cell on %s: %s", \
2339 connection_describe(TO_CONN(chan->conn)), \
2340 (s)); \
2341 connection_or_close_for_error(chan->conn, 0); \
2342 goto done; \
2343 } while (0)
2344
2345 if (chan->conn->base_.state != OR_CONN_STATE_OR_HANDSHAKING_V3)
2346 ERR("We're not currently doing a v3 handshake");
2347 if (chan->conn->link_proto < 3)
2348 ERR("We're not using link protocol >= 3");
2349 if (BUG(!chan->conn->handshake_state))
2350 ERR("Handshake state not present.");
2351 if (!(chan->conn->handshake_state->started_here))
2352 ERR("We didn't originate this connection");
2353 if (chan->conn->handshake_state->received_auth_challenge)
2354 ERR("We already received one");
2355 if (!(chan->conn->handshake_state->received_certs_cell))
2356 ERR("We haven't gotten a CERTS cell yet");
2357 if (cell->circ_id)
2358 ERR("It had a nonzero circuit ID");
2359
2360 if (auth_challenge_cell_parse(&ac, cell->payload, cell->payload_len) < 0)
2361 ERR("It was not well-formed.");
2362
2363 n_types = ac->n_methods;
2364
2365 /* Now see if there is an authentication type we can use */
2366 for (i = 0; i < n_types; ++i) {
2367 uint16_t authtype = auth_challenge_cell_get_methods(ac, i);
2368 if (authchallenge_type_is_supported(authtype)) {
2369 if (use_type == -1 ||
2370 authchallenge_type_is_better(authtype, use_type)) {
2371 use_type = authtype;
2372 }
2373 }
2374 }
2375
2376 chan->conn->handshake_state->received_auth_challenge = 1;
2377
2378 if (! public_server_mode(get_options())) {
2379 /* If we're not a public server then we don't want to authenticate on a
2380 connection we originated, and we already sent a NETINFO cell when we
2381 got the CERTS cell. We have nothing more to do. */
2382 goto done;
2383 }
2384
2385 if (use_type >= 0) {
2386 log_info(LD_OR,
2387 "Got an AUTH_CHALLENGE cell on %s: Sending "
2388 "authentication type %d",
2389 connection_describe(TO_CONN(chan->conn)),
2390 use_type);
2391
2392 if (connection_or_send_authenticate_cell(chan->conn, use_type) < 0) {
2393 log_warn(LD_OR,
2394 "Couldn't send authenticate cell");
2395 connection_or_close_for_error(chan->conn, 0);
2396 goto done;
2397 }
2398 } else {
2399 log_info(LD_OR,
2400 "Got an AUTH_CHALLENGE cell on %s, but we don't "
2401 "know any of its authentication types. Not authenticating.",
2402 connection_describe(TO_CONN(chan->conn)));
2403 }
2404
2405 if (connection_or_send_netinfo(chan->conn) < 0) {
2406 log_warn(LD_OR, "Couldn't send netinfo cell");
2407 connection_or_close_for_error(chan->conn, 0);
2408 goto done;
2409 }
2410
2411 done:
2412 auth_challenge_cell_free(ac);
2413
2414#undef ERR
2415}
2416
2417/**
2418 * Process an AUTHENTICATE cell from a channel_tls_t.
2419 *
2420 * If it's ill-formed or we weren't supposed to get one or we're not doing a
2421 * v3 handshake, then mark the connection. If it does not authenticate the
2422 * other side of the connection successfully (because it isn't signed right,
2423 * we didn't get a CERTS cell, etc) mark the connection. Otherwise, accept
2424 * the identity of the router on the other side of the connection.
2425 */
2426STATIC void
2428{
2429 var_cell_t *expected_cell = NULL;
2430 const uint8_t *auth;
2431 int authlen;
2432 int authtype;
2433 int bodylen;
2434
2435 tor_assert(cell);
2436 tor_assert(chan);
2437 tor_assert(chan->conn);
2438
2439#define ERR(s) \
2440 do { \
2441 log_fn(LOG_PROTOCOL_WARN, LD_PROTOCOL, \
2442 "Received a bad AUTHENTICATE cell on %s: %s", \
2443 connection_describe(TO_CONN(chan->conn)), \
2444 (s)); \
2445 connection_or_close_for_error(chan->conn, 0); \
2446 var_cell_free(expected_cell); \
2447 return; \
2448 } while (0)
2449
2450 if (chan->conn->base_.state != OR_CONN_STATE_OR_HANDSHAKING_V3)
2451 ERR("We're not doing a v3 handshake");
2452 if (chan->conn->link_proto < 3)
2453 ERR("We're not using link protocol >= 3");
2454 if (BUG(!chan->conn->handshake_state))
2455 ERR("Handshake state not present.");
2456 if (chan->conn->handshake_state->started_here)
2457 ERR("We originated this connection");
2458 if (chan->conn->handshake_state->received_authenticate)
2459 ERR("We already got one!");
2460 if (chan->conn->handshake_state->authenticated) {
2461 /* Should be impossible given other checks */
2462 ERR("The peer is already authenticated");
2463 }
2464 if (!(chan->conn->handshake_state->received_certs_cell))
2465 ERR("We never got a certs cell");
2466 if (chan->conn->handshake_state->certs->id_cert == NULL)
2467 ERR("We never got an identity certificate");
2468 if (cell->payload_len < 4)
2469 ERR("Cell was way too short");
2470
2471 auth = cell->payload;
2472 {
2473 uint16_t type = ntohs(get_uint16(auth));
2474 uint16_t len = ntohs(get_uint16(auth+2));
2475 if (4 + len > cell->payload_len)
2476 ERR("Authenticator was truncated");
2477
2478 if (! authchallenge_type_is_supported(type))
2479 ERR("Authenticator type was not recognized");
2480 authtype = type;
2481
2482 auth += 4;
2483 authlen = len;
2484 }
2485
2486 if (authlen < V3_AUTH_BODY_LEN + 1)
2487 ERR("Authenticator was too short");
2488
2490 chan->conn, authtype, NULL, 1);
2491 if (! expected_cell)
2492 ERR("Couldn't compute expected AUTHENTICATE cell body");
2493
2494 if (BUG(authtype != AUTHTYPE_ED25519_SHA256_RFC5705)) {
2495 /* We should have detected that we don't support this
2496 * authentication type earlier, when we called
2497 * authchallenge_type_is_supported(). */
2498 ERR("Unsupported authentication type");
2499 } else {
2500 /* Our earlier check had better have made sure we had room
2501 * for an ed25519 sig (inadvertently) */
2503 bodylen = authlen - ED25519_SIG_LEN;
2504 }
2505 if (expected_cell->payload_len != bodylen+4) {
2506 ERR("Expected AUTHENTICATE cell body len not as expected.");
2507 }
2508
2509 /* Length of random part. */
2510 if (BUG(bodylen < 24)) {
2511 // LCOV_EXCL_START
2512 ERR("Bodylen is somehow less than 24, which should really be impossible");
2513 // LCOV_EXCL_STOP
2514 }
2515
2516 if (tor_memneq(expected_cell->payload+4, auth, bodylen-24))
2517 ERR("Some field in the AUTHENTICATE cell body was not as expected");
2518
2519 {
2520 if (chan->conn->handshake_state->certs->ed_id_sign == NULL)
2521 ERR("We never got an Ed25519 identity certificate.");
2522 if (chan->conn->handshake_state->certs->ed_sign_auth == NULL)
2523 ERR("We never got an Ed25519 authentication certificate.");
2524
2525 const ed25519_public_key_t *authkey =
2526 &chan->conn->handshake_state->certs->ed_sign_auth->signed_key;
2528 tor_assert(authlen > ED25519_SIG_LEN);
2529 memcpy(&sig.sig, auth + authlen - ED25519_SIG_LEN, ED25519_SIG_LEN);
2530 if (ed25519_checksig(&sig, auth, authlen - ED25519_SIG_LEN, authkey)<0) {
2531 ERR("Ed25519 signature wasn't valid.");
2532 }
2533 }
2534
2535 /* Okay, we are authenticated. */
2536 chan->conn->handshake_state->received_authenticate = 1;
2537 chan->conn->handshake_state->authenticated = 1;
2538 chan->conn->handshake_state->authenticated_rsa = 1;
2539 chan->conn->handshake_state->digest_received_data = 0;
2540 {
2541 tor_x509_cert_t *id_cert = chan->conn->handshake_state->certs->id_cert;
2542 crypto_pk_t *identity_rcvd = tor_tls_cert_get_key(id_cert);
2543 const common_digests_t *id_digests = tor_x509_cert_get_id_digests(id_cert);
2544 const ed25519_public_key_t *ed_identity_received = NULL;
2545
2546 {
2547 chan->conn->handshake_state->authenticated_ed25519 = 1;
2548 ed_identity_received =
2549 &chan->conn->handshake_state->certs->ed_id_sign->signing_key;
2550 memcpy(&chan->conn->handshake_state->authenticated_ed25519_peer_id,
2551 ed_identity_received, sizeof(ed25519_public_key_t));
2552 }
2553
2554 /* This must exist; we checked key type when reading the cert. */
2555 tor_assert(id_digests);
2556
2557 memcpy(chan->conn->handshake_state->authenticated_rsa_peer_id,
2558 id_digests->d[DIGEST_SHA1], DIGEST_LEN);
2559
2560 channel_set_circid_type(TLS_CHAN_TO_BASE(chan), identity_rcvd,
2561 chan->conn->link_proto < MIN_LINK_PROTO_FOR_WIDE_CIRC_IDS);
2562 crypto_pk_free(identity_rcvd);
2563
2564 log_debug(LD_HANDSHAKE,
2565 "Calling connection_or_init_conn_from_address on %s "
2566 " from %s, with%s ed25519 id.",
2567 connection_describe(TO_CONN(chan->conn)),
2568 __func__,
2569 ed_identity_received ? "" : "out");
2570
2572 &(chan->conn->base_.addr),
2573 chan->conn->base_.port,
2574 (const char*)(chan->conn->handshake_state->
2575 authenticated_rsa_peer_id),
2576 ed_identity_received,
2577 0);
2578
2579 log_debug(LD_HANDSHAKE,
2580 "Got an AUTHENTICATE cell on %s, type %d: Looks good.",
2581 connection_describe(TO_CONN(chan->conn)),
2582 authtype);
2583 }
2584
2585 var_cell_free(expected_cell);
2586
2587#undef ERR
2588}
void tor_addr_copy(tor_addr_t *dest, const tor_addr_t *src)
Definition address.c:933
void tor_addr_make_unspec(tor_addr_t *a)
Definition address.c:225
int tor_addr_is_null(const tor_addr_t *addr)
Definition address.c:780
void tor_addr_from_ipv6_bytes(tor_addr_t *dest, const uint8_t *ipv6_bytes)
Definition address.c:900
#define fmt_and_decorate_addr(a)
Definition address.h:245
#define tor_addr_from_ipv4h(dest, v4addr)
Definition address.h:329
#define fmt_addr(a)
Definition address.h:241
#define tor_addr_eq(a, b)
Definition address.h:282
static bool tor_addr_is_unspec(const tor_addr_t *a)
Definition address.h:198
const char * hex_str(const char *from, size_t fromlen)
Definition binascii.c:34
static uint16_t get_uint16(const void *cp)
Definition bytes.h:42
Cell queue structures.
Fixed-size cell structure.
void channel_set_circid_type(channel_t *chan, crypto_pk_t *identity_rcvd, int consider_identity)
Definition channel.c:3427
void channel_listener_unregister(channel_listener_t *chan_l)
Definition channel.c:526
void channel_mark_local(channel_t *chan)
Definition channel.c:3088
void channel_mark_client(channel_t *chan)
Definition channel.c:3000
void channel_mark_incoming(channel_t *chan)
Definition channel.c:3056
void channel_init_listener(channel_listener_t *chan_l)
Definition channel.c:892
void channel_listener_mark_for_close(channel_listener_t *chan_l)
Definition channel.c:1189
void channel_process_cell(channel_t *chan, cell_t *cell)
Definition channel.c:2045
void channel_change_state_open(channel_t *chan)
Definition channel.c:1690
int channel_is_canonical(channel_t *chan)
Definition channel.c:3027
const char * channel_state_to_string(channel_state_t state)
Definition channel.c:317
void channel_register(channel_t *chan)
Definition channel.c:388
void channel_listener_register(channel_listener_t *chan_l)
Definition channel.c:485
void channel_mark_remote(channel_t *chan)
Definition channel.c:3104
int channel_is_local(channel_t *chan)
Definition channel.c:3073
const char * channel_describe_peer(channel_t *chan)
Definition channel.c:2909
void channel_mark_for_close(channel_t *chan)
Definition channel.c:1150
void channel_listener_change_state(channel_listener_t *chan_l, channel_listener_state_t to_state)
Definition channel.c:1707
void channel_mark_outgoing(channel_t *chan)
Definition channel.c:3133
void channel_init(channel_t *chan)
Definition channel.c:853
void channel_note_establishment_cancelled(channel_t *chan)
Definition channel.c:1290
void channel_change_state(channel_t *chan, channel_state_t to_state)
Definition channel.c:1680
Header file for channel.c.
@ CHANNEL_STATE_MAINT
Definition channel.h:94
@ CHANNEL_STATE_OPENING
Definition channel.h:70
@ CHANNEL_STATE_ERROR
Definition channel.h:118
@ CHANNEL_LISTENER_STATE_ERROR
Definition channel.h:167
@ CHANNEL_LISTENER_STATE_LISTENING
Definition channel.h:147
@ CHANNEL_LISTENER_STATE_CLOSING
Definition channel.h:157
@ CHANNEL_LISTENER_STATE_CLOSED
Definition channel.h:136
int channelpadding_update_padding_for_channel(channel_t *chan, const channelpadding_negotiate_t *pad_vars)
static int channel_tls_get_remote_addr_method(const channel_t *chan, tor_addr_t *addr_out)
Definition channeltls.c:607
void channel_tls_free_all(void)
Definition channeltls.c:327
void channel_tls_handle_var_cell(var_cell_t *var_cell, or_connection_t *conn)
static channel_listener_t * channel_tls_listener
Definition channeltls.c:101
STATIC void channel_tls_process_authenticate_cell(var_cell_t *cell, channel_tls_t *chan)
static void channel_tls_listener_close_method(channel_listener_t *chan_l)
Definition channeltls.c:949
void channel_mark_as_used_for_origin_circuit(channel_t *chan)
Definition channeltls.c:418
static int tor_addr_from_netinfo_addr(tor_addr_t *tor_addr, const netinfo_addr_t *netinfo_addr)
static const char * channel_tls_describe_transport_method(channel_t *chan)
Definition channeltls.c:513
channel_listener_t * channel_tls_start_listener(void)
Definition channeltls.c:296
static int channel_tls_write_var_cell_method(channel_t *chan, var_cell_t *var_cell)
Definition channeltls.c:918
static int channel_tls_has_queued_writes_method(channel_t *chan)
Definition channeltls.c:676
static void mark_channel_tls_endpoint_as_client(channel_tls_t *chan)
static double channel_tls_get_overhead_estimate_method(channel_t *chan)
Definition channeltls.c:567
void channel_tls_handle_state_change_on_orconn(channel_tls_t *chan, or_connection_t *conn, uint8_t state)
void channel_tls_handle_cell(cell_t *cell, or_connection_t *conn)
static int command_allowed_before_handshake(uint8_t command)
static bool can_process_netinfo_cell(const channel_tls_t *chan)
uint64_t stats_n_padding_cells_processed
Definition channeltls.c:84
channel_listener_t * channel_tls_get_listener(void)
Definition channeltls.c:284
cert_encoding_t
@ CERT_ENCODING_ED25519
@ CERT_ENCODING_UNKNOWN
@ CERT_ENCODING_X509
@ CERT_ENCODING_RSA_CROSSCERT
const channel_tls_t * channel_tls_from_base_const(const channel_t *chan)
Definition channeltls.c:475
channel_tls_t * channel_tls_from_base(channel_t *chan)
Definition channeltls.c:452
uint64_t stats_n_certs_cells_processed
Definition channeltls.c:92
static int channel_tls_write_cell_method(channel_t *chan, cell_t *cell)
Definition channeltls.c:856
STATIC void channel_tls_process_auth_challenge_cell(var_cell_t *cell, channel_tls_t *chan)
static void channel_tls_close_method(channel_t *chan)
Definition channeltls.c:490
static int channel_tls_num_cells_writeable_method(channel_t *chan)
Definition channeltls.c:827
static void channel_tls_process_padding_negotiate_cell(cell_t *cell, channel_tls_t *chan)
STATIC void channel_tls_process_certs_cell(var_cell_t *cell, channel_tls_t *chan)
channel_t * channel_tls_connect(const tor_addr_t *addr, uint16_t port, const char *id_digest, const ed25519_public_key_t *ed_id, const struct circuit_guard_state_t *guard_state, bool for_origin_circ)
Definition channeltls.c:194
uint64_t stats_n_auth_challenge_cells_processed
Definition channeltls.c:94
void channel_tls_update_marks(or_connection_t *conn)
uint64_t stats_n_authorize_cells_processed
Definition channeltls.c:98
uint64_t stats_n_versions_cells_processed
Definition channeltls.c:86
static int enter_v3_handshake_with_cell(var_cell_t *cell, channel_tls_t *tlschan)
const channel_t * channel_tls_to_base_const(const channel_tls_t *tlschan)
Definition channeltls.c:465
uint64_t stats_n_authenticate_cells_processed
Definition channeltls.c:96
static size_t channel_tls_num_bytes_queued_method(channel_t *chan)
Definition channeltls.c:809
static const char * channel_tls_describe_peer_method(const channel_t *chan)
Definition channeltls.c:657
channel_t * channel_tls_handle_incoming(or_connection_t *orconn)
Definition channeltls.c:359
static void channel_tls_free_method(channel_t *chan)
Definition channeltls.c:551
static cert_encoding_t certs_cell_typenum_to_cert_type(int typenum)
uint64_t stats_n_vpadding_cells_processed
Definition channeltls.c:90
static int channel_tls_is_canonical_method(channel_t *chan)
Definition channeltls.c:703
static int channel_tls_matches_extend_info_method(channel_t *chan, extend_info_t *extend_info)
Definition channeltls.c:732
static void channel_tls_process_netinfo_cell(cell_t *cell, channel_tls_t *tlschan)
channel_t * channel_tls_to_base(channel_tls_t *tlschan)
Definition channeltls.c:440
static int channel_tls_write_packed_cell_method(channel_t *chan, packed_cell_t *packed_cell)
Definition channeltls.c:887
static int channel_tls_get_transport_name_method(channel_t *chan, char **transport_out)
Definition channeltls.c:635
static const char * channel_tls_listener_describe_transport_method(channel_listener_t *chan_l)
Definition channeltls.c:989
static time_t time_abs(time_t val)
static void channel_tls_process_versions_cell(var_cell_t *cell, channel_tls_t *tlschan)
STATIC void channel_tls_common_init(channel_tls_t *tlschan)
Definition channeltls.c:154
static int channel_tls_matches_target_method(channel_t *chan, const tor_addr_t *target)
Definition channeltls.c:772
uint64_t stats_n_netinfo_cells_processed
Definition channeltls.c:88
Header file for channeltls.c.
void circuitmux_set_policy(circuitmux_t *cmux, const circuitmux_policy_t *pol)
Definition circuitmux.c:428
circuitmux_t * circuitmux_alloc(void)
Definition circuitmux.c:194
Header file for circuitmux.c.
Header file for circuitmux_ewma.c.
Header file for command.c.
#define ARRAY_LENGTH(x)
const or_options_t * get_options(void)
Definition config.c:949
tor_cmdline_mode_t command
Definition config.c:2481
Header file for config.c.
Public APIs for congestion control.
static uint32_t or_conn_highwatermark(void)
void clock_skew_warning(const connection_t *conn, long apparent_skew, int trusted, log_domain_mask_t domain, const char *received, const char *source)
void assert_connection_ok(connection_t *conn, time_t now)
const char * connection_describe_peer(const connection_t *conn)
Definition connection.c:523
const char * connection_describe(const connection_t *conn)
Definition connection.c:538
const char * conn_state_to_string(int type, int state)
Definition connection.c:301
Header file for connection.c.
#define CONN_TYPE_OR
Definition connection.h:44
int connection_or_nonopen_was_started_here(struct or_connection_t *conn)
int is_or_protocol_version_known(uint16_t v)
int connection_or_set_state_open(or_connection_t *conn)
void connection_or_write_cell_to_buf(const cell_t *cell, or_connection_t *conn)
int connection_or_send_versions(or_connection_t *conn, int v3_plus)
int connection_or_client_learned_peer_id(or_connection_t *conn, const uint8_t *rsa_peer_id, const ed25519_public_key_t *ed_peer_id)
void or_handshake_state_record_var_cell(or_connection_t *conn, or_handshake_state_t *state, const var_cell_t *cell, int incoming)
int connection_or_send_netinfo(or_connection_t *conn)
void connection_or_change_state(or_connection_t *conn, uint8_t state)
void or_handshake_state_record_cell(or_connection_t *conn, or_handshake_state_t *state, const cell_t *cell, int incoming)
int connection_or_digest_is_known_relay(const char *id_digest)
void connection_or_init_conn_from_address(or_connection_t *conn, const tor_addr_t *addr, uint16_t port, const char *id_digest, const ed25519_public_key_t *ed_id, int started_here)
void connection_or_close_for_error(or_connection_t *orconn, int flush)
or_connection_t * connection_or_connect(const tor_addr_t *_addr, uint16_t port, const char *id_digest, const ed25519_public_key_t *ed_id, channel_tls_t *chan, bool for_origin_circ)
ssize_t connection_or_num_cells_writeable(or_connection_t *conn)
int connection_init_or_handshake_state(or_connection_t *conn, int started_here)
void connection_or_close_normally(or_connection_t *orconn, int flush)
void connection_or_write_var_cell_to_buf(const var_cell_t *cell, or_connection_t *conn)
Header file for connection_or.c.
Header file for control.c.
int ed25519_checksig(const ed25519_signature_t *signature, const uint8_t *msg, size_t len, const ed25519_public_key_t *pubkey)
#define tor_memneq(a, b, sz)
Definition di_ops.h:21
#define DIGEST_LEN
Header file for dirlist.c.
void entry_guards_note_internet_connectivity(guard_selection_t *gs)
struct entry_guard_handle_t * entry_guard_handle_from_state(const circuit_guard_state_t *state)
guard_selection_t * get_guard_selection_info(void)
Definition entrynodes.c:313
Header file for circuitbuild.c.
bool extend_info_has_orport(const extend_info_t *ei, const tor_addr_t *addr, uint16_t port)
Definition extendinfo.c:250
Header for core/or/extendinfo.c.
Header file for geoip_stats.c.
@ GEOIP_CLIENT_CONNECT
Definition geoip_stats.h:24
#define log_fn(severity, domain, args,...)
Definition log.h:283
#define LD_PROTOCOL
Definition log.h:72
#define LD_CHANNEL
Definition log.h:105
#define LD_OR
Definition log.h:92
#define LD_HANDSHAKE
Definition log.h:101
#define LD_GENERAL
Definition log.h:62
#define LOG_WARN
Definition log.h:53
#define LOG_INFO
Definition log.h:45
static time_t current_second
Definition mainloop.c:2229
#define tor_free(p)
Definition malloc.h:56
Header file for networkstatus.c.
Master header file for Tor-specific functionality.
#define V3_AUTH_BODY_LEN
Definition or.h:692
#define CELL_PAYLOAD_SIZE
Definition or.h:529
#define TO_CONN(c)
Definition or.h:709
#define AUTHTYPE_ED25519_SHA256_RFC5705
Definition or.h:677
OR connection structure.
OR handshake certs structure.
OR handshake state structure.
#define OR_CONN_STATE_SERVER_VERSIONS_WAIT
#define OR_CONN_STATE_TLS_HANDSHAKING
#define OR_CONN_STATE_OR_HANDSHAKING_V3
#define OR_CONN_STATE_OPEN
int tor_asprintf(char **strp, const char *fmt,...)
Definition printf.c:75
Header file for relay.c.
void relay_address_new_suggestion(const tor_addr_t *suggested_addr, const tor_addr_t *peer_addr, const char *identity_digest)
Header file for relay_find_addr.c.
int connection_or_send_certs_cell(or_connection_t *conn)
var_cell_t * connection_or_compute_authenticate_cell_body(or_connection_t *conn, const int authtype, const ed25519_keypair_t *ed_signing_key, int server)
int connection_or_send_authenticate_cell(or_connection_t *conn, int authtype)
int connection_or_send_auth_challenge_cell(or_connection_t *conn)
Header for feature/relay/relay_handshake.c.
void rep_hist_note_negotiated_link_proto(unsigned link_proto, int started_here)
Definition rephist.c:2786
void rep_hist_padding_count_read(padding_type_t type)
Definition rephist.c:2846
Header file for rephist.c.
@ PADDING_TYPE_ENABLED_CELL
Definition rephist.h:158
@ PADDING_TYPE_TOTAL
Definition rephist.h:154
@ PADDING_TYPE_ENABLED_TOTAL
Definition rephist.h:156
@ PADDING_TYPE_CELL
Definition rephist.h:152
bool is_local_to_resolve_addr(const tor_addr_t *addr)
: Return true iff the given addr is judged to be local to our resolved address.
Header file for resolve_addr.c.
const routerinfo_t * router_get_my_routerinfo(void)
Definition router.c:1817
Header file for router.c.
Router descriptor structure.
Header file for routermode.c.
void scheduler_channel_wants_writes(channel_t *chan)
Definition scheduler.c:673
Header file for scheduler*.c.
#define SMARTLIST_FOREACH_BEGIN(sl, type, var)
uint8_t payload[CELL_PAYLOAD_SIZE]
Definition cell_st.h:21
uint8_t command
Definition cell_st.h:19
channel_listener_state_t state
Definition channel.h:469
const char *(* describe_transport)(channel_listener_t *)
Definition channel.h:499
uint64_t global_identifier
Definition channel.h:474
smartlist_t * incoming_list
Definition channel.h:507
void(* close)(channel_listener_t *)
Definition channel.h:497
void(* free_fn)(channel_t *)
Definition channel.h:317
channel_state_t state
Definition channel.h:193
int(* is_canonical)(channel_t *)
Definition channel.h:354
void(* close)(channel_t *)
Definition channel.h:319
enum channel_t::@9 reason_for_closing
int(* matches_extend_info)(channel_t *, extend_info_t *)
Definition channel.h:356
struct entry_guard_handle_t * establishment_guard
Definition channel.h:433
uint32_t magic
Definition channel.h:184
uint64_t global_identifier
Definition channel.h:198
int(* write_packed_cell)(channel_t *, packed_cell_t *)
Definition channel.h:366
const char *(* describe_transport)(channel_t *)
Definition channel.h:321
double(* get_overhead_estimate)(channel_t *)
Definition channel.h:334
int(* matches_target)(channel_t *, const tor_addr_t *)
Definition channel.h:358
const char *(* describe_peer)(const channel_t *)
Definition channel.h:347
int(* write_var_cell)(channel_t *, var_cell_t *)
Definition channel.h:368
circuitmux_t * cmux
Definition channel.h:398
int(* has_queued_writes)(channel_t *)
Definition channel.h:349
char d[N_COMMON_DIGEST_ALGORITHMS][DIGEST256_LEN]
uint16_t marked_for_close
channel_tls_t * chan
or_handshake_state_t * handshake_state
char body[CELL_MAX_NETWORK_SIZE]
tor_addr_t ipv6_addr
tor_addr_t ipv4_addr
uint8_t command
Definition var_cell_st.h:18
uint16_t payload_len
Definition var_cell_st.h:22
circid_t circ_id
Definition var_cell_st.h:20
uint8_t payload[FLEXIBLE_ARRAY_MEMBER]
Definition var_cell_st.h:24
#define STATIC
Definition testsupport.h:32
void tor_gettimeofday(struct timeval *timeval)
void or_handshake_certs_check_both(int severity, or_handshake_certs_t *certs, tor_tls_t *tls, time_t now, const ed25519_public_key_t **ed_id_out, const common_digests_t **rsa_id_out)
Definition torcert.c:685
tor_cert_t * tor_cert_parse(const uint8_t *encoded, const size_t len)
Definition torcert.c:159
Header for torcert.c.
Headers for tortls.c.
long tv_udiff(const struct timeval *start, const struct timeval *end)
Definition tvdiff.c:53
#define FALLTHROUGH_UNLESS_ALL_BUGS_ARE_FATAL
Definition util_bug.h:268
#define tor_assert(expr)
Definition util_bug.h:103
int fast_mem_is_zero(const char *mem, size_t len)
Definition util_string.c:76
int tor_digest_is_zero(const char *digest)
Definition util_string.c:98
Variable-length cell structure.
#define ED25519_SIG_LEN
Headers for tortls.c.
crypto_pk_t * tor_tls_cert_get_key(tor_x509_cert_t *cert)
Definition x509_nss.c:287
tor_x509_cert_t * tor_x509_cert_decode(const uint8_t *certificate, size_t certificate_len)
Definition x509_nss.c:271