Debian Codegraph @

tests/_test_tpm2_resume_volatile

raw
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)" || exit 1
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 source "${TESTDIR}/common"
34 [ "${SWTPM_INTERFACE}" == "cuse" ] && source "${TESTDIR}/test_cuse"
35
36 TPM_PATH=$tpmstatedir
37 VOLATILE_STATE_FILE="$TPM_PATH/tpm2-00.volatilestate"
38
39 # copy all the state files
40 cp "${TESTDIR}"/data/tpm2state1/* "${TPM_PATH}"
41
42 TPM_PATH=$TPM_PATH run_swtpm "${SWTPM_INTERFACE}" --tpm2
43
44 display_processes_by_name "swtpm"
45
46 if ! kill_quiet -0 "${SWTPM_PID}"; then
47 echo "Error: CUSE TPM did not start."
48 exit 1
49 fi
50
51 # Init the TPM
52 if ! run_swtpm_ioctl "${SWTPM_INTERFACE}" -i; then
53 echo "Error: Could not initialize the CUSE TPM."
54 exit 1
55 fi
56
57 if ! kill_quiet -0 "${SWTPM_PID}" 2>/dev/null; then
58 echo "Error: CUSE TPM not running anymore after INIT."
59 exit 1
60 fi
61
62 # Read PCR 10 (from pcrextend -ha 10 -ic test)
63 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')
64 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'
65 if [ "$RES" != "$exp" ]; then
66 echo "Error: (1) Did not get expected result from TPM_PCRRead(10)"
67 echo "expected: $exp"
68 echo "received: $RES"
69 exit 1
70 fi
71
72 # Save the volatile state again
73 if ! run_swtpm_ioctl "${SWTPM_INTERFACE}" -v; then
74 echo "Error: Could not have the CUSE TPM write the volatile state to a file."
75 exit 1
76 fi
77 if [ ! -r "$VOLATILE_STATE_FILE" ]; then
78 echo "Error: Volatile state file $VOLATILE_STATE_FILE does not exist."
79 exit 1
80 fi
81
82 # Shut the TPM down
83 run_swtpm_ioctl "${SWTPM_INTERFACE}" -s
84
85 echo "Test 1: Ok"
86
87 # 2nd test: with encrypted state
88 # copy all the state files
89 cp "${TESTDIR}"/data/tpm2state2/* "${TPM_PATH}"
90
91 TPM_PATH=$TPM_PATH run_swtpm "${SWTPM_INTERFACE}" \
92 --tpm2 \
93 --key "pwdfile=${TESTDIR}/data/tpm2state2/pwdfile.txt,kdf=sha512"
94
95 display_processes_by_name "swtpm"
96
97 if ! kill_quiet -0 "${SWTPM_PID}"; then
98 echo "Error: CUSE TPM did not start."
99 exit 1
100 fi
101
102 # Init the TPM
103 if ! run_swtpm_ioctl "${SWTPM_INTERFACE}" -i; then
104 echo "Error: CUSE TPM initialization failed."
105 exit 1
106 fi
107
108 if ! kill_quiet -0 "${SWTPM_PID}" 2>/dev/null; then
109 echo "Error: CUSE TPM not running anymore after INIT."
110 exit 1
111 fi
112
113 # Read PCR 10 (from pcrextend -ha 10 -ic test)
114 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')
115 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'
116 if [ "$RES" != "$exp" ]; then
117 echo "Error: (1) Did not get expected result from TPM_PCRRead(10)"
118 echo "expected: $exp"
119 echo "received: $RES"
120 exit 1
121 fi
122
123 # Save the volatile state again
124 if ! run_swtpm_ioctl "${SWTPM_INTERFACE}" -v; then
125 echo "Error: Could not have the CUSE TPM write the volatile state to a file."
126 exit 1
127 fi
128 if [ ! -r "$VOLATILE_STATE_FILE" ]; then
129 echo "Error: Volatile state file $VOLATILE_STATE_FILE does not exist."
130 exit 1
131 fi
132
133 # Shut the TPM down
134 if ! run_swtpm_ioctl "${SWTPM_INTERFACE}" -s; then
135 echo "Error: Could not shut down the CUSE TPM."
136 exit 1
137 fi
138
139 echo "Test 2: Ok"
140
141 # 3rd test: with encrypted state using aes-256-cbc
142 # copy all the state files
143 cp "${TESTDIR}"/data/tpm2state2b/* "${TPM_PATH}"
144
145 TPM_PATH=$TPM_PATH run_swtpm "${SWTPM_INTERFACE}" \
146 --tpm2 \
147 --key "pwdfile=${TESTDIR}/data/tpm2state2b/pwdfile.txt,mode=aes-256-cbc"
148
149 display_processes_by_name "swtpm"
150
151 if ! kill_quiet -0 "${SWTPM_PID}"; then
152 echo "Error: CUSE TPM did not start."
153 exit 1
154 fi
155
156 # Init the TPM
157 if ! run_swtpm_ioctl "${SWTPM_INTERFACE}" -i; then
158 echo "Error: CUSE TPM initialization failed."
159 exit 1
160 fi
161
162 if ! kill_quiet -0 "${SWTPM_PID}" 2>/dev/null; then
163 echo "Error: CUSE TPM not running anymore after INIT."
164 exit 1
165 fi
166
167 # Read PCR 10 (from pcrextend -ha 10 -ic test)
168 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')
169 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'
170 if [ "$RES" != "$exp" ]; then
171 echo "Error: (1) Did not get expected result from TPM_PCRRead(10)"
172 echo "expected: $exp"
173 echo "received: $RES"
174 exit 1
175 fi
176
177 # Save the volatile state again
178 if ! run_swtpm_ioctl "${SWTPM_INTERFACE}" -v; then
179 echo "Error: Could not have the CUSE TPM write the volatile state to a file."
180 exit 1
181 fi
182 if [ ! -r "$VOLATILE_STATE_FILE" ]; then
183 echo "Error: Volatile state file $VOLATILE_STATE_FILE does not exist."
184 exit 1
185 fi
186
187 # Shut the TPM down
188 if ! run_swtpm_ioctl "${SWTPM_INTERFACE}" -s; then
189 echo "Error: Could not shut down the CUSE TPM."
190 exit 1
191 fi
192
193 echo "Test 3: Ok"