libsignal_protocol/
error.rs

1//
2// Copyright 2020-2021 Signal Messenger, LLC.
3// SPDX-License-Identifier: AGPL-3.0-only
4//
5
6use std::panic::UnwindSafe;
7
8use displaydoc::Display;
9use thiserror::Error;
10use uuid::Uuid;
11
12use crate::curve::KeyType;
13use crate::kem;
14
15pub type Result<T> = std::result::Result<T, SignalProtocolError>;
16
17#[derive(Debug, Display, Error)]
18pub enum SignalProtocolError {
19    /// invalid argument: {0}
20    InvalidArgument(String),
21    /// invalid state for call to {0} to succeed: {1}
22    InvalidState(&'static str, String),
23
24    /// protobuf encoding was invalid
25    InvalidProtobufEncoding,
26
27    /// ciphertext serialized bytes were too short <{0}>
28    CiphertextMessageTooShort(usize),
29    /// ciphertext version was too old <{0}>
30    LegacyCiphertextVersion(u8),
31    /// ciphertext version was unrecognized <{0}>
32    UnrecognizedCiphertextVersion(u8),
33    /// unrecognized message version <{0}>
34    UnrecognizedMessageVersion(u32),
35
36    /// fingerprint version number mismatch them {0} us {1}
37    FingerprintVersionMismatch(u32, u32),
38    /// fingerprint parsing error
39    FingerprintParsingError,
40
41    /// no key type identifier
42    NoKeyTypeIdentifier,
43    /// bad key type <{0:#04x}>
44    BadKeyType(u8),
45    /// bad key length <{1}> for key with type <{0}>
46    BadKeyLength(KeyType, usize),
47
48    /// invalid signature detected
49    SignatureValidationFailed,
50
51    /// untrusted identity for address {0}
52    UntrustedIdentity(crate::ProtocolAddress),
53
54    /// invalid prekey identifier
55    InvalidPreKeyId,
56    /// invalid signed prekey identifier
57    InvalidSignedPreKeyId,
58    /// invalid Kyber prekey identifier
59    InvalidKyberPreKeyId,
60
61    /// invalid MAC key length <{0}>
62    InvalidMacKeyLength(usize),
63
64    /// missing sender key state for distribution ID {distribution_id}
65    NoSenderKeyState { distribution_id: Uuid },
66
67    /// session with {0} not found
68    SessionNotFound(crate::ProtocolAddress),
69    /// invalid session: {0}
70    InvalidSessionStructure(&'static str),
71    /// invalid sender key session with distribution ID {distribution_id}
72    InvalidSenderKeySession { distribution_id: Uuid },
73    /// session for {0} has invalid registration ID {1:X}
74    InvalidRegistrationId(crate::ProtocolAddress, u32),
75
76    /// message with old counter {0} / {1}
77    DuplicatedMessage(u32, u32),
78    /// invalid {0:?} message: {1}
79    InvalidMessage(crate::CiphertextMessageType, &'static str),
80
81    /// error while invoking an ffi callback: {0}
82    FfiBindingError(String),
83    /// error in method call '{0}': {1}
84    ApplicationCallbackError(
85        &'static str,
86        #[source] Box<dyn std::error::Error + Send + Sync + UnwindSafe + 'static>,
87    ),
88
89    /// invalid sealed sender message: {0}
90    InvalidSealedSenderMessage(String),
91    /// unknown sealed sender message version {0}
92    UnknownSealedSenderVersion(u8),
93    /// self send of a sealed sender message
94    SealedSenderSelfSend,
95
96    /// bad KEM key type <{0:#04x}>
97    BadKEMKeyType(u8),
98    /// unexpected KEM key type <{0:#04x}> (expected <{1:#04x}>)
99    WrongKEMKeyType(u8, u8),
100    /// bad KEM key length <{1}> for key with type <{0}>
101    BadKEMKeyLength(kem::KeyType, usize),
102    /// bad KEM ciphertext length <{1}> for key with type <{0}>
103    BadKEMCiphertextLength(kem::KeyType, usize),
104}
105
106impl SignalProtocolError {
107    /// Convenience factory for [`SignalProtocolError::ApplicationCallbackError`].
108    #[inline]
109    pub fn for_application_callback<E: std::error::Error + Send + Sync + UnwindSafe + 'static>(
110        method: &'static str,
111    ) -> impl FnOnce(E) -> Self {
112        move |error| Self::ApplicationCallbackError(method, Box::new(error))
113    }
114}