Debian Codegraph @

crt/s2n/tests/unit/s2n_tls_prf_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 <stdio.h>
17 #include <string.h>
18
19 #include "api/s2n.h"
20 #include "crypto/s2n_libcrypto.h"
21 #include "s2n_test.h"
22 #include "stuffer/s2n_stuffer.h"
23 #include "testlib/s2n_testlib.h"
24 /* To gain access to handshake_read and handshake_write */
25 #include "tls/s2n_handshake_io.c"
26
27 #define TEST_BLOB_SIZE 64
28
29 int s2n_prf(struct s2n_connection *conn, struct s2n_blob *secret,
30 struct s2n_blob *label, struct s2n_blob *seed_a,
31 struct s2n_blob *seed_b, struct s2n_blob *seed_c, struct s2n_blob *out);
32 S2N_RESULT s2n_prf_get_digest_for_ems(struct s2n_connection *conn,
33 struct s2n_blob *message, s2n_hash_algorithm hash_alg, struct s2n_blob *output);
34 S2N_RESULT s2n_prf_tls_extended_master_secret(struct s2n_connection *conn,
35 struct s2n_blob *premaster_secret, struct s2n_blob *session_hash, struct s2n_blob *sha1_hash);
36 int s2n_prf_tls_master_secret(struct s2n_connection *conn, struct s2n_blob *premaster_secret);
37
38 /*
39 * Grabbed from gnutls-cli --insecure -d 9 www.example.com --ciphers AES --macs SHA --protocols TLS1.0
40 *
41 * |<9>| INT: PREMASTER SECRET[48]: 0301bebf2a5707c7bda6bfe5a8971a351a9ebd019de412212da021fd802e03f49f231d4e959c7352679f892f9d7f9748
42 * |<9>| INT: CLIENT RANDOM[32]: 537eefc1e720b311ff8483d057ae750a3667af9d5b496cc0d2edfb0dd309a286
43 * |<9>| INT: SERVER RANDOM[32]: 537eefc29f337c5eedacd00a1889b031261701872d666a74fa999dc13bcd8821
44 * |<9>| INT: MASTER SECRET: c8c610686237cd024a2d8e0391f61a8a4464c2c9576ea2b5ccf3af68139ec07c6a1720097063de968f2341f77b837120
45 */
46 int main(int argc, char **argv)
47 {
48 BEGIN_TEST();
49 EXPECT_SUCCESS(s2n_disable_tls13_in_test());
50
51 S2N_BLOB_FROM_HEX(premaster_secret_in,
52 "0301bebf2a5707c7bda6bfe5a8971a351a9ebd019de412212da021fd802e03f49f231d4e959c7352679f892f9d7f9748");
53 S2N_BLOB_FROM_HEX(client_random_in,
54 "537eefc1e720b311ff8483d057ae750a3667af9d5b496cc0d2edfb0dd309a286");
55 S2N_BLOB_FROM_HEX(server_random_in,
56 "537eefc29f337c5eedacd00a1889b031261701872d666a74fa999dc13bcd8821");
57 S2N_BLOB_FROM_HEX(master_secret_in,
58 "c8c610686237cd024a2d8e0391f61a8a4464c2c9576ea2b5ccf3af68139ec07c6a1720097063de968f2341f77b837120");
59
60 /* s2n_prf tests */
61 {
62 uint8_t secret_bytes[TEST_BLOB_SIZE] = "secret";
63 struct s2n_blob secret = { 0 };
64 EXPECT_SUCCESS(s2n_blob_init(&secret, secret_bytes, sizeof(secret_bytes)));
65
66 uint8_t label_bytes[TEST_BLOB_SIZE] = "label";
67 struct s2n_blob label = { 0 };
68 EXPECT_SUCCESS(s2n_blob_init(&label, label_bytes, sizeof(label_bytes)));
69
70 uint8_t seed_a_bytes[TEST_BLOB_SIZE] = "seed a";
71 struct s2n_blob seed_a = { 0 };
72 EXPECT_SUCCESS(s2n_blob_init(&seed_a, seed_a_bytes, sizeof(seed_a_bytes)));
73
74 uint8_t seed_b_bytes[TEST_BLOB_SIZE] = "seed b";
75 struct s2n_blob seed_b = { 0 };
76 EXPECT_SUCCESS(s2n_blob_init(&seed_b, seed_b_bytes, sizeof(seed_b_bytes)));
77
78 uint8_t seed_c_bytes[TEST_BLOB_SIZE] = "seed c";
79 struct s2n_blob seed_c = { 0 };
80 EXPECT_SUCCESS(s2n_blob_init(&seed_c, seed_c_bytes, sizeof(seed_c_bytes)));
81
82 /* Safety */
83 {
84 DEFER_CLEANUP(struct s2n_connection *connection = s2n_connection_new(S2N_SERVER),
85 s2n_connection_ptr_free);
86 s2n_stack_blob(out, TEST_BLOB_SIZE, TEST_BLOB_SIZE);
87
88 EXPECT_FAILURE_WITH_ERRNO(s2n_prf(NULL, &secret, &label, &seed_a, &seed_b, &seed_c, &out),
89 S2N_ERR_NULL);
90 EXPECT_FAILURE_WITH_ERRNO(s2n_prf(connection, NULL, &label, &seed_a, &seed_b, &seed_c, &out),
91 S2N_ERR_NULL);
92 EXPECT_FAILURE_WITH_ERRNO(s2n_prf(connection, &secret, NULL, &seed_a, &seed_b, &seed_c, &out),
93 S2N_ERR_NULL);
94 EXPECT_FAILURE_WITH_ERRNO(s2n_prf(connection, &secret, &label, &seed_a, &seed_b, &seed_c, NULL),
95 S2N_ERR_NULL);
96
97 /* seed_a is required */
98 EXPECT_FAILURE_WITH_ERRNO(s2n_prf(connection, &secret, &label, NULL, &seed_b, &seed_c, &out),
99 S2N_ERR_PRF_INVALID_SEED);
100
101 /* seed_b and seed_c are optional */
102 EXPECT_SUCCESS(s2n_prf(connection, &secret, &label, &seed_a, NULL, NULL, &out));
103
104 /* seed_b is required if seed_c is provided */
105 EXPECT_FAILURE_WITH_ERRNO(s2n_prf(connection, &secret, &label, &seed_a, NULL, &seed_c, &out),
106 S2N_ERR_PRF_INVALID_SEED);
107
108 /* seed_c is optional */
109 EXPECT_SUCCESS(s2n_prf(connection, &secret, &label, &seed_a, &seed_b, NULL, &out));
110 }
111
112 /* Test: secret and label */
113 {
114 struct s2n_blob empty = { 0 };
115 DEFER_CLEANUP(struct s2n_connection *conn = s2n_connection_new(S2N_SERVER),
116 s2n_connection_ptr_free);
117 conn->actual_protocol_version = S2N_TLS11;
118
119 const char *expected_hex =
120 "d6 4d 7e e1 f3 ca ff a0 d4 7e 84 18 ff 64 97 d8"
121 "56 4d d1 99 5e ea 53 0d 29 b0 42 68 89 45 f3 58 86"
122 "f5 4b 12 ff b3 83 87 80 d5 ba 7d f6 26 10 a5 cc 39"
123 "d9 e7 37 6e eb 28 8e 21 29 53 13 54 f5 f6";
124 S2N_BLOB_FROM_HEX(expected, expected_hex);
125
126 s2n_stack_blob(out, TEST_BLOB_SIZE, TEST_BLOB_SIZE);
127 EXPECT_SUCCESS(s2n_prf(conn, &secret, &label, &empty, NULL, NULL, &out));
128 EXPECT_EQUAL(out.size, expected.size);
129 EXPECT_BYTEARRAY_EQUAL(out.data, expected.data, expected.size);
130 }
131
132 /* Test: secret, label, and seed_a */
133 {
134 DEFER_CLEANUP(struct s2n_connection *conn = s2n_connection_new(S2N_SERVER),
135 s2n_connection_ptr_free);
136 conn->actual_protocol_version = S2N_TLS11;
137
138 const char *expected_hex =
139 "4e f2 13 65 22 07 f1 e5 86 dc 03 aa 60 a2 c9 5c"
140 "ab 05 5e 43 f0 c8 3a 70 6c f4 be 21 44 9e b8 45 70"
141 "d9 e8 e1 83 3b f7 65 dd e9 d1 ea ae 14 f7 73 c5 47"
142 "57 da 0a a8 5f cc 75 ff b2 9e 3d 02 01 e9";
143 S2N_BLOB_FROM_HEX(expected, expected_hex);
144
145 s2n_stack_blob(out, TEST_BLOB_SIZE, TEST_BLOB_SIZE);
146 EXPECT_SUCCESS(s2n_prf(conn, &secret, &label, &seed_a, NULL, NULL, &out));
147 EXPECT_EQUAL(out.size, expected.size);
148 EXPECT_BYTEARRAY_EQUAL(out.data, expected.data, expected.size);
149 }
150
151 /* Test: secret, label, seed_a, and seed_b */
152 {
153 DEFER_CLEANUP(struct s2n_connection *conn = s2n_connection_new(S2N_SERVER),
154 s2n_connection_ptr_free);
155 conn->actual_protocol_version = S2N_TLS11;
156
157 const char *expected_hex =
158 "45 0b b0 da 24 d5 23 05 b6 00 f9 1c 71 6f ca 40"
159 "54 ba 1b 6c 91 43 77 9c 0f 6f 2c e6 e4 24 30 ee b3"
160 "fc 83 85 3a 02 60 1e 37 2c 2c ee d3 b1 8a 6a cc 75"
161 "d9 84 87 42 d8 b6 a9 9c 1b f8 72 8c 6b 71";
162 S2N_BLOB_FROM_HEX(expected, expected_hex);
163
164 s2n_stack_blob(out, TEST_BLOB_SIZE, TEST_BLOB_SIZE);
165 EXPECT_SUCCESS(s2n_prf(conn, &secret, &label, &seed_a, &seed_b, NULL, &out));
166 EXPECT_EQUAL(out.size, expected.size);
167 EXPECT_BYTEARRAY_EQUAL(out.data, expected.data, expected.size);
168 }
169
170 /* Test: all inputs */
171 {
172 DEFER_CLEANUP(struct s2n_connection *conn = s2n_connection_new(S2N_SERVER),
173 s2n_connection_ptr_free);
174 conn->actual_protocol_version = S2N_TLS11;
175
176 const char *expected_hex =
177 "4f b6 e2 ac d0 68 dc 55 70 43 ab 98 f6 23 a8 93"
178 "6b f2 0f 48 1c 74 50 7e a0 f2 ef 49 53 a6 b0 56 84"
179 "fe 2e a9 76 31 50 44 f7 8d e7 3d 52 97 ce 36 82 a8"
180 "d7 27 59 f7 7a 73 19 06 6c 0a 1a df 68 c6";
181 S2N_BLOB_FROM_HEX(expected, expected_hex);
182
183 s2n_stack_blob(out, TEST_BLOB_SIZE, TEST_BLOB_SIZE);
184 EXPECT_SUCCESS(s2n_prf(conn, &secret, &label, &seed_a, &seed_b, &seed_c, &out));
185 EXPECT_EQUAL(out.size, expected.size);
186 EXPECT_BYTEARRAY_EQUAL(out.data, expected.data, expected.size);
187 }
188
189 /* Test: cipher specific digest */
190 {
191 DEFER_CLEANUP(struct s2n_connection *conn = s2n_connection_new(S2N_SERVER),
192 s2n_connection_ptr_free);
193 conn->actual_protocol_version = S2N_TLS12;
194 conn->secure->cipher_suite = &s2n_ecdhe_ecdsa_with_aes_256_gcm_sha384;
195
196 const char *expected_hex =
197 "5b 54 71 ec b9 8a 49 ce f1 6a d9 31 cf c9 76 be"
198 "5a e6 25 bd a3 45 69 45 8c 6c 1a a1 98 06 d9 0d cc"
199 "c8 cd 7c aa d7 e2 59 25 4b 36 ff f7 01 a6 7e 89 22"
200 "0f bd 06 15 bf 9d 7e d1 53 45 a3 1b 36 da";
201 S2N_BLOB_FROM_HEX(expected, expected_hex);
202
203 s2n_stack_blob(out, TEST_BLOB_SIZE, TEST_BLOB_SIZE);
204 EXPECT_SUCCESS(s2n_prf(conn, &secret, &label, &seed_a, &seed_b, &seed_c, &out));
205 EXPECT_EQUAL(out.size, expected.size);
206 EXPECT_BYTEARRAY_EQUAL(out.data, expected.data, expected.size);
207 }
208
209 /* Test: large seeds fail for openssl-3.0-fips */
210 if (s2n_libcrypto_is_openssl_fips()) {
211 DEFER_CLEANUP(struct s2n_connection *conn = s2n_connection_new(S2N_SERVER),
212 s2n_connection_ptr_free);
213 conn->actual_protocol_version = S2N_TLS11;
214
215 DEFER_CLEANUP(struct s2n_blob small_seed = { 0 }, s2n_free);
216 EXPECT_SUCCESS(s2n_alloc(&small_seed, 30));
217
218 DEFER_CLEANUP(struct s2n_blob medium_seed = { 0 }, s2n_free);
219 EXPECT_SUCCESS(s2n_alloc(&medium_seed, 300));
220
221 DEFER_CLEANUP(struct s2n_blob large_seed = { 0 }, s2n_free);
222 EXPECT_SUCCESS(s2n_alloc(&large_seed, 3000));
223
224 s2n_stack_blob(out, TEST_BLOB_SIZE, TEST_BLOB_SIZE);
225 EXPECT_FAILURE_WITH_ERRNO(
226 s2n_prf(conn, &secret, &small_seed, &small_seed, &small_seed, &large_seed, &out),
227 S2N_ERR_PRF_INVALID_SEED);
228 EXPECT_FAILURE_WITH_ERRNO(
229 s2n_prf(conn, &secret, &medium_seed, &medium_seed, &medium_seed, &medium_seed, &out),
230 S2N_ERR_PRF_INVALID_SEED);
231 EXPECT_SUCCESS(s2n_prf(conn, &secret,
232 &small_seed, &small_seed, &small_seed, &small_seed, &out));
233 }
234
235 /* The custom PRF implementation is used when s2n-tls is not operating in FIPS mode */
236 if (!s2n_is_in_fips_mode()) {
237 DEFER_CLEANUP(struct s2n_connection *connection = s2n_connection_new(S2N_SERVER), s2n_connection_ptr_free);
238
239 uint8_t zeros[S2N_MAX_DIGEST_LEN] = { 0 };
240 EXPECT_EQUAL(memcmp(connection->prf_space->digest0, zeros, S2N_MAX_DIGEST_LEN), 0);
241
242 s2n_stack_blob(out, TEST_BLOB_SIZE, TEST_BLOB_SIZE);
243 EXPECT_SUCCESS(s2n_prf(connection, &secret, &label, &seed_a, &seed_b, &seed_c, &out));
244
245 /* The custom PRF implementation should modify the digest fields in the prf_space */
246 EXPECT_NOT_EQUAL(memcmp(connection->prf_space->digest0, zeros, S2N_MAX_DIGEST_LEN), 0);
247 }
248
249 /* The libcrypto PRF implementation is used when s2n-tls is in FIPS mode */
250 if (s2n_is_in_fips_mode()) {
251 DEFER_CLEANUP(struct s2n_connection *connection = s2n_connection_new(S2N_SERVER), s2n_connection_ptr_free);
252
253 uint8_t zeros[S2N_MAX_DIGEST_LEN] = { 0 };
254 EXPECT_EQUAL(memcmp(connection->prf_space->digest0, zeros, S2N_MAX_DIGEST_LEN), 0);
255
256 s2n_stack_blob(out, TEST_BLOB_SIZE, TEST_BLOB_SIZE);
257 EXPECT_SUCCESS(s2n_prf(connection, &secret, &label, &seed_a, &seed_b, &seed_c, &out));
258
259 /* The libcrypto PRF implementation will not modify the digest fields in the prf_space */
260 EXPECT_EQUAL(memcmp(connection->prf_space->digest0, zeros, S2N_MAX_DIGEST_LEN), 0);
261 }
262 }
263
264 /* s2n_tls_prf_master_secret */
265 {
266 DEFER_CLEANUP(struct s2n_connection *conn = s2n_connection_new(S2N_SERVER),
267 s2n_connection_ptr_free);
268 EXPECT_NOT_NULL(conn);
269
270 /* Check the most common PRF */
271 conn->actual_protocol_version = S2N_TLS11;
272
273 EXPECT_MEMCPY_SUCCESS(conn->secrets.version.tls12.rsa_premaster_secret, premaster_secret_in.data, premaster_secret_in.size);
274 EXPECT_MEMCPY_SUCCESS(conn->client_hello.random, client_random_in.data, client_random_in.size);
275 EXPECT_MEMCPY_SUCCESS(conn->handshake_params.server_random, server_random_in.data, server_random_in.size);
276
277 struct s2n_blob pms = { 0 };
278 EXPECT_SUCCESS(s2n_blob_init(&pms, conn->secrets.version.tls12.rsa_premaster_secret, sizeof(conn->secrets.version.tls12.rsa_premaster_secret)));
279 EXPECT_SUCCESS(s2n_prf_tls_master_secret(conn, &pms));
280 EXPECT_EQUAL(memcmp(conn->secrets.version.tls12.master_secret, master_secret_in.data, master_secret_in.size), 0);
281 };
282
283 /* s2n_tls_prf_extended_master_secret */
284 {
285 /* The test premaster secret, hash digest, and resulting
286 * extended master secret were pulled from an OpenSSL TLS1.2 EMS session
287 * using the s2n_ecdhe_ecdsa_with_aes_256_gcm_sha384 ciphersuite.
288 */
289 S2N_BLOB_FROM_HEX(premaster_secret,
290 "05e12675c9264d82b53fa15d589c829af9be1ae3d881ab0b023b7b8cad8bc058");
291
292 S2N_BLOB_FROM_HEX(hash_digest,
293 "e6cbbaa03909ea387714fe70c07546086dedfcee086fd2985dfdd50924393619"
294 "009115758e490e2e3b0c13bebdad5fbb");
295
296 S2N_BLOB_FROM_HEX(extended_master_secret,
297 "aef116e65e2cd77d4e96b1ceeadb7912ddd9aaf3a907aa3344ec3a2de6cc3b69"
298 "9ca768fe389eab3b53c98d8ccd830b06");
299
300 DEFER_CLEANUP(struct s2n_connection *conn = s2n_connection_new(S2N_SERVER),
301 s2n_connection_ptr_free);
302 EXPECT_NOT_NULL(conn);
303 conn->actual_protocol_version = S2N_TLS12;
304 conn->secure->cipher_suite = &s2n_ecdhe_ecdsa_with_aes_256_gcm_sha384;
305
306 /**
307 *= https://www.rfc-editor.org/rfc/rfc7627#section-4
308 *= type=test
309 *# When the extended master secret extension is negotiated in a full
310 *# handshake, the "master_secret" is computed as
311 *#
312 *# master_secret = PRF(pre_master_secret, "extended master secret",
313 *# session_hash)
314 *# [0..47];
315 */
316 EXPECT_OK(s2n_prf_tls_extended_master_secret(conn, &premaster_secret, &hash_digest, NULL));
317 EXPECT_BYTEARRAY_EQUAL(extended_master_secret.data, conn->secrets.version.tls12.master_secret, S2N_TLS_SECRET_LEN);
318 };
319
320 /* s2n_prf_calculate_master_secret */
321 {
322 DEFER_CLEANUP(struct s2n_connection *conn = s2n_connection_new(S2N_SERVER),
323 s2n_connection_ptr_free);
324 EXPECT_NOT_NULL(conn);
325
326 conn->secure->cipher_suite = &s2n_ecdhe_ecdsa_with_aes_256_gcm_sha384;
327
328 EXPECT_MEMCPY_SUCCESS(conn->secrets.version.tls12.rsa_premaster_secret, premaster_secret_in.data, premaster_secret_in.size);
329 EXPECT_MEMCPY_SUCCESS(conn->client_hello.random, client_random_in.data, client_random_in.size);
330 EXPECT_MEMCPY_SUCCESS(conn->handshake_params.server_random, server_random_in.data, server_random_in.size);
331
332 struct s2n_blob pms = { 0 };
333 EXPECT_SUCCESS(s2n_blob_init(&pms, conn->secrets.version.tls12.rsa_premaster_secret, sizeof(conn->secrets.version.tls12.rsa_premaster_secret)));
334
335 /* Errors when handshake is not at the Client Key Exchange message */
336 EXPECT_FAILURE_WITH_ERRNO(s2n_prf_calculate_master_secret(conn, &pms), S2N_ERR_SAFETY);
337
338 /* Advance handshake to Client Key Exchange message */
339 conn->handshake.handshake_type = NEGOTIATED | FULL_HANDSHAKE;
340 while (ACTIVE_MESSAGE(conn) != CLIENT_KEY) {
341 conn->handshake.message_number++;
342 }
343
344 /* Master secret is calculated when handshake is at Client Key Exchange message*/
345 EXPECT_SUCCESS(s2n_prf_calculate_master_secret(conn, &pms));
346 EXPECT_EQUAL(memcmp(conn->secrets.version.tls12.master_secret, master_secret_in.data, master_secret_in.size), 0);
347
348 /* s2n_prf_calculate_master_secret will produce the same master secret if given the same inputs */
349 EXPECT_SUCCESS(s2n_prf_calculate_master_secret(conn, &pms));
350 EXPECT_EQUAL(memcmp(conn->secrets.version.tls12.master_secret, master_secret_in.data, master_secret_in.size), 0);
351
352 conn->ems_negotiated = true;
353 EXPECT_SUCCESS(s2n_prf_calculate_master_secret(conn, &pms));
354
355 /* Extended master secret calculated is different than the master secret calculated */
356 EXPECT_NOT_EQUAL(memcmp(conn->secrets.version.tls12.master_secret, master_secret_in.data, master_secret_in.size), 0);
357 };
358
359 /* s2n_prf_get_digest_for_ems calculates the correct digest to generate an extended master secret.
360 * Here we test that the retrieved digest is the same as the digest after the Client Key Exchange
361 * message is added to the transcript hash.
362 *
363 *= https://www.rfc-editor.org/rfc/rfc7627#section-3
364 *= type=test
365 *# When a full TLS handshake takes place, we define
366 *#
367 *# session_hash = Hash(handshake_messages)
368 *#
369 *# where "handshake_messages" refers to all handshake messages sent or
370 *# received, starting at the ClientHello up to and including the
371 *# ClientKeyExchange message, including the type and length fields of
372 *# the handshake messages.
373 */
374 {
375 struct s2n_cert_chain_and_key *tls12_chain_and_key = NULL;
376 EXPECT_SUCCESS(s2n_test_cert_chain_and_key_new(&tls12_chain_and_key, S2N_DEFAULT_TEST_CERT_CHAIN,
377 S2N_DEFAULT_TEST_PRIVATE_KEY));
378
379 struct s2n_config *config = s2n_config_new();
380 EXPECT_SUCCESS(s2n_config_add_cert_chain_and_key_to_store(config, tls12_chain_and_key));
381 EXPECT_SUCCESS(s2n_config_set_unsafe_for_testing(config));
382 EXPECT_SUCCESS(s2n_config_set_cipher_preferences(config, "20170210"));
383
384 struct s2n_connection *client_conn = s2n_connection_new(S2N_CLIENT);
385 EXPECT_NOT_NULL(client_conn);
386
387 struct s2n_connection *server_conn = s2n_connection_new(S2N_SERVER);
388 EXPECT_NOT_NULL(server_conn);
389
390 EXPECT_SUCCESS(s2n_connection_set_config(client_conn, config));
391 EXPECT_SUCCESS(s2n_connection_set_config(server_conn, config));
392
393 DEFER_CLEANUP(struct s2n_stuffer client_to_server = { 0 }, s2n_stuffer_free);
394 DEFER_CLEANUP(struct s2n_stuffer server_to_client = { 0 }, s2n_stuffer_free);
395 EXPECT_SUCCESS(s2n_stuffer_growable_alloc(&client_to_server, 0));
396 EXPECT_SUCCESS(s2n_stuffer_growable_alloc(&server_to_client, 0));
397 EXPECT_SUCCESS(s2n_connection_set_io_stuffers(&server_to_client, &client_to_server, client_conn));
398 EXPECT_SUCCESS(s2n_connection_set_io_stuffers(&client_to_server, &server_to_client, server_conn));
399
400 EXPECT_OK(s2n_negotiate_test_server_and_client_until_message(server_conn, client_conn, CLIENT_KEY));
401
402 /* Client writes Client Key Exchange message */
403 EXPECT_SUCCESS(s2n_handshake_write_io(client_conn));
404
405 uint8_t data[S2N_MAX_DIGEST_LEN] = { 0 };
406 struct s2n_blob digest_for_ems = { 0 };
407 EXPECT_SUCCESS(s2n_blob_init(&digest_for_ems, data, sizeof(data)));
408
409 /* Get the Client Key transcript */
410 EXPECT_SUCCESS(s2n_stuffer_skip_read(&client_to_server, S2N_TLS_RECORD_HEADER_LENGTH));
411 uint8_t client_key_message_length = s2n_stuffer_data_available(&client_to_server);
412 uint8_t *client_key_message = s2n_stuffer_raw_read(&client_to_server, client_key_message_length);
413 struct s2n_blob message = { 0 };
414 EXPECT_SUCCESS(s2n_blob_init(&message, client_key_message, client_key_message_length));
415
416 s2n_hmac_algorithm prf_alg = server_conn->secure->cipher_suite->prf_alg;
417 s2n_hash_algorithm hash_alg = 0;
418 POSIX_GUARD(s2n_hmac_hash_alg(prf_alg, &hash_alg));
419 EXPECT_OK(s2n_prf_get_digest_for_ems(server_conn, &message, hash_alg, &digest_for_ems));
420
421 /* Server reads Client Key Exchange message */
422 EXPECT_SUCCESS(s2n_stuffer_rewind_read(&client_to_server, S2N_TLS_RECORD_HEADER_LENGTH + client_key_message_length));
423 EXPECT_SUCCESS(s2n_handshake_read_io(server_conn));
424
425 /* Calculate the digest message after the Server read the Client Key message */
426 DEFER_CLEANUP(struct s2n_hash_state current_hash_state = { 0 }, s2n_hash_free);
427 uint8_t server_digest[S2N_MAX_DIGEST_LEN] = { 0 };
428 uint8_t digest_size = 0;
429 EXPECT_SUCCESS(s2n_hash_digest_size(hash_alg, &digest_size));
430 EXPECT_SUCCESS(s2n_hash_new(&current_hash_state));
431 EXPECT_OK(s2n_handshake_copy_hash_state(server_conn, hash_alg, &current_hash_state));
432 EXPECT_SUCCESS(s2n_hash_digest(&current_hash_state, server_digest, digest_size));
433
434 /* Digest for generating the EMS and digest after reading the Client Key message
435 * should be the same. */
436 EXPECT_BYTEARRAY_EQUAL(server_digest, digest_for_ems.data, digest_size);
437
438 EXPECT_SUCCESS(s2n_connection_free(client_conn));
439 EXPECT_SUCCESS(s2n_connection_free(server_conn));
440
441 EXPECT_SUCCESS(s2n_config_free(config));
442 EXPECT_SUCCESS(s2n_cert_chain_and_key_free(tls12_chain_and_key));
443 }
444
445 /* PRF lifecyle */
446 {
447 /* Safety */
448 {
449 EXPECT_ERROR_WITH_ERRNO(s2n_prf_new(NULL), S2N_ERR_NULL);
450 EXPECT_ERROR_WITH_ERRNO(s2n_prf_wipe(NULL), S2N_ERR_NULL);
451 EXPECT_ERROR_WITH_ERRNO(s2n_prf_free(NULL), S2N_ERR_NULL);
452
453 struct s2n_connection conn_with_null_prf_space = { 0 };
454 EXPECT_NULL(conn_with_null_prf_space.prf_space);
455
456 EXPECT_ERROR_WITH_ERRNO(s2n_prf_wipe(&conn_with_null_prf_space), S2N_ERR_NULL);
457 EXPECT_OK(s2n_prf_free(&conn_with_null_prf_space));
458 };
459
460 /* Basic lifecyle */
461 {
462 struct s2n_connection connection = { 0 };
463 EXPECT_NULL(connection.prf_space);
464
465 EXPECT_OK(s2n_prf_new(&connection));
466 EXPECT_NOT_NULL(connection.prf_space);
467
468 EXPECT_OK(s2n_prf_wipe(&connection));
469 EXPECT_NOT_NULL(connection.prf_space);
470
471 EXPECT_OK(s2n_prf_free(&connection));
472 EXPECT_NULL(connection.prf_space);
473 };
474
475 /* PRF freed by s2n_connection_free_handshake */
476 {
477 DEFER_CLEANUP(struct s2n_connection *conn = s2n_connection_new(S2N_SERVER),
478 s2n_connection_ptr_free);
479 EXPECT_NOT_NULL(conn);
480 EXPECT_NOT_NULL(conn->prf_space);
481
482 EXPECT_SUCCESS(s2n_connection_free_handshake(conn));
483 EXPECT_NULL(conn->prf_space);
484 };
485
486 /* Freed PRF restored by s2n_connection_wipe */
487 {
488 DEFER_CLEANUP(struct s2n_connection *conn = s2n_connection_new(S2N_SERVER),
489 s2n_connection_ptr_free);
490 EXPECT_NOT_NULL(conn);
491 EXPECT_OK(s2n_prf_free(conn));
492 EXPECT_NULL(conn->prf_space);
493
494 EXPECT_SUCCESS(s2n_connection_wipe(conn));
495 EXPECT_NOT_NULL(conn->prf_space);
496 };
497
498 /* PRF usable throughout connection lifecycle */
499 {
500 struct s2n_blob pms = { 0 };
501 EXPECT_SUCCESS(s2n_blob_init(&pms, premaster_secret_in.data, premaster_secret_in.size));
502
503 DEFER_CLEANUP(struct s2n_connection *conn = s2n_connection_new(S2N_SERVER),
504 s2n_connection_ptr_free);
505 EXPECT_NOT_NULL(conn);
506 EXPECT_MEMCPY_SUCCESS(conn->secrets.version.tls12.rsa_premaster_secret, premaster_secret_in.data, premaster_secret_in.size);
507 EXPECT_MEMCPY_SUCCESS(conn->client_hello.random, client_random_in.data, client_random_in.size);
508 EXPECT_MEMCPY_SUCCESS(conn->handshake_params.server_random, server_random_in.data, server_random_in.size);
509 EXPECT_SUCCESS(s2n_prf_tls_master_secret(conn, &pms));
510 EXPECT_EQUAL(memcmp(conn->secrets.version.tls12.master_secret, master_secret_in.data, master_secret_in.size), 0);
511
512 EXPECT_SUCCESS(s2n_connection_free_handshake(conn));
513 EXPECT_MEMCPY_SUCCESS(conn->secrets.version.tls12.rsa_premaster_secret, premaster_secret_in.data, premaster_secret_in.size);
514 EXPECT_MEMCPY_SUCCESS(conn->client_hello.random, client_random_in.data, client_random_in.size);
515 EXPECT_MEMCPY_SUCCESS(conn->handshake_params.server_random, server_random_in.data, server_random_in.size);
516 EXPECT_FAILURE_WITH_ERRNO(s2n_prf_tls_master_secret(conn, &pms), S2N_ERR_NULL);
517
518 EXPECT_SUCCESS(s2n_connection_wipe(conn));
519 EXPECT_MEMCPY_SUCCESS(conn->secrets.version.tls12.rsa_premaster_secret, premaster_secret_in.data, premaster_secret_in.size);
520 EXPECT_MEMCPY_SUCCESS(conn->client_hello.random, client_random_in.data, client_random_in.size);
521 EXPECT_MEMCPY_SUCCESS(conn->handshake_params.server_random, server_random_in.data, server_random_in.size);
522 EXPECT_SUCCESS(s2n_prf_tls_master_secret(conn, &pms));
523 EXPECT_EQUAL(memcmp(conn->secrets.version.tls12.master_secret, master_secret_in.data, master_secret_in.size), 0);
524 };
525 };
526
527 END_TEST();
528 }