Merge branch 'newcrypto'
[torspec/neena.git] / proposals / 100-tor-spec-udp.txt
blob7f062222c5d6fd0417165c78ba86734ed7fb3e47
1 Filename: 100-tor-spec-udp.txt
2 Title: Tor Unreliable Datagram Extension Proposal
3 Author: Marc Liberatore
4 Created: 23 Feb 2006
5 Status: Dead
7 Overview:
9    This is a modified version of the Tor specification written by Marc
10    Liberatore to add UDP support to Tor.  For each TLS link, it adds a
11    corresponding DTLS link: control messages and TCP data flow over TLS, and
12    UDP data flows over DTLS.
14    This proposal is not likely to be accepted as-is; see comments at the end
15    of the document.
18 Contents
20 0. Introduction
22   Tor is a distributed overlay network designed to anonymize low-latency
23   TCP-based applications.  The current tor specification supports only
24   TCP-based traffic.  This limitation prevents the use of tor to anonymize
25   other important applications, notably voice over IP software.  This document
26   is a proposal to extend the tor specification to support UDP traffic.
28   The basic design philosophy of this extension is to add support for
29   tunneling unreliable datagrams through tor with as few modifications to the
30   protocol as possible.  As currently specified, tor cannot directly support
31   such tunneling, as connections between nodes are built using transport layer
32   security (TLS) atop TCP.  The latency incurred by TCP is likely unacceptable
33   to the operation of most UDP-based application level protocols.
35   Thus, we propose the addition of links between nodes using datagram
36   transport layer security (DTLS).  These links allow packets to traverse a
37   route through tor quickly, but their unreliable nature requires minor
38   changes to the tor protocol.  This proposal outlines the necessary
39   additions and changes to the tor specification to support UDP traffic.
41   We note that a separate set of DTLS links between nodes creates a second
42   overlay, distinct from the that composed of TLS links.  This separation and
43   resulting decrease in each anonymity set's size will make certain attacks
44   easier.  However, it is our belief that VoIP support in tor will
45   dramatically increase its appeal, and correspondingly, the size of its user
46   base, number of deployed nodes, and total traffic relayed.  These increases
47   should help offset the loss of anonymity that two distinct networks imply.
49 1. Overview of Tor-UDP and its complications
51   As described above, this proposal extends the Tor specification to support
52   UDP with as few changes as possible.  Tor's overlay network is managed
53   through TLS based connections; we will re-use this control plane to set up
54   and tear down circuits that relay UDP traffic.  These circuits be built atop
55   DTLS, in a fashion analogous to how Tor currently sends TCP traffic over
56   TLS.
58   The unreliability of DTLS circuits creates problems for Tor at two levels:
60       1. Tor's encryption of the relay layer does not allow independent
61       decryption of individual records. If record N is not received, then
62       record N+1 will not decrypt correctly, as the counter for AES/CTR is
63       maintained implicitly.
65       2. Tor's end-to-end integrity checking works under the assumption that
66       all RELAY cells are delivered.  This assumption is invalid when cells
67       are sent over DTLS.
69   The fix for the first problem is straightforward: add an explicit sequence
70   number to each cell.  To fix the second problem, we introduce a
71   system of nonces and hashes to RELAY packets.
73   In the following sections, we mirror the layout of the Tor Protocol
74   Specification, presenting the necessary modifications to the Tor protocol as
75   a series of deltas.
77 2. Connections
79   Tor-UDP uses DTLS for encryption of some links.  All DTLS links must have
80   corresponding TLS links, as all control messages are sent over TLS.  All
81   implementations MUST support the DTLS ciphersuite "[TODO]".
83   DTLS connections are formed using the same protocol as TLS connections.
84   This occurs upon request, following a CREATE_UDP or CREATE_FAST_UDP cell,
85   as detailed in section 4.6.
87   Once a paired TLS/DTLS connection is established, the two sides send cells
88   to one another.  All but two types of cells are sent over TLS links.  RELAY
89   cells containing the commands RELAY_UDP_DATA and RELAY_UDP_DROP, specified
90   below, are sent over DTLS links.  [Should all cells still be 512 bytes long?
91   Perhaps upon completion of a preliminary implementation, we should do a
92   performance evaluation for some class of UDP traffic, such as VoIP. - ML]
93   Cells may be sent embedded in TLS or DTLS records of any size or divided
94   across such records.  The framing of these records MUST NOT leak any more
95   information than the above differentiation on the basis of cell type.  [I am
96   uncomfortable with this leakage, but don't see any simple, elegant way
97   around it. -ML]
99   As with TLS connections, DTLS connections are not permanent.
101 3. Cell format
103   Each cell contains the following fields:
105         CircID                                [2 bytes]
106         Command                               [1 byte]
107         Sequence Number                       [2 bytes]
108         Payload (padded with 0 bytes)         [507 bytes]
109                                          [Total size: 512 bytes]
111   The 'Command' field holds one of the following values:
112        0 -- PADDING         (Padding)                     (See Sec 6.2)
113        1 -- CREATE          (Create a circuit)            (See Sec 4)
114        2 -- CREATED         (Acknowledge create)          (See Sec 4)
115        3 -- RELAY           (End-to-end data)             (See Sec 5)
116        4 -- DESTROY         (Stop using a circuit)        (See Sec 4)
117        5 -- CREATE_FAST     (Create a circuit, no PK)     (See Sec 4)
118        6 -- CREATED_FAST    (Circuit created, no PK)      (See Sec 4)
119        7 -- CREATE_UDP      (Create a UDP circuit)        (See Sec 4)
120        8 -- CREATED_UDP     (Acknowledge UDP create)      (See Sec 4)
121        9 -- CREATE_FAST_UDP (Create a UDP circuit, no PK) (See Sec 4)
122       10 -- CREATED_FAST_UDP(UDP circuit created, no PK)  (See Sec 4)
124   The sequence number allows for AES/CTR decryption of RELAY cells
125   independently of one another; this functionality is required to support
126   cells sent over DTLS.  The sequence number is described in more detail in
127   section 4.5.
129   [Should the sequence number only appear in RELAY packets?  The overhead is
130   small, and I'm hesitant to force more code paths on the implementor. -ML]
131   [There's already a separate relay header that has other material in it,
132   so it wouldn't be the end of the world to move it there if it's
133   appropriate. -RD]
135   [Having separate commands for UDP circuits seems necessary, unless we can
136   assume a flag day event for a large number of tor nodes. -ML]
138 4. Circuit management
140 4.2. Setting circuit keys
142   Keys are set up for UDP circuits in the same fashion as for TCP circuits.
143   Each UDP circuit shares keys with its corresponding TCP circuit.
145   [If the keys are used for both TCP and UDP connections, how does it
146   work to mix sequence-number-less cells with sequenced-numbered cells --
147   how do you know you have the encryption order right? -RD]
149 4.3. Creating circuits
151   UDP circuits are created as TCP circuits, using the *_UDP cells as
152   appropriate.
154 4.4. Tearing down circuits
156   UDP circuits are torn down as TCP circuits, using the *_UDP cells as
157   appropriate.
159 4.5. Routing relay cells
161   When an OR receives a RELAY cell, it checks the cell's circID and
162   determines whether it has a corresponding circuit along that
163   connection.  If not, the OR drops the RELAY cell.
165   Otherwise, if the OR is not at the OP edge of the circuit (that is,
166   either an 'exit node' or a non-edge node), it de/encrypts the payload
167   with AES/CTR, as follows:
168        'Forward' relay cell (same direction as CREATE):
169            Use Kf as key; decrypt, using sequence number to synchronize
170            ciphertext and keystream.
171        'Back' relay cell (opposite direction from CREATE):
172            Use Kb as key; encrypt, using sequence number to synchronize
173            ciphertext and keystream.
174   Note that in counter mode, decrypt and encrypt are the same operation.
175   [Since the sequence number is only 2 bytes, what do you do when it
176   rolls over? -RD]
178   Each stream encrypted by a Kf or Kb has a corresponding unique state,
179   captured by a sequence number; the originator of each such stream chooses
180   the initial sequence number randomly, and increments it only with RELAY
181   cells.  [This counts cells; unlike, say, TCP, tor uses fixed-size cells, so
182   there's no need for counting bytes directly.  Right? - ML]
183   [I believe this is true. You'll find out for sure when you try to
184   build it. ;) -RD]
186   The OR then decides whether it recognizes the relay cell, by
187   inspecting the payload as described in section 5.1 below.  If the OR
188   recognizes the cell, it processes the contents of the relay cell.
189   Otherwise, it passes the decrypted relay cell along the circuit if
190   the circuit continues.  If the OR at the end of the circuit
191   encounters an unrecognized relay cell, an error has occurred: the OR
192   sends a DESTROY cell to tear down the circuit.
194   When a relay cell arrives at an OP, the OP decrypts the payload
195   with AES/CTR as follows:
196         OP receives data cell:
197            For I=N...1,
198                Decrypt with Kb_I, using the sequence number as above.  If the
199                payload is recognized (see section 5.1), then stop and process
200                the payload.
202   For more information, see section 5 below.
204 4.6. CREATE_UDP and CREATED_UDP cells
206   Users set up UDP circuits incrementally.  The procedure is similar to that
207   for TCP circuits, as described in section 4.1.  In addition to the TLS
208   connection to the first node, the OP also attempts to open a DTLS
209   connection.  If this succeeds, the OP sends a CREATE_UDP cell, with a
210   payload in the same format as a CREATE cell.  To extend a UDP circuit past
211   the first hop, the OP sends an EXTEND_UDP relay cell (see section 5) which
212   instructs the last node in the circuit to send a CREATE_UDP cell to extend
213   the circuit.
215   The relay payload for an EXTEND_UDP relay cell consists of:
216          Address                       [4 bytes]
217          TCP port                      [2 bytes]
218          UDP port                      [2 bytes]
219          Onion skin                    [186 bytes]
220          Identity fingerprint          [20 bytes]
222   The address field and ports denote the IPV4 address and ports of the next OR
223   in the circuit.
225   The payload for a CREATED_UDP cell or the relay payload for an
226   RELAY_EXTENDED_UDP cell is identical to that of the corresponding CREATED or
227   RELAY_EXTENDED cell.  Both circuits are established using the same key.
229   Note that the existence of a UDP circuit implies the
230   existence of a corresponding TCP circuit, sharing keys, sequence numbers,
231   and any other relevant state.
233 4.6.1 CREATE_FAST_UDP/CREATED_FAST_UDP cells
235   As above, the OP must successfully connect using DTLS before attempting to
236   send a CREATE_FAST_UDP cell.  Otherwise, the procedure is the same as in
237   section 4.1.1.
239 5. Application connections and stream management
241 5.1. Relay cells
243   Within a circuit, the OP and the exit node use the contents of RELAY cells
244   to tunnel end-to-end commands, TCP connections ("Streams"), and UDP packets
245   across circuits.  End-to-end commands and UDP packets can be initiated by
246   either edge; streams are initiated by the OP.
248   The payload of each unencrypted RELAY cell consists of:
249         Relay command           [1 byte]
250         'Recognized'            [2 bytes]
251         StreamID                [2 bytes]
252         Digest                  [4 bytes]
253         Length                  [2 bytes]
254         Data                    [498 bytes]
256   The relay commands are:
257         1 -- RELAY_BEGIN        [forward]
258         2 -- RELAY_DATA         [forward or backward]
259         3 -- RELAY_END          [forward or backward]
260         4 -- RELAY_CONNECTED    [backward]
261         5 -- RELAY_SENDME       [forward or backward]
262         6 -- RELAY_EXTEND       [forward]
263         7 -- RELAY_EXTENDED     [backward]
264         8 -- RELAY_TRUNCATE     [forward]
265         9 -- RELAY_TRUNCATED    [backward]
266        10 -- RELAY_DROP         [forward or backward]
267        11 -- RELAY_RESOLVE      [forward]
268        12 -- RELAY_RESOLVED     [backward]
269        13 -- RELAY_BEGIN_UDP    [forward]
270        14 -- RELAY_DATA_UDP     [forward or backward]
271        15 -- RELAY_EXTEND_UDP   [forward]
272        16 -- RELAY_EXTENDED_UDP [backward]
273        17 -- RELAY_DROP_UDP     [forward or backward]
275   Commands labelled as "forward" must only be sent by the originator
276   of the circuit. Commands labelled as "backward" must only be sent by
277   other nodes in the circuit back to the originator. Commands marked
278   as either can be sent either by the originator or other nodes.
280   The 'recognized' field in any unencrypted relay payload is always set to
281   zero. 
283   The 'digest' field can have two meanings.  For all cells sent over TLS
284   connections (that is, all commands and all non-UDP RELAY data), it is
285   computed as the first four bytes of the running SHA-1 digest of all the
286   bytes that have been sent reliably and have been destined for this hop of
287   the circuit or originated from this hop of the circuit, seeded from Df or Db
288   respectively (obtained in section 4.2 above), and including this RELAY
289   cell's entire payload (taken with the digest field set to zero).  Cells sent
290   over DTLS connections do not affect this running digest.  Each cell sent
291   over DTLS (that is, RELAY_DATA_UDP and RELAY_DROP_UDP) has the digest field
292   set to the SHA-1 digest of the current RELAY cells' entire payload, with the
293   digest field set to zero.  Coupled with a randomly-chosen streamID, this
294   provides per-cell integrity checking on UDP cells.
295   [If you drop malformed UDP relay cells but don't close the circuit,
296   then this 8 bytes of digest is not as strong as what we get in the
297   TCP-circuit side. Is this a problem? -RD]
299   When the 'recognized' field of a RELAY cell is zero, and the digest
300   is correct, the cell is considered "recognized" for the purposes of
301   decryption (see section 4.5 above).
303   (The digest does not include any bytes from relay cells that do
304   not start or end at this hop of the circuit. That is, it does not
305   include forwarded data. Therefore if 'recognized' is zero but the
306   digest does not match, the running digest at that node should
307   not be updated, and the cell should be forwarded on.)
309   All RELAY cells pertaining to the same tunneled TCP stream have the
310   same streamID.  Such streamIDs are chosen arbitrarily by the OP.  RELAY
311   cells that affect the entire circuit rather than a particular
312   stream use a StreamID of zero.
314   All RELAY cells pertaining to the same UDP tunnel have the same streamID.
315   This streamID is chosen randomly by the OP, but cannot be zero.
317   The 'Length' field of a relay cell contains the number of bytes in
318   the relay payload which contain real payload data. The remainder of
319   the payload is padded with NUL bytes.
321   If the RELAY cell is recognized but the relay command is not
322   understood, the cell must be dropped and ignored. Its contents
323   still count with respect to the digests, though. [Before
324   0.1.1.10, Tor closed circuits when it received an unknown relay
325   command. Perhaps this will be more forward-compatible. -RD]
327 5.2.1.  Opening UDP tunnels and transferring data
329   To open a new anonymized UDP connection, the OP chooses an open
330   circuit to an exit that may be able to connect to the destination
331   address, selects a random streamID not yet used on that circuit,
332   and constructs a RELAY_BEGIN_UDP cell with a payload encoding the address
333   and port of the destination host.  The payload format is:
335         ADDRESS | ':' | PORT | [00]
337   where  ADDRESS can be a DNS hostname, or an IPv4 address in
338   dotted-quad format, or an IPv6 address surrounded by square brackets;
339   and where PORT is encoded in decimal.
341   [What is the [00] for? -NM]
342   [It's so the payload is easy to parse out with string funcs -RD]
344   Upon receiving this cell, the exit node resolves the address as necessary.
345   If the address cannot be resolved, the exit node replies with a RELAY_END
346   cell.  (See 5.4 below.)  Otherwise, the exit node replies with a
347   RELAY_CONNECTED cell, whose payload is in one of the following formats:
348       The IPv4 address to which the connection was made [4 octets]
349       A number of seconds (TTL) for which the address may be cached [4 octets]
350    or
351       Four zero-valued octets [4 octets]
352       An address type (6)     [1 octet]
353       The IPv6 address to which the connection was made [16 octets]
354       A number of seconds (TTL) for which the address may be cached [4 octets]
355   [XXXX Versions of Tor before 0.1.1.6 ignore and do not generate the TTL
356   field.  No version of Tor currently generates the IPv6 format.]
358   The OP waits for a RELAY_CONNECTED cell before sending any data.
359   Once a connection has been established, the OP and exit node
360   package UDP data in RELAY_DATA_UDP cells, and upon receiving such
361   cells, echo their contents to the corresponding socket.
362   RELAY_DATA_UDP cells sent to unrecognized streams are dropped.
364   Relay RELAY_DROP_UDP cells are long-range dummies; upon receiving such
365   a cell, the OR or OP must drop it.
367 5.3. Closing streams
369   UDP tunnels are closed in a fashion corresponding to TCP connections.
371 6. Flow Control
373   UDP streams are not subject to flow control.
375 7.2. Router descriptor format.
377 The items' formats are as follows:
378    "router" nickname address ORPort SocksPort DirPort UDPPort
380       Indicates the beginning of a router descriptor.  "address" must be
381       an IPv4 address in dotted-quad format. The last three numbers
382       indicate the TCP ports at which this OR exposes
383       functionality. ORPort is a port at which this OR accepts TLS
384       connections for the main OR protocol; SocksPort is deprecated and
385       should always be 0; DirPort is the port at which this OR accepts
386       directory-related HTTP connections; and UDPPort is a port at which
387       this OR accepts DTLS connections for UDP data.  If any port is not
388       supported, the value 0 is given instead of a port number.
390 Other sections:
392 What changes need to happen to each node's exit policy to support this? -RD
394 Switching to UDP means managing the queues of incoming packets better,
395 so we don't miss packets. How does this interact with doing large public
396 key operations (handshakes) in the same thread? -RD
398 ========================================================================
399 COMMENTS
400 ========================================================================
402 [16 May 2006]
404 I don't favor this approach; it makes packet traffic partitioned from
405 stream traffic end-to-end.  The architecture I'd like to see is:
407   A *All* Tor-to-Tor traffic is UDP/DTLS, unless we need to fall back on
408     TCP/TLS for firewall penetration or something.  (This also gives us an
409     upgrade path for routing through legacy servers.)
411   B Stream traffic is handled with end-to-end per-stream acks/naks and
412     retries.  On failure, the data is retransmitted in a new RELAY_DATA cell;
413     a cell isn't retransmitted.
415 We'll need to do A anyway, to fix our behavior on packet-loss.  Once we've
416 done so, B is more or less inevitable, and we can support end-to-end UDP
417 traffic "for free".
419 (Also, there are some details that this draft spec doesn't address.  For
420 example, what happens when a UDP packet doesn't fit in a single cell?)