Debian Codegraph @

crt/s2n/tests/unit/s2n_self_talk_tls13_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 <stdint.h>
17
18 #include "api/s2n.h"
19 #include "s2n_test.h"
20 #include "testlib/s2n_testlib.h"
21 #include "tls/s2n_connection.h"
22 #include "tls/s2n_tls13.h"
23
24 static uint64_t s2n_test_mock_time = 0;
25
26 static int s2n_test_mock_clock(void *in, uint64_t *out)
27 {
28 *out = s2n_test_mock_time;
29 return 0;
30 }
31
32 int main(int argc, char **argv)
33 {
34 s2n_blocked_status blocked;
35
36 BEGIN_TEST();
37
38 /* Set up server config */
39 struct s2n_cert_chain_and_key *chain_and_key = NULL;
40 EXPECT_SUCCESS(s2n_test_cert_chain_and_key_new(&chain_and_key,
41 S2N_DEFAULT_ECDSA_TEST_CERT_CHAIN, S2N_DEFAULT_ECDSA_TEST_PRIVATE_KEY));
42
43 DEFER_CLEANUP(struct s2n_config *server_config = s2n_config_new(), s2n_config_ptr_free);
44 EXPECT_NOT_NULL(server_config);
45 EXPECT_SUCCESS(s2n_config_set_cipher_preferences(server_config, "default_tls13"));
46 EXPECT_SUCCESS(s2n_config_add_cert_chain_and_key_to_store(server_config, chain_and_key));
47 EXPECT_SUCCESS(s2n_config_set_monotonic_clock(server_config, s2n_test_mock_clock, NULL));
48
49 /* Set up client config */
50 DEFER_CLEANUP(struct s2n_config *client_config = s2n_config_new(), s2n_config_ptr_free);
51 EXPECT_NOT_NULL(client_config);
52 EXPECT_SUCCESS(s2n_config_disable_x509_verification(client_config));
53 EXPECT_SUCCESS(s2n_config_set_cipher_preferences(client_config, "default_tls13"));
54 EXPECT_SUCCESS(s2n_config_set_monotonic_clock(client_config, s2n_test_mock_clock, NULL));
55
56 /* Set up server connection */
57 DEFER_CLEANUP(struct s2n_connection *server_conn = s2n_connection_new(S2N_SERVER), s2n_connection_ptr_free);
58 EXPECT_NOT_NULL(server_conn);
59 EXPECT_SUCCESS(s2n_connection_set_config(server_conn, server_config));
60 EXPECT_SUCCESS(s2n_connection_prefer_throughput(server_conn));
61
62 /* Set up client connection */
63 DEFER_CLEANUP(struct s2n_connection *client_conn = s2n_connection_new(S2N_CLIENT), s2n_connection_ptr_free);
64 EXPECT_NOT_NULL(client_conn);
65 EXPECT_SUCCESS(s2n_connection_set_config(client_conn, client_config));
66 EXPECT_SUCCESS(s2n_connection_prefer_throughput(client_conn));
67
68 /* Use in-memory IO stuffer pair */
69 DEFER_CLEANUP(struct s2n_test_io_stuffer_pair io_pair = { 0 }, s2n_io_stuffer_pair_free);
70 EXPECT_OK(s2n_io_stuffer_pair_init(&io_pair));
71 EXPECT_OK(s2n_connections_set_io_stuffer_pair(client_conn, server_conn, &io_pair));
72
73 /* Negotiate the handshake */
74 EXPECT_SUCCESS(s2n_negotiate_test_server_and_client(server_conn, client_conn));
75 EXPECT_NOT_NULL(s2n_connection_get_client_hello(server_conn));
76 EXPECT_EQUAL(server_conn->actual_protocol_version, s2n_get_highest_fully_supported_tls_version());
77 EXPECT_EQUAL(client_conn->actual_protocol_version, s2n_get_highest_fully_supported_tls_version());
78
79 /* Release handshake buffers on client */
80 EXPECT_SUCCESS(s2n_connection_free_handshake(client_conn));
81
82 /* Client sends data in increasing chunks, server reads and verifies.
83 * With dynamic record threshold enabled, the first 0x7fff bytes use
84 * small TLS records (fitting in one TCP segment) for lower latency.
85 */
86 char send_buffer[0xffff];
87 char recv_buffer[0xffff];
88
89 uint16_t timeout = 1;
90 EXPECT_SUCCESS(s2n_connection_set_dynamic_record_threshold(client_conn, 0x7fff, timeout));
91
92 int i = 0;
93 for (i = 1; i < 0xffff - 100; i += 100) {
94 for (int j = 0; j < i; j++) {
95 send_buffer[j] = 33;
96 }
97 EXPECT_EQUAL(s2n_send(client_conn, send_buffer, i, &blocked), i);
98
99 /* Server reads the data */
100 char *ptr = recv_buffer;
101 int size = i;
102 do {
103 int bytes_read = 0;
104 EXPECT_SUCCESS(bytes_read = s2n_recv(server_conn, ptr, size, &blocked));
105 size -= bytes_read;
106 ptr += bytes_read;
107 } while (size);
108
109 for (int j = 0; j < i; j++) {
110 EXPECT_EQUAL(recv_buffer[j], 33);
111 }
112
113 /* Reset the IO stuffer cursors after all data is consumed,
114 * preventing unbounded growth that causes expensive reallocs.
115 */
116 EXPECT_EQUAL(s2n_stuffer_data_available(&io_pair.server_in), 0);
117 EXPECT_SUCCESS(s2n_stuffer_rewrite(&io_pair.server_in));
118
119 /* Release the buffers here to validate we can continue IO after */
120 EXPECT_SUCCESS(s2n_connection_release_buffers(server_conn));
121 }
122
123 /* Fill the buffer for the final send */
124 for (int j = 0; j < i; j++) {
125 send_buffer[j] = 33;
126 }
127
128 /* Release buffers on client to validate we can continue IO after */
129 EXPECT_SUCCESS(s2n_connection_release_buffers(client_conn));
130
131 /* Simulate timeout for dynamic record threshold by advancing mock clock.
132 * After the timeout period, active_application_bytes_consumed is reset
133 * to 0 before writing data, so its value should equal bytes written
134 * after the send.
135 */
136 s2n_test_mock_time += (uint64_t) timeout * 1000000000 + 1;
137
138 ssize_t bytes_written = s2n_send(client_conn, send_buffer, i, &blocked);
139 EXPECT_TRUE(bytes_written > 0);
140 EXPECT_EQUAL((uint64_t) bytes_written, client_conn->active_application_bytes_consumed);
141
142 /* Server reads the final chunk */
143 {
144 char *ptr = recv_buffer;
145 int size = i;
146 do {
147 int bytes_read = 0;
148 EXPECT_SUCCESS(bytes_read = s2n_recv(server_conn, ptr, size, &blocked));
149 size -= bytes_read;
150 ptr += bytes_read;
151 } while (size);
152 }
153
154 /* Graceful shutdown (both sides) */
155 EXPECT_SUCCESS(s2n_shutdown_test_server_and_client(server_conn, client_conn));
156
157 EXPECT_SUCCESS(s2n_cert_chain_and_key_free(chain_and_key));
158
159 s2n_disable_tls13_in_test();
160
161 END_TEST();
162 }