2022-02-24 01:28:29 +01:00
#!/usr/bin/env bash
# SPDX-License-Identifier: LGPL-2.1-or-later
set -ex
2022-08-19 11:23:45 +02:00
set -o pipefail
2022-02-24 01:28:29 +01:00
2023-05-09 21:14:24 +02:00
SD_CRYPTSETUP = "/usr/lib/systemd/systemd-cryptsetup"
SD_MEASURE = "/usr/lib/systemd/systemd-measure"
SD_PCRPHASE = "/usr/lib/systemd/systemd-pcrphase"
2022-02-24 01:28:29 +01:00
export SYSTEMD_LOG_LEVEL = debug
2023-05-09 21:14:24 +02:00
cryptsetup_has_token_plugin_support( ) {
local plugin_path
plugin_path = " $( cryptsetup --help | sed -nr 's/.*LUKS2 external token plugin path: (.*)\./\1/p' ) /libcryptsetup-token-systemd-tpm2.so) "
cryptsetup --help | grep -q 'LUKS2 external token plugin support is compiled-in' && [ [ -f " $plugin_path " ] ]
}
tpm_has_pcr( ) {
local algorithm = " ${ 1 : ? } "
local pcr = " ${ 2 : ? } "
[ [ -f " /sys/class/tpm/tpm0/pcr- $algorithm / $pcr " ] ]
}
2023-07-07 13:37:28 -04:00
tpm_check_failure_with_wrong_pin( ) {
local testimg = " ${ 1 : ? } "
local badpin = " ${ 2 : ? } "
local goodpin = " ${ 3 : ? } "
# We need to be careful not to trigger DA lockout; allow 2 failures
tpm2_dictionarylockout -s -n 2
( ! PIN = $badpin " $SD_CRYPTSETUP " attach test-volume " $testimg " - tpm2-device= auto,headless= 1)
# Verify the correct PIN works, to be sure the failure wasn't a DA lockout
PIN = $goodpin " $SD_CRYPTSETUP " attach test-volume " $testimg " - tpm2-device= auto,headless= 1
" $SD_CRYPTSETUP " detach test-volume
# Clear/reset the DA lockout counter
tpm2_dictionarylockout -c
}
2023-05-09 21:14:24 +02:00
# Prepare a fresh disk image
img = "/tmp/test.img"
truncate -s 20M " $img "
2022-02-24 01:28:29 +01:00
echo -n passphrase >/tmp/passphrase
2023-07-15 21:33:50 -04:00
# Change file mode to avoid "/tmp/passphrase has 0644 mode that is too permissive" messages
chmod 0600 /tmp/passphrase
2023-05-09 21:14:24 +02:00
cryptsetup luksFormat -q --pbkdf pbkdf2 --pbkdf-force-iterations 1000 --use-urandom " $img " /tmp/passphrase
2022-02-24 01:28:29 +01:00
2022-07-27 22:10:07 +02:00
# Unlocking via keyfile
2023-05-09 21:14:24 +02:00
systemd-cryptenroll --unlock-key-file= /tmp/passphrase --tpm2-device= auto " $img "
2022-07-27 22:10:07 +02:00
2022-02-24 01:28:29 +01:00
# Enroll unlock with default PCR policy
2023-05-09 21:14:24 +02:00
PASSWORD = passphrase systemd-cryptenroll --tpm2-device= auto " $img "
" $SD_CRYPTSETUP " attach test-volume " $img " - tpm2-device= auto,headless= 1
" $SD_CRYPTSETUP " detach test-volume
2022-02-24 01:28:29 +01:00
# Check with wrong PCR
tpm2_pcrextend 7:sha256= 0000000000000000000000000000000000000000000000000000000000000000
2023-05-09 21:14:24 +02:00
( ! " $SD_CRYPTSETUP " attach test-volume " $img " - tpm2-device= auto,headless= 1)
2022-02-24 01:28:29 +01:00
# Enroll unlock with PCR+PIN policy
2023-05-09 21:14:24 +02:00
systemd-cryptenroll --wipe-slot= tpm2 " $img "
PASSWORD = passphrase NEWPIN = 123456 systemd-cryptenroll --tpm2-device= auto --tpm2-with-pin= true " $img "
PIN = 123456 " $SD_CRYPTSETUP " attach test-volume " $img " - tpm2-device= auto,headless= 1
" $SD_CRYPTSETUP " detach test-volume
2022-02-24 01:28:29 +01:00
2023-07-07 13:37:28 -04:00
# Check failure with wrong PIN; try a few times to make sure we avoid DA lockout
for _ in { 0..3} ; do
tpm_check_failure_with_wrong_pin " $img " 123457 123456
done
2022-02-24 01:28:29 +01:00
2022-05-25 14:06:44 +02:00
# Check LUKS2 token plugin unlock (i.e. without specifying tpm2-device=auto)
2023-05-09 21:14:24 +02:00
if cryptsetup_has_token_plugin_support; then
PIN = 123456 " $SD_CRYPTSETUP " attach test-volume " $img " - headless = 1
" $SD_CRYPTSETUP " detach test-volume
2022-05-25 14:06:44 +02:00
# Check failure with wrong PIN
2023-07-07 13:37:28 -04:00
for _ in { 0..3} ; do
tpm_check_failure_with_wrong_pin " $img " 123457 123456
done
2022-05-25 14:06:44 +02:00
else
echo 'cryptsetup has no LUKS2 token plugin support, skipping'
fi
2022-02-24 01:28:29 +01:00
# Check failure with wrong PCR (and correct PIN)
tpm2_pcrextend 7:sha256= 0000000000000000000000000000000000000000000000000000000000000000
2023-05-09 21:14:24 +02:00
( ! PIN = 123456 " $SD_CRYPTSETUP " attach test-volume " $img " - tpm2-device= auto,headless= 1)
2022-02-24 01:28:29 +01:00
# Enroll unlock with PCR 0+7
2023-05-09 21:14:24 +02:00
systemd-cryptenroll --wipe-slot= tpm2 " $img "
PASSWORD = passphrase systemd-cryptenroll --tpm2-device= auto --tpm2-pcrs= 0+7 " $img "
" $SD_CRYPTSETUP " attach test-volume " $img " - tpm2-device= auto,headless= 1
" $SD_CRYPTSETUP " detach test-volume
2022-02-24 01:28:29 +01:00
# Check with wrong PCR 0
tpm2_pcrextend 0:sha256= 0000000000000000000000000000000000000000000000000000000000000000
2023-07-13 16:02:37 -04:00
( ! " $SD_CRYPTSETUP " attach test-volume " $img " - tpm2-device= auto,headless= 1)
if tpm_has_pcr sha256 12; then
# Enroll using an explict PCR value (that does match current PCR value)
systemd-cryptenroll --wipe-slot= tpm2 " $img "
EXPECTED_PCR_VALUE = $( cat /sys/class/tpm/tpm0/pcr-sha256/12)
PASSWORD = passphrase systemd-cryptenroll --tpm2-device= auto --tpm2-pcrs= " 12:sha256= $EXPECTED_PCR_VALUE " " $img "
" $SD_CRYPTSETUP " attach test-volume " $img " - tpm2-device= auto,headless= 1
" $SD_CRYPTSETUP " detach test-volume
# Same as above plus more PCRs without the value or alg specified
systemd-cryptenroll --wipe-slot= tpm2 " $img "
EXPECTED_PCR_VALUE = $( cat /sys/class/tpm/tpm0/pcr-sha256/12)
PASSWORD = passphrase systemd-cryptenroll --tpm2-device= auto --tpm2-pcrs= " 1,12:sha256= $EXPECTED_PCR_VALUE ,3 " " $img "
" $SD_CRYPTSETUP " attach test-volume " $img " - tpm2-device= auto,headless= 1
" $SD_CRYPTSETUP " detach test-volume
# Same as above plus more PCRs with hash alg specified but hash value not specified
systemd-cryptenroll --wipe-slot= tpm2 " $img "
EXPECTED_PCR_VALUE = $( cat /sys/class/tpm/tpm0/pcr-sha256/12)
PASSWORD = passphrase systemd-cryptenroll --tpm2-device= auto --tpm2-pcrs= " 1:sha256,12:sha256= $EXPECTED_PCR_VALUE ,3 " " $img "
" $SD_CRYPTSETUP " attach test-volume " $img " - tpm2-device= auto,headless= 1
" $SD_CRYPTSETUP " detach test-volume
# Now the interesting part, enrolling using a hash value that doesn't match the current PCR value
systemd-cryptenroll --wipe-slot= tpm2 " $img "
tpm2_pcrread -Q -o /tmp/pcr.dat sha256:12
CURRENT_PCR_VALUE = $( cat /sys/class/tpm/tpm0/pcr-sha256/12)
EXPECTED_PCR_VALUE = $( cat /tmp/pcr.dat /tmp/pcr.dat | openssl dgst -sha256 -r | cut -d ' ' -f 1)
PASSWORD = passphrase systemd-cryptenroll --tpm2-device= auto --tpm2-pcrs= " 12:sha256= $EXPECTED_PCR_VALUE " " $img "
( ! " $SD_CRYPTSETUP " attach test-volume " $img " - tpm2-device= auto,headless= 1)
tpm2_pcrextend " 12:sha256= $CURRENT_PCR_VALUE "
" $SD_CRYPTSETUP " attach test-volume " $img " - tpm2-device= auto,headless= 1
" $SD_CRYPTSETUP " detach test-volume
rm -f /tmp/pcr.dat
fi
2022-02-24 01:28:29 +01:00
2023-05-09 21:14:24 +02:00
rm -f " ${ img : ? } "
2022-08-19 11:26:49 +02:00
2023-05-09 21:14:24 +02:00
if [ [ -x " $SD_MEASURE " ] ] ; then
2023-02-05 21:41:24 +01:00
echo HALLO >/tmp/tpmdata1
echo foobar >/tmp/tpmdata2
2022-08-04 11:00:10 +02:00
cat >/tmp/result <<EOF
2022-08-19 11:23:45 +02:00
11:sha1= 5177e4ad69db92192c10e5f80402bf81bfec8a81
11:sha256= 37b48bd0b222394dbe3cceff2fca4660c4b0a90ae9369ec90b42f14489989c13
11:sha384= 5573f9b2caf55b1d0a6a701f890662d682af961899f0419cf1e2d5ea4a6a68c1f25bd4f5b8a0865eeee82af90f5cb087
11:sha512= 961305d7e9981d6606d1ce97b3a9a1f92610cac033e9c39064895f0e306abc1680463d55767bd98e751eae115bdef3675a9ee1d29ed37da7885b1db45bb2555b
2022-07-29 18:45:53 +02:00
EOF
2023-05-09 21:14:24 +02:00
" $SD_MEASURE " calculate --linux= /tmp/tpmdata1 --initrd= /tmp/tpmdata2 --bank= sha1 --bank= sha256 --bank= sha384 --bank= sha512 --phase= : | cmp - /tmp/result
2022-08-19 11:26:49 +02:00
cat >/tmp/result.json <<EOF
{ "sha1" :[ { "pcr" :11,"hash" :"5177e4ad69db92192c10e5f80402bf81bfec8a81" } ] ,"sha256" :[ { "pcr" :11,"hash" :"37b48bd0b222394dbe3cceff2fca4660c4b0a90ae9369ec90b42f14489989c13" } ] ,"sha384" :[ { "pcr" :11,"hash" :"5573f9b2caf55b1d0a6a701f890662d682af961899f0419cf1e2d5ea4a6a68c1f25bd4f5b8a0865eeee82af90f5cb087" } ] ,"sha512" :[ { "pcr" :11,"hash" :"961305d7e9981d6606d1ce97b3a9a1f92610cac033e9c39064895f0e306abc1680463d55767bd98e751eae115bdef3675a9ee1d29ed37da7885b1db45bb2555b" } ] }
EOF
2023-05-09 21:14:24 +02:00
" $SD_MEASURE " calculate --linux= /tmp/tpmdata1 --initrd= /tmp/tpmdata2 --bank= sha1 --bank= sha256 --bank= sha384 --bank= sha512 --phase= : -j | diff -u - /tmp/result.json
2022-09-17 15:22:54 +02:00
cat >/tmp/result <<EOF
11:sha1= 6765ee305db063040c454d32697d922b3d4f232b
11:sha256= 21c49c1242042649e09c156546fd7d425ccc3c67359f840507b30be4e0f6f699
11:sha384= 08d0b003a134878eee552070d51d58abe942f457ca85704131dd36f73728e7327ca837594bc9d5ac7de818d02a3d5dd2
11:sha512= 65120f6ebc04b156421c6f3d543b2fad545363d9ca61c514205459e9c0e0b22e09c23605eae5853e38458ef3ca54e087168af8d8a882a98d220d9391e48be6d0
EOF
2023-05-09 21:14:24 +02:00
" $SD_MEASURE " calculate --linux= /tmp/tpmdata1 --initrd= /tmp/tpmdata2 --bank= sha1 --bank= sha256 --bank= sha384 --bank= sha512 --phase= foo | cmp - /tmp/result
2022-09-17 15:22:54 +02:00
cat >/tmp/result.json <<EOF
{ "sha1" :[ { "phase" :"foo" ,"pcr" :11,"hash" :"6765ee305db063040c454d32697d922b3d4f232b" } ] ,"sha256" :[ { "phase" :"foo" ,"pcr" :11,"hash" :"21c49c1242042649e09c156546fd7d425ccc3c67359f840507b30be4e0f6f699" } ] ,"sha384" :[ { "phase" :"foo" ,"pcr" :11,"hash" :"08d0b003a134878eee552070d51d58abe942f457ca85704131dd36f73728e7327ca837594bc9d5ac7de818d02a3d5dd2" } ] ,"sha512" :[ { "phase" :"foo" ,"pcr" :11,"hash" :"65120f6ebc04b156421c6f3d543b2fad545363d9ca61c514205459e9c0e0b22e09c23605eae5853e38458ef3ca54e087168af8d8a882a98d220d9391e48be6d0" } ] }
EOF
2023-05-09 21:14:24 +02:00
" $SD_MEASURE " calculate --linux= /tmp/tpmdata1 --initrd= /tmp/tpmdata2 --bank= sha1 --bank= sha256 --bank= sha384 --bank= sha512 --phase= foo -j | diff -u - /tmp/result.json
2022-08-19 11:26:49 +02:00
2022-09-17 15:22:54 +02:00
rm /tmp/result /tmp/result.json
2022-08-19 11:26:49 +02:00
else
2023-05-09 21:14:24 +02:00
echo " $SD_MEASURE not found, skipping PCR policy test case "
2022-08-19 11:26:49 +02:00
fi
2023-05-09 21:14:24 +02:00
if [ [ -x " $SD_MEASURE " ] ] && tpm_has_pcr sha1 11 && tpm_has_pcr sha256 11; then
2022-08-19 11:26:49 +02:00
# Generate key pair
openssl genpkey -algorithm RSA -pkeyopt rsa_keygen_bits:2048 -out "/tmp/pcrsign-private.pem"
openssl rsa -pubout -in "/tmp/pcrsign-private.pem" -out "/tmp/pcrsign-public.pem"
2022-12-02 12:48:26 +01:00
MEASURE_BANKS = ( "--bank=sha256" )
# Check if SHA1 signatures are supported
#
# Some distros have started phasing out SHA1, so make sure the SHA1
# signatures are supported before trying to use them.
if echo hello | openssl dgst -sign /tmp/pcrsign-private.pem -sha1 >/dev/null; then
MEASURE_BANKS += ( "--bank=sha1" )
fi
2022-08-19 11:26:49 +02:00
# Sign current PCR state with it
2023-05-09 21:14:24 +02:00
" $SD_MEASURE " sign --current " ${ MEASURE_BANKS [@] } " --private-key= "/tmp/pcrsign-private.pem" --public-key= "/tmp/pcrsign-public.pem" --phase= : | tee "/tmp/pcrsign.sig"
2022-08-19 11:26:49 +02:00
dd if = /dev/urandom of = /tmp/pcrtestdata bs = 1024 count = 64
systemd-creds encrypt /tmp/pcrtestdata /tmp/pcrtestdata.encrypted --with-key= host+tpm2-with-public-key --tpm2-public-key= "/tmp/pcrsign-public.pem"
systemd-creds decrypt /tmp/pcrtestdata.encrypted - --tpm2-signature= "/tmp/pcrsign.sig" | cmp - /tmp/pcrtestdata
# Invalidate PCR, decrypting should fail now
tpm2_pcrextend 11:sha256= 0000000000000000000000000000000000000000000000000000000000000000
2023-04-05 15:50:42 +02:00
( ! systemd-creds decrypt /tmp/pcrtestdata.encrypted - --tpm2-signature= "/tmp/pcrsign.sig" >/dev/null)
2022-08-19 11:26:49 +02:00
# Sign new PCR state, decrypting should work now.
2023-05-09 21:14:24 +02:00
" $SD_MEASURE " sign --current " ${ MEASURE_BANKS [@] } " --private-key= "/tmp/pcrsign-private.pem" --public-key= "/tmp/pcrsign-public.pem" --phase= : >"/tmp/pcrsign.sig2"
2022-08-19 11:26:49 +02:00
systemd-creds decrypt /tmp/pcrtestdata.encrypted - --tpm2-signature= "/tmp/pcrsign.sig2" | cmp - /tmp/pcrtestdata
# Now, do the same, but with a cryptsetup binding
2023-05-09 21:14:24 +02:00
truncate -s 20M " $img "
cryptsetup luksFormat -q --pbkdf pbkdf2 --pbkdf-force-iterations 1000 --use-urandom " $img " /tmp/passphrase
2023-02-08 02:10:28 +00:00
# Ensure that an unrelated signature, when not requested, is not used
touch /run/systemd/tpm2-pcr-signature.json
2023-05-09 21:14:24 +02:00
systemd-cryptenroll --unlock-key-file= /tmp/passphrase --tpm2-device= auto --tpm2-public-key= "/tmp/pcrsign-public.pem" " $img "
2023-02-08 02:10:28 +00:00
# Reset and use the signature now
rm -f /run/systemd/tpm2-pcr-signature.json
2023-05-09 21:14:24 +02:00
systemd-cryptenroll --wipe-slot= tpm2 " $img "
systemd-cryptenroll --unlock-key-file= /tmp/passphrase --tpm2-device= auto --tpm2-public-key= "/tmp/pcrsign-public.pem" --tpm2-signature= "/tmp/pcrsign.sig2" " $img "
2022-08-19 11:26:49 +02:00
# Check if we can activate that (without the token module stuff)
2023-05-09 21:14:24 +02:00
SYSTEMD_CRYPTSETUP_USE_TOKEN_MODULE = 0 " $SD_CRYPTSETUP " attach test-volume2 " $img " - tpm2-device= auto,tpm2-signature= "/tmp/pcrsign.sig2" ,headless= 1
SYSTEMD_CRYPTSETUP_USE_TOKEN_MODULE = 0 " $SD_CRYPTSETUP " detach test-volume2
2022-08-19 11:26:49 +02:00
2023-02-15 10:08:16 +09:00
# Check if we can activate that (and a second time with the token module stuff enabled)
2023-05-09 21:14:24 +02:00
SYSTEMD_CRYPTSETUP_USE_TOKEN_MODULE = 1 " $SD_CRYPTSETUP " attach test-volume2 " $img " - tpm2-device= auto,tpm2-signature= "/tmp/pcrsign.sig2" ,headless= 1
SYSTEMD_CRYPTSETUP_USE_TOKEN_MODULE = 1 " $SD_CRYPTSETUP " detach test-volume2
2022-08-19 11:26:49 +02:00
# After extending the PCR things should fail
tpm2_pcrextend 11:sha256= 0000000000000000000000000000000000000000000000000000000000000000
2023-05-09 21:14:24 +02:00
( ! SYSTEMD_CRYPTSETUP_USE_TOKEN_MODULE = 0 " $SD_CRYPTSETUP " attach test-volume2 " $img " - tpm2-device= auto,tpm2-signature= "/tmp/pcrsign.sig2" ,headless= 1)
( ! SYSTEMD_CRYPTSETUP_USE_TOKEN_MODULE = 1 " $SD_CRYPTSETUP " attach test-volume2 " $img " - tpm2-device= auto,tpm2-signature= "/tmp/pcrsign.sig2" ,headless= 1)
2022-08-19 11:26:49 +02:00
# But once we sign the current PCRs, we should be able to unlock again
2023-05-09 21:14:24 +02:00
" $SD_MEASURE " sign --current " ${ MEASURE_BANKS [@] } " --private-key= "/tmp/pcrsign-private.pem" --public-key= "/tmp/pcrsign-public.pem" --phase= : >"/tmp/pcrsign.sig3"
SYSTEMD_CRYPTSETUP_USE_TOKEN_MODULE = 0 " $SD_CRYPTSETUP " attach test-volume2 " $img " - tpm2-device= auto,tpm2-signature= "/tmp/pcrsign.sig3" ,headless= 1
" $SD_CRYPTSETUP " detach test-volume2
SYSTEMD_CRYPTSETUP_USE_TOKEN_MODULE = 1 " $SD_CRYPTSETUP " attach test-volume2 " $img " - tpm2-device= auto,tpm2-signature= "/tmp/pcrsign.sig3" ,headless= 1
" $SD_CRYPTSETUP " detach test-volume2
2022-08-19 11:26:49 +02:00
2022-12-15 11:39:33 +01:00
# Test --append mode and de-duplication. With the same parameters signing should not add a new entry
2023-05-09 21:14:24 +02:00
" $SD_MEASURE " sign --current " ${ MEASURE_BANKS [@] } " --private-key= "/tmp/pcrsign-private.pem" --public-key= "/tmp/pcrsign-public.pem" --phase= : --append= "/tmp/pcrsign.sig3" >"/tmp/pcrsign.sig4"
2022-12-15 11:39:33 +01:00
cmp "/tmp/pcrsign.sig3" "/tmp/pcrsign.sig4"
# Sign one more phase, this should
2023-05-09 21:14:24 +02:00
" $SD_MEASURE " sign --current " ${ MEASURE_BANKS [@] } " --private-key= "/tmp/pcrsign-private.pem" --public-key= "/tmp/pcrsign-public.pem" --phase= quux:waldo --append= "/tmp/pcrsign.sig4" >"/tmp/pcrsign.sig5"
2023-04-05 15:50:42 +02:00
( ! cmp "/tmp/pcrsign.sig4" "/tmp/pcrsign.sig5" )
2022-12-15 11:39:33 +01:00
# Should still be good to unlock, given the old entry still exists
2023-05-09 21:14:24 +02:00
SYSTEMD_CRYPTSETUP_USE_TOKEN_MODULE = 0 " $SD_CRYPTSETUP " attach test-volume2 " $img " - tpm2-device= auto,tpm2-signature= "/tmp/pcrsign.sig5" ,headless= 1
" $SD_CRYPTSETUP " detach test-volume2
2022-12-15 11:39:33 +01:00
2022-12-16 01:38:08 +09:00
# Adding both signatures once more should not change anything, due to the deduplication
2023-05-09 21:14:24 +02:00
" $SD_MEASURE " sign --current " ${ MEASURE_BANKS [@] } " --private-key= "/tmp/pcrsign-private.pem" --public-key= "/tmp/pcrsign-public.pem" --phase= : --append= "/tmp/pcrsign.sig5" >"/tmp/pcrsign.sig6"
" $SD_MEASURE " sign --current " ${ MEASURE_BANKS [@] } " --private-key= "/tmp/pcrsign-private.pem" --public-key= "/tmp/pcrsign-public.pem" --phase= quux:waldo --append= "/tmp/pcrsign.sig6" >"/tmp/pcrsign.sig7"
2022-12-15 11:39:33 +01:00
cmp "/tmp/pcrsign.sig5" "/tmp/pcrsign.sig7"
2023-05-09 21:14:24 +02:00
rm -f " $img "
2022-08-04 11:00:10 +02:00
else
2023-05-09 21:14:24 +02:00
echo " $SD_MEASURE or PCR sysfs files not found, skipping signed PCR policy test case "
2022-08-04 11:00:10 +02:00
fi
2022-07-29 18:45:53 +02:00
2023-05-09 21:14:24 +02:00
if [ [ -x " $SD_PCRPHASE " ] ] && tpm_has_pcr sha256 11 && tpm_has_pcr sha256 15; then
2022-12-16 16:25:34 +01:00
# Let's measure the machine ID
tpm2_pcrread sha256:15 -Q -o /tmp/oldpcr15
mv /etc/machine-id /etc/machine-id.save
echo 994013bf23864ee7992eab39a96dd3bb >/etc/machine-id
2023-05-09 21:14:24 +02:00
SYSTEMD_FORCE_MEASURE = 1 " $SD_PCRPHASE " --machine-id
2022-12-16 16:25:34 +01:00
mv /etc/machine-id.save /etc/machine-id
tpm2_pcrread sha256:15 -Q -o /tmp/newpcr15
# And check it matches expectations
2023-05-09 21:14:24 +02:00
diff /tmp/newpcr15 \
<( cat /tmp/oldpcr15 <( echo -n "machine-id:994013bf23864ee7992eab39a96dd3bb" | openssl dgst -binary -sha256) | openssl dgst -binary -sha256)
2022-12-16 16:25:34 +01:00
2023-05-09 21:14:24 +02:00
rm -f /tmp/oldpcr15 /tmp/newpcr15
2022-12-16 16:25:34 +01:00
2023-08-30 12:53:06 +02:00
# Check that the event log record was properly written:
test " $( jq --seq --slurp '.[0].pcr' < /var/log/systemd/tpm2-measure.log) " = = " $( printf '\x1e15' ) "
test " $( jq --seq --slurp --raw-output '.[0].digests[1].digest' < /var/log/systemd/tpm2-measure.log) *stdin " = = " $( echo -n "machine-id:994013bf23864ee7992eab39a96dd3bb" | openssl dgst -hex -sha256 -r) "
2022-12-16 16:25:34 +01:00
# And similar for the boot phase measurement into PCR 11
tpm2_pcrread sha256:11 -Q -o /tmp/oldpcr11
2023-05-09 21:14:24 +02:00
SYSTEMD_FORCE_MEASURE = 1 " $SD_PCRPHASE " foobar
2022-12-16 16:25:34 +01:00
tpm2_pcrread sha256:11 -Q -o /tmp/newpcr11
2023-05-09 21:14:24 +02:00
diff /tmp/newpcr11 \
<( cat /tmp/oldpcr11 <( echo -n "foobar" | openssl dgst -binary -sha256) | openssl dgst -binary -sha256)
2022-12-16 16:25:34 +01:00
2023-08-30 12:53:06 +02:00
# Check the event log for the 2nd record
jq --seq --slurp < /var/log/systemd/tpm2-measure.log
test " $( jq --seq --slurp .[ 1] .pcr < /var/log/systemd/tpm2-measure.log) " = = " $( printf '\x1e11' ) "
test " $( jq --seq --slurp --raw-output .[ 1] .digests[ 0] .digest < /var/log/systemd/tpm2-measure.log) *stdin " = = " $( echo -n "foobar" | openssl dgst -hex -sha256 -r) "
2023-05-09 21:14:24 +02:00
rm -f /tmp/oldpcr11 /tmp/newpcr11
2022-12-16 16:25:34 +01:00
else
2023-05-09 21:14:24 +02:00
echo " $SD_PCRPHASE or PCR sysfs files not found, skipping PCR extension test case "
2022-12-16 16:25:34 +01:00
fi
2023-02-08 00:25:00 +00:00
# Ensure that sandboxing doesn't stop creds from being accessible
echo "test" > /tmp/testdata
systemd-creds encrypt /tmp/testdata /tmp/testdata.encrypted --with-key= tpm2
2023-02-22 00:02:31 +11:00
# LoadCredentialEncrypted
2023-02-08 00:25:00 +00:00
systemd-run -p PrivateDevices = yes -p LoadCredentialEncrypted = testdata.encrypted:/tmp/testdata.encrypted --pipe --wait systemd-creds cat testdata.encrypted | cmp - /tmp/testdata
2023-02-22 00:02:31 +11:00
# SetCredentialEncrypted
systemd-run -p PrivateDevices = yes -p SetCredentialEncrypted = testdata.encrypted:" $( cat /tmp/testdata.encrypted) " --pipe --wait systemd-creds cat testdata.encrypted | cmp - /tmp/testdata
2023-05-09 21:14:24 +02:00
rm -f /tmp/testdata
2023-03-27 15:24:03 +00:00
2023-07-17 19:06:22 +01:00
# There is an external issue with libcryptsetup on ppc64 that hits 95% of Ubuntu ppc64 test runs, so skip it
machine = " $( uname -m) "
if [ " ${ machine } " = "ppc64le" ] ; then
touch /testok
exit 0
fi
2023-05-09 22:47:42 +02:00
cryptenroll_wipe_and_check( ) { (
set +o pipefail
: >/tmp/cryptenroll.out
systemd-cryptenroll " $@ " | & tee /tmp/cryptenroll.out
grep -qE "Wiped slot [[:digit:]]+" /tmp/cryptenroll.out
) }
2023-05-09 21:14:24 +02:00
img = "/tmp/cryptenroll.img"
truncate -s 20M " $img "
2023-03-27 15:24:03 +00:00
echo -n password >/tmp/password
2023-07-15 21:33:50 -04:00
# Change file mode to avoid "/tmp/password has 0644 mode that is too permissive" messages
chmod 0600 /tmp/password
2023-05-09 21:14:24 +02:00
cryptsetup luksFormat -q --pbkdf pbkdf2 --pbkdf-force-iterations 1000 --use-urandom " $img " /tmp/password
2023-03-27 15:24:03 +00:00
2023-05-09 22:47:42 +02:00
# Enroll additional tokens, keys, and passwords to exercise the list and wipe stuff
systemd-cryptenroll --unlock-key-file= /tmp/password --tpm2-device= auto " $img "
NEWPASSWORD = "" systemd-cryptenroll --unlock-key-file= /tmp/password --password " $img "
NEWPASSWORD = foo systemd-cryptenroll --unlock-key-file= /tmp/password --password " $img "
for _ in { 0..9} ; do
systemd-cryptenroll --unlock-key-file= /tmp/password --recovery-key " $img "
done
PASSWORD = "" NEWPIN = 123456 systemd-cryptenroll --tpm2-device= auto --tpm2-with-pin= true " $img "
# Do some basic checks before we start wiping stuff
systemd-cryptenroll " $img "
systemd-cryptenroll " $img " | grep password
systemd-cryptenroll " $img " | grep recovery
# Let's start wiping
cryptenroll_wipe_and_check " $img " --wipe= empty
( ! cryptenroll_wipe_and_check " $img " --wipe= empty)
cryptenroll_wipe_and_check " $img " --wipe= empty,0
2023-05-10 11:47:57 +02:00
PASSWORD = foo NEWPASSWORD = foo cryptenroll_wipe_and_check " $img " --wipe= 0,0,empty,0,pkcs11,fido2,000,recovery,password --password
2023-05-09 22:47:42 +02:00
systemd-cryptenroll " $img " | grep password
( ! systemd-cryptenroll " $img " | grep recovery)
# We shouldn't be able to wipe all keyslots without enrolling a new key first
( ! systemd-cryptenroll " $img " --wipe= all)
PASSWORD = foo NEWPASSWORD = foo cryptenroll_wipe_and_check " $img " --password --wipe= all
# Check if the newly (and only) enrolled password works
( ! systemd-cryptenroll --unlock-key-file= /tmp/password --recovery-key " $img " )
( ! PASSWORD = "" systemd-cryptenroll --recovery-key " $img " )
PASSWORD = foo systemd-cryptenroll --recovery-key " $img "
2023-05-09 21:14:24 +02:00
systemd-cryptenroll --fido2-with-client-pin= false " $img "
systemd-cryptenroll --fido2-with-user-presence= false " $img "
systemd-cryptenroll --fido2-with-user-verification= false " $img "
systemd-cryptenroll --tpm2-pcrs= 8 " $img "
systemd-cryptenroll --tpm2-pcrs= boot-loader-code+boot-loader-config " $img "
2023-03-27 15:24:03 +00:00
2023-05-09 21:14:24 +02:00
( ! systemd-cryptenroll --fido2-with-client-pin= false )
( ! systemd-cryptenroll --fido2-with-user-presence= f " $img " /tmp/foo)
( ! systemd-cryptenroll --fido2-with-client-pin= 1234 " $img " )
( ! systemd-cryptenroll --fido2-with-user-presence= 1234 " $img " )
( ! systemd-cryptenroll --fido2-with-user-verification= 1234 " $img " )
( ! systemd-cryptenroll --tpm2-with-pin= 1234 " $img " )
( ! systemd-cryptenroll --recovery-key --password " $img " )
( ! systemd-cryptenroll --password --recovery-key " $img " )
( ! systemd-cryptenroll --password --fido2-device= auto " $img " )
( ! systemd-cryptenroll --password --pkcs11-token-uri= auto " $img " )
( ! systemd-cryptenroll --password --tpm2-device= auto " $img " )
( ! systemd-cryptenroll --unlock-fido2-device= auto --unlock-fido2-device= auto " $img " )
( ! systemd-cryptenroll --unlock-fido2-device= auto --unlock-key-file= /tmp/unlock " $img " )
( ! systemd-cryptenroll --fido2-credential-algorithm= es512 " $img " )
( ! systemd-cryptenroll --tpm2-public-key-pcrs= key " $img " )
( ! systemd-cryptenroll --tpm2-pcrs= key " $img " )
( ! systemd-cryptenroll --tpm2-pcrs= 44+8 " $img " )
( ! systemd-cryptenroll --tpm2-pcrs= hello " $img " )
( ! systemd-cryptenroll --wipe-slot " $img " )
( ! systemd-cryptenroll --wipe-slot= 10240000 " $img " )
( ! systemd-cryptenroll --fido2-device= auto --unlock-fido2-device= auto " $img " )
2023-03-27 15:24:03 +00:00
2023-07-12 15:49:55 +02:00
touch /testok