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 NoKeyTypeIdentifier,
38 BadKeyType(u8),
40 BadKeyLength(KeyType, usize),
42
43 SignatureValidationFailed,
45
46 UntrustedIdentity(crate::ProtocolAddress),
48
49 InvalidPreKeyId,
51 InvalidSignedPreKeyId,
53 InvalidKyberPreKeyId,
55
56 InvalidMacKeyLength(usize),
58
59 NoSenderKeyState { distribution_id: Uuid },
61
62 InvalidProtocolAddress { name: String, device_id: u32 },
64 SessionNotFound(crate::ProtocolAddress),
66 InvalidSessionStructure(&'static str),
68 InvalidSenderKeySession { distribution_id: Uuid },
70 InvalidRegistrationId(crate::ProtocolAddress, u32),
72
73 DuplicatedMessage(u32, u32),
75 InvalidMessage(crate::CiphertextMessageType, &'static str),
77
78 FfiBindingError(String),
80 ApplicationCallbackError(
82 &'static str,
83 #[source] Box<dyn std::error::Error + Send + Sync + UnwindSafe + 'static>,
84 ),
85
86 InvalidSealedSenderMessage(String),
88 UnknownSealedSenderVersion(u8),
90 SealedSenderSelfSend,
92 UnknownSealedSenderServerCertificateId(u32),
94
95 BadKEMKeyType(u8),
97 WrongKEMKeyType(u8, u8),
99 BadKEMKeyLength(kem::KeyType, usize),
101 BadKEMCiphertextLength(kem::KeyType, usize),
103}
104
105impl SignalProtocolError {
106 #[inline]
108 pub fn for_application_callback<E: std::error::Error + Send + Sync + UnwindSafe + 'static>(
109 method: &'static str,
110 ) -> impl FnOnce(E) -> Self {
111 move |error| Self::ApplicationCallbackError(method, Box::new(error))
112 }
113}
114
115impl From<CurveError> for SignalProtocolError {
116 fn from(e: CurveError) -> Self {
117 match e {
118 CurveError::NoKeyTypeIdentifier => Self::NoKeyTypeIdentifier,
119 CurveError::BadKeyType(raw) => Self::BadKeyType(raw),
120 CurveError::BadKeyLength(key_type, len) => Self::BadKeyLength(key_type, len),
121 }
122 }
123}