| 1 |
#!/bin/bash |
| 2 |
|
| 3 |
# For the license, see the LICENSE file in the root directory. |
| 4 |
#set -x |
| 5 |
|
| 6 |
ROOT=${abs_top_builddir:-$(pwd)/..} |
| 7 |
TESTDIR=${abs_top_testdir:-$(dirname "$0")} |
| 8 |
|
| 9 |
VTPM_NAME="vtpm-test-tpm2-hashing3" |
| 10 |
SWTPM_DEV_NAME="/dev/${VTPM_NAME}" |
| 11 |
TPM_PATH="$(mktemp -d)" || exit 1 |
| 12 |
STATE_FILE=$TPM_PATH/tpm2-00.permall |
| 13 |
VOLATILE_STATE_FILE=$TPM_PATH/tpm2-00.volatilestate |
| 14 |
SWTPM_INTERFACE=${SWTPM_INTERFACE:-cuse} |
| 15 |
SWTPM_CMD_UNIX_PATH=${TPM_PATH}/unix-cmd.sock |
| 16 |
SWTPM_CTRL_UNIX_PATH=${TPM_PATH}/unix-ctrl.sock |
| 17 |
|
| 18 |
function cleanup() |
| 19 |
{ |
| 20 |
pid=${SWTPM_PID} |
| 21 |
if [ -n "$pid" ]; then |
| 22 |
kill_quiet -9 "$pid" |
| 23 |
fi |
| 24 |
rm -rf "$TPM_PATH" |
| 25 |
} |
| 26 |
|
| 27 |
trap "cleanup" EXIT |
| 28 |
|
| 29 |
source "${TESTDIR}/common" |
| 30 |
[ "${SWTPM_INTERFACE}" == "cuse" ] && source "${TESTDIR}/test_cuse" |
| 31 |
|
| 32 |
rm -f "$STATE_FILE" "$VOLATILE_STATE_FILE" 2>/dev/null |
| 33 |
|
| 34 |
TPM_PATH=$TPM_PATH run_swtpm "${SWTPM_INTERFACE}" --tpm2 |
| 35 |
|
| 36 |
display_processes_by_name "swtpm" |
| 37 |
|
| 38 |
if ! kill_quiet -0 "${SWTPM_PID}"; then |
| 39 |
echo "Error: ${SWTPM_INTERFACE} TPM did not start." |
| 40 |
exit 1 |
| 41 |
fi |
| 42 |
|
| 43 |
# Init the TPM |
| 44 |
if ! run_swtpm_ioctl "${SWTPM_INTERFACE}" -i; then |
| 45 |
echo "Error: Could not initialize the ${SWTPM_INTERFACE} TPM." |
| 46 |
exit 1 |
| 47 |
fi |
| 48 |
|
| 49 |
if ! kill_quiet -0 "${SWTPM_PID}" 2>/dev/null; then |
| 50 |
echo "Error: ${SWTPM_INTERFACE} TPM not running anymore after INIT." |
| 51 |
exit 1 |
| 52 |
fi |
| 53 |
|
| 54 |
# We do NOT need to startup the TPM; Hashing will affect PCR 0 |
| 55 |
|
| 56 |
# Check the TPM Established bit before the hashing |
| 57 |
if ! RES=$(run_swtpm_ioctl "${SWTPM_INTERFACE}" -e); then |
| 58 |
echo "Error: Could not get the TPM Established bit from the ${SWTPM_INTERFACE} TPM." |
| 59 |
exit 1 |
| 60 |
fi |
| 61 |
|
| 62 |
exp='tpmEstablished is 0' |
| 63 |
if [ "$RES" != "$exp" ]; then |
| 64 |
echo "Error (1): TPM Established flag has wrong value." |
| 65 |
echo "expected: $exp" |
| 66 |
echo "received: $RES" |
| 67 |
exit 1 |
| 68 |
fi |
| 69 |
|
| 70 |
# Hash |
| 71 |
if ! run_swtpm_ioctl "${SWTPM_INTERFACE}" -h 1234; then |
| 72 |
echo "Error: The hash command failed." |
| 73 |
exit 1 |
| 74 |
fi |
| 75 |
|
| 76 |
# Startup the TPM2 |
| 77 |
RES=$(swtpm_cmd_tx "${SWTPM_INTERFACE}" '\x80\x01\x00\x00\x00\x0C\x00\x00\x01\x44\x00\x00') |
| 78 |
exp=' 80 01 00 00 00 0a 00 00 00 00' |
| 79 |
if [ "$RES" != "$exp" ]; then |
| 80 |
echo "Error: Did not get expected result from TPM_Startup(ST_Clear)" |
| 81 |
echo "expected: $exp" |
| 82 |
echo "received: $RES" |
| 83 |
exit 1 |
| 84 |
fi |
| 85 |
|
| 86 |
# Read PCR 0 |
| 87 |
# length CC count hashalg sz |
| 88 |
RES=$(swtpm_cmd_tx "${SWTPM_INTERFACE}" '\x80\x01\x00\x00\x00\x14\x00\x00\x01\x7e\x00\x00\x00\x01\x00\x0b\x03\x01\x00\x00') |
| 89 |
exp=' 80 01 00 00 00 3e 00 00 00 00 00 00 00 14 00 00 00 01 00 0b 03 01 00 00 00 00 00 01 00 20 34 0a 23 3f ac 4c a0 14 98 6e 45 dd 95 ec 77 6a 7a 3f 86 a2 10 74 f4 3a 43 90 c5 b8 c3 ab ea f4' |
| 90 |
if [ "$RES" != "$exp" ]; then |
| 91 |
echo "Error: (1) Did not get expected result from TPM_PCRRead(0)" |
| 92 |
echo "expected: $exp" |
| 93 |
echo "received: $RES" |
| 94 |
exit 1 |
| 95 |
fi |
| 96 |
|
| 97 |
# Check the TPM Established bit after the hashing |
| 98 |
if ! RES=$(run_swtpm_ioctl "${SWTPM_INTERFACE}" -e); then |
| 99 |
echo "Error: Could not get the TPM Established bit from the ${SWTPM_INTERFACE} TPM." |
| 100 |
exit 1 |
| 101 |
fi |
| 102 |
|
| 103 |
exp='tpmEstablished is 1' |
| 104 |
if [ "$RES" != "$exp" ]; then |
| 105 |
echo "Error (2): TPM Established flag has wrong value." |
| 106 |
echo "expected: $exp" |
| 107 |
echo "received: $RES" |
| 108 |
exit 1 |
| 109 |
fi |
| 110 |
|
| 111 |
if ! run_swtpm_ioctl "${SWTPM_INTERFACE}" -s; then |
| 112 |
echo "Error: Could not shut down the ${SWTPM_INTERFACE} TPM." |
| 113 |
exit 1 |
| 114 |
fi |
| 115 |
|
| 116 |
if wait_process_gone "${SWTPM_PID}" 4; then |
| 117 |
echo "Error: ${SWTPM_INTERFACE} TPM should not be running anymore." |
| 118 |
exit 1 |
| 119 |
fi |
| 120 |
|
| 121 |
echo "OK" |
| 122 |
|
| 123 |
exit 0 |