tor-spec.txt 45 KB

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