| 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-test-hashing" |
| 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 | SWTPM_CMD_UNIX_PATH=${TPM_PATH}/unix-cmd.sock |
| 15 | SWTPM_CTRL_UNIX_PATH=${TPM_PATH}/unix-ctrl.sock |
| 16 | SWTPM_INTERFACE=${SWTPM_INTERFACE:-cuse} |
| 17 | |
| 18 | function cleanup() |
| 19 | { |
| 20 | pid=${SWTPM_PID} |
| 21 | if [ -n "$pid" ]; then |
| 22 | kill_quiet -9 "$pid" |
| 23 | fi |
| 24 | rm -rf "$TPM_PATH" |
| 25 | } |
| 26 | |
| 27 | trap "cleanup" EXIT |
| 28 | |
| 29 | source "${TESTDIR}/common" |
| 30 | [ "${SWTPM_INTERFACE}" == cuse ] && source "${TESTDIR}/test_cuse" |
| 31 | |
| 32 | rm -f "$STATE_FILE" "$VOLATILE_STATE_FILE" 2>/dev/null |
| 33 | |
| 34 | TPM_PATH=$TPM_PATH run_swtpm "${SWTPM_INTERFACE}" |
| 35 | |
| 36 | display_processes_by_name "swtpm" |
| 37 | |
| 38 | if ! kill_quiet -0 "${SWTPM_PID}"; then |
| 39 | echo "Error: ${SWTPM_INTERFACE} TPM did not start." |
| 40 | exit 1 |
| 41 | fi |
| 42 | |
| 43 | # Init the TPM |
| 44 | if ! run_swtpm_ioctl "${SWTPM_INTERFACE}" -i; then |
| 45 | echo "Error: Could not initialize the ${SWTPM_INTERFACE} TPM." |
| 46 | exit 1 |
| 47 | fi |
| 48 | |
| 49 | if ! kill_quiet -0 "${SWTPM_PID}" 2>/dev/null; then |
| 50 | echo "Error: ${SWTPM_INTERFACE} TPM not running anymore after INIT." |
| 51 | exit 1 |
| 52 | fi |
| 53 | |
| 54 | # Startup the TPM |
| 55 | RES=$(swtpm_cmd_tx "${SWTPM_INTERFACE}" '\x00\xC1\x00\x00\x00\x0C\x00\x00\x00\x99\x00\x01') |
| 56 | exp=' 00 c4 00 00 00 0a 00 00 00 00' |
| 57 | if [ "$RES" != "$exp" ]; then |
| 58 | echo "Error: Did not get expected result from TPM_Startup(ST_Clear)" |
| 59 | echo "expected: $exp" |
| 60 | echo "received: $RES" |
| 61 | exit 1 |
| 62 | fi |
| 63 | |
| 64 | # Check the TPM Established bit before the hashing |
| 65 | if ! RES=$(run_swtpm_ioctl "${SWTPM_INTERFACE}" -e); then |
| 66 | echo "Error: Could not get the TPM Established flag." |
| 67 | exit 1 |
| 68 | fi |
| 69 | exp='tpmEstablished is 0' |
| 70 | if [ "$RES" != "$exp" ]; then |
| 71 | echo "Error (1): TPM Established flag has wrong value." |
| 72 | echo "expected: $exp" |
| 73 | echo "received: $RES" |
| 74 | exit 1 |
| 75 | fi |
| 76 | |
| 77 | if ! run_swtpm_ioctl "${SWTPM_INTERFACE}" -h 1234; then |
| 78 | echo "Error: Hash command did not work." |
| 79 | exit 1 |
| 80 | fi |
| 81 | |
| 82 | # Read PCR 17 |
| 83 | RES=$(swtpm_cmd_tx "${SWTPM_INTERFACE}" '\x00\xC1\x00\x00\x00\x0E\x00\x00\x00\x15\x00\x00\x00\x11') |
| 84 | 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' |
| 85 | if [ "$RES" != "$exp" ]; then |
| 86 | echo "Error: (1) Did not get expected result from TPM_PCRRead(17)" |
| 87 | echo "expected: $exp" |
| 88 | echo "received: $RES" |
| 89 | exit 1 |
| 90 | fi |
| 91 | |
| 92 | # Check the TPM Established bit after the hashing |
| 93 | if ! RES=$(run_swtpm_ioctl "${SWTPM_INTERFACE}" -e); then |
| 94 | echo "Error: Could not get the TPM Established flag." |
| 95 | exit 1 |
| 96 | fi |
| 97 | exp='tpmEstablished is 1' |
| 98 | if [ "$RES" != "$exp" ]; then |
| 99 | echo "Error (2): TPM Established flag has wrong value." |
| 100 | echo "expected: $exp" |
| 101 | echo "received: $RES" |
| 102 | exit 1 |
| 103 | fi |
| 104 | |
| 105 | # Reset the establishment bit via locality 3 |
| 106 | if ! run_swtpm_ioctl "${SWTPM_INTERFACE}" -l 3; then |
| 107 | echo "Error: Could not set locality 3" |
| 108 | exit 1 |
| 109 | fi |
| 110 | |
| 111 | # \x40 or \x0B seems to confuse 'normal' echo |
| 112 | RES=$(swtpm_cmd_tx "${SWTPM_INTERFACE}" '\x00\xC1\x00\x00\x00\x0A\x40\x00\x00\x0B') |
| 113 | exp=' 00 c4 00 00 00 0a 00 00 00 00' |
| 114 | if [ "$RES" != "$exp" ]; then |
| 115 | echo "Error: Could not reset the establishment bit" |
| 116 | echo "expected: $exp" |
| 117 | echo "received: $RES" |
| 118 | exit 1 |
| 119 | fi |
| 120 | |
| 121 | # Check the TPM Established bit after the reset |
| 122 | if ! RES=$(run_swtpm_ioctl "${SWTPM_INTERFACE}" -e); then |
| 123 | echo "Error: Could not get the TPM Established flag." |
| 124 | exit 1 |
| 125 | fi |
| 126 | exp='tpmEstablished is 0' |
| 127 | if [ "$RES" != "$exp" ]; then |
| 128 | echo "Error (3): TPM Established flag has wrong value." |
| 129 | echo "expected: $exp" |
| 130 | echo "received: $RES" |
| 131 | exit 1 |
| 132 | fi |
| 133 | |
| 134 | # back to locality 0 |
| 135 | if ! run_swtpm_ioctl "${SWTPM_INTERFACE}" -l 0; then |
| 136 | echo "Error: Could not set locality 0." |
| 137 | exit 1 |
| 138 | fi |
| 139 | |
| 140 | # Read from a file |
| 141 | dd if=/dev/zero bs=1024 count=1024 2>/dev/null| \ |
| 142 | run_swtpm_ioctl "${SWTPM_INTERFACE}" -h - |
| 143 | |
| 144 | # Read PCR 17 |
| 145 | RES=$(swtpm_cmd_tx "${SWTPM_INTERFACE}" '\x00\xC1\x00\x00\x00\x0E\x00\x00\x00\x15\x00\x00\x00\x11') |
| 146 | exp=' 00 c4 00 00 00 1e 00 00 00 00 d8 0e 7a 7b 3c 37 88 7d b4 c2 88 08 1d a7 53 f6 4b 11 3a 9c' |
| 147 | if [ "$RES" != "$exp" ]; then |
| 148 | echo "Error: (2) Did not get expected result from TPM_PCRRead(17)" |
| 149 | echo "expected: $exp" |
| 150 | echo "received: $RES" |
| 151 | exit 1 |
| 152 | fi |
| 153 | |
| 154 | if ! run_swtpm_ioctl "${SWTPM_INTERFACE}" -s; then |
| 155 | echo "Error: Could not shut down the ${SWTPM_INTERFACE} TPM." |
| 156 | exit 1 |
| 157 | fi |
| 158 | |
| 159 | if wait_process_gone "${SWTPM_PID}" 4; then |
| 160 | echo "Error: ${SWTPM_INTERFACE} TPM should not be running anymore." |
| 161 | exit 1 |
| 162 | fi |
| 163 | |
| 164 | if [ ! -e "$STATE_FILE" ]; then |
| 165 | echo "Error: TPM state file $STATE_FILE does not exist." |
| 166 | exit 1 |
| 167 | fi |
| 168 | |
| 169 | echo "OK" |
| 170 | |
| 171 | exit 0 |