ssl_buffer.cc 7.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287
  1. /* Copyright (c) 2015, Google Inc.
  2. *
  3. * Permission to use, copy, modify, and/or distribute this software for any
  4. * purpose with or without fee is hereby granted, provided that the above
  5. * copyright notice and this permission notice appear in all copies.
  6. *
  7. * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
  8. * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
  9. * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY
  10. * SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
  11. * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN ACTION
  12. * OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF OR IN
  13. * CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE. */
  14. #include <openssl/ssl.h>
  15. #include <assert.h>
  16. #include <limits.h>
  17. #include <stdlib.h>
  18. #include <string.h>
  19. #include <openssl/bio.h>
  20. #include <openssl/err.h>
  21. #include <openssl/mem.h>
  22. #include "../crypto/internal.h"
  23. #include "internal.h"
  24. BSSL_NAMESPACE_BEGIN
  25. // BIO uses int instead of size_t. No lengths will exceed uint16_t, so this will
  26. // not overflow.
  27. static_assert(0xffff <= INT_MAX, "uint16_t does not fit in int");
  28. static_assert((SSL3_ALIGN_PAYLOAD & (SSL3_ALIGN_PAYLOAD - 1)) == 0,
  29. "SSL3_ALIGN_PAYLOAD must be a power of 2");
  30. void SSLBuffer::Clear() {
  31. free(buf_); // Allocated with malloc().
  32. buf_ = nullptr;
  33. offset_ = 0;
  34. size_ = 0;
  35. cap_ = 0;
  36. }
  37. bool SSLBuffer::EnsureCap(size_t header_len, size_t new_cap) {
  38. if (new_cap > 0xffff) {
  39. OPENSSL_PUT_ERROR(SSL, ERR_R_INTERNAL_ERROR);
  40. return false;
  41. }
  42. if (cap_ >= new_cap) {
  43. return true;
  44. }
  45. // Add up to |SSL3_ALIGN_PAYLOAD| - 1 bytes of slack for alignment.
  46. //
  47. // Since this buffer gets allocated quite frequently and doesn't contain any
  48. // sensitive data, we allocate with malloc rather than |OPENSSL_malloc| and
  49. // avoid zeroing on free.
  50. uint8_t *new_buf = (uint8_t *)malloc(new_cap + SSL3_ALIGN_PAYLOAD - 1);
  51. if (new_buf == NULL) {
  52. OPENSSL_PUT_ERROR(SSL, ERR_R_MALLOC_FAILURE);
  53. return false;
  54. }
  55. // Offset the buffer such that the record body is aligned.
  56. size_t new_offset =
  57. (0 - header_len - (uintptr_t)new_buf) & (SSL3_ALIGN_PAYLOAD - 1);
  58. if (buf_ != NULL) {
  59. OPENSSL_memcpy(new_buf + new_offset, buf_ + offset_, size_);
  60. free(buf_); // Allocated with malloc().
  61. }
  62. buf_ = new_buf;
  63. offset_ = new_offset;
  64. cap_ = new_cap;
  65. return true;
  66. }
  67. void SSLBuffer::DidWrite(size_t new_size) {
  68. if (new_size > cap() - size()) {
  69. abort();
  70. }
  71. size_ += new_size;
  72. }
  73. void SSLBuffer::Consume(size_t len) {
  74. if (len > size_) {
  75. abort();
  76. }
  77. offset_ += (uint16_t)len;
  78. size_ -= (uint16_t)len;
  79. cap_ -= (uint16_t)len;
  80. }
  81. void SSLBuffer::DiscardConsumed() {
  82. if (size_ == 0) {
  83. Clear();
  84. }
  85. }
  86. static int dtls_read_buffer_next_packet(SSL *ssl) {
  87. SSLBuffer *buf = &ssl->s3->read_buffer;
  88. if (!buf->empty()) {
  89. // It is an error to call |dtls_read_buffer_extend| when the read buffer is
  90. // not empty.
  91. OPENSSL_PUT_ERROR(SSL, ERR_R_INTERNAL_ERROR);
  92. return -1;
  93. }
  94. // Read a single packet from |ssl->rbio|. |buf->cap()| must fit in an int.
  95. int ret =
  96. BIO_read(ssl->rbio.get(), buf->data(), static_cast<int>(buf->cap()));
  97. if (ret <= 0) {
  98. ssl->s3->rwstate = SSL_ERROR_WANT_READ;
  99. return ret;
  100. }
  101. buf->DidWrite(static_cast<size_t>(ret));
  102. return 1;
  103. }
  104. static int tls_read_buffer_extend_to(SSL *ssl, size_t len) {
  105. SSLBuffer *buf = &ssl->s3->read_buffer;
  106. if (len > buf->cap()) {
  107. OPENSSL_PUT_ERROR(SSL, SSL_R_BUFFER_TOO_SMALL);
  108. return -1;
  109. }
  110. // Read until the target length is reached.
  111. while (buf->size() < len) {
  112. // The amount of data to read is bounded by |buf->cap|, which must fit in an
  113. // int.
  114. int ret = BIO_read(ssl->rbio.get(), buf->data() + buf->size(),
  115. static_cast<int>(len - buf->size()));
  116. if (ret <= 0) {
  117. ssl->s3->rwstate = SSL_ERROR_WANT_READ;
  118. return ret;
  119. }
  120. buf->DidWrite(static_cast<size_t>(ret));
  121. }
  122. return 1;
  123. }
  124. int ssl_read_buffer_extend_to(SSL *ssl, size_t len) {
  125. // |ssl_read_buffer_extend_to| implicitly discards any consumed data.
  126. ssl->s3->read_buffer.DiscardConsumed();
  127. if (SSL_is_dtls(ssl)) {
  128. static_assert(
  129. DTLS1_RT_HEADER_LENGTH + SSL3_RT_MAX_ENCRYPTED_LENGTH <= 0xffff,
  130. "DTLS read buffer is too large");
  131. // The |len| parameter is ignored in DTLS.
  132. len = DTLS1_RT_HEADER_LENGTH + SSL3_RT_MAX_ENCRYPTED_LENGTH;
  133. }
  134. if (!ssl->s3->read_buffer.EnsureCap(ssl_record_prefix_len(ssl), len)) {
  135. return -1;
  136. }
  137. if (ssl->rbio == nullptr) {
  138. OPENSSL_PUT_ERROR(SSL, SSL_R_BIO_NOT_SET);
  139. return -1;
  140. }
  141. int ret;
  142. if (SSL_is_dtls(ssl)) {
  143. // |len| is ignored for a datagram transport.
  144. ret = dtls_read_buffer_next_packet(ssl);
  145. } else {
  146. ret = tls_read_buffer_extend_to(ssl, len);
  147. }
  148. if (ret <= 0) {
  149. // If the buffer was empty originally and remained empty after attempting to
  150. // extend it, release the buffer until the next attempt.
  151. ssl->s3->read_buffer.DiscardConsumed();
  152. }
  153. return ret;
  154. }
  155. int ssl_handle_open_record(SSL *ssl, bool *out_retry, ssl_open_record_t ret,
  156. size_t consumed, uint8_t alert) {
  157. *out_retry = false;
  158. if (ret != ssl_open_record_partial) {
  159. ssl->s3->read_buffer.Consume(consumed);
  160. }
  161. if (ret != ssl_open_record_success) {
  162. // Nothing was returned to the caller, so discard anything marked consumed.
  163. ssl->s3->read_buffer.DiscardConsumed();
  164. }
  165. switch (ret) {
  166. case ssl_open_record_success:
  167. return 1;
  168. case ssl_open_record_partial: {
  169. int read_ret = ssl_read_buffer_extend_to(ssl, consumed);
  170. if (read_ret <= 0) {
  171. return read_ret;
  172. }
  173. *out_retry = true;
  174. return 1;
  175. }
  176. case ssl_open_record_discard:
  177. *out_retry = true;
  178. return 1;
  179. case ssl_open_record_close_notify:
  180. return 0;
  181. case ssl_open_record_error:
  182. if (alert != 0) {
  183. ssl_send_alert(ssl, SSL3_AL_FATAL, alert);
  184. }
  185. return -1;
  186. }
  187. assert(0);
  188. return -1;
  189. }
  190. static_assert(SSL3_RT_HEADER_LENGTH * 2 +
  191. SSL3_RT_SEND_MAX_ENCRYPTED_OVERHEAD * 2 +
  192. SSL3_RT_MAX_PLAIN_LENGTH <=
  193. 0xffff,
  194. "maximum TLS write buffer is too large");
  195. static_assert(DTLS1_RT_HEADER_LENGTH + SSL3_RT_SEND_MAX_ENCRYPTED_OVERHEAD +
  196. SSL3_RT_MAX_PLAIN_LENGTH <=
  197. 0xffff,
  198. "maximum DTLS write buffer is too large");
  199. static int tls_write_buffer_flush(SSL *ssl) {
  200. SSLBuffer *buf = &ssl->s3->write_buffer;
  201. while (!buf->empty()) {
  202. int ret = BIO_write(ssl->wbio.get(), buf->data(), buf->size());
  203. if (ret <= 0) {
  204. ssl->s3->rwstate = SSL_ERROR_WANT_WRITE;
  205. return ret;
  206. }
  207. buf->Consume(static_cast<size_t>(ret));
  208. }
  209. buf->Clear();
  210. return 1;
  211. }
  212. static int dtls_write_buffer_flush(SSL *ssl) {
  213. SSLBuffer *buf = &ssl->s3->write_buffer;
  214. if (buf->empty()) {
  215. return 1;
  216. }
  217. int ret = BIO_write(ssl->wbio.get(), buf->data(), buf->size());
  218. if (ret <= 0) {
  219. ssl->s3->rwstate = SSL_ERROR_WANT_WRITE;
  220. // If the write failed, drop the write buffer anyway. Datagram transports
  221. // can't write half a packet, so the caller is expected to retry from the
  222. // top.
  223. buf->Clear();
  224. return ret;
  225. }
  226. buf->Clear();
  227. return 1;
  228. }
  229. int ssl_write_buffer_flush(SSL *ssl) {
  230. if (ssl->wbio == nullptr) {
  231. OPENSSL_PUT_ERROR(SSL, SSL_R_BIO_NOT_SET);
  232. return -1;
  233. }
  234. if (SSL_is_dtls(ssl)) {
  235. return dtls_write_buffer_flush(ssl);
  236. } else {
  237. return tls_write_buffer_flush(ssl);
  238. }
  239. }
  240. BSSL_NAMESPACE_END