Debian Codegraph @

crt/s2n/tests/unit/s2n_tls13_key_schedule_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_tls13_key_schedule.h"
17
18 #include "s2n_test.h"
19 #include "testlib/s2n_testlib.h"
20 #include "tls/s2n_cipher_suites.h"
21
22 /* For access to secret handlers.
23 * We override them for mocking purposees. */
24 #include "tls/s2n_tls13_secrets.c"
25
26 #define NO_TRIGGER_MSG APPLICATION_DATA
27 #define TRAFFIC_SECRET_COUNT 6
28
29 static uint8_t empty_secret[S2N_TLS13_SECRET_MAX_LEN] = { 0 };
30
31 struct s2n_tls13_key_schedule_test_case {
32 s2n_mode conn_mode;
33 uint32_t handshake_type;
34 struct s2n_cipher_suite *cipher_suite;
35 bool is_early_data_requested;
36 };
37
38 struct s2n_test_secrets {
39 uint8_t bytes[TRAFFIC_SECRET_COUNT][S2N_TLS13_SECRET_MAX_LEN];
40 struct s2n_blob blobs[TRAFFIC_SECRET_COUNT];
41 };
42
43 static int s2n_test_secret_cb(void *context, struct s2n_connection *conn,
44 s2n_secret_type_t secret_type, uint8_t *secret_bytes, uint8_t secret_size)
45 {
46 struct s2n_test_secrets *secrets = (struct s2n_test_secrets *) context;
47 POSIX_ENSURE_REF(secrets);
48
49 POSIX_ENSURE_GTE(secret_type, 0);
50 POSIX_ENSURE_LT(secret_type, TRAFFIC_SECRET_COUNT);
51 POSIX_ENSURE_EQ(secrets->blobs[secret_type].size, 0);
52 POSIX_GUARD(s2n_blob_init(&secrets->blobs[secret_type],
53 secrets->bytes[secret_type], secret_size));
54 POSIX_CHECKED_MEMCPY(secrets->bytes[secret_type], secret_bytes, secret_size);
55 return S2N_SUCCESS;
56 }
57
58 static S2N_RESULT s2n_connection_verify_secrets(struct s2n_connection *conn, struct s2n_test_secrets *secrets,
59 bool expect_early_data_traffic_secret)
60 {
61 /* Test: All handshake and master traffic secrets calculated */
62 RESULT_ENSURE_EQ(conn->server, conn->secure);
63 RESULT_ENSURE_EQ(conn->client, conn->secure);
64 RESULT_ENSURE_GT(secrets->blobs[S2N_CLIENT_HANDSHAKE_TRAFFIC_SECRET].size, 0);
65 RESULT_ENSURE_GT(secrets->blobs[S2N_SERVER_HANDSHAKE_TRAFFIC_SECRET].size, 0);
66 RESULT_ENSURE_GT(secrets->blobs[S2N_CLIENT_APPLICATION_TRAFFIC_SECRET].size, 0);
67 RESULT_ENSURE_GT(secrets->blobs[S2N_SERVER_APPLICATION_TRAFFIC_SECRET].size, 0);
68
69 /* Test: Early traffic secrets calculated if early data requested */
70 if (expect_early_data_traffic_secret) {
71 RESULT_ENSURE_GT(secrets->blobs[S2N_CLIENT_EARLY_TRAFFIC_SECRET].size, 0);
72 } else {
73 RESULT_ENSURE_EQ(secrets->blobs[S2N_CLIENT_EARLY_TRAFFIC_SECRET].size, 0);
74 }
75
76 return S2N_RESULT_OK;
77 }
78
79 static S2N_RESULT s2n_mock_extract_method(struct s2n_connection *conn)
80 {
81 return S2N_RESULT_OK;
82 }
83
84 static S2N_RESULT s2n_mock_derive_method(struct s2n_connection *conn, struct s2n_blob *secret)
85 {
86 uint8_t size = 0;
87 s2n_hmac_algorithm hmac_alg = conn->secure->cipher_suite->prf_alg;
88 RESULT_GUARD_POSIX(s2n_hmac_digest_size(hmac_alg, &size));
89 RESULT_GUARD_POSIX(s2n_blob_init(secret, empty_secret, size));
90 return S2N_RESULT_OK;
91 }
92
93 int main(int argc, char **argv)
94 {
95 BEGIN_TEST();
96
97 const struct s2n_cipher_preferences *ciphers = &cipher_preferences_test_all_tls13;
98 const s2n_mode modes[] = { S2N_CLIENT, S2N_SERVER };
99 const bool early_data_req_states[] = { true, false };
100
101 /* Calculate a set of test cases */
102 struct s2n_tls13_key_schedule_test_case test_cases[2000] = { 0 };
103 size_t test_cases_count = 0;
104 struct s2n_connection handshake_test_conn = {
105 .actual_protocol_version = S2N_TLS13,
106 .handshake = { .message_number = 1, .state_machine = S2N_STATE_MACHINE_TLS13 },
107 };
108 for (uint32_t handshake_type = 0; handshake_type < S2N_HANDSHAKES_COUNT; handshake_type++) {
109 handshake_test_conn.handshake.handshake_type = handshake_type;
110 for (size_t req_i = 0; req_i < s2n_array_len(early_data_req_states); req_i++) {
111 for (size_t cipher_i = 0; cipher_i < ciphers->count; cipher_i++) {
112 for (size_t mode_i = 0; mode_i < s2n_array_len(modes); mode_i++) {
113 if (s2n_conn_get_current_message_type(&handshake_test_conn) == CLIENT_HELLO) {
114 /* Skip empty, all-CLIENT_HELLO handshakes */
115 continue;
116 }
117 if (WITH_EARLY_DATA(&handshake_test_conn) && !early_data_req_states[req_i]) {
118 /* Skip handshakes with inconsistent early data states */
119 continue;
120 }
121 if (!IS_NEGOTIATED(&handshake_test_conn)) {
122 /* Skip initial / incomplete handshakes */
123 continue;
124 }
125 if (!ciphers->suites[cipher_i]->available) {
126 /* Skip unavailable ciphers */
127 continue;
128 }
129 test_cases[test_cases_count] = (struct s2n_tls13_key_schedule_test_case){
130 .conn_mode = modes[mode_i],
131 .handshake_type = handshake_type,
132 .cipher_suite = ciphers->suites[cipher_i],
133 .is_early_data_requested = early_data_req_states[req_i],
134 };
135 test_cases_count++;
136 }
137 }
138 }
139 }
140 EXPECT_TRUE(test_cases_count > 0);
141
142 /* Test s2n_tls13_key_schedule_update */
143 {
144 /* Safety */
145 {
146 EXPECT_ERROR_WITH_ERRNO(s2n_tls13_key_schedule_update(NULL), S2N_ERR_NULL);
147
148 /* Minimal client connection that triggers an update */
149 struct s2n_connection empty_client_conn = {
150 .mode = S2N_CLIENT,
151 .client_protocol_version = S2N_TLS13,
152 .early_data_state = S2N_EARLY_DATA_REQUESTED
153 };
154 EXPECT_ERROR_WITH_ERRNO(s2n_tls13_key_schedule_update(&empty_client_conn), S2N_ERR_NULL);
155 empty_client_conn.client_protocol_version = S2N_TLS12;
156 EXPECT_OK(s2n_tls13_key_schedule_update(&empty_client_conn));
157
158 /* Minimal server connection that triggers an update */
159 struct s2n_connection empty_server_conn = {
160 .mode = S2N_SERVER,
161 .actual_protocol_version = S2N_TLS13,
162 .handshake = { .message_number = 1 }
163 };
164 EXPECT_ERROR_WITH_ERRNO(s2n_tls13_key_schedule_update(&empty_server_conn), S2N_ERR_NULL);
165 empty_server_conn.actual_protocol_version = S2N_TLS12;
166 EXPECT_OK(s2n_tls13_key_schedule_update(&empty_server_conn));
167 };
168
169 /* Test key schedule ordering */
170 {
171 /* Mock derive and extract methods */
172 uint8_t saved_derive_methods[sizeof(derive_methods)] = { 0 };
173 EXPECT_MEMCPY_SUCCESS(saved_derive_methods, derive_methods, sizeof(derive_methods));
174 for (size_t i = 0; i < s2n_array_len(derive_methods); i++) {
175 derive_methods[i][S2N_CLIENT] = &s2n_mock_derive_method;
176 derive_methods[i][S2N_SERVER] = &s2n_mock_derive_method;
177 }
178 uint8_t saved_extract_methods[sizeof(extract_methods)] = { 0 };
179 EXPECT_MEMCPY_SUCCESS(saved_extract_methods, extract_methods, sizeof(extract_methods));
180 for (size_t i = 0; i < s2n_array_len(extract_methods); i++) {
181 extract_methods[i] = &s2n_mock_extract_method;
182 }
183
184 for (size_t i = 0; i < test_cases_count; i++) {
185 DEFER_CLEANUP(struct s2n_connection *conn = s2n_connection_new(test_cases[i].conn_mode),
186 s2n_connection_ptr_free);
187 conn->actual_protocol_version = S2N_TLS13;
188 EXPECT_OK(s2n_conn_choose_state_machine(conn, S2N_TLS13));
189
190 conn->handshake.handshake_type = test_cases[i].handshake_type;
191 conn->secure->cipher_suite = test_cases[i].cipher_suite;
192 if (test_cases[i].is_early_data_requested) {
193 conn->early_data_state = S2N_EARLY_DATA_REQUESTED;
194 }
195
196 struct s2n_test_secrets secrets = { 0 };
197 EXPECT_SUCCESS(s2n_connection_set_secret_callback(conn,
198 s2n_test_secret_cb, (void *) &secrets));
199
200 /* Perform the handshake */
201 while (s2n_conn_get_current_message_type(conn) != APPLICATION_DATA) {
202 /*
203 * To avoid calculating the early traffic secret again when
204 * we receive the second ClientHello, we need to include
205 * the logic where a retry request rejects early data.
206 */
207 if (s2n_conn_get_current_message_type(conn) == HELLO_RETRY_MSG) {
208 conn->early_data_state = S2N_EARLY_DATA_REJECTED;
209 EXPECT_OK(s2n_tls13_key_schedule_reset(conn));
210 }
211
212 /*
213 * We know what secrets every message should be encrypted with.
214 * Verify those secrets are available in time for each message.
215 */
216 switch (s2n_conn_get_current_message_type(conn)) {
217 case CLIENT_HELLO:
218 /* Expect not encrypted */
219 EXPECT_EQUAL(conn->client, conn->initial);
220 break;
221 case HELLO_RETRY_MSG:
222 case SERVER_HELLO:
223 /* Expect not encrypted */
224 EXPECT_EQUAL(conn->server, conn->initial);
225 break;
226 case END_OF_EARLY_DATA:
227 /* Expect encrypted */
228 EXPECT_EQUAL(conn->client, conn->secure);
229 /* Expect correct secret available */
230 EXPECT_TRUE(secrets.blobs[S2N_CLIENT_EARLY_TRAFFIC_SECRET].size > 0);
231 break;
232 case ENCRYPTED_EXTENSIONS:
233 case SERVER_CERT:
234 case SERVER_CERT_VERIFY:
235 case SERVER_FINISHED:
236 case SERVER_CERT_REQ:
237 /* Expect encrypted */
238 EXPECT_EQUAL(conn->server, conn->secure);
239 /* Expect correct secret available */
240 EXPECT_TRUE(secrets.blobs[S2N_SERVER_HANDSHAKE_TRAFFIC_SECRET].size > 0);
241 break;
242 case CLIENT_CERT:
243 case CLIENT_CERT_VERIFY:
244 case CLIENT_FINISHED:
245 /* Expect encrypted */
246 EXPECT_EQUAL(conn->client, conn->secure);
247 /* Expect correct secret available */
248 EXPECT_TRUE(secrets.blobs[S2N_CLIENT_HANDSHAKE_TRAFFIC_SECRET].size > 0);
249 break;
250 case APPLICATION_DATA:
251 /* Expect encrypted */
252 EXPECT_EQUAL(conn->client, conn->secure);
253 EXPECT_EQUAL(conn->server, conn->secure);
254 /* Expect correct secrets available */
255 EXPECT_TRUE(secrets.blobs[S2N_CLIENT_APPLICATION_TRAFFIC_SECRET].size > 0);
256 EXPECT_TRUE(secrets.blobs[S2N_SERVER_APPLICATION_TRAFFIC_SECRET].size > 0);
257 break;
258 case SERVER_CHANGE_CIPHER_SPEC:
259 case CLIENT_CHANGE_CIPHER_SPEC:
260 /*
261 * Not relevant to TLS1.3 key schedule.
262 * We manually disable encryption when sending.
263 */
264 break;
265 case CLIENT_KEY:
266 case SERVER_KEY:
267 case SERVER_NEW_SESSION_TICKET:
268 case SERVER_CERT_STATUS:
269 case SERVER_HELLO_DONE:
270 case CLIENT_NPN:
271 FAIL_MSG("Unexpected TLS1.2 message");
272 break;
273 }
274
275 EXPECT_OK(s2n_tls13_secrets_update(conn));
276 EXPECT_OK(s2n_tls13_key_schedule_update(conn));
277 conn->handshake.message_number++;
278 }
279
280 EXPECT_OK(s2n_connection_verify_secrets(conn, &secrets, test_cases[i].is_early_data_requested));
281 }
282
283 /* Restore derive and extract methods */
284 EXPECT_MEMCPY_SUCCESS(derive_methods, saved_derive_methods, sizeof(saved_derive_methods));
285 EXPECT_MEMCPY_SUCCESS(extract_methods, saved_extract_methods, sizeof(saved_extract_methods));
286 };
287 };
288
289 END_TEST();
290 }