rustls/msgs/
base.rs

1use alloc::vec::Vec;
2use core::fmt;
3
4use pki_types::CertificateDer;
5use zeroize::Zeroize;
6
7use crate::error::InvalidMessage;
8use crate::msgs::codec;
9use crate::msgs::codec::{Codec, Reader};
10
11/// An externally length'd payload
12#[derive(Clone, Eq, PartialEq)]
13pub enum Payload<'a> {
14    Borrowed(&'a [u8]),
15    Owned(Vec<u8>),
16}
17
18impl<'a> Codec<'a> for Payload<'a> {
19    fn encode(&self, bytes: &mut Vec<u8>) {
20        bytes.extend_from_slice(self.bytes());
21    }
22
23    fn read(r: &mut Reader<'a>) -> Result<Self, InvalidMessage> {
24        Ok(Self::read(r))
25    }
26}
27
28impl<'a> Payload<'a> {
29    pub fn bytes(&self) -> &[u8] {
30        match self {
31            Self::Borrowed(bytes) => bytes,
32            Self::Owned(bytes) => bytes,
33        }
34    }
35
36    pub fn into_owned(self) -> Payload<'static> {
37        Payload::Owned(self.into_vec())
38    }
39
40    pub fn into_vec(self) -> Vec<u8> {
41        match self {
42            Self::Borrowed(bytes) => bytes.to_vec(),
43            Self::Owned(bytes) => bytes,
44        }
45    }
46
47    pub fn read(r: &mut Reader<'a>) -> Self {
48        Self::Borrowed(r.rest())
49    }
50}
51
52impl Payload<'static> {
53    pub fn new(bytes: impl Into<Vec<u8>>) -> Self {
54        Self::Owned(bytes.into())
55    }
56
57    pub fn empty() -> Self {
58        Self::Borrowed(&[])
59    }
60}
61
62impl<'a> Codec<'a> for CertificateDer<'a> {
63    fn encode(&self, bytes: &mut Vec<u8>) {
64        codec::u24(self.as_ref().len() as u32).encode(bytes);
65        bytes.extend(self.as_ref());
66    }
67
68    fn read(r: &mut Reader<'a>) -> Result<Self, InvalidMessage> {
69        let len = codec::u24::read(r)?.0 as usize;
70        let mut sub = r.sub(len)?;
71        let body = sub.rest();
72        Ok(Self::from(body))
73    }
74}
75
76impl fmt::Debug for Payload<'_> {
77    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
78        hex(f, self.bytes())
79    }
80}
81
82/// An arbitrary, unknown-content, u24-length-prefixed payload
83#[derive(Clone, Eq, PartialEq)]
84pub(crate) struct PayloadU24<'a>(pub(crate) Payload<'a>);
85
86impl PayloadU24<'_> {
87    pub(crate) fn into_owned(self) -> PayloadU24<'static> {
88        PayloadU24(self.0.into_owned())
89    }
90}
91
92impl<'a> Codec<'a> for PayloadU24<'a> {
93    fn encode(&self, bytes: &mut Vec<u8>) {
94        let inner = self.0.bytes();
95        codec::u24(inner.len() as u32).encode(bytes);
96        bytes.extend_from_slice(inner);
97    }
98
99    fn read(r: &mut Reader<'a>) -> Result<Self, InvalidMessage> {
100        let len = codec::u24::read(r)?.0 as usize;
101        let mut sub = r.sub(len)?;
102        Ok(Self(Payload::read(&mut sub)))
103    }
104}
105
106impl fmt::Debug for PayloadU24<'_> {
107    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
108        self.0.fmt(f)
109    }
110}
111
112/// An arbitrary, unknown-content, u16-length-prefixed payload
113#[derive(Clone, Eq, PartialEq)]
114pub struct PayloadU16(pub Vec<u8>);
115
116impl PayloadU16 {
117    pub fn new(bytes: Vec<u8>) -> Self {
118        Self(bytes)
119    }
120
121    pub fn empty() -> Self {
122        Self::new(Vec::new())
123    }
124
125    pub fn encode_slice(slice: &[u8], bytes: &mut Vec<u8>) {
126        (slice.len() as u16).encode(bytes);
127        bytes.extend_from_slice(slice);
128    }
129}
130
131impl Codec<'_> for PayloadU16 {
132    fn encode(&self, bytes: &mut Vec<u8>) {
133        Self::encode_slice(&self.0, bytes);
134    }
135
136    fn read(r: &mut Reader<'_>) -> Result<Self, InvalidMessage> {
137        let len = u16::read(r)? as usize;
138        let mut sub = r.sub(len)?;
139        let body = sub.rest().to_vec();
140        Ok(Self(body))
141    }
142}
143
144impl fmt::Debug for PayloadU16 {
145    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
146        hex(f, &self.0)
147    }
148}
149
150/// An arbitrary, unknown-content, u8-length-prefixed payload
151#[derive(Clone, Eq, PartialEq)]
152pub struct PayloadU8(pub(crate) Vec<u8>);
153
154impl PayloadU8 {
155    pub(crate) fn encode_slice(slice: &[u8], bytes: &mut Vec<u8>) {
156        (slice.len() as u8).encode(bytes);
157        bytes.extend_from_slice(slice);
158    }
159
160    pub(crate) fn new(bytes: Vec<u8>) -> Self {
161        Self(bytes)
162    }
163
164    pub(crate) fn empty() -> Self {
165        Self(Vec::new())
166    }
167}
168
169impl Codec<'_> for PayloadU8 {
170    fn encode(&self, bytes: &mut Vec<u8>) {
171        (self.0.len() as u8).encode(bytes);
172        bytes.extend_from_slice(&self.0);
173    }
174
175    fn read(r: &mut Reader<'_>) -> Result<Self, InvalidMessage> {
176        let len = u8::read(r)? as usize;
177        let mut sub = r.sub(len)?;
178        let body = sub.rest().to_vec();
179        Ok(Self(body))
180    }
181}
182
183impl Zeroize for PayloadU8 {
184    fn zeroize(&mut self) {
185        self.0.zeroize();
186    }
187}
188
189impl fmt::Debug for PayloadU8 {
190    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
191        hex(f, &self.0)
192    }
193}
194
195// Format an iterator of u8 into a hex string
196pub(super) fn hex<'a>(
197    f: &mut fmt::Formatter<'_>,
198    payload: impl IntoIterator<Item = &'a u8>,
199) -> fmt::Result {
200    for b in payload {
201        write!(f, "{:02x}", b)?;
202    }
203    Ok(())
204}