forked from osresearch/safeboot
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtpm2-pcr-validate
executable file
·63 lines (53 loc) · 1.58 KB
/
tpm2-pcr-validate
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
#!/usr/bin/env python3
# Validate that the parsed event log and the quoted PCRs both match
# the expected PCRs. Only the PCRs in the expected list will be compared
# to the quote and event log.
#
# Usage:
# tpm2-pcr-validate expected.txt [quote.txt [events.txt ... ]]
#
import sys
import hashlib
from binascii import unhexlify, hexlify
from yaml import load, dump
try:
from yaml import CLoader as Loader, CDumper as Dumper
except ImportError:
from yaml import Loader, Dumper
if len(sys.argv) <= 1:
print("Usage: tpm2-pcr-validate expected.txt quote.txt events.txt", file=sys.stderr)
exit(-1)
# hard code the algorithm
alg = 'sha256'
with open(sys.argv[1]) as f:
pcrs = load(f, Loader=Loader)["pcrs"][alg]
fail = False
verbose = False
pcr_list = []
for pcr in pcrs:
good_pcr = pcrs[pcr]
pcr_list.append(str(pcr))
if verbose:
print("PCR%d: %x" % (pcr, good_pcr), file=sys.stderr)
pcr_list = "%s:%s" % (alg, ",".join(pcr_list))
for filename in sys.argv[2:]:
with open(filename) as f:
if verbose:
print("%s: Reading PCRs" % (filename))
quote = load(f, Loader=Loader)["pcrs"][alg]
for pcr in pcrs:
good_pcr = pcrs[pcr]
if not pcr in quote:
print("%s: PCR%d missing" % (filename, pcr), file=sys.stderr)
fail = True
elif good_pcr != quote[pcr]:
print("%s: PCR%d mismatch %x" % (filename, pcr, quote[pcr]), file=sys.stderr)
fail = True
elif verbose:
print("%s: PCR%d: %s matches %s" % (filename, pcr, quote[pcr], good_pcr))
if fail:
print("%s: FAILED VALIDATION" % (pcr_list), file=sys.stderr)
exit(-1)
else:
print("%s: Valid" % (pcr_list), file=sys.stderr)
exit(0)