Skip to main content

pkix_path/
verification_error.rs

1//! The error type of [`SignatureVerifier`](crate::SignatureVerifier).
2
3#[cfg(not(feature = "std"))]
4use alloc::boxed::Box;
5#[cfg(feature = "std")]
6use std::boxed::Box;
7
8/// Why a [`SignatureVerifier`](crate::SignatureVerifier) did not accept a
9/// signature.
10///
11/// `validate_path` reports [`InvalidSignature`](Self::InvalidSignature) as
12/// [`Error::SignatureInvalid`](crate::Error::SignatureInvalid) and every
13/// other kind as
14/// [`Error::SignatureNotVerified`](crate::Error::SignatureNotVerified).
15#[derive(Clone, Copy, Debug, PartialEq, Eq, Hash)]
16#[cfg_attr(feature = "serde", derive(serde::Serialize, serde::Deserialize))]
17#[non_exhaustive]
18pub enum VerificationErrorKind {
19    /// The verifier does not implement the signature algorithm, or the
20    /// algorithm's parameters are not ones it supports (for example
21    /// RSASSA-PSS with SHA-1, or ECDSA parameters present).
22    UnsupportedAlgorithm,
23    /// The issuer's key is well-formed but not usable with this algorithm
24    /// or verifier: a different key type, an unsupported curve or size, or
25    /// key parameters that forbid the signature's parameters.
26    UnsupportedKey,
27    /// The issuer's `SubjectPublicKeyInfo` does not decode as a key of its
28    /// declared type.
29    MalformedKey,
30    /// The signature is not a well-formed signature value or does not
31    /// verify under the issuer's key.
32    InvalidSignature,
33}
34
35impl core::fmt::Display for VerificationErrorKind {
36    fn fmt(&self, f: &mut core::fmt::Formatter<'_>) -> core::fmt::Result {
37        f.write_str(match self {
38            Self::UnsupportedAlgorithm => "unsupported signature algorithm",
39            Self::UnsupportedKey => "issuer key not usable with this signature algorithm",
40            Self::MalformedKey => "malformed issuer public key",
41            Self::InvalidSignature => "invalid signature",
42        })
43    }
44}
45
46/// Error returned by [`SignatureVerifier::verify_signature`].
47///
48/// Carries a [`VerificationErrorKind`] and, optionally, the backend error
49/// that caused it, available through [`core::error::Error::source`].
50///
51/// [`SignatureVerifier::verify_signature`]: crate::SignatureVerifier::verify_signature
52#[derive(Debug)]
53pub struct VerificationError {
54    kind: VerificationErrorKind,
55    source: Option<Box<dyn core::error::Error + Send + Sync + 'static>>,
56}
57
58impl VerificationError {
59    /// An error of the given kind with no underlying cause.
60    #[must_use]
61    pub const fn new(kind: VerificationErrorKind) -> Self {
62        Self { kind, source: None }
63    }
64
65    /// An error of the given kind caused by `source`, typically the
66    /// backend's own error.
67    #[must_use]
68    pub fn with_source(
69        kind: VerificationErrorKind,
70        source: impl core::error::Error + Send + Sync + 'static,
71    ) -> Self {
72        Self {
73            kind,
74            source: Some(Box::new(source)),
75        }
76    }
77
78    /// Shorthand for [`VerificationErrorKind::UnsupportedAlgorithm`].
79    #[must_use]
80    pub const fn unsupported_algorithm() -> Self {
81        Self::new(VerificationErrorKind::UnsupportedAlgorithm)
82    }
83
84    /// Shorthand for [`VerificationErrorKind::UnsupportedKey`].
85    #[must_use]
86    pub const fn unsupported_key() -> Self {
87        Self::new(VerificationErrorKind::UnsupportedKey)
88    }
89
90    /// Shorthand for [`VerificationErrorKind::MalformedKey`].
91    #[must_use]
92    pub const fn malformed_key() -> Self {
93        Self::new(VerificationErrorKind::MalformedKey)
94    }
95
96    /// Shorthand for [`VerificationErrorKind::InvalidSignature`].
97    #[must_use]
98    pub const fn invalid_signature() -> Self {
99        Self::new(VerificationErrorKind::InvalidSignature)
100    }
101
102    /// What went wrong.
103    #[must_use]
104    pub const fn kind(&self) -> VerificationErrorKind {
105        self.kind
106    }
107}
108
109impl From<VerificationErrorKind> for VerificationError {
110    fn from(kind: VerificationErrorKind) -> Self {
111        Self::new(kind)
112    }
113}
114
115impl core::fmt::Display for VerificationError {
116    fn fmt(&self, f: &mut core::fmt::Formatter<'_>) -> core::fmt::Result {
117        core::fmt::Display::fmt(&self.kind, f)
118    }
119}
120
121// `core::error::Error` is stable since Rust 1.81, below the workspace MSRV,
122// so the impl does not need the `std` feature.
123impl core::error::Error for VerificationError {
124    fn source(&self) -> Option<&(dyn core::error::Error + 'static)> {
125        self.source
126            .as_deref()
127            .map(|e| e as &(dyn core::error::Error + 'static))
128    }
129}