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