//! `Noise_IKpsk2` handshake errors #![allow(clippy::module_name_repetitions)] use std::error::Error; use std::fmt::{Display, Formatter}; /// Represents a error while doing Noise handshake operations #[derive(Debug, Clone)] pub enum NoiseError { /// Represents an error while parsing a Noise packet PacketParseError(NoisePacketParseError), /// Represents an opaque error from ChaCha ChaCha20Error(chacha20poly1305::Error), /// Represents that the packet had a missing or incorrect cookie MAC. PacketUnauthenticated, /// Represents that the packet had the wrong i_i value UnrelatedHandshakePacket, /// Represents that the packet has been replayed and should be dropped PacketReplayed } impl Error for NoiseError {} impl Display for NoiseError { fn fmt(&self, f: &mut Formatter<'_>) -> std::fmt::Result { match &self { Self::PacketParseError(err) => write!(f, "{}", err), Self::ChaCha20Error(error) => write!(f, "Encryption error: {}", error), Self::PacketUnauthenticated => write!(f, "Unauthenticated packet"), Self::UnrelatedHandshakePacket => write!(f, "Unrelated handshake packet"), Self::PacketReplayed => write!(f, "Packet was replayed") } } } /// Represents and error while parsing a Noise packet #[derive(Debug, Clone)] pub enum NoisePacketParseError { /// Represents an invalid packet length while parsing a packet. The first was the expected length, the second was the actual length InvalidLength(usize, usize), /// Represents that the packet being parsed is of the incorrect type. The first value is the expected packet type, the second was the actual packet type WrongPacketType(usize, usize), /// Represents that the expected 3 bytes of reserved space are missing MissingReserved, /// Represents that the index expected to be present on the message is incorrect WrongIndex(usize, usize), /// Indicates that this packet has been replayed and should be ignored. ReplayAttack, } impl Display for NoisePacketParseError { fn fmt(&self, f: &mut Formatter<'_>) -> std::fmt::Result { match &self { Self::InvalidLength(expected, got) => write!(f, "Invalid packet length: expected {} got {}", expected, got), Self::WrongPacketType(expected, got) => write!(f, "Incorrect packet type: expected {} got {}", expected, got), Self::MissingReserved => write!(f, "Missing reserved bytes"), Self::WrongIndex(expected, got) => write!(f, "Incorrect session index: expected {} got {}", expected, got), Self::ReplayAttack => write!(f, "!!! !!! THIS PACKET HAS BEEN REPLAYED !!! !!! Something fishy is going on.") } } }