Skip to content

Commit 9fe312a

Browse files
authored
Merge pull request #227 from Zadamsa/tls-refactor
TLS refactor
2 parents dfbd2f2 + acbdca0 commit 9fe312a

File tree

10 files changed

+1202
-591
lines changed

10 files changed

+1202
-591
lines changed

Makefile.am

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -99,6 +99,7 @@ ipfixprobe_process_src=\
9999
process/tls.hpp \
100100
process/tls_parser.cpp \
101101
process/tls_parser.hpp \
102+
process/sha256.hpp \
102103
process/smtp.cpp \
103104
process/smtp.hpp \
104105
process/dns-utils.hpp \

include/ipfixprobe/ipfix-elements.hpp

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -185,10 +185,10 @@ namespace ipxp {
185185
#define TLS_VERSION(F) F(39499, 333, 2, nullptr)
186186
#define TLS_ALPN(F) F(39499, 337, -1, nullptr)
187187
#define TLS_JA3(F) F(39499, 357, -1, nullptr)
188+
#define TLS_JA4(F) F(39499, 358, -1, nullptr)
188189
#define TLS_EXT_TYPE(F) F(0, 291, -1, nullptr)
189190
#define TLS_EXT_LEN(F) F(0, 291, -1, nullptr)
190191

191-
192192
#define SMTP_COMMANDS(F) F(8057, 810, 4, nullptr)
193193
#define SMTP_MAIL_COUNT(F) F(8057, 811, 4, nullptr)
194194
#define SMTP_RCPT_COUNT(F) F(8057, 812, 4, nullptr)
@@ -389,6 +389,7 @@ namespace ipxp {
389389
F(TLS_SNI) \
390390
F(TLS_ALPN) \
391391
F(TLS_JA3) \
392+
F(TLS_JA4) \
392393
F(TLS_EXT_TYPE) \
393394
F(TLS_EXT_LEN)
394395

process/quic_parser.cpp

Lines changed: 54 additions & 82 deletions
Original file line numberDiff line numberDiff line change
@@ -248,95 +248,65 @@ uint64_t QUICParser::quic_get_variable_length(const uint8_t* start, uint64_t& of
248248
}
249249
} // QUICParser::quic_get_variable_length
250250

251-
bool QUICParser::quic_obtain_tls_data(TLSData& payload)
251+
bool QUICParser::quic_parse_tls_extensions()
252252
{
253-
quic_tls_extension_lengths_pos = 0;
254-
quic_tls_ext_type_pos = 0;
255-
quic_tls_ext_pos = 0;
256-
while (payload.start + sizeof(tls_ext) <= payload.end) {
257-
tls_ext* ext = (tls_ext*) payload.start;
258-
uint16_t type = ntohs(ext->type);
259-
uint16_t length = ntohs(ext->length);
260-
261-
// Store extension type
262-
if (quic_tls_ext_type_pos < MAX_QUIC_TLS_EXT_LEN) {
263-
quic_tls_ext_type[quic_tls_ext_type_pos] = type;
264-
quic_tls_ext_type_pos += 1;
265-
}
266-
267-
// Store extension type length
268-
if (quic_tls_extension_lengths_pos < MAX_QUIC_TLS_EXT_LEN) {
269-
quic_tls_extension_lengths[quic_tls_extension_lengths_pos] = length;
270-
quic_tls_extension_lengths_pos += 1;
271-
}
272-
273-
//
274-
payload.start += sizeof(tls_ext);
275-
276-
if (payload.start + length > payload.end) {
277-
break;
278-
}
279-
280-
// Save value payload except for length
281-
if (quic_tls_ext_pos + length < CURRENT_BUFFER_SIZE) {
253+
const bool extensions_parsed = tls_parser.parse_extensions([this](
254+
uint16_t extension_type,
255+
const uint8_t* extension_payload,
256+
uint16_t extension_length) {
257+
if (extension_type == TLS_EXT_SERVER_NAME && extension_length != 0) {
258+
tls_parser.parse_server_names(extension_payload, extension_length);
259+
} else if (
260+
(extension_type == TLS_EXT_QUIC_TRANSPORT_PARAMETERS_V1
261+
|| extension_type == TLS_EXT_QUIC_TRANSPORT_PARAMETERS
262+
|| extension_type == TLS_EXT_QUIC_TRANSPORT_PARAMETERS_V2)
263+
&& extension_length != 0) {
264+
tls_parser.parse_quic_user_agent(extension_payload, extension_length);
265+
}
266+
if (quic_tls_ext_pos + extension_length < CURRENT_BUFFER_SIZE) {
282267
#ifndef QUIC_CH_FULL_TLS_EXT
283-
if (type == TLS_EXT_ALPN || type == TLS_EXT_QUIC_TRANSPORT_PARAMETERS_V1
284-
|| type == TLS_EXT_QUIC_TRANSPORT_PARAMETERS
285-
|| type == TLS_EXT_QUIC_TRANSPORT_PARAMETERS_V2) {
268+
if (extension_type == TLS_EXT_ALPN || extension_type == TLS_EXT_QUIC_TRANSPORT_PARAMETERS_V1
269+
|| extension_type == TLS_EXT_QUIC_TRANSPORT_PARAMETERS
270+
|| extension_type == TLS_EXT_QUIC_TRANSPORT_PARAMETERS_V2) {
286271
#endif
287-
memcpy(quic_tls_ext + quic_tls_ext_pos, payload.start, length);
288-
quic_tls_ext_pos += length;
272+
memcpy(quic_tls_ext + quic_tls_ext_pos, extension_payload, extension_length);
273+
quic_tls_ext_pos += extension_length;
289274
#ifndef QUIC_CH_FULL_TLS_EXT
290-
}
275+
}
291276
#endif
292-
}
293-
294-
// Legacy extract specific fields
295-
if (type == TLS_EXT_SERVER_NAME && length != 0) {
296-
tls_parser.tls_get_server_name(payload, sni, BUFF_SIZE);
297-
} else if (
298-
(type == TLS_EXT_QUIC_TRANSPORT_PARAMETERS_V1
299-
|| type == TLS_EXT_QUIC_TRANSPORT_PARAMETERS
300-
|| type == TLS_EXT_QUIC_TRANSPORT_PARAMETERS_V2)
301-
&& length != 0) {
302-
tls_parser.tls_get_quic_user_agent(payload, user_agent, BUFF_SIZE);
303-
}
304-
payload.start += length;
305-
}
306-
return payload.obejcts_parsed != 0;
277+
}
278+
tls_parser.add_extension(extension_type, extension_length);
279+
});
280+
if (!extensions_parsed) {
281+
return false;
282+
}
283+
tls_parser.save_server_names(sni, BUFF_SIZE);
284+
tls_parser.save_quic_user_agent(user_agent, BUFF_SIZE);
285+
286+
const size_t copy_count = std::min<size_t>(tls_parser.get_extensions().size(), MAX_QUIC_TLS_EXT_LEN);
287+
std::transform(tls_parser.get_extensions().begin(),
288+
tls_parser.get_extensions().begin() + static_cast<ssize_t>(copy_count),
289+
std::begin(quic_tls_ext_type),
290+
[](const TLSExtension& typeLength) {
291+
return typeLength.type;
292+
});
293+
std::transform(tls_parser.get_extensions().begin(),
294+
tls_parser.get_extensions().begin() + static_cast<ssize_t>(copy_count),
295+
std::begin(quic_tls_extension_lengths),
296+
[](const TLSExtension& typeLength) {
297+
return typeLength.length;
298+
});
299+
quic_tls_ext_type_pos = quic_tls_extension_lengths_pos = copy_count;
300+
return true;
307301
}
308302

309303
bool QUICParser::quic_parse_tls()
310304
{
311-
TLSData payload = {
312-
payload.start = final_payload + quic_crypto_start,
313-
payload.end = final_payload + quic_crypto_start + quic_crypto_len,
314-
payload.obejcts_parsed = 0,
315-
};
316-
317-
if (!tls_parser.tls_check_handshake(payload)) {
318-
return false;
319-
}
320-
if (!tls_parser.tls_skip_random(payload)) {
321-
return false;
322-
}
323-
if (!tls_parser.tls_skip_sessid(payload)) {
324-
return false;
325-
}
326-
if (!tls_parser.tls_skip_cipher_suites(payload)) {
305+
if (!tls_parser.parse_quic_tls(final_payload + quic_crypto_start, quic_crypto_len)) {
327306
return false;
328307
}
329-
if (!tls_parser.tls_skip_compression_met(payload)) {
330-
return false;
331-
}
332-
if (!tls_parser.tls_check_ext_len(payload)) {
333-
return false;
334-
}
335-
// If no parameters were extracted. We also accept the QUIC connection. (no error check here)
336-
quic_obtain_tls_data(payload);
337-
338-
return true;
339-
} // QUICPlugin::quic_parse_tls
308+
return quic_parse_tls_extensions();
309+
}
340310

341311
uint8_t QUICParser::quic_draft_version(uint32_t version)
342312
{
@@ -1394,14 +1364,16 @@ bool QUICParser::quic_parse_headers(const Packet& pkt, bool forceInitialParsing)
13941364

13951365
bool QUICParser::quic_set_server_port(const Packet& pkt)
13961366
{
1397-
tls_handshake hs = tls_parser.tls_get_handshake();
1367+
if (!tls_parser.get_handshake().has_value()) {
1368+
return false;
1369+
}
13981370

13991371
switch (packet_type) {
14001372
case INITIAL:
1401-
tls_hs_type = hs.type;
1402-
if (hs.type == 1) {
1373+
tls_hs_type = tls_parser.get_handshake()->type;
1374+
if (tls_hs_type == 1) {
14031375
server_port = pkt.dst_port;
1404-
} else if (hs.type == 2) {
1376+
} else if (tls_hs_type == 2) {
14051377
// Won't be reached, since we don't supply the OCCID to quic_parser
14061378
server_port = pkt.src_port;
14071379
}

process/quic_parser.hpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -119,7 +119,7 @@ class QUICParser {
119119
uint64_t quic_get_variable_length(const uint8_t*, uint64_t&);
120120
bool quic_check_version(uint32_t, uint8_t);
121121
bool quic_check_pointer_pos(const uint8_t*, const uint8_t*);
122-
bool quic_obtain_tls_data(TLSData&);
122+
bool quic_parse_tls_extensions();
123123
bool quic_set_server_port(const Packet& pkt);
124124
bool quic_check_min_initial_size(const Packet& pkt);
125125
bool quic_check_supported_version(const uint32_t version);

process/sha256.hpp

Lines changed: 148 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,148 @@
1+
// Single file sha256
2+
// https://github.com/LekKit/sha256
3+
4+
#include <stdio.h>
5+
#include <string.h>
6+
7+
namespace sha256 {
8+
9+
struct sha256_buff {
10+
unsigned long data_size;
11+
unsigned int h[8];
12+
unsigned char last_chunk[64];
13+
unsigned char chunk_size;
14+
};
15+
16+
void sha256_init(struct sha256_buff* buff)
17+
{
18+
buff->h[0] = 0x6a09e667;
19+
buff->h[1] = 0xbb67ae85;
20+
buff->h[2] = 0x3c6ef372;
21+
buff->h[3] = 0xa54ff53a;
22+
buff->h[4] = 0x510e527f;
23+
buff->h[5] = 0x9b05688c;
24+
buff->h[6] = 0x1f83d9ab;
25+
buff->h[7] = 0x5be0cd19;
26+
buff->data_size = 0;
27+
buff->chunk_size = 0;
28+
}
29+
30+
static const unsigned int k[64] = {
31+
0x428a2f98, 0x71374491, 0xb5c0fbcf, 0xe9b5dba5, 0x3956c25b, 0x59f111f1, 0x923f82a4, 0xab1c5ed5,
32+
0xd807aa98, 0x12835b01, 0x243185be, 0x550c7dc3, 0x72be5d74, 0x80deb1fe, 0x9bdc06a7, 0xc19bf174,
33+
0xe49b69c1, 0xefbe4786, 0x0fc19dc6, 0x240ca1cc, 0x2de92c6f, 0x4a7484aa, 0x5cb0a9dc, 0x76f988da,
34+
0x983e5152, 0xa831c66d, 0xb00327c8, 0xbf597fc7, 0xc6e00bf3, 0xd5a79147, 0x06ca6351, 0x14292967,
35+
0x27b70a85, 0x2e1b2138, 0x4d2c6dfc, 0x53380d13, 0x650a7354, 0x766a0abb, 0x81c2c92e, 0x92722c85,
36+
0xa2bfe8a1, 0xa81a664b, 0xc24b8b70, 0xc76c51a3, 0xd192e819, 0xd6990624, 0xf40e3585, 0x106aa070,
37+
0x19a4c116, 0x1e376c08, 0x2748774c, 0x34b0bcb5, 0x391c0cb3, 0x4ed8aa4a, 0x5b9cca4f, 0x682e6ff3,
38+
0x748f82ee, 0x78a5636f, 0x84c87814, 0x8cc70208, 0x90befffa, 0xa4506ceb, 0xbef9a3f7, 0xc67178f2};
39+
40+
#define rotate_r(val, bits) (val >> bits | val << (32 - bits))
41+
42+
static void sha256_calc_chunk(struct sha256_buff* buff, const unsigned char* chunk)
43+
{
44+
unsigned int w[64];
45+
unsigned int tv[8];
46+
unsigned int i;
47+
48+
for (i = 0; i < 16; ++i) {
49+
w[i] = (unsigned int) chunk[0] << 24 | (unsigned int) chunk[1] << 16
50+
| (unsigned int) chunk[2] << 8 | (unsigned int) chunk[3];
51+
chunk += 4;
52+
}
53+
54+
for (i = 16; i < 64; ++i) {
55+
unsigned int s0 = rotate_r(w[i - 15], 7) ^ rotate_r(w[i - 15], 18) ^ (w[i - 15] >> 3);
56+
unsigned int s1 = rotate_r(w[i - 2], 17) ^ rotate_r(w[i - 2], 19) ^ (w[i - 2] >> 10);
57+
w[i] = w[i - 16] + s0 + w[i - 7] + s1;
58+
}
59+
60+
for (i = 0; i < 8; ++i)
61+
tv[i] = buff->h[i];
62+
63+
for (i = 0; i < 64; ++i) {
64+
unsigned int S1 = rotate_r(tv[4], 6) ^ rotate_r(tv[4], 11) ^ rotate_r(tv[4], 25);
65+
unsigned int ch = (tv[4] & tv[5]) ^ (~tv[4] & tv[6]);
66+
unsigned int temp1 = tv[7] + S1 + ch + k[i] + w[i];
67+
unsigned int S0 = rotate_r(tv[0], 2) ^ rotate_r(tv[0], 13) ^ rotate_r(tv[0], 22);
68+
unsigned int maj = (tv[0] & tv[1]) ^ (tv[0] & tv[2]) ^ (tv[1] & tv[2]);
69+
unsigned int temp2 = S0 + maj;
70+
71+
tv[7] = tv[6];
72+
tv[6] = tv[5];
73+
tv[5] = tv[4];
74+
tv[4] = tv[3] + temp1;
75+
tv[3] = tv[2];
76+
tv[2] = tv[1];
77+
tv[1] = tv[0];
78+
tv[0] = temp1 + temp2;
79+
}
80+
81+
for (i = 0; i < 8; ++i)
82+
buff->h[i] += tv[i];
83+
}
84+
85+
void sha256_update(struct sha256_buff* buff, const void* data, unsigned long size)
86+
{
87+
const unsigned char* ptr = (const unsigned char*) data;
88+
buff->data_size += size;
89+
/* If there is data left in buff, concatenate it to process as new chunk */
90+
if (size + buff->chunk_size >= 64) {
91+
unsigned char tmp_chunk[64];
92+
memcpy(tmp_chunk, buff->last_chunk, buff->chunk_size);
93+
memcpy(tmp_chunk + buff->chunk_size, ptr, 64 - buff->chunk_size);
94+
ptr += (64 - buff->chunk_size);
95+
size -= (64 - buff->chunk_size);
96+
buff->chunk_size = 0;
97+
sha256_calc_chunk(buff, tmp_chunk);
98+
}
99+
/* Run over data chunks */
100+
while (size >= 64) {
101+
sha256_calc_chunk(buff, ptr);
102+
ptr += 64;
103+
size -= 64;
104+
}
105+
106+
/* Save remaining data in buff, will be reused on next call or finalize */
107+
memcpy(buff->last_chunk + buff->chunk_size, ptr, size);
108+
buff->chunk_size += size;
109+
}
110+
111+
void sha256_finalize(struct sha256_buff* buff)
112+
{
113+
buff->last_chunk[buff->chunk_size] = 0x80;
114+
buff->chunk_size++;
115+
memset(buff->last_chunk + buff->chunk_size, 0, 64 - buff->chunk_size);
116+
117+
/* If there isn't enough space to fit int64, pad chunk with zeroes and prepare next chunk */
118+
if (buff->chunk_size > 56) {
119+
sha256_calc_chunk(buff, buff->last_chunk);
120+
memset(buff->last_chunk, 0, 64);
121+
}
122+
123+
/* Add total size as big-endian int64 x8 */
124+
unsigned long size = buff->data_size * 8;
125+
int i;
126+
for (i = 8; i > 0; --i) {
127+
buff->last_chunk[55 + i] = size & 255;
128+
size >>= 8;
129+
}
130+
131+
sha256_calc_chunk(buff, buff->last_chunk);
132+
}
133+
134+
void hash_it(const unsigned char* data, unsigned long data_size, unsigned char* hash)
135+
{
136+
struct sha256_buff buff;
137+
sha256_init(&buff);
138+
sha256_update(&buff, data, data_size);
139+
sha256_finalize(&buff);
140+
for (int i = 0; i < 8; i++) {
141+
hash[i * 4] = (buff.h[i] >> 24) & 255;
142+
hash[i * 4 + 1] = (buff.h[i] >> 16) & 255;
143+
hash[i * 4 + 2] = (buff.h[i] >> 8) & 255;
144+
hash[i * 4 + 3] = buff.h[i] & 255;
145+
}
146+
}
147+
148+
}

0 commit comments

Comments
 (0)