cross_authenticode/
error.rs

1use thiserror::Error;
2
3#[derive(Error, Debug, PartialEq)]
4pub enum AuthenticodeError {
5    #[error("failed to read slice from PE file: {0}")]
6    ReadSlice(String),
7
8    #[error("failed to parse PE file: {0}")]
9    ParsePe(String),
10
11    #[error("contains no win_certificate")]
12    NoWinCertificate,
13
14    #[error("no valid DER certificate: {0}")]
15    DerError(String),
16
17    #[error("no certificates found")]
18    NoCertificates,
19
20    #[error("invalid signature: {0}")]
21    InvalidSignature(String),
22
23    #[error("invalid ASN.1 encoding: {0}")]
24    Asn1Error(String),
25
26    #[error("no encapsulated content found")]
27    NoEncapsulatedContent,
28
29    #[error("invalid encapsulated content type: {0}")]
30    InvalidEncapsulatedContentType(String),
31
32    #[error("invalid encapsulated content")]
33    InvalidEncapsulatedContent,
34
35    #[error("invalid content type: {0}")]
36    InvalidContentType(String),
37
38    #[error("invalid hash algorithm")]
39    InvalidHashAlgorithm,
40
41    #[error("invalid hash algorithm: {0}")]
42    InvalidHashAlgorithmWithName(String),
43}
44
45impl From<cms::cert::x509::der::Error> for AuthenticodeError {
46    fn from(error: cms::cert::x509::der::Error) -> Self {
47        Self::DerError(error.to_string())
48    }
49}
50
51impl From<std::array::TryFromSliceError> for AuthenticodeError {
52    fn from(error: std::array::TryFromSliceError) -> Self {
53        Self::ReadSlice(error.to_string())
54    }
55}
56
57impl From<object::Error> for AuthenticodeError {
58    fn from(error: object::Error) -> Self {
59        Self::ParsePe(error.to_string())
60    }
61}
62
63pub trait OptionExt<T> {
64    fn err_slice(self) -> Result<T, AuthenticodeError>;
65    fn err_pe_oor(self) -> Result<T, AuthenticodeError>;
66}
67
68impl<T> OptionExt<T> for Option<T> {
69    fn err_slice(self) -> Result<T, AuthenticodeError> {
70        self.ok_or(AuthenticodeError::ReadSlice(
71            "Failed to read slice".to_string(),
72        ))
73    }
74    fn err_pe_oor(self) -> Result<T, AuthenticodeError> {
75        self.ok_or(AuthenticodeError::ParsePe(
76            "Result out of range".to_string(),
77        ))
78    }
79}