ssl_buffer.c 8.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312
  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 <openssl/type_check.h>
  23. #include "../crypto/internal.h"
  24. #include "internal.h"
  25. OPENSSL_COMPILE_ASSERT(0xffff <= INT_MAX, uint16_fits_in_int);
  26. OPENSSL_COMPILE_ASSERT((SSL3_ALIGN_PAYLOAD & (SSL3_ALIGN_PAYLOAD - 1)) == 0,
  27. align_to_a_power_of_two);
  28. /* setup_buffer initializes |buf| with capacity |cap|, aligned such that data
  29. * written after |header_len| is aligned to a |SSL3_ALIGN_PAYLOAD|-byte
  30. * boundary. It returns one on success and zero on error. */
  31. static int setup_buffer(SSL3_BUFFER *buf, size_t header_len, size_t cap) {
  32. if (buf->buf != NULL || cap > 0xffff) {
  33. OPENSSL_PUT_ERROR(SSL, ERR_R_INTERNAL_ERROR);
  34. return 0;
  35. }
  36. /* Add up to |SSL3_ALIGN_PAYLOAD| - 1 bytes of slack for alignment. */
  37. buf->buf = OPENSSL_malloc(cap + SSL3_ALIGN_PAYLOAD - 1);
  38. if (buf->buf == NULL) {
  39. OPENSSL_PUT_ERROR(SSL, ERR_R_MALLOC_FAILURE);
  40. return 0;
  41. }
  42. /* Arrange the buffer such that the record body is aligned. */
  43. buf->offset = (0 - header_len - (uintptr_t)buf->buf) &
  44. (SSL3_ALIGN_PAYLOAD - 1);
  45. buf->len = 0;
  46. buf->cap = cap;
  47. return 1;
  48. }
  49. static void consume_buffer(SSL3_BUFFER *buf, size_t len) {
  50. if (len > buf->len) {
  51. abort();
  52. }
  53. buf->offset += (uint16_t)len;
  54. buf->len -= (uint16_t)len;
  55. buf->cap -= (uint16_t)len;
  56. }
  57. static void clear_buffer(SSL3_BUFFER *buf) {
  58. OPENSSL_free(buf->buf);
  59. OPENSSL_memset(buf, 0, sizeof(SSL3_BUFFER));
  60. }
  61. OPENSSL_COMPILE_ASSERT(DTLS1_RT_HEADER_LENGTH + SSL3_RT_MAX_ENCRYPTED_LENGTH <=
  62. 0xffff,
  63. maximum_read_buffer_too_large);
  64. /* setup_read_buffer initializes the read buffer if not already initialized. It
  65. * returns one on success and zero on failure. */
  66. static int setup_read_buffer(SSL *ssl) {
  67. SSL3_BUFFER *buf = &ssl->s3->read_buffer;
  68. if (buf->buf != NULL) {
  69. return 1;
  70. }
  71. size_t header_len = ssl_record_prefix_len(ssl);
  72. size_t cap = SSL3_RT_MAX_ENCRYPTED_LENGTH;
  73. if (SSL_is_dtls(ssl)) {
  74. cap += DTLS1_RT_HEADER_LENGTH;
  75. } else {
  76. cap += SSL3_RT_HEADER_LENGTH;
  77. }
  78. return setup_buffer(buf, header_len, cap);
  79. }
  80. uint8_t *ssl_read_buffer(SSL *ssl) {
  81. return ssl->s3->read_buffer.buf + ssl->s3->read_buffer.offset;
  82. }
  83. size_t ssl_read_buffer_len(const SSL *ssl) {
  84. return ssl->s3->read_buffer.len;
  85. }
  86. static int dtls_read_buffer_next_packet(SSL *ssl) {
  87. SSL3_BUFFER *buf = &ssl->s3->read_buffer;
  88. if (buf->len > 0) {
  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->buf + buf->offset, (int)buf->cap);
  96. if (ret <= 0) {
  97. ssl->rwstate = SSL_READING;
  98. return ret;
  99. }
  100. /* |BIO_read| was bound by |buf->cap|, so this cannot overflow. */
  101. buf->len = (uint16_t)ret;
  102. return 1;
  103. }
  104. static int tls_read_buffer_extend_to(SSL *ssl, size_t len) {
  105. SSL3_BUFFER *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->len < 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, buf->buf + buf->offset + buf->len,
  115. (int)(len - buf->len));
  116. if (ret <= 0) {
  117. ssl->rwstate = SSL_READING;
  118. return ret;
  119. }
  120. /* |BIO_read| was bound by |buf->cap - buf->len|, so this cannot
  121. * overflow. */
  122. buf->len += (uint16_t)ret;
  123. }
  124. return 1;
  125. }
  126. int ssl_read_buffer_extend_to(SSL *ssl, size_t len) {
  127. /* |ssl_read_buffer_extend_to| implicitly discards any consumed data. */
  128. ssl_read_buffer_discard(ssl);
  129. if (!setup_read_buffer(ssl)) {
  130. return -1;
  131. }
  132. if (ssl->rbio == NULL) {
  133. OPENSSL_PUT_ERROR(SSL, SSL_R_BIO_NOT_SET);
  134. return -1;
  135. }
  136. int ret;
  137. if (SSL_is_dtls(ssl)) {
  138. /* |len| is ignored for a datagram transport. */
  139. ret = dtls_read_buffer_next_packet(ssl);
  140. } else {
  141. ret = tls_read_buffer_extend_to(ssl, len);
  142. }
  143. if (ret <= 0) {
  144. /* If the buffer was empty originally and remained empty after attempting to
  145. * extend it, release the buffer until the next attempt. */
  146. ssl_read_buffer_discard(ssl);
  147. }
  148. return ret;
  149. }
  150. void ssl_read_buffer_consume(SSL *ssl, size_t len) {
  151. SSL3_BUFFER *buf = &ssl->s3->read_buffer;
  152. consume_buffer(buf, len);
  153. /* The TLS stack never reads beyond the current record, so there will never be
  154. * unconsumed data. If read-ahead is ever reimplemented,
  155. * |ssl_read_buffer_discard| will require a |memcpy| to shift the excess back
  156. * to the front of the buffer, to ensure there is enough space for the next
  157. * record. */
  158. assert(SSL_is_dtls(ssl) || len == 0 || buf->len == 0);
  159. }
  160. void ssl_read_buffer_discard(SSL *ssl) {
  161. if (ssl->s3->read_buffer.len == 0) {
  162. ssl_read_buffer_clear(ssl);
  163. }
  164. }
  165. void ssl_read_buffer_clear(SSL *ssl) {
  166. clear_buffer(&ssl->s3->read_buffer);
  167. }
  168. int ssl_write_buffer_is_pending(const SSL *ssl) {
  169. return ssl->s3->write_buffer.len > 0;
  170. }
  171. OPENSSL_COMPILE_ASSERT(SSL3_RT_HEADER_LENGTH * 2 +
  172. SSL3_RT_SEND_MAX_ENCRYPTED_OVERHEAD * 2 +
  173. SSL3_RT_MAX_PLAIN_LENGTH <= 0xffff,
  174. maximum_tls_write_buffer_too_large);
  175. OPENSSL_COMPILE_ASSERT(DTLS1_RT_HEADER_LENGTH +
  176. SSL3_RT_SEND_MAX_ENCRYPTED_OVERHEAD +
  177. SSL3_RT_MAX_PLAIN_LENGTH <= 0xffff,
  178. maximum_dtls_write_buffer_too_large);
  179. int ssl_write_buffer_init(SSL *ssl, uint8_t **out_ptr, size_t max_len) {
  180. SSL3_BUFFER *buf = &ssl->s3->write_buffer;
  181. if (buf->buf != NULL) {
  182. OPENSSL_PUT_ERROR(SSL, ERR_R_INTERNAL_ERROR);
  183. return 0;
  184. }
  185. size_t header_len = ssl_seal_align_prefix_len(ssl);
  186. /* TODO(davidben): This matches the original behavior in keeping the malloc
  187. * size consistent. Does this matter? |cap| could just be |max_len|. */
  188. size_t cap = SSL3_RT_MAX_PLAIN_LENGTH + SSL3_RT_SEND_MAX_ENCRYPTED_OVERHEAD;
  189. if (SSL_is_dtls(ssl)) {
  190. cap += DTLS1_RT_HEADER_LENGTH;
  191. } else {
  192. cap += SSL3_RT_HEADER_LENGTH;
  193. if (ssl->mode & SSL_MODE_CBC_RECORD_SPLITTING) {
  194. cap += SSL3_RT_HEADER_LENGTH + SSL3_RT_SEND_MAX_ENCRYPTED_OVERHEAD;
  195. }
  196. }
  197. if (max_len > cap) {
  198. OPENSSL_PUT_ERROR(SSL, SSL_R_BUFFER_TOO_SMALL);
  199. return 0;
  200. }
  201. if (!setup_buffer(buf, header_len, cap)) {
  202. return 0;
  203. }
  204. *out_ptr = buf->buf + buf->offset;
  205. return 1;
  206. }
  207. void ssl_write_buffer_set_len(SSL *ssl, size_t len) {
  208. SSL3_BUFFER *buf = &ssl->s3->write_buffer;
  209. if (len > buf->cap) {
  210. abort();
  211. }
  212. buf->len = len;
  213. }
  214. static int tls_write_buffer_flush(SSL *ssl) {
  215. SSL3_BUFFER *buf = &ssl->s3->write_buffer;
  216. while (buf->len > 0) {
  217. int ret = BIO_write(ssl->wbio, buf->buf + buf->offset, buf->len);
  218. if (ret <= 0) {
  219. ssl->rwstate = SSL_WRITING;
  220. return ret;
  221. }
  222. consume_buffer(buf, (size_t)ret);
  223. }
  224. ssl_write_buffer_clear(ssl);
  225. return 1;
  226. }
  227. static int dtls_write_buffer_flush(SSL *ssl) {
  228. SSL3_BUFFER *buf = &ssl->s3->write_buffer;
  229. if (buf->len == 0) {
  230. return 1;
  231. }
  232. int ret = BIO_write(ssl->wbio, buf->buf + buf->offset, buf->len);
  233. if (ret <= 0) {
  234. ssl->rwstate = SSL_WRITING;
  235. /* If the write failed, drop the write buffer anyway. Datagram transports
  236. * can't write half a packet, so the caller is expected to retry from the
  237. * top. */
  238. ssl_write_buffer_clear(ssl);
  239. return ret;
  240. }
  241. ssl_write_buffer_clear(ssl);
  242. return 1;
  243. }
  244. int ssl_write_buffer_flush(SSL *ssl) {
  245. if (ssl->wbio == NULL) {
  246. OPENSSL_PUT_ERROR(SSL, SSL_R_BIO_NOT_SET);
  247. return -1;
  248. }
  249. if (SSL_is_dtls(ssl)) {
  250. return dtls_write_buffer_flush(ssl);
  251. } else {
  252. return tls_write_buffer_flush(ssl);
  253. }
  254. }
  255. void ssl_write_buffer_clear(SSL *ssl) {
  256. clear_buffer(&ssl->s3->write_buffer);
  257. }