Debian Codegraph @

crt/s2n/tests/unit/s2n_tls13_record_aead_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 "s2n_test.h"
21 #include "stuffer/s2n_stuffer.h"
22 #include "testlib/s2n_testlib.h"
23 #include "tls/s2n_cipher_suites.h"
24 #include "tls/s2n_record.h"
25 #include "tls/s2n_record_read.h"
26 #include "utils/s2n_safety.h"
27
28 const char protected_record_hex[] = "d1ff334a56f5bf"
29 "f6594a07cc87b580233f500f45e489e7f33af35edf"
30 "7869fcf40aa40aa2b8ea73f848a7ca07612ef9f945"
31 "cb960b4068905123ea78b111b429ba9191cd05d2a3"
32 "89280f526134aadc7fc78c4b729df828b5ecf7b13b"
33 "d9aefb0e57f271585b8ea9bb355c7c79020716cfb9"
34 "b1183ef3ab20e37d57a6b9d7477609aee6e122a4cf"
35 "51427325250c7d0e509289444c9b3a648f1d71035d"
36 "2ed65b0e3cdd0cbae8bf2d0b227812cbb360987255"
37 "cc744110c453baa4fcd610928d809810e4b7ed1a8f"
38 "d991f06aa6248204797e36a6a73b70a2559c09ead6"
39 "86945ba246ab66e5edd8044b4c6de3fcf2a89441ac"
40 "66272fd8fb330ef8190579b3684596c960bd596eea"
41 "520a56a8d650f563aad27409960dca63d3e688611e"
42 "a5e22f4415cf9538d51a200c27034272968a264ed6"
43 "540c84838d89f72c24461aad6d26f59ecaba9acbbb"
44 "317b66d902f4f292a36ac1b639c637ce343117b659"
45 "622245317b49eeda0c6258f100d7d961ffb138647e"
46 "92ea330faeea6dfa31c7a84dc3bd7e1b7a6c7178af"
47 "36879018e3f252107f243d243dc7339d5684c8b037"
48 "8bf30244da8c87c843f5e56eb4c5e8280a2b48052c"
49 "f93b16499a66db7cca71e4599426f7d461e66f9988"
50 "2bd89fc50800becca62d6c74116dbd2972fda1fa80"
51 "f85df881edbe5a37668936b335583b599186dc5c69"
52 "18a396fa48a181d6b6fa4f9d62d513afbb992f2b99"
53 "2f67f8afe67f76913fa388cb5630c8ca01e0c65d11"
54 "c66a1e2ac4c85977b7c7a6999bbf10dc35ae69f551"
55 "5614636c0b9b68c19ed2e31c0b3b66763038ebba42"
56 "f3b38edc0399f3a9f23faa63978c317fc9fa66a73f"
57 "60f0504de93b5b845e275592c12335ee340bbc4fdd"
58 "d502784016e4b3be7ef04dda49f4b440a30cb5d2af"
59 "939828fd4ae3794e44f94df5a631ede42c1719bfda"
60 "bf0253fe5175be898e750edc53370d2b"; /* includes tag */
61
62 const char plaintext_record_hex[] =
63 "080000240022000a00140012001d"
64 "00170018001901000101010201030104001c000240"
65 "01000000000b0001b9000001b50001b0308201ac30"
66 "820115a003020102020102300d06092a864886f70d"
67 "01010b0500300e310c300a06035504031303727361"
68 "301e170d3136303733303031323335395a170d3236"
69 "303733303031323335395a300e310c300a06035504"
70 "03130372736130819f300d06092a864886f70d0101"
71 "01050003818d0030818902818100b4bb498f827930"
72 "3d980836399b36c6988c0c68de55e1bdb826d3901a"
73 "2461eafd2de49a91d015abbc9a95137ace6c1af19e"
74 "aa6af98c7ced43120998e187a80ee0ccb0524b1b01"
75 "8c3e0b63264d449a6d38e22a5fda43084674803053"
76 "0ef0461c8ca9d9efbfae8ea6d1d03e2bd193eff0ab"
77 "9a8002c47428a6d35a8d88d79f7f1e3f0203010001"
78 "a31a301830090603551d1304023000300b0603551d"
79 "0f0404030205a0300d06092a864886f70d01010b05"
80 "000381810085aad2a0e5b9276b908c65f73a726717"
81 "0618a54c5f8a7b337d2df7a594365417f2eae8f8a5"
82 "8c8f8172f9319cf36b7fd6c55b80f21a0301515672"
83 "6096fd335e5e67f2dbf102702e608ccae6bec1fc63"
84 "a42a99be5c3eb7107c3c54e9b9eb2bd5203b1c3b84"
85 "e0a8b2f759409ba3eac9d91d402dcc0cc8f8961229"
86 "ac9187b42b4de100000f000084080400805a747c5d"
87 "88fa9bd2e55ab085a61015b7211f824cd484145ab3"
88 "ff52f1fda8477b0b7abc90db78e2d33a5c141a0786"
89 "53fa6bef780c5ea248eeaaa785c4f394cab6d30bbe"
90 "8d4859ee511f602957b15411ac027671459e46445c"
91 "9ea58c181e818e95b8c3fb0bf3278409d3be152a3d"
92 "a5043e063dda65cdf5aea20d53dfacd42f74f31400"
93 "00209b9b141d906337fbd2cbdce71df4deda4ab42c"
94 "309572cb7fffee5454b78f071816"; /* includes last byte for content type */
95
96 int main(int argc, char **argv)
97 {
98 BEGIN_TEST();
99 EXPECT_SUCCESS(s2n_disable_tls13_in_test());
100
101 /* Test s2n_tls13_aead_aad_init() */
102 {
103 s2n_stack_blob(aad, S2N_TLS13_AAD_LEN, S2N_TLS13_AAD_LEN);
104 EXPECT_OK(s2n_tls13_aead_aad_init(662, 12, &aad));
105 S2N_BLOB_FROM_HEX(expected_aad, "17030302a2");
106 S2N_BLOB_EXPECT_EQUAL(expected_aad, aad);
107
108 /* record length 16640 should be valid */
109 EXPECT_SUCCESS(s2n_blob_zero(&aad));
110 EXPECT_OK(s2n_tls13_aead_aad_init(16628, 12, &aad));
111
112 /* record length 16641 should be invalid */
113 EXPECT_SUCCESS(s2n_blob_zero(&aad));
114 EXPECT_ERROR_WITH_ERRNO(s2n_tls13_aead_aad_init(16629, 12, &aad), S2N_ERR_RECORD_LIMIT);
115
116 /* Test failure case: No AAD should be invalid */
117 EXPECT_SUCCESS(s2n_blob_zero(&aad));
118 EXPECT_ERROR(s2n_tls13_aead_aad_init(16629, 12, NULL));
119
120 /* Test failure case: 0-length tag should be invalid */
121 EXPECT_SUCCESS(s2n_blob_zero(&aad));
122 EXPECT_ERROR(s2n_tls13_aead_aad_init(16628, 0, &aad));
123
124 /* Test failure case: invalid record length (-1) should be invalid */
125 EXPECT_ERROR(s2n_tls13_aead_aad_init(-1, 0, &aad));
126 }
127
128 /* Test s2n_tls13_aes_128_gcm_sha256 cipher suite with TLS 1.3 test vectors */
129 {
130 struct s2n_connection *conn = NULL;
131 struct s2n_session_key session_key = { 0 };
132 EXPECT_SUCCESS(s2n_session_key_alloc(&session_key));
133
134 struct s2n_cipher_suite *cipher_suite = &s2n_tls13_aes_128_gcm_sha256;
135 EXPECT_NOT_NULL(conn = s2n_connection_new(S2N_SERVER));
136 conn->actual_protocol_version = S2N_TLS13;
137
138 /* init record algorithm */
139 EXPECT_OK(cipher_suite->record_alg->cipher->init(&session_key));
140 S2N_BLOB_FROM_HEX(key, "3fce516009c21727d0f2e4e86ee403bc");
141 EXPECT_OK(cipher_suite->record_alg->cipher->set_decryption_key(&session_key, &key));
142
143 /* write protected record to conn in for testing */
144 S2N_BLOB_FROM_HEX(protected_record, protected_record_hex);
145 EXPECT_SUCCESS(s2n_stuffer_write(&conn->in, &protected_record));
146
147 S2N_BLOB_FROM_HEX(iv, "5d313eb2671276ee13000b30");
148
149 /* Test parsing of tls 1.3 aead record */
150 EXPECT_SUCCESS(s2n_record_parse_aead(
151 cipher_suite,
152 conn,
153 0, /* content_type doesn't matter for TLS 1.3 */
154 protected_record.size,
155 iv.data, /* implicit_iv */
156 NULL, /* mac not used for TLS 1.3 */
157 conn->secure->client_sequence_number,
158 &session_key));
159
160 S2N_BLOB_FROM_HEX(plaintext_record, plaintext_record_hex);
161
162 /* Because the decrypted payload contains both plaintext and tag,
163 * we copy the contents for verification.
164 */
165 s2n_stack_blob(decrypted, plaintext_record.size, 1000);
166 struct s2n_stuffer decrypted_stuffer = { 0 };
167 EXPECT_SUCCESS(s2n_stuffer_init(&decrypted_stuffer, &decrypted));
168
169 EXPECT_SUCCESS(s2n_stuffer_write_bytes(&decrypted_stuffer, conn->in.blob.data, plaintext_record.size));
170 S2N_BLOB_EXPECT_EQUAL(plaintext_record, decrypted_stuffer.blob);
171
172 #define RESET_TEST \
173 /* wipe conn in stuffer and refill protected record */ \
174 EXPECT_SUCCESS(s2n_stuffer_wipe(&conn->in)); \
175 EXPECT_SUCCESS(s2n_stuffer_write(&conn->in, &protected_record)); \
176 /* reset sequence number */ \
177 conn->secure->client_sequence_number[7] = 0;
178
179 /* Repeat the test to prove RESET_TEST works */
180 RESET_TEST
181 EXPECT_SUCCESS(s2n_record_parse_aead(cipher_suite, conn, 0, protected_record.size,
182 iv.data, NULL, conn->secure->client_sequence_number, &session_key));
183
184 /* Test record parsing failure from aead tag change */
185 RESET_TEST
186 conn->in.blob.data[protected_record.size - 2]++;
187 EXPECT_FAILURE(s2n_record_parse_aead(cipher_suite, conn, 0, protected_record.size,
188 iv.data, NULL, conn->secure->client_sequence_number, &session_key));
189
190 /* Test incorrect ciphertext changes fails parsing */
191 RESET_TEST
192 conn->in.blob.data[0]++;
193 EXPECT_FAILURE(s2n_record_parse_aead(cipher_suite, conn, 0, protected_record.size,
194 iv.data, NULL, conn->secure->client_sequence_number, &session_key));
195
196 /* Test wrong sequence number fails parsing */
197 RESET_TEST
198 conn->secure->client_sequence_number[7] = 1;
199 EXPECT_FAILURE(s2n_record_parse_aead(cipher_suite, conn, 0, protected_record.size,
200 iv.data, NULL, conn->secure->client_sequence_number, &session_key));
201
202 /* Test IV changes fails parsing */
203 RESET_TEST
204 iv.data[0]++;
205 EXPECT_FAILURE(s2n_record_parse_aead(cipher_suite, conn, 0, protected_record.size,
206 iv.data, NULL, conn->secure->client_sequence_number, &session_key));
207
208 EXPECT_SUCCESS(s2n_connection_free(conn));
209 EXPECT_SUCCESS(s2n_session_key_free(&session_key));
210 };
211
212 /* Test s2n_tls13_aes_128_gcm_sha256 cipher suite ENCRYPTION with TLS 1.3 test vectors */
213 {
214 struct s2n_connection *conn = NULL;
215 struct s2n_cipher_suite *cipher_suite = &s2n_tls13_aes_128_gcm_sha256;
216 EXPECT_NOT_NULL(conn = s2n_connection_new(S2N_SERVER));
217 conn->actual_protocol_version = S2N_TLS13;
218
219 conn->server->cipher_suite = cipher_suite;
220 struct s2n_session_key *session_key = &conn->server->server_key;
221
222 uint8_t *implicit_iv = conn->server->server_implicit_iv;
223
224 /* init record algorithm */
225 EXPECT_OK(cipher_suite->record_alg->cipher->init(session_key));
226 S2N_BLOB_FROM_HEX(key, "3fce516009c21727d0f2e4e86ee403bc");
227 EXPECT_OK(cipher_suite->record_alg->cipher->set_encryption_key(session_key, &key));
228
229 S2N_BLOB_FROM_HEX(protected_record, protected_record_hex);
230
231 S2N_BLOB_FROM_HEX(iv, "5d313eb2671276ee13000b30");
232
233 /* copy iv bytes from input data */
234 for (size_t i = 0; i < iv.size; i++) {
235 implicit_iv[i] = iv.data[i];
236 }
237
238 /* Test parsing of tls 1.3 aead record */
239 S2N_BLOB_FROM_HEX(plaintext_record, plaintext_record_hex);
240
241 /* Make plaintext blob slice */
242 struct s2n_blob in = {
243 .data = &plaintext_record.data[0],
244 .size = plaintext_record.size - 1, /* 1 byte less to remove content type */
245 };
246
247 /* Takes an input blob and writes to out stuffer then encrypt the payload */
248 EXPECT_OK(s2n_record_write(conn, TLS_HANDSHAKE, &in));
249
250 /* Verify opaque content type in tls 1.3 */
251 EXPECT_EQUAL(conn->out.blob.data[0], TLS_APPLICATION_DATA);
252 /* Verify TLS legacy record version */
253 EXPECT_EQUAL(conn->out.blob.data[1], 3);
254 EXPECT_EQUAL(conn->out.blob.data[2], 3);
255 /* Verify payload length */
256 EXPECT_EQUAL((conn->out.blob.data[3] << 8) + conn->out.blob.data[4], protected_record.size);
257
258 /* Make a slice of output bytes to verify */
259 struct s2n_blob out = {
260 .data = &conn->out.blob.data[S2N_TLS13_AAD_LEN],
261 .size = protected_record.size
262 };
263
264 S2N_BLOB_EXPECT_EQUAL(out, protected_record);
265
266 EXPECT_SUCCESS(s2n_connection_free(conn));
267 };
268
269 /* Test encrypt-decrypt roundtrip */
270 {
271 struct s2n_connection *conn = NULL;
272 struct s2n_cipher_suite *cipher_suite = &s2n_tls13_aes_128_gcm_sha256;
273 EXPECT_NOT_NULL(conn = s2n_connection_new(S2N_SERVER));
274 conn->actual_protocol_version = S2N_TLS13;
275
276 conn->server->cipher_suite = cipher_suite;
277 struct s2n_session_key *session_key = &conn->server->server_key;
278
279 uint8_t *implicit_iv = conn->server->server_implicit_iv;
280
281 /* init record algorithm */
282 EXPECT_OK(cipher_suite->record_alg->cipher->init(session_key));
283 S2N_BLOB_FROM_HEX(key, "3fce516009c21727d0f2e4e86ee403bc");
284 EXPECT_OK(cipher_suite->record_alg->cipher->set_encryption_key(session_key, &key));
285 EXPECT_OK(cipher_suite->record_alg->cipher->set_decryption_key(session_key, &key));
286
287 S2N_BLOB_FROM_HEX(iv, "5d313eb2671276ee13000b30");
288
289 /* copy iv bytes from input data */
290 for (size_t i = 0; i < iv.size; i++) {
291 implicit_iv[i] = iv.data[i];
292 }
293
294 /* Test parsing of tls 1.3 aead record */
295 S2N_BLOB_LABEL(expect_plaintext, "Hello world");
296
297 static uint8_t hello_data[] = "Hello world";
298 struct s2n_blob plaintext = { 0 };
299 EXPECT_SUCCESS(s2n_blob_init(&plaintext, hello_data, sizeof(hello_data) - 1));
300
301 /* Takes an input blob and writes to out stuffer then encrypt the payload */
302 EXPECT_OK(s2n_record_write(conn, TLS_HANDSHAKE, &plaintext));
303
304 /* Reset sequence number */
305 conn->secure->client_sequence_number[7] = 0;
306
307 EXPECT_SUCCESS(s2n_stuffer_write_bytes(&conn->in, &conn->out.blob.data[S2N_TLS13_AAD_LEN], plaintext.size + 16 + 1)); /* tag length + content type */
308
309 /* Make a slice of output bytes to verify */
310 struct s2n_blob encrypted = {
311 .data = &conn->in.blob.data[0],
312 .size = plaintext.size + 16 + 1
313 };
314
315 /* Decrypt payload */
316 EXPECT_SUCCESS(s2n_record_parse_aead(
317 cipher_suite,
318 conn,
319 0, /* content_type */
320 encrypted.size,
321 iv.data, /* implicit_iv */
322 NULL, /* mac not used for TLS 1.3 */
323 conn->secure->client_sequence_number,
324 session_key));
325
326 struct s2n_blob decrypted = {
327 .data = &conn->in.blob.data[0],
328 .size = expect_plaintext.size
329 };
330
331 /* Verify decrypted payload */
332 S2N_BLOB_EXPECT_EQUAL(decrypted, expect_plaintext);
333
334 EXPECT_SUCCESS(s2n_connection_free(conn));
335 };
336
337 /* Test that CCS in TLS 1.3 modes should be sent without encryption */
338 {
339 s2n_mode modes[] = { S2N_SERVER, S2N_CLIENT };
340 for (size_t m = 0; m < s2n_array_len(modes); m++) {
341 struct s2n_connection *conn = NULL;
342 struct s2n_cipher_suite *cipher_suite = &s2n_tls13_aes_128_gcm_sha256;
343 EXPECT_NOT_NULL(conn = s2n_connection_new(modes[m]));
344 conn->actual_protocol_version = S2N_TLS13;
345 conn->server_protocol_version = S2N_TLS13;
346 conn->secure->cipher_suite = cipher_suite;
347 conn->server = conn->secure;
348 conn->client = conn->secure;
349
350 /* init record algorithm */
351 EXPECT_OK(cipher_suite->record_alg->cipher->init(&conn->secure->server_key));
352 EXPECT_OK(cipher_suite->record_alg->cipher->init(&conn->secure->client_key));
353 S2N_BLOB_FROM_HEX(key, "3fce516009c21727d0f2e4e86ee403bc");
354 EXPECT_OK(cipher_suite->record_alg->cipher->set_encryption_key(&conn->secure->server_key, &key));
355 EXPECT_OK(cipher_suite->record_alg->cipher->set_decryption_key(&conn->secure->client_key, &key));
356
357 S2N_BLOB_FROM_HEX(protected_record, protected_record_hex);
358 S2N_BLOB_FROM_HEX(iv, "5d313eb2671276ee13000b30");
359
360 /* copy iv bytes from input data */
361 for (size_t i = 0; i < iv.size; i++) {
362 conn->secure->server_implicit_iv[i] = iv.data[i];
363 conn->secure->client_implicit_iv[i] = iv.data[i];
364 }
365
366 /* Test parsing of tls 1.3 aead record */
367 S2N_BLOB_FROM_HEX(plaintext_record, plaintext_record_hex);
368
369 uint8_t change_cipher_spec[] = { 1 };
370 struct s2n_blob in = { .data = change_cipher_spec, .size = sizeof(change_cipher_spec) };
371
372 /* Takes an input blob and writes to out stuffer then encrypt the payload */
373 EXPECT_OK(s2n_record_write(conn, TLS_CHANGE_CIPHER_SPEC, &in));
374
375 S2N_STUFFER_READ_EXPECT_EQUAL(&conn->out, TLS_CHANGE_CIPHER_SPEC, uint8);
376 S2N_STUFFER_READ_EXPECT_EQUAL(&conn->out, 0x0303, uint16);
377 S2N_STUFFER_READ_EXPECT_EQUAL(&conn->out, 1, uint16);
378 S2N_STUFFER_READ_EXPECT_EQUAL(&conn->out, 1, uint8);
379
380 EXPECT_SUCCESS(s2n_stuffer_wipe(&conn->out));
381
382 /* An encrypted TLS 1.3 HANDSHAKE type will look like a APPLICATION_DATA over the wire */
383 EXPECT_OK(s2n_record_write(conn, TLS_HANDSHAKE, &in));
384
385 /* now test that application data writes encrypted payload */
386 S2N_STUFFER_READ_EXPECT_EQUAL(&conn->out, TLS_APPLICATION_DATA, uint8);
387 S2N_STUFFER_READ_EXPECT_EQUAL(&conn->out, 0x0303, uint16);
388 S2N_STUFFER_READ_EXPECT_EQUAL(&conn->out, 18, uint16);
389 S2N_STUFFER_READ_EXPECT_EQUAL(&conn->out, 216, uint8);
390
391 EXPECT_SUCCESS(s2n_stuffer_write_uint8(&conn->header_in, TLS_CHANGE_CIPHER_SPEC));
392 EXPECT_SUCCESS(s2n_stuffer_write_uint16(&conn->header_in, 0x0303));
393 EXPECT_SUCCESS(s2n_stuffer_write_uint16(&conn->header_in, 1));
394 EXPECT_SUCCESS(s2n_stuffer_write_uint8(&conn->in, 1));
395
396 /* Parses unencrypted CCS record correctly */
397 EXPECT_SUCCESS(s2n_record_parse(conn));
398
399 /* now if this was an application data, it cannot be parsed */
400 EXPECT_SUCCESS(s2n_stuffer_wipe(&conn->header_in));
401 EXPECT_SUCCESS(s2n_stuffer_wipe(&conn->in));
402 EXPECT_SUCCESS(s2n_stuffer_write_uint8(&conn->header_in, TLS_APPLICATION_DATA));
403 EXPECT_SUCCESS(s2n_stuffer_write_uint16(&conn->header_in, 0x0303));
404 EXPECT_SUCCESS(s2n_stuffer_write_uint16(&conn->header_in, 1));
405 EXPECT_SUCCESS(s2n_stuffer_write_uint8(&conn->in, 1));
406 EXPECT_FAILURE(s2n_record_parse(conn));
407
408 EXPECT_SUCCESS(s2n_connection_free(conn));
409 }
410 };
411
412 END_TEST();
413 }