Skip to main content

sew_e2ee/
error.rs

1//! Typed errors. No error ever holds key material: variants carry ids, epochs,
2//! slots, sizes and short static reasons only.
3
4use thiserror::Error;
5
6/// A decoder input over its cap ([`crate::config`]), or one that did not decode.
7#[derive(Debug, Clone, PartialEq, Eq, Error)]
8#[non_exhaustive]
9pub enum DecodeError {
10    /// Over the cap; nothing was parsed.
11    #[error("{what}: {len} bytes over the cap of {cap}")]
12    TooLarge {
13        /// The object.
14        what: &'static str,
15        /// Its size.
16        len: usize,
17        /// The cap.
18        cap: usize,
19    },
20    /// Malformed.
21    #[error("{0}: malformed")]
22    Malformed(&'static str),
23}
24
25/// Checks `len ≤ cap` before any parse.
26pub fn check_cap(what: &'static str, len: usize, cap: usize) -> Result<(), DecodeError> {
27    if len > cap {
28        Err(DecodeError::TooLarge { what, len, cap })
29    } else {
30        Ok(())
31    }
32}
33
34/// Trust-anchor errors.
35#[derive(Debug, Clone, PartialEq, Eq, Error)]
36#[non_exhaustive]
37pub enum TrustError {
38    /// A pinned CA, DS key or `external_sender` changed: `E2eeStatus::ServerIdentityChanged`.
39    #[error("the server's call identity changed ({what})")]
40    ServerIdentityChanged {
41        /// `ca` or `external_sender` (the `report` kind follows from it).
42        what: &'static str,
43    },
44    /// READY's `call_keys` is malformed.
45    #[error("READY call_keys: {0}")]
46    Malformed(&'static str),
47    /// The device store failed.
48    #[error("device store: {0}")]
49    Store(String),
50}
51
52/// Device identity and enrollment errors.
53#[derive(Debug, Clone, PartialEq, Eq, Error)]
54#[non_exhaustive]
55pub enum IdentityError {
56    /// The certificate is for another member.
57    #[error("the certificate names another member")]
58    WrongMember,
59    /// The certificate's key is not this device's key.
60    #[error("the certificate names another device key")]
61    WrongKey,
62    /// The certificate failed a check (decode, CA signature, install, validity, CRL).
63    #[error("device certificate refused: {0}")]
64    BadCert(&'static str),
65    /// Not enrolled, or the stored certificate is no longer usable.
66    #[error("the device is not enrolled")]
67    NotEnrolled,
68    /// A device signature did not verify.
69    #[error("the device signature does not verify")]
70    BadSignature,
71    /// The returned `key_package_ref` is not this package's `hash_ref`.
72    #[error("the key_package_ref does not match the package")]
73    RefMismatch,
74    /// The package has not been uploaded (no ref yet).
75    #[error("the key package has no ref (upload it first)")]
76    NoRef,
77    /// The package is stale (over 60 min old or past the server TTL).
78    #[error("the key package is stale")]
79    Stale,
80    /// MLS refused to build the package.
81    #[error("MLS: {0}")]
82    Mls(&'static str),
83    /// The server's identity changed (no joins).
84    #[error("the server's call identity changed")]
85    ServerIdentityChanged,
86    /// The store failed.
87    #[error("device store: {0}")]
88    Store(String),
89}
90
91/// Why a session could not start or continue.
92#[derive(Debug, Clone, PartialEq, Eq, Error)]
93#[non_exhaustive]
94pub enum SessionError {
95    /// A `params` value outside its allowed range.
96    #[error("params: {key} out of range")]
97    Params {
98        /// The key.
99        key: &'static str,
100    },
101    /// `key_ring_size` is not 64.
102    #[error("key_ring_size must be 64, got {got}")]
103    RingSize {
104        /// The value received.
105        got: u32,
106    },
107    /// `CallKeysInit.lk_identity` is not `p_` + base32(`join_id`).
108    #[error("lk_identity does not match join_id")]
109    IdentityMismatch,
110    /// The `external_sender` differs from the pinned bytes.
111    #[error("external_sender differs from the pin")]
112    ExternalSenderMismatch,
113    /// A ciphersuite other than 0x0001.
114    #[error("unsupported ciphersuite {0:#06x}")]
115    UnsupportedCiphersuite(u16),
116    /// The session has left (terminal).
117    #[error("the session has left")]
118    Left,
119    /// An input that does not apply in this state (ignored; the state is unchanged).
120    #[error("ignored: {0}")]
121    Ignored(&'static str),
122    /// A decoder refused its input.
123    #[error(transparent)]
124    Decode(#[from] DecodeError),
125    /// An MLS operation failed; recovery output is already queued.
126    #[error("MLS: {0}")]
127    Mls(&'static str),
128    /// A message failed a member-side check; recovery output is already queued.
129    #[error("refused: {0}")]
130    Refused(&'static str),
131    /// The device identity is unusable.
132    #[error(transparent)]
133    Identity(#[from] IdentityError),
134}
135
136/// Why a join failed for good.
137#[derive(Debug, Clone, PartialEq, Eq, Error)]
138#[non_exhaustive]
139pub enum JoinError {
140    /// `422 e2ee_unsupported_client` with `details.unsupported`.
141    #[error("this client cannot join encrypted calls ({unsupported})")]
142    Unsupported {
143        /// `sender_key_index`, `kdf`, `key_ring_size`, `browser`, …
144        unsupported: String,
145    },
146    /// The key-package ref was refused twice (`e2ee_key_package_ref_unknown` / `_used`).
147    #[error("the key package was refused twice")]
148    KeyPackage,
149    /// The session could not start.
150    #[error(transparent)]
151    Session(#[from] SessionError),
152}
153
154/// Why a recorder release could not be opened.
155#[derive(Debug, Clone, PartialEq, Eq, Error)]
156#[non_exhaustive]
157pub enum ReleaseError {
158    /// `enc` is not 32 bytes or `ct` not 48.
159    #[error("release sizes")]
160    Sizes,
161    /// The `info` binding or the ciphertext does not match (AEAD failure).
162    #[error("release does not open")]
163    Aead,
164    /// The release names a malformed slot or identity.
165    #[error("release: {0}")]
166    Malformed(&'static str),
167}
168
169/// Why a signed `LeafBinding` table was refused.
170#[derive(Debug, Clone, PartialEq, Eq, Error)]
171#[non_exhaustive]
172pub enum BindingError {
173    /// The DS signature does not verify for this call, group and epoch.
174    #[error("binding table signature")]
175    Signature,
176    /// An identity does not match `^p_[a-z2-7]{26}$` or `p_ + base32(join_id)`.
177    #[error("binding table identity")]
178    Identity,
179    /// A leaf, ref, join id or identity appears twice, or keys are mixed or unordered.
180    #[error("binding table duplicate or order")]
181    Duplicate,
182    /// The table is for another call, group or epoch, or has the wrong key type.
183    #[error("binding table scope: {0}")]
184    Scope(&'static str),
185    /// A tree leaf has no entry, or an entry names no leaf of the tree.
186    #[error("binding table does not cover the tree")]
187    Coverage,
188    /// A live leaf's identity changed.
189    #[error("binding table changes a live leaf's identity")]
190    Changed,
191    /// The local leaf's entry is not this session's join id.
192    #[error("binding table names another join for the local leaf")]
193    Local,
194    /// Over the size cap.
195    #[error(transparent)]
196    Decode(#[from] DecodeError),
197}
198
199/// Why a recording chunk could not be opened.
200#[derive(Debug, Clone, PartialEq, Eq, Error)]
201#[non_exhaustive]
202pub enum ChunkError {
203    /// The 16-byte header is malformed (magic, version, chunk size, reserved).
204    #[error("recording header: {0}")]
205    Header(&'static str),
206    /// The ciphertext is shorter than a tag or longer than a chunk.
207    #[error("recording chunk length")]
208    Length,
209    /// The tag does not verify (wrong key, index, `last` flag or tampering).
210    #[error("recording chunk does not open")]
211    Aead,
212    /// The file length does not fit the stream format.
213    #[error("recording file length")]
214    FileLength,
215}