Debian Codegraph @

crt/s2n/tests/unit/s2n_key_update_threads_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 <pthread.h>
17 #include <sys/types.h>
18 #include <sys/wait.h>
19
20 #include "crypto/s2n_sequence.h"
21 #include "s2n_test.h"
22 #include "testlib/s2n_examples.h"
23 #include "testlib/s2n_testlib.h"
24 #include "utils/s2n_random.h"
25
26 #define S2N_TEST_BUFFER_SIZE 1000
27 #define S2N_TEST_ENCRYPTION_LIMIT 3
28 #define S2N_TEST_KEY_UPDATE_COUNT 25
29 #define S2N_TEST_RECORD_COUNT (S2N_TEST_ENCRYPTION_LIMIT * S2N_TEST_KEY_UPDATE_COUNT)
30 #define S2N_TEST_BYTES_TO_SEND (S2N_DEFAULT_FRAGMENT_LENGTH * S2N_TEST_RECORD_COUNT)
31
32 #define S2N_CIPHER_SUITE_WITH_LIMIT(name, source, limit) \
33 struct s2n_cipher_suite name = *(source); \
34 struct s2n_record_algorithm _##name##_record_alg = *name.record_alg; \
35 _##name##_record_alg.encryption_limit = limit; \
36 name.record_alg = &_##name##_record_alg;
37
38 S2N_RESULT s2n_set_key_update_request_for_testing(s2n_peer_key_update request);
39
40 static void *s2n_send_random_data(void *arg)
41 {
42 struct s2n_connection *conn = (struct s2n_connection *) arg;
43
44 uint8_t buffer[S2N_TEST_BUFFER_SIZE] = "hello world";
45
46 size_t bytes_to_send = S2N_TEST_BYTES_TO_SEND;
47 s2n_blocked_status blocked = S2N_NOT_BLOCKED;
48 while (bytes_to_send) {
49 int r = s2n_send(conn, buffer, S2N_MIN(sizeof(buffer), bytes_to_send), &blocked);
50 if (r >= 0) {
51 bytes_to_send -= r;
52 } else if (s2n_error_get_type(s2n_errno) != S2N_ERR_T_BLOCKED) {
53 fprintf(stderr, "Send error: %s. %s\n", s2n_strerror(s2n_errno, NULL), s2n_strerror_debug(s2n_errno, NULL));
54 return NULL;
55 }
56 }
57 return conn;
58 }
59
60 static void *s2n_recv_random_data(void *arg)
61 {
62 struct s2n_connection *conn = (struct s2n_connection *) arg;
63
64 uint8_t buffer[S2N_TEST_BUFFER_SIZE] = { 0 };
65
66 size_t bytes_to_read = S2N_TEST_BYTES_TO_SEND;
67 s2n_blocked_status blocked = S2N_NOT_BLOCKED;
68 while (bytes_to_read) {
69 int r = s2n_recv(conn, buffer, S2N_MIN(sizeof(buffer), bytes_to_read), &blocked);
70 if (r >= 0) {
71 bytes_to_read -= r;
72 } else if (s2n_error_get_type(s2n_errno) != S2N_ERR_T_BLOCKED) {
73 fprintf(stderr, "Recv error: %s. %s\n", s2n_strerror(s2n_errno, NULL), s2n_strerror_debug(s2n_errno, NULL));
74 return NULL;
75 }
76 }
77 return conn;
78 }
79
80 static S2N_RESULT s2n_send_and_recv_random_data(struct s2n_connection *conn)
81 {
82 /*
83 * This test is intended to find concurrency issues when sending and receiving
84 * KeyUpdates, so we need to run the reader and writer in separate threads.
85 */
86
87 pthread_t reader = 0;
88 RESULT_ENSURE_EQ(pthread_create(&reader, NULL, s2n_recv_random_data, (void *) conn), 0);
89
90 pthread_t writer = 0;
91 RESULT_ENSURE_EQ(pthread_create(&writer, NULL, s2n_send_random_data, (void *) conn), 0);
92
93 void *reader_return = NULL;
94 RESULT_ENSURE_EQ(pthread_join(reader, &reader_return), 0);
95 RESULT_ENSURE_REF(reader_return);
96
97 void *writer_return = NULL;
98 RESULT_ENSURE_EQ(pthread_join(writer, &writer_return), 0);
99 RESULT_ENSURE_REF(writer_return);
100
101 RESULT_ENSURE_GT(conn->wire_bytes_out, S2N_TEST_BYTES_TO_SEND);
102 RESULT_ENSURE_GT(conn->wire_bytes_in, S2N_TEST_BYTES_TO_SEND);
103 return S2N_RESULT_OK;
104 }
105
106 static S2N_RESULT s2n_sanity_check_key_updates_sent(struct s2n_connection *conn)
107 {
108 struct s2n_blob seq_num_blob = { 0 };
109 RESULT_GUARD(s2n_connection_get_sequence_number(conn, conn->mode, &seq_num_blob));
110
111 uint64_t seq_num = 0;
112 RESULT_GUARD_POSIX(s2n_sequence_number_to_uint64(&seq_num_blob, &seq_num));
113 RESULT_ENSURE_LTE(seq_num, conn->secure->cipher_suite->record_alg->encryption_limit);
114
115 /* s2n-tls doesn't keep a running count of KeyUpdates, so to sanity check that
116 * at least one KeyUpdate occurred we have to rely on some math.
117 *
118 * wire_bytes_out represents the total bytes sent, and should therefore be
119 * less than or equal to (number of records sent) * (maximum size of a record).
120 *
121 * (maximum size of a record) can be calculated based on max_outgoing_fragment_length.
122 * We will call it max_record_size.
123 *
124 * (number of records sent) is seq_num, if no KeyUpdates were sent. seq_num
125 * starts at 0, is incremented by one for every record, and is reset to 0 by
126 * a KeyUpdate. So if no KeyUpdate occurs, seq_num represents the total number
127 * of records sent.
128 *
129 * If seq_num represents the total number of records sent, then wire_bytes_out
130 * must be less than or equal to (seq_num) * (max_record_size).
131 * If wire_bytes_out is instead greater than (seq_num) * (max_record_size),
132 * then more records were sent than seq_num accounts for. That means that seq_num
133 * must have been reset, which means that at least one KeyUpdate was sent.
134 */
135 size_t max_record_size = S2N_TLS13_MAX_RECORD_LEN_FOR(conn->max_outgoing_fragment_length);
136 RESULT_ENSURE_GT(conn->wire_bytes_out, max_record_size * seq_num);
137
138 return S2N_RESULT_OK;
139 }
140
141 static S2N_RESULT s2n_test_encryption_limits(struct s2n_connection *conn)
142 {
143 RESULT_GUARD_POSIX(s2n_example_negotiate(conn));
144
145 struct s2n_cipher_suite *original_suite = conn->secure->cipher_suite;
146 S2N_CIPHER_SUITE_WITH_LIMIT(key_limit_suite, original_suite, S2N_TEST_ENCRYPTION_LIMIT);
147
148 conn->secure->cipher_suite = &key_limit_suite;
149
150 RESULT_GUARD(s2n_send_and_recv_random_data(conn));
151 RESULT_GUARD(s2n_sanity_check_key_updates_sent(conn));
152
153 conn->secure->cipher_suite = original_suite;
154 return S2N_RESULT_OK;
155 }
156
157 static S2N_RESULT s2n_test_peer_requests(struct s2n_connection *conn)
158 {
159 RESULT_GUARD_POSIX(s2n_example_negotiate(conn));
160
161 struct s2n_cipher_suite *original_suite = conn->secure->cipher_suite;
162 S2N_CIPHER_SUITE_WITH_LIMIT(key_limit_suite, original_suite, S2N_TEST_ENCRYPTION_LIMIT);
163
164 conn->secure->cipher_suite = &key_limit_suite;
165 if (conn->mode == S2N_CLIENT) {
166 RESULT_GUARD(s2n_set_key_update_request_for_testing(S2N_KEY_UPDATE_REQUESTED));
167 }
168
169 RESULT_GUARD(s2n_send_and_recv_random_data(conn));
170 RESULT_GUARD(s2n_sanity_check_key_updates_sent(conn));
171
172 conn->secure->cipher_suite = original_suite;
173 return S2N_RESULT_OK;
174 }
175
176 typedef S2N_RESULT (*s2n_test_scenario)(struct s2n_connection *conn);
177 static S2N_RESULT s2n_run_self_talk_test(s2n_test_scenario scenario_fn)
178 {
179 struct s2n_cert_chain_and_key *chain_and_key = NULL;
180 RESULT_GUARD_POSIX(s2n_test_cert_chain_and_key_new(&chain_and_key,
181 S2N_DEFAULT_TEST_CERT_CHAIN, S2N_DEFAULT_TEST_PRIVATE_KEY));
182
183 struct s2n_config *config = s2n_config_new();
184 RESULT_ENSURE_REF(config);
185 RESULT_GUARD_POSIX(s2n_config_set_unsafe_for_testing(config));
186 RESULT_GUARD_POSIX(s2n_config_set_cipher_preferences(config, "default_tls13"));
187 RESULT_GUARD_POSIX(s2n_config_add_cert_chain_and_key_to_store(config, chain_and_key));
188
189 DEFER_CLEANUP(struct s2n_test_io_pair io_pair = { 0 }, s2n_io_pair_close);
190 RESULT_GUARD_POSIX(s2n_io_pair_init_non_blocking(&io_pair));
191
192 pid_t client_pid = fork();
193 if (client_pid == 0) {
194 /* Suppress stdout.
195 * This only affects the new client process.
196 */
197 fclose(stdout);
198
199 struct s2n_connection *client = s2n_connection_new(S2N_CLIENT);
200 EXPECT_NOT_NULL(client);
201 EXPECT_SUCCESS(s2n_connection_set_config(client, config));
202
203 EXPECT_SUCCESS(s2n_io_pair_close_one_end(&io_pair, S2N_SERVER));
204 EXPECT_SUCCESS(s2n_connection_set_io_pair(client, &io_pair));
205
206 EXPECT_OK(scenario_fn(client));
207
208 EXPECT_SUCCESS(s2n_connection_free(client));
209 EXPECT_SUCCESS(s2n_cert_chain_and_key_free(chain_and_key));
210 EXPECT_SUCCESS(s2n_config_free(config));
211 EXPECT_SUCCESS(s2n_io_pair_close_one_end(&io_pair, S2N_CLIENT));
212
213 exit(EXIT_SUCCESS);
214 }
215
216 pid_t server_pid = fork();
217 if (server_pid == 0) {
218 /* Suppress stdouts.
219 * This only affects the new server process.
220 */
221 fclose(stdout);
222
223 struct s2n_connection *server = s2n_connection_new(S2N_SERVER);
224 EXPECT_NOT_NULL(server);
225 EXPECT_SUCCESS(s2n_connection_set_config(server, config));
226
227 EXPECT_SUCCESS(s2n_io_pair_close_one_end(&io_pair, S2N_CLIENT));
228 EXPECT_SUCCESS(s2n_connection_set_io_pair(server, &io_pair));
229
230 EXPECT_OK(scenario_fn(server));
231
232 EXPECT_SUCCESS(s2n_connection_free(server));
233 EXPECT_SUCCESS(s2n_cert_chain_and_key_free(chain_and_key));
234 EXPECT_SUCCESS(s2n_config_free(config));
235 EXPECT_SUCCESS(s2n_io_pair_close_one_end(&io_pair, S2N_SERVER));
236
237 exit(EXIT_SUCCESS);
238 }
239
240 int status = 0;
241 RESULT_ENSURE_EQ(waitpid(client_pid, &status, 0), client_pid);
242 RESULT_ENSURE_EQ(status, EXIT_SUCCESS);
243 RESULT_ENSURE_EQ(waitpid(server_pid, &status, 0), server_pid);
244 RESULT_ENSURE_EQ(status, EXIT_SUCCESS);
245
246 EXPECT_SUCCESS(s2n_cert_chain_and_key_free(chain_and_key));
247 EXPECT_SUCCESS(s2n_config_free(config));
248
249 return S2N_RESULT_OK;
250 }
251
252 int main(int argc, char **argv)
253 {
254 BEGIN_TEST();
255
256 /* KeyUpdate requires TLS1.3 */
257 if (!s2n_is_tls13_fully_supported()) {
258 END_TEST();
259 }
260
261 /* We're going to fork, so flush the initial test output first */
262 EXPECT_EQUAL(fflush(stdout), 0);
263
264 EXPECT_OK(s2n_run_self_talk_test(s2n_test_encryption_limits));
265 EXPECT_OK(s2n_run_self_talk_test(s2n_test_peer_requests));
266
267 END_TEST();
268 }