Debian Codegraph @

crt/s2n/tls/s2n_quic_support.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_quic_support.h"
17
18 #include "tls/s2n_connection.h"
19 #include "tls/s2n_tls.h"
20 #include "tls/s2n_tls13.h"
21 #include "utils/s2n_mem.h"
22 #include "utils/s2n_safety.h"
23
24 /* When reading and writing records with TCP, S2N sets its input and output buffers
25 * to the maximum record fragment size to prevent resizing those buffers later.
26 *
27 * However, because S2N with QUIC reads and writes messages instead of records,
28 * the "maximum size" for the input and output buffers would be the maximum message size: 64k.
29 * Since most messages are MUCH smaller than that (<3k), setting the buffer that large is wasteful.
30 *
31 * Instead, we intentionally choose a smaller size and accept that an abnormally large message
32 * could cause the buffer to resize. */
33 #define S2N_EXPECTED_QUIC_MESSAGE_SIZE S2N_DEFAULT_FRAGMENT_LENGTH
34
35 S2N_RESULT s2n_read_in_bytes(struct s2n_connection *conn, struct s2n_stuffer *output, uint32_t length);
36
37 int s2n_config_enable_quic(struct s2n_config *config)
38 {
39 POSIX_ENSURE_REF(config);
40 config->quic_enabled = true;
41 return S2N_SUCCESS;
42 }
43
44 int s2n_connection_enable_quic(struct s2n_connection *conn)
45 {
46 POSIX_ENSURE_REF(conn);
47 POSIX_GUARD_RESULT(s2n_connection_validate_tls13_support(conn));
48 /* QUIC support is not currently compatible with recv_buffering */
49 POSIX_ENSURE(!conn->recv_buffering, S2N_ERR_INVALID_STATE);
50 conn->quic_enabled = true;
51 return S2N_SUCCESS;
52 }
53
54 bool s2n_connection_is_quic_enabled(struct s2n_connection *conn)
55 {
56 return (conn && conn->quic_enabled) || (conn && conn->config && conn->config->quic_enabled);
57 }
58
59 bool s2n_connection_are_session_tickets_enabled(struct s2n_connection *conn)
60 {
61 return conn && conn->config && conn->config->use_tickets;
62 }
63
64 int s2n_connection_set_quic_transport_parameters(struct s2n_connection *conn,
65 const uint8_t *data_buffer, uint16_t data_len)
66 {
67 POSIX_ENSURE_REF(conn);
68
69 POSIX_GUARD(s2n_free(&conn->our_quic_transport_parameters));
70 POSIX_GUARD(s2n_alloc(&conn->our_quic_transport_parameters, data_len));
71 POSIX_CHECKED_MEMCPY(conn->our_quic_transport_parameters.data, data_buffer, data_len);
72
73 return S2N_SUCCESS;
74 }
75
76 int s2n_connection_get_quic_transport_parameters(struct s2n_connection *conn,
77 const uint8_t **data_buffer, uint16_t *data_len)
78 {
79 POSIX_ENSURE_REF(conn);
80 POSIX_ENSURE_REF(data_buffer);
81 POSIX_ENSURE_REF(data_len);
82
83 *data_buffer = conn->peer_quic_transport_parameters.data;
84 *data_len = conn->peer_quic_transport_parameters.size;
85
86 return S2N_SUCCESS;
87 }
88
89 int s2n_connection_set_secret_callback(struct s2n_connection *conn, s2n_secret_cb cb_func, void *ctx)
90 {
91 POSIX_ENSURE_REF(conn);
92 POSIX_ENSURE_REF(cb_func);
93
94 conn->secret_cb = cb_func;
95 conn->secret_cb_context = ctx;
96
97 return S2N_SUCCESS;
98 }
99
100 /* Currently we need an API that quic can call to process post-handshake messages. Ideally
101 * we could re-use the s2n_recv API but that function needs to be refactored to support quic.
102 * For now we just call this API.
103 */
104 int s2n_recv_quic_post_handshake_message(struct s2n_connection *conn, s2n_blocked_status *blocked)
105 {
106 POSIX_ENSURE_REF(conn);
107 POSIX_ENSURE_REF(blocked);
108
109 *blocked = S2N_BLOCKED_ON_READ;
110
111 uint8_t message_type = 0;
112 /* This function uses the stuffer conn->handshake.io to read in the header. This stuffer is also used
113 * for sending post-handshake messages. This could cause a concurrency issue if we start both sending
114 * and receiving post-handshake messages while quic is enabled. Currently there's no post-handshake
115 * message that is both sent and received in quic (servers only send session tickets
116 * and clients only receive session tickets.) Therefore it is safe for us
117 * to use the stuffer here.
118 */
119 POSIX_GUARD_RESULT(s2n_quic_read_handshake_message(conn, &message_type));
120
121 /* The only post-handshake messages we support from QUIC currently are session tickets */
122 POSIX_ENSURE(message_type == TLS_SERVER_NEW_SESSION_TICKET, S2N_ERR_UNSUPPORTED_WITH_QUIC);
123 POSIX_GUARD_RESULT(s2n_post_handshake_process(conn, &conn->in, message_type));
124
125 /* Successfully read the message, wipe the header and message buffer */
126 POSIX_GUARD(s2n_stuffer_wipe(&conn->handshake.io));
127 POSIX_GUARD_RESULT(s2n_record_wipe(conn));
128
129 *blocked = S2N_NOT_BLOCKED;
130
131 return S2N_SUCCESS;
132 }
133
134 /* When using QUIC, S2N reads unencrypted handshake messages instead of encrypted records.
135 * This method sets up the S2N input buffers to match the results of using s2n_read_full_record.
136 */
137 S2N_RESULT s2n_quic_read_handshake_message(struct s2n_connection *conn, uint8_t *message_type)
138 {
139 RESULT_ENSURE_REF(conn);
140 /* The use of handshake.io here would complicate recv_buffering, and there's
141 * no real use case for recv_buffering when QUIC is handling the IO.
142 */
143 RESULT_ENSURE(!conn->recv_buffering, S2N_ERR_INVALID_STATE);
144
145 /* Allocate stuffer space now so that we don't have to realloc later in the handshake. */
146 RESULT_GUARD_POSIX(s2n_stuffer_resize_if_empty(&conn->buffer_in, S2N_EXPECTED_QUIC_MESSAGE_SIZE));
147
148 RESULT_GUARD(s2n_read_in_bytes(conn, &conn->handshake.io, TLS_HANDSHAKE_HEADER_LENGTH));
149
150 uint32_t message_len = 0;
151 RESULT_GUARD(s2n_handshake_parse_header(&conn->handshake.io, message_type, &message_len));
152 /* Reset the read cursor rather than wiping the stuffer. During the
153 * handshake, s2n_read_full_handshake_message expects the header to still
154 * be present in handshake.io so it can accumulate the full message.
155 */
156 RESULT_GUARD_POSIX(s2n_stuffer_reread(&conn->handshake.io));
157
158 RESULT_ENSURE(message_len < S2N_MAXIMUM_HANDSHAKE_MESSAGE_LENGTH, S2N_ERR_BAD_MESSAGE);
159 RESULT_GUARD(s2n_read_in_bytes(conn, &conn->buffer_in, message_len));
160
161 /* Although we call s2n_read_in_bytes, recv_greedy is always disabled for quic.
162 * Therefore buffer_in will always contain exactly message_len bytes of data.
163 * So we don't need to handle the possibility of extra data in buffer_in.
164 */
165 RESULT_ENSURE_EQ(s2n_stuffer_data_available(&conn->buffer_in), message_len);
166 RESULT_GUARD(s2n_recv_in_init(conn, message_len, message_len));
167 return S2N_RESULT_OK;
168 }
169
170 /* When using QUIC, S2N writes unencrypted handshake messages instead of encrypted records.
171 * This method sets up the S2N output buffer to match the result of using s2n_record_write.
172 */
173 S2N_RESULT s2n_quic_write_handshake_message(struct s2n_connection *conn)
174 {
175 RESULT_ENSURE_REF(conn);
176
177 /* Allocate stuffer space now so that we don't have to realloc later in the handshake. */
178 RESULT_GUARD_POSIX(s2n_stuffer_resize_if_empty(&conn->out, S2N_EXPECTED_QUIC_MESSAGE_SIZE));
179
180 RESULT_GUARD_POSIX(s2n_stuffer_copy(&conn->handshake.io, &conn->out,
181 s2n_stuffer_data_available(&conn->handshake.io)));
182 return S2N_RESULT_OK;
183 }