tls_parser/
tls_alert.rs

1use nom_derive::*;
2use rusticata_macros::newtype_enum;
3
4/// TLS alert severity
5#[derive(Clone, Copy, Debug, PartialEq, Eq, Nom)]
6pub struct TlsAlertSeverity(pub u8);
7
8newtype_enum! {
9impl display TlsAlertSeverity {
10    Warning = 0x01,
11    Fatal   = 0x02
12}
13}
14
15/// TLS alert description
16///
17/// Alerts are defined in the [IANA TLS Alert
18/// Registry](https://www.iana.org/assignments/tls-parameters/tls-parameters.xhtml#tls-parameters-6)
19#[derive(Clone, Copy, Debug, PartialEq, Eq, Nom)]
20pub struct TlsAlertDescription(pub u8);
21
22newtype_enum! {
23impl display TlsAlertDescription {
24    CloseNotify            = 0x00,
25    UnexpectedMessage      = 0x0A,
26    BadRecordMac           = 0x14,
27    DecryptionFailed       = 0x15,
28    RecordOverflow         = 0x16,
29    DecompressionFailure   = 0x1E,
30    HandshakeFailure       = 0x28,
31    NoCertificate          = 0x29,
32    BadCertificate         = 0x2A,
33    UnsupportedCertificate = 0x2B,
34    CertificateRevoked     = 0x2C,
35    CertificateExpired     = 0x2D,
36    CertificateUnknown     = 0x2E,
37    IllegalParameter       = 0x2F,
38    UnknownCa              = 0x30,
39    AccessDenied           = 0x31,
40    DecodeError            = 0x32,
41    DecryptError           = 0x33,
42    ExportRestriction      = 0x3C,
43    ProtocolVersion        = 0x46,
44    InsufficientSecurity   = 0x47,
45    InternalError          = 0x50,
46    InappropriateFallback  = 0x56,
47    UserCancelled          = 0x5A,
48    NoRenegotiation        = 0x64,
49    MissingExtension       = 0x6d,
50    UnsupportedExtension   = 0x6e,
51    CertUnobtainable       = 0x6f,
52    UnrecognizedName       = 0x70,
53    BadCertStatusResponse  = 0x71,
54    BadCertHashValue       = 0x72,
55    UnknownPskIdentity     = 0x73,
56    CertificateRequired    = 0x74,
57    NoApplicationProtocol  = 0x78 // [RFC7301]
58}
59}
60
61/// TLS alert message
62#[derive(Clone, PartialEq, Nom)]
63pub struct TlsMessageAlert {
64    /// Should match a [TlsAlertSeverity](enum.TlsAlertSeverity.html) value
65    pub severity: TlsAlertSeverity,
66    /// Should match a [TlsAlertDescription](enum.TlsAlertDescription.html) value
67    pub code: TlsAlertDescription,
68}
69
70#[cfg(test)]
71mod tests {
72    use crate::tls_alert::*;
73
74    #[test]
75    fn test_tlsalert_cast_severity() {
76        let a = TlsAlertSeverity::Warning;
77
78        let a_u8 = a.0;
79        assert_eq!(a_u8, 0x01);
80
81        let b = TlsAlertSeverity(a_u8);
82        assert_eq!(b, TlsAlertSeverity::Warning);
83
84        let s = format!("{}", b);
85        assert_eq!(s, "Warning");
86
87        let s = format!("{}", TlsAlertSeverity(129));
88        assert_eq!(s, "TlsAlertSeverity(129 / 0x81)");
89    }
90
91    #[test]
92    fn test_tlsalert_cast_description() {
93        let a = TlsAlertDescription::HandshakeFailure;
94
95        let a_u8 = a.0;
96        assert_eq!(a_u8, 0x28);
97
98        let b = TlsAlertDescription(a_u8);
99        assert_eq!(b, TlsAlertDescription::HandshakeFailure);
100    }
101} // mod tests