You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
 
 
 
 
 
 

287 lines
7.4 KiB

  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. namespace bssl {
  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 = BIO_read(ssl->rbio, buf->data(), static_cast<int>(buf->cap()));
  96. if (ret <= 0) {
  97. ssl->s3->rwstate = SSL_READING;
  98. return ret;
  99. }
  100. buf->DidWrite(static_cast<size_t>(ret));
  101. return 1;
  102. }
  103. static int tls_read_buffer_extend_to(SSL *ssl, size_t len) {
  104. SSLBuffer *buf = &ssl->s3->read_buffer;
  105. if (len > buf->cap()) {
  106. OPENSSL_PUT_ERROR(SSL, SSL_R_BUFFER_TOO_SMALL);
  107. return -1;
  108. }
  109. // Read until the target length is reached.
  110. while (buf->size() < len) {
  111. // The amount of data to read is bounded by |buf->cap|, which must fit in an
  112. // int.
  113. int ret = BIO_read(ssl->rbio, buf->data() + buf->size(),
  114. static_cast<int>(len - buf->size()));
  115. if (ret <= 0) {
  116. ssl->s3->rwstate = SSL_READING;
  117. return ret;
  118. }
  119. buf->DidWrite(static_cast<size_t>(ret));
  120. }
  121. return 1;
  122. }
  123. int ssl_read_buffer_extend_to(SSL *ssl, size_t len) {
  124. // |ssl_read_buffer_extend_to| implicitly discards any consumed data.
  125. ssl->s3->read_buffer.DiscardConsumed();
  126. if (SSL_is_dtls(ssl)) {
  127. static_assert(
  128. DTLS1_RT_HEADER_LENGTH + SSL3_RT_MAX_ENCRYPTED_LENGTH <= 0xffff,
  129. "DTLS read buffer is too large");
  130. // The |len| parameter is ignored in DTLS.
  131. len = DTLS1_RT_HEADER_LENGTH + SSL3_RT_MAX_ENCRYPTED_LENGTH;
  132. }
  133. if (!ssl->s3->read_buffer.EnsureCap(ssl_record_prefix_len(ssl), len)) {
  134. return -1;
  135. }
  136. if (ssl->rbio == NULL) {
  137. OPENSSL_PUT_ERROR(SSL, SSL_R_BIO_NOT_SET);
  138. return -1;
  139. }
  140. int ret;
  141. if (SSL_is_dtls(ssl)) {
  142. // |len| is ignored for a datagram transport.
  143. ret = dtls_read_buffer_next_packet(ssl);
  144. } else {
  145. ret = tls_read_buffer_extend_to(ssl, len);
  146. }
  147. if (ret <= 0) {
  148. // If the buffer was empty originally and remained empty after attempting to
  149. // extend it, release the buffer until the next attempt.
  150. ssl->s3->read_buffer.DiscardConsumed();
  151. }
  152. return ret;
  153. }
  154. int ssl_handle_open_record(SSL *ssl, bool *out_retry, ssl_open_record_t ret,
  155. size_t consumed, uint8_t alert) {
  156. *out_retry = false;
  157. if (ret != ssl_open_record_partial) {
  158. ssl->s3->read_buffer.Consume(consumed);
  159. }
  160. if (ret != ssl_open_record_success) {
  161. // Nothing was returned to the caller, so discard anything marked consumed.
  162. ssl->s3->read_buffer.DiscardConsumed();
  163. }
  164. switch (ret) {
  165. case ssl_open_record_success:
  166. return 1;
  167. case ssl_open_record_partial: {
  168. int read_ret = ssl_read_buffer_extend_to(ssl, consumed);
  169. if (read_ret <= 0) {
  170. return read_ret;
  171. }
  172. *out_retry = true;
  173. return 1;
  174. }
  175. case ssl_open_record_discard:
  176. *out_retry = true;
  177. return 1;
  178. case ssl_open_record_close_notify:
  179. return 0;
  180. case ssl_open_record_error:
  181. if (alert != 0) {
  182. ssl_send_alert(ssl, SSL3_AL_FATAL, alert);
  183. }
  184. return -1;
  185. }
  186. assert(0);
  187. return -1;
  188. }
  189. static_assert(SSL3_RT_HEADER_LENGTH * 2 +
  190. SSL3_RT_SEND_MAX_ENCRYPTED_OVERHEAD * 2 +
  191. SSL3_RT_MAX_PLAIN_LENGTH <=
  192. 0xffff,
  193. "maximum TLS write buffer is too large");
  194. static_assert(DTLS1_RT_HEADER_LENGTH + SSL3_RT_SEND_MAX_ENCRYPTED_OVERHEAD +
  195. SSL3_RT_MAX_PLAIN_LENGTH <=
  196. 0xffff,
  197. "maximum DTLS write buffer is too large");
  198. static int tls_write_buffer_flush(SSL *ssl) {
  199. SSLBuffer *buf = &ssl->s3->write_buffer;
  200. while (!buf->empty()) {
  201. int ret = BIO_write(ssl->wbio, buf->data(), buf->size());
  202. if (ret <= 0) {
  203. ssl->s3->rwstate = SSL_WRITING;
  204. return ret;
  205. }
  206. buf->Consume(static_cast<size_t>(ret));
  207. }
  208. buf->Clear();
  209. return 1;
  210. }
  211. static int dtls_write_buffer_flush(SSL *ssl) {
  212. SSLBuffer *buf = &ssl->s3->write_buffer;
  213. if (buf->empty()) {
  214. return 1;
  215. }
  216. int ret = BIO_write(ssl->wbio, buf->data(), buf->size());
  217. if (ret <= 0) {
  218. ssl->s3->rwstate = SSL_WRITING;
  219. // If the write failed, drop the write buffer anyway. Datagram transports
  220. // can't write half a packet, so the caller is expected to retry from the
  221. // top.
  222. buf->Clear();
  223. return ret;
  224. }
  225. buf->Clear();
  226. return 1;
  227. }
  228. int ssl_write_buffer_flush(SSL *ssl) {
  229. if (ssl->wbio == NULL) {
  230. OPENSSL_PUT_ERROR(SSL, SSL_R_BIO_NOT_SET);
  231. return -1;
  232. }
  233. if (SSL_is_dtls(ssl)) {
  234. return dtls_write_buffer_flush(ssl);
  235. } else {
  236. return tls_write_buffer_flush(ssl);
  237. }
  238. }
  239. } // namespace bssl