Skip to content

What a Tor Circuit Actually Carries

A courier pulls a sealed red envelope from an open timber carrier.

Open https://example.com through Tor. The browser gives the Tor client the site’s hostname and asks for a connection. The client already has a circuit through a guard, a middle and an exit. The exit will open the connection to the website. What crosses the circuit first?

The client assigns an identifier to this stream, one logical flow of bytes through the circuit. It sends the exit a RELAY_BEGIN message with that identifier, the hostname and the port. The exit resolves the hostname, opens a Transmission Control Protocol (TCP) connection and reports success with RELAY_CONNECTED. The stream identifier lets the exit associate later messages with that connection. Opening streams

The first and last arrows in the figure cross the circuit through the guard and middle. The connection in between belongs to the exit and the site.

The Tor client sends RELAY_BEGIN. The exit resolves the hostname, opens a TCP connection to the website and reports success with RELAY_CONNECTED.

One channel can carry several circuits

Tor clients and relays exchange messages called cells over channels, the connections between neighbors. A channel can carry several circuits. One circuit, in turn, can carry multiple eligible streams.

IdentifierScopeWhat it distinguishes
Circuit identifier (CircID)One channelCells for different circuits on the same connection. The same circuit can have a different identifier on its next channel.
Stream identifier (StreamID)One circuitRelay messages for different streams in that circuit. Stream identifiers are nonzero.

Imagine two separate browser connections that Tor can put on this circuit. Stream 11 could carry one connection and stream 27 the other. On the guard-to-middle link, both use that channel’s circuit identifier. At the exit, their different stream identifiers let it send each connection’s bytes to the right place.

A fixed-length cell has a 509-byte body. With a four-byte circuit identifier and a one-byte command, that comes to 514 bytes on link protocol version 4 and later, before the channel’s Transport Layer Security (TLS) framing. Tor also has variable-length cells. In the usual relay cell format, the body includes the relay message header, its data and any padding. A cell is a unit of Tor framing and can cross TLS record boundaries. Cell format , Relay cell format

The two directions use different keys

For a relay cell destined for the exit, the client wraps the body using the circuit keys it shares with the exit, the middle and the guard, in that order. Each relay removes the layer it can open. The exit recognizes the relay message and handles it.

On the way back, the website sends bytes to the exit. The exit packages them into relay cells and applies its return-direction layer. The middle and guard each add theirs. The client removes all three using the keys for that direction. Routing relay cells

Outbound cells lose the guard, middle and exit layers as they travel toward the exit. In the opposite direction, the exit, middle and guard add layers for the client to remove.

Each stack shows the layers around one relay-cell body on that link. Read the top row from left to right and the bottom row from right to left. The same layer owners appear on each link, but the relays remove layers in one direction and add them in the other.

The browser starts TLS after the exit connects

In this example, the browser starts its TLS session with the website after the exit confirms the stream. RELAY_DATA carries chunks of the stream’s bytes. These chunks can hold part of a TLS handshake, part of a request or part of a response. A SENDME acknowledgement helps regulate how much data can be in flight. The details depend on which flow or congestion control applies. Congestion control , Legacy flow control

For a Hypertext Transfer Protocol Secure (HTTPS) site, the browser and website are the endpoints of that inner TLS session. Tor’s circuit protection ends at the exit. The exit already knows the hostname from RELAY_BEGIN. The browser’s request and the site’s response travel inside their TLS session. If the browser uses plain Hypertext Transfer Protocol (HTTP), the exit can read the unencrypted traffic. HTTPS encryption and Tor

An onion service keeps both ends inside Tor. Its client first needs a way to find the service without its network address .