cadet_protocol.h 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541
  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. /**
  56. * Type: #GNUNET_MESSAGE_TYPE_CADET_CONNECTION_CREATE
  57. *
  58. * Size: sizeof (struct GNUNET_CADET_ConnectionCreateMessage) +
  59. * path_length * sizeof (struct GNUNET_PeerIdentity)
  60. */
  61. struct GNUNET_MessageHeader header;
  62. /**
  63. * Connection options in network byte order.
  64. * #GNUNET_CADET_OPTION_DEFAULT for buffered;
  65. * #GNUNET_CADET_OPTION_NOBUFFER for unbuffered.
  66. * Other flags are ignored and should not be set at this level.
  67. */
  68. uint32_t options GNUNET_PACKED;
  69. /**
  70. * This flag indicates the peer sending the connection create
  71. * meassage likes to trigger a KX handshake.
  72. */
  73. int has_monotime;
  74. /**
  75. * This monotonic time is set, if a peer likes to trigger a KX, but is not
  76. * the peer that should start the KX. (xrs,t3ss)
  77. */
  78. struct GNUNET_TIME_AbsoluteNBO monotime;
  79. /**
  80. * We sign the monotime. The receiving peer can check the signature, to verify
  81. * the sending peer.
  82. */
  83. struct GNUNET_CRYPTO_EddsaSignature monotime_sig;
  84. /**
  85. * ID of the connection
  86. */
  87. struct GNUNET_CADET_ConnectionTunnelIdentifier cid;
  88. /**
  89. * path_length structs defining the *whole* path from the origin [0] to the
  90. * final destination [path_length-1].
  91. */
  92. /* struct GNUNET_PeerIdentity peers[path_length]; */
  93. };
  94. /**
  95. * Message for ack'ing a connection
  96. */
  97. struct GNUNET_CADET_ConnectionCreateAckMessage
  98. {
  99. /**
  100. * Type: #GNUNET_MESSAGE_TYPE_CADET_CONNECTION_CREATE_ACK
  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. /**
  113. * Message for notifying a disconnection in a path
  114. */
  115. struct GNUNET_CADET_ConnectionBrokenMessage
  116. {
  117. /**
  118. * Type: #GNUNET_MESSAGE_TYPE_CADET_CONNECTION_BROKEN.
  119. */
  120. struct GNUNET_MessageHeader header;
  121. /**
  122. * For alignment.
  123. */
  124. uint32_t reserved GNUNET_PACKED;
  125. /**
  126. * ID of the connection.
  127. */
  128. struct GNUNET_CADET_ConnectionTunnelIdentifier cid;
  129. /**
  130. * ID of the endpoint
  131. */
  132. struct GNUNET_PeerIdentity peer1;
  133. /**
  134. * ID of the endpoint
  135. */
  136. struct GNUNET_PeerIdentity peer2;
  137. };
  138. /**
  139. * Message to destroy a connection.
  140. */
  141. struct GNUNET_CADET_ConnectionDestroyMessage
  142. {
  143. /**
  144. * Type: #GNUNET_MESSAGE_TYPE_CADET_CONNECTION_DESTROY
  145. */
  146. struct GNUNET_MessageHeader header;
  147. /**
  148. * For alignment.
  149. */
  150. uint32_t reserved GNUNET_PACKED;
  151. /**
  152. * ID of the connection.
  153. */
  154. struct GNUNET_CADET_ConnectionTunnelIdentifier cid;
  155. };
  156. /******************************************************************************/
  157. /******************************* TUNNEL ***********************************/
  158. /******************************************************************************/
  159. /**
  160. * Unique identifier (counter) for an encrypted message in a channel.
  161. * Used to match #GNUNET_MESSAGE_TYPE_CADET_CONNECTION_HOP_BY_HOP_ENCRYPTED_ACK
  162. * and #GNUNET_MESSAGE_TYPE_CADET_TUNNEL_ENCRYPTED_POLL messages
  163. * against the respective #GNUNET_MESSAGE_TYPE_CADET_TUNNEL_ENCRYPTED
  164. * messages.
  165. */
  166. struct CadetEncryptedMessageIdentifier
  167. {
  168. /**
  169. * This number is incremented by one per message. It may wrap around.
  170. * In network byte order.
  171. */
  172. uint32_t pid GNUNET_PACKED;
  173. };
  174. /**
  175. * Flags to be used in GNUNET_CADET_KX.
  176. */
  177. enum GNUNET_CADET_KX_Flags
  178. {
  179. /**
  180. * Should the peer reply with its KX details?
  181. */
  182. GNUNET_CADET_KX_FLAG_NONE = 0,
  183. /**
  184. * The peer should reply with its KX details?
  185. */
  186. GNUNET_CADET_KX_FLAG_FORCE_REPLY = 1
  187. };
  188. /**
  189. * Message for a Key eXchange for a tunnel.
  190. */
  191. struct GNUNET_CADET_TunnelKeyExchangeMessage
  192. {
  193. /**
  194. * Type: #GNUNET_MESSAGE_TYPE_CADET_TUNNEL_KX or
  195. * #GNUNET_MESSAGE_TYPE_CADET_TUNNEL_KX_AUTH as part
  196. * of `struct GNUNET_CADET_TunnelKeyExchangeAuthMessage`.
  197. */
  198. struct GNUNET_MessageHeader header;
  199. /**
  200. * Flags for the key exchange in NBO, based on
  201. * `enum GNUNET_CADET_KX_Flags`.
  202. */
  203. uint32_t flags GNUNET_PACKED;
  204. /**
  205. * ID of the connection.
  206. */
  207. struct GNUNET_CADET_ConnectionTunnelIdentifier cid;
  208. /**
  209. * Sender's ephemeral public ECC key encoded in a
  210. * format suitable for network transmission, as created
  211. * using 'gcry_sexp_sprint'.
  212. */
  213. struct GNUNET_CRYPTO_EcdhePublicKey ephemeral_key;
  214. #if DEBUG_KX
  215. /**
  216. * Sender's ephemeral public ECC key encoded in a
  217. * format suitable for network transmission, as created
  218. * using 'gcry_sexp_sprint'.
  219. */
  220. struct GNUNET_CRYPTO_EcdhePrivateKey ephemeral_key_XXX; // for debugging KX-crypto!
  221. /**
  222. * Sender's ephemeral public ECC key encoded in a
  223. * format suitable for network transmission, as created
  224. * using 'gcry_sexp_sprint'.
  225. */
  226. struct GNUNET_CRYPTO_EddsaPrivateKey private_key_XXX; // for debugging KX-crypto!
  227. #endif
  228. /**
  229. * Sender's next ephemeral public ECC key encoded in a
  230. * format suitable for network transmission, as created
  231. * using 'gcry_sexp_sprint'.
  232. */
  233. struct GNUNET_CRYPTO_EcdhePublicKey ratchet_key;
  234. };
  235. /**
  236. * Message for a Key eXchange for a tunnel, with authentication.
  237. * Used as a response to the initial KX as well as for rekeying.
  238. */
  239. struct GNUNET_CADET_TunnelKeyExchangeAuthMessage
  240. {
  241. /**
  242. * Message header with key material.
  243. */
  244. struct GNUNET_CADET_TunnelKeyExchangeMessage kx;
  245. #if DEBUG_KX
  246. /**
  247. * Received ephemeral public ECC key encoded in a
  248. * format suitable for network transmission, as created
  249. * using 'gcry_sexp_sprint'.
  250. */
  251. struct GNUNET_CRYPTO_EcdhePublicKey r_ephemeral_key_XXX; // for debugging KX-crypto!
  252. #endif
  253. /**
  254. * KDF-proof that sender could compute the 3-DH, used in lieu of a
  255. * signature or payload data.
  256. */
  257. struct GNUNET_HashCode auth;
  258. };
  259. /**
  260. * Encrypted axolotl header with numbers that identify which
  261. * keys in which ratchet are to be used to decrypt the body.
  262. */
  263. struct GNUNET_CADET_AxHeader
  264. {
  265. /**
  266. * Number of messages sent with the current ratchet key.
  267. */
  268. uint32_t Ns GNUNET_PACKED;
  269. /**
  270. * Number of messages sent with the previous ratchet key.
  271. */
  272. uint32_t PNs GNUNET_PACKED;
  273. /**
  274. * Current ratchet key.
  275. */
  276. struct GNUNET_CRYPTO_EcdhePublicKey DHRs;
  277. };
  278. /**
  279. * Axolotl-encrypted tunnel message with application payload.
  280. */
  281. struct GNUNET_CADET_TunnelEncryptedMessage
  282. {
  283. /**
  284. * Type: #GNUNET_MESSAGE_TYPE_CADET_TUNNEL_ENCRYPTED
  285. */
  286. struct GNUNET_MessageHeader header;
  287. /**
  288. * Reserved, for alignment.
  289. */
  290. uint32_t reserved GNUNET_PACKED;
  291. /**
  292. * ID of the connection.
  293. */
  294. struct GNUNET_CADET_ConnectionTunnelIdentifier cid;
  295. /**
  296. * MAC of the encrypted message, used to verify message integrity.
  297. * Everything after this value will be encrypted with the header key
  298. * and authenticated.
  299. */
  300. struct GNUNET_ShortHashCode hmac;
  301. /**
  302. * Axolotl-header that specifies which keys to use in which ratchet
  303. * to decrypt the body that follows.
  304. */
  305. struct GNUNET_CADET_AxHeader ax_header;
  306. /**
  307. * Encrypted content follows.
  308. */
  309. };
  310. /******************************************************************************/
  311. /******************************* CHANNEL ***********************************/
  312. /******************************************************************************/
  313. /**
  314. * Message to create a Channel.
  315. */
  316. struct GNUNET_CADET_ChannelOpenMessage
  317. {
  318. /**
  319. * Type: #GNUNET_MESSAGE_TYPE_CADET_CHANNEL_OPEN
  320. */
  321. struct GNUNET_MessageHeader header;
  322. /**
  323. * Channel options.
  324. */
  325. uint32_t opt GNUNET_PACKED;
  326. /**
  327. * Hash of destination port and listener.
  328. */
  329. struct GNUNET_HashCode h_port;
  330. /**
  331. * ID of the channel within the tunnel.
  332. */
  333. struct GNUNET_CADET_ChannelTunnelNumber ctn;
  334. };
  335. /**
  336. * Message to acknowledge opening a channel of type
  337. * #GNUNET_MESSAGE_TYPE_CADET_CHANNEL_OPEN_ACK.
  338. */
  339. struct GNUNET_CADET_ChannelOpenAckMessage
  340. {
  341. /**
  342. * Type: #GNUNET_MESSAGE_TYPE_CADET_CHANNEL_OPEN_ACK
  343. */
  344. struct GNUNET_MessageHeader header;
  345. /**
  346. * For alignment.
  347. */
  348. uint32_t reserved GNUNET_PACKED;
  349. /**
  350. * ID of the channel
  351. */
  352. struct GNUNET_CADET_ChannelTunnelNumber ctn;
  353. /**
  354. * Port number of the channel, used to prove to the
  355. * initiator that the receiver knows the port.
  356. */
  357. struct GNUNET_HashCode port;
  358. };
  359. /**
  360. * Message to destroy a channel of type
  361. * #GNUNET_MESSAGE_TYPE_CADET_CHANNEL_DESTROY.
  362. */
  363. struct GNUNET_CADET_ChannelDestroyMessage
  364. {
  365. /**
  366. * Type: #GNUNET_MESSAGE_TYPE_CADET_CHANNEL_DESTROY
  367. */
  368. struct GNUNET_MessageHeader header;
  369. /**
  370. * For alignment.
  371. */
  372. uint32_t reserved GNUNET_PACKED;
  373. /**
  374. * ID of the channel
  375. */
  376. struct GNUNET_CADET_ChannelTunnelNumber ctn;
  377. };
  378. /**
  379. * Number used to uniquely identify messages in a CADET Channel.
  380. */
  381. struct ChannelMessageIdentifier
  382. {
  383. /**
  384. * Unique ID of the message, cycles around, in NBO.
  385. */
  386. uint32_t mid GNUNET_PACKED;
  387. };
  388. /**
  389. * Message for cadet data traffic.
  390. */
  391. struct GNUNET_CADET_ChannelAppDataMessage
  392. {
  393. /**
  394. * Type: #GNUNET_MESSAGE_TYPE_CADET_CHANNEL_APP_DATA.
  395. */
  396. struct GNUNET_MessageHeader header;
  397. /**
  398. * Unique ID of the payload message.
  399. */
  400. struct ChannelMessageIdentifier mid;
  401. /**
  402. * ID of the channel
  403. */
  404. struct GNUNET_CADET_ChannelTunnelNumber ctn;
  405. /**
  406. * Payload follows
  407. */
  408. };
  409. /**
  410. * Message to acknowledge end-to-end data.
  411. */
  412. struct GNUNET_CADET_ChannelDataAckMessage
  413. {
  414. /**
  415. * Type: #GNUNET_MESSAGE_TYPE_CADET_CHANNEL_APP_DATA_ACK
  416. */
  417. struct GNUNET_MessageHeader header;
  418. /**
  419. * ID of the channel
  420. */
  421. struct GNUNET_CADET_ChannelTunnelNumber ctn;
  422. /**
  423. * Bitfield of already-received newer messages. Note that bit 0
  424. * corresponds to @e mid + 1.
  425. *
  426. * pid + 0 @ LSB
  427. * pid + 63 @ MSB
  428. */
  429. uint64_t futures GNUNET_PACKED;
  430. /**
  431. * Next message ID expected.
  432. */
  433. struct ChannelMessageIdentifier mid;
  434. };
  435. GNUNET_NETWORK_STRUCT_END
  436. #if 0 /* keep Emacsens' auto-indent happy */
  437. {
  438. #endif
  439. #ifdef __cplusplus
  440. }
  441. #endif
  442. /* ifndef CADET_PROTOCOL_H */
  443. #endif
  444. /* end of cadet_protocol.h */