| 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-tpm2-migration-key}" |
| 10 |
SWTPM_DEV_NAME=/dev/${VTPM_NAME} |
| 11 |
MIGRATION_PASSWORD="migration" |
| 12 |
VOLATILESTATE=${TESTDIR}/data/migkey1/tpm2-volatilestate.bin |
| 13 |
SWTPM_INTERFACE=${SWTPM_INTERFACE:-cuse} |
| 14 |
|
| 15 |
function cleanup() |
| 16 |
{ |
| 17 |
pid=$SWTPM_PID |
| 18 |
if [ -n "$pid" ]; then |
| 19 |
kill_quiet -9 "$pid" |
| 20 |
fi |
| 21 |
rm -rf "$migpwdfile" "$volatilestatefile" "$tpmstatedir" "$logfile" |
| 22 |
} |
| 23 |
|
| 24 |
trap "cleanup" EXIT |
| 25 |
|
| 26 |
tpmstatedir="$(mktemp -d)" |
| 27 |
if [ -z "$tpmstatedir" ]; then |
| 28 |
echo "Could not create temporary directory." |
| 29 |
exit 1 |
| 30 |
fi |
| 31 |
|
| 32 |
migpwdfile="$(mktemp)" |
| 33 |
if [ -z "$migpwdfile" ]; then |
| 34 |
echo "Could not create temporary file." |
| 35 |
exit 1 |
| 36 |
fi |
| 37 |
echo -n "$MIGRATION_PASSWORD" > "$migpwdfile" |
| 38 |
|
| 39 |
volatilestatefile="$(mktemp)" |
| 40 |
if [ -z "$volatilestatefile" ]; then |
| 41 |
echo "Could not create temporary file." |
| 42 |
exit 1 |
| 43 |
fi |
| 44 |
logfile="$(mktemp)" || exit 1 |
| 45 |
|
| 46 |
SWTPM_CMD_UNIX_PATH=${tpmstatedir}/unix-cmd.sock |
| 47 |
SWTPM_CTRL_UNIX_PATH=${tpmstatedir}/unix-ctrl.sock |
| 48 |
|
| 49 |
source "${TESTDIR}/common" |
| 50 |
[ "${SWTPM_INTERFACE}" == "cuse" ] && source "${TESTDIR}/test_cuse" |
| 51 |
|
| 52 |
# make a backup of the volatile state |
| 53 |
TPM_PATH=$tpmstatedir |
| 54 |
cp "${TESTDIR}"/data/tpm2state1/* "$TPM_PATH" |
| 55 |
|
| 56 |
rm -f "${logfile}" |
| 57 |
TPM_PATH=$TPM_PATH run_swtpm "${SWTPM_INTERFACE}" \ |
| 58 |
--log "file=${logfile}" \ |
| 59 |
--tpm2 \ |
| 60 |
--migration-key "pwdfile=$migpwdfile,remove=false,kdf=sha512" |
| 61 |
|
| 62 |
if ! kill_quiet -0 "${SWTPM_PID}"; then |
| 63 |
echo "Error: ${SWTPM_INTERFACE} TPM did not start." |
| 64 |
exit 1 |
| 65 |
fi |
| 66 |
|
| 67 |
# Init the TPM |
| 68 |
if ! run_swtpm_ioctl "${SWTPM_INTERFACE}" -i; then |
| 69 |
echo "Error: Initializing the ${SWTPM_INTERFACE} TPM failed." |
| 70 |
exit 1 |
| 71 |
fi |
| 72 |
|
| 73 |
if ! kill_quiet -0 "${SWTPM_PID}" 2>/dev/null; then |
| 74 |
echo "Error: ${SWTPM_INTERFACE} TPM not running anymore after INIT." |
| 75 |
exit 1 |
| 76 |
fi |
| 77 |
|
| 78 |
# Read PCR 10 (from pcrextend -ha 10 -ic test) |
| 79 |
RES=$(swtpm_cmd_tx "${SWTPM_INTERFACE}" '\x80\x01\x00\x00\x00\x14\x00\x00\x01\x7e\x00\x00\x00\x01\x00\x0b\x03\x00\x04\x00' 100) |
| 80 |
exp=' 80 01 00 00 00 3e 00 00 00 00 00 00 00 16 00 00 00 01 00 0b 03 00 04 00 00 00 00 01 00 20 f6 85 98 e5 86 8d e6 8b 97 29 99 60 f2 71 7d 17 67 89 a4 2f 9a ae a8 c7 b7 aa 79 a8 62 56 c1 de' |
| 81 |
if [ "$RES" != "$exp" ]; then |
| 82 |
echo "Error: (1) Did not get expected result from TPM_PCRRead(10)" |
| 83 |
echo "expected: $exp" |
| 84 |
echo "received: $RES" |
| 85 |
exit 1 |
| 86 |
fi |
| 87 |
|
| 88 |
# Save the volatile state into a file |
| 89 |
if ! run_swtpm_ioctl "${SWTPM_INTERFACE}" --save volatile "$volatilestatefile"; then |
| 90 |
echo "Error: Could not save the volatile state to ${volatilestatefile}." |
| 91 |
exit 1 |
| 92 |
fi |
| 93 |
if [ ! -r "$volatilestatefile" ]; then |
| 94 |
echo "Error: Volatile state file $volatilestatefile does not exist." |
| 95 |
exit 1 |
| 96 |
fi |
| 97 |
|
| 98 |
size=$(get_filesize "$volatilestatefile") |
| 99 |
expsize="^([5689][0-9]{3})$" |
| 100 |
if ! [[ $size =~ $expsize ]]; then |
| 101 |
echo "Error: Unexpected size of volatile state file." |
| 102 |
echo " Expected file with size of $expsize, found $size bytes." |
| 103 |
exit 1 |
| 104 |
fi |
| 105 |
|
| 106 |
if ! tmp=$(run_swtpm_ioctl "${SWTPM_INTERFACE}" -g | cut -d":" -f2); then |
| 107 |
echo "Error: Could not get the configuration flags of the ${SWTPM_INTERFACE} TPM." |
| 108 |
exit 1 |
| 109 |
fi |
| 110 |
|
| 111 |
if [ "$tmp" != " 0x2" ]; then |
| 112 |
echo "Error: Unexpected configuration flags: $tmp; expected 0x2." |
| 113 |
exit 1 |
| 114 |
fi |
| 115 |
|
| 116 |
# Shut the TPM down |
| 117 |
exec 100>&- |
| 118 |
run_swtpm_ioctl "${SWTPM_INTERFACE}" -s |
| 119 |
|
| 120 |
echo "Test 1: Ok" |
| 121 |
|
| 122 |
# Start the vTPM again and load the encrypted volatile state into it |
| 123 |
rm -f "${logfile}" |
| 124 |
TPM_PATH=$TPM_PATH run_swtpm "${SWTPM_INTERFACE}" \ |
| 125 |
--log "file=${logfile}" \ |
| 126 |
--tpm2 \ |
| 127 |
--migration-key "pwdfile=$migpwdfile,remove=false,kdf=sha512" |
| 128 |
|
| 129 |
display_processes_by_name "swtpm" |
| 130 |
|
| 131 |
if ! kill_quiet -0 "${SWTPM_PID}"; then |
| 132 |
echo "Error: ${SWTPM_INTERFACE} TPM did not start." |
| 133 |
exit 1 |
| 134 |
fi |
| 135 |
|
| 136 |
run_swtpm_ioctl "${SWTPM_INTERFACE}" --stop |
| 137 |
|
| 138 |
# Do NOT init the TPM now; first load volatile state |
| 139 |
|
| 140 |
# load the encrypted volatile state into it |
| 141 |
if ! run_swtpm_ioctl "${SWTPM_INTERFACE}" --load volatile "$volatilestatefile"; then |
| 142 |
echo "Error: Could not load encrypted volatile state into TPM." |
| 143 |
echo "swtpm logfile: " |
| 144 |
cat "$logfile" |
| 145 |
exit 1 |
| 146 |
fi |
| 147 |
|
| 148 |
# Now init the TPM |
| 149 |
if ! run_swtpm_ioctl "${SWTPM_INTERFACE}" -i; then |
| 150 |
echo "Error: Initializing the ${SWTPM_INTERFACE} TPM failed." |
| 151 |
exit 1 |
| 152 |
fi |
| 153 |
|
| 154 |
# Read PCR 10 |
| 155 |
RES=$(swtpm_cmd_tx "${SWTPM_INTERFACE}" '\x80\x01\x00\x00\x00\x14\x00\x00\x01\x7e\x00\x00\x00\x01\x00\x0b\x03\x00\x04\x00' 100) |
| 156 |
exp=' 80 01 00 00 00 3e 00 00 00 00 00 00 00 16 00 00 00 01 00 0b 03 00 04 00 00 00 00 01 00 20 f6 85 98 e5 86 8d e6 8b 97 29 99 60 f2 71 7d 17 67 89 a4 2f 9a ae a8 c7 b7 aa 79 a8 62 56 c1 de' |
| 157 |
if [ "$RES" != "$exp" ]; then |
| 158 |
echo "Error: (1) Did not get expected result from TPM_PCRRead(10)" |
| 159 |
echo "expected: $exp" |
| 160 |
echo "received: $RES" |
| 161 |
exit 1 |
| 162 |
fi |
| 163 |
|
| 164 |
# Shut the TPM down |
| 165 |
exec 100>&- |
| 166 |
if ! run_swtpm_ioctl "${SWTPM_INTERFACE}" -s; then |
| 167 |
echo "Error: Could not shut down the ${SWTPM_INTERFACE} TPM." |
| 168 |
exit 1 |
| 169 |
fi |
| 170 |
|
| 171 |
echo "Test 2: Ok" |
| 172 |
|
| 173 |
# Start the vTPM again and load the encrypted volatile state into it |
| 174 |
# This time we make this fail since we don't provide the migration key |
| 175 |
rm -f "${logfile}" |
| 176 |
TPM_PATH=$TPM_PATH run_swtpm "${SWTPM_INTERFACE}" \ |
| 177 |
--log "file=${logfile}" \ |
| 178 |
--tpm2 |
| 179 |
|
| 180 |
display_processes_by_name "swtpm" |
| 181 |
|
| 182 |
if ! kill_quiet -0 "${SWTPM_PID}"; then |
| 183 |
echo "Error: ${SWTPM_INTERFACE} TPM did not start." |
| 184 |
exit 1 |
| 185 |
fi |
| 186 |
|
| 187 |
# Do NOT init the TPM now; first load volatile state |
| 188 |
|
| 189 |
run_swtpm_ioctl "${SWTPM_INTERFACE}" --stop |
| 190 |
|
| 191 |
# load the encrypted volatile state into it |
| 192 |
# This will not work; the TPM writes the data into the volatile state file |
| 193 |
# and validates it |
| 194 |
if ERR="$(run_swtpm_ioctl "${SWTPM_INTERFACE}" --load volatile "$volatilestatefile" 2>&1)"; then |
| 195 |
echo "Error: Could load encrypted volatile state into TPM." |
| 196 |
cat "$logfile" |
| 197 |
exit 1 |
| 198 |
fi |
| 199 |
exp="TPM result from PTM_SET_STATEBLOB: 0xd" |
| 200 |
if [ "$ERR" != "$exp" ]; then |
| 201 |
echo "Error: Unexpected error message" |
| 202 |
echo "Received: $ERR" |
| 203 |
echo "Expected: $exp" |
| 204 |
exit 1 |
| 205 |
fi |
| 206 |
|
| 207 |
if ! run_swtpm_ioctl "${SWTPM_INTERFACE}" -s; then |
| 208 |
echo "Error: Could not shut down the ${SWTPM_INTERFACE} TPM." |
| 209 |
exit 1 |
| 210 |
fi |
| 211 |
|
| 212 |
rm -f "${TPM_PATH}/tpm2-00.volatilestate" |
| 213 |
|
| 214 |
echo "Test 3: Ok" |
| 215 |
|
| 216 |
# In this test we now feed it an encrypted volatile state |
| 217 |
|
| 218 |
# Start the vTPM again and load the encrypted volatile state into it |
| 219 |
rm -f "${logfile}" |
| 220 |
TPM_PATH=$TPM_PATH run_swtpm "${SWTPM_INTERFACE}" \ |
| 221 |
--log "file=${logfile}" \ |
| 222 |
--tpm2 \ |
| 223 |
--migration-key "pwdfile=$migpwdfile,remove=true,kdf=sha512" |
| 224 |
|
| 225 |
display_processes_by_name "swtpm" |
| 226 |
|
| 227 |
if ! kill_quiet -0 "${SWTPM_PID}"; then |
| 228 |
echo "Error: TPM did not start." |
| 229 |
cat "$logfile" |
| 230 |
exit 1 |
| 231 |
fi |
| 232 |
|
| 233 |
run_swtpm_ioctl "${SWTPM_INTERFACE}" --stop |
| 234 |
|
| 235 |
# load the encrypted volatile state into it |
| 236 |
if ! run_swtpm_ioctl "${SWTPM_INTERFACE}" --load volatile "$VOLATILESTATE"; then |
| 237 |
echo "Error: Could not load encrypted volatile state into TPM." |
| 238 |
exit 1 |
| 239 |
fi |
| 240 |
|
| 241 |
# Now init the TPM; this must work |
| 242 |
if ! run_swtpm_ioctl "${SWTPM_INTERFACE}" -i; then |
| 243 |
echo "Error: Could not initialize the ${SWTPM_INTERFACE} TPM." |
| 244 |
exit 1 |
| 245 |
fi |
| 246 |
|
| 247 |
# Read PCR 10 |
| 248 |
RES=$(swtpm_cmd_tx "${SWTPM_INTERFACE}" '\x80\x01\x00\x00\x00\x14\x00\x00\x01\x7e\x00\x00\x00\x01\x00\x0b\x03\x00\x04\x00' 100) |
| 249 |
exp=' 80 01 00 00 00 3e 00 00 00 00 00 00 00 16 00 00 00 01 00 0b 03 00 04 00 00 00 00 01 00 20 f6 85 98 e5 86 8d e6 8b 97 29 99 60 f2 71 7d 17 67 89 a4 2f 9a ae a8 c7 b7 aa 79 a8 62 56 c1 de' |
| 250 |
if [ "$RES" != "$exp" ]; then |
| 251 |
echo "Error: (1) Did not get expected result from TPM_PCRRead(10)" |
| 252 |
echo "expected: $exp" |
| 253 |
echo "received: $RES" |
| 254 |
exit 1 |
| 255 |
fi |
| 256 |
|
| 257 |
# Shut the TPM down |
| 258 |
exec 100>&- |
| 259 |
if ! run_swtpm_ioctl "${SWTPM_INTERFACE}" -s; then |
| 260 |
echo "Error: Could not shut down the ${SWTPM_INTERFACE} TPM." |
| 261 |
exit 1 |
| 262 |
fi |
| 263 |
|
| 264 |
echo "Test 4: Ok" |