1use std::{error, fmt, io};
3
4#[derive(Debug)]
6pub enum SecioError {
7 IoError(io::Error),
9
10 #[cfg(unix)]
12 Openssl(openssl::error::ErrorStack),
13
14 CryptoError,
16
17 NotSupportKeyProvider,
19
20 EphemeralKeyGenerationFailed,
22
23 SecretGenerationFailed,
25
26 NoSupportIntersection,
28
29 NonceVerificationFailed,
31
32 FrameTooShort,
34
35 ConnectSelf,
37
38 HandshakeParsingFailure,
40
41 SignatureVerificationFailed,
43
44 InvalidMessage,
46
47 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}