Skip to content

Commit c6fc63f

Browse files
authored
Reuse system TPM2TOOLS_TCTI envvar if it is set (#270)
* Flush tpm context cache after each TPM2 operation Bare TPM2 API handles only small amount of active contexts. Usually 3 at a time. Use of more contexts requires a TPM2 resource manager that transparently loads/saves contexts if needed. Linux kernel has a built-in tpm2 resource manager that gets activated when one opens '/dev/tpmrmXXX' (in contrast to regular '/dev/tpmXXX' dev file). But some TPM2 toolset, such as swtpm emulator does not provide a default resource manager. And using the Linux kernel module with swtpm is a bit of pain. Handle context management by the script itself rather than relying on existing resource manager. * Reuse system TPM2TOOLS_TCTI envvar if it is set This is a way to customize TPM used for clevis binding/unbinding. For example for tests against a TPM software emulator: TPM2TOOLS_TCTI=swtpm clevis encrypt tpm2 '{}' <<< 'hello, world' Closes #244
1 parent 92b09c9 commit c6fc63f

File tree

2 files changed

+49
-38
lines changed

2 files changed

+49
-38
lines changed

src/pins/tpm2/clevis-decrypt-tpm2

+24-19
Original file line numberDiff line numberDiff line change
@@ -54,26 +54,28 @@ if [[ $TPM2TOOLS_VERSION -lt 3 ]] || [[ $TPM2TOOLS_VERSION -gt 5 ]]; then
5454
exit 1
5555
fi
5656

57-
# Old environment variables for tpm2-tools 3.0
58-
export TPM2TOOLS_TCTI_NAME=device
59-
export TPM2TOOLS_DEVICE_FILE=
60-
for dev in /dev/tpmrm?; do
61-
[ -e "$dev" ] || continue
62-
TPM2TOOLS_DEVICE_FILE="$dev"
63-
break
64-
done
65-
66-
# New environment variable for tpm2-tools >= 3.1
67-
export TPM2TOOLS_TCTI="$TPM2TOOLS_TCTI_NAME:$TPM2TOOLS_DEVICE_FILE"
68-
69-
if [ -z "$TPM2TOOLS_DEVICE_FILE" ]; then
70-
echo "A TPM2 device with the in-kernel resource manager is needed!" >&2
71-
exit 1
72-
fi
57+
if [ -z "$TPM2TOOLS_TCTI" ]; then
58+
# Old environment variables for tpm2-tools 3.0
59+
export TPM2TOOLS_TCTI_NAME=device
60+
export TPM2TOOLS_DEVICE_FILE=
61+
for dev in /dev/tpmrm?; do
62+
[ -e "$dev" ] || continue
63+
TPM2TOOLS_DEVICE_FILE="$dev"
64+
break
65+
done
66+
67+
# New environment variable for tpm2-tools >= 3.1
68+
export TPM2TOOLS_TCTI="$TPM2TOOLS_TCTI_NAME:$TPM2TOOLS_DEVICE_FILE"
69+
70+
if [ -z "$TPM2TOOLS_DEVICE_FILE" ]; then
71+
echo "A TPM2 device with the in-kernel resource manager is needed!" >&2
72+
exit 1
73+
fi
7374

74-
if ! [[ -r "$TPM2TOOLS_DEVICE_FILE" && -w "$TPM2TOOLS_DEVICE_FILE" ]]; then
75-
echo "The $TPM2TOOLS_DEVICE_FILE device must be readable and writable!" >&2
76-
exit 1
75+
if ! [[ -r "$TPM2TOOLS_DEVICE_FILE" && -w "$TPM2TOOLS_DEVICE_FILE" ]]; then
76+
echo "The $TPM2TOOLS_DEVICE_FILE device must be readable and writable!" >&2
77+
exit 1
78+
fi
7779
fi
7880

7981
read -r -d . hdr
@@ -143,6 +145,7 @@ if [ -n "$fail" ]; then
143145
echo "Creating TPM2 primary key failed!" >&2
144146
exit 1
145147
fi
148+
tpm2_flushcontext -t
146149

147150
case "$TPM2TOOLS_VERSION" in
148151
3) tpm2_load -Q -c "$TMP"/primary.context -u "$TMP"/jwk.pub -r "$TMP"/jwk.priv \
@@ -155,6 +158,7 @@ if [ -n "$fail" ]; then
155158
echo "Loading jwk to TPM2 failed!" >&2
156159
exit 1
157160
fi
161+
tpm2_flushcontext -t
158162

