File: tpm2_key_protector_test.in

package info (click to toggle)
grub2 2.14-1
  • links: PTS, VCS
  • area: main
  • in suites:
  • size: 80,808 kB
  • sloc: ansic: 551,284; asm: 68,074; sh: 9,844; cpp: 2,095; makefile: 1,895; python: 1,546; sed: 450; lex: 393; yacc: 268; awk: 85; lisp: 54; perl: 31
file content (425 lines) | stat: -rw-r--r-- 10,972 bytes parent folder | download | duplicates (2)
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
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
#! @BUILD_SHEBANG@ -e

# Test GRUBs ability to unseal a LUKS key with TPM 2.0
# Copyright (C) 2024 Free Software Foundation, Inc.
#
# GRUB is free software: you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation, either version 3 of the License, or
# (at your option) any later version.
#
# GRUB is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with GRUB.  If not, see <http://www.gnu.org/licenses/>.

grubshell=@builddir@/grub-shell

. "@builddir@/grub-core/modinfo.sh"

if [ x${grub_modinfo_platform} != xemu ]; then
  exit 77
fi

builddir="@builddir@"

# Force build directory components
PATH="${builddir}:${PATH}"
export PATH

if [ "x${EUID}" = "x" ] ; then
  EUID=`id -u`
fi

if [ "${EUID}" != 0 ] ; then
   echo "not root; cannot test tpm2."
   exit 99
fi

if ! command -v cryptsetup >/dev/null 2>&1; then
   echo "cryptsetup not installed; cannot test tpm2."
   exit 99
fi

if ! grep -q tpm_vtpm_proxy /proc/modules && ! modprobe tpm_vtpm_proxy; then
   echo "no tpm_vtpm_proxy support; cannot test tpm2."
   exit 99
fi

if ! command -v swtpm >/dev/null 2>&1; then
   echo "swtpm not installed; cannot test tpm2."
   exit 99
fi

if ! command -v tpm2_startup >/dev/null 2>&1; then
   echo "tpm2-tools not installed; cannot test tpm2."
   exit 99
fi

tpm2testdir="`mktemp -d "${TMPDIR:-/tmp}/$(basename "$0").XXXXXXXXXX"`" || exit 99

disksize=20M

luksfile=${tpm2testdir}/luks.disk
lukskeyfile=${tpm2testdir}/password.txt

# Choose a low iteration number to reduce the time to decrypt the disk
csopt="--type luks2 --pbkdf pbkdf2 --iter-time 1000"

tpm2statedir=${tpm2testdir}/tpm
tpm2ctrl=${tpm2statedir}/ctrl
tpm2log=${tpm2statedir}/logfile

sealedkey=${tpm2testdir}/sealed.tpm

timeout=20

testoutput=${tpm2testdir}/testoutput

vtext="TEST VERIFIED"

ret=0

# Create the password file
echo -n "top secret" > "${lukskeyfile}"

# Setup LUKS2 image
truncate -s ${disksize} "${luksfile}" || exit 99
cryptsetup luksFormat -q ${csopt} "${luksfile}" "${lukskeyfile}" || exit 99

# Write vtext into the first block of the LUKS2 image
luksdev=/dev/mapper/`basename "${tpm2testdir}"`
cryptsetup open --key-file "${lukskeyfile}" "${luksfile}" `basename "${luksdev}"` || exit 99
echo "${vtext}" > "${luksdev}"
cryptsetup close "${luksdev}"

# Shutdown the swtpm instance on exit
cleanup() {
    RET=$?
    if [ -e "${tpm2ctrl}" ]; then
	swtpm_ioctl -s --unix "${tpm2ctrl}"
    fi
    if [ "${RET}" -eq 0 ]; then
	rm -rf "$tpm2testdir" || :
    fi
}
trap cleanup EXIT INT TERM KILL QUIT

mkdir -p "${tpm2statedir}"

# Create the swtpm chardev instance
swtpm chardev --vtpm-proxy --tpmstate dir="${tpm2statedir}" \
	--tpm2 --ctrl type=unixio,path="${tpm2ctrl}" \
	--flags startup-clear --daemon > "${tpm2log}" || ret=$?
if [ "${ret}" -ne 0 ]; then
    echo "Failed to start swtpm chardev: ${ret}" >&2
    exit 99
fi

# Wait for tpm2 chardev
tpm2timeout=${GRUB_TEST_SWTPM_DEFAULT_TIMEOUT:-3}
for count in `seq 1 ${tpm2timeout}`; do
    sleep 1

    tpm2dev=$(grep "New TPM device" "${tpm2log}" | cut -d' ' -f 4)
    if [ -c "${tpm2dev}" ]; then
	break
    elif [ "${count}" -eq "${tpm2timeout}" ]; then
	echo "TPM device did not appear." >&2
	exit 99
    fi
done

# Export the TCTI variable for tpm2-tools
export TPM2TOOLS_TCTI="device:${tpm2dev}"

# Check if the sha384 bank is available
if [ "$(tpm2_getcap pcrs | grep sha384)" != "" ]; then
    with_sha384=true
fi

