Skip to content

Commit f8e265a

Browse files
committed
bootscript: Use send-to-tpm for EK and TK methods
1 parent 75fb767 commit f8e265a

File tree

1 file changed

+3
-162
lines changed

1 file changed

+3
-162
lines changed

initramfs/bootscript

Lines changed: 3 additions & 162 deletions
Original file line numberDiff line numberDiff line change
@@ -46,169 +46,10 @@ if [[ -x $STARTUP ]]; then
4646
. "$STARTUP" || die "startup failed"
4747
fi
4848

49-
tpm2_ek_session() {
50-
tpm2 flushcontext -t
51-
tpm2 flushcontext -s
52-
tpm2 startauthsession \
53-
--session "ek-session.ctx" \
54-
--policy-session \
55-
|| die "tpm2: unable to start policy session"
56-
tpm2 policysecret \
57-
--session "ek-session.ctx" \
58-
--object-context endorsement \
59-
|| die "tpm2: unable to set policysecret for endorsement hierarchy"
60-
}
61-
tpm2_tk_session() {
62-
tpm2 startauthsession \
63-
--session "tk-session.ctx" \
64-
--policy-session \
65-
|| die "tpm2: unable to start tk-policy session"
66-
tpm2 policypcr \
67-
--session "tk-session.ctx" \
68-
--policy "tk-policy.dat" \
69-
--pcr-list="sha256:$POLICY_PCR" \
70-
|| die "tpm2: unable to load policy pcr"
71-
}
72-
73-
try_tk=false
74-
[[ -r "$TMPDIR/tk.seed" ]] && try_tk=true
75-
76-
cat > "$TMPDIR/wkpriv.pem" <<EOF
77-
-----BEGIN PRIVATE KEY-----
78-
MIG2AgEAMBAGByqGSM49AgEGBSuBBAAiBIGeMIGbAgEBBDAlMnCWue7CfXjNLibH
79-
PTJrsOLUcoxqU3FLWYEWMI+HuPnzcwwl7SkKN6cpf4H3oQihZANiAAQ1pw6D5QVw
80-
vymljYVDyrUriOet8zPB/9tq9XJ7A54qsVkaVufAuEJ6GIvD4xUZ27manMosJADS
81-
aW2TLJkwxecRh2eTwPtSx2U32M2/yHeuWRV/0juiIozefPsTAlHAi3E=
82-
-----END PRIVATE KEY-----
83-
EOF
84-
85-
# Try using TPM2_ActivateCredential to recover the rootfs key
8649
if [[ -f $TMPDIR/rootfs.key.symkeyenc ]]; then
87-
# See tpm-receive for the theory and details of what we're up to here.
88-
# This is a simplified version of that. Briefly: we're calling
89-
#
90-
# TPM2_ActivateCredential(EK, WK, blob, secret)
91-
#
92-
# where WK is the handle of a "well-known" private key with a
93-
# well-known policy and attributes. We're using MakeCredential as a
94-
# form of asymmetric key encryption with sender-asserted policy, and
95-
# we're relying on ActivateCredential to enforce that policy because
96-
# it's evaluated by a TPM.
97-
98-
# Compute the policy hash for the WK, if the server didn't tell us it
99-
# (yes, we can hard-code it here).
100-
if [[ ! -f ${TMPDIR}/activate-policy.dat ]]; then
101-
tpm2 flushcontext --transient-object
102-
tpm2 flushcontext --loaded-session
103-
tpm2 startauthsession --session "${TMPDIR}/trialsession.ctx"
104-
tpm2 policypcr --session "${TMPDIR}/trialsession.ctx" \
105-
--pcr-list="sha256:$POLICY_PCR"
106-
tpm2 policycommandcode \
107-
--session "${TMPDIR}/trialsession.ctx" \
108-
-L "${TMPDIR}/activate-policy.dat" \
109-
TPM2_CC_ActivateCredential
110-
111-
tpm2 flushcontext --saved-session
112-
fi
113-
114-
# Load the WK
115-
tpm2 flushcontext --transient-object
116-
tpm2 flushcontext --loaded-session
117-
tpm2 loadexternal \
118-
--hierarchy n \
119-
--key-algorithm ecc \
120-
--private "${TMPDIR}/wkpriv.pem" \
121-
--policy "${TMPDIR}/activate-policy.dat" \
122-
--attributes "sign|adminwithpolicy" \
123-
--key-context "${TMPDIR}/wk.ctx"
124-
125-
# Get the EK, make a session for it, and execute its policy
126-
tpm2 flushcontext --transient-object
127-
tpm2 flushcontext --loaded-session
128-
tpm2 createek --ek-context "$TMPDIR/ek.ctx"
129-
tpm2 flushcontext --transient-object
130-
tpm2 startauthsession --session "${TMPDIR}/sessionek.ctx" --policy-session
131-
tpm2 policysecret --session "${TMPDIR}/sessionek.ctx" --object-context endorsement
132-
133-
# Make a session for the wk and execute its policy
134-
tpm2 flushcontext --transient-object
135-
tpm2 flushcontext --loaded-session
136-
tpm2 startauthsession --session "${TMPDIR}/sessionwk.ctx" --policy-session
137-
tpm2 policypcr --session "${TMPDIR}/sessionwk.ctx" --pcr-list="sha256:$POLICY_PCR"
138-
tpm2 policycommandcode --session "${TMPDIR}/sessionwk.ctx" TPM2_CC_ActivateCredential
139-
140-
# Finally, decrypt via ActivateCredential
141-
if tpm2 activatecredential \
142-
--credentialedkey-context "${TMPDIR}/wk.ctx" \
143-
--credentialedkey-auth session:"${TMPDIR}/sessionwk.ctx" \
144-
--credentialkey-context "${TMPDIR}/ek.ctx" \
145-
--credentialkey-auth session:"${TMPDIR}/sessionek.ctx" \
146-
--credential-blob "$TMPDIR/rootfs.key.symkeyenc" \
147-
-o "$TMPDIR/rootfs.key.symkey"; then
148-
149-
# Success. We have a small, symmetric key, so use it to
150-
# authenticate and decrypt the plaintext.
151-
aead_decrypt "$TMPDIR/rootfs.key.enc" \
152-
"$TMPDIR/rootfs.key.symkey" \
153-
"$TMPDIR/rootfs.key"
154-
155-
try_tk=false
156-
else
157-
stat=$?
158-
159-
# If we have a TK, well, try that.
160-
$try_tk || (exit $stat)
161-
fi
162-
fi
163-
if $try_tk; then
164-
# attempt to load the secret wrapping key into our TPM
165-
# as a transient object
166-
tpm2 flushcontext --transient-object
167-
tpm2 flushcontext --loaded-session
168-
tpm2 createek --ek-context ek.ctx \
169-
|| die "tpm2: unable to create ek object"
170-
171-
warn "tpm2: Importing duplicate transport key"
172-
tpm2_ek_session
173-
174-
tpm2 import \
175-
--parent-context ek.ctx \
176-
--parent-auth "session:ek-session.ctx" \
177-
--key-algorithm rsa \
178-
--input "$TMPDIR/tk.dpriv" \
179-
--seed "$TMPDIR/tk.seed" \
180-
--public "$TMPDIR/tk.pub" \
181-
--private "$TMPDIR/tk.priv" \
182-
|| die "tpm2: unable to import duplicate transport key object"
183-
184-
warn "tpm2: Loading duplicate transport key"
185-
tpm2_ek_session
186-
tpm2 load \
187-
--parent-context ek.ctx \
188-
--auth "session:ek-session.ctx" \
189-
--key-context "$TMPDIR/tk.ctx" \
190-
--public "$TMPDIR/tk.pub" \
191-
--private "$TMPDIR/tk.priv" \
192-
|| die "tpm2: unable to load duplicate transport key object"
193-
194-
# attempt to decrypt the rootfs key with the transport key
195-
warn "tpm2: Decrypting rootfs key"
196-
tpm2_tk_session
197-
tpm2 rsadecrypt \
198-
--auth "session:tk-session.ctx" \
199-
--key-context "$TMPDIR/tk.ctx" \
200-
< "$TMPDIR/rootfs.symkeyenc" \
201-
> "$TMPDIR/rootfs.symkey" \
202-
|| die "tpm2: unable to decrypt the rootfs key with transport key"
203-
204-
aead_decrypt "$TMPDIR/rootfs.enc" \
205-
"$TMPDIR/rootfs.symkey" \
206-
"$TMPDIR/rootfs.key" \
207-
|| die "unable to decrypt the rootfs key"
208-
209-
# extend PCR11 so that the policy can not be satisfied
210-
echo "UNLOCKED" | tpm2 pcrevent "$POLICY_PCR" \
211-
|| die "tmp2: unable to extend PCR$POLICY_PCR"
50+
/safeboot/sbin/tpm-receive "$KEY.symkeyenc" "$KEY.symkey" \
51+
tpm2 policypcr --pcr-list="sha256:$POLICY_PCR"
52+
aead_decrypt "$KEY.enc" "$KEY.symkey" "$KEY"
21253
fi
21354

21455
# If we're testing, stop here.

0 commit comments

Comments
 (0)