Debian Codegraph @

crt/s2n/tls/s2n_early_data_io.c

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