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