| 1 |
#!/usr/bin/env bash |
| 2 |
|
| 3 |
# For the license, see the LICENSE file in the root directory. |
| 4 |
#set -x |
| 5 |
|
| 6 |
if [ "${SWTPM_TEST_IBMTSS2:-0}" -eq 0 ]; then |
| 7 |
echo "SWTPM_TEST_IBMTSS2 must be set to run this test." |
| 8 |
exit 77 |
| 9 |
fi |
| 10 |
|
| 11 |
if ! type -p nvdefinespace startup &>/dev/null; then |
| 12 |
PREFIX=tss |
| 13 |
if ! type -p ${PREFIX}nvdefinespace ${PREFIX}startup; then |
| 14 |
echo "Could not find TPM2 tools (e.g., (tss)startup, (tss)nvdefinespace) in PATH." |
| 15 |
exit 77 |
| 16 |
fi |
| 17 |
fi |
| 18 |
TOOLSPATH=$(dirname "$(type -P "${PREFIX}startup")") |
| 19 |
|
| 20 |
ROOT=${abs_top_builddir:-$(dirname "$0")/..} |
| 21 |
TESTDIR=${abs_top_testdir:-$(dirname "$0")} |
| 22 |
|
| 23 |
TPMDIR="$(mktemp -d)" || exit 1 |
| 24 |
PID_FILE=$TPMDIR/swtpm.pid |
| 25 |
SOCK_PATH=$TPMDIR/sock |
| 26 |
LOGFILE=$TPMDIR/logfile |
| 27 |
LOGFILE2=$TPMDIR/logfile2 |
| 28 |
TMPFILE=$TPMDIR/tmpfile |
| 29 |
BINFILE=$TPMDIR/binfile |
| 30 |
SIGFILE=$TPMDIR/sigfile |
| 31 |
STATEFILE=${STATEFILE:-$TPMDIR/state} |
| 32 |
|
| 33 |
STORE_PARAM="dir=$TPMDIR" |
| 34 |
if [ "${SWTPM_TEST_LINEAR_FILE:-0}" -ne 0 ]; then |
| 35 |
echo "Testing with linear file backend ($STATEFILE)" |
| 36 |
STORE_PARAM="backend-uri=file://$STATEFILE" |
| 37 |
fi |
| 38 |
|
| 39 |
source "${TESTDIR}/test_common" |
| 40 |
source "${TESTDIR}/common" |
| 41 |
skip_test_no_tpm20 "${SWTPM_EXE}" |
| 42 |
|
| 43 |
|
| 44 |
trap "cleanup" SIGTERM EXIT |
| 45 |
|
| 46 |
function cleanup() |
| 47 |
{ |
| 48 |
rm -rf "$TPMDIR" |
| 49 |
# remove files from tss tools |
| 50 |
rm -f h01*.bin nvp*.bin |
| 51 |
if [ -n "$PID" ]; then |
| 52 |
kill_quiet -SIGTERM "$PID" 2>/dev/null |
| 53 |
fi |
| 54 |
} |
| 55 |
|
| 56 |
# Fill up the NVRAM space with 2048 bit signing keys and then an NVRAM area that |
| 57 |
# fills it up to the last byte. We want to make sure that the OBJECTs that the |
| 58 |
# RSA keys are creating in NVRAM can be loaded into the NVRAM again when the size |
| 59 |
# of the OBJECT increases when for example the size of the RSA keys increases. |
| 60 |
# This may force us to increase the NVRAM memory space in libtpms then. |
| 61 |
function fillup_nvram() |
| 62 |
{ |
| 63 |
local create="$1" |
| 64 |
local check="$2" |
| 65 |
|
| 66 |
local i sz |
| 67 |
|
| 68 |
if [ "$create" -eq 1 ]; then |
| 69 |
# Fill up the NVRAM space with RSA 2048 keys; |
| 70 |
# exactly 65 have to fit |
| 71 |
if ! "${TOOLSPATH}/${PREFIX}createprimary" -hi o -si > "$TMPFILE"; then |
| 72 |
echo "Error: createprimary failed." |
| 73 |
exit 1 |
| 74 |
fi |
| 75 |
if ! grep -q 80000000 "$TMPFILE"; then |
| 76 |
echo "Error: createprimary did not result in expected handle 80000000" |
| 77 |
exit 1 |
| 78 |
fi |
| 79 |
for ((i = 0x81000000; i < 0x81000100; i++)); do |
| 80 |
"${TOOLSPATH}/${PREFIX}evictcontrol" \ |
| 81 |
-hi o \ |
| 82 |
-ho 80000000 \ |
| 83 |
-hp "$(printf "%x" "$i")" &>"$TMPFILE" || break |
| 84 |
done |
| 85 |
"${TOOLSPATH}/${PREFIX}getcapability" -cap 1 -pr 81000000 -pc 80 > "$TMPFILE" |
| 86 |
# We need know we need to see '65 Handles' for state created with |
| 87 |
# libtpms-0.6.0 and 128kb NVRAM size |
| 88 |
if ! grep -i "65 Handles" "$TMPFILE"; then |
| 89 |
echo "Error: Did not find '65 Handles' keyword in output" |
| 90 |
cat "$TMPFILE" |
| 91 |
exit 1 |
| 92 |
fi |
| 93 |
|
| 94 |
# Fill up the rest of the NVRAM with a single NVRAM index whose size |
| 95 |
# we now have to find; |
| 96 |
# for reference: libtpms v0.6.0 allowed 236 bytes |
| 97 |
for ((sz = 0; ; sz++)); do |
| 98 |
"${TOOLSPATH}/${PREFIX}nvdefinespace" \ |
| 99 |
-hi o \ |
| 100 |
-ha 01000000 \ |
| 101 |
-sz "${sz}" > "${TMPFILE}" || break |
| 102 |
# this worked, so lets remove it and try the next size |
| 103 |
#echo "NVRAM space of size $sz could be created" |
| 104 |
"${TOOLSPATH}/${PREFIX}nvundefinespace" \ |
| 105 |
-hi o \ |
| 106 |
-ha 01000000 > "${TMPFILE}" |
| 107 |
done |
| 108 |
if [ "$sz" -gt 0 ]; then |
| 109 |
sz=$((sz - 1)) |
| 110 |
echo "Creating final space of size ${sz}" |
| 111 |
if ! "${TOOLSPATH}/${PREFIX}nvdefinespace" \ |
| 112 |
-hi o \ |
| 113 |
-ha 01000000 \ |
| 114 |
-sz ${sz} > "${TMPFILE}"; |
| 115 |
then |
| 116 |
echo "Error: Could not create final NVRAM space." |
| 117 |
cat "${TMPFILE}" |
| 118 |
exit 1 |
| 119 |
fi |
| 120 |
fi |
| 121 |
if [ $sz -eq 0 ]; then |
| 122 |
echo "Error: NVRAM space could not be created at all; not enough space!" |
| 123 |
exit 1 |
| 124 |
elif [ $sz -lt 236 ]; then |
| 125 |
echo "Error: Insufficient NVRAM memory. Needed to create an NVRAM index with size 236 bytes." |
| 126 |
exit 1 |
| 127 |
elif [ $sz -gt 236 ]; then |
| 128 |
echo "Error: The NVRAM index is too large. Only needed 236 bytes but got $sz bytes." |
| 129 |
exit 1 |
| 130 |
else |
| 131 |
echo "The NVRAM index is exactly of the right size (236 bytes)." |
| 132 |
fi |
| 133 |
|
| 134 |
echo -n "123" > "$BINFILE" |
| 135 |
if ! "${TOOLSPATH}/${PREFIX}sign" \ |
| 136 |
-hk 81000000 \ |
| 137 |
-if "${BINFILE}" \ |
| 138 |
-os "${SIGFILE}" > "$TMPFILE"; |
| 139 |
then |
| 140 |
echo "Error: Could not create signature." |
| 141 |
cat "$TMPFILE" |
| 142 |
exit 1 |
| 143 |
fi |
| 144 |
fi |
| 145 |
|
| 146 |
if [ "$check" -eq 1 ]; then |
| 147 |
"${TOOLSPATH}/${PREFIX}getcapability" -cap 1 -pr 81000000 -pc 80 > "$TMPFILE" |
| 148 |
# We need know we need to see '65 Handles' for state created with |
| 149 |
# libtpms-0.6.0 and 128kb NVRAM size |
| 150 |
if ! grep -i "65 Handles" "$TMPFILE"; then |
| 151 |
echo "Error: Did not find '65 Handles' keyword in output" |
| 152 |
cat "$TMPFILE" |
| 153 |
exit 1 |
| 154 |
fi |
| 155 |
|
| 156 |
printf "Verifying signature with all the persisted keys\n" |
| 157 |
echo -n "123" > "$BINFILE" |
| 158 |
for ((i = 0x81000000; i < 0x81000040; i++)); do |
| 159 |
if ! "${TOOLSPATH}/${PREFIX}verifysignature" \ |
| 160 |
-hk "$(printf "%x" "$i")" \ |
| 161 |
-is "${SIGFILE}" \ |
| 162 |
-if "${BINFILE}" > "$TMPFILE"; |
| 163 |
then |
| 164 |
echo "Verifying signature failed for handle $(printf "%x" "$i")." |
| 165 |
exit 1 |
| 166 |
fi |
| 167 |
done |
| 168 |
fi |
| 169 |
} |
| 170 |
|
| 171 |
export TPM_SERVER_TYPE=raw |
| 172 |
export TPM_SERVER_NAME=127.0.0.1 |
| 173 |
export TPM_INTERFACE_TYPE=socsim |
| 174 |
export TPM_COMMAND_PORT=${TPM_COMMAND_PORT:-65460} |
| 175 |
export TPM_DATA_DIR=$TPMDIR |
| 176 |
export TPM_SESSION_ENCKEY="807e2bfe898ddaed8fa6310e716a24dc" # for sessions |
| 177 |
|
| 178 |
$SWTPM_EXE socket \ |
| 179 |
--server "port=${TPM_COMMAND_PORT}" \ |
| 180 |
--tpmstate "$STORE_PARAM" \ |
| 181 |
--pid "file=$PID_FILE" \ |
| 182 |
--ctrl "type=unixio,path=$SOCK_PATH" \ |
| 183 |
--log "file=$LOGFILE,level=20" \ |
| 184 |
--tpm2 \ |
| 185 |
${SWTPM_TEST_SECCOMP_OPT:+${SWTPM_TEST_SECCOMP_OPT}} & |
| 186 |
|
| 187 |
if wait_for_file "$PID_FILE" 3; then |
| 188 |
echo "Error: (1) Socket TPM did not write pidfile." |
| 189 |
exit 1 |
| 190 |
fi |
| 191 |
|
| 192 |
PID="$(cat "$PID_FILE")" |
| 193 |
|
| 194 |
# Send TPM_Init |
| 195 |
if ! act=$($SWTPM_IOCTL --unix "$SOCK_PATH" -i 2>&1); then |
| 196 |
echo "Error: $SWTPM_IOCTL CMD_INIT failed: $act" |
| 197 |
exit 1 |
| 198 |
fi |
| 199 |
|
| 200 |
if ! "${TOOLSPATH}/${PREFIX}startup" -c; then |
| 201 |
echo "Error: tpm_startup clear failed." |
| 202 |
exit 1 |
| 203 |
fi |
| 204 |
|
| 205 |
fillup_nvram 1 1 |
| 206 |
|
| 207 |
# Send Shutdown |
| 208 |
if ! act=$($SWTPM_IOCTL --unix "$SOCK_PATH" -s 2>&1); then |
| 209 |
echo "Error: $SWTPM_IOCTL CMD_SHUTDOWN failed: $act" |
| 210 |
exit 1 |
| 211 |
fi |
| 212 |
|
| 213 |
if wait_process_gone "${PID}" 4; then |
| 214 |
echo "Error: swtpm did not shut down" |
| 215 |
exit 1 |
| 216 |
fi |
| 217 |
|
| 218 |
echo "============================" >> "$LOGFILE" |
| 219 |
|
| 220 |
echo "TPM was shut down" |
| 221 |
|
| 222 |
# Store this state for later usage; use a really old version of libtpms: 0.6.0 |
| 223 |
#cp $TPMDIR/tpm2-00.permall ${TESTDIR}/data/tpm2state5; |
| 224 |
#cp $SIGFILE ${TESTDIR}/data/tpm2state5/signature.bin |
| 225 |
|
| 226 |
################################################################# |
| 227 |
# Run TPM2 with the created state and verify it's the same |
| 228 |
|
| 229 |
$SWTPM_EXE socket \ |
| 230 |
--server "port=${TPM_COMMAND_PORT}" \ |
| 231 |
--tpmstate "$STORE_PARAM" \ |
| 232 |
--pid "file=$PID_FILE" \ |
| 233 |
--ctrl "type=unixio,path=$SOCK_PATH" \ |
| 234 |
--log "file=$LOGFILE,level=20" \ |
| 235 |
--tpm2 \ |
| 236 |
${SWTPM_TEST_SECCOMP_OPT:+${SWTPM_TEST_SECCOMP_OPT}} & |
| 237 |
|
| 238 |
if wait_for_file "$PID_FILE" 3; then |
| 239 |
echo "Error: (2) Socket TPM did not write pidfile." |
| 240 |
exit 1 |
| 241 |
fi |
| 242 |
|
| 243 |
echo "TPM re-started" |
| 244 |
|
| 245 |
PID="$(cat "$PID_FILE")" |
| 246 |
|
| 247 |
# Send TPM_Init |
| 248 |
if ! act=$($SWTPM_IOCTL --unix "$SOCK_PATH" -i 2>&1); then |
| 249 |
echo "Error: $SWTPM_IOCTL CMD_INIT failed: $act" |
| 250 |
cat "$LOGFILE" |
| 251 |
exit 1 |
| 252 |
fi |
| 253 |
|
| 254 |
if ! "${TOOLSPATH}/${PREFIX}startup" -c; then |
| 255 |
echo "Error: tpm_startup clear failed." |
| 256 |
cat "$LOGFILE" |
| 257 |
exit 1 |
| 258 |
fi |
| 259 |
|
| 260 |
fillup_nvram 0 1 |
| 261 |
|
| 262 |
if ! "${TOOLSPATH}/${PREFIX}shutdown" -c; then |
| 263 |
echo "Error: tpm_shutdown clear failed." |
| 264 |
cat "$LOGFILE" |
| 265 |
exit 1 |
| 266 |
fi |
| 267 |
|
| 268 |
# Send Shutdown |
| 269 |
if ! act=$($SWTPM_IOCTL --unix "$SOCK_PATH" -s 2>&1); then |
| 270 |
echo "Error: $SWTPM_IOCTL CMD_SHUTDOWN failed: $act" |
| 271 |
exit 1 |
| 272 |
fi |
| 273 |
|
| 274 |
echo "============================" >> "$LOGFILE" |
| 275 |
|
| 276 |
echo "TPM was shut down" |
| 277 |
|
| 278 |
################################################################# |
| 279 |
# Run TPM2 with previously saved state and verify it's the same |
| 280 |
|
| 281 |
if [ "${SWTPM_TEST_LINEAR_FILE:-0}" -ne 0 ]; then |
| 282 |
echo "Test 1 OK (skipped last with linear file)" |
| 283 |
exit 0 |
| 284 |
fi |
| 285 |
|
| 286 |
rm -f "$TPMDIR"/* |
| 287 |
cp -f "${TESTDIR}/data/tpm2state5/tpm2-00.permall" "$TPMDIR/tpm2-00.permall" |
| 288 |
cp "${TESTDIR}/data/tpm2state5/signature.bin" "$SIGFILE" |
| 289 |
|
| 290 |
$SWTPM_EXE socket \ |
| 291 |
--server "port=${TPM_COMMAND_PORT}" \ |
| 292 |
--tpmstate "$STORE_PARAM,lock" \ |
| 293 |
--pid "file=$PID_FILE" \ |
| 294 |
--ctrl "type=unixio,path=$SOCK_PATH" \ |
| 295 |
--log "file=$LOGFILE,level=20" \ |
| 296 |
--tpm2 \ |
| 297 |
${SWTPM_TEST_SECCOMP_OPT:+${SWTPM_TEST_SECCOMP_OPT}} & |
| 298 |
|
| 299 |
if wait_for_file "$PID_FILE" 3; then |
| 300 |
echo "Error: (3) Socket TPM did not write pidfile." |
| 301 |
exit 1 |
| 302 |
fi |
| 303 |
|
| 304 |
echo "TPM started with previously generated state" |
| 305 |
|
| 306 |
PID="$(cat "$PID_FILE")" |
| 307 |
|
| 308 |
# Send TPM_Init |
| 309 |
if ! act=$($SWTPM_IOCTL --unix "$SOCK_PATH" -i 2>&1); then |
| 310 |
echo "Error: $SWTPM_IOCTL CMD_INIT failed: $act" |
| 311 |
exit 1 |
| 312 |
fi |
| 313 |
|
| 314 |
if ! "${TOOLSPATH}/${PREFIX}startup" -c; then |
| 315 |
echo "Error: tpm_startup clear failed." |
| 316 |
cat "$LOGFILE" |
| 317 |
exit 1 |
| 318 |
fi |
| 319 |
|
| 320 |
fillup_nvram 0 1 |
| 321 |
|
| 322 |
if ! "${TOOLSPATH}/${PREFIX}shutdown" -c; then |
| 323 |
echo "Error: tpm_shutdown clear failed." |
| 324 |
cat "$LOGFILE" |
| 325 |
exit 1 |
| 326 |
fi |
| 327 |
|
| 328 |
# Start another swtpm that must fail to start due to locked storage |
| 329 |
# The not-need-init flag causes the failure. |
| 330 |
if $SWTPM_EXE socket \ |
| 331 |
--tpmstate "$STORE_PARAM,lock" \ |
| 332 |
--log "file=${LOGFILE2},level=1" \ |
| 333 |
--flags not-need-init \ |
| 334 |
--tpm2 \ |
| 335 |
${SWTPM_TEST_SECCOMP_OPT:+${SWTPM_TEST_SECCOMP_OPT}}; then |
| 336 |
echo "Error: Starting 2nd instance on locked storage should have failed" |
| 337 |
exit 1 |
| 338 |
fi |
| 339 |
if ! grep -q "Resource temporarily unavailable" "${LOGFILE2}"; then |
| 340 |
echo "Error: Expected error 'Resource temporarily unavailable' in logfile" |
| 341 |
cat "${LOGFILE2}" |
| 342 |
exit 1 |
| 343 |
fi |
| 344 |
|
| 345 |
# Send Shutdown |
| 346 |
if ! act=$($SWTPM_IOCTL --unix "$SOCK_PATH" -s 2>&1); then |
| 347 |
echo "Error: $SWTPM_IOCTL CMD_SHUTDOWN failed: $act" |
| 348 |
exit 1 |
| 349 |
fi |
| 350 |
|
| 351 |
echo "Test 1 OK" |