| 1 |
|
| 2 |
|
| 3 |
|
| 4 |
|
| 5 |
|
| 6 |
|
| 7 |
|
| 8 |
|
| 9 |
|
| 10 |
|
| 11 |
|
| 12 |
|
| 13 |
|
| 14 |
|
| 15 |
|
| 16 |
#include <ctype.h> |
| 17 |
|
| 18 |
#include "crypto/s2n_hash.h" |
| 19 |
#include "stuffer/s2n_stuffer.h" |
| 20 |
#include "tls/extensions/s2n_client_supported_versions.h" |
| 21 |
#include "tls/extensions/s2n_extension_list.h" |
| 22 |
#include "tls/s2n_client_hello.h" |
| 23 |
#include "tls/s2n_fingerprint.h" |
| 24 |
#include "tls/s2n_protocol_preferences.h" |
| 25 |
#include "utils/s2n_blob.h" |
| 26 |
#include "utils/s2n_safety.h" |
| 27 |
|
| 28 |
#define S2N_JA4_LIST_DIV ',' |
| 29 |
#define S2N_JA4_PART_DIV '_' |
| 30 |
|
| 31 |
|
| 32 |
|
| 33 |
|
| 34 |
|
| 35 |
|
| 36 |
|
| 37 |
|
| 38 |
#define S2N_JA4_COUNT_SIZE 2 |
| 39 |
|
| 40 |
#define S2N_HEX_PER_BYTE 2 |
| 41 |
#define S2N_JA4_DIGEST_HEX_CHAR_LIMIT 12 |
| 42 |
#define S2N_JA4_DIGEST_BYTE_LIMIT (S2N_JA4_DIGEST_HEX_CHAR_LIMIT / S2N_HEX_PER_BYTE) |
| 43 |
|
| 44 |
#define S2N_JA4_A_SIZE 10 |
| 45 |
#define S2N_JA4_B_SIZE S2N_JA4_DIGEST_HEX_CHAR_LIMIT |
| 46 |
#define S2N_JA4_C_SIZE S2N_JA4_DIGEST_HEX_CHAR_LIMIT |
| 47 |
#define S2N_JA4_SIZE (S2N_JA4_A_SIZE + 1 + S2N_JA4_B_SIZE + 1 + S2N_JA4_C_SIZE) |
| 48 |
|
| 49 |
#define S2N_JA4_LIST_LIMIT 99 |
| 50 |
#define S2N_JA4_IANA_HEX_SIZE (S2N_HEX_PER_BYTE * sizeof(uint16_t)) |
| 51 |
#define S2N_JA4_IANA_ENTRY_SIZE (S2N_JA4_IANA_HEX_SIZE + 1) |
| 52 |
#define S2N_JA4_WORKSPACE_SIZE ((S2N_JA4_LIST_LIMIT * (S2N_JA4_IANA_ENTRY_SIZE))) |
| 53 |
|
| 54 |
const char *s2n_ja4_version_strings[] = { |
| 55 |
|
| 56 |
|
| 57 |
3” |
| 58 |
2” |
| 59 |
1” |
| 60 |
0” |
| 61 |
|
| 62 |
[0x0304] = "13", |
| 63 |
[0x0303] = "12", |
| 64 |
[0x0302] = "11", |
| 65 |
[0x0301] = "10", |
| 66 |
|
| 67 |
|
| 68 |
3” |
| 69 |
2” |
| 70 |
|
| 71 |
[0x0300] = "s3", |
| 72 |
[0x0002] = "s2", |
| 73 |
}; |
| 74 |
|
| 75 |
|
| 76 |
|
| 77 |
0” |
| 78 |
|
| 79 |
#define S2N_JA4_UNKNOWN_STR "00" |
| 80 |
|
| 81 |
DEFINE_POINTER_CLEANUP_FUNC(struct s2n_stuffer *, s2n_stuffer_wipe); |
| 82 |
|
| 83 |
static int s2n_fingerprint_ja4_iana_compare(const void *a, const void *b) |
| 84 |
{ |
| 85 |
const uint8_t *iana_a = (const uint8_t *) a; |
| 86 |
const uint8_t *iana_b = (const uint8_t *) b; |
| 87 |
for (size_t i = 0; i < S2N_JA4_IANA_HEX_SIZE; i++) { |
| 88 |
if (iana_a[i] != iana_b[i]) { |
| 89 |
return iana_a[i] - iana_b[i]; |
| 90 |
} |
| 91 |
} |
| 92 |
return 0; |
| 93 |
} |
| 94 |
|
| 95 |
static S2N_RESULT s2n_fingerprint_ja4_digest(struct s2n_fingerprint_hash *hash, |
| 96 |
struct s2n_stuffer *out) |
| 97 |
{ |
| 98 |
RESULT_ENSURE_REF(hash); |
| 99 |
if (!s2n_fingerprint_hash_do_digest(hash)) { |
| 100 |
return S2N_RESULT_OK; |
| 101 |
} |
| 102 |
|
| 103 |
|
| 104 |
|
| 105 |
|
| 106 |
|
| 107 |
|
| 108 |
|
| 109 |
|
| 110 |
|
| 111 |
|
| 112 |
|
| 113 |
|
| 114 |
uint64_t bytes = 0; |
| 115 |
RESULT_GUARD_POSIX(s2n_hash_get_currently_in_hash_total(hash->hash, &bytes)); |
| 116 |
if (bytes == 0) { |
| 117 |
RESULT_GUARD_POSIX(s2n_stuffer_write_str(out, "000000000000")); |
| 118 |
return S2N_RESULT_OK; |
| 119 |
} |
| 120 |
|
| 121 |
uint8_t digest_bytes[SHA256_DIGEST_LENGTH] = { 0 }; |
| 122 |
struct s2n_blob digest = { 0 }; |
| 123 |
RESULT_GUARD_POSIX(s2n_blob_init(&digest, digest_bytes, sizeof(digest_bytes))); |
| 124 |
RESULT_GUARD(s2n_fingerprint_hash_digest(hash, &digest)); |
| 125 |
|
| 126 |
|
| 127 |
RESULT_ENSURE_LTE(S2N_JA4_DIGEST_BYTE_LIMIT, digest.size); |
| 128 |
digest.size = S2N_JA4_DIGEST_BYTE_LIMIT; |
| 129 |
RESULT_GUARD(s2n_stuffer_write_hex(out, &digest)); |
| 130 |
return S2N_RESULT_OK; |
| 131 |
} |
| 132 |
|
| 133 |
|
| 134 |
|
| 135 |
es |
| 136 |
”. |
| 137 |
|
| 138 |
|
| 139 |
|
| 140 |
|
| 141 |
static S2N_RESULT s2n_fingerprint_ja4_count(struct s2n_blob *output, uint16_t count) |
| 142 |
{ |
| 143 |
RESULT_ENSURE_REF(output); |
| 144 |
|
| 145 |
|
| 146 |
|
| 147 |
99”. |
| 148 |
|
| 149 |
|
| 150 |
|
| 151 |
|
| 152 |
count = S2N_MIN(count, 99); |
| 153 |
|
| 154 |
RESULT_ENSURE_EQ(output->size, 2); |
| 155 |
output->data[0] = (count / 10) + '0'; |
| 156 |
output->data[1] = (count % 10) + '0'; |
| 157 |
return S2N_RESULT_OK; |
| 158 |
} |
| 159 |
|
| 160 |
static S2N_RESULT s2n_fingerprint_get_extension_version(struct s2n_client_hello *ch, |
| 161 |
uint16_t *client_version) |
| 162 |
{ |
| 163 |
RESULT_ENSURE_REF(ch); |
| 164 |
RESULT_ENSURE_REF(client_version); |
| 165 |
|
| 166 |
s2n_parsed_extension *extension = NULL; |
| 167 |
RESULT_GUARD_POSIX(s2n_client_hello_get_parsed_extension( |
| 168 |
S2N_EXTENSION_SUPPORTED_VERSIONS, &ch->extensions, &extension)); |
| 169 |
RESULT_ENSURE_REF(extension); |
| 170 |
|
| 171 |
struct s2n_stuffer supported_versions = { 0 }; |
| 172 |
RESULT_GUARD_POSIX(s2n_stuffer_init_written(&supported_versions, &extension->extension)); |
| 173 |
|
| 174 |
RESULT_GUARD_POSIX(s2n_stuffer_skip_read(&supported_versions, sizeof(uint8_t))); |
| 175 |
while (s2n_stuffer_data_available(&supported_versions)) { |
| 176 |
uint16_t version = 0; |
| 177 |
RESULT_GUARD_POSIX(s2n_stuffer_read_uint16(&supported_versions, &version)); |
| 178 |
|
| 179 |
|
| 180 |
|
| 181 |
|
| 182 |
if (s2n_fingerprint_is_grease_value(version)) { |
| 183 |
continue; |
| 184 |
} |
| 185 |
|
| 186 |
|
| 187 |
|
| 188 |
|
| 189 |
|
| 190 |
*client_version = S2N_MAX(*client_version, version); |
| 191 |
} |
| 192 |
return S2N_RESULT_OK; |
| 193 |
} |
| 194 |
|
| 195 |
static S2N_RESULT s2n_fingerprint_ja4_version(struct s2n_stuffer *output, |
| 196 |
struct s2n_client_hello *ch) |
| 197 |
{ |
| 198 |
uint16_t client_version = 0; |
| 199 |
if (s2n_result_is_error(s2n_fingerprint_get_extension_version(ch, &client_version))) { |
| 200 |
|
| 201 |
|
| 202 |
of |
| 203 |
|
| 204 |
|
| 205 |
RESULT_GUARD(s2n_fingerprint_get_legacy_version(ch, &client_version)); |
| 206 |
} |
| 207 |
|
| 208 |
|
| 209 |
|
| 210 |
|
| 211 |
|
| 212 |
|
| 213 |
const char *version_str = NULL; |
| 214 |
if (client_version < s2n_array_len(s2n_ja4_version_strings)) { |
| 215 |
version_str = s2n_ja4_version_strings[client_version]; |
| 216 |
} |
| 217 |
if (version_str == NULL) { |
| 218 |
version_str = S2N_JA4_UNKNOWN_STR; |
| 219 |
} |
| 220 |
RESULT_GUARD_POSIX(s2n_stuffer_write_str(output, version_str)); |
| 221 |
|
| 222 |
return S2N_RESULT_OK; |
| 223 |
} |
| 224 |
|
| 225 |
static S2N_RESULT s2n_client_hello_get_first_alpn(struct s2n_client_hello *ch, struct s2n_blob *first) |
| 226 |
{ |
| 227 |
RESULT_ENSURE_REF(ch); |
| 228 |
|
| 229 |
s2n_parsed_extension *extension = NULL; |
| 230 |
RESULT_GUARD_POSIX(s2n_client_hello_get_parsed_extension(S2N_EXTENSION_ALPN, |
| 231 |
&ch->extensions, &extension)); |
| 232 |
RESULT_ENSURE_REF(extension); |
| 233 |
|
| 234 |
struct s2n_stuffer protocols = { 0 }; |
| 235 |
RESULT_GUARD_POSIX(s2n_stuffer_init_written(&protocols, &extension->extension)); |
| 236 |
|
| 237 |
uint16_t list_size = 0; |
| 238 |
RESULT_GUARD_POSIX(s2n_stuffer_read_uint16(&protocols, &list_size)); |
| 239 |
|
| 240 |
RESULT_GUARD(s2n_protocol_preferences_read(&protocols, first)); |
| 241 |
return S2N_RESULT_OK; |
| 242 |
} |
| 243 |
|
| 244 |
|
| 245 |
|
| 246 |
|
| 247 |
|
| 248 |
|
| 249 |
static S2N_RESULT s2n_fingerprint_ja4_alpn(struct s2n_stuffer *output, |
| 250 |
struct s2n_client_hello *ch) |
| 251 |
{ |
| 252 |
struct s2n_blob protocol = { 0 }; |
| 253 |
if (s2n_result_is_error(s2n_client_hello_get_first_alpn(ch, &protocol))) { |
| 254 |
protocol.size = 0; |
| 255 |
} |
| 256 |
|
| 257 |
|
| 258 |
|
| 259 |
|
| 260 |
|
| 261 |
|
| 262 |
|
| 263 |
|
| 264 |
|
| 265 |
|
| 266 |
uint8_t first_char = '0', last_char = '0'; |
| 267 |
if (protocol.size > 0) { |
| 268 |
first_char = protocol.data[0]; |
| 269 |
last_char = protocol.data[protocol.size - 1]; |
| 270 |
} |
| 271 |
|
| 272 |
|
| 273 |
|
| 274 |
|
| 275 |
|
| 276 |
|
| 277 |
|
| 278 |
if (!isalnum(first_char) || !isalnum(last_char)) { |
| 279 |
RESULT_GUARD(s2n_hex_digit((first_char >> 4), &first_char)); |
| 280 |
RESULT_GUARD(s2n_hex_digit((last_char & 0x0F), &last_char)); |
| 281 |
} |
| 282 |
|
| 283 |
RESULT_GUARD_POSIX(s2n_stuffer_write_char(output, first_char)); |
| 284 |
RESULT_GUARD_POSIX(s2n_stuffer_write_char(output, last_char)); |
| 285 |
return S2N_RESULT_OK; |
| 286 |
} |
| 287 |
|
| 288 |
|
| 289 |
|
| 290 |
|
| 291 |
”t”) |
| 292 |
|
| 293 |
”i”) |
| 294 |
|
| 295 |
|
| 296 |
|
| 297 |
|
| 298 |
static S2N_RESULT s2n_fingerprint_ja4_a(struct s2n_fingerprint *fingerprint, |
| 299 |
struct s2n_stuffer *output, struct s2n_blob *ciphers_count, struct s2n_blob *extensions_count) |
| 300 |
{ |
| 301 |
RESULT_ENSURE_REF(fingerprint); |
| 302 |
|
| 303 |
|
| 304 |
|
| 305 |
”, |
| 306 |
”. |
| 307 |
|
| 308 |
|
| 309 |
|
| 310 |
bool is_quic = false; |
| 311 |
RESULT_GUARD_POSIX(s2n_client_hello_has_extension(fingerprint->client_hello, |
| 312 |
TLS_EXTENSION_QUIC_TRANSPORT_PARAMETERS, &is_quic)); |
| 313 |
char protocol_char = (is_quic) ? 'q' : 't'; |
| 314 |
RESULT_GUARD_POSIX(s2n_stuffer_write_char(output, protocol_char)); |
| 315 |
|
| 316 |
RESULT_GUARD(s2n_fingerprint_ja4_version(output, fingerprint->client_hello)); |
| 317 |
|
| 318 |
|
| 319 |
|
| 320 |
|
| 321 |
int. |
| 322 |
”. |
| 323 |
|
| 324 |
bool has_sni = false; |
| 325 |
RESULT_GUARD_POSIX(s2n_client_hello_has_extension(fingerprint->client_hello, |
| 326 |
TLS_EXTENSION_SERVER_NAME, &has_sni)); |
| 327 |
char sni_char = (has_sni) ? 'd' : 'i'; |
| 328 |
RESULT_GUARD_POSIX(s2n_stuffer_write_char(output, sni_char)); |
| 329 |
|
| 330 |
|
| 331 |
|
| 332 |
|
| 333 |
uint8_t *ciphers_count_mem = s2n_stuffer_raw_write(output, S2N_JA4_COUNT_SIZE); |
| 334 |
RESULT_GUARD_PTR(ciphers_count_mem); |
| 335 |
RESULT_GUARD_POSIX(s2n_blob_init(ciphers_count, ciphers_count_mem, S2N_JA4_COUNT_SIZE)); |
| 336 |
|
| 337 |
|
| 338 |
|
| 339 |
|
| 340 |
uint8_t *extensions_count_mem = s2n_stuffer_raw_write(output, S2N_JA4_COUNT_SIZE); |
| 341 |
RESULT_GUARD_PTR(extensions_count_mem); |
| 342 |
RESULT_GUARD_POSIX(s2n_blob_init(extensions_count, extensions_count_mem, S2N_JA4_COUNT_SIZE)); |
| 343 |
|
| 344 |
RESULT_GUARD(s2n_fingerprint_ja4_alpn(output, fingerprint->client_hello)); |
| 345 |
|
| 346 |
return S2N_RESULT_OK; |
| 347 |
} |
| 348 |
|
| 349 |
|
| 350 |
|
| 351 |
|
| 352 |
|
| 353 |
|
| 354 |
static S2N_RESULT s2n_fingerprint_ja4_ciphers(struct s2n_fingerprint_hash *hash, |
| 355 |
struct s2n_client_hello *ch, struct s2n_stuffer *sort_space, uint16_t *ciphers_count) |
| 356 |
{ |
| 357 |
RESULT_ENSURE_REF(ch); |
| 358 |
RESULT_ENSURE_REF(sort_space); |
| 359 |
RESULT_ENSURE_REF(ciphers_count); |
| 360 |
|
| 361 |
struct s2n_stuffer cipher_suites = { 0 }; |
| 362 |
RESULT_GUARD_POSIX(s2n_stuffer_init_written(&cipher_suites, &ch->cipher_suites)); |
| 363 |
|
| 364 |
DEFER_CLEANUP(struct s2n_stuffer *iana_list = sort_space, s2n_stuffer_wipe_pointer); |
| 365 |
while (s2n_stuffer_data_available(&cipher_suites)) { |
| 366 |
uint16_t iana = 0; |
| 367 |
RESULT_GUARD_POSIX(s2n_stuffer_read_uint16(&cipher_suites, &iana)); |
| 368 |
|
| 369 |
|
| 370 |
t. |
| 371 |
|
| 372 |
if (s2n_fingerprint_is_grease_value(iana)) { |
| 373 |
continue; |
| 374 |
} |
| 375 |
RESULT_GUARD(s2n_stuffer_write_uint16_hex(iana_list, iana)); |
| 376 |
RESULT_GUARD_POSIX(s2n_stuffer_write_char(iana_list, S2N_JA4_LIST_DIV)); |
| 377 |
} |
| 378 |
|
| 379 |
size_t iana_list_size = s2n_stuffer_data_available(iana_list); |
| 380 |
size_t iana_count = iana_list_size / S2N_JA4_IANA_ENTRY_SIZE; |
| 381 |
*ciphers_count = iana_count; |
| 382 |
if (iana_count == 0) { |
| 383 |
return S2N_RESULT_OK; |
| 384 |
} |
| 385 |
|
| 386 |
uint8_t *ianas = s2n_stuffer_raw_read(iana_list, iana_list_size); |
| 387 |
RESULT_ENSURE_REF(ianas); |
| 388 |
qsort(ianas, iana_count, S2N_JA4_IANA_ENTRY_SIZE, s2n_fingerprint_ja4_iana_compare); |
| 389 |
RESULT_GUARD(s2n_fingerprint_hash_add_bytes(hash, ianas, iana_list_size - 1)); |
| 390 |
return S2N_RESULT_OK; |
| 391 |
} |
| 392 |
|
| 393 |
|
| 394 |
|
| 395 |
|
| 396 |
|
| 397 |
|
| 398 |
static S2N_RESULT s2n_fingerprint_ja4_b(struct s2n_fingerprint *fingerprint, |
| 399 |
struct s2n_fingerprint_hash *hash, struct s2n_blob *ciphers_count, |
| 400 |
struct s2n_stuffer *output) |
| 401 |
{ |
| 402 |
RESULT_ENSURE_REF(fingerprint); |
| 403 |
|
| 404 |
uint16_t ciphers_count_value = 0; |
| 405 |
RESULT_GUARD(s2n_fingerprint_ja4_ciphers(hash, fingerprint->client_hello, |
| 406 |
&fingerprint->workspace, &ciphers_count_value)); |
| 407 |
|
| 408 |
RESULT_GUARD(s2n_fingerprint_ja4_digest(hash, output)); |
| 409 |
RESULT_GUARD(s2n_fingerprint_ja4_count(ciphers_count, ciphers_count_value)); |
| 410 |
return S2N_RESULT_OK; |
| 411 |
} |
| 412 |
|
| 413 |
|
| 414 |
|
| 415 |
|
| 416 |
|
| 417 |
|
| 418 |
static S2N_RESULT s2n_fingerprint_ja4_extensions(struct s2n_fingerprint_hash *hash, |
| 419 |
struct s2n_client_hello *ch, struct s2n_stuffer *sort_space, uint16_t *extensions_count) |
| 420 |
{ |
| 421 |
RESULT_ENSURE_REF(ch); |
| 422 |
RESULT_ENSURE_REF(sort_space); |
| 423 |
RESULT_ENSURE_REF(extensions_count); |
| 424 |
|
| 425 |
struct s2n_stuffer extensions = { 0 }; |
| 426 |
RESULT_GUARD_POSIX(s2n_stuffer_init_written(&extensions, &ch->extensions.raw)); |
| 427 |
|
| 428 |
DEFER_CLEANUP(struct s2n_stuffer *iana_list = sort_space, s2n_stuffer_wipe_pointer); |
| 429 |
while (s2n_stuffer_data_available(&extensions)) { |
| 430 |
uint16_t iana = 0; |
| 431 |
RESULT_GUARD(s2n_fingerprint_parse_extension(&extensions, &iana)); |
| 432 |
|
| 433 |
|
| 434 |
|
| 435 |
|
| 436 |
|
| 437 |
if (s2n_fingerprint_is_grease_value(iana)) { |
| 438 |
continue; |
| 439 |
} |
| 440 |
|
| 441 |
|
| 442 |
|
| 443 |
|
| 444 |
|
| 445 |
t. |
| 446 |
|
| 447 |
|
| 448 |
|
| 449 |
|
| 450 |
(*extensions_count)++; |
| 451 |
if (iana == TLS_EXTENSION_SERVER_NAME || iana == S2N_EXTENSION_ALPN) { |
| 452 |
continue; |
| 453 |
} |
| 454 |
RESULT_GUARD(s2n_stuffer_write_uint16_hex(iana_list, iana)); |
| 455 |
RESULT_GUARD_POSIX(s2n_stuffer_write_char(iana_list, S2N_JA4_LIST_DIV)); |
| 456 |
} |
| 457 |
|
| 458 |
size_t iana_list_size = s2n_stuffer_data_available(iana_list); |
| 459 |
size_t iana_count = iana_list_size / S2N_JA4_IANA_ENTRY_SIZE; |
| 460 |
if (iana_count == 0) { |
| 461 |
return S2N_RESULT_OK; |
| 462 |
} |
| 463 |
|
| 464 |
uint8_t *ianas = s2n_stuffer_raw_read(iana_list, iana_list_size); |
| 465 |
RESULT_ENSURE_REF(ianas); |
| 466 |
qsort(ianas, iana_count, S2N_JA4_IANA_ENTRY_SIZE, s2n_fingerprint_ja4_iana_compare); |
| 467 |
RESULT_GUARD(s2n_fingerprint_hash_add_bytes(hash, ianas, iana_list_size - 1)); |
| 468 |
return S2N_RESULT_OK; |
| 469 |
} |
| 470 |
|
| 471 |
static S2N_RESULT s2n_fingerprint_ja4_sig_algs(struct s2n_fingerprint_hash *hash, |
| 472 |
struct s2n_client_hello *ch) |
| 473 |
{ |
| 474 |
RESULT_ENSURE_REF(ch); |
| 475 |
|
| 476 |
s2n_parsed_extension *extension = NULL; |
| 477 |
int result = s2n_client_hello_get_parsed_extension(S2N_EXTENSION_SIGNATURE_ALGORITHMS, |
| 478 |
&ch->extensions, &extension); |
| 479 |
if (result != S2N_SUCCESS) { |
| 480 |
return S2N_RESULT_OK; |
| 481 |
} |
| 482 |
RESULT_ENSURE_REF(extension); |
| 483 |
|
| 484 |
struct s2n_stuffer sig_algs = { 0 }; |
| 485 |
RESULT_GUARD_POSIX(s2n_stuffer_init_written(&sig_algs, &extension->extension)); |
| 486 |
|
| 487 |
uint8_t entry_bytes[S2N_JA4_IANA_ENTRY_SIZE] = { 0 }; |
| 488 |
struct s2n_stuffer entry = { 0 }; |
| 489 |
RESULT_GUARD_POSIX(s2n_blob_init(&entry.blob, entry_bytes, sizeof(entry_bytes))); |
| 490 |
|
| 491 |
bool is_first = true; |
| 492 |
if (s2n_stuffer_skip_read(&sig_algs, sizeof(uint16_t)) != S2N_SUCCESS) { |
| 493 |
return S2N_RESULT_OK; |
| 494 |
} |
| 495 |
while (s2n_stuffer_data_available(&sig_algs)) { |
| 496 |
uint16_t iana = 0; |
| 497 |
RESULT_GUARD_POSIX(s2n_stuffer_read_uint16(&sig_algs, &iana)); |
| 498 |
if (s2n_fingerprint_is_grease_value(iana)) { |
| 499 |
continue; |
| 500 |
} |
| 501 |
if (is_first) { |
| 502 |
RESULT_GUARD(s2n_fingerprint_hash_add_char(hash, S2N_JA4_PART_DIV)); |
| 503 |
} else { |
| 504 |
RESULT_GUARD_POSIX(s2n_stuffer_write_char(&entry, S2N_JA4_LIST_DIV)); |
| 505 |
} |
| 506 |
RESULT_GUARD(s2n_stuffer_write_uint16_hex(&entry, iana)); |
| 507 |
RESULT_GUARD(s2n_fingerprint_hash_add_bytes(hash, entry_bytes, |
| 508 |
s2n_stuffer_data_available(&entry))); |
| 509 |
RESULT_GUARD_POSIX(s2n_stuffer_rewrite(&entry)); |
| 510 |
is_first = false; |
| 511 |
} |
| 512 |
return S2N_RESULT_OK; |
| 513 |
} |
| 514 |
|
| 515 |
|
| 516 |
|
| 517 |
|
| 518 |
|
| 519 |
|
| 520 |
|
| 521 |
static S2N_RESULT s2n_fingerprint_ja4_c(struct s2n_fingerprint *fingerprint, |
| 522 |
struct s2n_fingerprint_hash *hash, struct s2n_blob *extensions_count, |
| 523 |
struct s2n_stuffer *output) |
| 524 |
{ |
| 525 |
RESULT_ENSURE_REF(fingerprint); |
| 526 |
|
| 527 |
uint16_t extensions_count_value = 0; |
| 528 |
RESULT_GUARD(s2n_fingerprint_ja4_extensions(hash, fingerprint->client_hello, |
| 529 |
&fingerprint->workspace, &extensions_count_value)); |
| 530 |
|
| 531 |
|
| 532 |
|
| 533 |
|
| 534 |
|
| 535 |
|
| 536 |
|
| 537 |
|
| 538 |
|
| 539 |
|
| 540 |
|
| 541 |
|
| 542 |
|
| 543 |
|
| 544 |
RESULT_GUARD(s2n_fingerprint_ja4_sig_algs(hash, fingerprint->client_hello)); |
| 545 |
|
| 546 |
RESULT_GUARD(s2n_fingerprint_ja4_digest(hash, output)); |
| 547 |
RESULT_GUARD(s2n_fingerprint_ja4_count(extensions_count, extensions_count_value)); |
| 548 |
return S2N_RESULT_OK; |
| 549 |
} |
| 550 |
|
| 551 |
|
| 552 |
|
| 553 |
|
| 554 |
”t”) |
| 555 |
|
| 556 |
”i”) |
| 557 |
|
| 558 |
|
| 559 |
|
| 560 |
|
| 561 |
|
| 562 |
|
| 563 |
|
| 564 |
|
| 565 |
|
| 566 |
|
| 567 |
|
| 568 |
static S2N_RESULT s2n_fingerprint_ja4(struct s2n_fingerprint *fingerprint, |
| 569 |
struct s2n_fingerprint_hash *hash, struct s2n_stuffer *output) |
| 570 |
{ |
| 571 |
RESULT_ENSURE_REF(fingerprint); |
| 572 |
RESULT_ENSURE_REF(hash); |
| 573 |
RESULT_ENSURE_REF(output); |
| 574 |
|
| 575 |
if (s2n_stuffer_is_freed(&fingerprint->workspace)) { |
| 576 |
RESULT_GUARD_POSIX(s2n_stuffer_growable_alloc(&fingerprint->workspace, S2N_JA4_WORKSPACE_SIZE)); |
| 577 |
} |
| 578 |
|
| 579 |
struct s2n_blob ciphers_count = { 0 }; |
| 580 |
struct s2n_blob extensions_count = { 0 }; |
| 581 |
RESULT_GUARD(s2n_fingerprint_ja4_a(fingerprint, output, &ciphers_count, &extensions_count)); |
| 582 |
RESULT_GUARD_POSIX(s2n_stuffer_write_char(output, S2N_JA4_PART_DIV)); |
| 583 |
RESULT_GUARD(s2n_fingerprint_ja4_b(fingerprint, hash, &ciphers_count, output)); |
| 584 |
RESULT_GUARD_POSIX(s2n_stuffer_write_char(output, S2N_JA4_PART_DIV)); |
| 585 |
RESULT_GUARD(s2n_fingerprint_ja4_c(fingerprint, hash, &extensions_count, output)); |
| 586 |
|
| 587 |
if (s2n_fingerprint_hash_do_digest(hash)) { |
| 588 |
|
| 589 |
fingerprint->raw_size = hash->bytes_digested + S2N_JA4_A_SIZE + 2; |
| 590 |
} else { |
| 591 |
fingerprint->raw_size = s2n_stuffer_data_available(output); |
| 592 |
} |
| 593 |
|
| 594 |
return S2N_RESULT_OK; |
| 595 |
} |
| 596 |
|
| 597 |
struct s2n_fingerprint_method ja4_fingerprint = { |
| 598 |
.hash = S2N_HASH_SHA256, |
| 599 |
.hash_str_size = S2N_JA4_SIZE, |
| 600 |
.fingerprint = s2n_fingerprint_ja4, |
| 601 |
}; |