# Extend PCR 0
tpm2_pcrextend 0:sha256=$(echo "test0" | sha256sum | cut -d ' ' -f 1) || exit 99
if [ "${with_sha384}" = "true" ]; then
    tpm2_pcrextend 0:sha384=$(echo "test0" | sha384sum | cut -d ' ' -f 1) || exit 99
fi

# Extend PCR 1
tpm2_pcrextend 1:sha256=$(echo "test1" | sha256sum | cut -d ' ' -f 1) || exit 99
if [ "${with_sha384}" = "true" ]; then
    tpm2_pcrextend 1:sha384=$(echo "test1" | sha384sum | cut -d ' ' -f 1) || exit 99
fi

tpm2_seal_unseal() {
    srk_alg="$1"
    handle_type="$2"
    srk_test="$3"
    pcr_bank="$4"

    grub_srk_alg=${srk_alg}

    extra_opt=""
    extra_grub_opt=""

    persistent_handle="0x81000000"

    grub_cfg=${tpm2testdir}/testcase.cfg

    if [ "${handle_type}" = "persistent" ]; then
	extra_opt="--tpm2-srk=${persistent_handle}"
    fi

    if [ "${srk_alg}" != "default" ]; then
	extra_opt="${extra_opt} --tpm2-asymmetric=${srk_alg}"
    fi

    # Seal the password with grub-protect
    grub-protect ${extra_opt} \
	--tpm2-device="${tpm2dev}" \
	--action=add \
	--protector=tpm2 \
	--tpm2key \
	--tpm2-bank="${pcr_bank}" \
	--tpm2-pcrs=0,1 \
	--tpm2-keyfile="${lukskeyfile}" \
	--tpm2-outfile="${sealedkey}" || ret=$?
    if [ "${ret}" -ne 0 ]; then
	echo "Failed to seal the secret key: ${ret}" >&2
	return 99
    fi

    # Flip the asymmetric algorithm in grub.cfg to trigger fallback SRKs
    if [ "${srk_test}" = "fallback_srk" ]; then
	if [ -z "${srk_alg##RSA*}" ]; then
	    grub_srk_alg="ECC"
	elif [ -z "${srk_alg##ECC*}" ]; then
	    grub_srk_alg="RSA"
	fi
    fi

    if [ "${grub_srk_alg}" != "default" ] && [ "${handle_type}" != "persistent" ]; then
	extra_grub_opt="-a ${grub_srk_alg}"
    fi

    # Write the TPM unsealing script
    cat > "${grub_cfg}" <<EOF
loopback luks (host)${luksfile}
tpm2_key_protector_init -T (host)${sealedkey} ${extra_grub_opt}
if cryptomount -a --protector tpm2; then
    cat (crypto0)+1
fi
EOF

    # Test TPM unsealing with the same PCR
    ${grubshell} --timeout=${timeout} --emu-opts="-t ${tpm2dev}" < "${grub_cfg}" > "${testoutput}" || ret=$?

    # Remove the persistent handle
    if [ "${handle_type}" = "persistent" ]; then
	grub-protect \
		--tpm2-device="${tpm2dev}" \
		--protector=tpm2 \
		--action=remove \
		--tpm2-srk=${persistent_handle} \
		--tpm2-evict || :
    fi

    if [ "${ret}" -eq 0 ]; then
	if ! grep -q "^${vtext}$" "${testoutput}"; then
	    echo "error: test not verified [`cat ${testoutput}`]" >&2
	    return 1
	fi
    else
	echo "grub-emu exited with error: ${ret}" >&2
	return 99
    fi
}

tpm2_seal_unseal_nv() {
    handle_type="$1"
    key_type="$2"
    pcr_bank="$3"

    extra_opt=""
    extra_grub_opt=""

    if [ "$handle_type" = "nvindex" ]; then
	nv_index="0x1000000"
    else
	nv_index="0x81000000"
    fi

    if [ "$key_type" = "tpm2key" ]; then
	extra_opt="--tpm2key"
    else
	extra_grub_opt="--pcrs=0,1 -b ${pcr_bank}"
    fi

    grub_cfg=${tpm2testdir}/testcase.cfg

    # Seal the key into a NV index guarded by PCR 0 and 1
    grub-protect ${extra_opt} \
	--tpm2-device="${tpm2dev}" \
	--action=add \
	--protector=tpm2 \
	--tpm2-bank="${pcr_bank}" \
	--tpm2-pcrs=0,1 \
	--tpm2-keyfile="${lukskeyfile}" \
	--tpm2-nvindex="${nv_index}" || ret=$?
    if [ "${ret}" -ne 0 ]; then
	echo "Failed to seal the secret key into ${nv_index}: ${ret}" >&2
	return 99
    fi

    # Write the TPM unsealing script
    cat > ${grub_cfg} <<EOF
loopback luks (host)${luksfile}
tpm2_key_protector_init --mode=nv --nvindex=${nv_index} ${extra_grub_opt}
if cryptomount -a --protector tpm2; then
    cat (crypto0)+1
fi
EOF

    # Test TPM unsealing with the same PCR
    ${grubshell} --timeout=${timeout} --emu-opts="-t ${tpm2dev}" < "${grub_cfg}" > "${testoutput}" || ret=$?

    # Remove the object from the NV index
    grub-protect \
	--tpm2-device="${tpm2dev}" \
	--protector=tpm2 \
	--action=remove \
	--tpm2-nvindex=${nv_index} \
	--tpm2-evict || :

    if [ "${ret}" -eq 0 ]; then
	if ! grep -q "^${vtext}$" "${testoutput}"; then
	    echo "error: test not verified [`cat ${testoutput}`]" >&2
	    return 1
	fi
    else
	echo "grub-emu exited with error: ${ret}" >&2
	return 99
    fi
}

