Skip to main content

ssb_handshake/
error.rs

1use genio::error::ReadExactError;
2
3#[derive(Debug)]
4pub enum HandshakeError<IoErr> {
5    Io(IoErr),
6    UnexpectedEnd,
7    ClientHelloDeserializeFailed,
8    ClientHelloVerifyFailed,
9    ServerHelloDeserializeFailed,
10    ServerHelloVerifyFailed,
11    ClientAuthDeserializeFailed,
12    ClientAuthVerifyFailed,
13    ServerAcceptDeserializeFailed,
14    ServerAcceptVerifyFailed,
15    SharedAInvalid,
16    SharedBInvalid,
17    SharedCInvalid,
18}
19
20impl<IoErr> From<IoErr> for HandshakeError<IoErr> {
21    fn from(err: IoErr) -> Self {
22        HandshakeError::Io(err)
23    }
24}
25
26impl<IoErr> From<ReadExactError<IoErr>> for HandshakeError<IoErr> {
27    fn from(err: ReadExactError<IoErr>) -> Self {
28        match err {
29            ReadExactError::UnexpectedEnd => HandshakeError::UnexpectedEnd,
30            ReadExactError::Other(e) => HandshakeError::Io(e),
31        }
32    }
33}
34
35#[cfg(feature = "std")]
36impl<IoErr> std::fmt::Display for HandshakeError<IoErr>
37where
38    IoErr: std::fmt::Display,
39{
40    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
41        use HandshakeError::*;
42        match self {
43            Io(e) => write!(f, "IO error: {}", e),
44            UnexpectedEnd => write!(f, "Unexpected end when reading from stream"),
45            ClientHelloDeserializeFailed => write!(f, "Failed to read client hello message"),
46            ClientHelloVerifyFailed => write!(f, "Failed to verify client hello message"),
47            ServerHelloDeserializeFailed => write!(f, "Failed to read server hello message"),
48            ServerHelloVerifyFailed => write!(f, "Failed to verify server hello message"),
49            ClientAuthDeserializeFailed => write!(f, "Failed to read client auth message"),
50            ClientAuthVerifyFailed => write!(f, "Failed to verify client auth message"),
51            ServerAcceptDeserializeFailed => write!(f, "Failed to read server accept message"),
52            ServerAcceptVerifyFailed => write!(f, "Failed to verify server accept message"),
53            SharedAInvalid => write!(f, "Shared secret A is invalid"),
54            SharedBInvalid => write!(f, "Shared secret B is invalid"),
55            SharedCInvalid => write!(f, "Shared secret C is invalid"),
56        }
57    }
58}
59
60#[cfg(feature = "std")]
61impl<IoErr> std::error::Error for HandshakeError<IoErr>
62where
63    IoErr: std::error::Error + 'static,
64{
65    fn source(&self) -> Option<&(dyn std::error::Error + 'static)> {
66        match self {
67            HandshakeError::Io(e) => Some(e),
68            _ => None,
69        }
70    }
71}