/rust/registry/src/index.crates.io-1949cf8c6b5b557f/signature-2.2.0/src/error.rs
Line | Count | Source |
1 | | //! Signature error types |
2 | | |
3 | | use core::fmt::{self, Debug, Display}; |
4 | | |
5 | | #[cfg(feature = "std")] |
6 | | use std::boxed::Box; |
7 | | |
8 | | /// Result type. |
9 | | /// |
10 | | /// A result with the `signature` crate's [`Error`] type. |
11 | | pub type Result<T> = core::result::Result<T, Error>; |
12 | | |
13 | | /// Signature errors. |
14 | | /// |
15 | | /// This type is deliberately opaque as to avoid sidechannel leakage which |
16 | | /// could potentially be used recover signing private keys or forge signatures |
17 | | /// (e.g. [BB'06]). |
18 | | /// |
19 | | /// When the `std` feature is enabled, it impls [`std::error::Error`] and |
20 | | /// supports an optional [`std::error::Error::source`], which can be used by |
21 | | /// things like remote signers (e.g. HSM, KMS) to report I/O or auth errors. |
22 | | /// |
23 | | /// [BB'06]: https://en.wikipedia.org/wiki/Daniel_Bleichenbacher |
24 | | #[derive(Default)] |
25 | | #[non_exhaustive] |
26 | | pub struct Error { |
27 | | /// Source of the error (if applicable). |
28 | | #[cfg(feature = "std")] |
29 | | source: Option<Box<dyn std::error::Error + Send + Sync + 'static>>, |
30 | | } |
31 | | |
32 | | impl Error { |
33 | | /// Create a new error with no associated source |
34 | 0 | pub fn new() -> Self { |
35 | 0 | Self::default() |
36 | 0 | } |
37 | | |
38 | | /// Create a new error with an associated source. |
39 | | /// |
40 | | /// **NOTE:** The "source" should **NOT** be used to propagate cryptographic |
41 | | /// errors e.g. signature parsing or verification errors. The intended use |
42 | | /// cases are for propagating errors related to external signers, e.g. |
43 | | /// communication/authentication errors with HSMs, KMS, etc. |
44 | | #[cfg(feature = "std")] |
45 | | pub fn from_source( |
46 | | source: impl Into<Box<dyn std::error::Error + Send + Sync + 'static>>, |
47 | | ) -> Self { |
48 | | Self { |
49 | | source: Some(source.into()), |
50 | | } |
51 | | } |
52 | | } |
53 | | |
54 | | impl Debug for Error { |
55 | | #[cfg(not(feature = "std"))] |
56 | 0 | fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result { |
57 | 0 | f.write_str("signature::Error {}") |
58 | 0 | } |
59 | | |
60 | | #[cfg(feature = "std")] |
61 | | fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result { |
62 | | f.write_str("signature::Error { source: ")?; |
63 | | |
64 | | if let Some(source) = &self.source { |
65 | | write!(f, "Some({})", source)?; |
66 | | } else { |
67 | | f.write_str("None")?; |
68 | | } |
69 | | |
70 | | f.write_str(" }") |
71 | | } |
72 | | } |
73 | | |
74 | | impl Display for Error { |
75 | 0 | fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result { |
76 | 0 | f.write_str("signature error")?; |
77 | | |
78 | | #[cfg(feature = "std")] |
79 | | { |
80 | | if let Some(source) = &self.source { |
81 | | write!(f, ": {}", source)?; |
82 | | } |
83 | | } |
84 | | |
85 | 0 | Ok(()) |
86 | 0 | } |
87 | | } |
88 | | |
89 | | #[cfg(feature = "std")] |
90 | | impl From<Box<dyn std::error::Error + Send + Sync + 'static>> for Error { |
91 | | fn from(source: Box<dyn std::error::Error + Send + Sync + 'static>) -> Error { |
92 | | Self::from_source(source) |
93 | | } |
94 | | } |
95 | | |
96 | | #[cfg(feature = "rand_core")] |
97 | | impl From<rand_core::Error> for Error { |
98 | | #[cfg(not(feature = "std"))] |
99 | 0 | fn from(_source: rand_core::Error) -> Error { |
100 | 0 | Error::new() |
101 | 0 | } |
102 | | |
103 | | #[cfg(feature = "std")] |
104 | | fn from(source: rand_core::Error) -> Error { |
105 | | Error::from_source(source) |
106 | | } |
107 | | } |
108 | | |
109 | | #[cfg(feature = "std")] |
110 | | impl std::error::Error for Error { |
111 | | fn source(&self) -> Option<&(dyn std::error::Error + 'static)> { |
112 | | self.source |
113 | | .as_ref() |
114 | | .map(|source| source.as_ref() as &(dyn std::error::Error + 'static)) |
115 | | } |
116 | | } |