| 1 |
|
| 2 |
|
| 3 |
|
| 4 |
|
| 5 |
|
| 6 |
|
| 7 |
|
| 8 |
use crate::{ |
| 9 |
client_hello::ClientHello, |
| 10 |
error::{Error, Fallible}, |
| 11 |
ffi::*, |
| 12 |
}; |
| 13 |
|
| 14 |
use core::ptr::NonNull; |
| 15 |
|
| 16 |
#[non_exhaustive] |
| 17 |
#[derive(Copy, Clone)] |
| 18 |
pub enum FingerprintType { |
| 19 |
JA3, |
| 20 |
JA4, |
| 21 |
} |
| 22 |
|
| 23 |
impl From<FingerprintType> for s2n_tls_sys::s2n_fingerprint_type::Type { |
| 24 |
fn from(value: FingerprintType) -> Self { |
| 25 |
match value { |
| 26 |
FingerprintType::JA3 => s2n_tls_sys::s2n_fingerprint_type::FINGERPRINT_JA3, |
| 27 |
FingerprintType::JA4 => s2n_tls_sys::s2n_fingerprint_type::FINGERPRINT_JA4, |
| 28 |
} |
| 29 |
} |
| 30 |
} |
| 31 |
|
| 32 |
|
| 33 |
pub struct Fingerprint<'a>(&'a mut Builder); |
| 34 |
|
| 35 |
impl Fingerprint<'_> { |
| 36 |
|
| 37 |
|
| 38 |
|
| 39 |
pub fn hash_size(&self) -> Result<usize, Error> { |
| 40 |
self.0.hash_size() |
| 41 |
} |
| 42 |
|
| 43 |
|
| 44 |
|
| 45 |
|
| 46 |
pub fn hash(&mut self) -> Result<&str, Error> { |
| 47 |
if self.0.hash.is_empty() { |
| 48 |
let mut output_size = 0; |
| 49 |
unsafe { |
| 50 |
s2n_fingerprint_get_hash( |
| 51 |
self.0.ptr.as_ptr(), |
| 52 |
self.0.hash.capacity() as u32, |
| 53 |
self.0.hash.as_mut_ptr(), |
| 54 |
&mut output_size, |
| 55 |
) |
| 56 |
.into_result()?; |
| 57 |
|
| 58 |
self.0.hash.as_mut_vec().set_len(output_size as usize); |
| 59 |
} |
| 60 |
} |
| 61 |
Ok(&self.0.hash) |
| 62 |
} |
| 63 |
|
| 64 |
|
| 65 |
|
| 66 |
|
| 67 |
|
| 68 |
|
| 69 |
|
| 70 |
|
| 71 |
pub fn raw_size(&self) -> Result<usize, Error> { |
| 72 |
let mut raw_size = 0; |
| 73 |
unsafe { s2n_fingerprint_get_raw_size(self.0.ptr.as_ptr(), &mut raw_size).into_result() }?; |
| 74 |
Ok(raw_size as usize) |
| 75 |
} |
| 76 |
|
| 77 |
|
| 78 |
|
| 79 |
|
| 80 |
|
| 81 |
|
| 82 |
|
| 83 |
|
| 84 |
|
| 85 |
|
| 86 |
|
| 87 |
|
| 88 |
|
| 89 |
|
| 90 |
|
| 91 |
|
| 92 |
|
| 93 |
|
| 94 |
|
| 95 |
|
| 96 |
|
| 97 |
|
| 98 |
|
| 99 |
|
| 100 |
|
| 101 |
|
| 102 |
|
| 103 |
|
| 104 |
|
| 105 |
|
| 106 |
|
| 107 |
|
| 108 |
|
| 109 |
|
| 110 |
|
| 111 |
|
| 112 |
|
| 113 |
|
| 114 |
|
| 115 |
|
| 116 |
|
| 117 |
|
| 118 |
|
| 119 |
|
| 120 |
|
| 121 |
pub fn raw(&mut self) -> Result<&str, Error> { |
| 122 |
if self.0.raw.is_empty() { |
| 123 |
if self.0.raw_size.is_none() { |
| 124 |
self.0.raw.reserve_exact(self.raw_size()?); |
| 125 |
}; |
| 126 |
|
| 127 |
let mut output_size = 0; |
| 128 |
unsafe { |
| 129 |
s2n_fingerprint_get_raw( |
| 130 |
self.0.ptr.as_ptr(), |
| 131 |
self.0.raw.capacity() as u32, |
| 132 |
self.0.raw.as_mut_ptr(), |
| 133 |
&mut output_size, |
| 134 |
) |
| 135 |
.into_result()?; |
| 136 |
|
| 137 |
self.0.raw.as_mut_vec().set_len(output_size as usize); |
| 138 |
}; |
| 139 |
} |
| 140 |
Ok(&self.0.raw) |
| 141 |
} |
| 142 |
|
| 143 |
pub fn builder(method: FingerprintType) -> Result<Builder, Error> { |
| 144 |
Builder::new(method) |
| 145 |
} |
| 146 |
} |
| 147 |
|
| 148 |
impl Drop for Fingerprint<'_> { |
| 149 |
|
| 150 |
|
| 151 |
|
| 152 |
fn drop(&mut self) { |
| 153 |
unsafe { |
| 154 |
s2n_fingerprint_wipe(self.0.ptr.as_ptr()) |
| 155 |
.into_result() |
| 156 |
.unwrap() |
| 157 |
}; |
| 158 |
self.0.hash.clear(); |
| 159 |
self.0.raw.clear(); |
| 160 |
} |
| 161 |
} |
| 162 |
|
| 163 |
|
| 164 |
|
| 165 |
|
| 166 |
|
| 167 |
|
| 168 |
|
| 169 |
|
| 170 |
|
| 171 |
|
| 172 |
|
| 173 |
|
| 174 |
|
| 175 |
|
| 176 |
|
| 177 |
|
| 178 |
|
| 179 |
|
| 180 |
|
| 181 |
|
| 182 |
|
| 183 |
|
| 184 |
|
| 185 |
|
| 186 |
|
| 187 |
|
| 188 |
|
| 189 |
|
| 190 |
|
| 191 |
|
| 192 |
|
| 193 |
|
| 194 |
|
| 195 |
|
| 196 |
|
| 197 |
|
| 198 |
|
| 199 |
|
| 200 |
|
| 201 |
|
| 202 |
pub struct Builder { |
| 203 |
ptr: NonNull<s2n_fingerprint>, |
| 204 |
hash: String, |
| 205 |
raw: String, |
| 206 |
raw_size: Option<usize>, |
| 207 |
} |
| 208 |
|
| 209 |
impl Builder { |
| 210 |
|
| 211 |
|
| 212 |
|
| 213 |
pub fn new(method: FingerprintType) -> Result<Self, Error> { |
| 214 |
crate::init::init(); |
| 215 |
let ptr = unsafe { s2n_fingerprint_new(method.into()).into_result() }?; |
| 216 |
let hash = String::with_capacity(Self::ptr_hash_size(&ptr)?); |
| 217 |
let raw = String::new(); |
| 218 |
Ok(Builder { |
| 219 |
ptr, |
| 220 |
hash, |
| 221 |
raw, |
| 222 |
raw_size: None, |
| 223 |
}) |
| 224 |
} |
| 225 |
|
| 226 |
|
| 227 |
fn ptr_hash_size(ptr: &NonNull<s2n_fingerprint>) -> Result<usize, Error> { |
| 228 |
let mut hash_size = 0; |
| 229 |
unsafe { s2n_fingerprint_get_hash_size(ptr.as_ptr(), &mut hash_size).into_result() }?; |
| 230 |
Ok(hash_size as usize) |
| 231 |
} |
| 232 |
|
| 233 |
|
| 234 |
|
| 235 |
|
| 236 |
pub fn hash_size(&self) -> Result<usize, Error> { |
| 237 |
Self::ptr_hash_size(&self.ptr) |
| 238 |
} |
| 239 |
|
| 240 |
|
| 241 |
|
| 242 |
|
| 243 |
|
| 244 |
|
| 245 |
|
| 246 |
|
| 247 |
|
| 248 |
|
| 249 |
|
| 250 |
pub fn set_raw_size(&mut self, size: usize) -> Result<&mut Self, Error> { |
| 251 |
self.raw_size = Some(size); |
| 252 |
self.raw.reserve_exact(size); |
| 253 |
Ok(self) |
| 254 |
} |
| 255 |
|
| 256 |
|
| 257 |
|
| 258 |
|
| 259 |
|
| 260 |
|
| 261 |
|
| 262 |
|
| 263 |
|
| 264 |
|
| 265 |
|
| 266 |
|
| 267 |
|
| 268 |
|
| 269 |
|
| 270 |
pub fn build<'a>( |
| 271 |
&'a mut self, |
| 272 |
client_hello: &'a ClientHello, |
| 273 |
) -> Result<Fingerprint<'a>, Error> { |
| 274 |
unsafe { |
| 275 |
s2n_fingerprint_set_client_hello(self.ptr.as_ptr(), client_hello.deref_mut_ptr()) |
| 276 |
.into_result() |
| 277 |
}?; |
| 278 |
Ok(Fingerprint(self)) |
| 279 |
} |
| 280 |
} |
| 281 |
|
| 282 |
impl Drop for Builder { |
| 283 |
|
| 284 |
|
| 285 |
|
| 286 |
fn drop(&mut self) { |
| 287 |
let mut ptr: *mut s2n_fingerprint = unsafe { self.ptr.as_mut() }; |
| 288 |
unsafe { |
| 289 |
let _ = s2n_fingerprint_free(std::ptr::addr_of_mut!(ptr)); |
| 290 |
} |
| 291 |
} |
| 292 |
} |
| 293 |
|
| 294 |
|
| 295 |
const MD5_HASH_SIZE: u32 = 16; |
| 296 |
impl ClientHello { |
| 297 |
|
| 298 |
|
| 299 |
|
| 300 |
|
| 301 |
|
| 302 |
|
| 303 |
|
| 304 |
|
| 305 |
|
| 306 |
|
| 307 |
|
| 308 |
|
| 309 |
|
| 310 |
|
| 311 |
|
| 312 |
|
| 313 |
|
| 314 |
|
| 315 |
|
| 316 |
|
| 317 |
|
| 318 |
|
| 319 |
|
| 320 |
|
| 321 |
|
| 322 |
#[deprecated = "Users should prefer the Fingerprint::hash() method"] |
| 323 |
pub fn fingerprint_hash( |
| 324 |
&self, |
| 325 |
hash: FingerprintType, |
| 326 |
output: &mut Vec<u8>, |
| 327 |
) -> Result<u32, Error> { |
| 328 |
let mut hash_size: u32 = 0; |
| 329 |
let mut str_size: u32 = 0; |
| 330 |
|
| 331 |
if output.capacity() < MD5_HASH_SIZE as usize { |
| 332 |
output.reserve_exact(MD5_HASH_SIZE as usize - output.len()); |
| 333 |
} |
| 334 |
unsafe { |
| 335 |
s2n_client_hello_get_fingerprint_hash( |
| 336 |
self.deref_mut_ptr(), |
| 337 |
hash.into(), |
| 338 |
MD5_HASH_SIZE, |
| 339 |
output.as_mut_ptr(), |
| 340 |
&mut hash_size, |
| 341 |
&mut str_size, |
| 342 |
) |
| 343 |
.into_result()?; |
| 344 |
|
| 345 |
|
| 346 |
output.set_len(hash_size as usize); |
| 347 |
}; |
| 348 |
Ok(str_size) |
| 349 |
} |
| 350 |
|
| 351 |
|
| 352 |
|
| 353 |
|
| 354 |
|
| 355 |
|
| 356 |
#[deprecated = "Users should prefer the Fingerprint::raw() method"] |
| 357 |
pub fn fingerprint_string( |
| 358 |
&self, |
| 359 |
hash: FingerprintType, |
| 360 |
output: &mut String, |
| 361 |
) -> Result<(), Error> { |
| 362 |
let mut output_size = 0; |
| 363 |
unsafe { |
| 364 |
s2n_tls_sys::s2n_client_hello_get_fingerprint_string( |
| 365 |
self.deref_mut_ptr(), |
| 366 |
hash.into(), |
| 367 |
output.capacity() as u32, |
| 368 |
output.as_mut_ptr(), |
| 369 |
&mut output_size, |
| 370 |
) |
| 371 |
.into_result()?; |
| 372 |
|
| 373 |
|
| 374 |
output.as_mut_vec().set_len(output_size as usize); |
| 375 |
}; |
| 376 |
Ok(()) |
| 377 |
} |
| 378 |
} |
| 379 |
|
| 380 |
#[cfg(test)] |
| 381 |
mod tests { |
| 382 |
use super::*; |
| 383 |
use crate::{ |
| 384 |
error::ErrorType, |
| 385 |
security, |
| 386 |
security::Policy, |
| 387 |
testing::{build_config, TestPair}, |
| 388 |
}; |
| 389 |
use std::{collections::HashSet, error::Error}; |
| 390 |
|
| 391 |
const CLIENT_HELLO_BYTES: &[u8] = &[ |
| 392 |
0x01, 0x00, 0x00, 0xEC, 0x03, 0x03, 0x90, 0xe8, 0xcc, 0xee, 0xe5, 0x70, 0xa2, 0xa1, 0x2f, |
| 393 |
0x6b, 0x69, 0xd2, 0x66, 0x96, 0x0f, 0xcf, 0x20, 0xd5, 0x32, 0x6e, 0xc4, 0xb2, 0x8c, 0xc7, |
| 394 |
0xbd, 0x0a, 0x06, 0xc2, 0xa5, 0x14, 0xfc, 0x34, 0x20, 0xaf, 0x72, 0xbf, 0x39, 0x99, 0xfb, |
| 395 |
0x20, 0x70, 0xc3, 0x10, 0x83, 0x0c, 0xee, 0xfb, 0xfa, 0x72, 0xcc, 0x5d, 0xa8, 0x99, 0xb4, |
| 396 |
0xc5, 0x53, 0xd6, 0x3d, 0xa0, 0x53, 0x7a, 0x5c, 0xbc, 0xf5, 0x0b, 0x00, 0x1e, 0xc0, 0x2b, |
| 397 |
0xc0, 0x2f, 0xcc, 0xa9, 0xcc, 0xa8, 0xc0, 0x2c, 0xc0, 0x30, 0xc0, 0x0a, 0xc0, 0x09, 0xc0, |
| 398 |
0x13, 0xc0, 0x14, 0x00, 0x33, 0x00, 0x39, 0x00, 0x2f, 0x00, 0x35, 0x00, 0x0a, 0x01, 0x00, |
| 399 |
0x00, 0x85, 0x00, 0x00, 0x00, 0x23, 0x00, 0x21, 0x00, 0x00, 0x1e, 0x69, 0x6e, 0x63, 0x6f, |
| 400 |
0x6d, 0x69, 0x6e, 0x67, 0x2e, 0x74, 0x65, 0x6c, 0x65, 0x6d, 0x65, 0x74, 0x72, 0x79, 0x2e, |
| 401 |
0x6d, 0x6f, 0x7a, 0x69, 0x6c, 0x6c, 0x61, 0x2e, 0x6f, 0x72, 0x67, 0x00, 0x17, 0x00, 0x00, |
| 402 |
0xff, 0x01, 0x00, 0x01, 0x00, 0x00, 0x0a, 0x00, 0x0a, 0x00, 0x08, 0x00, 0x1d, 0x00, 0x17, |
| 403 |
0x00, 0x18, 0x00, 0x19, 0x00, 0x0b, 0x00, 0x02, 0x01, 0x00, 0x00, 0x23, 0x00, 0x00, 0x00, |
| 404 |
0x10, 0x00, 0x0e, 0x00, 0x0c, 0x02, 0x68, 0x32, 0x08, 0x68, 0x74, 0x74, 0x70, 0x2f, 0x31, |
| 405 |
0x2e, 0x31, 0x00, 0x05, 0x00, 0x05, 0x01, 0x00, 0x00, 0x00, 0x00, 0x00, 0x0d, 0x00, 0x18, |
| 406 |
0x00, 0x16, 0x04, 0x03, 0x05, 0x03, 0x06, 0x03, 0x08, 0x04, 0x08, 0x05, 0x08, 0x06, 0x04, |
| 407 |
0x01, 0x05, 0x01, 0x06, 0x01, 0x02, 0x03, 0x02, 0x01, 0x00, 0x1c, 0x00, 0x02, 0x40, 0x00, |
| 408 |
]; |
| 409 |
const JA3_FULL_STRING: &str = "771,49195-49199-52393-52392-49196-49200-\ |
| 410 |
49162-49161-49171-49172-51-57-47-53-10,0-\ |
| 411 |
23-65281-10-11-35-16-5-13-28,29-23-24-25,0"; |
| 412 |
const JA3_HASH: &str = "839bbe3ed07fed922ded5aaf714d6842"; |
| 413 |
|
| 414 |
fn simple_handshake() -> Result<TestPair, Box<dyn Error>> { |
| 415 |
let config = build_config(&security::DEFAULT)?; |
| 416 |
let mut pair = TestPair::from_config(&config); |
| 417 |
pair.handshake()?; |
| 418 |
Ok(pair) |
| 419 |
} |
| 420 |
|
| 421 |
#[test] |
| 422 |
fn connection_fingerprint() -> Result<(), Box<dyn Error>> { |
| 423 |
let pair = simple_handshake()?; |
| 424 |
let client_hello = pair.server.client_hello()?; |
| 425 |
|
| 426 |
let mut builder = Fingerprint::builder(FingerprintType::JA3)?; |
| 427 |
let mut fingerprint = builder.build(client_hello)?; |
| 428 |
|
| 429 |
let hash_size = fingerprint.hash_size()?; |
| 430 |
let hash = fingerprint.hash()?; |
| 431 |
assert_eq!(hash.len(), hash_size); |
| 432 |
hex::decode(hash)?; |
| 433 |
|
| 434 |
let raw_size = fingerprint.raw_size()?; |
| 435 |
let raw = fingerprint.raw()?; |
| 436 |
assert_eq!(raw.len(), raw_size); |
| 437 |
|
| 438 |
Ok(()) |
| 439 |
} |
| 440 |
|
| 441 |
#[test] |
| 442 |
fn known_value() -> Result<(), Box<dyn Error>> { |
| 443 |
let client_hello = ClientHello::parse_client_hello(CLIENT_HELLO_BYTES)?; |
| 444 |
|
| 445 |
let mut builder = Fingerprint::builder(FingerprintType::JA3)?; |
| 446 |
let mut fingerprint = builder.build(&client_hello)?; |
| 447 |
|
| 448 |
let hash_size = fingerprint.hash_size()?; |
| 449 |
let hash = fingerprint.hash()?; |
| 450 |
assert_eq!(hash.len(), hash_size); |
| 451 |
assert_eq!(hash, JA3_HASH); |
| 452 |
|
| 453 |
let raw_size = fingerprint.raw_size()?; |
| 454 |
let raw = fingerprint.raw()?; |
| 455 |
assert_eq!(raw.len(), raw_size); |
| 456 |
assert_eq!(raw, JA3_FULL_STRING); |
| 457 |
|
| 458 |
Ok(()) |
| 459 |
} |
| 460 |
|
| 461 |
#[test] |
| 462 |
fn multiple_fingerprints() -> Result<(), Box<dyn Error>> { |
| 463 |
let mut builder = Fingerprint::builder(FingerprintType::JA3)?; |
| 464 |
|
| 465 |
for _ in 1..10 { |
| 466 |
let client_hello = ClientHello::parse_client_hello(CLIENT_HELLO_BYTES)?; |
| 467 |
let mut fingerprint = builder.build(&client_hello)?; |
| 468 |
|
| 469 |
let hash = fingerprint.hash()?; |
| 470 |
assert_eq!(hash, JA3_HASH); |
| 471 |
} |
| 472 |
|
| 473 |
Ok(()) |
| 474 |
} |
| 475 |
|
| 476 |
#[test] |
| 477 |
fn multiple_fingerprints_reset() -> Result<(), Box<dyn Error>> { |
| 478 |
let mut builder = Fingerprint::builder(FingerprintType::JA3)?; |
| 479 |
let client_hello = ClientHello::parse_client_hello(CLIENT_HELLO_BYTES)?; |
| 480 |
|
| 481 |
{ |
| 482 |
let mut fingerprint = builder.build(&client_hello)?; |
| 483 |
|
| 484 |
|
| 485 |
fingerprint |
| 486 |
.raw_size() |
| 487 |
.expect_err("Raw size unexpectedly set"); |
| 488 |
|
| 489 |
|
| 490 |
fingerprint.hash()?; |
| 491 |
fingerprint.raw_size()?; |
| 492 |
} |
| 493 |
|
| 494 |
|
| 495 |
let client_hello = ClientHello::parse_client_hello(CLIENT_HELLO_BYTES)?; |
| 496 |
let fingerprint = builder.build(&client_hello)?; |
| 497 |
|
| 498 |
|
| 499 |
fingerprint |
| 500 |
.raw_size() |
| 501 |
.expect_err("Fingerprint state not reset"); |
| 502 |
|
| 503 |
Ok(()) |
| 504 |
} |
| 505 |
|
| 506 |
#[test] |
| 507 |
fn multiple_connection_fingerprints() -> Result<(), Box<dyn Error>> { |
| 508 |
let mut builder = Fingerprint::builder(FingerprintType::JA3)?; |
| 509 |
|
| 510 |
let configs = [ |
| 511 |
build_config(&Policy::from_version("20240501")?)?, |
| 512 |
build_config(&Policy::from_version("20240502")?)?, |
| 513 |
build_config(&Policy::from_version("20240503")?)?, |
| 514 |
build_config(&Policy::from_version("test_all")?)?, |
| 515 |
]; |
| 516 |
let mut fingerprints: Vec<String> = Vec::new(); |
| 517 |
|
| 518 |
let handshake_count = configs.len() * 5; |
| 519 |
for i in 0..handshake_count { |
| 520 |
let i = i % configs.len(); |
| 521 |
|
| 522 |
let config = &configs[i]; |
| 523 |
let mut pair = TestPair::from_config(config); |
| 524 |
pair.handshake()?; |
| 525 |
|
| 526 |
let client_hello = pair.server.client_hello()?; |
| 527 |
let mut fingerprint = builder.build(client_hello)?; |
| 528 |
|
| 529 |
let hash = fingerprint.hash()?; |
| 530 |
hex::decode(hash)?; |
| 531 |
|
| 532 |
|
| 533 |
if let Some(previous) = fingerprints.get(i) { |
| 534 |
assert_eq!(previous.as_str(), hash); |
| 535 |
} else { |
| 536 |
fingerprints.push(hash.to_string()); |
| 537 |
} |
| 538 |
} |
| 539 |
assert_eq!(configs.len(), fingerprints.len()); |
| 540 |
|
| 541 |
|
| 542 |
let unique: HashSet<&String> = HashSet::from_iter(fingerprints.iter()); |
| 543 |
assert_eq!(unique.len(), fingerprints.len()); |
| 544 |
|
| 545 |
Ok(()) |
| 546 |
} |
| 547 |
|
| 548 |
#[test] |
| 549 |
fn raw_sufficient_memory() -> Result<(), Box<dyn Error>> { |
| 550 |
let client_hello = ClientHello::parse_client_hello(CLIENT_HELLO_BYTES)?; |
| 551 |
|
| 552 |
let mut builder = Fingerprint::builder(FingerprintType::JA3)?; |
| 553 |
builder.set_raw_size(JA3_FULL_STRING.len())?; |
| 554 |
|
| 555 |
let mut fingerprint = builder.build(&client_hello)?; |
| 556 |
let raw = fingerprint.raw()?; |
| 557 |
assert_eq!(raw, JA3_FULL_STRING); |
| 558 |
|
| 559 |
Ok(()) |
| 560 |
} |
| 561 |
|
| 562 |
#[test] |
| 563 |
fn raw_insufficient_memory() -> Result<(), Box<dyn Error>> { |
| 564 |
let client_hello = ClientHello::parse_client_hello(CLIENT_HELLO_BYTES)?; |
| 565 |
|
| 566 |
let mut builder = Fingerprint::builder(FingerprintType::JA3)?; |
| 567 |
builder.set_raw_size(JA3_FULL_STRING.len() - 1)?; |
| 568 |
|
| 569 |
let mut fingerprint = builder.build(&client_hello)?; |
| 570 |
let error = fingerprint |
| 571 |
.raw() |
| 572 |
.expect_err("Calculated raw string despite insufficient memory"); |
| 573 |
assert_eq!(error.kind(), ErrorType::UsageError); |
| 574 |
assert_eq!(error.name(), "S2N_ERR_INSUFFICIENT_MEM_SIZE"); |
| 575 |
|
| 576 |
Ok(()) |
| 577 |
} |
| 578 |
|
| 579 |
#[test] |
| 580 |
fn hash_does_not_allocate_memory() -> Result<(), Box<dyn Error>> { |
| 581 |
let client_hello = ClientHello::parse_client_hello(CLIENT_HELLO_BYTES)?; |
| 582 |
|
| 583 |
let mut builder = Fingerprint::builder(FingerprintType::JA3)?; |
| 584 |
|
| 585 |
for _ in 0..10 { |
| 586 |
let snapshot = checkers::with(|| { |
| 587 |
let mut fingerprint = builder.build(&client_hello).unwrap(); |
| 588 |
fingerprint.hash().unwrap(); |
| 589 |
}); |
| 590 |
assert!(snapshot.events.is_empty()); |
| 591 |
} |
| 592 |
|
| 593 |
Ok(()) |
| 594 |
} |
| 595 |
|
| 596 |
#[test] |
| 597 |
fn raw_may_allocate_memory() -> Result<(), Box<dyn Error>> { |
| 598 |
let client_hello = ClientHello::parse_client_hello(CLIENT_HELLO_BYTES)?; |
| 599 |
|
| 600 |
let mut builder = Fingerprint::builder(FingerprintType::JA3)?; |
| 601 |
|
| 602 |
let minimum_size = JA3_FULL_STRING.len(); |
| 603 |
let large_size = minimum_size + 100; |
| 604 |
|
| 605 |
|
| 606 |
|
| 607 |
|
| 608 |
let snapshot = checkers::with(|| { |
| 609 |
let mut fingerprint = builder.build(&client_hello).unwrap(); |
| 610 |
|
| 611 |
|
| 612 |
|
| 613 |
fingerprint.hash().unwrap(); |
| 614 |
|
| 615 |
fingerprint.raw().unwrap(); |
| 616 |
}); |
| 617 |
|
| 618 |
assert_eq!(snapshot.events.allocs(), 1); |
| 619 |
assert_eq!(snapshot.events.reallocs(), 0); |
| 620 |
assert_eq!(snapshot.events.frees(), 0); |
| 621 |
assert_eq!(snapshot.events.max_memory_used().unwrap(), minimum_size); |
| 622 |
|
| 623 |
|
| 624 |
|
| 625 |
let mut full_snapshot = snapshot; |
| 626 |
|
| 627 |
|
| 628 |
|
| 629 |
let snapshot = checkers::with(|| { |
| 630 |
for _ in 0..10 { |
| 631 |
let mut fingerprint = builder.build(&client_hello).unwrap(); |
| 632 |
|
| 633 |
|
| 634 |
|
| 635 |
fingerprint.hash().unwrap(); |
| 636 |
|
| 637 |
for _ in 0..10 { |
| 638 |
fingerprint.raw().unwrap(); |
| 639 |
} |
| 640 |
} |
| 641 |
}); |
| 642 |
assert!(snapshot.events.is_empty()); |
| 643 |
|
| 644 |
|
| 645 |
|
| 646 |
let snapshot = checkers::with(|| { |
| 647 |
builder.set_raw_size(large_size).unwrap(); |
| 648 |
}); |
| 649 |
assert_eq!(snapshot.events.allocs(), 0); |
| 650 |
assert_eq!(snapshot.events.reallocs(), 1); |
| 651 |
assert_eq!(snapshot.events.frees(), 0); |
| 652 |
|
| 653 |
|
| 654 |
|
| 655 |
for event in snapshot.events.as_slice() { |
| 656 |
full_snapshot.events.push(event.clone()); |
| 657 |
} |
| 658 |
|
| 659 |
assert_eq!(full_snapshot.events.max_memory_used().unwrap(), large_size); |
| 660 |
|
| 661 |
|
| 662 |
|
| 663 |
let snapshot = checkers::with(|| { |
| 664 |
let mut fingerprint = builder.build(&client_hello).unwrap(); |
| 665 |
fingerprint.raw().unwrap(); |
| 666 |
}); |
| 667 |
assert!(snapshot.events.is_empty()); |
| 668 |
|
| 669 |
|
| 670 |
|
| 671 |
let snapshot = checkers::with(|| { |
| 672 |
builder.set_raw_size(minimum_size).unwrap(); |
| 673 |
}); |
| 674 |
assert!(snapshot.events.is_empty()); |
| 675 |
|
| 676 |
|
| 677 |
let snapshot = checkers::with(|| { |
| 678 |
for _ in 0..10 { |
| 679 |
let mut fingerprint = builder.build(&client_hello).unwrap(); |
| 680 |
fingerprint.raw().unwrap(); |
| 681 |
} |
| 682 |
}); |
| 683 |
assert!(snapshot.events.is_empty()); |
| 684 |
|
| 685 |
Ok(()) |
| 686 |
} |
| 687 |
|
| 688 |
#[test] |
| 689 |
#[allow(deprecated)] |
| 690 |
fn legacy_connection_fingerprint() -> Result<(), Box<dyn Error>> { |
| 691 |
let pair = simple_handshake()?; |
| 692 |
let client_hello = pair.server.client_hello()?; |
| 693 |
|
| 694 |
let mut hash = Vec::with_capacity(MD5_HASH_SIZE.try_into()?); |
| 695 |
let str_size = client_hello.fingerprint_hash(FingerprintType::JA3, &mut hash)?; |
| 696 |
assert_eq!(hash.len(), MD5_HASH_SIZE.try_into()?); |
| 697 |
|
| 698 |
let mut full_str = String::with_capacity(str_size.try_into()?); |
| 699 |
client_hello.fingerprint_string(FingerprintType::JA3, &mut full_str)?; |
| 700 |
assert_eq!(full_str.len(), str_size.try_into()?); |
| 701 |
|
| 702 |
Ok(()) |
| 703 |
} |
| 704 |
|
| 705 |
#[test] |
| 706 |
#[allow(deprecated)] |
| 707 |
fn legacy_known_value() -> Result<(), Box<dyn Error>> { |
| 708 |
let client_hello = ClientHello::parse_client_hello(CLIENT_HELLO_BYTES)?; |
| 709 |
|
| 710 |
let mut hash = Vec::with_capacity(MD5_HASH_SIZE.try_into()?); |
| 711 |
let str_size = client_hello.fingerprint_hash(FingerprintType::JA3, &mut hash)?; |
| 712 |
assert_eq!(hash.len(), MD5_HASH_SIZE.try_into()?); |
| 713 |
assert_eq!(hash, hex::decode(JA3_HASH)?); |
| 714 |
|
| 715 |
let mut full_str = String::with_capacity(str_size.try_into()?); |
| 716 |
client_hello.fingerprint_string(FingerprintType::JA3, &mut full_str)?; |
| 717 |
assert_eq!(full_str.len(), str_size.try_into()?); |
| 718 |
assert_eq!(full_str, JA3_FULL_STRING); |
| 719 |
|
| 720 |
Ok(()) |
| 721 |
} |
| 722 |
|
| 723 |
#[test] |
| 724 |
#[allow(deprecated)] |
| 725 |
fn legacy_hash_output_resizing() -> Result<(), Box<dyn Error>> { |
| 726 |
let client_hello = ClientHello::parse_client_hello(CLIENT_HELLO_BYTES)?; |
| 727 |
let hash_capacities = vec![0, MD5_HASH_SIZE, 1_000]; |
| 728 |
for initial_size in hash_capacities { |
| 729 |
let mut hash = Vec::with_capacity(initial_size.try_into()?); |
| 730 |
client_hello.fingerprint_hash(FingerprintType::JA3, &mut hash)?; |
| 731 |
assert_eq!(hash.len(), MD5_HASH_SIZE.try_into()?); |
| 732 |
} |
| 733 |
Ok(()) |
| 734 |
} |
| 735 |
|
| 736 |
#[test] |
| 737 |
#[allow(deprecated)] |
| 738 |
fn legacy_string_output_too_small() -> Result<(), Box<dyn Error>> { |
| 739 |
let client_hello = ClientHello::parse_client_hello(CLIENT_HELLO_BYTES)?; |
| 740 |
let mut fingerprint_string = String::with_capacity(JA3_FULL_STRING.len() - 1); |
| 741 |
let fingerprint_err = client_hello |
| 742 |
.fingerprint_string(FingerprintType::JA3, &mut fingerprint_string) |
| 743 |
.unwrap_err(); |
| 744 |
assert_eq!(fingerprint_err.kind(), ErrorType::UsageError); |
| 745 |
assert_eq!(fingerprint_err.name(), "S2N_ERR_INSUFFICIENT_MEM_SIZE"); |
| 746 |
Ok(()) |
| 747 |
} |
| 748 |
} |