Skip to content

Commit

Permalink
fixed bug where payload tag length was not added to overall packet le…
Browse files Browse the repository at this point in the history
…ngth resulting in inability to decrypt
  • Loading branch information
ilumary committed Aug 4, 2024
1 parent 5ff8cff commit df477f9
Show file tree
Hide file tree
Showing 2 changed files with 14 additions and 16 deletions.
5 changes: 3 additions & 2 deletions project/src/quic/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -230,6 +230,9 @@ impl Endpoint {
Err(error) => panic!("Error: {}", error),
};

print!("I: ");
partial_decode.debug_print();

//stop accepting new connections when entering graceful shutdown
if partial_decode.is_inital() && !cancellation_token.is_cancelled() {
tx_initial
Expand Down Expand Up @@ -313,8 +316,6 @@ impl Connection {
Err(error) => panic!("Error: {}", error),
};

println!("{:?}", head.debug_print());

let mut b = OctetsMut::with_slice(&mut buffer);
let (header_raw, mut payload_cipher) = b.split_at(header_length).unwrap();

Expand Down
25 changes: 11 additions & 14 deletions project/src/quic/packet.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ use rustls::quic::HeaderProtectionKey;

use std::{collections::VecDeque, marker::PhantomData, sync::Arc};

const MAX_PKT_NUM_LEN: u8 = 4;
const MAX_PKT_NUM_LEN: usize = 4;
const SAMPLE_LEN: usize = 16;

pub const LS_TYPE_BIT: u8 = 0x80;
Expand Down Expand Up @@ -64,30 +64,33 @@ impl DatagramBuilder {
let mut i: usize = 0;

while let Some(mut packet) = self.packets.pop_front() {
let tag_len = packet.keys.as_ref().unwrap().local.packet.tag_len();
//add tag length to payload length
packet.header.length += tag_len;

//calculate required buffer length
let mut required_space = packet.header.raw_length
+ packet.header.packet_num_length as usize
+ 1
+ packet.body.len()
+ packet.keys.as_ref().unwrap().local.packet.tag_len();
+ tag_len;

//check for minimum size if datagram contains initial packet
if (i + 1) == size && self.contains_initial && (offset + required_space) < 1200 {
println!("POLSTERING PACKET");
let additional_space = 1200 - (offset + required_space);
required_space += additional_space;
packet.header.length += additional_space;
}

packet.encode_and_encrypt(&mut dgram[offset..required_space])?;
packet.encode_and_encrypt(&mut dgram[offset..offset + required_space])?;

offset += required_space;
i += 1;
}

self.dgram.resize(offset, 0x00);
dgram.resize(offset, 0x00);

Ok((self.dgram, address))
Ok((dgram, address))
}
}

Expand Down Expand Up @@ -233,8 +236,6 @@ impl PacketBuilder<Completed> {

let tag_len = keys.local.packet.tag_len();

println!("NON ENCRYPT (len: {:?}): {:x?}", packet.len(), &packet);

let header_end_off: usize =
self.header.raw_length + self.header.packet_num_length as usize + 1;

Expand All @@ -248,8 +249,6 @@ impl PacketBuilder<Completed> {
.encrypt_in_place(self.header.packet_num as u64, &*header, payload)
.unwrap();

println!("TAG: {:x?}", tag.as_ref());

tag_storage.copy_from_slice(tag.as_ref());

//encrypts the header
Expand All @@ -267,8 +266,6 @@ impl PacketBuilder<Completed> {
)
.unwrap();

println!("ENCRYPT (len: {:?}): {:x?}", packet.len(), &packet);

Ok(())
}
}
Expand Down Expand Up @@ -852,8 +849,8 @@ impl Header {
let mut b = octets::OctetsMut::with_slice(buffer);
b.skip(self.raw_length)?;

let mut pn_and_sample = b.peek_bytes_mut(MAX_PKT_NUM_LEN as usize + SAMPLE_LEN)?;
let (mut pn_cipher, sample) = pn_and_sample.split_at(MAX_PKT_NUM_LEN as usize)?;
let mut pn_and_sample = b.peek_bytes_mut(MAX_PKT_NUM_LEN + SAMPLE_LEN)?;
let (mut pn_cipher, sample) = pn_and_sample.split_at(MAX_PKT_NUM_LEN)?;

match header_key.decrypt_in_place(sample.as_ref(), &mut self.hf, pn_cipher.as_mut()) {
Ok(_) => (),
Expand Down

0 comments on commit df477f9

Please sign in to comment.