use crate::{AttributeDecodeError, AttributeEncodeError, HeaderDecodeError, HeaderEncodeError, definitions::StunTransactionId};
use thiserror::Error;
#[derive(Error, Debug)]
pub enum IntegrityKeyGenerationError {
#[error("Failed to process key section via SASLprep.")]
SASLPrepFailure(#[from] stringprep::Error),
#[error("No username has been provided for long-term credential key generation")]
MissingUsername(),
}
#[derive(Error, Debug)]
pub enum MessageDecodeError {
#[error("Error reading field value.")]
ReadFailure(#[from] std::io::Error),
#[error("Error decoding STUN header.")]
HeaderDecodeFailure(#[from] HeaderDecodeError),
#[error("Error decoding STUN attribute.")]
AttributeDecodeFailure {
#[source]
source: AttributeDecodeError,
transaction_id: StunTransactionId
},
#[error("Error decoding STUN attribute.")]
IntegrityKeyGenerationFailure(#[from] IntegrityKeyGenerationError),
#[error("Fingerprint attribute is not the last one. Message length: {msg_len}, attribute position: {attr_pos}.")]
IncorrectFingerprintAttributePosition {
msg_len: usize,
attr_pos: usize,
},
#[error("Fingerprint value mismatch. Attribute value: {attr_value:#X?}, computed value: {computed_value:#X?}.")]
FingerprintMismatch {
attr_value: u32,
computed_value: u32,
},
#[error("Message integrity is compromised. Attribute HMAC value: {attr_value:#X?}, computed HMAC value: {computed_value:#X?}.")]
MessageIntegrityFail {
attr_value: Vec<u8>,
computed_value: Vec<u8>,
},
}
#[derive(Error, Debug)]
pub enum MessageEncodeError {
#[error("Error writing field value.")]
WriteFailure(#[from] std::io::Error),
#[error("Error encoding STUN header.")]
HeaderEncodeFailure(#[from] HeaderEncodeError),
#[error("Error encoding STUN attribute.")]
AttributeEncodeFailure(#[from] AttributeEncodeError),
#[error("Error decoding STUN attribute.")]
IntegrityKeyGenerationFailure(#[from] IntegrityKeyGenerationError),
#[error("Fingerprint attribute is not the last one. Attributes count: {attr_count}, fingerprint attribute index: {fingerprint_attr_idx}.")]
IncorrectFingerprintAttributePosition {
attr_count: usize,
fingerprint_attr_idx: usize,
},
#[error("An attribute was added after the MessageIntegrity attribute. Only the Fingerprint attribute can be placed after the MessageIntegrity attribute.")]
AttributeAfterIntegrity(),
#[error("Missing message integrity password. A placeholder HMAC value is set in MessageIntegrity attribute but no `integrity_password` is provided as an encoding argument.")]
MissingIntegrityPassword(),
}