use std::{borrow::Cow, collections::HashSet, fmt};
use serde::{de, Deserialize, Deserializer, Serialize, Serializer};
use tls_parser::{TlsCipherSuite as ParsedTlsCipherSuite, TlsExtensionType, TlsMessageHandshake};
use super::{
enums::{
is_grease_value, AuthenticatedEncryptionWithAssociatedData,
CertificateCompressionAlgorithm, CertificateStatusType, CompressionAlgorithm,
ECPointFormat, KeyDerivationFunction, PskKeyExchangeMode, SignatureAlgorithm, TlsVersion,
},
group::NamedGroup,
ja3::Ja3Fingerprint,
ja4::Ja4Fingerprint,
parser,
version::SupportedVersions,
};
use crate::quic::{
parse_transport_parameters, QuicTransportParameter, TRANSPORT_PARAMETERS_EXTENSION_ID,
};
const DEFAULT_CLIENT_HELLO_CAPACITY: usize = 2048;
const DEFAULT_CLIENT_HELLO_CAPTURE_LIMIT: usize = 64 * 1024;
const TLS_RECORD_HEADER_LEN: usize = 5;
const TLS_HANDSHAKE_HEADER_LEN: usize = 4;
const TLS_HANDSHAKE_CLIENT_HELLO: u8 = 1;
const TLS_HANDSHAKE_CONTENT_TYPE: u8 = 22;
#[derive(Debug, Clone)]
pub struct ClientHelloBuffer {
buf: Vec<u8>,
capture_limit: usize,
complete: bool,
capture: ClientHelloCaptureState,
}
#[derive(Debug)]
pub struct ClientHelloHandshakeBuffer {
buf: Vec<u8>,
capture_limit: usize,
handshake_len: Option<usize>,
error: Option<ClientHelloParseStage>,
}
#[derive(Debug, Clone, Default)]
struct ClientHelloCaptureState {
record_offset: usize,
handshake_header: [u8; TLS_HANDSHAKE_HEADER_LEN],
handshake_header_len: usize,
handshake_len: Option<usize>,
handshake_bytes_seen: usize,
error: Option<ClientHelloParseStage>,
}
impl ClientHelloCaptureState {
fn consume_payload(&mut self, payload: &[u8]) -> Result<bool, ClientHelloParseStage> {
let mut cursor = 0usize;
if self.handshake_header_len < TLS_HANDSHAKE_HEADER_LEN {
let remaining_header = TLS_HANDSHAKE_HEADER_LEN - self.handshake_header_len;
let copied = remaining_header.min(payload.len());
let header_end = self.handshake_header_len + copied;
self.handshake_header[self.handshake_header_len..header_end]
.copy_from_slice(&payload[..copied]);
self.handshake_header_len = header_end;
self.handshake_bytes_seen += copied;
cursor = copied;
if self.handshake_header_len < TLS_HANDSHAKE_HEADER_LEN {
return Ok(false);
}
self.handshake_len =
client_hello_handshake_len(&self.handshake_header).map_err(|error| error.stage)?;
}
let handshake_len = self
.handshake_len
.ok_or(ClientHelloParseStage::ClientHello)?;
let remaining = handshake_len.saturating_sub(self.handshake_bytes_seen);
let available = payload.len().saturating_sub(cursor);
self.handshake_bytes_seen += remaining.min(available);
Ok(self.handshake_bytes_seen >= handshake_len)
}
}
impl ClientHelloBuffer {
pub fn new() -> Self {
Self::with_capacity_and_limit(
DEFAULT_CLIENT_HELLO_CAPACITY,
DEFAULT_CLIENT_HELLO_CAPTURE_LIMIT,
)
}
pub fn with_capacity(capacity: usize) -> Self {
Self::with_capacity_and_limit(capacity, DEFAULT_CLIENT_HELLO_CAPTURE_LIMIT.max(capacity))
}
pub fn with_capture_limit(capture_limit: usize) -> Self {
Self::with_capacity_and_limit(
DEFAULT_CLIENT_HELLO_CAPACITY.min(capture_limit),
capture_limit,
)
}
pub fn with_capacity_and_limit(capacity: usize, capture_limit: usize) -> Self {
Self {
buf: Vec::with_capacity(capacity),
capture_limit: capture_limit.max(capacity),
capture: ClientHelloCaptureState::default(),
complete: false,
}
}
pub fn from_bytes(bytes: impl Into<Vec<u8>>) -> Self {
let buf = bytes.into();
let capture_limit = DEFAULT_CLIENT_HELLO_CAPTURE_LIMIT.max(buf.len());
let mut buffer = Self {
buf,
capture_limit,
complete: false,
capture: ClientHelloCaptureState::default(),
};
if let Some(record_end) = buffer.scan_available_records() {
buffer.buf.truncate(record_end);
}
buffer
}
pub fn parse(&self) -> Result<ClientHello, ClientHelloParseError> {
ClientHello::parse(&self.buf)
}
pub fn try_parse(&self) -> Result<Option<ClientHello>, ClientHelloParseError> {
if let Some(stage) = self.capture.error {
return Err(ClientHelloParseError::new(stage));
}
if !self.complete {
return Ok(None);
}
let Some(captured) = capture_client_hello(&self.buf)? else {
return Ok(None);
};
ClientHello::parse_handshake(captured.handshake.as_ref()).map(Some)
}
pub fn as_bytes(&self) -> &[u8] {
&self.buf
}
pub fn len(&self) -> usize {
self.buf.len()
}
pub fn is_empty(&self) -> bool {
self.buf.is_empty()
}
pub const fn is_complete(&self) -> bool {
self.complete
}
pub const fn is_invalid(&self) -> bool {
self.capture.error.is_some()
}
pub const fn capture_limit(&self) -> usize {
self.capture_limit
}
pub fn is_full(&self) -> bool {
self.buf.len() >= self.capture_limit
}
pub fn extend(&mut self, data: &[u8]) -> usize {
if self.complete || self.is_invalid() || self.is_full() {
return 0;
}
let initial_len = self.buf.len();
let accepted = data
.len()
.min(self.capture_limit.saturating_sub(initial_len));
self.buf.extend_from_slice(&data[..accepted]);
if let Some(record_end) = self.scan_available_records() {
self.buf.truncate(record_end);
}
self.buf.len().saturating_sub(initial_len)
}
fn scan_available_records(&mut self) -> Option<usize> {
while !self.complete && self.capture.error.is_none() {
let record_offset = self.capture.record_offset;
let Some(header_end) = record_offset.checked_add(TLS_RECORD_HEADER_LEN) else {
self.capture.error = Some(ClientHelloParseStage::TlsRecord);
return None;
};
let header = self.buf.get(record_offset..header_end)?;
if header[0] != TLS_HANDSHAKE_CONTENT_TYPE {
self.capture.error = Some(ClientHelloParseStage::RecordMessages);
return None;
}
let payload_len = usize::from(u16::from_be_bytes([header[3], header[4]]));
if payload_len > usize::from(tls_parser::MAX_RECORD_LEN) {
self.capture.error = Some(ClientHelloParseStage::TlsRecord);
return None;
}
let Some(record_end) = header_end.checked_add(payload_len) else {
self.capture.error = Some(ClientHelloParseStage::TlsRecord);
return None;
};
let payload = self.buf.get(header_end..record_end)?;
match self.capture.consume_payload(payload) {
Ok(true) => {
self.capture.record_offset = record_end;
self.complete = true;
return Some(record_end);
}
Ok(false) => self.capture.record_offset = record_end,
Err(stage) => {
self.capture.error = Some(stage);
return None;
}
}
}
None
}
}
impl ClientHelloHandshakeBuffer {
pub fn new() -> Self {
Self::with_capacity_and_limit(
DEFAULT_CLIENT_HELLO_CAPACITY,
DEFAULT_CLIENT_HELLO_CAPTURE_LIMIT,
)
}
pub fn with_capacity(capacity: usize) -> Self {
Self::with_capacity_and_limit(capacity, DEFAULT_CLIENT_HELLO_CAPTURE_LIMIT.max(capacity))
}
pub fn with_capture_limit(capture_limit: usize) -> Self {
Self::with_capacity_and_limit(
DEFAULT_CLIENT_HELLO_CAPACITY.min(capture_limit),
capture_limit,
)
}
pub fn with_capacity_and_limit(capacity: usize, capture_limit: usize) -> Self {
Self {
buf: Vec::with_capacity(capacity),
capture_limit: capture_limit.max(capacity),
handshake_len: None,
error: None,
}
}
pub fn from_bytes(bytes: impl Into<Vec<u8>>) -> Self {
let buf = bytes.into();
let capture_limit = DEFAULT_CLIENT_HELLO_CAPTURE_LIMIT.max(buf.len());
let mut buffer = Self {
buf,
capture_limit,
handshake_len: None,
error: None,
};
buffer.inspect_header();
buffer.truncate_complete();
buffer
}
pub fn parse(&self) -> Result<ClientHello, ClientHelloParseError> {
let length = self
.handshake_len
.filter(|length| self.buf.len() >= *length)
.ok_or(ClientHelloParseError::new(
self.error.unwrap_or(ClientHelloParseStage::ClientHello),
))?;
ClientHello::parse_handshake(&self.buf[..length])
}
pub fn try_parse(&self) -> Result<Option<ClientHello>, ClientHelloParseError> {
if let Some(stage) = self.error {
return Err(ClientHelloParseError::new(stage));
}
if !self.is_complete() {
return Ok(None);
}
self.parse().map(Some)
}
pub fn extend(&mut self, data: &[u8]) -> usize {
if self.is_complete() || self.is_invalid() || self.is_full() {
return 0;
}
let initial_len = self.buf.len();
let accepted = data
.len()
.min(self.capture_limit.saturating_sub(initial_len));
self.buf.extend_from_slice(&data[..accepted]);
self.inspect_header();
self.truncate_complete();
self.buf.len().saturating_sub(initial_len)
}
pub fn as_bytes(&self) -> &[u8] {
&self.buf
}
pub fn len(&self) -> usize {
self.buf.len()
}
pub fn is_empty(&self) -> bool {
self.buf.is_empty()
}
pub fn is_complete(&self) -> bool {
self.handshake_len
.is_some_and(|length| self.buf.len() >= length)
}
pub const fn is_invalid(&self) -> bool {
self.error.is_some()
}
pub const fn capture_limit(&self) -> usize {
self.capture_limit
}
pub fn is_full(&self) -> bool {
self.buf.len() >= self.capture_limit
}
fn inspect_header(&mut self) {
if self.handshake_len.is_some() || self.error.is_some() {
return;
}
match client_hello_handshake_len(&self.buf) {
Ok(length) => self.handshake_len = length,
Err(error) => self.error = Some(error.stage),
}
}
fn truncate_complete(&mut self) {
if let Some(length) = self.handshake_len {
self.buf.truncate(length);
}
}
}
struct CapturedClientHello<'a> {
handshake: Cow<'a, [u8]>,
}
fn capture_client_hello(
data: &[u8],
) -> Result<Option<CapturedClientHello<'_>>, ClientHelloParseError> {
let mut cursor = 0usize;
let mut handshake = Vec::new();
loop {
let Some(header_end) = cursor.checked_add(TLS_RECORD_HEADER_LEN) else {
return Err(ClientHelloParseError::new(ClientHelloParseStage::TlsRecord));
};
let Some(header) = data.get(cursor..header_end) else {
return Ok(None);
};
if header[0] != TLS_HANDSHAKE_CONTENT_TYPE {
return Err(ClientHelloParseError::new(
ClientHelloParseStage::RecordMessages,
));
}
let payload_len = usize::from(u16::from_be_bytes([header[3], header[4]]));
if payload_len > usize::from(tls_parser::MAX_RECORD_LEN) {
return Err(ClientHelloParseError::new(ClientHelloParseStage::TlsRecord));
}
let Some(record_end) = header_end.checked_add(payload_len) else {
return Err(ClientHelloParseError::new(ClientHelloParseStage::TlsRecord));
};
let Some(payload) = data.get(header_end..record_end) else {
return Ok(None);
};
if handshake.is_empty() {
if let Some(handshake_len) = client_hello_handshake_len(payload)? {
if let Some(handshake) = payload.get(..handshake_len) {
return Ok(Some(CapturedClientHello {
handshake: Cow::Borrowed(handshake),
}));
}
}
}
handshake.extend_from_slice(payload);
if let Some(handshake_len) = client_hello_handshake_len(&handshake)? {
if handshake.len() >= handshake_len {
handshake.truncate(handshake_len);
return Ok(Some(CapturedClientHello {
handshake: Cow::Owned(handshake),
}));
}
}
cursor = record_end;
}
}
fn client_hello_handshake_len(data: &[u8]) -> Result<Option<usize>, ClientHelloParseError> {
let Some(header) = data.get(..TLS_HANDSHAKE_HEADER_LEN) else {
return Ok(None);
};
if header[0] != TLS_HANDSHAKE_CLIENT_HELLO {
return Err(ClientHelloParseError::new(
ClientHelloParseStage::ClientHello,
));
}
let payload_len = usize::try_from(u32::from_be_bytes([0, header[1], header[2], header[3]]))
.map_err(|_| ClientHelloParseError::new(ClientHelloParseStage::ClientHello))?;
TLS_HANDSHAKE_HEADER_LEN
.checked_add(payload_len)
.map(Some)
.ok_or(ClientHelloParseError::new(
ClientHelloParseStage::ClientHello,
))
}
impl Default for ClientHelloHandshakeBuffer {
fn default() -> Self {
Self::new()
}
}
impl From<Vec<u8>> for ClientHelloHandshakeBuffer {
fn from(bytes: Vec<u8>) -> Self {
Self::from_bytes(bytes)
}
}
impl From<&[u8]> for ClientHelloHandshakeBuffer {
fn from(bytes: &[u8]) -> Self {
Self::from_bytes(bytes)
}
}
impl Default for ClientHelloBuffer {
fn default() -> Self {
Self::new()
}
}
impl From<Vec<u8>> for ClientHelloBuffer {
fn from(bytes: Vec<u8>) -> Self {
Self::from_bytes(bytes)
}
}
impl From<&[u8]> for ClientHelloBuffer {
fn from(bytes: &[u8]) -> Self {
Self::from_bytes(bytes)
}
}
#[derive(Clone, PartialEq, Eq, Hash, Default)]
pub struct HexBytes(Box<[u8]>);
impl HexBytes {
pub fn new(bytes: impl Into<Box<[u8]>>) -> Self {
Self(bytes.into())
}
pub fn as_bytes(&self) -> &[u8] {
&self.0
}
pub fn len(&self) -> usize {
self.0.len()
}
pub fn is_empty(&self) -> bool {
self.0.is_empty()
}
pub fn into_bytes(self) -> Box<[u8]> {
self.0
}
}
impl fmt::Display for HexBytes {
fn fmt(&self, formatter: &mut fmt::Formatter<'_>) -> fmt::Result {
for byte in self.as_bytes() {
write!(formatter, "{byte:02x}")?;
}
Ok(())
}
}
impl fmt::Debug for HexBytes {
fn fmt(&self, formatter: &mut fmt::Formatter<'_>) -> fmt::Result {
write!(formatter, "HexBytes({self})")
}
}
impl Serialize for HexBytes {
fn serialize<S>(&self, serializer: S) -> Result<S::Ok, S::Error>
where
S: Serializer,
{
serializer.serialize_str(&hex::encode(self.as_bytes()))
}
}
impl<'de> Deserialize<'de> for HexBytes {
fn deserialize<D>(deserializer: D) -> Result<Self, D::Error>
where
D: Deserializer<'de>,
{
let encoded = Box::<str>::deserialize(deserializer)?;
if encoded
.bytes()
.any(|byte| !byte.is_ascii_digit() && !(b'a'..=b'f').contains(&byte))
{
return Err(de::Error::custom(
"hexadecimal TLS bytes must use lowercase ASCII digits",
));
}
hex::decode(encoded.as_ref())
.map(Self::from)
.map_err(de::Error::custom)
}
}
impl AsRef<[u8]> for HexBytes {
fn as_ref(&self) -> &[u8] {
self.as_bytes()
}
}
impl From<Vec<u8>> for HexBytes {
fn from(bytes: Vec<u8>) -> Self {
Self(bytes.into_boxed_slice())
}
}
impl From<Box<[u8]>> for HexBytes {
fn from(bytes: Box<[u8]>) -> Self {
Self(bytes)
}
}
impl From<&[u8]> for HexBytes {
fn from(bytes: &[u8]) -> Self {
Self(bytes.into())
}
}
impl<const N: usize> From<[u8; N]> for HexBytes {
fn from(bytes: [u8; N]) -> Self {
Self(Vec::from(bytes).into_boxed_slice())
}
}
#[derive(Debug, Clone, Copy, PartialEq, Eq, thiserror::Error)]
#[error("TLS protocol names must contain 1 to 255 bytes, got {length}")]
pub struct ProtocolNameError {
pub length: usize,
}
#[derive(Clone, PartialEq, Eq, Hash)]
pub struct ProtocolName(Box<[u8]>);
impl ProtocolName {
pub fn from_bytes(bytes: impl Into<Box<[u8]>>) -> Result<Self, ProtocolNameError> {
let bytes = bytes.into();
if bytes.is_empty() || bytes.len() > usize::from(u8::MAX) {
return Err(ProtocolNameError {
length: bytes.len(),
});
}
Ok(Self(bytes))
}
pub fn as_bytes(&self) -> &[u8] {
&self.0
}
pub fn as_str(&self) -> Option<&str> {
std::str::from_utf8(self.as_bytes()).ok()
}
pub fn len(&self) -> usize {
self.0.len()
}
pub fn is_empty(&self) -> bool {
self.0.is_empty()
}
}
impl fmt::Display for ProtocolName {
fn fmt(&self, formatter: &mut fmt::Formatter<'_>) -> fmt::Result {
if let Some(value) = self.as_str() {
return formatter.write_str(value);
}
for byte in self.as_bytes() {
write!(formatter, "{byte:02x}")?;
}
Ok(())
}
}
impl fmt::Debug for ProtocolName {
fn fmt(&self, formatter: &mut fmt::Formatter<'_>) -> fmt::Result {
match self.as_str() {
Some(value) => formatter.debug_tuple("ProtocolName").field(&value).finish(),
None => write!(formatter, "ProtocolName({self})"),
}
}
}
impl Serialize for ProtocolName {
fn serialize<S>(&self, serializer: S) -> Result<S::Ok, S::Error>
where
S: Serializer,
{
match self.as_str() {
Some(value) => serializer.serialize_str(value),
None => ProtocolNameHex {
hex: hex::encode(self.as_bytes()).into_boxed_str(),
}
.serialize(serializer),
}
}
}
impl<'de> Deserialize<'de> for ProtocolName {
fn deserialize<D>(deserializer: D) -> Result<Self, D::Error>
where
D: Deserializer<'de>,
{
let bytes = match ProtocolNameRepr::deserialize(deserializer)? {
ProtocolNameRepr::Text(value) => value.into_boxed_bytes(),
ProtocolNameRepr::Bytes { hex } => hex.into_bytes(),
};
Self::from_bytes(bytes).map_err(de::Error::custom)
}
}
impl AsRef<[u8]> for ProtocolName {
fn as_ref(&self) -> &[u8] {
self.as_bytes()
}
}
impl TryFrom<Vec<u8>> for ProtocolName {
type Error = ProtocolNameError;
fn try_from(bytes: Vec<u8>) -> Result<Self, Self::Error> {
Self::from_bytes(bytes)
}
}
impl TryFrom<Box<[u8]>> for ProtocolName {
type Error = ProtocolNameError;
fn try_from(bytes: Box<[u8]>) -> Result<Self, Self::Error> {
Self::from_bytes(bytes)
}
}
impl TryFrom<&[u8]> for ProtocolName {
type Error = ProtocolNameError;
fn try_from(bytes: &[u8]) -> Result<Self, Self::Error> {
Self::from_bytes(bytes)
}
}
impl TryFrom<&str> for ProtocolName {
type Error = ProtocolNameError;
fn try_from(value: &str) -> Result<Self, Self::Error> {
Self::from_bytes(value.as_bytes())
}
}
#[derive(Deserialize)]
#[serde(untagged)]
enum ProtocolNameRepr {
Text(Box<str>),
Bytes { hex: HexBytes },
}
#[derive(Serialize)]
struct ProtocolNameHex {
hex: Box<str>,
}
#[derive(Debug, Clone, PartialEq, Eq, Serialize, Hash)]
pub struct TlsCipherSuite {
pub id: u16,
pub name: Box<str>,
}
impl TlsCipherSuite {
pub fn from_id(id: u16) -> Self {
Self {
id,
name: Self::name_for_id(id).into(),
}
}
pub fn from_name(name: &str) -> Option<Self> {
ParsedTlsCipherSuite::from_name(name).map(|cipher| Self {
id: cipher.id.0,
name: cipher.name.into(),
})
}
pub fn is_grease(&self) -> bool {
is_grease_value(self.id)
}
fn name_for_id(id: u16) -> &'static str {
ParsedTlsCipherSuite::from_id(id)
.map(|cipher| cipher.name)
.unwrap_or_else(|| {
if is_grease_value(id) {
"GREASE"
} else {
"Unknown"
}
})
}
}
impl From<u16> for TlsCipherSuite {
fn from(id: u16) -> Self {
Self::from_id(id)
}
}
#[derive(Deserialize)]
struct TlsCipherSuiteRepr {
id: u16,
name: Box<str>,
}
impl<'de> Deserialize<'de> for TlsCipherSuite {
fn deserialize<D>(deserializer: D) -> Result<Self, D::Error>
where
D: Deserializer<'de>,
{
let repr = TlsCipherSuiteRepr::deserialize(deserializer)?;
let expected = Self::name_for_id(repr.id);
if repr.name.as_ref() != expected {
return Err(de::Error::custom(format_args!(
"TLS cipher suite {:#06x} has name {expected:?}, not {:?}",
repr.id, repr.name,
)));
}
Ok(Self {
id: repr.id,
name: repr.name,
})
}
}
#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize)]
pub struct ClientHello {
pub tls_version: TlsVersion,
pub tls_version_negotiated: Option<TlsVersion>,
#[serde(deserialize_with = "client_hello_hex::random")]
pub client_random: HexBytes,
#[serde(deserialize_with = "client_hello_hex::session_id")]
pub session_id: Option<HexBytes>,
pub compression_algorithms: Vec<CompressionAlgorithm>,
pub cipher_suites: Vec<TlsCipherSuite>,
pub extensions: Vec<TlsExtension>,
}
mod client_hello_hex {
use serde::{de, Deserialize, Deserializer};
use super::HexBytes;
pub(super) fn random<'de, D>(deserializer: D) -> Result<HexBytes, D::Error>
where
D: Deserializer<'de>,
{
let value = HexBytes::deserialize(deserializer)?;
if value.len() != 32 {
return Err(de::Error::custom(format_args!(
"TLS ClientHello random must contain 32 bytes, got {}",
value.len(),
)));
}
Ok(value)
}
pub(super) fn session_id<'de, D>(deserializer: D) -> Result<Option<HexBytes>, D::Error>
where
D: Deserializer<'de>,
{
let value = Option::<HexBytes>::deserialize(deserializer)?;
if let Some(value) = &value {
if value.is_empty() || value.len() > 32 {
return Err(de::Error::custom(format_args!(
"TLS legacy session ID must contain 1 to 32 bytes, got {}",
value.len(),
)));
}
}
Ok(value)
}
}
#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize)]
#[serde(rename_all = "snake_case")]
pub enum TlsExtension {
ServerName {
#[serde(deserialize_with = "extension_id::server_name")]
value: u16,
data: Vec<Box<str>>,
},
SupportedGroups {
#[serde(deserialize_with = "extension_id::supported_groups")]
value: u16,
data: Vec<NamedGroup>,
},
EcPointFormats {
#[serde(deserialize_with = "extension_id::ec_point_formats")]
value: u16,
data: Vec<ECPointFormat>,
},
SignatureAlgorithms {
#[serde(deserialize_with = "extension_id::signature_algorithms")]
value: u16,
data: Vec<SignatureAlgorithm>,
},
StatusRequest {
#[serde(deserialize_with = "extension_id::status_request")]
value: u16,
data: StatusRequest,
},
ApplicationLayerProtocolNegotiation {
#[serde(deserialize_with = "extension_id::alpn")]
value: u16,
data: Vec<ProtocolName>,
},
ApplicationSettingsOld {
#[serde(deserialize_with = "extension_id::application_settings_old")]
value: u16,
data: Vec<ProtocolName>,
},
ApplicationSettings {
#[serde(deserialize_with = "extension_id::application_settings")]
value: u16,
data: Vec<ProtocolName>,
},
SupportedVersions {
#[serde(deserialize_with = "extension_id::supported_versions")]
value: u16,
data: SupportedVersions,
},
SessionTicket {
#[serde(deserialize_with = "extension_id::session_ticket")]
value: u16,
data: HexBytes,
},
CertificateCompression {
#[serde(deserialize_with = "extension_id::certificate_compression")]
value: u16,
data: Vec<CertificateCompressionAlgorithm>,
},
RecordSizeLimit {
#[serde(deserialize_with = "extension_id::record_size_limit")]
value: u16,
data: u16,
},
DelegatedCredentials {
#[serde(deserialize_with = "extension_id::delegated_credentials")]
value: u16,
data: Vec<SignatureAlgorithm>,
},
EncryptedClientHello {
#[serde(deserialize_with = "extension_id::encrypted_client_hello")]
value: u16,
data: ECHClientHello,
},
SignedCertificateTimestamp {
#[serde(deserialize_with = "extension_id::signed_certificate_timestamp")]
value: u16,
#[serde(skip_serializing_if = "Option::is_none")]
data: Option<HexBytes>,
},
RenegotiationInfo {
#[serde(deserialize_with = "extension_id::renegotiation_info")]
value: u16,
},
ExtendedMasterSecret {
#[serde(deserialize_with = "extension_id::extended_master_secret")]
value: u16,
},
Padding {
#[serde(deserialize_with = "extension_id::padding")]
value: u16,
data: HexBytes,
},
KeyShare {
#[serde(deserialize_with = "extension_id::key_share")]
value: u16,
data: Vec<KeyShare>,
},
PskKeyExchangeModes {
#[serde(deserialize_with = "extension_id::psk_key_exchange_modes")]
value: u16,
data: PskKeyExchangeModes,
},
PreSharedKey {
#[serde(deserialize_with = "extension_id::pre_shared_key")]
value: u16,
data: HexBytes,
},
QuicTransportParameters {
#[serde(deserialize_with = "extension_id::quic_transport_parameters")]
value: u16,
#[serde(deserialize_with = "deserialize_quic_transport_parameters")]
data: Vec<QuicTransportParameter>,
},
EncryptedServerName {
#[serde(deserialize_with = "extension_id::encrypted_server_name")]
value: u16,
ciphersuite: Box<str>,
group: NamedGroup,
key_share: HexBytes,
record_digest: HexBytes,
encrypted_sni: HexBytes,
},
OidFilters {
#[serde(deserialize_with = "extension_id::oid_filters")]
value: u16,
data: Vec<OidFilter>,
},
Grease {
#[serde(deserialize_with = "extension_id::grease")]
value: u16,
},
Opaque {
#[serde(deserialize_with = "extension_id::opaque")]
value: u16,
data: Option<HexBytes>,
},
}
fn deserialize_quic_transport_parameters<'de, D>(
deserializer: D,
) -> Result<Vec<QuicTransportParameter>, D::Error>
where
D: Deserializer<'de>,
{
let parameters = Vec::<QuicTransportParameter>::deserialize(deserializer)?;
let mut parameter_ids = HashSet::with_capacity(parameters.len());
for parameter in ¶meters {
if !parameter_ids.insert(parameter.id) {
return Err(de::Error::custom(format_args!(
"duplicate QUIC transport parameter {}",
parameter.id
)));
}
}
Ok(parameters)
}
mod extension_id {
use serde::{de, Deserialize, Deserializer};
use tls_parser::TlsExtensionType;
use super::{is_grease_value, TRANSPORT_PARAMETERS_EXTENSION_ID};
const CERTIFICATE_COMPRESSION: u16 = 27;
const DELEGATED_CREDENTIALS: u16 = 34;
const APPLICATION_SETTINGS_OLD: u16 = 17_513;
const APPLICATION_SETTINGS: u16 = 17_613;
const ENCRYPTED_CLIENT_HELLO: u16 = 65_037;
const DEDICATED_TYPES: &[u16] = &[
TlsExtensionType::ServerName.0,
TlsExtensionType::StatusRequest.0,
TlsExtensionType::SupportedGroups.0,
TlsExtensionType::EcPointFormats.0,
TlsExtensionType::SignatureAlgorithms.0,
TlsExtensionType::ApplicationLayerProtocolNegotiation.0,
TlsExtensionType::SignedCertificateTimestamp.0,
TlsExtensionType::Padding.0,
TlsExtensionType::ExtendedMasterSecret.0,
CERTIFICATE_COMPRESSION,
TlsExtensionType::RecordSizeLimit.0,
DELEGATED_CREDENTIALS,
TlsExtensionType::SessionTicketTLS.0,
TlsExtensionType::PreSharedKey.0,
TRANSPORT_PARAMETERS_EXTENSION_ID,
TlsExtensionType::SupportedVersions.0,
TlsExtensionType::PskExchangeModes.0,
TlsExtensionType::OidFilters.0,
TlsExtensionType::KeyShare.0,
APPLICATION_SETTINGS_OLD,
APPLICATION_SETTINGS,
ENCRYPTED_CLIENT_HELLO,
TlsExtensionType::RenegotiationInfo.0,
TlsExtensionType::EncryptedServerName.0,
];
macro_rules! exact_id {
($name:ident, $expected:expr) => {
pub(super) fn $name<'de, D>(deserializer: D) -> Result<u16, D::Error>
where
D: Deserializer<'de>,
{
exact(deserializer, $expected)
}
};
}
exact_id!(server_name, TlsExtensionType::ServerName.0);
exact_id!(status_request, TlsExtensionType::StatusRequest.0);
exact_id!(supported_groups, TlsExtensionType::SupportedGroups.0);
exact_id!(ec_point_formats, TlsExtensionType::EcPointFormats.0);
exact_id!(
signature_algorithms,
TlsExtensionType::SignatureAlgorithms.0
);
exact_id!(
alpn,
TlsExtensionType::ApplicationLayerProtocolNegotiation.0
);
exact_id!(
signed_certificate_timestamp,
TlsExtensionType::SignedCertificateTimestamp.0
);
exact_id!(padding, TlsExtensionType::Padding.0);
exact_id!(
extended_master_secret,
TlsExtensionType::ExtendedMasterSecret.0
);
exact_id!(certificate_compression, CERTIFICATE_COMPRESSION);
exact_id!(record_size_limit, TlsExtensionType::RecordSizeLimit.0);
exact_id!(delegated_credentials, DELEGATED_CREDENTIALS);
exact_id!(session_ticket, TlsExtensionType::SessionTicketTLS.0);
exact_id!(pre_shared_key, TlsExtensionType::PreSharedKey.0);
exact_id!(quic_transport_parameters, TRANSPORT_PARAMETERS_EXTENSION_ID);
exact_id!(supported_versions, TlsExtensionType::SupportedVersions.0);
exact_id!(psk_key_exchange_modes, TlsExtensionType::PskExchangeModes.0);
exact_id!(oid_filters, TlsExtensionType::OidFilters.0);
exact_id!(key_share, TlsExtensionType::KeyShare.0);
exact_id!(application_settings_old, APPLICATION_SETTINGS_OLD);
exact_id!(application_settings, APPLICATION_SETTINGS);
exact_id!(encrypted_client_hello, ENCRYPTED_CLIENT_HELLO);
exact_id!(renegotiation_info, TlsExtensionType::RenegotiationInfo.0);
exact_id!(
encrypted_server_name,
TlsExtensionType::EncryptedServerName.0
);
fn exact<'de, D>(deserializer: D, expected: u16) -> Result<u16, D::Error>
where
D: Deserializer<'de>,
{
let value = u16::deserialize(deserializer)?;
if value != expected {
return Err(de::Error::custom(format_args!(
"TLS extension variant expects ID {expected}, got {value}"
)));
}
Ok(value)
}
pub(super) fn grease<'de, D>(deserializer: D) -> Result<u16, D::Error>
where
D: Deserializer<'de>,
{
let value = u16::deserialize(deserializer)?;
if !is_grease_value(value) {
return Err(de::Error::custom(format_args!(
"TLS GREASE extension has non-GREASE ID {value}"
)));
}
Ok(value)
}
pub(super) fn opaque<'de, D>(deserializer: D) -> Result<u16, D::Error>
where
D: Deserializer<'de>,
{
let value = u16::deserialize(deserializer)?;
if is_grease_value(value) || DEDICATED_TYPES.contains(&value) {
return Err(de::Error::custom(format_args!(
"TLS extension ID {value} has a dedicated variant"
)));
}
Ok(value)
}
}
impl TlsExtension {
pub fn value(&self) -> u16 {
match self {
TlsExtension::ServerName { value, .. }
| TlsExtension::SupportedGroups { value, .. }
| TlsExtension::EcPointFormats { value, .. }
| TlsExtension::SignatureAlgorithms { value, .. }
| TlsExtension::StatusRequest { value, .. }
| TlsExtension::ApplicationLayerProtocolNegotiation { value, .. }
| TlsExtension::ApplicationSettingsOld { value, .. }
| TlsExtension::ApplicationSettings { value, .. }
| TlsExtension::SupportedVersions { value, .. }
| TlsExtension::SessionTicket { value, .. }
| TlsExtension::CertificateCompression { value, .. }
| TlsExtension::RecordSizeLimit { value, .. }
| TlsExtension::DelegatedCredentials { value, .. }
| TlsExtension::EncryptedClientHello { value, .. }
| TlsExtension::SignedCertificateTimestamp { value, .. }
| TlsExtension::RenegotiationInfo { value }
| TlsExtension::ExtendedMasterSecret { value }
| TlsExtension::Padding { value, .. }
| TlsExtension::KeyShare { value, .. }
| TlsExtension::PskKeyExchangeModes { value, .. }
| TlsExtension::PreSharedKey { value, .. }
| TlsExtension::QuicTransportParameters { value, .. }
| TlsExtension::EncryptedServerName { value, .. }
| TlsExtension::OidFilters { value, .. }
| TlsExtension::Grease { value }
| TlsExtension::Opaque { value, .. } => *value,
}
}
pub fn is_grease(&self) -> bool {
is_grease_value(self.value())
}
}
#[derive(Debug, Clone, PartialEq, Eq, Serialize, Hash)]
pub struct StatusRequest {
pub certificate_status_type: CertificateStatusType,
pub responder_id_list: u16,
pub request_extensions: u16,
}
impl<'de> Deserialize<'de> for StatusRequest {
fn deserialize<D>(deserializer: D) -> Result<Self, D::Error>
where
D: Deserializer<'de>,
{
let repr = StatusRequestRepr::deserialize(deserializer)?;
if repr.certificate_status_type != CertificateStatusType::OCSP {
return Err(de::Error::custom(
"ClientHello status_request currently supports only OCSP",
));
}
if matches!(repr.responder_id_list, 1 | 2) {
return Err(de::Error::custom(
"an OCSP responder ID list must be empty or contain at least three bytes",
));
}
Ok(Self {
certificate_status_type: repr.certificate_status_type,
responder_id_list: repr.responder_id_list,
request_extensions: repr.request_extensions,
})
}
}
#[derive(Deserialize)]
struct StatusRequestRepr {
certificate_status_type: CertificateStatusType,
responder_id_list: u16,
request_extensions: u16,
}
#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize, Hash)]
pub enum ECHClientHello {
Outer(ECHClientHelloOuter),
Inner,
}
#[derive(Debug, Clone, PartialEq, Eq, Serialize, Hash)]
pub struct ECHClientHelloOuter {
pub cipher_suite: HpkeSymmetricCipherSuite,
pub config_id: u8,
pub enc: HexBytes,
pub payload: HexBytes,
pub payload_length: u16,
}
impl<'de> Deserialize<'de> for ECHClientHelloOuter {
fn deserialize<D>(deserializer: D) -> Result<Self, D::Error>
where
D: Deserializer<'de>,
{
let repr = ECHClientHelloOuterRepr::deserialize(deserializer)?;
if repr.enc.len() > usize::from(u16::MAX) {
return Err(de::Error::custom(format_args!(
"ECH encapsulated key exceeds 65535 bytes: {}",
repr.enc.len(),
)));
}
let payload_length = u16::try_from(repr.payload.len()).map_err(|_| {
de::Error::custom(format_args!(
"ECH payload exceeds 65535 bytes: {}",
repr.payload.len(),
))
})?;
if payload_length == 0 {
return Err(de::Error::custom("ECH outer payload must not be empty"));
}
if payload_length != repr.payload_length {
return Err(de::Error::custom(format_args!(
"ECH payload length is {}, but payload_length is {}",
payload_length, repr.payload_length,
)));
}
Ok(Self {
cipher_suite: repr.cipher_suite,
config_id: repr.config_id,
enc: repr.enc,
payload: repr.payload,
payload_length: repr.payload_length,
})
}
}
#[derive(Deserialize)]
struct ECHClientHelloOuterRepr {
cipher_suite: HpkeSymmetricCipherSuite,
config_id: u8,
enc: HexBytes,
payload: HexBytes,
payload_length: u16,
}
#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize, Hash)]
pub struct HpkeSymmetricCipherSuite {
pub kdf_id: KeyDerivationFunction,
pub aead_id: AuthenticatedEncryptionWithAssociatedData,
}
#[derive(Debug, Clone, PartialEq, Eq, Serialize, Hash)]
pub struct KeyShare {
pub id: u16,
pub name: Box<str>,
#[serde(rename = "value", skip_serializing_if = "Option::is_none")]
pub key_exchange: Option<HexBytes>,
}
impl KeyShare {
fn from_wire(id: u16, key_exchange: Vec<u8>) -> Self {
let group = NamedGroup::from(id);
let key_exchange = (!group.is_grease()).then(|| HexBytes::from(key_exchange));
Self {
id: group.id,
name: group.name,
key_exchange,
}
}
}
impl<'de> Deserialize<'de> for KeyShare {
fn deserialize<D>(deserializer: D) -> Result<Self, D::Error>
where
D: Deserializer<'de>,
{
let repr = KeyShareRepr::deserialize(deserializer)?;
let id = repr.id;
let group = NamedGroup::from_serialized_parts(id, repr.name).map_err(|expected| {
de::Error::custom(format_args!(
"TLS key share group {id:#06x} has name {expected:?}, not the saved name",
))
})?;
match (group.is_grease(), repr.key_exchange) {
(true, Some(_)) => Err(de::Error::custom(
"GREASE key shares must omit key exchange bytes",
)),
(false, None) => Err(de::Error::custom(
"non-GREASE key shares require key exchange bytes",
)),
(false, Some(key_exchange))
if key_exchange.is_empty() || key_exchange.len() > usize::from(u16::MAX) =>
{
Err(de::Error::custom(
"non-GREASE key shares require 1 to 65535 key exchange bytes",
))
}
(_, key_exchange) => Ok(Self {
id: group.id,
name: group.name,
key_exchange,
}),
}
}
}
#[derive(Deserialize)]
struct KeyShareRepr {
id: u16,
name: Box<str>,
#[serde(rename = "value")]
key_exchange: Option<HexBytes>,
}
#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize, Hash)]
pub struct PskKeyExchangeModes {
pub ke_modes: Vec<PskKeyExchangeMode>,
}
#[derive(Debug, Clone, PartialEq, Eq, Serialize, Hash)]
pub struct OidFilter {
pub cert_ext_oid: HexBytes,
pub cert_ext_val: HexBytes,
}
impl<'de> Deserialize<'de> for OidFilter {
fn deserialize<D>(deserializer: D) -> Result<Self, D::Error>
where
D: Deserializer<'de>,
{
let repr = OidFilterRepr::deserialize(deserializer)?;
if repr.cert_ext_oid.is_empty() || repr.cert_ext_oid.len() > usize::from(u8::MAX) {
return Err(de::Error::custom(format_args!(
"certificate extension OID must contain 1 to 255 bytes, got {}",
repr.cert_ext_oid.len(),
)));
}
if repr.cert_ext_val.len() > usize::from(u16::MAX) {
return Err(de::Error::custom(format_args!(
"certificate extension value exceeds 65535 bytes: {}",
repr.cert_ext_val.len(),
)));
}
Ok(Self {
cert_ext_oid: repr.cert_ext_oid,
cert_ext_val: repr.cert_ext_val,
})
}
}
#[derive(Deserialize)]
struct OidFilterRepr {
cert_ext_oid: HexBytes,
cert_ext_val: HexBytes,
}
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
#[non_exhaustive]
pub enum ClientHelloParseStage {
TlsRecord,
RecordMessages,
ClientHello,
Extensions,
StatusRequest,
ApplicationLayerProtocolNegotiation,
ApplicationSettings,
DelegatedCredentials,
CertificateCompression,
EncryptedClientHello,
KeyShare,
QuicTransportParameters,
}
#[derive(Debug, PartialEq, Eq, thiserror::Error)]
#[error("failed to parse TLS ClientHello at {stage:?}")]
pub struct ClientHelloParseError {
pub stage: ClientHelloParseStage,
pub extension_id: Option<u16>,
pub payload_len: Option<usize>,
}
impl ClientHelloParseError {
const fn new(stage: ClientHelloParseStage) -> Self {
Self {
stage,
extension_id: None,
payload_len: None,
}
}
const fn extension(
stage: ClientHelloParseStage,
extension_id: u16,
payload_len: usize,
) -> Self {
Self {
stage,
extension_id: Some(extension_id),
payload_len: Some(payload_len),
}
}
}
impl ClientHello {
pub fn set_tls_version_negotiated(&mut self, version: Option<TlsVersion>) {
self.tls_version_negotiated = version;
}
pub fn parse(buf: &[u8]) -> Result<Self, ClientHelloParseError> {
match Self::parse_inner(buf) {
Ok(client_hello) => {
tracing::debug!(
capture_len = buf.len(),
legacy_version = %client_hello.tls_version,
cipher_count = client_hello.cipher_suites.len(),
extension_count = client_hello.extensions.len(),
"parsed TLS ClientHello",
);
Ok(client_hello)
}
Err(error) => {
tracing::debug!(
capture_len = buf.len(),
stage = ?error.stage,
extension_id = ?error.extension_id,
payload_len = ?error.payload_len,
"failed to parse TLS ClientHello",
);
Err(error)
}
}
}
fn parse_inner(buf: &[u8]) -> Result<Self, ClientHelloParseError> {
let captured = capture_client_hello(buf)?
.ok_or(ClientHelloParseError::new(ClientHelloParseStage::TlsRecord))?;
Self::parse_handshake(captured.handshake.as_ref())
}
pub fn parse_handshake(handshake: &[u8]) -> Result<Self, ClientHelloParseError> {
let Some(expected_len) = client_hello_handshake_len(handshake)? else {
return Err(ClientHelloParseError::new(
ClientHelloParseStage::RecordMessages,
));
};
if handshake.len() != expected_len {
return Err(ClientHelloParseError::new(
ClientHelloParseStage::RecordMessages,
));
}
let body = &handshake[TLS_HANDSHAKE_HEADER_LEN..];
let (remaining, message) = tls_parser::parse_tls_handshake_msg_client_hello(body)
.map_err(|_| ClientHelloParseError::new(ClientHelloParseStage::RecordMessages))?;
if !remaining.is_empty() {
return Err(ClientHelloParseError::new(
ClientHelloParseStage::RecordMessages,
));
}
let TlsMessageHandshake::ClientHello(payload) = message else {
return Err(ClientHelloParseError::new(
ClientHelloParseStage::ClientHello,
));
};
let cipher_suites = payload
.ciphers
.iter()
.map(|cipher| TlsCipherSuite::from(cipher.0))
.collect();
let mut client_hello = ClientHello {
tls_version: TlsVersion::from(payload.version.0),
tls_version_negotiated: None,
cipher_suites,
client_random: HexBytes::from(payload.random),
session_id: payload.session_id.map(HexBytes::from),
compression_algorithms: payload
.comp
.iter()
.map(|c| CompressionAlgorithm::from(c.0))
.collect(),
extensions: Vec::with_capacity(5),
};
let Some(extension_bytes) = payload.ext else {
return Ok(client_hello);
};
let (remaining, ext_list) = tls_parser::parse_tls_client_hello_extensions(extension_bytes)
.map_err(|_| ClientHelloParseError::new(ClientHelloParseStage::Extensions))?;
if !remaining.is_empty() {
return Err(ClientHelloParseError::new(
ClientHelloParseStage::Extensions,
));
}
let mut raw_extensions = extension_bytes;
for ext in ext_list {
let (remaining, (extension_id, extension_payload)) =
parser::parse_tls_extension_frame(raw_extensions)
.map_err(|_| ClientHelloParseError::new(ClientHelloParseStage::Extensions))?;
raw_extensions = remaining;
tracing::trace!(extension_id, "decoding TLS ClientHello extension");
match ext {
tls_parser::TlsExtension::SNI(name) => {
client_hello.extensions.push(TlsExtension::ServerName {
value: extension_id,
data: name
.into_iter()
.map(|n| n.1)
.map(|n| String::from_utf8_lossy(n).into_owned().into_boxed_str())
.collect(),
});
}
tls_parser::TlsExtension::EllipticCurves(groups) => {
client_hello.extensions.push(TlsExtension::SupportedGroups {
value: extension_id,
data: groups
.into_iter()
.map(|group| NamedGroup::from(group.0))
.collect(),
});
}
tls_parser::TlsExtension::SupportedVersions(versions) => {
client_hello
.extensions
.push(TlsExtension::SupportedVersions {
value: extension_id,
data: SupportedVersions::from_ids(
versions.into_iter().map(|version| version.0),
),
});
}
tls_parser::TlsExtension::SessionTicket(data) => {
client_hello.extensions.push(TlsExtension::SessionTicket {
value: extension_id,
data: HexBytes::from(data),
});
}
tls_parser::TlsExtension::SignatureAlgorithms(algorithms) => {
client_hello
.extensions
.push(TlsExtension::SignatureAlgorithms {
value: extension_id,
data: algorithms
.into_iter()
.map(SignatureAlgorithm::from)
.collect(),
});
}
tls_parser::TlsExtension::StatusRequest(data) => {
let Some((status, data)) = data else {
return Err(ClientHelloParseError::extension(
ClientHelloParseStage::StatusRequest,
extension_id,
extension_payload.len(),
));
};
let certificate_status_type = CertificateStatusType::from(status.0);
if certificate_status_type != CertificateStatusType::OCSP {
return Err(ClientHelloParseError::extension(
ClientHelloParseStage::StatusRequest,
extension_id,
extension_payload.len(),
));
}
let (_, (responder_id_list, request_extensions)) =
parser::parse_ocsp_status_request_lengths(data).map_err(|_| {
ClientHelloParseError::extension(
ClientHelloParseStage::StatusRequest,
extension_id,
extension_payload.len(),
)
})?;
client_hello.extensions.push(TlsExtension::StatusRequest {
value: extension_id,
data: StatusRequest {
certificate_status_type,
responder_id_list,
request_extensions,
},
});
}
tls_parser::TlsExtension::EcPointFormats(formats) => {
client_hello.extensions.push(TlsExtension::EcPointFormats {
value: extension_id,
data: formats.iter().map(|f| ECPointFormat::from(*f)).collect(),
});
}
tls_parser::TlsExtension::ALPN(_) => {
let (_, protocols) =
parser::parse_alpn_packet(extension_payload).map_err(|_| {
ClientHelloParseError::extension(
ClientHelloParseStage::ApplicationLayerProtocolNegotiation,
extension_id,
extension_payload.len(),
)
})?;
client_hello.extensions.push(
TlsExtension::ApplicationLayerProtocolNegotiation {
value: extension_id,
data: protocols,
},
);
}
tls_parser::TlsExtension::SignedCertificateTimestamp(timestamps) => {
client_hello
.extensions
.push(TlsExtension::SignedCertificateTimestamp {
value: extension_id,
data: timestamps.map(HexBytes::from),
});
}
tls_parser::TlsExtension::RenegotiationInfo(_) => {
client_hello
.extensions
.push(TlsExtension::RenegotiationInfo {
value: extension_id,
});
}
tls_parser::TlsExtension::Unknown(TlsExtensionType(34), algorithms) => {
let extension =
parser::parse_tls_extension_delegated_credentials(extension_id, algorithms)
.map_err(|_| {
ClientHelloParseError::extension(
ClientHelloParseStage::DelegatedCredentials,
extension_id,
algorithms.len(),
)
})?
.1;
client_hello.extensions.push(extension);
}
tls_parser::TlsExtension::RecordSizeLimit(limit) => {
client_hello.extensions.push(TlsExtension::RecordSizeLimit {
value: extension_id,
data: limit,
});
}
tls_parser::TlsExtension::Unknown(TlsExtensionType(27), data) => {
let extension =
parser::parse_tls_extension_certificate_compression(extension_id, data)
.map_err(|_| {
ClientHelloParseError::extension(
ClientHelloParseStage::CertificateCompression,
extension_id,
data.len(),
)
})?
.1;
client_hello.extensions.push(extension);
}
tls_parser::TlsExtension::Unknown(TlsExtensionType(65037), data) => {
let extension = parser::parse_tls_extension_ech(extension_id, data)
.map_err(|_| {
ClientHelloParseError::extension(
ClientHelloParseStage::EncryptedClientHello,
extension_id,
data.len(),
)
})?
.1;
client_hello.extensions.push(extension);
}
tls_parser::TlsExtension::Padding(padding) => {
client_hello.extensions.push(TlsExtension::Padding {
value: extension_id,
data: HexBytes::from(padding),
});
}
tls_parser::TlsExtension::KeyShare(data) => {
client_hello.extensions.push(TlsExtension::KeyShare {
value: extension_id,
data: parser::parse_key_share(data)
.ok_or(ClientHelloParseError::extension(
ClientHelloParseStage::KeyShare,
extension_id,
data.len(),
))?
.into_iter()
.map(|(group, key_exchange)| KeyShare::from_wire(group, key_exchange))
.collect(),
});
}
tls_parser::TlsExtension::PskExchangeModes(data) => {
client_hello
.extensions
.push(TlsExtension::PskKeyExchangeModes {
value: extension_id,
data: PskKeyExchangeModes {
ke_modes: data.into_iter().map(PskKeyExchangeMode::from).collect(),
},
});
}
tls_parser::TlsExtension::PreSharedKey(data) => {
client_hello.extensions.push(TlsExtension::PreSharedKey {
value: extension_id,
data: HexBytes::from(data),
});
}
tls_parser::TlsExtension::Unknown(extension_type, data)
if extension_type.0 == TRANSPORT_PARAMETERS_EXTENSION_ID =>
{
let parameters = parse_transport_parameters(data).map_err(|_| {
ClientHelloParseError::extension(
ClientHelloParseStage::QuicTransportParameters,
extension_id,
data.len(),
)
})?;
client_hello
.extensions
.push(TlsExtension::QuicTransportParameters {
value: extension_id,
data: parameters,
});
}
tls_parser::TlsExtension::Unknown(TlsExtensionType(17513), protocols) => {
let (_, protocols) = parser::parse_alps_packet(protocols).map_err(|_| {
ClientHelloParseError::extension(
ClientHelloParseStage::ApplicationSettings,
extension_id,
protocols.len(),
)
})?;
client_hello
.extensions
.push(TlsExtension::ApplicationSettingsOld {
value: extension_id,
data: protocols,
});
}
tls_parser::TlsExtension::Unknown(TlsExtensionType(17613), protocols) => {
let (_, protocols) = parser::parse_alps_packet(protocols).map_err(|_| {
ClientHelloParseError::extension(
ClientHelloParseStage::ApplicationSettings,
extension_id,
protocols.len(),
)
})?;
client_hello
.extensions
.push(TlsExtension::ApplicationSettings {
value: extension_id,
data: protocols,
});
}
tls_parser::TlsExtension::ExtendedMasterSecret => {
client_hello
.extensions
.push(TlsExtension::ExtendedMasterSecret {
value: extension_id,
});
}
tls_parser::TlsExtension::Grease(..) => {
client_hello.extensions.push(TlsExtension::Grease {
value: extension_id,
});
}
tls_parser::TlsExtension::MaxFragmentLength(data) => {
client_hello.extensions.push(TlsExtension::Opaque {
value: extension_id,
data: Some(HexBytes::from(data.to_be_bytes())),
});
}
tls_parser::TlsExtension::KeyShareOld(items) => {
client_hello.extensions.push(TlsExtension::Opaque {
value: extension_id,
data: Some(HexBytes::from(items)),
});
}
tls_parser::TlsExtension::EarlyData(data) => {
client_hello.extensions.push(TlsExtension::Opaque {
value: extension_id,
data: data.map(|value| HexBytes::from(value.to_be_bytes())),
});
}
tls_parser::TlsExtension::Cookie(items) => {
client_hello.extensions.push(TlsExtension::Opaque {
value: extension_id,
data: Some(HexBytes::from(items)),
});
}
tls_parser::TlsExtension::Heartbeat(data) => {
client_hello.extensions.push(TlsExtension::Opaque {
value: extension_id,
data: Some(HexBytes::from(data.to_be_bytes())),
});
}
tls_parser::TlsExtension::EncryptThenMac => {
client_hello.extensions.push(TlsExtension::Opaque {
value: extension_id,
data: None,
});
}
tls_parser::TlsExtension::OidFilters(oid_filters) => {
client_hello.extensions.push(TlsExtension::OidFilters {
value: extension_id,
data: oid_filters
.into_iter()
.map(|f| OidFilter {
cert_ext_oid: HexBytes::from(f.cert_ext_oid),
cert_ext_val: HexBytes::from(f.cert_ext_val),
})
.collect(),
});
}
tls_parser::TlsExtension::PostHandshakeAuth => {
client_hello.extensions.push(TlsExtension::Opaque {
value: extension_id,
data: None,
});
}
tls_parser::TlsExtension::NextProtocolNegotiation => {
client_hello.extensions.push(TlsExtension::Opaque {
value: extension_id,
data: None,
});
}
tls_parser::TlsExtension::EncryptedServerName {
ciphersuite,
group,
key_share,
record_digest,
encrypted_sni,
} => {
client_hello
.extensions
.push(TlsExtension::EncryptedServerName {
value: extension_id,
ciphersuite: tls_parser::TlsCipherSuite::from_id(ciphersuite.0)
.map(|c| c.name)
.unwrap_or("Unknown")
.into(),
group: NamedGroup::from(group.0),
key_share: HexBytes::from(key_share),
record_digest: HexBytes::from(record_digest),
encrypted_sni: HexBytes::from(encrypted_sni),
});
}
tls_parser::TlsExtension::Unknown(_, data) => {
client_hello.extensions.push(TlsExtension::Opaque {
value: extension_id,
data: Some(HexBytes::from(data)),
});
}
}
}
if !raw_extensions.is_empty() {
return Err(ClientHelloParseError::new(
ClientHelloParseStage::Extensions,
));
}
Ok(client_hello)
}
pub fn quic_transport_parameters(&self) -> Option<&[QuicTransportParameter]> {
self.extensions
.iter()
.find_map(|extension| match extension {
TlsExtension::QuicTransportParameters { data, .. } => Some(data.as_slice()),
_ => None,
})
}
pub fn ja4(&self) -> Ja4Fingerprint {
Ja4Fingerprint::from(self)
}
pub fn ja3(&self) -> Ja3Fingerprint {
Ja3Fingerprint::from(self)
}
}
#[cfg(test)]
mod tests {
use super::{
ClientHello, ClientHelloBuffer, ClientHelloHandshakeBuffer, ClientHelloParseStage,
ECHClientHelloOuter, HexBytes, KeyShare, ProtocolName, StatusRequest, TlsCipherSuite,
TlsExtension,
};
use crate::tls::{CompressionAlgorithm, NamedGroup, SupportedVersions, TlsVersion};
fn tls_record(payload: &[u8]) -> Vec<u8> {
let length = u16::try_from(payload.len()).unwrap();
let mut record = vec![0x16, 0x03, 0x03];
record.extend_from_slice(&length.to_be_bytes());
record.extend_from_slice(payload);
record
}
fn client_hello_handshake(extensions: Option<&[u8]>) -> Vec<u8> {
let mut body = vec![0x03, 0x03];
body.extend_from_slice(&[0; 32]);
body.push(0);
body.extend_from_slice(&[0, 2, 0x13, 0x01]);
body.extend_from_slice(&[1, 0]);
if let Some(extensions) = extensions {
let length = u16::try_from(extensions.len()).unwrap();
body.extend_from_slice(&length.to_be_bytes());
body.extend_from_slice(extensions);
}
let length = u32::try_from(body.len()).unwrap().to_be_bytes();
let mut handshake = vec![1, length[1], length[2], length[3]];
handshake.extend_from_slice(&body);
handshake
}
#[test]
fn tls_cipher_suite_resolves_and_validates_ids_and_names() {
let known = TlsCipherSuite::from_id(0x1301);
assert_eq!(known.id, 0x1301);
assert_eq!(known.name.as_ref(), "TLS_AES_128_GCM_SHA256");
assert!(!known.is_grease());
assert_eq!(
TlsCipherSuite::from_name("TLS_AES_128_GCM_SHA256"),
Some(known)
);
assert!(TlsCipherSuite::from_name("GREASE").is_none());
let grease = TlsCipherSuite::from_id(0x0a0a);
assert_eq!(grease.name.as_ref(), "GREASE");
assert!(grease.is_grease());
let unknown = TlsCipherSuite::from_id(0xffff);
assert_eq!(unknown.name.as_ref(), "Unknown");
assert!(!unknown.is_grease());
let error = serde_json::from_value::<TlsCipherSuite>(serde_json::json!({
"id": 0x1301,
"name": "TLS_AES_256_GCM_SHA384"
}))
.expect_err("mismatched cipher-suite name must fail");
assert!(
error
.to_string()
.contains("has name \"TLS_AES_128_GCM_SHA256\""),
"{error}"
);
}
#[test]
fn hex_bytes_serialize_canonically_and_reject_invalid_text() {
let bytes = HexBytes::from([0x00, 0xab, 0xff]);
let json = serde_json::to_value(&bytes).expect("hex bytes serialize");
assert_eq!(json, serde_json::json!("00abff"));
assert_eq!(
serde_json::from_value::<HexBytes>(json).expect("hex bytes deserialize"),
bytes
);
for invalid in ["0", "00AB", "00gg"] {
assert!(serde_json::from_value::<HexBytes>(serde_json::json!(invalid)).is_err());
}
}
#[test]
fn protocol_names_roundtrip_text_and_opaque_bytes() {
let h2 = ProtocolName::try_from("h2").unwrap();
let h2_json = serde_json::to_value(&h2).expect("UTF-8 protocol name serializes");
assert_eq!(h2_json, serde_json::json!("h2"));
assert_eq!(serde_json::from_value::<ProtocolName>(h2_json).unwrap(), h2);
let opaque = ProtocolName::try_from(&[0xff, b'x'][..]).unwrap();
let opaque_json = serde_json::to_value(&opaque).expect("opaque protocol name serializes");
assert_eq!(opaque_json, serde_json::json!({"hex": "ff78"}));
assert_eq!(
serde_json::from_value::<ProtocolName>(opaque_json).unwrap(),
opaque
);
for invalid in [
serde_json::json!(""),
serde_json::json!({"hex": ""}),
serde_json::json!({"hex": "FF"}),
serde_json::json!("a".repeat(256)),
] {
assert!(serde_json::from_value::<ProtocolName>(invalid).is_err());
}
}
#[test]
fn ech_json_validates_payload_length() {
let valid = serde_json::json!({
"cipher_suite": {
"kdf_id": "HKDF_SHA256",
"aead_id": "AES_128_GCM"
},
"config_id": 7,
"enc": "",
"payload": "aa",
"payload_length": 1
});
let outer = serde_json::from_value::<ECHClientHelloOuter>(valid.clone())
.expect("valid ECH outer deserializes");
assert_eq!(outer.payload.as_bytes(), [0xaa]);
let mut mismatch = valid.clone();
mismatch["payload_length"] = serde_json::json!(2);
assert!(serde_json::from_value::<ECHClientHelloOuter>(mismatch).is_err());
let mut empty = valid;
empty["payload"] = serde_json::json!("");
empty["payload_length"] = serde_json::json!(0);
assert!(serde_json::from_value::<ECHClientHelloOuter>(empty).is_err());
}
#[test]
fn key_shares_serialize_id_name_and_optional_exchange_bytes() {
let grease = KeyShare::from_wire(0x3a3a, vec![0x00]);
assert_eq!(grease.id, 0x3a3a);
assert_eq!(grease.name.as_ref(), "GREASE");
assert_eq!(grease.key_exchange, None);
let grease_json = serde_json::to_value(&grease).expect("GREASE KeyShare serializes");
assert_eq!(
grease_json,
serde_json::json!({
"id": 0x3a3a,
"name": "GREASE"
})
);
let x25519 = KeyShare::from_wire(29, vec![0x01, 0x02]);
let x25519_json = serde_json::to_value(&x25519).expect("x25519 KeyShare serializes");
assert_eq!(
x25519_json,
serde_json::json!({
"id": 29,
"name": "x25519",
"value": "0102"
})
);
assert_eq!(
serde_json::from_value::<KeyShare>(grease_json).expect("GREASE KeyShare deserializes"),
grease
);
assert_eq!(
serde_json::from_value::<KeyShare>(x25519_json).expect("x25519 KeyShare deserializes"),
x25519
);
assert!(serde_json::from_value::<KeyShare>(serde_json::json!({
"id": 29,
"name": "x25519"
}))
.is_err());
assert!(serde_json::from_value::<KeyShare>(serde_json::json!({
"id": 29,
"name": "x25519",
"value": ""
}))
.is_err());
assert!(serde_json::from_value::<KeyShare>(serde_json::json!({
"id": 0x3a3a,
"name": "GREASE",
"value": "00"
}))
.is_err());
}
#[test]
fn malformed_client_hello_reports_parse_stages() {
let Err(record_error) = ClientHello::parse(&[]) else {
panic!("expected malformed TLS record to fail");
};
assert_eq!(record_error.stage, ClientHelloParseStage::TlsRecord);
assert_eq!(record_error.extension_id, None);
assert_eq!(record_error.payload_len, None);
let Err(message_error) = ClientHello::parse(&[0; 5]) else {
panic!("expected malformed TLS messages to fail");
};
assert_eq!(message_error.stage, ClientHelloParseStage::RecordMessages);
assert_eq!(message_error.extension_id, None);
assert_eq!(message_error.payload_len, None);
}
#[test]
fn client_hello_buffer_uses_requested_initial_capacity() {
let mut buffer = ClientHelloBuffer::with_capacity(64);
assert!(buffer.is_empty());
assert!(buffer.buf.capacity() >= 64);
assert_eq!(buffer.extend(&[1, 2, 3]), 3);
assert_eq!(buffer.as_bytes(), [1, 2, 3]);
}
#[test]
fn client_hello_buffer_distinguishes_incomplete_and_malformed_records() {
let mut buffer = ClientHelloBuffer::from(vec![0; 4]);
assert!(buffer.try_parse().unwrap().is_none());
buffer.extend(&[0]);
let error = buffer.try_parse().unwrap_err();
assert_eq!(error.stage, ClientHelloParseStage::RecordMessages);
assert!(buffer.is_invalid());
assert_eq!(buffer.extend(&[1, 2, 3]), 0);
assert_eq!(buffer.as_bytes(), &[0; 5]);
let borrowed = ClientHelloBuffer::from(&[1, 2, 3][..]);
assert_eq!(borrowed.as_bytes(), [1, 2, 3]);
}
#[test]
fn client_hello_buffer_reassembles_records_without_an_extension_block() {
let handshake = client_hello_handshake(None);
let first = tls_record(&handshake[..13]);
let second = tls_record(&handshake[13..]);
let extra = [0x17, 0x03, 0x03, 0, 1, 0];
let mut buffer = ClientHelloBuffer::new();
assert_eq!(buffer.extend(&first), first.len());
assert!(!buffer.is_complete());
assert!(buffer.try_parse().unwrap().is_none());
let mut tail = second.clone();
tail.extend_from_slice(&extra);
assert_eq!(buffer.extend(&tail), second.len());
assert!(buffer.is_complete());
assert_eq!(buffer.len(), first.len() + second.len());
assert_eq!(buffer.extend(&extra), 0);
let client_hello = buffer.try_parse().unwrap().unwrap();
assert_eq!(client_hello.cipher_suites[0].id, 0x1301);
assert!(client_hello.extensions.is_empty());
assert_eq!(buffer.parse().unwrap(), client_hello);
}
#[test]
fn client_hello_buffer_from_bytes_discards_following_records() {
let expected = tls_record(&client_hello_handshake(None));
let mut capture = expected.clone();
capture.extend_from_slice(&[0x17, 0x03, 0x03, 0, 1, 0]);
let buffer = ClientHelloBuffer::from(capture);
assert!(buffer.is_complete());
assert_eq!(buffer.as_bytes(), expected);
}
#[test]
fn handshake_buffer_reassembles_quic_crypto_bytes_and_discards_tail() {
let handshake = client_hello_handshake(None);
let mut buffer = ClientHelloHandshakeBuffer::with_capacity(32);
for chunk in handshake.chunks(3) {
buffer.extend(chunk);
}
buffer.extend(&[0x14, 0, 0, 0]);
assert!(buffer.is_complete());
assert!(!buffer.is_invalid());
assert_eq!(buffer.as_bytes(), handshake);
assert_eq!(buffer.extend(&[0]), 0);
let client_hello = buffer.try_parse().unwrap().unwrap();
assert_eq!(client_hello.cipher_suites[0].id, 0x1301);
let malformed = ClientHelloHandshakeBuffer::from(&[2, 0, 0, 0][..]);
assert!(malformed.is_invalid());
assert!(malformed.parse().is_err());
}
#[test]
fn parse_handshake_rejects_an_incomplete_header() {
let error = ClientHello::parse_handshake(&[1, 0, 0]).unwrap_err();
assert_eq!(error.stage, ClientHelloParseStage::RecordMessages);
}
#[test]
fn parse_handshake_rejects_a_non_client_hello_message() {
let mut handshake = client_hello_handshake(None);
handshake[0] = 2;
let error = ClientHello::parse_handshake(&handshake).unwrap_err();
assert_eq!(error.stage, ClientHelloParseStage::ClientHello);
}
#[test]
fn parse_handshake_rejects_declared_length_mismatches() {
let handshake = client_hello_handshake(None);
let payload_len = handshake.len() - 4;
for declared_len in [payload_len - 1, payload_len + 1] {
let mut malformed = handshake.clone();
let encoded = u32::try_from(declared_len).unwrap().to_be_bytes();
malformed[1..4].copy_from_slice(&encoded[1..]);
let error = ClientHello::parse_handshake(&malformed).unwrap_err();
assert_eq!(error.stage, ClientHelloParseStage::RecordMessages);
}
}
#[test]
fn parse_handshake_rejects_trailing_bytes() {
let mut handshake = client_hello_handshake(None);
handshake.push(0);
let error = ClientHello::parse_handshake(&handshake).unwrap_err();
assert_eq!(error.stage, ClientHelloParseStage::RecordMessages);
}
#[test]
fn client_hello_buffer_tracks_fragmented_records_incrementally() {
let handshake = client_hello_handshake(None);
let records = handshake.chunks(1).flat_map(tls_record).collect::<Vec<_>>();
let mut buffer = ClientHelloBuffer::new();
for (index, byte) in records.chunks(1).enumerate() {
assert_eq!(buffer.extend(byte), 1);
if index + 1 < records.len() {
assert!(buffer.try_parse().unwrap().is_none());
}
}
assert!(buffer.is_complete());
assert!(!buffer.is_invalid());
assert_eq!(buffer.capture.record_offset, records.len());
let client_hello = buffer.try_parse().unwrap().unwrap();
assert_eq!(client_hello.cipher_suites[0].id, 0x1301);
}
#[test]
fn client_hello_rejects_an_unconsumed_extension_tail() {
let handshake = client_hello_handshake(Some(&[0]));
let error = ClientHello::parse(&tls_record(&handshake)).unwrap_err();
assert_eq!(error.stage, ClientHelloParseStage::Extensions);
}
#[test]
fn client_hello_reports_strict_extension_parse_stages() {
let cases = [
(
&[0x00, 0x05, 0x00, 0x00][..],
ClientHelloParseStage::StatusRequest,
5,
0,
),
(
&[0x00, 0x05, 0x00, 0x01, 0x02][..],
ClientHelloParseStage::StatusRequest,
5,
1,
),
(
&[0x00, 0x10, 0x00, 0x03, 0x00, 0x01, 0x00][..],
ClientHelloParseStage::ApplicationLayerProtocolNegotiation,
16,
3,
),
(
&[0x00, 0x10, 0x00, 0x06, 0x00, 0x03, 0x02, b'h', b'2', 0x00][..],
ClientHelloParseStage::ApplicationLayerProtocolNegotiation,
16,
6,
),
(
&[0x44, 0xcd, 0x00, 0x02, 0x00, 0x00][..],
ClientHelloParseStage::ApplicationSettings,
17_613,
2,
),
(
&[0x00, 0x39, 0x00, 0x03, 0x04, 0x02, 0x20][..],
ClientHelloParseStage::QuicTransportParameters,
57,
3,
),
];
for (extension, stage, extension_id, payload_len) in cases {
let handshake = client_hello_handshake(Some(extension));
let error = ClientHello::parse(&tls_record(&handshake)).unwrap_err();
assert_eq!(error.stage, stage);
assert_eq!(error.extension_id, Some(extension_id));
assert_eq!(error.payload_len, Some(payload_len));
}
}
#[test]
fn status_request_deserialization_rejects_non_ocsp_metadata() {
let valid = serde_json::json!({"certificate_status_type": "OCSP", "responder_id_list": 0, "request_extensions": 0});
assert!(serde_json::from_value::<StatusRequest>(valid.clone()).is_ok());
let mut unknown_type = valid.clone();
unknown_type["certificate_status_type"] = serde_json::json!("Unknown (0x0002)");
assert!(serde_json::from_value::<StatusRequest>(unknown_type).is_err());
for responder_id_list in [1, 2] {
let mut invalid_length = valid.clone();
invalid_length["responder_id_list"] = serde_json::json!(responder_id_list);
assert!(serde_json::from_value::<StatusRequest>(invalid_length).is_err());
}
}
#[test]
fn client_hello_buffer_enforces_the_capture_limit() {
let mut buffer = ClientHelloBuffer::with_capture_limit(32);
let input = vec![0; 48];
assert_eq!(buffer.extend(&input), 32);
assert_eq!(buffer.len(), 32);
assert_eq!(buffer.capture_limit(), 32);
assert!(buffer.is_full());
assert_eq!(buffer.extend(&[1, 2, 3]), 0);
let oversized = ClientHelloBuffer::from(&[0x16, 0x03, 0x03, 0x41, 0x01][..]);
let error = oversized.try_parse().unwrap_err();
assert_eq!(error.stage, ClientHelloParseStage::TlsRecord);
}
#[test]
fn client_hello_json_roundtrip_preserves_fingerprints() {
let client_hello = ClientHello {
tls_version: TlsVersion::TLSv1_2,
tls_version_negotiated: Some(TlsVersion::TLSv1_3),
cipher_suites: vec![TlsCipherSuite::from(0x0a0a), TlsCipherSuite::from(0x1301)],
client_random: HexBytes::from([0; 32]),
session_id: None,
compression_algorithms: vec![CompressionAlgorithm::Null],
extensions: vec![
TlsExtension::SupportedVersions {
value: 0x002b,
data: SupportedVersions::from_ids([0x2a2a, 0x0304]),
},
TlsExtension::SupportedGroups {
value: 0x000a,
data: vec![NamedGroup::from(29), NamedGroup::from(0x4a4a)],
},
],
};
let ja3 = client_hello.ja3();
let ja4 = client_hello.ja4();
let json = serde_json::to_value(&client_hello).expect("ClientHello serializes");
assert_eq!(
json["cipher_suites"],
serde_json::json!([
{
"id": 2570,
"name": "GREASE"
},
{
"id": 4865,
"name": "TLS_AES_128_GCM_SHA256"
}
])
);
assert_eq!(
json["extensions"][0]["supported_versions"]["data"],
serde_json::json!({
"versions": [
{
"id": 10794,
"name": "GREASE"
},
{
"id": 772,
"name": "TLSv1_3"
}
]
})
);
assert!(json.get("cipher_values").is_none());
assert!(json.get("ciphers").is_none());
let mut invalid_random = json.clone();
invalid_random["client_random"] = serde_json::json!("00");
assert!(serde_json::from_value::<ClientHello>(invalid_random).is_err());
let mut empty_session_id = json.clone();
empty_session_id["session_id"] = serde_json::json!("");
assert!(serde_json::from_value::<ClientHello>(empty_session_id).is_err());
let mut oversized_session_id = json.clone();
oversized_session_id["session_id"] = serde_json::json!("00".repeat(33));
assert!(serde_json::from_value::<ClientHello>(oversized_session_id).is_err());
let mut legacy_json = json.clone();
let legacy_object = legacy_json
.as_object_mut()
.expect("ClientHello serializes as an object");
let cipher_suites = legacy_object
.remove("cipher_suites")
.expect("cipher_suites is present");
legacy_object.insert("ciphers".to_owned(), cipher_suites);
assert!(serde_json::from_value::<ClientHello>(legacy_json).is_err());
let restored: ClientHello =
serde_json::from_value(json.clone()).expect("ClientHello deserializes");
assert_eq!(restored, client_hello);
assert_eq!(restored.ja3(), ja3);
assert_eq!(restored.ja4(), ja4);
assert_eq!(
serde_json::to_value(restored).expect("restored ClientHello serializes"),
json
);
}
#[test]
fn quic_transport_parameters_are_decoded_in_client_hello() {
let handshake = client_hello_handshake(Some(&[0x00, 0x39, 0x00, 0x03, 0x04, 0x01, 0x20]));
let client_hello = ClientHello::parse_handshake(&handshake).expect("ClientHello parses");
let parameters = client_hello
.quic_transport_parameters()
.expect("parameters exist");
assert_eq!(parameters[0].id, 4);
let extension = client_hello
.extensions
.first()
.expect("extension is present");
let json = serde_json::to_value(extension).expect("QUIC extension serializes");
assert_eq!(
json,
serde_json::json!({
"quic_transport_parameters": {
"value": 57,
"data": [{"id": 4, "name": "initial_max_data", "value": 32}]
}
})
);
let restored = serde_json::from_value::<TlsExtension>(json.clone())
.expect("QUIC extension deserializes");
assert_eq!(&restored, extension);
let mut duplicate = json.clone();
let parameters = duplicate["quic_transport_parameters"]["data"]
.as_array_mut()
.unwrap();
for index in 0..4_096_u64 {
parameters.push(serde_json::json!({
"id": (1_u64 << 40) + index * 31,
"name": "other",
"value": ""
}));
}
parameters.push(parameters[0].clone());
let mut wrong_id = json;
wrong_id["quic_transport_parameters"]["value"] = serde_json::json!(58);
let opaque = serde_json::json!({"opaque": {"value": 57, "data": "040120"}});
assert!(serde_json::from_value::<TlsExtension>(duplicate).is_err());
assert!(serde_json::from_value::<TlsExtension>(wrong_id).is_err());
assert!(serde_json::from_value::<TlsExtension>(opaque).is_err());
}
#[test]
fn tls_extensions_reject_variant_id_mismatches() {
let wrong_known_id = serde_json::json!({
"supported_versions": {"value": 0, "data": {"versions": []}}
});
let non_grease_id = serde_json::json!({
"grease": {"value": 0}
});
let dedicated_opaque_id = serde_json::json!({
"opaque": {"value": 43, "data": null}
});
assert!(serde_json::from_value::<TlsExtension>(wrong_known_id).is_err());
assert!(serde_json::from_value::<TlsExtension>(non_grease_id).is_err());
assert!(serde_json::from_value::<TlsExtension>(dedicated_opaque_id).is_err());
let grease = serde_json::json!({"grease": {"value": 2570}});
let opaque = serde_json::json!({"opaque": {"value": 1, "data": null}});
assert!(serde_json::from_value::<TlsExtension>(grease).is_ok());
assert!(serde_json::from_value::<TlsExtension>(opaque).is_ok());
}
}