| 1 |
#!/usr/bin/env bash |
| 2 |
|
| 3 |
# For the license, see the LICENSE file in the root directory. |
| 4 |
|
| 5 |
# shellcheck disable=SC1091 |
| 6 |
|
| 7 |
TESTDIR=${abs_top_testdir:-$(dirname "$0")} |
| 8 |
ROOT=${abs_top_builddir:-$(dirname "$0")/..} |
| 9 |
source "${TESTDIR}/common" |
| 10 |
skip_test_no_tpm20 "${SWTPM_EXE}" |
| 11 |
|
| 12 |
PATH=$ROOT/src/swtpm:$PATH |
| 13 |
|
| 14 |
PARAMETERS=( |
| 15 |
"" |
| 16 |
"--createek" |
| 17 |
"--createek --create-ek-cert --create-platform-cert --config ${TESTDIR}/swtpm_setup.conf --vmid test --display" |
| 18 |
"--createek --create-ek-cert --create-platform-cert --config ${TESTDIR}/swtpm_setup.conf --vmid test --display --keyfile ${TESTDIR}/data/keyfile.txt" |
| 19 |
"--createek --create-ek-cert --create-platform-cert --config ${TESTDIR}/swtpm_setup.conf --vmid test --display --pwdfile ${TESTDIR}/data/pwdfile.txt" |
| 20 |
"--createek --allow-signing" |
| 21 |
"--createek --allow-signing --create-ek-cert --create-platform-cert --config ${TESTDIR}/swtpm_setup.conf --vmid test --display" |
| 22 |
"--createek --allow-signing --create-ek-cert --create-platform-cert --config ${TESTDIR}/swtpm_setup.conf --vmid test --display --keyfile ${TESTDIR}/data/keyfile.txt" |
| 23 |
"--createek --allow-signing --create-ek-cert --create-platform-cert --config ${TESTDIR}/swtpm_setup.conf --vmid test --display --pwdfile ${TESTDIR}/data/pwdfile.txt" |
| 24 |
"--createek --allow-signing --decryption --create-ek-cert --create-platform-cert --config ${TESTDIR}/swtpm_setup.conf --vmid test --display --keyfile ${TESTDIR}/data/keyfile256bit.txt --cipher aes-256-cbc" |
| 25 |
"--createek --allow-signing --decryption --create-ek-cert --create-platform-cert --config ${TESTDIR}/swtpm_setup.conf --vmid test --display --pwdfile ${TESTDIR}/data/pwdfile.txt --cipher aes-256-cbc" |
| 26 |
"--createek --create-spk --create-ek-cert --create-platform-cert --config ${TESTDIR}/swtpm_setup.conf --vmid test --lock-nvram" |
| 27 |
"--ecc --createek" |
| 28 |
"--ecc --createek --create-ek-cert --create-platform-cert --config ${TESTDIR}/swtpm_setup.conf --vmid test --display" |
| 29 |
"--ecc --createek --create-ek-cert --create-platform-cert --config ${TESTDIR}/swtpm_setup.conf --vmid test --display --keyfile ${TESTDIR}/data/keyfile.txt" |
| 30 |
"--ecc --createek --create-ek-cert --create-platform-cert --config ${TESTDIR}/swtpm_setup.conf --vmid test --display --pwdfile ${TESTDIR}/data/pwdfile.txt" |
| 31 |
"--ecc --createek --allow-signing" |
| 32 |
"--ecc --createek --allow-signing --create-ek-cert --create-platform-cert --config ${TESTDIR}/swtpm_setup.conf --vmid test --display" |
| 33 |
"--ecc --createek --allow-signing --create-ek-cert --create-platform-cert --config ${TESTDIR}/swtpm_setup.conf --vmid test --display --keyfile ${TESTDIR}/data/keyfile.txt" |
| 34 |
"--ecc --createek --allow-signing --create-ek-cert --create-platform-cert --config ${TESTDIR}/swtpm_setup.conf --vmid test --display --pwdfile ${TESTDIR}/data/pwdfile.txt" |
| 35 |
"--ecc --createek --allow-signing --create-ek-cert --create-platform-cert --config ${TESTDIR}/swtpm_setup.conf --vmid test --display --keyfile ${TESTDIR}/data/keyfile256bit.txt --cipher aes-256-cbc" |
| 36 |
"--ecc --createek --allow-signing --create-ek-cert --create-platform-cert --config ${TESTDIR}/swtpm_setup.conf --vmid test --display --pwdfile ${TESTDIR}/data/pwdfile.txt --cipher aes-256-cbc" |
| 37 |
"--ecc --createek --allow-signing --decryption --create-ek-cert --create-platform-cert --config ${TESTDIR}/swtpm_setup.conf --vmid test --display --keyfile-fd 100 --cipher aes-256-cbc" |
| 38 |
"--ecc --createek --allow-signing --decryption --create-ek-cert --create-platform-cert --config ${TESTDIR}/swtpm_setup.conf --vmid test --display --pwdfile-fd 101 --cipher aes-256-cbc" |
| 39 |
"--ecc --createek --create-spk --create-ek-cert --create-platform-cert --config ${TESTDIR}/swtpm_setup.conf --vmid test --lock-nvram" |
| 40 |
) |
| 41 |
|
| 42 |
# Tests for 3072 bit RSA keys to be appended to above array if RSA 3072 keys are supported |
| 43 |
PARAMETERS_3072=( |
| 44 |
"--createek --rsa-keysize 3072" |
| 45 |
"--createek --create-ek-cert --create-platform-cert --config ${TESTDIR}/swtpm_setup.conf --vmid test --display --rsa-keysize 3072" |
| 46 |
"--createek --create-ek-cert --create-platform-cert --config ${TESTDIR}/swtpm_setup.conf --vmid test --display --keyfile ${TESTDIR}/data/keyfile.txt --rsa-keysize 3072" |
| 47 |
"--createek --create-ek-cert --create-platform-cert --config ${TESTDIR}/swtpm_setup.conf --vmid test --display --pwdfile ${TESTDIR}/data/pwdfile.txt --rsa-keysize 3072" |
| 48 |
"--createek --allow-signing --rsa-keysize 3072" |
| 49 |
"--createek --allow-signing --create-ek-cert --create-platform-cert --config ${TESTDIR}/swtpm_setup.conf --vmid test --display --rsa-keysize 3072" |
| 50 |
"--createek --allow-signing --create-ek-cert --create-platform-cert --config ${TESTDIR}/swtpm_setup.conf --vmid test --display --keyfile ${TESTDIR}/data/keyfile.txt --rsa-keysize 3072" |
| 51 |
"--createek --allow-signing --create-ek-cert --create-platform-cert --config ${TESTDIR}/swtpm_setup.conf --vmid test --display --pwdfile ${TESTDIR}/data/pwdfile.txt --rsa-keysize 3072" |
| 52 |
"--createek --allow-signing --decryption --create-ek-cert --create-platform-cert --config ${TESTDIR}/swtpm_setup.conf --vmid test --display --keyfile ${TESTDIR}/data/keyfile256bit.txt --cipher aes-256-cbc --rsa-keysize 3072" |
| 53 |
"--createek --allow-signing --decryption --create-ek-cert --create-platform-cert --config ${TESTDIR}/swtpm_setup.conf --vmid test --display --pwdfile ${TESTDIR}/data/pwdfile.txt --cipher aes-256-cbc --rsa-keysize 3072" |
| 54 |
"--createek --create-spk --create-ek-cert --create-platform-cert --config ${TESTDIR}/swtpm_setup.conf --vmid test --lock-nvram --rsa-keysize 3072" |
| 55 |
) |
| 56 |
|
| 57 |
# Open read-only file descriptors referenced in test cases |
| 58 |
exec 100<"${TESTDIR}/data/keyfile256bit.txt" |
| 59 |
exec 101<"${TESTDIR}/data/pwdfile.txt" |
| 60 |
|
| 61 |
# produced file size is always the same with TPM2 |
| 62 |
|
| 63 |
TPMDIR="$(mktemp -d)" || exit 1 |
| 64 |
TPMAUTHORING="$SWTPM_SETUP --tpm2 --config ${SWTPM_SETUP_CONF}" |
| 65 |
PATH=${ROOT}/src/swtpm_bios:$PATH |
| 66 |
|
| 67 |
|
| 68 |
trap "cleanup" SIGTERM EXIT |
| 69 |
|
| 70 |
function cleanup() |
| 71 |
{ |
| 72 |
if [ -n "$TPMDIR" ]; then |
| 73 |
rm -rf "$TPMDIR" |
| 74 |
fi |
| 75 |
} |
| 76 |
|
| 77 |
if $TPMAUTHORING --tpm2 --print-capabilities | grep -q tpm2-rsa-keysize-3072; then |
| 78 |
PARAMETERS+=( "${PARAMETERS_3072[@]}" ) |
| 79 |
fi |
| 80 |
|
| 81 |
# swtpm_setup.conf points to the local create_certs.sh |
| 82 |
# For create_certs.sh to be found (with out full path) |
| 83 |
# add this directory to the PATH |
| 84 |
PATH=$PATH:$TESTDIR |
| 85 |
|
| 86 |
for (( i=0; i<${#PARAMETERS[*]}; i++)); do |
| 87 |
rm -rf "${TPMDIR:?}"/* |
| 88 |
echo -n "Test $i: " |
| 89 |
params=${PARAMETERS[$i]} |
| 90 |
if ! $TPMAUTHORING \ |
| 91 |
--tpm-state "$TPMDIR" \ |
| 92 |
--tpm "$SWTPM_EXE socket ${SWTPM_TEST_SECCOMP_OPT}" \ |
| 93 |
${params:+${params}} &>/dev/null; |
| 94 |
then |
| 95 |
echo "ERROR: Test with parameters '${params}' failed." |
| 96 |
exit 1 |
| 97 |
elif [ ! -f "$TPMDIR/tpm2-00.permall" ]; then |
| 98 |
echo "ERROR: Test with parameters '${params}' did not |
| 99 |
produce file $TPMDIR/tpm2-00.permall." |
| 100 |
exit 1 |
| 101 |
fi |
| 102 |
|
| 103 |
# Make sure the state is encrypted when a key was given. |
| 104 |
# We expect sequences of 4 0-bytes in unencrypted state |
| 105 |
# and no such sequences in encrypted state. |
| 106 |
nullseq="$(od -t x1 -A n < "$TPMDIR/tpm2-00.permall" | tr -d '\n' | tr -s ' ' | |
| 107 |
grep "00 00 00 00")" |
| 108 |
if [[ "${params}" =~ (keyfile|pwdfile) ]]; then |
| 109 |
if [ -n "${nullseq}" ]; then |
| 110 |
echo "ERROR: State file is not encrypted with" \ |
| 111 |
"parameters '${params}'" |
| 112 |
fi |
| 113 |
else |
| 114 |
if [ -z "${nullseq}" ]; then |
| 115 |
echo "ERROR: State must not be encrypted with" \ |
| 116 |
"parameters '${params}'" |
| 117 |
fi |
| 118 |
fi |
| 119 |
|
| 120 |
echo "SUCCESS with parameters '${params}'." |
| 121 |
done |
| 122 |
|
| 123 |
exec 100>&- |
| 124 |
exec 101>&- |