Debian Codegraph @

crt/s2n/tests/unit/s2n_tls13_secrets_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 #include "tls/s2n_tls13_secrets.h"
16
17 #include "crypto/s2n_ecc_evp.h"
18 #include "s2n_test.h"
19 #include "testlib/s2n_testlib.h"
20
21 S2N_RESULT s2n_tls13_extract_secret(struct s2n_connection *conn, s2n_extract_secret_type_t secret_type);
22 S2N_RESULT s2n_tls13_derive_secret(struct s2n_connection *conn, s2n_extract_secret_type_t secret_type,
23 s2n_mode mode, struct s2n_blob *secret);
24
25 static S2N_RESULT s2n_set_test_key_shares(struct s2n_connection *conn, const struct s2n_ecc_named_curve *curve)
26 {
27 conn->kex_params.server_ecc_evp_params.negotiated_curve = curve;
28 RESULT_GUARD_POSIX(s2n_ecc_evp_generate_ephemeral_key(&conn->kex_params.server_ecc_evp_params));
29
30 conn->kex_params.client_ecc_evp_params.negotiated_curve = curve;
31 RESULT_GUARD_POSIX(s2n_ecc_evp_generate_ephemeral_key(&conn->kex_params.client_ecc_evp_params));
32
33 return S2N_RESULT_OK;
34 }
35
36 struct s2n_tls13_secrets_test_case {
37 s2n_extract_secret_type_t curr_secret_type;
38 s2n_extract_secret_type_t next_secret_type;
39 s2n_mode secret_mode;
40 s2n_mode conn_mode;
41 struct s2n_cipher_suite *cipher_suite;
42 const struct s2n_ecc_named_curve *curve;
43 };
44
45 int main(int argc, char **argv)
46 {
47 BEGIN_TEST();
48
49 const uint8_t empty_secret[S2N_TLS13_SECRET_MAX_LEN] = { 0 };
50
51 const struct s2n_cipher_preferences *ciphers = &cipher_preferences_test_all_tls13;
52 const struct s2n_ecc_preferences *curves = &s2n_ecc_preferences_test_all;
53 const s2n_mode modes[] = { S2N_CLIENT, S2N_SERVER };
54
55 struct s2n_blob test_secret = { 0 };
56 uint8_t test_secret_bytes[S2N_TLS13_SECRET_MAX_LEN] = "hello world";
57 EXPECT_SUCCESS(s2n_blob_init(&test_secret, test_secret_bytes, sizeof(test_secret_bytes)));
58
59 struct s2n_tls13_secrets_test_case test_cases[1000] = { 0 };
60 size_t test_cases_count = 0;
61 for (s2n_extract_secret_type_t next_type = S2N_EARLY_SECRET; next_type <= S2N_MASTER_SECRET; next_type++) {
62 for (s2n_extract_secret_type_t curr_type = S2N_NONE_SECRET; curr_type <= S2N_MASTER_SECRET; curr_type++) {
63 for (size_t cipher_i = 0; cipher_i < ciphers->count; cipher_i++) {
64 for (size_t curve_i = 0; curve_i < curves->count; curve_i++) {
65 for (size_t m1_i = 0; m1_i < s2n_array_len(modes); m1_i++) {
66 for (size_t m2_i = 0; m2_i < s2n_array_len(modes); m2_i++) {
67 if (curr_type > next_type) {
68 /* Secret schedule MUST be evaluated in order */
69 continue;
70 }
71 test_cases[test_cases_count] = (struct s2n_tls13_secrets_test_case){
72 .curr_secret_type = curr_type,
73 .next_secret_type = next_type,
74 .secret_mode = modes[m1_i],
75 .conn_mode = modes[m2_i],
76 .cipher_suite = ciphers->suites[cipher_i],
77 .curve = curves->ecc_curves[curve_i],
78 };
79 test_cases_count++;
80 }
81 }
82 }
83 }
84 }
85 }
86 EXPECT_TRUE(test_cases_count > 0);
87
88 /* Test: s2n_tls13_extract_secret */
89 {
90 /* Safety */
91 {
92 struct s2n_connection empty_conn = { 0 };
93 DEFER_CLEANUP(struct s2n_connection *conn = s2n_connection_new(S2N_SERVER),
94 s2n_connection_ptr_free);
95
96 EXPECT_ERROR_WITH_ERRNO(s2n_tls13_extract_secret(NULL, S2N_EARLY_SECRET), S2N_ERR_NULL);
97 EXPECT_ERROR_WITH_ERRNO(s2n_tls13_extract_secret(&empty_conn, S2N_EARLY_SECRET), S2N_ERR_NULL);
98 EXPECT_ERROR_WITH_ERRNO(s2n_tls13_extract_secret(conn, -1), S2N_ERR_SAFETY);
99 EXPECT_ERROR_WITH_ERRNO(s2n_tls13_extract_secret(conn, 255), S2N_ERR_SAFETY);
100 EXPECT_ERROR_WITH_ERRNO(s2n_tls13_extract_secret(conn, (S2N_MASTER_SECRET + 1)), S2N_ERR_SAFETY);
101 };
102
103 /* No-op if secret already exists */
104 {
105 DEFER_CLEANUP(struct s2n_connection *conn = s2n_connection_new(S2N_SERVER),
106 s2n_connection_ptr_free);
107 conn->secure->cipher_suite = &s2n_tls13_aes_256_gcm_sha384;
108 conn->secrets.extract_secret_type = S2N_EARLY_SECRET;
109
110 EXPECT_OK(s2n_tls13_extract_secret(conn, S2N_EARLY_SECRET));
111 EXPECT_EQUAL(conn->secrets.extract_secret_type, S2N_EARLY_SECRET);
112 EXPECT_BYTEARRAY_EQUAL(conn->secrets.version.tls13.extract_secret, empty_secret, sizeof(empty_secret));
113 };
114
115 /* Generate all secrets sequentially */
116 {
117 DEFER_CLEANUP(struct s2n_connection *conn = s2n_connection_new(S2N_SERVER),
118 s2n_connection_ptr_free);
119 conn->secure->cipher_suite = &s2n_tls13_aes_128_gcm_sha256;
120 conn->secrets.extract_secret_type = S2N_NONE_SECRET;
121
122 EXPECT_OK(s2n_tls13_extract_secret(conn, S2N_EARLY_SECRET));
123 EXPECT_EQUAL(conn->secrets.extract_secret_type, S2N_EARLY_SECRET);
124 EXPECT_BYTEARRAY_NOT_EQUAL(conn->secrets.version.tls13.extract_secret, empty_secret, sizeof(empty_secret));
125
126 EXPECT_OK(s2n_set_test_key_shares(conn, &s2n_ecc_curve_secp256r1));
127 EXPECT_OK(s2n_tls13_extract_secret(conn, S2N_HANDSHAKE_SECRET));
128 EXPECT_EQUAL(conn->secrets.extract_secret_type, S2N_HANDSHAKE_SECRET);
129 EXPECT_BYTEARRAY_NOT_EQUAL(conn->secrets.version.tls13.extract_secret, empty_secret, sizeof(empty_secret));
130
131 EXPECT_OK(s2n_tls13_extract_secret(conn, S2N_MASTER_SECRET));
132 EXPECT_EQUAL(conn->secrets.extract_secret_type, S2N_MASTER_SECRET);
133 EXPECT_BYTEARRAY_NOT_EQUAL(conn->secrets.version.tls13.extract_secret, empty_secret, sizeof(empty_secret));
134 };
135
136 /* Generate all secrets at once (backfill) */
137 {
138 DEFER_CLEANUP(struct s2n_connection *conn = s2n_connection_new(S2N_SERVER),
139 s2n_connection_ptr_free);
140 conn->secure->cipher_suite = &s2n_tls13_aes_256_gcm_sha384;
141 conn->secrets.extract_secret_type = S2N_NONE_SECRET;
142 EXPECT_OK(s2n_set_test_key_shares(conn, &s2n_ecc_curve_secp256r1));
143
144 EXPECT_OK(s2n_tls13_extract_secret(conn, S2N_MASTER_SECRET));
145 EXPECT_EQUAL(conn->secrets.extract_secret_type, S2N_MASTER_SECRET);
146 EXPECT_BYTEARRAY_NOT_EQUAL(conn->secrets.version.tls13.extract_secret, empty_secret, sizeof(empty_secret));
147 }
148
149 /* All valid parameter combinations should succeed */
150 for (size_t i = 0; i < test_cases_count; i++) {
151 DEFER_CLEANUP(struct s2n_connection *conn = s2n_connection_new(test_cases[i].conn_mode),
152 s2n_connection_ptr_free);
153 conn->secure->cipher_suite = test_cases[i].cipher_suite;
154 conn->secrets.extract_secret_type = test_cases[i].curr_secret_type;
155 EXPECT_OK(s2n_set_test_key_shares(conn, test_cases[i].curve));
156 EXPECT_OK(s2n_tls13_extract_secret(conn, test_cases[i].next_secret_type));
157 }
158 };
159
160 /* Test: s2n_tls13_derive_secret */
161 {
162 const uint32_t handshake_type = NEGOTIATED | FULL_HANDSHAKE;
163 const int message_nums[] = {
164 [S2N_EARLY_SECRET] = 0,
165 [S2N_HANDSHAKE_SECRET] = 1,
166 [S2N_MASTER_SECRET] = 5,
167 };
168
169 /* Safety */
170 {
171 struct s2n_blob blob = { 0 };
172 struct s2n_connection empty_conn = { 0 };
173 DEFER_CLEANUP(struct s2n_connection *conn = s2n_connection_new(S2N_SERVER),
174 s2n_connection_ptr_free);
175
176 EXPECT_ERROR_WITH_ERRNO(s2n_tls13_derive_secret(NULL, S2N_EARLY_SECRET, S2N_CLIENT, &blob), S2N_ERR_NULL);
177 EXPECT_ERROR_WITH_ERRNO(s2n_tls13_derive_secret(&empty_conn, S2N_EARLY_SECRET, S2N_CLIENT, &blob), S2N_ERR_NULL);
178 EXPECT_ERROR_WITH_ERRNO(s2n_tls13_derive_secret(conn, -1, S2N_CLIENT, &blob), S2N_ERR_SAFETY);
179 EXPECT_ERROR_WITH_ERRNO(s2n_tls13_derive_secret(conn, 255, S2N_CLIENT, &blob), S2N_ERR_SAFETY);
180 EXPECT_ERROR_WITH_ERRNO(s2n_tls13_derive_secret(conn, S2N_EARLY_SECRET, S2N_CLIENT, NULL), S2N_ERR_NULL);
181 };
182
183 /* Generates a secret */
184 {
185 uint8_t output_bytes[S2N_TLS13_SECRET_MAX_LEN] = { 0 };
186 struct s2n_blob output = { 0 };
187 EXPECT_SUCCESS(s2n_blob_init(&output, output_bytes, sizeof(output_bytes)));
188
189 DEFER_CLEANUP(struct s2n_connection *conn = s2n_connection_new(S2N_SERVER),
190 s2n_connection_ptr_free);
191 conn->secure->cipher_suite = &s2n_tls13_aes_256_gcm_sha384;
192 conn->secrets.extract_secret_type = S2N_NONE_SECRET;
193
194 EXPECT_OK(s2n_tls13_derive_secret(conn, S2N_EARLY_SECRET, S2N_SERVER, &output));
195 EXPECT_BYTEARRAY_NOT_EQUAL(output.data, empty_secret, sizeof(empty_secret));
196 };
197
198 /* Fails if correct transcript digest not available */
199 {
200 uint8_t output_bytes[S2N_TLS13_SECRET_MAX_LEN] = { 0 };
201 struct s2n_blob output = { 0 };
202 EXPECT_SUCCESS(s2n_blob_init(&output, output_bytes, sizeof(output_bytes)));
203
204 DEFER_CLEANUP(struct s2n_connection *conn = s2n_connection_new(S2N_SERVER),
205 s2n_connection_ptr_free);
206 conn->actual_protocol_version = S2N_TLS13;
207 EXPECT_OK(s2n_conn_choose_state_machine(conn, S2N_TLS13));
208 conn->secure->cipher_suite = &s2n_tls13_aes_256_gcm_sha384;
209 conn->handshake.handshake_type = handshake_type;
210 EXPECT_OK(s2n_set_test_key_shares(conn, &s2n_ecc_curve_secp256r1));
211
212 /* Fails with incorrect transcript */
213 conn->handshake.message_number = message_nums[S2N_HANDSHAKE_SECRET];
214 EXPECT_ERROR_WITH_ERRNO(s2n_tls13_derive_secret(conn, S2N_MASTER_SECRET, S2N_SERVER, &output),
215 S2N_ERR_SECRET_SCHEDULE_STATE);
216
217 /* Succeeds with correct transcript */
218 conn->handshake.message_number = message_nums[S2N_MASTER_SECRET];
219 EXPECT_OK(s2n_tls13_derive_secret(conn, S2N_MASTER_SECRET, S2N_SERVER, &output));
220 };
221
222 /* Calculates previous extract secrets if necessary */
223 {
224 uint8_t output_bytes[S2N_TLS13_SECRET_MAX_LEN] = { 0 };
225 struct s2n_blob output = { 0 };
226 EXPECT_SUCCESS(s2n_blob_init(&output, output_bytes, sizeof(output_bytes)));
227
228 DEFER_CLEANUP(struct s2n_connection *conn = s2n_connection_new(S2N_SERVER),
229 s2n_connection_ptr_free);
230 conn->secure->cipher_suite = &s2n_tls13_aes_256_gcm_sha384;
231 EXPECT_OK(s2n_conn_choose_state_machine(conn, S2N_TLS13));
232 conn->actual_protocol_version = S2N_TLS13;
233 conn->handshake.handshake_type = handshake_type;
234 conn->handshake.message_number = message_nums[S2N_HANDSHAKE_SECRET];
235 EXPECT_OK(s2n_set_test_key_shares(conn, &s2n_ecc_curve_secp256r1));
236
237 conn->secrets.extract_secret_type = S2N_NONE_SECRET;
238 EXPECT_OK(s2n_tls13_derive_secret(conn, S2N_HANDSHAKE_SECRET, S2N_SERVER, &output));
239 EXPECT_EQUAL(conn->secrets.extract_secret_type, S2N_HANDSHAKE_SECRET);
240 EXPECT_BYTEARRAY_NOT_EQUAL(conn->secrets.version.tls13.extract_secret, empty_secret, sizeof(empty_secret));
241 EXPECT_BYTEARRAY_NOT_EQUAL(output.data, empty_secret, sizeof(empty_secret));
242 };
243
244 /* All valid parameter combinations should succeed */
245 for (size_t i = 0; i < test_cases_count; i++) {
246 uint8_t output_bytes[S2N_TLS13_SECRET_MAX_LEN] = { 0 };
247 struct s2n_blob output = { 0 };
248 EXPECT_SUCCESS(s2n_blob_init(&output, output_bytes, sizeof(output_bytes)));
249
250 DEFER_CLEANUP(struct s2n_connection *conn = s2n_connection_new(test_cases[i].conn_mode),
251 s2n_connection_ptr_free);
252 conn->secure->cipher_suite = test_cases[i].cipher_suite;
253 conn->secrets.extract_secret_type = test_cases[i].curr_secret_type;
254 EXPECT_OK(s2n_conn_choose_state_machine(conn, S2N_TLS13));
255 conn->actual_protocol_version = S2N_TLS13;
256 conn->handshake.handshake_type = handshake_type;
257 conn->handshake.message_number = message_nums[test_cases[i].next_secret_type];
258 EXPECT_OK(s2n_set_test_key_shares(conn, test_cases[i].curve));
259 EXPECT_OK(s2n_tls13_derive_secret(conn, test_cases[i].next_secret_type, test_cases[i].secret_mode, &output));
260 EXPECT_BYTEARRAY_NOT_EQUAL(output.data, empty_secret, sizeof(empty_secret));
261 }
262 };
263
264 /* s2n_tls13_secrets_clean */
265 {
266 /* Safety */
267 EXPECT_ERROR_WITH_ERRNO(s2n_tls13_secrets_clean(NULL), S2N_ERR_NULL);
268
269 /* Wipes all secrets */
270 {
271 DEFER_CLEANUP(struct s2n_connection *conn = s2n_connection_new(S2N_SERVER),
272 s2n_connection_ptr_free);
273 conn->secure->cipher_suite = &s2n_tls13_aes_128_gcm_sha256;
274 conn->actual_protocol_version = S2N_TLS13;
275
276 EXPECT_MEMCPY_SUCCESS(conn->secrets.version.tls13.extract_secret, test_secret.data, test_secret.size);
277 EXPECT_MEMCPY_SUCCESS(conn->secrets.version.tls13.client_early_secret, test_secret.data, test_secret.size);
278 EXPECT_MEMCPY_SUCCESS(conn->secrets.version.tls13.client_handshake_secret, test_secret.data, test_secret.size);
279 EXPECT_MEMCPY_SUCCESS(conn->secrets.version.tls13.server_handshake_secret, test_secret.data, test_secret.size);
280 EXPECT_MEMCPY_SUCCESS(conn->secrets.version.tls13.client_app_secret, test_secret.data, test_secret.size);
281 EXPECT_MEMCPY_SUCCESS(conn->secrets.version.tls13.server_app_secret, test_secret.data, test_secret.size);
282 EXPECT_MEMCPY_SUCCESS(conn->secrets.version.tls13.exporter_master_secret, test_secret.data, test_secret.size);
283 EXPECT_MEMCPY_SUCCESS(conn->secrets.version.tls13.resumption_master_secret, test_secret.data, test_secret.size);
284
285 EXPECT_BYTEARRAY_NOT_EQUAL(conn->secrets.version.tls13.extract_secret, empty_secret, sizeof(empty_secret));
286 EXPECT_BYTEARRAY_NOT_EQUAL(conn->secrets.version.tls13.client_early_secret, empty_secret, sizeof(empty_secret));
287 EXPECT_BYTEARRAY_NOT_EQUAL(conn->secrets.version.tls13.client_handshake_secret, empty_secret, sizeof(empty_secret));
288 EXPECT_BYTEARRAY_NOT_EQUAL(conn->secrets.version.tls13.server_handshake_secret, empty_secret, sizeof(empty_secret));
289 EXPECT_BYTEARRAY_NOT_EQUAL(conn->secrets.version.tls13.client_app_secret, empty_secret, sizeof(empty_secret));
290 EXPECT_BYTEARRAY_NOT_EQUAL(conn->secrets.version.tls13.server_app_secret, empty_secret, sizeof(empty_secret));
291 EXPECT_BYTEARRAY_NOT_EQUAL(conn->secrets.version.tls13.exporter_master_secret, empty_secret, sizeof(empty_secret));
292 EXPECT_BYTEARRAY_NOT_EQUAL(conn->secrets.version.tls13.resumption_master_secret, empty_secret, sizeof(empty_secret));
293
294 EXPECT_OK(s2n_tls13_secrets_clean(conn));
295
296 EXPECT_BYTEARRAY_EQUAL(conn->secrets.version.tls13.extract_secret, empty_secret, sizeof(empty_secret));
297 EXPECT_BYTEARRAY_EQUAL(conn->secrets.version.tls13.client_early_secret, empty_secret, sizeof(empty_secret));
298 EXPECT_BYTEARRAY_EQUAL(conn->secrets.version.tls13.client_handshake_secret, empty_secret, sizeof(empty_secret));
299 EXPECT_BYTEARRAY_EQUAL(conn->secrets.version.tls13.server_handshake_secret, empty_secret, sizeof(empty_secret));
300 EXPECT_BYTEARRAY_NOT_EQUAL(conn->secrets.version.tls13.client_app_secret, empty_secret, sizeof(empty_secret));
301 EXPECT_BYTEARRAY_NOT_EQUAL(conn->secrets.version.tls13.server_app_secret, empty_secret, sizeof(empty_secret));
302 EXPECT_BYTEARRAY_NOT_EQUAL(conn->secrets.version.tls13.exporter_master_secret, empty_secret, sizeof(empty_secret));
303 EXPECT_BYTEARRAY_NOT_EQUAL(conn->secrets.version.tls13.resumption_master_secret, empty_secret, sizeof(empty_secret));
304 };
305 };
306
307 /* Test s2n_tls13_secrets_get */
308 {
309 /* Safety */
310 {
311 struct s2n_blob result = { 0 };
312 DEFER_CLEANUP(struct s2n_connection *conn = s2n_connection_new(S2N_CLIENT), s2n_connection_ptr_free);
313 EXPECT_ERROR_WITH_ERRNO(s2n_tls13_secrets_get(NULL, S2N_HANDSHAKE_SECRET, S2N_CLIENT, &result), S2N_ERR_NULL);
314 EXPECT_ERROR_WITH_ERRNO(s2n_tls13_secrets_get(conn, S2N_HANDSHAKE_SECRET, S2N_CLIENT, NULL), S2N_ERR_NULL);
315 EXPECT_ERROR_WITH_ERRNO(s2n_tls13_secrets_get(conn, S2N_NONE_SECRET, S2N_CLIENT, &result), S2N_ERR_SAFETY);
316 EXPECT_ERROR_WITH_ERRNO(s2n_tls13_secrets_get(conn, -1, S2N_CLIENT, &result), S2N_ERR_SAFETY);
317 EXPECT_ERROR_WITH_ERRNO(s2n_tls13_secrets_get(conn, 100, S2N_CLIENT, &result), S2N_ERR_SAFETY);
318 EXPECT_ERROR_WITH_ERRNO(s2n_tls13_secrets_get(conn, S2N_EARLY_SECRET, S2N_SERVER, &result), S2N_ERR_SAFETY);
319
320 conn->secrets.extract_secret_type = S2N_NONE_SECRET;
321 EXPECT_ERROR_WITH_ERRNO(s2n_tls13_secrets_get(conn, S2N_HANDSHAKE_SECRET, S2N_CLIENT, &result), S2N_ERR_SAFETY);
322
323 struct s2n_crypto_parameters *secure = conn->secure;
324 conn->secure = NULL;
325 EXPECT_ERROR_WITH_ERRNO(s2n_tls13_secrets_get(conn, S2N_HANDSHAKE_SECRET, S2N_CLIENT, &result), S2N_ERR_NULL);
326 conn->secure = secure;
327
328 struct s2n_cipher_suite *cipher_suite = conn->secure->cipher_suite;
329 conn->secure->cipher_suite = NULL;
330 EXPECT_ERROR_WITH_ERRNO(s2n_tls13_secrets_get(conn, S2N_HANDSHAKE_SECRET, S2N_CLIENT, &result), S2N_ERR_NULL);
331 conn->secure->cipher_suite = cipher_suite;
332 };
333
334 /* Retrieves a secret */
335 {
336 DEFER_CLEANUP(struct s2n_connection *conn = s2n_connection_new(S2N_SERVER),
337 s2n_connection_ptr_free);
338 conn->secure->cipher_suite = &s2n_tls13_aes_128_gcm_sha256;
339 conn->actual_protocol_version = S2N_TLS13;
340
341 EXPECT_MEMCPY_SUCCESS(conn->secrets.version.tls13.client_handshake_secret,
342 test_secret.data, test_secret.size);
343 conn->secrets.extract_secret_type = S2N_HANDSHAKE_SECRET;
344
345 struct s2n_blob result = { 0 };
346 uint8_t result_bytes[S2N_TLS13_SECRET_MAX_LEN] = { 0 };
347 EXPECT_SUCCESS(s2n_blob_init(&result, result_bytes, sizeof(result_bytes)));
348 EXPECT_OK(s2n_tls13_secrets_get(conn, S2N_HANDSHAKE_SECRET, S2N_CLIENT, &result));
349
350 EXPECT_TRUE(result.size > 0);
351 EXPECT_TRUE(result.size <= S2N_TLS13_SECRET_MAX_LEN);
352 EXPECT_BYTEARRAY_EQUAL(result.data, test_secret.data, result.size);
353 };
354 };
355
356 /* s2n_tls13_secrets_update */
357 {
358 /* Safety */
359 EXPECT_ERROR_WITH_ERRNO(s2n_tls13_secrets_update(NULL), S2N_ERR_NULL);
360
361 /* Derives early secret on CLIENT_HELLO */
362 {
363 DEFER_CLEANUP(struct s2n_connection *conn = s2n_connection_new(S2N_SERVER),
364 s2n_connection_ptr_free);
365 conn->secure->cipher_suite = &s2n_tls13_aes_128_gcm_sha256;
366 conn->actual_protocol_version = S2N_TLS13;
367 EXPECT_BYTEARRAY_EQUAL(conn->secrets.version.tls13.client_early_secret,
368 empty_secret, sizeof(empty_secret));
369
370 /* Early secret not derived if early data not requested */
371 conn->early_data_state = S2N_EARLY_DATA_NOT_REQUESTED;
372 EXPECT_OK(s2n_tls13_secrets_update(conn));
373 EXPECT_BYTEARRAY_EQUAL(conn->secrets.version.tls13.client_early_secret,
374 empty_secret, sizeof(empty_secret));
375
376 /* Early secret not derived if early data rejected */
377 conn->early_data_state = S2N_EARLY_DATA_REJECTED;
378 EXPECT_OK(s2n_tls13_secrets_update(conn));
379 EXPECT_BYTEARRAY_EQUAL(conn->secrets.version.tls13.client_early_secret,
380 empty_secret, sizeof(empty_secret));
381
382 /* Early secret derived if early data requested */
383 conn->early_data_state = S2N_EARLY_DATA_REQUESTED;
384 EXPECT_OK(s2n_tls13_secrets_update(conn));
385 EXPECT_BYTEARRAY_NOT_EQUAL(conn->secrets.version.tls13.client_early_secret,
386 empty_secret, sizeof(empty_secret));
387
388 /* Clear secret */
389 EXPECT_MEMCPY_SUCCESS(conn->secrets.version.tls13.client_early_secret,
390 empty_secret, sizeof(empty_secret));
391
392 /* Early secret derived if early data accepted */
393 conn->early_data_state = S2N_EARLY_DATA_ACCEPTED;
394 EXPECT_OK(s2n_tls13_secrets_update(conn));
395 EXPECT_BYTEARRAY_NOT_EQUAL(conn->secrets.version.tls13.client_early_secret,
396 empty_secret, sizeof(empty_secret));
397 };
398
399 /* Derives handshake secrets on SERVER_HELLO */
400 {
401 DEFER_CLEANUP(struct s2n_connection *conn = s2n_connection_new(S2N_SERVER),
402 s2n_connection_ptr_free);
403 conn->secure->cipher_suite = &s2n_tls13_aes_128_gcm_sha256;
404 conn->actual_protocol_version = S2N_TLS13;
405 EXPECT_OK(s2n_connection_set_test_handshake_secret(conn, &test_secret));
406 EXPECT_BYTEARRAY_EQUAL(conn->secrets.version.tls13.client_handshake_secret,
407 empty_secret, sizeof(empty_secret));
408 EXPECT_BYTEARRAY_EQUAL(conn->secrets.version.tls13.server_handshake_secret,
409 empty_secret, sizeof(empty_secret));
410
411 EXPECT_OK(s2n_connection_set_test_message_type(conn, SERVER_HELLO));
412 EXPECT_OK(s2n_tls13_secrets_update(conn));
413
414 EXPECT_BYTEARRAY_NOT_EQUAL(conn->secrets.version.tls13.client_handshake_secret,
415 empty_secret, sizeof(empty_secret));
416 EXPECT_BYTEARRAY_NOT_EQUAL(conn->secrets.version.tls13.server_handshake_secret,
417 empty_secret, sizeof(empty_secret));
418 };
419
420 /* Computes finished keys on SERVER_HELLO */
421 {
422 DEFER_CLEANUP(struct s2n_connection *conn = s2n_connection_new(S2N_SERVER),
423 s2n_connection_ptr_free);
424 conn->secure->cipher_suite = &s2n_tls13_aes_128_gcm_sha256;
425 conn->actual_protocol_version = S2N_TLS13;
426 EXPECT_OK(s2n_connection_set_test_handshake_secret(conn, &test_secret));
427 EXPECT_EQUAL(conn->handshake.finished_len, 0);
428 EXPECT_BYTEARRAY_EQUAL(conn->handshake.client_finished,
429 empty_secret, sizeof(empty_secret));
430 EXPECT_BYTEARRAY_EQUAL(conn->handshake.server_finished,
431 empty_secret, sizeof(empty_secret));
432
433 EXPECT_OK(s2n_connection_set_test_message_type(conn, SERVER_HELLO));
434 EXPECT_OK(s2n_tls13_secrets_update(conn));
435
436 uint8_t expected_len = 0;
437 EXPECT_SUCCESS(s2n_hmac_digest_size(conn->secure->cipher_suite->prf_alg, &expected_len));
438 EXPECT_EQUAL(conn->handshake.finished_len, expected_len);
439 EXPECT_BYTEARRAY_NOT_EQUAL(conn->handshake.client_finished,
440 empty_secret, sizeof(empty_secret));
441 EXPECT_BYTEARRAY_NOT_EQUAL(conn->handshake.server_finished,
442 empty_secret, sizeof(empty_secret));
443 };
444
445 /* Derives application secrets on SERVER_FINISHED */
446 {
447 DEFER_CLEANUP(struct s2n_connection *conn = s2n_connection_new(S2N_SERVER),
448 s2n_connection_ptr_free);
449 conn->secure->cipher_suite = &s2n_tls13_aes_128_gcm_sha256;
450 conn->actual_protocol_version = S2N_TLS13;
451 EXPECT_OK(s2n_connection_set_test_master_secret(conn, &test_secret));
452 EXPECT_BYTEARRAY_EQUAL(conn->secrets.version.tls13.client_app_secret,
453 empty_secret, sizeof(empty_secret));
454 EXPECT_BYTEARRAY_EQUAL(conn->secrets.version.tls13.server_app_secret,
455 empty_secret, sizeof(empty_secret));
456
457 EXPECT_OK(s2n_connection_set_test_message_type(conn, SERVER_FINISHED));
458 EXPECT_OK(s2n_tls13_secrets_update(conn));
459
460 EXPECT_BYTEARRAY_NOT_EQUAL(conn->secrets.version.tls13.client_app_secret,
461 empty_secret, sizeof(empty_secret));
462 EXPECT_BYTEARRAY_NOT_EQUAL(conn->secrets.version.tls13.server_app_secret,
463 empty_secret, sizeof(empty_secret));
464 };
465
466 /* Derives resumption secret on CLIENT_FINISHED */
467 {
468 DEFER_CLEANUP(struct s2n_connection *conn = s2n_connection_new(S2N_SERVER),
469 s2n_connection_ptr_free);
470 conn->secure->cipher_suite = &s2n_tls13_aes_128_gcm_sha256;
471 conn->actual_protocol_version = S2N_TLS13;
472 EXPECT_OK(s2n_connection_set_test_master_secret(conn, &test_secret));
473 EXPECT_BYTEARRAY_EQUAL(conn->secrets.version.tls13.resumption_master_secret,
474 empty_secret, sizeof(empty_secret));
475
476 EXPECT_OK(s2n_connection_set_test_message_type(conn, CLIENT_FINISHED));
477 EXPECT_OK(s2n_tls13_secrets_update(conn));
478
479 EXPECT_BYTEARRAY_NOT_EQUAL(conn->secrets.version.tls13.resumption_master_secret,
480 empty_secret, sizeof(empty_secret));
481 };
482 };
483
484 /* s2n_connection_export_secret */
485 {
486 /* Derives exporter secret on SERVER_FINISHED */
487 {
488 DEFER_CLEANUP(struct s2n_connection *conn = s2n_connection_new(S2N_SERVER),
489 s2n_connection_ptr_free);
490 conn->secure->cipher_suite = &s2n_tls13_aes_128_gcm_sha256;
491 conn->actual_protocol_version = S2N_TLS13;
492 EXPECT_OK(s2n_connection_set_test_master_secret(conn, &test_secret));
493 EXPECT_BYTEARRAY_EQUAL(conn->secrets.version.tls13.exporter_master_secret,
494 empty_secret, sizeof(empty_secret));
495
496 EXPECT_OK(s2n_connection_set_test_message_type(conn, SERVER_FINISHED));
497 EXPECT_OK(s2n_tls13_secrets_update(conn));
498
499 EXPECT_BYTEARRAY_NOT_EQUAL(conn->secrets.version.tls13.exporter_master_secret,
500 empty_secret, sizeof(empty_secret));
501
502 /*
503 * s2n_connection_tls_exporter requires us to finish the handshake.
504 * The above is needed since s2n_tls13_secrets_update will only
505 * initialize when it sees the SERVER_FINISHED frame.
506 */
507 EXPECT_OK(s2n_skip_handshake(conn));
508
509 uint8_t output[32] = { 0 };
510 int result = s2n_connection_tls_exporter(
511 conn,
512 (const uint8_t *) "label",
513 sizeof("label") - 1,
514 (const uint8_t *) "context",
515 sizeof("context") - 1,
516 output,
517 sizeof(output));
518 EXPECT_SUCCESS(result);
519 /*
520 * If updating this value, it's a good idea to make sure the update
521 * matches OpenSSL's SSL_export_keying_material. The easiest known
522 * way of doing that is building a simple client/server pair and
523 * calling the s2n and OpenSSL APIs after a handshake on both
524 * sides; you should get identical results with identical
525 * label/context parameters. This particular value though is not
526 * checked as its dependent on the s2n-specific test master secret.
527 */
528 S2N_BLOB_FROM_HEX(expected, "3a 72 eb 08 10 a3 69 f3 06 f2 77 11 70 ad d5 76 bd 21 15 \
529 46 d4 c8 fb 80 1a 93 04 1e ac 59 aa 47");
530 EXPECT_EQUAL(sizeof(output), expected.size);
531 EXPECT_BYTEARRAY_EQUAL(output, expected.data, expected.size);
532 };
533 };
534
535 END_TEST();
536 }