Skip to main content

tentacle_secio/
error.rs

1/// I borrowed the error type of `rust-libp2p`, deleted some error types, and added an error type.
2use std::{error, fmt, io};
3
4/// Error at the SECIO layer communication.
5#[derive(Debug)]
6pub enum SecioError {
7    /// I/O error.
8    IoError(io::Error),
9
10    /// Openssl stack error
11    #[cfg(unix)]
12    Openssl(openssl::error::ErrorStack),
13
14    /// Crypto error
15    CryptoError,
16
17    /// Sign operation not supported
18    NotSupportKeyProvider,
19
20    /// Failed to generate ephemeral key.
21    EphemeralKeyGenerationFailed,
22
23    /// Failed to generate the secret shared key from the ephemeral key.
24    SecretGenerationFailed,
25
26    /// There is no protocol supported by both the local and remote hosts.
27    NoSupportIntersection,
28
29    /// The final check of the handshake failed.
30    NonceVerificationFailed,
31
32    /// The received frame was of invalid length.
33    FrameTooShort,
34
35    /// Connect yourself
36    ConnectSelf,
37
38    /// Failed to parse one of the handshake bincode messages.
39    HandshakeParsingFailure,
40
41    /// The signature of the exchange packet doesn't verify the remote public key.
42    SignatureVerificationFailed,
43
44    /// Invalid message message found during handshake
45    InvalidMessage,
46
47    /// We received an invalid proposition from remote.
48    InvalidProposition(&'static str),
49}
50
51impl PartialEq for SecioError {
52    fn eq(&self, other: &SecioError) -> bool {
53        use self::SecioError::*;
54        match (self, other) {
55            (InvalidProposition(i), InvalidProposition(j)) => i == j,
56            (EphemeralKeyGenerationFailed, EphemeralKeyGenerationFailed)
57            | (SecretGenerationFailed, SecretGenerationFailed)
58            | (NoSupportIntersection, NoSupportIntersection)
59            | (NonceVerificationFailed, NonceVerificationFailed)
60            | (FrameTooShort, FrameTooShort)
61            | (ConnectSelf, ConnectSelf)
62            | (HandshakeParsingFailure, HandshakeParsingFailure)
63            | (SignatureVerificationFailed, SignatureVerificationFailed)
64            | (InvalidMessage, InvalidMessage)
65            | (NotSupportKeyProvider, NotSupportKeyProvider) => true,
66            _ => false,
67        }
68    }
69}
70
71impl From<io::Error> for SecioError {
72    #[inline]
73    fn from(err: io::Error) -> SecioError {
74        SecioError::IoError(err)
75    }
76}
77
78impl From<SecioError> for io::Error {
79    #[inline]
80    fn from(err: SecioError) -> io::Error {
81        match err {
82            SecioError::IoError(e) => e,
83            e => io::Error::new(io::ErrorKind::BrokenPipe, e.to_string()),
84        }
85    }
86}
87
88#[cfg(unix)]
89impl From<openssl::error::ErrorStack> for SecioError {
90    fn from(err: openssl::error::ErrorStack) -> SecioError {
91        SecioError::Openssl(err)
92    }
93}
94
95#[cfg(not(target_family = "wasm"))]
96impl From<ring::error::Unspecified> for SecioError {
97    fn from(_err: ring::error::Unspecified) -> SecioError {
98        SecioError::CryptoError
99    }
100}
101
102impl error::Error for SecioError {}
103
104impl fmt::Display for SecioError {
105    fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
106        match self {
107            SecioError::IoError(e) => fmt::Display::fmt(&e, f),
108            #[cfg(unix)]
109            SecioError::Openssl(e) => fmt::Display::fmt(&e, f),
110            SecioError::CryptoError => write!(f, "Crypto Error"),
111            SecioError::EphemeralKeyGenerationFailed => write!(f, "EphemeralKey Generation Failed"),
112            SecioError::SecretGenerationFailed => write!(f, "Secret Generation Failed"),
113            SecioError::NoSupportIntersection => write!(f, "No Support Intersection"),
114            SecioError::NonceVerificationFailed => write!(f, "Nonce Verification Failed"),
115            SecioError::FrameTooShort => write!(f, "Frame Too Short"),
116            SecioError::ConnectSelf => write!(f, "Connect Self"),
117            SecioError::HandshakeParsingFailure => write!(f, "Handshake Parsing Failure"),
118            SecioError::InvalidMessage => write!(f, "Invalid Message"),
119            SecioError::SignatureVerificationFailed => write!(f, "Signature Verification Failed"),
120            SecioError::InvalidProposition(e) => write!(f, "Invalid Proposition: {}", e),
121            SecioError::NotSupportKeyProvider => write!(f, "Sign operation not supported"),
122        }
123    }
124}