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    /// no key type identifier
37    NoKeyTypeIdentifier,
38    /// bad key type <{0:#04x}>
39    BadKeyType(u8),
40    /// bad key length <{1}> for key with type <{0}>
41    BadKeyLength(KeyType, usize),
42
43    /// invalid signature detected
44    SignatureValidationFailed,
45
46    /// untrusted identity for address {0}
47    UntrustedIdentity(crate::ProtocolAddress),
48
49    /// invalid prekey identifier
50    InvalidPreKeyId,
51    /// invalid signed prekey identifier
52    InvalidSignedPreKeyId,
53    /// invalid Kyber prekey identifier
54    InvalidKyberPreKeyId,
55
56    /// invalid MAC key length <{0}>
57    InvalidMacKeyLength(usize),
58
59    /// missing sender key state for distribution ID {distribution_id}
60    NoSenderKeyState { distribution_id: Uuid },
61
62    /// protocol address is invalid: {name}.{device_id}
63    InvalidProtocolAddress { name: String, device_id: u32 },
64    /// session with {0} not found
65    SessionNotFound(crate::ProtocolAddress),
66    /// invalid session: {0}
67    InvalidSessionStructure(&'static str),
68    /// invalid sender key session with distribution ID {distribution_id}
69    InvalidSenderKeySession { distribution_id: Uuid },
70    /// session for {0} has invalid registration ID {1:X}
71    InvalidRegistrationId(crate::ProtocolAddress, u32),
72
73    /// message with old counter {0} / {1}
74    DuplicatedMessage(u32, u32),
75    /// invalid {0:?} message: {1}
76    InvalidMessage(crate::CiphertextMessageType, &'static str),
77
78    /// error while invoking an ffi callback: {0}
79    FfiBindingError(String),
80    /// error in method call '{0}': {1}
81    ApplicationCallbackError(
82        &'static str,
83        #[source] Box<dyn std::error::Error + Send + Sync + UnwindSafe + 'static>,
84    ),
85
86    /// invalid sealed sender message: {0}
87    InvalidSealedSenderMessage(String),
88    /// unknown sealed sender message version {0}
89    UnknownSealedSenderVersion(u8),
90    /// self send of a sealed sender message
91    SealedSenderSelfSend,
92    /// unknown server certificate ID: {0}
93    UnknownSealedSenderServerCertificateId(u32),
94
95    /// bad KEM key type <{0:#04x}>
96    BadKEMKeyType(u8),
97    /// unexpected KEM key type <{0:#04x}> (expected <{1:#04x}>)
98    WrongKEMKeyType(u8, u8),
99    /// bad KEM key length <{1}> for key with type <{0}>
100    BadKEMKeyLength(kem::KeyType, usize),
101    /// bad KEM ciphertext length <{1}> for key with type <{0}>
102    BadKEMCiphertextLength(kem::KeyType, usize),
103}
104
105impl SignalProtocolError {
106    /// Convenience factory for [`SignalProtocolError::ApplicationCallbackError`].
107    #[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}