Debian Codegraph @

crt/s2n/tests/unit/s2n_fingerprint_ja4_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 "api/unstable/fingerprint.h"
17 #include "s2n_test.h"
18 #include "testlib/s2n_testlib.h"
19 #include "tls/s2n_tls.h"
20
21 #define S2N_TEST_OUTPUT_SIZE 200
22
23 #define S2N_TEST_CLIENT_HELLO_VERSION \
24 0x00, 0x00
25
26 /* clang-format off */
27 #define S2N_TEST_CLIENT_HELLO_AFTER_VERSION \
28 /* random */ \
29 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, \
30 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, \
31 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, \
32 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, \
33 /* session id */ \
34 0x00
35 /* clang-format on */
36
37 /* clang-format off */
38 #define S2N_TEST_CLIENT_HELLO_CIPHERS \
39 /* cipher suites size */ \
40 0x00, 0x02, \
41 /* cipher suites */ \
42 0x00, 0x00
43 /* clang-format on */
44
45 #define S2N_TEST_CLIENT_HELLO_AFTER_CIPHERS \
46 /* legacy compression methods */ \
47 0x01, 0x00
48
49 #define S2N_TEST_CLIENT_HELLO_EMPTY_EXTENSIONS \
50 0x00, 0x00
51 #define S2N_TEST_CLIENT_HELLO_EMPTY_EXTENSION \
52 0x00, 0x00
53
54 /* This macro currently assumes that the message size is only one byte (<=255). */
55 #define S2N_INIT_CLIENT_HELLO(name, ...) \
56 uint8_t _##name##_message[] = { __VA_ARGS__ }; \
57 EXPECT_TRUE(sizeof(_##name##_message) <= UINT8_MAX); \
58 uint8_t name[] = { \
59 TLS_CLIENT_HELLO, \
60 0x00, 0x00, sizeof(_##name##_message), \
61 __VA_ARGS__ \
62 }
63
64 /* clang-format off */
65 #define S2N_INIT_CLIENT_HELLO_WITH_EXTENSION(name, extension) \
66 S2N_INIT_CLIENT_HELLO(name, \
67 S2N_TEST_CLIENT_HELLO_VERSION, \
68 S2N_TEST_CLIENT_HELLO_AFTER_VERSION, \
69 S2N_TEST_CLIENT_HELLO_CIPHERS, \
70 S2N_TEST_CLIENT_HELLO_AFTER_CIPHERS, \
71 /* extensions size */ \
72 0x00, 0x04, \
73 /* extension */ \
74 0x00, extension, S2N_TEST_CLIENT_HELLO_EMPTY_EXTENSION \
75 )
76 /* clang-format on */
77
78 enum {
79 S2N_JA4_A_PROTOCOL = 0,
80 S2N_JA4_A_VERSION_1,
81 S2N_JA4_A_VERSION_2,
82 S2N_JA4_A_DEST,
83 S2N_JA4_A_CIPHER_COUNT_1,
84 S2N_JA4_A_CIPHER_COUNT_2,
85 S2N_JA4_A_EXT_COUNT_1,
86 S2N_JA4_A_EXT_COUNT_2,
87 S2N_JA4_A_ALPN_FIRST,
88 S2N_JA4_A_ALPN_LAST,
89 S2N_JA4_A_SIZE
90 } s2n_ja4_a_fields;
91 #define S2N_JA4_B_START (S2N_JA4_A_SIZE + 1)
92 #define S2N_JA4_C_HASH_START (S2N_JA4_B_START + 12 + 1)
93 /* This assumes a single ciphers suite / use of S2N_TEST_CLIENT_HELLO_CIPHERS */
94 #define S2N_JA4_C_RAW_START (S2N_JA4_B_START + 4 + 1)
95
96 static S2N_RESULT s2n_test_ja4_hash_from_bytes(
97 uint8_t *client_hello_bytes, size_t client_hello_bytes_size,
98 size_t max_hash_size, uint8_t *hash, uint32_t *hash_size)
99 {
100 DEFER_CLEANUP(struct s2n_client_hello *client_hello = NULL, s2n_client_hello_free);
101 client_hello = s2n_client_hello_parse_message(client_hello_bytes, client_hello_bytes_size);
102 RESULT_GUARD_PTR(client_hello);
103
104 DEFER_CLEANUP(struct s2n_fingerprint *fingerprint = NULL, s2n_fingerprint_free);
105 fingerprint = s2n_fingerprint_new(S2N_FINGERPRINT_JA4);
106 RESULT_GUARD_PTR(fingerprint);
107 RESULT_GUARD_POSIX(s2n_fingerprint_set_client_hello(fingerprint, client_hello));
108
109 RESULT_GUARD_POSIX(s2n_fingerprint_get_hash(fingerprint,
110 max_hash_size, hash, hash_size));
111 return S2N_RESULT_OK;
112 }
113
114 static S2N_RESULT s2n_test_ja4_raw_from_bytes(
115 uint8_t *client_hello_bytes, size_t client_hello_bytes_size,
116 size_t max_output_size, uint8_t *output, uint32_t *output_size)
117 {
118 DEFER_CLEANUP(struct s2n_client_hello *client_hello = NULL, s2n_client_hello_free);
119 client_hello = s2n_client_hello_parse_message(client_hello_bytes, client_hello_bytes_size);
120 RESULT_GUARD_PTR(client_hello);
121
122 DEFER_CLEANUP(struct s2n_fingerprint *fingerprint = NULL, s2n_fingerprint_free);
123 fingerprint = s2n_fingerprint_new(S2N_FINGERPRINT_JA4);
124 RESULT_GUARD_PTR(fingerprint);
125 RESULT_GUARD_POSIX(s2n_fingerprint_set_client_hello(fingerprint, client_hello));
126
127 RESULT_GUARD_POSIX(s2n_fingerprint_get_raw(fingerprint,
128 max_output_size, output, output_size));
129 return S2N_RESULT_OK;
130 }
131
132 static S2N_RESULT s2n_test_ja4_hash_from_cipher_count(uint16_t cipher_count,
133 size_t max_hash_size, uint8_t *hash, uint32_t *hash_size)
134 {
135 DEFER_CLEANUP(struct s2n_stuffer bytes = { 0 }, s2n_stuffer_free);
136 RESULT_GUARD_POSIX(s2n_stuffer_growable_alloc(&bytes, 100));
137
138 struct s2n_stuffer_reservation message_size = { 0 };
139 RESULT_GUARD_POSIX(s2n_stuffer_write_uint8(&bytes, TLS_CLIENT_HELLO));
140 RESULT_GUARD_POSIX(s2n_stuffer_reserve_uint24(&bytes, &message_size));
141
142 uint8_t before_ciphers[] = {
143 S2N_TEST_CLIENT_HELLO_VERSION,
144 S2N_TEST_CLIENT_HELLO_AFTER_VERSION,
145 };
146 RESULT_GUARD_POSIX(s2n_stuffer_write_bytes(&bytes, before_ciphers, sizeof(before_ciphers)));
147
148 size_t ciphers_size = cipher_count * sizeof(uint16_t);
149 RESULT_GUARD_POSIX(s2n_stuffer_write_uint16(&bytes, ciphers_size));
150 for (size_t i = 0; i < cipher_count; i++) {
151 RESULT_GUARD_POSIX(s2n_stuffer_write_uint16(&bytes, 0));
152 }
153
154 uint8_t after_ciphers[] = {
155 S2N_TEST_CLIENT_HELLO_AFTER_CIPHERS,
156 S2N_TEST_CLIENT_HELLO_EMPTY_EXTENSIONS,
157 };
158 RESULT_GUARD_POSIX(s2n_stuffer_write_bytes(&bytes, after_ciphers, sizeof(after_ciphers)));
159 RESULT_GUARD_POSIX(s2n_stuffer_write_vector_size(&message_size));
160
161 size_t bytes_size = s2n_stuffer_data_available(&bytes);
162 uint8_t *bytes_ptr = s2n_stuffer_raw_read(&bytes, bytes_size);
163 RESULT_GUARD_PTR(bytes_ptr);
164
165 DEFER_CLEANUP(struct s2n_client_hello *client_hello = NULL, s2n_client_hello_free);
166 client_hello = s2n_client_hello_parse_message(bytes_ptr, bytes_size);
167 RESULT_GUARD_PTR(client_hello);
168
169 DEFER_CLEANUP(struct s2n_fingerprint *fingerprint = NULL, s2n_fingerprint_free);
170 fingerprint = s2n_fingerprint_new(S2N_FINGERPRINT_JA4);
171 RESULT_GUARD_PTR(fingerprint);
172 RESULT_GUARD_POSIX(s2n_fingerprint_set_client_hello(fingerprint, client_hello));
173
174 RESULT_GUARD_POSIX(s2n_fingerprint_get_hash(fingerprint,
175 max_hash_size, hash, hash_size));
176 return S2N_RESULT_OK;
177 }
178
179 static S2N_RESULT s2n_test_ja4_hash_from_extension_count(uint16_t extension_count,
180 size_t max_hash_size, uint8_t *hash, uint32_t *hash_size)
181 {
182 DEFER_CLEANUP(struct s2n_stuffer bytes = { 0 }, s2n_stuffer_free);
183 RESULT_GUARD_POSIX(s2n_stuffer_growable_alloc(&bytes, 100));
184
185 struct s2n_stuffer_reservation message_size = { 0 };
186 RESULT_GUARD_POSIX(s2n_stuffer_write_uint8(&bytes, TLS_CLIENT_HELLO));
187 RESULT_GUARD_POSIX(s2n_stuffer_reserve_uint24(&bytes, &message_size));
188
189 uint8_t before_ciphers[] = {
190 S2N_TEST_CLIENT_HELLO_VERSION,
191 S2N_TEST_CLIENT_HELLO_AFTER_VERSION,
192 S2N_TEST_CLIENT_HELLO_CIPHERS,
193 S2N_TEST_CLIENT_HELLO_AFTER_CIPHERS,
194 };
195 RESULT_GUARD_POSIX(s2n_stuffer_write_bytes(&bytes, before_ciphers, sizeof(before_ciphers)));
196
197 size_t extensions_size = extension_count * 4;
198 RESULT_GUARD_POSIX(s2n_stuffer_write_uint16(&bytes, extensions_size));
199 for (size_t i = 0; i < extension_count; i++) {
200 RESULT_GUARD_POSIX(s2n_stuffer_write_uint16(&bytes, UINT16_MAX - i));
201 RESULT_GUARD_POSIX(s2n_stuffer_write_uint16(&bytes, 0));
202 }
203
204 RESULT_GUARD_POSIX(s2n_stuffer_write_vector_size(&message_size));
205
206 size_t bytes_size = s2n_stuffer_data_available(&bytes);
207 uint8_t *bytes_ptr = s2n_stuffer_raw_read(&bytes, bytes_size);
208 RESULT_GUARD_PTR(bytes_ptr);
209
210 DEFER_CLEANUP(struct s2n_client_hello *client_hello = NULL, s2n_client_hello_free);
211 client_hello = s2n_client_hello_parse_message(bytes_ptr, bytes_size);
212 RESULT_GUARD_PTR(client_hello);
213
214 DEFER_CLEANUP(struct s2n_fingerprint *fingerprint = NULL, s2n_fingerprint_free);
215 fingerprint = s2n_fingerprint_new(S2N_FINGERPRINT_JA4);
216 RESULT_GUARD_PTR(fingerprint);
217 RESULT_GUARD_POSIX(s2n_fingerprint_set_client_hello(fingerprint, client_hello));
218
219 RESULT_GUARD_POSIX(s2n_fingerprint_get_hash(fingerprint,
220 max_hash_size, hash, hash_size));
221 return S2N_RESULT_OK;
222 }
223
224 int main(int argc, char **argv)
225 {
226 BEGIN_TEST();
227
228 S2N_INIT_CLIENT_HELLO(minimal_client_hello_bytes,
229 S2N_TEST_CLIENT_HELLO_VERSION,
230 S2N_TEST_CLIENT_HELLO_AFTER_VERSION,
231 S2N_TEST_CLIENT_HELLO_CIPHERS,
232 S2N_TEST_CLIENT_HELLO_AFTER_CIPHERS,
233 S2N_TEST_CLIENT_HELLO_EMPTY_EXTENSIONS);
234 DEFER_CLEANUP(struct s2n_client_hello *minimal_client_hello = NULL, s2n_client_hello_free);
235 minimal_client_hello = s2n_client_hello_parse_message(
236 minimal_client_hello_bytes, sizeof(minimal_client_hello_bytes));
237 EXPECT_NOT_NULL(minimal_client_hello);
238
239 /* Test JA4_a: prefix
240 *
241 * JA4_a is a plaintext prefix describing the ClientHello, so we can make
242 * specific assertions about its contents.
243 */
244 {
245 /* Test protocol
246 *
247 *= https://raw.githubusercontent.com/FoxIO-LLC/ja4/df3c067/technical_details/JA4.md#quic-and-dtls
248 *= type=test
249 *# If the protocol is QUIC then the first character of the fingerprint
250 *# is “q”, if DTLS it is "d", else it is “t”.
251 */
252 {
253 /* Test QUIC */
254 {
255 S2N_INIT_CLIENT_HELLO_WITH_EXTENSION(client_hello_bytes,
256 TLS_EXTENSION_QUIC_TRANSPORT_PARAMETERS);
257
258 uint8_t output[S2N_TEST_OUTPUT_SIZE] = { 0 };
259 uint32_t output_size = 0;
260 EXPECT_OK(s2n_test_ja4_hash_from_bytes(
261 client_hello_bytes, sizeof(client_hello_bytes),
262 sizeof(output), output, &output_size));
263
264 EXPECT_TRUE(output_size > S2N_JA4_A_PROTOCOL);
265 EXPECT_EQUAL(output[S2N_JA4_A_PROTOCOL], 'q');
266 };
267
268 /* Test not QUIC */
269 {
270 uint8_t output[S2N_TEST_OUTPUT_SIZE] = { 0 };
271 uint32_t output_size = 0;
272 EXPECT_OK(s2n_test_ja4_hash_from_bytes(
273 minimal_client_hello_bytes, sizeof(minimal_client_hello_bytes),
274 sizeof(output), output, &output_size));
275
276 EXPECT_TRUE(output_size > S2N_JA4_A_PROTOCOL);
277 EXPECT_EQUAL(output[S2N_JA4_A_PROTOCOL], 't');
278 };
279 };
280
281 /* Test destination
282 *
283 *= https://raw.githubusercontent.com/FoxIO-LLC/ja4/df3c067/technical_details/JA4.md#sni
284 *= type=test
285 *# If the SNI extension (0x0000) exists, then the destination of the connection
286 *# is a domain, or “d” in the fingerprint.
287 *# If the SNI does not exist, then the destination is an IP address, or “i”.
288 */
289 {
290 /* Test with SNI */
291 {
292 S2N_INIT_CLIENT_HELLO_WITH_EXTENSION(client_hello_bytes,
293 TLS_EXTENSION_SERVER_NAME);
294
295 uint8_t output[S2N_TEST_OUTPUT_SIZE] = { 0 };
296 uint32_t output_size = 0;
297 EXPECT_OK(s2n_test_ja4_hash_from_bytes(
298 client_hello_bytes, sizeof(client_hello_bytes),
299 sizeof(output), output, &output_size));
300
301 EXPECT_TRUE(output_size > S2N_JA4_A_DEST);
302 EXPECT_EQUAL(output[S2N_JA4_A_DEST], 'd');
303 };
304
305 /* Test without SNI */
306 {
307 uint8_t output[S2N_TEST_OUTPUT_SIZE] = { 0 };
308 uint32_t output_size = 0;
309 EXPECT_OK(s2n_test_ja4_hash_from_bytes(
310 minimal_client_hello_bytes, sizeof(minimal_client_hello_bytes),
311 sizeof(output), output, &output_size));
312
313 EXPECT_TRUE(output_size > S2N_JA4_A_DEST);
314 EXPECT_EQUAL(output[S2N_JA4_A_DEST], 'i');
315 };
316 };
317
318 /* Test version */
319 {
320 struct {
321 uint8_t bytes[2];
322 uint8_t version;
323 const char *str;
324 } test_cases[] = {
325 /**
326 *= https://raw.githubusercontent.com/FoxIO-LLC/ja4/df3c067/technical_details/JA4.md#tls-and-dtls-version
327 *= type=test
328 *# 0x0304 = TLS 1.3 = “13”
329 *# 0x0303 = TLS 1.2 = “12”
330 *# 0x0302 = TLS 1.1 = “11”
331 *# 0x0301 = TLS 1.0 = “10”
332 */
333 { .bytes = { 0x03, 0x04 }, .version = S2N_TLS13, .str = "13" },
334 { .bytes = { 0x03, 0x03 }, .version = S2N_TLS12, .str = "12" },
335 { .bytes = { 0x03, 0x02 }, .version = S2N_TLS11, .str = "11" },
336 { .bytes = { 0x03, 0x01 }, .version = S2N_TLS10, .str = "10" },
337 /**
338 *= https://raw.githubusercontent.com/FoxIO-LLC/ja4/df3c067/technical_details/JA4.md#tls-and-dtls-version
339 *= type=test
340 *# 0x0300 = SSL 3.0 = “s3”
341 *# 0x0002 = SSL 2.0 = “s2”
342 */
343 { .bytes = { 0x03, 0x00 }, .version = S2N_SSLv3, .str = "s3" },
344 { .bytes = { 0x00, 0x02 }, .version = S2N_SSLv2, .str = "s2" },
345 /* Bad values */
346 { .bytes = { 0x01, 0x00 }, .version = 10, .str = "00" },
347 { .bytes = { 0x00, 0x00 }, .version = 0, .str = "00" },
348 { .bytes = { 0x00, 0xFF }, .version = UINT8_MAX, .str = "00" },
349 { .bytes = { 0xFF, 0xFF }, .version = UINT8_MAX, .str = "00" },
350 };
351
352 for (size_t i = 0; i < s2n_array_len(test_cases); i++) {
353 /* Test record version not used.
354 *
355 *= https://raw.githubusercontent.com/FoxIO-LLC/ja4/df3c067/technical_details/JA4.md#tls-and-dtls-version
356 *= type=test
357 *# Handshake version (located at the top of the packet) should be ignored.
358 */
359 if (strcmp(test_cases[i].str, "00") != 0) {
360 struct s2n_client_hello client_hello = *minimal_client_hello;
361 client_hello.legacy_version = 0;
362 client_hello.legacy_record_version = test_cases[i].version;
363
364 DEFER_CLEANUP(struct s2n_fingerprint *fingerprint = NULL,
365 s2n_fingerprint_free);
366 fingerprint = s2n_fingerprint_new(S2N_FINGERPRINT_JA4);
367 EXPECT_NOT_NULL(fingerprint);
368 EXPECT_SUCCESS(s2n_fingerprint_set_client_hello(fingerprint,
369 &client_hello));
370
371 uint8_t output[S2N_TEST_OUTPUT_SIZE] = { 0 };
372 uint32_t output_size = 0;
373 EXPECT_SUCCESS(s2n_fingerprint_get_hash(fingerprint,
374 sizeof(output), output, &output_size));
375
376 EXPECT_TRUE(output_size > S2N_JA4_A_VERSION_2);
377 EXPECT_FALSE((output[S2N_JA4_A_VERSION_1] == test_cases[i].str[0])
378 && (output[S2N_JA4_A_VERSION_2] == test_cases[i].str[1]));
379 };
380
381 /* Test version from extension
382 *
383 *= https://raw.githubusercontent.com/FoxIO-LLC/ja4/df3c067/technical_details/JA4.md#tls-and-dtls-version
384 *= type=test
385 *# If extension 0x002b exists (supported_versions), then the version
386 *# is the highest value in the extension.
387 */
388 {
389 S2N_INIT_CLIENT_HELLO(client_hello_bytes,
390 S2N_TEST_CLIENT_HELLO_VERSION,
391 S2N_TEST_CLIENT_HELLO_AFTER_VERSION,
392 S2N_TEST_CLIENT_HELLO_CIPHERS,
393 S2N_TEST_CLIENT_HELLO_AFTER_CIPHERS,
394 /* extensions size */
395 0x00, 7,
396 /* extensions: supported versions */
397 0x00, TLS_EXTENSION_SUPPORTED_VERSIONS, 0x00, 3,
398 0x02, test_cases[i].bytes[0], test_cases[i].bytes[1]);
399
400 uint8_t output[S2N_TEST_OUTPUT_SIZE] = { 0 };
401 uint32_t output_size = 0;
402 EXPECT_OK(s2n_test_ja4_hash_from_bytes(
403 client_hello_bytes, sizeof(client_hello_bytes),
404 sizeof(output), output, &output_size));
405
406 EXPECT_TRUE(output_size > S2N_JA4_A_VERSION_2);
407 EXPECT_EQUAL(output[S2N_JA4_A_VERSION_1], test_cases[i].str[0]);
408 EXPECT_EQUAL(output[S2N_JA4_A_VERSION_2], test_cases[i].str[1]);
409 };
410
411 /* Test version from legacy field
412 *
413 *= https://raw.githubusercontent.com/FoxIO-LLC/ja4/df3c067/technical_details/JA4.md#tls-and-dtls-version
414 *= type=test
415 *# If the extension doesn’t exist, then the TLS version is the value
416 *# of the Protocol Version.
417 */
418 {
419 S2N_INIT_CLIENT_HELLO(client_hello_bytes,
420 /* protocol version */
421 test_cases[i].bytes[0], test_cases[i].bytes[1],
422 S2N_TEST_CLIENT_HELLO_AFTER_VERSION,
423 S2N_TEST_CLIENT_HELLO_CIPHERS,
424 S2N_TEST_CLIENT_HELLO_AFTER_CIPHERS,
425 S2N_TEST_CLIENT_HELLO_EMPTY_EXTENSIONS);
426
427 uint8_t output[S2N_TEST_OUTPUT_SIZE] = { 0 };
428 uint32_t output_size = 0;
429 EXPECT_OK(s2n_test_ja4_hash_from_bytes(
430 client_hello_bytes, sizeof(client_hello_bytes),
431 sizeof(output), output, &output_size));
432
433 EXPECT_TRUE(output_size > S2N_JA4_A_VERSION_2);
434 EXPECT_EQUAL(output[S2N_JA4_A_VERSION_1], test_cases[i].str[0]);
435 EXPECT_EQUAL(output[S2N_JA4_A_VERSION_2], test_cases[i].str[1]);
436 };
437 }
438
439 /* Test with grease values
440 *
441 *= https://raw.githubusercontent.com/FoxIO-LLC/ja4/df3c067/technical_details/JA4.md#tls-and-dtls-version
442 *= type=test
443 *# Remember to ignore GREASE values.
444 */
445 {
446 S2N_INIT_CLIENT_HELLO(client_hello_bytes,
447 S2N_TEST_CLIENT_HELLO_VERSION,
448 S2N_TEST_CLIENT_HELLO_AFTER_VERSION,
449 S2N_TEST_CLIENT_HELLO_CIPHERS,
450 S2N_TEST_CLIENT_HELLO_AFTER_CIPHERS,
451 /* extensions size */
452 0x00, 13,
453 /* extensions: supported versions */
454 0x00, TLS_EXTENSION_SUPPORTED_VERSIONS, 0x00, 9,
455 /* supported version size */
456 8,
457 /* grease values */
458 0x0A, 0x0A,
459 0xAA, 0xAA,
460 0xFA, 0xFA,
461 /* actual value - lower than grease values */
462 test_cases[0].bytes[0], test_cases[0].bytes[1]);
463
464 /* assert that test case is less than grease values */
465 EXPECT_TRUE(test_cases[0].bytes[0] < 0x0A);
466
467 uint8_t output[S2N_TEST_OUTPUT_SIZE] = { 0 };
468 uint32_t output_size = 0;
469 EXPECT_OK(s2n_test_ja4_hash_from_bytes(
470 client_hello_bytes, sizeof(client_hello_bytes),
471 sizeof(output), output, &output_size));
472
473 EXPECT_TRUE(output_size > S2N_JA4_A_VERSION_2);
474 EXPECT_EQUAL(output[S2N_JA4_A_VERSION_1], test_cases[0].str[0]);
475 EXPECT_EQUAL(output[S2N_JA4_A_VERSION_2], test_cases[0].str[1]);
476 };
477 };
478
479 const struct {
480 uint8_t size;
481 const char *str;
482 } count_test_cases[] = {
483 /**
484 *= https://raw.githubusercontent.com/FoxIO-LLC/ja4/df3c067/technical_details/JA4.md#number-of-ciphers
485 *= type=test
486 *# if there’s 6 cipher suites in the hello packet, then the
487 *# value should be “06”.
488 */
489 { .size = 6, .str = "06" },
490 { .size = 12, .str = "12" },
491 { .size = 50, .str = "50" },
492 { .size = 99, .str = "99" },
493 /**
494 *= https://raw.githubusercontent.com/FoxIO-LLC/ja4/df3c067/technical_details/JA4.md#number-of-ciphers
495 *= type=test
496 *# If there’s > 99, which there should never be, then output “99”.
497 */
498 { .size = 100, .str = "99" },
499 { .size = 255, .str = "99" },
500 };
501
502 /* Test number of cipher suites */
503 {
504 /* Test basic cipher suite list
505 *
506 *= https://raw.githubusercontent.com/FoxIO-LLC/ja4/df3c067/technical_details/JA4.md#number-of-ciphers
507 *= type=test
508 *# 2 character number of cipher suites, so if there’s 6 cipher suites
509 *# in the hello packet, then the value should be “06”.
510 */
511 for (size_t i = 0; i < s2n_array_len(count_test_cases); i++) {
512 uint8_t output[S2N_TEST_OUTPUT_SIZE] = { 0 };
513 uint32_t output_size = 0;
514 EXPECT_OK(s2n_test_ja4_hash_from_cipher_count(count_test_cases[i].size,
515 sizeof(output), output, &output_size));
516
517 EXPECT_TRUE(output_size > S2N_JA4_A_CIPHER_COUNT_2);
518 EXPECT_EQUAL(output[S2N_JA4_A_CIPHER_COUNT_1], count_test_cases[i].str[0]);
519 EXPECT_EQUAL(output[S2N_JA4_A_CIPHER_COUNT_2], count_test_cases[i].str[1]);
520 };
521
522 /* Test with grease values
523 *
524 *= https://raw.githubusercontent.com/FoxIO-LLC/ja4/df3c067/technical_details/JA4.md#number-of-ciphers
525 *= type=test
526 *# Remember, ignore GREASE values. They don’t count.
527 */
528 {
529 S2N_INIT_CLIENT_HELLO(client_hello_bytes,
530 S2N_TEST_CLIENT_HELLO_VERSION,
531 S2N_TEST_CLIENT_HELLO_AFTER_VERSION,
532 /* cipher suites size */
533 0x00, 8,
534 /* ciphers suites */
535 /* grease values */
536 0x0A, 0x0A,
537 0xAA, 0xAA,
538 0xFA, 0xFA,
539 /* non-grease value */
540 0x00, 0x00,
541 S2N_TEST_CLIENT_HELLO_AFTER_CIPHERS,
542 S2N_TEST_CLIENT_HELLO_EMPTY_EXTENSIONS);
543
544 uint8_t output[S2N_TEST_OUTPUT_SIZE] = { 0 };
545 uint32_t output_size = 0;
546 EXPECT_OK(s2n_test_ja4_hash_from_bytes(
547 client_hello_bytes, sizeof(client_hello_bytes),
548 sizeof(output), output, &output_size));
549
550 EXPECT_TRUE(output_size > S2N_JA4_A_CIPHER_COUNT_2);
551 EXPECT_EQUAL(output[S2N_JA4_A_CIPHER_COUNT_1], '0');
552 EXPECT_EQUAL(output[S2N_JA4_A_CIPHER_COUNT_2], '1');
553 };
554 };
555
556 /* Test number of extensions
557 *
558 *= https://raw.githubusercontent.com/FoxIO-LLC/ja4/df3c067/technical_details/JA4.md#number-of-extensions
559 *= type=test
560 *# Same as counting ciphers.
561 */
562 {
563 /* Test basic extension list */
564 for (size_t i = 0; i < s2n_array_len(count_test_cases); i++) {
565 uint8_t output[S2N_TEST_OUTPUT_SIZE] = { 0 };
566 uint32_t output_size = 0;
567 EXPECT_OK(s2n_test_ja4_hash_from_extension_count(count_test_cases[i].size,
568 sizeof(output), output, &output_size));
569
570 EXPECT_TRUE(output_size > S2N_JA4_A_EXT_COUNT_2);
571 EXPECT_EQUAL(output[S2N_JA4_A_EXT_COUNT_1], count_test_cases[i].str[0]);
572 EXPECT_EQUAL(output[S2N_JA4_A_EXT_COUNT_2], count_test_cases[i].str[1]);
573 };
574
575 /* Test with grease values
576 *
577 *= https://raw.githubusercontent.com/FoxIO-LLC/ja4/df3c067/technical_details/JA4.md#number-of-extensions
578 *= type=test
579 *# Ignore GREASE.
580 */
581 {
582 S2N_INIT_CLIENT_HELLO(client_hello_bytes,
583 S2N_TEST_CLIENT_HELLO_VERSION,
584 S2N_TEST_CLIENT_HELLO_AFTER_VERSION,
585 S2N_TEST_CLIENT_HELLO_CIPHERS,
586 S2N_TEST_CLIENT_HELLO_AFTER_CIPHERS,
587 /* extensions size */
588 0x00, 16,
589 /* extensions */
590 /* grease values */
591 0x0A, 0x0A, S2N_TEST_CLIENT_HELLO_EMPTY_EXTENSION,
592 0xAA, 0xAA, S2N_TEST_CLIENT_HELLO_EMPTY_EXTENSION,
593 0xFA, 0xFA, S2N_TEST_CLIENT_HELLO_EMPTY_EXTENSION,
594 /* non-grease values */
595 0x00, 0x00, S2N_TEST_CLIENT_HELLO_EMPTY_EXTENSION);
596
597 uint8_t output[S2N_TEST_OUTPUT_SIZE] = { 0 };
598 uint32_t output_size = 0;
599 EXPECT_OK(s2n_test_ja4_hash_from_bytes(
600 client_hello_bytes, sizeof(client_hello_bytes),
601 sizeof(output), output, &output_size));
602
603 EXPECT_TRUE(output_size > S2N_JA4_A_EXT_COUNT_2);
604 EXPECT_EQUAL(output[S2N_JA4_A_EXT_COUNT_1], '0');
605 EXPECT_EQUAL(output[S2N_JA4_A_EXT_COUNT_2], '1');
606 };
607
608 /* Ensure SNI and ALPN are included
609 *
610 *= https://raw.githubusercontent.com/FoxIO-LLC/ja4/df3c067/technical_details/JA4.md#number-of-extensions
611 *= type=test
612 *# Include SNI and ALPN.
613 */
614 {
615 S2N_INIT_CLIENT_HELLO(client_hello_bytes,
616 S2N_TEST_CLIENT_HELLO_VERSION,
617 S2N_TEST_CLIENT_HELLO_AFTER_VERSION,
618 S2N_TEST_CLIENT_HELLO_CIPHERS,
619 S2N_TEST_CLIENT_HELLO_AFTER_CIPHERS,
620 /* extensions size */
621 0x00, 8,
622 /* extensions */
623 0x00, TLS_EXTENSION_SERVER_NAME, S2N_TEST_CLIENT_HELLO_EMPTY_EXTENSION,
624 0x00, TLS_EXTENSION_ALPN, S2N_TEST_CLIENT_HELLO_EMPTY_EXTENSION);
625
626 uint8_t output[S2N_TEST_OUTPUT_SIZE] = { 0 };
627 uint32_t output_size = 0;
628 EXPECT_OK(s2n_test_ja4_hash_from_bytes(
629 client_hello_bytes, sizeof(client_hello_bytes),
630 sizeof(output), output, &output_size));
631
632 EXPECT_TRUE(output_size > S2N_JA4_A_EXT_COUNT_2);
633 EXPECT_EQUAL(output[S2N_JA4_A_EXT_COUNT_1], '0');
634 EXPECT_EQUAL(output[S2N_JA4_A_EXT_COUNT_2], '2');
635 };
636 };
637
638 /* Test ALPN */
639 {
640 /* Test basic ALPN
641 *
642 *= https://raw.githubusercontent.com/FoxIO-LLC/ja4/df3c067/technical_details/JA4.md#alpn-extension-value
643 *= type=test
644 *# The first and last alphanumeric characters of the ALPN
645 *# (Application-Layer Protocol Negotiation) first value.
646 */
647 {
648 /* 2 characters: h2
649 *
650 *= https://raw.githubusercontent.com/FoxIO-LLC/ja4/df3c067/technical_details/JA4.md#alpn-extension-value
651 *= type=test
652 *# In the above example, the first ALPN value is h2 so the first
653 *# and last characters to use in the fingerprint are “h2”.
654 */
655 {
656 S2N_INIT_CLIENT_HELLO(client_hello_bytes,
657 S2N_TEST_CLIENT_HELLO_VERSION,
658 S2N_TEST_CLIENT_HELLO_AFTER_VERSION,
659 S2N_TEST_CLIENT_HELLO_CIPHERS,
660 S2N_TEST_CLIENT_HELLO_AFTER_CIPHERS,
661 /* extensions size */
662 0x00, 12,
663 /* extension: alpn */
664 0x00, TLS_EXTENSION_ALPN, 0x00, 8,
665 0x00, 6,
666 2, 'h', '2',
667 2, 'h', '3');
668
669 uint8_t output[S2N_TEST_OUTPUT_SIZE] = { 0 };
670 uint32_t output_size = 0;
671 EXPECT_OK(s2n_test_ja4_hash_from_bytes(
672 client_hello_bytes, sizeof(client_hello_bytes),
673 sizeof(output), output, &output_size));
674
675 EXPECT_TRUE(output_size > S2N_JA4_A_ALPN_LAST);
676 EXPECT_EQUAL(output[S2N_JA4_A_ALPN_FIRST], 'h');
677 EXPECT_EQUAL(output[S2N_JA4_A_ALPN_LAST], '2');
678 };
679
680 /* More than 2 characters: "http/1.1"
681 *
682 *= https://raw.githubusercontent.com/FoxIO-LLC/ja4/df3c067/technical_details/JA4.md#alpn-extension-value
683 *= type=test
684 *# If the first ALPN listed was http/1.1 then the first and last
685 *# characters to use in the fingerprint would be “h1”.
686 */
687 {
688 S2N_INIT_CLIENT_HELLO(client_hello_bytes,
689 S2N_TEST_CLIENT_HELLO_VERSION,
690 S2N_TEST_CLIENT_HELLO_AFTER_VERSION,
691 S2N_TEST_CLIENT_HELLO_CIPHERS,
692 S2N_TEST_CLIENT_HELLO_AFTER_CIPHERS,
693 /* extensions size */
694 0x00, 20,
695 /* extension: alpn */
696 0x00, TLS_EXTENSION_ALPN, 0x00, 16,
697 0x00, 14,
698 8, 'h', 't', 't', 'p', '/', '1', '.', '1',
699 4, 'q', 'u', 'i', 'c');
700
701 uint8_t output[S2N_TEST_OUTPUT_SIZE] = { 0 };
702 uint32_t output_size = 0;
703 EXPECT_OK(s2n_test_ja4_hash_from_bytes(
704 client_hello_bytes, sizeof(client_hello_bytes),
705 sizeof(output), output, &output_size));
706
707 EXPECT_TRUE(output_size > S2N_JA4_A_ALPN_LAST);
708 EXPECT_EQUAL(output[S2N_JA4_A_ALPN_FIRST], 'h');
709 EXPECT_EQUAL(output[S2N_JA4_A_ALPN_LAST], '1');
710 };
711 };
712
713 /* Test 1-byte alpn value
714 *
715 *= https://raw.githubusercontent.com/FoxIO-LLC/ja4/df3c067/technical_details/JA4.md#alpn-extension-value
716 *= type=test
717 *# If the first ALPN value is only a single character, then that character
718 *# is treated as both the first and last character.
719 */
720 {
721 S2N_INIT_CLIENT_HELLO(client_hello_bytes,
722 S2N_TEST_CLIENT_HELLO_VERSION,
723 S2N_TEST_CLIENT_HELLO_AFTER_VERSION,
724 S2N_TEST_CLIENT_HELLO_CIPHERS,
725 S2N_TEST_CLIENT_HELLO_AFTER_CIPHERS,
726 /* extensions size */
727 0x00, 8,
728 /* extension: alpn */
729 0x00, TLS_EXTENSION_ALPN, 0x00, 4,
730 0x00, 2,
731 1, 'q');
732
733 uint8_t output[S2N_TEST_OUTPUT_SIZE] = { 0 };
734 uint32_t output_size = 0;
735 EXPECT_OK(s2n_test_ja4_hash_from_bytes(
736 client_hello_bytes, sizeof(client_hello_bytes),
737 sizeof(output), output, &output_size));
738
739 EXPECT_TRUE(output_size > S2N_JA4_A_ALPN_LAST);
740 EXPECT_EQUAL(output[S2N_JA4_A_ALPN_FIRST], 'q');
741 EXPECT_EQUAL(output[S2N_JA4_A_ALPN_LAST], 'q');
742 };
743
744 /* Test non-ascii alpn values
745 *
746 *= https://raw.githubusercontent.com/FoxIO-LLC/ja4/df3c067/technical_details/JA4.md#alpn-extension-value
747 *= type=test
748 *# If the first or last byte of the first ALPN is non-alphanumeric
749 *# (meaning not `0x30-0x39`, `0x41-0x5A`, or `0x61-0x7A`), then we
750 *# print the first and last characters of the hex representation of
751 *# the first ALPN instead.
752 */
753 {
754 struct {
755 const uint8_t bytes[4];
756 const size_t bytes_size;
757 const char *str;
758 } test_cases[] = {
759 /**
760 *= https://raw.githubusercontent.com/FoxIO-LLC/ja4/df3c067/technical_details/JA4.md#alpn-extension-value
761 *= type=test
762 *# For example:
763 *# * `0xAB` would be printed as "ab"
764 *# * `0xAB 0xCD` would be printed as "ad"
765 *# * `0x30 0xAB` would be printed as "3b"
766 *# * `0x30 0x31 0xAB 0xCD` would be printed as "3d"
767 */
768 { .bytes = { 0xAB }, .str = "ab", .bytes_size = 1 },
769 { .bytes = { 0xAB, 0xCD }, .str = "ad", .bytes_size = 2 },
770 { .bytes = { 0x30, 0xAB }, .str = "3b", .bytes_size = 2 },
771 { .bytes = { 0x30, 0x31, 0xAB, 0xCD }, .str = "3d", .bytes_size = 4 },
772 /**
773 *= https://raw.githubusercontent.com/FoxIO-LLC/ja4/df3c067/technical_details/JA4.md#alpn-extension-value
774 *= type=test
775 *# * `0x30 0xAB 0xCD 0x31` would be printed as "01"
776 *
777 * Why is this value "01" instead of "31"?
778 * Because 0x30 and 0x31 are both alphanumeric, so we use their
779 * ascii values ('0' and '1') rather than their hex values.
780 * It doesn't matter than 0xAB and 0xCD aren't alphanumeric.
781 */
782 { .bytes = { 0x30, 0xAB, 0xCD, 0x31 }, .str = "01", .bytes_size = 4 },
783 };
784
785 S2N_INIT_CLIENT_HELLO(client_hello_bytes,
786 S2N_TEST_CLIENT_HELLO_VERSION,
787 S2N_TEST_CLIENT_HELLO_AFTER_VERSION,
788 S2N_TEST_CLIENT_HELLO_CIPHERS,
789 S2N_TEST_CLIENT_HELLO_AFTER_CIPHERS,
790 /* extensions size */
791 0x00, 11,
792 /* extension: alpn */
793 0x00, TLS_EXTENSION_ALPN, 0x00, 7,
794 0x00, 5,
795 0, 0, 0, 0, 0);
796
797 /* We allocated enough space in the above client hello for
798 * a 1-byte length and a 4-byte alpn */
799 EXPECT_EQUAL(sizeof(test_cases[0].bytes), 4);
800 const size_t alpn_mem_size = sizeof(test_cases[0].bytes);
801 const size_t offset = sizeof(client_hello_bytes) - alpn_mem_size;
802 uint8_t *const bytes_ptr = client_hello_bytes + offset;
803 uint8_t *const length_ptr = bytes_ptr - 1;
804
805 for (size_t i = 0; i < s2n_array_len(test_cases); i++) {
806 *length_ptr = test_cases[i].bytes_size;
807 EXPECT_MEMCPY_SUCCESS(bytes_ptr,
808 test_cases[i].bytes, sizeof(test_cases[i].bytes));
809
810 uint8_t output[S2N_TEST_OUTPUT_SIZE] = { 0 };
811 uint32_t output_size = 0;
812 EXPECT_OK(s2n_test_ja4_hash_from_bytes(
813 client_hello_bytes, sizeof(client_hello_bytes),
814 sizeof(output), output, &output_size));
815
816 EXPECT_TRUE(output_size > S2N_JA4_A_ALPN_LAST);
817 EXPECT_EQUAL(output[S2N_JA4_A_ALPN_FIRST], test_cases[i].str[0]);
818 EXPECT_EQUAL(output[S2N_JA4_A_ALPN_LAST], test_cases[i].str[1]);
819 }
820 };
821
822 /* Test no ALPN
823 *
824 *= https://raw.githubusercontent.com/FoxIO-LLC/ja4/df3c067/technical_details/JA4.md#alpn-extension-value
825 *= type=test
826 *# If there is no ALPN extension, no ALPN values, or the first ALPN
827 *# value is empty, then we print "00" as the value in the fingerprint.
828 */
829 {
830 /* Test no ALPN extension */
831 {
832 uint8_t output[S2N_TEST_OUTPUT_SIZE] = { 0 };
833 uint32_t output_size = 0;
834 EXPECT_OK(s2n_test_ja4_hash_from_bytes(
835 minimal_client_hello_bytes, sizeof(minimal_client_hello_bytes),
836 sizeof(output), output, &output_size));
837
838 EXPECT_TRUE(output_size > S2N_JA4_A_ALPN_LAST);
839 EXPECT_EQUAL(output[S2N_JA4_A_ALPN_FIRST], '0');
840 EXPECT_EQUAL(output[S2N_JA4_A_ALPN_LAST], '0');
841 };
842
843 /* Test no ALPN values */
844 {
845 S2N_INIT_CLIENT_HELLO(client_hello_bytes,
846 S2N_TEST_CLIENT_HELLO_VERSION,
847 S2N_TEST_CLIENT_HELLO_AFTER_VERSION,
848 S2N_TEST_CLIENT_HELLO_CIPHERS,
849 S2N_TEST_CLIENT_HELLO_AFTER_CIPHERS,
850 /* extensions size */
851 0x00, 6,
852 /* extension: alpn */
853 0x00, TLS_EXTENSION_ALPN, 0x00, 2,
854 0x00, 0);
855
856 uint8_t output[S2N_TEST_OUTPUT_SIZE] = { 0 };
857 uint32_t output_size = 0;
858 EXPECT_OK(s2n_test_ja4_hash_from_bytes(
859 client_hello_bytes, sizeof(client_hello_bytes),
860 sizeof(output), output, &output_size));
861
862 EXPECT_TRUE(output_size > S2N_JA4_A_ALPN_LAST);
863 EXPECT_EQUAL(output[S2N_JA4_A_ALPN_FIRST], '0');
864 EXPECT_EQUAL(output[S2N_JA4_A_ALPN_LAST], '0');
865 };
866
867 /* Test empty first alpn value */
868 {
869 S2N_INIT_CLIENT_HELLO(client_hello_bytes,
870 S2N_TEST_CLIENT_HELLO_VERSION,
871 S2N_TEST_CLIENT_HELLO_AFTER_VERSION,
872 S2N_TEST_CLIENT_HELLO_CIPHERS,
873 S2N_TEST_CLIENT_HELLO_AFTER_CIPHERS,
874 /* extensions size */
875 0x00, 7,
876 /* extension: alpn */
877 0x00, TLS_EXTENSION_ALPN, 0x00, 3,
878 0x00, 1,
879 0);
880
881 uint8_t output[S2N_TEST_OUTPUT_SIZE] = { 0 };
882 uint32_t output_size = 0;
883 EXPECT_OK(s2n_test_ja4_hash_from_bytes(
884 client_hello_bytes, sizeof(client_hello_bytes),
885 sizeof(output), output, &output_size));
886
887 EXPECT_TRUE(output_size > S2N_JA4_A_ALPN_LAST);
888 EXPECT_EQUAL(output[S2N_JA4_A_ALPN_FIRST], '0');
889 EXPECT_EQUAL(output[S2N_JA4_A_ALPN_LAST], '0');
890 };
891 };
892 };
893 };
894
895 /* Test JA4_b: cipher suites */
896 {
897 /* clang-format off */
898 S2N_INIT_CLIENT_HELLO(client_hello_bytes,
899 S2N_TEST_CLIENT_HELLO_VERSION,
900 S2N_TEST_CLIENT_HELLO_AFTER_VERSION,
901 /* cipher suites size */
902 0x00, 30,
903 /* cipher suites
904 *
905 *= https://raw.githubusercontent.com/FoxIO-LLC/ja4/df3c067/technical_details/JA4.md#cipher-hash
906 *= type=test
907 *# Example:
908 *# ```
909 *# 1301,1302,1303,c02b,c02f,
910 */
911 0x13, 0x01, 0x13, 0x02, 0x13, 0x03, 0xc0, 0x2b, 0xc0, 0x2f,
912 /*
913 *= https://raw.githubusercontent.com/FoxIO-LLC/ja4/df3c067/technical_details/JA4.md#cipher-hash
914 *= type=test
915 *# c02c,c030,cca9,cca8,c013,
916 */
917 0xc0, 0x2c, 0xc0, 0x30, 0xcc, 0xa9, 0xcc, 0xa8, 0xc0, 0x13,
918 /*
919 *= https://raw.githubusercontent.com/FoxIO-LLC/ja4/df3c067/technical_details/JA4.md#cipher-hash
920 *= type=test
921 *# c014,009c,009d,002f,0035
922 *# ```
923 */
924 0xc0, 0x14, 0x00, 0x9c, 0x00, 0x9d, 0x00, 0x2f, 0x00, 0x35,
925 S2N_TEST_CLIENT_HELLO_AFTER_CIPHERS,
926 S2N_TEST_CLIENT_HELLO_EMPTY_EXTENSIONS,
927 );
928 /* clang-format on */
929
930 /* Test raw list
931 *
932 *= https://raw.githubusercontent.com/FoxIO-LLC/ja4/df3c067/technical_details/JA4.md#cipher-hash
933 *= type=test
934 *# The list is created using the 4 character hex values of the ciphers,
935 *# lower case, comma delimited, ignoring GREASE.
936 */
937 {
938 /* Expected result from docs
939 *
940 *= https://raw.githubusercontent.com/FoxIO-LLC/ja4/df3c067/technical_details/JA4.md#cipher-hash
941 *= type=test
942 *# 002f,0035,009c,009d,1301,1302,1303,c013,c014,c02b,c02c,c02f,c030,cca8,cca9
943 */
944 const char expected[] =
945 "002f,0035,009c,009d,1301,1302,1303,c013,c014,c02b,c02c,c02f,c030,cca8,cca9";
946
947 uint8_t output[S2N_TEST_OUTPUT_SIZE] = { 0 };
948 uint32_t output_size = 0;
949 EXPECT_OK(s2n_test_ja4_raw_from_bytes(
950 client_hello_bytes, sizeof(client_hello_bytes),
951 sizeof(output), output, &output_size));
952
953 EXPECT_TRUE(output_size > S2N_JA4_B_START);
954 uint8_t *output_b = &output[S2N_JA4_B_START];
955 EXPECT_TRUE(output_size >= S2N_JA4_B_START + strlen(expected));
956 EXPECT_BYTEARRAY_EQUAL(output_b, expected, strlen(expected));
957 };
958
959 /* Test hashed list
960 *
961 *= https://raw.githubusercontent.com/FoxIO-LLC/ja4/df3c067/technical_details/JA4.md#cipher-hash
962 *= type=test
963 *# A 12 character truncated sha256 hash of the list of ciphers sorted
964 *# in hex order, first 12 characters.
965 */
966 {
967 /* Expected result from docs
968 *
969 *= https://raw.githubusercontent.com/FoxIO-LLC/ja4/df3c067/technical_details/JA4.md#cipher-hash
970 *= type=test
971 *# = 8daaf6152771
972 */
973 const char expected[] = "8daaf6152771";
974
975 uint8_t output[S2N_TEST_OUTPUT_SIZE] = { 0 };
976 uint32_t output_size = 0;
977 EXPECT_OK(s2n_test_ja4_hash_from_bytes(
978 client_hello_bytes, sizeof(client_hello_bytes),
979 sizeof(output), output, &output_size));
980
981 EXPECT_TRUE(output_size > S2N_JA4_B_START);
982 uint8_t *output_b = &output[S2N_JA4_B_START];
983 EXPECT_TRUE(output_size >= S2N_JA4_B_START + strlen(expected));
984 EXPECT_BYTEARRAY_EQUAL(output_b, expected, strlen(expected));
985 };
986
987 /* Test empty list
988 *
989 * s2n-tls treats an empty cipher suite list as a parsing error, since
990 * the RFC requires it to contain at least one cipher suite. The definition
991 * of the field is:
992 * CipherSuite cipher_suites<2..2^16-2>;
993 * Meaning that the list must contain at least two bytes, which means
994 * at least one CipherSuite.
995 *
996 * However, s2n-tls could later decide to error on empty cipher suite lists
997 * when processing the ClientHello instead of when parsing it, so we
998 * should still test this case by manipulating the parsed ClientHello
999 * before fingerprinting it.
1000 *
1001 *= https://raw.githubusercontent.com/FoxIO-LLC/ja4/df3c067/technical_details/JA4.md#cipher-hash
1002 *= type=test
1003 *# If there are no ciphers in the sorted cipher list, then the value of
1004 *# JA4_b is set to `000000000000`
1005 */
1006 {
1007 const char expected[] = "000000000000";
1008
1009 uint8_t output[S2N_TEST_OUTPUT_SIZE] = { 0 };
1010 uint32_t output_size = 0;
1011
1012 DEFER_CLEANUP(struct s2n_client_hello *client_hello = NULL, s2n_client_hello_free);
1013 client_hello = s2n_client_hello_parse_message(client_hello_bytes, sizeof(client_hello_bytes));
1014 EXPECT_NOT_NULL(client_hello);
1015
1016 /* Override the size of the parsed cipher_suites to imitate having
1017 * read an empty list.
1018 */
1019 client_hello->cipher_suites.size = 0;
1020
1021 DEFER_CLEANUP(struct s2n_fingerprint *fingerprint = NULL, s2n_fingerprint_free);
1022 fingerprint = s2n_fingerprint_new(S2N_FINGERPRINT_JA4);
1023 EXPECT_NOT_NULL(fingerprint);
1024 EXPECT_SUCCESS(s2n_fingerprint_set_client_hello(fingerprint, client_hello));
1025 EXPECT_SUCCESS(s2n_fingerprint_get_hash(fingerprint,
1026 sizeof(output), output, &output_size));
1027
1028 EXPECT_TRUE(output_size > S2N_JA4_B_START);
1029 uint8_t *output_b = &output[S2N_JA4_B_START];
1030 EXPECT_TRUE(output_size >= S2N_JA4_B_START + strlen(expected));
1031 EXPECT_BYTEARRAY_EQUAL(output_b, expected, strlen(expected));
1032 };
1033
1034 /* Test sorting
1035 *
1036 *= https://raw.githubusercontent.com/FoxIO-LLC/ja4/df3c067/technical_details/JA4.md#cipher-hash
1037 *= type=test
1038 *# list of ciphers sorted in hex order
1039 */
1040 {
1041 const char expected[] =
1042 "0001,0002,0003,"
1043 "000a,000b,000c,"
1044 "0100,0200,0300,"
1045 "0a00,0b00,0c00";
1046
1047 /* Already sorted */
1048 {
1049 S2N_INIT_CLIENT_HELLO(bytes,
1050 S2N_TEST_CLIENT_HELLO_VERSION,
1051 S2N_TEST_CLIENT_HELLO_AFTER_VERSION,
1052 /* cipher suites size */
1053 0x00, 24,
1054 /* cipher suites in sorted order */
1055 0x00, 0x01, 0x00, 0x02, 0x00, 0x03,
1056 0x01, 0x00, 0x02, 0x00, 0x03, 0x00,
1057 0x00, 0x0a, 0x00, 0x0b, 0x00, 0x0c,
1058 0x0a, 0x00, 0x0b, 0x00, 0x0c, 0x00,
1059 S2N_TEST_CLIENT_HELLO_AFTER_CIPHERS,
1060 S2N_TEST_CLIENT_HELLO_EMPTY_EXTENSIONS);
1061
1062 uint8_t output[S2N_TEST_OUTPUT_SIZE] = { 0 };
1063 uint32_t output_size = 0;
1064 EXPECT_OK(s2n_test_ja4_raw_from_bytes(bytes, sizeof(bytes),
1065 sizeof(output), output, &output_size));
1066
1067 EXPECT_TRUE(output_size > S2N_JA4_B_START);
1068 uint8_t *output_b = &output[S2N_JA4_B_START];
1069 EXPECT_TRUE(output_size >= S2N_JA4_B_START + strlen(expected));
1070 EXPECT_BYTEARRAY_EQUAL(output_b, expected, strlen(expected));
1071 };
1072
1073 /* Reversed */
1074 {
1075 S2N_INIT_CLIENT_HELLO(bytes,
1076 S2N_TEST_CLIENT_HELLO_VERSION,
1077 S2N_TEST_CLIENT_HELLO_AFTER_VERSION,
1078 /* cipher suites size */
1079 0x00, 24,
1080 /* cipher suites in reversed order */
1081 0x0c, 0x00, 0x0b, 0x00, 0x0a, 0x00,
1082 0x00, 0x0c, 0x00, 0x0b, 0x00, 0x0a,
1083 0x03, 0x00, 0x02, 0x00, 0x01, 0x00,
1084 0x00, 0x03, 0x00, 0x02, 0x00, 0x01,
1085 S2N_TEST_CLIENT_HELLO_AFTER_CIPHERS,
1086 S2N_TEST_CLIENT_HELLO_EMPTY_EXTENSIONS);
1087
1088 uint8_t output[S2N_TEST_OUTPUT_SIZE] = { 0 };
1089 uint32_t output_size = 0;
1090 EXPECT_OK(s2n_test_ja4_raw_from_bytes(bytes, sizeof(bytes),
1091 sizeof(output), output, &output_size));
1092
1093 EXPECT_TRUE(output_size > S2N_JA4_B_START);
1094 uint8_t *output_b = &output[S2N_JA4_B_START];
1095 EXPECT_TRUE(output_size >= S2N_JA4_B_START + strlen(expected));
1096 EXPECT_BYTEARRAY_EQUAL(output_b, expected, strlen(expected));
1097 };
1098
1099 /* Randomized */
1100 {
1101 S2N_INIT_CLIENT_HELLO(bytes,
1102 S2N_TEST_CLIENT_HELLO_VERSION,
1103 S2N_TEST_CLIENT_HELLO_AFTER_VERSION,
1104 /* cipher suites size */
1105 0x00, 24,
1106 /* cipher suites in randomized order */
1107 0x0a, 0x00, 0x00, 0x0b, 0x00, 0x03,
1108 0x00, 0x01, 0x02, 0x00, 0x0b, 0x00,
1109 0x01, 0x00, 0x00, 0x0a, 0x0c, 0x00,
1110 0x00, 0x0c, 0x03, 0x00, 0x00, 0x02,
1111 S2N_TEST_CLIENT_HELLO_AFTER_CIPHERS,
1112 S2N_TEST_CLIENT_HELLO_EMPTY_EXTENSIONS);
1113
1114 uint8_t output[S2N_TEST_OUTPUT_SIZE] = { 0 };
1115 uint32_t output_size = 0;
1116 EXPECT_OK(s2n_test_ja4_raw_from_bytes(bytes, sizeof(bytes),
1117 sizeof(output), output, &output_size));
1118
1119 EXPECT_TRUE(output_size > S2N_JA4_B_START);
1120 uint8_t *output_b = &output[S2N_JA4_B_START];
1121 EXPECT_TRUE(output_size >= S2N_JA4_B_START + strlen(expected));
1122 EXPECT_BYTEARRAY_EQUAL(output_b, expected, strlen(expected));
1123 };
1124 };
1125 };
1126
1127 /* Test JA4_c: extensions and signature algorithms */
1128 {
1129 /* clang-format off */
1130 S2N_INIT_CLIENT_HELLO(client_hello_bytes,
1131 S2N_TEST_CLIENT_HELLO_VERSION,
1132 S2N_TEST_CLIENT_HELLO_AFTER_VERSION,
1133 S2N_TEST_CLIENT_HELLO_CIPHERS,
1134 S2N_TEST_CLIENT_HELLO_AFTER_CIPHERS,
1135 /* extensions size
1136 * There are 16 extensions (2 byte type + 2 bytes size),
1137 * and the signature algorithms contribute the remaining bytes. */
1138 0x00, (16 * 4) + (9 * 2),
1139 /* extensions
1140 *
1141 *= https://raw.githubusercontent.com/FoxIO-LLC/ja4/df3c067/technical_details/JA4.md#extension-hash
1142 *= type=test
1143 *# For example:
1144 *# ```
1145 *# 001b,0000,0033,0010,4469,0017,002d,000d,
1146 */
1147 0x00, 0x1b, S2N_TEST_CLIENT_HELLO_EMPTY_EXTENSION,
1148 0x00, 0x00, S2N_TEST_CLIENT_HELLO_EMPTY_EXTENSION,
1149 0x00, 0x33, S2N_TEST_CLIENT_HELLO_EMPTY_EXTENSION,
1150 0x00, 0x10, S2N_TEST_CLIENT_HELLO_EMPTY_EXTENSION,
1151 0x44, 0x69, S2N_TEST_CLIENT_HELLO_EMPTY_EXTENSION,
1152 0x00, 0x17, S2N_TEST_CLIENT_HELLO_EMPTY_EXTENSION,
1153 0x00, 0x2d, S2N_TEST_CLIENT_HELLO_EMPTY_EXTENSION,
1154 0x00, 0x0d, 0x00, 9 * 2,
1155 /* signature algorithms size */
1156 0x00, 8 * 2,
1157 /* signature algorithms
1158 *= https://raw.githubusercontent.com/FoxIO-LLC/ja4/df3c067/technical_details/JA4.md#extension-hash
1159 *= type=test
1160 *# For example the signature algorithms:
1161 *# ```
1162 *# 0403,0804,0401,0503,0805,0501,0806,0601
1163 *# ```
1164 */
1165 0x04, 0x03, 0x08, 0x04, 0x04, 0x01, 0x05, 0x03,
1166 0x08, 0x05, 0x05, 0x01, 0x08, 0x06, 0x06, 0x01,
1167 /* more extensions
1168 *
1169 *= https://raw.githubusercontent.com/FoxIO-LLC/ja4/df3c067/technical_details/JA4.md#extension-hash
1170 *= type=test
1171 *# 0005,0023,0012,002b,ff01,000b,000a,0015
1172 *# ```
1173 */
1174 0x00, 0x05, S2N_TEST_CLIENT_HELLO_EMPTY_EXTENSION,
1175 0x00, 0x23, S2N_TEST_CLIENT_HELLO_EMPTY_EXTENSION,
1176 0x00, 0x12, S2N_TEST_CLIENT_HELLO_EMPTY_EXTENSION,
1177 0x00, 0x2b, S2N_TEST_CLIENT_HELLO_EMPTY_EXTENSION,
1178 0xff, 0x01, S2N_TEST_CLIENT_HELLO_EMPTY_EXTENSION,
1179 0x00, 0x0b, S2N_TEST_CLIENT_HELLO_EMPTY_EXTENSION,
1180 0x00, 0x0a, S2N_TEST_CLIENT_HELLO_EMPTY_EXTENSION,
1181 0x00, 0x15, S2N_TEST_CLIENT_HELLO_EMPTY_EXTENSION,
1182 );
1183 /* clang-format on */
1184
1185 /* Expected raw extensions string from docs
1186 *
1187 *= https://raw.githubusercontent.com/FoxIO-LLC/ja4/df3c067/technical_details/JA4.md#extension-hash
1188 *= type=test
1189 *# 0005,000a,000b,000d,0012,0015,0017,001b,0023,002b,002d,0033,4469,ff01
1190 */
1191 const char expected_raw_extensions[] =
1192 "0005,000a,000b,000d,0012,0015,0017,001b,0023,002b,002d,0033,4469,ff01";
1193
1194 /* Test that SNI and ALPN are ignored
1195 *
1196 * Instead of actually testing this, we just test that the known value
1197 * will test that SNI and ALPN are ignored.
1198 *
1199 *= https://raw.githubusercontent.com/FoxIO-LLC/ja4/df3c067/technical_details/JA4.md#extension-hash
1200 *= type=test
1201 *# Ignore the SNI extension (0000) and the ALPN extension (0010)
1202 *# as we’ve already captured them in the _a_ section of the fingerprint.
1203 */
1204 {
1205 const char sni_str[] = "0000";
1206 const char alpn_str[] = "0010";
1207
1208 /* Known value from docs, in string form
1209 *
1210 *= https://raw.githubusercontent.com/FoxIO-LLC/ja4/df3c067/technical_details/JA4.md#extension-hash
1211 *= type=test
1212 *# For example:
1213 *# ```
1214 *# 001b,0000,0033,0010,4469,0017,002d,000d,0005,0023,0012,002b,ff01,000b,000a,0015
1215 *# ```
1216 */
1217 const char extensions[] =
1218 "001b,0000,0033,0010,4469,0017,002d,000d,0005,0023,0012,002b,ff01,000b,000a,0015";
1219 EXPECT_NOT_NULL(strstr(extensions, sni_str));
1220 EXPECT_NOT_NULL(strstr(extensions, alpn_str));
1221
1222 /* Expected result from docs
1223 *
1224 *= https://raw.githubusercontent.com/FoxIO-LLC/ja4/df3c067/technical_details/JA4.md#extension-hash
1225 *= type=test
1226 *# 0005,000a,000b,000d,0012,0015,0017,001b,0023,002b,002d,0033,4469,ff01
1227 */
1228 EXPECT_NULL(strstr(expected_raw_extensions, sni_str));
1229 EXPECT_NULL(strstr(expected_raw_extensions, alpn_str));
1230 };
1231
1232 /* Test raw extension list
1233 *
1234 *= https://raw.githubusercontent.com/FoxIO-LLC/ja4/df3c067/technical_details/JA4.md#extension-hash
1235 *= type=test
1236 *# The extension list is created using the 4 character hex values of the extensions,
1237 *# lower case, comma delimited, sorted (not in the order they appear).
1238 */
1239 {
1240 const char *expected = expected_raw_extensions;
1241
1242 uint8_t output[S2N_TEST_OUTPUT_SIZE] = { 0 };
1243 uint32_t output_size = 0;
1244 EXPECT_OK(s2n_test_ja4_raw_from_bytes(
1245 client_hello_bytes, sizeof(client_hello_bytes),
1246 sizeof(output), output, &output_size));
1247
1248 EXPECT_TRUE(output_size > S2N_JA4_C_RAW_START);
1249 uint8_t *output_c = &output[S2N_JA4_C_RAW_START];
1250 EXPECT_TRUE(output_size >= S2N_JA4_C_RAW_START + strlen(expected));
1251 EXPECT_BYTEARRAY_EQUAL(output_c, expected, strlen(expected));
1252 };
1253
1254 /* Test raw signature algorithms list
1255 *
1256 *= https://raw.githubusercontent.com/FoxIO-LLC/ja4/df3c067/technical_details/JA4.md#extension-hash
1257 *= type=test
1258 *# The signature algorithm hex values are then added to the end of the
1259 *# list in the order that they appear (not sorted) with an underscore
1260 *# delimiting the two lists.
1261 */
1262 {
1263 /* Expected result from docs
1264 *
1265 *= https://raw.githubusercontent.com/FoxIO-LLC/ja4/df3c067/technical_details/JA4.md#extension-hash
1266 *= type=test
1267 *# Are added to the end of the previous string to create:
1268 *# ```
1269 *# 0005,000a,000b,000d,0012,0015,0017,001b,0023,002b,002d,0033,4469,ff01_0403,0804,0401,0503,0805,0501,0806,0601
1270 *# ```
1271 */
1272 const char expected[] =
1273 "0005,000a,000b,000d,0012,0015,0017,001b,0023,002b,002d,"
1274 "0033,4469,ff01_0403,0804,0401,0503,0805,0501,0806,0601";
1275
1276 uint8_t output[S2N_TEST_OUTPUT_SIZE] = { 0 };
1277 uint32_t output_size = 0;
1278 EXPECT_OK(s2n_test_ja4_raw_from_bytes(
1279 client_hello_bytes, sizeof(client_hello_bytes),
1280 sizeof(output), output, &output_size));
1281
1282 EXPECT_TRUE(output_size > S2N_JA4_C_RAW_START);
1283 uint8_t *output_c = &output[S2N_JA4_C_RAW_START];
1284 EXPECT_TRUE(output_size >= S2N_JA4_C_RAW_START + strlen(expected));
1285 EXPECT_BYTEARRAY_EQUAL(output_c, expected, strlen(expected));
1286 };
1287
1288 /* Test hashed lists
1289 *
1290 *= https://raw.githubusercontent.com/FoxIO-LLC/ja4/df3c067/technical_details/JA4.md#extension-hash
1291 *= type=test
1292 *# A 12 character truncated sha256 hash of the list of extensions,
1293 *# sorted by hex value, followed by the list of signature algorithms,
1294 *# in the order that they appear (not sorted).
1295 */
1296 {
1297 /* Expected result from docs
1298 *
1299 *= https://raw.githubusercontent.com/FoxIO-LLC/ja4/df3c067/technical_details/JA4.md#extension-hash
1300 *= type=test
1301 *# Hashed to:
1302 *# ```
1303 *# e5627efa2ab19723084c1033a96c694a45826ab5a460d2d3fd5ffcfe97161c95
1304 *# ```
1305 *# Truncated to first 12 characters:
1306 *# ```
1307 *# e5627efa2ab1
1308 *# ```
1309 */
1310 const char expected[] = "e5627efa2ab1";
1311
1312 uint8_t output[S2N_TEST_OUTPUT_SIZE] = { 0 };
1313 uint32_t output_size = 0;
1314 EXPECT_OK(s2n_test_ja4_hash_from_bytes(
1315 client_hello_bytes, sizeof(client_hello_bytes),
1316 sizeof(output), output, &output_size));
1317
1318 EXPECT_TRUE(output_size > S2N_JA4_C_HASH_START);
1319 uint8_t *output_c = &output[S2N_JA4_C_HASH_START];
1320 EXPECT_TRUE(output_size >= S2N_JA4_C_HASH_START + strlen(expected));
1321 EXPECT_BYTEARRAY_EQUAL(output_c, expected, strlen(expected));
1322 };
1323
1324 /* Test with no signature schemes
1325 *
1326 *= https://raw.githubusercontent.com/FoxIO-LLC/ja4/df3c067/technical_details/JA4.md#extension-hash
1327 *= type=test
1328 *# If there are no signature algorithms in the hello packet,
1329 *# then the string ends without an underscore and is hashed.
1330 */
1331 {
1332 /* Extensions exist -- does NOT end in an underscore */
1333 {
1334 S2N_INIT_CLIENT_HELLO(bytes,
1335 S2N_TEST_CLIENT_HELLO_VERSION,
1336 S2N_TEST_CLIENT_HELLO_AFTER_VERSION,
1337 S2N_TEST_CLIENT_HELLO_CIPHERS,
1338 S2N_TEST_CLIENT_HELLO_AFTER_CIPHERS,
1339 /* Add some extensions so that part c isn't completely empty */
1340 0x00, 4,
1341 0x00, 0x01, S2N_TEST_CLIENT_HELLO_EMPTY_EXTENSION);
1342
1343 uint8_t output[S2N_TEST_OUTPUT_SIZE] = { 0 };
1344 uint32_t output_size = 0;
1345 EXPECT_OK(s2n_test_ja4_raw_from_bytes(bytes, sizeof(bytes),
1346 sizeof(output), output, &output_size));
1347
1348 /* Last character is not an underscore */
1349 EXPECT_TRUE(output_size > 1);
1350 EXPECT_NOT_EQUAL(output[output_size - 1], '_');
1351 };
1352
1353 /* No extensions -- does end in an underscore */
1354 {
1355 S2N_INIT_CLIENT_HELLO(bytes,
1356 S2N_TEST_CLIENT_HELLO_VERSION,
1357 S2N_TEST_CLIENT_HELLO_AFTER_VERSION,
1358 S2N_TEST_CLIENT_HELLO_CIPHERS,
1359 S2N_TEST_CLIENT_HELLO_AFTER_CIPHERS,
1360 S2N_TEST_CLIENT_HELLO_EMPTY_EXTENSIONS);
1361
1362 uint8_t output[S2N_TEST_OUTPUT_SIZE] = { 0 };
1363 uint32_t output_size = 0;
1364 EXPECT_OK(s2n_test_ja4_raw_from_bytes(bytes, sizeof(bytes),
1365 sizeof(output), output, &output_size));
1366
1367 /* Does end in an underscore */
1368 EXPECT_TRUE(output_size > 1);
1369 EXPECT_EQUAL(output[output_size - 1], '_');
1370
1371 /* We only expect the underscores between parts a, b, and c (a_b_c).
1372 * The final underscore should be due to part c being missing
1373 * completely, not due to missing signature algorithms.
1374 */
1375 size_t underscore_count = 0;
1376 for (size_t i = 0; i < output_size; i++) {
1377 if (output[i] == '_') {
1378 underscore_count++;
1379 }
1380 }
1381 EXPECT_EQUAL(underscore_count, 2);
1382 };
1383 };
1384
1385 /* Test empty extension list
1386 *
1387 *= https://raw.githubusercontent.com/FoxIO-LLC/ja4/df3c067/technical_details/JA4.md#extension-hash
1388 *= type=test
1389 *# If there are no extensions in the sorted extensions list, then the
1390 *# value of JA4_c is set to `000000000000`
1391 */
1392 {
1393 const char expected[] = "000000000000";
1394
1395 uint8_t output[S2N_TEST_OUTPUT_SIZE] = { 0 };
1396 uint32_t output_size = 0;
1397 EXPECT_OK(s2n_test_ja4_hash_from_bytes(
1398 minimal_client_hello_bytes, sizeof(minimal_client_hello_bytes),
1399 sizeof(output), output, &output_size));
1400
1401 EXPECT_TRUE(output_size > S2N_JA4_C_HASH_START);
1402 uint8_t *output_c = &output[S2N_JA4_C_HASH_START];
1403 EXPECT_TRUE(output_size >= S2N_JA4_C_HASH_START + strlen(expected));
1404 EXPECT_BYTEARRAY_EQUAL(output_c, expected, strlen(expected));
1405 };
1406 };
1407
1408 /* Test with malformed extensions
1409 *
1410 * Make each extension used by the JA4 algorithm 0-length to test error handling.
1411 */
1412 {
1413 S2N_INIT_CLIENT_HELLO(client_hello_bytes,
1414 S2N_TEST_CLIENT_HELLO_VERSION,
1415 S2N_TEST_CLIENT_HELLO_AFTER_VERSION,
1416 S2N_TEST_CLIENT_HELLO_CIPHERS,
1417 S2N_TEST_CLIENT_HELLO_AFTER_CIPHERS,
1418 /* extensions size */
1419 0x00, (3 * 4),
1420 /* extensions */
1421 /* signature_algorithms */
1422 0x00, 13, S2N_TEST_CLIENT_HELLO_EMPTY_EXTENSION,
1423 /* application_layer_protocol_negotiation */
1424 0x00, 16, S2N_TEST_CLIENT_HELLO_EMPTY_EXTENSION,
1425 /* supported_versions */
1426 0x00, 43, S2N_TEST_CLIENT_HELLO_EMPTY_EXTENSION);
1427
1428 uint8_t output[S2N_TEST_OUTPUT_SIZE] = { 0 };
1429 uint32_t output_size = 0;
1430 EXPECT_OK(s2n_test_ja4_raw_from_bytes(client_hello_bytes, sizeof(client_hello_bytes),
1431 sizeof(output), output, &output_size));
1432 }
1433
1434 /* Known value tests */
1435 {
1436 /* Known value from Wireshark parsing
1437 * https://github.com/FoxIO-LLC/ja4/blob/259739593049478dc68c84a436eca75b9f404e6e/pcap/tls12.pcap
1438 */
1439 {
1440 uint8_t packet_bytes[] = {
1441 0x01, 0x00, 0x01, 0xfc, 0x03, 0x03, 0xec, 0xb2,
1442 0x69, 0x1a, 0xdd, 0xb2, 0xbf, 0x6c, 0x59, 0x9c,
1443 0x7a, 0xaa, 0xe2, 0x3d, 0xe5, 0xf4, 0x25, 0x61,
1444 0xcc, 0x04, 0xeb, 0x41, 0x02, 0x9a, 0xcc, 0x6f,
1445 0xc0, 0x50, 0xa1, 0x6a, 0xc1, 0xd2, 0x20, 0x46,
1446 0xf8, 0x61, 0x7b, 0x58, 0x0a, 0xc9, 0x35, 0x8e,
1447 0x2a, 0xa4, 0x4e, 0x30, 0x6d, 0x52, 0x46, 0x6b,
1448 0xcc, 0x98, 0x9c, 0x87, 0xc8, 0xca, 0x64, 0x30,
1449 0x9f, 0x5f, 0xaf, 0x50, 0xba, 0x7b, 0x4d, 0x00,
1450 0x22, 0x13, 0x01, 0x13, 0x03, 0x13, 0x02, 0xc0,
1451 0x2b, 0xc0, 0x2f, 0xcc, 0xa9, 0xcc, 0xa8, 0xc0,
1452 0x2c, 0xc0, 0x30, 0xc0, 0x0a, 0xc0, 0x09, 0xc0,
1453 0x13, 0xc0, 0x14, 0x00, 0x9c, 0x00, 0x9d, 0x00,
1454 0x2f, 0x00, 0x35, 0x01, 0x00, 0x01, 0x91, 0x00,
1455 0x00, 0x00, 0x21, 0x00, 0x1f, 0x00, 0x00, 0x1c,
1456 0x63, 0x6f, 0x6e, 0x74, 0x69, 0x6c, 0x65, 0x2e,
1457 0x73, 0x65, 0x72, 0x76, 0x69, 0x63, 0x65, 0x73,
1458 0x2e, 0x6d, 0x6f, 0x7a, 0x69, 0x6c, 0x6c, 0x61,
1459 0x2e, 0x63, 0x6f, 0x6d, 0x00, 0x17, 0x00, 0x00,
1460 0xff, 0x01, 0x00, 0x01, 0x00, 0x00, 0x0a, 0x00,
1461 0x0e, 0x00, 0x0c, 0x00, 0x1d, 0x00, 0x17, 0x00,
1462 0x18, 0x00, 0x19, 0x01, 0x00, 0x01, 0x01, 0x00,
1463 0x0b, 0x00, 0x02, 0x01, 0x00, 0x00, 0x23, 0x00,
1464 0x00, 0x00, 0x10, 0x00, 0x0e, 0x00, 0x0c, 0x02,
1465 0x68, 0x32, 0x08, 0x68, 0x74, 0x74, 0x70, 0x2f,
1466 0x31, 0x2e, 0x31, 0x00, 0x05, 0x00, 0x05, 0x01,
1467 0x00, 0x00, 0x00, 0x00, 0x00, 0x22, 0x00, 0x0a,
1468 0x00, 0x08, 0x04, 0x03, 0x05, 0x03, 0x06, 0x03,
1469 0x02, 0x03, 0x00, 0x33, 0x00, 0x6b, 0x00, 0x69,
1470 0x00, 0x1d, 0x00, 0x20, 0x89, 0x09, 0x85, 0x8f,
1471 0xbe, 0xb6, 0xed, 0x2f, 0x12, 0x48, 0xba, 0x5b,
1472 0x9e, 0x29, 0x78, 0xbe, 0xad, 0x0e, 0x84, 0x01,
1473 0x10, 0x19, 0x2c, 0x61, 0xda, 0xed, 0x00, 0x96,
1474 0x79, 0x8b, 0x18, 0x44, 0x00, 0x17, 0x00, 0x41,
1475 0x04, 0x4d, 0x18, 0x3d, 0x91, 0xf5, 0xee, 0xd3,
1476 0x57, 0x91, 0xfa, 0x98, 0x24, 0x64, 0xe3, 0xb0,
1477 0x21, 0x4a, 0xaa, 0x5f, 0x5d, 0x1b, 0x78, 0x61,
1478 0x6d, 0x9b, 0x9f, 0xbe, 0xbc, 0x22, 0xd1, 0x1f,
1479 0x53, 0x5b, 0x2f, 0x94, 0xc6, 0x86, 0x14, 0x31,
1480 0x36, 0xaa, 0x79, 0x5e, 0x6e, 0x5a, 0x87, 0x5d,
1481 0x6c, 0x08, 0x06, 0x4a, 0xd5, 0xb7, 0x6d, 0x44,
1482 0xca, 0xad, 0x76, 0x6e, 0x24, 0x83, 0x01, 0x27,
1483 0x48, 0x00, 0x2b, 0x00, 0x05, 0x04, 0x03, 0x04,
1484 0x03, 0x03, 0x00, 0x0d, 0x00, 0x18, 0x00, 0x16,
1485 0x04, 0x03, 0x05, 0x03, 0x06, 0x03, 0x08, 0x04,
1486 0x08, 0x05, 0x08, 0x06, 0x04, 0x01, 0x05, 0x01,
1487 0x06, 0x01, 0x02, 0x03, 0x02, 0x01, 0x00, 0x2d,
1488 0x00, 0x02, 0x01, 0x01, 0x00, 0x1c, 0x00, 0x02,
1489 0x40, 0x01, 0x00, 0x15, 0x00, 0x7a, 0x00, 0x00,
1490 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
1491 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
1492 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
1493 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
1494 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
1495 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
1496 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
1497 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
1498 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
1499 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
1500 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
1501 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
1502 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
1503 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
1504 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00
1505 };
1506
1507 uint8_t output[S2N_TEST_OUTPUT_SIZE] = { 0 };
1508 uint32_t output_size = 0;
1509 EXPECT_OK(s2n_test_ja4_hash_from_bytes(
1510 packet_bytes, sizeof(packet_bytes),
1511 sizeof(output), output, &output_size));
1512
1513 const char expected[] = "t13d1715h2_5b57614c22b0_3d5424432f57";
1514 EXPECT_EQUAL(strlen(expected), output_size);
1515 EXPECT_BYTEARRAY_EQUAL(expected, output, output_size);
1516 };
1517 };
1518
1519 END_TEST();
1520 }