| 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_NAME:-vtpm-test-save-load-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 |
MY_VOLATILE_STATE_FILE=$TPM_PATH/my.volatilestate |
| 16 |
MY_PERMANENT_STATE_FILE=$TPM_PATH/my.permanent |
| 17 |
SWTPM_CMD_UNIX_PATH=${TPM_PATH}/unix-cmd.sock |
| 18 |
SWTPM_CTRL_UNIX_PATH=${TPM_PATH}/unix-ctrl.sock |
| 19 |
SWTPM_INTERFACE=${SWTPM_INTERFACE:-cuse} |
| 20 |
|
| 21 |
keyfile="$(mktemp)" || exit 1 |
| 22 |
logfile="$(mktemp)" || exit 1 |
| 23 |
echo "$KEY" > "$keyfile" |
| 24 |
|
| 25 |
function cleanup() |
| 26 |
{ |
| 27 |
pid=${SWTPM_PID} |
| 28 |
if [ -n "$pid" ]; then |
| 29 |
kill_quiet -9 "$pid" |
| 30 |
fi |
| 31 |
rm -f "$keyfile" "$logfile" |
| 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 |
exec 101<"$keyfile" |
| 43 |
TPM_PATH=$TPM_PATH run_swtpm "${SWTPM_INTERFACE}" \ |
| 44 |
--key fd=101,mode=aes-cbc,format=hex \ |
| 45 |
--log "file=$logfile" |
| 46 |
exec 101>&- |
| 47 |
|
| 48 |
display_processes_by_name "swtpm" |
| 49 |
|
| 50 |
if ! kill_quiet -0 "${SWTPM_PID}"; then |
| 51 |
echo "Error: ${SWTPM_INTERFACE} TPM did not start." |
| 52 |
echo "TPM Logfile:" |
| 53 |
cat "$logfile" |
| 54 |
exit 1 |
| 55 |
fi |
| 56 |
|
| 57 |
# Init the TPM |
| 58 |
if ! run_swtpm_ioctl "${SWTPM_INTERFACE}" -i; then |
| 59 |
echo "Error: ${SWTPM_INTERFACE} TPM initialization failed." |
| 60 |
echo "TPM Logfile:" |
| 61 |
cat "$logfile" |
| 62 |
exit 1 |
| 63 |
fi |
| 64 |
|
| 65 |
if ! kill_quiet -0 "${SWTPM_PID}" 2>/dev/null; then |
| 66 |
echo "Error: ${SWTPM_INTERFACE} TPM not running anymore after INIT." |
| 67 |
echo "TPM Logfile:" |
| 68 |
cat "$logfile" |
| 69 |
exit 1 |
| 70 |
fi |
| 71 |
|
| 72 |
# Startup the TPM |
| 73 |
RES=$(swtpm_cmd_tx "${SWTPM_INTERFACE}" '\x00\xC1\x00\x00\x00\x0C\x00\x00\x00\x99\x00\x01') |
| 74 |
exp=' 00 c4 00 00 00 0a 00 00 00 00' |
| 75 |
if [ "$RES" != "$exp" ]; then |
| 76 |
echo "Error: Did not get expected result from TPM_Startup(ST_Clear)" |
| 77 |
echo "expected: $exp" |
| 78 |
echo "received: $RES" |
| 79 |
exit 1 |
| 80 |
fi |
| 81 |
|
| 82 |
if ! run_swtpm_ioctl "${SWTPM_INTERFACE}" -h 1234; then |
| 83 |
echo "Error: Could not hash the data." |
| 84 |
echo "TPM Logfile:" |
| 85 |
cat "$logfile" |
| 86 |
exit 1 |
| 87 |
fi |
| 88 |
|
| 89 |
# Read PCR 17 |
| 90 |
RES=$(swtpm_cmd_tx "${SWTPM_INTERFACE}" '\x00\xC1\x00\x00\x00\x0E\x00\x00\x00\x15\x00\x00\x00\x11') |
| 91 |
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' |
| 92 |
if [ "$RES" != "$exp" ]; then |
| 93 |
echo "Error: (1) Did not get expected result from TPM_PCRRead(17)" |
| 94 |
echo "expected: $exp" |
| 95 |
echo "received: $RES" |
| 96 |
exit 1 |
| 97 |
fi |
| 98 |
|
| 99 |
# Assert physical presence |
| 100 |
RES=$(swtpm_cmd_tx "${SWTPM_INTERFACE}" '\x00\xC1\x00\x00\x00\x0C\x40\x00\x00\x0A\x00\x20') |
| 101 |
exp=' 00 c4 00 00 00 0a 00 00 00 00' |
| 102 |
if [ "$RES" != "$exp" ]; then |
| 103 |
echo "Error: (1) Did not get expected result from TSC_PhysicalPresence(ENABLE)" |
| 104 |
echo "expected: $exp" |
| 105 |
echo "received: $RES" |
| 106 |
exit 1 |
| 107 |
fi |
| 108 |
|
| 109 |
# Create a big NVRAM Area with 4000 bytes (0xfa0) |
| 110 |
tmp='\x00\xC1\x00\x00\x00\x65\x00\x00\x00\xcc\x00\x18\x00\x00\x00\x01' |
| 111 |
tmp+='\x00\x03\x00\x00\x00\x01\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00' |
| 112 |
tmp+='\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x03\x00\x00\x00\x01' |
| 113 |
tmp+='\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00' |
| 114 |
tmp+='\x00\x00\x00\x00\x00\x17\x00\x01\x00\x01\x00\x00\x00\x00\x00\x0f' |
| 115 |
tmp+='\xa0\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00' |
| 116 |
tmp+='\x00\x00\x00\x00\x00' |
| 117 |
RES=$(swtpm_cmd_tx "${SWTPM_INTERFACE}" $tmp) |
| 118 |
exp=' 00 c4 00 00 00 0a 00 00 00 00' |
| 119 |
if [ "$RES" != "$exp" ]; then |
| 120 |
echo "Error: (1) Did not get expected result from TPM_NVDefineSpace()" |
| 121 |
echo "expected: $exp" |
| 122 |
echo "received: $RES" |
| 123 |
exit 1 |
| 124 |
fi |
| 125 |
|
| 126 |
if ! run_swtpm_ioctl "${SWTPM_INTERFACE}" --save permanent "$MY_PERMANENT_STATE_FILE"; then |
| 127 |
echo "Error: Could not write permanent state file $MY_PERMANENT_STATE_FILE." |
| 128 |
echo "TPM Logfile:" |
| 129 |
cat "$logfile" |
| 130 |
exit 1 |
| 131 |
fi |
| 132 |
if [ ! -r "$MY_PERMANENT_STATE_FILE" ]; then |
| 133 |
echo "Error: Permanent state file $MY_PERMANENT_STATE_FILE does not exist." |
| 134 |
echo "TPM Logfile:" |
| 135 |
cat "$logfile" |
| 136 |
exit 1 |
| 137 |
fi |
| 138 |
echo "Saved permanent state." |
| 139 |
|
| 140 |
if ! run_swtpm_ioctl "${SWTPM_INTERFACE}" --save volatile "$MY_VOLATILE_STATE_FILE"; then |
| 141 |
echo "Error: Could not write volatile state file $MY_PERMANENT_STATE_FILE." |
| 142 |
echo "TPM Logfile:" |
| 143 |
cat "$logfile" |
| 144 |
exit 1 |
| 145 |
fi |
| 146 |
if [ ! -r "$MY_VOLATILE_STATE_FILE" ]; then |
| 147 |
echo "Error: Volatile state file $MY_VOLATILE_STATE_FILE does not exist." |
| 148 |
echo "TPM Logfile:" |
| 149 |
cat "$logfile" |
| 150 |
exit 1 |
| 151 |
fi |
| 152 |
echo "Saved volatile state." |
| 153 |
|
| 154 |
#ls -l $(dirname $MY_VOLATILE_STATE_FILE)/* |
| 155 |
#sha1sum $(dirname $MY_VOLATILE_STATE_FILE)/* |
| 156 |
|
| 157 |
# we will use our own volatile state |
| 158 |
rm -f "$VOLATILE_STATE_FILE" "$STATE_FILE" |
| 159 |
|
| 160 |
# Stop the TPM; this will not shut it down |
| 161 |
if ! run_swtpm_ioctl "${SWTPM_INTERFACE}" --stop; then |
| 162 |
echo "Error (2): Could not stop the ${SWTPM_INTERFACE} TPM" |
| 163 |
echo "TPM Logfile:" |
| 164 |
cat "$logfile" |
| 165 |
exit 1 |
| 166 |
fi |
| 167 |
|
| 168 |
if ! kill_quiet -0 "${SWTPM_PID}"; then |
| 169 |
echo "Error (2): ${SWTPM_INTERFACE} TPM is not running anymore." |
| 170 |
echo "TPM Logfile:" |
| 171 |
cat "$logfile" |
| 172 |
exit 1 |
| 173 |
fi |
| 174 |
|
| 175 |
# load state into the TPM |
| 176 |
if ! run_swtpm_ioctl "${SWTPM_INTERFACE}" --load permanent "$MY_PERMANENT_STATE_FILE"; then |
| 177 |
echo "Could not load permanent state into vTPM" |
| 178 |
echo "TPM Logfile:" |
| 179 |
cat "$logfile" |
| 180 |
exit 1 |
| 181 |
fi |
| 182 |
echo "Loaded permanent state." |
| 183 |
|
| 184 |
if ! run_swtpm_ioctl "${SWTPM_INTERFACE}" --load volatile "$MY_VOLATILE_STATE_FILE"; then |
| 185 |
echo "Could not load volatile state into vTPM" |
| 186 |
echo "TPM Logfile:" |
| 187 |
cat "$logfile" |
| 188 |
exit 1 |
| 189 |
fi |
| 190 |
echo "Loaded volatile state." |
| 191 |
|
| 192 |
#ls -l $(dirname $MY_VOLATILE_STATE_FILE)/* |
| 193 |
#sha1sum $(dirname $MY_VOLATILE_STATE_FILE)/* |
| 194 |
|
| 195 |
# Init the TPM |
| 196 |
if ! run_swtpm_ioctl "${SWTPM_INTERFACE}" -i; then |
| 197 |
echo "TPM Init failed." |
| 198 |
echo "TPM Logfile:" |
| 199 |
cat "$logfile" |
| 200 |
exit 1 |
| 201 |
fi |
| 202 |
|
| 203 |
# Volatile state must have been removed by TPM now |
| 204 |
if [ -r "$VOLATILE_STATE_FILE" ]; then |
| 205 |
echo "Error: Volatile state file $VOLATILE_STATE_FILE still exists." |
| 206 |
echo "TPM Logfile:" |
| 207 |
cat "$logfile" |
| 208 |
exit 1 |
| 209 |
fi |
| 210 |
|
| 211 |
# Read the PCR again ... |
| 212 |
RES=$(swtpm_cmd_tx "${SWTPM_INTERFACE}" '\x00\xC1\x00\x00\x00\x0E\x00\x00\x00\x15\x00\x00\x00\x11') |
| 213 |
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' |
| 214 |
if [ "$RES" != "$exp" ]; then |
| 215 |
echo "Error: (2) Did not get expected result from TPM_PCRRead(17)" |
| 216 |
echo "expected: $exp" |
| 217 |
echo "received: $RES" |
| 218 |
exit 1 |
| 219 |
fi |
| 220 |
|
| 221 |
# Save the volatile state again |
| 222 |
if ! run_swtpm_ioctl "${SWTPM_INTERFACE}" -v; then |
| 223 |
echo "Error: Could not have the ${SWTPM_INTERFACE} TPM write the volatile state to a file." |
| 224 |
echo "TPM Logfile:" |
| 225 |
cat "$logfile" |
| 226 |
exit 1 |
| 227 |
fi |
| 228 |
if [ ! -r "$VOLATILE_STATE_FILE" ]; then |
| 229 |
echo "Error: Volatile state file $VOLATILE_STATE_FILE does not exist." |
| 230 |
echo "TPM Logfile:" |
| 231 |
cat "$logfile" |
| 232 |
exit 1 |
| 233 |
fi |
| 234 |
|
| 235 |
# Send a new TPM_Init |
| 236 |
if ! run_swtpm_ioctl "${SWTPM_INTERFACE}" -i; then |
| 237 |
echo "Error: ${SWTPM_INTERFACE} TPM initialization failed." |
| 238 |
echo "TPM Logfile:" |
| 239 |
cat "$logfile" |
| 240 |
exit 1 |
| 241 |
fi |
| 242 |
|
| 243 |
# Volatile state must have been removed by TPM now |
| 244 |
if [ -r "$VOLATILE_STATE_FILE" ]; then |
| 245 |
echo "Error: Volatile state file $VOLATILE_STATE_FILE still exists." |
| 246 |
echo "TPM Logfile:" |
| 247 |
cat "$logfile" |
| 248 |
exit 1 |
| 249 |
fi |
| 250 |
|
| 251 |
# Read the PCR again ... |
| 252 |
RES=$(swtpm_cmd_tx "${SWTPM_INTERFACE}" '\x00\xC1\x00\x00\x00\x0E\x00\x00\x00\x15\x00\x00\x00\x11') |
| 253 |
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' |
| 254 |
if [ "$RES" != "$exp" ]; then |
| 255 |
echo "Error: (2) Did not get expected result from TPM_PCRRead(17)" |
| 256 |
echo "expected: $exp" |
| 257 |
echo "received: $RES" |
| 258 |
exit 1 |
| 259 |
fi |
| 260 |
|
| 261 |
if ! run_swtpm_ioctl "${SWTPM_INTERFACE}" -s; then |
| 262 |
echo "Error: Could not shut down the ${SWTPM_INTERFACE} TPM." |
| 263 |
echo "TPM Logfile:" |
| 264 |
cat "$logfile" |
| 265 |
exit 1 |
| 266 |
fi |
| 267 |
|
| 268 |
if wait_process_gone "${SWTPM_PID}" 4; then |
| 269 |
echo "Error: ${SWTPM_INTERFACE} TPM should not be running anymore." |
| 270 |
echo "TPM Logfile:" |
| 271 |
cat "$logfile" |
| 272 |
exit 1 |
| 273 |
fi |
| 274 |
|
| 275 |
echo "Test 1: Ok" |
| 276 |
|
| 277 |
# This time start we start the TPM with a wrong state encryption key |
| 278 |
# (key used as password) and try to start it. It has to fail and |
| 279 |
# the state must not have been modified. |
| 280 |
|
| 281 |
# volatile state file does not exist |
| 282 |
sha1_volatile=$(get_sha1_file "${VOLATILE_STATE_FILE}") |
| 283 |
sha1_permanent=$(get_sha1_file "${STATE_FILE}") |
| 284 |
echo "sha1(volatile) : $sha1_volatile" |
| 285 |
echo "sha1(permanent): $sha1_permanent" |
| 286 |
|
| 287 |
exec 101<"$keyfile" |
| 288 |
TPM_PATH=$TPM_PATH run_swtpm "${SWTPM_INTERFACE}" \ |
| 289 |
--key pwdfd=101 \ |
| 290 |
--log "file=$logfile" |
| 291 |
exec 101>&- |
| 292 |
|
| 293 |
display_processes_by_name "swtpm" |
| 294 |
|
| 295 |
if ! kill_quiet -0 "${SWTPM_PID}"; then |
| 296 |
echo "Error: ${SWTPM_INTERFACE} TPM did not start." |
| 297 |
echo "TPM Logfile:" |
| 298 |
cat "$logfile" |
| 299 |
exit 1 |
| 300 |
fi |
| 301 |
|
| 302 |
# Init the TPM |
| 303 |
if ERR="$(run_swtpm_ioctl "${SWTPM_INTERFACE}" -i 2>&1)"; then |
| 304 |
echo "Error: ${SWTPM_INTERFACE} TPM initialization should have failed." |
| 305 |
echo "TPM Logfile:" |
| 306 |
cat "$logfile" |
| 307 |
exit 1 |
| 308 |
fi |
| 309 |
exp="TPM result from PTM_INIT: 0x21" |
| 310 |
if [ "$ERR" != "$exp" ]; then |
| 311 |
echo "Error: Unexpected error message" |
| 312 |
echo "Received: $ERR" |
| 313 |
echo "Expected: $exp" |
| 314 |
exit 1 |
| 315 |
fi |
| 316 |
|
| 317 |
if ! kill_quiet -0 "${SWTPM_PID}" 2>/dev/null; then |
| 318 |
echo "Error: ${SWTPM_INTERFACE} TPM not running anymore after failed INIT." |
| 319 |
echo "TPM Logfile:" |
| 320 |
cat "$logfile" |
| 321 |
exit 1 |
| 322 |
fi |
| 323 |
|
| 324 |
if [ "${sha1_volatile}" != "$(get_sha1_file "${VOLATILE_STATE_FILE}")" ]; then |
| 325 |
echo "Error: Volatile state file was modified during failed init." |
| 326 |
exit 1 |
| 327 |
fi |
| 328 |
|
| 329 |
if [ "${sha1_permanent}" != "$(get_sha1_file "${STATE_FILE}")" ]; then |
| 330 |
echo "Error: Permanent state file was modified during failed init." |
| 331 |
exit 1 |
| 332 |
fi |
| 333 |
|
| 334 |
# shut it down |
| 335 |
if ! run_swtpm_ioctl "${SWTPM_INTERFACE}" -s; then |
| 336 |
echo "Error: Could not shut down the ${SWTPM_INTERFACE} TPM." |
| 337 |
echo "TPM Logfile:" |
| 338 |
cat "$logfile" |
| 339 |
exit 1 |
| 340 |
fi |
| 341 |
|
| 342 |
if wait_process_gone "${SWTPM_PID}" 4; then |
| 343 |
echo "Error: ${SWTPM_INTERFACE} TPM should not be running anymore." |
| 344 |
echo "TPM Logfile:" |
| 345 |
cat "$logfile" |
| 346 |
exit 1 |
| 347 |
fi |
| 348 |
|
| 349 |
echo "Test 2: Ok" |
| 350 |
|
| 351 |
# This time start we start the TPM with a different cipher and try to |
| 352 |
# start it. It has to fail and the state must not have been modified. |
| 353 |
|
| 354 |
# volatile state file does not exist |
| 355 |
sha1_volatile=$(get_sha1_file "${VOLATILE_STATE_FILE}") |
| 356 |
sha1_permanent=$(get_sha1_file "${STATE_FILE}") |
| 357 |
echo "sha1(volatile) : $sha1_volatile" |
| 358 |
echo "sha1(permanent): $sha1_permanent" |
| 359 |
|
| 360 |
# we need a 256bit key |
| 361 |
echo "${KEY}${KEY}" > "$keyfile" |
| 362 |
|
| 363 |
TPM_PATH=$TPM_PATH run_swtpm "${SWTPM_INTERFACE}" \ |
| 364 |
--key "pwdfile=$keyfile,mode=aes-256-cbc" \ |
| 365 |
--log "file=$logfile" |
| 366 |
|
| 367 |
display_processes_by_name "swtpm" |
| 368 |
|
| 369 |
if ! kill_quiet -0 "${SWTPM_PID}"; then |
| 370 |
echo "Error: ${SWTPM_INTERFACE} TPM did not start." |
| 371 |
echo "TPM Logfile:" |
| 372 |
cat "$logfile" |
| 373 |
exit 1 |
| 374 |
fi |
| 375 |
|
| 376 |
# Init the TPM |
| 377 |
if ERR="$(run_swtpm_ioctl "${SWTPM_INTERFACE}" -i 2>&1)"; then |
| 378 |
echo "Error: ${SWTPM_INTERFACE} TPM initialization should have failed." |
| 379 |
echo "TPM Logfile:" |
| 380 |
cat "$logfile" |
| 381 |
exit 1 |
| 382 |
fi |
| 383 |
exp="TPM result from PTM_INIT: 0x28" |
| 384 |
if [ "$ERR" != "$exp" ]; then |
| 385 |
echo "Error: Unexpected error message" |
| 386 |
echo "Received: $ERR" |
| 387 |
echo "Expected: $exp" |
| 388 |
exit 1 |
| 389 |
fi |
| 390 |
|
| 391 |
if ! kill_quiet -0 "${SWTPM_PID}" 2>/dev/null; then |
| 392 |
echo "Error: ${SWTPM_INTERFACE} TPM not running anymore after failed INIT." |
| 393 |
echo "TPM Logfile:" |
| 394 |
cat "$logfile" |
| 395 |
exit 1 |
| 396 |
fi |
| 397 |
|
| 398 |
if [ "${sha1_volatile}" != "$(get_sha1_file "${VOLATILE_STATE_FILE}")" ]; then |
| 399 |
echo "Error: Volatile state file was modified during failed init." |
| 400 |
exit 1 |
| 401 |
fi |
| 402 |
|
| 403 |
if [ "${sha1_permanent}" != "$(get_sha1_file "${STATE_FILE}")" ]; then |
| 404 |
echo "Error: Permanent state file was modified during failed init." |
| 405 |
exit 1 |
| 406 |
fi |
| 407 |
|
| 408 |
echo "Test 3: Ok" |
| 409 |
|
| 410 |
# Final shut down |
| 411 |
if ! run_swtpm_ioctl "${SWTPM_INTERFACE}" -s; then |
| 412 |
echo "Error: Could not shut down the ${SWTPM_INTERFACE} TPM." |
| 413 |
echo "TPM Logfile:" |
| 414 |
cat "$logfile" |
| 415 |
exit 1 |
| 416 |
fi |
| 417 |
|
| 418 |
if wait_process_gone "${SWTPM_PID}" 4; then |
| 419 |
echo "Error: ${SWTPM_INTERFACE} TPM should not be running anymore." |
| 420 |
echo "TPM Logfile:" |
| 421 |
cat "$logfile" |
| 422 |
exit 1 |
| 423 |
fi |
| 424 |
|
| 425 |
if [ ! -e "$STATE_FILE" ]; then |
| 426 |
echo "Error: TPM state file $STATE_FILE does not exist." |
| 427 |
echo "TPM Logfile:" |
| 428 |
cat "$logfile" |
| 429 |
exit 1 |
| 430 |
fi |
| 431 |
|
| 432 |
echo "OK" |
| 433 |
|
| 434 |
exit 0 |