Files
philips-tv-speicher-voll/patches/baseline/inspect_apk_signing_block.py

36 lines
1.2 KiB
Python

#!/usr/bin/env python3
"""List APK Signing Block pair IDs without changing the APK."""
import struct
import sys
from pathlib import Path
NAMES = {
0x7109871A: "APK Signature Scheme v2",
0xF05368C0: "APK Signature Scheme v3",
0x1B93AD61: "APK Signature Scheme v3.1",
0x6DFF800D: "Source stamp",
0x42726577: "verity padding",
}
for arg in sys.argv[1:]:
path = Path(arg)
data = path.read_bytes()
eocd = data.rfind(b"PK\x05\x06")
if eocd < 0:
raise SystemExit(f"{path}: EOCD not found")
central = struct.unpack_from("<I", data, eocd + 16)[0]
size, magic = struct.unpack_from("<Q16s", data, central - 24)
if magic != b"APK Sig Block 42":
print(f"{path}: no APK Signing Block")
continue
start = central - size - 8
assert struct.unpack_from("<Q", data, start)[0] == size
pos, end, ids = start + 8, central - 24, []
while pos < end:
length = struct.unpack_from("<Q", data, pos)[0]
pair_id = struct.unpack_from("<I", data, pos + 8)[0]
ids.append((pair_id, NAMES.get(pair_id, "unknown")))
pos += 8 + length
print(f"{path}: " + ", ".join(f"0x{i:08x} {n}" for i, n in ids))