cadet_protocol.h 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507
  1. /*
  2. This file is part of GNUnet.
  3. Copyright (C) 2007 - 2017 GNUnet e.V.
  4. GNUnet is free software: you can redistribute it and/or modify it
  5. under the terms of the GNU Affero General Public License as published
  6. by the Free Software Foundation, either version 3 of the License,
  7. or (at your option) any later version.
  8. GNUnet is distributed in the hope that it will be useful, but
  9. WITHOUT ANY WARRANTY; without even the implied warranty of
  10. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  11. Affero General Public License for more details.
  12. You should have received a copy of the GNU Affero General Public License
  13. along with this program. If not, see <http://www.gnu.org/licenses/>.
  14. SPDX-License-Identifier: AGPL3.0-or-later
  15. */
  16. /**
  17. * @file cadet/cadet_protocol.h
  18. * @brief P2P messages used by CADET
  19. * @author Bartlomiej Polot
  20. * @author Christian Grothoff
  21. */
  22. #ifndef CADET_PROTOCOL_H_
  23. #define CADET_PROTOCOL_H_
  24. /**
  25. * At best, enable when debugging #5328!
  26. */
  27. #define DEBUG_KX 0
  28. #if DEBUG_KX
  29. #warning NEVER run this in production! KX debugging is on!
  30. #endif
  31. #include "platform.h"
  32. #include "gnunet_util_lib.h"
  33. #include "cadet.h"
  34. #ifdef __cplusplus
  35. struct GNUNET_CADET_TunnelMessage;
  36. extern "C"
  37. {
  38. #if 0
  39. /* keep Emacsens' auto-indent happy */
  40. }
  41. #endif
  42. #endif
  43. /******************************************************************************/
  44. /******************** CADET NETWORK MESSAGES **************************/
  45. /******************************************************************************/
  46. GNUNET_NETWORK_STRUCT_BEGIN
  47. /******************************************************************************/
  48. /***************************** CONNECTION **********************************/
  49. /******************************************************************************/
  50. /**
  51. * Message for cadet connection creation.
  52. */
  53. struct GNUNET_CADET_ConnectionCreateMessage {
  54. /**
  55. * Type: #GNUNET_MESSAGE_TYPE_CADET_CONNECTION_CREATE
  56. *
  57. * Size: sizeof (struct GNUNET_CADET_ConnectionCreateMessage) +
  58. * path_length * sizeof (struct GNUNET_PeerIdentity)
  59. */
  60. struct GNUNET_MessageHeader header;
  61. /**
  62. * Connection options in network byte order.
  63. * #GNUNET_CADET_OPTION_DEFAULT for buffered;
  64. * #GNUNET_CADET_OPTION_NOBUFFER for unbuffered.
  65. * Other flags are ignored and should not be set at this level.
  66. */
  67. uint32_t options GNUNET_PACKED;
  68. /**
  69. * ID of the connection
  70. */
  71. struct GNUNET_CADET_ConnectionTunnelIdentifier cid;
  72. /**
  73. * path_length structs defining the *whole* path from the origin [0] to the
  74. * final destination [path_length-1].
  75. */
  76. /* struct GNUNET_PeerIdentity peers[path_length]; */
  77. };
  78. /**
  79. * Message for ack'ing a connection
  80. */
  81. struct GNUNET_CADET_ConnectionCreateAckMessage {
  82. /**
  83. * Type: #GNUNET_MESSAGE_TYPE_CADET_CONNECTION_CREATE_ACK
  84. */
  85. struct GNUNET_MessageHeader header;
  86. /**
  87. * For alignment.
  88. */
  89. uint32_t reserved GNUNET_PACKED;
  90. /**
  91. * ID of the connection.
  92. */
  93. struct GNUNET_CADET_ConnectionTunnelIdentifier cid;
  94. };
  95. /**
  96. * Message for notifying a disconnection in a path
  97. */
  98. struct GNUNET_CADET_ConnectionBrokenMessage {
  99. /**
  100. * Type: #GNUNET_MESSAGE_TYPE_CADET_CONNECTION_BROKEN.
  101. */
  102. struct GNUNET_MessageHeader header;
  103. /**
  104. * For alignment.
  105. */
  106. uint32_t reserved GNUNET_PACKED;
  107. /**
  108. * ID of the connection.
  109. */
  110. struct GNUNET_CADET_ConnectionTunnelIdentifier cid;
  111. /**
  112. * ID of the endpoint
  113. */
  114. struct GNUNET_PeerIdentity peer1;
  115. /**
  116. * ID of the endpoint
  117. */
  118. struct GNUNET_PeerIdentity peer2;
  119. };
  120. /**
  121. * Message to destroy a connection.
  122. */
  123. struct GNUNET_CADET_ConnectionDestroyMessage {
  124. /**
  125. * Type: #GNUNET_MESSAGE_TYPE_CADET_CONNECTION_DESTROY
  126. */
  127. struct GNUNET_MessageHeader header;
  128. /**
  129. * For alignment.
  130. */
  131. uint32_t reserved GNUNET_PACKED;
  132. /**
  133. * ID of the connection.
  134. */
  135. struct GNUNET_CADET_ConnectionTunnelIdentifier cid;
  136. };
  137. /******************************************************************************/
  138. /******************************* TUNNEL ***********************************/
  139. /******************************************************************************/
  140. /**
  141. * Unique identifier (counter) for an encrypted message in a channel.
  142. * Used to match #GNUNET_MESSAGE_TYPE_CADET_CONNECTION_HOP_BY_HOP_ENCRYPTED_ACK
  143. * and #GNUNET_MESSAGE_TYPE_CADET_TUNNEL_ENCRYPTED_POLL messages
  144. * against the respective #GNUNET_MESSAGE_TYPE_CADET_TUNNEL_ENCRYPTED
  145. * messages.
  146. */
  147. struct CadetEncryptedMessageIdentifier {
  148. /**
  149. * This number is incremented by one per message. It may wrap around.
  150. * In network byte order.
  151. */
  152. uint32_t pid GNUNET_PACKED;
  153. };
  154. /**
  155. * Flags to be used in GNUNET_CADET_KX.
  156. */
  157. enum GNUNET_CADET_KX_Flags {
  158. /**
  159. * Should the peer reply with its KX details?
  160. */
  161. GNUNET_CADET_KX_FLAG_NONE = 0,
  162. /**
  163. * The peer should reply with its KX details?
  164. */
  165. GNUNET_CADET_KX_FLAG_FORCE_REPLY = 1
  166. };
  167. /**
  168. * Message for a Key eXchange for a tunnel.
  169. */
  170. struct GNUNET_CADET_TunnelKeyExchangeMessage {
  171. /**
  172. * Type: #GNUNET_MESSAGE_TYPE_CADET_TUNNEL_KX or
  173. * #GNUNET_MESSAGE_TYPE_CADET_TUNNEL_KX_AUTH as part
  174. * of `struct GNUNET_CADET_TunnelKeyExchangeAuthMessage`.
  175. */
  176. struct GNUNET_MessageHeader header;
  177. /**
  178. * Flags for the key exchange in NBO, based on
  179. * `enum GNUNET_CADET_KX_Flags`.
  180. */
  181. uint32_t flags GNUNET_PACKED;
  182. /**
  183. * ID of the connection.
  184. */
  185. struct GNUNET_CADET_ConnectionTunnelIdentifier cid;
  186. /**
  187. * Sender's ephemeral public ECC key encoded in a
  188. * format suitable for network transmission, as created
  189. * using 'gcry_sexp_sprint'.
  190. */
  191. struct GNUNET_CRYPTO_EcdhePublicKey ephemeral_key;
  192. #if DEBUG_KX
  193. /**
  194. * Sender's ephemeral public ECC key encoded in a
  195. * format suitable for network transmission, as created
  196. * using 'gcry_sexp_sprint'.
  197. */
  198. struct GNUNET_CRYPTO_EcdhePrivateKey ephemeral_key_XXX; // for debugging KX-crypto!
  199. /**
  200. * Sender's ephemeral public ECC key encoded in a
  201. * format suitable for network transmission, as created
  202. * using 'gcry_sexp_sprint'.
  203. */
  204. struct GNUNET_CRYPTO_EddsaPrivateKey private_key_XXX; // for debugging KX-crypto!
  205. #endif
  206. /**
  207. * Sender's next ephemeral public ECC key encoded in a
  208. * format suitable for network transmission, as created
  209. * using 'gcry_sexp_sprint'.
  210. */
  211. struct GNUNET_CRYPTO_EcdhePublicKey ratchet_key;
  212. };
  213. /**
  214. * Message for a Key eXchange for a tunnel, with authentication.
  215. * Used as a response to the initial KX as well as for rekeying.
  216. */
  217. struct GNUNET_CADET_TunnelKeyExchangeAuthMessage {
  218. /**
  219. * Message header with key material.
  220. */
  221. struct GNUNET_CADET_TunnelKeyExchangeMessage kx;
  222. #if DEBUG_KX
  223. /**
  224. * Received ephemeral public ECC key encoded in a
  225. * format suitable for network transmission, as created
  226. * using 'gcry_sexp_sprint'.
  227. */
  228. struct GNUNET_CRYPTO_EcdhePublicKey r_ephemeral_key_XXX; // for debugging KX-crypto!
  229. #endif
  230. /**
  231. * KDF-proof that sender could compute the 3-DH, used in lieu of a
  232. * signature or payload data.
  233. */
  234. struct GNUNET_HashCode auth;
  235. };
  236. /**
  237. * Encrypted axolotl header with numbers that identify which
  238. * keys in which ratchet are to be used to decrypt the body.
  239. */
  240. struct GNUNET_CADET_AxHeader {
  241. /**
  242. * Number of messages sent with the current ratchet key.
  243. */
  244. uint32_t Ns GNUNET_PACKED;
  245. /**
  246. * Number of messages sent with the previous ratchet key.
  247. */
  248. uint32_t PNs GNUNET_PACKED;
  249. /**
  250. * Current ratchet key.
  251. */
  252. struct GNUNET_CRYPTO_EcdhePublicKey DHRs;
  253. };
  254. /**
  255. * Axolotl-encrypted tunnel message with application payload.
  256. */
  257. struct GNUNET_CADET_TunnelEncryptedMessage {
  258. /**
  259. * Type: #GNUNET_MESSAGE_TYPE_CADET_TUNNEL_ENCRYPTED
  260. */
  261. struct GNUNET_MessageHeader header;
  262. /**
  263. * Reserved, for alignment.
  264. */
  265. uint32_t reserved GNUNET_PACKED;
  266. /**
  267. * ID of the connection.
  268. */
  269. struct GNUNET_CADET_ConnectionTunnelIdentifier cid;
  270. /**
  271. * MAC of the encrypted message, used to verify message integrity.
  272. * Everything after this value will be encrypted with the header key
  273. * and authenticated.
  274. */
  275. struct GNUNET_ShortHashCode hmac;
  276. /**
  277. * Axolotl-header that specifies which keys to use in which ratchet
  278. * to decrypt the body that follows.
  279. */
  280. struct GNUNET_CADET_AxHeader ax_header;
  281. /**
  282. * Encrypted content follows.
  283. */
  284. };
  285. /******************************************************************************/
  286. /******************************* CHANNEL ***********************************/
  287. /******************************************************************************/
  288. /**
  289. * Message to create a Channel.
  290. */
  291. struct GNUNET_CADET_ChannelOpenMessage {
  292. /**
  293. * Type: #GNUNET_MESSAGE_TYPE_CADET_CHANNEL_OPEN
  294. */
  295. struct GNUNET_MessageHeader header;
  296. /**
  297. * Channel options.
  298. */
  299. uint32_t opt GNUNET_PACKED;
  300. /**
  301. * Hash of destination port and listener.
  302. */
  303. struct GNUNET_HashCode h_port;
  304. /**
  305. * ID of the channel within the tunnel.
  306. */
  307. struct GNUNET_CADET_ChannelTunnelNumber ctn;
  308. };
  309. /**
  310. * Message to acknowledge opening a channel of type
  311. * #GNUNET_MESSAGE_TYPE_CADET_CHANNEL_OPEN_ACK.
  312. */
  313. struct GNUNET_CADET_ChannelOpenAckMessage {
  314. /**
  315. * Type: #GNUNET_MESSAGE_TYPE_CADET_CHANNEL_OPEN_ACK
  316. */
  317. struct GNUNET_MessageHeader header;
  318. /**
  319. * For alignment.
  320. */
  321. uint32_t reserved GNUNET_PACKED;
  322. /**
  323. * ID of the channel
  324. */
  325. struct GNUNET_CADET_ChannelTunnelNumber ctn;
  326. /**
  327. * Port number of the channel, used to prove to the
  328. * initiator that the receiver knows the port.
  329. */
  330. struct GNUNET_HashCode port;
  331. };
  332. /**
  333. * Message to destroy a channel of type
  334. * #GNUNET_MESSAGE_TYPE_CADET_CHANNEL_DESTROY.
  335. */
  336. struct GNUNET_CADET_ChannelDestroyMessage {
  337. /**
  338. * Type: #GNUNET_MESSAGE_TYPE_CADET_CHANNEL_DESTROY
  339. */
  340. struct GNUNET_MessageHeader header;
  341. /**
  342. * For alignment.
  343. */
  344. uint32_t reserved GNUNET_PACKED;
  345. /**
  346. * ID of the channel
  347. */
  348. struct GNUNET_CADET_ChannelTunnelNumber ctn;
  349. };
  350. /**
  351. * Number used to uniquely identify messages in a CADET Channel.
  352. */
  353. struct ChannelMessageIdentifier {
  354. /**
  355. * Unique ID of the message, cycles around, in NBO.
  356. */
  357. uint32_t mid GNUNET_PACKED;
  358. };
  359. /**
  360. * Message for cadet data traffic.
  361. */
  362. struct GNUNET_CADET_ChannelAppDataMessage {
  363. /**
  364. * Type: #GNUNET_MESSAGE_TYPE_CADET_CHANNEL_APP_DATA.
  365. */
  366. struct GNUNET_MessageHeader header;
  367. /**
  368. * Unique ID of the payload message.
  369. */
  370. struct ChannelMessageIdentifier mid;
  371. /**
  372. * ID of the channel
  373. */
  374. struct GNUNET_CADET_ChannelTunnelNumber ctn;
  375. /**
  376. * Payload follows
  377. */
  378. };
  379. /**
  380. * Message to acknowledge end-to-end data.
  381. */
  382. struct GNUNET_CADET_ChannelDataAckMessage {
  383. /**
  384. * Type: #GNUNET_MESSAGE_TYPE_CADET_CHANNEL_APP_DATA_ACK
  385. */
  386. struct GNUNET_MessageHeader header;
  387. /**
  388. * ID of the channel
  389. */
  390. struct GNUNET_CADET_ChannelTunnelNumber ctn;
  391. /**
  392. * Bitfield of already-received newer messages. Note that bit 0
  393. * corresponds to @e mid + 1.
  394. *
  395. * pid + 0 @ LSB
  396. * pid + 63 @ MSB
  397. */
  398. uint64_t futures GNUNET_PACKED;
  399. /**
  400. * Next message ID expected.
  401. */
  402. struct ChannelMessageIdentifier mid;
  403. };
  404. GNUNET_NETWORK_STRUCT_END
  405. #if 0 /* keep Emacsens' auto-indent happy */
  406. {
  407. #endif
  408. #ifdef __cplusplus
  409. }
  410. #endif
  411. /* ifndef CADET_PROTOCOL_H */
  412. #endif
  413. /* end of cadet_protocol.h */