| 1 |
|
| 2 |
|
| 3 |
|
| 4 |
|
| 5 |
|
| 6 |
|
| 7 |
|
| 8 |
|
| 9 |
|
| 10 |
|
| 11 |
|
| 12 |
|
| 13 |
|
| 14 |
|
| 15 |
|
| 16 |
#include "tls/extensions/s2n_extension_list.h" |
| 17 |
#include "tls/s2n_fingerprint.h" |
| 18 |
#include "utils/s2n_blob.h" |
| 19 |
#include "utils/s2n_safety.h" |
| 20 |
|
| 21 |
#define S2N_JA3_FIELD_DIV ',' |
| 22 |
#define S2N_JA3_LIST_DIV '-' |
| 23 |
|
| 24 |
|
| 25 |
#define S2N_UINT16_STR_MAX_SIZE 5 |
| 26 |
|
| 27 |
static S2N_RESULT s2n_fingerprint_ja3_digest(struct s2n_fingerprint_hash *hash, |
| 28 |
struct s2n_stuffer *out) |
| 29 |
{ |
| 30 |
if (!s2n_fingerprint_hash_do_digest(hash)) { |
| 31 |
return S2N_RESULT_OK; |
| 32 |
} |
| 33 |
|
| 34 |
uint8_t digest_bytes[MD5_DIGEST_LENGTH] = { 0 }; |
| 35 |
struct s2n_blob digest = { 0 }; |
| 36 |
RESULT_GUARD_POSIX(s2n_blob_init(&digest, digest_bytes, sizeof(digest_bytes))); |
| 37 |
RESULT_GUARD(s2n_fingerprint_hash_digest(hash, &digest)); |
| 38 |
RESULT_GUARD(s2n_stuffer_write_hex(out, &digest)); |
| 39 |
|
| 40 |
return S2N_RESULT_OK; |
| 41 |
} |
| 42 |
|
| 43 |
static S2N_RESULT s2n_fingerprint_ja3_iana(struct s2n_fingerprint_hash *hash, |
| 44 |
bool *is_list, uint16_t iana) |
| 45 |
{ |
| 46 |
if (s2n_fingerprint_is_grease_value(iana)) { |
| 47 |
return S2N_RESULT_OK; |
| 48 |
} |
| 49 |
|
| 50 |
|
| 51 |
|
| 52 |
|
| 53 |
|
| 54 |
if (*is_list) { |
| 55 |
RESULT_GUARD(s2n_fingerprint_hash_add_char(hash, S2N_JA3_LIST_DIV)); |
| 56 |
} else { |
| 57 |
*is_list = true; |
| 58 |
} |
| 59 |
|
| 60 |
|
| 61 |
|
| 62 |
char str[S2N_UINT16_STR_MAX_SIZE + 1] = { 0 }; |
| 63 |
int written = snprintf(str, sizeof(str), "%u", iana); |
| 64 |
RESULT_ENSURE_GT(written, 0); |
| 65 |
RESULT_ENSURE_LTE(written, S2N_UINT16_STR_MAX_SIZE); |
| 66 |
|
| 67 |
RESULT_GUARD(s2n_fingerprint_hash_add_str(hash, str, written)); |
| 68 |
return S2N_RESULT_OK; |
| 69 |
} |
| 70 |
|
| 71 |
static S2N_RESULT s2n_fingerprint_ja3_version(struct s2n_fingerprint_hash *hash, |
| 72 |
struct s2n_client_hello *ch) |
| 73 |
{ |
| 74 |
uint16_t version = 0; |
| 75 |
RESULT_GUARD(s2n_fingerprint_get_legacy_version(ch, &version)); |
| 76 |
|
| 77 |
bool is_list = false; |
| 78 |
RESULT_GUARD(s2n_fingerprint_ja3_iana(hash, &is_list, version)); |
| 79 |
return S2N_RESULT_OK; |
| 80 |
} |
| 81 |
|
| 82 |
static S2N_RESULT s2n_fingerprint_ja3_cipher_suites(struct s2n_fingerprint_hash *hash, |
| 83 |
struct s2n_client_hello *ch) |
| 84 |
{ |
| 85 |
RESULT_ENSURE_REF(ch); |
| 86 |
|
| 87 |
struct s2n_stuffer ciphers = { 0 }; |
| 88 |
RESULT_GUARD_POSIX(s2n_stuffer_init_written(&ciphers, &ch->cipher_suites)); |
| 89 |
|
| 90 |
bool found = false; |
| 91 |
while (s2n_stuffer_data_available(&ciphers)) { |
| 92 |
uint16_t iana = 0; |
| 93 |
RESULT_GUARD_POSIX(s2n_stuffer_read_uint16(&ciphers, &iana)); |
| 94 |
RESULT_GUARD(s2n_fingerprint_ja3_iana(hash, &found, iana)); |
| 95 |
} |
| 96 |
return S2N_RESULT_OK; |
| 97 |
} |
| 98 |
|
| 99 |
static S2N_RESULT s2n_fingerprint_ja3_extensions(struct s2n_fingerprint_hash *hash, |
| 100 |
struct s2n_client_hello *ch) |
| 101 |
{ |
| 102 |
RESULT_ENSURE_REF(ch); |
| 103 |
|
| 104 |
|
| 105 |
|
| 106 |
|
| 107 |
|
| 108 |
struct s2n_stuffer extensions = { 0 }; |
| 109 |
RESULT_GUARD_POSIX(s2n_stuffer_init_written(&extensions, &ch->extensions.raw)); |
| 110 |
|
| 111 |
bool found = false; |
| 112 |
while (s2n_stuffer_data_available(&extensions)) { |
| 113 |
uint16_t iana = 0; |
| 114 |
RESULT_GUARD(s2n_fingerprint_parse_extension(&extensions, &iana)); |
| 115 |
RESULT_GUARD(s2n_fingerprint_ja3_iana(hash, &found, iana)); |
| 116 |
} |
| 117 |
return S2N_RESULT_OK; |
| 118 |
} |
| 119 |
|
| 120 |
static S2N_RESULT s2n_fingerprint_ja3_elliptic_curves(struct s2n_fingerprint_hash *hash, |
| 121 |
struct s2n_client_hello *ch) |
| 122 |
{ |
| 123 |
RESULT_ENSURE_REF(ch); |
| 124 |
|
| 125 |
s2n_parsed_extension *extension = NULL; |
| 126 |
int result = s2n_client_hello_get_parsed_extension(S2N_EXTENSION_SUPPORTED_GROUPS, |
| 127 |
&ch->extensions, &extension); |
| 128 |
if (result != S2N_SUCCESS) { |
| 129 |
return S2N_RESULT_OK; |
| 130 |
} |
| 131 |
|
| 132 |
struct s2n_stuffer elliptic_curves = { 0 }; |
| 133 |
RESULT_GUARD_POSIX(s2n_stuffer_init_written(&elliptic_curves, &extension->extension)); |
| 134 |
RESULT_GUARD_POSIX(s2n_stuffer_skip_read(&elliptic_curves, sizeof(uint16_t))); |
| 135 |
|
| 136 |
bool found = false; |
| 137 |
while (s2n_stuffer_data_available(&elliptic_curves)) { |
| 138 |
uint16_t iana = 0; |
| 139 |
RESULT_GUARD_POSIX(s2n_stuffer_read_uint16(&elliptic_curves, &iana)); |
| 140 |
RESULT_GUARD(s2n_fingerprint_ja3_iana(hash, &found, iana)); |
| 141 |
} |
| 142 |
return S2N_RESULT_OK; |
| 143 |
} |
| 144 |
|
| 145 |
static S2N_RESULT s2n_fingerprint_ja3_point_formats(struct s2n_fingerprint_hash *hash, |
| 146 |
struct s2n_client_hello *ch) |
| 147 |
{ |
| 148 |
RESULT_ENSURE_REF(ch); |
| 149 |
|
| 150 |
s2n_parsed_extension *extension = NULL; |
| 151 |
int result = s2n_client_hello_get_parsed_extension(S2N_EXTENSION_EC_POINT_FORMATS, |
| 152 |
&ch->extensions, &extension); |
| 153 |
if (result != S2N_SUCCESS) { |
| 154 |
return S2N_RESULT_OK; |
| 155 |
} |
| 156 |
|
| 157 |
struct s2n_stuffer point_formats = { 0 }; |
| 158 |
RESULT_GUARD_POSIX(s2n_stuffer_init_written(&point_formats, &extension->extension)); |
| 159 |
RESULT_GUARD_POSIX(s2n_stuffer_skip_read(&point_formats, sizeof(uint8_t))); |
| 160 |
|
| 161 |
bool found = false; |
| 162 |
while (s2n_stuffer_data_available(&point_formats)) { |
| 163 |
uint8_t iana = 0; |
| 164 |
RESULT_GUARD_POSIX(s2n_stuffer_read_uint8(&point_formats, &iana)); |
| 165 |
RESULT_GUARD(s2n_fingerprint_ja3_iana(hash, &found, iana)); |
| 166 |
} |
| 167 |
return S2N_RESULT_OK; |
| 168 |
} |
| 169 |
|
| 170 |
|
| 171 |
|
| 172 |
|
| 173 |
|
| 174 |
|
| 175 |
|
| 176 |
static S2N_RESULT s2n_fingerprint_ja3_write(struct s2n_fingerprint_hash *hash, |
| 177 |
struct s2n_client_hello *ch) |
| 178 |
{ |
| 179 |
RESULT_GUARD(s2n_fingerprint_ja3_version(hash, ch)); |
| 180 |
RESULT_GUARD(s2n_fingerprint_hash_add_char(hash, S2N_JA3_FIELD_DIV)); |
| 181 |
RESULT_GUARD(s2n_fingerprint_ja3_cipher_suites(hash, ch)); |
| 182 |
RESULT_GUARD(s2n_fingerprint_hash_add_char(hash, S2N_JA3_FIELD_DIV)); |
| 183 |
RESULT_GUARD(s2n_fingerprint_ja3_extensions(hash, ch)); |
| 184 |
RESULT_GUARD(s2n_fingerprint_hash_add_char(hash, S2N_JA3_FIELD_DIV)); |
| 185 |
RESULT_GUARD(s2n_fingerprint_ja3_elliptic_curves(hash, ch)); |
| 186 |
RESULT_GUARD(s2n_fingerprint_hash_add_char(hash, S2N_JA3_FIELD_DIV)); |
| 187 |
RESULT_GUARD(s2n_fingerprint_ja3_point_formats(hash, ch)); |
| 188 |
return S2N_RESULT_OK; |
| 189 |
} |
| 190 |
|
| 191 |
S2N_RESULT s2n_fingerprint_ja3(struct s2n_fingerprint *fingerprint, |
| 192 |
struct s2n_fingerprint_hash *hash, struct s2n_stuffer *output) |
| 193 |
{ |
| 194 |
RESULT_ENSURE_REF(fingerprint); |
| 195 |
RESULT_GUARD(s2n_fingerprint_ja3_write(hash, fingerprint->client_hello)); |
| 196 |
RESULT_GUARD(s2n_fingerprint_ja3_digest(hash, output)); |
| 197 |
|
| 198 |
if (s2n_fingerprint_hash_do_digest(hash)) { |
| 199 |
fingerprint->raw_size = hash->bytes_digested; |
| 200 |
} else { |
| 201 |
fingerprint->raw_size = s2n_stuffer_data_available(output); |
| 202 |
} |
| 203 |
return S2N_RESULT_OK; |
| 204 |
} |
| 205 |
|
| 206 |
struct s2n_fingerprint_method ja3_fingerprint = { |
| 207 |
|
| 208 |
|
| 209 |
.hash = S2N_HASH_MD5, |
| 210 |
|
| 211 |
.hash_str_size = S2N_JA3_HASH_STR_SIZE, |
| 212 |
.fingerprint = s2n_fingerprint_ja3, |
| 213 |
}; |