Skip to content

Commit 215b5db

Browse files
authored
Fix checking packet header under disable_1rtt_encryption mode (#413)
1 parent 2622b4c commit 215b5db

File tree

3 files changed

+34
-14
lines changed

3 files changed

+34
-14
lines changed

.github/workflows/tquic-integration.yml

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,3 +23,7 @@ jobs:
2323
run: |
2424
cd tools/tests/
2525
bash ./tquic_tools_test.sh -b ../../target/release/ -t multipath_redundant,multipath_minrtt,multipath_roundrobin -f 1000M -p 5
26+
- name: Run integration tests for disable_1rtt_encryption
27+
run: |
28+
cd tools/tests/
29+
bash ./tquic_tools_test.sh -b ../../target/release/ -t multipath_minrtt -c '~~disable-encryption' -s '~~disable-encryption'

src/connection/connection.rs

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1680,7 +1680,11 @@ impl Connection {
16801680
.tls_session
16811681
.get_overhead(level)
16821682
.ok_or(Error::InternalError)?;
1683-
let total_overhead = pkt_num_offset + pkt_num_len + crypto_overhead;
1683+
let total_overhead = if !self.is_encryption_disabled(hdr.pkt_type) {
1684+
pkt_num_offset + pkt_num_len + crypto_overhead
1685+
} else {
1686+
pkt_num_offset + pkt_num_len
1687+
};
16841688

16851689
match left.checked_sub(total_overhead) {
16861690
Some(val) => left = val,
@@ -1727,6 +1731,8 @@ impl Connection {
17271731
// fields) in bytes
17281732
let payload_len = write_status.written;
17291733
if pkt_type != PacketType::OneRTT {
1734+
// Note: This type of packet is always encrypted, even if the disable_1rtt_encryption
1735+
// transport parameter is successfully negotiated.
17301736
let len = pkt_num_len + payload_len + crypto_overhead;
17311737
let mut out = &mut out[hdr_offset..];
17321738
out.write_varint_with_len(len as u64, crate::LENGTH_FIELD_LEN)?;

src/packet.rs

Lines changed: 23 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -541,33 +541,43 @@ pub(crate) fn decrypt_header(
541541
aead: &Open,
542542
plaintext_mode: bool,
543543
) -> Result<()> {
544-
if pkt_buf.len() < pkt_num_offset + MAX_PKT_NUM_LEN + SAMPLE_LEN {
544+
let pkt_buf_min = if !plaintext_mode {
545+
pkt_num_offset + MAX_PKT_NUM_LEN + SAMPLE_LEN
546+
} else {
547+
// All aspects of encryption on 1-RTT packets are removed and it is no
548+
// longer including an AEAD tag.
549+
pkt_num_offset + MAX_PKT_NUM_LEN
550+
};
551+
if pkt_buf.len() < pkt_buf_min {
545552
return Err(Error::BufferTooShort);
546553
}
547554

555+
// Decrypt packet haader if needed
548556
let mut first = pkt_buf[0];
549-
let sample_start = pkt_num_offset + MAX_PKT_NUM_LEN;
550-
let sample = &pkt_buf[sample_start..sample_start + SAMPLE_LEN];
551-
let mask = aead.new_mask(sample)?;
552-
553-
// Remove protection of bits in the first byte
554-
if !plaintext_mode {
557+
let (pkt_num_len, pkt_num_buf) = if !plaintext_mode {
558+
// Remove protection of bits in the first byte
559+
let sample_start = pkt_num_offset + MAX_PKT_NUM_LEN;
560+
let sample = &pkt_buf[sample_start..sample_start + SAMPLE_LEN];
561+
let mask = aead.new_mask(sample)?;
555562
if PacketHeader::long_header(first) {
556563
first ^= mask[0] & 0x0f;
557564
} else {
558565
first ^= mask[0] & 0x1f;
559566
}
560-
}
561567

562-
let pkt_num_len = usize::from((first & PKT_NUM_LEN_MASK) + 1);
563-
let pkt_num_buf = &mut pkt_buf[pkt_num_offset..pkt_num_offset + pkt_num_len];
568+
let pkt_num_len = usize::from((first & PKT_NUM_LEN_MASK) + 1);
569+
let pkt_num_buf = &mut pkt_buf[pkt_num_offset..pkt_num_offset + pkt_num_len];
564570

565-
// Remove protection of packet number field
566-
if !plaintext_mode {
571+
// Remove protection of packet number field
567572
for i in 0..pkt_num_len {
568573
pkt_num_buf[i] ^= mask[i + 1];
569574
}
570-
}
575+
(pkt_num_len, pkt_num_buf)
576+
} else {
577+
let pkt_num_len = usize::from((first & PKT_NUM_LEN_MASK) + 1);
578+
let pkt_num_buf = &mut pkt_buf[pkt_num_offset..pkt_num_offset + pkt_num_len];
579+
(pkt_num_len, pkt_num_buf)
580+
};
571581

572582
// Extract packet number corresponding to the length.
573583
let pkt_num = {

0 commit comments

Comments
 (0)