159163
case "$TPM2TOOLS_VERSION" in
160164
3) jwk="$(tpm2_unseal -c "$TMP"/load.context ${pcr_spec:+-L $pcr_spec})" || fail=$?;;
@@ -165,6 +169,7 @@ if [ -n "$fail" ]; then
165169
echo "Unsealing jwk from TPM failed!" >&2
166170
exit 1
167171
fi
172+
tpm2_flushcontext -t
168173

169174
(echo -n "$jwk$hdr."; /bin/cat) | jose jwe dec -k- -i-
170175
exit $?

src/pins/tpm2/clevis-encrypt-tpm2

+25-19
Original file line numberDiff line numberDiff line change
@@ -97,26 +97,28 @@ if [[ $TPM2TOOLS_VERSION -lt 3 ]] || [[ $TPM2TOOLS_VERSION -gt 5 ]]; then
9797
exit 1
9898
fi
9999

100-
# Old environment variables for tpm2-tools 3.0
101-
export TPM2TOOLS_TCTI_NAME=device
102-
export TPM2TOOLS_DEVICE_FILE=
103-
for dev in /dev/tpmrm?; do
104-
[ -e "$dev" ] || continue
105-
TPM2TOOLS_DEVICE_FILE="$dev"
106-
break
107-
done
108-
109-
# New environment variable for tpm2-tools >= 3.1
110-
export TPM2TOOLS_TCTI="$TPM2TOOLS_TCTI_NAME:$TPM2TOOLS_DEVICE_FILE"
111-
112-
if [ -z "$TPM2TOOLS_DEVICE_FILE" ]; then
113-
echo "A TPM2 device with the in-kernel resource manager is needed!" >&2
114-
exit 1
115-
fi
100+
if [ -z "$TPM2TOOLS_TCTI" ]; then
101+
# Old environment variables for tpm2-tools 3.0
102+
export TPM2TOOLS_TCTI_NAME=device
103+
export TPM2TOOLS_DEVICE_FILE=
104+
for dev in /dev/tpmrm?; do
105+
[ -e "$dev" ] || continue
106+
TPM2TOOLS_DEVICE_FILE="$dev"
107+
break
108+
done
109+
110+
# New environment variable for tpm2-tools >= 3.1
111+
export TPM2TOOLS_TCTI="$TPM2TOOLS_TCTI_NAME:$TPM2TOOLS_DEVICE_FILE"
112+
113+
if [ -z "$TPM2TOOLS_DEVICE_FILE" ]; then
114+
echo "A TPM2 device with the in-kernel resource manager is needed!" >&2
115+
exit 1
116+
fi
116117

117-
if ! [[ -r "$TPM2TOOLS_DEVICE_FILE" && -w "$TPM2TOOLS_DEVICE_FILE" ]]; then
118-
echo "The $TPM2TOOLS_DEVICE_FILE device must be readable and writable!" >&2
119-
exit 1
118+
if ! [[ -r "$TPM2TOOLS_DEVICE_FILE" && -w "$TPM2TOOLS_DEVICE_FILE" ]]; then
119+
echo "The $TPM2TOOLS_DEVICE_FILE device must be readable and writable!" >&2
120+
exit 1
121+
fi
120122
fi
121123

122124
if ! cfg="$(jose fmt -j "$1" -Oo- 2>/dev/null)"; then
@@ -187,6 +189,7 @@ if [ -n "$fail" ]; then
187189
echo "Creating TPM2 primary key failed!" >&2
188190
exit 1
189191
fi
192+
tpm2_flushcontext -t
190193

191194
policy_options=()
192195
if [ -n "$pcr_ids" ]; then
@@ -200,6 +203,7 @@ if [ -n "$pcr_ids" ]; then
200203
echo "Creating PCR hashes file failed!" >&2
201204
exit 1
202205
fi
206+
tpm2_flushcontext -t
203207
else
204208
if ! jose b64 dec -i- -O "$TMP"/pcr.digest <<< "$pcr_digest"; then
205209
echo "Error decoding PCR digest!" >&2
@@ -218,6 +222,7 @@ if [ -n "$pcr_ids" ]; then
218222
echo "create policy fail, please check the environment or parameters!"
219223
exit 1
220224
fi
225+
tpm2_flushcontext -t
221226

222227
policy_options+=(-L "$TMP/pcr.policy")
223228
else
@@ -235,6 +240,7 @@ if [ -n "$fail" ]; then
235240
echo "Creating TPM2 object for jwk failed!" >&2
236241
exit 1
237242
fi
243+
tpm2_flushcontext -t
238244

239245
if ! jwk_pub="$(jose b64 enc -I "$TMP"/jwk.pub)"; then
240246
echo "Encoding jwk.pub in Base64 failed!" >&2

0 commit comments

Comments
 (0)