1use crate::msgs::enums::{AlertDescription, ContentType, HandshakeType};
2use sct;
3use std::error::Error;
4use std::fmt;
5use webpki;
6
7#[derive(Debug, PartialEq, Clone)]
9pub enum TLSError {
10    InappropriateMessage {
16        expect_types: Vec<ContentType>,
18        got_type: ContentType,
20    },
21
22    InappropriateHandshakeMessage {
26        expect_types: Vec<HandshakeType>,
28        got_type: HandshakeType,
30    },
31
32    CorruptMessage,
34
35    CorruptMessagePayload(ContentType),
37
38    NoCertificatesPresented,
40
41    DecryptError,
43
44    PeerIncompatibleError(String),
47
48    PeerMisbehavedError(String),
51
52    AlertReceived(AlertDescription),
54
55    WebPKIError(webpki::Error),
57
58    InvalidSCT(sct::Error),
60
61    General(String),
63
64    FailedToGetCurrentTime,
66
67    HandshakeNotComplete,
70
71    PeerSentOversizedRecord,
73
74    NoApplicationProtocol,
76}
77
78fn join<T: fmt::Debug>(items: &[T]) -> String {
79    items
80        .iter()
81        .map(|x| format!("{:?}", x))
82        .collect::<Vec<String>>()
83        .join(" or ")
84}
85
86impl fmt::Display for TLSError {
87    fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
88        match *self {
89            TLSError::InappropriateMessage {
90                ref expect_types,
91                ref got_type,
92            } => write!(
93                f,
94                "received unexpected message: got {:?} when expecting {}",
95                got_type,
96                join::<ContentType>(expect_types)
97            ),
98            TLSError::InappropriateHandshakeMessage {
99                ref expect_types,
100                ref got_type,
101            } => write!(
102                f,
103                "received unexpected handshake message: got {:?} when expecting {}",
104                got_type,
105                join::<HandshakeType>(expect_types)
106            ),
107            TLSError::CorruptMessagePayload(ref typ) => {
108                write!(f, "received corrupt message of type {:?}", typ)
109            }
110            TLSError::PeerIncompatibleError(ref why) => write!(f, "peer is incompatible: {}", why),
111            TLSError::PeerMisbehavedError(ref why) => write!(f, "peer misbehaved: {}", why),
112            TLSError::AlertReceived(ref alert) => write!(f, "received fatal alert: {:?}", alert),
113            TLSError::WebPKIError(ref err) => write!(f, "invalid certificate: {:?}", err),
114            TLSError::CorruptMessage => write!(f, "received corrupt message"),
115            TLSError::NoCertificatesPresented => write!(f, "peer sent no certificates"),
116            TLSError::DecryptError => write!(f, "cannot decrypt peer's message"),
117            TLSError::PeerSentOversizedRecord => write!(f, "peer sent excess record size"),
118            TLSError::HandshakeNotComplete => write!(f, "handshake not complete"),
119            TLSError::NoApplicationProtocol => write!(f, "peer doesn't support any known protocol"),
120            TLSError::InvalidSCT(ref err) => write!(f, "invalid certificate timestamp: {:?}", err),
121            TLSError::FailedToGetCurrentTime => write!(f, "failed to get current time"),
122            TLSError::General(ref err) => write!(f, "unexpected error: {}", err), }
124    }
125}
126
127impl Error for TLSError {}
128
129#[cfg(test)]
130mod tests {
131    #[test]
132    fn smoke() {
133        use super::TLSError;
134        use crate::msgs::enums::{AlertDescription, ContentType, HandshakeType};
135        use sct;
136        use webpki;
137
138        let all = vec![
139            TLSError::InappropriateMessage {
140                expect_types: vec![ContentType::Alert],
141                got_type: ContentType::Handshake,
142            },
143            TLSError::InappropriateHandshakeMessage {
144                expect_types: vec![HandshakeType::ClientHello, HandshakeType::Finished],
145                got_type: HandshakeType::ServerHello,
146            },
147            TLSError::CorruptMessage,
148            TLSError::CorruptMessagePayload(ContentType::Alert),
149            TLSError::NoCertificatesPresented,
150            TLSError::DecryptError,
151            TLSError::PeerIncompatibleError("no tls1.2".to_string()),
152            TLSError::PeerMisbehavedError("inconsistent something".to_string()),
153            TLSError::AlertReceived(AlertDescription::ExportRestriction),
154            TLSError::WebPKIError(webpki::Error::ExtensionValueInvalid),
155            TLSError::InvalidSCT(sct::Error::MalformedSCT),
156            TLSError::General("undocumented error".to_string()),
157            TLSError::FailedToGetCurrentTime,
158            TLSError::HandshakeNotComplete,
159            TLSError::PeerSentOversizedRecord,
160            TLSError::NoApplicationProtocol,
161        ];
162
163        for err in all {
164            println!("{:?}:", err);
165            println!("  fmt '{}'", err);
166        }
167    }
168}