Skip to main content

oxabl_cache_crypto/
error.rs

1use std::fmt;
2use std::io;
3
4use oxabl_cache_format::FormatError;
5
6/// Why a cryptographic operation failed. Variants never carry secret material.
7#[derive(Debug, Clone, PartialEq, Eq)]
8#[non_exhaustive]
9pub enum Error {
10    /// The bytes are not a valid frame of the format.
11    Format(FormatError),
12    /// The operating system's random generator failed.
13    Random,
14    /// The key commitment in the header differs from the one derived from the key.
15    KeyCommitment,
16    /// An authentication tag did not verify: the bytes were altered, truncated or reordered,
17    /// or the key is wrong.
18    Authentication,
19    /// The BLAKE3 of the sealed segment differs from the expected segment id.
20    SegmentId,
21    /// An Ed25519 signature or public key did not verify.
22    Signature,
23    /// The compressed stream is invalid or does not meet the rules of the format.
24    Compression(&'static str),
25    /// A size or count exceeds a limit.
26    Limit(&'static str),
27    /// A reader or writer failed.
28    Io(io::ErrorKind),
29    /// A record's signer is not accepted for its namespace under the current roster.
30    SignerNotAccepted,
31    /// A record names a roster version newer than the current roster.
32    RosterVersionAhead,
33    /// A record does not belong with the segment or dictionary it is opened against.
34    Mismatch(&'static str),
35    /// A roster failed verification or is malformed.
36    Roster(&'static str),
37    /// The identity is not a member of the roster.
38    IdentityNotInRoster,
39    /// The keyring does not unwrap to a root key for this roster.
40    Keyring(&'static str),
41    /// An identity string or file is invalid or could not be unlocked.
42    Identity(&'static str),
43}
44
45impl fmt::Display for Error {
46    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
47        match self {
48            Self::Format(e) => write!(f, "format: {e}"),
49            Self::Random => f.write_str("random generator failed"),
50            Self::KeyCommitment => f.write_str("key commitment mismatch"),
51            Self::Authentication => f.write_str("authentication failed"),
52            Self::SegmentId => f.write_str("segment id mismatch"),
53            Self::Signature => f.write_str("signature invalid"),
54            Self::Compression(what) => write!(f, "compression: {what}"),
55            Self::Limit(what) => write!(f, "limit exceeded: {what}"),
56            Self::Io(kind) => write!(f, "i/o: {kind}"),
57            Self::SignerNotAccepted => f.write_str("signer not accepted"),
58            Self::RosterVersionAhead => f.write_str("record is ahead of the roster"),
59            Self::Mismatch(what) => write!(f, "mismatch: {what}"),
60            Self::Roster(what) => write!(f, "roster: {what}"),
61            Self::IdentityNotInRoster => f.write_str("identity is not in the roster"),
62            Self::Keyring(what) => write!(f, "keyring: {what}"),
63            Self::Identity(what) => write!(f, "identity: {what}"),
64        }
65    }
66}
67
68impl std::error::Error for Error {}
69
70impl From<FormatError> for Error {
71    fn from(e: FormatError) -> Self {
72        Self::Format(e)
73    }
74}
75
76impl From<io::Error> for Error {
77    fn from(e: io::Error) -> Self {
78        Self::Io(e.kind())
79    }
80}
81
82/// Result of every operation in this crate.
83pub type Result<T> = std::result::Result<T, Error>;