1mod handshake;
2#[doc(inline)]
3pub use handshake::*;
4
5mod alert;
6#[doc(inline)]
7pub use alert::*;
8
9use crate::error::RecordError;
10
11use zerocopy::byteorder::network_endian::U16 as N16;
12use zerocopy::{Immutable, IntoBytes, KnownLayout, TryFromBytes, Unaligned};
13
14use ytls_traits::ClientHelloProcessor;
15use ytls_traits::ServerRecordProcessor;
16
17#[derive(TryFromBytes, IntoBytes, KnownLayout, Immutable, Unaligned)]
19#[repr(u8)]
20#[derive(Copy, Clone, Debug)]
21pub enum ContentType {
22 ChangeCipherSpec = 20,
24 Alert = 21,
26 Handshake = 22,
28 ApplicationData = 23,
30}
31
32#[derive(TryFromBytes, IntoBytes, KnownLayout, Immutable, Unaligned)]
34#[repr(C)]
35#[derive(Debug)]
36pub struct RecordHeader {
37 content_type: ContentType,
38 legacy_version: [u8; 2],
39 record_length: N16,
40}
41
42#[derive(Debug)]
44pub struct Record<'r> {
45 header: &'r RecordHeader,
46 raw_bytes: &'r [u8],
47 content: Content<'r>,
48}
49
50#[derive(Debug)]
52pub enum Content<'r> {
53 ChangeCipherSpec,
54 ApplicationData,
56 Handshake(HandshakeMsg<'r>),
58 Alert(AlertMsg<'r>),
60}
61
62impl<'r> Record<'r> {
63 #[inline]
65 pub fn header_as_bytes(&self) -> &[u8] {
66 self.header.as_bytes()
67 }
68 #[inline]
70 pub fn as_bytes(&self) -> &[u8] {
71 self.raw_bytes
72 }
73 #[inline]
75 pub fn content_type(&self) -> ContentType {
76 self.header.content_type
77 }
78 #[inline]
80 pub fn content(&'r self) -> &'r Content<'r> {
81 &self.content
82 }
83 #[inline]
85 pub fn parse_client_appdata(bytes: &'r [u8]) -> Result<(Record<'r>, &'r [u8]), RecordError> {
86 let (hdr, rest) =
87 RecordHeader::try_ref_from_prefix(bytes).map_err(|e| RecordError::from_zero_copy(e))?;
88
89 if hdr.record_length > 16384 {
90 return Err(RecordError::OverflowLength);
91 }
92 let raw_bytes = &rest[0..usize::from(hdr.record_length)];
93
94 let (content, rest_next) = match hdr.content_type {
95 ContentType::Alert => {
96 let (c, r_next) = AlertMsg::client_parse(rest).unwrap();
97 (Content::Alert(c), r_next)
98 }
99 ContentType::ChangeCipherSpec => {
100 let r_next = &rest[usize::from(hdr.record_length)..];
101 (Content::ChangeCipherSpec, r_next)
102 }
103 ContentType::ApplicationData => {
104 let r_next = &rest[usize::from(hdr.record_length)..];
105 (Content::ApplicationData, r_next)
106 }
107 _ => return Err(RecordError::NotAllowed),
108 };
109
110 Ok((
111 Self {
112 header: hdr,
113 raw_bytes,
114 content,
115 },
116 rest_next,
117 ))
118 }
119 pub fn parse_server<P: ServerRecordProcessor>(
121 prc: &mut P,
122 bytes: &'r [u8],
123 ) -> Result<(Record<'r>, &'r [u8]), RecordError> {
124 let (hdr, rest) =
125 RecordHeader::try_ref_from_prefix(bytes).map_err(|e| RecordError::from_zero_copy(e))?;
126
127 if hdr.record_length > 8192 {
128 return Err(RecordError::OverflowLength);
129 }
130
131 let raw_bytes = &rest[0..usize::from(hdr.record_length)];
132
133 let (content, rest_next) = match hdr.content_type {
134 ContentType::Handshake => {
135 let (c, r_next) = HandshakeMsg::server_parse(prc, rest).unwrap();
136 (Content::Handshake(c), r_next)
137 }
138 ContentType::Alert => {
139 let (c, r_next) = AlertMsg::client_parse(rest).unwrap();
140 (Content::Alert(c), r_next)
141 }
142 ContentType::ApplicationData => {
143 let r_next = &rest[usize::from(hdr.record_length)..];
144 (Content::ApplicationData, r_next)
145 }
146 ContentType::ChangeCipherSpec => {
147 let r_next = &rest[usize::from(hdr.record_length)..];
148 (Content::ChangeCipherSpec, r_next)
149 }
150 };
151
152 Ok((
153 Self {
154 header: hdr,
155 raw_bytes,
156 content,
157 },
158 rest_next,
159 ))
160 }
161 #[inline]
163 pub fn parse_client<P: ClientHelloProcessor>(
164 prc: &mut P,
165 bytes: &'r [u8],
166 ) -> Result<(Record<'r>, &'r [u8]), RecordError> {
167 let (hdr, rest) =
168 RecordHeader::try_ref_from_prefix(bytes).map_err(|e| RecordError::from_zero_copy(e))?;
169
170 if hdr.record_length > 8196 {
171 return Err(RecordError::OverflowLength);
172 }
173
174 let raw_bytes = &rest[0..usize::from(hdr.record_length)];
175
176 let (content, rest_next) = match hdr.content_type {
177 ContentType::Handshake => {
178 let (c, r_next) = HandshakeMsg::client_parse(prc, rest).unwrap();
179 (Content::Handshake(c), r_next)
180 }
181 ContentType::Alert => {
182 let (c, r_next) = AlertMsg::client_parse(rest).unwrap();
183 (Content::Alert(c), r_next)
184 }
185 ContentType::ApplicationData => {
186 let r_next = &rest[usize::from(hdr.record_length)..];
187 (Content::ApplicationData, r_next)
188 }
189 ContentType::ChangeCipherSpec => {
190 let r_next = &rest[usize::from(hdr.record_length)..];
191 (Content::ChangeCipherSpec, r_next)
192 }
193 };
194
195 Ok((
196 Self {
197 header: hdr,
198 raw_bytes,
199 content,
200 },
201 rest_next,
202 ))
203 }
204}
205
206#[cfg(test)]
207mod test {
208
209 use super::*;
210 use hex_literal::hex;
211
212 #[derive(Debug, PartialEq)]
213 struct Tester {
214 suites_encountered: Vec<[u8; 2]>,
215 extensions_encountered: Vec<u16>,
216 }
217 impl ClientHelloProcessor for Tester {
218 fn handle_extension(&mut self, ext_id: u16, _ext_data: &[u8]) -> () {
219 self.extensions_encountered.push(ext_id);
220 }
221 fn handle_cipher_suite(&mut self, cipher_suite: &[u8; 2]) -> () {
222 self.suites_encountered
223 .push([cipher_suite[0], cipher_suite[1]]);
224 }
225 fn handle_client_random(&mut self, _cr: &[u8; 32]) {
226 }
228 fn handle_session_id(&mut self, _sid: &[u8]) {
229 }
231 }
232
233 #[test]
234 fn test_firefox_handshake_client_hello() {
235 let mut tester = Tester {
236 suites_encountered: vec![],
237 extensions_encountered: vec![],
238 };
239 let data = hex!("16030102970100029303030b77e4fa04ceb4dc026c74213fe2a55c14883219b9e6f7b0b503ee2b4a331d842065dcc0babe8c401c1e8afe1f5e40e54155dd0f28e1c7be6e2326143f89bcd95d0022130113031302c02bc02fcca9cca8c02cc030c00ac009c013c014009c009d002f003501000228000000150013000010746573742e72757374637279702e746f00170000ff01000100000a000e000c001d00170018001901000101000b00020100002300000010000e000c02683208687474702f312e310005000501000000000022000a00080403050306030203001200000033006b0069001d0020978142d12fa56febea3f967a43cf5accea191ce4cd5dcfe9d1fd7a5817bbc72700170041043d89d5b8f29cb5c29230bcc6eae0c2890f489724426bd26e2a72581231956ae99117c739f4d24d564143a732a73e92421b49ff51a9c44f729460f6ee251e537b002b00050403040303000d0018001604030503060308040805080604010501060102030201002d00020101001c00024001fe0d01190000010003af0020e80e102405db3ad2cfb267f6e11556f1f8b6364f5a02b07897b8eaee4e0d5a1900efe3b3a5d24df62d045ab566ba61536b5443cec82be022b712882204f783afe7c7eb59b93e6b9d30d623fe9a85d0895936be8f85d54818e9a06889e6ed53e3d5aa94e0812c872d5eb40277f6d2b9c1afdbab70bc7da5d6281d2632895855675bc5e7ddadd6aefec02342135082950c430deb6c4ce3d9294929271722aaddb06a7770594ec2bd395378e061b292dfdaa537e2535ca7ee5c698991f8dd8b5c227295e2ceccb7a9b84db5cadcb055f1ef019d6699f76959260a0a49574d18456be3936e74f76d3e5e5b418ddc45b2b219cee91c9ddf0c58dd3c0fb87d954cb59a43d897ed11f7ea0a51fb7b093ad547d2b0");
240 let (r, rest) = Record::parse_client(&mut tester, &data).unwrap();
241
242 insta::assert_debug_snapshot!(r);
243 assert_eq!(rest.len(), 0);
244 }
245}