tor-spec.txt 44 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012
  1. $Id$
  2. Tor Protocol Specification
  3. Roger Dingledine
  4. Nick Mathewson
  5. Note: This is an attempt to specify Tor as currently implemented. Future
  6. versions of Tor will implement improved protocols, and compatibility is not
  7. guaranteed.
  8. This is not a design document; most design criteria are not examined. For
  9. more information on why Tor acts as it does, see tor-design.pdf.
  10. TODO: (very soon)
  11. - REASON_CONNECTFAILED should include an IP.
  12. - Copy prose from tor-design to make everything more readable.
  13. when do we rotate which keys (tls, link, etc)?
  14. 0. Notation:
  15. PK -- a public key.
  16. SK -- a private key
  17. K -- a key for a symmetric cypher
  18. a|b -- concatenation of 'a' and 'b'.
  19. [A0 B1 C2] -- a three-byte sequence, containing the bytes with
  20. hexadecimal values A0, B1, and C2, in that order.
  21. All numeric values are encoded in network (big-endian) order.
  22. Unless otherwise specified, all symmetric ciphers are AES in counter
  23. mode, with an IV of all 0 bytes. Asymmetric ciphers are either RSA
  24. with 1024-bit keys and exponents of 65537, or DH where the generator (g)
  25. is 2 and the modulus (p) is the 1024-bit safe prime from rfc2409,
  26. section 6.2, whose hex representation is:
  27. "FFFFFFFFFFFFFFFFC90FDAA22168C234C4C6628B80DC1CD129024E08"
  28. "8A67CC74020BBEA63B139B22514A08798E3404DDEF9519B3CD3A431B"
  29. "302B0A6DF25F14374FE1356D6D51C245E485B576625E7EC6F44C42E9"
  30. "A637ED6B0BFF5CB6F406B7EDEE386BFB5A899FA5AE9F24117C4B1FE6"
  31. "49286651ECE65381FFFFFFFFFFFFFFFF"
  32. As an optimization, implementations SHOULD choose DH private keys (x) of
  33. 320 bits. Implementations that do this MUST never use any DH key more
  34. than once.
  35. All "hashes" are 20-byte SHA1 cryptographic digests.
  36. When we refer to "the hash of a public key", we mean the SHA1 hash of the
  37. DER encoding of an ASN.1 RSA public key (as specified in PKCS.1).
  38. 1. System overview
  39. Onion Routing is a distributed overlay network designed to anonymize
  40. low-latency TCP-based applications such as web browsing, secure shell,
  41. and instant messaging. Clients choose a path through the network and
  42. build a ``circuit'', in which each node (or ``onion router'' or ``OR'')
  43. in the path knows its predecessor and successor, but no other nodes in
  44. the circuit. Traffic flowing down the circuit is sent in fixed-size
  45. ``cells'', which are unwrapped by a symmetric key at each node (like
  46. the layers of an onion) and relayed downstream.
  47. 2. Connections
  48. There are two ways to connect to an onion router (OR). The first is
  49. as an onion proxy (OP), which allows the OP to authenticate the OR
  50. without authenticating itself. The second is as another OR, which
  51. allows mutual authentication.
  52. Tor uses TLS for link encryption. All implementations MUST support
  53. the TLS ciphersuite "TLS_EDH_RSA_WITH_DES_192_CBC3_SHA", and SHOULD
  54. support "TLS_DHE_RSA_WITH_AES_128_CBC_SHA" if it is available.
  55. Implementations MAY support other ciphersuites, but MUST NOT
  56. support any suite without ephemeral keys, symmetric keys of at
  57. least 128 bits, and digests of at least 160 bits.
  58. An OP or OR always sends a two-certificate chain, consisting of a
  59. certificate using a short-term connection key and a second, self-
  60. signed certificate containing the OR's identity key. The commonName of the
  61. first certificate is the OR's nickname, and the commonName of the second
  62. certificate is the OR's nickname, followed by a space and the string
  63. "<identity>".
  64. All parties receiving certificates must confirm that the identity key is
  65. as expected. (When initiating a connection, the expected identity key is
  66. the one given in the directory; when creating a connection because of an
  67. EXTEND cell, the expected identity key is the one given in the cell.) If
  68. the key is not as expected, the party must close the connection.
  69. All parties SHOULD reject connections to or from ORs that have malformed
  70. or missing certificates. ORs MAY accept or reject connections from OPs
  71. with malformed or missing certificates.
  72. Once a TLS connection is established, the two sides send cells
  73. (specified below) to one another. Cells are sent serially. All
  74. cells are 512 bytes long. Cells may be sent embedded in TLS
  75. records of any size or divided across TLS records, but the framing
  76. of TLS records MUST NOT leak information about the type or contents
  77. of the cells.
  78. TLS connections are not permanent. An OP or an OR may close a
  79. connection to an OR if there are no circuits running over the
  80. connection, and an amount of time (KeepalivePeriod, defaults to 5
  81. minutes) has passed.
  82. (As an exception, directory servers may try to stay connected to all of
  83. the ORs.)
  84. 3. Cell Packet format
  85. The basic unit of communication for onion routers and onion
  86. proxies is a fixed-width "cell". Each cell contains the following
  87. fields:
  88. CircID [2 bytes]
  89. Command [1 byte]
  90. Payload (padded with 0 bytes) [509 bytes]
  91. [Total size: 512 bytes]
  92. The CircID field determines which circuit, if any, the cell is
  93. associated with.
  94. The 'Command' field holds one of the following values:
  95. 0 -- PADDING (Padding) (See Sec 6.2)
  96. 1 -- CREATE (Create a circuit) (See Sec 4)
  97. 2 -- CREATED (Acknowledge create) (See Sec 4)
  98. 3 -- RELAY (End-to-end data) (See Sec 5)
  99. 4 -- DESTROY (Stop using a circuit) (See Sec 4)
  100. 5 -- CREATE_FAST (Create a circuit, no PK) (See sec 4)
  101. 6 -- CREATED_FAST (Circtuit created, no PK) (See Sec 4)
  102. The interpretation of 'Payload' depends on the type of the cell.
  103. PADDING: Payload is unused.
  104. CREATE: Payload contains the handshake challenge.
  105. CREATED: Payload contains the handshake response.
  106. RELAY: Payload contains the relay header and relay body.
  107. DESTROY: Payload contains a reason for closing the circuit.
  108. (see 4.4)
  109. Upon receiving any other value for the command field, an OR must
  110. drop the cell.
  111. The payload is padded with 0 bytes.
  112. PADDING cells are currently used to implement connection keepalive.
  113. If there is no other traffic, ORs and OPs send one another a PADDING
  114. cell every few minutes.
  115. CREATE, CREATED, and DESTROY cells are used to manage circuits;
  116. see section 4 below.
  117. RELAY cells are used to send commands and data along a circuit; see
  118. section 5 below.
  119. 4. Circuit management
  120. 4.1. CREATE and CREATED cells
  121. Users set up circuits incrementally, one hop at a time. To create a
  122. new circuit, OPs send a CREATE cell to the first node, with the
  123. first half of the DH handshake; that node responds with a CREATED
  124. cell with the second half of the DH handshake plus the first 20 bytes
  125. of derivative key data (see section 4.2). To extend a circuit past
  126. the first hop, the OP sends an EXTEND relay cell (see section 5)
  127. which instructs the last node in the circuit to send a CREATE cell
  128. to extend the circuit.
  129. The payload for a CREATE cell is an 'onion skin', which consists
  130. of the first step of the DH handshake data (also known as g^x).
  131. The data is encrypted to Bob's PK as follows: Suppose Bob's PK
  132. modulus is L octets long. If the data to be encrypted is shorter
  133. than L-42, then it is encrypted directly (with OAEP padding: see
  134. ftp://ftp.rsasecurity.com/pub/pkcs/pkcs-1/pkcs-1v2-1.pdf). If the
  135. data is at least as long as L-42, then a randomly generated 16-byte
  136. symmetric key is prepended to the data, after which the first L-16-42
  137. bytes of the data are encrypted with Bob's PK; and the rest of the
  138. data is encrypted with the symmetric key.
  139. So in this case, the onion skin on the wire looks like:
  140. RSA-encrypted:
  141. OAEP padding [42 bytes]
  142. Symmetric key [16 bytes]
  143. First part of g^x [70 bytes]
  144. Symmetrically encrypted:
  145. Second part of g^x [58 bytes]
  146. The relay payload for an EXTEND relay cell consists of:
  147. Address [4 bytes]
  148. Port [2 bytes]
  149. Onion skin [186 bytes]
  150. Identity fingerprint [20 bytes]
  151. The port and address field denote the IPV4 address and port of the next
  152. onion router in the circuit; the public key hash is the SHA1 hash of the
  153. PKCS#1 ASN1 encoding of the next onion router's identity (signing) key.
  154. (Including this hash allows the extending OR verify that it is indeed
  155. connected to the correct target OR, and prevents certain man-in-the-middle
  156. attacks.)
  157. The payload for a CREATED cell, or the relay payload for an
  158. EXTENDED cell, contains:
  159. DH data (g^y) [128 bytes]
  160. Derivative key data (KH) [20 bytes] <see 4.2 below>
  161. The CircID for a CREATE cell is an arbitrarily chosen 2-byte integer,
  162. selected by the node (OP or OR) that sends the CREATE cell. To prevent
  163. CircID collisions, when one OR sends a CREATE cell to another, it chooses
  164. from only one half of the possible values based on the ORs' public
  165. identity keys: if the sending OR has a lower key, it chooses a CircID with
  166. an MSB of 0; otherwise, it chooses a CircID with an MSB of 1.
  167. Public keys are compared numerically by modulus.
  168. As usual with DH, x and y MUST be generated randomly.
  169. (Older versions of Tor compared OR nicknames, and did it in a broken and
  170. unreliable way. To support versions of Tor earlier than 0.0.9pre6,
  171. implementations should notice when the other side of a connection is
  172. sending CREATE cells with the "wrong" MSB, and switch accordingly.)
  173. 4.1.1. CREATE_FAST/CREATED_FAST cells
  174. When initializing the first hop of a circuit, the OP has already
  175. established the OR's identity and negotiated a secret key using TLS.
  176. Because of this, it is not always necessary for the OP to perform the
  177. public key operations to create a circuit. In this case, the
  178. OP MAY send a CREATE_FAST cell instead of a CREATE cell for the first
  179. hop only. The OR responds with a CREATED_FAST cell, and the circuit is
  180. created.
  181. A CREATE_FAST cell contains:
  182. Key material (X) [20 bytes]
  183. A CREATED_FAST cell contains:
  184. Key material (Y) [20 bytes]
  185. Derivative key data [20 bytes] (See 4.2 below)
  186. The values of X and Y must be generated randomly.
  187. [Versions of Tor before 0.1.0.6-rc did not support these cell types;
  188. clients should not send CREATE_FAST cells to older Tor servers.]
  189. 4.2. Setting circuit keys
  190. Once the handshake between the OP and an OR is completed, both can
  191. now calculate g^xy with ordinary DH. Before computing g^xy, both client
  192. and server MUST verify that the received g^x or g^y value is not degenerate;
  193. that is, it must be strictly greater than 1 and strictly less than p-1
  194. where p is the DH modulus. Implementations MUST NOT complete a handshake
  195. with degenerate keys. Implementions MAY discard other "weak" g^x values.
  196. (Discarding degenerate keys is critical for security; if bad keys are not
  197. discarded, an attacker can substitute the server's CREATED cell's g^y with
  198. 0 or 1, thus creating a known g^xy and impersonating the server.)
  199. (The mainline Tor implementation, in the 0.1.1.x-alpha series, discarded
  200. all g^x values less than 2^24, greater than p-2^24, or having more than
  201. 1024-16 identical bits. This served no useful purpose, and we stopped.)
  202. If CREATE or EXTEND is used to extend a circuit, the client and server
  203. base their key material on K0=g^xy, represented as a big-endian unsigned
  204. integer.
  205. If CREATE_FAST is used, the client and server base their key material on
  206. K0=X|Y.
  207. From the base key material g^xy, they compute derivative key material as
  208. follows. Next, the server computes 100 bytes of key data as K = SHA1(K0
  209. | [00]) | SHA1(K0 | [01]) | ... SHA1(K0 | [04]) where "00" is a single
  210. octet whose value is zero, [01] is a single octet whose value is one, etc.
  211. The first 20 bytes of K form KH, bytes 21-40 form the forward digest Df,
  212. 41-60 form the backward digest Db, 61-76 form Kf, and 77-92 form Kb.
  213. KH is used in the handshake response to demonstrate knowledge of the
  214. computed shared key. Df is used to seed the integrity-checking hash
  215. for the stream of data going from the OP to the OR, and Db seeds the
  216. integrity-checking hash for the data stream from the OR to the OP. Kf
  217. is used to encrypt the stream of data going from the OP to the OR, and
  218. Kb is used to encrypt the stream of data going from the OR to the OP.
  219. The fast-setup case uses the same formula, except that X|Y is used
  220. in place of g^xy in determining K. That is,
  221. K = SHA1(X|Y | [00]) | SHA1(X|Y | [01]) | ... SHA1(X|Y| | [04])
  222. The values KH, Kf, Kb, Df, and Db are established and used as before.
  223. 4.3. Creating circuits
  224. When creating a circuit through the network, the circuit creator
  225. (OP) performs the following steps:
  226. 1. Choose an onion router as an exit node (R_N), such that the onion
  227. router's exit policy includes at least one pending stream that
  228. needs a circuit (if there are any).
  229. 2. Choose a chain of (N-1) onion routers
  230. (R_1...R_N-1) to constitute the path, such that no router
  231. appears in the path twice.
  232. 3. If not already connected to the first router in the chain,
  233. open a new connection to that router.
  234. 4. Choose a circID not already in use on the connection with the
  235. first router in the chain; send a CREATE cell along the
  236. connection, to be received by the first onion router.
  237. 5. Wait until a CREATED cell is received; finish the handshake
  238. and extract the forward key Kf_1 and the backward key Kb_1.
  239. 6. For each subsequent onion router R (R_2 through R_N), extend
  240. the circuit to R.
  241. To extend the circuit by a single onion router R_M, the OP performs
  242. these steps:
  243. 1. Create an onion skin, encrypted to R_M's public key.
  244. 2. Send the onion skin in a relay EXTEND cell along
  245. the circuit (see section 5).
  246. 3. When a relay EXTENDED cell is received, verify KH, and
  247. calculate the shared keys. The circuit is now extended.
  248. When an onion router receives an EXTEND relay cell, it sends a CREATE
  249. cell to the next onion router, with the enclosed onion skin as its
  250. payload. The initiating onion router chooses some circID not yet
  251. used on the connection between the two onion routers. (But see
  252. section 4.1. above, concerning choosing circIDs based on
  253. lexicographic order of nicknames.)
  254. When an onion router receives a CREATE cell, if it already has a
  255. circuit on the given connection with the given circID, it drops the
  256. cell. Otherwise, after receiving the CREATE cell, it completes the
  257. DH handshake, and replies with a CREATED cell. Upon receiving a
  258. CREATED cell, an onion router packs it payload into an EXTENDED relay
  259. cell (see section 5), and sends that cell up the circuit. Upon
  260. receiving the EXTENDED relay cell, the OP can retrieve g^y.
  261. (As an optimization, OR implementations may delay processing onions
  262. until a break in traffic allows time to do so without harming
  263. network latency too greatly.)
  264. 4.4. Tearing down circuits
  265. Circuits are torn down when an unrecoverable error occurs along
  266. the circuit, or when all streams on a circuit are closed and the
  267. circuit's intended lifetime is over. Circuits may be torn down
  268. either completely or hop-by-hop.
  269. To tear down a circuit completely, an OR or OP sends a DESTROY
  270. cell to the adjacent nodes on that circuit, using the appropriate
  271. direction's circID.
  272. Upon receiving an outgoing DESTROY cell, an OR frees resources
  273. associated with the corresponding circuit. If it's not the end of
  274. the circuit, it sends a DESTROY cell for that circuit to the next OR
  275. in the circuit. If the node is the end of the circuit, then it tears
  276. down any associated edge connections (see section 5.1).
  277. After a DESTROY cell has been processed, an OR ignores all data or
  278. destroy cells for the corresponding circuit.
  279. To tear down part of a circuit, the OP may send a RELAY_TRUNCATE cell
  280. signaling a given OR (Stream ID zero). That OR sends a DESTROY
  281. cell to the next node in the circuit, and replies to the OP with a
  282. RELAY_TRUNCATED cell.
  283. When an unrecoverable error occurs along one connection in a
  284. circuit, the nodes on either side of the connection should, if they
  285. are able, act as follows: the node closer to the OP should send a
  286. RELAY_TRUNCATED cell towards the OP; the node farther from the OP
  287. should send a DESTROY cell down the circuit.
  288. The payload of a RELAY_TRUNCATED or DESTROY cell contains a single octet,
  289. describing why the circuit is being closed or truncated. When sending a
  290. TRUNCATED or DESTROY cell because of another TRUNCATED or DESTROY cell,
  291. the error code should be propagated. The origin of a circuit always sets
  292. this error code to 0, to avoid leaking its version.
  293. The error codes are:
  294. 0 -- NONE (No reason given.)
  295. 1 -- PROTOCOL (Tor protocol violation.)
  296. 2 -- INTERNAL (Internal error.)
  297. 3 -- REQUESTED (A client sent a TRUNCATE command.)
  298. 4 -- HIBERNATING (Not currently operating; trying to save bandwidth.)
  299. 5 -- RESOURCELIMIT (Out of memory, sockets, or circuit IDs.)
  300. 6 -- CONNECTFAILED (Unable to reach server.)
  301. 7 -- OR_IDENTITY (Connected to server, but its OR identity was not
  302. as expected.)
  303. 8 -- OR_CONN_CLOSED (The OR connection that was carrying this circuit
  304. died.)
  305. [Versions of Tor prior to 0.1.0.11 didn't sent versions; implementations
  306. MUST accept empty TRUNCATED and DESTROY cells.]
  307. 4.5. Routing relay cells
  308. When an OR receives a RELAY cell, it checks the cell's circID and
  309. determines whether it has a corresponding circuit along that
  310. connection. If not, the OR drops the RELAY cell.
  311. Otherwise, if the OR is not at the OP edge of the circuit (that is,
  312. either an 'exit node' or a non-edge node), it de/encrypts the payload
  313. with AES/CTR, as follows:
  314. 'Forward' relay cell (same direction as CREATE):
  315. Use Kf as key; decrypt.
  316. 'Back' relay cell (opposite direction from CREATE):
  317. Use Kb as key; encrypt.
  318. Note that in counter mode, decrypt and encrypt are the same operation.
  319. The OR then decides whether it recognizes the relay cell, by
  320. inspecting the payload as described in section 5.1 below. If the OR
  321. recognizes the cell, it processes the contents of the relay cell.
  322. Otherwise, it passes the decrypted relay cell along the circuit if
  323. the circuit continues. If the OR at the end of the circuit
  324. encounters an unrecognized relay cell, an error has occurred: the OR
  325. sends a DESTROY cell to tear down the circuit.
  326. When a relay cell arrives at an OP, the OP decrypts the payload
  327. with AES/CTR as follows:
  328. OP receives data cell:
  329. For I=N...1,
  330. Decrypt with Kb_I. If the payload is recognized (see
  331. section 5.1), then stop and process the payload.
  332. For more information, see section 5 below.
  333. 5. Application connections and stream management
  334. 5.1. Relay cells
  335. Within a circuit, the OP and the exit node use the contents of
  336. RELAY packets to tunnel end-to-end commands and TCP connections
  337. ("Streams") across circuits. End-to-end commands can be initiated
  338. by either edge; streams are initiated by the OP.
  339. The payload of each unencrypted RELAY cell consists of:
  340. Relay command [1 byte]
  341. 'Recognized' [2 bytes]
  342. StreamID [2 bytes]
  343. Digest [4 bytes]
  344. Length [2 bytes]
  345. Data [498 bytes]
  346. The relay commands are:
  347. 1 -- RELAY_BEGIN [forward]
  348. 2 -- RELAY_DATA [forward or backward]
  349. 3 -- RELAY_END [forward or backward]
  350. 4 -- RELAY_CONNECTED [backward]
  351. 5 -- RELAY_SENDME [forward or backward]
  352. 6 -- RELAY_EXTEND [forward]
  353. 7 -- RELAY_EXTENDED [backward]
  354. 8 -- RELAY_TRUNCATE [forward]
  355. 9 -- RELAY_TRUNCATED [backward]
  356. 10 -- RELAY_DROP [forward or backward]
  357. 11 -- RELAY_RESOLVE [forward]
  358. 12 -- RELAY_RESOLVED [backward]
  359. Commands labelled as "forward" must only be sent by the originator
  360. of the circuit. Commands labelled as "backward" must only be sent by
  361. other nodes in the circuit back to the originator. Commands marked
  362. as either can be sent either by the originator or other nodes.
  363. The 'recognized' field in any unencrypted relay payload is always set
  364. to zero; the 'digest' field is computed as the first four bytes of
  365. the running SHA-1 digest of all the bytes that have been destined for
  366. this hop of the circuit or originated from this hop of the circuit,
  367. seeded from Df or Db respectively (obtained in section 4.2 above),
  368. and including this RELAY cell's entire payload (taken with the digest
  369. field set to zero).
  370. When the 'recognized' field of a RELAY cell is zero, and the digest
  371. is correct, the cell is considered "recognized" for the purposes of
  372. decryption (see section 4.5 above).
  373. (The digest does not include any bytes from relay cells that do
  374. not start or end at this hop of the circuit. That is, it does not
  375. include forwarded data. Therefore if 'recognized' is zero but the
  376. digest does not match, the running digest at that node should
  377. not be updated, and the cell should be forwarded on.)
  378. All RELAY cells pertaining to the same tunneled stream have the
  379. same stream ID. StreamIDs are chosen arbitrarily by the OP. RELAY
  380. cells that affect the entire circuit rather than a particular
  381. stream use a StreamID of zero.
  382. The 'Length' field of a relay cell contains the number of bytes in
  383. the relay payload which contain real payload data. The remainder of
  384. the payload is padded with NUL bytes.
  385. If the RELAY cell is recognized but the relay command is not
  386. understood, the cell must be dropped and ignored. Its contents
  387. still count with respect to the digests, though. [Before
  388. 0.1.1.10, Tor closed circuits when it received an unknown relay
  389. command. Perhaps this will be more forward-compatible. -RD]
  390. 5.2. Opening streams and transferring data
  391. To open a new anonymized TCP connection, the OP chooses an open
  392. circuit to an exit that may be able to connect to the destination
  393. address, selects an arbitrary StreamID not yet used on that circuit,
  394. and constructs a RELAY_BEGIN cell with a payload encoding the address
  395. and port of the destination host. The payload format is:
  396. ADDRESS | ':' | PORT | [00]
  397. where ADDRESS can be a DNS hostname, or an IPv4 address in
  398. dotted-quad format, or an IPv6 address surrounded by square brackets;
  399. and where PORT is encoded in decimal.
  400. [What is the [00] for? -NM]
  401. [It's so the payload is easy to parse out with string funcs -RD]
  402. Upon receiving this cell, the exit node resolves the address as
  403. necessary, and opens a new TCP connection to the target port. If the
  404. address cannot be resolved, or a connection can't be established, the
  405. exit node replies with a RELAY_END cell. (See 5.4 below.)
  406. Otherwise, the exit node replies with a RELAY_CONNECTED cell, whose
  407. payload is in one of the following formats:
  408. The IPv4 address to which the connection was made [4 octets]
  409. A number of seconds (TTL) for which the address may be cached [4 octets]
  410. or
  411. Four zero-valued octets [4 octets]
  412. An address type (6) [1 octet]
  413. The IPv6 address to which the connection was made [16 octets]
  414. A number of seconds (TTL) for which the address may be cached [4 octets]
  415. [XXXX Versions of Tor before 0.1.1.6 ignore and do not generate the TTL
  416. field. No version of Tor currently generates the IPv6 format.]
  417. The OP waits for a RELAY_CONNECTED cell before sending any data.
  418. Once a connection has been established, the OP and exit node
  419. package stream data in RELAY_DATA cells, and upon receiving such
  420. cells, echo their contents to the corresponding TCP stream.
  421. RELAY_DATA cells sent to unrecognized streams are dropped.
  422. Relay RELAY_DROP cells are long-range dummies; upon receiving such
  423. a cell, the OR or OP must drop it.
  424. 5.3. Closing streams
  425. When an anonymized TCP connection is closed, or an edge node
  426. encounters error on any stream, it sends a 'RELAY_END' cell along the
  427. circuit (if possible) and closes the TCP connection immediately. If
  428. an edge node receives a 'RELAY_END' cell for any stream, it closes
  429. the TCP connection completely, and sends nothing more along the
  430. circuit for that stream.
  431. The payload of a RELAY_END cell begins with a single 'reason' byte to
  432. describe why the stream is closing, plus optional data (depending on
  433. the reason.) The values are:
  434. 1 -- REASON_MISC (catch-all for unlisted reasons)
  435. 2 -- REASON_RESOLVEFAILED (couldn't look up hostname)
  436. 3 -- REASON_CONNECTREFUSED (remote host refused connection) [*]
  437. 4 -- REASON_EXITPOLICY (OR refuses to connect to host or port)
  438. 5 -- REASON_DESTROY (Circuit is being destroyed)
  439. 6 -- REASON_DONE (Anonymized TCP connection was closed)
  440. 7 -- REASON_TIMEOUT (Connection timed out, or OR timed out
  441. while connecting)
  442. 8 -- (unallocated) [**]
  443. 9 -- REASON_HIBERNATING (OR is temporarily hibernating)
  444. 10 -- REASON_INTERNAL (Internal error at the OR)
  445. 11 -- REASON_RESOURCELIMIT (OR has no resources to fulfill request)
  446. 12 -- REASON_CONNRESET (Connection was unexpectedly reset)
  447. 13 -- REASON_TORPROTOCOL (Sent when closing connection because of
  448. Tor protocol violations.)
  449. (With REASON_EXITPOLICY, the 4-byte IPv4 address or 16-byte IPv6 address
  450. forms the optional data; no other reason currently has extra data.
  451. As of 0.1.1.6, the body also contains a 4-byte TTL.)
  452. OPs and ORs MUST accept reasons not on the above list, since future
  453. versions of Tor may provide more fine-grained reasons.
  454. [*] Older versions of Tor also send this reason when connections are
  455. reset.
  456. [**] Due to a bug in versions of Tor through 0095, error reason 8 must
  457. remain allocated until that version is obsolete.
  458. --- [The rest of this section describes unimplemented functionality.]
  459. Because TCP connections can be half-open, we follow an equivalent
  460. to TCP's FIN/FIN-ACK/ACK protocol to close streams.
  461. An exit connection can have a TCP stream in one of three states:
  462. 'OPEN', 'DONE_PACKAGING', and 'DONE_DELIVERING'. For the purposes
  463. of modeling transitions, we treat 'CLOSED' as a fourth state,
  464. although connections in this state are not, in fact, tracked by the
  465. onion router.
  466. A stream begins in the 'OPEN' state. Upon receiving a 'FIN' from
  467. the corresponding TCP connection, the edge node sends a 'RELAY_FIN'
  468. cell along the circuit and changes its state to 'DONE_PACKAGING'.
  469. Upon receiving a 'RELAY_FIN' cell, an edge node sends a 'FIN' to
  470. the corresponding TCP connection (e.g., by calling
  471. shutdown(SHUT_WR)) and changing its state to 'DONE_DELIVERING'.
  472. When a stream in already in 'DONE_DELIVERING' receives a 'FIN', it
  473. also sends a 'RELAY_FIN' along the circuit, and changes its state
  474. to 'CLOSED'. When a stream already in 'DONE_PACKAGING' receives a
  475. 'RELAY_FIN' cell, it sends a 'FIN' and changes its state to
  476. 'CLOSED'.
  477. If an edge node encounters an error on any stream, it sends a
  478. 'RELAY_END' cell (if possible) and closes the stream immediately.
  479. 5.4. Remote hostname lookup
  480. To find the address associated with a hostname, the OP sends a
  481. RELAY_RESOLVE cell containing the hostname to be resolved. (For a reverse
  482. lookup, the OP sends a RELAY_RESOLVE cell containing an in-addr.arpa
  483. address.) The OR replies with a RELAY_RESOLVED cell containing a status
  484. byte, and any number of answers. Each answer is of the form:
  485. Type (1 octet)
  486. Length (1 octet)
  487. Value (variable-width)
  488. TTL (4 octets)
  489. "Length" is the length of the Value field.
  490. "Type" is one of:
  491. 0x00 -- Hostname
  492. 0x04 -- IPv4 address
  493. 0x06 -- IPv6 address
  494. 0xF0 -- Error, transient
  495. 0xF1 -- Error, nontransient
  496. If any answer has a type of 'Error', then no other answer may be given.
  497. The RELAY_RESOLVE cell must use a nonzero, distinct streamID; the
  498. corresponding RELAY_RESOLVED cell must use the same streamID. No stream
  499. is actually created by the OR when resolving the name.
  500. 6. Flow control
  501. 6.1. Link throttling
  502. Each node should do appropriate bandwidth throttling to keep its
  503. user happy.
  504. Communicants rely on TCP's default flow control to push back when they
  505. stop reading.
  506. 6.2. Link padding
  507. Currently nodes are not required to do any sort of link padding or
  508. dummy traffic. Because strong attacks exist even with link padding,
  509. and because link padding greatly increases the bandwidth requirements
  510. for running a node, we plan to leave out link padding until this
  511. tradeoff is better understood.
  512. 6.3. Circuit-level flow control
  513. To control a circuit's bandwidth usage, each OR keeps track of
  514. two 'windows', consisting of how many RELAY_DATA cells it is
  515. allowed to package for transmission, and how many RELAY_DATA cells
  516. it is willing to deliver to streams outside the network.
  517. Each 'window' value is initially set to 1000 data cells
  518. in each direction (cells that are not data cells do not affect
  519. the window). When an OR is willing to deliver more cells, it sends a
  520. RELAY_SENDME cell towards the OP, with Stream ID zero. When an OR
  521. receives a RELAY_SENDME cell with stream ID zero, it increments its
  522. packaging window.
  523. Each of these cells increments the corresponding window by 100.
  524. The OP behaves identically, except that it must track a packaging
  525. window and a delivery window for every OR in the circuit.
  526. An OR or OP sends cells to increment its delivery window when the
  527. corresponding window value falls under some threshold (900).
  528. If a packaging window reaches 0, the OR or OP stops reading from
  529. TCP connections for all streams on the corresponding circuit, and
  530. sends no more RELAY_DATA cells until receiving a RELAY_SENDME cell.
  531. [this stuff is badly worded; copy in the tor-design section -RD]
  532. 6.4. Stream-level flow control
  533. Edge nodes use RELAY_SENDME cells to implement end-to-end flow
  534. control for individual connections across circuits. Similarly to
  535. circuit-level flow control, edge nodes begin with a window of cells
  536. (500) per stream, and increment the window by a fixed value (50)
  537. upon receiving a RELAY_SENDME cell. Edge nodes initiate RELAY_SENDME
  538. cells when both a) the window is <= 450, and b) there are less than
  539. ten cell payloads remaining to be flushed at that edge.
  540. 7. Directories and routers
  541. 7.1. Extensible information format
  542. Router descriptors and directories both obey the following lightweight
  543. extensible information format.
  544. The highest level object is a Document, which consists of one or more Items.
  545. Every Item begins with a KeywordLine, followed by one or more Objects. A
  546. KeywordLine begins with a Keyword, optionally followed by whitespace and more
  547. non-newline characters, and ends with a newline. A Keyword is a sequence of
  548. one or more characters in the set [A-Za-z0-9-]. An Object is a block of
  549. encoded data in pseudo-Open-PGP-style armor. (cf. RFC 2440)
  550. More formally:
  551. Document ::= (Item | NL)+
  552. Item ::= KeywordLine Object*
  553. KeywordLine ::= Keyword NL | Keyword WS ArgumentsChar+ NL
  554. Keyword = KeywordChar+
  555. KeywordChar ::= 'A' ... 'Z' | 'a' ... 'z' | '0' ... '9' | '-'
  556. ArgumentChar ::= any printing ASCII character except NL.
  557. WS = (SP | TAB)+
  558. Object ::= BeginLine Base-64-encoded-data EndLine
  559. BeginLine ::= "-----BEGIN " Keyword "-----" NL
  560. EndLine ::= "-----END " Keyword "-----" NL
  561. The BeginLine and EndLine of an Object must use the same keyword.
  562. When interpreting a Document, software MUST reject any document containing a
  563. KeywordLine that starts with a keyword it doesn't recognize.
  564. The "opt" keyword is reserved for non-critical future extensions. All
  565. implementations MUST ignore any item of the form "opt keyword ....." when
  566. they would not recognize "keyword ....."; and MUST treat "opt keyword ....."
  567. as synonymous with "keyword ......" when keyword is recognized.
  568. 7.2. Router descriptor format.
  569. Every router descriptor MUST start with a "router" Item; MUST end with a
  570. "router-signature" Item and an extra NL; and MUST contain exactly one
  571. instance of each of the following Items: "published" "onion-key" "link-key"
  572. "signing-key" "bandwidth". Additionally, a router descriptor MAY contain any
  573. number of "accept", "reject", "fingerprint", "uptime", and "opt" Items.
  574. Other than "router" and "router-signature", the items may appear in any
  575. order.
  576. The items' formats are as follows:
  577. "router" nickname address ORPort SocksPort DirPort
  578. Indicates the beginning of a router descriptor. "address"
  579. must be an IPv4 address in dotted-quad format. The last
  580. three numbers indicate the TCP ports at which this OR exposes
  581. functionality. ORPort is a port at which this OR accepts TLS
  582. connections for the main OR protocol; SocksPort is deprecated and
  583. should always be 0; and DirPort is the port at which this OR accepts
  584. directory-related HTTP connections. If any port is not supported,
  585. the value 0 is given instead of a port number.
  586. "bandwidth" bandwidth-avg bandwidth-burst bandwidth-observed
  587. Estimated bandwidth for this router, in bytes per second. The
  588. "average" bandwidth is the volume per second that the OR is willing
  589. to sustain over long periods; the "burst" bandwidth is the volume
  590. that the OR is willing to sustain in very short intervals. The
  591. "observed" value is an estimate of the capacity this server can
  592. handle. The server remembers the max bandwidth sustained output
  593. over any ten second period in the past day, and another sustained
  594. input. The "observed" value is the lesser of these two numbers.
  595. "platform" string
  596. A human-readable string describing the system on which this OR is
  597. running. This MAY include the operating system, and SHOULD include
  598. the name and version of the software implementing the Tor protocol.
  599. "published" YYYY-MM-DD HH:MM:SS
  600. The time, in GMT, when this descriptor was generated.
  601. "fingerprint"
  602. A fingerprint (20 byte SHA1 hash of asn1 encoded public key, encoded
  603. in hex, with a single space after every 4 characters) for this router's
  604. identity key.
  605. [We didn't start parsing this line until Tor 0.1.0.6-rc; it should
  606. be marked with "opt" until earlier versions of Tor are obsolete.]
  607. "hibernating" 0|1
  608. If the value is 1, then the Tor server was hibernating when the
  609. descriptor was published, and shouldn't be used to build circuits.
  610. [We didn't start parsing this line until Tor 0.1.0.6-rc; it should
  611. be marked with "opt" until earlier versions of Tor are obsolete.]
  612. "uptime"
  613. The number of seconds that this OR process has been running.
  614. "onion-key" NL a public key in PEM format
  615. This key is used to encrypt EXTEND cells for this OR. The key MUST
  616. be accepted for at least XXXX hours after any new key is published in
  617. a subsequent descriptor.
  618. "signing-key" NL a public key in PEM format
  619. The OR's long-term identity key.
  620. "accept" exitpattern
  621. "reject" exitpattern
  622. These lines, in order, describe the rules that an OR follows when
  623. deciding whether to allow a new stream to a given address. The
  624. 'exitpattern' syntax is described below.
  625. "router-signature" NL Signature NL
  626. The "SIGNATURE" object contains a signature of the PKCS1-padded SHA1
  627. hash of the entire router descriptor, taken from the beginning of the
  628. "router" line, through the newline after the "router-signature" line.
  629. The router descriptor is invalid unless the signature is performed
  630. with the router's identity key.
  631. "contact" info NL
  632. Describes a way to contact the server's administrator, preferably
  633. including an email address and a PGP key fingerprint.
  634. "family" names NL
  635. 'Names' is a whitespace-separated list of server nicknames. If two ORs
  636. list one another in their "family" entries, then OPs should treat them
  637. as a single OR for the purpose of path selection.
  638. For example, if node A's descriptor contains "family B", and node B's
  639. descriptor contains "family A", then node A and node B should never
  640. be used on the same circuit.
  641. "read-history" YYYY-MM-DD HH:MM:SS (NSEC s) NUM,NUM,NUM,NUM,NUM... NL
  642. "write-history" YYYY-MM-DD HH:MM:SS (NSEC s) NUM,NUM,NUM,NUM,NUM... NL
  643. Declare how much bandwidth the OR has used recently. Usage is divided
  644. into intervals of NSEC seconds. The YYYY-MM-DD HH:MM:SS field defines
  645. the end of the most recent interval. The numbers are the number of
  646. bytes used in the most recent intervals, ordered from oldest to newest.
  647. [We didn't start parsing these lines until Tor 0.1.0.6-rc; they should
  648. be marked with "opt" until earlier versions of Tor are obsolete.]
  649. nickname ::= between 1 and 19 alphanumeric characters, case-insensitive.
  650. exitpattern ::= addrspec ":" portspec
  651. portspec ::= "*" | port | port "-" port
  652. port ::= an integer between 1 and 65535, inclusive.
  653. addrspec ::= "*" | ip4spec | ip6spec
  654. ipv4spec ::= ip4 | ip4 "/" num_ip4_bits | ip4 "/" ip4mask
  655. ip4 ::= an IPv4 address in dotted-quad format
  656. ip4mask ::= an IPv4 mask in dotted-quad format
  657. num_ip4_bits ::= an integer between 0 and 32
  658. ip6spec ::= ip6 | ip6 "/" num_ip6_bits
  659. ip6 ::= an IPv6 address, surrounded by square brackets.
  660. num_ip6_bits ::= an integer between 0 and 128
  661. Ports are required; if they are not included in the router
  662. line, they must appear in the "ports" lines.
  663. 7.3. Directory format
  664. A Directory begins with a "signed-directory" item, followed by one each of
  665. the following, in any order: "recommended-software", "published",
  666. "router-status", "dir-signing-key". It may include any number of "opt"
  667. items. After these items, a directory includes any number of router
  668. descriptors, and a single "directory-signature" item.
  669. "signed-directory"
  670. Indicates the start of a directory.
  671. "published" YYYY-MM-DD HH:MM:SS
  672. The time at which this directory was generated and signed, in GMT.
  673. "dir-signing-key"
  674. The key used to sign this directory; see "signing-key" for format.
  675. "recommended-software" comma-separated-version-list
  676. A list of which versions of which implementations are currently
  677. believed to be secure and compatible with the network.
  678. "running-routers" whitespace-separated-list
  679. A description of which routers are currently believed to be up or
  680. down. Every entry consists of an optional "!", followed by either an
  681. OR's nickname, or "$" followed by a hexadecimal encoding of the hash
  682. of an OR's identity key. If the "!" is included, the router is
  683. believed not to be running; otherwise, it is believed to be running.
  684. If a router's nickname is given, exactly one router of that nickname
  685. will appear in the directory, and that router is "approved" by the
  686. directory server. If a hashed identity key is given, that OR is not
  687. "approved". [XXXX The 'running-routers' line is only provided for
  688. backward compatibility. New code should parse 'router-status'
  689. instead.]
  690. "router-status" whitespace-separated-list
  691. A description of which routers are currently believed to be up or
  692. down, and which are verified or unverified. Contains one entry for
  693. every router that the directory server knows. Each entry is of the
  694. format:
  695. !name=$digest [Verified router, currently not live.]
  696. name=$digest [Verified router, currently live.]
  697. !$digest [Unverified router, currently not live.]
  698. or $digest [Unverified router, currently live.]
  699. (where 'name' is the router's nickname and 'digest' is a hexadecimal
  700. encoding of the hash of the routers' identity key).
  701. When parsing this line, clients should only mark a router as
  702. 'verified' if its nickname AND digest match the one provided.
  703. "directory-signature" nickname-of-dirserver NL Signature
  704. The signature is computed by computing the SHA-1 hash of the
  705. directory, from the characters "signed-directory", through the newline
  706. after "directory-signature". This digest is then padded with PKCS.1,
  707. and signed with the directory server's signing key.
  708. If software encounters an unrecognized keyword in a single router descriptor,
  709. it MUST reject only that router descriptor, and continue using the
  710. others. Because this mechanism is used to add 'critical' extensions to
  711. future versions of the router descriptor format, implementation should treat
  712. it as a normal occurrence and not, for example, report it to the user as an
  713. error. [Versions of Tor prior to 0.1.1 did this.]
  714. If software encounters an unrecognized keyword in the directory header,
  715. it SHOULD reject the entire directory.
  716. 7.4. Network-status descriptor
  717. A "network-status" (a.k.a "running-routers") document is a truncated
  718. directory that contains only the current status of a list of nodes, not
  719. their actual descriptors. It contains exactly one of each of the following
  720. entries.
  721. "network-status"
  722. Must appear first.
  723. "published" YYYY-MM-DD HH:MM:SS
  724. (see 7.3 above)
  725. "router-status" list
  726. (see 7.3 above)
  727. "directory-signature" NL signature
  728. (see 7.3 above)
  729. 7.5. Behavior of a directory server
  730. lists nodes that are connected currently
  731. speaks HTTP on a socket, spits out directory on request
  732. Directory servers listen on a certain port (the DirPort), and speak a
  733. limited version of HTTP 1.0. Clients send either GET or POST commands.
  734. The basic interactions are:
  735. "%s %s HTTP/1.0\r\nContent-Length: %lu\r\nHost: %s\r\n\r\n",
  736. command, url, content-length, host.
  737. Get "/tor/" to fetch a full directory.
  738. Get "/tor/dir.z" to fetch a compressed full directory.
  739. Get "/tor/running-routers" to fetch a network-status descriptor.
  740. Post "/tor/" to post a server descriptor, with the body of the
  741. request containing the descriptor.
  742. "host" is used to specify the address:port of the dirserver, so
  743. the request can survive going through HTTP proxies.
  744. A.1. Differences between spec and implementation
  745. - The current specification requires all ORs to have IPv4 addresses, but
  746. allows servers to exit and resolve to IPv6 addresses, and to declare IPv6
  747. addresses in their exit policies. The current codebase has no IPv6
  748. support at all.
  749. B. Things that should change in a later version of the Tor protocol
  750. B.1. ... but which will require backward-incompatible change
  751. - Circuit IDs should be longer.
  752. - IPv6 everywhere.
  753. - Maybe, keys should be longer.
  754. - Drop backward compatibility.
  755. - We should use a 128-bit subgroup of our DH prime.
  756. - Handshake should use HMAC.
  757. - Multiple cell lengths
  758. - Ability to split circuits across paths (If this is useful.)
  759. - SENDME windows should be dynamic.
  760. - Directory
  761. - Stop ever mentioning socks ports
  762. B.1. ... and that will require no changes
  763. - Mention multiple addr/port combos
  764. - Advertised outbound IP?
  765. - Migrate streams across circuits.
  766. B.2. ... and that we have no idea how to do.
  767. - UDP (as transport)
  768. - UDP (as content)
  769. - Use a better AES mode that has built-in integrity checking,
  770. doesn't grow with the number of hops, is not patented, and
  771. is implemented and maintained by smart people.