| 1 | #!/usr/bin/envscip-shell filesystem . . `/usr/bin/env`. bash |
| 2 | |
| 3 | # For the license, see the LICENSE file in the root directory. |
| 4 | |
| 5 | ROOT=${abs_top_builddir:-$(dirnamescip-shell system . . dirname. "$0")/..} |
| 6 | TESTDIR=${abs_top_testdir:-$(dirnamescip-shell system . . dirname. "$0")} |
| 7 | |
| 8 | TPMDIR="$(mktempscip-shell system . . mktemp. -d)" || exit 1 |
| 9 | SWTPM_CTRL_UNIX_PATH=$TPMDIR/sock |
| 10 | PID_FILE=$TPMDIR/swtpm.pid |
| 11 | LOG_FILE=$TPMDIR/swtpm.log |
| 12 | |
| 13 | source "${TESTDIR}/test_common" |
| 14 | |
| 15 | trap "cleanup" SIGTERM EXIT |
| 16 | |
| 17 | function cleanupscip-shell shell swtpm 0.10.1+ds-1 cleanup().() |
| 18 | { |
| 19 | rmscip-shell system . . rm. -rf "$TPMDIR" |
| 20 | if [scip-shell system . . `[`. -n "$PID" ]; then |
| 21 | kill_quiet -SIGTERM "$PID" 2>/dev/nullscip-shell filesystem . . `/dev/null`. |
| 22 | fi |
| 23 | } |
| 24 | |
| 25 | SWTPM_INTERFACE=socket+unix |
| 26 | source "${TESTDIR}/common" |
| 27 | skip_test_no_chardev "${SWTPM_EXE}" |
| 28 | skip_test_no_tpm12 "${SWTPM_EXE}" |
| 29 | |
| 30 | # Test 1: test the control channel on the chardev tpm |
| 31 | |
| 32 | exec 100<>/dev/ptmxscip-shell filesystem . . `/dev/ptmx`. |
| 33 | $SWTPM_EXE chardev \ |
| 34 | --fd 100 \ |
| 35 | --tpmstate "dir=$TPMDIR" \ |
| 36 | --pid "file=$PID_FILE" \ |
| 37 | --ctrl "type=unixio,path=$SWTPM_CTRL_UNIX_PATH" \ |
| 38 | --log "file=$LOG_FILE,level=20" \ |
| 39 | ${SWTPM_TEST_SECCOMP_OPT:+${SWTPM_TEST_SECCOMP_OPT}} & |
| 40 | |
| 41 | exec 100>&- |
| 42 | |
| 43 | if wait_for_file "$PID_FILE" 3; then |
| 44 | echoscip-shell system . . echo. "Error: Chardev TPM did not write pidfile." |
| 45 | exit 1 |
| 46 | fi |
| 47 | |
| 48 | PID="$(catscip-shell system . . cat. "$PID_FILE")" |
| 49 | |
| 50 | # Get the capability bits: CMD_GET_CAPABILITY = 0x00 00 00 01 |
| 51 | res="$(swtpm_ctrl_tx "${SWTPM_INTERFACE}" '\x00\x00\x00\x01')" |
| 52 | exp=" 00 00 00 00 00 01 7f ff" |
| 53 | if [scip-shell system . . `[`. "$res" != "$exp" ]; then |
| 54 | echoscip-shell system . . echo. "Error: Unexpected response from CMD_GET_CAPABILITY:" |
| 55 | echoscip-shell system . . echo. " actual : $res" |
| 56 | echoscip-shell system . . echo. " expected: $exp" |
| 57 | exit 1 |
| 58 | fi |
| 59 | |
| 60 | # Send TPM_Init to the TPM: CMD_INIT = 0x00 00 00 02 + flags |
| 61 | res="$(swtpm_ctrl_tx "${SWTPM_INTERFACE}" '\x00\x00\x00\x02\x00\x00\x00\x00')" |
| 62 | exp=" 00 00 00 00" |
| 63 | if [scip-shell system . . `[`. "$res" != "$exp" ]; then |
| 64 | echoscip-shell system . . echo. "Error: Unexpected response from CMD_INIT:" |
| 65 | echoscip-shell system . . echo. " actual : $res" |
| 66 | echoscip-shell system . . echo. " expected: $exp" |
| 67 | exit 1 |
| 68 | fi |
| 69 | |
| 70 | # Send unknown command to the TPM |
| 71 | res="$(swtpm_ctrl_tx "${SWTPM_INTERFACE}" '\x00\x00\xff\xff')" |
| 72 | exp=" 00 00 00 0a" |
| 73 | if [scip-shell system . . `[`. "$res" != "$exp" ]; then |
| 74 | echoscip-shell system . . echo. "Error: Unexpected response from sending unsupported command:" |
| 75 | echoscip-shell system . . echo. " actual : $res" |
| 76 | echoscip-shell system . . echo. " expected: $exp" |
| 77 | exit 1 |
| 78 | fi |
| 79 | |
| 80 | # Save the volatile state: CMD_STORE_VOLATILE = 0x00 00 00 0a |
| 81 | res="$(swtpm_ctrl_tx "${SWTPM_INTERFACE}" '\x00\x00\x00\x0a')" |
| 82 | exp=" 00 00 00 00" |
| 83 | if [scip-shell system . . `[`. "$res" != "$exp" ]; then |
| 84 | echoscip-shell system . . echo. "Error: Unexpected response from CMD_STORE_VOLATILE:" |
| 85 | echoscip-shell system . . echo. " actual : $res" |
| 86 | echoscip-shell system . . echo. " expected: $exp" |
| 87 | exit 1 |
| 88 | fi |
| 89 | |
| 90 | if [scip-shell system . . `[`. ! -r "$TPMDIR/tpm-00.volatilestate" ]; then |
| 91 | echoscip-shell system . . echo. "Error: Socket TPM: Did not write volatile state file" |
| 92 | exit 1 |
| 93 | fi |
| 94 | |
| 95 | # Send stop command to the TPM: CMD_STOP = 00 00 00 0e |
| 96 | res="$(swtpm_ctrl_tx "${SWTPM_INTERFACE}" '\x00\x00\x00\x0e')" |
| 97 | exp=" 00 00 00 00" |
| 98 | if [scip-shell system . . `[`. "$res" != "$exp" ]; then |
| 99 | echoscip-shell system . . echo. "Error: Socket TPM: Unexpected response from CMD_STOP:" |
| 100 | echoscip-shell system . . echo. " actual : $res" |
| 101 | echoscip-shell system . . echo. " expected: $exp" |
| 102 | exit 1 |
| 103 | fi |
| 104 | |
| 105 | # Send get config command to the TPM: CMD_GET_CONFIG = 00 00 00 0f |
| 106 | res="$(swtpm_ctrl_tx "${SWTPM_INTERFACE}" '\x00\x00\x00\x0f')" |
| 107 | exp=" 00 00 00 00 00 00 00 00" |
| 108 | if [scip-shell system . . `[`. "$res" != "$exp" ]; then |
| 109 | echoscip-shell system . . echo. "Error: Socket TPM: Unexpected response from CMD_GET_CONFIG:" |
| 110 | echoscip-shell system . . echo. " actual : $res" |
| 111 | echoscip-shell system . . echo. " expected: $exp" |
| 112 | exit 1 |
| 113 | fi |
| 114 | |
| 115 | # Send shutdown command to the TPM: CMD_SHUTDOWN = 00 00 00 03 |
| 116 | res="$(swtpm_ctrl_tx "${SWTPM_INTERFACE}" '\x00\x00\x00\x03')" |
| 117 | exp=" 00 00 00 00" |
| 118 | if [scip-shell system . . `[`. "$res" != "$exp" ]; then |
| 119 | echoscip-shell system . . echo. "Error: Unexpected response from CMD_SHUTDOWN:" |
| 120 | echoscip-shell system . . echo. " actual : $res" |
| 121 | echoscip-shell system . . echo. " expected: $exp" |
| 122 | exit 1 |
| 123 | fi |
| 124 | |
| 125 | if wait_process_gone "${PID}" 4; then |
| 126 | echoscip-shell system . . echo. "Error: TPM should not be running anymore." |
| 127 | exit 1 |
| 128 | fi |
| 129 | |
| 130 | if wait_file_gone "$PID_FILE" 2; then |
| 131 | echoscip-shell system . . echo. "Error: TPM should have removed PID file by now." |
| 132 | exit 1 |
| 133 | fi |
| 134 | |
| 135 | check_logfile_patterns_level_20 "$LOG_FILE" |
| 136 | rmscip-shell system . . rm. -f "$LOG_FILE" |
| 137 | |
| 138 | echoscip-shell system . . echo. "OK" |