Skip to main content

keres/
error.rs

1use core::fmt;
2
3/// Errors returned by keres operations.
4///
5/// Variants are deliberately coarse-grained: cryptographic failures never
6/// reveal whether the password was wrong or the data was tampered with, and
7/// internal details (key material, paths inside the vault) are never included
8/// in error text.
9#[derive(Debug, thiserror::Error)]
10#[non_exhaustive]
11pub enum Error {
12    /// Underlying filesystem I/O failed.
13    #[error("io error")]
14    Io(#[from] std::io::Error),
15
16    /// The container header is malformed or truncated.
17    #[error("invalid or corrupted container header")]
18    InvalidHeader,
19
20    /// The container format version is not supported by this build.
21    #[error("unsupported container version: {0}")]
22    UnsupportedVersion(u32),
23
24    /// The vault was written by an older format that this build can upgrade
25    /// but will not read in place. Run `keres migrate <vault>` (or
26    /// [`crate::Vault::migrate`]) before using it.
27    #[error("vault uses a legacy format; run `keres migrate` to upgrade it")]
28    MigrationRequired,
29
30    /// The file is not a current Keres container. This covers every other
31    /// format, including pre-rename Krypton releases and pre-0.4 formats.
32    /// Extract the data with the release that wrote it.
33    #[error("incompatible container format")]
34    IncompatibleFormat,
35
36    /// AEAD authentication failed — wrong password, corrupted data, or
37    /// deliberate tampering.
38    #[error("authentication failed: wrong password or corrupted data")]
39    Authentication,
40
41    /// Symmetric encryption failed.
42    #[error("encryption failed")]
43    Encryption,
44
45    /// Argon2id key derivation failed.
46    #[error("key derivation failed")]
47    KeyDerivation,
48
49    /// KDF parameters are outside the accepted safety bounds.
50    #[error("invalid KDF parameters")]
51    InvalidKdfParams,
52
53    /// A vault-level structure could not be parsed.
54    #[error("invalid vault structure")]
55    InvalidVault,
56
57    /// The vault has already been initialized.
58    #[error("vault already initialized")]
59    VaultExists,
60
61    /// The vault does not exist or is missing required structures.
62    #[error("vault not found")]
63    VaultNotFound,
64
65    /// The operation requires an unlocked vault.
66    #[error("vault is locked")]
67    VaultLocked,
68
69    /// Another process holds the vault's on-disk lock.
70    #[error("vault is busy: another process is using it")]
71    VaultBusy,
72
73    /// The vault is already unlocked.
74    #[error("vault already unlocked")]
75    VaultUnlocked,
76
77    /// An entry name violates naming rules (empty, too long, path separators,
78    /// `..` components).
79    #[error("invalid entry name: {0}")]
80    InvalidEntryName(String),
81
82    /// The requested entry does not exist in the vault.
83    #[error("entry not found: {0}")]
84    EntryNotFound(String),
85
86    /// An entry with this name already exists.
87    #[error("entry already exists: {0}")]
88    EntryExists(String),
89
90    /// A conditional write's precondition did not hold (if-match/if-none-match
91    /// failed). The vault was not modified.
92    #[error("precondition failed: vault state changed")]
93    Conflict,
94
95    /// Serialization/deserialization of an encrypted structure failed after
96    /// successful decryption.
97    #[error("malformed decrypted payload")]
98    MalformedPayload,
99
100    /// A user-level operation could not be performed (e.g. password
101    /// confirmation mismatch).
102    #[error("{0}")]
103    Operation(String),
104
105    /// A streaming operation was cancelled through
106    /// [`crate::streaming::StreamOptions`].
107    #[error("operation cancelled")]
108    Cancelled,
109}
110
111impl Error {
112    /// Convenience constructor for [`Error::InvalidEntryName`].
113    pub(crate) fn invalid_name(name: impl fmt::Display) -> Self {
114        // Only report a redacted preview; never echo arbitrary attacker
115        // controlled bytes at full length.
116        let s = name.to_string();
117        let preview: String = s.chars().take(32).collect();
118        Error::InvalidEntryName(preview)
119    }
120}
121
122/// Result alias used throughout the crate.
123pub type Result<T> = std::result::Result<T, Error>;