Skip to main content

openmls/treesync/
errors.rs

1//! TreeSync errors
2//!
3//! This module exposes [`ApplyUpdatePathError`] and [`PublicTreeError`].
4
5use thiserror::Error;
6
7use super::*;
8use crate::{
9    binary_tree::MlsBinaryTreeDiffError, ciphersuite::signable::SignatureError,
10    error::LibraryError, treesync::node::leaf_node::LeafNodeBuildError,
11};
12
13// === Public errors ===
14
15/// Public tree error
16#[derive(Error, Debug, PartialEq, Eq, Clone)]
17pub enum PublicTreeError {
18    /// See [`LibraryError`] for more details.
19    #[error(transparent)]
20    LibraryError(#[from] LibraryError),
21    /// The derived public key doesn't match the one in the tree.
22    #[error("The derived public key doesn't match the one in the tree.")]
23    PublicKeyMismatch,
24    /// Found two KeyPackages with the same public key.
25    #[error("Found two KeyPackages with the same public key.")]
26    DuplicateKeyPackage,
27    /// Couldn't find our own key package in this tree.
28    #[error("Couldn't find our own key package in this tree.")]
29    MissingKeyPackage,
30    /// The tree is malformed.
31    #[error("The tree is malformed.")]
32    MalformedTree,
33    /// A parent hash was invalid.
34    #[error("A parent hash was invalid.")]
35    InvalidParentHash,
36    /// An update failed because the provided credential has a different identity than the one in the leaf node.
37    #[error("An update failed because the provided credential has a different identity than the one in the leaf node.")]
38    IdentityMismatch,
39    /// See [`SignatureError`] for more details.
40    #[error(transparent)]
41    SignatureError(#[from] SignatureError),
42}
43
44/// Apply update path error
45#[derive(Error, Debug, PartialEq, Clone)]
46pub enum ApplyUpdatePathError {
47    /// See [`LibraryError`] for more details.
48    #[error(transparent)]
49    LibraryError(#[from] LibraryError),
50    /// The length of the received update path and that of the sender's direct path do not match.
51    #[error(
52        "The length of the received update path and that of the sender's direct path do not match."
53    )]
54    PathLengthMismatch,
55    /// The received update path and the derived nodes are not identical.
56    #[error("The received update path and the derived nodes are not identical.")]
57    PathMismatch,
58    /// The parent hash of the ney key package is invalid.
59    #[error("The parent hash of the ney key package is invalid.")]
60    ParentHashMismatch,
61    /// The parent hash of the ney key package is missing.
62    #[error("The parent hash of the ney key package is missing.")]
63    MissingParentHash,
64    /// Unable to decrypt the path node.
65    #[error("Unable to decrypt the path node.")]
66    UnableToDecrypt,
67    /// Unable to find sender in tree.
68    #[error("Unable to find sender in tree.")]
69    MissingSender,
70    /// Tree is already at maximum size.
71    #[error("Tree is already at maximum size.")]
72    TreeFull,
73    /// External Committer used the wrong index.
74    #[error("External Committer used the wrong index.")]
75    InconsistentSenderIndex,
76    /// The own leaf is not in the tree.
77    #[error("The own leaf is not in the tree.")]
78    MissingOwnLeaf,
79    /// The ciphertext is missing in the update path node.
80    #[error("The ciphertext is missing in the update path node.")]
81    MissingCiphertext,
82}
83
84// === Crate errors ===
85
86// `UnsupportedExtension` is only used in tests for now
87#[allow(dead_code)]
88/// TreeSync error
89#[derive(Error, Debug, PartialEq, Clone)]
90pub(crate) enum TreeSyncError {
91    /// See [`LibraryError`] for more details.
92    #[error(transparent)]
93    LibraryError(#[from] LibraryError),
94    /// A requested leaf is not in the tree.
95    #[error("The leaf does not exist in the tree.")]
96    LeafNotInTree,
97    /// See [`TreeSyncSetPathError`] for more details.
98    #[error(transparent)]
99    SetPathError(#[from] DerivePathError),
100    /// See [`MlsBinaryTreeError`] for more details.
101    #[error(transparent)]
102    BinaryTreeError(#[from] MlsBinaryTreeError),
103    /// See [`TreeSyncDiffError`] for more details.
104    #[error(transparent)]
105    TreeSyncDiffError(#[from] TreeSyncDiffError),
106    /// See [`PathSecretError`] for more details.
107    #[error(transparent)]
108    DerivationError(#[from] PathSecretError),
109
110    /// See [`CryptoError`] for more details.
111    #[error(transparent)]
112    CryptoError(#[from] CryptoError),
113    /// An extension type is not supported by a leaf in the tree.
114    #[error("An extension type is not supported by a leaf in the tree.")]
115    UnsupportedExtension,
116    /// A capability is not supported by a leaf in the tree.
117    #[error("A capability is not supported by a leaf in the tree.")]
118    UnsupportedCapabilities,
119    /// A proposal is not supported by a leaf in the tree.
120    #[error("A proposal is not supported by a leaf in the tree.")]
121    UnsupportedProposal,
122}
123
124/// Derive path error
125#[derive(Error, Debug, PartialEq, Clone)]
126pub(crate) enum DerivePathError {
127    /// See [`LibraryError`] for more details.
128    #[error(transparent)]
129    LibraryError(#[from] LibraryError),
130    /// The derived public key doesn't match the one in the tree.
131    #[error("The derived public key doesn't match the one in the tree.")]
132    PublicKeyMismatch,
133}
134
135/// TreeSync add leaf error
136#[derive(Error, Debug, PartialEq, Clone)]
137pub enum TreeSyncAddLeaf {
138    /// See [`LibraryError`] for more details.
139    #[error(transparent)]
140    LibraryError(#[from] LibraryError),
141    /// The tree is full, we cannot add any more leaves.
142    #[error("The tree is full, we cannot add any more leaves.")]
143    TreeFull,
144}
145
146/// Error applying this client's own update path.
147///
148/// Distinct from [`TreeSyncAddLeaf`] because applying an own update path also
149/// *builds* the new leaf, which inserting an already-signed one does not.
150#[derive(Error, Debug, PartialEq, Clone)]
151pub enum ApplyOwnUpdatePathError {
152    /// See [`LibraryError`] for more details.
153    #[error(transparent)]
154    LibraryError(#[from] LibraryError),
155    /// The tree is full, we cannot add any more leaves.
156    #[error("The tree is full, we cannot add any more leaves.")]
157    TreeFull,
158    /// The leaf node's capabilities don't cover what the leaf itself uses.
159    #[error(transparent)]
160    LeafNodeBuild(#[from] LeafNodeBuildError),
161}
162
163impl From<TreeSyncAddLeaf> for ApplyOwnUpdatePathError {
164    fn from(e: TreeSyncAddLeaf) -> Self {
165        match e {
166            TreeSyncAddLeaf::LibraryError(e) => Self::LibraryError(e),
167            TreeSyncAddLeaf::TreeFull => Self::TreeFull,
168        }
169    }
170}
171
172/// TreeSync from nodes error
173#[derive(Error, Debug, PartialEq, Clone)]
174pub enum TreeSyncFromNodesError {
175    /// See [`LibraryError`] for more details.
176    #[error(transparent)]
177    LibraryError(#[from] LibraryError),
178    /// See [`PublicTreeError`] for more details.
179    #[error(transparent)]
180    PublicTreeError(#[from] PublicTreeError),
181    /// See [`RatchetTreeError`] for more details.
182    #[error(transparent)]
183    RatchetTreeError(#[from] RatchetTreeError),
184}
185
186/// TreeSync parent hash error
187#[derive(Error, Debug, PartialEq, Clone)]
188pub(crate) enum TreeSyncParentHashError {
189    /// See [`LibraryError`] for more details.
190    #[error(transparent)]
191    LibraryError(#[from] LibraryError),
192    /// Parent hash mismatch.
193    #[error("Parent hash mismatch.")]
194    InvalidParentHash,
195}
196
197/// TreeSync parent hash error
198#[derive(Error, Debug, PartialEq, Clone)]
199pub(crate) enum TreeSyncDiffError {
200    /// See [`LibraryError`] for more details.
201    #[error(transparent)]
202    LibraryError(#[from] LibraryError),
203    #[error(
204        "Couldn't find a fitting private key in the filtered resolution of the given leaf index."
205    )]
206    NoPrivateKeyFound,
207    /// See [`MlsBinaryTreeDiffError`] for more details.
208    #[error(transparent)]
209    TreeDiffError(#[from] MlsBinaryTreeDiffError),
210    /// See [`PathSecretError`] for more details.
211    #[error(transparent)]
212    DerivationError(#[from] PathSecretError),
213    /// See [`MlsBinaryTreeError`] for more details.
214    #[error(transparent)]
215    CreationError(#[from] MlsBinaryTreeError),
216}
217
218/// Errors that can happen during leaf node validation.
219#[derive(Clone, Debug, Error, Eq, PartialEq)]
220pub enum LeafNodeValidationError {
221    /// See [`LifetimeError`] for more details.
222    #[error(transparent)]
223    Lifetime(#[from] LifetimeError),
224    /// Extensions are not acceptable.
225    #[error("Extensions are not acceptable.")]
226    UnsupportedExtensions,
227    /// Proposals are not acceptable.
228    #[error("Proposals are not acceptable.")]
229    UnsupportedProposals,
230    /// Credentials are not acceptable.
231    #[error("Credentials are not acceptable.")]
232    UnsupportedCredentials,
233    /// The leaf node's credential type is not listed in the leaf node's capabilities."
234    #[error("The leaf node's credential type is not listed in the leaf node's capabilities.")]
235    CredentialNotInCapabilities,
236    /// The leaf node's extension types are not (all) listed in the leaf node's capabilities.
237    #[error(
238        "The leaf node's extension types are not (all) listed in the leaf node's capabilities."
239    )]
240    ExtensionsNotInCapabilities,
241    /// The group's ciphersuite is not listed in the leaf node's capabilities.
242    #[error("The group's ciphersuite is not listed in the leaf node's capabilities.")]
243    CiphersuiteNotInCapabilities,
244    /// The leaf node's signature key is already used in the group.
245    #[error("The leaf node's signature key is already used in the group.")]
246    SignatureKeyAlreadyInUse,
247    /// The leaf node's encryption key is already used in the group.
248    #[error("The leaf node's encryption key is already used in the group.")]
249    EncryptionKeyAlreadyInUse,
250    /// The leaf node source is invalid in the given context.
251    #[error("The leaf node source is invalid in the given context.")]
252    InvalidLeafNodeSource,
253    /// The leaf node credential is not supported by all members in the group.
254    #[error("The leaf node credential is not supported by all members in the group.")]
255    LeafNodeCredentialNotSupportedByMember,
256    /// The credential used by a member is not supported by this leaf node.
257    #[error("The credential used by a member is not supported by this leaf node.")]
258    MemberCredentialNotSupportedByLeafNode,
259}
260
261/// Errors that can happen during lifetime validation.
262#[derive(Clone, Debug, Error, Eq, PartialEq)]
263pub enum LifetimeError {
264    /// Lifetime is in the past.
265    #[error("Lifetime is in the past: not_after={not_after}, now={now}.")]
266    Expired {
267        /// The not_after value of the lifetime.
268        not_after: u64,
269        /// The current time when the validation was performed.
270        now: u64,
271    },
272    /// Lifetime is in the future.
273    #[error("Lifetime is in the future: not_before={not_before}, now={now}.")]
274    NotValidYet {
275        /// The not_before value of the lifetime.
276        not_before: u64,
277        /// The current time when the validation was performed.
278        now: u64,
279    },
280    /// System time is before UNIX epoch.
281    #[error("System time is before UNIX epoch.")]
282    SystemTimeBeforeUnixEpoch,
283}
284
285/// Errors that can happen during path validation.
286#[derive(Debug, Clone, PartialEq, Eq, Error)]
287pub enum UpdatePathError {
288    /// The update path contains an invalid type of leaf node.
289    #[error("The update path contains an invalid type of leaf node.")]
290    InvalidType,
291    /// See [`SignatureError`] for more details.
292    #[error(transparent)]
293    SignatureError(#[from] SignatureError),
294}