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