Skip to content

Commit

Permalink
tests: add JA4 pcap tests
Browse files Browse the repository at this point in the history
  • Loading branch information
lrstewart committed Aug 19, 2024
1 parent 87f4a05 commit 21d5018
Show file tree
Hide file tree
Showing 2 changed files with 36 additions and 2 deletions.
10 changes: 10 additions & 0 deletions tests/pcap/src/client_hello.rs
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,16 @@ impl ClientHello {
self.0.packet.metadata(Self::JA3_STR).map(str::to_owned)
}

const JA4_HASH: &'static str = "tls.handshake.ja4";
pub fn ja4_hash(&self) -> Option<String> {
self.0.packet.metadata(Self::JA4_HASH).map(str::to_owned)
}

const JA4_STR: &'static str = "tls.handshake.ja4_r";
pub fn ja4_string(&self) -> Option<String> {
self.0.packet.metadata(Self::JA4_STR).map(str::to_owned)
}

pub fn message(&self) -> &HandshakeMessage {
&self.0
}
Expand Down
28 changes: 26 additions & 2 deletions tests/pcap/tests/s2n_client_hellos.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ use pcap::all_pcaps;
use pcap::client_hello::ClientHello as PcapHello;
use pcap::handshake_message::Builder;
use s2n_tls::client_hello::{ClientHello as S2NHello, FingerprintType};
use s2n_tls::fingerprint;

fn get_s2n_hello(pcap_hello: &PcapHello) -> Result<Box<S2NHello>> {
let bytes = pcap_hello.message().bytes();
Expand All @@ -14,9 +15,9 @@ fn get_s2n_hello(pcap_hello: &PcapHello) -> Result<Box<S2NHello>> {
Ok(r?)
}

fn test_all_client_hellos<F>(test_fn: F) -> Result<()>
fn test_all_client_hellos<F>(mut test_fn: F) -> Result<()>
where
F: FnOnce(PcapHello, Box<S2NHello>) -> Result<()> + Copy,
F: FnMut(PcapHello, Box<S2NHello>) -> Result<()>,
{
let pcaps = all_pcaps();
for pcap in pcaps {
Expand Down Expand Up @@ -62,3 +63,26 @@ fn ja3_fingerprints() -> Result<()> {
Ok(())
})
}

#[test]
fn ja4_fingerprints() -> Result<()> {
let mut builder = fingerprint::Builder::new(FingerprintType::JA4)?;

test_all_client_hellos(|pcap_hello, s2n_hello| {
let mut fingerprint = builder.build(&s2n_hello)?;

let s2n_ja4_hash = fingerprint
.hash()
.context("s2n failed to calculate ja4 hash")?
.to_owned();

let s2n_ja4_str = fingerprint
.raw()
.context("s2n failed to calculate ja4 string")?
.to_owned();

assert_eq!(pcap_hello.ja4_hash(), Some(s2n_ja4_hash));
assert_eq!(pcap_hello.ja4_string(), Some(s2n_ja4_str));
Ok(())
})
}

0 comments on commit 21d5018

Please sign in to comment.