| 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 |
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 |
source "${TESTDIR}/common" |
| 38 |
[ "${SWTPM_INTERFACE}" == cuse ] && source "${TESTDIR}/test_cuse" |
| 39 |
|
| 40 |
rm -f "$STATE_FILE" "$VOLATILE_STATE_FILE" 2>/dev/null |
| 41 |
|
| 42 |
TPM_PATH=$TPM_PATH 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 |
if ! kill_quiet -0 "${SWTPM_PID}"; then |
| 49 |
echo "Error: ${SWTPM_INTERFACE} TPM did not start." |
| 50 |
echo "TPM Logfile:" |
| 51 |
cat "$logfile" |
| 52 |
exit 1 |
| 53 |
fi |
| 54 |
|
| 55 |
if wait_file_gone "${keyfile}" 4; then |
| 56 |
echo "Error: Keyfile $keyfile was not removed by ${SWTPM_INTERFACE} tpm." |
| 57 |
echo "TPM Logfile:" |
| 58 |
cat "$logfile" |
| 59 |
exit 1 |
| 60 |
fi |
| 61 |
|
| 62 |
# Init the TPM |
| 63 |
if ! run_swtpm_ioctl "${SWTPM_INTERFACE}" -i; then |
| 64 |
echo "Error: ${SWTPM_INTERFACE} TPM initialization failed." |
| 65 |
echo "TPM Logfile:" |
| 66 |
cat "$logfile" |
| 67 |
exit 1 |
| 68 |
fi |
| 69 |
|
| 70 |
if ! kill_quiet -0 "${SWTPM_PID}" 2>/dev/null; then |
| 71 |
echo "Error: ${SWTPM_INTERFACE} TPM not running anymore after INIT." |
| 72 |
echo "TPM Logfile:" |
| 73 |
cat "$logfile" |
| 74 |
exit 1 |
| 75 |
fi |
| 76 |
|
| 77 |
# Startup the TPM |
| 78 |
RES=$(swtpm_cmd_tx "${SWTPM_INTERFACE}" '\x00\xC1\x00\x00\x00\x0C\x00\x00\x00\x99\x00\x01') |
| 79 |
exp=' 00 c4 00 00 00 0a 00 00 00 00' |
| 80 |
if [ "$RES" != "$exp" ]; then |
| 81 |
echo "Error: Did not get expected result from TPM_Startup(ST_Clear)" |
| 82 |
echo "expected: $exp" |
| 83 |
echo "received: $RES" |
| 84 |
exit 1 |
| 85 |
fi |
| 86 |
|
| 87 |
if ! run_swtpm_ioctl "${SWTPM_INTERFACE}" -h 1234; then |
| 88 |
echo "Error: Hash command did not work." |
| 89 |
echo "TPM Logfile:" |
| 90 |
cat "$logfile" |
| 91 |
exit 1 |
| 92 |
fi |
| 93 |
|
| 94 |
# Read PCR 17 |
| 95 |
RES=$(swtpm_cmd_tx "${SWTPM_INTERFACE}" '\x00\xC1\x00\x00\x00\x0E\x00\x00\x00\x15\x00\x00\x00\x11') |
| 96 |
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' |
| 97 |
if [ "$RES" != "$exp" ]; then |
| 98 |
echo "Error: (1) Did not get expected result from TPM_PCRRead(17)" |
| 99 |
echo "expected: $exp" |
| 100 |
echo "received: $RES" |
| 101 |
exit 1 |
| 102 |
fi |
| 103 |
|
| 104 |
# Save the volatile state |
| 105 |
if ! run_swtpm_ioctl "${SWTPM_INTERFACE}" -v; then |
| 106 |
echo "Error: Saving the volatile state failed." |
| 107 |
echo "TPM Logfile:" |
| 108 |
cat "$logfile" |
| 109 |
exit 1 |
| 110 |
fi |
| 111 |
|
| 112 |
if [ ! -r "$VOLATILE_STATE_FILE" ]; then |
| 113 |
echo "Error: Volatile state file $VOLATILE_STATE_FILE does not exist." |
| 114 |
echo "TPM Logfile:" |
| 115 |
cat "$logfile" |
| 116 |
exit 1 |
| 117 |
fi |
| 118 |
|
| 119 |
if ! tmp=$(run_swtpm_ioctl "${SWTPM_INTERFACE}" -g | cut -d":" -f2); then |
| 120 |
echo "Error: Could not get the configuration flags of the ${SWTPM_INTERFACE} TPM." |
| 121 |
echo "TPM Logfile:" |
| 122 |
cat "$logfile" |
| 123 |
exit 1 |
| 124 |
fi |
| 125 |
|
| 126 |
if [ "$tmp" != " 0x1" ]; then |
| 127 |
echo "Error: Unexpected configuration flags: $tmp; expected 0x1." |
| 128 |
echo "TPM Logfile:" |
| 129 |
cat "$logfile" |
| 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 |
echo "TPM Logfile:" |
| 137 |
cat "$logfile" |
| 138 |
exit 1 |
| 139 |
fi |
| 140 |
|
| 141 |
# Start the TPM again; have the keyfile removed |
| 142 |
TPM_PATH=$TPM_PATH run_swtpm "${SWTPM_INTERFACE}" \ |
| 143 |
--key "file=$binkeyfile,mode=aes-cbc,format=binary,remove" \ |
| 144 |
--log "file=$logfile" |
| 145 |
|
| 146 |
display_processes_by_name "swtpm" |
| 147 |
|
| 148 |
if ! kill_quiet -0 "${SWTPM_PID}"; then |
| 149 |
echo "Error (2): ${SWTPM_INTERFACE} TPM did not start." |
| 150 |
echo "TPM Logfile:" |
| 151 |
cat "$logfile" |
| 152 |
exit 1 |
| 153 |
fi |
| 154 |
|
| 155 |
if wait_file_gone "${binkeyfile}" 4; then |
| 156 |
echo "Error: Keyfile $binkeyfile was not removed by ${SWTPM_INTERFACE} tpm." |
| 157 |
echo "TPM Logfile:" |
| 158 |
cat "$logfile" |
| 159 |
exit 1 |
| 160 |
fi |
| 161 |
|
| 162 |
# Init the TPM |
| 163 |
if ! run_swtpm_ioctl "${SWTPM_INTERFACE}" -i; then |
| 164 |
echo "Error: ${SWTPM_INTERFACE} TPM initialization failed." |
| 165 |
echo "TPM Logfile:" |
| 166 |
cat "$logfile" |
| 167 |
exit 1 |
| 168 |
fi |
| 169 |
|
| 170 |
# Volatile state must have been removed by TPM now |
| 171 |
if [ -r "$VOLATILE_STATE_FILE" ]; then |
| 172 |
echo "Error: Volatile state file $VOLATILE_STATE_FILE still exists." |
| 173 |
echo "TPM Logfile:" |
| 174 |
cat "$logfile" |
| 175 |
exit 1 |
| 176 |
fi |
| 177 |
|
| 178 |
# Read the PCR again ... |
| 179 |
RES=$(swtpm_cmd_tx "${SWTPM_INTERFACE}" '\x00\xC1\x00\x00\x00\x0E\x00\x00\x00\x15\x00\x00\x00\x11') |
| 180 |
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' |
| 181 |
if [ "$RES" != "$exp" ]; then |
| 182 |
echo "Error: (2) Did not get expected result from TPM_PCRRead(17)" |
| 183 |
echo "expected: $exp" |
| 184 |
echo "received: $RES" |
| 185 |
exit 1 |
| 186 |
fi |
| 187 |
|
| 188 |
# Save the volatile state again |
| 189 |
if ! run_swtpm_ioctl "${SWTPM_INTERFACE}" -v; then |
| 190 |
echo "Error: Saving the volatile state failed." |
| 191 |
echo "TPM Logfile:" |
| 192 |
cat "$logfile" |
| 193 |
exit 1 |
| 194 |
fi |
| 195 |
|
| 196 |
if [ ! -r "$VOLATILE_STATE_FILE" ]; then |
| 197 |
echo "Error: Volatile state file $VOLATILE_STATE_FILE does not exist." |
| 198 |
echo "TPM Logfile:" |
| 199 |
cat "$logfile" |
| 200 |
exit 1 |
| 201 |
fi |
| 202 |
|
| 203 |
# Send a new TPM_Init |
| 204 |
if ! run_swtpm_ioctl "${SWTPM_INTERFACE}" -i; then |
| 205 |
echo "Error: ${SWTPM_INTERFACE} TPM initialization failed." |
| 206 |
echo "TPM Logfile:" |
| 207 |
cat "$logfile" |
| 208 |
exit 1 |
| 209 |
fi |
| 210 |
|
| 211 |
# Volatile state must have been removed by TPM now |
| 212 |
if [ -r "$VOLATILE_STATE_FILE" ]; then |
| 213 |
echo "Error: Volatile state file $VOLATILE_STATE_FILE still exists." |
| 214 |
echo "TPM Logfile:" |
| 215 |
cat "$logfile" |
| 216 |
exit 1 |
| 217 |
fi |
| 218 |
|
| 219 |
# Read the PCR again ... |
| 220 |
RES=$(swtpm_cmd_tx "${SWTPM_INTERFACE}" '\x00\xC1\x00\x00\x00\x0E\x00\x00\x00\x15\x00\x00\x00\x11') |
| 221 |
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' |
| 222 |
if [ "$RES" != "$exp" ]; then |
| 223 |
echo "Error: (2) Did not get expected result from TPM_PCRRead(17)" |
| 224 |
echo "expected: $exp" |
| 225 |
echo "received: $RES" |
| 226 |
exit 1 |
| 227 |
fi |
| 228 |
|
| 229 |
|
| 230 |
# Final shut down |
| 231 |
if ! run_swtpm_ioctl "${SWTPM_INTERFACE}" -s; then |
| 232 |
echo "Error: Could not shut down the ${SWTPM_INTERFACE} TPM." |
| 233 |
echo "TPM Logfile:" |
| 234 |
cat "$logfile" |
| 235 |
exit 1 |
| 236 |
fi |
| 237 |
|
| 238 |
if wait_process_gone "${SWTPM_PID}" 4; then |
| 239 |
echo "Error: ${SWTPM_INTERFACE} TPM should not be running anymore." |
| 240 |
echo "TPM Logfile:" |
| 241 |
cat "$logfile" |
| 242 |
exit 1 |
| 243 |
fi |
| 244 |
|
| 245 |
if [ ! -e "$STATE_FILE" ]; then |
| 246 |
echo "Error: TPM state file $STATE_FILE does not exist." |
| 247 |
echo "TPM Logfile:" |
| 248 |
cat "$logfile" |
| 249 |
exit 1 |
| 250 |
fi |
| 251 |
|
| 252 |
echo "OK" |
| 253 |
|
| 254 |
exit 0 |