tor-spec.txt 43 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007
  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 K0, they compute 100 bytes of derivative
  208. key data as K = SHA1(K0 | [00]) | SHA1(K0 | [01]) | ... SHA1(K0 |
  209. [04]) where "00" is a single octet whose value is zero, [01] is a
  210. single octet whose value is one, etc. The first 20 bytes of K form
  211. KH, bytes 21-40 form the forward digest Df, 41-60 form the backward
  212. 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. 4.3. Creating circuits
  220. When creating a circuit through the network, the circuit creator
  221. (OP) performs the following steps:
  222. 1. Choose an onion router as an exit node (R_N), such that the onion
  223. router's exit policy includes at least one pending stream that
  224. needs a circuit (if there are any).
  225. 2. Choose a chain of (N-1) onion routers
  226. (R_1...R_N-1) to constitute the path, such that no router
  227. appears in the path twice.
  228. 3. If not already connected to the first router in the chain,
  229. open a new connection to that router.
  230. 4. Choose a circID not already in use on the connection with the
  231. first router in the chain; send a CREATE cell along the
  232. connection, to be received by the first onion router.
  233. 5. Wait until a CREATED cell is received; finish the handshake
  234. and extract the forward key Kf_1 and the backward key Kb_1.
  235. 6. For each subsequent onion router R (R_2 through R_N), extend
  236. the circuit to R.
  237. To extend the circuit by a single onion router R_M, the OP performs
  238. these steps:
  239. 1. Create an onion skin, encrypted to R_M's public key.
  240. 2. Send the onion skin in a relay EXTEND cell along
  241. the circuit (see section 5).
  242. 3. When a relay EXTENDED cell is received, verify KH, and
  243. calculate the shared keys. The circuit is now extended.
  244. When an onion router receives an EXTEND relay cell, it sends a CREATE
  245. cell to the next onion router, with the enclosed onion skin as its
  246. payload. The initiating onion router chooses some circID not yet
  247. used on the connection between the two onion routers. (But see
  248. section 4.1. above, concerning choosing circIDs based on
  249. lexicographic order of nicknames.)
  250. When an onion router receives a CREATE cell, if it already has a
  251. circuit on the given connection with the given circID, it drops the
  252. cell. Otherwise, after receiving the CREATE cell, it completes the
  253. DH handshake, and replies with a CREATED cell. Upon receiving a
  254. CREATED cell, an onion router packs it payload into an EXTENDED relay
  255. cell (see section 5), and sends that cell up the circuit. Upon
  256. receiving the EXTENDED relay cell, the OP can retrieve g^y.
  257. (As an optimization, OR implementations may delay processing onions
  258. until a break in traffic allows time to do so without harming
  259. network latency too greatly.)
  260. 4.4. Tearing down circuits
  261. Circuits are torn down when an unrecoverable error occurs along
  262. the circuit, or when all streams on a circuit are closed and the
  263. circuit's intended lifetime is over. Circuits may be torn down
  264. either completely or hop-by-hop.
  265. To tear down a circuit completely, an OR or OP sends a DESTROY
  266. cell to the adjacent nodes on that circuit, using the appropriate
  267. direction's circID.
  268. Upon receiving an outgoing DESTROY cell, an OR frees resources
  269. associated with the corresponding circuit. If it's not the end of
  270. the circuit, it sends a DESTROY cell for that circuit to the next OR
  271. in the circuit. If the node is the end of the circuit, then it tears
  272. down any associated edge connections (see section 5.1).
  273. After a DESTROY cell has been processed, an OR ignores all data or
  274. destroy cells for the corresponding circuit.
  275. To tear down part of a circuit, the OP may send a RELAY_TRUNCATE cell
  276. signaling a given OR (Stream ID zero). That OR sends a DESTROY
  277. cell to the next node in the circuit, and replies to the OP with a
  278. RELAY_TRUNCATED cell.
  279. When an unrecoverable error occurs along one connection in a
  280. circuit, the nodes on either side of the connection should, if they
  281. are able, act as follows: the node closer to the OP should send a
  282. RELAY_TRUNCATED cell towards the OP; the node farther from the OP
  283. should send a DESTROY cell down the circuit.
  284. The payload of a RELAY_TRUNCATED or DESTROY cell contains a single octet,
  285. describing why the circuit is being closed or truncated. When sending a
  286. TRUNCATED or DESTROY cell because of another TRUNCATED or DESTROY cell,
  287. the error code should be propagated. The origin of a circuit always sets
  288. this error code to 0, to avoid leaking its version.
  289. The error codes are:
  290. 0 -- NONE (No reason given.)
  291. 1 -- PROTOCOL (Tor protocol violation.)
  292. 2 -- INTERNAL (Internal error.)
  293. 3 -- REQUESTED (A client sent a TRUNCATE command.)
  294. 4 -- HIBERNATING (Not currently operating; trying to save bandwidth.)
  295. 5 -- RESOURCELIMIT (Out of memory, sockets, or circuit IDs.)
  296. 6 -- CONNECTFAILED (Unable to reach server.)
  297. 7 -- OR_IDENTITY (Connected to server, but its OR identity was not
  298. as expected.)
  299. 8 -- OR_CONN_CLOSED (The OR connection that was carrying this circuit
  300. died.)
  301. [Versions of Tor prior to 0.1.0.11 didn't sent versions; implementations
  302. MUST accept empty TRUNCATED and DESTROY cells.]
  303. 4.5. Routing relay cells
  304. When an OR receives a RELAY cell, it checks the cell's circID and
  305. determines whether it has a corresponding circuit along that
  306. connection. If not, the OR drops the RELAY cell.
  307. Otherwise, if the OR is not at the OP edge of the circuit (that is,
  308. either an 'exit node' or a non-edge node), it de/encrypts the payload
  309. with AES/CTR, as follows:
  310. 'Forward' relay cell (same direction as CREATE):
  311. Use Kf as key; decrypt.
  312. 'Back' relay cell (opposite direction from CREATE):
  313. Use Kb as key; encrypt.
  314. Note that in counter mode, decrypt and encrypt are the same operation.
  315. The OR then decides whether it recognizes the relay cell, by
  316. inspecting the payload as described in section 5.1 below. If the OR
  317. recognizes the cell, it processes the contents of the relay cell.
  318. Otherwise, it passes the decrypted relay cell along the circuit if
  319. the circuit continues. If the OR at the end of the circuit
  320. encounters an unrecognized relay cell, an error has occurred: the OR
  321. sends a DESTROY cell to tear down the circuit.
  322. When a relay cell arrives at an OP, the OP decrypts the payload
  323. with AES/CTR as follows:
  324. OP receives data cell:
  325. For I=N...1,
  326. Decrypt with Kb_I. If the payload is recognized (see
  327. section 5.1), then stop and process the payload.
  328. For more information, see section 5 below.
  329. 5. Application connections and stream management
  330. 5.1. Relay cells
  331. Within a circuit, the OP and the exit node use the contents of
  332. RELAY packets to tunnel end-to-end commands and TCP connections
  333. ("Streams") across circuits. End-to-end commands can be initiated
  334. by either edge; streams are initiated by the OP.
  335. The payload of each unencrypted RELAY cell consists of:
  336. Relay command [1 byte]
  337. 'Recognized' [2 bytes]
  338. StreamID [2 bytes]
  339. Digest [4 bytes]
  340. Length [2 bytes]
  341. Data [498 bytes]
  342. The relay commands are:
  343. 1 -- RELAY_BEGIN [forward]
  344. 2 -- RELAY_DATA [forward or backward]
  345. 3 -- RELAY_END [forward or backward]
  346. 4 -- RELAY_CONNECTED [backward]
  347. 5 -- RELAY_SENDME [forward or backward]
  348. 6 -- RELAY_EXTEND [forward]
  349. 7 -- RELAY_EXTENDED [backward]
  350. 8 -- RELAY_TRUNCATE [forward]
  351. 9 -- RELAY_TRUNCATED [backward]
  352. 10 -- RELAY_DROP [forward or backward]
  353. 11 -- RELAY_RESOLVE [forward]
  354. 12 -- RELAY_RESOLVED [backward]
  355. Commands labelled as "forward" must only be sent by the originator
  356. of the circuit. Commands labelled as "backward" must only be sent by
  357. other nodes in the circuit back to the originator. Commands marked
  358. as either can be sent either by the originator or other nodes.
  359. The 'recognized' field in any unencrypted relay payload is always set
  360. to zero; the 'digest' field is computed as the first four bytes of
  361. the running SHA-1 digest of all the bytes that have been destined for
  362. this hop of the circuit or originated from this hop of the circuit,
  363. seeded from Df or Db respectively (obtained in section 4.2 above),
  364. and including this RELAY cell's entire payload (taken with the digest
  365. field set to zero).
  366. When the 'recognized' field of a RELAY cell is zero, and the digest
  367. is correct, the cell is considered "recognized" for the purposes of
  368. decryption (see section 4.5 above).
  369. (The digest does not include any bytes from relay cells that do
  370. not start or end at this hop of the circuit. That is, it does not
  371. include forwarded data. Therefore if 'recognized' is zero but the
  372. digest does not match, the running digest at that node should
  373. not be updated, and the cell should be forwarded on.)
  374. All RELAY cells pertaining to the same tunneled stream have the
  375. same stream ID. StreamIDs are chosen arbitrarily by the OP. RELAY
  376. cells that affect the entire circuit rather than a particular
  377. stream use a StreamID of zero.
  378. The 'Length' field of a relay cell contains the number of bytes in
  379. the relay payload which contain real payload data. The remainder of
  380. the payload is padded with NUL bytes.
  381. If the RELAY cell is recognized but the relay command is not
  382. understood, the cell must be dropped and ignored. Its contents
  383. still count with respect to the digests, though. [Before
  384. 0.1.1.10, Tor closed circuits when it received an unknown relay
  385. command. Perhaps this will be more forward-compatible. -RD]
  386. 5.2. Opening streams and transferring data
  387. To open a new anonymized TCP connection, the OP chooses an open
  388. circuit to an exit that may be able to connect to the destination
  389. address, selects an arbitrary StreamID not yet used on that circuit,
  390. and constructs a RELAY_BEGIN cell with a payload encoding the address
  391. and port of the destination host. The payload format is:
  392. ADDRESS | ':' | PORT | [00]
  393. where ADDRESS can be a DNS hostname, or an IPv4 address in
  394. dotted-quad format, or an IPv6 address surrounded by square brackets;
  395. and where PORT is encoded in decimal.
  396. [What is the [00] for? -NM]
  397. [It's so the payload is easy to parse out with string funcs -RD]
  398. Upon receiving this cell, the exit node resolves the address as
  399. necessary, and opens a new TCP connection to the target port. If the
  400. address cannot be resolved, or a connection can't be established, the
  401. exit node replies with a RELAY_END cell. (See 5.4 below.)
  402. Otherwise, the exit node replies with a RELAY_CONNECTED cell, whose
  403. payload is in one of the following formats:
  404. The IPv4 address to which the connection was made [4 octets]
  405. A number of seconds (TTL) for which the address may be cached [4 octets]
  406. or
  407. Four zero-valued octets [4 octets]
  408. An address type (6) [1 octet]
  409. The IPv6 address to which the connection was made [16 octets]
  410. A number of seconds (TTL) for which the address may be cached [4 octets]
  411. [XXXX Versions of Tor before 0.1.1.6 ignore and do not generate the TTL
  412. field. No version of Tor currently generates the IPv6 format.]
  413. The OP waits for a RELAY_CONNECTED cell before sending any data.
  414. Once a connection has been established, the OP and exit node
  415. package stream data in RELAY_DATA cells, and upon receiving such
  416. cells, echo their contents to the corresponding TCP stream.
  417. RELAY_DATA cells sent to unrecognized streams are dropped.
  418. Relay RELAY_DROP cells are long-range dummies; upon receiving such
  419. a cell, the OR or OP must drop it.
  420. 5.3. Closing streams
  421. When an anonymized TCP connection is closed, or an edge node
  422. encounters error on any stream, it sends a 'RELAY_END' cell along the
  423. circuit (if possible) and closes the TCP connection immediately. If
  424. an edge node receives a 'RELAY_END' cell for any stream, it closes
  425. the TCP connection completely, and sends nothing more along the
  426. circuit for that stream.
  427. The payload of a RELAY_END cell begins with a single 'reason' byte to
  428. describe why the stream is closing, plus optional data (depending on
  429. the reason.) The values are:
  430. 1 -- REASON_MISC (catch-all for unlisted reasons)
  431. 2 -- REASON_RESOLVEFAILED (couldn't look up hostname)
  432. 3 -- REASON_CONNECTREFUSED (remote host refused connection) [*]
  433. 4 -- REASON_EXITPOLICY (OR refuses to connect to host or port)
  434. 5 -- REASON_DESTROY (Circuit is being destroyed)
  435. 6 -- REASON_DONE (Anonymized TCP connection was closed)
  436. 7 -- REASON_TIMEOUT (Connection timed out, or OR timed out
  437. while connecting)
  438. 8 -- (unallocated) [**]
  439. 9 -- REASON_HIBERNATING (OR is temporarily hibernating)
  440. 10 -- REASON_INTERNAL (Internal error at the OR)
  441. 11 -- REASON_RESOURCELIMIT (OR has no resources to fulfill request)
  442. 12 -- REASON_CONNRESET (Connection was unexpectedly reset)
  443. 13 -- REASON_TORPROTOCOL (Sent when closing connection because of
  444. Tor protocol violations.)
  445. (With REASON_EXITPOLICY, the 4-byte IPv4 address or 16-byte IPv6 address
  446. forms the optional data; no other reason currently has extra data.
  447. As of 0.1.1.6, the body also contains a 4-byte TTL.)
  448. OPs and ORs MUST accept reasons not on the above list, since future
  449. versions of Tor may provide more fine-grained reasons.
  450. [*] Older versions of Tor also send this reason when connections are
  451. reset.
  452. [**] Due to a bug in versions of Tor through 0095, error reason 8 must
  453. remain allocated until that version is obsolete.
  454. --- [The rest of this section describes unimplemented functionality.]
  455. Because TCP connections can be half-open, we follow an equivalent
  456. to TCP's FIN/FIN-ACK/ACK protocol to close streams.
  457. An exit connection can have a TCP stream in one of three states:
  458. 'OPEN', 'DONE_PACKAGING', and 'DONE_DELIVERING'. For the purposes
  459. of modeling transitions, we treat 'CLOSED' as a fourth state,
  460. although connections in this state are not, in fact, tracked by the
  461. onion router.
  462. A stream begins in the 'OPEN' state. Upon receiving a 'FIN' from
  463. the corresponding TCP connection, the edge node sends a 'RELAY_FIN'
  464. cell along the circuit and changes its state to 'DONE_PACKAGING'.
  465. Upon receiving a 'RELAY_FIN' cell, an edge node sends a 'FIN' to
  466. the corresponding TCP connection (e.g., by calling
  467. shutdown(SHUT_WR)) and changing its state to 'DONE_DELIVERING'.
  468. When a stream in already in 'DONE_DELIVERING' receives a 'FIN', it
  469. also sends a 'RELAY_FIN' along the circuit, and changes its state
  470. to 'CLOSED'. When a stream already in 'DONE_PACKAGING' receives a
  471. 'RELAY_FIN' cell, it sends a 'FIN' and changes its state to
  472. 'CLOSED'.
  473. If an edge node encounters an error on any stream, it sends a
  474. 'RELAY_END' cell (if possible) and closes the stream immediately.
  475. 5.4. Remote hostname lookup
  476. To find the address associated with a hostname, the OP sends a
  477. RELAY_RESOLVE cell containing the hostname to be resolved. (For a reverse
  478. lookup, the OP sends a RELAY_RESOLVE cell containing an in-addr.arpa
  479. address.) The OR replies with a RELAY_RESOLVED cell containing a status
  480. byte, and any number of answers. Each answer is of the form:
  481. Type (1 octet)
  482. Length (1 octet)
  483. Value (variable-width)
  484. TTL (4 octets)
  485. "Length" is the length of the Value field.
  486. "Type" is one of:
  487. 0x00 -- Hostname
  488. 0x04 -- IPv4 address
  489. 0x06 -- IPv6 address
  490. 0xF0 -- Error, transient
  491. 0xF1 -- Error, nontransient
  492. If any answer has a type of 'Error', then no other answer may be given.
  493. The RELAY_RESOLVE cell must use a nonzero, distinct streamID; the
  494. corresponding RELAY_RESOLVED cell must use the same streamID. No stream
  495. is actually created by the OR when resolving the name.
  496. 6. Flow control
  497. 6.1. Link throttling
  498. Each node should do appropriate bandwidth throttling to keep its
  499. user happy.
  500. Communicants rely on TCP's default flow control to push back when they
  501. stop reading.
  502. 6.2. Link padding
  503. Currently nodes are not required to do any sort of link padding or
  504. dummy traffic. Because strong attacks exist even with link padding,
  505. and because link padding greatly increases the bandwidth requirements
  506. for running a node, we plan to leave out link padding until this
  507. tradeoff is better understood.
  508. 6.3. Circuit-level flow control
  509. To control a circuit's bandwidth usage, each OR keeps track of
  510. two 'windows', consisting of how many RELAY_DATA cells it is
  511. allowed to package for transmission, and how many RELAY_DATA cells
  512. it is willing to deliver to streams outside the network.
  513. Each 'window' value is initially set to 1000 data cells
  514. in each direction (cells that are not data cells do not affect
  515. the window). When an OR is willing to deliver more cells, it sends a
  516. RELAY_SENDME cell towards the OP, with Stream ID zero. When an OR
  517. receives a RELAY_SENDME cell with stream ID zero, it increments its
  518. packaging window.
  519. Each of these cells increments the corresponding window by 100.
  520. The OP behaves identically, except that it must track a packaging
  521. window and a delivery window for every OR in the circuit.
  522. An OR or OP sends cells to increment its delivery window when the
  523. corresponding window value falls under some threshold (900).
  524. If a packaging window reaches 0, the OR or OP stops reading from
  525. TCP connections for all streams on the corresponding circuit, and
  526. sends no more RELAY_DATA cells until receiving a RELAY_SENDME cell.
  527. [this stuff is badly worded; copy in the tor-design section -RD]
  528. 6.4. Stream-level flow control
  529. Edge nodes use RELAY_SENDME cells to implement end-to-end flow
  530. control for individual connections across circuits. Similarly to
  531. circuit-level flow control, edge nodes begin with a window of cells
  532. (500) per stream, and increment the window by a fixed value (50)
  533. upon receiving a RELAY_SENDME cell. Edge nodes initiate RELAY_SENDME
  534. cells when both a) the window is <= 450, and b) there are less than
  535. ten cell payloads remaining to be flushed at that edge.
  536. 7. Directories and routers
  537. 7.1. Extensible information format
  538. Router descriptors and directories both obey the following lightweight
  539. extensible information format.
  540. The highest level object is a Document, which consists of one or more Items.
  541. Every Item begins with a KeywordLine, followed by one or more Objects. A
  542. KeywordLine begins with a Keyword, optionally followed by whitespace and more
  543. non-newline characters, and ends with a newline. A Keyword is a sequence of
  544. one or more characters in the set [A-Za-z0-9-]. An Object is a block of
  545. encoded data in pseudo-Open-PGP-style armor. (cf. RFC 2440)
  546. More formally:
  547. Document ::= (Item | NL)+
  548. Item ::= KeywordLine Object*
  549. KeywordLine ::= Keyword NL | Keyword WS ArgumentsChar+ NL
  550. Keyword = KeywordChar+
  551. KeywordChar ::= 'A' ... 'Z' | 'a' ... 'z' | '0' ... '9' | '-'
  552. ArgumentChar ::= any printing ASCII character except NL.
  553. WS = (SP | TAB)+
  554. Object ::= BeginLine Base-64-encoded-data EndLine
  555. BeginLine ::= "-----BEGIN " Keyword "-----" NL
  556. EndLine ::= "-----END " Keyword "-----" NL
  557. The BeginLine and EndLine of an Object must use the same keyword.
  558. When interpreting a Document, software MUST reject any document containing a
  559. KeywordLine that starts with a keyword it doesn't recognize.
  560. The "opt" keyword is reserved for non-critical future extensions. All
  561. implementations MUST ignore any item of the form "opt keyword ....." when
  562. they would not recognize "keyword ....."; and MUST treat "opt keyword ....."
  563. as synonymous with "keyword ......" when keyword is recognized.
  564. 7.2. Router descriptor format.
  565. Every router descriptor MUST start with a "router" Item; MUST end with a
  566. "router-signature" Item and an extra NL; and MUST contain exactly one
  567. instance of each of the following Items: "published" "onion-key" "link-key"
  568. "signing-key" "bandwidth". Additionally, a router descriptor MAY contain any
  569. number of "accept", "reject", "fingerprint", "uptime", and "opt" Items.
  570. Other than "router" and "router-signature", the items may appear in any
  571. order.
  572. The items' formats are as follows:
  573. "router" nickname address ORPort SocksPort DirPort
  574. Indicates the beginning of a router descriptor. "address"
  575. must be an IPv4 address in dotted-quad format. The last
  576. three numbers indicate the TCP ports at which this OR exposes
  577. functionality. ORPort is a port at which this OR accepts TLS
  578. connections for the main OR protocol; SocksPort is deprecated and
  579. should always be 0; and DirPort is the port at which this OR accepts
  580. directory-related HTTP connections. If any port is not supported,
  581. the value 0 is given instead of a port number.
  582. "bandwidth" bandwidth-avg bandwidth-burst bandwidth-observed
  583. Estimated bandwidth for this router, in bytes per second. The
  584. "average" bandwidth is the volume per second that the OR is willing
  585. to sustain over long periods; the "burst" bandwidth is the volume
  586. that the OR is willing to sustain in very short intervals. The
  587. "observed" value is an estimate of the capacity this server can
  588. handle. The server remembers the max bandwidth sustained output
  589. over any ten second period in the past day, and another sustained
  590. input. The "observed" value is the lesser of these two numbers.
  591. "platform" string
  592. A human-readable string describing the system on which this OR is
  593. running. This MAY include the operating system, and SHOULD include
  594. the name and version of the software implementing the Tor protocol.
  595. "published" YYYY-MM-DD HH:MM:SS
  596. The time, in GMT, when this descriptor was generated.
  597. "fingerprint"
  598. A fingerprint (20 byte SHA1 hash of asn1 encoded public key, encoded
  599. in hex, with a single space after every 4 characters) for this router's
  600. identity key.
  601. [We didn't start parsing this line until Tor 0.1.0.6-rc; it should
  602. be marked with "opt" until earlier versions of Tor are obsolete.]
  603. "hibernating" 0|1
  604. If the value is 1, then the Tor server was hibernating when the
  605. descriptor was published, and shouldn't be used to build circuits.
  606. [We didn't start parsing this line until Tor 0.1.0.6-rc; it should
  607. be marked with "opt" until earlier versions of Tor are obsolete.]
  608. "uptime"
  609. The number of seconds that this OR process has been running.
  610. "onion-key" NL a public key in PEM format
  611. This key is used to encrypt EXTEND cells for this OR. The key MUST
  612. be accepted for at least XXXX hours after any new key is published in
  613. a subsequent descriptor.
  614. "signing-key" NL a public key in PEM format
  615. The OR's long-term identity key.
  616. "accept" exitpattern
  617. "reject" exitpattern
  618. These lines, in order, describe the rules that an OR follows when
  619. deciding whether to allow a new stream to a given address. The
  620. 'exitpattern' syntax is described below.
  621. "router-signature" NL Signature NL
  622. The "SIGNATURE" object contains a signature of the PKCS1-padded SHA1
  623. hash of the entire router descriptor, taken from the beginning of the
  624. "router" line, through the newline after the "router-signature" line.
  625. The router descriptor is invalid unless the signature is performed
  626. with the router's identity key.
  627. "contact" info NL
  628. Describes a way to contact the server's administrator, preferably
  629. including an email address and a PGP key fingerprint.
  630. "family" names NL
  631. 'Names' is a whitespace-separated list of server nicknames. If two ORs
  632. list one another in their "family" entries, then OPs should treat them
  633. as a single OR for the purpose of path selection.
  634. For example, if node A's descriptor contains "family B", and node B's
  635. descriptor contains "family A", then node A and node B should never
  636. be used on the same circuit.
  637. "read-history" YYYY-MM-DD HH:MM:SS (NSEC s) NUM,NUM,NUM,NUM,NUM... NL
  638. "write-history" YYYY-MM-DD HH:MM:SS (NSEC s) NUM,NUM,NUM,NUM,NUM... NL
  639. Declare how much bandwidth the OR has used recently. Usage is divided
  640. into intervals of NSEC seconds. The YYYY-MM-DD HH:MM:SS field defines
  641. the end of the most recent interval. The numbers are the number of
  642. bytes used in the most recent intervals, ordered from oldest to newest.
  643. [We didn't start parsing these lines until Tor 0.1.0.6-rc; they should
  644. be marked with "opt" until earlier versions of Tor are obsolete.]
  645. nickname ::= between 1 and 19 alphanumeric characters, case-insensitive.
  646. exitpattern ::= addrspec ":" portspec
  647. portspec ::= "*" | port | port "-" port
  648. port ::= an integer between 1 and 65535, inclusive.
  649. addrspec ::= "*" | ip4spec | ip6spec
  650. ipv4spec ::= ip4 | ip4 "/" num_ip4_bits | ip4 "/" ip4mask
  651. ip4 ::= an IPv4 address in dotted-quad format
  652. ip4mask ::= an IPv4 mask in dotted-quad format
  653. num_ip4_bits ::= an integer between 0 and 32
  654. ip6spec ::= ip6 | ip6 "/" num_ip6_bits
  655. ip6 ::= an IPv6 address, surrounded by square brackets.
  656. num_ip6_bits ::= an integer between 0 and 128
  657. Ports are required; if they are not included in the router
  658. line, they must appear in the "ports" lines.
  659. 7.3. Directory format
  660. A Directory begins with a "signed-directory" item, followed by one each of
  661. the following, in any order: "recommended-software", "published",
  662. "router-status", "dir-signing-key". It may include any number of "opt"
  663. items. After these items, a directory includes any number of router
  664. descriptors, and a single "directory-signature" item.
  665. "signed-directory"
  666. Indicates the start of a directory.
  667. "published" YYYY-MM-DD HH:MM:SS
  668. The time at which this directory was generated and signed, in GMT.
  669. "dir-signing-key"
  670. The key used to sign this directory; see "signing-key" for format.
  671. "recommended-software" comma-separated-version-list
  672. A list of which versions of which implementations are currently
  673. believed to be secure and compatible with the network.
  674. "running-routers" whitespace-separated-list
  675. A description of which routers are currently believed to be up or
  676. down. Every entry consists of an optional "!", followed by either an
  677. OR's nickname, or "$" followed by a hexadecimal encoding of the hash
  678. of an OR's identity key. If the "!" is included, the router is
  679. believed not to be running; otherwise, it is believed to be running.
  680. If a router's nickname is given, exactly one router of that nickname
  681. will appear in the directory, and that router is "approved" by the
  682. directory server. If a hashed identity key is given, that OR is not
  683. "approved". [XXXX The 'running-routers' line is only provided for
  684. backward compatibility. New code should parse 'router-status'
  685. instead.]
  686. "router-status" whitespace-separated-list
  687. A description of which routers are currently believed to be up or
  688. down, and which are verified or unverified. Contains one entry for
  689. every router that the directory server knows. Each entry is of the
  690. format:
  691. !name=$digest [Verified router, currently not live.]
  692. name=$digest [Verified router, currently live.]
  693. !$digest [Unverified router, currently not live.]
  694. or $digest [Unverified router, currently live.]
  695. (where 'name' is the router's nickname and 'digest' is a hexadecimal
  696. encoding of the hash of the routers' identity key).
  697. When parsing this line, clients should only mark a router as
  698. 'verified' if its nickname AND digest match the one provided.
  699. "directory-signature" nickname-of-dirserver NL Signature
  700. The signature is computed by computing the SHA-1 hash of the
  701. directory, from the characters "signed-directory", through the newline
  702. after "directory-signature". This digest is then padded with PKCS.1,
  703. and signed with the directory server's signing key.
  704. If software encounters an unrecognized keyword in a single router descriptor,
  705. it MUST reject only that router descriptor, and continue using the
  706. others. Because this mechanism is used to add 'critical' extensions to
  707. future versions of the router descriptor format, implementation should treat
  708. it as a normal occurrence and not, for example, report it to the user as an
  709. error. [Versions of Tor prior to 0.1.1 did this.]
  710. If software encounters an unrecognized keyword in the directory header,
  711. it SHOULD reject the entire directory.
  712. 7.4. Network-status descriptor
  713. A "network-status" (a.k.a "running-routers") document is a truncated
  714. directory that contains only the current status of a list of nodes, not
  715. their actual descriptors. It contains exactly one of each of the following
  716. entries.
  717. "network-status"
  718. Must appear first.
  719. "published" YYYY-MM-DD HH:MM:SS
  720. (see 7.3 above)
  721. "router-status" list
  722. (see 7.3 above)
  723. "directory-signature" NL signature
  724. (see 7.3 above)
  725. 7.5. Behavior of a directory server
  726. lists nodes that are connected currently
  727. speaks HTTP on a socket, spits out directory on request
  728. Directory servers listen on a certain port (the DirPort), and speak a
  729. limited version of HTTP 1.0. Clients send either GET or POST commands.
  730. The basic interactions are:
  731. "%s %s HTTP/1.0\r\nContent-Length: %lu\r\nHost: %s\r\n\r\n",
  732. command, url, content-length, host.
  733. Get "/tor/" to fetch a full directory.
  734. Get "/tor/dir.z" to fetch a compressed full directory.
  735. Get "/tor/running-routers" to fetch a network-status descriptor.
  736. Post "/tor/" to post a server descriptor, with the body of the
  737. request containing the descriptor.
  738. "host" is used to specify the address:port of the dirserver, so
  739. the request can survive going through HTTP proxies.
  740. A.1. Differences between spec and implementation
  741. - The current specification requires all ORs to have IPv4 addresses, but
  742. allows servers to exit and resolve to IPv6 addresses, and to declare IPv6
  743. addresses in their exit policies. The current codebase has no IPv6
  744. support at all.
  745. B. Things that should change in a later version of the Tor protocol
  746. B.1. ... but which will require backward-incompatible change
  747. - Circuit IDs should be longer.
  748. - IPv6 everywhere.
  749. - Maybe, keys should be longer.
  750. - Drop backward compatibility.
  751. - We should use a 128-bit subgroup of our DH prime.
  752. - Handshake should use HMAC.
  753. - Multiple cell lengths
  754. - Ability to split circuits across paths (If this is useful.)
  755. - SENDME windows should be dynamic.
  756. - Directory
  757. - Stop ever mentioning socks ports
  758. B.1. ... and that will require no changes
  759. - Mention multiple addr/port combos
  760. - Advertised outbound IP?
  761. - Migrate streams across circuits.
  762. B.2. ... and that we have no idea how to do.
  763. - UDP (as transport)
  764. - UDP (as content)
  765. - Use a better AES mode that has built-in integrity checking,
  766. doesn't grow with the number of hops, is not patented, and
  767. is implemented and maintained by smart people.