netproto_packet.c 8.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339
  1. #include <sys/select.h>
  2. #include <errno.h>
  3. #include <stdint.h>
  4. #include <stdlib.h>
  5. #include "crypto_verify_bytes.h"
  6. #include "sysendian.h"
  7. #include "warnp.h"
  8. #include "crypto.h"
  9. #include "netproto_internal.h"
  10. #include "tsnetwork.h"
  11. #include "netproto.h"
  12. /**
  13. * Packet format:
  14. * position length
  15. * 0 1 packet type (encrypted)
  16. * 1 4 data length, big-endian (encrypted)
  17. * 5 32 SHA256(data) (encrypted)
  18. * 37 32 HMAC(ciphertext bytes 0--36) (not encrypted)
  19. * 69 N packet data (encrypted)
  20. */
  21. struct writepacket_internal {
  22. network_callback * callback;
  23. void * cookie;
  24. NETPROTO_CONNECTION * C;
  25. size_t len;
  26. uint8_t * buf;
  27. };
  28. struct readpacket_internal {
  29. int (* callback_getbuf)(void *, uint8_t, uint8_t **, size_t);
  30. network_callback * callback_done;
  31. void * cookie;
  32. NETPROTO_CONNECTION * C;
  33. uint8_t header[69];
  34. size_t len;
  35. uint8_t * buf;
  36. };
  37. static network_callback packet_sent;
  38. static network_callback header_received;
  39. static network_callback data_received;
  40. static int
  41. packet_sent(void * cookie, int status)
  42. {
  43. struct writepacket_internal * WC = cookie;
  44. int rc;
  45. /* Adjust traffic statistics. */
  46. WC->C->bytesqueued -= WC->len;
  47. if (status == NETWORK_STATUS_OK)
  48. WC->C->bytesout += WC->len;
  49. /* Call upstream callback. */
  50. rc = (WC->callback)(WC->cookie, status);
  51. /* Free buffer and writepacket cookie. */
  52. free(WC->buf);
  53. free(WC);
  54. /* Return value from callback. */
  55. return (rc);
  56. }
  57. static int
  58. header_received(void * cookie, int status)
  59. {
  60. struct readpacket_internal * RC = cookie;
  61. struct timeval to0, to1;
  62. uint32_t len;
  63. int rc = 0; /* No error unless specified otherwise. */
  64. if (status != NETWORK_STATUS_OK)
  65. goto err1;
  66. /* Adjust traffic statistics. */
  67. RC->C->bytesin += 69;
  68. /* Verify that the header is authentic. */
  69. if (crypto_session_verify(RC->C->keys, RC->header, 37,
  70. RC->header + 37)) {
  71. status = NETPROTO_STATUS_PROTERR;
  72. goto err1;
  73. }
  74. /* Decrypt header in-place. */
  75. crypto_session_decrypt(RC->C->keys, RC->header, RC->header, 37);
  76. /* Decode packet length. */
  77. len = be32dec(&RC->header[1]);
  78. #if SIZE_MAX < UINT32_MAX
  79. if (len > SIZE_MAX) {
  80. errno = ENOMEM;
  81. goto err2;
  82. }
  83. #endif
  84. RC->len = len;
  85. /* Ask callback to provide a buffer. */
  86. status = (RC->callback_getbuf)(RC->cookie, RC->header[0], &RC->buf,
  87. RC->len);
  88. if (status != NETWORK_STATUS_OK)
  89. goto err1;
  90. /*
  91. * If the packet data is zero bytes long, we have finished reading
  92. * the packet; invoke the upstream callback. We don't bother to
  93. * verify that the included SHA256 hash is equal to SHA256(nothing),
  94. * since the authenticity of the packet data length is "proved" by
  95. * the HMAC on the header and there is only one possible data block
  96. * of length zero bytes.
  97. */
  98. if (RC->len == 0) {
  99. rc = (RC->callback_done)(RC->cookie, NETWORK_STATUS_OK);
  100. free(RC);
  101. goto done;
  102. }
  103. /*
  104. * Read data into the provided buffer. Allow up to 5 minutes for
  105. * the entire packet to arrive -- this works out to a 256kB packet
  106. * arriving at a rate of 7kbps, which is a reasonable lower bound
  107. * for the bandwidth of systems running tarsnap.
  108. */
  109. to0.tv_sec = 60;
  110. to1.tv_sec = 300;
  111. to0.tv_usec = to1.tv_usec = 0;
  112. if (tsnetwork_read(RC->C->fd, RC->buf, RC->len, &to0, &to1,
  113. data_received, RC))
  114. goto err2;
  115. done:
  116. /* Success! */
  117. return (rc);
  118. err2:
  119. status = NETWORK_STATUS_ERR;
  120. err1:
  121. rc = (RC->callback_done)(RC->cookie, status);
  122. free(RC);
  123. /* Failure! */
  124. return (rc);
  125. }
  126. static int
  127. data_received(void * cookie, int status)
  128. {
  129. struct readpacket_internal * RC = cookie;
  130. uint8_t hash[32];
  131. int rc;
  132. if (status != NETWORK_STATUS_OK)
  133. goto err1;
  134. /* Adjust traffic statistics. */
  135. RC->C->bytesin += RC->len;
  136. /* Decrypt the data in-place. */
  137. crypto_session_decrypt(RC->C->keys, RC->buf, RC->buf, RC->len);
  138. /*
  139. * Verify that the data is not corrupt. Protecting against a timing
  140. * side channel which would allow an attacker to determine in which
  141. * byte of its SHA256 hash a buffer which he mangled had changed is
  142. * probably a little bit of overkill, but you can never have too much
  143. * overkill where security is concerned. :-)
  144. */
  145. if (crypto_hash_data(CRYPTO_KEY_HMAC_SHA256, RC->buf, RC->len,
  146. hash)) {
  147. warn0("Programmer error: "
  148. "SHA256 should never fail");
  149. goto err1;
  150. }
  151. if (crypto_verify_bytes(&RC->header[5], hash, 32)) {
  152. status = NETPROTO_STATUS_PROTERR;
  153. goto err1;
  154. }
  155. err1:
  156. /* We're done. Tell the callback. */
  157. rc = (RC->callback_done)(RC->cookie, status);
  158. /* Free readpacket cookie. */
  159. free(RC);
  160. /* Return the value from the callback. */
  161. return (rc);
  162. }
  163. /**
  164. * netproto_writepacket(C, type, buf, buflen, callback, cookie):
  165. * Write the provided packet to the connection. When complete, call
  166. * callback(cookie, status), where status is a NETPROTO_STATUS_* value.
  167. */
  168. int
  169. netproto_writepacket(NETPROTO_CONNECTION * C, uint8_t type,
  170. const uint8_t * buf, size_t buflen, network_callback * callback,
  171. void * cookie)
  172. {
  173. struct writepacket_internal * WC;
  174. uint8_t header[37];
  175. struct timeval timeout;
  176. /*
  177. * Print a warning if the connection is broken. This should never
  178. * happen, so we could make this a fatal error; but it's not actually
  179. * going to cause any harm (the remote host will detect a protocol
  180. * error), so in the interest of resilience we might as well just
  181. * print the warning and keep on going.
  182. */
  183. if (C->broken) {
  184. warn0("Programmer error: "
  185. "attempt to write to connection marked as broken");
  186. }
  187. /* Sanity check buffer length. */
  188. if (buflen > UINT32_MAX) {
  189. warn0("Programmer error: "
  190. "buffer too large in netproto_writepacket");
  191. goto err0;
  192. }
  193. /* Allocate storage for writepacket cookie. */
  194. if ((WC = malloc(sizeof(struct writepacket_internal))) == NULL)
  195. goto err0;
  196. /* Store callback and cookie. */
  197. WC->callback = callback;
  198. WC->cookie = cookie;
  199. /* Record parameters needed for statistics purposes. */
  200. WC->C = C;
  201. WC->len = buflen + 69;
  202. /* Allocate storage for buffered packet. */
  203. if (buflen > SIZE_MAX - 69) {
  204. errno = ENOMEM;
  205. goto err1;
  206. }
  207. if ((WC->buf = malloc(buflen + 69)) == NULL)
  208. goto err1;
  209. /* Construct header. */
  210. header[0] = type;
  211. be32enc(&header[1], (uint32_t)buflen);
  212. if (crypto_hash_data(CRYPTO_KEY_HMAC_SHA256, buf, buflen,
  213. &header[5])) {
  214. warn0("Programmer error: "
  215. "SHA256 should never fail");
  216. goto err2;
  217. }
  218. /* Encrypt packet header. */
  219. crypto_session_encrypt(C->keys, header, WC->buf, 37);
  220. /* Compute HMAC of packet header. */
  221. crypto_session_sign(C->keys, WC->buf, 37, WC->buf + 37);
  222. /* Encrypt packet data. */
  223. crypto_session_encrypt(C->keys, buf, WC->buf + 69, buflen);
  224. /*
  225. * Add packet to connection write queue. See comments in
  226. * header_received concerning the timeout.
  227. */
  228. timeout.tv_sec = 300;
  229. timeout.tv_usec = 0;
  230. if (network_writeq_add(C->Q, WC->buf, buflen + 69, &timeout,
  231. packet_sent, WC))
  232. goto err2;
  233. C->bytesqueued += WC->len;
  234. /* Success! */
  235. return (0);
  236. err2:
  237. free(WC->buf);
  238. err1:
  239. free(WC);
  240. err0:
  241. /* Failure! */
  242. return (-1);
  243. }
  244. /**
  245. * netproto_readpacket(C, callback_getbuf, callback_done, cookie):
  246. * Read a packet from the connection. Once the type and length of the
  247. * packet is known, call callback_getbuf(cookie, type, buf, buflen); once
  248. * the packet is read or fails, call callback_done(cookie, status), where
  249. * status is a NETPROTO_STATUS_* value.
  250. */
  251. int
  252. netproto_readpacket(NETPROTO_CONNECTION * C,
  253. int (* callback_getbuf)(void *, uint8_t, uint8_t **, size_t),
  254. network_callback * callback_done, void * cookie)
  255. {
  256. struct readpacket_internal * RC;
  257. struct timeval to0, to1;
  258. /* Allocate space for readpacket cookie. */
  259. if ((RC = malloc(sizeof(struct readpacket_internal))) == NULL)
  260. goto err0;
  261. /* Store callbacks, cookie, and connection. */
  262. RC->callback_getbuf = callback_getbuf;
  263. RC->callback_done = callback_done;
  264. RC->cookie = cookie;
  265. RC->C = C;
  266. /*
  267. * Read packet header. Timeout if no data is received for 60s or
  268. * if the complete header is not received for 120s; this allows an
  269. * idle connection to be safely distinguished from a dead connection.
  270. */
  271. to0.tv_sec = 60;
  272. to1.tv_sec = 120;
  273. to0.tv_usec = to1.tv_usec = 0;
  274. if (tsnetwork_read(C->fd, RC->header, 69, &to0, &to1,
  275. header_received, RC))
  276. goto err1;
  277. /* Success! */
  278. return (0);
  279. err1:
  280. free(RC);
  281. err0:
  282. /* Failure! */
  283. return (-1);
  284. }