pub fn validate_target_fields(
label: &str,
ja3: &str,
ja4: &str,
akamai_h2: &str,
peet_h2: &str,
) -> Result<(), String> {
if label.is_empty() {
return Err("empty label".to_string());
}
if !ja3.starts_with("771,") {
return Err(format!(
"JA3 must start with `771,` (got `{}…`)",
&ja3[..ja3.len().min(12)]
));
}
if !ja4.starts_with("t13") {
return Err(format!(
"JA4 must start with `t13` (got `{}…`)",
&ja4[..ja4.len().min(8)]
));
}
if let Err(e) = crate::fingerprint::akamai_h2::AkamaiH2Fingerprint::parse(akamai_h2) {
return Err(format!("Akamai-H2 invalid: {e}"));
}
if peet_h2.is_empty() {
return Err("empty peetprint".to_string());
}
let expected = crate::fingerprint::ja3::md5_string(akamai_h2);
if peet_h2 != expected {
return Err(format!(
"peet_h2 must be md5(akamai_h2); for {akamai_h2:?} that is {expected}, not {peet_h2}"
));
}
Ok(())
}
pub fn ja4_counts_match_ja3(ja3: &str, ja4: &str) -> Result<(), String> {
let fields: Vec<&str> = ja3.split(',').collect();
if fields.len() != 5 {
return Err(format!(
"JA3 must have 5 comma-separated fields, got {}",
fields.len()
));
}
let count = |field: &str| {
if field.is_empty() {
0
} else {
field.split('-').count()
}
};
let ja3_ciphers = count(fields[1]);
let ja3_exts = count(fields[2]);
if ja4.len() < 8 {
return Err(format!("JA4 too short to carry counts: {ja4}"));
}
let ja4_ciphers: usize = ja4[4..6]
.parse()
.map_err(|_| format!("JA4 cipher-count digits not numeric: {ja4}"))?;
let ja4_exts: usize = ja4[6..8]
.parse()
.map_err(|_| format!("JA4 extension-count digits not numeric: {ja4}"))?;
if ja4_ciphers != ja3_ciphers {
return Err(format!(
"JA4 cipher count {ja4_ciphers} != JA3 cipher-list length {ja3_ciphers} ({ja4} vs {ja3})"
));
}
if ja4_exts != ja3_exts {
return Err(format!(
"JA4 extension count {ja4_exts} != JA3 extension-list length {ja3_exts} ({ja4} vs {ja3})"
));
}
Ok(())
}