tor-spec.txt 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251
  1. $Id$
  2. TOR (The Onion Router) Spec
  3. Note: This is an attempt to specify TOR as it exists as implemented in
  4. early March, 2003. It is not recommended that others implement this
  5. design as it stands; future versions of TOR will implement improved
  6. protocols.
  7. 0. Notation:
  8. PK -- a public key.
  9. SK -- a private key
  10. K -- a key for a symmetric cypher
  11. All numeric values are encoded in network (big-endian) order.
  12. Unless otherwise specified, all symmetric ciphers are DES in OFB
  13. mode, with an IV of all 0 bytes. All asymmetric ciphers are RSA
  14. with 1024-bit keys, and exponents of 65537.
  15. [Comments: DES? This should be AES. Why are -NM]
  16. [We will move to AES once we can assume everybody will have it. -RD]
  17. 1. System overview
  18. [Something to start with here. Do feel free to change/expand. -RD]
  19. Tor is an implementation of version 2 of Onion Routing.
  20. Onion Routing is a connection-oriented anonymizing communication
  21. service. Users build a layered block of asymmetric encryptions
  22. (an "onion") which describes a source-routed path through a set of
  23. nodes. Those nodes build a "virtual circuit" through the network, in which
  24. each node knows its predecessor and successor, but no others. Traffic
  25. flowing down the circuit is unwrapped by a symmetric key at each node,
  26. which reveals the downstream node.
  27. 2. Connections
  28. 2.1. Establishing OR-to-OR connections
  29. When one onion router opens a connection to another, the initiating
  30. OR (called the 'client') and the listening OR (called the 'server')
  31. perform the following handshake.
  32. Before the handshake begins, the client and server know one
  33. another's (1024-bit) public keys, IPV4 addresses, and ports.
  34. 1. Client connects to server:
  35. The client generates a pair of 8-byte symmetric keys (one
  36. [K_f] for the 'forward' stream from client to server, and one
  37. [K_b] for the 'backward' stream from server to client.
  38. The client then generates a 'Client authentication' message [M]
  39. containing:
  40. The client's published IPV4 address [4 bytes]
  41. The client's published port [2 bytes]
  42. The server's published IPV4 address [4 bytes]
  43. The server's published port [2 bytes]
  44. The forward key (K_f) [8 bytes]
  45. The backward key (K_f) [8 bytes]
  46. The maximum bandwidth (bytes/s) [4 bytes]
  47. [Total: 36 bytes]
  48. The client then RSA-encrypts the message with the server's
  49. public key, and PKCS1 padding to given an encrypted message
  50. [Commentary: 1024 bytes is probably too short, and this protocol can't
  51. support IPv6. -NM]
  52. [1024 is too short for a high-latency remailer; but perhaps it's
  53. fine for us, given our need for speed and also given our greater
  54. vulnerability to other attacks? Onions are infrequent enough now
  55. that maybe we could handle it; but I worry it will impact
  56. scalability, and handling more users is important.-RD]
  57. The client then opens a TCP connection to the server, sends
  58. the 128-byte RSA-encrypted data to the server, and waits for a
  59. reply.
  60. 2. Server authenticates to client:
  61. Upon receiving a TCP connection, the server waits to receive
  62. 128 bytes from the client. It decrypts the message with its
  63. private key, and checks the PKCS1 padding. If the padding is
  64. incorrect, or if the message's length is other than 32 bytes,
  65. the server closes the TCP connection and stops handshaking.
  66. The server then checks the list of known ORs for one with the
  67. address and port given in the client's authentication. If no
  68. such OR is known, or if the server is already connected to
  69. that OR, the server closes the current TCP connection and
  70. stops handshaking.
  71. For later use, the server sets its keys for this connection,
  72. setting K_f to the client's K_b, and K_b to the client's K_f.
  73. The server then creates a server authentication message[M2] as
  74. follows:
  75. Modified client authentication [32 bytes]
  76. A random nonce [N] [8 bytes]
  77. [Total: 40 bytes]
  78. The client authentication is generated from M by replacing
  79. the client's preferred bandwidth [B_c] with the server's
  80. preferred bandwidth [B_s], if B_s < B_c.
  81. The server encrypts M2 with the client's public key (found
  82. from the list of known routers), using PKCS1 padding.
  83. The server sends the 128-byte encrypted message to the client,
  84. and waits for a reply.
  85. 3. Client authenticates to server.
  86. Once the client has received 128 bytes, it decrypts them with
  87. its public key, and checks the PKCS1 padding. If the padding
  88. is invalid, or the decrypted message's length is other than 40
  89. bytes, the client closes the TCP connection.
  90. The client checks that the addresses and keys in the reply
  91. message are the same as the ones it originally sent. If not,
  92. it closes the TCP connection.
  93. The client updates the connection's bandwidth to that set by
  94. the server, and generates the following authentication message [M3]:
  95. The client's published IPV4 address [4 bytes]
  96. The client's published port [2 bytes]
  97. The server's published IPV4 address [4 bytes]
  98. The server's published port [2 bytes]
  99. The server-generated nonce [N] [8 bytes]
  100. [Total: 20 bytes]
  101. Once again, the client encrypts this message using the
  102. server's public key and PKCS1 padding, and sends the resulting
  103. 128-byte message to the server.
  104. 4. Server checks client authentication
  105. The server once again waits to receive 128 bytes from the
  106. client, decrypts the message with its private key, and checks
  107. the PKCS1 padding. If the padding is incorrect, or if the
  108. message's length is other than 20 bytes, the server closes the
  109. TCP connection and stops handshaking.
  110. If the addresses in the decrypted message M3 match those in M
  111. and M2, and if the nonce in M3 is the same as in M2, the
  112. handshake is complete, and the client and server begin sending
  113. cells to one another. Otherwise, the server closes the TCP
  114. connection.
  115. 2.2. Establishing OP-to-OR connections
  116. When an Onion Proxy (OP) needs to establish a connection to an OR,
  117. the handshake is simpler because the OR does not need to verify the
  118. OP's identity. The OP and OR establish the following steps:
  119. 1. OP connects to OR:
  120. First, the OP generates a pair of 8-byte symmetric keys (one
  121. [K_f] for the 'forward' stream from OP to OR, and one
  122. [K_b] for the 'backward' stream from OR to OP.
  123. The OP generates a message [M] in the following format:
  124. Maximum bandwidth (bytes/s) [4 bytes]
  125. Forward key [K_f] [8 bytes]
  126. Backward key [K_b] [8 bytes]
  127. [Total: 20 bytes]
  128. The OP encrypts M with the OR's public key and PKCS1 padding,
  129. opens a TCP connection to the OR's TCP port, and sends the
  130. resulting 128-byte encrypted message to the OR.
  131. 2. OR receives keys:
  132. When the OR receives a connection from an OP [This is on a
  133. different port, right? How does it know the difference? -NM],
  134. [Correct. The 'or_port' config variable specifies the OR port,
  135. and the op_port variable specified the OP port. -RD]
  136. it waits for 128 bytes of data, and decrypts the resulting
  137. data with its private key, checking the PKCS1 padding. If the
  138. padding is invalid, or the message is not 20 bytes long, the
  139. OR closes the connection.
  140. Otherwise, the connection is established, and the O is ready
  141. to receive cells.
  142. The server sets its keys for this connection, setting K_f to
  143. the client's K_b, and K_b to the client's K_f.
  144. 2.3. Sending cells and link encryption
  145. Once the handshake is complete, the ORs or OR and OP send cells
  146. (specified below) to one another. Cells are sent serially,
  147. encrypted with the DES-OFB keystream specified by the handshake
  148. protocol. Over a connection, communicants encrypt outgoing cells
  149. with the connection's K_f, and decrypt incoming cells with the
  150. connection's K_b.
  151. [Commentary: This means that OR/OP->OR connections are malleable; I
  152. can flip bits in cells as they go across the wire, and see flipped
  153. bits coming out the cells as they are decrypted at the next
  154. server. I need to look more at the data format to see whether
  155. this is exploitable, but if there's no integrity checking there
  156. either, I suspect we may have an attack here. -NM]
  157. [Yes, this protocol is open to tagging attacks. The payloads are
  158. encrypted inside the network, so it's only at the edge node and beyond
  159. that it's a worry. But adversaries can already count packets and
  160. observe/modify timing. It's not worth putting in hashes; indeed, it
  161. would be quite hard, because one of the sides of the circuit doesn't
  162. know the keys that are used for de/encrypting at each hop, so couldn't
  163. craft hashes anyway. See the Bandwidth Throttling (threat model)
  164. thread on http://archives.seul.org/or/dev/Jul-2002/threads.html. -RD]
  165. 3. Cell Packet format
  166. The basic unit of communication between onion routers and onion
  167. proxies is a fixed-width "Cell." Each Cell contains the following
  168. fields:
  169. ACI (anonymous circuit identifier) [2 bytes]
  170. Command [1 byte]
  171. Length [1 byte]
  172. Sequence number (unused) [4 bytes]
  173. Payload (padded with 0 bytes) [120 bytes]
  174. [Total size: 128 bytes]
  175. The 'Command' field holds one of the following values:
  176. 0 -- PADDING (Padding)
  177. 1 -- CREATE (Create a circuit)
  178. 2 -- DATA (End-to-end data)
  179. 3 -- DESTROY (Stop using a circuit)
  180. 4 -- SENDME (For flow control)
  181. The interpretation of 'Length' and 'Payload' depend on....
  182. 4. Onions and circuit management
  183. 5. Topic management
  184. 6. Flow control