tor-spec.txt 23 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543
  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. a|b -- concatenation of 'a' with 'b'.
  12. a[i:j] -- Bytes 'i' through 'j'-1 (inclusive) of the string a.
  13. All numeric values are encoded in network (big-endian) order.
  14. Unless otherwise specified, all symmetric ciphers are DES in OFB
  15. mode, with an IV of all 0 bytes. All asymmetric ciphers are RSA
  16. with 1024-bit keys, and exponents of 65537.
  17. [We will move to AES once we can assume everybody will have it. -RD]
  18. 1. System overview
  19. [Something to start with here. Do feel free to change/expand. -RD]
  20. Tor is an implementation of version 2 of Onion Routing.
  21. Onion Routing is a connection-oriented anonymizing communication
  22. service. Users build a layered block of asymmetric encryptions
  23. (an "onion") which describes a source-routed path through a set of
  24. nodes. Those nodes build a "virtual circuit" through the network, in which
  25. each node knows its predecessor and successor, but no others. Traffic
  26. flowing down the circuit is unwrapped by a symmetric key at each node,
  27. which reveals the downstream node.
  28. 2. Connections
  29. 2.1. Establishing OR-to-OR connections
  30. When one onion router opens a connection to another, the initiating
  31. OR (called the 'client') and the listening OR (called the 'server')
  32. perform the following handshake.
  33. Before the handshake begins, the client and server know one
  34. another's (1024-bit) public keys, IPV4 addresses, and ports.
  35. 1. Client connects to server:
  36. The client generates a pair of 8-byte symmetric keys (one
  37. [K_f] for the 'forward' stream from client to server, and one
  38. [K_b] for the 'backward' stream from server to client.
  39. The client then generates a 'Client authentication' message [M]
  40. containing:
  41. The client's published IPV4 address [4 bytes]
  42. The client's published port [2 bytes]
  43. The server's published IPV4 address [4 bytes]
  44. The server's published port [2 bytes]
  45. The forward key (K_f) [16 bytes]
  46. The backward key (K_f) [16 bytes]
  47. The maximum bandwidth (bytes/s) [4 bytes]
  48. [Total: 48 bytes]
  49. The client then RSA-encrypts the message with the server's
  50. public key, and PKCS1 padding to given an encrypted message
  51. [Commentary: 1024 bytes is probably too short, and this protocol can't
  52. support IPv6. -NM]
  53. [1024 is too short for a high-latency remailer; but perhaps it's
  54. fine for us, given our need for speed and also given our greater
  55. vulnerability to other attacks? Onions are infrequent enough now
  56. that maybe we could handle it; but I worry it will impact
  57. scalability, and handling more users is important.-RD]
  58. The client then opens a TCP connection to the server, sends
  59. the 128-byte RSA-encrypted data to the server, and waits for a
  60. reply.
  61. 2. Server authenticates to client:
  62. Upon receiving a TCP connection, the server waits to receive
  63. 128 bytes from the client. It decrypts the message with its
  64. private key, and checks the PKCS1 padding. If the padding is
  65. incorrect, or if the message's length is other than 32 bytes,
  66. the server closes the TCP connection and stops handshaking.
  67. The server then checks the list of known ORs for one with the
  68. address and port given in the client's authentication. If no
  69. such OR is known, or if the server is already connected to
  70. that OR, the server closes the current TCP connection and
  71. stops handshaking.
  72. For later use, the server sets its keys for this connection,
  73. setting K_f to the client's K_b, and K_b to the client's K_f.
  74. The server then creates a server authentication message[M2] as
  75. follows:
  76. Modified client authentication [48 bytes]
  77. A random nonce [N] [8 bytes]
  78. [Total: 56 bytes]
  79. The client authentication is generated from M by replacing
  80. the client's preferred bandwidth [B_c] with the server's
  81. preferred bandwidth [B_s], if B_s < B_c.
  82. The server encrypts M2 with the client's public key (found
  83. from the list of known routers), using PKCS1 padding.
  84. The server sends the 128-byte encrypted message to the client,
  85. and waits for a reply.
  86. 3. Client authenticates to server.
  87. Once the client has received 128 bytes, it decrypts them with
  88. its public key, and checks the PKCS1 padding. If the padding
  89. is invalid, or the decrypted message's length is other than 40
  90. bytes, the client closes the TCP connection.
  91. The client checks that the addresses and keys in the reply
  92. message are the same as the ones it originally sent. If not,
  93. it closes the TCP connection.
  94. The client updates the connection's bandwidth to that set by
  95. the server, and generates the following authentication message [M3]:
  96. The client's published IPV4 address [4 bytes]
  97. The client's published port [2 bytes]
  98. The server's published IPV4 address [4 bytes]
  99. The server's published port [2 bytes]
  100. The server-generated nonce [N] [8 bytes]
  101. [Total: 20 bytes]
  102. Once again, the client encrypts this message using the
  103. server's public key and PKCS1 padding, and sends the resulting
  104. 128-byte message to the server.
  105. 4. Server checks client authentication
  106. The server once again waits to receive 128 bytes from the
  107. client, decrypts the message with its private key, and checks
  108. the PKCS1 padding. If the padding is incorrect, or if the
  109. message's length is other than 20 bytes, the server closes the
  110. TCP connection and stops handshaking.
  111. If the addresses in the decrypted message M3 match those in M
  112. and M2, and if the nonce in M3 is the same as in M2, the
  113. handshake is complete, and the client and server begin sending
  114. cells to one another. Otherwise, the server closes the TCP
  115. connection.
  116. 2.2. Establishing OP-to-OR connections
  117. When an Onion Proxy (OP) needs to establish a connection to an OR,
  118. the handshake is simpler because the OR does not need to verify the
  119. OP's identity. The OP and OR establish the following steps:
  120. 1. OP connects to OR:
  121. First, the OP generates a pair of 8-byte symmetric keys (one
  122. [K_f] for the 'forward' stream from OP to OR, and one
  123. [K_b] for the 'backward' stream from OR to OP).
  124. The OP generates a message [M] in the following format:
  125. Maximum bandwidth (bytes/s) [4 bytes]
  126. Forward key [K_f] [16 bytes]
  127. Backward key [K_b] [16 bytes]
  128. [Total: 32 bytes]
  129. The OP encrypts M with the OR's public key and PKCS1 padding,
  130. opens a TCP connection to the OR's TCP port, and sends the
  131. resulting 128-byte encrypted message to the OR.
  132. 2. OR receives keys:
  133. When the OR receives a connection from an OP [This is on a
  134. different port, right? How does it know the difference? -NM],
  135. [Correct. The 'or_port' config variable specifies the OR port,
  136. and the op_port variable specified the OP port. -RD]
  137. it waits for 128 bytes of data, and decrypts the resulting
  138. data with its private key, checking the PKCS1 padding. If the
  139. padding is invalid, or the message is not 20 bytes long, the
  140. OR closes the connection.
  141. Otherwise, the connection is established, and the O is ready
  142. to receive cells.
  143. The server sets its keys for this connection, setting K_f to
  144. the client's K_b, and K_b to the client's K_f.
  145. 2.3. Sending cells and link encryption
  146. Once the handshake is complete, the ORs or OR and OP send cells
  147. (specified below) to one another. Cells are sent serially,
  148. encrypted with the 3DES-OFB keystream specified by the handshake
  149. protocol. Over a connection, communicants encrypt outgoing cells
  150. with the connection's K_f, and decrypt incoming cells with the
  151. connection's K_b.
  152. [Commentary: This means that OR/OP->OR connections are malleable; I
  153. can flip bits in cells as they go across the wire, and see flipped
  154. bits coming out the cells as they are decrypted at the next
  155. server. I need to look more at the data format to see whether
  156. this is exploitable, but if there's no integrity checking there
  157. either, I suspect we may have an attack here. -NM]
  158. [Yes, this protocol is open to tagging attacks. The payloads are
  159. encrypted inside the network, so it's only at the edge node and beyond
  160. that it's a worry. But adversaries can already count packets and
  161. observe/modify timing. It's not worth putting in hashes; indeed, it
  162. would be quite hard, because one of the sides of the circuit doesn't
  163. know the keys that are used for de/encrypting at each hop, so couldn't
  164. craft hashes anyway. See the Bandwidth Throttling (threat model)
  165. thread on http://archives.seul.org/or/dev/Jul-2002/threads.html. -RD]
  166. [Even if I don't control both sides of the connection, I can still
  167. do evil stuff. For instance, if I can guess that a cell is a
  168. TOPIC_COMMAND_BEGIN cell to www.slashdot.org:80 , I can change the
  169. address and port to point to a machine I control. -NM]
  170. 3. Cell Packet format
  171. The basic unit of communication for onion routers and onion
  172. proxies is a fixed-width "cell". Each cell contains the following
  173. fields:
  174. ACI (anonymous circuit identifier) [2 bytes]
  175. Command [1 byte]
  176. Length [1 byte]
  177. Sequence number (unused, set to 0) [4 bytes]
  178. Payload (padded with 0 bytes) [248 bytes]
  179. [Total size: 256 bytes]
  180. The 'Command' field holds one of the following values:
  181. 0 -- PADDING (Padding) (See Sec 6.2)
  182. 1 -- CREATE (Create a circuit) (See Sec 4)
  183. 2 -- DATA (End-to-end data) (See Sec 5)
  184. 3 -- DESTROY (Stop using a circuit) (See Sec 4)
  185. 4 -- SENDME (For flow control) (See Sec 6.1)
  186. The interpretation of 'Length' and 'Payload' depend on the type of
  187. the cell.
  188. PADDING: Length is 0; Payload is 248 bytes of 0's.
  189. CREATE: Length is a value between 1 and 248; the first 'length'
  190. bytes of payload contain a portion of an onion.
  191. DATA: Length is a value between 4 and 248; the first 'length'
  192. bytes of payload contain useful data.
  193. DESTROY: Neither field is used.
  194. SENDME: Length encodes a window size, payload is unused.
  195. Unused fields are filled with 0 bytes. The payload is padded with
  196. 0 bytes.
  197. PADDING cells are currently used to implement connection
  198. keepalive. ORs and OPs send one another a PADDING cell every few
  199. minutes.
  200. CREATE and DESTROY cells are used to manage circuits; see section
  201. 4 below.
  202. DATA cells are used to send commands and data along a circuit; see
  203. section 5 below.
  204. SENDME cells are used for flow control; see section 6 below.
  205. 4. Onions and circuit management
  206. 4.1. Setting up circuits
  207. An onion is a multi-layered structure, with one layer for each node
  208. in a circuit. Each (unencrypted) layer has the following fields:
  209. Version [1 byte]
  210. Port [2 bytes]
  211. Address [4 bytes]
  212. Expiration time [4 bytes]
  213. Key seed material [16 bytes]
  214. [Total: 27 bytes]
  215. The value of Version is currently 2.
  216. The port and address field denote the IPV4 address and port of
  217. the next onion router in the circuit, or are set to 0 for the
  218. last hop.
  219. The expiration time is a number of seconds since the epoch (1
  220. Jan 1970); by default, it is set to the current time plus one
  221. day.
  222. When constructing an onion to create a circuit from OR_1,
  223. OR_2... OR_N, the onion creator performs the following steps:
  224. 1. Let M = 100 random bytes.
  225. 2. For I=N downto 1:
  226. A. Create an onion layer L, setting Version=2,
  227. ExpirationTime=now + 1 day, and Seed=16 random bytes.
  228. If I=N, set Port=Address=0. Else, set Port and Address to
  229. the IPV4 port and address of OR_{I+1}.
  230. B. Let M = L | M.
  231. C. Let K1_I = SHA1(Seed).
  232. Let K2_I = SHA1(K1_I).
  233. Let K3_I = SHA1(K2_I).
  234. D. Encrypt the first 128 bytes of M with the RSA key of
  235. OR_I, using no padding. Encrypt the remaining portion of
  236. M with 3DES/OFB, using K1_I as a key and an all-0 IV.
  237. 3. M is now the onion.
  238. To create a connection using the onion M, an OP or OR performs the
  239. following steps:
  240. 1. If not already connected to the first router in the chain,
  241. open a new connection to that router.
  242. 2. Choose an ACI not already in use on the connection with the
  243. first router in the chain. If our address/port pair is
  244. numerically higher than the address/port pair of the other
  245. side, then let the high bit of the ACI be 1, else 0.
  246. 3. To send M over the wire, prepend a 4-byte integer containing
  247. Len(M). Call the result M'. Let N=ceil(Len(M')/248).
  248. Divide M' into N chunks, such that:
  249. Chunk_I = M'[(I-1)*248:I*248] for 1 <= I <= N-1
  250. Chunk_N = M'[(N-1)*248:Len(M')]
  251. 4. Send N CREATE cells along the connection, setting the ACI
  252. on each to the selected ACI, setting the payload on each to
  253. the corresponding 'Chunk_I', and setting the length on each
  254. to the length of the payload.
  255. Upon receiving a CREATE cell along a connection, an OR performs
  256. the following steps:
  257. 1. If we already have an 'open' circuit along this connection
  258. with this ACI, drop the cell.
  259. Otherwise, if we have no circuit along this connection with
  260. this ACI, let L = the integer value of the first 4 bytes of
  261. the payload. Create a half-open circuit with this ACI, and
  262. begin queueing CREATE cells for this circuit.
  263. Otherwise, we have a half-open circuit. If the total payload
  264. length of the CREATE cells for this circuit is exactly equal
  265. to the onion length specified in the first cell (minus 4), then
  266. process the onion. If it is more, then tear down the circuit.
  267. 2. Once we have a complete onion, decrypt the first 128 bytes
  268. of the onion with this OR's RSA private key, and extract
  269. the outmost onion layer. If the version, back cipher, or
  270. forward cipher is unrecognized, or the expiration time is
  271. in the past, then tear down the circuit (see section 4.2).
  272. Compute K1 through K3 as above. Use K1 to decrypt the rest
  273. of the onion using 3DES/OFB.
  274. If we are not the exit node, remove the first layer from the
  275. decrypted onion, and send the remainder to the next OR
  276. on the circuit, as specified above. (Note that we'll
  277. choose a different ACI for this circuit on the connection
  278. with the next OR.)
  279. As an optimization, OR implementations may delay processing onions
  280. until a break in traffic allows time to do so without harming
  281. network latency too greatly.
  282. 4.2. Tearing down circuits
  283. Circuits are torn down when an unrecoverable error occurs along
  284. the circuit, or when all topics on a circuit are closed and the
  285. circuit's intended lifetime is over.
  286. To tear down a circuit, an OR or OP sends a DESTROY cell with that
  287. direction's ACI to the adjacent nodes on that circuit.
  288. Upon receiving a DESTROY cell, an OR frees resources associated
  289. with the corresponding circuit. If it's not the start or end of the
  290. circuit, it sends a DESTROY cell for that circuit to the next OR in
  291. the circuit. If the node is the start or end of the circuit, then
  292. it tears down any associated edge connections (see section 5.1).
  293. After a DESTROY cell has been processed, an OR ignores all data or
  294. destroy cells for the corresponding circuit.
  295. 4.3. Routing data cells
  296. When an OR receives a DATA cell, it checks the cell's ACI and
  297. determines whether it has a corresponding circuit along that
  298. connection. If not, the OR drops the DATA cell.
  299. Otherwise, if the OR is not at the OP edge of the circuit (that is,
  300. either an 'exit node' or a non-edge node), it de/encrypts the length
  301. field and the payload with 3DES/OFB, as follows:
  302. 'Forward' data cell (same direction as onion):
  303. Use K2 as key; encrypt.
  304. 'Back' data cell (opposite direction from onion):
  305. Use K3 as key; decrypt.
  306. Otherwise, if the data cell has arrived to the OP edge of the circuit,
  307. the OP de/encrypts the length and payload fields with 3DES/OFB as
  308. follows:
  309. OP sends data cell:
  310. For I=1...N, decrypt with K2_I.
  311. OP receives data cell:
  312. For I=N...1, encrypt with K3_I.
  313. Edge nodes process the length and payload fields of DATA cells as
  314. described in section 5 below.
  315. 5. Application connections and topic management
  316. 5.1. Topics and TCP streams
  317. Within a circuit, the OP and the exit node use the contents of DATA
  318. packets to tunnel TCP connections ("Topics") across circuits.
  319. These connections are initiated by the OP.
  320. The first 4 bytes of each data cell are reserved as follows:
  321. Topic command [1 byte]
  322. Unused, set to 0. [1 byte]
  323. Topic ID [2 bytes]
  324. The recognized topic commands are:
  325. 1 -- TOPIC_BEGIN
  326. 2 -- TOPIC_DATA
  327. 3 -- TOPIC_END
  328. 4 -- TOPIC_CONNECTED
  329. 5 -- TOPIC_SENDME
  330. All DATA cells pertaining to the same tunneled connection have the
  331. same topic ID.
  332. To create a new anonymized TCP connection, the OP sends a
  333. TOPIC_BEGIN data cell with a payload encoding the address and port
  334. of the destination host. The payload format is:
  335. ADDRESS | ':' | PORT | '\000'
  336. where ADDRESS may be a DNS hostname, or an IPv4 address in
  337. dotted-quad format; and where PORT is encoded in decimal.
  338. Upon receiving this packet, the exit node resolves the address as
  339. necessary, and opens a new TCP connection to the target port. If
  340. the address cannot be resolved, or a connection can't be
  341. established, the exit node replies with a TOPIC_END cell.
  342. Otherwise, the exit node replies with a TOPIC_CONNECTED cell.
  343. The OP waits for a TOPIC_CONNECTED cell before sending any data.
  344. Once a connection has been established, the OP and exit node
  345. package stream data in TOPIC_DATA cells, and upon receiving such
  346. cells, echo their contents to the corresponding TCP stream.
  347. [XXX Mention zlib encoding. -NM]
  348. When one side of the TCP stream is closed, the corresponding edge
  349. node sends a TOPIC_END cell along the circuit; upon receiving a
  350. TOPIC_END cell, the edge node closes the corresponding TCP stream.
  351. [This should probably become:
  352. When one side of the TCP stream is closed, the corresponding edge
  353. node sends a TOPIC_END cell along the circuit; upon receiving a
  354. TOPIC_END cell, the edge node closes its side of the corresponding
  355. TCP stream (by sending a FIN packet), but continues to accept and
  356. package incoming data until both sides of the TCP stream are
  357. closed. At that point, the edge node sends a second TOPIC_END
  358. cell, and drops its record of the topic. -NM]
  359. 6. Flow control
  360. 6.1. Link throttling
  361. As discussed above in section 2.1, ORs and OPs negotiate a maximum
  362. bandwidth upon startup. The communicants only read up to that
  363. number of bytes per second on average, though they may use mechanisms
  364. to handle spikes (eg token buckets).
  365. Communicants rely on TCP's default flow control to push back when they
  366. stop reading, so nodes that don't obey this bandwidth limit can't do
  367. too much damage.
  368. 6.2. Link padding
  369. Currently nodes are not required to do any sort of link padding or
  370. dummy traffic. Because strong attacks exist even with link padding,
  371. and because link padding greatly increases the bandwidth requirements
  372. for running a node, we plan to leave out link padding until this
  373. tradeoff is better understood.
  374. 6.3. Circuit flow control
  375. To control a circuit's bandwidth usage, each node keeps track of
  376. how many data cells it is allowed to send to the next hop in the
  377. circuit. This 'window' value is initially set to 1000 data cells
  378. in each direction (cells that are not data cells do not affect
  379. the window). Each edge node on a circuit sends a SENDME cell
  380. (with length=100) every time it has received 100 data cells on the
  381. circuit. When a node receives a SENDME cell for a circuit, it increases
  382. the circuit's window in the corresponding direction (that is, for
  383. sending data cells back in the direction from which the sendme arrived)
  384. by the value of the cell's length field. If it's not an edge node,
  385. it passes an equivalent SENDME cell to the next node in the circuit.
  386. If the window value reaches 0 at the edge of a circuit, the OR stops
  387. reading from the edge connections. (It may finish processing what
  388. it's already read, and queue those cells for when a SENDME cell
  389. arrives.) Otherwise (when not at the edge of a circuit), if the
  390. window value is 0 and a data cell arrives, the node must tear down
  391. the circuit.
  392. 6.4. Topic flow control
  393. Edge nodes use TOPIC_SENDME data cells to implement end-to-end flow
  394. control for individual connections across circuits. As with circuit
  395. flow control, edge nodes begin with a window of cells (500) per
  396. topic, and increment the window by a fixed value (50) upon receiving
  397. a TOPIC_SENDME data cell. Edge nodes initiate TOPIC_SENDME data
  398. cells when both a) the window is <= 450, and b) there are less than
  399. ten cell payloads remaining to be flushed at that edge.
  400. 7. Directories and routers
  401. 7.1. Router descriptor format.
  402. Line format : address ORPort OPPort APPort DirPort bandwidth(bytes/s)
  403. followed by the router's public key.
  404. ORport is where the router listens for other routers (speaking cells)
  405. OPPort is where the router listens for onion proxies (speaking cells)
  406. APPort is where the router listens for applications (speaking socks)
  407. DirPort is where the router listens for directory download requests
  408. Example:
  409. moria.mit.edu 9001 9011 9021 9031 100000
  410. -----BEGIN RSA PUBLIC KEY-----
  411. MIGJAoGBAMBBuk1sYxEg5jLAJy86U3GGJ7EGMSV7yoA6mmcsEVU3pwTUrpbpCmwS
  412. 7BvovoY3z4zk63NZVBErgKQUDkn3pp8n83xZgEf4GI27gdWIIwaBjEimuJlEY+7K
  413. nZ7kVMRoiXCbjL6VAtNa4Zy1Af/GOm0iCIDpholeujQ95xew7rQnAgMA//8=
  414. -----END RSA PUBLIC KEY-----