Debian Codegraph @

tests/_test_tpm2_avoid_da_lockout

raw symbols ◀ ▶ SCIP
1 Debian patch touch this file
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 # 0x922 : retry command
58 RES=$(swtpm_cmd_tx "${SWTPM_INTERFACE}" ${cmd})
59 fi
60 exp=' 80 01 00 00 00 0a 00 00 09 8e'
61 if [ "$RES" != "$exp" ]; then
62 echo "Error: Did not get expected result from TPM2_NV_Write"
63 echo "expected: $exp"
64 echo "received: $RES"
65 exit 1
66 fi
67
68 # The TPM_PT_LOCKOUT_COUNTER must be 0 now: tssgetcapability -cap 6 -pr 0x20e -pc 1
69 cmd='\x80\x01\x00\x00\x00\x16\x00\x00\x01\x7a\x00\x00\x00\x06\x00\x00\x02\x0e\x00\x00\x00\x01'
70 RES=$(swtpm_cmd_tx "${SWTPM_INTERFACE}" ${cmd})
•71 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 01'
72 if [ "$RES" != "$exp" ]; then
•73 echo "Error: Did not get expected result from 1st TPM2_GetCapability(TPM_PT_LOCKOUT_COUNTER)"
74 echo "expected: $exp"
75 echo "received: $RES"
76 exit 1
77 fi
78
79 # Abruptly init the TPM: swtpm will internally send TPM2_Shutdown()
80 if ! run_swtpm_ioctl "${SWTPM_INTERFACE}" -i; then
81 echo "Error: Could not initialize the ${SWTPM_INTERFACE} TPM."
82 exit 1
83 fi
84
85 # send TPM2_Startup(SU_CLEAR)
86 cmd='\x80\x01\x00\x00\x00\x0c\x00\x00\x01\x44\x00\x00'
87 RES=$(swtpm_cmd_tx "${SWTPM_INTERFACE}" ${cmd})
88 exp=' 80 01 00 00 00 0a 00 00 00 00'
89 if [ "$RES" != "$exp" ]; then
90 echo "Error: Did not get expected result from TPM2_Startup(SU_CLEAR)"
91 echo "expected: $exp"
92 echo "received: $RES"
93 exit 1
94 fi
95
96 # The TPM_PT_LOCKOUT_COUNTER must still be '0' now: tssgetcapability -cap 6 -pr 0x20e -pc 1
97 # Without swtpm sending TPM2_Shutdown, it would be '1' now
98 cmd='\x80\x01\x00\x00\x00\x16\x00\x00\x01\x7a\x00\x00\x00\x06\x00\x00\x02\x0e\x00\x00\x00\x01'
99 RES=$(swtpm_cmd_tx "${SWTPM_INTERFACE}" ${cmd})
•100 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 01'
101 if [ "$RES" != "$exp" ]; then
•102 echo "Error: Did not get expected result from 2nd TPM2_GetCapability(TPM_PT_LOCKOUT_COUNTER)"
103 echo "expected: $exp"
104 echo "received: $RES"
105 exit 1
106 fi
107
108 # Again write to NV space without password: tssnvwrite -ha 01000000 -ic A
109 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'
110 RES=$(swtpm_cmd_tx "${SWTPM_INTERFACE}" ${cmd})
111 exp=' 80 01 00 00 00 0a 00 00 09 22'
•112 if [ "$RES" == "$exp" ]; then
113 # 0x922 : retry command
114 RES=$(swtpm_cmd_tx "${SWTPM_INTERFACE}" ${cmd})
115 fi
116 exp=' 80 01 00 00 00 0a 00 00 09 8e'
117 if [ "$RES" != "$exp" ]; then
118 echo "Error: Did not get expected result from TPM2_NV_Write"
119 echo "expected: $exp"
120 echo "received: $RES"
121 exit 1
122 fi
123
124 # CMD_STOP: swtpm will internally send TPM2_Shutdown()
125 if ! run_swtpm_ioctl "${SWTPM_INTERFACE}" -s; then
126 echo "Error: Could not shut down the ${SWTPM_INTERFACE} TPM."
127 exit 1
128 fi
129
130 if wait_process_gone "${SWTPM_PID}" 4; then
131 echo "Error: ${SWTPM_INTERFACE} TPM should not be running anymore."
132 exit 1
133 fi
134
135 TPM_PATH=$TPM_PATH run_swtpm "${SWTPM_INTERFACE}" \
136 --tpm2 \
137 --log "file=$LOG_FILE,level=20" \
138 --flags not-need-init,startup-clear
139
140 if ! kill_quiet -0 "${SWTPM_PID}"; then
141 echo "Error: ${SWTPM_INTERFACE} TPM did not start."
142 exit 1
143 fi
144
145 # The TPM_PT_LOCKOUT_COUNTER must still be '0' now: tssgetcapability -cap 6 -pr 0x20e -pc 1
146 # Without swtpm sending TPM2_Shutdown, it would be '2' now
147 cmd='\x80\x01\x00\x00\x00\x16\x00\x00\x01\x7a\x00\x00\x00\x06\x00\x00\x02\x0e\x00\x00\x00\x01'
148 RES=$(swtpm_cmd_tx "${SWTPM_INTERFACE}" ${cmd})
•149 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 02'
150 if [ "$RES" != "$exp" ]; then
•151 echo "Error: Did not get expected result from 3rd TPM2_GetCapability(TPM_PT_LOCKOUT_COUNTER)"
152 echo "expected: $exp"
153 echo "received: $RES"
154 exit 1
155 fi
156
157 if ! run_swtpm_ioctl "${SWTPM_INTERFACE}" -s; then
158 echo "Error: Could not shut down the ${SWTPM_INTERFACE} TPM."
159 exit 1
160 fi
161
162 if wait_process_gone "${SWTPM_PID}" 4; then
163 echo "Error: ${SWTPM_INTERFACE} TPM should not be running anymore."
164 exit 1
165 fi
166
167 echo "OK"
168
169 exit 0