Debian Codegraph @

crt/s2n/tests/unit/s2n_post_handshake_test.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_post_handshake.h"
17
18 #include "error/s2n_errno.h"
19 #include "s2n_test.h"
20 #include "testlib/s2n_testlib.h"
21 #include "tls/s2n_connection.h"
22 #include "tls/s2n_key_update.h"
23 #include "utils/s2n_safety.h"
24
25 /* Include to get access to the handshake state machine
26 * to verify we don't allow its messages post-handshake.
27 */
28 #include "tls/s2n_handshake_io.c"
29
30 #define KEY_UPDATE_MESSAGE_SIZE sizeof(uint8_t) + /* message id */ \
31 SIZEOF_UINT24 + /* message len */ \
32 sizeof(uint8_t) /* message */
33
34 int s2n_key_update_write(struct s2n_blob *out);
35
36 int main(int argc, char **argv)
37 {
38 BEGIN_TEST();
39
40 /* s2n_post_handshake_recv */
41 {
42 /* post_handshake_recv processes a key update requested message */
43 {
44 struct s2n_connection *conn = NULL;
45 EXPECT_NOT_NULL(conn = s2n_connection_new(S2N_SERVER));
46 conn->actual_protocol_version = S2N_TLS13;
47 conn->secure->cipher_suite = &s2n_tls13_aes_256_gcm_sha384;
48 EXPECT_FALSE(s2n_atomic_flag_test(&conn->key_update_pending));
49
50 /* Write key update requested to conn->in */
51 EXPECT_SUCCESS(s2n_stuffer_write_uint8(&conn->in, TLS_KEY_UPDATE));
52 EXPECT_SUCCESS(s2n_stuffer_write_uint24(&conn->in, S2N_KEY_UPDATE_LENGTH));
53 EXPECT_SUCCESS(s2n_stuffer_write_uint8(&conn->in, S2N_KEY_UPDATE_REQUESTED));
54
55 EXPECT_OK(s2n_post_handshake_recv(conn));
56 EXPECT_TRUE(s2n_atomic_flag_test(&conn->key_update_pending));
57
58 EXPECT_SUCCESS(s2n_connection_free(conn));
59 };
60
61 /* post_handshake_recv rejects an unknown post handshake message */
62 {
63 struct s2n_connection *conn = NULL;
64 EXPECT_NOT_NULL(conn = s2n_connection_new(S2N_SERVER));
65 conn->actual_protocol_version = S2N_TLS13;
66 conn->secure->cipher_suite = &s2n_tls13_aes_256_gcm_sha384;
67 EXPECT_FALSE(s2n_atomic_flag_test(&conn->key_update_pending));
68
69 /* Write key update requested to conn->in */
70 EXPECT_SUCCESS(s2n_stuffer_write_uint8(&conn->in, -1));
71 EXPECT_SUCCESS(s2n_stuffer_write_uint24(&conn->in, S2N_KEY_UPDATE_LENGTH));
72 EXPECT_SUCCESS(s2n_stuffer_write_uint8(&conn->in, S2N_KEY_UPDATE_REQUESTED));
73
74 EXPECT_ERROR_WITH_ERRNO(s2n_post_handshake_recv(conn), S2N_ERR_BAD_MESSAGE);
75 EXPECT_FALSE(s2n_atomic_flag_test(&conn->key_update_pending));
76
77 EXPECT_SUCCESS(s2n_connection_free(conn));
78 };
79
80 /* post_handshake_recv processes a malformed post handshake message */
81 {
82 struct s2n_connection *conn = NULL;
83 EXPECT_NOT_NULL(conn = s2n_connection_new(S2N_SERVER));
84 conn->actual_protocol_version = S2N_TLS13;
85 conn->secure->cipher_suite = &s2n_tls13_aes_256_gcm_sha384;
86 EXPECT_FALSE(s2n_atomic_flag_test(&conn->key_update_pending));
87
88 /* Write key update requested to conn->in */
89 EXPECT_SUCCESS(s2n_stuffer_write_uint8(&conn->in, TLS_KEY_UPDATE));
90 EXPECT_SUCCESS(s2n_stuffer_write_uint8(&conn->in, S2N_KEY_UPDATE_LENGTH));
91
92 EXPECT_ERROR(s2n_post_handshake_recv(conn));
93 EXPECT_FALSE(s2n_atomic_flag_test(&conn->key_update_pending));
94
95 EXPECT_SUCCESS(s2n_connection_free(conn));
96 };
97
98 /* Functional test: Multiple post handshake messages can be received in the same record */
99 {
100 struct s2n_connection *conn = s2n_connection_new(S2N_SERVER);
101 EXPECT_NOT_NULL(conn);
102 conn->actual_protocol_version = S2N_TLS13;
103 conn->secure->cipher_suite = &s2n_tls13_aes_256_gcm_sha384;
104 uint8_t num_key_updates = 3;
105
106 /* Write three key update messages in one record. We cannot call s2n_post_handshake_send
107 * multiple times here because s2n only sends one handshake message per record */
108 for (size_t i = 0; i < num_key_updates; i++) {
109 uint8_t data[KEY_UPDATE_MESSAGE_SIZE] = { 0 };
110 struct s2n_blob key_update_message = { 0 };
111 EXPECT_SUCCESS(s2n_blob_init(&key_update_message, data, sizeof(data)));
112 EXPECT_SUCCESS(s2n_key_update_write(&key_update_message));
113 EXPECT_SUCCESS(s2n_stuffer_write_bytes(&conn->in, key_update_message.data, key_update_message.size));
114 }
115
116 EXPECT_OK(s2n_post_handshake_recv(conn));
117
118 /* All three key update messages have been read */
119 EXPECT_EQUAL(s2n_stuffer_data_available(&conn->in), 0);
120
121 EXPECT_SUCCESS(s2n_connection_free(conn));
122 };
123
124 /* HELLO_REQUEST messages can be received post-handshake. */
125 {
126 struct s2n_connection *conn = s2n_connection_new(S2N_CLIENT);
127 EXPECT_NOT_NULL(conn);
128 conn->actual_protocol_version = S2N_TLS12;
129
130 EXPECT_SUCCESS(s2n_stuffer_write_uint8(&conn->in, TLS_HELLO_REQUEST));
131 EXPECT_SUCCESS(s2n_stuffer_write_uint24(&conn->in, 0));
132 EXPECT_OK(s2n_post_handshake_recv(conn));
133
134 EXPECT_SUCCESS(s2n_connection_free(conn));
135 };
136
137 /* No non-post handshake messages can be received.
138 * This means that no handshake message that appears in the handshake state machine
139 * should be allowed.
140 */
141 {
142 /* For TLS1.2 */
143 for (size_t i = 0; i < s2n_array_len(state_machine); i++) {
144 if (state_machine[i].record_type != TLS_HANDSHAKE) {
145 break;
146 }
147
148 struct s2n_connection *conn = NULL;
149 EXPECT_NOT_NULL(conn = s2n_connection_new(S2N_SERVER));
150 conn->actual_protocol_version = S2N_TLS13;
151
152 EXPECT_SUCCESS(s2n_stuffer_write_uint8(&conn->in, state_machine[i].message_type));
153 EXPECT_SUCCESS(s2n_stuffer_write_uint24(&conn->in, 0));
154 EXPECT_ERROR_WITH_ERRNO(s2n_post_handshake_recv(conn), S2N_ERR_BAD_MESSAGE);
155
156 EXPECT_SUCCESS(s2n_connection_free(conn));
157 }
158
159 /* For TLS1.3 */
160 for (size_t i = 0; i < s2n_array_len(tls13_state_machine); i++) {
161 if (tls13_state_machine[i].record_type != TLS_HANDSHAKE) {
162 break;
163 }
164
165 struct s2n_connection *conn = NULL;
166 EXPECT_NOT_NULL(conn = s2n_connection_new(S2N_SERVER));
167 conn->actual_protocol_version = S2N_TLS13;
168
169 EXPECT_SUCCESS(s2n_stuffer_write_uint8(&conn->in, tls13_state_machine[i].message_type));
170 EXPECT_SUCCESS(s2n_stuffer_write_uint24(&conn->in, 0));
171 EXPECT_ERROR_WITH_ERRNO(s2n_post_handshake_recv(conn), S2N_ERR_BAD_MESSAGE);
172
173 EXPECT_SUCCESS(s2n_connection_free(conn));
174 }
175 };
176 };
177
178 /* post_handshake_send */
179 {
180 /* Post handshake messages can be sent */
181 {
182 DEFER_CLEANUP(struct s2n_connection *conn = s2n_connection_new(S2N_CLIENT),
183 s2n_connection_ptr_free);
184 EXPECT_NOT_NULL(conn);
185 EXPECT_OK(s2n_connection_set_secrets(conn));
186
187 DEFER_CLEANUP(struct s2n_stuffer output = { 0 }, s2n_stuffer_free);
188 EXPECT_SUCCESS(s2n_stuffer_growable_alloc(&output, 0));
189 EXPECT_SUCCESS(s2n_connection_set_send_io_stuffer(&output, conn));
190
191 conn->actual_protocol_version = S2N_TLS13;
192 s2n_atomic_flag_set(&conn->key_update_pending);
193
194 s2n_blocked_status blocked = S2N_NOT_BLOCKED;
195 EXPECT_SUCCESS(s2n_post_handshake_send(conn, &blocked));
196
197 EXPECT_TRUE(s2n_stuffer_data_available(&output) > 0);
198 };
199
200 /* No messages sent if no post-handshake messages required */
201 {
202 DEFER_CLEANUP(struct s2n_connection *conn = s2n_connection_new(S2N_CLIENT),
203 s2n_connection_ptr_free);
204 EXPECT_NOT_NULL(conn);
205 EXPECT_OK(s2n_connection_set_secrets(conn));
206
207 conn->actual_protocol_version = S2N_TLS13;
208 s2n_atomic_flag_clear(&conn->key_update_pending);
209
210 s2n_blocked_status blocked = S2N_NOT_BLOCKED;
211 EXPECT_SUCCESS(s2n_post_handshake_send(conn, &blocked));
212
213 EXPECT_EQUAL(s2n_stuffer_data_available(&conn->out), 0);
214 };
215
216 /* No messages sent if <TLS1.3 */
217 {
218 DEFER_CLEANUP(struct s2n_connection *conn = s2n_connection_new(S2N_CLIENT),
219 s2n_connection_ptr_free);
220 EXPECT_NOT_NULL(conn);
221 EXPECT_OK(s2n_connection_set_secrets(conn));
222
223 conn->actual_protocol_version = S2N_TLS12;
224 s2n_atomic_flag_set(&conn->key_update_pending);
225
226 s2n_blocked_status blocked = S2N_NOT_BLOCKED;
227 EXPECT_SUCCESS(s2n_post_handshake_send(conn, &blocked));
228
229 EXPECT_TRUE(s2n_atomic_flag_test(&conn->key_update_pending));
230 EXPECT_EQUAL(s2n_stuffer_data_available(&conn->out), 0);
231 };
232 };
233
234 /* Errors while processing post-handshake messages close the connection */
235 {
236 struct s2n_connection *client_conn = s2n_connection_new(S2N_CLIENT);
237 EXPECT_NOT_NULL(client_conn);
238 EXPECT_SUCCESS(s2n_connection_set_cipher_preferences(client_conn, "default_tls13"));
239
240 struct s2n_connection *server_conn = s2n_connection_new(S2N_SERVER);
241 EXPECT_NOT_NULL(server_conn);
242 EXPECT_SUCCESS(s2n_connection_set_blinding(server_conn, S2N_SELF_SERVICE_BLINDING));
243 EXPECT_SUCCESS(s2n_connection_set_cipher_preferences(server_conn, "default_tls13"));
244
245 DEFER_CLEANUP(struct s2n_stuffer io_stuffer = { 0 }, s2n_stuffer_free);
246 EXPECT_SUCCESS(s2n_stuffer_growable_alloc(&io_stuffer, 0));
247 EXPECT_SUCCESS(s2n_connection_set_io_stuffers(&io_stuffer, &io_stuffer, client_conn));
248 EXPECT_SUCCESS(s2n_connection_set_io_stuffers(&io_stuffer, &io_stuffer, server_conn));
249
250 /* Send just the ClientHello */
251 s2n_blocked_status blocked = S2N_NOT_BLOCKED;
252 EXPECT_OK(s2n_negotiate_until_message(client_conn, &blocked, SERVER_HELLO));
253
254 /* Try to read the ClientHello as a post-handshake message */
255 uint8_t output_buffer[10] = { 0 };
256 EXPECT_FAILURE_WITH_ERRNO(s2n_recv(server_conn, output_buffer, sizeof(output_buffer), &blocked), S2N_ERR_BAD_MESSAGE);
257
258 /* Error closed connection */
259 EXPECT_TRUE(s2n_connection_check_io_status(server_conn, S2N_IO_CLOSED));
260
261 /* Error triggers blinding */
262 EXPECT_NOT_EQUAL(s2n_connection_get_delay(server_conn), 0);
263
264 EXPECT_SUCCESS(s2n_connection_free(client_conn));
265 EXPECT_SUCCESS(s2n_connection_free(server_conn));
266 };
267
268 END_TEST();
269 }