ytls_record/record/
alert.rs

1//! Handhsake Record
2
3use ytls_traits::ClientHelloProcessor;
4
5use crate::error::RecordError;
6
7use zerocopy::byteorder::network_endian::U16 as N16;
8use zerocopy::{Immutable, IntoBytes, KnownLayout, TryFromBytes, Unaligned};
9
10#[derive(TryFromBytes, IntoBytes, KnownLayout, Immutable, Unaligned)]
11#[repr(u8)]
12#[derive(Copy, Clone, Debug, PartialEq)]
13pub enum AlertDescription {
14    CloseNotify = 0,
15    UnexpectedMessage = 10,
16    BadRecordMac = 20,
17    RecordOverflow = 22,
18    HandshakeFailure = 40,
19    BadCertificate = 42,
20    UnsupportedCertificate = 43,
21    CertificateRevoked = 44,
22    CertificateExpired = 45,
23    CertificateUnknown = 46,
24    IllegalParameter = 47,
25    UnknownCa = 48,
26    AccessDenied = 49,
27    DecodeError = 50,
28    DecryptError = 51,
29    ProtocolVersion = 70,
30    InsufficientSecurity = 71,
31    InternalError = 80,
32    InappropriateFallback = 86,
33    UserCanceled = 90,
34    MissingExtension = 109,
35    UnsupportedExtension = 110,
36    UnrecognizedName = 112,
37    BadCertificateStatusResponse = 113,
38    UnknownPskIdentity = 115,
39    CertificateRequired = 116,
40    NoApplicationProtocol = 120,
41}
42
43#[derive(TryFromBytes, IntoBytes, KnownLayout, Immutable, Unaligned)]
44#[repr(u8)]
45#[derive(Copy, Clone, Debug, PartialEq)]
46pub enum AlertLevel {
47    Warning = 1,
48    Fatal = 2,
49}
50
51#[derive(TryFromBytes, IntoBytes, KnownLayout, Immutable, Unaligned)]
52#[repr(C)]
53#[derive(Debug, PartialEq)]
54pub struct Alert {
55    level: AlertLevel,
56    description: AlertDescription,
57}
58
59#[derive(Debug, PartialEq)]
60pub struct AlertMsg<'r> {
61    alert: &'r Alert,
62}
63
64impl<'r> AlertMsg<'r> {
65    /// Alert Level
66    pub fn level(&self) -> AlertLevel {
67        self.alert.level
68    }
69    /// Alert Description
70    pub fn description(&self) -> AlertDescription {
71        self.alert.description
72    }
73    /// Parse Client Record
74    pub fn client_parse(bytes: &'r [u8]) -> Result<(Self, &'r [u8]), RecordError> {
75        let (msg, rest) =
76            Alert::try_ref_from_prefix(bytes).map_err(|e| RecordError::from_zero_copy(e))?;
77
78        Ok((Self { alert: msg }, rest))
79    }
80}