mirror of
https://github.com/Waujito/youtubeUnblock.git
synced 2025-12-06 11:36:45 +03:00
Remaster tls parser for quic crypto
This commit is contained in:
14
src/mangle.c
14
src/mangle.c
@@ -177,11 +177,13 @@ int process_tcp_packet(const struct section_config_t *section, const uint8_t *ra
|
||||
lgtrace_addp("TLS analyzed");
|
||||
|
||||
if (vrd.sni_len != 0) {
|
||||
lgtrace_addp("SNI detected: %.*s", vrd.sni_len, data + vrd.sni_offset);
|
||||
lgtrace_addp("SNI detected: %.*s", vrd.sni_len, vrd.sni_ptr);
|
||||
}
|
||||
|
||||
if (vrd.target_sni) {
|
||||
lgdebugmsg("Target SNI detected: %.*s", vrd.sni_len, data + vrd.sni_offset);
|
||||
lgdebugmsg("Target SNI detected: %.*s", vrd.sni_len, vrd.sni_ptr);
|
||||
size_t sni_offset = vrd.sni_ptr - data;
|
||||
size_t target_sni_offset = vrd.target_sni_ptr - data;
|
||||
|
||||
uint32_t payload_len = raw_payload_len;
|
||||
NETBUF_ALLOC(payload, MAX_PACKET_SIZE);
|
||||
@@ -238,8 +240,8 @@ int process_tcp_packet(const struct section_config_t *section, const uint8_t *ra
|
||||
|
||||
switch (section->fragmentation_strategy) {
|
||||
case FRAG_STRAT_TCP: {
|
||||
ipd_offset = vrd.sni_target_offset;
|
||||
mid_offset = ipd_offset + vrd.sni_target_len / 2;
|
||||
ipd_offset = target_sni_offset;
|
||||
mid_offset = ipd_offset + vrd.target_sni_len / 2;
|
||||
|
||||
uint32_t poses[2];
|
||||
int cnt = 0;
|
||||
@@ -269,8 +271,8 @@ int process_tcp_packet(const struct section_config_t *section, const uint8_t *ra
|
||||
break;
|
||||
case FRAG_STRAT_IP:
|
||||
if (ipxv == IP4VERSION) {
|
||||
ipd_offset = ((char *)data - (char *)tcph) + vrd.sni_target_offset;
|
||||
mid_offset = ipd_offset + vrd.sni_target_len / 2;
|
||||
ipd_offset = ((char *)data - (char *)tcph) + target_sni_offset;
|
||||
mid_offset = ipd_offset + vrd.target_sni_len / 2;
|
||||
mid_offset += 8 - mid_offset % 8;
|
||||
|
||||
uint32_t poses[2];
|
||||
|
||||
63
src/quic.c
63
src/quic.c
@@ -18,7 +18,10 @@ uint64_t quic_parse_varlength(const uint8_t *variable, uint64_t *mlen) {
|
||||
uint8_t len = 1 << (*variable >> 6);
|
||||
|
||||
if (mlen) {
|
||||
if (*mlen < len) return 0;
|
||||
if (*mlen < len) {
|
||||
*mlen = 0;
|
||||
return 0;
|
||||
}
|
||||
*mlen = len;
|
||||
}
|
||||
|
||||
@@ -164,6 +167,47 @@ invalid_packet:
|
||||
return -EINVAL;
|
||||
}
|
||||
|
||||
ssize_t quic_parse_crypto(struct quic_frame_crypto *crypto_frame,
|
||||
const uint8_t *frame, uint64_t flen) {
|
||||
const uint8_t *curptr = frame;
|
||||
uint64_t curptr_len = flen;
|
||||
uint64_t vln;
|
||||
*crypto_frame = (struct quic_frame_crypto){0};
|
||||
|
||||
if (flen == 0 || *frame != QUIC_FRAME_CRYPTO ||
|
||||
crypto_frame == NULL)
|
||||
return -EINVAL;
|
||||
|
||||
|
||||
curptr++, curptr_len--;
|
||||
|
||||
vln = curptr_len;
|
||||
uint64_t offset = quic_parse_varlength(curptr, &vln);
|
||||
curptr += vln, curptr_len -= vln;
|
||||
if (vln == 0) {
|
||||
return -EINVAL;
|
||||
}
|
||||
|
||||
vln = curptr_len;
|
||||
uint64_t length = quic_parse_varlength(curptr, &vln);
|
||||
curptr += vln, curptr_len -= vln;
|
||||
if (vln == 0) {
|
||||
return -EINVAL;
|
||||
}
|
||||
|
||||
if (length > curptr_len)
|
||||
return -EINVAL;
|
||||
|
||||
crypto_frame->offset = offset;
|
||||
crypto_frame->payload_length = length;
|
||||
crypto_frame->payload = curptr;
|
||||
|
||||
curptr += length;
|
||||
curptr_len -= length;
|
||||
|
||||
return flen - curptr_len;
|
||||
}
|
||||
|
||||
int udp_fail_packet(struct udp_failing_strategy strategy, uint8_t *payload, uint32_t *plen, uint32_t avail_buflen) {
|
||||
void *iph;
|
||||
uint32_t iph_len;
|
||||
@@ -313,20 +357,11 @@ int detect_udp_filtered(const struct section_config_t *section,
|
||||
|
||||
lgtrace_addp("QUIC detected");
|
||||
|
||||
uint8_t qtype = qch->type;
|
||||
if (qch->version == QUIC_V1)
|
||||
qtype = quic_convtype_v1(qtype);
|
||||
else if (qch->version == QUIC_V2)
|
||||
qtype = quic_convtype_v2(qtype);
|
||||
|
||||
if (qtype != QUIC_INITIAL_TYPE) {
|
||||
lgtrace_addp("QUIC message type: %d", qtype);
|
||||
goto match_port;
|
||||
|
||||
if (quic_check_is_initial(qch)) {
|
||||
lgtrace_addp("QUIC initial message");
|
||||
goto approve;
|
||||
}
|
||||
|
||||
lgtrace_addp("QUIC initial message");
|
||||
|
||||
goto approve;
|
||||
}
|
||||
|
||||
match_port:
|
||||
|
||||
17
src/quic.h
17
src/quic.h
@@ -41,6 +41,9 @@
|
||||
#define QUIC_RETRY_TYPE_V2 0b00
|
||||
#define quic_convtype_v2(type) (((type) + 1) & 0b11)
|
||||
|
||||
#define QUIC_FRAME_CRYPTO 0x06
|
||||
#define QUIC_FRAME_PADDING 0x00
|
||||
|
||||
#define QUIC_V1 1 // RFC 9000
|
||||
#define QUIC_V2 0x6b3343cf // RFC 9369
|
||||
|
||||
@@ -130,6 +133,8 @@ int quic_parse_data(const uint8_t *raw_payload, uint32_t raw_payload_len,
|
||||
*
|
||||
* \mlen Used to signal about variable length and validate left length
|
||||
* in the buffer.
|
||||
*
|
||||
* On error/buffer overflow mlen set to 0, otherwise it is higher
|
||||
*/
|
||||
uint64_t quic_parse_varlength(const uint8_t *variable, uint64_t *mlen);
|
||||
|
||||
@@ -161,6 +166,18 @@ int64_t quic_get_version(const struct quic_lhdr *qch);
|
||||
*/
|
||||
int quic_check_is_initial(const struct quic_lhdr *qch);
|
||||
|
||||
struct quic_frame_crypto {
|
||||
uint64_t offset;
|
||||
uint64_t payload_length;
|
||||
const uint8_t *payload;
|
||||
};
|
||||
/**
|
||||
* Parses quic crypto frame
|
||||
* Returns parsed size or -EINVAL on error
|
||||
*/
|
||||
ssize_t quic_parse_crypto(struct quic_frame_crypto *crypto_frame,
|
||||
const uint8_t *frame, uint64_t flen);
|
||||
|
||||
|
||||
/**
|
||||
* Parses QUIC initial message header.
|
||||
|
||||
251
src/tls.c
251
src/tls.c
@@ -17,7 +17,7 @@ static int bruteforce_analyze_sni_str(
|
||||
if (section->all_domains) {
|
||||
vrd->target_sni = 1;
|
||||
vrd->sni_len = 0;
|
||||
vrd->sni_offset = dlen / 2;
|
||||
vrd->sni_ptr = data + dlen / 2;
|
||||
return 0;
|
||||
}
|
||||
|
||||
@@ -53,9 +53,9 @@ static int bruteforce_analyze_sni_str(
|
||||
if (zbuf[k] == domain_len) {
|
||||
vrd->target_sni = 1;
|
||||
vrd->sni_len = domain_len;
|
||||
vrd->sni_offset = (k - domain_len - 1);
|
||||
vrd->sni_target_offset = vrd->sni_offset;
|
||||
vrd->sni_target_len = vrd->sni_len;
|
||||
vrd->sni_ptr = data + (k - domain_len - 1);
|
||||
vrd->target_sni_ptr = vrd->sni_ptr;
|
||||
vrd->target_sni_len = vrd->sni_len;
|
||||
NETBUF_FREE(buf);
|
||||
NETBUF_FREE(nzbuf);
|
||||
return 0;
|
||||
@@ -71,7 +71,7 @@ static int bruteforce_analyze_sni_str(
|
||||
}
|
||||
static int analyze_sni_str(
|
||||
const struct section_config_t *section,
|
||||
const char *sni_name, int sni_len, const uint8_t *data,
|
||||
const char *sni_name, int sni_len,
|
||||
struct tls_verdict *vrd
|
||||
) {
|
||||
if (section->all_domains) {
|
||||
@@ -89,8 +89,8 @@ static int analyze_sni_str(
|
||||
domain_startp,
|
||||
sne->domain_len)) {
|
||||
vrd->target_sni = 1;
|
||||
vrd->sni_target_offset = (const uint8_t *)sni_startp - data;
|
||||
vrd->sni_target_len = sne->domain_len;
|
||||
vrd->target_sni_ptr = (const uint8_t *)sni_startp;
|
||||
vrd->target_sni_len = sne->domain_len;
|
||||
break;
|
||||
}
|
||||
}
|
||||
@@ -108,7 +108,7 @@ check_domain:
|
||||
sne->domain_len)) {
|
||||
vrd->target_sni = 0;
|
||||
lgdebugmsg("Excluded SNI: %.*s",
|
||||
vrd->sni_len, data + vrd->sni_offset);
|
||||
vrd->sni_len, vrd->sni_ptr);
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -116,11 +116,115 @@ check_domain:
|
||||
return 0;
|
||||
}
|
||||
|
||||
int analyze_tls_message(
|
||||
const struct section_config_t *section,
|
||||
const uint8_t *message_data,
|
||||
size_t message_length,
|
||||
struct tls_verdict *tlsv
|
||||
) {
|
||||
*tlsv = (struct tls_verdict){0};
|
||||
const uint8_t *handshakeProto = message_data;
|
||||
const uint8_t *data_end = message_data + message_length;
|
||||
|
||||
#define TLS_CONTENT_TYPE_HANDSHAKE 0x16
|
||||
#define TLS_HANDSHAKE_TYPE_CLIENT_HELLO 0x01
|
||||
#define TLS_EXTENSION_SNI 0x0000
|
||||
#define TLS_EXTENSION_CLIENT_HELLO_ENCRYPTED 0xfe0d
|
||||
if (handshakeProto + 1 >= data_end)
|
||||
goto invalid;
|
||||
|
||||
uint8_t handshakeType = *handshakeProto;
|
||||
|
||||
if (handshakeType != TLS_HANDSHAKE_TYPE_CLIENT_HELLO)
|
||||
goto next;
|
||||
|
||||
const uint8_t *msgPtr = handshakeProto;
|
||||
msgPtr += 1;
|
||||
msgPtr += 3 + 2 + 32;
|
||||
|
||||
if (msgPtr + 1 >= data_end)
|
||||
goto invalid;
|
||||
uint8_t sessionIdLength = *msgPtr;
|
||||
msgPtr++;
|
||||
msgPtr += sessionIdLength;
|
||||
|
||||
if (msgPtr + 2 >= data_end)
|
||||
goto invalid;
|
||||
uint16_t ciphersLength = ntohs(*(uint16_t *)msgPtr);
|
||||
msgPtr += 2;
|
||||
msgPtr += ciphersLength;
|
||||
|
||||
if (msgPtr + 1 >= data_end)
|
||||
goto invalid;
|
||||
uint8_t compMethodsLen = *msgPtr;
|
||||
msgPtr++;
|
||||
msgPtr += compMethodsLen;
|
||||
|
||||
if (msgPtr + 2 >= data_end)
|
||||
goto invalid;
|
||||
uint16_t extensionsLen = ntohs(*(uint16_t *)msgPtr);
|
||||
msgPtr += 2;
|
||||
|
||||
const uint8_t *extensionsPtr = msgPtr;
|
||||
const uint8_t *extensions_end = extensionsPtr + extensionsLen;
|
||||
if (extensions_end > data_end) extensions_end = data_end;
|
||||
|
||||
while (extensionsPtr < extensions_end) {
|
||||
const uint8_t *extensionPtr = extensionsPtr;
|
||||
if (extensionPtr + 4 >= extensions_end)
|
||||
goto invalid;
|
||||
|
||||
uint16_t extensionType =
|
||||
ntohs(*(uint16_t *)extensionPtr);
|
||||
extensionPtr += 2;
|
||||
|
||||
uint16_t extensionLen =
|
||||
ntohs(*(uint16_t *)extensionPtr);
|
||||
extensionPtr += 2;
|
||||
|
||||
|
||||
if (extensionPtr + extensionLen > extensions_end)
|
||||
goto invalid;
|
||||
|
||||
if (extensionType != TLS_EXTENSION_SNI)
|
||||
goto nextExtension;
|
||||
|
||||
const uint8_t *sni_ext_ptr = extensionPtr;
|
||||
|
||||
if (sni_ext_ptr + 2 >= extensions_end)
|
||||
goto invalid;
|
||||
uint16_t sni_ext_dlen = ntohs(*(uint16_t *)sni_ext_ptr);
|
||||
|
||||
sni_ext_ptr += 2;
|
||||
|
||||
const uint8_t *sni_ext_end = sni_ext_ptr + sni_ext_dlen;
|
||||
if (sni_ext_end >= extensions_end)
|
||||
goto invalid;
|
||||
|
||||
if (sni_ext_ptr + 3 >= sni_ext_end)
|
||||
goto invalid;
|
||||
sni_ext_ptr++;
|
||||
uint16_t sni_len = ntohs(*(uint16_t *)sni_ext_ptr);
|
||||
sni_ext_ptr += 2;
|
||||
|
||||
if (sni_ext_ptr + sni_len > sni_ext_end)
|
||||
goto invalid;
|
||||
|
||||
const char *sni_name = (char *)sni_ext_ptr;
|
||||
|
||||
tlsv->sni_ptr = (const uint8_t *)sni_name;
|
||||
tlsv->sni_len = sni_len;
|
||||
tlsv->target_sni_ptr = tlsv->sni_ptr;
|
||||
tlsv->target_sni_len = tlsv->sni_len;
|
||||
|
||||
analyze_sni_str(section, sni_name, sni_len, tlsv);
|
||||
return TLS_MESSAGE_ANALYZE_FOUND;
|
||||
|
||||
nextExtension:
|
||||
extensionsPtr += 2 + 2 + extensionLen;
|
||||
}
|
||||
|
||||
next:
|
||||
return TLS_MESSAGE_ANALYZE_GOTO_NEXT;
|
||||
invalid:
|
||||
return TLS_MESSAGE_ANALYZE_INVALID;
|
||||
}
|
||||
|
||||
/**
|
||||
* Processes tls payload of the tcp request.
|
||||
@@ -135,115 +239,56 @@ struct tls_verdict analyze_tls_data(
|
||||
{
|
||||
struct tls_verdict vrd = {0};
|
||||
|
||||
size_t i = 0;
|
||||
const uint8_t *data_end = data + dlen;
|
||||
const uint8_t *message_ptr = data;
|
||||
int ret;
|
||||
|
||||
if (section->sni_detection == SNI_DETECTION_BRUTE) {
|
||||
bruteforce_analyze_sni_str(section, data, dlen, &vrd);
|
||||
goto out;
|
||||
}
|
||||
|
||||
while (i + 4 < dlen) {
|
||||
const uint8_t *msgData = data + i;
|
||||
while (message_ptr + 5 < data_end) {
|
||||
uint8_t tls_content_type = *message_ptr;
|
||||
message_ptr++;
|
||||
|
||||
uint8_t tls_content_type = *msgData;
|
||||
uint8_t tls_vmajor = *(msgData + 1);
|
||||
uint16_t message_length = ntohs(*(uint16_t *)(msgData + 3));
|
||||
uint8_t tls_vmajor = *message_ptr;
|
||||
if (tls_vmajor != 0x03) break;
|
||||
message_ptr++;
|
||||
|
||||
if (tls_vmajor != 0x03) goto nextMessage;
|
||||
uint8_t tls_vminor = *message_ptr;
|
||||
message_ptr++;
|
||||
|
||||
if (i + 5 > dlen) break;
|
||||
uint16_t message_length = ntohs(*(const uint16_t *)message_ptr);
|
||||
message_ptr += 2;
|
||||
|
||||
|
||||
const uint8_t *tls_message_data = message_ptr;
|
||||
// Since real length may be truncated use minimum of two
|
||||
size_t tls_message_length = min(message_length, data_end - message_ptr);
|
||||
|
||||
if (tls_content_type != TLS_CONTENT_TYPE_HANDSHAKE)
|
||||
goto nextMessage;
|
||||
|
||||
const uint8_t *handshakeProto = msgData + 5;
|
||||
ret = analyze_tls_message(
|
||||
section,
|
||||
tls_message_data,
|
||||
tls_message_length,
|
||||
&vrd
|
||||
);
|
||||
|
||||
if (handshakeProto + 1 >= data_end) break;
|
||||
|
||||
uint8_t handshakeType = *handshakeProto;
|
||||
|
||||
if (handshakeType != TLS_HANDSHAKE_TYPE_CLIENT_HELLO)
|
||||
goto nextMessage;
|
||||
|
||||
const uint8_t *msgPtr = handshakeProto;
|
||||
msgPtr += 1;
|
||||
msgPtr += 3 + 2 + 32;
|
||||
|
||||
if (msgPtr + 1 >= data_end) break;
|
||||
uint8_t sessionIdLength = *msgPtr;
|
||||
msgPtr++;
|
||||
msgPtr += sessionIdLength;
|
||||
|
||||
if (msgPtr + 2 >= data_end) break;
|
||||
uint16_t ciphersLength = ntohs(*(uint16_t *)msgPtr);
|
||||
msgPtr += 2;
|
||||
msgPtr += ciphersLength;
|
||||
|
||||
if (msgPtr + 1 >= data_end) break;
|
||||
uint8_t compMethodsLen = *msgPtr;
|
||||
msgPtr++;
|
||||
msgPtr += compMethodsLen;
|
||||
|
||||
if (msgPtr + 2 >= data_end) break;
|
||||
uint16_t extensionsLen = ntohs(*(uint16_t *)msgPtr);
|
||||
msgPtr += 2;
|
||||
|
||||
const uint8_t *extensionsPtr = msgPtr;
|
||||
const uint8_t *extensions_end = extensionsPtr + extensionsLen;
|
||||
if (extensions_end > data_end) extensions_end = data_end;
|
||||
|
||||
while (extensionsPtr < extensions_end) {
|
||||
const uint8_t *extensionPtr = extensionsPtr;
|
||||
if (extensionPtr + 4 >= extensions_end) break;
|
||||
|
||||
uint16_t extensionType =
|
||||
ntohs(*(uint16_t *)extensionPtr);
|
||||
extensionPtr += 2;
|
||||
|
||||
uint16_t extensionLen =
|
||||
ntohs(*(uint16_t *)extensionPtr);
|
||||
extensionPtr += 2;
|
||||
|
||||
|
||||
if (extensionPtr + extensionLen > extensions_end)
|
||||
break;
|
||||
|
||||
if (extensionType != TLS_EXTENSION_SNI)
|
||||
goto nextExtension;
|
||||
|
||||
const uint8_t *sni_ext_ptr = extensionPtr;
|
||||
|
||||
if (sni_ext_ptr + 2 >= extensions_end) break;
|
||||
uint16_t sni_ext_dlen = ntohs(*(uint16_t *)sni_ext_ptr);
|
||||
|
||||
sni_ext_ptr += 2;
|
||||
|
||||
const uint8_t *sni_ext_end = sni_ext_ptr + sni_ext_dlen;
|
||||
if (sni_ext_end >= extensions_end) break;
|
||||
switch (ret) {
|
||||
case TLS_MESSAGE_ANALYZE_GOTO_NEXT:
|
||||
goto nextMessage;
|
||||
case TLS_MESSAGE_ANALYZE_FOUND:
|
||||
case TLS_MESSAGE_ANALYZE_INVALID:
|
||||
default:
|
||||
goto out;
|
||||
|
||||
if (sni_ext_ptr + 3 >= sni_ext_end) break;
|
||||
sni_ext_ptr++;
|
||||
uint16_t sni_len = ntohs(*(uint16_t *)sni_ext_ptr);
|
||||
sni_ext_ptr += 2;
|
||||
|
||||
if (sni_ext_ptr + sni_len > sni_ext_end) break;
|
||||
|
||||
const char *sni_name = (char *)sni_ext_ptr;
|
||||
|
||||
vrd.sni_offset = (uint8_t *)sni_name - data;
|
||||
vrd.sni_target_offset = vrd.sni_offset;
|
||||
vrd.sni_len = sni_len;
|
||||
vrd.sni_target_len = vrd.sni_len;
|
||||
|
||||
analyze_sni_str(section, sni_name, sni_len, data, &vrd);
|
||||
goto out;
|
||||
|
||||
nextExtension:
|
||||
extensionsPtr += 2 + 2 + extensionLen;
|
||||
}
|
||||
|
||||
nextMessage:
|
||||
i += 5 + message_length;
|
||||
message_ptr += tls_message_length;
|
||||
}
|
||||
|
||||
out:
|
||||
|
||||
28
src/tls.h
28
src/tls.h
@@ -9,16 +9,36 @@
|
||||
* Result of analyze_tls_data function
|
||||
*/
|
||||
struct tls_verdict {
|
||||
int target_sni; /* google video hello packet */
|
||||
int sni_offset; /* offset from start of tcp _payload_ */
|
||||
int sni_target_offset; /* offset of target domain instead of entire sni */
|
||||
int sni_target_len; /* offset of target domain instead of entire sni */
|
||||
const uint8_t *sni_ptr;
|
||||
int sni_len;
|
||||
|
||||
int target_sni; /* boolean, 1 if target found */
|
||||
const uint8_t *target_sni_ptr; /* pointer to target domain instead of entire sni */
|
||||
int target_sni_len; /* length of target domain instead of entire sni */
|
||||
};
|
||||
|
||||
#define TLS_CONTENT_TYPE_HANDSHAKE 0x16
|
||||
#define TLS_HANDSHAKE_TYPE_CLIENT_HELLO 0x01
|
||||
#define TLS_EXTENSION_SNI 0x0000
|
||||
#define TLS_EXTENSION_CLIENT_HELLO_ENCRYPTED 0xfe0d
|
||||
|
||||
#define TLS_MESSAGE_ANALYZE_INVALID -1
|
||||
#define TLS_MESSAGE_ANALYZE_FOUND 0
|
||||
#define TLS_MESSAGE_ANALYZE_GOTO_NEXT 1
|
||||
|
||||
int analyze_tls_message(
|
||||
const struct section_config_t *section,
|
||||
const uint8_t *message_data,
|
||||
size_t message_length,
|
||||
struct tls_verdict *tlsv
|
||||
);
|
||||
|
||||
|
||||
/**
|
||||
* Processes the packet and finds TLS Client Hello information inside it.
|
||||
* data pointer points to start of TLS Message (TCP Payload)
|
||||
*
|
||||
* Note that all the constant pointers of tls_verdict will be relative to data pointer
|
||||
*/
|
||||
struct tls_verdict analyze_tls_data(const struct section_config_t *section, const uint8_t *data, uint32_t dlen);
|
||||
|
||||
|
||||
122
test/quic.c
122
test/quic.c
@@ -1,6 +1,21 @@
|
||||
// RFC 9001 Appendix A.
|
||||
static const char quic_testing_payload[] = "\xc0\x00\x00\x00\x01\x08\x83\x94\xc8\xf0\x3e\x51\x57\x08\x00\x00\x44\x9e\x7b\x9a\xec\x34\xd1\xb1\xc9\x8d\xd7\x68\x9f\xb8\xec\x11\xd2\x42\xb1\x23\xdc\x9b\xd8\xba\xb9\x36\xb4\x7d\x92\xec\x35\x6c\x0b\xab\x7d\xf5\x97\x6d\x27\xcd\x44\x9f\x63\x30\x00\x99\xf3\x99\x1c\x26\x0e\xc4\xc6\x0d\x17\xb3\x1f\x84\x29\x15\x7b\xb3\x5a\x12\x82\xa6\x43\xa8\xd2\x26\x2c\xad\x67\x50\x0c\xad\xb8\xe7\x37\x8c\x8e\xb7\x53\x9e\xc4\xd4\x90\x5f\xed\x1b\xee\x1f\xc8\xaa\xfb\xa1\x7c\x75\x0e\x2c\x7a\xce\x01\xe6\x00\x5f\x80\xfc\xb7\xdf\x62\x12\x30\xc8\x37\x11\xb3\x93\x43\xfa\x02\x8c\xea\x7f\x7f\xb5\xff\x89\xea\xc2\x30\x82\x49\xa0\x22\x52\x15\x5e\x23\x47\xb6\x3d\x58\xc5\x45\x7a\xfd\x84\xd0\x5d\xff\xfd\xb2\x03\x92\x84\x4a\xe8\x12\x15\x46\x82\xe9\xcf\x01\x2f\x90\x21\xa6\xf0\xbe\x17\xdd\xd0\xc2\x08\x4d\xce\x25\xff\x9b\x06\xcd\xe5\x35\xd0\xf9\x20\xa2\xdb\x1b\xf3\x62\xc2\x3e\x59\x6d\x11\xa4\xf5\xa6\xcf\x39\x48\x83\x8a\x3a\xec\x4e\x15\xda\xf8\x50\x0a\x6e\xf6\x9e\xc4\xe3\xfe\xb6\xb1\xd9\x8e\x61\x0a\xc8\xb7\xec\x3f\xaf\x6a\xd7\x60\xb7\xba\xd1\xdb\x4b\xa3\x48\x5e\x8a\x94\xdc\x25\x0a\xe3\xfd\xb4\x1e\xd1\x5f\xb6\xa8\xe5\xeb\xa0\xfc\x3d\xd6\x0b\xc8\xe3\x0c\x5c\x42\x87\xe5\x38\x05\xdb\x05\x9a\xe0\x64\x8d\xb2\xf6\x42\x64\xed\x5e\x39\xbe\x2e\x20\xd8\x2d\xf5\x66\xda\x8d\xd5\x99\x8c\xca\xbd\xae\x05\x30\x60\xae\x6c\x7b\x43\x78\xe8\x46\xd2\x9f\x37\xed\x7b\x4e\xa9\xec\x5d\x82\xe7\x96\x1b\x7f\x25\xa9\x32\x38\x51\xf6\x81\xd5\x82\x36\x3a\xa5\xf8\x99\x37\xf5\xa6\x72\x58\xbf\x63\xad\x6f\x1a\x0b\x1d\x96\xdb\xd4\xfa\xdd\xfc\xef\xc5\x26\x6b\xa6\x61\x17\x22\x39\x5c\x90\x65\x56\xbe\x52\xaf\xe3\xf5\x65\x63\x6a\xd1\xb1\x7d\x50\x8b\x73\xd8\x74\x3e\xeb\x52\x4b\xe2\x2b\x3d\xcb\xc2\xc7\x46\x8d\x54\x11\x9c\x74\x68\x44\x9a\x13\xd8\xe3\xb9\x58\x11\xa1\x98\xf3\x49\x1d\xe3\xe7\xfe\x94\x2b\x33\x04\x07\xab\xf8\x2a\x4e\xd7\xc1\xb3\x11\x66\x3a\xc6\x98\x90\xf4\x15\x70\x15\x85\x3d\x91\xe9\x23\x03\x7c\x22\x7a\x33\xcd\xd5\xec\x28\x1c\xa3\xf7\x9c\x44\x54\x6b\x9d\x90\xca\x00\xf0\x64\xc9\x9e\x3d\xd9\x79\x11\xd3\x9f\xe9\xc5\xd0\xb2\x3a\x22\x9a\x23\x4c\xb3\x61\x86\xc4\x81\x9e\x8b\x9c\x59\x27\x72\x66\x32\x29\x1d\x6a\x41\x82\x11\xcc\x29\x62\xe2\x0f\xe4\x7f\xeb\x3e\xdf\x33\x0f\x2c\x60\x3a\x9d\x48\xc0\xfc\xb5\x69\x9d\xbf\xe5\x89\x64\x25\xc5\xba\xc4\xae\xe8\x2e\x57\xa8\x5a\xaf\x4e\x25\x13\xe4\xf0\x57\x96\xb0\x7b\xa2\xee\x47\xd8\x05\x06\xf8\xd2\xc2\x5e\x50\xfd\x14\xde\x71\xe6\xc4\x18\x55\x93\x02\xf9\x39\xb0\xe1\xab\xd5\x76\xf2\x79\xc4\xb2\xe0\xfe\xb8\x5c\x1f\x28\xff\x18\xf5\x88\x91\xff\xef\x13\x2e\xef\x2f\xa0\x93\x46\xae\xe3\x3c\x28\xeb\x13\x0f\xf2\x8f\x5b\x76\x69\x53\x33\x41\x13\x21\x19\x96\xd2\x00\x11\xa1\x98\xe3\xfc\x43\x3f\x9f\x25\x41\x01\x0a\xe1\x7c\x1b\xf2\x02\x58\x0f\x60\x47\x47\x2f\xb3\x68\x57\xfe\x84\x3b\x19\xf5\x98\x40\x09\xdd\xc3\x24\x04\x4e\x84\x7a\x4f\x4a\x0a\xb3\x4f\x71\x95\x95\xde\x37\x25\x2d\x62\x35\x36\x5e\x9b\x84\x39\x2b\x06\x10\x85\x34\x9d\x73\x20\x3a\x4a\x13\xe9\x6f\x54\x32\xec\x0f\xd4\xa1\xee\x65\xac\xcd\xd5\xe3\x90\x4d\xf5\x4c\x1d\xa5\x10\xb0\xff\x20\xdc\xc0\xc7\x7f\xcb\x2c\x0e\x0e\xb6\x05\xcb\x05\x04\xdb\x87\x63\x2c\xf3\xd8\xb4\xda\xe6\xe7\x05\x76\x9d\x1d\xe3\x54\x27\x01\x23\xcb\x11\x45\x0e\xfc\x60\xac\x47\x68\x3d\x7b\x8d\x0f\x81\x13\x65\x56\x5f\xd9\x8c\x4c\x8e\xb9\x36\xbc\xab\x8d\x06\x9f\xc3\x3b\xd8\x01\xb0\x3a\xde\xa2\xe1\xfb\xc5\xaa\x46\x3d\x08\xca\x19\x89\x6d\x2b\xf5\x9a\x07\x1b\x85\x1e\x6c\x23\x90\x52\x17\x2f\x29\x6b\xfb\x5e\x72\x40\x47\x90\xa2\x18\x10\x14\xf3\xb9\x4a\x4e\x97\xd1\x17\xb4\x38\x13\x03\x68\xcc\x39\xdb\xb2\xd1\x98\x06\x5a\xe3\x98\x65\x47\x92\x6c\xd2\x16\x2f\x40\xa2\x9f\x0c\x3c\x87\x45\xc0\xf5\x0f\xba\x38\x52\xe5\x66\xd4\x45\x75\xc2\x9d\x39\xa0\x3f\x0c\xda\x72\x19\x84\xb6\xf4\x40\x59\x1f\x35\x5e\x12\xd4\x39\xff\x15\x0a\xab\x76\x13\x49\x9d\xbd\x49\xad\xab\xc8\x67\x6e\xef\x02\x3b\x15\xb6\x5b\xfc\x5c\xa0\x69\x48\x10\x9f\x23\xf3\x50\xdb\x82\x12\x35\x35\xeb\x8a\x74\x33\xbd\xab\xcb\x90\x92\x71\xa6\xec\xbc\xb5\x8b\x93\x6a\x88\xcd\x4e\x8f\x2e\x6f\xf5\x80\x01\x75\xf1\x13\x25\x3d\x8f\xa9\xca\x88\x85\xc2\xf5\x52\xe6\x57\xdc\x60\x3f\x25\x2e\x1a\x8e\x30\x8f\x76\xf0\xbe\x79\xe2\xfb\x8f\x5d\x5f\xbb\xe2\xe3\x0e\xca\xdd\x22\x07\x23\xc8\xc0\xae\xa8\x07\x8c\xdf\xcb\x38\x68\x26\x3f\xf8\xf0\x94\x00\x54\xda\x48\x78\x18\x93\xa7\xe4\x9a\xd5\xaf\xf4\xaf\x30\x0c\xd8\x04\xa6\xb6\x27\x9a\xb3\xff\x3a\xfb\x64\x49\x1c\x85\x19\x4a\xab\x76\x0d\x58\xa6\x06\x65\x4f\x9f\x44\x00\xe8\xb3\x85\x91\x35\x6f\xbf\x64\x25\xac\xa2\x6d\xc8\x52\x44\x25\x9f\xf2\xb1\x9c\x41\xb9\xf9\x6f\x3c\xa9\xec\x1d\xde\x43\x4d\xa7\xd2\xd3\x92\xb9\x05\xdd\xf3\xd1\xf9\xaf\x93\xd1\xaf\x59\x50\xbd\x49\x3f\x5a\xa7\x31\xb4\x05\x6d\xf3\x1b\xd2\x67\xb6\xb9\x0a\x07\x98\x31\xaa\xf5\x79\xbe\x0a\x39\x01\x31\x37\xaa\xc6\xd4\x04\xf5\x18\xcf\xd4\x68\x40\x64\x7e\x78\xbf\xe7\x06\xca\x4c\xf5\xe9\xc5\x45\x3e\x9f\x7c\xfd\x2b\x8b\x4c\x8d\x16\x9a\x44\xe5\x5c\x88\xd4\xa9\xa7\xf9\x47\x42\x41\xe2\x21\xaf\x44\x86\x00\x18\xab\x08\x56\x97\x2e\x19\x4c\xd9\x34";
|
||||
static const char quic_decrypted_header[] = "\xc3\x00\x00\x00\x01\x08\x83\x94\xc8\xf0\x3e\x51\x57\x08\x00\x00\x44\x9e\x00\x00\x00\x02";
|
||||
static const char quic_decrypted_crypto[] = "\x06\x00\x40\xf1\x01\x00\x00\xed\x03\x03\xeb\xf8\xfa\x56\xf1\x29\x39\xb9\x58\x4a\x38\x96\x47\x2e\xc4\x0b\xb8\x63\xcf\xd3\xe8\x68\x04\xfe\x3a\x47\xf0\x6a\x2b\x69\x48\x4c\x00\x00\x04\x13\x01\x13\x02\x01\x00\x00\xc0\x00\x00\x00\x10\x00\x0e\x00\x00\x0b\x65\x78\x61\x6d\x70\x6c\x65\x2e\x63\x6f\x6d\xff\x01\x00\x01\x00\x00\x0a\x00\x08\x00\x06\x00\x1d\x00\x17\x00\x18\x00\x10\x00\x07\x00\x05\x04\x61\x6c\x70\x6e\x00\x05\x00\x05\x01\x00\x00\x00\x00\x00\x33\x00\x26\x00\x24\x00\x1d\x00\x20\x93\x70\xb2\xc9\xca\xa4\x7f\xba\xba\xf4\x55\x9f\xed\xba\x75\x3d\xe1\x71\xfa\x71\xf5\x0f\x1c\xe1\x5d\x43\xe9\x94\xec\x74\xd7\x48\x00\x2b\x00\x03\x02\x03\x04\x00\x0d\x00\x10\x00\x0e\x04\x03\x05\x03\x06\x03\x02\x03\x08\x04\x08\x05\x08\x06\x00\x2d\x00\x02\x01\x01\x00\x1c\x00\x02\x40\x01\x00\x39\x00\x32\x04\x08\xff\xff\xff\xff\xff\xff\xff\xff\x05\x04\x80\x00\xff\xff\x07\x04\x80\x00\xff\xff\x08\x01\x10\x01\x04\x80\x00\x75\x30\x09\x01\x10\x0f\x08\x83\x94\xc8\xf0\x3e\x51\x57\x08\x06\x04\x80\x00\xff\xff";
|
||||
static const int quic_padding_len = 917;
|
||||
|
||||
|
||||
#include "unity.h"
|
||||
#include "unity_fixture.h"
|
||||
|
||||
#include "quic.h"
|
||||
#include "types.h"
|
||||
#include <stdio.h>
|
||||
#include "tls.h"
|
||||
#include "config.h"
|
||||
|
||||
static struct section_config_t sconf = default_section_config;
|
||||
|
||||
TEST_GROUP(QuicTest);
|
||||
|
||||
TEST_SETUP(QuicTest)
|
||||
@@ -11,7 +26,114 @@ TEST_TEAR_DOWN(QuicTest)
|
||||
{
|
||||
}
|
||||
|
||||
TEST(QuicTest, Test_decrypts)
|
||||
{
|
||||
int ret;
|
||||
uint8_t *decrypted_payload;
|
||||
uint32_t decrypted_payload_len;
|
||||
const uint8_t *decrypted_message;
|
||||
uint32_t decrypted_message_len;
|
||||
|
||||
ret = quic_parse_initial_message(
|
||||
(const uint8_t *)quic_testing_payload, sizeof(quic_testing_payload) - 1,
|
||||
&decrypted_payload, &decrypted_payload_len,
|
||||
&decrypted_message, &decrypted_message_len
|
||||
);
|
||||
|
||||
TEST_ASSERT_EQUAL(ret, 0);
|
||||
|
||||
TEST_ASSERT_EQUAL(sizeof(quic_testing_payload) - 1, decrypted_payload_len);
|
||||
TEST_ASSERT_EQUAL_MEMORY(quic_decrypted_header, decrypted_payload, sizeof(quic_decrypted_header) - 1);
|
||||
TEST_ASSERT_EQUAL(decrypted_message, decrypted_payload + sizeof(quic_decrypted_header) - 1);
|
||||
TEST_ASSERT_EQUAL_MEMORY(quic_decrypted_crypto, decrypted_message, sizeof(quic_decrypted_crypto) - 1);
|
||||
|
||||
const uint8_t *curptr = decrypted_message + (sizeof(quic_decrypted_crypto) - 1);
|
||||
ssize_t curptr_len = decrypted_message_len - (sizeof(quic_decrypted_crypto) - 1);
|
||||
TEST_ASSERT_EQUAL(quic_padding_len, curptr_len);
|
||||
|
||||
while (curptr_len-- > 0) {
|
||||
TEST_ASSERT_EQUAL(0x00, *curptr++);
|
||||
}
|
||||
TEST_ASSERT_EQUAL(decrypted_message + decrypted_message_len, curptr);
|
||||
|
||||
// tag is left
|
||||
TEST_ASSERT_EQUAL(16, decrypted_payload + decrypted_payload_len - curptr);
|
||||
}
|
||||
|
||||
TEST(QuicTest, Test_crypto_parser_valid)
|
||||
{
|
||||
ssize_t fret;
|
||||
struct quic_frame_crypto fr_cr;
|
||||
|
||||
fret = quic_parse_crypto(&fr_cr, (const uint8_t *)quic_decrypted_crypto, sizeof(quic_decrypted_crypto));
|
||||
TEST_ASSERT_EQUAL(sizeof(quic_decrypted_crypto) - 1, fret);
|
||||
TEST_ASSERT_EQUAL(0, fr_cr.offset);
|
||||
TEST_ASSERT_EQUAL(241, fr_cr.payload_length);
|
||||
// one for type, one for offset, two for length
|
||||
TEST_ASSERT_EQUAL(quic_decrypted_crypto + 4, fr_cr.payload);
|
||||
}
|
||||
|
||||
TEST(QuicTest, Test_crypto_parser_tls)
|
||||
{
|
||||
ssize_t fret;
|
||||
int ret;
|
||||
struct quic_frame_crypto fr_cr;
|
||||
struct tls_verdict tlsv;
|
||||
|
||||
fret = quic_parse_crypto(&fr_cr, (const uint8_t *)quic_decrypted_crypto, sizeof(quic_decrypted_crypto));
|
||||
ret = analyze_tls_message(&sconf, fr_cr.payload, fr_cr.payload_length, &tlsv);
|
||||
TEST_ASSERT_EQUAL_STRING_LEN("example.com", tlsv.sni_ptr, 11);
|
||||
}
|
||||
|
||||
TEST(QuicTest, Test_crypto_parser_invalid)
|
||||
{
|
||||
ssize_t fret;
|
||||
struct quic_frame_crypto fr_cr;
|
||||
|
||||
fret = quic_parse_crypto(&fr_cr, NULL, 0);
|
||||
TEST_ASSERT_EQUAL(-EINVAL, fret);
|
||||
}
|
||||
|
||||
TEST(QuicTest, Test_varlength_parser)
|
||||
{
|
||||
uint8_t varlv[4];
|
||||
uint64_t mlen, var;
|
||||
|
||||
varlv[0] = 0x00;
|
||||
varlv[1] = 0x00;
|
||||
varlv[2] = 0x00;
|
||||
varlv[3] = 0x00;
|
||||
mlen = 4;
|
||||
var = quic_parse_varlength(varlv, &mlen);
|
||||
TEST_ASSERT_EQUAL(0, var);
|
||||
TEST_ASSERT_EQUAL(1, mlen);
|
||||
|
||||
varlv[0] = 0x40;
|
||||
varlv[1] = 0xf1;
|
||||
varlv[2] = 0x00;
|
||||
varlv[3] = 0x00;
|
||||
mlen = 4;
|
||||
var = quic_parse_varlength(varlv, &mlen);
|
||||
TEST_ASSERT_EQUAL(241, var);
|
||||
TEST_ASSERT_EQUAL(2, mlen);
|
||||
|
||||
mlen = 2;
|
||||
var = quic_parse_varlength(varlv, &mlen);
|
||||
TEST_ASSERT_EQUAL(241, var);
|
||||
TEST_ASSERT_EQUAL(2, mlen);
|
||||
|
||||
// overflow
|
||||
mlen = 1;
|
||||
var = quic_parse_varlength(varlv, &mlen);
|
||||
TEST_ASSERT_EQUAL(0, var);
|
||||
TEST_ASSERT_EQUAL(0, mlen);
|
||||
}
|
||||
|
||||
TEST_GROUP_RUNNER(QuicTest)
|
||||
{
|
||||
RUN_TEST_CASE(QuicTest, Test_decrypts);
|
||||
RUN_TEST_CASE(QuicTest, Test_crypto_parser_valid);
|
||||
RUN_TEST_CASE(QuicTest, Test_crypto_parser_tls);
|
||||
RUN_TEST_CASE(QuicTest, Test_crypto_parser_invalid);
|
||||
RUN_TEST_CASE(QuicTest, Test_varlength_parser);
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user