]> git.proxmox.com Git - swtpm.git/blob - tests/_test_tpm2_resume_volatile
tests: Set test-check local user.name and user.email before git am
[swtpm.git] / tests / _test_tpm2_resume_volatile
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-tpm2-resume-volatile:-VTPM_NAME}
10 SWTPM_DEV_NAME="/dev/${VTPM_NAME}"
11 SWTPM_INTERFACE=${SWTPM_INTERFACE:-cuse}
12
13 tpmstatedir="$(mktemp -d)"
14 if [ -z "$tpmstatedir" ]; then
15 echo "Could not create temporary directory"
16 exit 1
17 fi
18
19 SWTPM_CMD_UNIX_PATH=${tpmstatedir}/unix-cmd.sock
20 SWTPM_CTRL_UNIX_PATH=${tpmstatedir}/unix-ctrl.sock
21
22 function cleanup()
23 {
24 pid=${SWTPM_PID}
25 if [ -n "$pid" ]; then
26 kill_quiet -9 $pid
27 fi
28 rm -rf $tpmstatedir
29 }
30
31 trap "cleanup" EXIT
32
33 [ "${SWTPM_INTERFACE}" == "cuse" ] && source ${TESTDIR}/test_cuse
34 source ${TESTDIR}/common
35
36 export TPM_PATH=$tpmstatedir
37
38 # copy all the state files
39 cp ${TESTDIR}/data/tpm2state1/* ${TPM_PATH}
40
41 run_swtpm ${SWTPM_INTERFACE} --tpm2
42
43 display_processes_by_name "$SWTPM"
44
45 kill_quiet -0 ${SWTPM_PID}
46 if [ $? -ne 0 ]; then
47 echo "Error: CUSE TPM did not start."
48 exit 1
49 fi
50
51 # Init the TPM
52 run_swtpm_ioctl ${SWTPM_INTERFACE} -i
53 if [ $? -ne 0 ]; then
54 echo "Error: Could not initialize the CUSE TPM."
55 exit 1
56 fi
57
58 kill_quiet -0 ${SWTPM_PID} 2>/dev/null
59 if [ $? -ne 0 ]; then
60 echo "Error: CUSE TPM not running anymore after INIT."
61 exit 1
62 fi
63
64 swtpm_open_cmddev ${SWTPM_INTERFACE} 100
65
66 # Read PCR 10 (from pcrextend -ha 10 -ic test)
67 RES=$(swtpm_cmd_tx ${SWTPM_INTERFACE} '\x80\x01\x00\x00\x00\x14\x00\x00\x01\x7e\x00\x00\x00\x01\x00\x0b\x03\x00\x04\x00')
68 exp=' 80 01 00 00 00 3e 00 00 00 00 00 00 00 16 00 00 00 01 00 0b 03 00 04 00 00 00 00 01 00 20 f6 85 98 e5 86 8d e6 8b 97 29 99 60 f2 71 7d 17 67 89 a4 2f 9a ae a8 c7 b7 aa 79 a8 62 56 c1 de'
69 if [ "$RES" != "$exp" ]; then
70 echo "Error: (1) Did not get expected result from TPM_PCRRead(10)"
71 echo "expected: $exp"
72 echo "received: $RES"
73 exit 1
74 fi
75
76 # Save the volatile state again
77 run_swtpm_ioctl ${SWTPM_INTERFACE} -v
78 if [ $? -ne 0 ]; then
79 echo "Error: Could not have the CUSE TPM write the volatile state to a file."
80 exit 1
81 fi
82 if [ ! -r $VOLATILE_STATE_FILE ]; then
83 echo "Error: Volatile state file $VOLATILE_STATE_FILE does not exist."
84 exit 1
85 fi
86
87 # Shut the TPM down
88 exec 100>&-
89 run_swtpm_ioctl ${SWTPM_INTERFACE} -s
90
91 echo "Test 1: Ok"
92
93 # 2nd test: with encrypted state
94 # copy all the state files
95 cp ${TESTDIR}/data/tpm2state2/* ${TPM_PATH}
96
97 run_swtpm ${SWTPM_INTERFACE} \
98 --tpm2 \
99 --key pwdfile=${TESTDIR}/data/tpm2state2/pwdfile.txt,kdf=sha512
100
101 display_processes_by_name "$SWTPM"
102
103 kill_quiet -0 ${SWTPM_PID}
104 if [ $? -ne 0 ]; then
105 echo "Error: CUSE TPM did not start."
106 exit 1
107 fi
108
109 # Init the TPM
110 run_swtpm_ioctl ${SWTPM_INTERFACE} -i
111 if [ $? -ne 0 ]; then
112 echo "Error: CUSE TPM initialization failed."
113 exit 1
114 fi
115
116 kill_quiet -0 ${SWTPM_PID} 2>/dev/null
117 if [ $? -ne 0 ]; then
118 echo "Error: CUSE TPM not running anymore after INIT."
119 exit 1
120 fi
121
122 swtpm_open_cmddev ${SWTPM_INTERFACE} 100
123 if [ $? -ne 0 ]; then
124 echo "Error: Could not open command interface."
125 ext 1
126 fi
127
128 # Read PCR 10 (from pcrextend -ha 10 -ic test)
129 RES=$(swtpm_cmd_tx ${SWTPM_INTERFACE} '\x80\x01\x00\x00\x00\x14\x00\x00\x01\x7e\x00\x00\x00\x01\x00\x0b\x03\x00\x04\x00')
130 exp=' 80 01 00 00 00 3e 00 00 00 00 00 00 00 16 00 00 00 01 00 0b 03 00 04 00 00 00 00 01 00 20 f6 85 98 e5 86 8d e6 8b 97 29 99 60 f2 71 7d 17 67 89 a4 2f 9a ae a8 c7 b7 aa 79 a8 62 56 c1 de'
131 if [ "$RES" != "$exp" ]; then
132 echo "Error: (1) Did not get expected result from TPM_PCRRead(10)"
133 echo "expected: $exp"
134 echo "received: $RES"
135 exit 1
136 fi
137
138 # Save the volatile state again
139 run_swtpm_ioctl ${SWTPM_INTERFACE} -v
140 if [ $? -ne 0 ]; then
141 echo "Error: Could not have the CUSE TPM write the volatile state to a file."
142 exit 1
143 fi
144 if [ ! -r $VOLATILE_STATE_FILE ]; then
145 echo "Error: Volatile state file $VOLATILE_STATE_FILE does not exist."
146 exit 1
147 fi
148
149 # Shut the TPM down
150 exec 100>&-
151 run_swtpm_ioctl ${SWTPM_INTERFACE} -s
152 if [ $? -ne 0 ]; then
153 echo "Error: Could not shut down the CUSE TPM."
154 exit 1
155 fi
156
157 echo "Test 2: Ok"
158
159 # 3rd test: with encrypted state using aes-256-cbc
160 # copy all the state files
161 cp ${TESTDIR}/data/tpm2state2b/* ${TPM_PATH}
162
163 run_swtpm ${SWTPM_INTERFACE} \
164 --tpm2 \
165 --key pwdfile=${TESTDIR}/data/tpm2state2b/pwdfile.txt,mode=aes-256-cbc
166
167 display_processes_by_name "$SWTPM"
168
169 kill_quiet -0 ${SWTPM_PID}
170 if [ $? -ne 0 ]; then
171 echo "Error: CUSE TPM did not start."
172 exit 1
173 fi
174
175 # Init the TPM
176 run_swtpm_ioctl ${SWTPM_INTERFACE} -i
177 if [ $? -ne 0 ]; then
178 echo "Error: CUSE TPM initialization failed."
179 exit 1
180 fi
181
182 kill_quiet -0 ${SWTPM_PID} 2>/dev/null
183 if [ $? -ne 0 ]; then
184 echo "Error: CUSE TPM not running anymore after INIT."
185 exit 1
186 fi
187
188 swtpm_open_cmddev ${SWTPM_INTERFACE} 100
189 if [ $? -ne 0 ]; then
190 echo "Error: Could not open command interface."
191 ext 1
192 fi
193
194 # Read PCR 10 (from pcrextend -ha 10 -ic test)
195 RES=$(swtpm_cmd_tx ${SWTPM_INTERFACE} '\x80\x01\x00\x00\x00\x14\x00\x00\x01\x7e\x00\x00\x00\x01\x00\x0b\x03\x00\x04\x00')
196 exp=' 80 01 00 00 00 3e 00 00 00 00 00 00 00 16 00 00 00 01 00 0b 03 00 04 00 00 00 00 01 00 20 f6 85 98 e5 86 8d e6 8b 97 29 99 60 f2 71 7d 17 67 89 a4 2f 9a ae a8 c7 b7 aa 79 a8 62 56 c1 de'
197 if [ "$RES" != "$exp" ]; then
198 echo "Error: (1) Did not get expected result from TPM_PCRRead(10)"
199 echo "expected: $exp"
200 echo "received: $RES"
201 exit 1
202 fi
203
204 # Save the volatile state again
205 run_swtpm_ioctl ${SWTPM_INTERFACE} -v
206 if [ $? -ne 0 ]; then
207 echo "Error: Could not have the CUSE TPM write the volatile state to a file."
208 exit 1
209 fi
210 if [ ! -r $VOLATILE_STATE_FILE ]; then
211 echo "Error: Volatile state file $VOLATILE_STATE_FILE does not exist."
212 exit 1
213 fi
214
215 # Shut the TPM down
216 exec 100>&-
217 run_swtpm_ioctl ${SWTPM_INTERFACE} -s
218 if [ $? -ne 0 ]; then
219 echo "Error: Could not shut down the CUSE TPM."
220 exit 1
221 fi
222
223 echo "Test 3: Ok"