Debian Codegraph @

tests/_test_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-resume-volatile"
10 SWTPM_DEV_NAME="/dev/${VTPM_NAME}"
11
12 tpmstatedir="$(mktemp -d)"
13 if [ -z "$tpmstatedir" ]; then
14 echo "Could not create temporary directory"
15 exit 1
16 fi
17
18 SWTPM_CMD_UNIX_PATH=${tpmstatedir}/unix-cmd.sock
19 SWTPM_CTRL_UNIX_PATH=${tpmstatedir}/unix-ctrl.sock
20 SWTPM_INTERFACE=${SWTPM_INTERFACE:-cuse}
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/tpm-00.volatilestate"
38
39 # copy all the state files
40 cp "${TESTDIR}"/data/tpmstate1/* "${TPM_PATH}"
41
42 TPM_PATH=$TPM_PATH run_swtpm "${SWTPM_INTERFACE}"
43
44 display_processes_by_name "swtpm"
45
46 if ! kill_quiet -0 "${SWTPM_PID}"; then
47 echo "Error: ${SWTPM_INTERFACE} 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 ${SWTPM_INTERFACE} TPM."
54 exit 1
55 fi
56
57 if ! kill_quiet -0 "${SWTPM_PID}" 2>/dev/null; then
58 echo "Error: ${SWTPM_INTERFACE} TPM not running anymore after INIT."
59 exit 1
60 fi
61
62 # Read PCR 10 (extend -ix 10 -ic test)
63 RES=$(swtpm_cmd_tx "${SWTPM_INTERFACE}" '\x00\xC1\x00\x00\x00\x0E\x00\x00\x00\x15\x00\x00\x00\x0a')
64 exp=' 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'
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 ${SWTPM_INTERFACE} 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
88 # 2nd test: with encrypted state
89 # copy all the state files
90 cp "${TESTDIR}"/data/tpmstate2/* "${TPM_PATH}"
91
92 TPM_PATH=$TPM_PATH run_swtpm "${SWTPM_INTERFACE}" \
93 --key "pwdfile=${TESTDIR}/data/tpmstate2/pwdfile.txt,kdf=sha512"
94
95 display_processes_by_name "swtpm"
96
97 if ! kill_quiet -0 "${SWTPM_PID}"; then
98 echo "Error: ${SWTPM_INTERFACE} 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: ${SWTPM_INTERFACE} TPM initialization failed."
105 exit 1
106 fi
107
108 if ! kill_quiet -0 "${SWTPM_PID}" 2>/dev/null; then
109 echo "Error: ${SWTPM_INTERFACE} TPM not running anymore after INIT."
110 exit 1
111 fi
112
113 # Read PCR 10 (extend -ix 10 -ic test)
114 RES=$(swtpm_cmd_tx "${SWTPM_INTERFACE}" '\x00\xC1\x00\x00\x00\x0E\x00\x00\x00\x15\x00\x00\x00\x0a')
115 exp=' 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'
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 ${SWTPM_INTERFACE} 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 ${SWTPM_INTERFACE} 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/tpmstate2b/* "${TPM_PATH}"
144
145 TPM_PATH=$TPM_PATH run_swtpm "${SWTPM_INTERFACE}" \
146 --key "pwdfile=${TESTDIR}/data/tpmstate2b/pwdfile.txt,mode=aes-256-cbc"
147
148 display_processes_by_name "swtpm"
149
150 if ! kill_quiet -0 "${SWTPM_PID}"; then
151 echo "Error: ${SWTPM_INTERFACE} TPM did not start."
152 exit 1
153 fi
154
155 # Init the TPM
156 if ! run_swtpm_ioctl "${SWTPM_INTERFACE}" -i; then
157 echo "Error: ${SWTPM_INTERFACE} TPM initialization failed."
158 exit 1
159 fi
160
161 if ! kill_quiet -0 "${SWTPM_PID}" 2>/dev/null; then
162 echo "Error: ${SWTPM_INTERFACE} TPM not running anymore after INIT."
163 exit 1
164 fi
165
166 # Read PCR 10
167 RES=$(swtpm_cmd_tx "${SWTPM_INTERFACE}" '\x00\xC1\x00\x00\x00\x0E\x00\x00\x00\x15\x00\x00\x00\x0a')
168 exp=' 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'
169 if [ "$RES" != "$exp" ]; then
170 echo "Error: (1) Did not get expected result from TPM_PCRRead(10)"
171 echo "expected: $exp"
172 echo "received: $RES"
173 exit 1
174 fi
175
176 # Save the volatile state again
177 if ! run_swtpm_ioctl "${SWTPM_INTERFACE}" -v; then
178 echo "Error: Could not have the ${SWTPM_INTERFACE} TPM write the volatile state to a file."
179 exit 1
180 fi
181 if [ ! -r "$VOLATILE_STATE_FILE" ]; then
182 echo "Error: Volatile state file $VOLATILE_STATE_FILE does not exist."
183 exit 1
184 fi
185
186 # Shut the TPM down
187 if ! run_swtpm_ioctl "${SWTPM_INTERFACE}" -s; then
188 echo "Error: Could not shut down the ${SWTPM_INTERFACE} TPM."
189 exit 1
190 fi
191
192 echo "Test 3: Ok"