scripts/check_evidence.py
"""Check an explicit local QNX build/capture bundle; no credential or SDK reads."""
import argparse
import hashlib
import json
from pathlib import Path
import re
import sys
ROOT = Path(__file__).resolve().parents[1]
sys.path.insert(0, str(ROOT))
from waitlens.core import InputError, analyze
from scripts.build_qnx import elf_machine
def check(build_dir, capture_dir, *, allow_synthetic=False):
receipt = json.loads((build_dir / 'build-receipt.json').read_text(encoding='utf-8'))
if not isinstance(receipt, dict):
raise ValueError('build receipt must be a JSON object')
synthetic = receipt.get('evidence_mode') == 'synthetic' or (capture_dir / 'SYNTHETIC.txt').exists()
if synthetic and not allow_synthetic:
raise ValueError('synthetic dry-run material is not real QNX evidence')
if receipt.get('status') != 'built' or receipt.get('compiler_exit') != 0:
raise ValueError('a successful actual compiler receipt is required')
source_hash = hashlib.sha256((ROOT / 'target/waitlens_probe.c').read_bytes()).hexdigest()
binary = (build_dir / 'waitlens_probe').read_bytes()
if source_hash != receipt.get('source_sha256') or hashlib.sha256(binary).hexdigest() != receipt.get('binary_sha256'):
raise ValueError('source or binary hash differs from the build receipt')
expected = {'gcc_ntox86_64': 62, 'gcc_ntoaarch64le': 183}.get(receipt.get('variant'))
if expected is None or elf_machine(binary) != expected:
raise ValueError('binary architecture does not match the build variant')
# Require a returned target binary too, linking the captured workload to the built artifact.
if (capture_dir / 'waitlens_probe').read_bytes() != binary:
raise ValueError('returned target binary differs from the host build')
platform = (capture_dir / 'platform.txt').read_text(encoding='utf-8').splitlines()
if len(platform) != 2 or platform[0].strip() != 'QNX' or not re.fullmatch(r'8\.0(?:[.\w-]*)', platform[1].strip()):
raise ValueError('platform.txt must contain uname -s = QNX and uname -r = 8.0...')
log = (capture_dir / 'probe.log').read_text(encoding='utf-8')
if 'compiler_qnx=800' not in log or 'WaitLens probe: completed' not in log or 'reply=OK' not in log:
raise ValueError('probe did not record a completed QNX 8.0 exchange')
identity = re.search(r'server=(\d+) client=(\d+) phase=delay_before_receive', log)
if not identity:
raise ValueError('server/client identities missing from the probe log')
server, client = map(int, identity.groups())
inputs, tids = {}, []
for name, state in [('send.txt', 'SEND'), ('reply.txt', 'REPLY')]:
content = (capture_dir / name).read_text(encoding='utf-8-sig')
report = analyze(content, provenance='target-capture', label=name)
observed = {e['source_tid'] for e in report['edges'] if e['source_pid'] == client
and e['destination_pid'] == server and e['state'] == state and e['destination_observed']}
if not observed:
raise ValueError(f'{name} does not show the expected {state} client/server pair; recapture')
tids.append(observed)
inputs[name] = hashlib.sha256((capture_dir / name).read_bytes()).hexdigest()
for name in ['platform.txt', 'probe.log', 'waitlens_probe']:
inputs[name] = hashlib.sha256((capture_dir / name).read_bytes()).hexdigest()
if not tids[0] & tids[1]:
raise ValueError('SEND and REPLY must show the same client TID; recapture')
return {'schema_version': 1, 'status': 'synthetic-control-flow-only' if synthetic else 'consistent-local-evidence',
'independently_authenticated': False, 'runtime_claim': 'Owner-supplied QNX 8.0 capture bundle',
'source_sha256': source_hash, 'binary_sha256': receipt['binary_sha256'],
'capture_sha256': inputs,
'limitation': 'Consistency checks cannot authenticate an OS or replace screen footage and reviewer reproduction.'}
def main(argv=None):
parser = argparse.ArgumentParser(description=__doc__)
parser.add_argument('--build-dir', type=Path, default=ROOT / 'build/qnx')
parser.add_argument('--capture-dir', type=Path, required=True)
parser.add_argument('--out', type=Path)
args = parser.parse_args(argv)
try:
result = check(args.build_dir, args.capture_dir)
data = json.dumps(result, indent=2) + '\n'
if args.out:
protected = [args.build_dir / 'build-receipt.json', args.build_dir / 'waitlens_probe']
protected += [args.capture_dir / n for n in ['platform.txt', 'probe.log', 'waitlens_probe', 'send.txt', 'reply.txt']]
if args.out.resolve() in {p.resolve() for p in protected}:
raise ValueError('output must not overwrite an evidence input')
args.out.parent.mkdir(parents=True, exist_ok=True)
args.out.write_text(data, encoding='utf-8')
else:
print(data, end='')
return 0
except (OSError, ValueError, InputError, KeyError, TypeError) as error:
print('Evidence check failed: ' + str(error), file=sys.stderr)
return 2
if __name__ == '__main__':
raise SystemExit(main())