| 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-tpm2-test-save-load-state-locking}" |
| 10 |
SWTPM_DEV_NAME="/dev/${VTPM_NAME}" |
| 11 |
TPM_PATH="$(mktemp -d)" || exit 1 |
| 12 |
STATE_FILE="$TPM_PATH/tpm2-00.permall" |
| 13 |
VOLATILE_STATE_FILE="$TPM_PATH/tpm2-00.volatilestate" |
| 14 |
MY_VOLATILE_STATE_FILE="$TPM_PATH/my.volatilestate" |
| 15 |
MY_PERMANENT_STATE_FILE="$TPM_PATH/my.permanent" |
| 16 |
MY_SAVESTATE_STATE_FILE="$TPM_PATH/my.savestate" |
| 17 |
SWTPM_INTERFACE="${SWTPM_INTERFACE:-cuse}" |
| 18 |
SWTPM_CMD_UNIX_PATH="${TPM_PATH}/unix-cmd.sock" |
| 19 |
SWTPM_CTRL_UNIX_PATH="${TPM_PATH}/unix-ctrl.sock" |
| 20 |
|
| 21 |
function cleanup() |
| 22 |
{ |
| 23 |
if [ -n "${SWTPM_PID}" ]; then |
| 24 |
kill_quiet -9 "${SWTPM_PID}" |
| 25 |
fi |
| 26 |
rm -rf "$TPM_PATH" |
| 27 |
} |
| 28 |
|
| 29 |
trap "cleanup" EXIT |
| 30 |
|
| 31 |
source "${TESTDIR}/common" |
| 32 |
[ "${SWTPM_INTERFACE}" == "cuse" ] && source "${TESTDIR}/test_cuse" |
| 33 |
|
| 34 |
TPM_PATH=$TPM_PATH run_swtpm "${SWTPM_INTERFACE}" \ |
| 35 |
--migration release-lock-outgoing \ |
| 36 |
--tpm2 |
| 37 |
|
| 38 |
if ! kill_quiet -0 "${SWTPM_PID}"; then |
| 39 |
echo "Error: ${SWTPM_INTERFACE} TPM did not start." |
| 40 |
exit 1 |
| 41 |
fi |
| 42 |
check_swtpm_no_storage_lock "${SWTPM_PID}" |
| 43 |
|
| 44 |
# Init the TPM |
| 45 |
if ! run_swtpm_ioctl "${SWTPM_INTERFACE}" -i; then |
| 46 |
echo "Error: Could not initialize the ${SWTPM_INTERFACE} TPM." |
| 47 |
exit 1 |
| 48 |
fi |
| 49 |
check_swtpm_storage_locked "${SWTPM_PID}" |
| 50 |
swtpm_lock_storage "re-lock" |
| 51 |
|
| 52 |
if ! kill_quiet -0 "${SWTPM_PID}" 2>/dev/null; then |
| 53 |
echo "Error: ${SWTPM_INTERFACE} TPM not running anymore after INIT." |
| 54 |
exit 1 |
| 55 |
fi |
| 56 |
|
| 57 |
# Startup the TPM |
| 58 |
RES=$(swtpm_cmd_tx "${SWTPM_INTERFACE}" '\x80\x01\x00\x00\x00\x0c\x00\x00\x01\x44\x00\x00') |
| 59 |
exp=' 80 01 00 00 00 0a 00 00 00 00' |
| 60 |
if [ "$RES" != "$exp" ]; then |
| 61 |
echo "Error: Did not get expected result from TPM2_Startup(ST_Clear)" |
| 62 |
echo "expected: $exp" |
| 63 |
echo "received: $RES" |
| 64 |
exit 1 |
| 65 |
fi |
| 66 |
check_swtpm_storage_locked "${SWTPM_PID}" |
| 67 |
swtpm_lock_storage "re-lock" |
| 68 |
|
| 69 |
if ! run_swtpm_ioctl "${SWTPM_INTERFACE}" -h 1234; then |
| 70 |
echo "Error: Hash command did not work." |
| 71 |
exit 1 |
| 72 |
fi |
| 73 |
|
| 74 |
# Read PCR 17 |
| 75 |
# length CC count hashalg sz |
| 76 |
RES=$(swtpm_cmd_tx "${SWTPM_INTERFACE}" '\x80\x01\x00\x00\x00\x14\x00\x00\x01\x7e\x00\x00\x00\x01\x00\x0b\x03\x00\x00\x02') |
| 77 |
exp=' 80 01 00 00 00 3e 00 00 00 00 00 00 00 18 00 00 00 01 00 0b 03 00 00 02 00 00 00 01 00 20 fc a5 d6 49 bf b0 c9 22 fd 33 0f 79 b2 00 43 28 9d af d6 0d 01 a4 c4 37 3c f2 8a db 56 c9 b4 54' |
| 78 |
if [ "$RES" != "$exp" ]; then |
| 79 |
echo "Error: (1) Did not get expected result from TPM_PCRRead(17)" |
| 80 |
echo "expected: $exp" |
| 81 |
echo "received: $RES" |
| 82 |
exit 1 |
| 83 |
fi |
| 84 |
|
| 85 |
if ! run_swtpm_ioctl "${SWTPM_INTERFACE}" --save permanent "$MY_PERMANENT_STATE_FILE"; then |
| 86 |
echo "Error: Could not write permanent state file $MY_PERMANENT_STATE_FILE." |
| 87 |
exit 1 |
| 88 |
fi |
| 89 |
if [ ! -r "$MY_PERMANENT_STATE_FILE" ]; then |
| 90 |
echo "Error: Permanent state file $MY_PERMANENT_STATE_FILE does not exist." |
| 91 |
exit 1 |
| 92 |
fi |
| 93 |
echo "Saved permanent state." |
| 94 |
check_swtpm_storage_locked "${SWTPM_PID}" |
| 95 |
swtpm_lock_storage "re-lock" |
| 96 |
|
| 97 |
if ! run_swtpm_ioctl "${SWTPM_INTERFACE}" --save volatile "$MY_VOLATILE_STATE_FILE"; then |
| 98 |
echo "Error: Could not write volatile state file $MY_VOLATILE_STATE_FILE." |
| 99 |
exit 1 |
| 100 |
fi |
| 101 |
if [ ! -r "$MY_VOLATILE_STATE_FILE" ]; then |
| 102 |
echo "Error: Volatile state file $MY_VOLATILE_STATE_FILE does not exist." |
| 103 |
exit 1 |
| 104 |
fi |
| 105 |
echo "Saved volatile state." |
| 106 |
check_swtpm_storage_locked "${SWTPM_PID}" |
| 107 |
swtpm_lock_storage "re-lock" |
| 108 |
|
| 109 |
if ! run_swtpm_ioctl "${SWTPM_INTERFACE}" --save savestate "$MY_SAVESTATE_STATE_FILE"; then |
| 110 |
echo "Error: Could not write savestate state file $MY_SAVESTATE_STATE_FILE." |
| 111 |
exit 1 |
| 112 |
fi |
| 113 |
if [ ! -r "$MY_SAVESTATE_STATE_FILE" ]; then |
| 114 |
echo "Error: Volatile state file $MY_SAVESTATE_STATE_FILE does not exist." |
| 115 |
exit 1 |
| 116 |
fi |
| 117 |
echo "Saved savestate state." |
| 118 |
check_swtpm_no_storage_lock "${SWTPM_PID}" |
| 119 |
|
| 120 |
swtpm_lock_storage "lock" |
| 121 |
swtpm_lock_storage "re-lock" |
| 122 |
|
| 123 |
check_swtpm_storage_locked "${SWTPM_PID}" |
| 124 |
swtpm_lock_storage "re-lock" |
| 125 |
|
| 126 |
# Upon the next command swtpm must again lock storage; this simulates TPM usage after migration fall-back |
| 127 |
RES=$(swtpm_cmd_tx "${SWTPM_INTERFACE}" '\x80\x01\x00\x00\x00\x0a\x00\x00\x01\x7a') |
| 128 |
exp=' 80 01 00 00 00 0a 00 00 01 da' |
| 129 |
if [ "$RES" != "$exp" ]; then |
| 130 |
echo "Error: (1) Did not get expected result from TPM_PCRRead(17)" |
| 131 |
echo "expected: $exp" |
| 132 |
echo "received: $RES" |
| 133 |
exit 1 |
| 134 |
fi |
| 135 |
check_swtpm_storage_locked "${SWTPM_PID}" |
| 136 |
swtpm_lock_storage "re-lock" |
| 137 |
|
| 138 |
if ! run_swtpm_ioctl "${SWTPM_INTERFACE}" -s; then |
| 139 |
echo "Error: Could not shut down the ${SWTPM_INTERFACE} TPM." |
| 140 |
exit 1 |
| 141 |
fi |
| 142 |
|
| 143 |
if wait_process_gone "${SWTPM_PID}" 4; then |
| 144 |
echo "Error: ${SWTPM_INTERFACE} TPM should not be running anymore." |
| 145 |
exit 1 |
| 146 |
fi |
| 147 |
|
| 148 |
# Restart swtpm |
| 149 |
rm -f "$VOLATILE_STATE_FILE" "$STATE_FILE" |
| 150 |
TPM_PATH=$TPM_PATH run_swtpm "${SWTPM_INTERFACE}" \ |
| 151 |
--migration incoming,release-lock-outgoing \ |
| 152 |
--tpm2 |
| 153 |
|
| 154 |
if ! kill_quiet -0 "${SWTPM_PID}"; then |
| 155 |
echo "Error: ${SWTPM_INTERFACE} TPM did not start." |
| 156 |
exit 1 |
| 157 |
fi |
| 158 |
check_swtpm_no_storage_lock "${SWTPM_PID}" |
| 159 |
|
| 160 |
# load state into the TPM |
| 161 |
if ! run_swtpm_ioctl "${SWTPM_INTERFACE}" --load permanent "$MY_PERMANENT_STATE_FILE"; then |
| 162 |
echo "Could not load permanent state into vTPM" |
| 163 |
exit 1 |
| 164 |
fi |
| 165 |
echo "Loaded permanent state." |
| 166 |
check_swtpm_storage_locked "${SWTPM_PID}" |
| 167 |
swtpm_lock_storage "re-lock" |
| 168 |
|
| 169 |
if ! run_swtpm_ioctl "${SWTPM_INTERFACE}" --load volatile "$MY_VOLATILE_STATE_FILE"; then |
| 170 |
echo "Could not load volatile state into vTPM" |
| 171 |
exit 1 |
| 172 |
fi |
| 173 |
echo "Loaded volatile state." |
| 174 |
check_swtpm_storage_locked "${SWTPM_PID}" |
| 175 |
|
| 176 |
# Init the TPM |
| 177 |
if ! run_swtpm_ioctl "${SWTPM_INTERFACE}" -i; then |
| 178 |
echo "TPM Init failed." |
| 179 |
exit 1 |
| 180 |
fi |
| 181 |
check_swtpm_storage_locked "${SWTPM_PID}" |
| 182 |
swtpm_lock_storage "re-lock" |
| 183 |
|
| 184 |
# Volatile state must have been removed by TPM now |
| 185 |
if [ -r "$VOLATILE_STATE_FILE" ]; then |
| 186 |
echo "Error: Volatile state file $VOLATILE_STATE_FILE still exists." |
| 187 |
exit 1 |
| 188 |
fi |
| 189 |
|
| 190 |
# Read the PCR again ... |
| 191 |
RES=$(swtpm_cmd_tx "${SWTPM_INTERFACE}" '\x80\x01\x00\x00\x00\x14\x00\x00\x01\x7e\x00\x00\x00\x01\x00\x0b\x03\x00\x00\x02') |
| 192 |
exp=' 80 01 00 00 00 3e 00 00 00 00 00 00 00 18 00 00 00 01 00 0b 03 00 00 02 00 00 00 01 00 20 fc a5 d6 49 bf b0 c9 22 fd 33 0f 79 b2 00 43 28 9d af d6 0d 01 a4 c4 37 3c f2 8a db 56 c9 b4 54' |
| 193 |
if [ "$RES" != "$exp" ]; then |
| 194 |
echo "Error: (2) Did not get expected result from TPM_PCRRead(17)" |
| 195 |
echo "expected: $exp" |
| 196 |
echo "received: $RES" |
| 197 |
exit 1 |
| 198 |
fi |
| 199 |
|
| 200 |
# Save the volatile state again |
| 201 |
if ! run_swtpm_ioctl "${SWTPM_INTERFACE}" -v; then |
| 202 |
echo "Error: Could not have the ${SWTPM_INTERFACE} TPM store the volatile state to a file." |
| 203 |
exit 1 |
| 204 |
fi |
| 205 |
|
| 206 |
if [ ! -r "$VOLATILE_STATE_FILE" ]; then |
| 207 |
echo "Error: Volatile state file $VOLATILE_STATE_FILE does not exist." |
| 208 |
exit 1 |
| 209 |
fi |
| 210 |
|
| 211 |
# Send a new TPM_Init |
| 212 |
if ! run_swtpm_ioctl "${SWTPM_INTERFACE}" -i; then |
| 213 |
echo "Error: Could not initialize the ${SWTPM_INTERFACE} TPM." |
| 214 |
exit 1 |
| 215 |
fi |
| 216 |
|
| 217 |
# Volatile state must have been removed by TPM now |
| 218 |
if [ -r "$VOLATILE_STATE_FILE" ]; then |
| 219 |
echo "Error: Volatile state file $VOLATILE_STATE_FILE still exists." |
| 220 |
exit 1 |
| 221 |
fi |
| 222 |
|
| 223 |
# Read the PCR again ... |
| 224 |
RES=$(swtpm_cmd_tx "${SWTPM_INTERFACE}" '\x80\x01\x00\x00\x00\x14\x00\x00\x01\x7e\x00\x00\x00\x01\x00\x0b\x03\x00\x00\x02') |
| 225 |
exp=' 80 01 00 00 00 3e 00 00 00 00 00 00 00 18 00 00 00 01 00 0b 03 00 00 02 00 00 00 01 00 20 fc a5 d6 49 bf b0 c9 22 fd 33 0f 79 b2 00 43 28 9d af d6 0d 01 a4 c4 37 3c f2 8a db 56 c9 b4 54' |
| 226 |
if [ "$RES" != "$exp" ]; then |
| 227 |
echo "Error: (3) Did not get expected result from TPM_PCRRead(17)" |
| 228 |
echo "expected: $exp" |
| 229 |
echo "received: $RES" |
| 230 |
exit 1 |
| 231 |
fi |
| 232 |
|
| 233 |
# Final shut down |
| 234 |
if ! run_swtpm_ioctl "${SWTPM_INTERFACE}" -s; then |
| 235 |
echo "Error: Could not shut down the ${SWTPM_INTERFACE} TPM." |
| 236 |
exit 1 |
| 237 |
fi |
| 238 |
|
| 239 |
if wait_process_gone "${SWTPM_PID}" 4; then |
| 240 |
echo "Error: ${SWTPM_INTERFACE} TPM should not be running anymore." |
| 241 |
exit 1 |
| 242 |
fi |
| 243 |
|
| 244 |
if [ ! -e "$STATE_FILE" ]; then |
| 245 |
echo "Error: TPM state file $STATE_FILE does not exist." |
| 246 |
exit 1 |
| 247 |
fi |
| 248 |
|
| 249 |
echo "OK" |
| 250 |
|
| 251 |
exit 0 |