]>
Commit | Line | Data |
---|---|---|
6852f6c8 SB |
1 | #!/bin/bash |
2 | ||
3 | # For the license, see the LICENSE file in the root directory. | |
4 | ||
5 | DIR=$(dirname "$0") | |
6 | ROOT=${DIR}/.. | |
7 | SWTPM=swtpm | |
8 | SWTPM_EXE=$ROOT/src/swtpm/$SWTPM | |
9 | TPMDIR=`mktemp -d` | |
10 | PID_FILE=$TPMDIR/${SWTPM}.pid | |
11 | SOCK_PATH=$TPMDIR/sock | |
12 | CMD_PATH=$TPMDIR/cmd | |
13 | RESP_PATH=$TPMDIR/resp | |
14 | ||
15 | trap "cleanup" SIGTERM EXIT | |
16 | ||
17 | function cleanup() | |
18 | { | |
19 | rm -rf $TPMDIR | |
20 | if [ -n "$PID" ]; then | |
21 | kill -SIGTERM $PID 2>/dev/null | |
22 | fi | |
23 | } | |
24 | ||
9ddc6998 | 25 | # Test 1: test the control channel on the chardev tpm |
6852f6c8 SB |
26 | |
27 | # use a pseudo terminal | |
28 | exec 100<>/dev/ptmx | |
804e7472 | 29 | $SWTPM_EXE chardev --fd 100 --tpmstate dir=$TPMDIR --pid file=$PID_FILE --ctrl type=unixio,path=$SOCK_PATH & |
6852f6c8 SB |
30 | sleep 0.5 |
31 | ||
32 | if [ ! -r $PID_FILE ]; then | |
33 | echo "Error: Chardev TPM did not write pidfile." | |
34 | exit 1 | |
35 | fi | |
36 | ||
37 | PID="$(cat $PID_FILE)" | |
38 | ||
39 | ||
40 | # Get the capability bits: CMD_GET_CAPABILITY = 0x00 00 00 01 | |
41 | echo -en '\x00\x00\x00\x01' > $CMD_PATH | |
e11a7552 | 42 | socat -x -t10 FILE:$CMD_PATH,rdonly UNIX-CLIENT:$SOCK_PATH 2>&1 | \ |
6852f6c8 SB |
43 | sed -n '/^ /p' | \ |
44 | tail -n1 > $RESP_PATH | |
45 | res="$(cat $RESP_PATH)" | |
804e7472 | 46 | exp=" 00 00 00 00 00 00 00 03" |
6852f6c8 SB |
47 | if [ "$res" != "$exp" ]; then |
48 | echo "Error: Unexpected response from CMD_GET_CAPABILITY:" | |
49 | echo " actual : $res" | |
50 | echo " expected: $exp" | |
51 | exit 1 | |
52 | fi | |
53 | ||
804e7472 SB |
54 | # Send TPM_Init to the TPM: CMD_INIT = 0x00 00 00 02 + flags |
55 | echo -en '\x00\x00\x00\x02\x00\x00\x00\x00' > $CMD_PATH | |
e11a7552 | 56 | socat -x -t10 FILE:$CMD_PATH,rdonly UNIX-CONNECT:$SOCK_PATH 2>&1 | \ |
804e7472 SB |
57 | sed -n '/^ /p' | \ |
58 | tail -n1 > $RESP_PATH | |
59 | res="$(cat $RESP_PATH)" | |
60 | exp=" 00 00 00 00" | |
61 | if [ "$res" != "$exp" ]; then | |
62 | echo "Error: Unexpected response from CMD_INIT:" | |
63 | echo " actual : $res" | |
64 | echo " expected: $exp" | |
65 | exit 1 | |
66 | fi | |
67 | ||
68 | # Send unknown command to the TPM | |
69 | echo -en '\x00\x00\xff\xff' > $CMD_PATH | |
e11a7552 | 70 | socat -x -t10 FILE:$CMD_PATH,rdonly UNIX-CONNECT:$SOCK_PATH 2>&1 | \ |
804e7472 SB |
71 | sed -n '/^ /p' | \ |
72 | tail -n1 > $RESP_PATH | |
73 | res="$(cat $RESP_PATH)" | |
74 | exp=" 00 00 00 0a" | |
75 | if [ "$res" != "$exp" ]; then | |
76 | echo "Error: Unexpected response from sending unsupported command:" | |
77 | echo " actual : $res" | |
78 | echo " expected: $exp" | |
79 | exit 1 | |
80 | fi | |
81 | ||
82 | # Send shutdown command to the TPM: CMD_SHUTDOWN = 00 00 00 03 | |
83 | echo -en '\x00\x00\x00\x03' > $CMD_PATH | |
e11a7552 | 84 | socat -x -t10 FILE:$CMD_PATH,rdonly UNIX-CONNECT:$SOCK_PATH 2>&1 | \ |
804e7472 SB |
85 | sed -n '/^ /p' | \ |
86 | tail -n1 > $RESP_PATH | |
87 | res="$(cat $RESP_PATH)" | |
88 | exp=" 00 00 00 00" | |
89 | if [ "$res" != "$exp" ]; then | |
90 | echo "Error: Unexpected response from CMD_SHUTDOWN:" | |
91 | echo " actual : $res" | |
92 | echo " expected: $exp" | |
93 | exit 1 | |
94 | fi | |
95 | ||
96 | sleep 0.2 | |
97 | kill -0 $PID 2>/dev/null | |
98 | if [ $? -eq 0 ]; then | |
99 | echo "Error: TPM should not be running anymore." | |
100 | exit 1 | |
101 | fi | |
102 | ||
103 | if [ -f $PID_FILE ]; then | |
104 | echo "Error: TPM should have removed the PID file." | |
105 | exit 1 | |
106 | fi | |
107 | ||
6852f6c8 SB |
108 | echo "OK" |
109 | ||
9ddc6998 SB |
110 | |
111 | # Test 2: test the control channel on the socket tpm | |
112 | ||
113 | # use a pseudo terminal | |
4716d35a | 114 | $SWTPM_EXE socket --server port=65530 --tpmstate dir=$TPMDIR --pid file=$PID_FILE --ctrl type=unixio,path=$SOCK_PATH & |
9ddc6998 SB |
115 | sleep 0.5 |
116 | ||
117 | if [ ! -r $PID_FILE ]; then | |
118 | echo "Error: Socket TPM did not write pidfile." | |
119 | exit 1 | |
120 | fi | |
121 | ||
122 | PID="$(cat $PID_FILE)" | |
123 | ||
124 | ||
125 | # Get the capability bits: CMD_GET_CAPABILITY = 0x00 00 00 01 | |
126 | echo -en '\x00\x00\x00\x01' > $CMD_PATH | |
127 | socat -x -t10 FILE:$CMD_PATH,rdonly UNIX-CLIENT:$SOCK_PATH 2>&1 | \ | |
128 | sed -n '/^ /p' | \ | |
129 | tail -n1 > $RESP_PATH | |
130 | res="$(cat $RESP_PATH)" | |
131 | exp=" 00 00 00 00 00 00 00 03" | |
132 | if [ "$res" != "$exp" ]; then | |
133 | echo "Error: Socket TPM: Unexpected response from CMD_GET_CAPABILITY:" | |
134 | echo " actual : $res" | |
135 | echo " expected: $exp" | |
136 | exit 1 | |
137 | fi | |
138 | ||
139 | # Send TPM_Init to the TPM: CMD_INIT = 0x00 00 00 02 + flags | |
140 | echo -en '\x00\x00\x00\x02\x00\x00\x00\x00' > $CMD_PATH | |
141 | socat -x -t10 FILE:$CMD_PATH,rdonly UNIX-CONNECT:$SOCK_PATH 2>&1 | \ | |
142 | sed -n '/^ /p' | \ | |
143 | tail -n1 > $RESP_PATH | |
144 | res="$(cat $RESP_PATH)" | |
145 | exp=" 00 00 00 00" | |
146 | if [ "$res" != "$exp" ]; then | |
147 | echo "Error: Socket TPM: Unexpected response from CMD_INIT:" | |
148 | echo " actual : $res" | |
149 | echo " expected: $exp" | |
150 | exit 1 | |
151 | fi | |
152 | ||
153 | # Send unknown command to the TPM | |
154 | echo -en '\x00\x00\xff\xff' > $CMD_PATH | |
155 | socat -x -t10 FILE:$CMD_PATH,rdonly UNIX-CONNECT:$SOCK_PATH 2>&1 | \ | |
156 | sed -n '/^ /p' | \ | |
157 | tail -n1 > $RESP_PATH | |
158 | res="$(cat $RESP_PATH)" | |
159 | exp=" 00 00 00 0a" | |
160 | if [ "$res" != "$exp" ]; then | |
161 | echo "Error: Socket TPM: Unexpected response from sending unsupported command:" | |
162 | echo " actual : $res" | |
163 | echo " expected: $exp" | |
164 | exit 1 | |
165 | fi | |
166 | ||
167 | # Send shutdown command to the TPM: CMD_SHUTDOWN = 00 00 00 03 | |
168 | echo -en '\x00\x00\x00\x03' > $CMD_PATH | |
169 | socat -x -t10 FILE:$CMD_PATH,rdonly UNIX-CONNECT:$SOCK_PATH 2>&1 | \ | |
170 | sed -n '/^ /p' | \ | |
171 | tail -n1 > $RESP_PATH | |
172 | res="$(cat $RESP_PATH)" | |
173 | exp=" 00 00 00 00" | |
174 | if [ "$res" != "$exp" ]; then | |
175 | echo "Error: Socket TPM: Unexpected response from CMD_SHUTDOWN:" | |
176 | echo " actual : $res" | |
177 | echo " expected: $exp" | |
178 | exit 1 | |
179 | fi | |
180 | ||
181 | sleep 0.2 | |
182 | kill -0 $PID 2>/dev/null | |
183 | if [ $? -eq 0 ]; then | |
184 | echo "Error: Socket TPM should not be running anymore." | |
185 | exit 1 | |
186 | fi | |
187 | ||
188 | if [ -f $PID_FILE ]; then | |
189 | echo "Error: Socket TPM should have removed the PID file." | |
190 | exit 1 | |
191 | fi | |
192 | ||
193 | echo "OK" | |
194 | ||
6852f6c8 | 195 | exit 0 |