]> git.proxmox.com Git - swtpm.git/blob - tests/_test_volatilestate
packaging: track dbgsym package for swtpm-libs and swtpm-tools
[swtpm.git] / tests / _test_volatilestate
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-volatilestate"
10 SWTPM_DEV_NAME="/dev/${VTPM_NAME}"
11 export TPM_PATH="$(mktemp -d)" || exit 1
12 STATE_FILE=$TPM_PATH/tpm-00.permall
13 VOLATILE_STATE_FILE=$TPM_PATH/tpm-00.volatilestate
14 SWTPM_CMD_UNIX_PATH=${TPM_PATH}/unix-cmd.sock
15 SWTPM_CTRL_UNIX_PATH=${TPM_PATH}/unix-ctrl.sock
16 SWTPM_INTERFACE=${SWTPM_INTERFACE:-cuse}
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 [ "${SWTPM_INTERFACE}" == cuse ] && source ${TESTDIR}/test_cuse
30 source ${TESTDIR}/common
31
32 rm -f $STATE_FILE $VOLATILE_STATE_FILE 2>/dev/null
33
34 run_swtpm ${SWTPM_INTERFACE}
35
36 display_processes_by_name "$SWTPM"
37
38 kill_quiet -0 ${SWTPM_PID}
39 if [ $? -ne 0 ]; then
40 echo "Error: ${SWTPM_INTERFACE} TPM did not start."
41 exit 1
42 fi
43
44 # Init the TPM
45 run_swtpm_ioctl ${SWTPM_INTERFACE} -i
46 if [ $? -ne 0 ]; then
47 echo "Error: Could not initialize the ${SWTPM_INTERFACE} TPM."
48 exit 1
49 fi
50
51 kill_quiet -0 ${SWTPM_PID} 2>/dev/null
52 if [ $? -ne 0 ]; then
53 echo "Error: ${SWTPM_INTERFACE} TPM not running anymore after INIT."
54 exit 1
55 fi
56
57 # Startup the TPM
58 RES=$(swtpm_cmd_tx ${SWTPM_INTERFACE} '\x00\xC1\x00\x00\x00\x0C\x00\x00\x00\x99\x00\x01')
59 exp=' 00 c4 00 00 00 0a 00 00 00 00'
60 if [ "$RES" != "$exp" ]; then
61 echo "Error: Did not get expected result from TPM_Startup(ST_Clear)"
62 echo "expected: $exp"
63 echo "received: $RES"
64 exit 1
65 fi
66
67 run_swtpm_ioctl ${SWTPM_INTERFACE} -h 1234
68 if [ $? -ne 0 ]; then
69 echo "Error: Hash command did not work."
70 exit 1
71 fi
72
73 # Read PCR 17
74 RES=$(swtpm_cmd_tx ${SWTPM_INTERFACE} '\x00\xC1\x00\x00\x00\x0E\x00\x00\x00\x15\x00\x00\x00\x11')
75 exp=' 00 c4 00 00 00 1e 00 00 00 00 97 e9 76 e4 f2 2c d6 d2 4a fd 21 20 85 ad 7a 86 64 7f 2a e5'
76 if [ "$RES" != "$exp" ]; then
77 echo "Error: (1) Did not get expected result from TPM_PCRRead(17)"
78 echo "expected: $exp"
79 echo "received: $RES"
80 exit 1
81 fi
82
83 # Check the TPM Established bit after the hashing
84 RES=$(run_swtpm_ioctl ${SWTPM_INTERFACE} -e)
85 exp='tpmEstablished is 1'
86 if [ "$RES" != "$exp" ]; then
87 echo "Error (2): TPM Established flag has wrong value."
88 echo "expected: $exp"
89 echo "received: $RES"
90 exit 1
91 fi
92
93 # Save the volatile state
94 run_swtpm_ioctl ${SWTPM_INTERFACE} -v
95 if [ $? -ne 0 ]; then
96 echo "Error: Could not have the ${SWTPM_INTERFACE} TPM store the volatile state to a file."
97 exit 1
98 fi
99
100 if [ ! -r $VOLATILE_STATE_FILE ]; then
101 echo "Error: Volatile state file $VOLATILE_STATE_FILE does not exist."
102 exit 1
103 fi
104
105 # Shut the TPM down
106 run_swtpm_ioctl ${SWTPM_INTERFACE} -s
107 if [ $? -ne 0 ]; then
108 echo "Error: Could not shut down the ${SWTPM_INTERFACE} TPM."
109 exit 1
110 fi
111
112 # Start the TPM again
113 run_swtpm ${SWTPM_INTERFACE}
114
115 display_processes_by_name "$SWTPM"
116
117 kill_quiet -0 ${SWTPM_PID}
118 if [ $? -ne 0 ]; then
119 echo "Error: ${SWTPM_INTERFACE} TPM did not start."
120 exit 1
121 fi
122
123 # Init the TPM
124 run_swtpm_ioctl ${SWTPM_INTERFACE} -i
125 if [ $? -ne 0 ]; then
126 echo "Error: Could not initialize the ${SWTPM_INTERFACE} TPM."
127 exit 1
128 fi
129
130 # Volatile state must have been removed by TPM now
131 if [ -r $VOLATILE_STATE_FILE ]; then
132 echo "Error: Volatile state file $VOLATILE_STATE_FILE still exists."
133 exit 1
134 fi
135
136 # Read the PCR again ...
137 RES=$(swtpm_cmd_tx ${SWTPM_INTERFACE} '\x00\xC1\x00\x00\x00\x0E\x00\x00\x00\x15\x00\x00\x00\x11')
138 exp=' 00 c4 00 00 00 1e 00 00 00 00 97 e9 76 e4 f2 2c d6 d2 4a fd 21 20 85 ad 7a 86 64 7f 2a e5'
139 if [ "$RES" != "$exp" ]; then
140 echo "Error: (2) Did not get expected result from TPM_PCRRead(17)"
141 echo "expected: $exp"
142 echo "received: $RES"
143 exit 1
144 fi
145
146 # Check that the TPM Established bit is still set
147 RES=$(run_swtpm_ioctl ${SWTPM_INTERFACE} -e)
148 exp='tpmEstablished is 1'
149 if [ "$RES" != "$exp" ]; then
150 echo "Error (2): TPM Established flag has wrong value."
151 echo "expected: $exp"
152 echo "received: $RES"
153 exit 1
154 fi
155
156 # Save the volatile state again
157 run_swtpm_ioctl ${SWTPM_INTERFACE} -v
158 if [ $? -ne 0 ]; then
159 echo "Error: Could not have the ${SWTPM_INTERFACE} TPM store the volatile state to a file."
160 exit 1
161 fi
162
163 if [ ! -r $VOLATILE_STATE_FILE ]; then
164 echo "Error: Volatile state file $VOLATILE_STATE_FILE does not exist."
165 exit 1
166 fi
167
168 # Send a new TPM_Init
169 run_swtpm_ioctl ${SWTPM_INTERFACE} -i
170 if [ $? -ne 0 ]; then
171 echo "Error: Could not initialize the ${SWTPM_INTERFACE} TPM."
172 exit 1
173 fi
174
175 # Volatile state must have been removed by TPM now
176 if [ -r $VOLATILE_STATE_FILE ]; then
177 echo "Error: Volatile state file $VOLATILE_STATE_FILE still exists."
178 exit 1
179 fi
180
181 # Read the PCR again ...
182 RES=$(swtpm_cmd_tx ${SWTPM_INTERFACE} '\x00\xC1\x00\x00\x00\x0E\x00\x00\x00\x15\x00\x00\x00\x11')
183 exp=' 00 c4 00 00 00 1e 00 00 00 00 97 e9 76 e4 f2 2c d6 d2 4a fd 21 20 85 ad 7a 86 64 7f 2a e5'
184 if [ "$RES" != "$exp" ]; then
185 echo "Error: (2) Did not get expected result from TPM_PCRRead(17)"
186 echo "expected: $exp"
187 echo "received: $RES"
188 exit 1
189 fi
190
191 # Check that the TPM Established bit is still set
192 RES=$(run_swtpm_ioctl ${SWTPM_INTERFACE} -e)
193 if [ $? -ne 0 ]; then
194 echo "Error: Could not get the TPM Established flag from the ${SWTPM_INTERFACE} TPM."
195 exit 1
196 fi
197 exp='tpmEstablished is 1'
198 if [ "$RES" != "$exp" ]; then
199 echo "Error (2): TPM Established flag has wrong value."
200 echo "expected: $exp"
201 echo "received: $RES"
202 exit 1
203 fi
204
205 # Final shut down
206 run_swtpm_ioctl ${SWTPM_INTERFACE} -s
207 if [ $? -ne 0 ]; then
208 echo "Error: Could not shut down the ${SWTPM_INTERFACE} TPM."
209 exit 1
210 fi
211
212 if wait_process_gone ${SWTPM_PID} 4; then
213 echo "Error: ${SWTPM_INTERFACE} TPM should not be running anymore."
214 exit 1
215 fi
216
217 if [ ! -e $STATE_FILE ]; then
218 echo "Error: TPM state file $STATE_FILE does not exist."
219 exit 1
220 fi
221
222 echo "OK"
223
224 exit 0