Debian Codegraph @

crt/s2n/tls/s2n_fingerprint.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 "utils/s2n_blob.h"
19 #include "utils/s2n_mem.h"
20 #include "utils/s2n_safety.h"
21
22 static S2N_RESULT s2n_fingerprint_init(struct s2n_fingerprint *fingerprint,
23 s2n_fingerprint_type type)
24 {
25 RESULT_ENSURE_REF(fingerprint);
26
27 switch (type) {
28 case S2N_FINGERPRINT_JA3:
29 fingerprint->method = &ja3_fingerprint;
30 break;
31 case S2N_FINGERPRINT_JA4:
32 fingerprint->method = &ja4_fingerprint;
33 break;
34 default:
35 RESULT_BAIL(S2N_ERR_INVALID_ARGUMENT);
36 }
37
38 const struct s2n_fingerprint_method *method = fingerprint->method;
39 RESULT_ENSURE_REF(method);
40 RESULT_GUARD_POSIX(s2n_hash_new(&fingerprint->hash));
41 RESULT_GUARD_POSIX(s2n_hash_init(&fingerprint->hash, method->hash));
42 return S2N_RESULT_OK;
43 }
44
45 struct s2n_fingerprint *s2n_fingerprint_new(s2n_fingerprint_type type)
46 {
47 DEFER_CLEANUP(struct s2n_blob mem = { 0 }, s2n_free);
48 PTR_GUARD_POSIX(s2n_alloc(&mem, sizeof(struct s2n_fingerprint)));
49 PTR_GUARD_POSIX(s2n_blob_zero(&mem));
50 struct s2n_fingerprint *fingerprint = (struct s2n_fingerprint *) (void *) mem.data;
51 PTR_ENSURE_REF(fingerprint);
52 PTR_GUARD_RESULT(s2n_fingerprint_init(fingerprint, type));
53 ZERO_TO_DISABLE_DEFER_CLEANUP(mem);
54 return fingerprint;
55 }
56
57 static S2N_CLEANUP_RESULT s2n_fingerprint_free_fields(struct s2n_fingerprint *fingerprint)
58 {
59 if (!fingerprint) {
60 return S2N_RESULT_OK;
61 }
62 RESULT_GUARD_POSIX(s2n_hash_free(&fingerprint->hash));
63 RESULT_GUARD_POSIX(s2n_stuffer_free(&fingerprint->workspace));
64 return S2N_RESULT_OK;
65 }
66
67 int s2n_fingerprint_free(struct s2n_fingerprint **fingerprint_ptr)
68 {
69 if (!fingerprint_ptr) {
70 return S2N_SUCCESS;
71 }
72 POSIX_GUARD_RESULT(s2n_fingerprint_free_fields(*fingerprint_ptr));
73 POSIX_GUARD(s2n_free_object((uint8_t **) (void **) fingerprint_ptr,
74 sizeof(struct s2n_fingerprint)));
75 return S2N_SUCCESS;
76 }
77
78 int s2n_fingerprint_wipe(struct s2n_fingerprint *fingerprint)
79 {
80 POSIX_ENSURE(fingerprint, S2N_ERR_INVALID_ARGUMENT);
81 fingerprint->client_hello = NULL;
82 fingerprint->raw_size = 0;
83 return S2N_SUCCESS;
84 }
85
86 int s2n_fingerprint_set_client_hello(struct s2n_fingerprint *fingerprint, struct s2n_client_hello *ch)
87 {
88 POSIX_ENSURE(fingerprint, S2N_ERR_INVALID_ARGUMENT);
89 POSIX_ENSURE(ch, S2N_ERR_INVALID_ARGUMENT);
90 POSIX_ENSURE(!ch->sslv2, S2N_ERR_PROTOCOL_VERSION_UNSUPPORTED);
91 POSIX_GUARD(s2n_fingerprint_wipe(fingerprint));
92 fingerprint->client_hello = ch;
93 return S2N_SUCCESS;
94 }
95
96 int s2n_fingerprint_get_hash_size(const struct s2n_fingerprint *fingerprint, uint32_t *size)
97 {
98 POSIX_ENSURE(fingerprint, S2N_ERR_INVALID_ARGUMENT);
99 const struct s2n_fingerprint_method *method = fingerprint->method;
100 POSIX_ENSURE_REF(method);
101 POSIX_ENSURE(size, S2N_ERR_INVALID_ARGUMENT);
102 *size = method->hash_str_size;
103 return S2N_SUCCESS;
104 }
105
106 int s2n_fingerprint_get_hash(struct s2n_fingerprint *fingerprint,
107 uint32_t max_output_size, uint8_t *output, uint32_t *output_size)
108 {
109 POSIX_ENSURE(fingerprint, S2N_ERR_INVALID_ARGUMENT);
110 const struct s2n_fingerprint_method *method = fingerprint->method;
111 POSIX_ENSURE_REF(method);
112
113 POSIX_ENSURE(max_output_size >= method->hash_str_size, S2N_ERR_INSUFFICIENT_MEM_SIZE);
114 POSIX_ENSURE(output, S2N_ERR_INVALID_ARGUMENT);
115 POSIX_ENSURE(output_size, S2N_ERR_INVALID_ARGUMENT);
116 *output_size = 0;
117
118 struct s2n_fingerprint_hash hash = {
119 .hash = &fingerprint->hash,
120 };
121 POSIX_GUARD(s2n_hash_reset(&fingerprint->hash));
122
123 struct s2n_stuffer output_stuffer = { 0 };
124 POSIX_GUARD(s2n_blob_init(&output_stuffer.blob, output, max_output_size));
125
126 POSIX_ENSURE(fingerprint->client_hello, S2N_ERR_INVALID_STATE);
127 POSIX_GUARD_RESULT(method->fingerprint(fingerprint, &hash, &output_stuffer));
128
129 *output_size = s2n_stuffer_data_available(&output_stuffer);
130 return S2N_SUCCESS;
131 }
132
133 int s2n_fingerprint_get_raw_size(const struct s2n_fingerprint *fingerprint, uint32_t *size)
134 {
135 POSIX_ENSURE(fingerprint, S2N_ERR_INVALID_ARGUMENT);
136 POSIX_ENSURE(size, S2N_ERR_INVALID_ARGUMENT);
137 /* A zero-length raw string is impossible for all fingerprinting methods
138 * currently supported, so raw_size == 0 indicates that raw_size has not been
139 * calculated yet.
140 */
141 POSIX_ENSURE(fingerprint->raw_size != 0, S2N_ERR_INVALID_STATE);
142 *size = fingerprint->raw_size;
143 return S2N_SUCCESS;
144 }
145
146 int s2n_fingerprint_get_raw(struct s2n_fingerprint *fingerprint,
147 uint32_t max_output_size, uint8_t *output, uint32_t *output_size)
148 {
149 POSIX_ENSURE(fingerprint, S2N_ERR_INVALID_ARGUMENT);
150 const struct s2n_fingerprint_method *method = fingerprint->method;
151 POSIX_ENSURE_REF(method);
152
153 POSIX_ENSURE(max_output_size > 0, S2N_ERR_INSUFFICIENT_MEM_SIZE);
154 POSIX_ENSURE(output, S2N_ERR_INVALID_ARGUMENT);
155 POSIX_ENSURE(output_size, S2N_ERR_INVALID_ARGUMENT);
156 *output_size = 0;
157
158 struct s2n_stuffer output_stuffer = { 0 };
159 POSIX_GUARD(s2n_blob_init(&output_stuffer.blob, output, max_output_size));
160 struct s2n_fingerprint_hash hash = {
161 .buffer = &output_stuffer,
162 };
163
164 POSIX_ENSURE(fingerprint->client_hello, S2N_ERR_INVALID_STATE);
165 POSIX_GUARD_RESULT(method->fingerprint(fingerprint, &hash, &output_stuffer));
166 *output_size = s2n_stuffer_data_available(&output_stuffer);
167 return S2N_SUCCESS;
168 }
169
170 /* See https://datatracker.ietf.org/doc/html/rfc8701
171 * for an explanation of GREASE and lists of the GREASE values.
172 */
173 static S2N_RESULT s2n_assert_grease_value(uint16_t val)
174 {
175 uint8_t byte1 = val >> 8;
176 uint8_t byte2 = val & 0x00FF;
177 /* Both bytes of the GREASE values are identical */
178 RESULT_ENSURE_EQ(byte1, byte2);
179 /* The GREASE value bytes all follow the format 0x[0-F]A.
180 * So 0x0A, 0x1A, 0x2A etc, up to 0xFA. */
181 RESULT_ENSURE_EQ((byte1 | 0xF0), 0xFA);
182 return S2N_RESULT_OK;
183 }
184
185 /**
186 *= https://raw.githubusercontent.com/FoxIO-LLC/ja4/df3c067/technical_details/JA4.md#details
187 *# The program needs to ignore GREASE values anywhere it sees them
188 */
189 bool s2n_fingerprint_is_grease_value(uint16_t val)
190 {
191 return s2n_result_is_ok(s2n_assert_grease_value(val));
192 }
193
194 S2N_RESULT s2n_fingerprint_parse_extension(struct s2n_stuffer *input, uint16_t *iana)
195 {
196 uint16_t size = 0;
197 RESULT_GUARD_POSIX(s2n_stuffer_read_uint16(input, iana));
198 RESULT_GUARD_POSIX(s2n_stuffer_read_uint16(input, &size));
199 RESULT_GUARD_POSIX(s2n_stuffer_skip_read(input, size));
200 return S2N_RESULT_OK;
201 }
202
203 S2N_RESULT s2n_fingerprint_get_legacy_version(struct s2n_client_hello *ch, uint16_t *version)
204 {
205 RESULT_ENSURE_REF(ch);
206 RESULT_ENSURE_REF(version);
207 uint8_t high_byte = (ch->legacy_version / 10);
208 uint8_t low_byte = (ch->legacy_version % 10);
209 *version = high_byte << 8 | low_byte;
210 return S2N_RESULT_OK;
211 }
212
213 S2N_RESULT s2n_fingerprint_hash_add_char(struct s2n_fingerprint_hash *hash, char c)
214 {
215 RESULT_ENSURE_REF(hash);
216 if (hash->hash) {
217 RESULT_GUARD_POSIX(s2n_hash_update(hash->hash, &c, 1));
218 } else {
219 RESULT_ENSURE_REF(hash->buffer);
220 RESULT_ENSURE(s2n_stuffer_space_remaining(hash->buffer) >= 1,
221 S2N_ERR_INSUFFICIENT_MEM_SIZE);
222 RESULT_GUARD_POSIX(s2n_stuffer_write_char(hash->buffer, c));
223 }
224 return S2N_RESULT_OK;
225 }
226
227 S2N_RESULT s2n_fingerprint_hash_add_str(struct s2n_fingerprint_hash *hash,
228 const char *str, size_t str_size)
229 {
230 return s2n_fingerprint_hash_add_bytes(hash, (const uint8_t *) str, str_size);
231 }
232
233 S2N_RESULT s2n_fingerprint_hash_add_bytes(struct s2n_fingerprint_hash *hash,
234 const uint8_t *bytes, size_t size)
235 {
236 RESULT_ENSURE_REF(hash);
237 RESULT_ENSURE(S2N_MEM_IS_READABLE(bytes, size), S2N_ERR_NULL);
238 if (hash->hash) {
239 RESULT_GUARD_POSIX(s2n_hash_update(hash->hash, bytes, size));
240 } else {
241 RESULT_ENSURE_REF(hash->buffer);
242 RESULT_ENSURE(s2n_stuffer_space_remaining(hash->buffer) >= size,
243 S2N_ERR_INSUFFICIENT_MEM_SIZE);
244 RESULT_GUARD_POSIX(s2n_stuffer_write_text(hash->buffer, bytes, size));
245 }
246 return S2N_RESULT_OK;
247 }
248
249 S2N_RESULT s2n_fingerprint_hash_digest(struct s2n_fingerprint_hash *hash, struct s2n_blob *out)
250 {
251 RESULT_ENSURE_REF(hash);
252 RESULT_ENSURE_REF(hash->hash);
253 RESULT_ENSURE_REF(out);
254
255 uint64_t bytes = 0;
256 RESULT_GUARD_POSIX(s2n_hash_get_currently_in_hash_total(hash->hash, &bytes));
257 hash->bytes_digested += bytes;
258
259 RESULT_GUARD_POSIX(s2n_hash_digest(hash->hash, out->data, out->size));
260 RESULT_GUARD_POSIX(s2n_hash_reset(hash->hash));
261 return S2N_RESULT_OK;
262 }
263
264 bool s2n_fingerprint_hash_do_digest(struct s2n_fingerprint_hash *hash)
265 {
266 return hash && hash->hash;
267 }
268
269 int s2n_client_hello_get_fingerprint_hash(struct s2n_client_hello *ch, s2n_fingerprint_type type,
270 uint32_t max_output_size, uint8_t *output, uint32_t *output_size, uint32_t *str_size)
271 {
272 POSIX_ENSURE(type == S2N_FINGERPRINT_JA3, S2N_ERR_INVALID_ARGUMENT);
273 POSIX_ENSURE(max_output_size >= MD5_DIGEST_LENGTH, S2N_ERR_INSUFFICIENT_MEM_SIZE);
274 POSIX_ENSURE(str_size, S2N_ERR_INVALID_ARGUMENT);
275 POSIX_ENSURE(output_size, S2N_ERR_INVALID_ARGUMENT);
276 POSIX_ENSURE(output, S2N_ERR_INVALID_ARGUMENT);
277
278 DEFER_CLEANUP(struct s2n_fingerprint fingerprint = { 0 }, s2n_fingerprint_free_fields);
279 POSIX_GUARD_RESULT(s2n_fingerprint_init(&fingerprint, type));
280 POSIX_GUARD(s2n_fingerprint_set_client_hello(&fingerprint, ch));
281
282 uint32_t hex_hash_size = 0;
283 uint8_t hex_hash[S2N_JA3_HASH_STR_SIZE] = { 0 };
284 POSIX_GUARD(s2n_fingerprint_get_hash(&fingerprint, sizeof(hex_hash), hex_hash, &hex_hash_size));
285
286 /* s2n_client_hello_get_fingerprint_hash expects the raw bytes of the JA3 hash,
287 * but s2n_fingerprint_get_hash returns a hex string instead.
288 * We need to translate back to the raw bytes.
289 */
290 struct s2n_blob bytes_out = { 0 };
291 POSIX_GUARD(s2n_blob_init(&bytes_out, output, MD5_DIGEST_LENGTH));
292 struct s2n_stuffer hex_in = { 0 };
293 POSIX_GUARD(s2n_blob_init(&hex_in.blob, hex_hash, hex_hash_size));
294 POSIX_GUARD(s2n_stuffer_skip_write(&hex_in, hex_hash_size));
295 POSIX_GUARD_RESULT(s2n_stuffer_read_hex(&hex_in, &bytes_out));
296 *output_size = bytes_out.size;
297
298 POSIX_GUARD(s2n_fingerprint_get_raw_size(&fingerprint, str_size));
299 return S2N_SUCCESS;
300 }
301
302 int s2n_client_hello_get_fingerprint_string(struct s2n_client_hello *ch, s2n_fingerprint_type type,
303 uint32_t max_output_size, uint8_t *output, uint32_t *output_size)
304 {
305 POSIX_ENSURE(type == S2N_FINGERPRINT_JA3, S2N_ERR_INVALID_ARGUMENT);
306 DEFER_CLEANUP(struct s2n_fingerprint fingerprint = { 0 },
307 s2n_fingerprint_free_fields);
308 POSIX_GUARD_RESULT(s2n_fingerprint_init(&fingerprint, type));
309 POSIX_GUARD(s2n_fingerprint_set_client_hello(&fingerprint, ch));
310 POSIX_GUARD(s2n_fingerprint_get_raw(&fingerprint, max_output_size, output, output_size));
311 return S2N_SUCCESS;
312 }