buffers_tls.c 5.1 KB

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