#!/bin/bash

# For the license, see the LICENSE file in the root directory.
#set -x


ROOT=${abs_top_builddir:-$(pwd)/..}
TESTDIR=${abs_top_testdir:-$(dirname "$0")}

VTPM_NAME="${VTPM_NAME:-vtpm-tpm2-test-save-load-state-da-timeout}"
SWTPM_DEV_NAME="/dev/${VTPM_NAME}"
TPM_PATH="$(mktemp -d)" || exit 1
STATE_FILE=$TPM_PATH/tpm2-00.permall
VOLATILE_STATE_FILE=$TPM_PATH/tpm2-00.volatilestate
MY_VOLATILE_STATE_FILE=$TPM_PATH/my.volatilestate
MY_PERMANENT_STATE_FILE=$TPM_PATH/my.permanent
SWTPM_INTERFACE=${SWTPM_INTERFACE:-cuse}
SWTPM_CMD_UNIX_PATH=${TPM_PATH}/unix-cmd.sock
SWTPM_CTRL_UNIX_PATH=${TPM_PATH}/unix-ctrl.sock

logfile="$(mktemp)" || exit 1

function cleanup()
{
	pid=${SWTPM_PID}
	if [ -n "$pid" ]; then
		kill_quiet -9 "$pid"
	fi
	rm -f "$logfile"
	rm -rf "$TPM_PATH"
}

trap "cleanup" EXIT

source "${TESTDIR}/common"
[ "${SWTPM_INTERFACE}" == "cuse" ] && source "${TESTDIR}/test_cuse"

rm -f "$STATE_FILE" "$VOLATILE_STATE_FILE" 2>/dev/null

TPM_PATH=$TPM_PATH run_swtpm "${SWTPM_INTERFACE}" \
	--log "file=$logfile,level=20" \
	--tpm2

display_processes_by_name "swtpm"

if ! kill_quiet -0 "${SWTPM_PID}"; then
	echo "Error: ${SWTPM_INTERFACE} TPM did not start."
	echo "TPM Logfile:"
	cat "$logfile"
	exit 1
fi

# Init the TPM
if ! run_swtpm_ioctl "${SWTPM_INTERFACE}" -i; then
	echo "Error: Could not initialize the ${SWTPM_INTERFACE} TPM."
	echo "TPM Logfile:"
	cat "$logfile"
	exit 1
fi

if ! kill_quiet -0 "${SWTPM_PID}" 2>/dev/null; then
	echo "Error: ${SWTPM_INTERFACE} TPM not running anymore after INIT."
	echo "TPM Logfile:"
	cat "$logfile"
	exit 1
fi

# Startup the TPM
RES=$(swtpm_cmd_tx "${SWTPM_INTERFACE}" '\x80\x01\x00\x00\x00\x0c\x00\x00\x01\x44\x00\x00')
exp=' 80 01 00 00 00 0a 00 00 00 00'
if [ "$RES" != "$exp" ]; then
	echo "Error: Did not get expected result from TPM2_Startup(ST_Clear)"
	echo "expected: $exp"
	echo "received: $RES"
	exit 1
fi

# Create an NVRAM location: nvdefinespace -hi o -ha 01000000 -pwdn nnn  -sz 16 -at da
RES=$(swtpm_cmd_tx "${SWTPM_INTERFACE}" '\x80\x02\x00\x00\x00\x30\x00\x00\x01\x2a\x40\x00\x00\x01\x00\x00\x00\x09\x40\x00\x00\x09\x00\x00\x00\x00\x00\x00\x03\x6e\x6e\x6e\x00\x0e\x01\x00\x00\x00\x00\x0b\x00\x04\x00\x04\x00\x00\x00\x10')
exp=' 80 02 00 00 00 13 00 00 00 00 00 00 00 00 00 00 01 00 00'
if [ "$RES" != "$exp" ]; then
	echo "Error: Did not get expected result from TPM2_NV_DefineSpace()"
	echo "expected: $exp"
	echo "received: $RES"
	exit 1
fi

sleep 1

# TPM2_NV_Write with good password 'nnn' and bad password 'nn'
NVWRITE_GOOD='\x80\x02\x00\x00\x00\x2a\x00\x00\x01\x37\x01\x00\x00\x00\x01\x00\x00\x00\x00\x00\x00\x0c\x40\x00\x00\x09\x00\x00\x00\x00\x03\x6e\x6e\x6e\x00\x04\x74\x65\x73\x74\x00\x00'
NVWRITE_BAD='\x80\x02\x00\x00\x00\x29\x00\x00\x01\x37\x01\x00\x00\x00\x01\x00\x00\x00\x00\x00\x00\x0b\x40\x00\x00\x09\x00\x00\x00\x00\x02\x6e\x6e\x00\x04\x74\x65\x73\x74\x00\x00'

