/rust/registry/src/index.crates.io-1949cf8c6b5b557f/rustls-0.23.44/src/msgs/base.rs
Line | Count | Source |
1 | | use alloc::vec::Vec; |
2 | | use core::fmt; |
3 | | use core::marker::PhantomData; |
4 | | |
5 | | use pki_types::CertificateDer; |
6 | | use zeroize::Zeroize; |
7 | | |
8 | | use crate::error::InvalidMessage; |
9 | | use crate::msgs::codec; |
10 | | use crate::msgs::codec::{Codec, Reader}; |
11 | | |
12 | | /// An externally length'd payload |
13 | | #[derive(Clone, Eq, PartialEq)] |
14 | | pub enum Payload<'a> { |
15 | | Borrowed(&'a [u8]), |
16 | | Owned(Vec<u8>), |
17 | | } |
18 | | |
19 | | impl<'a> Codec<'a> for Payload<'a> { |
20 | 0 | fn encode(&self, bytes: &mut Vec<u8>) { |
21 | 0 | bytes.extend_from_slice(self.bytes()); |
22 | 0 | } Unexecuted instantiation: <rustls::msgs::base::Payload as rustls::msgs::codec::Codec>::encode Unexecuted instantiation: <rustls::msgs::base::Payload as rustls::msgs::codec::Codec>::encode |
23 | | |
24 | 0 | fn read(r: &mut Reader<'a>) -> Result<Self, InvalidMessage> { |
25 | 0 | Ok(Self::read(r)) |
26 | 0 | } Unexecuted instantiation: <rustls::msgs::base::Payload as rustls::msgs::codec::Codec>::read Unexecuted instantiation: <rustls::msgs::base::Payload as rustls::msgs::codec::Codec>::read |
27 | | } |
28 | | |
29 | | impl<'a> Payload<'a> { |
30 | 0 | pub fn bytes(&self) -> &[u8] { |
31 | 0 | match self { |
32 | 0 | Self::Borrowed(bytes) => bytes, |
33 | 0 | Self::Owned(bytes) => bytes, |
34 | | } |
35 | 0 | } Unexecuted instantiation: <rustls::msgs::base::Payload>::bytes Unexecuted instantiation: <rustls::msgs::base::Payload>::bytes |
36 | | |
37 | 0 | pub fn into_owned(self) -> Payload<'static> { |
38 | 0 | Payload::Owned(self.into_vec()) |
39 | 0 | } Unexecuted instantiation: <rustls::msgs::base::Payload>::into_owned Unexecuted instantiation: <rustls::msgs::base::Payload>::into_owned |
40 | | |
41 | 0 | pub fn into_vec(self) -> Vec<u8> { |
42 | 0 | match self { |
43 | 0 | Self::Borrowed(bytes) => bytes.to_vec(), |
44 | 0 | Self::Owned(bytes) => bytes, |
45 | | } |
46 | 0 | } Unexecuted instantiation: <rustls::msgs::base::Payload>::into_vec Unexecuted instantiation: <rustls::msgs::base::Payload>::into_vec |
47 | | |
48 | 0 | pub fn read(r: &mut Reader<'a>) -> Self { |
49 | 0 | Self::Borrowed(r.rest()) |
50 | 0 | } Unexecuted instantiation: <rustls::msgs::base::Payload>::read Unexecuted instantiation: <rustls::msgs::base::Payload>::read |
51 | | } |
52 | | |
53 | | impl Payload<'static> { |
54 | 0 | pub fn new(bytes: impl Into<Vec<u8>>) -> Self { |
55 | 0 | Self::Owned(bytes.into()) |
56 | 0 | } Unexecuted instantiation: <rustls::msgs::base::Payload>::new::<alloc::vec::Vec<u8>> Unexecuted instantiation: <rustls::msgs::base::Payload>::new::<&[u8]> Unexecuted instantiation: <rustls::msgs::base::Payload>::new::<alloc::vec::Vec<u8>> Unexecuted instantiation: <rustls::msgs::base::Payload>::new::<&[u8]> |
57 | | |
58 | 0 | pub fn empty() -> Self { |
59 | 0 | Self::Borrowed(&[]) |
60 | 0 | } Unexecuted instantiation: <rustls::msgs::base::Payload>::empty Unexecuted instantiation: <rustls::msgs::base::Payload>::empty |
61 | | } |
62 | | |
63 | | impl<'a> Codec<'a> for CertificateDer<'a> { |
64 | 0 | fn encode(&self, bytes: &mut Vec<u8>) { |
65 | 0 | codec::u24(self.as_ref().len() as u32).encode(bytes); |
66 | 0 | bytes.extend(self.as_ref()); |
67 | 0 | } Unexecuted instantiation: <rustls_pki_types::CertificateDer as rustls::msgs::codec::Codec>::encode Unexecuted instantiation: <rustls_pki_types::CertificateDer as rustls::msgs::codec::Codec>::encode |
68 | | |
69 | 0 | fn read(r: &mut Reader<'a>) -> Result<Self, InvalidMessage> { |
70 | 0 | let len = codec::u24::read(r)?.0 as usize; |
71 | 0 | let mut sub = r.sub(len)?; |
72 | 0 | let body = sub.rest(); |
73 | 0 | Ok(Self::from(body)) |
74 | 0 | } Unexecuted instantiation: <rustls_pki_types::CertificateDer as rustls::msgs::codec::Codec>::read Unexecuted instantiation: <rustls_pki_types::CertificateDer as rustls::msgs::codec::Codec>::read |
75 | | } |
76 | | |
77 | | impl fmt::Debug for Payload<'_> { |
78 | 0 | fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result { |
79 | 0 | hex(f, self.bytes()) |
80 | 0 | } Unexecuted instantiation: <rustls::msgs::base::Payload as core::fmt::Debug>::fmt Unexecuted instantiation: <rustls::msgs::base::Payload as core::fmt::Debug>::fmt |
81 | | } |
82 | | |
83 | | /// An arbitrary, unknown-content, u24-length-prefixed payload |
84 | | #[derive(Clone, Eq, PartialEq)] |
85 | | pub(crate) struct PayloadU24<'a, C: Cardinality = MaybeEmpty>( |
86 | | pub(crate) Payload<'a>, |
87 | | PhantomData<C>, |
88 | | ); |
89 | | |
90 | | impl<C: Cardinality> PayloadU24<'_, C> { |
91 | 0 | pub(crate) fn into_owned(self) -> PayloadU24<'static, C> { |
92 | 0 | PayloadU24(self.0.into_owned(), PhantomData) |
93 | 0 | } Unexecuted instantiation: <rustls::msgs::base::PayloadU24<rustls::msgs::base::NonEmpty>>::into_owned Unexecuted instantiation: <rustls::msgs::base::PayloadU24>::into_owned Unexecuted instantiation: <rustls::msgs::base::PayloadU24<rustls::msgs::base::NonEmpty>>::into_owned Unexecuted instantiation: <rustls::msgs::base::PayloadU24>::into_owned |
94 | | } |
95 | | |
96 | | impl<'a, C: Cardinality> Codec<'a> for PayloadU24<'a, C> { |
97 | 0 | fn encode(&self, bytes: &mut Vec<u8>) { |
98 | 0 | let inner = self.0.bytes(); |
99 | 0 | debug_assert!(inner.len() >= C::MIN); |
100 | 0 | codec::u24(inner.len() as u32).encode(bytes); |
101 | 0 | bytes.extend_from_slice(inner); |
102 | 0 | } Unexecuted instantiation: <rustls::msgs::base::PayloadU24<rustls::msgs::base::NonEmpty> as rustls::msgs::codec::Codec>::encode Unexecuted instantiation: <rustls::msgs::base::PayloadU24 as rustls::msgs::codec::Codec>::encode Unexecuted instantiation: <rustls::msgs::base::PayloadU24<rustls::msgs::base::NonEmpty> as rustls::msgs::codec::Codec>::encode Unexecuted instantiation: <rustls::msgs::base::PayloadU24 as rustls::msgs::codec::Codec>::encode |
103 | | |
104 | 0 | fn read(r: &mut Reader<'a>) -> Result<Self, InvalidMessage> { |
105 | 0 | let len = codec::u24::read(r)?.0 as usize; |
106 | 0 | if len < C::MIN { |
107 | 0 | return Err(InvalidMessage::IllegalEmptyList("PayloadU24")); |
108 | 0 | } |
109 | 0 | let mut sub = r.sub(len)?; |
110 | 0 | Ok(Self(Payload::read(&mut sub), PhantomData)) |
111 | 0 | } Unexecuted instantiation: <rustls::msgs::base::PayloadU24<rustls::msgs::base::NonEmpty> as rustls::msgs::codec::Codec>::read Unexecuted instantiation: <rustls::msgs::base::PayloadU24 as rustls::msgs::codec::Codec>::read Unexecuted instantiation: <rustls::msgs::base::PayloadU24<rustls::msgs::base::NonEmpty> as rustls::msgs::codec::Codec>::read Unexecuted instantiation: <rustls::msgs::base::PayloadU24 as rustls::msgs::codec::Codec>::read |
112 | | } |
113 | | |
114 | | impl<'a, C: Cardinality> From<Payload<'a>> for PayloadU24<'a, C> { |
115 | 0 | fn from(value: Payload<'a>) -> Self { |
116 | 0 | debug_assert!(value.bytes().len() >= C::MIN); |
117 | 0 | Self(value, PhantomData) |
118 | 0 | } Unexecuted instantiation: <rustls::msgs::base::PayloadU24<rustls::msgs::base::NonEmpty> as core::convert::From<rustls::msgs::base::Payload>>::from Unexecuted instantiation: <rustls::msgs::base::PayloadU24 as core::convert::From<rustls::msgs::base::Payload>>::from Unexecuted instantiation: <rustls::msgs::base::PayloadU24<rustls::msgs::base::NonEmpty> as core::convert::From<rustls::msgs::base::Payload>>::from Unexecuted instantiation: <rustls::msgs::base::PayloadU24 as core::convert::From<rustls::msgs::base::Payload>>::from |
119 | | } |
120 | | |
121 | | impl<C: Cardinality> AsRef<[u8]> for PayloadU24<'_, C> { |
122 | 0 | fn as_ref(&self) -> &[u8] { |
123 | 0 | self.0.bytes() |
124 | 0 | } Unexecuted instantiation: <rustls::msgs::base::PayloadU24<_> as core::convert::AsRef<[u8]>>::as_ref Unexecuted instantiation: <rustls::msgs::base::PayloadU24<_> as core::convert::AsRef<[u8]>>::as_ref |
125 | | } |
126 | | |
127 | | impl<C: Cardinality> fmt::Debug for PayloadU24<'_, C> { |
128 | 0 | fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result { |
129 | 0 | self.0.fmt(f) |
130 | 0 | } Unexecuted instantiation: <rustls::msgs::base::PayloadU24<rustls::msgs::base::NonEmpty> as core::fmt::Debug>::fmt Unexecuted instantiation: <rustls::msgs::base::PayloadU24<rustls::msgs::base::NonEmpty> as core::fmt::Debug>::fmt |
131 | | } |
132 | | |
133 | | /// An arbitrary, unknown-content, u16-length-prefixed payload |
134 | | /// |
135 | | /// The `C` type parameter controls whether decoded values may |
136 | | /// be empty. |
137 | | #[derive(Clone, Eq, PartialEq)] |
138 | | pub struct PayloadU16<C: Cardinality = MaybeEmpty>(pub(crate) Vec<u8>, PhantomData<C>); |
139 | | |
140 | | impl<C: Cardinality> PayloadU16<C> { |
141 | 0 | pub fn new(bytes: Vec<u8>) -> Self { |
142 | 0 | debug_assert!(bytes.len() >= C::MIN); |
143 | 0 | Self(bytes, PhantomData) |
144 | 0 | } Unexecuted instantiation: <rustls::msgs::base::PayloadU16<rustls::msgs::base::NonEmpty>>::new Unexecuted instantiation: <rustls::msgs::base::PayloadU16>::new Unexecuted instantiation: <rustls::msgs::base::PayloadU16<rustls::msgs::base::NonEmpty>>::new Unexecuted instantiation: <rustls::msgs::base::PayloadU16>::new |
145 | | } |
146 | | |
147 | | impl PayloadU16<MaybeEmpty> { |
148 | 0 | pub(crate) fn empty() -> Self { |
149 | 0 | Self::new(Vec::new()) |
150 | 0 | } Unexecuted instantiation: <rustls::msgs::base::PayloadU16>::empty Unexecuted instantiation: <rustls::msgs::base::PayloadU16>::empty |
151 | | } |
152 | | |
153 | | impl<C: Cardinality> Codec<'_> for PayloadU16<C> { |
154 | 0 | fn encode(&self, bytes: &mut Vec<u8>) { |
155 | 0 | debug_assert!(self.0.len() >= C::MIN); |
156 | 0 | (self.0.len() as u16).encode(bytes); |
157 | 0 | bytes.extend_from_slice(&self.0); |
158 | 0 | } Unexecuted instantiation: <rustls::msgs::base::PayloadU16<rustls::msgs::base::NonEmpty> as rustls::msgs::codec::Codec>::encode Unexecuted instantiation: <rustls::msgs::base::PayloadU16 as rustls::msgs::codec::Codec>::encode Unexecuted instantiation: <rustls::msgs::base::PayloadU16<rustls::msgs::base::NonEmpty> as rustls::msgs::codec::Codec>::encode Unexecuted instantiation: <rustls::msgs::base::PayloadU16 as rustls::msgs::codec::Codec>::encode |
159 | | |
160 | 0 | fn read(r: &mut Reader<'_>) -> Result<Self, InvalidMessage> { |
161 | 0 | let len = u16::read(r)? as usize; |
162 | 0 | if len < C::MIN { |
163 | 0 | return Err(InvalidMessage::IllegalEmptyValue); |
164 | 0 | } |
165 | 0 | let mut sub = r.sub(len)?; |
166 | 0 | let body = sub.rest().to_vec(); |
167 | 0 | Ok(Self(body, PhantomData)) |
168 | 0 | } Unexecuted instantiation: <rustls::msgs::base::PayloadU16<rustls::msgs::base::NonEmpty> as rustls::msgs::codec::Codec>::read Unexecuted instantiation: <rustls::msgs::base::PayloadU16 as rustls::msgs::codec::Codec>::read Unexecuted instantiation: <rustls::msgs::base::PayloadU16<rustls::msgs::base::NonEmpty> as rustls::msgs::codec::Codec>::read Unexecuted instantiation: <rustls::msgs::base::PayloadU16 as rustls::msgs::codec::Codec>::read |
169 | | } |
170 | | |
171 | | impl<C: Cardinality> fmt::Debug for PayloadU16<C> { |
172 | 0 | fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result { |
173 | 0 | hex(f, &self.0) |
174 | 0 | } Unexecuted instantiation: <rustls::msgs::base::PayloadU16<rustls::msgs::base::NonEmpty> as core::fmt::Debug>::fmt Unexecuted instantiation: <rustls::msgs::base::PayloadU16 as core::fmt::Debug>::fmt Unexecuted instantiation: <rustls::msgs::base::PayloadU16<rustls::msgs::base::NonEmpty> as core::fmt::Debug>::fmt Unexecuted instantiation: <rustls::msgs::base::PayloadU16 as core::fmt::Debug>::fmt |
175 | | } |
176 | | |
177 | | /// An arbitrary, unknown-content, u8-length-prefixed payload |
178 | | /// |
179 | | /// `C` controls the minimum length accepted when decoding. |
180 | | #[derive(Clone, Eq, PartialEq)] |
181 | | pub(crate) struct PayloadU8<C: Cardinality = MaybeEmpty>(pub(crate) Vec<u8>, PhantomData<C>); |
182 | | |
183 | | impl<C: Cardinality> PayloadU8<C> { |
184 | 0 | pub(crate) fn encode_slice(slice: &[u8], bytes: &mut Vec<u8>) { |
185 | 0 | (slice.len() as u8).encode(bytes); |
186 | 0 | bytes.extend_from_slice(slice); |
187 | 0 | } Unexecuted instantiation: <rustls::msgs::base::PayloadU8>::encode_slice Unexecuted instantiation: <rustls::msgs::base::PayloadU8>::encode_slice |
188 | | |
189 | 0 | pub(crate) fn new(bytes: Vec<u8>) -> Self { |
190 | 0 | debug_assert!(bytes.len() >= C::MIN); |
191 | 0 | Self(bytes, PhantomData) |
192 | 0 | } Unexecuted instantiation: <rustls::msgs::base::PayloadU8<rustls::msgs::base::NonEmpty>>::new Unexecuted instantiation: <rustls::msgs::base::PayloadU8>::new Unexecuted instantiation: <rustls::msgs::base::PayloadU8<rustls::msgs::base::NonEmpty>>::new Unexecuted instantiation: <rustls::msgs::base::PayloadU8>::new |
193 | | } |
194 | | |
195 | | impl PayloadU8<MaybeEmpty> { |
196 | 0 | pub(crate) fn empty() -> Self { |
197 | 0 | Self(Vec::new(), PhantomData) |
198 | 0 | } Unexecuted instantiation: <rustls::msgs::base::PayloadU8>::empty Unexecuted instantiation: <rustls::msgs::base::PayloadU8>::empty |
199 | | } |
200 | | |
201 | | impl<C: Cardinality> Codec<'_> for PayloadU8<C> { |
202 | 0 | fn encode(&self, bytes: &mut Vec<u8>) { |
203 | 0 | debug_assert!(self.0.len() >= C::MIN); |
204 | 0 | (self.0.len() as u8).encode(bytes); |
205 | 0 | bytes.extend_from_slice(&self.0); |
206 | 0 | } Unexecuted instantiation: <rustls::msgs::base::PayloadU8<rustls::msgs::base::NonEmpty> as rustls::msgs::codec::Codec>::encode Unexecuted instantiation: <rustls::msgs::base::PayloadU8 as rustls::msgs::codec::Codec>::encode Unexecuted instantiation: <rustls::msgs::base::PayloadU8<rustls::msgs::base::NonEmpty> as rustls::msgs::codec::Codec>::encode Unexecuted instantiation: <rustls::msgs::base::PayloadU8 as rustls::msgs::codec::Codec>::encode |
207 | | |
208 | 0 | fn read(r: &mut Reader<'_>) -> Result<Self, InvalidMessage> { |
209 | 0 | let len = u8::read(r)? as usize; |
210 | 0 | if len < C::MIN { |
211 | 0 | return Err(InvalidMessage::IllegalEmptyValue); |
212 | 0 | } |
213 | 0 | let mut sub = r.sub(len)?; |
214 | 0 | let body = sub.rest().to_vec(); |
215 | 0 | Ok(Self(body, PhantomData)) |
216 | 0 | } Unexecuted instantiation: <rustls::msgs::base::PayloadU8<rustls::msgs::base::NonEmpty> as rustls::msgs::codec::Codec>::read Unexecuted instantiation: <rustls::msgs::base::PayloadU8 as rustls::msgs::codec::Codec>::read Unexecuted instantiation: <rustls::msgs::base::PayloadU8<rustls::msgs::base::NonEmpty> as rustls::msgs::codec::Codec>::read Unexecuted instantiation: <rustls::msgs::base::PayloadU8 as rustls::msgs::codec::Codec>::read |
217 | | } |
218 | | |
219 | | impl<C: Cardinality> Zeroize for PayloadU8<C> { |
220 | 0 | fn zeroize(&mut self) { |
221 | 0 | self.0.zeroize(); |
222 | 0 | } Unexecuted instantiation: <rustls::msgs::base::PayloadU8 as zeroize::Zeroize>::zeroize Unexecuted instantiation: <rustls::msgs::base::PayloadU8 as zeroize::Zeroize>::zeroize |
223 | | } |
224 | | |
225 | | impl<C: Cardinality> fmt::Debug for PayloadU8<C> { |
226 | 0 | fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result { |
227 | 0 | hex(f, &self.0) |
228 | 0 | } Unexecuted instantiation: <rustls::msgs::base::PayloadU8<rustls::msgs::base::NonEmpty> as core::fmt::Debug>::fmt Unexecuted instantiation: <rustls::msgs::base::PayloadU8 as core::fmt::Debug>::fmt Unexecuted instantiation: <rustls::msgs::base::PayloadU8<rustls::msgs::base::NonEmpty> as core::fmt::Debug>::fmt Unexecuted instantiation: <rustls::msgs::base::PayloadU8 as core::fmt::Debug>::fmt |
229 | | } |
230 | | |
231 | | pub trait Cardinality: Clone + Eq + PartialEq { |
232 | | const MIN: usize; |
233 | | } |
234 | | |
235 | | #[derive(Clone, Eq, PartialEq)] |
236 | | pub struct MaybeEmpty; |
237 | | |
238 | | impl Cardinality for MaybeEmpty { |
239 | | const MIN: usize = 0; |
240 | | } |
241 | | |
242 | | #[derive(Clone, Eq, PartialEq)] |
243 | | pub struct NonEmpty; |
244 | | |
245 | | impl Cardinality for NonEmpty { |
246 | | const MIN: usize = 1; |
247 | | } |
248 | | |
249 | | // Format an iterator of u8 into a hex string |
250 | 0 | pub(super) fn hex<'a>( |
251 | 0 | f: &mut fmt::Formatter<'_>, |
252 | 0 | payload: impl IntoIterator<Item = &'a u8>, |
253 | 0 | ) -> fmt::Result { |
254 | 0 | for b in payload { |
255 | 0 | write!(f, "{b:02x}")?; |
256 | | } |
257 | 0 | Ok(()) |
258 | 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]> 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]> |