Debian Codegraph @

crt/s2n/tests/unit/s2n_fingerprint_test.c

raw symbols SCIP
1 /*
2 * Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved.
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License").
5 * You may not use this file except in compliance with the License.
6 * A copy of the License is located at
7 *
8 * http://aws.amazon.com/apache2.0
9 *
10 * or in the "license" file accompanying this file. This file is distributed
11 * on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either
12 * express or implied. See the License for the specific language governing
13 * permissions and limitations under the License.
14 */
15
16 #include "tls/s2n_fingerprint.h"
17
18 #include "api/unstable/fingerprint.h"
19 #include "crypto/s2n_hash.h"
20 #include "s2n_test.h"
21 #include "testlib/s2n_testlib.h"
22
23 #define S2N_TEST_HASH S2N_HASH_SHA256
24 #define TEST_COUNT 10
25
26 #define S2N_TEST_OUTPUT_SIZE 100
27
28 static S2N_RESULT s2n_test_hash_state_new(struct s2n_hash_state *hash_state)
29 {
30 EXPECT_SUCCESS(s2n_hash_new(hash_state));
31 EXPECT_SUCCESS(s2n_hash_init(hash_state, S2N_TEST_HASH));
32 return S2N_RESULT_OK;
33 }
34
35 int main(int argc, char **argv)
36 {
37 BEGIN_TEST();
38
39 const char test_char = '!';
40 const char test_str[] = "hello";
41 const size_t test_str_len = strlen(test_str);
42 EXPECT_NOT_EQUAL(test_char, test_str[0]);
43
44 const uint8_t test_str_digest[] = {
45 0x2c, 0xf2, 0x4d, 0xba, 0x5f, 0xb0, 0xa3, 0xe, 0x26, 0xe8, 0x3b,
46 0x2a, 0xc5, 0xb9, 0xe2, 0x9e, 0x1b, 0x16, 0x1e, 0x5c, 0x1f, 0xa7,
47 0x42, 0x5e, 0x73, 0x4, 0x33, 0x62, 0x93, 0x8b, 0x98, 0x24
48 };
49
50 /* clang-format off */
51 const uint8_t raw_client_hello[] = {
52 /* message type */
53 TLS_CLIENT_HELLO,
54 /* message size */
55 0x00, 0x00, 43,
56 /* protocol version - TLS1.2 */
57 0x03, 0x03,
58 /* random */
59 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
60 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
61 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
62 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
63 /* session id */
64 0x00,
65 /* cipher suites */
66 0x00, 0x02,
67 TLS_ECDHE_RSA_WITH_AES_128_GCM_SHA256,
68 /* legacy compression methods */
69 0x01, 0x00,
70 /* extensions - empty */
71 0x00, 0x00,
72 };
73 /* clang-format on */
74 DEFER_CLEANUP(struct s2n_client_hello *client_hello = NULL, s2n_client_hello_free);
75 client_hello = s2n_client_hello_parse_message(raw_client_hello, sizeof(raw_client_hello));
76 EXPECT_NOT_NULL(client_hello);
77 const uint32_t client_hello_ja3_full_size = 12;
78
79 /* Test s2n_fingerprint_hash_add_char */
80 {
81 /* Safety */
82 EXPECT_ERROR_WITH_ERRNO(s2n_fingerprint_hash_add_char(NULL, test_char),
83 S2N_ERR_NULL);
84
85 /* Add to stuffer */
86 {
87 DEFER_CLEANUP(struct s2n_stuffer output = { 0 }, s2n_stuffer_free);
88 EXPECT_SUCCESS(s2n_stuffer_alloc(&output, TEST_COUNT));
89 struct s2n_fingerprint_hash hash = { .buffer = &output };
90
91 for (size_t i = 1; i <= TEST_COUNT; i++) {
92 EXPECT_OK(s2n_fingerprint_hash_add_char(&hash, test_char));
93 EXPECT_EQUAL(s2n_stuffer_data_available(&output), 1);
94
95 char actual_value = 0;
96 EXPECT_SUCCESS(s2n_stuffer_read_char(&output, &actual_value));
97 EXPECT_EQUAL(actual_value, test_char);
98 }
99 };
100
101 /* Add to hash */
102 {
103 DEFER_CLEANUP(struct s2n_hash_state hash_state = { 0 }, s2n_hash_free);
104 EXPECT_OK(s2n_test_hash_state_new(&hash_state));
105 struct s2n_fingerprint_hash hash = { .hash = &hash_state };
106
107 for (size_t i = 1; i <= TEST_COUNT; i++) {
108 EXPECT_OK(s2n_fingerprint_hash_add_char(&hash, test_char));
109 EXPECT_EQUAL(hash.hash->currently_in_hash, i);
110 }
111 };
112
113 /* Error due to insufficient space */
114 {
115 DEFER_CLEANUP(struct s2n_stuffer output = { 0 }, s2n_stuffer_free);
116 struct s2n_fingerprint_hash hash = { .buffer = &output };
117 EXPECT_ERROR_WITH_ERRNO(s2n_fingerprint_hash_add_char(&hash, test_char),
118 S2N_ERR_INSUFFICIENT_MEM_SIZE);
119
120 EXPECT_SUCCESS(s2n_stuffer_alloc(&output, 1));
121 EXPECT_OK(s2n_fingerprint_hash_add_char(&hash, test_char));
122 EXPECT_ERROR_WITH_ERRNO(s2n_fingerprint_hash_add_char(&hash, test_char),
123 S2N_ERR_INSUFFICIENT_MEM_SIZE);
124 };
125 };
126
127 /* Test s2n_fingerprint_hash_add_str */
128 {
129 /* Safety */
130 {
131 /* Null hash */
132 EXPECT_ERROR_WITH_ERRNO(s2n_fingerprint_hash_add_str(NULL, test_str, 0),
133 S2N_ERR_NULL);
134
135 /* Null str with stuffer */
136 {
137 DEFER_CLEANUP(struct s2n_stuffer output = { 0 }, s2n_stuffer_free);
138 EXPECT_SUCCESS(s2n_stuffer_alloc(&output, 100));
139 struct s2n_fingerprint_hash hash = { .buffer = &output };
140 EXPECT_ERROR_WITH_ERRNO(s2n_fingerprint_hash_add_str(&hash, NULL, 10),
141 S2N_ERR_NULL);
142 EXPECT_OK(s2n_fingerprint_hash_add_str(&hash, NULL, 0));
143 };
144
145 /* Null str with hash */
146 {
147 DEFER_CLEANUP(struct s2n_hash_state hash_state = { 0 }, s2n_hash_free);
148 EXPECT_OK(s2n_test_hash_state_new(&hash_state));
149 struct s2n_fingerprint_hash hash = { .hash = &hash_state };
150 EXPECT_ERROR_WITH_ERRNO(s2n_fingerprint_hash_add_str(&hash, NULL, 10),
151 S2N_ERR_NULL);
152 EXPECT_OK(s2n_fingerprint_hash_add_str(&hash, NULL, 0));
153 };
154 };
155
156 /* Add to stuffer */
157 {
158 DEFER_CLEANUP(struct s2n_stuffer output = { 0 }, s2n_stuffer_free);
159 EXPECT_SUCCESS(s2n_stuffer_alloc(&output, test_str_len * TEST_COUNT));
160 struct s2n_fingerprint_hash hash = { .buffer = &output };
161
162 for (size_t i = 1; i <= TEST_COUNT; i++) {
163 EXPECT_OK(s2n_fingerprint_hash_add_str(&hash, test_str, test_str_len));
164 EXPECT_EQUAL(s2n_stuffer_data_available(&output), test_str_len);
165
166 uint8_t actual_value[sizeof(test_str)] = { 0 };
167 EXPECT_SUCCESS(s2n_stuffer_read_bytes(&output, actual_value, test_str_len));
168 EXPECT_BYTEARRAY_EQUAL(actual_value, test_str, test_str_len);
169 }
170 };
171
172 /* Add to hash */
173 {
174 DEFER_CLEANUP(struct s2n_hash_state hash_state = { 0 }, s2n_hash_free);
175 EXPECT_OK(s2n_test_hash_state_new(&hash_state));
176 struct s2n_fingerprint_hash hash = { .hash = &hash_state };
177
178 for (size_t i = 1; i <= TEST_COUNT; i++) {
179 EXPECT_OK(s2n_fingerprint_hash_add_str(&hash, test_str, test_str_len));
180 EXPECT_EQUAL(hash.hash->currently_in_hash, test_str_len * i);
181 }
182 };
183
184 /* Error due to insufficient space */
185 {
186 struct s2n_stuffer output = { 0 };
187 struct s2n_fingerprint_hash hash = { .buffer = &output };
188 EXPECT_ERROR_WITH_ERRNO(
189 s2n_fingerprint_hash_add_str(&hash, test_str, test_str_len),
190 S2N_ERR_INSUFFICIENT_MEM_SIZE);
191
192 EXPECT_SUCCESS(s2n_stuffer_alloc(&output, 1));
193 EXPECT_ERROR_WITH_ERRNO(
194 s2n_fingerprint_hash_add_str(&hash, test_str, test_str_len),
195 S2N_ERR_INSUFFICIENT_MEM_SIZE);
196 EXPECT_SUCCESS(s2n_stuffer_free(&output));
197
198 EXPECT_SUCCESS(s2n_stuffer_alloc(&output, test_str_len - 1));
199 EXPECT_ERROR_WITH_ERRNO(
200 s2n_fingerprint_hash_add_str(&hash, test_str, test_str_len),
201 S2N_ERR_INSUFFICIENT_MEM_SIZE);
202 EXPECT_SUCCESS(s2n_stuffer_free(&output));
203
204 EXPECT_SUCCESS(s2n_stuffer_alloc(&output, test_str_len));
205 EXPECT_OK(s2n_fingerprint_hash_add_char(&hash, test_char));
206 EXPECT_ERROR_WITH_ERRNO(
207 s2n_fingerprint_hash_add_str(&hash, test_str, test_str_len),
208 S2N_ERR_INSUFFICIENT_MEM_SIZE);
209 EXPECT_SUCCESS(s2n_stuffer_free(&output));
210 };
211 };
212
213 /* Test s2n_fingerprint_hash_digest */
214 {
215 /* Safety */
216 {
217 struct s2n_fingerprint_hash hash = { 0 };
218 struct s2n_blob output = { 0 };
219 EXPECT_ERROR_WITH_ERRNO(s2n_fingerprint_hash_digest(NULL, &output), S2N_ERR_NULL);
220 EXPECT_ERROR_WITH_ERRNO(s2n_fingerprint_hash_digest(&hash, NULL), S2N_ERR_NULL);
221 };
222
223 /* Digest successfully calculated */
224 {
225 DEFER_CLEANUP(struct s2n_hash_state hash_state = { 0 }, s2n_hash_free);
226 EXPECT_OK(s2n_test_hash_state_new(&hash_state));
227 struct s2n_fingerprint_hash hash = { .hash = &hash_state };
228
229 EXPECT_OK(s2n_fingerprint_hash_add_str(&hash, test_str, test_str_len));
230 EXPECT_EQUAL(hash.hash->currently_in_hash, test_str_len);
231
232 uint8_t digest_bytes[sizeof(test_str_digest)] = { 0 };
233 struct s2n_blob actual_digest = { 0 };
234 EXPECT_SUCCESS(s2n_blob_init(&actual_digest, digest_bytes, sizeof(digest_bytes)));
235
236 EXPECT_OK(s2n_fingerprint_hash_digest(&hash, &actual_digest));
237 EXPECT_BYTEARRAY_EQUAL(test_str_digest, actual_digest.data, actual_digest.size);
238 EXPECT_EQUAL(test_str_len, hash.bytes_digested);
239 };
240
241 /* Hash can be reused after digest */
242 {
243 DEFER_CLEANUP(struct s2n_hash_state hash_state = { 0 }, s2n_hash_free);
244 EXPECT_OK(s2n_test_hash_state_new(&hash_state));
245 struct s2n_fingerprint_hash hash = { .hash = &hash_state };
246
247 const size_t count = 10;
248 for (size_t i = 0; i < count; i++) {
249 uint8_t digest_bytes[sizeof(test_str_digest)] = { 0 };
250 struct s2n_blob actual_digest = { 0 };
251 EXPECT_SUCCESS(s2n_blob_init(&actual_digest, digest_bytes, sizeof(digest_bytes)));
252
253 EXPECT_OK(s2n_fingerprint_hash_add_str(&hash, test_str, test_str_len));
254 EXPECT_OK(s2n_fingerprint_hash_digest(&hash, &actual_digest));
255
256 EXPECT_BYTEARRAY_EQUAL(test_str_digest, actual_digest.data, actual_digest.size);
257 }
258 EXPECT_EQUAL(hash.bytes_digested, test_str_len * count);
259 };
260 };
261
262 /* Test s2n_fingerprint_hash_do_digest */
263 {
264 /* Safety */
265 EXPECT_FALSE(s2n_fingerprint_hash_do_digest(NULL));
266
267 struct s2n_fingerprint_hash hash = { 0 };
268 EXPECT_FALSE(s2n_fingerprint_hash_do_digest(&hash));
269
270 struct s2n_stuffer output = { 0 };
271 hash.buffer = &output;
272 EXPECT_FALSE(s2n_fingerprint_hash_do_digest(&hash));
273
274 struct s2n_hash_state hash_state = { 0 };
275 hash.hash = &hash_state;
276 EXPECT_TRUE(s2n_fingerprint_hash_do_digest(&hash));
277 };
278
279 /* Test s2n_assert_grease_value */
280 {
281 EXPECT_TRUE(s2n_fingerprint_is_grease_value(0x0A0A));
282 EXPECT_TRUE(s2n_fingerprint_is_grease_value(0xFAFA));
283 EXPECT_FALSE(s2n_fingerprint_is_grease_value(0x0000));
284 EXPECT_FALSE(s2n_fingerprint_is_grease_value(0x0001));
285 };
286
287 /* Test s2n_fingerprint_new / s2n_fingerprint_free */
288 {
289 /* New fails for an invalid fingerprint method */
290 EXPECT_NULL_WITH_ERRNO(s2n_fingerprint_new(-1), S2N_ERR_INVALID_ARGUMENT);
291
292 /* Free is a no-op for a NULL pointer */
293 EXPECT_SUCCESS(s2n_fingerprint_free(NULL));
294
295 /* New succeeds for a valid fingerprint method */
296 struct s2n_fingerprint *fingerprint = s2n_fingerprint_new(S2N_FINGERPRINT_JA3);
297 EXPECT_NOT_NULL(fingerprint);
298 EXPECT_EQUAL(fingerprint->method, &ja3_fingerprint);
299 EXPECT_TRUE(s2n_hash_is_ready_for_input(&fingerprint->hash));
300 EXPECT_NULL(fingerprint->client_hello);
301 EXPECT_EQUAL(fingerprint->raw_size, 0);
302
303 /* Free cleans up the fingerprint */
304 EXPECT_SUCCESS(s2n_fingerprint_free(&fingerprint));
305 EXPECT_NULL(fingerprint);
306
307 /* Free succeeds again for a freed / NULL fingerprint */
308 EXPECT_SUCCESS(s2n_fingerprint_free(&fingerprint));
309 };
310
311 /* Test s2n_fingerprint_wipe */
312 {
313 /* Safety */
314 EXPECT_FAILURE_WITH_ERRNO(s2n_fingerprint_wipe(NULL), S2N_ERR_INVALID_ARGUMENT);
315
316 /* Initialize an invalid fingerprint with every field set to all 1s */
317 struct s2n_fingerprint fingerprint = { 0 };
318 EXPECT_NOT_NULL(memset(&fingerprint, 1, sizeof(struct s2n_fingerprint)));
319 EXPECT_NOT_NULL(fingerprint.method);
320 EXPECT_TRUE(fingerprint.hash.is_ready_for_input);
321 EXPECT_NOT_NULL(fingerprint.client_hello);
322 EXPECT_NOT_EQUAL(fingerprint.raw_size, 0);
323
324 /* Verify that wipe only clears the expected fields */
325 EXPECT_SUCCESS(s2n_fingerprint_wipe(&fingerprint));
326 EXPECT_NOT_NULL(fingerprint.method);
327 EXPECT_TRUE(fingerprint.hash.is_ready_for_input);
328 EXPECT_NULL(fingerprint.client_hello);
329 EXPECT_EQUAL(fingerprint.raw_size, 0);
330 };
331
332 /* Test s2n_fingerprint_set_client_hello */
333 {
334 /* Safety */
335 {
336 struct s2n_fingerprint fingerprint = { 0 };
337 EXPECT_FAILURE_WITH_ERRNO(
338 s2n_fingerprint_set_client_hello(&fingerprint, NULL),
339 S2N_ERR_INVALID_ARGUMENT);
340 EXPECT_FAILURE_WITH_ERRNO(
341 s2n_fingerprint_set_client_hello(NULL, client_hello),
342 S2N_ERR_INVALID_ARGUMENT);
343 };
344
345 /* SSLv2 not allowed */
346 {
347 DEFER_CLEANUP(struct s2n_fingerprint *fingerprint = s2n_fingerprint_new(S2N_FINGERPRINT_JA3),
348 s2n_fingerprint_free);
349 EXPECT_NOT_NULL(fingerprint);
350
351 struct s2n_client_hello sslv2_client_hello = *client_hello;
352 sslv2_client_hello.sslv2 = true;
353
354 EXPECT_FAILURE_WITH_ERRNO(
355 s2n_fingerprint_set_client_hello(fingerprint, &sslv2_client_hello),
356 S2N_ERR_PROTOCOL_VERSION_UNSUPPORTED);
357 };
358
359 /* Success */
360 {
361 DEFER_CLEANUP(struct s2n_fingerprint *fingerprint = s2n_fingerprint_new(S2N_FINGERPRINT_JA3),
362 s2n_fingerprint_free);
363 EXPECT_NOT_NULL(fingerprint);
364 EXPECT_SUCCESS(s2n_fingerprint_set_client_hello(fingerprint, client_hello));
365 EXPECT_EQUAL(fingerprint->client_hello, client_hello);
366 };
367
368 /* Other values reset */
369 {
370 struct s2n_fingerprint fingerprint = { 0 };
371
372 /* Initialize an invalid fingerprint with every field set to all 1s */
373 EXPECT_NOT_NULL(memset(&fingerprint, 1, sizeof(struct s2n_fingerprint)));
374 EXPECT_NOT_EQUAL(fingerprint.client_hello, client_hello);
375 EXPECT_NOT_EQUAL(fingerprint.raw_size, 0);
376
377 /* Setting the client hello resets stateful values */
378 EXPECT_SUCCESS(s2n_fingerprint_set_client_hello(&fingerprint, client_hello));
379 EXPECT_EQUAL(fingerprint.client_hello, client_hello);
380 EXPECT_EQUAL(fingerprint.raw_size, 0);
381 };
382 };
383
384 /* Test s2n_fingerprint_get_hash_size */
385 {
386 /* Safety */
387 {
388 DEFER_CLEANUP(struct s2n_fingerprint *fingerprint = s2n_fingerprint_new(S2N_FINGERPRINT_JA3),
389 s2n_fingerprint_free);
390
391 EXPECT_FAILURE_WITH_ERRNO(
392 s2n_fingerprint_get_hash_size(fingerprint, NULL),
393 S2N_ERR_INVALID_ARGUMENT);
394
395 uint32_t size = 0;
396 EXPECT_FAILURE_WITH_ERRNO(
397 s2n_fingerprint_get_hash_size(NULL, &size),
398 S2N_ERR_INVALID_ARGUMENT);
399 };
400
401 /* Success */
402 {
403 DEFER_CLEANUP(struct s2n_fingerprint *fingerprint = s2n_fingerprint_new(S2N_FINGERPRINT_JA3),
404 s2n_fingerprint_free);
405 uint32_t actual_size = 0;
406 EXPECT_SUCCESS(s2n_fingerprint_get_hash_size(fingerprint, &actual_size));
407 EXPECT_EQUAL(actual_size, ja3_fingerprint.hash_str_size);
408 };
409 };
410
411 /* Test s2n_fingerprint_get_hash */
412 {
413 /* Safety */
414 {
415 DEFER_CLEANUP(struct s2n_fingerprint *fingerprint = s2n_fingerprint_new(S2N_FINGERPRINT_JA3),
416 s2n_fingerprint_free);
417 uint8_t output[S2N_TEST_OUTPUT_SIZE] = { 0 };
418 uint32_t output_size = 0;
419
420 EXPECT_FAILURE_WITH_ERRNO(
421 s2n_fingerprint_get_hash(NULL, sizeof(output), output, &output_size),
422 S2N_ERR_INVALID_ARGUMENT);
423 EXPECT_FAILURE_WITH_ERRNO(
424 s2n_fingerprint_get_hash(fingerprint, sizeof(output), NULL, &output_size),
425 S2N_ERR_INVALID_ARGUMENT);
426 EXPECT_FAILURE_WITH_ERRNO(
427 s2n_fingerprint_get_hash(fingerprint, sizeof(output), output, NULL),
428 S2N_ERR_INVALID_ARGUMENT);
429 };
430
431 /* No ClientHello set */
432 {
433 DEFER_CLEANUP(struct s2n_fingerprint *fingerprint = s2n_fingerprint_new(S2N_FINGERPRINT_JA3),
434 s2n_fingerprint_free);
435 uint8_t output[S2N_TEST_OUTPUT_SIZE] = { 0 };
436 uint32_t output_size = 0;
437
438 EXPECT_FAILURE_WITH_ERRNO(
439 s2n_fingerprint_get_hash(fingerprint, sizeof(output), output, &output_size),
440 S2N_ERR_INVALID_STATE);
441 };
442
443 /* Success */
444 {
445 DEFER_CLEANUP(struct s2n_fingerprint *fingerprint = s2n_fingerprint_new(S2N_FINGERPRINT_JA3),
446 s2n_fingerprint_free);
447 EXPECT_SUCCESS(s2n_fingerprint_set_client_hello(fingerprint, client_hello));
448
449 uint8_t output[S2N_TEST_OUTPUT_SIZE] = { 0 };
450 uint32_t output_size = 0;
451 EXPECT_SUCCESS(s2n_fingerprint_get_hash(fingerprint,
452 sizeof(output), output, &output_size));
453
454 EXPECT_EQUAL(output_size, ja3_fingerprint.hash_str_size);
455 };
456
457 /* Insufficient memory */
458 {
459 DEFER_CLEANUP(struct s2n_fingerprint *fingerprint = s2n_fingerprint_new(S2N_FINGERPRINT_JA3),
460 s2n_fingerprint_free);
461 EXPECT_SUCCESS(s2n_fingerprint_set_client_hello(fingerprint, client_hello));
462
463 uint8_t output[S2N_TEST_OUTPUT_SIZE] = { 0 };
464 uint32_t output_size = 0;
465 EXPECT_TRUE(ja3_fingerprint.hash_str_size <= sizeof(output));
466
467 EXPECT_FAILURE_WITH_ERRNO(
468 s2n_fingerprint_get_hash(fingerprint, 0, output, &output_size),
469 S2N_ERR_INSUFFICIENT_MEM_SIZE);
470 EXPECT_FAILURE_WITH_ERRNO(
471 s2n_fingerprint_get_hash(fingerprint, ja3_fingerprint.hash_str_size - 1, output, &output_size),
472 S2N_ERR_INSUFFICIENT_MEM_SIZE);
473 EXPECT_SUCCESS(
474 s2n_fingerprint_get_hash(fingerprint, ja3_fingerprint.hash_str_size, output, &output_size));
475 };
476 };
477
478 /* Test s2n_fingerprint_get_raw_size */
479 {
480 /* Safety */
481 {
482 DEFER_CLEANUP(struct s2n_fingerprint *fingerprint = s2n_fingerprint_new(S2N_FINGERPRINT_JA3),
483 s2n_fingerprint_free);
484
485 EXPECT_FAILURE_WITH_ERRNO(
486 s2n_fingerprint_get_raw_size(fingerprint, NULL),
487 S2N_ERR_INVALID_ARGUMENT);
488
489 uint32_t size = 0;
490 EXPECT_FAILURE_WITH_ERRNO(
491 s2n_fingerprint_get_raw_size(NULL, &size),
492 S2N_ERR_INVALID_ARGUMENT);
493 };
494
495 /* Raw size not yet calculated */
496 {
497 DEFER_CLEANUP(struct s2n_fingerprint *fingerprint = s2n_fingerprint_new(S2N_FINGERPRINT_JA3),
498 s2n_fingerprint_free);
499 uint32_t actual_size = 0;
500 EXPECT_FAILURE_WITH_ERRNO(
501 s2n_fingerprint_get_raw_size(fingerprint, &actual_size),
502 S2N_ERR_INVALID_STATE);
503 };
504
505 /* Success */
506 {
507 DEFER_CLEANUP(struct s2n_fingerprint *fingerprint = s2n_fingerprint_new(S2N_FINGERPRINT_JA3),
508 s2n_fingerprint_free);
509 EXPECT_SUCCESS(s2n_fingerprint_set_client_hello(fingerprint, client_hello));
510
511 uint8_t hash[S2N_TEST_OUTPUT_SIZE] = { 0 };
512 uint32_t hash_size = 0;
513 EXPECT_SUCCESS(s2n_fingerprint_get_hash(fingerprint,
514 sizeof(hash), hash, &hash_size));
515
516 uint32_t raw_size = 0;
517 EXPECT_SUCCESS(s2n_fingerprint_get_raw_size(fingerprint, &raw_size));
518 EXPECT_EQUAL(raw_size, client_hello_ja3_full_size);
519 };
520 };
521
522 /* Test s2n_fingerprint_get_raw */
523 {
524 /* Safety */
525 {
526 DEFER_CLEANUP(struct s2n_fingerprint *fingerprint = s2n_fingerprint_new(S2N_FINGERPRINT_JA3),
527 s2n_fingerprint_free);
528 uint8_t output[S2N_TEST_OUTPUT_SIZE] = { 0 };
529 uint32_t output_size = 0;
530
531 EXPECT_FAILURE_WITH_ERRNO(
532 s2n_fingerprint_get_raw(NULL, sizeof(output), output, &output_size),
533 S2N_ERR_INVALID_ARGUMENT);
534 EXPECT_FAILURE_WITH_ERRNO(
535 s2n_fingerprint_get_raw(fingerprint, sizeof(output), NULL, &output_size),
536 S2N_ERR_INVALID_ARGUMENT);
537 EXPECT_FAILURE_WITH_ERRNO(
538 s2n_fingerprint_get_raw(fingerprint, sizeof(output), output, NULL),
539 S2N_ERR_INVALID_ARGUMENT);
540 };
541
542 /* Success */
543 {
544 DEFER_CLEANUP(struct s2n_fingerprint *fingerprint = s2n_fingerprint_new(S2N_FINGERPRINT_JA3),
545 s2n_fingerprint_free);
546 EXPECT_SUCCESS(s2n_fingerprint_set_client_hello(fingerprint, client_hello));
547
548 uint8_t output[S2N_TEST_OUTPUT_SIZE] = { 0 };
549 uint32_t output_size = 0;
550 EXPECT_SUCCESS(s2n_fingerprint_get_raw(fingerprint,
551 sizeof(output), output, &output_size));
552
553 EXPECT_EQUAL(output_size, client_hello_ja3_full_size);
554 };
555
556 /* Insufficient memory */
557 {
558 DEFER_CLEANUP(struct s2n_fingerprint *fingerprint = s2n_fingerprint_new(S2N_FINGERPRINT_JA3),
559 s2n_fingerprint_free);
560 EXPECT_SUCCESS(s2n_fingerprint_set_client_hello(fingerprint, client_hello));
561
562 uint8_t output[S2N_TEST_OUTPUT_SIZE] = { 0 };
563 uint32_t output_size = 0;
564 EXPECT_TRUE(client_hello_ja3_full_size <= sizeof(output));
565
566 EXPECT_FAILURE_WITH_ERRNO(
567 s2n_fingerprint_get_raw(fingerprint, 0, output, &output_size),
568 S2N_ERR_INSUFFICIENT_MEM_SIZE);
569 EXPECT_FAILURE_WITH_ERRNO(
570 s2n_fingerprint_get_raw(fingerprint, client_hello_ja3_full_size - 1, output, &output_size),
571 S2N_ERR_INSUFFICIENT_MEM_SIZE);
572 EXPECT_SUCCESS(
573 s2n_fingerprint_get_raw(fingerprint, client_hello_ja3_full_size, output, &output_size));
574 };
575 };
576
577 /* Test new s2n_fingerprint methods match old s2n_client_hello_get_fingerprint_hash method */
578 {
579 DEFER_CLEANUP(struct s2n_fingerprint *fingerprint = s2n_fingerprint_new(S2N_FINGERPRINT_JA3),
580 s2n_fingerprint_free);
581 EXPECT_SUCCESS(s2n_fingerprint_set_client_hello(fingerprint, client_hello));
582
583 uint8_t legacy_output[S2N_TEST_OUTPUT_SIZE] = { 0 };
584 uint32_t legacy_output_size = 0;
585 uint32_t legacy_raw_size = 0;
586 EXPECT_SUCCESS(s2n_client_hello_get_fingerprint_hash(client_hello, S2N_FINGERPRINT_JA3,
587 sizeof(legacy_output), legacy_output, &legacy_output_size, &legacy_raw_size));
588
589 /* output matches s2n_fingerprint_get_hash */
590 {
591 uint8_t output[S2N_TEST_OUTPUT_SIZE] = { 0 };
592 uint32_t output_size = 0;
593 EXPECT_SUCCESS(s2n_fingerprint_get_hash(fingerprint,
594 sizeof(output), output, &output_size));
595
596 struct s2n_stuffer output_stuffer = { 0 };
597 EXPECT_SUCCESS(s2n_blob_init(&output_stuffer.blob, output, output_size));
598 EXPECT_SUCCESS(s2n_stuffer_skip_write(&output_stuffer, output_size));
599
600 /* Legacy output is raw bytes, but new output is hex */
601 EXPECT_EQUAL(output_size, legacy_output_size * 2);
602 for (size_t i = 0; i < legacy_output_size; i++) {
603 uint8_t output_byte = 0;
604 EXPECT_OK(s2n_stuffer_read_uint8_hex(&output_stuffer, &output_byte));
605 EXPECT_EQUAL(output_byte, legacy_output[i]);
606 }
607 };
608
609 /* output size matches s2n_fingerprint_get_hash_size */
610 {
611 uint32_t hash_size = 0;
612 EXPECT_SUCCESS(s2n_fingerprint_get_hash_size(fingerprint, &hash_size));
613 EXPECT_EQUAL(hash_size, legacy_output_size * 2);
614 };
615
616 /* raw size matches s2n_fingerprint_get_raw_size */
617 {
618 uint32_t raw_size = 0;
619 EXPECT_SUCCESS(s2n_fingerprint_get_raw_size(fingerprint, &raw_size));
620 EXPECT_EQUAL(raw_size, legacy_raw_size);
621 };
622 };
623
624 /* Test new s2n_fingerprint methods match old s2n_client_hello_get_fingerprint_string method */
625 {
626 DEFER_CLEANUP(struct s2n_fingerprint *fingerprint = s2n_fingerprint_new(S2N_FINGERPRINT_JA3),
627 s2n_fingerprint_free);
628 EXPECT_SUCCESS(s2n_fingerprint_set_client_hello(fingerprint, client_hello));
629
630 uint8_t legacy_output[S2N_TEST_OUTPUT_SIZE] = { 0 };
631 uint32_t legacy_output_size = 0;
632 EXPECT_SUCCESS(s2n_client_hello_get_fingerprint_string(client_hello, S2N_FINGERPRINT_JA3,
633 sizeof(legacy_output), legacy_output, &legacy_output_size));
634
635 /* output matches s2n_fingerprint_get_raw */
636 {
637 uint8_t output[S2N_TEST_OUTPUT_SIZE] = { 0 };
638 uint32_t output_size = 0;
639 EXPECT_SUCCESS(s2n_fingerprint_get_raw(fingerprint,
640 sizeof(output), output, &output_size));
641
642 EXPECT_EQUAL(output_size, legacy_output_size);
643 EXPECT_BYTEARRAY_EQUAL(output, legacy_output, legacy_output_size);
644 };
645
646 /* output size matches s2n_fingerprint_get_raw_size */
647 {
648 uint32_t raw_size = 0;
649 EXPECT_SUCCESS(s2n_fingerprint_get_raw_size(fingerprint, &raw_size));
650 EXPECT_EQUAL(raw_size, legacy_output_size);
651 };
652 };
653
654 /* Test reusing fingerprint structures */
655 {
656 DEFER_CLEANUP(struct s2n_fingerprint *fingerprint = s2n_fingerprint_new(S2N_FINGERPRINT_JA3),
657 s2n_fingerprint_free);
658
659 uint8_t raw_client_hello_2[sizeof(raw_client_hello)] = { 0 };
660 EXPECT_MEMCPY_SUCCESS(raw_client_hello_2, raw_client_hello, sizeof(raw_client_hello));
661 /* Make a minor change so that we will calculate a different fingerprint.
662 * This changes the protocol version from 1.2 to 1.3. */
663 raw_client_hello_2[5] = 0x04;
664
665 DEFER_CLEANUP(struct s2n_client_hello *client_hello_2 = NULL, s2n_client_hello_free);
666 client_hello_2 = s2n_client_hello_parse_message(raw_client_hello_2, sizeof(raw_client_hello_2));
667 EXPECT_NOT_NULL(client_hello_2);
668
669 struct s2n_test_hello {
670 struct s2n_client_hello *hello;
671 uint8_t hash[S2N_TEST_OUTPUT_SIZE];
672 uint8_t raw[S2N_TEST_OUTPUT_SIZE];
673 unsigned int init : 1;
674 } hellos[] = {
675 { .hello = client_hello },
676 { .hello = client_hello_2 },
677 };
678
679 for (size_t i = 0; i < 10; i++) {
680 struct s2n_test_hello *test_case = &hellos[i % s2n_array_len(hellos)];
681 EXPECT_SUCCESS(s2n_fingerprint_set_client_hello(fingerprint, test_case->hello));
682
683 /* Verify no state leftover after reset */
684 uint32_t size = 0;
685 EXPECT_FAILURE_WITH_ERRNO(
686 s2n_fingerprint_get_raw_size(fingerprint, &size),
687 S2N_ERR_INVALID_STATE);
688
689 /* Store initial copy of hash / raw string for later comparisons */
690 if (test_case->init == false) {
691 test_case->init = true;
692 EXPECT_SUCCESS(s2n_fingerprint_get_hash(fingerprint,
693 sizeof(test_case->hash), test_case->hash, &size));
694 EXPECT_SUCCESS(s2n_fingerprint_get_raw(fingerprint,
695 sizeof(test_case->raw), test_case->raw, &size));
696 }
697
698 /* Verify all hash calculations match across resets */
699 uint8_t hash_output[S2N_TEST_OUTPUT_SIZE] = { 0 };
700 EXPECT_SUCCESS(s2n_fingerprint_get_hash(fingerprint,
701 sizeof(hash_output), hash_output, &size));
702 EXPECT_BYTEARRAY_EQUAL(hash_output, test_case->hash, S2N_TEST_OUTPUT_SIZE);
703
704 /* Verify all raw string calculations match across resets */
705 uint8_t raw_output[S2N_TEST_OUTPUT_SIZE] = { 0 };
706 EXPECT_SUCCESS(s2n_fingerprint_get_raw(fingerprint,
707 sizeof(raw_output), raw_output, &size));
708 EXPECT_BYTEARRAY_EQUAL(raw_output, test_case->raw, S2N_TEST_OUTPUT_SIZE);
709
710 /* Reset for the next iteration */
711 EXPECT_SUCCESS(s2n_fingerprint_wipe(fingerprint));
712 }
713
714 /* Verify that we actually tested with different hellos */
715 EXPECT_BYTEARRAY_NOT_EQUAL(hellos[0].hash, hellos[1].hash, S2N_TEST_OUTPUT_SIZE);
716 EXPECT_BYTEARRAY_NOT_EQUAL(hellos[0].raw, hellos[1].raw, S2N_TEST_OUTPUT_SIZE);
717 };
718
719 END_TEST();
720 }