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 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    /// 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    /// protocol address is invalid: {name}.{device_id}
68    InvalidProtocolAddress { name: String, device_id: u32 },
69    /// session with {0} not found
70    SessionNotFound(crate::ProtocolAddress),
71    /// invalid session: {0}
72    InvalidSessionStructure(&'static str),
73    /// invalid sender key session with distribution ID {distribution_id}
74    InvalidSenderKeySession { distribution_id: Uuid },
75    /// session for {0} has invalid registration ID {1:X}
76    InvalidRegistrationId(crate::ProtocolAddress, u32),
77
78    /// message with old counter {0} / {1}
79    DuplicatedMessage(u32, u32),
80    /// invalid {0:?} message: {1}
81    InvalidMessage(crate::CiphertextMessageType, &'static str),
82
83    /// error while invoking an ffi callback: {0}
84    FfiBindingError(String),
85    /// error in method call '{0}': {1}
86    ApplicationCallbackError(
87        &'static str,
88        #[source] Box<dyn std::error::Error + Send + Sync + UnwindSafe + 'static>,
89    ),
90
91    /// invalid sealed sender message: {0}
92    InvalidSealedSenderMessage(String),
93    /// unknown sealed sender message version {0}
94    UnknownSealedSenderVersion(u8),
95    /// self send of a sealed sender message
96    SealedSenderSelfSend,
97
98    /// bad KEM key type <{0:#04x}>
99    BadKEMKeyType(u8),
100    /// unexpected KEM key type <{0:#04x}> (expected <{1:#04x}>)
101    WrongKEMKeyType(u8, u8),
102    /// bad KEM key length <{1}> for key with type <{0}>
103    BadKEMKeyLength(kem::KeyType, usize),
104    /// bad KEM ciphertext length <{1}> for key with type <{0}>
105    BadKEMCiphertextLength(kem::KeyType, usize),
106}
107
108impl SignalProtocolError {
109    /// Convenience factory for [`SignalProtocolError::ApplicationCallbackError`].
110    #[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}