| 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-avoid-da-lockout" |
| 10 |
SWTPM_DEV_NAME="/dev/${VTPM_NAME}" |
| 11 |
TPM_PATH="$(mktemp -d)" || exit 1 |
| 12 |
LOG_FILE=$TPM_PATH/tpm-00.log |
| 13 |
SWTPM_CMD_UNIX_PATH=${TPM_PATH}/unix-cmd.sock |
| 14 |
SWTPM_CTRL_UNIX_PATH=${TPM_PATH}/unix-ctrl.sock |
| 15 |
|
| 16 |
function cleanup() |
| 17 |
{ |
| 18 |
pid=${SWTPM_PID} |
| 19 |
if [ -n "$pid" ]; then |
| 20 |
kill_quiet -9 "$pid" |
| 21 |
fi |
| 22 |
rm -rf "$TPM_PATH" |
| 23 |
} |
| 24 |
|
| 25 |
trap "cleanup" EXIT |
| 26 |
|
| 27 |
source "${TESTDIR}/common" |
| 28 |
source "${TESTDIR}/test_common" |
| 29 |
[ "${SWTPM_INTERFACE}" == "cuse" ] && source "${TESTDIR}/test_cuse" |
| 30 |
|
| 31 |
TPM_PATH=$TPM_PATH run_swtpm "${SWTPM_INTERFACE}" \ |
| 32 |
--tpm2 \ |
| 33 |
--log "file=$LOG_FILE,level=20" \ |
| 34 |
--flags not-need-init,startup-clear |
| 35 |
|
| 36 |
if ! kill_quiet -0 "${SWTPM_PID}"; then |
| 37 |
echo "Error: ${SWTPM_INTERFACE} TPM did not start." |
| 38 |
exit 1 |
| 39 |
fi |
| 40 |
|
| 41 |
# Define password-protected NV space with DA attribute set: tssnvdefinespace -ha 01000000 -hi o -pwdn test -sz 1 -at da |
| 42 |
cmd='\x80\x02\x00\x00\x00\x31\x00\x00\x01\x2a\x40\x00\x00\x01\x00\x00\x00\x09\x40\x00\x00\x09\x00\x00\x00\x00\x00\x00\x04\x74\x65\x73\x74\x00\x0e\x01\x00\x00\x00\x00\x0b\x00\x04\x00\x04\x00\x00\x00\x01' |
| 43 |
RES=$(swtpm_cmd_tx "${SWTPM_INTERFACE}" ${cmd}) |
| 44 |
exp=' 80 02 00 00 00 13 00 00 00 00 00 00 00 00 00 00 01 00 00' |
| 45 |
if [ "$RES" != "$exp" ]; then |
| 46 |
echo "Error: Did not get expected result from TPM2_NV_DefineSpace" |
| 47 |
echo "expected: $exp" |
| 48 |
echo "received: $RES" |
| 49 |
exit 1 |
| 50 |
fi |
| 51 |
|
| 52 |
# Write to NV space without password: tssnvwrite -ha 01000000 -ic A |
| 53 |
cmd='\x80\x02\x00\x00\x00\x24\x00\x00\x01\x37\x01\x00\x00\x00\x01\x00\x00\x00\x00\x00\x00\x09\x40\x00\x00\x09\x00\x00\x00\x00\x00\x00\x01\x41\x00\x00' |
| 54 |
RES=$(swtpm_cmd_tx "${SWTPM_INTERFACE}" ${cmd}) |
| 55 |
exp=' 80 01 00 00 00 0a 00 00 09 22' |
| 56 |
if [ "$RES" != "$exp" ]; then |
| 57 |
echo "Error: Did not get expected result from TPM2_NV_Write" |
| 58 |
echo "expected: $exp" |
| 59 |
echo "received: $RES" |
| 60 |
exit 1 |
| 61 |
fi |
| 62 |
|
| 63 |
# The TPM_PT_LOCKOUT_COUNTER must be 0 now: tssgetcapability -cap 6 -pr 0x20e -pc 1 |
| 64 |
cmd='\x80\x01\x00\x00\x00\x16\x00\x00\x01\x7a\x00\x00\x00\x06\x00\x00\x02\x0e\x00\x00\x00\x01' |
| 65 |
RES=$(swtpm_cmd_tx "${SWTPM_INTERFACE}" ${cmd}) |
| 66 |
exp=' 80 01 00 00 00 1b 00 00 00 00 01 00 00 00 06 00 00 00 01 00 00 02 0e 00 00 00 00' |
| 67 |
if [ "$RES" != "$exp" ]; then |
| 68 |
echo "Error: Did not get expected result from TPM2_GetCapability(TPM_PT_LOCKOUT_COUNTER)" |
| 69 |
echo "expected: $exp" |
| 70 |
echo "received: $RES" |
| 71 |
exit 1 |
| 72 |
fi |
| 73 |
|
| 74 |
# Abruptly init the TPM: swtpm will internally send TPM2_Shutdown() |
| 75 |
if ! run_swtpm_ioctl "${SWTPM_INTERFACE}" -i; then |
| 76 |
echo "Error: Could not initialize the ${SWTPM_INTERFACE} TPM." |
| 77 |
exit 1 |
| 78 |
fi |
| 79 |
|
| 80 |
# send TPM2_Startup(SU_CLEAR) |
| 81 |
cmd='\x80\x01\x00\x00\x00\x0c\x00\x00\x01\x44\x00\x00' |
| 82 |
RES=$(swtpm_cmd_tx "${SWTPM_INTERFACE}" ${cmd}) |
| 83 |
exp=' 80 01 00 00 00 0a 00 00 00 00' |
| 84 |
if [ "$RES" != "$exp" ]; then |
| 85 |
echo "Error: Did not get expected result from TPM2_Startup(SU_CLEAR)" |
| 86 |
echo "expected: $exp" |
| 87 |
echo "received: $RES" |
| 88 |
exit 1 |
| 89 |
fi |
| 90 |
|
| 91 |
# The TPM_PT_LOCKOUT_COUNTER must still be '0' now: tssgetcapability -cap 6 -pr 0x20e -pc 1 |
| 92 |
# Without swtpm sending TPM2_Shutdown, it would be '1' now |
| 93 |
cmd='\x80\x01\x00\x00\x00\x16\x00\x00\x01\x7a\x00\x00\x00\x06\x00\x00\x02\x0e\x00\x00\x00\x01' |
| 94 |
RES=$(swtpm_cmd_tx "${SWTPM_INTERFACE}" ${cmd}) |
| 95 |
exp=' 80 01 00 00 00 1b 00 00 00 00 01 00 00 00 06 00 00 00 01 00 00 02 0e 00 00 00 00' |
| 96 |
if [ "$RES" != "$exp" ]; then |
| 97 |
echo "Error: Did not get expected result from TPM2_GetCapability(TPM_PT_LOCKOUT_COUNTER)" |
| 98 |
echo "expected: $exp" |
| 99 |
echo "received: $RES" |
| 100 |
exit 1 |
| 101 |
fi |
| 102 |
|
| 103 |
# Again write to NV space without password: tssnvwrite -ha 01000000 -ic A |
| 104 |
cmd='\x80\x02\x00\x00\x00\x24\x00\x00\x01\x37\x01\x00\x00\x00\x01\x00\x00\x00\x00\x00\x00\x09\x40\x00\x00\x09\x00\x00\x00\x00\x00\x00\x01\x41\x00\x00' |
| 105 |
RES=$(swtpm_cmd_tx "${SWTPM_INTERFACE}" ${cmd}) |
| 106 |
exp=' 80 01 00 00 00 0a 00 00 09 22' |
| 107 |
if [ "$RES" != "$exp" ]; then |
| 108 |
echo "Error: Did not get expected result from TPM2_NV_Write" |
| 109 |
echo "expected: $exp" |
| 110 |
echo "received: $RES" |
| 111 |
exit 1 |
| 112 |
fi |
| 113 |
|
| 114 |
# CMD_STOP: swtpm will internally send TPM2_Shutdown() |
| 115 |
if ! run_swtpm_ioctl "${SWTPM_INTERFACE}" -s; then |
| 116 |
echo "Error: Could not shut down the ${SWTPM_INTERFACE} TPM." |
| 117 |
exit 1 |
| 118 |
fi |
| 119 |
|
| 120 |
if wait_process_gone "${SWTPM_PID}" 4; then |
| 121 |
echo "Error: ${SWTPM_INTERFACE} TPM should not be running anymore." |
| 122 |
exit 1 |
| 123 |
fi |
| 124 |
|
| 125 |
TPM_PATH=$TPM_PATH run_swtpm "${SWTPM_INTERFACE}" \ |
| 126 |
--tpm2 \ |
| 127 |
--log "file=$LOG_FILE,level=20" \ |
| 128 |
--flags not-need-init,startup-clear |
| 129 |
|
| 130 |
if ! kill_quiet -0 "${SWTPM_PID}"; then |
| 131 |
echo "Error: ${SWTPM_INTERFACE} TPM did not start." |
| 132 |
exit 1 |
| 133 |
fi |
| 134 |
|
| 135 |
# The TPM_PT_LOCKOUT_COUNTER must still be '0' now: tssgetcapability -cap 6 -pr 0x20e -pc 1 |
| 136 |
# Without swtpm sending TPM2_Shutdown, it would be '2' now |
| 137 |
cmd='\x80\x01\x00\x00\x00\x16\x00\x00\x01\x7a\x00\x00\x00\x06\x00\x00\x02\x0e\x00\x00\x00\x01' |
| 138 |
RES=$(swtpm_cmd_tx "${SWTPM_INTERFACE}" ${cmd}) |
| 139 |
exp=' 80 01 00 00 00 1b 00 00 00 00 01 00 00 00 06 00 00 00 01 00 00 02 0e 00 00 00 00' |
| 140 |
if [ "$RES" != "$exp" ]; then |
| 141 |
echo "Error: Did not get expected result from TPM2_GetCapability(TPM_PT_LOCKOUT_COUNTER)" |
| 142 |
echo "expected: $exp" |
| 143 |
echo "received: $RES" |
| 144 |
exit 1 |
| 145 |
fi |
| 146 |
|
| 147 |
if ! run_swtpm_ioctl "${SWTPM_INTERFACE}" -s; then |
| 148 |
echo "Error: Could not shut down the ${SWTPM_INTERFACE} TPM." |
| 149 |
exit 1 |
| 150 |
fi |
| 151 |
|
| 152 |
if wait_process_gone "${SWTPM_PID}" 4; then |
| 153 |
echo "Error: ${SWTPM_INTERFACE} TPM should not be running anymore." |
| 154 |
exit 1 |
| 155 |
fi |
| 156 |
|
| 157 |
echo "OK" |
| 158 |
|
| 159 |
exit 0 |