Review: enroll keeps foreign db keys, configurable paths, on-demand eac-run, launcher ebuild, db dedupe tool
This commit is contained in:
+33
-1
@@ -2,10 +2,13 @@
|
||||
"""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 `sbctl enroll-keys --export esl`).
|
||||
(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
|
||||
@@ -39,6 +42,26 @@ def entries(b):
|
||||
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:]
|
||||
@@ -49,12 +72,21 @@ def selftest():
|
||||
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")
|
||||
|
||||
Reference in New Issue
Block a user