]> git.proxmox.com Git - swtpm.git/blob - tests/_test_tpm2_encrypted_state
tests: Call function rather than running 'ps aux' to display proceses
[swtpm.git] / tests / _test_tpm2_encrypted_state
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_NAME:-vtpm-test-tpm2-encrypted-state}
10 SWTPM_DEV_NAME="/dev/${VTPM_NAME}"
11 export TPM_PATH=$(mktemp -d)
12 STATE_FILE=$TPM_PATH/tpm2-00.permall
13 VOLATILE_STATE_FILE=$TPM_PATH/tpm2-00.volatilestate
14 KEY=1234567890abcdef1234567890abcdef
15 SWTPM_INTERFACE=${SWTPM_INTERFACE:-cuse}
16 SWTPM_CMD_UNIX_PATH=${TPM_PATH}/unix-cmd.sock
17 SWTPM_CTRL_UNIX_PATH=${TPM_PATH}/unix-ctrl.sock
18
19 keyfile=$(mktemp)
20 logfile=$(mktemp)
21 echo "$KEY" > $keyfile
22
23 function cleanup()
24 {
25 pid=${SWTPM_PID}
26 if [ -n "$pid" ]; then
27 kill_quiet -9 $pid
28 fi
29 rm -f $keyfile $logfile
30 rm -rf $TPM_PATH
31 }
32
33 trap "cleanup" EXIT
34
35 [ "${SWTPM_INTERFACE}" == "cuse" ] && source ${TESTDIR}/test_cuse
36 source ${TESTDIR}/common
37
38 rm -f $STATE_FILE $VOLATILE_STATE_FILE 2>/dev/null
39
40 run_swtpm ${SWTPM_INTERFACE} \
41 --key file=$keyfile,mode=aes-cbc,format=hex \
42 --log file=$logfile \
43 --tpm2
44
45 display_processes_by_name "$SWTPM"
46
47 kill_quiet -0 ${SWTPM_PID}
48 if [ $? -ne 0 ]; then
49 echo "Error: ${SWTPM_INTERFACE} TPM did not start."
50 echo "TPM Logfile:"
51 cat $logfile
52 exit 1
53 fi
54
55 # Init the TPM
56 run_swtpm_ioctl ${SWTPM_INTERFACE} -i
57 if [ $? -ne 0 ]; then
58 echo "Error: ${SWTPM_INTERFACE} TPM initialization failed."
59 echo "TPM Logfile:"
60 cat $logfile
61 exit 1
62 fi
63
64 kill_quiet -0 ${SWTPM_PID} 2>/dev/null
65 if [ $? -ne 0 ]; then
66 echo "Error: ${SWTPM_INTERFACE} TPM not running anymore after INIT."
67 echo "TPM Logfile:"
68 cat $logfile
69 exit 1
70 fi
71
72 # Startup the TPM
73 swtpm_open_cmddev ${SWTPM_INTERFACE} 100
74 RES=$(swtpm_cmd_tx ${SWTPM_INTERFACE} '\x80\x01\x00\x00\x00\x0c\x00\x00\x01\x44\x00\x00')
75 exp=' 80 01 00 00 00 0a 00 00 00 00'
76 if [ "$RES" != "$exp" ]; then
77 echo "Error: Did not get expected result from TPM2_Startup(SU_Clear)"
78 echo "expected: $exp"
79 echo "received: $RES"
80 exit 1
81 fi
82
83 run_swtpm_ioctl ${SWTPM_INTERFACE} -h 1234
84 if [ $? -ne 0 ]; then
85 echo "Error: Hash command did not work."
86 echo "TPM Logfile:"
87 cat $logfile
88 exit 1
89 fi
90
91 # Read PCR 17
92 swtpm_open_cmddev ${SWTPM_INTERFACE} 100
93 RES=$(swtpm_cmd_tx ${SWTPM_INTERFACE} '\x80\x01\x00\x00\x00\x14\x00\x00\x01\x7e\x00\x00\x00\x01\x00\x0b\x03\x00\x00\x02')
94 exp=' 80 01 00 00 00 3e 00 00 00 00 00 00 00 19 00 00 00 01 00 0b 03 00 00 02 00 00 00 01 00 20 fc a5 d6 49 bf b0 c9 22 fd 33 0f 79 b2 00 43 28 9d af d6 0d 01 a4 c4 37 3c f2 8a db 56 c9 b4 54'
95 if [ "$RES" != "$exp" ]; then
96 echo "Error: (1) Did not get expected result from TPM_PCRRead(17)"
97 echo "expected: $exp"
98 echo "received: $RES"
99 exit 1
100 fi
101
102 # Save the volatile state
103 run_swtpm_ioctl ${SWTPM_INTERFACE} -v
104 if [ $? -ne 0 ]; then
105 echo "Error: Saving the volatile state failed."
106 echo "TPM Logfile:"
107 cat $logfile
108 exit 1
109 fi
110
111 if [ ! -r $VOLATILE_STATE_FILE ]; then
112 echo "Error: Volatile state file $VOLATILE_STATE_FILE does not exist."
113 echo "TPM Logfile:"
114 cat $logfile
115 exit 1
116 fi
117
118 tmp=$(run_swtpm_ioctl ${SWTPM_INTERFACE} -g | cut -d":" -f2)
119 if [ $? -ne 0 ]; then
120 echo "Error: Could not get the configration flags of the ${SWTPM_INTERFACE} TPM."
121 echo "TPM Logfile:"
122 cat $logfile
123 exit 1
124 fi
125
126 if [ "$tmp" != " 0x1" ]; then
127 echo "Error: Unexpected configuration flags: $tmp; expected 0x1."
128 echo "TPM Logfile:"
129 cat $logfile
130 exit 1
131 fi
132
133 # Shut the TPM down
134 exec 100>&-
135 run_swtpm_ioctl ${SWTPM_INTERFACE} -s
136 if [ $? -ne 0 ]; then
137 echo "Error: Could not shut down the ${SWTPM_INTERFACE} TPM."
138 echo "TPM Logfile:"
139 cat $logfile
140 exit 1
141 fi
142
143 # Start the TPM again; have the keyfile removed
144 run_swtpm ${SWTPM_INTERFACE} \
145 --key file=$keyfile,mode=aes-cbc,format=hex,remove \
146 --log file=$logfile \
147 --tpm2
148
149 display_processes_by_name "$SWTPM"
150
151 kill_quiet -0 ${SWTPM_PID}
152 if [ $? -ne 0 ]; then
153 echo "Error (2): ${SWTPM_INTERFACE} TPM did not start."
154 echo "TPM Logfile:"
155 cat $logfile
156 exit 1
157 fi
158
159 if [ -r $keyfile ]; then
160 echo "Error: Keyfile $keyfile was not removed by swtpm_cuse."
161 echo "TPM Logfile:"
162 cat $logfile
163 exit 1
164 fi
165
166 # Init the TPM
167 run_swtpm_ioctl ${SWTPM_INTERFACE} -i
168 if [ $? -ne 0 ]; then
169 echo "Error: ${SWTPM_INTERFACE} TPM initialization failed."
170 echo "TPM Logfile:"
171 cat $logfile
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 echo "TPM Logfile:"
179 cat $logfile
180 exit 1
181 fi
182
183 # Read the PCR again ...
184 swtpm_open_cmddev ${SWTPM_INTERFACE} 100
185 RES=$(swtpm_cmd_tx ${SWTPM_INTERFACE} '\x80\x01\x00\x00\x00\x14\x00\x00\x01\x7e\x00\x00\x00\x01\x00\x0b\x03\x00\x00\x02')
186 exp=' 80 01 00 00 00 3e 00 00 00 00 00 00 00 19 00 00 00 01 00 0b 03 00 00 02 00 00 00 01 00 20 fc a5 d6 49 bf b0 c9 22 fd 33 0f 79 b2 00 43 28 9d af d6 0d 01 a4 c4 37 3c f2 8a db 56 c9 b4 54'
187 if [ "$RES" != "$exp" ]; then
188 echo "Error: (2) Did not get expected result from TPM_PCRRead(17)"
189 echo "expected: $exp"
190 echo "received: $RES"
191 exit 1
192 fi
193
194 # Save the volatile state again
195 run_swtpm_ioctl ${SWTPM_INTERFACE} -v
196 if [ $? -ne 0 ]; then
197 echo "Error: Saving the volatile state failed."
198 echo "TPM Logfile:"
199 cat $logfile
200 exit 1
201 fi
202
203 if [ ! -r $VOLATILE_STATE_FILE ]; then
204 echo "Error: Volatile state file $VOLATILE_STATE_FILE does not exist."
205 echo "TPM Logfile:"
206 cat $logfile
207 exit 1
208 fi
209
210 # Send a new TPM_Init
211 run_swtpm_ioctl ${SWTPM_INTERFACE} -i
212 if [ $? -ne 0 ]; then
213 echo "Error: ${SWTPM_INTERFACE} TPM initialization failed."
214 echo "TPM Logfile:"
215 cat $logfile
216 exit 1
217 fi
218
219 # Volatile state must have been removed by TPM now
220 if [ -r $VOLATILE_STATE_FILE ]; then
221 echo "Error: Volatile state file $VOLATILE_STATE_FILE still exists."
222 echo "TPM Logfile:"
223 cat $logfile
224 exit 1
225 fi
226
227 # Read the PCR again ...
228 swtpm_open_cmddev ${SWTPM_INTERFACE} 100
229 RES=$(swtpm_cmd_tx ${SWTPM_INTERFACE} '\x80\x01\x00\x00\x00\x14\x00\x00\x01\x7e\x00\x00\x00\x01\x00\x0b\x03\x00\x00\x02')
230 exp=' 80 01 00 00 00 3e 00 00 00 00 00 00 00 19 00 00 00 01 00 0b 03 00 00 02 00 00 00 01 00 20 fc a5 d6 49 bf b0 c9 22 fd 33 0f 79 b2 00 43 28 9d af d6 0d 01 a4 c4 37 3c f2 8a db 56 c9 b4 54'
231 if [ "$RES" != "$exp" ]; then
232 echo "Error: (2) Did not get expected result from TPM_PCRRead(17)"
233 echo "expected: $exp"
234 echo "received: $RES"
235 exit 1
236 fi
237
238
239 # Final shut down
240 exec 100>&-
241 run_swtpm_ioctl ${SWTPM_INTERFACE} -s
242 if [ $? -ne 0 ]; then
243 echo "Error: Could not shut down the ${SWTPM_INTERFACE} TPM."
244 echo "TPM Logfile:"
245 cat $logfile
246 exit 1
247 fi
248
249 if wait_process_gone ${SWTPM_PID} 1; then
250 echo "Error: ${SWTPM_INTERFACE} TPM should not be running anymore."
251 echo "TPM Logfile:"
252 cat $logfile
253 exit 1
254 fi
255
256 if [ ! -e $STATE_FILE ]; then
257 echo "Error: TPM state file $STATE_FILE does not exist."
258 echo "TPM Logfile:"
259 cat $logfile
260 exit 1
261 fi
262
263 echo "OK"
264
265 exit 0