tlspuffin/tls/rustls/msgs/
base.rs1use std::fmt::Debug;
2
3use comparable::Comparable;
4use extractable_macro::Extractable;
5use puffin::codec;
6use puffin::codec::{Codec, Reader};
7
8use crate::protocol::TLSProtocolTypes;
9use crate::tls::rustls::key;
10
11#[derive(Debug, Clone, PartialEq, Extractable, Comparable)]
13#[extractable(TLSProtocolTypes)]
14pub struct Payload(
15 #[extractable_no_recursion]
16 #[comparable_ignore]
17 pub Vec<u8>,
18);
19
20impl Codec for Payload {
21 fn encode(&self, bytes: &mut Vec<u8>) {
22 bytes.extend_from_slice(&self.0);
23 }
24
25 fn read(r: &mut Reader) -> Option<Self> {
26 Some(Self::read(r))
27 }
28}
29
30impl Payload {
31 pub fn new(bytes: impl Into<Vec<u8>>) -> Self {
32 Self(bytes.into())
33 }
34
35 pub fn empty() -> Self {
36 Self::new(Vec::new())
37 }
38
39 pub fn read(r: &mut Reader) -> Self {
40 Self(r.rest().to_vec())
41 }
42}
43
44impl Codec for key::Certificate {
45 fn encode(&self, bytes: &mut Vec<u8>) {
46 codec::u24(self.0.len() as u32).encode(bytes);
47 bytes.extend_from_slice(&self.0);
48 }
49
50 fn read(r: &mut Reader) -> Option<Self> {
51 let len = codec::u24::read(r)?.0 as usize;
52 let mut sub = r.sub(len)?;
53 let body = sub.rest().to_vec();
54 Some(Self(body))
55 }
56}
57
58#[derive(Debug, Clone, PartialEq, Extractable, Comparable)]
60#[extractable(TLSProtocolTypes)]
61pub struct PayloadU24(pub Vec<u8>);
62
63impl PayloadU24 {
64 pub fn new(bytes: Vec<u8>) -> Self {
65 Self(bytes)
66 }
67}
68
69impl Codec for PayloadU24 {
70 fn encode(&self, bytes: &mut Vec<u8>) {
71 codec::u24(self.0.len() as u32).encode(bytes);
72 bytes.extend_from_slice(&self.0);
73 }
74
75 fn read(r: &mut Reader) -> Option<Self> {
76 let len = codec::u24::read(r)?.0 as usize;
77 let mut sub = r.sub(len)?;
78 let body = sub.rest().to_vec();
79 Some(Self(body))
80 }
81}
82
83#[derive(Debug, Clone, PartialEq, Extractable, Comparable)]
85#[extractable(TLSProtocolTypes)]
86pub struct PayloadU16(#[extractable_no_recursion] pub Vec<u8>);
87
88impl PayloadU16 {
89 pub fn new(bytes: Vec<u8>) -> Self {
90 Self(bytes)
91 }
92
93 pub fn empty() -> Self {
94 Self::new(Vec::new())
95 }
96
97 pub fn encode_slice(slice: &[u8], bytes: &mut Vec<u8>) {
98 (slice.len() as u16).encode(bytes);
99 bytes.extend_from_slice(slice);
100 }
101}
102
103impl Codec for PayloadU16 {
104 fn encode(&self, bytes: &mut Vec<u8>) {
105 Self::encode_slice(&self.0, bytes);
106 }
107
108 fn read(r: &mut Reader) -> Option<Self> {
109 let len = u16::read(r)? as usize;
110 let mut sub = r.sub(len)?;
111 let body = sub.rest().to_vec();
112 Some(Self(body))
113 }
114}
115
116#[derive(Debug, Clone, PartialEq, Extractable, Comparable)]
118#[extractable(TLSProtocolTypes)]
119pub struct PayloadU8(pub Vec<u8>);
120
121impl PayloadU8 {
122 pub fn new(bytes: Vec<u8>) -> Self {
123 Self(bytes)
124 }
125
126 pub fn empty() -> Self {
127 Self(Vec::new())
128 }
129
130 pub fn into_inner(self) -> Vec<u8> {
131 self.0
132 }
133}
134
135impl Codec for PayloadU8 {
136 fn encode(&self, bytes: &mut Vec<u8>) {
137 (self.0.len() as u8).encode(bytes);
138 bytes.extend_from_slice(&self.0);
139 }
140
141 fn read(r: &mut Reader) -> Option<Self> {
142 let len = u8::read(r)? as usize;
143 let mut sub = r.sub(len)?;
144 let body = sub.rest().to_vec();
145 Some(Self(body))
146 }
147}
148
149impl codec::VecCodecWoSize for PayloadU8 {}
151impl codec::VecCodecWoSize for PayloadU16 {}
152impl codec::VecCodecWoSize for PayloadU24 {}