1
  2
  3
  4
  5
  6
  7
  8
  9
 10
 11
 12
 13
 14
 15
 16
 17
 18
 19
 20
 21
 22
 23
 24
 25
 26
 27
 28
 29
 30
 31
 32
 33
 34
 35
 36
 37
 38
 39
 40
 41
 42
 43
 44
 45
 46
 47
 48
 49
 50
 51
 52
 53
 54
 55
 56
 57
 58
 59
 60
 61
 62
 63
 64
 65
 66
 67
 68
 69
 70
 71
 72
 73
 74
 75
 76
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
mod raw;
mod header;
mod incoming;
mod outgoing;
mod payload;

pub use raw::*;
pub use header::*;
pub use incoming::*;
pub use outgoing::*;
pub use payload::*;

#[derive(Debug, Eq, PartialEq, Copy, Clone)]
pub enum PacketType {
    Connection,
    Payload,
    Heartbeat,
    Disconnect,
    Disconnected,
}

impl PacketType {
    pub fn from_u8(value: u8) -> Option<Self> {
        match value {
            0 => Some(Self::Connection),
            1 => Some(Self::Payload),
            2 => Some(Self::Heartbeat),
            3 => Some(Self::Disconnect),
            4 => Some(Self::Disconnected),
            _ => None,
        }
    }

    pub fn to_u8(&self) -> u8 {
        match self {
            Self::Connection => 0,
            Self::Payload => 1,
            Self::Heartbeat => 2,
            Self::Disconnect => 3,
            Self::Disconnected => 4,
        }
    }
}

#[cfg(test)]
mod tests {
    use super::*;

    #[test]
    fn sign_and_verify_packet() {
        use std::io::{Read, Write};
        use crate::security::Secret;

        let secret = Secret::from_bytes([
            0x2, 0x1, 0x2, 0x4, 0x8, 0x24, 0x2, 0x1,
            0x2, 0x4, 0x8, 0x24, 0x2, 0x1, 0x2, 0x4,
            0x8, 0x24, 0x2, 0x1, 0x2, 0x4, 0x8, 0x24,
            0x2, 0x1, 0x2, 0x4, 0x8, 0x24, 0x0, 0x64]);
        let mut outgoing = OutgoingPacket::new();

        outgoing.write(&[0x1, 0x2, 0x3, 0x4, 0x5, 0x6]).expect("It writes into the buffer");

        let buffer = outgoing.write_header_and_sign(15, 12, [0x3, 0x2, 0x1, 0x0], PacketType::Payload.to_u8(), &secret);

        let mut incoming = buffer.verify(&secret).expect("The verification succeeds");

        let mut read_into: [u8; 6] = [0; 6];
        incoming.read(&mut read_into).expect("It reads into the buffer");

        assert_eq!(read_into, [0x1, 0x2, 0x3, 0x4, 0x5, 0x6], "The body matches the written data");
        assert_eq!(incoming.get_sequence_number(), 15);
        assert_eq!(incoming.get_ack_sequence_number(), 12);
        assert_eq!(incoming.get_ack_bits(), [0x3, 0x2, 0x1, 0x0]);
        assert_eq!(incoming.get_packet_type(), Some(PacketType::Payload));
        assert_eq!(incoming.get_body_length(), 6);
    }

    #[test]
    fn it_rejects_a_tampered_packet() {
        use std::io::Write;
        use crate::security::Secret;

        let secret = Secret::from_bytes([
            0x2, 0x1, 0x2, 0x4, 0x8, 0x24, 0x2, 0x1,
            0x2, 0x4, 0x8, 0x24, 0x2, 0x1, 0x2, 0x4,
            0x8, 0x24, 0x2, 0x1, 0x2, 0x4, 0x8, 0x24,
            0x2, 0x1, 0x2, 0x4, 0x8, 0x24, 0x0, 0x64]);
        let mut outgoing = OutgoingPacket::new();

        outgoing.write(&[0x1, 0x2, 0x3, 0x4, 0x5, 0x6]).expect("It writes into the buffer");

        let mut buffer = outgoing.write_header_and_sign(0, 1, [0x0, 0x0, 0x0, 0x0], 1, &secret);

        buffer.get_buffer_mut()[56] = 0x2;

        assert!(buffer.verify(&secret).is_none(), "The packet is invalid");
    }

    #[test]
    fn it_rejects_a_packet_signed_with_a_different_secret() {
        use std::io::Write;
        use crate::security::Secret;

        let secret = Secret::from_bytes([
            0x2, 0x1, 0x2, 0x4, 0x8, 0x24, 0x2, 0x1,
            0x2, 0x4, 0x8, 0x24, 0x2, 0x1, 0x2, 0x4,
            0x8, 0x24, 0x2, 0x1, 0x2, 0x4, 0x8, 0x24,
            0x2, 0x1, 0x2, 0x4, 0x8, 0x24, 0x0, 0x64]);
        let mut outgoing = OutgoingPacket::new();

        outgoing.write(&[0x1, 0x2, 0x3, 0x4, 0x5, 0x6]).expect("It writes into the buffer");

        let buffer = outgoing.write_header_and_sign(0, 1, [0x0, 0x0, 0x0, 0x0], 1, &secret);

        let secret = Secret::from_bytes([
            0x5, 0x1, 0x2, 0x4, 0x8, 0x24, 0x2, 0x1,
            0x2, 0x4, 0x8, 0x24, 0x2, 0x1, 0x2, 0x4,
            0x8, 0x24, 0x2, 0x1, 0x2, 0x4, 0x8, 0x24,
            0x2, 0x1, 0x2, 0x4, 0x8, 0x24, 0x0, 0x64]);

        assert!(buffer.verify(&secret).is_none(), "The packet is invalid");
    }
}