Source code for core.models

"""Data structures for memory dump analysis."""

from __future__ import annotations

import functools
from dataclasses import dataclass, field
from pathlib import Path
from typing import TYPE_CHECKING, Dict, List, Optional, Tuple

if TYPE_CHECKING:
    # Late import to avoid a cycle: dataset_metadata is a leaf module but
    # keeping the import behind TYPE_CHECKING makes the dependency edge
    # visible without forcing it at runtime.
    from .dataset_metadata import DatasetMeta


[docs] def deprecated_kwarg(cls, old_name: str, new_name: str): """Let a dataclass constructor accept a deprecated keyword. Map *old_name* to *new_name* when the caller supplies only the old name. If both names are supplied, leave validation to the constructor. """ orig_init = cls.__init__ @functools.wraps(orig_init) def wrapper(self, *args, **kwargs): if old_name in kwargs and new_name not in kwargs: kwargs[new_name] = kwargs.pop(old_name) orig_init(self, *args, **kwargs) cls.__init__ = wrapper
[docs] @dataclass class CryptoSecret: """Store a cryptographic secret and its protocol identifier. Equality and hashing use the secret type and value. """ secret_type: str identifier: bytes secret_value: bytes protocol: str = "TLS" @property def client_random(self) -> bytes: """Backward-compatible alias for identifier.""" return self.identifier @client_random.setter def client_random(self, value: bytes) -> None: self.identifier = value def __hash__(self): return hash((self.secret_type, self.secret_value)) def __eq__(self, other): if not isinstance(other, CryptoSecret): return NotImplemented return self.secret_type == other.secret_type and self.secret_value == other.secret_value
deprecated_kwarg(CryptoSecret, "client_random", "identifier") # Backward-compatible alias TLSSecret = CryptoSecret
[docs] @dataclass class KeyOccurrence: """A found key occurrence in a dump file.""" offset: int secret: CryptoSecret context_before: bytes key_bytes: bytes context_after: bytes @property def context_start_offset(self) -> int: return self.offset - len(self.context_before)
[docs] @dataclass class DumpFile: """Identify a dump file, its recorded phase, and its capture format.""" path: Path timestamp: str phase_prefix: str phase_name: str canonical_phase: Optional[str] = None kind: str = "raw" @property def full_phase(self) -> str: return f"{self.phase_prefix}_{self.phase_name}" @property def canonical_or_raw(self) -> str: return self.canonical_phase if self.canonical_phase else self.full_phase
[docs] @dataclass class RunDirectory: """Group a run's dump files, secrets, and capture status. The metadata is optional. A capture can be present, absent, or unreadable. """ path: Path library: str protocol_version: str run_number: int dumps: List[DumpFile] = field(default_factory=list) secrets: List[CryptoSecret] = field(default_factory=list) secret_source: str = "none" phase_mappings: Dict[str, str] = field(default_factory=dict) meta: Optional["DatasetMeta"] = None capture_path: Optional[Path] = None # Three states, mirroring the ``secrets`` + ``secret_source`` pairing above: # "present" — a capture file was found and has a non-zero size # "absent" — no capture file exists for this run # "unreadable" — a capture file exists but is zero-byte or cannot be # stat-ed. Reported distinctly from "absent" so a corpus # denominator is never silently deflated by a truncated # or permission-denied capture. capture_status: str = "absent" @property def tls_version(self) -> str: """Backward-compatible alias for protocol_version.""" return self.protocol_version @tls_version.setter def tls_version(self, value: str) -> None: self.protocol_version = value
[docs] def get_dump_for_phase(self, phase: str) -> Optional[DumpFile]: for d in self.dumps: if d.full_phase == phase: return d return None
[docs] def available_phases(self) -> List[str]: return sorted(set(d.full_phase for d in self.dumps))
deprecated_kwarg(RunDirectory, "tls_version", "protocol_version")
[docs] @dataclass class ComparisonRegion: """Aligned regions across dumps for multi-run comparison.""" secret_type: str key_length: int context_size: int run_data: List[Tuple[bytes, bytes, bytes]] = field(default_factory=list) run_labels: List[str] = field(default_factory=list) run_offsets: List[int] = field(default_factory=list)