libsignal_protocol/
error.rs1use std::panic::UnwindSafe;
7
8use displaydoc::Display;
9use libsignal_core::curve::{CurveError, KeyType};
10use thiserror::Error;
11use uuid::Uuid;
12
13use crate::kem;
14
15pub type Result<T> = std::result::Result<T, SignalProtocolError>;
16
17#[derive(Debug, Display, Error)]
18pub enum SignalProtocolError {
19 InvalidArgument(String),
21 InvalidState(&'static str, String),
23
24 InvalidProtobufEncoding,
26
27 CiphertextMessageTooShort(usize),
29 LegacyCiphertextVersion(u8),
31 UnrecognizedCiphertextVersion(u8),
33 UnrecognizedMessageVersion(u32),
35
36 FingerprintVersionMismatch(u32, u32),
38 FingerprintParsingError,
40
41 NoKeyTypeIdentifier,
43 BadKeyType(u8),
45 BadKeyLength(KeyType, usize),
47
48 SignatureValidationFailed,
50
51 UntrustedIdentity(crate::ProtocolAddress),
53
54 InvalidPreKeyId,
56 InvalidSignedPreKeyId,
58 InvalidKyberPreKeyId,
60
61 InvalidMacKeyLength(usize),
63
64 NoSenderKeyState { distribution_id: Uuid },
66
67 InvalidProtocolAddress { name: String, device_id: u32 },
69 SessionNotFound(crate::ProtocolAddress),
71 InvalidSessionStructure(&'static str),
73 InvalidSenderKeySession { distribution_id: Uuid },
75 InvalidRegistrationId(crate::ProtocolAddress, u32),
77
78 DuplicatedMessage(u32, u32),
80 InvalidMessage(crate::CiphertextMessageType, &'static str),
82
83 FfiBindingError(String),
85 ApplicationCallbackError(
87 &'static str,
88 #[source] Box<dyn std::error::Error + Send + Sync + UnwindSafe + 'static>,
89 ),
90
91 InvalidSealedSenderMessage(String),
93 UnknownSealedSenderVersion(u8),
95 SealedSenderSelfSend,
97
98 BadKEMKeyType(u8),
100 WrongKEMKeyType(u8, u8),
102 BadKEMKeyLength(kem::KeyType, usize),
104 BadKEMCiphertextLength(kem::KeyType, usize),
106}
107
108impl SignalProtocolError {
109 #[inline]
111 pub fn for_application_callback<E: std::error::Error + Send + Sync + UnwindSafe + 'static>(
112 method: &'static str,
113 ) -> impl FnOnce(E) -> Self {
114 move |error| Self::ApplicationCallbackError(method, Box::new(error))
115 }
116}
117
118impl From<CurveError> for SignalProtocolError {
119 fn from(e: CurveError) -> Self {
120 match e {
121 CurveError::NoKeyTypeIdentifier => Self::NoKeyTypeIdentifier,
122 CurveError::BadKeyType(raw) => Self::BadKeyType(raw),
123 CurveError::BadKeyLength(key_type, len) => Self::BadKeyLength(key_type, len),
124 }
125 }
126}