]> git.proxmox.com Git - swtpm.git/blob - tests/_test_encrypted_state
packaging: track dbgsym package for swtpm-libs and swtpm-tools
[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)" || exit 1
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 RES=$(swtpm_cmd_tx ${SWTPM_INTERFACE} '\x00\xC1\x00\x00\x00\x0C\x00\x00\x00\x99\x00\x01')
82 exp=' 00 c4 00 00 00 0a 00 00 00 00'
83 if [ "$RES" != "$exp" ]; then
84 echo "Error: Did not get expected result from TPM_Startup(ST_Clear)"
85 echo "expected: $exp"
86 echo "received: $RES"
87 exit 1
88 fi
89
90 run_swtpm_ioctl ${SWTPM_INTERFACE} -h 1234
91 if [ $? -ne 0 ]; then
92 echo "Error: Hash command did not work."
93 echo "TPM Logfile:"
94 cat $logfile
95 exit 1
96 fi
97
98 # Read PCR 17
99 RES=$(swtpm_cmd_tx ${SWTPM_INTERFACE} '\x00\xC1\x00\x00\x00\x0E\x00\x00\x00\x15\x00\x00\x00\x11')
100 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'
101 if [ "$RES" != "$exp" ]; then
102 echo "Error: (1) Did not get expected result from TPM_PCRRead(17)"
103 echo "expected: $exp"
104 echo "received: $RES"
105 exit 1
106 fi
107
108 # Save the volatile state
109 run_swtpm_ioctl ${SWTPM_INTERFACE} -v
110 if [ $? -ne 0 ]; then
111 echo "Error: Saving the volatile state failed."
112 echo "TPM Logfile:"
113 cat $logfile
114 exit 1
115 fi
116
117 if [ ! -r $VOLATILE_STATE_FILE ]; then
118 echo "Error: Volatile state file $VOLATILE_STATE_FILE does not exist."
119 echo "TPM Logfile:"
120 cat $logfile
121 exit 1
122 fi
123
124 tmp=$(run_swtpm_ioctl ${SWTPM_INTERFACE} -g | cut -d":" -f2)
125 if [ $? -ne 0 ]; then
126 echo "Error: Could not get the configration flags of the ${SWTPM_INTERFACE} TPM."
127 echo "TPM Logfile:"
128 cat $logfile
129 exit 1
130 fi
131
132 if [ "$tmp" != " 0x1" ]; then
133 echo "Error: Unexpected configuration flags: $tmp; expected 0x1."
134 echo "TPM Logfile:"
135 cat $logfile
136 exit 1
137 fi
138
139 # Shut the TPM down
140 run_swtpm_ioctl ${SWTPM_INTERFACE} -s
141 if [ $? -ne 0 ]; then
142 echo "Error: Could not shut down the ${SWTPM_INTERFACE} TPM."
143 echo "TPM Logfile:"
144 cat $logfile
145 exit 1
146 fi
147
148 # Start the TPM again; have the keyfile removed
149 run_swtpm ${SWTPM_INTERFACE} \
150 --key file=$binkeyfile,mode=aes-cbc,format=binary,remove \
151 --log file=$logfile
152
153 display_processes_by_name "$SWTPM"
154
155 kill_quiet -0 ${SWTPM_PID}
156 if [ $? -ne 0 ]; then
157 echo "Error (2): ${SWTPM_INTERFACE} TPM did not start."
158 echo "TPM Logfile:"
159 cat $logfile
160 exit 1
161 fi
162
163 if wait_file_gone ${binkeyfile} 4; then
164 echo "Error: Keyfile $binkeyfile was not removed by ${SWTPM_INTERFACE} tpm."
165 echo "TPM Logfile:"
166 cat $logfile
167 exit 1
168 fi
169
170 # Init the TPM
171 run_swtpm_ioctl ${SWTPM_INTERFACE} -i
172 if [ $? -ne 0 ]; then
173 echo "Error: ${SWTPM_INTERFACE} TPM initialization failed."
174 echo "TPM Logfile:"
175 cat $logfile
176 exit 1
177 fi
178
179 # Volatile state must have been removed by TPM now
180 if [ -r $VOLATILE_STATE_FILE ]; then
181 echo "Error: Volatile state file $VOLATILE_STATE_FILE still exists."
182 echo "TPM Logfile:"
183 cat $logfile
184 exit 1
185 fi
186
187 # Read the PCR again ...
188 RES=$(swtpm_cmd_tx ${SWTPM_INTERFACE} '\x00\xC1\x00\x00\x00\x0E\x00\x00\x00\x15\x00\x00\x00\x11')
189 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'
190 if [ "$RES" != "$exp" ]; then
191 echo "Error: (2) Did not get expected result from TPM_PCRRead(17)"
192 echo "expected: $exp"
193 echo "received: $RES"
194 exit 1
195 fi
196
197 # Save the volatile state again
198 run_swtpm_ioctl ${SWTPM_INTERFACE} -v
199 if [ $? -ne 0 ]; then
200 echo "Error: Saving the volatile state failed."
201 echo "TPM Logfile:"
202 cat $logfile
203 exit 1
204 fi
205
206 if [ ! -r $VOLATILE_STATE_FILE ]; then
207 echo "Error: Volatile state file $VOLATILE_STATE_FILE does not exist."
208 echo "TPM Logfile:"
209 cat $logfile
210 exit 1
211 fi
212
213 # Send a new TPM_Init
214 run_swtpm_ioctl ${SWTPM_INTERFACE} -i
215 if [ $? -ne 0 ]; then
216 echo "Error: ${SWTPM_INTERFACE} TPM initialization failed."
217 echo "TPM Logfile:"
218 cat $logfile
219 exit 1
220 fi
221
222 # Volatile state must have been removed by TPM now
223 if [ -r $VOLATILE_STATE_FILE ]; then
224 echo "Error: Volatile state file $VOLATILE_STATE_FILE still exists."
225 echo "TPM Logfile:"
226 cat $logfile
227 exit 1
228 fi
229
230 # Read the PCR again ...
231 RES=$(swtpm_cmd_tx ${SWTPM_INTERFACE} '\x00\xC1\x00\x00\x00\x0E\x00\x00\x00\x15\x00\x00\x00\x11')
232 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'
233 if [ "$RES" != "$exp" ]; then
234 echo "Error: (2) Did not get expected result from TPM_PCRRead(17)"
235 echo "expected: $exp"
236 echo "received: $RES"
237 exit 1
238 fi
239
240
241 # Final shut down
242 run_swtpm_ioctl ${SWTPM_INTERFACE} -s
243 if [ $? -ne 0 ]; then
244 echo "Error: Could not shut down the ${SWTPM_INTERFACE} TPM."
245 echo "TPM Logfile:"
246 cat $logfile
247 exit 1
248 fi
249
250 if wait_process_gone ${SWTPM_PID} 4; then
251 echo "Error: ${SWTPM_INTERFACE} TPM should not be running anymore."
252 echo "TPM Logfile:"
253 cat $logfile
254 exit 1
255 fi
256
257 if [ ! -e $STATE_FILE ]; then
258 echo "Error: TPM state file $STATE_FILE does not exist."
259 echo "TPM Logfile:"
260 cat $logfile
261 exit 1
262 fi
263
264 echo "OK"
265
266 exit 0