| 1 |
#!/usr/bin/env bash |
| 2 |
|
| 3 |
# For the license, see the LICENSE file in the root directory. |
| 4 |
# set -x |
| 5 |
|
| 6 |
cd "$(dirname "$0")" || exit 1 |
| 7 |
|
| 8 |
ROOT=${abs_top_builddir:-$(pwd)/..} |
| 9 |
TESTDIR=${abs_top_testdir:-$(dirname "$0")} |
| 10 |
|
| 11 |
export SWTPM_INTERFACE=cuse |
| 12 |
|
| 13 |
VTPM_NAME="vtpm-test-tpm2-partial-reads" |
| 14 |
SWTPM_DEV_NAME="/dev/${VTPM_NAME}" |
| 15 |
TPM_PATH="$(mktemp -d)" || exit 1 |
| 16 |
CMD_PATH="${TPM_PATH}/cmd" |
| 17 |
STATE_FILE="$TPM_PATH/tpm2-00.permall" |
| 18 |
|
| 19 |
function cleanup() |
| 20 |
{ |
| 21 |
pid=${SWTPM_PID} |
| 22 |
if [ -n "$pid" ]; then |
| 23 |
kill_quiet -9 "$pid" |
| 24 |
fi |
| 25 |
rm -rf "$TPM_PATH" |
| 26 |
} |
| 27 |
|
| 28 |
function swtpm_read_n_bytes_fd100() |
| 29 |
{ |
| 30 |
# read n bytes from fd 100 and write to stdout |
| 31 |
python -c "import os; os.write(1, os.read(100, $1))" | \ |
| 32 |
od -t x1 -A n |
| 33 |
} |
| 34 |
|
| 35 |
trap "cleanup" EXIT |
| 36 |
|
| 37 |
source "${TESTDIR}/common" |
| 38 |
[ "${SWTPM_INTERFACE}" == "cuse" ] && source test_cuse |
| 39 |
skip_test_no_tpm20 "${SWTPM_EXE}" |
| 40 |
|
| 41 |
TPM_PATH=$TPM_PATH run_swtpm "${SWTPM_INTERFACE}" --tpm2 |
| 42 |
|
| 43 |
if ! kill_quiet -0 "${SWTPM_PID}"; then |
| 44 |
echo "Error: ${SWTPM_INTERFACE} TPM did not start." |
| 45 |
exit 1 |
| 46 |
fi |
| 47 |
|
| 48 |
# Init the TPM |
| 49 |
if ! run_swtpm_ioctl "${SWTPM_INTERFACE}" -i; then |
| 50 |
echo "Error: Could not initialize the ${SWTPM_INTERFACE} TPM." |
| 51 |
exit 1 |
| 52 |
fi |
| 53 |
|
| 54 |
if ! kill_quiet -0 "${SWTPM_PID}" 2>/dev/null; then |
| 55 |
echo "Error: ${SWTPM_INTERFACE} TPM not running anymore after INIT." |
| 56 |
exit 1 |
| 57 |
fi |
| 58 |
|
| 59 |
# Prepare the TPM2_Startup |
| 60 |
echo -en '\x80\x01\x00\x00\x00\x0c\x00\x00\x01\x44\x00\x00' > "${CMD_PATH}" |
| 61 |
|
| 62 |
swtpm_open_cmddev "${SWTPM_INTERFACE}" 100 |
| 63 |
|
| 64 |
# Startup the TPM2 |
| 65 |
cat "${CMD_PATH}" >&100 |
| 66 |
|
| 67 |
# Read 4 and then 6 bytes of the response |
| 68 |
res1=$(swtpm_read_n_bytes_fd100 4) |
| 69 |
exp1=' 80 01 00 00' |
| 70 |
if [ "$res1" != "$exp1" ]; then |
| 71 |
echo "1st Startup: Unexpected 1st response part" |
| 72 |
echo "Expected: $exp1" |
| 73 |
echo "Actual : $res1" |
| 74 |
exit 1 |
| 75 |
fi |
| 76 |
|
| 77 |
res2=$(swtpm_read_n_bytes_fd100 6) |
| 78 |
exp2=' 00 0a 00 00 00 00' |
| 79 |
if [ "$res2" != "$exp2" ]; then |
| 80 |
echo "1st Startup: Unexpected 2nd response part" |
| 81 |
echo "Expected: $exp2" |
| 82 |
echo "Actual : $res2" |
| 83 |
exit 1 |
| 84 |
fi |
| 85 |
|
| 86 |
# Startup the TPM2 again (will fail, but that's ok) |
| 87 |
cat "${CMD_PATH}" >&100 |
| 88 |
|
| 89 |
# Read 4 and then only 4 bytes of the response |
| 90 |
res1=$(swtpm_read_n_bytes_fd100 4) |
| 91 |
exp1=' 80 01 00 00' |
| 92 |
if [ "$res1" != "$exp1" ]; then |
| 93 |
echo "2nd Startup: Unexpected 1st response part" |
| 94 |
echo "Expected: $exp1" |
| 95 |
echo "Actual : $res1" |
| 96 |
exit 1 |
| 97 |
fi |
| 98 |
|
| 99 |
res2=$(swtpm_read_n_bytes_fd100 4) |
| 100 |
exp2=' 00 0a 00 00' |
| 101 |
if [ "$res2" != "$exp2" ]; then |
| 102 |
echo "2nd Startup: Unexpected 2nd part" |
| 103 |
echo "Expected: $exp2" |
| 104 |
echo "Actual : $res2" |
| 105 |
exit 1 |
| 106 |
fi |
| 107 |
|
| 108 |
# Startup the TPM2 again (will fail, but that's ok) |
| 109 |
cat "${CMD_PATH}" >&100 |
| 110 |
|
| 111 |
# Read 4 and then 6 bytes of the response |
| 112 |
res1=$(swtpm_read_n_bytes_fd100 4) |
| 113 |
exp1=' 80 01 00 00' |
| 114 |
if [ "$res1" != "$exp1" ]; then |
| 115 |
echo "3rd Startup: Unexpected 1st response part" |
| 116 |
echo "Expected: $exp1" |
| 117 |
echo "Actual : $res1" |
| 118 |
exit 1 |
| 119 |
fi |
| 120 |
|
| 121 |
res2=$(swtpm_read_n_bytes_fd100 6) |
| 122 |
exp2=' 00 0a 00 00 01 00' |
| 123 |
if [ "$res2" != "$exp2" ]; then |
| 124 |
echo "3rd Startup: Unexpected 2nd part" |
| 125 |
echo "Expected: $exp2" |
| 126 |
echo "Actual : $res2" |
| 127 |
exit 1 |
| 128 |
fi |
| 129 |
exec 100>&- |
| 130 |
|
| 131 |
if ! run_swtpm_ioctl "${SWTPM_INTERFACE}" -s; then |
| 132 |
echo "Error: Could not shut down the ${SWTPM_INTERFACE} TPM." |
| 133 |
exit 1 |
| 134 |
fi |
| 135 |
|
| 136 |
if wait_process_gone "${SWTPM_PID}" 4; then |
| 137 |
echo "Error: ${SWTPM_INTERFACE} TPM should not be running anymore." |
| 138 |
exit 1 |
| 139 |
fi |
| 140 |
|
| 141 |
if [ ! -e "$STATE_FILE" ]; then |
| 142 |
echo "Error: TPM state file $STATE_FILE does not exist." |
| 143 |
exit 1 |
| 144 |
fi |
| 145 |
|
| 146 |
echo "OK" |
| 147 |
|
| 148 |
exit 0 |