Debian Codegraph @

crt/s2n/api/unstable/fingerprint.h

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 #pragma once
17
18 #include <s2n.h>
19
20 #ifdef __cplusplus
21 extern "C" {
22 #endif
23
24 /**
25 * @file fingerprint.h
26 *
27 * The following APIs enable applications to calculate fingerprints to
28 * identify ClientHellos.
29 *
30 * The fingerprinting APIs are currently considered unstable. They will be finalized
31 * and marked as stable after an initial customer integration and feedback.
32 */
33
34 /* Available fingerprinting methods.
35 *
36 * The current recommendation is to use JA4. JA4 sorts some of the lists it includes
37 * in the fingerprint, making it more resistant to the list reordering done by
38 * Chrome and other clients.
39 */
40 typedef enum {
41 /* See https://engineering.salesforce.com/tls-fingerprinting-with-ja3-and-ja3s-247362855967 */
42 S2N_FINGERPRINT_JA3,
43 /* See https://github.com/FoxIO-LLC/ja4/tree/main */
44 S2N_FINGERPRINT_JA4,
45 } s2n_fingerprint_type;
46
47 struct s2n_fingerprint;
48
49 /**
50 * Create a reusable fingerprint structure.
51 *
52 * Fingerprinting is primarily used to identify malicious or abusive clients,
53 * so fingerprinting needs to be efficient and require minimal resources.
54 * The `s2n_client_hello_get_fingerprint_hash` and `s2n_client_hello_get_fingerprint_string`
55 * methods may require additional memory to calculate the fingerprint. Reusing
56 * the same `s2n_fingerprint` structure to calculate multiple fingerprints reduces
57 * the cost of each individual fingerprint.
58 *
59 * @param type The algorithm to use for the fingerprint.
60 * @returns S2N_SUCCESS on success, S2N_FAILURE on failure.
61 */
62 S2N_API struct s2n_fingerprint *s2n_fingerprint_new(s2n_fingerprint_type type);
63
64 /**
65 * Frees the memory allocated by `s2n_fingerprint_new` for a fingerprint structure.
66 *
67 * @param fingerprint The s2n_fingerprint structure to be freed.
68 * @returns S2N_SUCCESS on success, S2N_FAILURE on failure.
69 */
70 S2N_API int s2n_fingerprint_free(struct s2n_fingerprint **fingerprint);
71
72 /**
73 * Resets the fingerprint for safe reuse with a different ClientHello.
74 *
75 * @param fingerprint The s2n_fingerprint structure to be reset.
76 * @returns S2N_SUCCESS on success, S2N_FAILURE on failure.
77 */
78 S2N_API int s2n_fingerprint_wipe(struct s2n_fingerprint *fingerprint);
79
80 /**
81 * Sets the ClientHello to be fingerprinted.
82 *
83 * @param fingerprint The s2n_fingerprint to be modified
84 * @param ch The client hello to be fingerprinted. It will not be copied, so needs
85 * to live at least as long as this fingerprinting operation.
86 * @returns S2N_SUCCESS on success, S2N_FAILURE on failure.
87 */
88 S2N_API int s2n_fingerprint_set_client_hello(struct s2n_fingerprint *fingerprint, struct s2n_client_hello *ch);
89
90 /**
91 * Get the size of the fingerprint hash.
92 *
93 * Fingerprint hashes should be a constant size, but that size will vary based
94 * on the fingerprinting method used.
95 *
96 * @param fingerprint The s2n_fingerprint to be used for the hash
97 * @param size Output variable to be set to the size of the hash
98 * @returns S2N_SUCCESS on success, S2N_FAILURE on failure.
99 */
100 S2N_API int s2n_fingerprint_get_hash_size(const struct s2n_fingerprint *fingerprint, uint32_t *size);
101
102 /**
103 * Calculates a fingerprint hash.
104 *
105 * The output of this method depends on the type of fingerprint.
106 *
107 * JA3: A hex-encoded string representing the MD5 hash of the raw string.
108 * - See https://engineering.salesforce.com/tls-fingerprinting-with-ja3-and-ja3s-247362855967
109 * - Example: "c34a54599a1fbaf1786aa6d633545a60"
110 *
111 * JA4: A string consisting of three parts, separated by underscores: the prefix,
112 * and the hex-encoded truncated SHA256 hashes of the other two parts of the raw string.
113 * - See https://github.com/FoxIO-LLC/ja4/blob/df3c067/technical_details/JA4.md
114 * - Example: "t13i310900_e8f1e7e78f70_1f22a2ca17c4"
115 *
116 * @param fingerprint The s2n_fingerprint to be used for the hash
117 * @param max_output_size The maximum size of data that may be written to `output`.
118 * If `output` is too small, an S2N_ERR_T_USAGE error will occur.
119 * @param output The location that the requested hash will be written to.
120 * @param output_size Output variable to be set to the actual size of the data
121 * written to `output`.
122 * @returns S2N_SUCCESS on success, S2N_FAILURE on failure.
123 */
124 S2N_API int s2n_fingerprint_get_hash(struct s2n_fingerprint *fingerprint,
125 uint32_t max_output_size, uint8_t *output, uint32_t *output_size);
126
127 /**
128 * Get the size of the raw fingerprint string.
129 *
130 * The size of the raw string depends on the ClientHello and cannot be known
131 * without calculating the fingerprint. Either `s2n_fingerprint_get_hash` or
132 * `s2n_fingerprint_get_raw` must be called before this method.
133 *
134 * @param fingerprint The s2n_fingerprint to be used for the raw string
135 * @param size Output variable to be set to the size of the raw string
136 * @returns S2N_SUCCESS on success, S2N_FAILURE on failure.
137 */
138 S2N_API int s2n_fingerprint_get_raw_size(const struct s2n_fingerprint *fingerprint, uint32_t *size);
139
140 /**
141 * Calculates the raw string for a fingerprint.
142 *
143 * The output of this method depends on the type of fingerprint.
144 *
145 * JA3: A string consisting of lists of decimal values.
146 * - See https://engineering.salesforce.com/tls-fingerprinting-with-ja3-and-ja3s-247362855967
147 * - Example: "771,4866-4867-4865-49196-49200-159-52393-52392-52394-49195-49199-158-
148 * 49188-49192-107-49187-49191-103-49162-49172-57-49161-49171-51-157-
149 * 156-61-60-53-47-255,11-10-35-22-23-13-43-45-51,29-23-30-25-24,0-1-2"
150 *
151 * JA4: A string consisting of three parts: a prefix, and two lists of hex values.
152 * - See https://github.com/FoxIO-LLC/ja4/blob/df3c067/technical_details/JA4.md
153 * - Example: "t13i310900_002f,0033,0035,0039,003c,003d,0067,006b,009c,009d,009e,
154 * 009f,00ff,1301,1302,1303,c009,c00a,c013,c014,c023,c024,c027,c028,
155 * c02b,c02c,c02f,c030,cca8,cca9,ccaa_000a,000b,000d,0016,0017,0023,
156 * 002b,002d,0033_0403,0503,0603,0807,0808,0809,080a,080b,0804,0805,
157 * 0806,0401,0501,0601,0303,0301,0302,0402,0502,0602"
158 *
159 * @param fingerprint The s2n_fingerprint to be used for the raw string
160 * @param max_output_size The maximum size of data that may be written to `output`.
161 * If `output` is too small, an S2N_ERR_T_USAGE error will occur.
162 * @param output The location that the requested raw string will be written to.
163 * @param output_size Output variable to be set to the actual size of the data
164 * written to `output`.
165 * @returns S2N_SUCCESS on success, S2N_FAILURE on failure.
166 */
167 S2N_API int s2n_fingerprint_get_raw(struct s2n_fingerprint *fingerprint,
168 uint32_t max_output_size, uint8_t *output, uint32_t *output_size);
169
170 /**
171 * Calculates a fingerprint hash for a given ClientHello.
172 *
173 * Currently the only type supported is S2N_FINGERPRINT_JA3, which uses MD5 and
174 * requires at least 16 bytes of memory.
175 *
176 * @param ch The ClientHello to fingerprint.
177 * @param type The algorithm to use for the fingerprint. Currently only JA3 is supported.
178 * @param max_hash_size The maximum size of data that may be written to `hash`.
179 * If too small for the requested hash, an S2N_ERR_T_USAGE error will occur.
180 * @param hash The location that the requested hash will be written to.
181 * @param hash_size The actual size of the data written to `hash`.
182 * @param str_size The actual size of the full string associated with this hash.
183 * This size can be used to ensure that sufficient memory is provided for the
184 * output of `s2n_client_hello_get_fingerprint_string`.
185 * @returns S2N_SUCCESS on success, S2N_FAILURE on failure.
186 */
187 S2N_API int s2n_client_hello_get_fingerprint_hash(struct s2n_client_hello *ch,
188 s2n_fingerprint_type type, uint32_t max_hash_size,
189 uint8_t *hash, uint32_t *hash_size, uint32_t *str_size);
190
191 /**
192 * Calculates a full, variable-length fingerprint string for a given ClientHello.
193 *
194 * Because the length of the string is variable and unknown until the string is
195 * calculated, `s2n_client_hello_get_fingerprint_hash` can be called first to
196 * determine `max_size` and ensure `output` is sufficiently large.
197 *
198 * @param ch The ClientHello to fingerprint.
199 * @param type The algorithm to use for the fingerprint. Currently only JA3 is supported.
200 * @param max_size The maximum size of data that may be written to `output`.
201 * If too small for the requested string, an S2N_ERR_T_USAGE error will occur.
202 * @param output The location that the requested string will be written to.
203 * @param output_size The actual size of the data written to `output`.
204 * @returns S2N_SUCCESS on success, S2N_FAILURE on failure.
205 */
206 S2N_API int s2n_client_hello_get_fingerprint_string(struct s2n_client_hello *ch,
207 s2n_fingerprint_type type, uint32_t max_size,
208 uint8_t *output, uint32_t *output_size);
209
210 #ifdef __cplusplus
211 }
212 #endif