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