# Write some data into the NVRAM area: nvwrite -ha 01000000 -ic test -pwdn nnn
# Due to -at da being used on TPM2_NV_DefineSpace, the first time the command will request a retry
for ((i = 0; i < 2; i++)); do
	RES=$(swtpm_cmd_tx "${SWTPM_INTERFACE}" ${NVWRITE_GOOD})
	exp=' 80 02 00 00 00 13 00 00 00 00 00 00 00 00 00 00 01 00 00'
	if [ "$i" -eq 1 ] && [ "$RES" != "$exp" ]; then
		echo "Error: Did not get expected result from TPM2_NV_Write()"
		echo "expected: $exp"
		echo "received: $RES"
		exit 1
	fi
done

# Set the dictionary attack parameters: dictionaryattackparameters -lr 6 -nrt 6
RES=$(swtpm_cmd_tx "${SWTPM_INTERFACE}" '\x80\x02\x00\x00\x00\x27\x00\x00\x01\x3a\x40\x00\x00\x0a\x00\x00\x00\x09\x40\x00\x00\x09\x00\x00\x00\x00\x00\x00\x00\x00\x01\x00\x00\x00\x06\x00\x00\x00\x06')
exp=' 80 02 00 00 00 13 00 00 00 00 00 00 00 00 00 00 01 00 00'
if [ "$RES" != "$exp" ]; then
	echo "Error: Did not get expected result from TPM2_DictionaryAttackParameters()"
	echo "expected: $exp"
	echo "received: $RES"
	exit 1
fi

timenow=$(date +%s)
timeout=$((timenow + 6))
# Send TPM2_NV_Write with wrong password
RES=$(swtpm_cmd_tx "${SWTPM_INTERFACE}" ${NVWRITE_BAD})
exp=' 80 01 00 00 00 0a 00 00 09 8e'
if [ "$RES" != "$exp" ]; then
	echo "Error: Did not get expected result from TPM2_NV_Write() with bad password"
	echo "expected: $exp"
	echo "received: $RES"
	exit 1
fi
timenow=$(date +%s)
timerecovery=$((timenow + 6))

# TPM2_NV_Write with good password must now fail until $timeout
while :; do
	timenow=$(date +%s)
	echo "Writing with good password failed due to lockout until $timeout - now is $timenow."
	RES=$(swtpm_cmd_tx "${SWTPM_INTERFACE}" ${NVWRITE_GOOD})
	exp=' 80 01 00 00 00 0a 00 00 09 21'
	# busy systems may run the above at >= $timeout and get an unexpected result; check time again
	timenow=$(date +%s)
	if [ "$RES" != "$exp" ] && [ "$timenow" -lt "$timeout" ]; then
		echo "Error: Did not get expected failure from TPM2_NV_Write() with good password. Lockout should be enabled."
		echo "expected: $exp"
		echo "received: $RES"
		exit 1
	fi
	[ "$timenow" -ge "$timeout" ] && break
	sleep 1
done

while :; do
	sleep 1
	timenow=$(date +%s)
	[ "$timenow" -gt "$timerecovery" ] && break
done
timenow_after=$(date +%s)
echo "Time is now ${timenow_after} -- trying with good password should work now."
# Now writing with the good password must work again
RES=$(swtpm_cmd_tx "${SWTPM_INTERFACE}" ${NVWRITE_GOOD})
exp=' 80 02 00 00 00 13 00 00 00 00 00 00 00 00 00 00 01 00 00'
if [ "$RES" != "$exp" ]; then
	echo "Error: Did not get expected result from TPM2_NV_Write() with good password"
	echo "expected: $exp"
	echo "received: $RES"
	# OS X special check
	if [ "$timenow_after" -lt "$timenow" ]; then
		echo "Time went backwards..."
		exit 77
	fi
	exit 1
fi

timenow=$(date +%s)
timeout=$((timenow + 6))
# Again cause lockout: Send TPM2_NV_Write with wrong password
RES=$(swtpm_cmd_tx "${SWTPM_INTERFACE}" ${NVWRITE_BAD})
exp=' 80 01 00 00 00 0a 00 00 09 8e'
if [ "$RES" != "$exp" ]; then
	echo "Error: Did not get expected result from TPM2_NV_Write() with bad password"
	echo "expected: $exp"
	echo "received: $RES"
	exit 1
fi
timenow=$(date +%s)
timerecovery=$((timenow + 6))

# Save the state and restore it and then try to poll again
if ! run_swtpm_ioctl "${SWTPM_INTERFACE}" --save permanent "$MY_PERMANENT_STATE_FILE"; then
	echo "Error: Could not write permanent state file $MY_PERMANENT_STATE_FILE."
	echo "TPM Logfile:"
	cat "$logfile"
	exit 1
fi
if [ ! -r "$MY_PERMANENT_STATE_FILE" ]; then
	echo "Error: Permanent state file $MY_PERMANENT_STATE_FILE does not exist."
	echo "TPM Logfile:"
	cat "$logfile"
	exit 1
fi
echo "Saved permanent state."

