s2n_early_data_io.c 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277
  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 <sys/param.h>
  16. #include "tls/s2n_connection.h"
  17. #include "tls/s2n_early_data.h"
  18. #include "utils/s2n_mem.h"
  19. #include "utils/s2n_safety.h"
  20. int s2n_end_of_early_data_send(struct s2n_connection *conn)
  21. {
  22. if (conn->early_data_expected) {
  23. POSIX_GUARD(s2n_stuffer_wipe(&conn->handshake.io));
  24. POSIX_BAIL(S2N_ERR_EARLY_DATA_BLOCKED);
  25. }
  26. POSIX_GUARD_RESULT(s2n_connection_set_early_data_state(conn, S2N_END_OF_EARLY_DATA));
  27. return S2N_SUCCESS;
  28. }
  29. int s2n_end_of_early_data_recv(struct s2n_connection *conn)
  30. {
  31. POSIX_ENSURE(!s2n_connection_is_quic_enabled(conn), S2N_ERR_BAD_MESSAGE);
  32. POSIX_GUARD_RESULT(s2n_connection_set_early_data_state(conn, S2N_END_OF_EARLY_DATA));
  33. return S2N_SUCCESS;
  34. }
  35. /**
  36. *= https://tools.ietf.org/rfc/rfc8446#section-4.2.10
  37. *# If the client attempts a 0-RTT handshake but the server
  38. *# rejects it, the server will generally not have the 0-RTT record
  39. *# protection keys and must instead use trial decryption (either with
  40. *# the 1-RTT handshake keys or by looking for a cleartext ClientHello in
  41. *# the case of a HelloRetryRequest) to find the first non-0-RTT message.
  42. */
  43. bool s2n_early_data_is_trial_decryption_allowed(struct s2n_connection *conn, uint8_t record_type)
  44. {
  45. return conn && (conn->early_data_state == S2N_EARLY_DATA_REJECTED)
  46. && record_type == TLS_APPLICATION_DATA
  47. /* Only servers receive early data. */
  48. && (conn->mode == S2N_SERVER)
  49. /* Early data is only expected during the handshake. */
  50. && (s2n_conn_get_current_message_type(conn) != APPLICATION_DATA);
  51. }
  52. static bool s2n_is_early_data_io(struct s2n_connection *conn)
  53. {
  54. if (s2n_conn_get_current_message_type(conn) == APPLICATION_DATA) {
  55. return false;
  56. }
  57. /* It would be more accurate to not include this check.
  58. * However, before the early data feature was added, s2n_send and s2n_recv
  59. * did not verify that they were being called after a complete handshake.
  60. * Enforcing that broke several S2N tests, and might have broken customers too.
  61. *
  62. * Therefore, only consider this early data if the customer has indicated that
  63. * they are aware of early data, either because early data is currently expected
  64. * or early data is in a state that indicates that early data was previously expected.
  65. */
  66. if (conn->early_data_expected
  67. || (conn->mode == S2N_CLIENT && conn->early_data_state == S2N_EARLY_DATA_REQUESTED)
  68. || conn->early_data_state == S2N_EARLY_DATA_ACCEPTED
  69. || conn->early_data_state == S2N_END_OF_EARLY_DATA) {
  70. return true;
  71. }
  72. return false;
  73. }
  74. S2N_RESULT s2n_early_data_record_bytes(struct s2n_connection *conn, ssize_t data_len)
  75. {
  76. RESULT_ENSURE_REF(conn);
  77. if (data_len < 0 || !s2n_is_early_data_io(conn)) {
  78. return S2N_RESULT_OK;
  79. }
  80. /* Ensure the bytes read are within the bounds of what we can actually record. */
  81. if ((size_t) data_len > (UINT64_MAX - conn->early_data_bytes)) {
  82. conn->early_data_bytes = UINT64_MAX;
  83. RESULT_BAIL(S2N_ERR_INTEGER_OVERFLOW);
  84. }
  85. /* Record the early data bytes read, even if they exceed the max_early_data_size.
  86. * This will ensure that if this method is called again, it will fail again:
  87. * Once we receive too many bytes, we can't proceed with the connection. */
  88. conn->early_data_bytes += data_len;
  89. uint32_t max_early_data_size = 0;
  90. RESULT_GUARD_POSIX(s2n_connection_get_max_early_data_size(conn, &max_early_data_size));
  91. RESULT_ENSURE(conn->early_data_bytes <= max_early_data_size, S2N_ERR_MAX_EARLY_DATA_SIZE);
  92. return S2N_RESULT_OK;
  93. }
  94. S2N_RESULT s2n_early_data_validate_send(struct s2n_connection *conn, uint32_t bytes_to_send)
  95. {
  96. RESULT_ENSURE_REF(conn);
  97. if (!s2n_is_early_data_io(conn)) {
  98. return S2N_RESULT_OK;
  99. }
  100. RESULT_ENSURE(conn->early_data_expected, S2N_ERR_EARLY_DATA_NOT_ALLOWED);
  101. RESULT_ENSURE(conn->mode == S2N_CLIENT, S2N_ERR_EARLY_DATA_NOT_ALLOWED);
  102. RESULT_ENSURE(conn->early_data_state == S2N_EARLY_DATA_REQUESTED
  103. || conn->early_data_state == S2N_EARLY_DATA_ACCEPTED,
  104. S2N_ERR_EARLY_DATA_NOT_ALLOWED);
  105. uint32_t allowed_early_data_size = 0;
  106. RESULT_GUARD_POSIX(s2n_connection_get_remaining_early_data_size(conn, &allowed_early_data_size));
  107. RESULT_ENSURE(bytes_to_send <= allowed_early_data_size, S2N_ERR_MAX_EARLY_DATA_SIZE);
  108. return S2N_RESULT_OK;
  109. }
  110. S2N_RESULT s2n_early_data_validate_recv(struct s2n_connection *conn)
  111. {
  112. RESULT_ENSURE_REF(conn);
  113. if (!s2n_is_early_data_io(conn)) {
  114. return S2N_RESULT_OK;
  115. }
  116. RESULT_ENSURE(conn->early_data_expected, S2N_ERR_EARLY_DATA_NOT_ALLOWED);
  117. RESULT_ENSURE(conn->mode == S2N_SERVER, S2N_ERR_EARLY_DATA_NOT_ALLOWED);
  118. RESULT_ENSURE(conn->early_data_state == S2N_EARLY_DATA_ACCEPTED, S2N_ERR_EARLY_DATA_NOT_ALLOWED);
  119. RESULT_ENSURE(s2n_conn_get_current_message_type(conn) == END_OF_EARLY_DATA, S2N_ERR_EARLY_DATA_NOT_ALLOWED);
  120. return S2N_RESULT_OK;
  121. }
  122. static bool s2n_early_data_can_continue(struct s2n_connection *conn)
  123. {
  124. uint32_t remaining_early_data_size = 0;
  125. return s2n_connection_get_remaining_early_data_size(conn, &remaining_early_data_size) >= S2N_SUCCESS
  126. && remaining_early_data_size > 0;
  127. }
  128. S2N_RESULT s2n_send_early_data_impl(struct s2n_connection *conn, const uint8_t *data, ssize_t data_len_signed,
  129. ssize_t *data_sent, s2n_blocked_status *blocked)
  130. {
  131. RESULT_ENSURE_GTE(data_len_signed, 0);
  132. size_t data_len = data_len_signed;
  133. RESULT_ENSURE_REF(conn);
  134. RESULT_ENSURE_REF(blocked);
  135. *blocked = S2N_NOT_BLOCKED;
  136. RESULT_ENSURE_REF(data_sent);
  137. *data_sent = 0;
  138. RESULT_ENSURE(conn->mode == S2N_CLIENT, S2N_ERR_SERVER_MODE);
  139. RESULT_ENSURE(s2n_connection_supports_tls13(conn), S2N_ERR_PROTOCOL_VERSION_UNSUPPORTED);
  140. if (!s2n_early_data_can_continue(conn)) {
  141. return S2N_RESULT_OK;
  142. }
  143. /* Attempt to make progress in the handshake even if s2n_send eventually fails.
  144. * We only care about the result of this call if it would prevent us from calling s2n_send. */
  145. int negotiate_result = s2n_negotiate(conn, blocked);
  146. if (negotiate_result < S2N_SUCCESS) {
  147. if (s2n_error_get_type(s2n_errno) != S2N_ERR_T_BLOCKED) {
  148. return S2N_RESULT_ERROR;
  149. } else if (*blocked != S2N_BLOCKED_ON_EARLY_DATA && *blocked != S2N_BLOCKED_ON_READ) {
  150. return S2N_RESULT_ERROR;
  151. }
  152. }
  153. /* Save the error status for later */
  154. int negotiate_error = s2n_errno;
  155. s2n_blocked_status negotiate_blocked = *blocked;
  156. /* Attempt to send the early data.
  157. * We only care about the result of this call if it fails. */
  158. uint32_t early_data_to_send = 0;
  159. RESULT_GUARD_POSIX(s2n_connection_get_remaining_early_data_size(conn, &early_data_to_send));
  160. early_data_to_send = MIN(data_len, early_data_to_send);
  161. if (early_data_to_send) {
  162. ssize_t send_result = s2n_send(conn, data, early_data_to_send, blocked);
  163. RESULT_GUARD_POSIX(send_result);
  164. *data_sent = send_result;
  165. }
  166. *blocked = S2N_NOT_BLOCKED;
  167. /* Since the send was successful, report the result of the original negotiate call.
  168. * If we got this far, the result must have been success or a blocking error. */
  169. if (negotiate_result < S2N_SUCCESS) {
  170. RESULT_ENSURE_EQ(s2n_error_get_type(negotiate_error), S2N_ERR_T_BLOCKED);
  171. if (negotiate_blocked == S2N_BLOCKED_ON_EARLY_DATA) {
  172. return S2N_RESULT_OK;
  173. } else if (s2n_early_data_can_continue(conn)) {
  174. *blocked = negotiate_blocked;
  175. RESULT_BAIL(negotiate_error);
  176. } else {
  177. return S2N_RESULT_OK;
  178. }
  179. }
  180. return S2N_RESULT_OK;
  181. }
  182. int s2n_send_early_data(struct s2n_connection *conn, const uint8_t *data, ssize_t data_len,
  183. ssize_t *data_sent, s2n_blocked_status *blocked)
  184. {
  185. POSIX_ENSURE_REF(conn);
  186. /* Calling this method indicates that we expect early data. */
  187. POSIX_GUARD(s2n_connection_set_early_data_expected(conn));
  188. s2n_result result = s2n_send_early_data_impl(conn, data, data_len, data_sent, blocked);
  189. /* Unless s2n_send_early_data is called again (undoing this), we are done sending early data.
  190. * If s2n_negotiate is called next, we could send the EndOfEarlyData message. */
  191. POSIX_GUARD(s2n_connection_set_end_of_early_data(conn));
  192. POSIX_GUARD_RESULT(result);
  193. return S2N_SUCCESS;
  194. }
  195. S2N_RESULT s2n_recv_early_data_impl(struct s2n_connection *conn, uint8_t *data, ssize_t max_data_len,
  196. ssize_t *data_received, s2n_blocked_status *blocked)
  197. {
  198. RESULT_ENSURE_REF(conn);
  199. RESULT_ENSURE_REF(blocked);
  200. *blocked = S2N_NOT_BLOCKED;
  201. RESULT_ENSURE_REF(data_received);
  202. *data_received = 0;
  203. RESULT_ENSURE(conn->mode == S2N_SERVER, S2N_ERR_CLIENT_MODE);
  204. if (!s2n_early_data_can_continue(conn)) {
  205. return S2N_RESULT_OK;
  206. }
  207. while (s2n_negotiate(conn, blocked) < S2N_SUCCESS) {
  208. if (s2n_error_get_type(s2n_errno) != S2N_ERR_T_BLOCKED) {
  209. return S2N_RESULT_ERROR;
  210. } else if (max_data_len <= *data_received) {
  211. return S2N_RESULT_ERROR;
  212. } else if (*blocked != S2N_BLOCKED_ON_EARLY_DATA) {
  213. if (s2n_early_data_can_continue(conn)) {
  214. return S2N_RESULT_ERROR;
  215. } else {
  216. *blocked = S2N_NOT_BLOCKED;
  217. return S2N_RESULT_OK;
  218. }
  219. }
  220. ssize_t recv_result = s2n_recv(conn, data + *data_received,
  221. max_data_len - *data_received, blocked);
  222. RESULT_GUARD_POSIX(recv_result);
  223. *data_received += recv_result;
  224. }
  225. return S2N_RESULT_OK;
  226. }
  227. int s2n_recv_early_data(struct s2n_connection *conn, uint8_t *data, ssize_t max_data_len,
  228. ssize_t *data_received, s2n_blocked_status *blocked)
  229. {
  230. /* Calling this method indicates that we expect early data. */
  231. POSIX_GUARD(s2n_connection_set_early_data_expected(conn));
  232. s2n_result result = s2n_recv_early_data_impl(conn, data, max_data_len, data_received, blocked);
  233. /* Unless s2n_recv_early_data is called again (undoing this), we are done accepting early data. */
  234. POSIX_GUARD(s2n_connection_set_end_of_early_data(conn));
  235. POSIX_GUARD_RESULT(result);
  236. return S2N_SUCCESS;
  237. }