Debian Codegraph @

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