pkix_path/
verification_error.rs1#[cfg(not(feature = "std"))]
4use alloc::boxed::Box;
5#[cfg(feature = "std")]
6use std::boxed::Box;
7
8#[derive(Clone, Copy, Debug, PartialEq, Eq, Hash)]
16#[cfg_attr(feature = "serde", derive(serde::Serialize, serde::Deserialize))]
17#[non_exhaustive]
18pub enum VerificationErrorKind {
19 UnsupportedAlgorithm,
23 UnsupportedKey,
27 MalformedKey,
30 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#[derive(Debug)]
53pub struct VerificationError {
54 kind: VerificationErrorKind,
55 source: Option<Box<dyn core::error::Error + Send + Sync + 'static>>,
56}
57
58impl VerificationError {
59 #[must_use]
61 pub const fn new(kind: VerificationErrorKind) -> Self {
62 Self { kind, source: None }
63 }
64
65 #[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 #[must_use]
80 pub const fn unsupported_algorithm() -> Self {
81 Self::new(VerificationErrorKind::UnsupportedAlgorithm)
82 }
83
84 #[must_use]
86 pub const fn unsupported_key() -> Self {
87 Self::new(VerificationErrorKind::UnsupportedKey)
88 }
89
90 #[must_use]
92 pub const fn malformed_key() -> Self {
93 Self::new(VerificationErrorKind::MalformedKey)
94 }
95
96 #[must_use]
98 pub const fn invalid_signature() -> Self {
99 Self::new(VerificationErrorKind::InvalidSignature)
100 }
101
102 #[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
121impl 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}