Skip to main content

hpke_http/
error.rs

1//! Stable public error categories.
2
3use thiserror::Error;
4
5/// Errors returned by the protocol engine.
6///
7/// Messages contain no credentials, plaintext, ciphertext, or parser offsets.
8#[derive(Clone, Copy, Debug, Eq, Error, PartialEq)]
9#[non_exhaustive]
10pub enum Error {
11    /// Configuration is missing, inconsistent, or outside the protocol limits.
12    #[error("invalid configuration")]
13    InvalidConfiguration,
14    /// A caller-provided value exceeds a configured or hard protocol limit.
15    #[error("limit exceeded")]
16    LimitExceeded,
17    /// The envelope is truncated, has trailing data, or has an invalid field.
18    #[error("malformed envelope")]
19    MalformedEnvelope,
20    /// The envelope uses a protocol version this implementation does not support.
21    #[error("unsupported protocol version")]
22    UnsupportedVersion,
23    /// The envelope names a cryptographic suite outside protocol version 3.
24    #[error("unsupported cryptographic suite")]
25    UnsupportedSuite,
26    /// The request uses an HTTP method outside protocol version 3.
27    #[error("unsupported HTTP method")]
28    UnsupportedMethod,
29    /// The request names a recipient key that is not configured on this server.
30    #[error("unknown recipient key")]
31    UnknownRecipientKey,
32    /// A supplied PSK violates the credential length or public-ID rules.
33    #[error("invalid credential")]
34    InvalidCredential,
35    /// An authenticated operation failed.
36    #[error("authentication failed")]
37    AuthenticationFailed,
38    /// The replay-admission provider rejected this authenticated request.
39    #[error("request replay rejected")]
40    ReplayRejected,
41    /// The authenticated request timestamp is expired or too far in the future.
42    #[error("invalid request time")]
43    InvalidRequestTime,
44    /// A replay decision was created for another authenticated request.
45    #[error("replay decision does not match request")]
46    ReplayDecisionMismatch,
47    /// The platform could not provide a usable Unix time.
48    #[error("system clock unavailable")]
49    ClockUnavailable,
50    /// The platform could not provide cryptographically secure entropy.
51    #[error("secure entropy unavailable")]
52    EntropyUnavailable,
53    /// A cryptographic primitive rejected otherwise bounded input.
54    #[error("cryptographic operation failed")]
55    CryptoFailure,
56}
57
58impl Error {
59    /// Return the stable language-neutral error code.
60    #[must_use]
61    pub const fn code(self) -> &'static str {
62        match self {
63            Self::InvalidConfiguration => "invalid_configuration",
64            Self::LimitExceeded => "limit_exceeded",
65            Self::MalformedEnvelope => "malformed_envelope",
66            Self::UnsupportedVersion => "unsupported_version",
67            Self::UnsupportedSuite => "unsupported_suite",
68            Self::UnsupportedMethod => "unsupported_method",
69            Self::UnknownRecipientKey => "unknown_recipient_key",
70            Self::InvalidCredential => "invalid_credential",
71            Self::AuthenticationFailed => "authentication_failed",
72            Self::ReplayRejected => "replay_rejected",
73            Self::InvalidRequestTime => "invalid_request_time",
74            Self::ReplayDecisionMismatch => "replay_decision_mismatch",
75            Self::ClockUnavailable => "clock_unavailable",
76            Self::EntropyUnavailable => "entropy_unavailable",
77            Self::CryptoFailure => "crypto_failure",
78        }
79    }
80}