tpm2_seal_unseal_cap() {
    pcr_bank="sha256"

    original_pcr1="$(tpm2_pcrread ${pcr_bank}:1) | tail -1 | cut -d' ' -f7"

    grub_cfg=${tpm2testdir}/testcase.cfg

    # Seal the password with grub-protect
    grub-protect \
	--tpm2-device="${tpm2dev}" \
	--action=add \
	--protector=tpm2 \
	--tpm2key \
	--tpm2-bank="${pcr_bank}" \
	--tpm2-pcrs=0,1 \
	--tpm2-keyfile="${lukskeyfile}" \
	--tpm2-outfile="${sealedkey}" || ret=$?
    if [ "${ret}" -ne 0 ]; then
	echo "Failed to seal the secret key: ${ret}" >&2
	return 99
    fi

    # Write the TPM unsealing script and cap PCR 1
    cat > "${grub_cfg}" <<EOF
loopback luks (host)${luksfile}
tpm2_key_protector_init -T (host)${sealedkey} -c 1
if cryptomount -a --protector tpm2; then
    cat (crypto0)+1
fi
EOF

    # Test TPM unsealing with the same PCR
    ${grubshell} --timeout=${timeout} --emu-opts="-t ${tpm2dev}" < "${grub_cfg}" > "${testoutput}" || ret=$?

    if [ "${ret}" -eq 0 ]; then
	if ! grep -q "^${vtext}$" "${testoutput}"; then
	    echo "error: test not verified [`cat ${testoutput}`]" >&2
	    return 1
	fi
    else
	echo "grub-emu exited with error: ${ret}" >&2
	return 99
    fi

    capped_pcr1="$(tpm2_pcrread ${pcr_bank}:1) | tail -1 | cut -d' ' -f7"

    if [ "${original_pcr1}" = "${capped_pcr1}" ]; then
	echo "error: PCR 1 not capped" >&2
	return 1
    fi
}

# Testcases for SRK mode
declare -a srktests=()
srktests+=("default transient no_fallback_srk sha256")
srktests+=("RSA transient no_fallback_srk sha256")
srktests+=("ECC transient no_fallback_srk sha256")
srktests+=("RSA persistent no_fallback_srk sha256")
srktests+=("ECC persistent no_fallback_srk sha256")
srktests+=("RSA transient fallback_srk sha256")
srktests+=("ECC transient fallback_srk sha256")
if [ "${with_sha384}" = "true" ]; then
    srktests+=("default transient no_fallback_srk sha384")
fi

exit_status=0

for i in "${!srktests[@]}"; do
    tpm2_seal_unseal ${srktests[$i]} || ret=$?
    if [ "${ret}" -eq 0 ]; then
	echo "TPM2 [SRK][${srktests[$i]}]: PASS"
    elif [ "${ret}" -eq 1 ]; then
	echo "TPM2 [SRK][${srktests[$i]}]: FAIL"
	ret=0
	exit_status=1
    else
	echo "Unexpected failure [SRK][${srktests[$i]}]" >&2
	exit ${ret}
    fi
done

# Testcases for NV index mode
declare -a nvtests=()
nvtests+=("persistent raw sha256")
nvtests+=("nvindex raw sha256")
nvtests+=("nvindex tpm2key sha256")
if [ "${with_sha384}" = "true" ]; then
    nvtests+=("persistent raw sha384")
    nvtests+=("nvindex tpm2key sha384")
fi

for i in "${!nvtests[@]}"; do
    tpm2_seal_unseal_nv ${nvtests[$i]} || ret=$?
    if [ "${ret}" -eq 0 ]; then
	echo "TPM2 [NV Index][${nvtests[$i]}]: PASS"
    elif [ "${ret}" -eq 1 ]; then
	echo "TPM2 [NV Index][${nvtests[$i]}]: FAIL"
	ret=0
	exit_status=1
    else
	echo "Unexpected failure [NV index][${nvtests[$i]}]" >&2
	exit ${ret}
    fi
done

# Testcase for PCR Capping
tpm2_seal_unseal_cap || ret=$?
if [ "${ret}" -eq 0 ]; then
    echo "TPM2 [PCR Capping]: PASS"
elif [ "${ret}" -eq 1 ]; then
    echo "TPM2 [PCR Capping]: FAIL"
    ret=0
    exit_status=1
else
    echo "Unexpected failure [PCR Capping]" >&2
    exit ${ret}
fi

exit ${exit_status}