s2n_send.c 9.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254
  1. /*
  2. * Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved.
  3. *
  4. * Licensed under the Apache License, Version 2.0 (the "License").
  5. * You may not use this file except in compliance with the License.
  6. * A copy of the License is located at
  7. *
  8. * http://aws.amazon.com/apache2.0
  9. *
  10. * or in the "license" file accompanying this file. This file is distributed
  11. * on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either
  12. * express or implied. See the License for the specific language governing
  13. * permissions and limitations under the License.
  14. */
  15. #include <errno.h>
  16. #include <sys/param.h>
  17. #include "api/s2n.h"
  18. #include "crypto/s2n_cipher.h"
  19. #include "error/s2n_errno.h"
  20. #include "stuffer/s2n_stuffer.h"
  21. #include "tls/s2n_alerts.h"
  22. #include "tls/s2n_cipher_suites.h"
  23. #include "tls/s2n_connection.h"
  24. #include "tls/s2n_handshake.h"
  25. #include "tls/s2n_ktls.h"
  26. #include "tls/s2n_post_handshake.h"
  27. #include "tls/s2n_record.h"
  28. #include "utils/s2n_blob.h"
  29. #include "utils/s2n_io.h"
  30. #include "utils/s2n_safety.h"
  31. /*
  32. * Determine whether there is currently sufficient space in the send buffer to construct
  33. * another record, or if we need to flush now.
  34. *
  35. * We only buffer multiple records when sending application data, NOT when
  36. * sending handshake messages or alerts. If the next record is a post-handshake message
  37. * or an alert, then the send buffer will be flushed regardless of the result of this method.
  38. * Therefore we don't need to consider the size of any potential KeyUpdate messages,
  39. * NewSessionTicket messages, or Alerts.
  40. */
  41. bool s2n_should_flush(struct s2n_connection *conn, ssize_t total_message_size)
  42. {
  43. /* Always flush if not buffering multiple records. */
  44. if (!conn->multirecord_send) {
  45. return true;
  46. }
  47. /* Flush if all data has been sent. */
  48. ssize_t remaining_payload_size = total_message_size - conn->current_user_data_consumed;
  49. if (remaining_payload_size <= 0) {
  50. return true;
  51. }
  52. uint16_t max_payload_size = 0;
  53. if (!s2n_result_is_ok(s2n_record_max_write_payload_size(conn, &max_payload_size))) {
  54. /* When in doubt, flush */
  55. return true;
  56. }
  57. max_payload_size = MIN(max_payload_size, remaining_payload_size);
  58. uint16_t max_write_size = 0;
  59. if (!s2n_result_is_ok(s2n_record_max_write_size(conn, max_payload_size, &max_write_size))) {
  60. /* When in doubt, flush */
  61. return true;
  62. }
  63. /* Flush if the stuffer can't store the max possible record size without growing.
  64. *
  65. * However, the stuffer is allocated when the record is sent, so if the stuffer
  66. * hasn't been allocated, assume it will have enough space.
  67. */
  68. uint32_t available_space = s2n_stuffer_space_remaining(&conn->out);
  69. if (available_space < max_write_size && !s2n_stuffer_is_freed(&conn->out)) {
  70. return true;
  71. }
  72. return false;
  73. }
  74. int s2n_flush(struct s2n_connection *conn, s2n_blocked_status *blocked)
  75. {
  76. *blocked = S2N_BLOCKED_ON_WRITE;
  77. /* Write any data that's already pending */
  78. while (s2n_stuffer_data_available(&conn->out)) {
  79. errno = 0;
  80. int w = s2n_connection_send_stuffer(&conn->out, conn, s2n_stuffer_data_available(&conn->out));
  81. POSIX_GUARD_RESULT(s2n_io_check_write_result(w));
  82. conn->wire_bytes_out += w;
  83. }
  84. POSIX_GUARD(s2n_stuffer_rewrite(&conn->out));
  85. if (conn->reader_warning_out) {
  86. POSIX_GUARD_RESULT(s2n_alerts_write_warning(conn));
  87. conn->reader_warning_out = 0;
  88. POSIX_GUARD(s2n_flush(conn, blocked));
  89. }
  90. *blocked = S2N_NOT_BLOCKED;
  91. return 0;
  92. }
  93. ssize_t s2n_sendv_with_offset_impl(struct s2n_connection *conn, const struct iovec *bufs,
  94. ssize_t count, ssize_t offs, s2n_blocked_status *blocked)
  95. {
  96. ssize_t user_data_sent, total_size = 0;
  97. POSIX_ENSURE(s2n_connection_check_io_status(conn, S2N_IO_WRITABLE), S2N_ERR_CLOSED);
  98. POSIX_ENSURE(!s2n_connection_is_quic_enabled(conn), S2N_ERR_UNSUPPORTED_WITH_QUIC);
  99. /* Flush any pending I/O */
  100. POSIX_GUARD(s2n_flush(conn, blocked));
  101. if (conn->ktls_send_enabled) {
  102. return s2n_ktls_sendv_with_offset(conn, bufs, count, offs, blocked);
  103. }
  104. /* Acknowledge consumed and flushed user data as sent */
  105. user_data_sent = conn->current_user_data_consumed;
  106. *blocked = S2N_BLOCKED_ON_WRITE;
  107. uint16_t max_payload_size = 0;
  108. POSIX_GUARD_RESULT(s2n_record_max_write_payload_size(conn, &max_payload_size));
  109. /* TLS 1.0 and SSLv3 are vulnerable to the so-called Beast attack. Work
  110. * around this by splitting messages into one byte records, and then
  111. * the remainder can follow as usual.
  112. */
  113. int cbcHackUsed = 0;
  114. struct s2n_crypto_parameters *writer = conn->server;
  115. if (conn->mode == S2N_CLIENT) {
  116. writer = conn->client;
  117. }
  118. /* Defensive check against an invalid retry */
  119. if (offs > 0) {
  120. const struct iovec *_bufs = bufs;
  121. ssize_t _count = count;
  122. while ((size_t) offs >= _bufs->iov_len && _count > 0) {
  123. offs -= _bufs->iov_len;
  124. _bufs++;
  125. _count--;
  126. }
  127. bufs = _bufs;
  128. count = _count;
  129. }
  130. for (ssize_t i = 0; i < count; i++) {
  131. total_size += bufs[i].iov_len;
  132. }
  133. total_size -= offs;
  134. S2N_ERROR_IF(conn->current_user_data_consumed > total_size, S2N_ERR_SEND_SIZE);
  135. POSIX_GUARD_RESULT(s2n_early_data_validate_send(conn, total_size));
  136. if (conn->dynamic_record_timeout_threshold > 0) {
  137. uint64_t elapsed;
  138. POSIX_GUARD_RESULT(s2n_timer_elapsed(conn->config, &conn->write_timer, &elapsed));
  139. /* Reset record size back to a single segment after threshold seconds of inactivity */
  140. if (elapsed - conn->last_write_elapsed > (uint64_t) conn->dynamic_record_timeout_threshold * 1000000000) {
  141. conn->active_application_bytes_consumed = 0;
  142. }
  143. conn->last_write_elapsed = elapsed;
  144. }
  145. /* Now write the data we were asked to send this round */
  146. while (total_size - conn->current_user_data_consumed) {
  147. ssize_t to_write = MIN(total_size - conn->current_user_data_consumed, max_payload_size);
  148. /* If dynamic record size is enabled,
  149. * use small TLS records that fit into a single TCP segment for the threshold bytes of data
  150. */
  151. if (conn->active_application_bytes_consumed < (uint64_t) conn->dynamic_record_resize_threshold) {
  152. uint16_t min_payload_size = 0;
  153. POSIX_GUARD_RESULT(s2n_record_min_write_payload_size(conn, &min_payload_size));
  154. to_write = MIN(min_payload_size, to_write);
  155. }
  156. /* Don't split messages in server mode for interoperability with naive clients.
  157. * Some clients may have expectations based on the amount of content in the first record.
  158. */
  159. if (conn->actual_protocol_version < S2N_TLS11
  160. && writer->cipher_suite->record_alg->cipher->type == S2N_CBC && conn->mode != S2N_SERVER) {
  161. if (to_write > 1 && cbcHackUsed == 0) {
  162. to_write = 1;
  163. cbcHackUsed = 1;
  164. }
  165. }
  166. POSIX_GUARD(s2n_post_handshake_send(conn, blocked));
  167. /* Write and encrypt the record */
  168. int written_to_record = s2n_record_writev(conn, TLS_APPLICATION_DATA, bufs, count,
  169. conn->current_user_data_consumed + offs, to_write);
  170. POSIX_GUARD(written_to_record);
  171. conn->current_user_data_consumed += written_to_record;
  172. conn->active_application_bytes_consumed += written_to_record;
  173. /* Send it, unless we're waiting for more records */
  174. if (s2n_should_flush(conn, total_size)) {
  175. if (s2n_flush(conn, blocked) < 0) {
  176. if (s2n_errno == S2N_ERR_IO_BLOCKED && user_data_sent > 0) {
  177. /* We successfully sent >0 user bytes on the wire, but not the full requested payload
  178. * because we became blocked on I/O. Acknowledge the data sent. */
  179. conn->current_user_data_consumed -= user_data_sent;
  180. return user_data_sent;
  181. } else {
  182. S2N_ERROR_PRESERVE_ERRNO();
  183. }
  184. }
  185. /* Acknowledge consumed and flushed user data as sent */
  186. user_data_sent = conn->current_user_data_consumed;
  187. }
  188. }
  189. /* If everything has been written, then there's no user data pending */
  190. conn->current_user_data_consumed = 0;
  191. *blocked = S2N_NOT_BLOCKED;
  192. return total_size;
  193. }
  194. ssize_t s2n_sendv_with_offset(struct s2n_connection *conn, const struct iovec *bufs, ssize_t count,
  195. ssize_t offs, s2n_blocked_status *blocked)
  196. {
  197. POSIX_ENSURE(!conn->send_in_use, S2N_ERR_REENTRANCY);
  198. conn->send_in_use = true;
  199. ssize_t result = s2n_sendv_with_offset_impl(conn, bufs, count, offs, blocked);
  200. POSIX_GUARD_RESULT(s2n_early_data_record_bytes(conn, result));
  201. POSIX_GUARD_RESULT(s2n_connection_dynamic_free_out_buffer(conn));
  202. conn->send_in_use = false;
  203. return result;
  204. }
  205. ssize_t s2n_sendv(struct s2n_connection *conn, const struct iovec *bufs, ssize_t count, s2n_blocked_status *blocked)
  206. {
  207. return s2n_sendv_with_offset(conn, bufs, count, 0, blocked);
  208. }
  209. ssize_t s2n_send(struct s2n_connection *conn, const void *buf, ssize_t size, s2n_blocked_status *blocked)
  210. {
  211. struct iovec iov;
  212. iov.iov_base = (void *) (uintptr_t) buf;
  213. iov.iov_len = size;
  214. return s2n_sendv_with_offset(conn, &iov, 1, 0, blocked);
  215. }