Files

95 lines
3.5 KiB
Python
Executable File

#!/usr/bin/env python3
"""List the entries of UEFI signature lists and count duplicates.
Usage: efi-sig-list.py FILE...
efi-sig-list.py --unique OUT.esl FILE
FILE = an efivarfs dump (/sys/firmware/efi/efivars/{PK,KEK,db,dbx}-<guid> or a
`cat` copy; the 4-byte attribute prefix is skipped) or a plain `.esl` file
(e.g. from `efi-readvar -v db -o db.esl`).
Prints per entry: sha256 prefix, subject CN, issuer CN; then entries vs unique.
--unique writes FILE's signature lists to OUT.esl with every signature once (first
occurrence, original order and owner); nothing else is dropped.
No arguments runs a self-check.
"""
import hashlib
import struct
import subprocess
import sys
X509 = bytes.fromhex("a159c0a5e494a74a87b5ab155c2bf072")
SHA256 = bytes.fromhex("2616c4c14c509240aca941f936934328")
def cn(der, field):
out = subprocess.run(["openssl", "x509", "-inform", "DER", "-noout", f"-{field}", "-nameopt", "multiline"],
input=der, capture_output=True).stdout.decode()
return next((l.split("=", 1)[1].strip() for l in out.splitlines() if "commonName" in l), "?")
def entries(b):
i = 0
while i + 28 <= len(b):
stype = b[i:i + 16]
lsize, hsize, ssize = struct.unpack_from("<III", b, i + 16)
if lsize < 28:
break
j = i + 28 + hsize
while j + ssize <= i + lsize:
data = b[j + 16:j + ssize] # skip the owner GUID
fp = hashlib.sha256(data).hexdigest()[:12]
yield (fp, cn(data, "subject"), cn(data, "issuer")) if stype == X509 else (fp, "hash", "")
j += ssize
i += lsize
def unique(b):
seen, out, i = set(), b"", 0
while i + 28 <= len(b):
stype = b[i:i + 16]
lsize, hsize, ssize = struct.unpack_from("<III", b, i + 16)
if lsize < 28:
break
header, keep, j = b[i + 28:i + 28 + hsize], [], i + 28 + hsize
while j + ssize <= i + lsize:
sig = b[j:j + ssize]
if (stype, sig[16:]) not in seen:
seen.add((stype, sig[16:]))
keep.append(sig)
j += ssize
if keep:
out += stype + struct.pack("<III", 28 + hsize + len(keep) * ssize, hsize, ssize) + header + b"".join(keep)
i += lsize
return out
def load(path):
blob = open(path, "rb").read()
return blob if path.endswith(".esl") else blob[4:]
def selftest():
sig = b"\0" * 16 + b"\x11" * 32
lst = SHA256 + struct.pack("<III", 28 + 2 * len(sig), 0, len(sig)) + 2 * sig
found = list(entries(lst))
assert len(found) == 2 and len({f[0] for f in found}) == 1
other = SHA256 + struct.pack("<III", 28 + len(sig), 0, len(sig)) + b"\1" * 16 + b"\x22" * 32
once = unique(lst + other + lst)
assert [f[0] for f in entries(once)] == [found[0][0], hashlib.sha256(b"\x22" * 32).hexdigest()[:12]]
assert once == SHA256 + struct.pack("<III", 28 + len(sig), 0, len(sig)) + sig + other
print("selftest ok")
if __name__ == "__main__":
if len(sys.argv) == 1:
selftest()
if sys.argv[1:2] == ["--unique"]:
if len(sys.argv) != 4:
sys.exit("usage: efi-sig-list.py --unique OUT.esl FILE")
open(sys.argv[2], "wb").write(unique(load(sys.argv[3])))
sys.argv[1:] = [sys.argv[2]]
for path in sys.argv[1:]:
found = list(entries(load(path)))
print(f"{path}: {len(found)} entries, {len({f[0] for f in found})} unique")
for fp, subject, issuer in found:
print(f" {fp} {subject:<40} issued by {issuer}")