if ! run_swtpm_ioctl "${SWTPM_INTERFACE}" --save volatile "$MY_VOLATILE_STATE_FILE"; then
	echo "Error: Could not write volatile state file $MY_VOLATILE_STATE_FILE."
	echo "TPM Logfile:"
	cat "$logfile"
	exit 1
fi
if [ ! -r "$MY_VOLATILE_STATE_FILE" ]; then
	echo "Error: Volatile state file $MY_VOLATILE_STATE_FILE does not exist."
	echo "TPM Logfile:"
	cat "$logfile"
	exit 1
fi
echo "Saved volatile state."

# we will use our own volatile state
rm -f "$VOLATILE_STATE_FILE" "$STATE_FILE"

# Stop the TPM; this will not shut it down
if ! run_swtpm_ioctl "${SWTPM_INTERFACE}" --stop; then
	echo "Error: Could not stop the ${SWTPM_INTERFACE} TPM."
	echo "TPM Logfile:"
	cat "$logfile"
	exit 1
fi

if ! kill_quiet -0 "${SWTPM_PID}"; then
	echo "Error (2): ${SWTPM_INTERFACE} TPM is not running anymore."
	echo "TPM Logfile:"
	cat "$logfile"
	exit 1
fi

# load state into the TPM
if ! run_swtpm_ioctl "${SWTPM_INTERFACE}" --load permanent "$MY_PERMANENT_STATE_FILE"; then
	echo "Could not load permanent state into vTPM"
	echo "TPM Logfile:"
	cat "$logfile"
	exit 1
fi
echo "Loaded permanent state."

if ! run_swtpm_ioctl "${SWTPM_INTERFACE}" --load volatile "$MY_VOLATILE_STATE_FILE"; then
	echo "Could not load volatile state into vTPM"
	echo "TPM Logfile:"
	cat "$logfile"
	exit 1
fi
echo "Loaded volatile state."

#ls -l $(dirname $MY_VOLATILE_STATE_FILE)/*
#sha1sum $(dirname $MY_VOLATILE_STATE_FILE)/*

# Init the TPM
if ! run_swtpm_ioctl "${SWTPM_INTERFACE}" -i; then
	echo "TPM Init failed."
	echo "TPM Logfile:"
	cat "$logfile"
	exit 1
fi

# Volatile state must have been removed by TPM now
if [ -r "$VOLATILE_STATE_FILE" ]; then
	echo "Error: Volatile state file $VOLATILE_STATE_FILE still exists."
	echo "TPM Logfile:"
	cat "$logfile"
	exit 1
fi


# TPM2_NV_Write with good password must now fail until $timeout
while :; do
	timenow=$(date +%s)
	echo "Writing with good password failed due to lockout until $timeout - now is $timenow."
	RES=$(swtpm_cmd_tx "${SWTPM_INTERFACE}" ${NVWRITE_GOOD})
	exp=' 80 01 00 00 00 0a 00 00 09 21'
	# busy systems may run the above at >= $timeout and get an unexpected result; check time again
	timenow=$(date +%s)
	if [ "$RES" != "$exp" ] && [ "$timenow" -lt "$timeout" ]; then
		echo "Error: Did not get expected failure from TPM2_NV_Write() with good password. Lockout should be enabled."
		echo "expected: $exp"
		echo "received: $RES"
		exit 1
	fi
	[ "$timenow" -ge "$timeout" ] && break
	sleep 1
done

while :; do
	sleep 1
	timenow=$(date +%s)
	[ "$timenow" -gt "$timerecovery" ] && break
done
timenow_after=$(date +%s)
echo "Time is now $timenow_after -- trying with good password should work now."
# Now writing with the good password must work again
RES=$(swtpm_cmd_tx "${SWTPM_INTERFACE}" ${NVWRITE_GOOD})
exp=' 80 02 00 00 00 13 00 00 00 00 00 00 00 00 00 00 01 00 00'
if [ "$RES" != "$exp" ]; then
	echo "Error: Did not get expected result from TPM2_NV_Write() with good password"
	echo "expected: $exp"
	echo "received: $RES"
	# OS X special check
	if [ "$timenow_after" -lt "$timenow" ]; then
		echo "Time went backwards..."
		exit 77
	fi
	exit 1
fi

# Final shut down
if ! run_swtpm_ioctl "${SWTPM_INTERFACE}" -s; then
	echo "Error: Could not shut down the ${SWTPM_INTERFACE} TPM."
	echo "TPM Logfile:"
	cat "$logfile"
	exit 1
fi

if wait_process_gone "${SWTPM_PID}" 4; then
	echo "Error: ${SWTPM_INTERFACE} TPM should not be running anymore."
	echo "TPM Logfile:"
	cat "$logfile"
	exit 1
fi

if [ ! -e "$STATE_FILE" ]; then
	echo "Error: TPM state file $STATE_FILE does not exist."
	echo "TPM Logfile:"
	cat "$logfile"
	exit 1
fi

echo "OK"

exit 0
