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 key agreement
44    InvalidKeyAgreement,
45
46    /// invalid signature detected
47    SignatureValidationFailed,
48
49    /// untrusted identity for address {0}
50    UntrustedIdentity(crate::ProtocolAddress),
51
52    /// invalid prekey identifier
53    InvalidPreKeyId,
54    /// invalid signed prekey identifier
55    InvalidSignedPreKeyId,
56    /// invalid Kyber prekey identifier
57    InvalidKyberPreKeyId,
58
59    /// invalid MAC key length <{0}>
60    InvalidMacKeyLength(usize),
61
62    /// missing sender key state for distribution ID {distribution_id}
63    NoSenderKeyState { distribution_id: Uuid },
64
65    /// protocol address is invalid: {name}.{device_id}
66    InvalidProtocolAddress { name: String, device_id: u32 },
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    /// unknown server certificate ID: {0}
96    UnknownSealedSenderServerCertificateId(u32),
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            CurveError::InvalidKeyAgreement => Self::InvalidKeyAgreement,
125        }
126    }
127}