/rust/registry/src/index.crates.io-1949cf8c6b5b557f/rustls-0.23.46/src/msgs/handshake.rs
Line | Count | Source |
1 | | use alloc::boxed::Box; |
2 | | use alloc::collections::BTreeSet; |
3 | | #[cfg(feature = "logging")] |
4 | | use alloc::string::String; |
5 | | use alloc::vec; |
6 | | use alloc::vec::Vec; |
7 | | use core::ops::{Deref, DerefMut}; |
8 | | use core::{fmt, iter}; |
9 | | |
10 | | use pki_types::{CertificateDer, DnsName}; |
11 | | |
12 | | #[cfg(feature = "tls12")] |
13 | | use crate::crypto::ActiveKeyExchange; |
14 | | use crate::crypto::SecureRandom; |
15 | | use crate::enums::{ |
16 | | CertificateCompressionAlgorithm, CertificateType, CipherSuite, EchClientHelloType, |
17 | | HandshakeType, ProtocolVersion, SignatureScheme, |
18 | | }; |
19 | | use crate::error::InvalidMessage; |
20 | | #[cfg(feature = "tls12")] |
21 | | use crate::ffdhe_groups::FfdheGroup; |
22 | | use crate::log::warn; |
23 | | use crate::msgs::base::{MaybeEmpty, NonEmpty, Payload, PayloadU8, PayloadU16, PayloadU24}; |
24 | | use crate::msgs::codec::{ |
25 | | self, Codec, LengthPrefixedBuffer, ListLength, Reader, TlsListElement, TlsListIter, |
26 | | }; |
27 | | use crate::msgs::enums::{ |
28 | | CertificateStatusType, ClientCertificateType, Compression, ECCurveType, ECPointFormat, |
29 | | EchVersion, ExtensionType, HpkeAead, HpkeKdf, HpkeKem, KeyUpdateRequest, NamedGroup, |
30 | | PskKeyExchangeMode, ServerNameType, |
31 | | }; |
32 | | use crate::rand; |
33 | | use crate::sync::Arc; |
34 | | use crate::verify::DigitallySignedStruct; |
35 | | use crate::x509::wrap_in_sequence; |
36 | | |
37 | | /// Create a newtype wrapper around a given type. |
38 | | /// |
39 | | /// This is used to create newtypes for the various TLS message types which is used to wrap |
40 | | /// the `PayloadU8` or `PayloadU16` types. This is typically used for types where we don't need |
41 | | /// anything other than access to the underlying bytes. |
42 | | macro_rules! wrapped_payload( |
43 | | ($(#[$comment:meta])* $vis:vis struct $name:ident, $inner:ident$(<$inner_ty:ty>)?,) => { |
44 | | $(#[$comment])* |
45 | | #[derive(Clone, Debug)] |
46 | | $vis struct $name($inner$(<$inner_ty>)?); |
47 | | |
48 | | impl From<Vec<u8>> for $name { |
49 | 0 | fn from(v: Vec<u8>) -> Self { |
50 | 0 | Self($inner::new(v)) |
51 | 0 | } Unexecuted instantiation: <rustls::msgs::handshake::ProtocolName as core::convert::From<alloc::vec::Vec<u8>>>::from Unexecuted instantiation: <rustls::msgs::handshake::PresharedKeyBinder as core::convert::From<alloc::vec::Vec<u8>>>::from Unexecuted instantiation: <rustls::msgs::handshake::ResponderId as core::convert::From<alloc::vec::Vec<u8>>>::from Unexecuted instantiation: <rustls::msgs::handshake::DistinguishedName as core::convert::From<alloc::vec::Vec<u8>>>::from Unexecuted instantiation: <rustls::msgs::handshake::ProtocolName as core::convert::From<alloc::vec::Vec<u8>>>::from Unexecuted instantiation: <rustls::msgs::handshake::PresharedKeyBinder as core::convert::From<alloc::vec::Vec<u8>>>::from Unexecuted instantiation: <rustls::msgs::handshake::ResponderId as core::convert::From<alloc::vec::Vec<u8>>>::from Unexecuted instantiation: <rustls::msgs::handshake::DistinguishedName as core::convert::From<alloc::vec::Vec<u8>>>::from Unexecuted instantiation: <rustls::msgs::handshake::ProtocolName as core::convert::From<alloc::vec::Vec<u8>>>::from Unexecuted instantiation: <rustls::msgs::handshake::PresharedKeyBinder as core::convert::From<alloc::vec::Vec<u8>>>::from Unexecuted instantiation: <rustls::msgs::handshake::ResponderId as core::convert::From<alloc::vec::Vec<u8>>>::from Unexecuted instantiation: <rustls::msgs::handshake::DistinguishedName as core::convert::From<alloc::vec::Vec<u8>>>::from |
52 | | } |
53 | | |
54 | | impl AsRef<[u8]> for $name { |
55 | 0 | fn as_ref(&self) -> &[u8] { |
56 | 0 | self.0.0.as_slice() |
57 | 0 | } Unexecuted instantiation: <rustls::msgs::handshake::ProtocolName as core::convert::AsRef<[u8]>>::as_ref Unexecuted instantiation: <rustls::msgs::handshake::PresharedKeyBinder as core::convert::AsRef<[u8]>>::as_ref Unexecuted instantiation: <rustls::msgs::handshake::ResponderId as core::convert::AsRef<[u8]>>::as_ref Unexecuted instantiation: <rustls::msgs::handshake::DistinguishedName as core::convert::AsRef<[u8]>>::as_ref Unexecuted instantiation: <rustls::msgs::handshake::ProtocolName as core::convert::AsRef<[u8]>>::as_ref Unexecuted instantiation: <rustls::msgs::handshake::PresharedKeyBinder as core::convert::AsRef<[u8]>>::as_ref Unexecuted instantiation: <rustls::msgs::handshake::ResponderId as core::convert::AsRef<[u8]>>::as_ref Unexecuted instantiation: <rustls::msgs::handshake::DistinguishedName as core::convert::AsRef<[u8]>>::as_ref Unexecuted instantiation: <rustls::msgs::handshake::ProtocolName as core::convert::AsRef<[u8]>>::as_ref Unexecuted instantiation: <rustls::msgs::handshake::PresharedKeyBinder as core::convert::AsRef<[u8]>>::as_ref Unexecuted instantiation: <rustls::msgs::handshake::ResponderId as core::convert::AsRef<[u8]>>::as_ref Unexecuted instantiation: <rustls::msgs::handshake::DistinguishedName as core::convert::AsRef<[u8]>>::as_ref |
58 | | } |
59 | | |
60 | | impl Codec<'_> for $name { |
61 | 0 | fn encode(&self, bytes: &mut Vec<u8>) { |
62 | 0 | self.0.encode(bytes); |
63 | 0 | } Unexecuted instantiation: <rustls::msgs::handshake::ResponderId as rustls::msgs::codec::Codec>::encode Unexecuted instantiation: <rustls::msgs::handshake::ProtocolName as rustls::msgs::codec::Codec>::encode Unexecuted instantiation: <rustls::msgs::handshake::PresharedKeyBinder as rustls::msgs::codec::Codec>::encode Unexecuted instantiation: <rustls::msgs::handshake::DistinguishedName as rustls::msgs::codec::Codec>::encode Unexecuted instantiation: <rustls::msgs::handshake::ResponderId as rustls::msgs::codec::Codec>::encode Unexecuted instantiation: <rustls::msgs::handshake::ProtocolName as rustls::msgs::codec::Codec>::encode Unexecuted instantiation: <rustls::msgs::handshake::PresharedKeyBinder as rustls::msgs::codec::Codec>::encode Unexecuted instantiation: <rustls::msgs::handshake::DistinguishedName as rustls::msgs::codec::Codec>::encode Unexecuted instantiation: <rustls::msgs::handshake::ResponderId as rustls::msgs::codec::Codec>::encode Unexecuted instantiation: <rustls::msgs::handshake::ProtocolName as rustls::msgs::codec::Codec>::encode Unexecuted instantiation: <rustls::msgs::handshake::PresharedKeyBinder as rustls::msgs::codec::Codec>::encode Unexecuted instantiation: <rustls::msgs::handshake::DistinguishedName as rustls::msgs::codec::Codec>::encode |
64 | | |
65 | 0 | fn read(r: &mut Reader<'_>) -> Result<Self, InvalidMessage> { |
66 | 0 | Ok(Self($inner::read(r)?)) |
67 | 0 | } Unexecuted instantiation: <rustls::msgs::handshake::ResponderId as rustls::msgs::codec::Codec>::read Unexecuted instantiation: <rustls::msgs::handshake::ProtocolName as rustls::msgs::codec::Codec>::read Unexecuted instantiation: <rustls::msgs::handshake::PresharedKeyBinder as rustls::msgs::codec::Codec>::read Unexecuted instantiation: <rustls::msgs::handshake::DistinguishedName as rustls::msgs::codec::Codec>::read Unexecuted instantiation: <rustls::msgs::handshake::ResponderId as rustls::msgs::codec::Codec>::read Unexecuted instantiation: <rustls::msgs::handshake::ProtocolName as rustls::msgs::codec::Codec>::read Unexecuted instantiation: <rustls::msgs::handshake::PresharedKeyBinder as rustls::msgs::codec::Codec>::read Unexecuted instantiation: <rustls::msgs::handshake::DistinguishedName as rustls::msgs::codec::Codec>::read Unexecuted instantiation: <rustls::msgs::handshake::ResponderId as rustls::msgs::codec::Codec>::read Unexecuted instantiation: <rustls::msgs::handshake::ProtocolName as rustls::msgs::codec::Codec>::read Unexecuted instantiation: <rustls::msgs::handshake::PresharedKeyBinder as rustls::msgs::codec::Codec>::read Unexecuted instantiation: <rustls::msgs::handshake::DistinguishedName as rustls::msgs::codec::Codec>::read |
68 | | } |
69 | | } |
70 | | ); |
71 | | |
72 | | #[derive(Clone, Copy, Eq, PartialEq)] |
73 | | pub(crate) struct Random(pub(crate) [u8; 32]); |
74 | | |
75 | | impl fmt::Debug for Random { |
76 | 0 | fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result { |
77 | 0 | super::base::hex(f, &self.0) |
78 | 0 | } Unexecuted instantiation: <rustls::msgs::handshake::Random as core::fmt::Debug>::fmt Unexecuted instantiation: <rustls::msgs::handshake::Random as core::fmt::Debug>::fmt Unexecuted instantiation: <rustls::msgs::handshake::Random as core::fmt::Debug>::fmt |
79 | | } |
80 | | |
81 | | static HELLO_RETRY_REQUEST_RANDOM: Random = Random([ |
82 | | 0xcf, 0x21, 0xad, 0x74, 0xe5, 0x9a, 0x61, 0x11, 0xbe, 0x1d, 0x8c, 0x02, 0x1e, 0x65, 0xb8, 0x91, |
83 | | 0xc2, 0xa2, 0x11, 0x16, 0x7a, 0xbb, 0x8c, 0x5e, 0x07, 0x9e, 0x09, 0xe2, 0xc8, 0xa8, 0x33, 0x9c, |
84 | | ]); |
85 | | |
86 | | static ZERO_RANDOM: Random = Random([0u8; 32]); |
87 | | |
88 | | impl Codec<'_> for Random { |
89 | 0 | fn encode(&self, bytes: &mut Vec<u8>) { |
90 | 0 | bytes.extend_from_slice(&self.0); |
91 | 0 | } Unexecuted instantiation: <rustls::msgs::handshake::Random as rustls::msgs::codec::Codec>::encode Unexecuted instantiation: <rustls::msgs::handshake::Random as rustls::msgs::codec::Codec>::encode Unexecuted instantiation: <rustls::msgs::handshake::Random as rustls::msgs::codec::Codec>::encode |
92 | | |
93 | 0 | fn read(r: &mut Reader<'_>) -> Result<Self, InvalidMessage> { |
94 | 0 | let Some(bytes) = r.take(32) else { |
95 | 0 | return Err(InvalidMessage::MissingData("Random")); |
96 | | }; |
97 | | |
98 | 0 | let mut opaque = [0; 32]; |
99 | 0 | opaque.clone_from_slice(bytes); |
100 | 0 | Ok(Self(opaque)) |
101 | 0 | } Unexecuted instantiation: <rustls::msgs::handshake::Random as rustls::msgs::codec::Codec>::read Unexecuted instantiation: <rustls::msgs::handshake::Random as rustls::msgs::codec::Codec>::read Unexecuted instantiation: <rustls::msgs::handshake::Random as rustls::msgs::codec::Codec>::read |
102 | | } |
103 | | |
104 | | impl Random { |
105 | 0 | pub(crate) fn new(secure_random: &dyn SecureRandom) -> Result<Self, rand::GetRandomFailed> { |
106 | 0 | let mut data = [0u8; 32]; |
107 | 0 | secure_random.fill(&mut data)?; |
108 | 0 | Ok(Self(data)) |
109 | 0 | } Unexecuted instantiation: <rustls::msgs::handshake::Random>::new Unexecuted instantiation: <rustls::msgs::handshake::Random>::new Unexecuted instantiation: <rustls::msgs::handshake::Random>::new |
110 | | } |
111 | | |
112 | | impl From<[u8; 32]> for Random { |
113 | | #[inline] |
114 | 0 | fn from(bytes: [u8; 32]) -> Self { |
115 | 0 | Self(bytes) |
116 | 0 | } Unexecuted instantiation: <rustls::msgs::handshake::Random as core::convert::From<[u8; 32]>>::from Unexecuted instantiation: <rustls::msgs::handshake::Random as core::convert::From<[u8; 32]>>::from Unexecuted instantiation: <rustls::msgs::handshake::Random as core::convert::From<[u8; 32]>>::from |
117 | | } |
118 | | |
119 | | #[derive(Copy, Clone)] |
120 | | pub(crate) struct SessionId { |
121 | | len: usize, |
122 | | data: [u8; 32], |
123 | | } |
124 | | |
125 | | impl fmt::Debug for SessionId { |
126 | 0 | fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result { |
127 | 0 | super::base::hex(f, &self.data[..self.len]) |
128 | 0 | } Unexecuted instantiation: <rustls::msgs::handshake::SessionId as core::fmt::Debug>::fmt Unexecuted instantiation: <rustls::msgs::handshake::SessionId as core::fmt::Debug>::fmt Unexecuted instantiation: <rustls::msgs::handshake::SessionId as core::fmt::Debug>::fmt |
129 | | } |
130 | | |
131 | | impl PartialEq for SessionId { |
132 | 0 | fn eq(&self, other: &Self) -> bool { |
133 | 0 | if self.len != other.len { |
134 | 0 | return false; |
135 | 0 | } |
136 | | |
137 | 0 | let mut diff = 0u8; |
138 | 0 | for i in 0..self.len { |
139 | 0 | diff |= self.data[i] ^ other.data[i]; |
140 | 0 | } |
141 | | |
142 | 0 | diff == 0u8 |
143 | 0 | } Unexecuted instantiation: <rustls::msgs::handshake::SessionId as core::cmp::PartialEq>::eq Unexecuted instantiation: <rustls::msgs::handshake::SessionId as core::cmp::PartialEq>::eq Unexecuted instantiation: <rustls::msgs::handshake::SessionId as core::cmp::PartialEq>::eq |
144 | | } |
145 | | |
146 | | impl Codec<'_> for SessionId { |
147 | 0 | fn encode(&self, bytes: &mut Vec<u8>) { |
148 | 0 | debug_assert!(self.len <= 32); |
149 | 0 | bytes.push(self.len as u8); |
150 | 0 | bytes.extend_from_slice(self.as_ref()); |
151 | 0 | } Unexecuted instantiation: <rustls::msgs::handshake::SessionId as rustls::msgs::codec::Codec>::encode Unexecuted instantiation: <rustls::msgs::handshake::SessionId as rustls::msgs::codec::Codec>::encode Unexecuted instantiation: <rustls::msgs::handshake::SessionId as rustls::msgs::codec::Codec>::encode |
152 | | |
153 | 0 | fn read(r: &mut Reader<'_>) -> Result<Self, InvalidMessage> { |
154 | 0 | let len = u8::read(r)? as usize; |
155 | 0 | if len > 32 { |
156 | 0 | return Err(InvalidMessage::TrailingData("SessionID")); |
157 | 0 | } |
158 | | |
159 | 0 | let Some(bytes) = r.take(len) else { |
160 | 0 | return Err(InvalidMessage::MissingData("SessionID")); |
161 | | }; |
162 | | |
163 | 0 | let mut out = [0u8; 32]; |
164 | 0 | out[..len].clone_from_slice(&bytes[..len]); |
165 | 0 | Ok(Self { data: out, len }) |
166 | 0 | } Unexecuted instantiation: <rustls::msgs::handshake::SessionId as rustls::msgs::codec::Codec>::read Unexecuted instantiation: <rustls::msgs::handshake::SessionId as rustls::msgs::codec::Codec>::read Unexecuted instantiation: <rustls::msgs::handshake::SessionId as rustls::msgs::codec::Codec>::read |
167 | | } |
168 | | |
169 | | impl SessionId { |
170 | 0 | pub(crate) fn random(secure_random: &dyn SecureRandom) -> Result<Self, rand::GetRandomFailed> { |
171 | 0 | let mut data = [0u8; 32]; |
172 | 0 | secure_random.fill(&mut data)?; |
173 | 0 | Ok(Self { data, len: 32 }) |
174 | 0 | } Unexecuted instantiation: <rustls::msgs::handshake::SessionId>::random Unexecuted instantiation: <rustls::msgs::handshake::SessionId>::random Unexecuted instantiation: <rustls::msgs::handshake::SessionId>::random |
175 | | |
176 | 0 | pub(crate) fn empty() -> Self { |
177 | 0 | Self { |
178 | 0 | data: [0u8; 32], |
179 | 0 | len: 0, |
180 | 0 | } |
181 | 0 | } Unexecuted instantiation: <rustls::msgs::handshake::SessionId>::empty Unexecuted instantiation: <rustls::msgs::handshake::SessionId>::empty Unexecuted instantiation: <rustls::msgs::handshake::SessionId>::empty |
182 | | |
183 | | #[cfg(feature = "tls12")] |
184 | | pub(crate) fn is_empty(&self) -> bool { |
185 | | self.len == 0 |
186 | | } |
187 | | } |
188 | | |
189 | | impl AsRef<[u8]> for SessionId { |
190 | 0 | fn as_ref(&self) -> &[u8] { |
191 | 0 | &self.data[..self.len] |
192 | 0 | } Unexecuted instantiation: <rustls::msgs::handshake::SessionId as core::convert::AsRef<[u8]>>::as_ref Unexecuted instantiation: <rustls::msgs::handshake::SessionId as core::convert::AsRef<[u8]>>::as_ref Unexecuted instantiation: <rustls::msgs::handshake::SessionId as core::convert::AsRef<[u8]>>::as_ref |
193 | | } |
194 | | |
195 | | #[derive(Clone, Debug, PartialEq)] |
196 | | pub struct UnknownExtension { |
197 | | pub(crate) typ: ExtensionType, |
198 | | pub(crate) payload: Payload<'static>, |
199 | | } |
200 | | |
201 | | impl UnknownExtension { |
202 | 0 | fn encode(&self, bytes: &mut Vec<u8>) { |
203 | 0 | self.payload.encode(bytes); |
204 | 0 | } Unexecuted instantiation: <rustls::msgs::handshake::UnknownExtension>::encode Unexecuted instantiation: <rustls::msgs::handshake::UnknownExtension>::encode Unexecuted instantiation: <rustls::msgs::handshake::UnknownExtension>::encode |
205 | | |
206 | 0 | fn read(typ: ExtensionType, r: &mut Reader<'_>) -> Self { |
207 | 0 | let payload = Payload::read(r).into_owned(); |
208 | 0 | Self { typ, payload } |
209 | 0 | } Unexecuted instantiation: <rustls::msgs::handshake::UnknownExtension>::read Unexecuted instantiation: <rustls::msgs::handshake::UnknownExtension>::read Unexecuted instantiation: <rustls::msgs::handshake::UnknownExtension>::read |
210 | | } |
211 | | |
212 | | #[derive(Clone, Copy, Debug)] |
213 | | pub(crate) struct SupportedEcPointFormats { |
214 | | pub(crate) uncompressed: bool, |
215 | | } |
216 | | |
217 | | impl Codec<'_> for SupportedEcPointFormats { |
218 | 0 | fn encode(&self, bytes: &mut Vec<u8>) { |
219 | 0 | let inner = LengthPrefixedBuffer::new(ECPointFormat::SIZE_LEN, bytes); |
220 | | |
221 | 0 | if self.uncompressed { |
222 | 0 | ECPointFormat::Uncompressed.encode(inner.buf); |
223 | 0 | } |
224 | 0 | } Unexecuted instantiation: <rustls::msgs::handshake::SupportedEcPointFormats as rustls::msgs::codec::Codec>::encode Unexecuted instantiation: <rustls::msgs::handshake::SupportedEcPointFormats as rustls::msgs::codec::Codec>::encode Unexecuted instantiation: <rustls::msgs::handshake::SupportedEcPointFormats as rustls::msgs::codec::Codec>::encode |
225 | | |
226 | 0 | fn read(r: &mut Reader<'_>) -> Result<Self, InvalidMessage> { |
227 | 0 | let mut uncompressed = false; |
228 | | |
229 | 0 | for pf in TlsListIter::<ECPointFormat>::new(r)? { |
230 | 0 | if let ECPointFormat::Uncompressed = pf? { |
231 | 0 | uncompressed = true; |
232 | 0 | } |
233 | | } |
234 | | |
235 | 0 | Ok(Self { uncompressed }) |
236 | 0 | } Unexecuted instantiation: <rustls::msgs::handshake::SupportedEcPointFormats as rustls::msgs::codec::Codec>::read Unexecuted instantiation: <rustls::msgs::handshake::SupportedEcPointFormats as rustls::msgs::codec::Codec>::read Unexecuted instantiation: <rustls::msgs::handshake::SupportedEcPointFormats as rustls::msgs::codec::Codec>::read |
237 | | } |
238 | | |
239 | | impl Default for SupportedEcPointFormats { |
240 | 0 | fn default() -> Self { |
241 | 0 | Self { uncompressed: true } |
242 | 0 | } Unexecuted instantiation: <rustls::msgs::handshake::SupportedEcPointFormats as core::default::Default>::default Unexecuted instantiation: <rustls::msgs::handshake::SupportedEcPointFormats as core::default::Default>::default Unexecuted instantiation: <rustls::msgs::handshake::SupportedEcPointFormats as core::default::Default>::default |
243 | | } |
244 | | |
245 | | /// RFC8422: `ECPointFormat ec_point_format_list<1..2^8-1>` |
246 | | impl TlsListElement for ECPointFormat { |
247 | | const SIZE_LEN: ListLength = ListLength::NonZeroU8 { |
248 | | empty_error: InvalidMessage::IllegalEmptyList("ECPointFormats"), |
249 | | }; |
250 | | } |
251 | | |
252 | | /// RFC8422: `NamedCurve named_curve_list<2..2^16-1>` |
253 | | impl TlsListElement for NamedGroup { |
254 | | const SIZE_LEN: ListLength = ListLength::NonZeroU16 { |
255 | | empty_error: InvalidMessage::IllegalEmptyList("NamedGroups"), |
256 | | }; |
257 | | } |
258 | | |
259 | | /// RFC8446: `SignatureScheme supported_signature_algorithms<2..2^16-2>;` |
260 | | impl TlsListElement for SignatureScheme { |
261 | | const SIZE_LEN: ListLength = ListLength::NonZeroU16 { |
262 | | empty_error: InvalidMessage::NoSignatureSchemes, |
263 | | }; |
264 | | } |
265 | | |
266 | | #[derive(Clone, Debug)] |
267 | | pub(crate) enum ServerNamePayload<'a> { |
268 | | /// A successfully decoded value: |
269 | | SingleDnsName(DnsName<'a>), |
270 | | |
271 | | /// A DNS name which was actually an IP address |
272 | | IpAddress, |
273 | | |
274 | | /// A successfully decoded, but syntactically-invalid value. |
275 | | Invalid, |
276 | | } |
277 | | |
278 | | impl ServerNamePayload<'_> { |
279 | 0 | fn into_owned(self) -> ServerNamePayload<'static> { |
280 | 0 | match self { |
281 | 0 | Self::SingleDnsName(d) => ServerNamePayload::SingleDnsName(d.to_owned()), |
282 | 0 | Self::IpAddress => ServerNamePayload::IpAddress, |
283 | 0 | Self::Invalid => ServerNamePayload::Invalid, |
284 | | } |
285 | 0 | } Unexecuted instantiation: <rustls::msgs::handshake::ServerNamePayload>::into_owned Unexecuted instantiation: <rustls::msgs::handshake::ServerNamePayload>::into_owned Unexecuted instantiation: <rustls::msgs::handshake::ServerNamePayload>::into_owned |
286 | | |
287 | | /// RFC6066: `ServerName server_name_list<1..2^16-1>` |
288 | | const SIZE_LEN: ListLength = ListLength::NonZeroU16 { |
289 | | empty_error: InvalidMessage::IllegalEmptyList("ServerNames"), |
290 | | }; |
291 | | } |
292 | | |
293 | | /// Simplified encoding/decoding for a `ServerName` extension payload to/from `DnsName` |
294 | | /// |
295 | | /// This is possible because: |
296 | | /// |
297 | | /// - the spec (RFC6066) disallows multiple names for a given name type |
298 | | /// - name types other than ServerNameType::HostName are not defined, and they and |
299 | | /// any data that follows them cannot be skipped over. |
300 | | impl<'a> Codec<'a> for ServerNamePayload<'a> { |
301 | 0 | fn encode(&self, bytes: &mut Vec<u8>) { |
302 | 0 | let server_name_list = LengthPrefixedBuffer::new(Self::SIZE_LEN, bytes); |
303 | | |
304 | 0 | let ServerNamePayload::SingleDnsName(dns_name) = self else { |
305 | 0 | return; |
306 | | }; |
307 | | |
308 | 0 | ServerNameType::HostName.encode(server_name_list.buf); |
309 | 0 | let name_slice = dns_name.as_ref().as_bytes(); |
310 | 0 | (name_slice.len() as u16).encode(server_name_list.buf); |
311 | 0 | server_name_list |
312 | 0 | .buf |
313 | 0 | .extend_from_slice(name_slice); |
314 | 0 | } Unexecuted instantiation: <rustls::msgs::handshake::ServerNamePayload as rustls::msgs::codec::Codec>::encode Unexecuted instantiation: <rustls::msgs::handshake::ServerNamePayload as rustls::msgs::codec::Codec>::encode Unexecuted instantiation: <rustls::msgs::handshake::ServerNamePayload as rustls::msgs::codec::Codec>::encode |
315 | | |
316 | 0 | fn read(r: &mut Reader<'a>) -> Result<Self, InvalidMessage> { |
317 | 0 | let mut found = None; |
318 | | |
319 | 0 | let len = Self::SIZE_LEN.read(r)?; |
320 | 0 | let mut sub = r.sub(len)?; |
321 | | |
322 | 0 | while sub.any_left() { |
323 | 0 | let typ = ServerNameType::read(&mut sub)?; |
324 | | |
325 | 0 | let payload = match typ { |
326 | 0 | ServerNameType::HostName => HostNamePayload::read(&mut sub)?, |
327 | | _ => { |
328 | | // Consume remainder of extension bytes. Since the length of the item |
329 | | // is an unknown encoding, we cannot continue. |
330 | 0 | sub.rest(); |
331 | 0 | break; |
332 | | } |
333 | | }; |
334 | | |
335 | | // "The ServerNameList MUST NOT contain more than one name of |
336 | | // the same name_type." - RFC6066 |
337 | 0 | if found.is_some() { |
338 | 0 | warn!("Illegal SNI extension: duplicate host_name received"); |
339 | 0 | return Err(InvalidMessage::InvalidServerName); |
340 | 0 | } |
341 | | |
342 | 0 | found = match payload { |
343 | 0 | HostNamePayload::HostName(dns_name) => { |
344 | 0 | Some(Self::SingleDnsName(dns_name.to_owned())) |
345 | | } |
346 | | |
347 | 0 | HostNamePayload::IpAddress(_invalid) => { |
348 | 0 | warn!( |
349 | | "Illegal SNI extension: ignoring IP address presented as hostname ({_invalid:?})" |
350 | | ); |
351 | 0 | Some(Self::IpAddress) |
352 | | } |
353 | | |
354 | 0 | HostNamePayload::Invalid(_invalid) => { |
355 | 0 | warn!( |
356 | | "Illegal SNI hostname received {:?}", |
357 | | String::from_utf8_lossy(&_invalid.0) |
358 | | ); |
359 | 0 | Some(Self::Invalid) |
360 | | } |
361 | | }; |
362 | | } |
363 | | |
364 | 0 | Ok(found.unwrap_or(Self::Invalid)) |
365 | 0 | } Unexecuted instantiation: <rustls::msgs::handshake::ServerNamePayload as rustls::msgs::codec::Codec>::read Unexecuted instantiation: <rustls::msgs::handshake::ServerNamePayload as rustls::msgs::codec::Codec>::read Unexecuted instantiation: <rustls::msgs::handshake::ServerNamePayload as rustls::msgs::codec::Codec>::read |
366 | | } |
367 | | |
368 | | impl<'a> From<&DnsName<'a>> for ServerNamePayload<'static> { |
369 | 0 | fn from(value: &DnsName<'a>) -> Self { |
370 | 0 | Self::SingleDnsName(trim_hostname_trailing_dot_for_sni(value)) |
371 | 0 | } Unexecuted instantiation: <rustls::msgs::handshake::ServerNamePayload as core::convert::From<&rustls_pki_types::server_name::DnsName>>::from Unexecuted instantiation: <rustls::msgs::handshake::ServerNamePayload as core::convert::From<&rustls_pki_types::server_name::DnsName>>::from Unexecuted instantiation: <rustls::msgs::handshake::ServerNamePayload as core::convert::From<&rustls_pki_types::server_name::DnsName>>::from |
372 | | } |
373 | | |
374 | | #[derive(Clone, Debug)] |
375 | | pub(crate) enum HostNamePayload { |
376 | | HostName(DnsName<'static>), |
377 | | IpAddress(PayloadU16<NonEmpty>), |
378 | | Invalid(PayloadU16<NonEmpty>), |
379 | | } |
380 | | |
381 | | impl HostNamePayload { |
382 | 0 | fn read(r: &mut Reader<'_>) -> Result<Self, InvalidMessage> { |
383 | | use pki_types::ServerName; |
384 | 0 | let raw = PayloadU16::<NonEmpty>::read(r)?; |
385 | | |
386 | 0 | match ServerName::try_from(raw.0.as_slice()) { |
387 | 0 | Ok(ServerName::DnsName(d)) => Ok(Self::HostName(d.to_owned())), |
388 | 0 | Ok(ServerName::IpAddress(_)) => Ok(Self::IpAddress(raw)), |
389 | 0 | Ok(_) | Err(_) => Ok(Self::Invalid(raw)), |
390 | | } |
391 | 0 | } Unexecuted instantiation: <rustls::msgs::handshake::HostNamePayload>::read Unexecuted instantiation: <rustls::msgs::handshake::HostNamePayload>::read Unexecuted instantiation: <rustls::msgs::handshake::HostNamePayload>::read |
392 | | } |
393 | | |
394 | | wrapped_payload!( |
395 | | /// RFC7301: `opaque ProtocolName<1..2^8-1>;` |
396 | | pub(crate) struct ProtocolName, PayloadU8<NonEmpty>, |
397 | | ); |
398 | | |
399 | | impl PartialEq for ProtocolName { |
400 | 0 | fn eq(&self, other: &Self) -> bool { |
401 | 0 | self.0 == other.0 |
402 | 0 | } Unexecuted instantiation: <rustls::msgs::handshake::ProtocolName as core::cmp::PartialEq>::eq Unexecuted instantiation: <rustls::msgs::handshake::ProtocolName as core::cmp::PartialEq>::eq Unexecuted instantiation: <rustls::msgs::handshake::ProtocolName as core::cmp::PartialEq>::eq |
403 | | } |
404 | | |
405 | | impl Deref for ProtocolName { |
406 | | type Target = [u8]; |
407 | | |
408 | 0 | fn deref(&self) -> &Self::Target { |
409 | 0 | self.as_ref() |
410 | 0 | } Unexecuted instantiation: <rustls::msgs::handshake::ProtocolName as core::ops::deref::Deref>::deref Unexecuted instantiation: <rustls::msgs::handshake::ProtocolName as core::ops::deref::Deref>::deref Unexecuted instantiation: <rustls::msgs::handshake::ProtocolName as core::ops::deref::Deref>::deref |
411 | | } |
412 | | |
413 | | /// RFC7301: `ProtocolName protocol_name_list<2..2^16-1>` |
414 | | impl TlsListElement for ProtocolName { |
415 | | const SIZE_LEN: ListLength = ListLength::NonZeroU16 { |
416 | | empty_error: InvalidMessage::IllegalEmptyList("ProtocolNames"), |
417 | | }; |
418 | | } |
419 | | |
420 | | /// RFC7301 encodes a single protocol name as `Vec<ProtocolName>` |
421 | | #[derive(Clone, Debug)] |
422 | | pub(crate) struct SingleProtocolName(ProtocolName); |
423 | | |
424 | | impl SingleProtocolName { |
425 | 0 | pub(crate) fn new(single: ProtocolName) -> Self { |
426 | 0 | Self(single) |
427 | 0 | } Unexecuted instantiation: <rustls::msgs::handshake::SingleProtocolName>::new Unexecuted instantiation: <rustls::msgs::handshake::SingleProtocolName>::new Unexecuted instantiation: <rustls::msgs::handshake::SingleProtocolName>::new |
428 | | |
429 | | const SIZE_LEN: ListLength = ListLength::NonZeroU16 { |
430 | | empty_error: InvalidMessage::IllegalEmptyList("ProtocolNames"), |
431 | | }; |
432 | | } |
433 | | |
434 | | impl Codec<'_> for SingleProtocolName { |
435 | 0 | fn encode(&self, bytes: &mut Vec<u8>) { |
436 | 0 | let body = LengthPrefixedBuffer::new(Self::SIZE_LEN, bytes); |
437 | 0 | self.0.encode(body.buf); |
438 | 0 | } Unexecuted instantiation: <rustls::msgs::handshake::SingleProtocolName as rustls::msgs::codec::Codec>::encode Unexecuted instantiation: <rustls::msgs::handshake::SingleProtocolName as rustls::msgs::codec::Codec>::encode Unexecuted instantiation: <rustls::msgs::handshake::SingleProtocolName as rustls::msgs::codec::Codec>::encode |
439 | | |
440 | 0 | fn read(reader: &mut Reader<'_>) -> Result<Self, InvalidMessage> { |
441 | 0 | let len = Self::SIZE_LEN.read(reader)?; |
442 | 0 | let mut sub = reader.sub(len)?; |
443 | | |
444 | 0 | let item = ProtocolName::read(&mut sub)?; |
445 | | |
446 | 0 | if sub.any_left() { |
447 | 0 | Err(InvalidMessage::TrailingData("SingleProtocolName")) |
448 | | } else { |
449 | 0 | Ok(Self(item)) |
450 | | } |
451 | 0 | } Unexecuted instantiation: <rustls::msgs::handshake::SingleProtocolName as rustls::msgs::codec::Codec>::read Unexecuted instantiation: <rustls::msgs::handshake::SingleProtocolName as rustls::msgs::codec::Codec>::read Unexecuted instantiation: <rustls::msgs::handshake::SingleProtocolName as rustls::msgs::codec::Codec>::read |
452 | | } |
453 | | |
454 | | impl AsRef<ProtocolName> for SingleProtocolName { |
455 | 0 | fn as_ref(&self) -> &ProtocolName { |
456 | 0 | &self.0 |
457 | 0 | } Unexecuted instantiation: <rustls::msgs::handshake::SingleProtocolName as core::convert::AsRef<rustls::msgs::handshake::ProtocolName>>::as_ref Unexecuted instantiation: <rustls::msgs::handshake::SingleProtocolName as core::convert::AsRef<rustls::msgs::handshake::ProtocolName>>::as_ref Unexecuted instantiation: <rustls::msgs::handshake::SingleProtocolName as core::convert::AsRef<rustls::msgs::handshake::ProtocolName>>::as_ref |
458 | | } |
459 | | |
460 | | // --- TLS 1.3 Key shares --- |
461 | | #[derive(Clone, Debug)] |
462 | | pub(crate) struct KeyShareEntry { |
463 | | pub(crate) group: NamedGroup, |
464 | | /// RFC8446: `opaque key_exchange<1..2^16-1>;` |
465 | | pub(crate) payload: PayloadU16<NonEmpty>, |
466 | | } |
467 | | |
468 | | impl KeyShareEntry { |
469 | 0 | pub(crate) fn new(group: NamedGroup, payload: impl Into<Vec<u8>>) -> Self { |
470 | 0 | Self { |
471 | 0 | group, |
472 | 0 | payload: PayloadU16::new(payload.into()), |
473 | 0 | } |
474 | 0 | } Unexecuted instantiation: <rustls::msgs::handshake::KeyShareEntry>::new::<alloc::vec::Vec<u8>> Unexecuted instantiation: <rustls::msgs::handshake::KeyShareEntry>::new::<&[u8]> Unexecuted instantiation: <rustls::msgs::handshake::KeyShareEntry>::new::<alloc::vec::Vec<u8>> Unexecuted instantiation: <rustls::msgs::handshake::KeyShareEntry>::new::<&[u8]> Unexecuted instantiation: <rustls::msgs::handshake::KeyShareEntry>::new::<alloc::vec::Vec<u8>> Unexecuted instantiation: <rustls::msgs::handshake::KeyShareEntry>::new::<&[u8]> |
475 | | } |
476 | | |
477 | | impl Codec<'_> for KeyShareEntry { |
478 | 0 | fn encode(&self, bytes: &mut Vec<u8>) { |
479 | 0 | self.group.encode(bytes); |
480 | 0 | self.payload.encode(bytes); |
481 | 0 | } Unexecuted instantiation: <rustls::msgs::handshake::KeyShareEntry as rustls::msgs::codec::Codec>::encode Unexecuted instantiation: <rustls::msgs::handshake::KeyShareEntry as rustls::msgs::codec::Codec>::encode Unexecuted instantiation: <rustls::msgs::handshake::KeyShareEntry as rustls::msgs::codec::Codec>::encode |
482 | | |
483 | 0 | fn read(r: &mut Reader<'_>) -> Result<Self, InvalidMessage> { |
484 | 0 | let group = NamedGroup::read(r)?; |
485 | 0 | let payload = PayloadU16::read(r)?; |
486 | | |
487 | 0 | Ok(Self { group, payload }) |
488 | 0 | } Unexecuted instantiation: <rustls::msgs::handshake::KeyShareEntry as rustls::msgs::codec::Codec>::read Unexecuted instantiation: <rustls::msgs::handshake::KeyShareEntry as rustls::msgs::codec::Codec>::read Unexecuted instantiation: <rustls::msgs::handshake::KeyShareEntry as rustls::msgs::codec::Codec>::read |
489 | | } |
490 | | |
491 | | // --- TLS 1.3 PresharedKey offers --- |
492 | | #[derive(Clone, Debug)] |
493 | | pub(crate) struct PresharedKeyIdentity { |
494 | | /// RFC8446: `opaque identity<1..2^16-1>;` |
495 | | pub(crate) identity: PayloadU16<NonEmpty>, |
496 | | pub(crate) obfuscated_ticket_age: u32, |
497 | | } |
498 | | |
499 | | impl PresharedKeyIdentity { |
500 | 0 | pub(crate) fn new(id: Vec<u8>, age: u32) -> Self { |
501 | 0 | Self { |
502 | 0 | identity: PayloadU16::new(id), |
503 | 0 | obfuscated_ticket_age: age, |
504 | 0 | } |
505 | 0 | } Unexecuted instantiation: <rustls::msgs::handshake::PresharedKeyIdentity>::new Unexecuted instantiation: <rustls::msgs::handshake::PresharedKeyIdentity>::new Unexecuted instantiation: <rustls::msgs::handshake::PresharedKeyIdentity>::new |
506 | | } |
507 | | |
508 | | impl Codec<'_> for PresharedKeyIdentity { |
509 | 0 | fn encode(&self, bytes: &mut Vec<u8>) { |
510 | 0 | self.identity.encode(bytes); |
511 | 0 | self.obfuscated_ticket_age.encode(bytes); |
512 | 0 | } Unexecuted instantiation: <rustls::msgs::handshake::PresharedKeyIdentity as rustls::msgs::codec::Codec>::encode Unexecuted instantiation: <rustls::msgs::handshake::PresharedKeyIdentity as rustls::msgs::codec::Codec>::encode Unexecuted instantiation: <rustls::msgs::handshake::PresharedKeyIdentity as rustls::msgs::codec::Codec>::encode |
513 | | |
514 | 0 | fn read(r: &mut Reader<'_>) -> Result<Self, InvalidMessage> { |
515 | | Ok(Self { |
516 | 0 | identity: PayloadU16::read(r)?, |
517 | 0 | obfuscated_ticket_age: u32::read(r)?, |
518 | | }) |
519 | 0 | } Unexecuted instantiation: <rustls::msgs::handshake::PresharedKeyIdentity as rustls::msgs::codec::Codec>::read Unexecuted instantiation: <rustls::msgs::handshake::PresharedKeyIdentity as rustls::msgs::codec::Codec>::read Unexecuted instantiation: <rustls::msgs::handshake::PresharedKeyIdentity as rustls::msgs::codec::Codec>::read |
520 | | } |
521 | | |
522 | | /// RFC8446: `PskIdentity identities<7..2^16-1>;` |
523 | | impl TlsListElement for PresharedKeyIdentity { |
524 | | const SIZE_LEN: ListLength = ListLength::NonZeroU16 { |
525 | | empty_error: InvalidMessage::IllegalEmptyList("PskIdentities"), |
526 | | }; |
527 | | } |
528 | | |
529 | | wrapped_payload!( |
530 | | /// RFC8446: `opaque PskBinderEntry<32..255>;` |
531 | | pub(crate) struct PresharedKeyBinder, PayloadU8<NonEmpty>, |
532 | | ); |
533 | | |
534 | | /// RFC8446: `PskBinderEntry binders<33..2^16-1>;` |
535 | | impl TlsListElement for PresharedKeyBinder { |
536 | | const SIZE_LEN: ListLength = ListLength::NonZeroU16 { |
537 | | empty_error: InvalidMessage::IllegalEmptyList("PskBinders"), |
538 | | }; |
539 | | } |
540 | | |
541 | | #[derive(Clone, Debug)] |
542 | | pub(crate) struct PresharedKeyOffer { |
543 | | pub(crate) identities: Vec<PresharedKeyIdentity>, |
544 | | pub(crate) binders: Vec<PresharedKeyBinder>, |
545 | | } |
546 | | |
547 | | impl PresharedKeyOffer { |
548 | | /// Make a new one with one entry. |
549 | 0 | pub(crate) fn new(id: PresharedKeyIdentity, binder: Vec<u8>) -> Self { |
550 | 0 | Self { |
551 | 0 | identities: vec![id], |
552 | 0 | binders: vec![PresharedKeyBinder::from(binder)], |
553 | 0 | } |
554 | 0 | } Unexecuted instantiation: <rustls::msgs::handshake::PresharedKeyOffer>::new Unexecuted instantiation: <rustls::msgs::handshake::PresharedKeyOffer>::new Unexecuted instantiation: <rustls::msgs::handshake::PresharedKeyOffer>::new |
555 | | } |
556 | | |
557 | | impl Codec<'_> for PresharedKeyOffer { |
558 | 0 | fn encode(&self, bytes: &mut Vec<u8>) { |
559 | 0 | self.identities.encode(bytes); |
560 | 0 | self.binders.encode(bytes); |
561 | 0 | } Unexecuted instantiation: <rustls::msgs::handshake::PresharedKeyOffer as rustls::msgs::codec::Codec>::encode Unexecuted instantiation: <rustls::msgs::handshake::PresharedKeyOffer as rustls::msgs::codec::Codec>::encode Unexecuted instantiation: <rustls::msgs::handshake::PresharedKeyOffer as rustls::msgs::codec::Codec>::encode |
562 | | |
563 | 0 | fn read(r: &mut Reader<'_>) -> Result<Self, InvalidMessage> { |
564 | | Ok(Self { |
565 | 0 | identities: Vec::read(r)?, |
566 | 0 | binders: Vec::read(r)?, |
567 | | }) |
568 | 0 | } Unexecuted instantiation: <rustls::msgs::handshake::PresharedKeyOffer as rustls::msgs::codec::Codec>::read Unexecuted instantiation: <rustls::msgs::handshake::PresharedKeyOffer as rustls::msgs::codec::Codec>::read Unexecuted instantiation: <rustls::msgs::handshake::PresharedKeyOffer as rustls::msgs::codec::Codec>::read |
569 | | } |
570 | | |
571 | | // --- RFC6066 certificate status request --- |
572 | | wrapped_payload!(pub(crate) struct ResponderId, PayloadU16,); |
573 | | |
574 | | /// RFC6066: `ResponderID responder_id_list<0..2^16-1>;` |
575 | | impl TlsListElement for ResponderId { |
576 | | const SIZE_LEN: ListLength = ListLength::U16; |
577 | | } |
578 | | |
579 | | #[derive(Clone, Debug)] |
580 | | pub(crate) struct OcspCertificateStatusRequest { |
581 | | pub(crate) responder_ids: Vec<ResponderId>, |
582 | | pub(crate) extensions: PayloadU16, |
583 | | } |
584 | | |
585 | | impl Codec<'_> for OcspCertificateStatusRequest { |
586 | 0 | fn encode(&self, bytes: &mut Vec<u8>) { |
587 | 0 | CertificateStatusType::OCSP.encode(bytes); |
588 | 0 | self.responder_ids.encode(bytes); |
589 | 0 | self.extensions.encode(bytes); |
590 | 0 | } Unexecuted instantiation: <rustls::msgs::handshake::OcspCertificateStatusRequest as rustls::msgs::codec::Codec>::encode Unexecuted instantiation: <rustls::msgs::handshake::OcspCertificateStatusRequest as rustls::msgs::codec::Codec>::encode Unexecuted instantiation: <rustls::msgs::handshake::OcspCertificateStatusRequest as rustls::msgs::codec::Codec>::encode |
591 | | |
592 | 0 | fn read(r: &mut Reader<'_>) -> Result<Self, InvalidMessage> { |
593 | | Ok(Self { |
594 | 0 | responder_ids: Vec::read(r)?, |
595 | 0 | extensions: PayloadU16::read(r)?, |
596 | | }) |
597 | 0 | } Unexecuted instantiation: <rustls::msgs::handshake::OcspCertificateStatusRequest as rustls::msgs::codec::Codec>::read Unexecuted instantiation: <rustls::msgs::handshake::OcspCertificateStatusRequest as rustls::msgs::codec::Codec>::read Unexecuted instantiation: <rustls::msgs::handshake::OcspCertificateStatusRequest as rustls::msgs::codec::Codec>::read |
598 | | } |
599 | | |
600 | | #[derive(Clone, Debug)] |
601 | | pub(crate) enum CertificateStatusRequest { |
602 | | Ocsp(OcspCertificateStatusRequest), |
603 | | Unknown((CertificateStatusType, Payload<'static>)), |
604 | | } |
605 | | |
606 | | impl Codec<'_> for CertificateStatusRequest { |
607 | 0 | fn encode(&self, bytes: &mut Vec<u8>) { |
608 | 0 | match self { |
609 | 0 | Self::Ocsp(r) => r.encode(bytes), |
610 | 0 | Self::Unknown((typ, payload)) => { |
611 | 0 | typ.encode(bytes); |
612 | 0 | payload.encode(bytes); |
613 | 0 | } |
614 | | } |
615 | 0 | } Unexecuted instantiation: <rustls::msgs::handshake::CertificateStatusRequest as rustls::msgs::codec::Codec>::encode Unexecuted instantiation: <rustls::msgs::handshake::CertificateStatusRequest as rustls::msgs::codec::Codec>::encode Unexecuted instantiation: <rustls::msgs::handshake::CertificateStatusRequest as rustls::msgs::codec::Codec>::encode |
616 | | |
617 | 0 | fn read(r: &mut Reader<'_>) -> Result<Self, InvalidMessage> { |
618 | 0 | let typ = CertificateStatusType::read(r)?; |
619 | | |
620 | 0 | match typ { |
621 | | CertificateStatusType::OCSP => { |
622 | 0 | let ocsp_req = OcspCertificateStatusRequest::read(r)?; |
623 | 0 | Ok(Self::Ocsp(ocsp_req)) |
624 | | } |
625 | | _ => { |
626 | 0 | let data = Payload::read(r).into_owned(); |
627 | 0 | Ok(Self::Unknown((typ, data))) |
628 | | } |
629 | | } |
630 | 0 | } Unexecuted instantiation: <rustls::msgs::handshake::CertificateStatusRequest as rustls::msgs::codec::Codec>::read Unexecuted instantiation: <rustls::msgs::handshake::CertificateStatusRequest as rustls::msgs::codec::Codec>::read Unexecuted instantiation: <rustls::msgs::handshake::CertificateStatusRequest as rustls::msgs::codec::Codec>::read |
631 | | } |
632 | | |
633 | | impl CertificateStatusRequest { |
634 | 0 | pub(crate) fn build_ocsp() -> Self { |
635 | 0 | let ocsp = OcspCertificateStatusRequest { |
636 | 0 | responder_ids: Vec::new(), |
637 | 0 | extensions: PayloadU16::empty(), |
638 | 0 | }; |
639 | 0 | Self::Ocsp(ocsp) |
640 | 0 | } Unexecuted instantiation: <rustls::msgs::handshake::CertificateStatusRequest>::build_ocsp Unexecuted instantiation: <rustls::msgs::handshake::CertificateStatusRequest>::build_ocsp Unexecuted instantiation: <rustls::msgs::handshake::CertificateStatusRequest>::build_ocsp |
641 | | } |
642 | | |
643 | | // --- |
644 | | |
645 | | /// RFC8446: `PskKeyExchangeMode ke_modes<1..255>;` |
646 | | #[derive(Clone, Copy, Debug, Default)] |
647 | | pub(crate) struct PskKeyExchangeModes { |
648 | | pub(crate) psk_dhe: bool, |
649 | | pub(crate) psk: bool, |
650 | | } |
651 | | |
652 | | impl Codec<'_> for PskKeyExchangeModes { |
653 | 0 | fn encode(&self, bytes: &mut Vec<u8>) { |
654 | 0 | let inner = LengthPrefixedBuffer::new(PskKeyExchangeMode::SIZE_LEN, bytes); |
655 | 0 | if self.psk_dhe { |
656 | 0 | PskKeyExchangeMode::PSK_DHE_KE.encode(inner.buf); |
657 | 0 | } |
658 | 0 | if self.psk { |
659 | 0 | PskKeyExchangeMode::PSK_KE.encode(inner.buf); |
660 | 0 | } |
661 | 0 | } Unexecuted instantiation: <rustls::msgs::handshake::PskKeyExchangeModes as rustls::msgs::codec::Codec>::encode Unexecuted instantiation: <rustls::msgs::handshake::PskKeyExchangeModes as rustls::msgs::codec::Codec>::encode Unexecuted instantiation: <rustls::msgs::handshake::PskKeyExchangeModes as rustls::msgs::codec::Codec>::encode |
662 | | |
663 | 0 | fn read(reader: &mut Reader<'_>) -> Result<Self, InvalidMessage> { |
664 | 0 | let mut psk_dhe = false; |
665 | 0 | let mut psk = false; |
666 | | |
667 | 0 | for ke in TlsListIter::<PskKeyExchangeMode>::new(reader)? { |
668 | 0 | match ke? { |
669 | 0 | PskKeyExchangeMode::PSK_DHE_KE => psk_dhe = true, |
670 | 0 | PskKeyExchangeMode::PSK_KE => psk = true, |
671 | 0 | _ => continue, |
672 | | }; |
673 | | } |
674 | | |
675 | 0 | Ok(Self { psk_dhe, psk }) |
676 | 0 | } Unexecuted instantiation: <rustls::msgs::handshake::PskKeyExchangeModes as rustls::msgs::codec::Codec>::read Unexecuted instantiation: <rustls::msgs::handshake::PskKeyExchangeModes as rustls::msgs::codec::Codec>::read Unexecuted instantiation: <rustls::msgs::handshake::PskKeyExchangeModes as rustls::msgs::codec::Codec>::read |
677 | | } |
678 | | |
679 | | impl TlsListElement for PskKeyExchangeMode { |
680 | | const SIZE_LEN: ListLength = ListLength::NonZeroU8 { |
681 | | empty_error: InvalidMessage::IllegalEmptyList("PskKeyExchangeModes"), |
682 | | }; |
683 | | } |
684 | | |
685 | | /// RFC8446: `KeyShareEntry client_shares<0..2^16-1>;` |
686 | | impl TlsListElement for KeyShareEntry { |
687 | | const SIZE_LEN: ListLength = ListLength::U16; |
688 | | } |
689 | | |
690 | | /// The body of the `SupportedVersions` extension when it appears in a |
691 | | /// `ClientHello` |
692 | | /// |
693 | | /// This is documented as a preference-order vector, but we (as a server) |
694 | | /// ignore the preference of the client. |
695 | | /// |
696 | | /// RFC8446: `ProtocolVersion versions<2..254>;` |
697 | | #[derive(Clone, Copy, Debug, Default)] |
698 | | pub(crate) struct SupportedProtocolVersions { |
699 | | pub(crate) tls13: bool, |
700 | | pub(crate) tls12: bool, |
701 | | } |
702 | | |
703 | | impl SupportedProtocolVersions { |
704 | | /// Return true if `filter` returns true for any enabled version. |
705 | 0 | pub(crate) fn any(&self, filter: impl Fn(ProtocolVersion) -> bool) -> bool { |
706 | 0 | if self.tls13 && filter(ProtocolVersion::TLSv1_3) { |
707 | 0 | return true; |
708 | 0 | } |
709 | 0 | if self.tls12 && filter(ProtocolVersion::TLSv1_2) { |
710 | 0 | return true; |
711 | 0 | } |
712 | 0 | false |
713 | 0 | } Unexecuted instantiation: <rustls::msgs::handshake::SupportedProtocolVersions>::any::<rustls::client::hs::emit_client_hello_for_retry::{closure#0}::{closure#0}>Unexecuted instantiation: <rustls::msgs::handshake::SupportedProtocolVersions>::any::<rustls::client::hs::emit_client_hello_for_retry::{closure#13}>Unexecuted instantiation: <rustls::msgs::handshake::SupportedProtocolVersions>::any::<rustls::client::hs::emit_client_hello_for_retry::{closure#0}::{closure#0}>Unexecuted instantiation: <rustls::msgs::handshake::SupportedProtocolVersions>::any::<rustls::client::hs::emit_client_hello_for_retry::{closure#13}>Unexecuted instantiation: <rustls::msgs::handshake::SupportedProtocolVersions>::any::<rustls::client::hs::emit_client_hello_for_retry::{closure#0}::{closure#0}>Unexecuted instantiation: <rustls::msgs::handshake::SupportedProtocolVersions>::any::<rustls::client::hs::emit_client_hello_for_retry::{closure#13}> |
714 | | |
715 | | const LIST_LENGTH: ListLength = ListLength::NonZeroU8 { |
716 | | empty_error: InvalidMessage::IllegalEmptyList("ProtocolVersions"), |
717 | | }; |
718 | | } |
719 | | |
720 | | impl Codec<'_> for SupportedProtocolVersions { |
721 | 0 | fn encode(&self, bytes: &mut Vec<u8>) { |
722 | 0 | let inner = LengthPrefixedBuffer::new(Self::LIST_LENGTH, bytes); |
723 | 0 | if self.tls13 { |
724 | 0 | ProtocolVersion::TLSv1_3.encode(inner.buf); |
725 | 0 | } |
726 | 0 | if self.tls12 { |
727 | 0 | ProtocolVersion::TLSv1_2.encode(inner.buf); |
728 | 0 | } |
729 | 0 | } Unexecuted instantiation: <rustls::msgs::handshake::SupportedProtocolVersions as rustls::msgs::codec::Codec>::encode Unexecuted instantiation: <rustls::msgs::handshake::SupportedProtocolVersions as rustls::msgs::codec::Codec>::encode Unexecuted instantiation: <rustls::msgs::handshake::SupportedProtocolVersions as rustls::msgs::codec::Codec>::encode |
730 | | |
731 | 0 | fn read(reader: &mut Reader<'_>) -> Result<Self, InvalidMessage> { |
732 | 0 | let mut tls12 = false; |
733 | 0 | let mut tls13 = false; |
734 | | |
735 | 0 | for pv in TlsListIter::<ProtocolVersion>::new(reader)? { |
736 | 0 | match pv? { |
737 | 0 | ProtocolVersion::TLSv1_3 => tls13 = true, |
738 | 0 | ProtocolVersion::TLSv1_2 => tls12 = true, |
739 | 0 | _ => continue, |
740 | | }; |
741 | | } |
742 | | |
743 | 0 | Ok(Self { tls13, tls12 }) |
744 | 0 | } Unexecuted instantiation: <rustls::msgs::handshake::SupportedProtocolVersions as rustls::msgs::codec::Codec>::read Unexecuted instantiation: <rustls::msgs::handshake::SupportedProtocolVersions as rustls::msgs::codec::Codec>::read Unexecuted instantiation: <rustls::msgs::handshake::SupportedProtocolVersions as rustls::msgs::codec::Codec>::read |
745 | | } |
746 | | |
747 | | impl TlsListElement for ProtocolVersion { |
748 | | const SIZE_LEN: ListLength = ListLength::NonZeroU8 { |
749 | | empty_error: InvalidMessage::IllegalEmptyList("ProtocolVersions"), |
750 | | }; |
751 | | } |
752 | | |
753 | | /// RFC7250: `CertificateType client_certificate_types<1..2^8-1>;` |
754 | | /// |
755 | | /// Ditto `CertificateType server_certificate_types<1..2^8-1>;` |
756 | | impl TlsListElement for CertificateType { |
757 | | const SIZE_LEN: ListLength = ListLength::NonZeroU8 { |
758 | | empty_error: InvalidMessage::IllegalEmptyList("CertificateTypes"), |
759 | | }; |
760 | | } |
761 | | |
762 | | /// RFC8879: `CertificateCompressionAlgorithm algorithms<2..2^8-2>;` |
763 | | impl TlsListElement for CertificateCompressionAlgorithm { |
764 | | const SIZE_LEN: ListLength = ListLength::NonZeroU8 { |
765 | | empty_error: InvalidMessage::IllegalEmptyList("CertificateCompressionAlgorithms"), |
766 | | }; |
767 | | } |
768 | | |
769 | | /// A precursor to `ClientExtensions`, allowing customisation. |
770 | | /// |
771 | | /// This is smaller than `ClientExtensions`, as it only contains the extensions |
772 | | /// we need to vary between different protocols (eg, TCP-TLS versus QUIC). |
773 | | #[derive(Clone, Default)] |
774 | | pub(crate) struct ClientExtensionsInput<'a> { |
775 | | /// QUIC transport parameters |
776 | | pub(crate) transport_parameters: Option<TransportParameters<'a>>, |
777 | | |
778 | | /// ALPN protocols |
779 | | pub(crate) protocols: Option<Vec<ProtocolName>>, |
780 | | } |
781 | | |
782 | | impl ClientExtensionsInput<'_> { |
783 | 0 | pub(crate) fn from_alpn(alpn_protocols: Vec<Vec<u8>>) -> ClientExtensionsInput<'static> { |
784 | 0 | let protocols = match alpn_protocols.is_empty() { |
785 | 0 | true => None, |
786 | 0 | false => Some( |
787 | 0 | alpn_protocols |
788 | 0 | .into_iter() |
789 | 0 | .map(ProtocolName::from) |
790 | 0 | .collect::<Vec<_>>(), |
791 | 0 | ), |
792 | | }; |
793 | | |
794 | 0 | ClientExtensionsInput { |
795 | 0 | transport_parameters: None, |
796 | 0 | protocols, |
797 | 0 | } |
798 | 0 | } Unexecuted instantiation: <rustls::msgs::handshake::ClientExtensionsInput>::from_alpn Unexecuted instantiation: <rustls::msgs::handshake::ClientExtensionsInput>::from_alpn Unexecuted instantiation: <rustls::msgs::handshake::ClientExtensionsInput>::from_alpn |
799 | | |
800 | 0 | pub(crate) fn into_owned(self) -> ClientExtensionsInput<'static> { |
801 | 0 | let Self { |
802 | 0 | transport_parameters, |
803 | 0 | protocols, |
804 | 0 | } = self; |
805 | | ClientExtensionsInput { |
806 | 0 | transport_parameters: transport_parameters.map(|x| x.into_owned()), Unexecuted instantiation: <rustls::msgs::handshake::ClientExtensionsInput>::into_owned::{closure#0}Unexecuted instantiation: <rustls::msgs::handshake::ClientExtensionsInput>::into_owned::{closure#0}Unexecuted instantiation: <rustls::msgs::handshake::ClientExtensionsInput>::into_owned::{closure#0} |
807 | 0 | protocols, |
808 | | } |
809 | 0 | } Unexecuted instantiation: <rustls::msgs::handshake::ClientExtensionsInput>::into_owned Unexecuted instantiation: <rustls::msgs::handshake::ClientExtensionsInput>::into_owned Unexecuted instantiation: <rustls::msgs::handshake::ClientExtensionsInput>::into_owned |
810 | | } |
811 | | |
812 | | #[derive(Clone)] |
813 | | pub(crate) enum TransportParameters<'a> { |
814 | | /// QUIC transport parameters (RFC9001 prior to draft 33) |
815 | | QuicDraft(Payload<'a>), |
816 | | |
817 | | /// QUIC transport parameters (RFC9001) |
818 | | Quic(Payload<'a>), |
819 | | } |
820 | | |
821 | | impl TransportParameters<'_> { |
822 | 0 | pub(crate) fn into_owned(self) -> TransportParameters<'static> { |
823 | 0 | match self { |
824 | 0 | Self::QuicDraft(v) => TransportParameters::QuicDraft(v.into_owned()), |
825 | 0 | Self::Quic(v) => TransportParameters::Quic(v.into_owned()), |
826 | | } |
827 | 0 | } Unexecuted instantiation: <rustls::msgs::handshake::TransportParameters>::into_owned Unexecuted instantiation: <rustls::msgs::handshake::TransportParameters>::into_owned Unexecuted instantiation: <rustls::msgs::handshake::TransportParameters>::into_owned |
828 | | } |
829 | | |
830 | | /// RFC 9149: ClientTicketRequest extension payload. |
831 | | #[derive(Clone, Copy, Debug, PartialEq)] |
832 | | pub(crate) struct ClientTicketRequest { |
833 | | /// Tickets desired when the server negotiates a new connection. |
834 | | pub new_session_count: u8, |
835 | | /// Tickets desired when the server resumes using a presented ticket. |
836 | | pub resumption_count: u8, |
837 | | } |
838 | | |
839 | | impl Codec<'_> for ClientTicketRequest { |
840 | 0 | fn encode(&self, bytes: &mut Vec<u8>) { |
841 | 0 | self.new_session_count.encode(bytes); |
842 | 0 | self.resumption_count.encode(bytes); |
843 | 0 | } Unexecuted instantiation: <rustls::msgs::handshake::ClientTicketRequest as rustls::msgs::codec::Codec>::encode Unexecuted instantiation: <rustls::msgs::handshake::ClientTicketRequest as rustls::msgs::codec::Codec>::encode Unexecuted instantiation: <rustls::msgs::handshake::ClientTicketRequest as rustls::msgs::codec::Codec>::encode |
844 | | |
845 | 0 | fn read(r: &mut Reader<'_>) -> Result<Self, InvalidMessage> { |
846 | | Ok(Self { |
847 | 0 | new_session_count: u8::read(r)?, |
848 | 0 | resumption_count: u8::read(r)?, |
849 | | }) |
850 | 0 | } Unexecuted instantiation: <rustls::msgs::handshake::ClientTicketRequest as rustls::msgs::codec::Codec>::read Unexecuted instantiation: <rustls::msgs::handshake::ClientTicketRequest as rustls::msgs::codec::Codec>::read Unexecuted instantiation: <rustls::msgs::handshake::ClientTicketRequest as rustls::msgs::codec::Codec>::read |
851 | | } |
852 | | |
853 | | /// RFC 9149: ServerTicketRequestHint extension payload. |
854 | | #[derive(Clone, Copy, Debug, PartialEq)] |
855 | | pub(crate) struct ServerTicketRequestHint { |
856 | | pub(crate) expected_count: u8, |
857 | | } |
858 | | |
859 | | impl Codec<'_> for ServerTicketRequestHint { |
860 | 0 | fn encode(&self, bytes: &mut Vec<u8>) { |
861 | 0 | self.expected_count.encode(bytes); |
862 | 0 | } Unexecuted instantiation: <rustls::msgs::handshake::ServerTicketRequestHint as rustls::msgs::codec::Codec>::encode Unexecuted instantiation: <rustls::msgs::handshake::ServerTicketRequestHint as rustls::msgs::codec::Codec>::encode Unexecuted instantiation: <rustls::msgs::handshake::ServerTicketRequestHint as rustls::msgs::codec::Codec>::encode |
863 | | |
864 | 0 | fn read(r: &mut Reader<'_>) -> Result<Self, InvalidMessage> { |
865 | | Ok(Self { |
866 | 0 | expected_count: u8::read(r)?, |
867 | | }) |
868 | 0 | } Unexecuted instantiation: <rustls::msgs::handshake::ServerTicketRequestHint as rustls::msgs::codec::Codec>::read Unexecuted instantiation: <rustls::msgs::handshake::ServerTicketRequestHint as rustls::msgs::codec::Codec>::read Unexecuted instantiation: <rustls::msgs::handshake::ServerTicketRequestHint as rustls::msgs::codec::Codec>::read |
869 | | } |
870 | | |
871 | | extension_struct! { |
872 | | /// A representation of extensions present in a `ClientHello` message |
873 | | /// |
874 | | /// All extensions are optional (by definition) so are represented with `Option<T>`. |
875 | | /// |
876 | | /// Some extensions have an empty value and are represented with Option<()>. |
877 | | /// |
878 | | /// Unknown extensions are dropped during parsing. |
879 | | pub(crate) struct ClientExtensions<'a> { |
880 | | /// Requested server name indication (RFC6066) |
881 | | ExtensionType::ServerName => |
882 | | pub(crate) server_name: Option<ServerNamePayload<'a>>, |
883 | | |
884 | | /// Certificate status is requested (RFC6066) |
885 | | ExtensionType::StatusRequest => |
886 | | pub(crate) certificate_status_request: Option<CertificateStatusRequest>, |
887 | | |
888 | | /// Supported groups (RFC4492/RFC8446) |
889 | | ExtensionType::EllipticCurves => |
890 | | pub(crate) named_groups: Option<Vec<NamedGroup>>, |
891 | | |
892 | | /// Supported EC point formats (RFC4492) |
893 | | ExtensionType::ECPointFormats => |
894 | | pub(crate) ec_point_formats: Option<SupportedEcPointFormats>, |
895 | | |
896 | | /// Supported signature schemes (RFC5246/RFC8446) |
897 | | ExtensionType::SignatureAlgorithms => |
898 | | pub(crate) signature_schemes: Option<Vec<SignatureScheme>>, |
899 | | |
900 | | /// Offered ALPN protocols (RFC6066) |
901 | | ExtensionType::ALProtocolNegotiation => |
902 | | pub(crate) protocols: Option<Vec<ProtocolName>>, |
903 | | |
904 | | /// Available client certificate types (RFC7250) |
905 | | ExtensionType::ClientCertificateType => |
906 | | pub(crate) client_certificate_types: Option<Vec<CertificateType>>, |
907 | | |
908 | | /// Acceptable server certificate types (RFC7250) |
909 | | ExtensionType::ServerCertificateType => |
910 | | pub(crate) server_certificate_types: Option<Vec<CertificateType>>, |
911 | | |
912 | | /// Extended master secret is requested (RFC7627) |
913 | | ExtensionType::ExtendedMasterSecret => |
914 | | pub(crate) extended_master_secret_request: Option<()>, |
915 | | |
916 | | /// Offered certificate compression methods (RFC8879) |
917 | | ExtensionType::CompressCertificate => |
918 | | pub(crate) certificate_compression_algorithms: Option<Vec<CertificateCompressionAlgorithm>>, |
919 | | |
920 | | /// Session ticket offer or request (RFC5077/RFC8446) |
921 | | ExtensionType::SessionTicket => |
922 | | pub(crate) session_ticket: Option<ClientSessionTicket>, |
923 | | |
924 | | /// Offered preshared keys (RFC8446) |
925 | | ExtensionType::PreSharedKey => |
926 | | pub(crate) preshared_key_offer: Option<PresharedKeyOffer>, |
927 | | |
928 | | /// Early data is requested (RFC8446) |
929 | | ExtensionType::EarlyData => |
930 | | pub(crate) early_data_request: Option<()>, |
931 | | |
932 | | /// Supported TLS versions (RFC8446) |
933 | | ExtensionType::SupportedVersions => |
934 | | pub(crate) supported_versions: Option<SupportedProtocolVersions>, |
935 | | |
936 | | /// Stateless HelloRetryRequest cookie (RFC8446) |
937 | | ExtensionType::Cookie => |
938 | | pub(crate) cookie: Option<PayloadU16<NonEmpty>>, |
939 | | |
940 | | /// Offered preshared key modes (RFC8446) |
941 | | ExtensionType::PSKKeyExchangeModes => |
942 | | pub(crate) preshared_key_modes: Option<PskKeyExchangeModes>, |
943 | | |
944 | | /// Certificate authority names (RFC8446) |
945 | | ExtensionType::CertificateAuthorities => |
946 | | pub(crate) certificate_authority_names: Option<Vec<DistinguishedName>>, |
947 | | |
948 | | /// Offered key exchange shares (RFC8446) |
949 | | ExtensionType::KeyShare => |
950 | | pub(crate) key_shares: Option<Vec<KeyShareEntry>>, |
951 | | |
952 | | /// QUIC transport parameters (RFC9001) |
953 | | ExtensionType::TransportParameters => |
954 | | pub(crate) transport_parameters: Option<Payload<'a>>, |
955 | | |
956 | | /// Ticket request (RFC9149) |
957 | | ExtensionType::TicketRequest => |
958 | | pub(crate) ticket_request: Option<ClientTicketRequest>, |
959 | | |
960 | | /// Secure renegotiation (RFC5746) |
961 | | ExtensionType::RenegotiationInfo => |
962 | | pub(crate) renegotiation_info: Option<PayloadU8>, |
963 | | |
964 | | /// QUIC transport parameters (RFC9001 prior to draft 33) |
965 | | ExtensionType::TransportParametersDraft => |
966 | | pub(crate) transport_parameters_draft: Option<Payload<'a>>, |
967 | | |
968 | | /// Encrypted inner client hello (draft-ietf-tls-esni) |
969 | | ExtensionType::EncryptedClientHello => |
970 | | pub(crate) encrypted_client_hello: Option<EncryptedClientHello>, |
971 | | |
972 | | /// Encrypted client hello outer extensions (draft-ietf-tls-esni) |
973 | | ExtensionType::EncryptedClientHelloOuterExtensions => |
974 | | pub(crate) encrypted_client_hello_outer: Option<Vec<ExtensionType>>, |
975 | | } + { |
976 | | /// Order randomization seed. |
977 | | pub(crate) order_seed: u16, |
978 | | |
979 | | /// Extensions that must appear contiguously. |
980 | | pub(crate) contiguous_extensions: Vec<ExtensionType>, |
981 | | } |
982 | | } |
983 | | |
984 | | impl ClientExtensions<'_> { |
985 | 0 | pub(crate) fn into_owned(self) -> ClientExtensions<'static> { |
986 | 0 | let Self { |
987 | 0 | server_name, |
988 | 0 | certificate_status_request, |
989 | 0 | named_groups, |
990 | 0 | ec_point_formats, |
991 | 0 | signature_schemes, |
992 | 0 | protocols, |
993 | 0 | client_certificate_types, |
994 | 0 | server_certificate_types, |
995 | 0 | extended_master_secret_request, |
996 | 0 | certificate_compression_algorithms, |
997 | 0 | session_ticket, |
998 | 0 | preshared_key_offer, |
999 | 0 | early_data_request, |
1000 | 0 | supported_versions, |
1001 | 0 | cookie, |
1002 | 0 | preshared_key_modes, |
1003 | 0 | certificate_authority_names, |
1004 | 0 | key_shares, |
1005 | 0 | transport_parameters, |
1006 | 0 | ticket_request, |
1007 | 0 | renegotiation_info, |
1008 | 0 | transport_parameters_draft, |
1009 | 0 | encrypted_client_hello, |
1010 | 0 | encrypted_client_hello_outer, |
1011 | 0 | order_seed, |
1012 | 0 | contiguous_extensions, |
1013 | 0 | } = self; |
1014 | | ClientExtensions { |
1015 | 0 | server_name: server_name.map(|x| x.into_owned()), Unexecuted instantiation: <rustls::msgs::handshake::ClientExtensions>::into_owned::{closure#0}Unexecuted instantiation: <rustls::msgs::handshake::ClientExtensions>::into_owned::{closure#0}Unexecuted instantiation: <rustls::msgs::handshake::ClientExtensions>::into_owned::{closure#0} |
1016 | 0 | certificate_status_request, |
1017 | 0 | named_groups, |
1018 | 0 | ec_point_formats, |
1019 | 0 | signature_schemes, |
1020 | 0 | protocols, |
1021 | 0 | client_certificate_types, |
1022 | 0 | server_certificate_types, |
1023 | 0 | extended_master_secret_request, |
1024 | 0 | certificate_compression_algorithms, |
1025 | 0 | session_ticket, |
1026 | 0 | preshared_key_offer, |
1027 | 0 | early_data_request, |
1028 | 0 | supported_versions, |
1029 | 0 | cookie, |
1030 | 0 | preshared_key_modes, |
1031 | 0 | certificate_authority_names, |
1032 | 0 | key_shares, |
1033 | 0 | transport_parameters: transport_parameters.map(|x| x.into_owned()), Unexecuted instantiation: <rustls::msgs::handshake::ClientExtensions>::into_owned::{closure#1}Unexecuted instantiation: <rustls::msgs::handshake::ClientExtensions>::into_owned::{closure#1}Unexecuted instantiation: <rustls::msgs::handshake::ClientExtensions>::into_owned::{closure#1} |
1034 | 0 | ticket_request, |
1035 | 0 | renegotiation_info, |
1036 | 0 | transport_parameters_draft: transport_parameters_draft.map(|x| x.into_owned()), Unexecuted instantiation: <rustls::msgs::handshake::ClientExtensions>::into_owned::{closure#2}Unexecuted instantiation: <rustls::msgs::handshake::ClientExtensions>::into_owned::{closure#2}Unexecuted instantiation: <rustls::msgs::handshake::ClientExtensions>::into_owned::{closure#2} |
1037 | 0 | encrypted_client_hello, |
1038 | 0 | encrypted_client_hello_outer, |
1039 | 0 | order_seed, |
1040 | 0 | contiguous_extensions, |
1041 | | } |
1042 | 0 | } Unexecuted instantiation: <rustls::msgs::handshake::ClientExtensions>::into_owned Unexecuted instantiation: <rustls::msgs::handshake::ClientExtensions>::into_owned Unexecuted instantiation: <rustls::msgs::handshake::ClientExtensions>::into_owned |
1043 | | |
1044 | 0 | pub(crate) fn used_extensions_in_encoding_order(&self) -> Vec<ExtensionType> { |
1045 | 0 | let mut exts = self.order_insensitive_extensions_in_random_order(); |
1046 | 0 | exts.extend(&self.contiguous_extensions); |
1047 | | |
1048 | 0 | if self |
1049 | 0 | .encrypted_client_hello_outer |
1050 | 0 | .is_some() |
1051 | 0 | { |
1052 | 0 | exts.push(ExtensionType::EncryptedClientHelloOuterExtensions); |
1053 | 0 | } |
1054 | 0 | if self.encrypted_client_hello.is_some() { |
1055 | 0 | exts.push(ExtensionType::EncryptedClientHello); |
1056 | 0 | } |
1057 | 0 | if self.preshared_key_offer.is_some() { |
1058 | 0 | exts.push(ExtensionType::PreSharedKey); |
1059 | 0 | } |
1060 | 0 | exts |
1061 | 0 | } Unexecuted instantiation: <rustls::msgs::handshake::ClientExtensions>::used_extensions_in_encoding_order Unexecuted instantiation: <rustls::msgs::handshake::ClientExtensions>::used_extensions_in_encoding_order Unexecuted instantiation: <rustls::msgs::handshake::ClientExtensions>::used_extensions_in_encoding_order |
1062 | | |
1063 | | /// Returns extensions which don't need a specific order, in randomized order. |
1064 | | /// |
1065 | | /// Extensions are encoded in three portions: |
1066 | | /// |
1067 | | /// - First, extensions not otherwise dealt with by other cases. |
1068 | | /// These are encoded in random order, controlled by `self.order_seed`, |
1069 | | /// and this is the set of extensions returned by this function. |
1070 | | /// |
1071 | | /// - Second, extensions named in `self.contiguous_extensions`, in the order |
1072 | | /// given by that field. |
1073 | | /// |
1074 | | /// - Lastly, any ECH and PSK extensions (in that order). These |
1075 | | /// are required to be last by the standard. |
1076 | 0 | fn order_insensitive_extensions_in_random_order(&self) -> Vec<ExtensionType> { |
1077 | 0 | let mut order = self.collect_used(); |
1078 | | |
1079 | | // Remove extensions which have specific order requirements. |
1080 | 0 | order.retain(|ext| { |
1081 | 0 | !(matches!( |
1082 | 0 | ext, |
1083 | | ExtensionType::PreSharedKey |
1084 | | | ExtensionType::EncryptedClientHello |
1085 | | | ExtensionType::EncryptedClientHelloOuterExtensions |
1086 | 0 | ) || self.contiguous_extensions.contains(ext)) |
1087 | 0 | }); Unexecuted instantiation: <rustls::msgs::handshake::ClientExtensions>::order_insensitive_extensions_in_random_order::{closure#0}Unexecuted instantiation: <rustls::msgs::handshake::ClientExtensions>::order_insensitive_extensions_in_random_order::{closure#0}Unexecuted instantiation: <rustls::msgs::handshake::ClientExtensions>::order_insensitive_extensions_in_random_order::{closure#0} |
1088 | | |
1089 | 0 | order.sort_by_cached_key(|new_ext| { |
1090 | 0 | let seed = ((self.order_seed as u32) << 16) | (u16::from(*new_ext) as u32); |
1091 | 0 | low_quality_integer_hash(seed) |
1092 | 0 | }); Unexecuted instantiation: <rustls::msgs::handshake::ClientExtensions>::order_insensitive_extensions_in_random_order::{closure#1}Unexecuted instantiation: <rustls::msgs::handshake::ClientExtensions>::order_insensitive_extensions_in_random_order::{closure#1}Unexecuted instantiation: <rustls::msgs::handshake::ClientExtensions>::order_insensitive_extensions_in_random_order::{closure#1} |
1093 | | |
1094 | 0 | order |
1095 | 0 | } Unexecuted instantiation: <rustls::msgs::handshake::ClientExtensions>::order_insensitive_extensions_in_random_order Unexecuted instantiation: <rustls::msgs::handshake::ClientExtensions>::order_insensitive_extensions_in_random_order Unexecuted instantiation: <rustls::msgs::handshake::ClientExtensions>::order_insensitive_extensions_in_random_order |
1096 | | } |
1097 | | |
1098 | | impl<'a> Codec<'a> for ClientExtensions<'a> { |
1099 | 0 | fn encode(&self, bytes: &mut Vec<u8>) { |
1100 | 0 | let order = self.used_extensions_in_encoding_order(); |
1101 | | |
1102 | 0 | if order.is_empty() { |
1103 | 0 | return; |
1104 | 0 | } |
1105 | | |
1106 | 0 | let body = LengthPrefixedBuffer::new(ListLength::U16, bytes); |
1107 | 0 | for item in order { |
1108 | 0 | self.encode_one(item, body.buf); |
1109 | 0 | } |
1110 | 0 | } Unexecuted instantiation: <rustls::msgs::handshake::ClientExtensions as rustls::msgs::codec::Codec>::encode Unexecuted instantiation: <rustls::msgs::handshake::ClientExtensions as rustls::msgs::codec::Codec>::encode Unexecuted instantiation: <rustls::msgs::handshake::ClientExtensions as rustls::msgs::codec::Codec>::encode |
1111 | | |
1112 | 0 | fn read(r: &mut Reader<'a>) -> Result<Self, InvalidMessage> { |
1113 | 0 | let mut out = Self::default(); |
1114 | | |
1115 | | // extensions length can be absent if no extensions |
1116 | 0 | if !r.any_left() { |
1117 | 0 | return Ok(out); |
1118 | 0 | } |
1119 | | |
1120 | 0 | let mut checker = DuplicateExtensionChecker::new(); |
1121 | | |
1122 | 0 | let len = usize::from(u16::read(r)?); |
1123 | 0 | let mut sub = r.sub(len)?; |
1124 | | |
1125 | 0 | while sub.any_left() { |
1126 | 0 | let typ = out.read_one(&mut sub, |unknown| checker.check(unknown))?; Unexecuted instantiation: <rustls::msgs::handshake::ClientExtensions as rustls::msgs::codec::Codec>::read::{closure#0}Unexecuted instantiation: <rustls::msgs::handshake::ClientExtensions as rustls::msgs::codec::Codec>::read::{closure#0}Unexecuted instantiation: <rustls::msgs::handshake::ClientExtensions as rustls::msgs::codec::Codec>::read::{closure#0} |
1127 | | |
1128 | | // PreSharedKey offer must come last |
1129 | 0 | if typ == ExtensionType::PreSharedKey && sub.any_left() { |
1130 | 0 | return Err(InvalidMessage::PreSharedKeyIsNotFinalExtension); |
1131 | 0 | } |
1132 | | } |
1133 | | |
1134 | 0 | Ok(out) |
1135 | 0 | } Unexecuted instantiation: <rustls::msgs::handshake::ClientExtensions as rustls::msgs::codec::Codec>::read Unexecuted instantiation: <rustls::msgs::handshake::ClientExtensions as rustls::msgs::codec::Codec>::read Unexecuted instantiation: <rustls::msgs::handshake::ClientExtensions as rustls::msgs::codec::Codec>::read |
1136 | | } |
1137 | | |
1138 | 0 | fn trim_hostname_trailing_dot_for_sni(dns_name: &DnsName<'_>) -> DnsName<'static> { |
1139 | 0 | let dns_name_str = dns_name.as_ref(); |
1140 | | |
1141 | | // RFC6066: "The hostname is represented as a byte string using |
1142 | | // ASCII encoding without a trailing dot" |
1143 | 0 | if dns_name_str.ends_with('.') { |
1144 | 0 | let trimmed = &dns_name_str[0..dns_name_str.len() - 1]; |
1145 | 0 | DnsName::try_from(trimmed) |
1146 | 0 | .unwrap() |
1147 | 0 | .to_owned() |
1148 | | } else { |
1149 | 0 | dns_name.to_owned() |
1150 | | } |
1151 | 0 | } Unexecuted instantiation: rustls::msgs::handshake::trim_hostname_trailing_dot_for_sni Unexecuted instantiation: rustls::msgs::handshake::trim_hostname_trailing_dot_for_sni Unexecuted instantiation: rustls::msgs::handshake::trim_hostname_trailing_dot_for_sni |
1152 | | |
1153 | | #[derive(Clone, Debug)] |
1154 | | pub(crate) enum ClientSessionTicket { |
1155 | | Request, |
1156 | | Offer(Payload<'static>), |
1157 | | } |
1158 | | |
1159 | | impl<'a> Codec<'a> for ClientSessionTicket { |
1160 | 0 | fn encode(&self, bytes: &mut Vec<u8>) { |
1161 | 0 | match self { |
1162 | 0 | Self::Request => (), |
1163 | 0 | Self::Offer(p) => p.encode(bytes), |
1164 | | } |
1165 | 0 | } Unexecuted instantiation: <rustls::msgs::handshake::ClientSessionTicket as rustls::msgs::codec::Codec>::encode Unexecuted instantiation: <rustls::msgs::handshake::ClientSessionTicket as rustls::msgs::codec::Codec>::encode Unexecuted instantiation: <rustls::msgs::handshake::ClientSessionTicket as rustls::msgs::codec::Codec>::encode |
1166 | | |
1167 | 0 | fn read(r: &mut Reader<'a>) -> Result<Self, InvalidMessage> { |
1168 | 0 | Ok(match r.left() { |
1169 | 0 | 0 => Self::Request, |
1170 | 0 | _ => Self::Offer(Payload::read(r).into_owned()), |
1171 | | }) |
1172 | 0 | } Unexecuted instantiation: <rustls::msgs::handshake::ClientSessionTicket as rustls::msgs::codec::Codec>::read Unexecuted instantiation: <rustls::msgs::handshake::ClientSessionTicket as rustls::msgs::codec::Codec>::read Unexecuted instantiation: <rustls::msgs::handshake::ClientSessionTicket as rustls::msgs::codec::Codec>::read |
1173 | | } |
1174 | | |
1175 | | #[derive(Default)] |
1176 | | pub(crate) struct ServerExtensionsInput<'a> { |
1177 | | /// QUIC transport parameters |
1178 | | pub(crate) transport_parameters: Option<TransportParameters<'a>>, |
1179 | | } |
1180 | | |
1181 | | extension_struct! { |
1182 | | pub(crate) struct ServerExtensions<'a> { |
1183 | | /// Supported EC point formats (RFC4492) |
1184 | | ExtensionType::ECPointFormats => |
1185 | | pub(crate) ec_point_formats: Option<SupportedEcPointFormats>, |
1186 | | |
1187 | | /// Server name indication acknowledgement (RFC6066) |
1188 | | ExtensionType::ServerName => |
1189 | | pub(crate) server_name_ack: Option<()>, |
1190 | | |
1191 | | /// Session ticket acknowledgement (RFC5077) |
1192 | | ExtensionType::SessionTicket => |
1193 | | pub(crate) session_ticket_ack: Option<()>, |
1194 | | |
1195 | | ExtensionType::RenegotiationInfo => |
1196 | | pub(crate) renegotiation_info: Option<PayloadU8>, |
1197 | | |
1198 | | /// Selected ALPN protocol (RFC7301) |
1199 | | ExtensionType::ALProtocolNegotiation => |
1200 | | pub(crate) selected_protocol: Option<SingleProtocolName>, |
1201 | | |
1202 | | /// Key exchange server share (RFC8446) |
1203 | | ExtensionType::KeyShare => |
1204 | | pub(crate) key_share: Option<KeyShareEntry>, |
1205 | | |
1206 | | /// Selected preshared key index (RFC8446) |
1207 | | ExtensionType::PreSharedKey => |
1208 | | pub(crate) preshared_key: Option<u16>, |
1209 | | |
1210 | | /// Required client certificate type (RFC7250) |
1211 | | ExtensionType::ClientCertificateType => |
1212 | | pub(crate) client_certificate_type: Option<CertificateType>, |
1213 | | |
1214 | | /// Selected server certificate type (RFC7250) |
1215 | | ExtensionType::ServerCertificateType => |
1216 | | pub(crate) server_certificate_type: Option<CertificateType>, |
1217 | | |
1218 | | /// Extended master secret is in use (RFC7627) |
1219 | | ExtensionType::ExtendedMasterSecret => |
1220 | | pub(crate) extended_master_secret_ack: Option<()>, |
1221 | | |
1222 | | /// Certificate status acknowledgement (RFC6066) |
1223 | | ExtensionType::StatusRequest => |
1224 | | pub(crate) certificate_status_request_ack: Option<()>, |
1225 | | |
1226 | | /// Selected TLS version (RFC8446) |
1227 | | ExtensionType::SupportedVersions => |
1228 | | pub(crate) selected_version: Option<ProtocolVersion>, |
1229 | | |
1230 | | /// QUIC transport parameters (RFC9001) |
1231 | | ExtensionType::TransportParameters => |
1232 | | pub(crate) transport_parameters: Option<Payload<'a>>, |
1233 | | |
1234 | | /// QUIC transport parameters (RFC9001 prior to draft 33) |
1235 | | ExtensionType::TransportParametersDraft => |
1236 | | pub(crate) transport_parameters_draft: Option<Payload<'a>>, |
1237 | | |
1238 | | /// Early data is accepted (RFC8446) |
1239 | | ExtensionType::EarlyData => |
1240 | | pub(crate) early_data_ack: Option<()>, |
1241 | | |
1242 | | /// Ticket request hint (RFC9149) |
1243 | | ExtensionType::TicketRequest => |
1244 | | pub(crate) ticket_request: Option<ServerTicketRequestHint>, |
1245 | | |
1246 | | /// Encrypted inner client hello response (draft-ietf-tls-esni) |
1247 | | ExtensionType::EncryptedClientHello => |
1248 | | pub(crate) encrypted_client_hello_ack: Option<ServerEncryptedClientHello>, |
1249 | | } + { |
1250 | | pub(crate) unknown_extensions: BTreeSet<u16>, |
1251 | | } |
1252 | | } |
1253 | | |
1254 | | impl ServerExtensions<'_> { |
1255 | 0 | fn into_owned(self) -> ServerExtensions<'static> { |
1256 | 0 | let Self { |
1257 | 0 | ec_point_formats, |
1258 | 0 | server_name_ack, |
1259 | 0 | session_ticket_ack, |
1260 | 0 | renegotiation_info, |
1261 | 0 | selected_protocol, |
1262 | 0 | key_share, |
1263 | 0 | preshared_key, |
1264 | 0 | client_certificate_type, |
1265 | 0 | server_certificate_type, |
1266 | 0 | extended_master_secret_ack, |
1267 | 0 | certificate_status_request_ack, |
1268 | 0 | selected_version, |
1269 | 0 | transport_parameters, |
1270 | 0 | transport_parameters_draft, |
1271 | 0 | early_data_ack, |
1272 | 0 | ticket_request, |
1273 | 0 | encrypted_client_hello_ack, |
1274 | 0 | unknown_extensions, |
1275 | 0 | } = self; |
1276 | | ServerExtensions { |
1277 | 0 | ec_point_formats, |
1278 | 0 | server_name_ack, |
1279 | 0 | session_ticket_ack, |
1280 | 0 | renegotiation_info, |
1281 | 0 | selected_protocol, |
1282 | 0 | key_share, |
1283 | 0 | preshared_key, |
1284 | 0 | client_certificate_type, |
1285 | 0 | server_certificate_type, |
1286 | 0 | extended_master_secret_ack, |
1287 | 0 | certificate_status_request_ack, |
1288 | 0 | selected_version, |
1289 | 0 | transport_parameters: transport_parameters.map(|x| x.into_owned()), Unexecuted instantiation: <rustls::msgs::handshake::ServerExtensions>::into_owned::{closure#0}Unexecuted instantiation: <rustls::msgs::handshake::ServerExtensions>::into_owned::{closure#0}Unexecuted instantiation: <rustls::msgs::handshake::ServerExtensions>::into_owned::{closure#0} |
1290 | 0 | transport_parameters_draft: transport_parameters_draft.map(|x| x.into_owned()), Unexecuted instantiation: <rustls::msgs::handshake::ServerExtensions>::into_owned::{closure#1}Unexecuted instantiation: <rustls::msgs::handshake::ServerExtensions>::into_owned::{closure#1}Unexecuted instantiation: <rustls::msgs::handshake::ServerExtensions>::into_owned::{closure#1} |
1291 | 0 | early_data_ack, |
1292 | 0 | ticket_request, |
1293 | 0 | encrypted_client_hello_ack, |
1294 | 0 | unknown_extensions, |
1295 | | } |
1296 | 0 | } Unexecuted instantiation: <rustls::msgs::handshake::ServerExtensions>::into_owned Unexecuted instantiation: <rustls::msgs::handshake::ServerExtensions>::into_owned Unexecuted instantiation: <rustls::msgs::handshake::ServerExtensions>::into_owned |
1297 | | } |
1298 | | |
1299 | | impl<'a> Codec<'a> for ServerExtensions<'a> { |
1300 | 0 | fn encode(&self, bytes: &mut Vec<u8>) { |
1301 | 0 | let extensions = LengthPrefixedBuffer::new(ListLength::U16, bytes); |
1302 | | |
1303 | 0 | for ext in Self::ALL_EXTENSIONS { |
1304 | 0 | self.encode_one(*ext, extensions.buf); |
1305 | 0 | } |
1306 | 0 | } Unexecuted instantiation: <rustls::msgs::handshake::ServerExtensions as rustls::msgs::codec::Codec>::encode Unexecuted instantiation: <rustls::msgs::handshake::ServerExtensions as rustls::msgs::codec::Codec>::encode Unexecuted instantiation: <rustls::msgs::handshake::ServerExtensions as rustls::msgs::codec::Codec>::encode |
1307 | | |
1308 | 0 | fn read(r: &mut Reader<'a>) -> Result<Self, InvalidMessage> { |
1309 | 0 | let mut out = Self::default(); |
1310 | 0 | let mut checker = DuplicateExtensionChecker::new(); |
1311 | | |
1312 | 0 | let len = usize::from(u16::read(r)?); |
1313 | 0 | let mut sub = r.sub(len)?; |
1314 | | |
1315 | 0 | while sub.any_left() { |
1316 | 0 | out.read_one(&mut sub, |unknown| checker.check(unknown))?; Unexecuted instantiation: <rustls::msgs::handshake::ServerExtensions as rustls::msgs::codec::Codec>::read::{closure#0}Unexecuted instantiation: <rustls::msgs::handshake::ServerExtensions as rustls::msgs::codec::Codec>::read::{closure#0}Unexecuted instantiation: <rustls::msgs::handshake::ServerExtensions as rustls::msgs::codec::Codec>::read::{closure#0} |
1317 | | } |
1318 | | |
1319 | 0 | out.unknown_extensions = checker.0; |
1320 | 0 | Ok(out) |
1321 | 0 | } Unexecuted instantiation: <rustls::msgs::handshake::ServerExtensions as rustls::msgs::codec::Codec>::read Unexecuted instantiation: <rustls::msgs::handshake::ServerExtensions as rustls::msgs::codec::Codec>::read Unexecuted instantiation: <rustls::msgs::handshake::ServerExtensions as rustls::msgs::codec::Codec>::read |
1322 | | } |
1323 | | |
1324 | | #[derive(Clone, Debug)] |
1325 | | pub(crate) struct ClientHelloPayload { |
1326 | | pub(crate) client_version: ProtocolVersion, |
1327 | | pub(crate) random: Random, |
1328 | | pub(crate) session_id: SessionId, |
1329 | | pub(crate) cipher_suites: Vec<CipherSuite>, |
1330 | | pub(crate) compression_methods: Vec<Compression>, |
1331 | | pub(crate) extensions: Box<ClientExtensions<'static>>, |
1332 | | } |
1333 | | |
1334 | | impl ClientHelloPayload { |
1335 | 0 | pub(crate) fn ech_inner_encoding(&self, to_compress: Vec<ExtensionType>) -> Vec<u8> { |
1336 | 0 | let mut bytes = Vec::new(); |
1337 | 0 | self.payload_encode(&mut bytes, Encoding::EchInnerHello { to_compress }); |
1338 | 0 | bytes |
1339 | 0 | } Unexecuted instantiation: <rustls::msgs::handshake::ClientHelloPayload>::ech_inner_encoding Unexecuted instantiation: <rustls::msgs::handshake::ClientHelloPayload>::ech_inner_encoding Unexecuted instantiation: <rustls::msgs::handshake::ClientHelloPayload>::ech_inner_encoding |
1340 | | |
1341 | 0 | pub(crate) fn payload_encode(&self, bytes: &mut Vec<u8>, purpose: Encoding) { |
1342 | 0 | self.client_version.encode(bytes); |
1343 | 0 | self.random.encode(bytes); |
1344 | | |
1345 | 0 | match purpose { |
1346 | | // SessionID is required to be empty in the encoded inner client hello. |
1347 | 0 | Encoding::EchInnerHello { .. } => SessionId::empty().encode(bytes), |
1348 | 0 | _ => self.session_id.encode(bytes), |
1349 | | } |
1350 | | |
1351 | 0 | self.cipher_suites.encode(bytes); |
1352 | 0 | self.compression_methods.encode(bytes); |
1353 | | |
1354 | 0 | let to_compress = match purpose { |
1355 | 0 | Encoding::EchInnerHello { to_compress } if !to_compress.is_empty() => to_compress, |
1356 | | _ => { |
1357 | 0 | self.extensions.encode(bytes); |
1358 | 0 | return; |
1359 | | } |
1360 | | }; |
1361 | | |
1362 | 0 | let mut compressed = self.extensions.clone(); |
1363 | | |
1364 | | // First, eliminate the full-fat versions of the extensions |
1365 | 0 | for e in &to_compress { |
1366 | 0 | compressed.clear(*e); |
1367 | 0 | } |
1368 | | |
1369 | | // Replace with the marker noting which extensions were elided. |
1370 | 0 | compressed.encrypted_client_hello_outer = Some(to_compress); |
1371 | | |
1372 | | // And encode as normal. |
1373 | 0 | compressed.encode(bytes); |
1374 | 0 | } Unexecuted instantiation: <rustls::msgs::handshake::ClientHelloPayload>::payload_encode Unexecuted instantiation: <rustls::msgs::handshake::ClientHelloPayload>::payload_encode Unexecuted instantiation: <rustls::msgs::handshake::ClientHelloPayload>::payload_encode |
1375 | | |
1376 | 0 | pub(crate) fn has_keyshare_extension_with_duplicates(&self) -> bool { |
1377 | 0 | self.key_shares |
1378 | 0 | .as_ref() |
1379 | 0 | .map(|entries| { |
1380 | 0 | has_duplicates::<_, _, u16>( |
1381 | 0 | entries |
1382 | 0 | .iter() |
1383 | 0 | .map(|kse| u16::from(kse.group)), Unexecuted instantiation: <rustls::msgs::handshake::ClientHelloPayload>::has_keyshare_extension_with_duplicates::{closure#0}::{closure#0}Unexecuted instantiation: <rustls::msgs::handshake::ClientHelloPayload>::has_keyshare_extension_with_duplicates::{closure#0}::{closure#0}Unexecuted instantiation: <rustls::msgs::handshake::ClientHelloPayload>::has_keyshare_extension_with_duplicates::{closure#0}::{closure#0} |
1384 | | ) |
1385 | 0 | }) Unexecuted instantiation: <rustls::msgs::handshake::ClientHelloPayload>::has_keyshare_extension_with_duplicates::{closure#0}Unexecuted instantiation: <rustls::msgs::handshake::ClientHelloPayload>::has_keyshare_extension_with_duplicates::{closure#0}Unexecuted instantiation: <rustls::msgs::handshake::ClientHelloPayload>::has_keyshare_extension_with_duplicates::{closure#0} |
1386 | 0 | .unwrap_or_default() |
1387 | 0 | } Unexecuted instantiation: <rustls::msgs::handshake::ClientHelloPayload>::has_keyshare_extension_with_duplicates Unexecuted instantiation: <rustls::msgs::handshake::ClientHelloPayload>::has_keyshare_extension_with_duplicates Unexecuted instantiation: <rustls::msgs::handshake::ClientHelloPayload>::has_keyshare_extension_with_duplicates |
1388 | | |
1389 | 0 | pub(crate) fn has_certificate_compression_extension_with_duplicates(&self) -> bool { |
1390 | 0 | if let Some(algs) = &self.certificate_compression_algorithms { |
1391 | 0 | has_duplicates::<_, _, u16>(algs.iter().cloned()) |
1392 | | } else { |
1393 | 0 | false |
1394 | | } |
1395 | 0 | } Unexecuted instantiation: <rustls::msgs::handshake::ClientHelloPayload>::has_certificate_compression_extension_with_duplicates Unexecuted instantiation: <rustls::msgs::handshake::ClientHelloPayload>::has_certificate_compression_extension_with_duplicates Unexecuted instantiation: <rustls::msgs::handshake::ClientHelloPayload>::has_certificate_compression_extension_with_duplicates |
1396 | | } |
1397 | | |
1398 | | impl Codec<'_> for ClientHelloPayload { |
1399 | 0 | fn encode(&self, bytes: &mut Vec<u8>) { |
1400 | 0 | self.payload_encode(bytes, Encoding::Standard) |
1401 | 0 | } Unexecuted instantiation: <rustls::msgs::handshake::ClientHelloPayload as rustls::msgs::codec::Codec>::encode Unexecuted instantiation: <rustls::msgs::handshake::ClientHelloPayload as rustls::msgs::codec::Codec>::encode Unexecuted instantiation: <rustls::msgs::handshake::ClientHelloPayload as rustls::msgs::codec::Codec>::encode |
1402 | | |
1403 | 0 | fn read(r: &mut Reader<'_>) -> Result<Self, InvalidMessage> { |
1404 | 0 | let ret = Self { |
1405 | 0 | client_version: ProtocolVersion::read(r)?, |
1406 | 0 | random: Random::read(r)?, |
1407 | 0 | session_id: SessionId::read(r)?, |
1408 | 0 | cipher_suites: Vec::read(r)?, |
1409 | 0 | compression_methods: Vec::read(r)?, |
1410 | 0 | extensions: Box::new(ClientExtensions::read(r)?.into_owned()), |
1411 | | }; |
1412 | | |
1413 | 0 | match r.any_left() { |
1414 | 0 | true => Err(InvalidMessage::TrailingData("ClientHelloPayload")), |
1415 | 0 | false => Ok(ret), |
1416 | | } |
1417 | 0 | } Unexecuted instantiation: <rustls::msgs::handshake::ClientHelloPayload as rustls::msgs::codec::Codec>::read Unexecuted instantiation: <rustls::msgs::handshake::ClientHelloPayload as rustls::msgs::codec::Codec>::read Unexecuted instantiation: <rustls::msgs::handshake::ClientHelloPayload as rustls::msgs::codec::Codec>::read |
1418 | | } |
1419 | | |
1420 | | impl Deref for ClientHelloPayload { |
1421 | | type Target = ClientExtensions<'static>; |
1422 | 0 | fn deref(&self) -> &Self::Target { |
1423 | 0 | &self.extensions |
1424 | 0 | } Unexecuted instantiation: <rustls::msgs::handshake::ClientHelloPayload as core::ops::deref::Deref>::deref Unexecuted instantiation: <rustls::msgs::handshake::ClientHelloPayload as core::ops::deref::Deref>::deref Unexecuted instantiation: <rustls::msgs::handshake::ClientHelloPayload as core::ops::deref::Deref>::deref |
1425 | | } |
1426 | | |
1427 | | impl DerefMut for ClientHelloPayload { |
1428 | 0 | fn deref_mut(&mut self) -> &mut Self::Target { |
1429 | 0 | &mut self.extensions |
1430 | 0 | } Unexecuted instantiation: <rustls::msgs::handshake::ClientHelloPayload as core::ops::deref::DerefMut>::deref_mut Unexecuted instantiation: <rustls::msgs::handshake::ClientHelloPayload as core::ops::deref::DerefMut>::deref_mut Unexecuted instantiation: <rustls::msgs::handshake::ClientHelloPayload as core::ops::deref::DerefMut>::deref_mut |
1431 | | } |
1432 | | |
1433 | | /// RFC8446: `CipherSuite cipher_suites<2..2^16-2>;` |
1434 | | impl TlsListElement for CipherSuite { |
1435 | | const SIZE_LEN: ListLength = ListLength::NonZeroU16 { |
1436 | | empty_error: InvalidMessage::IllegalEmptyList("CipherSuites"), |
1437 | | }; |
1438 | | } |
1439 | | |
1440 | | /// RFC5246: `CompressionMethod compression_methods<1..2^8-1>;` |
1441 | | impl TlsListElement for Compression { |
1442 | | const SIZE_LEN: ListLength = ListLength::NonZeroU8 { |
1443 | | empty_error: InvalidMessage::IllegalEmptyList("Compressions"), |
1444 | | }; |
1445 | | } |
1446 | | |
1447 | | /// draft-ietf-tls-esni-17: `ExtensionType OuterExtensions<2..254>;` |
1448 | | impl TlsListElement for ExtensionType { |
1449 | | const SIZE_LEN: ListLength = ListLength::NonZeroU8 { |
1450 | | empty_error: InvalidMessage::IllegalEmptyList("ExtensionTypes"), |
1451 | | }; |
1452 | | } |
1453 | | |
1454 | | extension_struct! { |
1455 | | /// A representation of extensions present in a `HelloRetryRequest` message |
1456 | | pub(crate) struct HelloRetryRequestExtensions<'a> { |
1457 | | ExtensionType::KeyShare => |
1458 | | pub(crate) key_share: Option<NamedGroup>, |
1459 | | |
1460 | | ExtensionType::Cookie => |
1461 | | pub(crate) cookie: Option<PayloadU16<NonEmpty>>, |
1462 | | |
1463 | | ExtensionType::SupportedVersions => |
1464 | | pub(crate) supported_versions: Option<ProtocolVersion>, |
1465 | | |
1466 | | ExtensionType::EncryptedClientHello => |
1467 | | pub(crate) encrypted_client_hello: Option<Payload<'a>>, |
1468 | | } + { |
1469 | | /// Records decoding order of records, and controls encoding order. |
1470 | | pub(crate) order: Option<Vec<ExtensionType>>, |
1471 | | } |
1472 | | } |
1473 | | |
1474 | | impl HelloRetryRequestExtensions<'_> { |
1475 | 0 | fn into_owned(self) -> HelloRetryRequestExtensions<'static> { |
1476 | 0 | let Self { |
1477 | 0 | key_share, |
1478 | 0 | cookie, |
1479 | 0 | supported_versions, |
1480 | 0 | encrypted_client_hello, |
1481 | 0 | order, |
1482 | 0 | } = self; |
1483 | | HelloRetryRequestExtensions { |
1484 | 0 | key_share, |
1485 | 0 | cookie, |
1486 | 0 | supported_versions, |
1487 | 0 | encrypted_client_hello: encrypted_client_hello.map(|x| x.into_owned()), Unexecuted instantiation: <rustls::msgs::handshake::HelloRetryRequestExtensions>::into_owned::{closure#0}Unexecuted instantiation: <rustls::msgs::handshake::HelloRetryRequestExtensions>::into_owned::{closure#0}Unexecuted instantiation: <rustls::msgs::handshake::HelloRetryRequestExtensions>::into_owned::{closure#0} |
1488 | 0 | order, |
1489 | | } |
1490 | 0 | } Unexecuted instantiation: <rustls::msgs::handshake::HelloRetryRequestExtensions>::into_owned Unexecuted instantiation: <rustls::msgs::handshake::HelloRetryRequestExtensions>::into_owned Unexecuted instantiation: <rustls::msgs::handshake::HelloRetryRequestExtensions>::into_owned |
1491 | | } |
1492 | | |
1493 | | impl<'a> Codec<'a> for HelloRetryRequestExtensions<'a> { |
1494 | 0 | fn encode(&self, bytes: &mut Vec<u8>) { |
1495 | 0 | let extensions = LengthPrefixedBuffer::new(ListLength::U16, bytes); |
1496 | | |
1497 | 0 | for ext in self |
1498 | 0 | .order |
1499 | 0 | .as_deref() |
1500 | 0 | .unwrap_or(Self::ALL_EXTENSIONS) |
1501 | 0 | { |
1502 | 0 | self.encode_one(*ext, extensions.buf); |
1503 | 0 | } |
1504 | 0 | } Unexecuted instantiation: <rustls::msgs::handshake::HelloRetryRequestExtensions as rustls::msgs::codec::Codec>::encode Unexecuted instantiation: <rustls::msgs::handshake::HelloRetryRequestExtensions as rustls::msgs::codec::Codec>::encode Unexecuted instantiation: <rustls::msgs::handshake::HelloRetryRequestExtensions as rustls::msgs::codec::Codec>::encode |
1505 | | |
1506 | 0 | fn read(r: &mut Reader<'a>) -> Result<Self, InvalidMessage> { |
1507 | 0 | let mut out = Self::default(); |
1508 | | |
1509 | | // we must record order, so re-encoding round trips. this is needed, |
1510 | | // unfortunately, for ECH HRR confirmation |
1511 | 0 | let mut order = vec![]; |
1512 | | |
1513 | 0 | let len = usize::from(u16::read(r)?); |
1514 | 0 | let mut sub = r.sub(len)?; |
1515 | | |
1516 | 0 | while sub.any_left() { |
1517 | 0 | let typ = out.read_one(&mut sub, |_unk| { |
1518 | 0 | Err(InvalidMessage::UnknownHelloRetryRequestExtension) |
1519 | 0 | })?; Unexecuted instantiation: <rustls::msgs::handshake::HelloRetryRequestExtensions as rustls::msgs::codec::Codec>::read::{closure#0}Unexecuted instantiation: <rustls::msgs::handshake::HelloRetryRequestExtensions as rustls::msgs::codec::Codec>::read::{closure#0}Unexecuted instantiation: <rustls::msgs::handshake::HelloRetryRequestExtensions as rustls::msgs::codec::Codec>::read::{closure#0} |
1520 | | |
1521 | 0 | order.push(typ); |
1522 | | } |
1523 | | |
1524 | 0 | out.order = Some(order); |
1525 | 0 | Ok(out) |
1526 | 0 | } Unexecuted instantiation: <rustls::msgs::handshake::HelloRetryRequestExtensions as rustls::msgs::codec::Codec>::read Unexecuted instantiation: <rustls::msgs::handshake::HelloRetryRequestExtensions as rustls::msgs::codec::Codec>::read Unexecuted instantiation: <rustls::msgs::handshake::HelloRetryRequestExtensions as rustls::msgs::codec::Codec>::read |
1527 | | } |
1528 | | |
1529 | | #[derive(Clone, Debug)] |
1530 | | pub(crate) struct HelloRetryRequest { |
1531 | | pub(crate) legacy_version: ProtocolVersion, |
1532 | | pub(crate) session_id: SessionId, |
1533 | | pub(crate) cipher_suite: CipherSuite, |
1534 | | pub(crate) extensions: HelloRetryRequestExtensions<'static>, |
1535 | | } |
1536 | | |
1537 | | impl Codec<'_> for HelloRetryRequest { |
1538 | 0 | fn encode(&self, bytes: &mut Vec<u8>) { |
1539 | 0 | self.payload_encode(bytes, Encoding::Standard) |
1540 | 0 | } Unexecuted instantiation: <rustls::msgs::handshake::HelloRetryRequest as rustls::msgs::codec::Codec>::encode Unexecuted instantiation: <rustls::msgs::handshake::HelloRetryRequest as rustls::msgs::codec::Codec>::encode Unexecuted instantiation: <rustls::msgs::handshake::HelloRetryRequest as rustls::msgs::codec::Codec>::encode |
1541 | | |
1542 | 0 | fn read(r: &mut Reader<'_>) -> Result<Self, InvalidMessage> { |
1543 | 0 | let session_id = SessionId::read(r)?; |
1544 | 0 | let cipher_suite = CipherSuite::read(r)?; |
1545 | 0 | let compression = Compression::read(r)?; |
1546 | | |
1547 | 0 | if compression != Compression::Null { |
1548 | 0 | return Err(InvalidMessage::UnsupportedCompression); |
1549 | 0 | } |
1550 | | |
1551 | | Ok(Self { |
1552 | 0 | legacy_version: ProtocolVersion::Unknown(0), |
1553 | 0 | session_id, |
1554 | 0 | cipher_suite, |
1555 | 0 | extensions: HelloRetryRequestExtensions::read(r)?.into_owned(), |
1556 | | }) |
1557 | 0 | } Unexecuted instantiation: <rustls::msgs::handshake::HelloRetryRequest as rustls::msgs::codec::Codec>::read Unexecuted instantiation: <rustls::msgs::handshake::HelloRetryRequest as rustls::msgs::codec::Codec>::read Unexecuted instantiation: <rustls::msgs::handshake::HelloRetryRequest as rustls::msgs::codec::Codec>::read |
1558 | | } |
1559 | | |
1560 | | impl HelloRetryRequest { |
1561 | 0 | fn payload_encode(&self, bytes: &mut Vec<u8>, purpose: Encoding) { |
1562 | 0 | self.legacy_version.encode(bytes); |
1563 | 0 | HELLO_RETRY_REQUEST_RANDOM.encode(bytes); |
1564 | 0 | self.session_id.encode(bytes); |
1565 | 0 | self.cipher_suite.encode(bytes); |
1566 | 0 | Compression::Null.encode(bytes); |
1567 | | |
1568 | 0 | match purpose { |
1569 | | // For the purpose of ECH confirmation, the Encrypted Client Hello extension |
1570 | | // must have its payload replaced by 8 zero bytes. |
1571 | | // |
1572 | | // See draft-ietf-tls-esni-18 7.2.1: |
1573 | | // <https://datatracker.ietf.org/doc/html/draft-ietf-tls-esni-18#name-sending-helloretryrequest-2> |
1574 | | Encoding::EchConfirmation |
1575 | 0 | if self |
1576 | 0 | .extensions |
1577 | 0 | .encrypted_client_hello |
1578 | 0 | .is_some() => |
1579 | 0 | { |
1580 | 0 | let hrr_confirmation = [0u8; 8]; |
1581 | 0 | HelloRetryRequestExtensions { |
1582 | 0 | encrypted_client_hello: Some(Payload::Borrowed(&hrr_confirmation)), |
1583 | 0 | ..self.extensions.clone() |
1584 | 0 | } |
1585 | 0 | .encode(bytes); |
1586 | 0 | } |
1587 | 0 | _ => self.extensions.encode(bytes), |
1588 | | } |
1589 | 0 | } Unexecuted instantiation: <rustls::msgs::handshake::HelloRetryRequest>::payload_encode Unexecuted instantiation: <rustls::msgs::handshake::HelloRetryRequest>::payload_encode Unexecuted instantiation: <rustls::msgs::handshake::HelloRetryRequest>::payload_encode |
1590 | | } |
1591 | | |
1592 | | impl Deref for HelloRetryRequest { |
1593 | | type Target = HelloRetryRequestExtensions<'static>; |
1594 | 0 | fn deref(&self) -> &Self::Target { |
1595 | 0 | &self.extensions |
1596 | 0 | } Unexecuted instantiation: <rustls::msgs::handshake::HelloRetryRequest as core::ops::deref::Deref>::deref Unexecuted instantiation: <rustls::msgs::handshake::HelloRetryRequest as core::ops::deref::Deref>::deref Unexecuted instantiation: <rustls::msgs::handshake::HelloRetryRequest as core::ops::deref::Deref>::deref |
1597 | | } |
1598 | | |
1599 | | impl DerefMut for HelloRetryRequest { |
1600 | 0 | fn deref_mut(&mut self) -> &mut Self::Target { |
1601 | 0 | &mut self.extensions |
1602 | 0 | } Unexecuted instantiation: <rustls::msgs::handshake::HelloRetryRequest as core::ops::deref::DerefMut>::deref_mut Unexecuted instantiation: <rustls::msgs::handshake::HelloRetryRequest as core::ops::deref::DerefMut>::deref_mut Unexecuted instantiation: <rustls::msgs::handshake::HelloRetryRequest as core::ops::deref::DerefMut>::deref_mut |
1603 | | } |
1604 | | |
1605 | | #[derive(Clone, Debug)] |
1606 | | pub(crate) struct ServerHelloPayload { |
1607 | | pub(crate) legacy_version: ProtocolVersion, |
1608 | | pub(crate) random: Random, |
1609 | | pub(crate) session_id: SessionId, |
1610 | | pub(crate) cipher_suite: CipherSuite, |
1611 | | pub(crate) compression_method: Compression, |
1612 | | pub(crate) extensions: Box<ServerExtensions<'static>>, |
1613 | | } |
1614 | | |
1615 | | impl Codec<'_> for ServerHelloPayload { |
1616 | 0 | fn encode(&self, bytes: &mut Vec<u8>) { |
1617 | 0 | self.payload_encode(bytes, Encoding::Standard) |
1618 | 0 | } Unexecuted instantiation: <rustls::msgs::handshake::ServerHelloPayload as rustls::msgs::codec::Codec>::encode Unexecuted instantiation: <rustls::msgs::handshake::ServerHelloPayload as rustls::msgs::codec::Codec>::encode Unexecuted instantiation: <rustls::msgs::handshake::ServerHelloPayload as rustls::msgs::codec::Codec>::encode |
1619 | | |
1620 | | // minus version and random, which have already been read. |
1621 | 0 | fn read(r: &mut Reader<'_>) -> Result<Self, InvalidMessage> { |
1622 | 0 | let session_id = SessionId::read(r)?; |
1623 | 0 | let suite = CipherSuite::read(r)?; |
1624 | 0 | let compression = Compression::read(r)?; |
1625 | | |
1626 | | // RFC5246: |
1627 | | // "The presence of extensions can be detected by determining whether |
1628 | | // there are bytes following the compression_method field at the end of |
1629 | | // the ServerHello." |
1630 | 0 | let extensions = Box::new( |
1631 | 0 | if r.any_left() { |
1632 | 0 | ServerExtensions::read(r)? |
1633 | | } else { |
1634 | 0 | ServerExtensions::default() |
1635 | | } |
1636 | 0 | .into_owned(), |
1637 | | ); |
1638 | | |
1639 | 0 | let ret = Self { |
1640 | 0 | legacy_version: ProtocolVersion::Unknown(0), |
1641 | 0 | random: ZERO_RANDOM, |
1642 | 0 | session_id, |
1643 | 0 | cipher_suite: suite, |
1644 | 0 | compression_method: compression, |
1645 | 0 | extensions, |
1646 | 0 | }; |
1647 | | |
1648 | 0 | r.expect_empty("ServerHelloPayload") |
1649 | 0 | .map(|_| ret) |
1650 | 0 | } Unexecuted instantiation: <rustls::msgs::handshake::ServerHelloPayload as rustls::msgs::codec::Codec>::read Unexecuted instantiation: <rustls::msgs::handshake::ServerHelloPayload as rustls::msgs::codec::Codec>::read Unexecuted instantiation: <rustls::msgs::handshake::ServerHelloPayload as rustls::msgs::codec::Codec>::read |
1651 | | } |
1652 | | |
1653 | | impl ServerHelloPayload { |
1654 | 0 | fn payload_encode(&self, bytes: &mut Vec<u8>, encoding: Encoding) { |
1655 | 0 | debug_assert!( |
1656 | 0 | !matches!(encoding, Encoding::EchConfirmation), |
1657 | 0 | "we cannot compute an ECH confirmation on a received ServerHello" |
1658 | | ); |
1659 | | |
1660 | 0 | self.legacy_version.encode(bytes); |
1661 | 0 | self.random.encode(bytes); |
1662 | 0 | self.session_id.encode(bytes); |
1663 | 0 | self.cipher_suite.encode(bytes); |
1664 | 0 | self.compression_method.encode(bytes); |
1665 | 0 | self.extensions.encode(bytes); |
1666 | 0 | } Unexecuted instantiation: <rustls::msgs::handshake::ServerHelloPayload>::payload_encode Unexecuted instantiation: <rustls::msgs::handshake::ServerHelloPayload>::payload_encode Unexecuted instantiation: <rustls::msgs::handshake::ServerHelloPayload>::payload_encode |
1667 | | } |
1668 | | |
1669 | | impl Deref for ServerHelloPayload { |
1670 | | type Target = ServerExtensions<'static>; |
1671 | 0 | fn deref(&self) -> &Self::Target { |
1672 | 0 | &self.extensions |
1673 | 0 | } Unexecuted instantiation: <rustls::msgs::handshake::ServerHelloPayload as core::ops::deref::Deref>::deref Unexecuted instantiation: <rustls::msgs::handshake::ServerHelloPayload as core::ops::deref::Deref>::deref Unexecuted instantiation: <rustls::msgs::handshake::ServerHelloPayload as core::ops::deref::Deref>::deref |
1674 | | } |
1675 | | |
1676 | | impl DerefMut for ServerHelloPayload { |
1677 | 0 | fn deref_mut(&mut self) -> &mut Self::Target { |
1678 | 0 | &mut self.extensions |
1679 | 0 | } Unexecuted instantiation: <rustls::msgs::handshake::ServerHelloPayload as core::ops::deref::DerefMut>::deref_mut Unexecuted instantiation: <rustls::msgs::handshake::ServerHelloPayload as core::ops::deref::DerefMut>::deref_mut Unexecuted instantiation: <rustls::msgs::handshake::ServerHelloPayload as core::ops::deref::DerefMut>::deref_mut |
1680 | | } |
1681 | | |
1682 | | #[derive(Clone, Default, Debug)] |
1683 | | pub(crate) struct CertificateChain<'a>(pub(crate) Vec<CertificateDer<'a>>); |
1684 | | |
1685 | | impl CertificateChain<'_> { |
1686 | 0 | pub(crate) fn into_owned(self) -> CertificateChain<'static> { |
1687 | | CertificateChain( |
1688 | 0 | self.0 |
1689 | 0 | .into_iter() |
1690 | 0 | .map(|c| c.into_owned()) Unexecuted instantiation: <rustls::msgs::handshake::CertificateChain>::into_owned::{closure#0}Unexecuted instantiation: <rustls::msgs::handshake::CertificateChain>::into_owned::{closure#0}Unexecuted instantiation: <rustls::msgs::handshake::CertificateChain>::into_owned::{closure#0} |
1691 | 0 | .collect(), |
1692 | | ) |
1693 | 0 | } Unexecuted instantiation: <rustls::msgs::handshake::CertificateChain>::into_owned Unexecuted instantiation: <rustls::msgs::handshake::CertificateChain>::into_owned Unexecuted instantiation: <rustls::msgs::handshake::CertificateChain>::into_owned |
1694 | | } |
1695 | | |
1696 | | impl<'a> Codec<'a> for CertificateChain<'a> { |
1697 | 0 | fn encode(&self, bytes: &mut Vec<u8>) { |
1698 | 0 | Vec::encode(&self.0, bytes) |
1699 | 0 | } Unexecuted instantiation: <rustls::msgs::handshake::CertificateChain as rustls::msgs::codec::Codec>::encode Unexecuted instantiation: <rustls::msgs::handshake::CertificateChain as rustls::msgs::codec::Codec>::encode Unexecuted instantiation: <rustls::msgs::handshake::CertificateChain as rustls::msgs::codec::Codec>::encode |
1700 | | |
1701 | 0 | fn read(r: &mut Reader<'a>) -> Result<Self, InvalidMessage> { |
1702 | 0 | Vec::read(r).map(Self) |
1703 | 0 | } Unexecuted instantiation: <rustls::msgs::handshake::CertificateChain as rustls::msgs::codec::Codec>::read Unexecuted instantiation: <rustls::msgs::handshake::CertificateChain as rustls::msgs::codec::Codec>::read Unexecuted instantiation: <rustls::msgs::handshake::CertificateChain as rustls::msgs::codec::Codec>::read |
1704 | | } |
1705 | | |
1706 | | impl<'a> Deref for CertificateChain<'a> { |
1707 | | type Target = [CertificateDer<'a>]; |
1708 | | |
1709 | 0 | fn deref(&self) -> &[CertificateDer<'a>] { |
1710 | 0 | &self.0 |
1711 | 0 | } Unexecuted instantiation: <rustls::msgs::handshake::CertificateChain as core::ops::deref::Deref>::deref Unexecuted instantiation: <rustls::msgs::handshake::CertificateChain as core::ops::deref::Deref>::deref Unexecuted instantiation: <rustls::msgs::handshake::CertificateChain as core::ops::deref::Deref>::deref |
1712 | | } |
1713 | | |
1714 | | impl TlsListElement for CertificateDer<'_> { |
1715 | | const SIZE_LEN: ListLength = ListLength::U24 { |
1716 | | max: CERTIFICATE_MAX_SIZE_LIMIT, |
1717 | | error: InvalidMessage::CertificatePayloadTooLarge, |
1718 | | }; |
1719 | | } |
1720 | | |
1721 | | /// TLS has a 16MB size limit on any handshake message, |
1722 | | /// plus a 16MB limit on any given certificate. |
1723 | | /// |
1724 | | /// We contract that to 64KB to limit the amount of memory allocation |
1725 | | /// that is directly controllable by the peer. |
1726 | | pub(crate) const CERTIFICATE_MAX_SIZE_LIMIT: usize = 0x1_0000; |
1727 | | |
1728 | | extension_struct! { |
1729 | | pub(crate) struct CertificateExtensions<'a> { |
1730 | | ExtensionType::StatusRequest => |
1731 | | pub(crate) status: Option<CertificateStatus<'a>>, |
1732 | | } |
1733 | | } |
1734 | | |
1735 | | impl CertificateExtensions<'_> { |
1736 | 0 | fn into_owned(self) -> CertificateExtensions<'static> { |
1737 | | CertificateExtensions { |
1738 | 0 | status: self.status.map(|s| s.into_owned()), Unexecuted instantiation: <rustls::msgs::handshake::CertificateExtensions>::into_owned::{closure#0}Unexecuted instantiation: <rustls::msgs::handshake::CertificateExtensions>::into_owned::{closure#0}Unexecuted instantiation: <rustls::msgs::handshake::CertificateExtensions>::into_owned::{closure#0} |
1739 | | } |
1740 | 0 | } Unexecuted instantiation: <rustls::msgs::handshake::CertificateExtensions>::into_owned Unexecuted instantiation: <rustls::msgs::handshake::CertificateExtensions>::into_owned Unexecuted instantiation: <rustls::msgs::handshake::CertificateExtensions>::into_owned |
1741 | | } |
1742 | | |
1743 | | impl<'a> Codec<'a> for CertificateExtensions<'a> { |
1744 | 0 | fn encode(&self, bytes: &mut Vec<u8>) { |
1745 | 0 | let extensions = LengthPrefixedBuffer::new(ListLength::U16, bytes); |
1746 | | |
1747 | 0 | for ext in Self::ALL_EXTENSIONS { |
1748 | 0 | self.encode_one(*ext, extensions.buf); |
1749 | 0 | } |
1750 | 0 | } Unexecuted instantiation: <rustls::msgs::handshake::CertificateExtensions as rustls::msgs::codec::Codec>::encode Unexecuted instantiation: <rustls::msgs::handshake::CertificateExtensions as rustls::msgs::codec::Codec>::encode Unexecuted instantiation: <rustls::msgs::handshake::CertificateExtensions as rustls::msgs::codec::Codec>::encode |
1751 | | |
1752 | 0 | fn read(r: &mut Reader<'a>) -> Result<Self, InvalidMessage> { |
1753 | 0 | let mut out = Self::default(); |
1754 | | |
1755 | 0 | let len = usize::from(u16::read(r)?); |
1756 | 0 | let mut sub = r.sub(len)?; |
1757 | | |
1758 | 0 | while sub.any_left() { |
1759 | 0 | out.read_one(&mut sub, |_unk| { |
1760 | 0 | Err(InvalidMessage::UnknownCertificateExtension) |
1761 | 0 | })?; Unexecuted instantiation: <rustls::msgs::handshake::CertificateExtensions as rustls::msgs::codec::Codec>::read::{closure#0}Unexecuted instantiation: <rustls::msgs::handshake::CertificateExtensions as rustls::msgs::codec::Codec>::read::{closure#0}Unexecuted instantiation: <rustls::msgs::handshake::CertificateExtensions as rustls::msgs::codec::Codec>::read::{closure#0} |
1762 | | } |
1763 | | |
1764 | 0 | Ok(out) |
1765 | 0 | } Unexecuted instantiation: <rustls::msgs::handshake::CertificateExtensions as rustls::msgs::codec::Codec>::read Unexecuted instantiation: <rustls::msgs::handshake::CertificateExtensions as rustls::msgs::codec::Codec>::read Unexecuted instantiation: <rustls::msgs::handshake::CertificateExtensions as rustls::msgs::codec::Codec>::read |
1766 | | } |
1767 | | |
1768 | | #[derive(Debug)] |
1769 | | pub(crate) struct CertificateEntry<'a> { |
1770 | | pub(crate) cert: CertificateDer<'a>, |
1771 | | pub(crate) extensions: CertificateExtensions<'a>, |
1772 | | } |
1773 | | |
1774 | | impl<'a> Codec<'a> for CertificateEntry<'a> { |
1775 | 0 | fn encode(&self, bytes: &mut Vec<u8>) { |
1776 | 0 | self.cert.encode(bytes); |
1777 | 0 | self.extensions.encode(bytes); |
1778 | 0 | } Unexecuted instantiation: <rustls::msgs::handshake::CertificateEntry as rustls::msgs::codec::Codec>::encode Unexecuted instantiation: <rustls::msgs::handshake::CertificateEntry as rustls::msgs::codec::Codec>::encode Unexecuted instantiation: <rustls::msgs::handshake::CertificateEntry as rustls::msgs::codec::Codec>::encode |
1779 | | |
1780 | 0 | fn read(r: &mut Reader<'a>) -> Result<Self, InvalidMessage> { |
1781 | | Ok(Self { |
1782 | 0 | cert: CertificateDer::read(r)?, |
1783 | 0 | extensions: CertificateExtensions::read(r)?.into_owned(), |
1784 | | }) |
1785 | 0 | } Unexecuted instantiation: <rustls::msgs::handshake::CertificateEntry as rustls::msgs::codec::Codec>::read Unexecuted instantiation: <rustls::msgs::handshake::CertificateEntry as rustls::msgs::codec::Codec>::read Unexecuted instantiation: <rustls::msgs::handshake::CertificateEntry as rustls::msgs::codec::Codec>::read |
1786 | | } |
1787 | | |
1788 | | impl<'a> CertificateEntry<'a> { |
1789 | 0 | pub(crate) fn new(cert: CertificateDer<'a>) -> Self { |
1790 | 0 | Self { |
1791 | 0 | cert, |
1792 | 0 | extensions: CertificateExtensions::default(), |
1793 | 0 | } |
1794 | 0 | } Unexecuted instantiation: <rustls::msgs::handshake::CertificateEntry>::new Unexecuted instantiation: <rustls::msgs::handshake::CertificateEntry>::new Unexecuted instantiation: <rustls::msgs::handshake::CertificateEntry>::new |
1795 | | |
1796 | 0 | pub(crate) fn into_owned(self) -> CertificateEntry<'static> { |
1797 | 0 | CertificateEntry { |
1798 | 0 | cert: self.cert.into_owned(), |
1799 | 0 | extensions: self.extensions.into_owned(), |
1800 | 0 | } |
1801 | 0 | } Unexecuted instantiation: <rustls::msgs::handshake::CertificateEntry>::into_owned Unexecuted instantiation: <rustls::msgs::handshake::CertificateEntry>::into_owned Unexecuted instantiation: <rustls::msgs::handshake::CertificateEntry>::into_owned |
1802 | | } |
1803 | | |
1804 | | impl TlsListElement for CertificateEntry<'_> { |
1805 | | const SIZE_LEN: ListLength = ListLength::U24 { |
1806 | | max: CERTIFICATE_MAX_SIZE_LIMIT, |
1807 | | error: InvalidMessage::CertificatePayloadTooLarge, |
1808 | | }; |
1809 | | } |
1810 | | |
1811 | | #[derive(Debug)] |
1812 | | pub(crate) struct CertificatePayloadTls13<'a> { |
1813 | | pub(crate) context: PayloadU8, |
1814 | | pub(crate) entries: Vec<CertificateEntry<'a>>, |
1815 | | } |
1816 | | |
1817 | | impl<'a> Codec<'a> for CertificatePayloadTls13<'a> { |
1818 | 0 | fn encode(&self, bytes: &mut Vec<u8>) { |
1819 | 0 | self.context.encode(bytes); |
1820 | 0 | self.entries.encode(bytes); |
1821 | 0 | } Unexecuted instantiation: <rustls::msgs::handshake::CertificatePayloadTls13 as rustls::msgs::codec::Codec>::encode Unexecuted instantiation: <rustls::msgs::handshake::CertificatePayloadTls13 as rustls::msgs::codec::Codec>::encode Unexecuted instantiation: <rustls::msgs::handshake::CertificatePayloadTls13 as rustls::msgs::codec::Codec>::encode |
1822 | | |
1823 | 0 | fn read(r: &mut Reader<'a>) -> Result<Self, InvalidMessage> { |
1824 | | Ok(Self { |
1825 | 0 | context: PayloadU8::read(r)?, |
1826 | 0 | entries: Vec::read(r)?, |
1827 | | }) |
1828 | 0 | } Unexecuted instantiation: <rustls::msgs::handshake::CertificatePayloadTls13 as rustls::msgs::codec::Codec>::read Unexecuted instantiation: <rustls::msgs::handshake::CertificatePayloadTls13 as rustls::msgs::codec::Codec>::read Unexecuted instantiation: <rustls::msgs::handshake::CertificatePayloadTls13 as rustls::msgs::codec::Codec>::read |
1829 | | } |
1830 | | |
1831 | | impl<'a> CertificatePayloadTls13<'a> { |
1832 | 0 | pub(crate) fn new( |
1833 | 0 | certs: impl Iterator<Item = &'a CertificateDer<'a>>, |
1834 | 0 | ocsp_response: Option<&'a [u8]>, |
1835 | 0 | ) -> Self { |
1836 | | Self { |
1837 | 0 | context: PayloadU8::empty(), |
1838 | 0 | entries: certs |
1839 | | // zip certificate iterator with `ocsp_response` followed by |
1840 | | // an infinite-length iterator of `None`. |
1841 | 0 | .zip( |
1842 | 0 | ocsp_response |
1843 | 0 | .into_iter() |
1844 | 0 | .map(Some) |
1845 | 0 | .chain(iter::repeat(None)), |
1846 | | ) |
1847 | 0 | .map(|(cert, ocsp)| { |
1848 | 0 | let mut e = CertificateEntry::new(cert.clone()); |
1849 | 0 | if let Some(ocsp) = ocsp { |
1850 | 0 | e.extensions.status = Some(CertificateStatus::new(ocsp)); |
1851 | 0 | } |
1852 | 0 | e |
1853 | 0 | }) Unexecuted instantiation: <rustls::msgs::handshake::CertificatePayloadTls13>::new::<core::slice::iter::Iter<rustls_pki_types::CertificateDer>>::{closure#0}Unexecuted instantiation: <rustls::msgs::handshake::CertificatePayloadTls13>::new::<core::slice::iter::Iter<rustls_pki_types::CertificateDer>>::{closure#0}Unexecuted instantiation: <rustls::msgs::handshake::CertificatePayloadTls13>::new::<core::slice::iter::Iter<rustls_pki_types::CertificateDer>>::{closure#0} |
1854 | 0 | .collect(), |
1855 | | } |
1856 | 0 | } Unexecuted instantiation: <rustls::msgs::handshake::CertificatePayloadTls13>::new::<core::slice::iter::Iter<rustls_pki_types::CertificateDer>> Unexecuted instantiation: <rustls::msgs::handshake::CertificatePayloadTls13>::new::<core::slice::iter::Iter<rustls_pki_types::CertificateDer>> Unexecuted instantiation: <rustls::msgs::handshake::CertificatePayloadTls13>::new::<core::slice::iter::Iter<rustls_pki_types::CertificateDer>> |
1857 | | |
1858 | 0 | pub(crate) fn into_owned(self) -> CertificatePayloadTls13<'static> { |
1859 | 0 | CertificatePayloadTls13 { |
1860 | 0 | context: self.context, |
1861 | 0 | entries: self |
1862 | 0 | .entries |
1863 | 0 | .into_iter() |
1864 | 0 | .map(CertificateEntry::into_owned) |
1865 | 0 | .collect(), |
1866 | 0 | } |
1867 | 0 | } Unexecuted instantiation: <rustls::msgs::handshake::CertificatePayloadTls13>::into_owned Unexecuted instantiation: <rustls::msgs::handshake::CertificatePayloadTls13>::into_owned Unexecuted instantiation: <rustls::msgs::handshake::CertificatePayloadTls13>::into_owned |
1868 | | |
1869 | 0 | pub(crate) fn end_entity_ocsp(&self) -> Vec<u8> { |
1870 | 0 | let Some(entry) = self.entries.first() else { |
1871 | 0 | return vec![]; |
1872 | | }; |
1873 | 0 | entry |
1874 | 0 | .extensions |
1875 | 0 | .status |
1876 | 0 | .as_ref() |
1877 | 0 | .map(|status| { |
1878 | 0 | status |
1879 | 0 | .ocsp_response |
1880 | 0 | .0 |
1881 | 0 | .clone() |
1882 | 0 | .into_vec() |
1883 | 0 | }) Unexecuted instantiation: <rustls::msgs::handshake::CertificatePayloadTls13>::end_entity_ocsp::{closure#0}Unexecuted instantiation: <rustls::msgs::handshake::CertificatePayloadTls13>::end_entity_ocsp::{closure#0}Unexecuted instantiation: <rustls::msgs::handshake::CertificatePayloadTls13>::end_entity_ocsp::{closure#0} |
1884 | 0 | .unwrap_or_default() |
1885 | 0 | } Unexecuted instantiation: <rustls::msgs::handshake::CertificatePayloadTls13>::end_entity_ocsp Unexecuted instantiation: <rustls::msgs::handshake::CertificatePayloadTls13>::end_entity_ocsp Unexecuted instantiation: <rustls::msgs::handshake::CertificatePayloadTls13>::end_entity_ocsp |
1886 | | |
1887 | 0 | pub(crate) fn into_certificate_chain(self) -> CertificateChain<'a> { |
1888 | | CertificateChain( |
1889 | 0 | self.entries |
1890 | 0 | .into_iter() |
1891 | 0 | .map(|e| e.cert) |
1892 | 0 | .collect(), |
1893 | | ) |
1894 | 0 | } Unexecuted instantiation: <rustls::msgs::handshake::CertificatePayloadTls13>::into_certificate_chain Unexecuted instantiation: <rustls::msgs::handshake::CertificatePayloadTls13>::into_certificate_chain Unexecuted instantiation: <rustls::msgs::handshake::CertificatePayloadTls13>::into_certificate_chain |
1895 | | } |
1896 | | |
1897 | | /// Describes supported key exchange mechanisms. |
1898 | | #[derive(Clone, Copy, Debug, PartialEq)] |
1899 | | #[non_exhaustive] |
1900 | | pub enum KeyExchangeAlgorithm { |
1901 | | /// Diffie-Hellman Key exchange (with only known parameters as defined in [RFC 7919]). |
1902 | | /// |
1903 | | /// [RFC 7919]: https://datatracker.ietf.org/doc/html/rfc7919 |
1904 | | DHE, |
1905 | | /// Key exchange performed via elliptic curve Diffie-Hellman. |
1906 | | ECDHE, |
1907 | | } |
1908 | | |
1909 | | pub(crate) static ALL_KEY_EXCHANGE_ALGORITHMS: &[KeyExchangeAlgorithm] = |
1910 | | &[KeyExchangeAlgorithm::ECDHE, KeyExchangeAlgorithm::DHE]; |
1911 | | |
1912 | | // We don't support arbitrary curves. It's a terrible |
1913 | | // idea and unnecessary attack surface. Please, |
1914 | | // get a grip. |
1915 | | #[derive(Debug)] |
1916 | | pub(crate) struct EcParameters { |
1917 | | pub(crate) curve_type: ECCurveType, |
1918 | | pub(crate) named_group: NamedGroup, |
1919 | | } |
1920 | | |
1921 | | impl Codec<'_> for EcParameters { |
1922 | 0 | fn encode(&self, bytes: &mut Vec<u8>) { |
1923 | 0 | self.curve_type.encode(bytes); |
1924 | 0 | self.named_group.encode(bytes); |
1925 | 0 | } Unexecuted instantiation: <rustls::msgs::handshake::EcParameters as rustls::msgs::codec::Codec>::encode Unexecuted instantiation: <rustls::msgs::handshake::EcParameters as rustls::msgs::codec::Codec>::encode Unexecuted instantiation: <rustls::msgs::handshake::EcParameters as rustls::msgs::codec::Codec>::encode |
1926 | | |
1927 | 0 | fn read(r: &mut Reader<'_>) -> Result<Self, InvalidMessage> { |
1928 | 0 | let ct = ECCurveType::read(r)?; |
1929 | 0 | if ct != ECCurveType::NamedCurve { |
1930 | 0 | return Err(InvalidMessage::UnsupportedCurveType); |
1931 | 0 | } |
1932 | | |
1933 | 0 | let grp = NamedGroup::read(r)?; |
1934 | | |
1935 | 0 | Ok(Self { |
1936 | 0 | curve_type: ct, |
1937 | 0 | named_group: grp, |
1938 | 0 | }) |
1939 | 0 | } Unexecuted instantiation: <rustls::msgs::handshake::EcParameters as rustls::msgs::codec::Codec>::read Unexecuted instantiation: <rustls::msgs::handshake::EcParameters as rustls::msgs::codec::Codec>::read Unexecuted instantiation: <rustls::msgs::handshake::EcParameters as rustls::msgs::codec::Codec>::read |
1940 | | } |
1941 | | |
1942 | | #[cfg(feature = "tls12")] |
1943 | | pub(crate) trait KxDecode<'a>: fmt::Debug + Sized { |
1944 | | /// Decode a key exchange message given the key_exchange `algo` |
1945 | | fn decode(r: &mut Reader<'a>, algo: KeyExchangeAlgorithm) -> Result<Self, InvalidMessage>; |
1946 | | } |
1947 | | |
1948 | | #[cfg(feature = "tls12")] |
1949 | | #[derive(Debug)] |
1950 | | pub(crate) enum ClientKeyExchangeParams { |
1951 | | Ecdh(ClientEcdhParams), |
1952 | | Dh(ClientDhParams), |
1953 | | } |
1954 | | |
1955 | | #[cfg(feature = "tls12")] |
1956 | | impl ClientKeyExchangeParams { |
1957 | | pub(crate) fn pub_key(&self) -> &[u8] { |
1958 | | match self { |
1959 | | Self::Ecdh(ecdh) => &ecdh.public.0, |
1960 | | Self::Dh(dh) => &dh.public.0, |
1961 | | } |
1962 | | } |
1963 | | |
1964 | | pub(crate) fn encode(&self, buf: &mut Vec<u8>) { |
1965 | | match self { |
1966 | | Self::Ecdh(ecdh) => ecdh.encode(buf), |
1967 | | Self::Dh(dh) => dh.encode(buf), |
1968 | | } |
1969 | | } |
1970 | | } |
1971 | | |
1972 | | #[cfg(feature = "tls12")] |
1973 | | impl KxDecode<'_> for ClientKeyExchangeParams { |
1974 | | fn decode(r: &mut Reader<'_>, algo: KeyExchangeAlgorithm) -> Result<Self, InvalidMessage> { |
1975 | | use KeyExchangeAlgorithm::*; |
1976 | | Ok(match algo { |
1977 | | ECDHE => Self::Ecdh(ClientEcdhParams::read(r)?), |
1978 | | DHE => Self::Dh(ClientDhParams::read(r)?), |
1979 | | }) |
1980 | | } |
1981 | | } |
1982 | | |
1983 | | #[cfg(feature = "tls12")] |
1984 | | #[derive(Debug)] |
1985 | | pub(crate) struct ClientEcdhParams { |
1986 | | /// RFC4492: `opaque point <1..2^8-1>;` |
1987 | | pub(crate) public: PayloadU8<NonEmpty>, |
1988 | | } |
1989 | | |
1990 | | #[cfg(feature = "tls12")] |
1991 | | impl Codec<'_> for ClientEcdhParams { |
1992 | | fn encode(&self, bytes: &mut Vec<u8>) { |
1993 | | self.public.encode(bytes); |
1994 | | } |
1995 | | |
1996 | | fn read(r: &mut Reader<'_>) -> Result<Self, InvalidMessage> { |
1997 | | let pb = PayloadU8::read(r)?; |
1998 | | Ok(Self { public: pb }) |
1999 | | } |
2000 | | } |
2001 | | |
2002 | | #[cfg(feature = "tls12")] |
2003 | | #[derive(Debug)] |
2004 | | pub(crate) struct ClientDhParams { |
2005 | | /// RFC5246: `opaque dh_Yc<1..2^16-1>;` |
2006 | | pub(crate) public: PayloadU16<NonEmpty>, |
2007 | | } |
2008 | | |
2009 | | #[cfg(feature = "tls12")] |
2010 | | impl Codec<'_> for ClientDhParams { |
2011 | | fn encode(&self, bytes: &mut Vec<u8>) { |
2012 | | self.public.encode(bytes); |
2013 | | } |
2014 | | |
2015 | | fn read(r: &mut Reader<'_>) -> Result<Self, InvalidMessage> { |
2016 | | Ok(Self { |
2017 | | public: PayloadU16::read(r)?, |
2018 | | }) |
2019 | | } |
2020 | | } |
2021 | | |
2022 | | #[derive(Debug)] |
2023 | | pub(crate) struct ServerEcdhParams { |
2024 | | pub(crate) curve_params: EcParameters, |
2025 | | /// RFC4492: `opaque point <1..2^8-1>;` |
2026 | | pub(crate) public: PayloadU8<NonEmpty>, |
2027 | | } |
2028 | | |
2029 | | impl ServerEcdhParams { |
2030 | | #[cfg(feature = "tls12")] |
2031 | | pub(crate) fn new(kx: &dyn ActiveKeyExchange) -> Self { |
2032 | | Self { |
2033 | | curve_params: EcParameters { |
2034 | | curve_type: ECCurveType::NamedCurve, |
2035 | | named_group: kx.group(), |
2036 | | }, |
2037 | | public: PayloadU8::new(kx.pub_key().to_vec()), |
2038 | | } |
2039 | | } |
2040 | | } |
2041 | | |
2042 | | impl Codec<'_> for ServerEcdhParams { |
2043 | 0 | fn encode(&self, bytes: &mut Vec<u8>) { |
2044 | 0 | self.curve_params.encode(bytes); |
2045 | 0 | self.public.encode(bytes); |
2046 | 0 | } Unexecuted instantiation: <rustls::msgs::handshake::ServerEcdhParams as rustls::msgs::codec::Codec>::encode Unexecuted instantiation: <rustls::msgs::handshake::ServerEcdhParams as rustls::msgs::codec::Codec>::encode Unexecuted instantiation: <rustls::msgs::handshake::ServerEcdhParams as rustls::msgs::codec::Codec>::encode |
2047 | | |
2048 | 0 | fn read(r: &mut Reader<'_>) -> Result<Self, InvalidMessage> { |
2049 | 0 | let cp = EcParameters::read(r)?; |
2050 | 0 | let pb = PayloadU8::read(r)?; |
2051 | | |
2052 | 0 | Ok(Self { |
2053 | 0 | curve_params: cp, |
2054 | 0 | public: pb, |
2055 | 0 | }) |
2056 | 0 | } Unexecuted instantiation: <rustls::msgs::handshake::ServerEcdhParams as rustls::msgs::codec::Codec>::read Unexecuted instantiation: <rustls::msgs::handshake::ServerEcdhParams as rustls::msgs::codec::Codec>::read Unexecuted instantiation: <rustls::msgs::handshake::ServerEcdhParams as rustls::msgs::codec::Codec>::read |
2057 | | } |
2058 | | |
2059 | | #[derive(Debug)] |
2060 | | #[allow(non_snake_case)] |
2061 | | pub(crate) struct ServerDhParams { |
2062 | | /// RFC5246: `opaque dh_p<1..2^16-1>;` |
2063 | | pub(crate) dh_p: PayloadU16<NonEmpty>, |
2064 | | /// RFC5246: `opaque dh_g<1..2^16-1>;` |
2065 | | pub(crate) dh_g: PayloadU16<NonEmpty>, |
2066 | | /// RFC5246: `opaque dh_Ys<1..2^16-1>;` |
2067 | | pub(crate) dh_Ys: PayloadU16<NonEmpty>, |
2068 | | } |
2069 | | |
2070 | | impl ServerDhParams { |
2071 | | #[cfg(feature = "tls12")] |
2072 | | pub(crate) fn new(kx: &dyn ActiveKeyExchange) -> Self { |
2073 | | let Some(params) = kx.ffdhe_group() else { |
2074 | | panic!("invalid NamedGroup for DHE key exchange: {:?}", kx.group()); |
2075 | | }; |
2076 | | |
2077 | | Self { |
2078 | | dh_p: PayloadU16::new(params.p.to_vec()), |
2079 | | dh_g: PayloadU16::new(params.g.to_vec()), |
2080 | | dh_Ys: PayloadU16::new(kx.pub_key().to_vec()), |
2081 | | } |
2082 | | } |
2083 | | |
2084 | | #[cfg(feature = "tls12")] |
2085 | | pub(crate) fn as_ffdhe_group(&self) -> FfdheGroup<'_> { |
2086 | | FfdheGroup::from_params_trimming_leading_zeros(&self.dh_p.0, &self.dh_g.0) |
2087 | | } |
2088 | | } |
2089 | | |
2090 | | impl Codec<'_> for ServerDhParams { |
2091 | 0 | fn encode(&self, bytes: &mut Vec<u8>) { |
2092 | 0 | self.dh_p.encode(bytes); |
2093 | 0 | self.dh_g.encode(bytes); |
2094 | 0 | self.dh_Ys.encode(bytes); |
2095 | 0 | } Unexecuted instantiation: <rustls::msgs::handshake::ServerDhParams as rustls::msgs::codec::Codec>::encode Unexecuted instantiation: <rustls::msgs::handshake::ServerDhParams as rustls::msgs::codec::Codec>::encode Unexecuted instantiation: <rustls::msgs::handshake::ServerDhParams as rustls::msgs::codec::Codec>::encode |
2096 | | |
2097 | 0 | fn read(r: &mut Reader<'_>) -> Result<Self, InvalidMessage> { |
2098 | | Ok(Self { |
2099 | 0 | dh_p: PayloadU16::read(r)?, |
2100 | 0 | dh_g: PayloadU16::read(r)?, |
2101 | 0 | dh_Ys: PayloadU16::read(r)?, |
2102 | | }) |
2103 | 0 | } Unexecuted instantiation: <rustls::msgs::handshake::ServerDhParams as rustls::msgs::codec::Codec>::read Unexecuted instantiation: <rustls::msgs::handshake::ServerDhParams as rustls::msgs::codec::Codec>::read Unexecuted instantiation: <rustls::msgs::handshake::ServerDhParams as rustls::msgs::codec::Codec>::read |
2104 | | } |
2105 | | |
2106 | | #[allow(dead_code)] |
2107 | | #[derive(Debug)] |
2108 | | pub(crate) enum ServerKeyExchangeParams { |
2109 | | Ecdh(ServerEcdhParams), |
2110 | | Dh(ServerDhParams), |
2111 | | } |
2112 | | |
2113 | | impl ServerKeyExchangeParams { |
2114 | | #[cfg(feature = "tls12")] |
2115 | | pub(crate) fn new(kx: &dyn ActiveKeyExchange) -> Self { |
2116 | | match kx.group().key_exchange_algorithm() { |
2117 | | KeyExchangeAlgorithm::DHE => Self::Dh(ServerDhParams::new(kx)), |
2118 | | KeyExchangeAlgorithm::ECDHE => Self::Ecdh(ServerEcdhParams::new(kx)), |
2119 | | } |
2120 | | } |
2121 | | |
2122 | | #[cfg(feature = "tls12")] |
2123 | | pub(crate) fn pub_key(&self) -> &[u8] { |
2124 | | match self { |
2125 | | Self::Ecdh(ecdh) => &ecdh.public.0, |
2126 | | Self::Dh(dh) => &dh.dh_Ys.0, |
2127 | | } |
2128 | | } |
2129 | | |
2130 | 0 | pub(crate) fn encode(&self, buf: &mut Vec<u8>) { |
2131 | 0 | match self { |
2132 | 0 | Self::Ecdh(ecdh) => ecdh.encode(buf), |
2133 | 0 | Self::Dh(dh) => dh.encode(buf), |
2134 | | } |
2135 | 0 | } Unexecuted instantiation: <rustls::msgs::handshake::ServerKeyExchangeParams>::encode Unexecuted instantiation: <rustls::msgs::handshake::ServerKeyExchangeParams>::encode Unexecuted instantiation: <rustls::msgs::handshake::ServerKeyExchangeParams>::encode |
2136 | | } |
2137 | | |
2138 | | #[cfg(feature = "tls12")] |
2139 | | impl KxDecode<'_> for ServerKeyExchangeParams { |
2140 | | fn decode(r: &mut Reader<'_>, algo: KeyExchangeAlgorithm) -> Result<Self, InvalidMessage> { |
2141 | | use KeyExchangeAlgorithm::*; |
2142 | | Ok(match algo { |
2143 | | ECDHE => Self::Ecdh(ServerEcdhParams::read(r)?), |
2144 | | DHE => Self::Dh(ServerDhParams::read(r)?), |
2145 | | }) |
2146 | | } |
2147 | | } |
2148 | | |
2149 | | #[derive(Debug)] |
2150 | | pub(crate) struct ServerKeyExchange { |
2151 | | pub(crate) params: ServerKeyExchangeParams, |
2152 | | pub(crate) dss: DigitallySignedStruct, |
2153 | | } |
2154 | | |
2155 | | impl ServerKeyExchange { |
2156 | 0 | pub(crate) fn encode(&self, buf: &mut Vec<u8>) { |
2157 | 0 | self.params.encode(buf); |
2158 | 0 | self.dss.encode(buf); |
2159 | 0 | } Unexecuted instantiation: <rustls::msgs::handshake::ServerKeyExchange>::encode Unexecuted instantiation: <rustls::msgs::handshake::ServerKeyExchange>::encode Unexecuted instantiation: <rustls::msgs::handshake::ServerKeyExchange>::encode |
2160 | | } |
2161 | | |
2162 | | #[derive(Debug)] |
2163 | | pub(crate) enum ServerKeyExchangePayload { |
2164 | | Known(ServerKeyExchange), |
2165 | | Unknown(Payload<'static>), |
2166 | | } |
2167 | | |
2168 | | impl From<ServerKeyExchange> for ServerKeyExchangePayload { |
2169 | 0 | fn from(value: ServerKeyExchange) -> Self { |
2170 | 0 | Self::Known(value) |
2171 | 0 | } Unexecuted instantiation: <rustls::msgs::handshake::ServerKeyExchangePayload as core::convert::From<rustls::msgs::handshake::ServerKeyExchange>>::from Unexecuted instantiation: <rustls::msgs::handshake::ServerKeyExchangePayload as core::convert::From<rustls::msgs::handshake::ServerKeyExchange>>::from Unexecuted instantiation: <rustls::msgs::handshake::ServerKeyExchangePayload as core::convert::From<rustls::msgs::handshake::ServerKeyExchange>>::from |
2172 | | } |
2173 | | |
2174 | | impl Codec<'_> for ServerKeyExchangePayload { |
2175 | 0 | fn encode(&self, bytes: &mut Vec<u8>) { |
2176 | 0 | match self { |
2177 | 0 | Self::Known(x) => x.encode(bytes), |
2178 | 0 | Self::Unknown(x) => x.encode(bytes), |
2179 | | } |
2180 | 0 | } Unexecuted instantiation: <rustls::msgs::handshake::ServerKeyExchangePayload as rustls::msgs::codec::Codec>::encode Unexecuted instantiation: <rustls::msgs::handshake::ServerKeyExchangePayload as rustls::msgs::codec::Codec>::encode Unexecuted instantiation: <rustls::msgs::handshake::ServerKeyExchangePayload as rustls::msgs::codec::Codec>::encode |
2181 | | |
2182 | 0 | fn read(r: &mut Reader<'_>) -> Result<Self, InvalidMessage> { |
2183 | | // read as Unknown, fully parse when we know the |
2184 | | // KeyExchangeAlgorithm |
2185 | 0 | Ok(Self::Unknown(Payload::read(r).into_owned())) |
2186 | 0 | } Unexecuted instantiation: <rustls::msgs::handshake::ServerKeyExchangePayload as rustls::msgs::codec::Codec>::read Unexecuted instantiation: <rustls::msgs::handshake::ServerKeyExchangePayload as rustls::msgs::codec::Codec>::read Unexecuted instantiation: <rustls::msgs::handshake::ServerKeyExchangePayload as rustls::msgs::codec::Codec>::read |
2187 | | } |
2188 | | |
2189 | | impl ServerKeyExchangePayload { |
2190 | | #[cfg(feature = "tls12")] |
2191 | | pub(crate) fn unwrap_given_kxa(&self, kxa: KeyExchangeAlgorithm) -> Option<ServerKeyExchange> { |
2192 | | if let Self::Unknown(unk) = self { |
2193 | | let mut rd = Reader::init(unk.bytes()); |
2194 | | |
2195 | | let result = ServerKeyExchange { |
2196 | | params: ServerKeyExchangeParams::decode(&mut rd, kxa).ok()?, |
2197 | | dss: DigitallySignedStruct::read(&mut rd).ok()?, |
2198 | | }; |
2199 | | |
2200 | | if !rd.any_left() { |
2201 | | return Some(result); |
2202 | | }; |
2203 | | } |
2204 | | |
2205 | | None |
2206 | | } |
2207 | | } |
2208 | | |
2209 | | /// RFC5246: `ClientCertificateType certificate_types<1..2^8-1>;` |
2210 | | impl TlsListElement for ClientCertificateType { |
2211 | | const SIZE_LEN: ListLength = ListLength::NonZeroU8 { |
2212 | | empty_error: InvalidMessage::IllegalEmptyList("ClientCertificateTypes"), |
2213 | | }; |
2214 | | } |
2215 | | |
2216 | | wrapped_payload!( |
2217 | | /// A `DistinguishedName` is a `Vec<u8>` wrapped in internal types. |
2218 | | /// |
2219 | | /// It contains the DER or BER encoded [`Subject` field from RFC 5280](https://datatracker.ietf.org/doc/html/rfc5280#section-4.1.2.6) |
2220 | | /// for a single certificate. The Subject field is [encoded as an RFC 5280 `Name`](https://datatracker.ietf.org/doc/html/rfc5280#page-116). |
2221 | | /// It can be decoded using [x509-parser's FromDer trait](https://docs.rs/x509-parser/latest/x509_parser/prelude/trait.FromDer.html). |
2222 | | /// |
2223 | | /// ```ignore |
2224 | | /// for name in distinguished_names { |
2225 | | /// use x509_parser::prelude::FromDer; |
2226 | | /// println!("{}", x509_parser::x509::X509Name::from_der(&name.0)?.1); |
2227 | | /// } |
2228 | | /// ``` |
2229 | | /// |
2230 | | /// The TLS encoding is defined in RFC5246: `opaque DistinguishedName<1..2^16-1>;` |
2231 | | pub struct DistinguishedName, |
2232 | | PayloadU16<NonEmpty>, |
2233 | | ); |
2234 | | |
2235 | | impl DistinguishedName { |
2236 | | /// Create a [`DistinguishedName`] after prepending its outer SEQUENCE encoding. |
2237 | | /// |
2238 | | /// This can be decoded using [x509-parser's FromDer trait](https://docs.rs/x509-parser/latest/x509_parser/prelude/trait.FromDer.html). |
2239 | | /// |
2240 | | /// ```ignore |
2241 | | /// use x509_parser::prelude::FromDer; |
2242 | | /// println!("{}", x509_parser::x509::X509Name::from_der(dn.as_ref())?.1); |
2243 | | /// ``` |
2244 | 0 | pub fn in_sequence(bytes: &[u8]) -> Self { |
2245 | 0 | Self(PayloadU16::new(wrap_in_sequence(bytes))) |
2246 | 0 | } Unexecuted instantiation: <rustls::msgs::handshake::DistinguishedName>::in_sequence Unexecuted instantiation: <rustls::msgs::handshake::DistinguishedName>::in_sequence Unexecuted instantiation: <rustls::msgs::handshake::DistinguishedName>::in_sequence |
2247 | | } |
2248 | | |
2249 | | /// RFC8446: `DistinguishedName authorities<3..2^16-1>;` however, |
2250 | | /// RFC5246: `DistinguishedName certificate_authorities<0..2^16-1>;` |
2251 | | impl TlsListElement for DistinguishedName { |
2252 | | const SIZE_LEN: ListLength = ListLength::U16; |
2253 | | } |
2254 | | |
2255 | | #[derive(Debug)] |
2256 | | pub(crate) struct CertificateRequestPayload { |
2257 | | pub(crate) certtypes: Vec<ClientCertificateType>, |
2258 | | pub(crate) sigschemes: Vec<SignatureScheme>, |
2259 | | pub(crate) canames: Vec<DistinguishedName>, |
2260 | | } |
2261 | | |
2262 | | impl Codec<'_> for CertificateRequestPayload { |
2263 | 0 | fn encode(&self, bytes: &mut Vec<u8>) { |
2264 | 0 | self.certtypes.encode(bytes); |
2265 | 0 | self.sigschemes.encode(bytes); |
2266 | 0 | self.canames.encode(bytes); |
2267 | 0 | } Unexecuted instantiation: <rustls::msgs::handshake::CertificateRequestPayload as rustls::msgs::codec::Codec>::encode Unexecuted instantiation: <rustls::msgs::handshake::CertificateRequestPayload as rustls::msgs::codec::Codec>::encode Unexecuted instantiation: <rustls::msgs::handshake::CertificateRequestPayload as rustls::msgs::codec::Codec>::encode |
2268 | | |
2269 | 0 | fn read(r: &mut Reader<'_>) -> Result<Self, InvalidMessage> { |
2270 | 0 | let certtypes = Vec::read(r)?; |
2271 | 0 | let sigschemes = Vec::read(r)?; |
2272 | 0 | let canames = Vec::read(r)?; |
2273 | | |
2274 | 0 | if sigschemes.is_empty() { |
2275 | 0 | warn!("meaningless CertificateRequest message"); |
2276 | 0 | Err(InvalidMessage::NoSignatureSchemes) |
2277 | | } else { |
2278 | 0 | Ok(Self { |
2279 | 0 | certtypes, |
2280 | 0 | sigschemes, |
2281 | 0 | canames, |
2282 | 0 | }) |
2283 | | } |
2284 | 0 | } Unexecuted instantiation: <rustls::msgs::handshake::CertificateRequestPayload as rustls::msgs::codec::Codec>::read Unexecuted instantiation: <rustls::msgs::handshake::CertificateRequestPayload as rustls::msgs::codec::Codec>::read Unexecuted instantiation: <rustls::msgs::handshake::CertificateRequestPayload as rustls::msgs::codec::Codec>::read |
2285 | | } |
2286 | | |
2287 | | extension_struct! { |
2288 | | pub(crate) struct CertificateRequestExtensions { |
2289 | | ExtensionType::SignatureAlgorithms => |
2290 | | pub(crate) signature_algorithms: Option<Vec<SignatureScheme>>, |
2291 | | |
2292 | | ExtensionType::CertificateAuthorities => |
2293 | | pub(crate) authority_names: Option<Vec<DistinguishedName>>, |
2294 | | |
2295 | | ExtensionType::CompressCertificate => |
2296 | | pub(crate) certificate_compression_algorithms: Option<Vec<CertificateCompressionAlgorithm>>, |
2297 | | } |
2298 | | } |
2299 | | |
2300 | | impl Codec<'_> for CertificateRequestExtensions { |
2301 | 0 | fn encode(&self, bytes: &mut Vec<u8>) { |
2302 | 0 | let extensions = LengthPrefixedBuffer::new(ListLength::U16, bytes); |
2303 | | |
2304 | 0 | for ext in Self::ALL_EXTENSIONS { |
2305 | 0 | self.encode_one(*ext, extensions.buf); |
2306 | 0 | } |
2307 | 0 | } Unexecuted instantiation: <rustls::msgs::handshake::CertificateRequestExtensions as rustls::msgs::codec::Codec>::encode Unexecuted instantiation: <rustls::msgs::handshake::CertificateRequestExtensions as rustls::msgs::codec::Codec>::encode Unexecuted instantiation: <rustls::msgs::handshake::CertificateRequestExtensions as rustls::msgs::codec::Codec>::encode |
2308 | | |
2309 | 0 | fn read(r: &mut Reader<'_>) -> Result<Self, InvalidMessage> { |
2310 | 0 | let mut out = Self::default(); |
2311 | | |
2312 | 0 | let mut checker = DuplicateExtensionChecker::new(); |
2313 | | |
2314 | 0 | let len = usize::from(u16::read(r)?); |
2315 | 0 | let mut sub = r.sub(len)?; |
2316 | | |
2317 | 0 | while sub.any_left() { |
2318 | 0 | out.read_one(&mut sub, |unknown| checker.check(unknown))?; Unexecuted instantiation: <rustls::msgs::handshake::CertificateRequestExtensions as rustls::msgs::codec::Codec>::read::{closure#0}Unexecuted instantiation: <rustls::msgs::handshake::CertificateRequestExtensions as rustls::msgs::codec::Codec>::read::{closure#0}Unexecuted instantiation: <rustls::msgs::handshake::CertificateRequestExtensions as rustls::msgs::codec::Codec>::read::{closure#0} |
2319 | | } |
2320 | | |
2321 | 0 | if out |
2322 | 0 | .signature_algorithms |
2323 | 0 | .as_ref() |
2324 | 0 | .map(|algs| algs.is_empty()) Unexecuted instantiation: <rustls::msgs::handshake::CertificateRequestExtensions as rustls::msgs::codec::Codec>::read::{closure#1}Unexecuted instantiation: <rustls::msgs::handshake::CertificateRequestExtensions as rustls::msgs::codec::Codec>::read::{closure#1}Unexecuted instantiation: <rustls::msgs::handshake::CertificateRequestExtensions as rustls::msgs::codec::Codec>::read::{closure#1} |
2325 | 0 | .unwrap_or_default() |
2326 | | { |
2327 | 0 | return Err(InvalidMessage::NoSignatureSchemes); |
2328 | 0 | } |
2329 | | |
2330 | 0 | Ok(out) |
2331 | 0 | } Unexecuted instantiation: <rustls::msgs::handshake::CertificateRequestExtensions as rustls::msgs::codec::Codec>::read Unexecuted instantiation: <rustls::msgs::handshake::CertificateRequestExtensions as rustls::msgs::codec::Codec>::read Unexecuted instantiation: <rustls::msgs::handshake::CertificateRequestExtensions as rustls::msgs::codec::Codec>::read |
2332 | | } |
2333 | | |
2334 | | #[derive(Debug)] |
2335 | | pub(crate) struct CertificateRequestPayloadTls13 { |
2336 | | pub(crate) context: PayloadU8, |
2337 | | pub(crate) extensions: CertificateRequestExtensions, |
2338 | | } |
2339 | | |
2340 | | impl Codec<'_> for CertificateRequestPayloadTls13 { |
2341 | 0 | fn encode(&self, bytes: &mut Vec<u8>) { |
2342 | 0 | self.context.encode(bytes); |
2343 | 0 | self.extensions.encode(bytes); |
2344 | 0 | } Unexecuted instantiation: <rustls::msgs::handshake::CertificateRequestPayloadTls13 as rustls::msgs::codec::Codec>::encode Unexecuted instantiation: <rustls::msgs::handshake::CertificateRequestPayloadTls13 as rustls::msgs::codec::Codec>::encode Unexecuted instantiation: <rustls::msgs::handshake::CertificateRequestPayloadTls13 as rustls::msgs::codec::Codec>::encode |
2345 | | |
2346 | 0 | fn read(r: &mut Reader<'_>) -> Result<Self, InvalidMessage> { |
2347 | 0 | let context = PayloadU8::read(r)?; |
2348 | 0 | let extensions = CertificateRequestExtensions::read(r)?; |
2349 | | |
2350 | 0 | Ok(Self { |
2351 | 0 | context, |
2352 | 0 | extensions, |
2353 | 0 | }) |
2354 | 0 | } Unexecuted instantiation: <rustls::msgs::handshake::CertificateRequestPayloadTls13 as rustls::msgs::codec::Codec>::read Unexecuted instantiation: <rustls::msgs::handshake::CertificateRequestPayloadTls13 as rustls::msgs::codec::Codec>::read Unexecuted instantiation: <rustls::msgs::handshake::CertificateRequestPayloadTls13 as rustls::msgs::codec::Codec>::read |
2355 | | } |
2356 | | |
2357 | | // -- NewSessionTicket -- |
2358 | | #[derive(Debug)] |
2359 | | pub(crate) struct NewSessionTicketPayload { |
2360 | | pub(crate) lifetime_hint: u32, |
2361 | | // Tickets can be large (KB), so we deserialise this straight |
2362 | | // into an Arc, so it can be passed directly into the client's |
2363 | | // session object without copying. |
2364 | | pub(crate) ticket: Arc<PayloadU16>, |
2365 | | } |
2366 | | |
2367 | | impl NewSessionTicketPayload { |
2368 | | #[cfg(feature = "tls12")] |
2369 | | pub(crate) fn new(lifetime_hint: u32, ticket: Vec<u8>) -> Self { |
2370 | | Self { |
2371 | | lifetime_hint, |
2372 | | ticket: Arc::new(PayloadU16::new(ticket)), |
2373 | | } |
2374 | | } |
2375 | | } |
2376 | | |
2377 | | impl Codec<'_> for NewSessionTicketPayload { |
2378 | 0 | fn encode(&self, bytes: &mut Vec<u8>) { |
2379 | 0 | self.lifetime_hint.encode(bytes); |
2380 | 0 | self.ticket.encode(bytes); |
2381 | 0 | } Unexecuted instantiation: <rustls::msgs::handshake::NewSessionTicketPayload as rustls::msgs::codec::Codec>::encode Unexecuted instantiation: <rustls::msgs::handshake::NewSessionTicketPayload as rustls::msgs::codec::Codec>::encode Unexecuted instantiation: <rustls::msgs::handshake::NewSessionTicketPayload as rustls::msgs::codec::Codec>::encode |
2382 | | |
2383 | 0 | fn read(r: &mut Reader<'_>) -> Result<Self, InvalidMessage> { |
2384 | 0 | let lifetime = u32::read(r)?; |
2385 | 0 | let ticket = Arc::new(PayloadU16::read(r)?); |
2386 | | |
2387 | 0 | Ok(Self { |
2388 | 0 | lifetime_hint: lifetime, |
2389 | 0 | ticket, |
2390 | 0 | }) |
2391 | 0 | } Unexecuted instantiation: <rustls::msgs::handshake::NewSessionTicketPayload as rustls::msgs::codec::Codec>::read Unexecuted instantiation: <rustls::msgs::handshake::NewSessionTicketPayload as rustls::msgs::codec::Codec>::read Unexecuted instantiation: <rustls::msgs::handshake::NewSessionTicketPayload as rustls::msgs::codec::Codec>::read |
2392 | | } |
2393 | | |
2394 | | // -- NewSessionTicket electric boogaloo -- |
2395 | | extension_struct! { |
2396 | | pub(crate) struct NewSessionTicketExtensions { |
2397 | | ExtensionType::EarlyData => |
2398 | | pub(crate) max_early_data_size: Option<u32>, |
2399 | | } |
2400 | | } |
2401 | | |
2402 | | impl Codec<'_> for NewSessionTicketExtensions { |
2403 | 0 | fn encode(&self, bytes: &mut Vec<u8>) { |
2404 | 0 | let extensions = LengthPrefixedBuffer::new(ListLength::U16, bytes); |
2405 | | |
2406 | 0 | for ext in Self::ALL_EXTENSIONS { |
2407 | 0 | self.encode_one(*ext, extensions.buf); |
2408 | 0 | } |
2409 | 0 | } Unexecuted instantiation: <rustls::msgs::handshake::NewSessionTicketExtensions as rustls::msgs::codec::Codec>::encode Unexecuted instantiation: <rustls::msgs::handshake::NewSessionTicketExtensions as rustls::msgs::codec::Codec>::encode Unexecuted instantiation: <rustls::msgs::handshake::NewSessionTicketExtensions as rustls::msgs::codec::Codec>::encode |
2410 | | |
2411 | 0 | fn read(r: &mut Reader<'_>) -> Result<Self, InvalidMessage> { |
2412 | 0 | let mut out = Self::default(); |
2413 | | |
2414 | 0 | let mut checker = DuplicateExtensionChecker::new(); |
2415 | | |
2416 | 0 | let len = usize::from(u16::read(r)?); |
2417 | 0 | let mut sub = r.sub(len)?; |
2418 | | |
2419 | 0 | while sub.any_left() { |
2420 | 0 | out.read_one(&mut sub, |unknown| checker.check(unknown))?; Unexecuted instantiation: <rustls::msgs::handshake::NewSessionTicketExtensions as rustls::msgs::codec::Codec>::read::{closure#0}Unexecuted instantiation: <rustls::msgs::handshake::NewSessionTicketExtensions as rustls::msgs::codec::Codec>::read::{closure#0}Unexecuted instantiation: <rustls::msgs::handshake::NewSessionTicketExtensions as rustls::msgs::codec::Codec>::read::{closure#0} |
2421 | | } |
2422 | | |
2423 | 0 | Ok(out) |
2424 | 0 | } Unexecuted instantiation: <rustls::msgs::handshake::NewSessionTicketExtensions as rustls::msgs::codec::Codec>::read Unexecuted instantiation: <rustls::msgs::handshake::NewSessionTicketExtensions as rustls::msgs::codec::Codec>::read Unexecuted instantiation: <rustls::msgs::handshake::NewSessionTicketExtensions as rustls::msgs::codec::Codec>::read |
2425 | | } |
2426 | | |
2427 | | #[derive(Debug)] |
2428 | | pub(crate) struct NewSessionTicketPayloadTls13 { |
2429 | | pub(crate) lifetime: u32, |
2430 | | pub(crate) age_add: u32, |
2431 | | pub(crate) nonce: PayloadU8, |
2432 | | pub(crate) ticket: Arc<PayloadU16>, |
2433 | | pub(crate) extensions: NewSessionTicketExtensions, |
2434 | | } |
2435 | | |
2436 | | impl NewSessionTicketPayloadTls13 { |
2437 | 0 | pub(crate) fn new(lifetime: u32, age_add: u32, nonce: Vec<u8>, ticket: Vec<u8>) -> Self { |
2438 | 0 | Self { |
2439 | 0 | lifetime, |
2440 | 0 | age_add, |
2441 | 0 | nonce: PayloadU8::new(nonce), |
2442 | 0 | ticket: Arc::new(PayloadU16::new(ticket)), |
2443 | 0 | extensions: NewSessionTicketExtensions::default(), |
2444 | 0 | } |
2445 | 0 | } Unexecuted instantiation: <rustls::msgs::handshake::NewSessionTicketPayloadTls13>::new Unexecuted instantiation: <rustls::msgs::handshake::NewSessionTicketPayloadTls13>::new Unexecuted instantiation: <rustls::msgs::handshake::NewSessionTicketPayloadTls13>::new |
2446 | | } |
2447 | | |
2448 | | impl Codec<'_> for NewSessionTicketPayloadTls13 { |
2449 | 0 | fn encode(&self, bytes: &mut Vec<u8>) { |
2450 | 0 | self.lifetime.encode(bytes); |
2451 | 0 | self.age_add.encode(bytes); |
2452 | 0 | self.nonce.encode(bytes); |
2453 | 0 | self.ticket.encode(bytes); |
2454 | 0 | self.extensions.encode(bytes); |
2455 | 0 | } Unexecuted instantiation: <rustls::msgs::handshake::NewSessionTicketPayloadTls13 as rustls::msgs::codec::Codec>::encode Unexecuted instantiation: <rustls::msgs::handshake::NewSessionTicketPayloadTls13 as rustls::msgs::codec::Codec>::encode Unexecuted instantiation: <rustls::msgs::handshake::NewSessionTicketPayloadTls13 as rustls::msgs::codec::Codec>::encode |
2456 | | |
2457 | 0 | fn read(r: &mut Reader<'_>) -> Result<Self, InvalidMessage> { |
2458 | 0 | let lifetime = u32::read(r)?; |
2459 | 0 | let age_add = u32::read(r)?; |
2460 | 0 | let nonce = PayloadU8::read(r)?; |
2461 | | // nb. RFC8446: `opaque ticket<1..2^16-1>;` |
2462 | 0 | let ticket = Arc::new(match PayloadU16::<NonEmpty>::read(r) { |
2463 | 0 | Err(InvalidMessage::IllegalEmptyValue) => Err(InvalidMessage::EmptyTicketValue), |
2464 | 0 | Err(err) => Err(err), |
2465 | 0 | Ok(pl) => Ok(PayloadU16::new(pl.0)), |
2466 | 0 | }?); |
2467 | 0 | let extensions = NewSessionTicketExtensions::read(r)?; |
2468 | | |
2469 | 0 | Ok(Self { |
2470 | 0 | lifetime, |
2471 | 0 | age_add, |
2472 | 0 | nonce, |
2473 | 0 | ticket, |
2474 | 0 | extensions, |
2475 | 0 | }) |
2476 | 0 | } Unexecuted instantiation: <rustls::msgs::handshake::NewSessionTicketPayloadTls13 as rustls::msgs::codec::Codec>::read Unexecuted instantiation: <rustls::msgs::handshake::NewSessionTicketPayloadTls13 as rustls::msgs::codec::Codec>::read Unexecuted instantiation: <rustls::msgs::handshake::NewSessionTicketPayloadTls13 as rustls::msgs::codec::Codec>::read |
2477 | | } |
2478 | | |
2479 | | // -- RFC6066 certificate status types |
2480 | | |
2481 | | /// Only supports OCSP |
2482 | | #[derive(Clone, Debug)] |
2483 | | pub(crate) struct CertificateStatus<'a> { |
2484 | | /// `opaque OCSPResponse<1..2^24-1>;` |
2485 | | pub(crate) ocsp_response: PayloadU24<'a, NonEmpty>, |
2486 | | } |
2487 | | |
2488 | | impl<'a> Codec<'a> for CertificateStatus<'a> { |
2489 | 0 | fn encode(&self, bytes: &mut Vec<u8>) { |
2490 | 0 | CertificateStatusType::OCSP.encode(bytes); |
2491 | 0 | self.ocsp_response.encode(bytes); |
2492 | 0 | } Unexecuted instantiation: <rustls::msgs::handshake::CertificateStatus as rustls::msgs::codec::Codec>::encode Unexecuted instantiation: <rustls::msgs::handshake::CertificateStatus as rustls::msgs::codec::Codec>::encode Unexecuted instantiation: <rustls::msgs::handshake::CertificateStatus as rustls::msgs::codec::Codec>::encode |
2493 | | |
2494 | 0 | fn read(r: &mut Reader<'a>) -> Result<Self, InvalidMessage> { |
2495 | 0 | let typ = CertificateStatusType::read(r)?; |
2496 | | |
2497 | 0 | match typ { |
2498 | | CertificateStatusType::OCSP => Ok(Self { |
2499 | 0 | ocsp_response: PayloadU24::read(r)?, |
2500 | | }), |
2501 | 0 | _ => Err(InvalidMessage::InvalidCertificateStatusType), |
2502 | | } |
2503 | 0 | } Unexecuted instantiation: <rustls::msgs::handshake::CertificateStatus as rustls::msgs::codec::Codec>::read Unexecuted instantiation: <rustls::msgs::handshake::CertificateStatus as rustls::msgs::codec::Codec>::read Unexecuted instantiation: <rustls::msgs::handshake::CertificateStatus as rustls::msgs::codec::Codec>::read |
2504 | | } |
2505 | | |
2506 | | impl<'a> CertificateStatus<'a> { |
2507 | 0 | pub(crate) fn new(ocsp: &'a [u8]) -> Self { |
2508 | 0 | CertificateStatus { |
2509 | 0 | ocsp_response: PayloadU24::from(Payload::Borrowed(ocsp)), |
2510 | 0 | } |
2511 | 0 | } Unexecuted instantiation: <rustls::msgs::handshake::CertificateStatus>::new Unexecuted instantiation: <rustls::msgs::handshake::CertificateStatus>::new Unexecuted instantiation: <rustls::msgs::handshake::CertificateStatus>::new |
2512 | | |
2513 | | #[cfg(feature = "tls12")] |
2514 | | pub(crate) fn into_inner(self) -> Vec<u8> { |
2515 | | self.ocsp_response.0.into_vec() |
2516 | | } |
2517 | | |
2518 | 0 | pub(crate) fn into_owned(self) -> CertificateStatus<'static> { |
2519 | 0 | CertificateStatus { |
2520 | 0 | ocsp_response: self.ocsp_response.into_owned(), |
2521 | 0 | } |
2522 | 0 | } Unexecuted instantiation: <rustls::msgs::handshake::CertificateStatus>::into_owned Unexecuted instantiation: <rustls::msgs::handshake::CertificateStatus>::into_owned Unexecuted instantiation: <rustls::msgs::handshake::CertificateStatus>::into_owned |
2523 | | } |
2524 | | |
2525 | | // -- RFC8879 compressed certificates |
2526 | | |
2527 | | #[derive(Debug)] |
2528 | | pub(crate) struct CompressedCertificatePayload<'a> { |
2529 | | pub(crate) alg: CertificateCompressionAlgorithm, |
2530 | | pub(crate) uncompressed_len: u32, |
2531 | | pub(crate) compressed: PayloadU24<'a>, |
2532 | | } |
2533 | | |
2534 | | impl<'a> Codec<'a> for CompressedCertificatePayload<'a> { |
2535 | 0 | fn encode(&self, bytes: &mut Vec<u8>) { |
2536 | 0 | self.alg.encode(bytes); |
2537 | 0 | codec::u24(self.uncompressed_len).encode(bytes); |
2538 | 0 | self.compressed.encode(bytes); |
2539 | 0 | } Unexecuted instantiation: <rustls::msgs::handshake::CompressedCertificatePayload as rustls::msgs::codec::Codec>::encode Unexecuted instantiation: <rustls::msgs::handshake::CompressedCertificatePayload as rustls::msgs::codec::Codec>::encode Unexecuted instantiation: <rustls::msgs::handshake::CompressedCertificatePayload as rustls::msgs::codec::Codec>::encode |
2540 | | |
2541 | 0 | fn read(r: &mut Reader<'a>) -> Result<Self, InvalidMessage> { |
2542 | | Ok(Self { |
2543 | 0 | alg: CertificateCompressionAlgorithm::read(r)?, |
2544 | 0 | uncompressed_len: codec::u24::read(r)?.0, |
2545 | 0 | compressed: PayloadU24::read(r)?, |
2546 | | }) |
2547 | 0 | } Unexecuted instantiation: <rustls::msgs::handshake::CompressedCertificatePayload as rustls::msgs::codec::Codec>::read Unexecuted instantiation: <rustls::msgs::handshake::CompressedCertificatePayload as rustls::msgs::codec::Codec>::read Unexecuted instantiation: <rustls::msgs::handshake::CompressedCertificatePayload as rustls::msgs::codec::Codec>::read |
2548 | | } |
2549 | | |
2550 | | impl CompressedCertificatePayload<'_> { |
2551 | 0 | fn into_owned(self) -> CompressedCertificatePayload<'static> { |
2552 | 0 | CompressedCertificatePayload { |
2553 | 0 | compressed: self.compressed.into_owned(), |
2554 | 0 | ..self |
2555 | 0 | } |
2556 | 0 | } Unexecuted instantiation: <rustls::msgs::handshake::CompressedCertificatePayload>::into_owned Unexecuted instantiation: <rustls::msgs::handshake::CompressedCertificatePayload>::into_owned Unexecuted instantiation: <rustls::msgs::handshake::CompressedCertificatePayload>::into_owned |
2557 | | |
2558 | 0 | pub(crate) fn as_borrowed(&self) -> CompressedCertificatePayload<'_> { |
2559 | 0 | CompressedCertificatePayload { |
2560 | 0 | alg: self.alg, |
2561 | 0 | uncompressed_len: self.uncompressed_len, |
2562 | 0 | compressed: PayloadU24::from(Payload::Borrowed(self.compressed.0.bytes())), |
2563 | 0 | } |
2564 | 0 | } Unexecuted instantiation: <rustls::msgs::handshake::CompressedCertificatePayload>::as_borrowed Unexecuted instantiation: <rustls::msgs::handshake::CompressedCertificatePayload>::as_borrowed Unexecuted instantiation: <rustls::msgs::handshake::CompressedCertificatePayload>::as_borrowed |
2565 | | } |
2566 | | |
2567 | | #[derive(Debug)] |
2568 | | pub(crate) enum HandshakePayload<'a> { |
2569 | | HelloRequest, |
2570 | | ClientHello(ClientHelloPayload), |
2571 | | ServerHello(ServerHelloPayload), |
2572 | | HelloRetryRequest(HelloRetryRequest), |
2573 | | Certificate(CertificateChain<'a>), |
2574 | | CertificateTls13(CertificatePayloadTls13<'a>), |
2575 | | CompressedCertificate(CompressedCertificatePayload<'a>), |
2576 | | ServerKeyExchange(ServerKeyExchangePayload), |
2577 | | CertificateRequest(CertificateRequestPayload), |
2578 | | CertificateRequestTls13(CertificateRequestPayloadTls13), |
2579 | | CertificateVerify(DigitallySignedStruct), |
2580 | | ServerHelloDone, |
2581 | | EndOfEarlyData, |
2582 | | ClientKeyExchange(Payload<'a>), |
2583 | | NewSessionTicket(NewSessionTicketPayload), |
2584 | | NewSessionTicketTls13(NewSessionTicketPayloadTls13), |
2585 | | EncryptedExtensions(Box<ServerExtensions<'a>>), |
2586 | | KeyUpdate(KeyUpdateRequest), |
2587 | | Finished(Payload<'a>), |
2588 | | CertificateStatus(CertificateStatus<'a>), |
2589 | | MessageHash(Payload<'a>), |
2590 | | Unknown((HandshakeType, Payload<'a>)), |
2591 | | } |
2592 | | |
2593 | | impl HandshakePayload<'_> { |
2594 | 0 | fn encode(&self, bytes: &mut Vec<u8>) { |
2595 | | use self::HandshakePayload::*; |
2596 | 0 | match self { |
2597 | 0 | HelloRequest | ServerHelloDone | EndOfEarlyData => {} |
2598 | 0 | ClientHello(x) => x.encode(bytes), |
2599 | 0 | ServerHello(x) => x.encode(bytes), |
2600 | 0 | HelloRetryRequest(x) => x.encode(bytes), |
2601 | 0 | Certificate(x) => x.encode(bytes), |
2602 | 0 | CertificateTls13(x) => x.encode(bytes), |
2603 | 0 | CompressedCertificate(x) => x.encode(bytes), |
2604 | 0 | ServerKeyExchange(x) => x.encode(bytes), |
2605 | 0 | ClientKeyExchange(x) => x.encode(bytes), |
2606 | 0 | CertificateRequest(x) => x.encode(bytes), |
2607 | 0 | CertificateRequestTls13(x) => x.encode(bytes), |
2608 | 0 | CertificateVerify(x) => x.encode(bytes), |
2609 | 0 | NewSessionTicket(x) => x.encode(bytes), |
2610 | 0 | NewSessionTicketTls13(x) => x.encode(bytes), |
2611 | 0 | EncryptedExtensions(x) => x.encode(bytes), |
2612 | 0 | KeyUpdate(x) => x.encode(bytes), |
2613 | 0 | Finished(x) => x.encode(bytes), |
2614 | 0 | CertificateStatus(x) => x.encode(bytes), |
2615 | 0 | MessageHash(x) => x.encode(bytes), |
2616 | 0 | Unknown((_, x)) => x.encode(bytes), |
2617 | | } |
2618 | 0 | } Unexecuted instantiation: <rustls::msgs::handshake::HandshakePayload>::encode Unexecuted instantiation: <rustls::msgs::handshake::HandshakePayload>::encode Unexecuted instantiation: <rustls::msgs::handshake::HandshakePayload>::encode |
2619 | | |
2620 | 0 | pub(crate) fn handshake_type(&self) -> HandshakeType { |
2621 | | use self::HandshakePayload::*; |
2622 | 0 | match self { |
2623 | 0 | HelloRequest => HandshakeType::HelloRequest, |
2624 | 0 | ClientHello(_) => HandshakeType::ClientHello, |
2625 | 0 | ServerHello(_) => HandshakeType::ServerHello, |
2626 | 0 | HelloRetryRequest(_) => HandshakeType::HelloRetryRequest, |
2627 | 0 | Certificate(_) | CertificateTls13(_) => HandshakeType::Certificate, |
2628 | 0 | CompressedCertificate(_) => HandshakeType::CompressedCertificate, |
2629 | 0 | ServerKeyExchange(_) => HandshakeType::ServerKeyExchange, |
2630 | 0 | CertificateRequest(_) | CertificateRequestTls13(_) => HandshakeType::CertificateRequest, |
2631 | 0 | CertificateVerify(_) => HandshakeType::CertificateVerify, |
2632 | 0 | ServerHelloDone => HandshakeType::ServerHelloDone, |
2633 | 0 | EndOfEarlyData => HandshakeType::EndOfEarlyData, |
2634 | 0 | ClientKeyExchange(_) => HandshakeType::ClientKeyExchange, |
2635 | 0 | NewSessionTicket(_) | NewSessionTicketTls13(_) => HandshakeType::NewSessionTicket, |
2636 | 0 | EncryptedExtensions(_) => HandshakeType::EncryptedExtensions, |
2637 | 0 | KeyUpdate(_) => HandshakeType::KeyUpdate, |
2638 | 0 | Finished(_) => HandshakeType::Finished, |
2639 | 0 | CertificateStatus(_) => HandshakeType::CertificateStatus, |
2640 | 0 | MessageHash(_) => HandshakeType::MessageHash, |
2641 | 0 | Unknown((t, _)) => *t, |
2642 | | } |
2643 | 0 | } Unexecuted instantiation: <rustls::msgs::handshake::HandshakePayload>::handshake_type Unexecuted instantiation: <rustls::msgs::handshake::HandshakePayload>::handshake_type Unexecuted instantiation: <rustls::msgs::handshake::HandshakePayload>::handshake_type |
2644 | | |
2645 | 0 | fn wire_handshake_type(&self) -> HandshakeType { |
2646 | 0 | match self.handshake_type() { |
2647 | | // A `HelloRetryRequest` appears on the wire as a `ServerHello` with a magic `random` value. |
2648 | 0 | HandshakeType::HelloRetryRequest => HandshakeType::ServerHello, |
2649 | 0 | other => other, |
2650 | | } |
2651 | 0 | } Unexecuted instantiation: <rustls::msgs::handshake::HandshakePayload>::wire_handshake_type Unexecuted instantiation: <rustls::msgs::handshake::HandshakePayload>::wire_handshake_type Unexecuted instantiation: <rustls::msgs::handshake::HandshakePayload>::wire_handshake_type |
2652 | | |
2653 | 0 | fn into_owned(self) -> HandshakePayload<'static> { |
2654 | | use HandshakePayload::*; |
2655 | | |
2656 | 0 | match self { |
2657 | 0 | HelloRequest => HelloRequest, |
2658 | 0 | ClientHello(x) => ClientHello(x), |
2659 | 0 | ServerHello(x) => ServerHello(x), |
2660 | 0 | HelloRetryRequest(x) => HelloRetryRequest(x), |
2661 | 0 | Certificate(x) => Certificate(x.into_owned()), |
2662 | 0 | CertificateTls13(x) => CertificateTls13(x.into_owned()), |
2663 | 0 | CompressedCertificate(x) => CompressedCertificate(x.into_owned()), |
2664 | 0 | ServerKeyExchange(x) => ServerKeyExchange(x), |
2665 | 0 | CertificateRequest(x) => CertificateRequest(x), |
2666 | 0 | CertificateRequestTls13(x) => CertificateRequestTls13(x), |
2667 | 0 | CertificateVerify(x) => CertificateVerify(x), |
2668 | 0 | ServerHelloDone => ServerHelloDone, |
2669 | 0 | EndOfEarlyData => EndOfEarlyData, |
2670 | 0 | ClientKeyExchange(x) => ClientKeyExchange(x.into_owned()), |
2671 | 0 | NewSessionTicket(x) => NewSessionTicket(x), |
2672 | 0 | NewSessionTicketTls13(x) => NewSessionTicketTls13(x), |
2673 | 0 | EncryptedExtensions(x) => EncryptedExtensions(Box::new(x.into_owned())), |
2674 | 0 | KeyUpdate(x) => KeyUpdate(x), |
2675 | 0 | Finished(x) => Finished(x.into_owned()), |
2676 | 0 | CertificateStatus(x) => CertificateStatus(x.into_owned()), |
2677 | 0 | MessageHash(x) => MessageHash(x.into_owned()), |
2678 | 0 | Unknown((t, x)) => Unknown((t, x.into_owned())), |
2679 | | } |
2680 | 0 | } Unexecuted instantiation: <rustls::msgs::handshake::HandshakePayload>::into_owned Unexecuted instantiation: <rustls::msgs::handshake::HandshakePayload>::into_owned Unexecuted instantiation: <rustls::msgs::handshake::HandshakePayload>::into_owned |
2681 | | } |
2682 | | |
2683 | | #[derive(Debug)] |
2684 | | pub struct HandshakeMessagePayload<'a>(pub(crate) HandshakePayload<'a>); |
2685 | | |
2686 | | impl<'a> Codec<'a> for HandshakeMessagePayload<'a> { |
2687 | 0 | fn encode(&self, bytes: &mut Vec<u8>) { |
2688 | 0 | self.payload_encode(bytes, Encoding::Standard); |
2689 | 0 | } Unexecuted instantiation: <rustls::msgs::handshake::HandshakeMessagePayload as rustls::msgs::codec::Codec>::encode Unexecuted instantiation: <rustls::msgs::handshake::HandshakeMessagePayload as rustls::msgs::codec::Codec>::encode Unexecuted instantiation: <rustls::msgs::handshake::HandshakeMessagePayload as rustls::msgs::codec::Codec>::encode |
2690 | | |
2691 | 0 | fn read(r: &mut Reader<'a>) -> Result<Self, InvalidMessage> { |
2692 | 0 | Self::read_version(r, ProtocolVersion::TLSv1_2) |
2693 | 0 | } Unexecuted instantiation: <rustls::msgs::handshake::HandshakeMessagePayload as rustls::msgs::codec::Codec>::read Unexecuted instantiation: <rustls::msgs::handshake::HandshakeMessagePayload as rustls::msgs::codec::Codec>::read Unexecuted instantiation: <rustls::msgs::handshake::HandshakeMessagePayload as rustls::msgs::codec::Codec>::read |
2694 | | } |
2695 | | |
2696 | | impl<'a> HandshakeMessagePayload<'a> { |
2697 | 0 | pub(crate) fn read_version( |
2698 | 0 | r: &mut Reader<'a>, |
2699 | 0 | vers: ProtocolVersion, |
2700 | 0 | ) -> Result<Self, InvalidMessage> { |
2701 | 0 | let typ = HandshakeType::read(r)?; |
2702 | 0 | let len = codec::u24::read(r)?.0 as usize; |
2703 | 0 | let mut sub = r.sub(len)?; |
2704 | | |
2705 | 0 | let payload = match typ { |
2706 | 0 | HandshakeType::HelloRequest if sub.left() == 0 => HandshakePayload::HelloRequest, |
2707 | | HandshakeType::ClientHello => { |
2708 | 0 | HandshakePayload::ClientHello(ClientHelloPayload::read(&mut sub)?) |
2709 | | } |
2710 | | HandshakeType::ServerHello => { |
2711 | 0 | let version = ProtocolVersion::read(&mut sub)?; |
2712 | 0 | let random = Random::read(&mut sub)?; |
2713 | | |
2714 | 0 | if random == HELLO_RETRY_REQUEST_RANDOM { |
2715 | 0 | let mut hrr = HelloRetryRequest::read(&mut sub)?; |
2716 | 0 | hrr.legacy_version = version; |
2717 | 0 | HandshakePayload::HelloRetryRequest(hrr) |
2718 | | } else { |
2719 | 0 | let mut shp = ServerHelloPayload::read(&mut sub)?; |
2720 | 0 | shp.legacy_version = version; |
2721 | 0 | shp.random = random; |
2722 | 0 | HandshakePayload::ServerHello(shp) |
2723 | | } |
2724 | | } |
2725 | 0 | HandshakeType::Certificate if vers == ProtocolVersion::TLSv1_3 => { |
2726 | 0 | let p = CertificatePayloadTls13::read(&mut sub)?; |
2727 | 0 | HandshakePayload::CertificateTls13(p) |
2728 | | } |
2729 | | HandshakeType::Certificate => { |
2730 | 0 | HandshakePayload::Certificate(CertificateChain::read(&mut sub)?) |
2731 | | } |
2732 | | HandshakeType::ServerKeyExchange => { |
2733 | 0 | let p = ServerKeyExchangePayload::read(&mut sub)?; |
2734 | 0 | HandshakePayload::ServerKeyExchange(p) |
2735 | | } |
2736 | | HandshakeType::ServerHelloDone => { |
2737 | 0 | sub.expect_empty("ServerHelloDone")?; |
2738 | 0 | HandshakePayload::ServerHelloDone |
2739 | | } |
2740 | | HandshakeType::ClientKeyExchange => { |
2741 | 0 | HandshakePayload::ClientKeyExchange(Payload::read(&mut sub)) |
2742 | | } |
2743 | 0 | HandshakeType::CertificateRequest if vers == ProtocolVersion::TLSv1_3 => { |
2744 | 0 | let p = CertificateRequestPayloadTls13::read(&mut sub)?; |
2745 | 0 | HandshakePayload::CertificateRequestTls13(p) |
2746 | | } |
2747 | | HandshakeType::CertificateRequest => { |
2748 | 0 | let p = CertificateRequestPayload::read(&mut sub)?; |
2749 | 0 | HandshakePayload::CertificateRequest(p) |
2750 | | } |
2751 | | HandshakeType::CompressedCertificate => HandshakePayload::CompressedCertificate( |
2752 | 0 | CompressedCertificatePayload::read(&mut sub)?, |
2753 | | ), |
2754 | | HandshakeType::CertificateVerify => { |
2755 | 0 | HandshakePayload::CertificateVerify(DigitallySignedStruct::read(&mut sub)?) |
2756 | | } |
2757 | 0 | HandshakeType::NewSessionTicket if vers == ProtocolVersion::TLSv1_3 => { |
2758 | 0 | let p = NewSessionTicketPayloadTls13::read(&mut sub)?; |
2759 | 0 | HandshakePayload::NewSessionTicketTls13(p) |
2760 | | } |
2761 | | HandshakeType::NewSessionTicket => { |
2762 | 0 | let p = NewSessionTicketPayload::read(&mut sub)?; |
2763 | 0 | HandshakePayload::NewSessionTicket(p) |
2764 | | } |
2765 | | HandshakeType::EncryptedExtensions => { |
2766 | 0 | HandshakePayload::EncryptedExtensions(Box::new(ServerExtensions::read(&mut sub)?)) |
2767 | | } |
2768 | | HandshakeType::KeyUpdate => { |
2769 | 0 | HandshakePayload::KeyUpdate(KeyUpdateRequest::read(&mut sub)?) |
2770 | | } |
2771 | | HandshakeType::EndOfEarlyData => { |
2772 | 0 | sub.expect_empty("EndOfEarlyData")?; |
2773 | 0 | HandshakePayload::EndOfEarlyData |
2774 | | } |
2775 | 0 | HandshakeType::Finished => HandshakePayload::Finished(Payload::read(&mut sub)), |
2776 | | HandshakeType::CertificateStatus => { |
2777 | 0 | HandshakePayload::CertificateStatus(CertificateStatus::read(&mut sub)?) |
2778 | | } |
2779 | | HandshakeType::MessageHash => { |
2780 | | // does not appear on the wire |
2781 | 0 | return Err(InvalidMessage::UnexpectedMessage("MessageHash")); |
2782 | | } |
2783 | | HandshakeType::HelloRetryRequest => { |
2784 | | // not legal on wire |
2785 | 0 | return Err(InvalidMessage::UnexpectedMessage("HelloRetryRequest")); |
2786 | | } |
2787 | 0 | _ => HandshakePayload::Unknown((typ, Payload::read(&mut sub))), |
2788 | | }; |
2789 | | |
2790 | 0 | sub.expect_empty("HandshakeMessagePayload") |
2791 | 0 | .map(|_| Self(payload)) Unexecuted instantiation: <rustls::msgs::handshake::HandshakeMessagePayload>::read_version::{closure#0}Unexecuted instantiation: <rustls::msgs::handshake::HandshakeMessagePayload>::read_version::{closure#0}Unexecuted instantiation: <rustls::msgs::handshake::HandshakeMessagePayload>::read_version::{closure#0} |
2792 | 0 | } Unexecuted instantiation: <rustls::msgs::handshake::HandshakeMessagePayload>::read_version Unexecuted instantiation: <rustls::msgs::handshake::HandshakeMessagePayload>::read_version Unexecuted instantiation: <rustls::msgs::handshake::HandshakeMessagePayload>::read_version |
2793 | | |
2794 | 0 | pub(crate) fn encoding_for_binder_signing(&self) -> Vec<u8> { |
2795 | 0 | let mut ret = self.get_encoding(); |
2796 | 0 | let ret_len = ret |
2797 | 0 | .len() |
2798 | 0 | .saturating_sub(self.total_binder_length()); |
2799 | 0 | ret.truncate(ret_len); |
2800 | 0 | ret |
2801 | 0 | } Unexecuted instantiation: <rustls::msgs::handshake::HandshakeMessagePayload>::encoding_for_binder_signing Unexecuted instantiation: <rustls::msgs::handshake::HandshakeMessagePayload>::encoding_for_binder_signing Unexecuted instantiation: <rustls::msgs::handshake::HandshakeMessagePayload>::encoding_for_binder_signing |
2802 | | |
2803 | 0 | pub(crate) fn total_binder_length(&self) -> usize { |
2804 | 0 | match &self.0 { |
2805 | 0 | HandshakePayload::ClientHello(ch) => match &ch.preshared_key_offer { |
2806 | 0 | Some(offer) => { |
2807 | 0 | let mut binders_encoding = Vec::new(); |
2808 | 0 | offer |
2809 | 0 | .binders |
2810 | 0 | .encode(&mut binders_encoding); |
2811 | 0 | binders_encoding.len() |
2812 | | } |
2813 | 0 | _ => 0, |
2814 | | }, |
2815 | 0 | _ => 0, |
2816 | | } |
2817 | 0 | } Unexecuted instantiation: <rustls::msgs::handshake::HandshakeMessagePayload>::total_binder_length Unexecuted instantiation: <rustls::msgs::handshake::HandshakeMessagePayload>::total_binder_length Unexecuted instantiation: <rustls::msgs::handshake::HandshakeMessagePayload>::total_binder_length |
2818 | | |
2819 | 0 | pub(crate) fn payload_encode(&self, bytes: &mut Vec<u8>, encoding: Encoding) { |
2820 | | // output type, length, and encoded payload |
2821 | 0 | self.0 |
2822 | 0 | .wire_handshake_type() |
2823 | 0 | .encode(bytes); |
2824 | | |
2825 | 0 | let nested = LengthPrefixedBuffer::new( |
2826 | 0 | ListLength::U24 { |
2827 | 0 | max: usize::MAX, |
2828 | 0 | error: InvalidMessage::MessageTooLarge, |
2829 | 0 | }, |
2830 | 0 | bytes, |
2831 | | ); |
2832 | | |
2833 | 0 | match &self.0 { |
2834 | | // for Server Hello and HelloRetryRequest payloads we need to encode the payload |
2835 | | // differently based on the purpose of the encoding. |
2836 | 0 | HandshakePayload::ServerHello(payload) => payload.payload_encode(nested.buf, encoding), |
2837 | 0 | HandshakePayload::HelloRetryRequest(payload) => { |
2838 | 0 | payload.payload_encode(nested.buf, encoding) |
2839 | | } |
2840 | | |
2841 | | // All other payload types are encoded the same regardless of purpose. |
2842 | 0 | _ => self.0.encode(nested.buf), |
2843 | | } |
2844 | 0 | } Unexecuted instantiation: <rustls::msgs::handshake::HandshakeMessagePayload>::payload_encode Unexecuted instantiation: <rustls::msgs::handshake::HandshakeMessagePayload>::payload_encode Unexecuted instantiation: <rustls::msgs::handshake::HandshakeMessagePayload>::payload_encode |
2845 | | |
2846 | 0 | pub(crate) fn build_handshake_hash(hash: &[u8]) -> Self { |
2847 | 0 | Self(HandshakePayload::MessageHash(Payload::new(hash.to_vec()))) |
2848 | 0 | } Unexecuted instantiation: <rustls::msgs::handshake::HandshakeMessagePayload>::build_handshake_hash Unexecuted instantiation: <rustls::msgs::handshake::HandshakeMessagePayload>::build_handshake_hash Unexecuted instantiation: <rustls::msgs::handshake::HandshakeMessagePayload>::build_handshake_hash |
2849 | | |
2850 | 0 | pub(crate) fn into_owned(self) -> HandshakeMessagePayload<'static> { |
2851 | 0 | HandshakeMessagePayload(self.0.into_owned()) |
2852 | 0 | } Unexecuted instantiation: <rustls::msgs::handshake::HandshakeMessagePayload>::into_owned Unexecuted instantiation: <rustls::msgs::handshake::HandshakeMessagePayload>::into_owned Unexecuted instantiation: <rustls::msgs::handshake::HandshakeMessagePayload>::into_owned |
2853 | | } |
2854 | | |
2855 | | #[derive(Clone, Copy, Debug, Default, Eq, PartialEq)] |
2856 | | pub struct HpkeSymmetricCipherSuite { |
2857 | | pub kdf_id: HpkeKdf, |
2858 | | pub aead_id: HpkeAead, |
2859 | | } |
2860 | | |
2861 | | impl Codec<'_> for HpkeSymmetricCipherSuite { |
2862 | 0 | fn encode(&self, bytes: &mut Vec<u8>) { |
2863 | 0 | self.kdf_id.encode(bytes); |
2864 | 0 | self.aead_id.encode(bytes); |
2865 | 0 | } Unexecuted instantiation: <rustls::msgs::handshake::HpkeSymmetricCipherSuite as rustls::msgs::codec::Codec>::encode Unexecuted instantiation: <rustls::msgs::handshake::HpkeSymmetricCipherSuite as rustls::msgs::codec::Codec>::encode Unexecuted instantiation: <rustls::msgs::handshake::HpkeSymmetricCipherSuite as rustls::msgs::codec::Codec>::encode |
2866 | | |
2867 | 0 | fn read(r: &mut Reader<'_>) -> Result<Self, InvalidMessage> { |
2868 | | Ok(Self { |
2869 | 0 | kdf_id: HpkeKdf::read(r)?, |
2870 | 0 | aead_id: HpkeAead::read(r)?, |
2871 | | }) |
2872 | 0 | } Unexecuted instantiation: <rustls::msgs::handshake::HpkeSymmetricCipherSuite as rustls::msgs::codec::Codec>::read Unexecuted instantiation: <rustls::msgs::handshake::HpkeSymmetricCipherSuite as rustls::msgs::codec::Codec>::read Unexecuted instantiation: <rustls::msgs::handshake::HpkeSymmetricCipherSuite as rustls::msgs::codec::Codec>::read |
2873 | | } |
2874 | | |
2875 | | /// draft-ietf-tls-esni-24: `HpkeSymmetricCipherSuite cipher_suites<4..2^16-4>;` |
2876 | | impl TlsListElement for HpkeSymmetricCipherSuite { |
2877 | | const SIZE_LEN: ListLength = ListLength::NonZeroU16 { |
2878 | | empty_error: InvalidMessage::IllegalEmptyList("HpkeSymmetricCipherSuites"), |
2879 | | }; |
2880 | | } |
2881 | | |
2882 | | #[derive(Clone, Debug, PartialEq)] |
2883 | | pub struct HpkeKeyConfig { |
2884 | | pub config_id: u8, |
2885 | | pub kem_id: HpkeKem, |
2886 | | /// draft-ietf-tls-esni-24: `opaque HpkePublicKey<1..2^16-1>;` |
2887 | | pub public_key: PayloadU16<NonEmpty>, |
2888 | | pub symmetric_cipher_suites: Vec<HpkeSymmetricCipherSuite>, |
2889 | | } |
2890 | | |
2891 | | impl Codec<'_> for HpkeKeyConfig { |
2892 | 0 | fn encode(&self, bytes: &mut Vec<u8>) { |
2893 | 0 | self.config_id.encode(bytes); |
2894 | 0 | self.kem_id.encode(bytes); |
2895 | 0 | self.public_key.encode(bytes); |
2896 | 0 | self.symmetric_cipher_suites |
2897 | 0 | .encode(bytes); |
2898 | 0 | } Unexecuted instantiation: <rustls::msgs::handshake::HpkeKeyConfig as rustls::msgs::codec::Codec>::encode Unexecuted instantiation: <rustls::msgs::handshake::HpkeKeyConfig as rustls::msgs::codec::Codec>::encode Unexecuted instantiation: <rustls::msgs::handshake::HpkeKeyConfig as rustls::msgs::codec::Codec>::encode |
2899 | | |
2900 | 0 | fn read(r: &mut Reader<'_>) -> Result<Self, InvalidMessage> { |
2901 | | Ok(Self { |
2902 | 0 | config_id: u8::read(r)?, |
2903 | 0 | kem_id: HpkeKem::read(r)?, |
2904 | 0 | public_key: PayloadU16::read(r)?, |
2905 | 0 | symmetric_cipher_suites: Vec::<HpkeSymmetricCipherSuite>::read(r)?, |
2906 | | }) |
2907 | 0 | } Unexecuted instantiation: <rustls::msgs::handshake::HpkeKeyConfig as rustls::msgs::codec::Codec>::read Unexecuted instantiation: <rustls::msgs::handshake::HpkeKeyConfig as rustls::msgs::codec::Codec>::read Unexecuted instantiation: <rustls::msgs::handshake::HpkeKeyConfig as rustls::msgs::codec::Codec>::read |
2908 | | } |
2909 | | |
2910 | | #[derive(Clone, Debug, PartialEq)] |
2911 | | pub struct EchConfigContents { |
2912 | | pub key_config: HpkeKeyConfig, |
2913 | | pub maximum_name_length: u8, |
2914 | | pub public_name: DnsName<'static>, |
2915 | | pub extensions: Vec<EchConfigExtension>, |
2916 | | } |
2917 | | |
2918 | | impl EchConfigContents { |
2919 | | /// Returns true if there is more than one extension of a given |
2920 | | /// type. |
2921 | 0 | pub(crate) fn has_duplicate_extension(&self) -> bool { |
2922 | 0 | has_duplicates::<_, _, u16>( |
2923 | 0 | self.extensions |
2924 | 0 | .iter() |
2925 | 0 | .map(|ext| ext.ext_type()), Unexecuted instantiation: <rustls::msgs::handshake::EchConfigContents>::has_duplicate_extension::{closure#0}Unexecuted instantiation: <rustls::msgs::handshake::EchConfigContents>::has_duplicate_extension::{closure#0}Unexecuted instantiation: <rustls::msgs::handshake::EchConfigContents>::has_duplicate_extension::{closure#0} |
2926 | | ) |
2927 | 0 | } Unexecuted instantiation: <rustls::msgs::handshake::EchConfigContents>::has_duplicate_extension Unexecuted instantiation: <rustls::msgs::handshake::EchConfigContents>::has_duplicate_extension Unexecuted instantiation: <rustls::msgs::handshake::EchConfigContents>::has_duplicate_extension |
2928 | | |
2929 | | /// Returns true if there is at least one mandatory unsupported extension. |
2930 | 0 | pub(crate) fn has_unknown_mandatory_extension(&self) -> bool { |
2931 | 0 | self.extensions |
2932 | 0 | .iter() |
2933 | | // An extension is considered mandatory if the high bit of its type is set. |
2934 | 0 | .any(|ext| { |
2935 | 0 | matches!(ext.ext_type(), ExtensionType::Unknown(_)) |
2936 | 0 | && u16::from(ext.ext_type()) & 0x8000 != 0 |
2937 | 0 | }) Unexecuted instantiation: <rustls::msgs::handshake::EchConfigContents>::has_unknown_mandatory_extension::{closure#0}Unexecuted instantiation: <rustls::msgs::handshake::EchConfigContents>::has_unknown_mandatory_extension::{closure#0}Unexecuted instantiation: <rustls::msgs::handshake::EchConfigContents>::has_unknown_mandatory_extension::{closure#0} |
2938 | 0 | } Unexecuted instantiation: <rustls::msgs::handshake::EchConfigContents>::has_unknown_mandatory_extension Unexecuted instantiation: <rustls::msgs::handshake::EchConfigContents>::has_unknown_mandatory_extension Unexecuted instantiation: <rustls::msgs::handshake::EchConfigContents>::has_unknown_mandatory_extension |
2939 | | } |
2940 | | |
2941 | | impl Codec<'_> for EchConfigContents { |
2942 | 0 | fn encode(&self, bytes: &mut Vec<u8>) { |
2943 | 0 | self.key_config.encode(bytes); |
2944 | 0 | self.maximum_name_length.encode(bytes); |
2945 | 0 | let dns_name = &self.public_name.borrow(); |
2946 | 0 | PayloadU8::<MaybeEmpty>::encode_slice(dns_name.as_ref().as_ref(), bytes); |
2947 | 0 | self.extensions.encode(bytes); |
2948 | 0 | } Unexecuted instantiation: <rustls::msgs::handshake::EchConfigContents as rustls::msgs::codec::Codec>::encode Unexecuted instantiation: <rustls::msgs::handshake::EchConfigContents as rustls::msgs::codec::Codec>::encode Unexecuted instantiation: <rustls::msgs::handshake::EchConfigContents as rustls::msgs::codec::Codec>::encode |
2949 | | |
2950 | 0 | fn read(r: &mut Reader<'_>) -> Result<Self, InvalidMessage> { |
2951 | | Ok(Self { |
2952 | 0 | key_config: HpkeKeyConfig::read(r)?, |
2953 | 0 | maximum_name_length: u8::read(r)?, |
2954 | | public_name: { |
2955 | 0 | DnsName::try_from( |
2956 | 0 | PayloadU8::<MaybeEmpty>::read(r)? |
2957 | | .0 |
2958 | 0 | .as_slice(), |
2959 | | ) |
2960 | 0 | .map_err(|_| InvalidMessage::InvalidServerName)? |
2961 | 0 | .to_owned() |
2962 | | }, |
2963 | 0 | extensions: Vec::read(r)?, |
2964 | | }) |
2965 | 0 | } Unexecuted instantiation: <rustls::msgs::handshake::EchConfigContents as rustls::msgs::codec::Codec>::read Unexecuted instantiation: <rustls::msgs::handshake::EchConfigContents as rustls::msgs::codec::Codec>::read Unexecuted instantiation: <rustls::msgs::handshake::EchConfigContents as rustls::msgs::codec::Codec>::read |
2966 | | } |
2967 | | |
2968 | | /// An encrypted client hello (ECH) config. |
2969 | | #[derive(Clone, Debug, PartialEq)] |
2970 | | pub enum EchConfigPayload { |
2971 | | /// A recognized V18 ECH configuration. |
2972 | | V18(EchConfigContents), |
2973 | | /// An unknown version ECH configuration. |
2974 | | Unknown { |
2975 | | version: EchVersion, |
2976 | | contents: PayloadU16, |
2977 | | }, |
2978 | | } |
2979 | | |
2980 | | impl TlsListElement for EchConfigPayload { |
2981 | | const SIZE_LEN: ListLength = ListLength::U16; |
2982 | | } |
2983 | | |
2984 | | impl Codec<'_> for EchConfigPayload { |
2985 | 0 | fn encode(&self, bytes: &mut Vec<u8>) { |
2986 | 0 | match self { |
2987 | 0 | Self::V18(c) => { |
2988 | 0 | // Write the version, the length, and the contents. |
2989 | 0 | EchVersion::V18.encode(bytes); |
2990 | 0 | let inner = LengthPrefixedBuffer::new(ListLength::U16, bytes); |
2991 | 0 | c.encode(inner.buf); |
2992 | 0 | } |
2993 | 0 | Self::Unknown { version, contents } => { |
2994 | 0 | // Unknown configuration versions are opaque. |
2995 | 0 | version.encode(bytes); |
2996 | 0 | contents.encode(bytes); |
2997 | 0 | } |
2998 | | } |
2999 | 0 | } Unexecuted instantiation: <rustls::msgs::handshake::EchConfigPayload as rustls::msgs::codec::Codec>::encode Unexecuted instantiation: <rustls::msgs::handshake::EchConfigPayload as rustls::msgs::codec::Codec>::encode Unexecuted instantiation: <rustls::msgs::handshake::EchConfigPayload as rustls::msgs::codec::Codec>::encode |
3000 | | |
3001 | 0 | fn read(r: &mut Reader<'_>) -> Result<Self, InvalidMessage> { |
3002 | 0 | let version = EchVersion::read(r)?; |
3003 | 0 | let length = u16::read(r)?; |
3004 | 0 | let mut contents = r.sub(length as usize)?; |
3005 | | |
3006 | 0 | Ok(match version { |
3007 | 0 | EchVersion::V18 => Self::V18(EchConfigContents::read(&mut contents)?), |
3008 | | _ => { |
3009 | | // Note: we don't PayloadU16::read() here because we've already read the length prefix. |
3010 | 0 | let data = PayloadU16::new(contents.rest().into()); |
3011 | 0 | Self::Unknown { |
3012 | 0 | version, |
3013 | 0 | contents: data, |
3014 | 0 | } |
3015 | | } |
3016 | | }) |
3017 | 0 | } Unexecuted instantiation: <rustls::msgs::handshake::EchConfigPayload as rustls::msgs::codec::Codec>::read Unexecuted instantiation: <rustls::msgs::handshake::EchConfigPayload as rustls::msgs::codec::Codec>::read Unexecuted instantiation: <rustls::msgs::handshake::EchConfigPayload as rustls::msgs::codec::Codec>::read |
3018 | | } |
3019 | | |
3020 | | #[derive(Clone, Debug, PartialEq)] |
3021 | | pub enum EchConfigExtension { |
3022 | | Unknown(UnknownExtension), |
3023 | | } |
3024 | | |
3025 | | impl EchConfigExtension { |
3026 | 0 | pub(crate) fn ext_type(&self) -> ExtensionType { |
3027 | 0 | match self { |
3028 | 0 | Self::Unknown(r) => r.typ, |
3029 | | } |
3030 | 0 | } Unexecuted instantiation: <rustls::msgs::handshake::EchConfigExtension>::ext_type Unexecuted instantiation: <rustls::msgs::handshake::EchConfigExtension>::ext_type Unexecuted instantiation: <rustls::msgs::handshake::EchConfigExtension>::ext_type |
3031 | | } |
3032 | | |
3033 | | impl Codec<'_> for EchConfigExtension { |
3034 | 0 | fn encode(&self, bytes: &mut Vec<u8>) { |
3035 | 0 | self.ext_type().encode(bytes); |
3036 | | |
3037 | 0 | let nested = LengthPrefixedBuffer::new(ListLength::U16, bytes); |
3038 | 0 | match self { |
3039 | 0 | Self::Unknown(r) => r.encode(nested.buf), |
3040 | | } |
3041 | 0 | } Unexecuted instantiation: <rustls::msgs::handshake::EchConfigExtension as rustls::msgs::codec::Codec>::encode Unexecuted instantiation: <rustls::msgs::handshake::EchConfigExtension as rustls::msgs::codec::Codec>::encode Unexecuted instantiation: <rustls::msgs::handshake::EchConfigExtension as rustls::msgs::codec::Codec>::encode |
3042 | | |
3043 | 0 | fn read(r: &mut Reader<'_>) -> Result<Self, InvalidMessage> { |
3044 | 0 | let typ = ExtensionType::read(r)?; |
3045 | 0 | let len = u16::read(r)? as usize; |
3046 | 0 | let mut sub = r.sub(len)?; |
3047 | | |
3048 | | #[allow(clippy::match_single_binding)] // Future-proofing. |
3049 | 0 | let ext = match typ { |
3050 | 0 | _ => Self::Unknown(UnknownExtension::read(typ, &mut sub)), |
3051 | | }; |
3052 | | |
3053 | 0 | sub.expect_empty("EchConfigExtension") |
3054 | 0 | .map(|_| ext) |
3055 | 0 | } Unexecuted instantiation: <rustls::msgs::handshake::EchConfigExtension as rustls::msgs::codec::Codec>::read Unexecuted instantiation: <rustls::msgs::handshake::EchConfigExtension as rustls::msgs::codec::Codec>::read Unexecuted instantiation: <rustls::msgs::handshake::EchConfigExtension as rustls::msgs::codec::Codec>::read |
3056 | | } |
3057 | | |
3058 | | impl TlsListElement for EchConfigExtension { |
3059 | | const SIZE_LEN: ListLength = ListLength::U16; |
3060 | | } |
3061 | | |
3062 | | /// Representation of the `ECHClientHello` client extension specified in |
3063 | | /// [draft-ietf-tls-esni Section 5]. |
3064 | | /// |
3065 | | /// [draft-ietf-tls-esni Section 5]: <https://www.ietf.org/archive/id/draft-ietf-tls-esni-18.html#section-5> |
3066 | | #[derive(Clone, Debug)] |
3067 | | pub(crate) enum EncryptedClientHello { |
3068 | | /// A `ECHClientHello` with type [EchClientHelloType::ClientHelloOuter]. |
3069 | | Outer(EncryptedClientHelloOuter), |
3070 | | /// An empty `ECHClientHello` with type [EchClientHelloType::ClientHelloInner]. |
3071 | | /// |
3072 | | /// This variant has no payload. |
3073 | | Inner, |
3074 | | } |
3075 | | |
3076 | | impl Codec<'_> for EncryptedClientHello { |
3077 | 0 | fn encode(&self, bytes: &mut Vec<u8>) { |
3078 | 0 | match self { |
3079 | 0 | Self::Outer(payload) => { |
3080 | 0 | EchClientHelloType::ClientHelloOuter.encode(bytes); |
3081 | 0 | payload.encode(bytes); |
3082 | 0 | } |
3083 | 0 | Self::Inner => { |
3084 | 0 | EchClientHelloType::ClientHelloInner.encode(bytes); |
3085 | 0 | // Empty payload. |
3086 | 0 | } |
3087 | | } |
3088 | 0 | } Unexecuted instantiation: <rustls::msgs::handshake::EncryptedClientHello as rustls::msgs::codec::Codec>::encode Unexecuted instantiation: <rustls::msgs::handshake::EncryptedClientHello as rustls::msgs::codec::Codec>::encode Unexecuted instantiation: <rustls::msgs::handshake::EncryptedClientHello as rustls::msgs::codec::Codec>::encode |
3089 | | |
3090 | 0 | fn read(r: &mut Reader<'_>) -> Result<Self, InvalidMessage> { |
3091 | 0 | match EchClientHelloType::read(r)? { |
3092 | | EchClientHelloType::ClientHelloOuter => { |
3093 | 0 | Ok(Self::Outer(EncryptedClientHelloOuter::read(r)?)) |
3094 | | } |
3095 | 0 | EchClientHelloType::ClientHelloInner => Ok(Self::Inner), |
3096 | 0 | _ => Err(InvalidMessage::InvalidContentType), |
3097 | | } |
3098 | 0 | } Unexecuted instantiation: <rustls::msgs::handshake::EncryptedClientHello as rustls::msgs::codec::Codec>::read Unexecuted instantiation: <rustls::msgs::handshake::EncryptedClientHello as rustls::msgs::codec::Codec>::read Unexecuted instantiation: <rustls::msgs::handshake::EncryptedClientHello as rustls::msgs::codec::Codec>::read |
3099 | | } |
3100 | | |
3101 | | /// Representation of the ECHClientHello extension with type outer specified in |
3102 | | /// [draft-ietf-tls-esni Section 5]. |
3103 | | /// |
3104 | | /// [draft-ietf-tls-esni Section 5]: <https://www.ietf.org/archive/id/draft-ietf-tls-esni-18.html#section-5> |
3105 | | #[derive(Clone, Debug)] |
3106 | | pub(crate) struct EncryptedClientHelloOuter { |
3107 | | /// The cipher suite used to encrypt ClientHelloInner. Must match a value from |
3108 | | /// ECHConfigContents.cipher_suites list. |
3109 | | pub cipher_suite: HpkeSymmetricCipherSuite, |
3110 | | /// The ECHConfigContents.key_config.config_id for the chosen ECHConfig. |
3111 | | pub config_id: u8, |
3112 | | /// The HPKE encapsulated key, used by servers to decrypt the corresponding payload field. |
3113 | | /// This field is empty in a ClientHelloOuter sent in response to a HelloRetryRequest. |
3114 | | pub enc: PayloadU16, |
3115 | | /// The serialized and encrypted ClientHelloInner structure, encrypted using HPKE. |
3116 | | pub payload: PayloadU16<NonEmpty>, |
3117 | | } |
3118 | | |
3119 | | impl Codec<'_> for EncryptedClientHelloOuter { |
3120 | 0 | fn encode(&self, bytes: &mut Vec<u8>) { |
3121 | 0 | self.cipher_suite.encode(bytes); |
3122 | 0 | self.config_id.encode(bytes); |
3123 | 0 | self.enc.encode(bytes); |
3124 | 0 | self.payload.encode(bytes); |
3125 | 0 | } Unexecuted instantiation: <rustls::msgs::handshake::EncryptedClientHelloOuter as rustls::msgs::codec::Codec>::encode Unexecuted instantiation: <rustls::msgs::handshake::EncryptedClientHelloOuter as rustls::msgs::codec::Codec>::encode Unexecuted instantiation: <rustls::msgs::handshake::EncryptedClientHelloOuter as rustls::msgs::codec::Codec>::encode |
3126 | | |
3127 | 0 | fn read(r: &mut Reader<'_>) -> Result<Self, InvalidMessage> { |
3128 | | Ok(Self { |
3129 | 0 | cipher_suite: HpkeSymmetricCipherSuite::read(r)?, |
3130 | 0 | config_id: u8::read(r)?, |
3131 | 0 | enc: PayloadU16::read(r)?, |
3132 | 0 | payload: PayloadU16::read(r)?, |
3133 | | }) |
3134 | 0 | } Unexecuted instantiation: <rustls::msgs::handshake::EncryptedClientHelloOuter as rustls::msgs::codec::Codec>::read Unexecuted instantiation: <rustls::msgs::handshake::EncryptedClientHelloOuter as rustls::msgs::codec::Codec>::read Unexecuted instantiation: <rustls::msgs::handshake::EncryptedClientHelloOuter as rustls::msgs::codec::Codec>::read |
3135 | | } |
3136 | | |
3137 | | /// Representation of the ECHEncryptedExtensions extension specified in |
3138 | | /// [draft-ietf-tls-esni Section 5]. |
3139 | | /// |
3140 | | /// [draft-ietf-tls-esni Section 5]: <https://www.ietf.org/archive/id/draft-ietf-tls-esni-18.html#section-5> |
3141 | | #[derive(Clone, Debug)] |
3142 | | pub(crate) struct ServerEncryptedClientHello { |
3143 | | pub(crate) retry_configs: Vec<EchConfigPayload>, |
3144 | | } |
3145 | | |
3146 | | impl Codec<'_> for ServerEncryptedClientHello { |
3147 | 0 | fn encode(&self, bytes: &mut Vec<u8>) { |
3148 | 0 | self.retry_configs.encode(bytes); |
3149 | 0 | } Unexecuted instantiation: <rustls::msgs::handshake::ServerEncryptedClientHello as rustls::msgs::codec::Codec>::encode Unexecuted instantiation: <rustls::msgs::handshake::ServerEncryptedClientHello as rustls::msgs::codec::Codec>::encode Unexecuted instantiation: <rustls::msgs::handshake::ServerEncryptedClientHello as rustls::msgs::codec::Codec>::encode |
3150 | | |
3151 | 0 | fn read(r: &mut Reader<'_>) -> Result<Self, InvalidMessage> { |
3152 | | Ok(Self { |
3153 | 0 | retry_configs: Vec::<EchConfigPayload>::read(r)?, |
3154 | | }) |
3155 | 0 | } Unexecuted instantiation: <rustls::msgs::handshake::ServerEncryptedClientHello as rustls::msgs::codec::Codec>::read Unexecuted instantiation: <rustls::msgs::handshake::ServerEncryptedClientHello as rustls::msgs::codec::Codec>::read Unexecuted instantiation: <rustls::msgs::handshake::ServerEncryptedClientHello as rustls::msgs::codec::Codec>::read |
3156 | | } |
3157 | | |
3158 | | /// The method of encoding to use for a handshake message. |
3159 | | /// |
3160 | | /// In some cases a handshake message may be encoded differently depending on the purpose |
3161 | | /// the encoded message is being used for. |
3162 | | pub(crate) enum Encoding { |
3163 | | /// Standard RFC 8446 encoding. |
3164 | | Standard, |
3165 | | /// Encoding for ECH confirmation for HRR. |
3166 | | EchConfirmation, |
3167 | | /// Encoding for ECH inner client hello. |
3168 | | EchInnerHello { to_compress: Vec<ExtensionType> }, |
3169 | | } |
3170 | | |
3171 | 0 | fn has_duplicates<I: IntoIterator<Item = E>, E: Into<T>, T: Eq + Ord>(iter: I) -> bool { |
3172 | 0 | let mut seen = BTreeSet::new(); |
3173 | | |
3174 | 0 | for x in iter { |
3175 | 0 | if !seen.insert(x.into()) { |
3176 | 0 | return true; |
3177 | 0 | } |
3178 | | } |
3179 | | |
3180 | 0 | false |
3181 | 0 | } Unexecuted instantiation: rustls::msgs::handshake::has_duplicates::<core::iter::adapters::map::Map<core::slice::iter::Iter<rustls::msgs::handshake::KeyShareEntry>, <rustls::msgs::handshake::ClientHelloPayload>::has_keyshare_extension_with_duplicates::{closure#0}::{closure#0}>, u16, u16>Unexecuted instantiation: rustls::msgs::handshake::has_duplicates::<core::iter::adapters::map::Map<core::slice::iter::Iter<rustls::msgs::handshake::EchConfigExtension>, <rustls::msgs::handshake::EchConfigContents>::has_duplicate_extension::{closure#0}>, rustls::msgs::enums::ExtensionType, u16>Unexecuted instantiation: rustls::msgs::handshake::has_duplicates::<core::iter::adapters::cloned::Cloned<core::slice::iter::Iter<rustls::enums::CertificateCompressionAlgorithm>>, rustls::enums::CertificateCompressionAlgorithm, u16> Unexecuted instantiation: rustls::msgs::handshake::has_duplicates::<core::iter::adapters::map::Map<core::slice::iter::Iter<rustls::msgs::handshake::KeyShareEntry>, <rustls::msgs::handshake::ClientHelloPayload>::has_keyshare_extension_with_duplicates::{closure#0}::{closure#0}>, u16, u16>Unexecuted instantiation: rustls::msgs::handshake::has_duplicates::<core::iter::adapters::map::Map<core::slice::iter::Iter<rustls::msgs::handshake::EchConfigExtension>, <rustls::msgs::handshake::EchConfigContents>::has_duplicate_extension::{closure#0}>, rustls::msgs::enums::ExtensionType, u16>Unexecuted instantiation: rustls::msgs::handshake::has_duplicates::<core::iter::adapters::cloned::Cloned<core::slice::iter::Iter<rustls::enums::CertificateCompressionAlgorithm>>, rustls::enums::CertificateCompressionAlgorithm, u16> Unexecuted instantiation: rustls::msgs::handshake::has_duplicates::<core::iter::adapters::map::Map<core::slice::iter::Iter<rustls::msgs::handshake::KeyShareEntry>, <rustls::msgs::handshake::ClientHelloPayload>::has_keyshare_extension_with_duplicates::{closure#0}::{closure#0}>, u16, u16>Unexecuted instantiation: rustls::msgs::handshake::has_duplicates::<core::iter::adapters::map::Map<core::slice::iter::Iter<rustls::msgs::handshake::EchConfigExtension>, <rustls::msgs::handshake::EchConfigContents>::has_duplicate_extension::{closure#0}>, rustls::msgs::enums::ExtensionType, u16>Unexecuted instantiation: rustls::msgs::handshake::has_duplicates::<core::iter::adapters::cloned::Cloned<core::slice::iter::Iter<rustls::enums::CertificateCompressionAlgorithm>>, rustls::enums::CertificateCompressionAlgorithm, u16> |
3182 | | |
3183 | | struct DuplicateExtensionChecker(BTreeSet<u16>); |
3184 | | |
3185 | | impl DuplicateExtensionChecker { |
3186 | 0 | fn new() -> Self { |
3187 | 0 | Self(BTreeSet::new()) |
3188 | 0 | } Unexecuted instantiation: <rustls::msgs::handshake::DuplicateExtensionChecker>::new Unexecuted instantiation: <rustls::msgs::handshake::DuplicateExtensionChecker>::new Unexecuted instantiation: <rustls::msgs::handshake::DuplicateExtensionChecker>::new |
3189 | | |
3190 | 0 | fn check(&mut self, typ: ExtensionType) -> Result<(), InvalidMessage> { |
3191 | 0 | let u = u16::from(typ); |
3192 | 0 | match self.0.insert(u) { |
3193 | 0 | true => Ok(()), |
3194 | 0 | false => Err(InvalidMessage::DuplicateExtension(u)), |
3195 | | } |
3196 | 0 | } Unexecuted instantiation: <rustls::msgs::handshake::DuplicateExtensionChecker>::check Unexecuted instantiation: <rustls::msgs::handshake::DuplicateExtensionChecker>::check Unexecuted instantiation: <rustls::msgs::handshake::DuplicateExtensionChecker>::check |
3197 | | } |
3198 | | |
3199 | 0 | fn low_quality_integer_hash(mut x: u32) -> u32 { |
3200 | 0 | x = x |
3201 | 0 | .wrapping_add(0x7ed55d16) |
3202 | 0 | .wrapping_add(x << 12); |
3203 | 0 | x = (x ^ 0xc761c23c) ^ (x >> 19); |
3204 | 0 | x = x |
3205 | 0 | .wrapping_add(0x165667b1) |
3206 | 0 | .wrapping_add(x << 5); |
3207 | 0 | x = x.wrapping_add(0xd3a2646c) ^ (x << 9); |
3208 | 0 | x = x |
3209 | 0 | .wrapping_add(0xfd7046c5) |
3210 | 0 | .wrapping_add(x << 3); |
3211 | 0 | x = (x ^ 0xb55a4f09) ^ (x >> 16); |
3212 | 0 | x |
3213 | 0 | } Unexecuted instantiation: rustls::msgs::handshake::low_quality_integer_hash Unexecuted instantiation: rustls::msgs::handshake::low_quality_integer_hash Unexecuted instantiation: rustls::msgs::handshake::low_quality_integer_hash |
3214 | | |
3215 | | #[cfg(test)] |
3216 | | mod tests { |
3217 | | use super::*; |
3218 | | |
3219 | | #[test] |
3220 | | fn test_ech_config_dupe_exts() { |
3221 | | let unknown_ext = EchConfigExtension::Unknown(UnknownExtension { |
3222 | | typ: ExtensionType::Unknown(0x42), |
3223 | | payload: Payload::new(vec![0x42]), |
3224 | | }); |
3225 | | let mut config = config_template(); |
3226 | | config |
3227 | | .extensions |
3228 | | .push(unknown_ext.clone()); |
3229 | | config.extensions.push(unknown_ext); |
3230 | | |
3231 | | assert!(config.has_duplicate_extension()); |
3232 | | assert!(!config.has_unknown_mandatory_extension()); |
3233 | | } |
3234 | | |
3235 | | #[test] |
3236 | | fn test_ech_config_mandatory_exts() { |
3237 | | let mandatory_unknown_ext = EchConfigExtension::Unknown(UnknownExtension { |
3238 | | typ: ExtensionType::Unknown(0x42 | 0x8000), // Note: high bit set. |
3239 | | payload: Payload::new(vec![0x42]), |
3240 | | }); |
3241 | | let mut config = config_template(); |
3242 | | config |
3243 | | .extensions |
3244 | | .push(mandatory_unknown_ext); |
3245 | | |
3246 | | assert!(!config.has_duplicate_extension()); |
3247 | | assert!(config.has_unknown_mandatory_extension()); |
3248 | | } |
3249 | | |
3250 | | fn config_template() -> EchConfigContents { |
3251 | | EchConfigContents { |
3252 | | key_config: HpkeKeyConfig { |
3253 | | config_id: 0, |
3254 | | kem_id: HpkeKem::DHKEM_P256_HKDF_SHA256, |
3255 | | public_key: PayloadU16::new(b"xxx".into()), |
3256 | | symmetric_cipher_suites: vec![HpkeSymmetricCipherSuite { |
3257 | | kdf_id: HpkeKdf::HKDF_SHA256, |
3258 | | aead_id: HpkeAead::AES_128_GCM, |
3259 | | }], |
3260 | | }, |
3261 | | maximum_name_length: 0, |
3262 | | public_name: DnsName::try_from("example.com").unwrap(), |
3263 | | extensions: vec![], |
3264 | | } |
3265 | | } |
3266 | | } |