Skip to content

Commit

Permalink
Merge pull request #253 from tox-rs/fix_connection_id_verification
Browse files Browse the repository at this point in the history
Fix connection_id verification
  • Loading branch information
kpp authored Oct 27, 2018
2 parents 6bd5568 + e2fac59 commit 2ac99c6
Show file tree
Hide file tree
Showing 5 changed files with 17 additions and 38 deletions.
39 changes: 9 additions & 30 deletions src/toxcore/tcp/codec.rs
Original file line number Diff line number Diff line change
Expand Up @@ -335,50 +335,29 @@ mod tests {
// Mallory cannot decode the payload of EncryptedPacket
assert!(mallory_codec.decode(&mut buf).err().is_some());
}
fn encode_bytes_to_packet(channel: &Channel, bytes: &[u8]) -> Vec<u8> {
// encrypt it
let encrypted = channel.encrypt(bytes);

// create EncryptedPacket
let encrypted_packet = EncryptedPacket { payload: encrypted };

// serialize EncryptedPacket to binary form
let mut stack_buf = [0; MAX_TCP_ENC_PACKET_SIZE];
let (_, encrypted_packet_size) = encrypted_packet.to_bytes((&mut stack_buf, 0)).unwrap();
stack_buf[..encrypted_packet_size].to_vec()
}
#[test]
fn decode_packet_imcomplete() {
let (alice_channel, bob_channel) = create_channels();
let (alice_channel, _) = create_channels();

let mut buf = BytesMut::from(encode_bytes_to_packet(&alice_channel,b"\x00"));
let mut bob_codec = Codec::new(bob_channel);
let mut buf = BytesMut::new();
let mut bob_codec = Codec::new(alice_channel);

// not enought bytes to decode Packet
assert!(bob_codec.decode(&mut buf).err().is_some());
// not enough bytes to decode Packet
assert!(bob_codec.decode(&mut buf).unwrap().is_none());
}
#[test]
fn decode_packet_error() {
let (alice_channel, bob_channel) = create_channels();
let (alice_channel, _) = create_channels();

let mut alice_codec = Codec::new(alice_channel);
let mut bob_codec = Codec::new(bob_channel);

let mut buf = BytesMut::new();

// bad Data with connection id = 0x0F
let packet = Packet::Data( Data { connection_id: 0x0F, data: vec![13; 42] } );
// bad Data with connection id = 0
let packet = Packet::Data( Data { connection_id: 0, data: vec![13; 42] } );

alice_codec.encode(packet.clone(), &mut buf).expect("Alice should encode");
assert!(bob_codec.decode(&mut buf).is_err());

buf.clear();

// bad Data with connection id = 0xF0
let packet = Packet::Data( Data { connection_id: 0xF0, data: vec![13; 42] } );

alice_codec.encode(packet.clone(), &mut buf).expect("Alice should encode");
assert!(bob_codec.decode(&mut buf).is_err());
assert!(alice_codec.decode(&mut buf).is_err());
}

#[test]
Expand Down
4 changes: 2 additions & 2 deletions src/toxcore/tcp/packet/connect_notification.rs
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ Serialized form:
Length | Content
------ | ------
`1` | `0x02`
`1` | connection_id
`1` | connection_id [ `0x10` .. `0xFF` ]
*/
#[derive(Debug, PartialEq, Clone)]
Expand All @@ -25,7 +25,7 @@ pub struct ConnectNotification {
impl FromBytes for ConnectNotification {
named!(from_bytes<ConnectNotification>, do_parse!(
tag!("\x02") >>
connection_id: be_u8 >>
connection_id: verify!(be_u8, |id| id >= 0x10) >>
(ConnectNotification { connection_id })
));
}
Expand Down
4 changes: 2 additions & 2 deletions src/toxcore/tcp/packet/data.rs
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ Serialized form:
Length | Content
-------- | ------
`1` | connection_id [ `0x10` .. `0xF0` )
`1` | connection_id [ `0x10` .. `0xFF` ]
variable | Data
*/
Expand All @@ -27,7 +27,7 @@ pub struct Data {

impl FromBytes for Data {
named!(from_bytes<Data>, do_parse!(
connection_id: verify!(be_u8, |id| id >= 0x10 && id < 0xF0) >>
connection_id: verify!(be_u8, |id| id >= 0x10) >>
data: rest >>
(Data { connection_id, data: data.to_vec() })
));
Expand Down
4 changes: 2 additions & 2 deletions src/toxcore/tcp/packet/disconnect_notification.rs
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ Serialized form:
Length | Content
------ | ------
`1` | `0x03`
`1` | connection_id
`1` | connection_id [ `0x10` .. `0xFF` ]
*/
#[derive(Debug, PartialEq, Clone)]
Expand All @@ -35,7 +35,7 @@ pub struct DisconnectNotification {
impl FromBytes for DisconnectNotification {
named!(from_bytes<DisconnectNotification>, do_parse!(
tag!("\x03") >>
connection_id: verify!(be_u8, |id| id >= 0x10 && id < 0xF0) >>
connection_id: verify!(be_u8, |id| id >= 0x10) >>
(DisconnectNotification { connection_id })
));
}
Expand Down
4 changes: 2 additions & 2 deletions src/toxcore/tcp/packet/route_response.rs
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ Serialized form:
Length | Content
------ | ------
`1` | `0x01`
`1` | connection_id
`1` | connection_id [ `0x10` .. `0xFF` ]
`32` | Public Key
*/
Expand All @@ -34,7 +34,7 @@ pub struct RouteResponse {
impl FromBytes for RouteResponse {
named!(from_bytes<RouteResponse>, do_parse!(
tag!("\x01") >>
connection_id: be_u8 >>
connection_id: verify!(be_u8, |id| id >= 0x10) >>
pk: call!(PublicKey::from_bytes) >>
(RouteResponse { connection_id, pk })
));
Expand Down

0 comments on commit 2ac99c6

Please sign in to comment.