1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
#[cfg(test)]
mod alert_test;
use super::content::*;
use crate::error::Result;
use byteorder::{ReadBytesExt, WriteBytesExt};
use std::fmt;
use std::io::{Read, Write};
#[derive(Copy, Clone, PartialEq, Debug)]
pub(crate) enum AlertLevel {
Warning = 1,
Fatal = 2,
Invalid,
}
impl fmt::Display for AlertLevel {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
match *self {
AlertLevel::Warning => write!(f, "LevelWarning"),
AlertLevel::Fatal => write!(f, "LevelFatal"),
_ => write!(f, "Invalid alert level"),
}
}
}
impl From<u8> for AlertLevel {
fn from(val: u8) -> Self {
match val {
1 => AlertLevel::Warning,
2 => AlertLevel::Fatal,
_ => AlertLevel::Invalid,
}
}
}
#[derive(Copy, Clone, PartialEq, Debug)]
pub(crate) enum AlertDescription {
CloseNotify = 0,
UnexpectedMessage = 10,
BadRecordMac = 20,
DecryptionFailed = 21,
RecordOverflow = 22,
DecompressionFailure = 30,
HandshakeFailure = 40,
NoCertificate = 41,
BadCertificate = 42,
UnsupportedCertificate = 43,
CertificateRevoked = 44,
CertificateExpired = 45,
CertificateUnknown = 46,
IllegalParameter = 47,
UnknownCa = 48,
AccessDenied = 49,
DecodeError = 50,
DecryptError = 51,
ExportRestriction = 60,
ProtocolVersion = 70,
InsufficientSecurity = 71,
InternalError = 80,
UserCanceled = 90,
NoRenegotiation = 100,
UnsupportedExtension = 110,
Invalid,
}
impl fmt::Display for AlertDescription {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
match *self {
AlertDescription::CloseNotify => write!(f, "CloseNotify"),
AlertDescription::UnexpectedMessage => write!(f, "UnexpectedMessage"),
AlertDescription::BadRecordMac => write!(f, "BadRecordMac"),
AlertDescription::DecryptionFailed => write!(f, "DecryptionFailed"),
AlertDescription::RecordOverflow => write!(f, "RecordOverflow"),
AlertDescription::DecompressionFailure => write!(f, "DecompressionFailure"),
AlertDescription::HandshakeFailure => write!(f, "HandshakeFailure"),
AlertDescription::NoCertificate => write!(f, "NoCertificate"),
AlertDescription::BadCertificate => write!(f, "BadCertificate"),
AlertDescription::UnsupportedCertificate => write!(f, "UnsupportedCertificate"),
AlertDescription::CertificateRevoked => write!(f, "CertificateRevoked"),
AlertDescription::CertificateExpired => write!(f, "CertificateExpired"),
AlertDescription::CertificateUnknown => write!(f, "CertificateUnknown"),
AlertDescription::IllegalParameter => write!(f, "IllegalParameter"),
AlertDescription::UnknownCa => write!(f, "UnknownCA"),
AlertDescription::AccessDenied => write!(f, "AccessDenied"),
AlertDescription::DecodeError => write!(f, "DecodeError"),
AlertDescription::DecryptError => write!(f, "DecryptError"),
AlertDescription::ExportRestriction => write!(f, "ExportRestriction"),
AlertDescription::ProtocolVersion => write!(f, "ProtocolVersion"),
AlertDescription::InsufficientSecurity => write!(f, "InsufficientSecurity"),
AlertDescription::InternalError => write!(f, "InternalError"),
AlertDescription::UserCanceled => write!(f, "UserCanceled"),
AlertDescription::NoRenegotiation => write!(f, "NoRenegotiation"),
AlertDescription::UnsupportedExtension => write!(f, "UnsupportedExtension"),
_ => write!(f, "Invalid alert description"),
}
}
}
impl From<u8> for AlertDescription {
fn from(val: u8) -> Self {
match val {
0 => AlertDescription::CloseNotify,
10 => AlertDescription::UnexpectedMessage,
20 => AlertDescription::BadRecordMac,
21 => AlertDescription::DecryptionFailed,
22 => AlertDescription::RecordOverflow,
30 => AlertDescription::DecompressionFailure,
40 => AlertDescription::HandshakeFailure,
41 => AlertDescription::NoCertificate,
42 => AlertDescription::BadCertificate,
43 => AlertDescription::UnsupportedCertificate,
44 => AlertDescription::CertificateRevoked,
45 => AlertDescription::CertificateExpired,
46 => AlertDescription::CertificateUnknown,
47 => AlertDescription::IllegalParameter,
48 => AlertDescription::UnknownCa,
49 => AlertDescription::AccessDenied,
50 => AlertDescription::DecodeError,
51 => AlertDescription::DecryptError,
60 => AlertDescription::ExportRestriction,
70 => AlertDescription::ProtocolVersion,
71 => AlertDescription::InsufficientSecurity,
80 => AlertDescription::InternalError,
90 => AlertDescription::UserCanceled,
100 => AlertDescription::NoRenegotiation,
110 => AlertDescription::UnsupportedExtension,
_ => AlertDescription::Invalid,
}
}
}
#[derive(Copy, Clone, PartialEq, Debug)]
pub struct Alert {
pub(crate) alert_level: AlertLevel,
pub(crate) alert_description: AlertDescription,
}
impl fmt::Display for Alert {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
write!(f, "Alert {}: {}", self.alert_level, self.alert_description)
}
}
impl Alert {
pub fn content_type(&self) -> ContentType {
ContentType::Alert
}
pub fn size(&self) -> usize {
2
}
pub fn marshal<W: Write>(&self, writer: &mut W) -> Result<()> {
writer.write_u8(self.alert_level as u8)?;
writer.write_u8(self.alert_description as u8)?;
Ok(writer.flush()?)
}
pub fn unmarshal<R: Read>(reader: &mut R) -> Result<Self> {
let alert_level = reader.read_u8()?.into();
let alert_description = reader.read_u8()?.into();
Ok(Alert {
alert_level,
alert_description,
})
}
}