]> git.proxmox.com Git - swtpm.git/blame - tests/_test_migration_key
packaging: track dbgsym package for swtpm-libs and swtpm-tools
[swtpm.git] / tests / _test_migration_key
CommitLineData
01aa2ed3
SB
1#!/bin/bash
2
3# For the license, see the LICENSE file in the root directory.
4# set -x
5
313cf75c
SB
6ROOT=${abs_top_builddir:-$(pwd)/..}
7TESTDIR=${abs_top_testdir:-$(dirname "$0")}
8
01aa2ed3
SB
9VTPM_NAME="${VTPM_NAME:-vtpm-test-migration-key}"
10SWTPM_DEV_NAME="/dev/${VTPM_NAME}"
11MIGRATION_PASSWORD="migration"
313cf75c 12VOLATILESTATE=${TESTDIR}/data/migkey1/volatilestate.bin
01aa2ed3 13
cce7503c 14tpmstatedir="$(mktemp -d)"
01aa2ed3
SB
15if [ -z "$tpmstatedir" ]; then
16 echo "Could not create temporary directory."
17 exit 1
18fi
19
cce7503c 20migpwdfile="$(mktemp)"
01aa2ed3
SB
21if [ -z "$migpwdfile" ]; then
22 echo "Could not create temporary file."
23 exit 1
24fi
25echo -n "$MIGRATION_PASSWORD" > $migpwdfile
26
cce7503c 27volatilestatefile="$(mktemp)"
01aa2ed3
SB
28if [ -z "$volatilestatefile" ]; then
29 echo "Could not create temporary file."
30 exit 1
31fi
32
33SWTPM_CMD_UNIX_PATH=${tpmstatedir}/unix-cmd.sock
34SWTPM_CTRL_UNIX_PATH=${tpmstatedir}/unix-ctrl.sock
35SWTPM_INTERFACE=${SWTPM_INTERFACE:-cuse}
36
37function cleanup()
38{
39 pid=${SWTPM_PID}
40 if [ -n "$pid" ]; then
47c7ea77 41 kill_quiet -9 $pid
01aa2ed3
SB
42 fi
43 rm -rf $migpwdfile $volatilestatefile $tpmstatedir
44}
45
46trap "cleanup" EXIT
47
313cf75c
SB
48[ "${SWTPM_INTERFACE}" == cuse ] && source ${TESTDIR}/test_cuse
49source ${TESTDIR}/common
01aa2ed3
SB
50
51# make a backup of the volatile state
52export TPM_PATH=$tpmstatedir
313cf75c 53cp ${TESTDIR}/data/tpmstate1/* $TPM_PATH
01aa2ed3 54
a39f098f
SB
55run_swtpm ${SWTPM_INTERFACE} \
56 --migration-key pwdfile=$migpwdfile,remove=false,kdf=sha512
01aa2ed3 57
100317d5 58display_processes_by_name "$SWTPM"
01aa2ed3 59
47c7ea77 60kill_quiet -0 ${SWTPM_PID}
01aa2ed3
SB
61if [ $? -ne 0 ]; then
62 echo "Error: ${SWTPM_INTERFACE} TPM did not start."
63 exit 1
64fi
65
66# Init the TPM
67run_swtpm_ioctl ${SWTPM_INTERFACE} -i
68if [ $? -ne 0 ]; then
69 echo "Error: Initializing the ${SWTPM_INTERFACE} TPM failed."
70 exit 1
71fi
72
47c7ea77 73kill_quiet -0 ${SWTPM_PID} 2>/dev/null
01aa2ed3
SB
74if [ $? -ne 0 ]; then
75 echo "Error: ${SWTPM_INTERFACE} TPM not running anymore after INIT."
76 exit 1
77fi
78
79# Read PCR 10
01aa2ed3
SB
80RES=$(swtpm_cmd_tx ${SWTPM_INTERFACE} '\x00\xC1\x00\x00\x00\x0E\x00\x00\x00\x15\x00\x00\x00\x0a')
81exp=' 00 c4 00 00 00 1e 00 00 00 00 c7 8a 6e 94 c7 3c 4d 7f c3 05 c8 a6 6b bf 15 45 f4 ed b7 a5'
82if [ "$RES" != "$exp" ]; then
83 echo "Error: (1) Did not get expected result from TPM_PCRRead(10)"
84 echo "expected: $exp"
85 echo "received: $RES"
86 exit 1
87fi
88
89# Assert physical presence
01aa2ed3
SB
90RES=$(swtpm_cmd_tx ${SWTPM_INTERFACE} '\x00\xC1\x00\x00\x00\x0C\x40\x00\x00\x0A\x00\x20')
91exp=' 00 c4 00 00 00 0a 00 00 00 00'
92if [ "$RES" != "$exp" ]; then
93 echo "Error: (1) Did not get expected result from TSC_PhysicalPresence(ENABLE)"
94 echo "expected: $exp"
95 echo "received: $RES"
96 exit 1
97fi
98
99# Create a big NVRAM Area with 4000 bytes (0xfa0)
100tmp='\x00\xC1\x00\x00\x00\x65\x00\x00\x00\xcc\x00\x18\x00\x00\x00\x01'
101tmp+='\x00\x03\x00\x00\x00\x01\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00'
102tmp+='\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x03\x00\x00\x00\x01'
103tmp+='\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00'
104tmp+='\x00\x00\x00\x00\x00\x17\x00\x01\x00\x01\x00\x00\x00\x00\x00\x0f'
105tmp+='\xa0\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00'
106tmp+='\x00\x00\x00\x00\x00'
01aa2ed3
SB
107RES=$(swtpm_cmd_tx ${SWTPM_INTERFACE} $tmp)
108exp=' 00 c4 00 00 00 0a 00 00 00 00'
109if [ "$RES" != "$exp" ]; then
110 echo "Error: (1) Did not get expected result from TPM_NVDefineSpace()"
111 echo "expected: $exp"
112 echo "received: $RES"
113 exit 1
114fi
115
116# Save the volatile state into a file
117run_swtpm_ioctl ${SWTPM_INTERFACE} --save volatile $volatilestatefile
118if [ $? -ne 0 ]; then
119 echo "Error: Could not save the volatile state to ${volatilestatefile}."
120 exit 1
121fi
122if [ ! -r $volatilestatefile ]; then
123 echo "Error: Volatile state file $volatilestatefile does not exist."
124 exit 1
125fi
126
127#ls -l $volatilestatefile
0f648eda 128size=$(get_filesize $volatilestatefile)
638bd3ba 129expsize=1324
01aa2ed3
SB
130if [ $size -ne $expsize ]; then
131 echo "Error: Unexpected size of volatile state file."
132 echo " Expected file with size of $expsize, found $size bytes."
133 exit 1
134fi
135
01aa2ed3
SB
136tmp=$(run_swtpm_ioctl ${SWTPM_INTERFACE} -g | cut -d":" -f2)
137if [ $? -ne 0 ]; then
138 echo "Error: Could not get the configration flags of the ${SWTPM_INTERFACE} TPM."
139 exit 1
140fi
141
142if [ "$tmp" != " 0x2" ]; then
143 echo "Error: Unexpected configuration flags: $tmp; expected 0x2."
144 exit 1
145fi
146
147# Shut the TPM down
148exec 100>&-
149run_swtpm_ioctl ${SWTPM_INTERFACE} -s
150
151echo "Test 1: Ok"
152
153# Start the vTPM again and load the encrypted volatile state into it
a39f098f
SB
154run_swtpm ${SWTPM_INTERFACE} \
155 --migration-key pwdfile=$migpwdfile,remove=false,kdf=sha512
01aa2ed3 156
100317d5 157display_processes_by_name "$SWTPM"
01aa2ed3 158
47c7ea77 159kill_quiet -0 ${SWTPM_PID}
01aa2ed3
SB
160if [ $? -ne 0 ]; then
161 echo "Error: ${SWTPM_INTERFACE} TPM did not start."
162 exit 1
163fi
164
165# Do NOT init the TPM now; first load volatile state
166
167# load the encrypted volatile state into it
168run_swtpm_ioctl ${SWTPM_INTERFACE} --load volatile $volatilestatefile
169if [ $? -ne 0 ]; then
170 echo "Error: Could not load encrypted volatile state into TPM."
171 exit 1
172fi
173
174# Now init the TPM
175run_swtpm_ioctl ${SWTPM_INTERFACE} -i
176if [ $? -ne 0 ]; then
177 echo "Error: Initializing the ${SWTPM_INTERFACE} TPM failed."
178 exit 1
179fi
180
181# Read PCR 10
01aa2ed3
SB
182RES=$(swtpm_cmd_tx ${SWTPM_INTERFACE} '\x00\xC1\x00\x00\x00\x0E\x00\x00\x00\x15\x00\x00\x00\x0a')
183exp=' 00 c4 00 00 00 1e 00 00 00 00 c7 8a 6e 94 c7 3c 4d 7f c3 05 c8 a6 6b bf 15 45 f4 ed b7 a5'
184if [ "$RES" != "$exp" ]; then
185 echo "Error: (1) Did not get expected result from TPM_PCRRead(10)"
186 echo "expected: $exp"
187 echo "received: $RES"
188 exit 1
189fi
190
191# Shut the TPM down
192exec 100>&-
193run_swtpm_ioctl ${SWTPM_INTERFACE} -s
194if [ $? -ne 0 ]; then
195 echo "Error: Could not shut down the ${SWTPM_INTERFACE} TPM."
196 exit 1
197fi
198
199echo "Test 2: Ok"
200
201
202# Start the vTPM again and load the encrypted volatile state into it
203# This time we make this fail since we don't provide the migration key
204run_swtpm ${SWTPM_INTERFACE}
205
100317d5 206display_processes_by_name "$SWTPM"
01aa2ed3 207
47c7ea77 208kill_quiet -0 ${SWTPM_PID}
01aa2ed3
SB
209if [ $? -ne 0 ]; then
210 echo "Error: ${SWTPM_INTERFACE} TPM did not start."
211 exit 1
212fi
213
214# Do NOT init the TPM now; first load volatile state
215
216# load the encrypted volatile state into it
a31a26ea
SB
217# This will not work; the TPM writes the data into the volatile state file
218# and validates it
f759520c 219ERR=$(run_swtpm_ioctl ${SWTPM_INTERFACE} --load volatile $volatilestatefile 2>&1)
a31a26ea
SB
220if [ $? -eq 0 ]; then
221 echo "Error: Could load encrypted volatile state into TPM."
01aa2ed3
SB
222 exit 1
223fi
f759520c
SB
224exp="TPM result from PTM_SET_STATEBLOB: 0xd"
225if [ "$ERR" != "$exp" ]; then
226 echo "Error: Unexpected error message"
227 echo "Received: $ERR"
228 echo "Expected: $exp"
229 exit 1
230fi
01aa2ed3 231
01aa2ed3
SB
232run_swtpm_ioctl ${SWTPM_INTERFACE} -s
233if [ $? -ne 0 ]; then
234 echo "Error: Could not shut down the ${SWTPM_INTERFACE} TPM."
235 exit 1
236fi
237
238echo "Test 3: Ok"
239
240# In this test we now feed it an encrypted volatile state
241
242# Start the vTPM again and load the encrypted volatile state into it
a39f098f
SB
243run_swtpm ${SWTPM_INTERFACE} \
244 --migration-key pwdfile=$migpwdfile,remove=true,kdf=sha512
01aa2ed3 245
100317d5 246display_processes_by_name "$SWTPM"
01aa2ed3 247
47c7ea77 248kill_quiet -0 ${SWTPM_PID}
01aa2ed3
SB
249if [ $? -ne 0 ]; then
250 echo "Error: ${SWTPM_INTERFACE} TPM did not start."
251 exit 1
252fi
253
254# load the encrypted volatile state into it
255run_swtpm_ioctl ${SWTPM_INTERFACE} --load volatile $VOLATILESTATE
256if [ $? -ne 0 ]; then
257 echo "Error: Could not load encrypted volatile state into TPM."
258 exit 1
259fi
260
261# Now init the TPM; this must work
262run_swtpm_ioctl ${SWTPM_INTERFACE} -i
263if [ $? -ne 0 ]; then
264 echo "Error: Could not initialize the ${SWTPM_INTERFACE} TPM."
265 exit 1
266fi
267
268# Read PCR 10
01aa2ed3
SB
269RES=$(swtpm_cmd_tx ${SWTPM_INTERFACE} '\x00\xC1\x00\x00\x00\x0E\x00\x00\x00\x15\x00\x00\x00\x0a')
270exp=' 00 c4 00 00 00 1e 00 00 00 00 c7 8a 6e 94 c7 3c 4d 7f c3 05 c8 a6 6b bf 15 45 f4 ed b7 a5'
271if [ "$RES" != "$exp" ]; then
272 echo "Error: (1) Did not get expected result from TPM_PCRRead(10)"
273 echo "expected: $exp"
274 echo "received: $RES"
275 exit 1
276fi
277
278# Shut the TPM down
279exec 100>&-
280run_swtpm_ioctl ${SWTPM_INTERFACE} -s
281if [ $? -ne 0 ]; then
282 echo "Error: Could not shut down the ${SWTPM_INTERFACE} TPM."
283 exit 1
284fi
285
286echo "Test 4: Ok"