Debian Codegraph @

crt/s2n/tls/s2n_recv.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 /* Use usleep */
17 #define _XOPEN_SOURCE 500
18 #include <errno.h>
19 #include <unistd.h>
20
21 #include "api/s2n.h"
22 #include "error/s2n_errno.h"
23 #include "stuffer/s2n_stuffer.h"
24 #include "tls/s2n_alerts.h"
25 #include "tls/s2n_connection.h"
26 #include "tls/s2n_handshake.h"
27 #include "tls/s2n_ktls.h"
28 #include "tls/s2n_post_handshake.h"
29 #include "tls/s2n_record.h"
30 #include "tls/s2n_resume.h"
31 #include "tls/s2n_tls.h"
32 #include "utils/s2n_blob.h"
33 #include "utils/s2n_io.h"
34 #include "utils/s2n_safety.h"
35
36 S2N_RESULT s2n_recv_in_init(struct s2n_connection *conn, uint32_t written, uint32_t total)
37 {
38 RESULT_ENSURE_REF(conn);
39
40 /* If we're going to initialize conn->in to point to more memory than
41 * is actually readable, make sure that the additional memory exists.
42 */
43 RESULT_ENSURE_LTE(written, total);
44 uint32_t remaining = total - written;
45 RESULT_ENSURE_LTE(remaining, s2n_stuffer_space_remaining(&conn->buffer_in));
46
47 uint8_t *data = s2n_stuffer_raw_read(&conn->buffer_in, written);
48 RESULT_ENSURE_REF(data);
49 RESULT_GUARD_POSIX(s2n_stuffer_free(&conn->in));
50 RESULT_GUARD_POSIX(s2n_blob_init(&conn->in.blob, data, total));
51 RESULT_GUARD_POSIX(s2n_stuffer_skip_write(&conn->in, written));
52 return S2N_RESULT_OK;
53 }
54
55 S2N_RESULT s2n_read_in_bytes(struct s2n_connection *conn, struct s2n_stuffer *output, uint32_t length)
56 {
57 while (s2n_stuffer_data_available(output) < length) {
58 uint32_t remaining = length - s2n_stuffer_data_available(output);
59 if (conn->recv_buffering) {
60 remaining = S2N_MAX(remaining, s2n_stuffer_space_remaining(output));
61 }
62 errno = 0;
63 int r = s2n_connection_recv_stuffer(output, conn, remaining);
64 if (r == 0) {
65 s2n_atomic_flag_set(&conn->read_closed);
66 }
67 RESULT_GUARD(s2n_io_check_read_result(r));
68 conn->wire_bytes_in += r;
69 }
70
71 return S2N_RESULT_OK;
72 }
73
74 static S2N_RESULT s2n_recv_buffer_in(struct s2n_connection *conn, size_t min_size)
75 {
76 RESULT_GUARD_POSIX(s2n_stuffer_resize_if_empty(&conn->buffer_in, S2N_LARGE_FRAGMENT_LENGTH));
77 uint32_t buffer_in_available = s2n_stuffer_data_available(&conn->buffer_in);
78 if (buffer_in_available < min_size) {
79 uint32_t remaining = min_size - buffer_in_available;
80 if (s2n_stuffer_space_remaining(&conn->buffer_in) < remaining) {
81 RESULT_GUARD_POSIX(s2n_stuffer_shift(&conn->buffer_in));
82 }
83 RESULT_GUARD(s2n_read_in_bytes(conn, &conn->buffer_in, min_size));
84 }
85 return S2N_RESULT_OK;
86 }
87
88 int s2n_read_full_record(struct s2n_connection *conn, uint8_t *record_type, int *isSSLv2)
89 {
90 *isSSLv2 = 0;
91
92 if (conn->ktls_recv_enabled) {
93 return s2n_ktls_read_full_record(conn, record_type);
94 }
95
96 /* If the record has already been decrypted, then leave it alone */
97 if (conn->in_status == PLAINTEXT) {
98 /* Only application data packets count as plaintext */
99 *record_type = TLS_APPLICATION_DATA;
100 return S2N_SUCCESS;
101 }
102
103 /* Read the record until we at least have a header */
104 POSIX_GUARD(s2n_stuffer_reread(&conn->header_in));
105 uint32_t header_available = s2n_stuffer_data_available(&conn->header_in);
106 if (header_available < S2N_TLS_RECORD_HEADER_LENGTH) {
107 uint32_t header_remaining = S2N_TLS_RECORD_HEADER_LENGTH - header_available;
108 s2n_result ret = s2n_recv_buffer_in(conn, header_remaining);
109 uint32_t header_read = S2N_MIN(header_remaining, s2n_stuffer_data_available(&conn->buffer_in));
110 POSIX_GUARD(s2n_stuffer_copy(&conn->buffer_in, &conn->header_in, header_read));
111 POSIX_GUARD_RESULT(ret);
112 }
113
114 uint16_t fragment_length = 0;
115
116 /* If the first bit is set then this is an SSLv2 record */
117 if (conn->header_in.blob.data[0] & S2N_TLS_SSLV2_HEADER_FLAG) {
118 *isSSLv2 = 1;
119 WITH_ERROR_BLINDING(conn, POSIX_GUARD(s2n_sslv2_record_header_parse(conn, record_type, &conn->client_hello.legacy_version, &fragment_length)));
120 } else {
121 WITH_ERROR_BLINDING(conn, POSIX_GUARD(s2n_record_header_parse(conn, record_type, &fragment_length)));
122 }
123
124 /* Read enough to have the whole record */
125 uint32_t fragment_available = s2n_stuffer_data_available(&conn->in);
126 if (fragment_available < fragment_length || fragment_length == 0) {
127 POSIX_GUARD(s2n_stuffer_rewind_read(&conn->buffer_in, fragment_available));
128 s2n_result ret = s2n_recv_buffer_in(conn, fragment_length);
129 uint32_t fragment_read = S2N_MIN(fragment_length, s2n_stuffer_data_available(&conn->buffer_in));
130 POSIX_GUARD_RESULT(s2n_recv_in_init(conn, fragment_read, fragment_length));
131 POSIX_GUARD_RESULT(ret);
132 }
133
134 if (*isSSLv2) {
135 return 0;
136 }
137
138 /* Decrypt and parse the record */
139 if (s2n_early_data_is_trial_decryption_allowed(conn, *record_type)) {
140 POSIX_ENSURE(s2n_record_parse(conn) >= S2N_SUCCESS, S2N_ERR_EARLY_DATA_TRIAL_DECRYPT);
141 } else {
142 WITH_ERROR_BLINDING(conn, POSIX_GUARD(s2n_record_parse(conn)));
143 }
144
145 /* In TLS 1.3, encrypted handshake records would appear to be of record type
146 * TLS_APPLICATION_DATA. The actual record content type is found after the encrypted
147 * is decrypted.
148 */
149 if (conn->actual_protocol_version == S2N_TLS13 && *record_type == TLS_APPLICATION_DATA) {
150 POSIX_GUARD(s2n_tls13_parse_record_type(&conn->in, record_type));
151 }
152
153 return 0;
154 }
155
156 ssize_t s2n_recv_impl(struct s2n_connection *conn, void *buf, ssize_t size_signed, s2n_blocked_status *blocked)
157 {
158 POSIX_ENSURE_GTE(size_signed, 0);
159 size_t size = size_signed;
160 ssize_t bytes_read = 0;
161 struct s2n_blob out = { 0 };
162 POSIX_GUARD(s2n_blob_init(&out, (uint8_t *) buf, 0));
163
164 /*
165 * Set the `blocked` status to BLOCKED_ON_READ by default
166 *
167 * The only case in which it should be updated is on a successful read into the provided buffer.
168 *
169 * Unfortunately, the current `blocked` behavior has become ossified by buggy applications that ignore
170 * error types and only read `blocked`. As such, it's very important to avoid changing how this value is updated
171 * as it could break applications.
172 */
173 *blocked = S2N_BLOCKED_ON_READ;
174
175 if (!s2n_connection_check_io_status(conn, S2N_IO_READABLE)) {
176 /*
177 *= https://www.rfc-editor.org/rfc/rfc8446#6.1
178 *# If a transport-level close
179 *# is received prior to a "close_notify", the receiver cannot know that
180 *# all the data that was sent has been received.
181 *
182 *= https://www.rfc-editor.org/rfc/rfc8446#6.1
183 *# If the application protocol using TLS provides that any data may be
184 *# carried over the underlying transport after the TLS connection is
185 *# closed, the TLS implementation MUST receive a "close_notify" alert
186 *# before indicating end-of-data to the application layer.
187 */
188 POSIX_ENSURE(s2n_atomic_flag_test(&conn->close_notify_received), S2N_ERR_CLOSED);
189 *blocked = S2N_NOT_BLOCKED;
190 return 0;
191 }
192
193 POSIX_ENSURE(!s2n_connection_is_quic_enabled(conn), S2N_ERR_UNSUPPORTED_WITH_QUIC);
194 POSIX_GUARD_RESULT(s2n_early_data_validate_recv(conn));
195
196 while (size && s2n_connection_check_io_status(conn, S2N_IO_READABLE)) {
197 int isSSLv2 = 0;
198 uint8_t record_type = 0;
199 int r = s2n_read_full_record(conn, &record_type, &isSSLv2);
200 if (r < 0) {
201 /* Don't propagate the error if we already read some bytes. */
202 if (bytes_read && (s2n_errno == S2N_ERR_CLOSED || s2n_errno == S2N_ERR_IO_BLOCKED)) {
203 break;
204 }
205
206 /* If we get here, it's an error condition.
207 * For stateful resumption, invalidate the session on error to prevent resumption with
208 * potentially corrupted session state. This ensures that a bad session state does not
209 * lead to repeated failures during resumption attempts.
210 */
211 if (s2n_errno != S2N_ERR_IO_BLOCKED && s2n_allowed_to_cache_connection(conn) && conn->session_id_len) {
212 conn->config->cache_delete(conn, conn->config->cache_delete_data, conn->session_id, conn->session_id_len);
213 }
214
215 S2N_ERROR_PRESERVE_ERRNO();
216 }
217
218 S2N_ERROR_IF(isSSLv2, S2N_ERR_BAD_MESSAGE);
219
220 if (record_type != TLS_HANDSHAKE) {
221 /*
222 *= https://www.rfc-editor.org/rfc/rfc8446#section-5.1
223 *# - Handshake messages MUST NOT be interleaved with other record
224 *# types. That is, if a handshake message is split over two or more
225 *# records, there MUST NOT be any other records between them.
226 */
227 POSIX_ENSURE(s2n_stuffer_is_wiped(&conn->post_handshake.in), S2N_ERR_BAD_MESSAGE);
228
229 /* If not handling a handshake message, free the post-handshake memory.
230 * Post-handshake messages are infrequent enough that we don't want to
231 * keep a potentially large buffer around unnecessarily.
232 */
233 if (!s2n_stuffer_is_freed(&conn->post_handshake.in)) {
234 POSIX_GUARD(s2n_stuffer_free(&conn->post_handshake.in));
235 }
236 }
237
238 if (record_type != TLS_APPLICATION_DATA) {
239 switch (record_type) {
240 case TLS_ALERT:
241 POSIX_GUARD(s2n_process_alert_fragment(conn));
242 break;
243 case TLS_HANDSHAKE: {
244 s2n_result result = s2n_post_handshake_recv(conn);
245 /* Ignore any errors due to insufficient input data from io.
246 * The next iteration of this loop will attempt to read more input data.
247 */
248 if (s2n_result_is_error(result) && s2n_errno != S2N_ERR_IO_BLOCKED) {
249 WITH_ERROR_BLINDING(conn, POSIX_GUARD_RESULT(result));
250 }
251 break;
252 }
253 }
254 POSIX_GUARD_RESULT(s2n_record_wipe(conn));
255 continue;
256 }
257
258 out.size = S2N_MIN(size, s2n_stuffer_data_available(&conn->in));
259
260 POSIX_GUARD(s2n_stuffer_erase_and_read(&conn->in, &out));
261 bytes_read += out.size;
262
263 out.data += out.size;
264 size -= out.size;
265
266 /* Are we ready for more encrypted data? */
267 if (s2n_stuffer_data_available(&conn->in) == 0) {
268 POSIX_GUARD_RESULT(s2n_record_wipe(conn));
269 }
270
271 /* If we've read some data, return it in legacy mode */
272 if (bytes_read && !conn->config->recv_multi_record) {
273 break;
274 }
275 }
276
277 /* Due to the history of this API, some applications depend on the blocked status to know if
278 * the connection's `in` stuffer was completely cleared. This behavior needs to be preserved.
279 *
280 * Moving forward, applications should instead use `s2n_peek`, which accomplishes the same thing
281 * without conflating being blocked on reading from the OS socket vs blocked on the application's
282 * buffer size.
283 */
284 if (s2n_stuffer_data_available(&conn->in) == 0) {
285 *blocked = S2N_NOT_BLOCKED;
286 }
287
288 return bytes_read;
289 }
290
291 ssize_t s2n_recv(struct s2n_connection *conn, void *buf, ssize_t size, s2n_blocked_status *blocked)
292 {
293 POSIX_ENSURE_REF(conn);
294 POSIX_ENSURE(!conn->recv_in_use, S2N_ERR_REENTRANCY);
295 conn->recv_in_use = true;
296
297 ssize_t result = s2n_recv_impl(conn, buf, size, blocked);
298 POSIX_GUARD_RESULT(s2n_early_data_record_bytes(conn, result));
299
300 /* finish the recv call */
301 POSIX_GUARD_RESULT(s2n_connection_dynamic_free_in_buffer(conn));
302
303 conn->recv_in_use = false;
304 return result;
305 }
306
307 uint32_t s2n_peek(struct s2n_connection *conn)
308 {
309 if (conn == NULL) {
310 return 0;
311 }
312
313 /* If we have partially buffered an encrypted record,
314 * we should not report those bytes as available to read.
315 */
316 if (conn->in_status != PLAINTEXT) {
317 return 0;
318 }
319
320 return s2n_stuffer_data_available(&conn->in);
321 }
322
323 uint32_t s2n_peek_buffered(struct s2n_connection *conn)
324 {
325 if (conn == NULL) {
326 return 0;
327 }
328 return s2n_stuffer_data_available(&conn->buffer_in);
329 }