-
Notifications
You must be signed in to change notification settings - Fork 9
/
Copy pathtest-tpm2key.sh
executable file
·125 lines (102 loc) · 2.52 KB
/
test-tpm2key.sh
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
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
#!/bin/bash
#
# This script needs to be run with root privilege
#
# TESTDIR=policy.test
PCR_MASK=0,2,4,12
pcr_oracle=pcr-oracle
if [ -x pcr-oracle ]; then
pcr_oracle=$PWD/pcr-oracle
fi
function call_oracle {
echo "****************"
echo "pcr-oracle $*"
$pcr_oracle --target-platform tpm2.0 -d "$@"
}
if [ -z "$TESTDIR" ]; then
tmpdir=$(mktemp -d /tmp/pcrtestXXXXXX)
trap "cd / && rm -rf $tmpdir" 0 1 2 10 11 15
TESTDIR=$tmpdir
fi
trap "echo 'FAIL: command exited with error'; exit 1" ERR
echo "This is super secret" >$TESTDIR/secret
set -e
cd $TESTDIR
echo "Seal the secret with PCR policy"
call_oracle \
--from current \
--input secret \
--output sealed \
seal-secret $PCR_MASK
echo "Unseal the sealed with PCR policy"
call_oracle \
--input sealed \
--output recovered \
unseal-secret
if ! cmp secret recovered; then
echo "BAD: Unable to recover original secret"
echo "Secret:"
od -tx1c secret
echo "Recovered:"
od -tx1c recovered
exit 1
else
echo "NICE: we were able to recover the original secret"
fi
rm -f sealed recovered
call_oracle \
--rsa-generate-key \
--private-key policy-key.pem \
--auth authorized.policy \
create-authorized-policy $PCR_MASK
call_oracle \
--private-key policy-key.pem \
--public-key policy-pubkey \
store-public-key
call_oracle \
--auth authorized.policy \
--input secret \
--output sealed \
seal-secret
for attempt in first second; do
echo "Sign the set of PCRs we want to authorize"
call_oracle \
--policy-name "authorized-policy-test" \
--private-key policy-key.pem \
--from current \
--input sealed \
--output sealed-signed \
sign $PCR_MASK
echo "$attempt attempt to unseal the secret"
call_oracle \
--input sealed-signed \
--output recovered \
unseal-secret
if ! cmp secret recovered; then
echo "BAD: Unable to recover original secret"
echo "Secret:"
od -tx1c secret
echo "Recovered:"
od -tx1c recovered
exit 1
else
echo "NICE: we were able to recover the original secret"
fi
if [ "$attempt" = "second" ]; then
break
fi
echo "Extend PCR 12. Unsealing should fail afterwards"
tpm2_pcrextend 12:sha256=21d2013e3081f1e455fdd5ba6230a8620c3cfc9a9c31981d857fe3891f79449e
rm -f recovered
call_oracle \
--input sealed-signed \
--output recovered \
unseal-secret || true
if [ -s recovered ] && ! cmp secret recovered; then
echo "BAD: We were still able to recover the original secret. Something stinks"
exit 1
else
echo "GOOD: After changing a PCR, the secret can no longer be unsealed"
fi
echo "Now recreate the signed PCR policy"
done