protocol-1.0.txt 18 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397
  1. This file aims to document the low-level details of communication and protocol
  2. in Ricochet.
  3. Unless otherwise noted, this document describes protocol version 0. Section 1
  4. (protocol negotiation) is intended to be invariant, as it is responsible for
  5. negotiating the protocol version.
  6. Table of contents:
  7. 0. Conventions in this document
  8. 1. The hidden service layer
  9. 2. Protocol negotiation
  10. 2.1. Introduction syntax
  11. 3. Connection purpose and authentication
  12. 3.1. Authenticated contact connections
  13. 4. Connection management
  14. 4.1. Command connections
  15. 4.2. Data connections
  16. 5. Message processing
  17. 6. Commands and replies
  18. 6.1. Length field
  19. 6.2. Command field
  20. 6.3. State field
  21. 6.4. Identifier field
  22. 6.5. Data
  23. 7. Defined commands
  24. 7.1. 0x00 - Ping
  25. 7.2. 0x01 - Get connection secret
  26. 7.3. 0x10 - Chat message
  27. 8. Contact request connections
  28. 0. Conventions in this document
  29. 'Client' refers to the peer creating the connection
  30. 'Server' refers to the peer receiving an incoming connection. After
  31. authentication, this distinction is irrelevant.
  32. 1. The hidden service layer
  33. [TBD: describe use of hidden services and the properties they make available]
  34. Connections are made to port 9878 of the hidden service.
  35. 2. Protocol negotiation
  36. Immediately after establishing a connection, the client must send an
  37. introduction. The server is expected to expire any connection on which
  38. this introduction does not arrive in a reasonable amount of time.
  39. 2.1. Introduction syntax
  40. The client must send the following sequence:
  41. 0x49
  42. 0x4D
  43. nVersions [1 octet]
  44. versions [nVersions bytes]
  45. 'nVersions' describes the number of version fields to follow. Each version
  46. field is a single octet, where each value identifies an incompatible
  47. protocol. The value 0xFF is reserved to indicate failure in a later reply.
  48. In response, once the entire introduction has been received, the server
  49. responds with a single octet containing the protocol version chosen from
  50. the list provided by the client. This should be the highest mutually
  51. supported version. If there is no mutual version, the server sends 0xff
  52. (which is not a valid version value), and terminates the connection.
  53. Implementations MUST NOT expect any more than 4 octets (the introduction
  54. with one version) to arrive before attempting to process them. The client
  55. MUST NOT expect more than one octet (the response version) before
  56. processing that data, regardless of any data which may immediately follow.
  57. 3. Connection purpose and authentication
  58. Immediately after version negotiation has finished (i.e. the server has
  59. responded), the client must send a connection purpose field, which defines
  60. the type of connection. This is a single octet, with the following
  61. currently recognized values:
  62. 0x00 Contact command connection
  63. 0x01 Contact data connection
  64. 0x80 Contact request
  65. Any value below 0x20 (non-inclusive) shares the same immediate usage for
  66. authentication. The 0x00 value indicates a command connection (4.),
  67. while all others are auxiliary connections. The protocol version must be
  68. increased when introducing a new purpose.
  69. 3.1. Authenticated contact connections
  70. For connections with a purpose value below 0x20, the following
  71. authentication process is used to prove the identity of existing
  72. contacts.
  73. After sending the purpose, the client must follow with a 16-octet
  74. pre-exchanged secret. This secret is specific to each peer, and is defined
  75. during the contact request (or shortly thereafter). Because of the
  76. hidden service layer pre-authenticating the server end of the connection,
  77. and the existing layers of encryption, this value is sent in plaintext.
  78. The server looks up this value and matches it to any known contacts.
  79. It then sends a one-octet response code; the value 0x00 for this code
  80. indicates success, while all other values are assumed to be failure.
  81. Any failure response may be followed by the server immediately closing
  82. the connection, or may be followed by additional information. No response
  83. other than 0x00 will result in the connection continuing to exist. The
  84. following responses are currently defined:
  85. 0x00 Success
  86. 0x01 General failure; immediately closes the connection
  87. 0x02 Unrecognized secret
  88. After a success reply, the connection transitions into message parsing
  89. and may be used bidirectionally as desired. The "unrecognized secret"
  90. response should be considered a permanent failure and disable future
  91. connection attempts without issuing a new contact request.
  92. 4. Connection management
  93. 4.1. Command connections
  94. Connections initiated with a purpose of 0x00 (see section 3.) are command
  95. connections, over which the protocol defined in section 5 through 7 is
  96. used. Generally, there may only be one command connection with a peer,
  97. and a new connection is considered to replace any existing connection
  98. (causing failure of all incomplete commands).
  99. A race is possible when two peers connect simultaniously and each
  100. establish a command connection. To resolve this situation, the following
  101. rules are used:
  102. - If the remote peer establishes a command connection prior to
  103. sending authentication data for an outgoing attempt, the outgoing
  104. attempt is aborted and the peer's connection is used.
  105. - If the existing connection is more than 30 seconds old, measured
  106. from the time authentication is sent, it is replaced and closed.
  107. - Otherwise, both peers close the connection for which the server's
  108. onion-formatted hostname is considered less by a strcmp function.
  109. 4.2. Data connections
  110. All commands and general communication take place over the command
  111. connection. It's important that command connections keep low latency
  112. regardless of throughput, so they are not suitable for transferring
  113. large data.
  114. Peers may establish data connections with a purpose of 0x01 for the
  115. transfer of larger amounts of data. These transfers must first be
  116. negotiated on the command connection, where the receiving peer assigns
  117. a 4 octet identifier for the blob of data.
  118. Either peer may use a data connection regardless of why it was
  119. originally established. Connections should not be closed by expiration
  120. if a pending identifier has been issued for the peer's use. It should
  121. be possible for the sending peer in a transfer to indicate that the
  122. recipient should establish a data connection, to avoid relying on
  123. the connectivity of both peers.
  124. Data arrives in the following form:
  125. identifier [4 octets]
  126. length [64-bit unsigned big endian integer]
  127. data [length octets]
  128. Unexpected identifiers may be ignored or result in closing the
  129. data connection.
  130. 5. Message processing
  131. Most communication takes place over the command or message protocol, used
  132. on authenticated connections with contacts. This is a sequence of messages,
  133. each of which is a command or a reply.
  134. All commands must generate a reply, unless otherwise specifically defined.
  135. This is done to allow intelligent handling of various error situations, and
  136. to enable better internal API for managing outstanding commands. Replies
  137. have a concept of finality; a command may generate many replies, but must
  138. have exactly one final reply, which obviously must be the last.
  139. Each command also has an identifier; this is a 16-bit value unique to the
  140. sender of the command, which serves to relate replies back to the original
  141. command. As a result of this, there is no requirement that replies be
  142. immediate, and they may even be interleaved with other replies. The
  143. identifier may be considered unused immediately after a final reply arrives
  144. with that identifier set. Identifiers are specific to a single connection.
  145. 0 is reserved for identifiers, and should not be used.
  146. One last distinction is between two types of messages that have very
  147. different behavior for processing. Buffered messages (which are the
  148. majority) include their length, which may not exceed 65,540 octets
  149. inclusive of the header and the length itself, meaning that they may
  150. have a maximum payload of 65,534 octets. As the name implies, buffered
  151. messages are buffered until all data has arrived, and processed once.
  152. 6. Commands and replies
  153. The syntax of a message (a command or reply) is:
  154. length [16-bit big endian integer]
  155. command [1 octet]
  156. state [1 octet]
  157. identifier [16-bit big endian integer]
  158. data [length-1 octets, or unspecified]
  159. These fields will be addressed in order.
  160. 6.1. Length field
  161. The length is a 16-bit big endian integer defining the size in bytes of the
  162. data payload in the message. This length does not include the header of the
  163. message. It may be 0 for messages with no associated data.
  164. 6.2. Command field
  165. The command is a one octet identifier for the command that should be
  166. performed. When adding commands, great care should be taken to avoid
  167. collisions with any other implementation. Command values below 0x80 should
  168. be reserved for definition in this official document, while those above
  169. are open for third party usage with appropriate precautions. A feature
  170. inspection command may be introduced in the future to handle this issue.
  171. For details on existing commands, see section 7.
  172. 6.3. State field
  173. The state field can be thought of as a subdivision of the command. It
  174. enables different usages of the same command, and is also used to indicate
  175. replies and reply status. The state is one octet, which is to be
  176. interpreted as follows, where 0 is the most significant bit (0x80), and 7
  177. is the least significant (0x01).
  178. Bit 0 (0x80) indicates if the message is a reply.
  179. If bit 0 is NOT set, the message is a command:
  180. Bit 1 (0x40) is reserved, and should have a value of 1.
  181. Bits 2 through 7 are available for command-specific usage.
  182. If bit 0 IS set:
  183. Bit 1 (0x40) indicates if the reply is a final reply. No more replies
  184. may arrive with the same identifier for that command after the final
  185. reply.
  186. Bit 2 (0x20) indicates if the command was successful. This bit should
  187. be set for any reply which does not indicate failure, but is largely
  188. intepreted by the command itself.
  189. IF bit 2 is NOT set (i.e. the command failed), all values with bit 3
  190. (0x10) set are reserved for protocol usage as defined below.
  191. All other bits (3-7 for success, 4-7 for failure) are available for
  192. command-specific usage.
  193. The following chart describes these values and their meaning:
  194. 0x00 to 0x3F Reserved (unused)
  195. 0x40 to 0x7F Command
  196. 0x80 to 0x8F Reply Intermediate Failure Available
  197. 0x90 to 0x9F Reply Intermediate Failure Reserved
  198. 0xA0 to 0xBF Reply Intermediate Successful
  199. 0xC0 to 0xCF Reply Final Failure Available
  200. 0xD0 to 0xDF Reply Final Failure Reserved
  201. 0xE0 to 0xFF Reply Final Successful
  202. 6.4. Identifier field
  203. The identifier is a 16-bit big endian integer, selected when sending the
  204. command. It is unique to the peer and connection. It is illegal to send a
  205. command with the same identifier as a command for which a final reply has
  206. not arrived. It is illegal to send a reply with an identifier that does not
  207. match an outstanding command from the peer.
  208. The identifier 0 is reserved.
  209. 6.5. Data
  210. The data is arbitrary data, specific to the command (and possibly state).
  211. Its length is defined by the length field, which may be 0.
  212. 7. Defined commands
  213. This is a comprehensive listing of all commands implemented in the version
  214. of Ricochet to which this document corrosponds, and potentially those used
  215. by other applications that are considered reserved for that purpose.
  216. 7.1. 0x00 - Ping
  217. The ping command has no data, and results in a single, final, successful
  218. reply with a command-specific state of 0 (0xE0). It has no other effect.
  219. 7.2. 0x01 - Get connection secret
  220. The command has no data. If successful, the peer will send a single, final
  221. reply with a command state of 0 (0xE0), and the 16-byte secret that is
  222. used to authenticate the local (command outgoing) end when establishing a
  223. connection.
  224. This is a dirty trick used during contact requests to allow the requesting
  225. end of the request to discover the secret that it should use.
  226. 7.3. 0x10 - Chat message
  227. The command sends the following data:
  228. timeDelta 32-bit signed big-endian integer; seconds
  229. before now that the message was written. A
  230. negative value (indicating the future) may be
  231. rejected.
  232. lastReceived 16-bit big-endian integer; command identifier
  233. of the last chat message that had been received
  234. from the peer when this message was sent.
  235. length 16-big unsigned big-endian integer; length in
  236. octets of the text field
  237. text UTF8-encoded message text of 'length' octets.
  238. If successful, a single, final reply will be sent.
  239. 8. Contact request connections
  240. Contact requests are indicated by a connection with a purpose of 0x80.
  241. Immediately after receiving the purpose, the server will respond with a
  242. 16-octet randomly generated cookie.
  243. Once the cookie is received, the client (i.e. the peer that is sending a
  244. request) must send the following structure:
  245. length 16-bit big-endian unsigned integer; length in
  246. octets of the entire request message, inclusive
  247. of itself.
  248. serverHostname 16 octets, base32-encoded onion hostname of the
  249. intended recipient of the request.
  250. serverCookie 16 octets, the cookie provided by the server
  251. connSecret 16 octets, random data that the recipient can
  252. use to authenticate for a contact connection.
  253. pubKeyLength 16-bit big-endian unsigned integer; length in
  254. octets of the pubKey field
  255. pubKey PEM-encoded hidden service public key of
  256. pubKeyLength octets
  257. nicknameLength 16-bit big-endian unsigned integer; length in
  258. octets of the nickname field
  259. nickname UTF-8 encoded string of nicknameLength octets;
  260. suggested nickname for this contact
  261. messageLength 16-bit big-endian unsigned integer; length in
  262. octets of the message field
  263. message UTF-8 encoded string of messageLength octets;
  264. freeform message to be shown with the request
  265. signatureLength 16-bit big-endian unsigned integer; length in
  266. octets of the signature field
  267. signature RSA signature of the SHA256 digest of the fields
  268. from serverHostname to message inclusive.
  269. Upon receiving the request, the server must verify that it is correct:
  270. - length must be greater than 58
  271. - serverHostname must equal the onion hostname that received this request
  272. - serverCookie must equal the random cookie sent by the server
  273. - pubKey must be a parseable and valid RSA public key
  274. - at least one of nickname or message must be non-empty
  275. - signature must be a valid signature by pubKey of the previous fields,
  276. excluding the length.
  277. Verifying serverHostname and serverCookie prevents replay and forwarding
  278. attacks on the request. The signature authenticates that the origin of the
  279. request is able to publish the hidden service represented by pubKey. The
  280. requesting service's hostname can be calculated from pubKey.
  281. After the request, the server will respond with a response code. All
  282. responses with the exception of 0x00 (acknowledge) and 0x01 (accept) are
  283. considered errors. The following are currently-defined responses:
  284. 0x00 Acknowledged (see below)
  285. 0x01 Accepted (see below)
  286. 0x40 Rejected by user
  287. 0x80 Syntax error
  288. 0x81 Verification error
  289. 0x82 Request requires either a nickname or a message
  290. All of the error responses (that is, any with a value greater than 0x01)
  291. will be followed closing the connection. In the case of user rejection,
  292. and potentially others, the contact ID used for this request may be added
  293. to a blacklist, causing any further requests to be immediately rejected
  294. without notifying the user.
  295. The acknowledged response is a special case; this indicates that the
  296. request was valid and has been processed, but that no response has yet been
  297. made. After the acknowledged response, the client should leave open the
  298. connection and wait for another response code to arrive. However, this is
  299. not a requirement.
  300. Immediately after the accepted response, both peers are expected to
  301. transition the connection to an established and authenticated command
  302. connection (i.e., they begin the message protocol).
  303. The requesting peer MUST be willing to accept normal contact connections,
  304. authenticated with the connSecret field from the request, at any time after
  305. the request has been sent. If such a connection is established, the
  306. requesting peer should treat that as implicitly accepting the request. This
  307. allows the request to be accepted if the request connection has since been
  308. lost.