pesign/
errors.rs

1use std::fmt;
2
3#[derive(Clone, Copy, Debug, Eq, PartialEq)]
4pub enum PeSignErrorKind {
5    /// IO Error.
6    IoError,
7
8    /// Invalid PE File.
9    InvalidPeFile,
10
11    /// Invalid Certificate ContentInfo.
12    InvalidContentInfo,
13
14    /// Invalid ContentType.
15    InvalidContentType,
16
17    /// Invalid SignedData.
18    InvalidSignedData,
19
20    /// Invalid Encapsulated ContentType.
21    InvalidEncapsulatedContentType,
22
23    /// Empty EncapsulatedContent.
24    EmptyEncapsulatedContent,
25
26    /// Invalid SpcIndirectDataContent.
27    InvalidSpcIndirectDataContent,
28
29    /// Empty Certificate.
30    EmptyCertificate,
31
32    /// Unsupported Certificate Format.
33    UnsupportedCertificateFormat,
34
35    /// Unsupported Algorithm.
36    UnsupportedAlgorithm,
37
38    /// Invalid Certificate Extension.
39    InvalidCertificateExtension,
40
41    /// Wrong Certificate Chain Build Param.
42    WrongCertChainBuildParam,
43
44    /// Invalid PEM Certificate.
45    InvalidPEMCertificate,
46
47    /// Invalid Public Key.
48    InvalidPublicKey,
49
50    /// No Found SignerInfo.
51    NoFoundSignerInfo,
52
53    /// Unknown Signer.
54    UnknownSigner,
55
56    /// No Found Message Digest.
57    NoFoundMessageDigest,
58
59    /// Invalid Counter Signature.
60    InvalidCounterSignature,
61
62    /// Invalid SigningTime.
63    InvalidSigningTime,
64
65    /// No Found SigningTime.
66    NoFoundSigningTime,
67
68    /// Invalid TSTInfo.
69    InvalidTSTInfo,
70
71    /// Export as DER Error.
72    ExportDerError,
73
74    /// Export as PEM Error.
75    ExportPemError,
76
77    /// Unknown Error.
78    Unknown,
79}
80
81#[derive(Debug)]
82pub struct PeSignError {
83    pub kind: PeSignErrorKind,
84    pub message: String,
85}
86
87impl fmt::Display for PeSignError {
88    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
89        write!(f, "{:?}", self)
90    }
91}
92
93impl std::error::Error for PeSignError {}
94
95pub trait PeSignResult<T> {
96    fn map_app_err(self: Self, kind: PeSignErrorKind) -> Result<T, PeSignError>;
97    fn map_unknown_err(self: Self) -> Result<T, PeSignError>;
98}
99
100impl<T, E> PeSignResult<T> for std::result::Result<T, E>
101where
102    E: std::error::Error + 'static,
103{
104    fn map_app_err(self: Self, kind: PeSignErrorKind) -> Result<T, PeSignError> {
105        self.map_err(|err| PeSignError {
106            kind: kind,
107            message: err.to_string(),
108        })
109    }
110
111    fn map_unknown_err(self: Self) -> Result<T, PeSignError> {
112        self.map_app_err(PeSignErrorKind::Unknown)
113    }
114}