stun_coder/message/
errors.rs

1use crate::{AttributeDecodeError, AttributeEncodeError, HeaderDecodeError, HeaderEncodeError, definitions::StunTransactionId};
2use thiserror::Error;
3
4/// Integrity Key Errors
5#[derive(Error, Debug)]
6pub enum IntegrityKeyGenerationError {
7    /// SASLprep failure during key generation
8    #[error("Failed to process key section via SASLprep.")]
9    SASLPrepFailure(#[from] stringprep::Error),
10    /// Fails key generation due to missing `username`. May happen when a STUN message contains the REALM but not the username attribute
11    #[error("No username has been provided for long-term credential key generation")]
12    MissingUsername(),
13}
14
15/// Message encoding errors.
16#[derive(Error, Debug)]
17pub enum MessageDecodeError {
18    /// IO error when reading a field value
19    #[error("Error reading field value.")]
20    ReadFailure(#[from] std::io::Error),
21    /// Failure to decode the STUN header section
22    #[error("Error decoding STUN header.")]
23    HeaderDecodeFailure(#[from] HeaderDecodeError),
24    /// Failure to decode a STUN attribute
25    #[error("Error decoding STUN attribute.")]
26    AttributeDecodeFailure {
27        #[source]
28        ///
29        source: AttributeDecodeError,
30        /// STUN transaction id.
31        transaction_id: StunTransactionId
32    },
33    /// Failure to generate an integrity verification key
34    #[error("Error decoding STUN attribute.")]
35    IntegrityKeyGenerationFailure(#[from] IntegrityKeyGenerationError),
36    /// Fingerprint attribute is not the last one.
37    /// This can mean that either the provided byte data contains more than only the STUN message, or message integrity has been compromised.
38    #[error("Fingerprint attribute is not the last one. Message length: {msg_len}, attribute position: {attr_pos}.")]
39    IncorrectFingerprintAttributePosition {
40        /// STUN message length
41        msg_len: usize,
42        /// Fingerprint attribute position in that message
43        attr_pos: usize,
44    },
45    /// Stored and calculated fingerprints mismatch.
46    /// Means that the message integrity has been compromised.
47    #[error("Fingerprint value mismatch. Attribute value: {attr_value:#X?}, computed value: {computed_value:#X?}.")]
48    FingerprintMismatch {
49        /// Provided CRC32 value
50        attr_value: u32,
51        /// Computed CRC32 value
52        computed_value: u32,
53    },
54    /// The calculated HMAC value doesn't match with the provided one.
55    /// Either the provided `integrity_key` is incorrect or the message integrity has been compromised.
56    #[error("Message integrity is compromised. Attribute HMAC value: {attr_value:#X?}, computed HMAC value: {computed_value:#X?}.")]
57    MessageIntegrityFail {
58        /// Provided HMAC
59        attr_value: Vec<u8>,
60        /// Calculated HMAC
61        computed_value: Vec<u8>,
62    },
63}
64
65/// Message decoding errors.
66#[derive(Error, Debug)]
67pub enum MessageEncodeError {
68    /// IO error when writing a field value
69    #[error("Error writing field value.")]
70    WriteFailure(#[from] std::io::Error),
71    /// Failure to encode the STUN header section.
72    #[error("Error encoding STUN header.")]
73    HeaderEncodeFailure(#[from] HeaderEncodeError),
74    /// Failure to encode a STUN attribute
75    #[error("Error encoding STUN attribute.")]
76    AttributeEncodeFailure(#[from] AttributeEncodeError),
77    /// Failure to generate an integrity verification key
78    #[error("Error decoding STUN attribute.")]
79    IntegrityKeyGenerationFailure(#[from] IntegrityKeyGenerationError),
80    /// The Fingerprint attribute is not the last one provided.
81    #[error("Fingerprint attribute is not the last one. Attributes count: {attr_count}, fingerprint attribute index: {fingerprint_attr_idx}.")]
82    IncorrectFingerprintAttributePosition {
83        /// Amount of provided attributes
84        attr_count: usize,
85        /// Index of the Fingerprint attribute
86        fingerprint_attr_idx: usize,
87    },
88    /// An attribute was added after the MessageIntegrity attribute. Only a single Fingerprint attribute can be added after it.
89    #[error("An attribute was added after the MessageIntegrity attribute. Only the Fingerprint attribute can be placed after the MessageIntegrity attribute.")]
90    AttributeAfterIntegrity(),
91    /// A placeholder MessageIntegrity attribute was set, but no `integrity_key` argument was provided to the `encode` function making the HMAC computation impossible.
92    #[error("Missing message integrity password. A placeholder HMAC value is set in MessageIntegrity attribute but no `integrity_password` is provided as an encoding argument.")]
93    MissingIntegrityPassword(),
94}