buffers_tls.c 5.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175
  1. /* Copyright (c) 2001 Matej Pfajfar.
  2. * Copyright (c) 2001-2004, Roger Dingledine.
  3. * Copyright (c) 2004-2006, Roger Dingledine, Nick Mathewson.
  4. * Copyright (c) 2007-2017, The Tor Project, Inc. */
  5. /* See LICENSE for licensing information */
  6. #define BUFFERS_PRIVATE
  7. #include "orconfig.h"
  8. #include <stddef.h>
  9. #include "buffers.h"
  10. #include "buffers_tls.h"
  11. #include "compat.h"
  12. #include "compress.h"
  13. #include "util.h"
  14. #include "torint.h"
  15. #include "torlog.h"
  16. #include "tortls.h"
  17. #ifdef HAVE_UNISTD_H
  18. #include <unistd.h>
  19. #endif
  20. /** As read_to_chunk(), but return (negative) error code on error, blocking,
  21. * or TLS, and the number of bytes read otherwise. */
  22. static inline int
  23. read_to_chunk_tls(buf_t *buf, chunk_t *chunk, tor_tls_t *tls,
  24. size_t at_most)
  25. {
  26. int read_result;
  27. tor_assert(CHUNK_REMAINING_CAPACITY(chunk) >= at_most);
  28. read_result = tor_tls_read(tls, CHUNK_WRITE_PTR(chunk), at_most);
  29. if (read_result < 0)
  30. return read_result;
  31. buf->datalen += read_result;
  32. chunk->datalen += read_result;
  33. return read_result;
  34. }
  35. /** As read_to_buf, but reads from a TLS connection, and returns a TLS
  36. * status value rather than the number of bytes read.
  37. *
  38. * Using TLS on OR connections complicates matters in two ways.
  39. *
  40. * First, a TLS stream has its own read buffer independent of the
  41. * connection's read buffer. (TLS needs to read an entire frame from
  42. * the network before it can decrypt any data. Thus, trying to read 1
  43. * byte from TLS can require that several KB be read from the network
  44. * and decrypted. The extra data is stored in TLS's decrypt buffer.)
  45. * Because the data hasn't been read by Tor (it's still inside the TLS),
  46. * this means that sometimes a connection "has stuff to read" even when
  47. * poll() didn't return POLLIN. The tor_tls_get_pending_bytes function is
  48. * used in connection.c to detect TLS objects with non-empty internal
  49. * buffers and read from them again.
  50. *
  51. * Second, the TLS stream's events do not correspond directly to network
  52. * events: sometimes, before a TLS stream can read, the network must be
  53. * ready to write -- or vice versa.
  54. */
  55. int
  56. buf_read_from_tls(buf_t *buf, tor_tls_t *tls, size_t at_most)
  57. {
  58. int r = 0;
  59. size_t total_read = 0;
  60. check_no_tls_errors();
  61. if (BUG(buf->datalen >= INT_MAX))
  62. return -1;
  63. if (BUG(buf->datalen >= INT_MAX - at_most))
  64. return -1;
  65. while (at_most > total_read) {
  66. size_t readlen = at_most - total_read;
  67. chunk_t *chunk;
  68. if (!buf->tail || CHUNK_REMAINING_CAPACITY(buf->tail) < MIN_READ_LEN) {
  69. chunk = buf_add_chunk_with_capacity(buf, at_most, 1);
  70. if (readlen > chunk->memlen)
  71. readlen = chunk->memlen;
  72. } else {
  73. size_t cap = CHUNK_REMAINING_CAPACITY(buf->tail);
  74. chunk = buf->tail;
  75. if (cap < readlen)
  76. readlen = cap;
  77. }
  78. r = read_to_chunk_tls(buf, chunk, tls, readlen);
  79. if (r < 0)
  80. return r; /* Error */
  81. tor_assert(total_read+r < INT_MAX);
  82. total_read += r;
  83. if ((size_t)r < readlen) /* eof, block, or no more to read. */
  84. break;
  85. }
  86. return (int)total_read;
  87. }
  88. /** Helper for buf_flush_to_tls(): try to write <b>sz</b> bytes from chunk
  89. * <b>chunk</b> of buffer <b>buf</b> onto socket <b>s</b>. (Tries to write
  90. * more if there is a forced pending write size.) On success, deduct the
  91. * bytes written from *<b>buf_flushlen</b>. Return the number of bytes
  92. * written on success, and a TOR_TLS error code on failure or blocking.
  93. */
  94. static inline int
  95. flush_chunk_tls(tor_tls_t *tls, buf_t *buf, chunk_t *chunk,
  96. size_t sz, size_t *buf_flushlen)
  97. {
  98. int r;
  99. size_t forced;
  100. char *data;
  101. forced = tor_tls_get_forced_write_size(tls);
  102. if (forced > sz)
  103. sz = forced;
  104. if (chunk) {
  105. data = chunk->data;
  106. tor_assert(sz <= chunk->datalen);
  107. } else {
  108. data = NULL;
  109. tor_assert(sz == 0);
  110. }
  111. r = tor_tls_write(tls, data, sz);
  112. if (r < 0)
  113. return r;
  114. if (*buf_flushlen > (size_t)r)
  115. *buf_flushlen -= r;
  116. else
  117. *buf_flushlen = 0;
  118. buf_drain(buf, r);
  119. log_debug(LD_NET,"flushed %d bytes, %d ready to flush, %d remain.",
  120. r,(int)*buf_flushlen,(int)buf->datalen);
  121. return r;
  122. }
  123. /** As buf_flush_to_socket(), but writes data to a TLS connection. Can write
  124. * more than <b>flushlen</b> bytes.
  125. */
  126. int
  127. buf_flush_to_tls(buf_t *buf, tor_tls_t *tls, size_t flushlen,
  128. size_t *buf_flushlen)
  129. {
  130. int r;
  131. size_t flushed = 0;
  132. ssize_t sz;
  133. tor_assert(buf_flushlen);
  134. tor_assert(*buf_flushlen <= buf->datalen);
  135. tor_assert(flushlen <= *buf_flushlen);
  136. sz = (ssize_t) flushlen;
  137. /* we want to let tls write even if flushlen is zero, because it might
  138. * have a partial record pending */
  139. check_no_tls_errors();
  140. do {
  141. size_t flushlen0;
  142. if (buf->head) {
  143. if ((ssize_t)buf->head->datalen >= sz)
  144. flushlen0 = sz;
  145. else
  146. flushlen0 = buf->head->datalen;
  147. } else {
  148. flushlen0 = 0;
  149. }
  150. r = flush_chunk_tls(tls, buf, buf->head, flushlen0, buf_flushlen);
  151. if (r < 0)
  152. return r;
  153. flushed += r;
  154. sz -= r;
  155. if (r == 0) /* Can't flush any more now. */
  156. break;
  157. } while (sz > 0);
  158. tor_assert(flushed < INT_MAX);
  159. return (int)flushed;
  160. }