Non puoi selezionare più di 25 argomenti Gli argomenti devono iniziare con una lettera o un numero, possono includere trattini ('-') e possono essere lunghi fino a 35 caratteri.
 
 
 
 
 
 

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