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