| 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 |
TMPFILE=$TPMDIR/tmpfile |
| 28 |
BINFILE=$TPMDIR/binfile |
| 29 |
SIGFILE=$TPMDIR/sigfile |
| 30 |
SIGFILE2=$TPMDIR/sigfile2 |
| 31 |
TMP2FILE=$TPMDIR/tmpfile2 |
| 32 |
PRIVKEY=$TPMDIR/privkey.pem |
| 33 |
PUBKEY=$TPMDIR/pubkey.pem |
| 34 |
PUBKEYCONTEXT=$TPMDIR/pubkey.context |
| 35 |
|
| 36 |
HKEYPRIV=${TESTDIR}/data/tpm2state3/hkey.priv |
| 37 |
HKEYPUB=${TESTDIR}/data/tpm2state3/hkey.pub |
| 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 |
function test_nvram_state() |
| 57 |
{ |
| 58 |
local create="$1" |
| 59 |
local check="$2" |
| 60 |
|
| 61 |
local i res rc act exp ody |
| 62 |
|
| 63 |
if [ "$create" -eq 1 ]; then |
| 64 |
# the 1st and 2nd spaces are 'orderly' and will be cleared by reset |
| 65 |
ody="+at ody" |
| 66 |
for ((i=0; i < 10; i++)); do |
| 67 |
printf "Creating NVRAM location 01%06x\n" "$i" |
| 68 |
# the '+at wd' allows us to only write once |
| 69 |
if ! "${TOOLSPATH}/${PREFIX}nvdefinespace" \ |
| 70 |
-ha "$(printf "01%06x" "$i")" \ |
| 71 |
-sz $((100 + i * 10)) \ |
| 72 |
-pwdn nnn \ |
| 73 |
+at wst \ |
| 74 |
+at wd \ |
| 75 |
${ody:+${ody}} \ |
| 76 |
-hi o >/dev/null; |
| 77 |
then |
| 78 |
echo "Error: nvdefinespace failed for i = $i." |
| 79 |
exit 1 |
| 80 |
fi |
| 81 |
|
| 82 |
if [ "$i" -eq 1 ]; then |
| 83 |
ody="" |
| 84 |
fi |
| 85 |
|
| 86 |
if ! "${TOOLSPATH}/${PREFIX}nvwrite" \ |
| 87 |
-ha "$(printf "01%06x" "$i")" \ |
| 88 |
-ic "Hello TPM2" \ |
| 89 |
-pwdn nnn; |
| 90 |
then |
| 91 |
echo "Error: nwrite failed for i = $i." |
| 92 |
exit 1 |
| 93 |
fi |
| 94 |
|
| 95 |
if ! "${TOOLSPATH}/${PREFIX}nvwritelock" \ |
| 96 |
-ha "$(printf "01%06x" "$i")" \ |
| 97 |
-pwdn nnn; |
| 98 |
then |
| 99 |
echo "Error: nwritelock failed for i = $i." |
| 100 |
exit 1 |
| 101 |
fi |
| 102 |
done |
| 103 |
|
| 104 |
# Create a counter space |
| 105 |
echo "Creating NVRAM location 01000010 for counter" |
| 106 |
if ! "${TOOLSPATH}/${PREFIX}nvdefinespace" \ |
| 107 |
-hi o \ |
| 108 |
-ha 01000010 \ |
| 109 |
-pwdn nnn \ |
| 110 |
-ty c >/dev/null; |
| 111 |
then |
| 112 |
echo "Error: nvdefinespace for counter failed." |
| 113 |
exit 1 |
| 114 |
fi |
| 115 |
|
| 116 |
echo "Incrementing the counter at location 01000010" |
| 117 |
if ! "${TOOLSPATH}/${PREFIX}nvincrement" \ |
| 118 |
-ha 01000010 \ |
| 119 |
-pwdn nnn >/dev/null; |
| 120 |
then |
| 121 |
echo "Error: nvincrement failed." |
| 122 |
exit 1 |
| 123 |
fi |
| 124 |
fi |
| 125 |
|
| 126 |
if [ "$check" -eq 1 ]; then |
| 127 |
local last=0 |
| 128 |
|
| 129 |
if [ "$create" -eq 0 ]; then |
| 130 |
last=2 |
| 131 |
fi |
| 132 |
|
| 133 |
# The orderly indices must not be readable UNLESS they were just |
| 134 |
# created. In the latter case we skip this first loop here. |
| 135 |
for ((i=0; i < last; i++)); do |
| 136 |
printf "Checking orderly NVRAM location 01%06x after reset\n" "$i" |
| 137 |
if "${TOOLSPATH}/${PREFIX}nvread" \ |
| 138 |
-ha "$(printf "01%06x" "$i")" \ |
| 139 |
-pwdn nnn \ |
| 140 |
-sz 10 > "$TMPFILE"; |
| 141 |
then |
| 142 |
echo "Error: nvread succeeded for orderly NVRAM index; i = $i" |
| 143 |
cat "$TMPFILE" |
| 144 |
exit 1 |
| 145 |
fi |
| 146 |
done |
| 147 |
|
| 148 |
# test the non-orderly indices OR orderly we just created above |
| 149 |
for ((i=last; i < 10; i++)); do |
| 150 |
printf "Checking NVRAM location 01%06x\n" "$i" |
| 151 |
if ! "${TOOLSPATH}/${PREFIX}nvread" \ |
| 152 |
-ha "$(printf "01%06x" "$i")" \ |
| 153 |
-pwdn nnn \ |
| 154 |
-sz 10 > "$TMPFILE"; |
| 155 |
then |
| 156 |
echo "Error: nvread failed for i = $i" |
| 157 |
cat "$TMPFILE" |
| 158 |
exit 1 |
| 159 |
fi |
| 160 |
|
| 161 |
# we want one line with xdigits and spaces |
| 162 |
res=$(grep -c -E "^[ [:xdigit:]]+$" < "$TMPFILE") |
| 163 |
if [ "$res" -ne 1 ]; then |
| 164 |
echo "Error: nvread did not show expected results" |
| 165 |
cat "$TMPFILE" |
| 166 |
fi |
| 167 |
|
| 168 |
if "${TOOLSPATH}/${PREFIX}nvwrite" \ |
| 169 |
-ha "$(printf "01%06x" "$i")" \ |
| 170 |
-ic "Hello TPM2" \ |
| 171 |
-pwdn nnn > "$TMPFILE"; |
| 172 |
then |
| 173 |
echo "Error: nwrite succeeded for i = $i." |
| 174 |
exit 1 |
| 175 |
fi |
| 176 |
done |
| 177 |
|
| 178 |
# Read the counter |
| 179 |
echo "Checking counter value at location 01000010" |
| 180 |
if ! "${TOOLSPATH}/${PREFIX}nvread" \ |
| 181 |
-ha 01000010 \ |
| 182 |
-pwdn nnn \ |
| 183 |
-sz 8 \ |
| 184 |
-of "$BINFILE" > "$TMPFILE"; |
| 185 |
then |
| 186 |
echo "Error: nvread of counter failed." |
| 187 |
cat "$TMPFILE" |
| 188 |
exit 1 |
| 189 |
fi |
| 190 |
exp=' 00 00 00 00 00 00 00 01' |
| 191 |
act="$(od -t x1 -A n < "$BINFILE")" |
| 192 |
if [ "$act" != "$exp" ]; then |
| 193 |
echo "Error: Counter has unexpected value." |
| 194 |
echo " expected: $exp" |
| 195 |
echo " actual : $act" |
| 196 |
fi |
| 197 |
fi |
| 198 |
} |
| 199 |
|
| 200 |
function test_primary() |
| 201 |
{ |
| 202 |
local create="$1" |
| 203 |
local check="$2" |
| 204 |
# whether we are using previous stored stated that had a different |
| 205 |
# key and we have to use the old signature |
| 206 |
local previousstate="$3" |
| 207 |
|
| 208 |
local i res rc |
| 209 |
|
| 210 |
if [ "$create" -eq 1 ]; then |
| 211 |
# Create a permanent primary key that we expect |
| 212 |
# to again see after the TPM has been restarted |
| 213 |
if ! "${TOOLSPATH}/${PREFIX}createprimary" -hi o -si > "$TMPFILE"; then |
| 214 |
echo "Error: createprimary failed." |
| 215 |
exit 1 |
| 216 |
fi |
| 217 |
if ! grep -q 80000000 "$TMPFILE"; then |
| 218 |
echo "Error: createprimary did not result in expected handle 80000000" |
| 219 |
exit 1 |
| 220 |
fi |
| 221 |
if ! "${TOOLSPATH}/${PREFIX}evictcontrol" -ho 80000000 -hp 81000000 -hi o; then |
| 222 |
echo "Error: evictcontrol did not work" |
| 223 |
exit 1 |
| 224 |
fi |
| 225 |
"${TOOLSPATH}/${PREFIX}flushcontext" -ha 80000000 |
| 226 |
|
| 227 |
echo -n "123" > "$BINFILE" |
| 228 |
if ! "${TOOLSPATH}/${PREFIX}sign" -hk 81000000 -if "${BINFILE}" -os "${SIGFILE}" > "$TMPFILE"; then |
| 229 |
echo "Error: Could not create signature." |
| 230 |
cat "$TMPFILE" |
| 231 |
exit 1 |
| 232 |
fi |
| 233 |
fi |
| 234 |
|
| 235 |
if [ "$check" -eq 1 ]; then |
| 236 |
printf "Checking availability of key with perm. handle 0x81000000\n" |
| 237 |
"${TOOLSPATH}/${PREFIX}getcapability" -cap 1 -pr 0x81000000 >"$TMPFILE" |
| 238 |
if ! grep -q 81000000 "$TMPFILE"; then |
| 239 |
echo "Could not find key with permanent handle 0x81000000" |
| 240 |
exit 1 |
| 241 |
fi |
| 242 |
printf "Verifying signature with this key\n" |
| 243 |
echo -n "123" > "$BINFILE" |
| 244 |
if [ "$previousstate" -eq 0 ]; then |
| 245 |
"${TOOLSPATH}/${PREFIX}verifysignature" -hk 81000000 \ |
| 246 |
-is "${SIGFILE}" \ |
| 247 |
-if "${BINFILE}" > "$TMPFILE" |
| 248 |
rc=$? |
| 249 |
else |
| 250 |
"${TOOLSPATH}/${PREFIX}verifysignature" -hk 81000000 \ |
| 251 |
-is "${TESTDIR}/data/tpm2state3/signature.bin" \ |
| 252 |
-if "${BINFILE}" > "$TMPFILE" |
| 253 |
rc=$? |
| 254 |
fi |
| 255 |
if [ $rc -ne 0 ]; then |
| 256 |
echo "Verifying signature failed." |
| 257 |
exit 1 |
| 258 |
fi |
| 259 |
fi |
| 260 |
} |
| 261 |
|
| 262 |
# Allocate a SHA256 PCR bank |
| 263 |
# This will prevent shutdown -s (with state) |
| 264 |
function test_pcr_allocation() |
| 265 |
{ |
| 266 |
local create="$1" |
| 267 |
local check="$2" |
| 268 |
|
| 269 |
local i res rc ha |
| 270 |
|
| 271 |
if ! "$TOOLSPATH/${PREFIX}pcrallocate" | grep -q sha512; then |
| 272 |
echo " Skipping PCR Allocate test since it does not support sha512" |
| 273 |
return 0 |
| 274 |
fi |
| 275 |
|
| 276 |
if [ "$create" -eq 1 ]; then |
| 277 |
echo "Allocating SHA256 PCR bank" |
| 278 |
"${TOOLSPATH}/${PREFIX}pcrallocate" -sha512 +sha256 |
| 279 |
|
| 280 |
for ((ha = 0; ha < 24; ha++)); do |
| 281 |
"${TOOLSPATH}/${PREFIX}pcrread" -ha "${ha}" -halg sha512 > "$TMPFILE" |
| 282 |
if ! grep -q "^count 1.*$" "$TMPFILE"; then |
| 283 |
echo "Error: PCR ${ha} in SHA512 bank should be available for read before reboot" |
| 284 |
cat "$TMPFILE" |
| 285 |
exit 1 |
| 286 |
fi |
| 287 |
"${TOOLSPATH}/${PREFIX}pcrread" -ha "${ha}" -halg sha256 > "$TMPFILE" |
| 288 |
if ! grep -q "^count 1.*$" "$TMPFILE"; then |
| 289 |
echo "Error: PCR ${ha} in SHA256 bank should be available for read before reboot" |
| 290 |
cat "$TMPFILE" |
| 291 |
exit 1 |
| 292 |
fi |
| 293 |
done |
| 294 |
fi |
| 295 |
|
| 296 |
if [ "$check" -eq 1 ]; then |
| 297 |
echo "Checking the PCR Allocation" |
| 298 |
|
| 299 |
for ((ha = 0; ha < 24; ha++)); do |
| 300 |
"${TOOLSPATH}/${PREFIX}pcrread" -ha "${ha}" -halg sha512 > "$TMPFILE" |
| 301 |
if ! grep -q "^count 0.*$" "$TMPFILE"; then |
| 302 |
echo "Error: PCR ${ha} in SHA512 bank should be unavailable for read after reboot" |
| 303 |
cat "$TMPFILE" |
| 304 |
exit 1 |
| 305 |
fi |
| 306 |
|
| 307 |
"${TOOLSPATH}/${PREFIX}pcrread" -ha "${ha}" -halg sha256 > "$TMPFILE" |
| 308 |
if ! grep -q "^count 1.*$" "$TMPFILE"; then |
| 309 |
echo "Error: PCR ${ha} in SHA256 bank should be available for read after reboot" |
| 310 |
exit 1 |
| 311 |
fi |
| 312 |
done |
| 313 |
fi |
| 314 |
} |
| 315 |
|
| 316 |
function test_hierarchy() |
| 317 |
{ |
| 318 |
local create="$1" |
| 319 |
local check="$2" |
| 320 |
|
| 321 |
local hi pwdn pwda |
| 322 |
|
| 323 |
if [ "$create" -eq 1 ]; then |
| 324 |
echo "Setting hierarchy passwords" |
| 325 |
# Change the hierarchy password; the 'p' hierarchy has |
| 326 |
# no effect on permanent RAM, so we won't test that |
| 327 |
for hi in "l" "e" "o"; do |
| 328 |
pwdn="${hi}${hi}${hi}" |
| 329 |
if ! "${TOOLSPATH}/${PREFIX}hierarchychangeauth" \ |
| 330 |
-hi "${hi}" \ |
| 331 |
-pwdn "${pwdn}" > "$TMPFILE"; |
| 332 |
then |
| 333 |
echo "Error: hierarchychangeauth failed to set password." |
| 334 |
cat "$TMPFILE" |
| 335 |
exit 1 |
| 336 |
fi |
| 337 |
done |
| 338 |
fi |
| 339 |
|
| 340 |
if [ "$check" -eq 1 ]; then |
| 341 |
echo "Checking previously set hierarchy passwords" |
| 342 |
for hi in "l" "e" "o"; do |
| 343 |
pwda="${hi}${hi}${hi}" |
| 344 |
pwdn="new-${pwda}" |
| 345 |
|
| 346 |
if ! "${TOOLSPATH}/${PREFIX}hierarchychangeauth" \ |
| 347 |
-hi "${hi}" \ |
| 348 |
-pwda "${pwda}" \ |
| 349 |
-pwdn "${pwdn}" > "$TMPFILE"; |
| 350 |
then |
| 351 |
echo "Error: hierarchychangeauth failed to change password." |
| 352 |
cat "$TMPFILE" |
| 353 |
exit 1 |
| 354 |
fi |
| 355 |
|
| 356 |
# change back |
| 357 |
if ! "${TOOLSPATH}/${PREFIX}hierarchychangeauth" \ |
| 358 |
-hi "${hi}" \ |
| 359 |
-pwda "${pwdn}" \ |
| 360 |
-pwdn "${pwda}" > "$TMPFILE"; |
| 361 |
then |
| 362 |
echo "Error: hierarchychangeauth failed to change back password." |
| 363 |
cat "$TMPFILE" |
| 364 |
exit 1 |
| 365 |
fi |
| 366 |
done |
| 367 |
fi |
| 368 |
} |
| 369 |
|
| 370 |
function test_hash_context() |
| 371 |
{ |
| 372 |
local create="$1" |
| 373 |
local check="$2" |
| 374 |
|
| 375 |
local res |
| 376 |
|
| 377 |
if [ "$create" -eq 1 ]; then |
| 378 |
echo -n "123" > "${TMP2FILE}" |
| 379 |
|
| 380 |
echo "Starting a sha1 sequence" |
| 381 |
if ! res="$("${TOOLSPATH}/${PREFIX}hashsequencestart" -halg sha1)"; then |
| 382 |
echo "Error: Could not start hash sequence." |
| 383 |
exit 1 |
| 384 |
fi |
| 385 |
SHA1_SEQUENCE_HANDLE="$(echo "$res" | cut -d " " -f3)" |
| 386 |
echo "sha1 sequence handle: $SHA1_SEQUENCE_HANDLE" |
| 387 |
|
| 388 |
if ! "${TOOLSPATH}/${PREFIX}sequenceupdate" \ |
| 389 |
-hs "${SHA1_SEQUENCE_HANDLE}" \ |
| 390 |
-if "${TMP2FILE}"; |
| 391 |
then |
| 392 |
echo "Error: Could not updated the sha1 sequence." |
| 393 |
exit 1 |
| 394 |
fi |
| 395 |
echo "Updated sha1 sequence." |
| 396 |
|
| 397 |
echo "Starting a sha256 sequence" |
| 398 |
if ! res="$("${TOOLSPATH}/${PREFIX}hashsequencestart" -halg sha256)"; then |
| 399 |
echo "Error: Could not start sha256 sequence." |
| 400 |
exit 1 |
| 401 |
fi |
| 402 |
SHA256_SEQUENCE_HANDLE="$(echo "$res" | cut -d " " -f3)" |
| 403 |
echo "sha256 sequence handle: $SHA256_SEQUENCE_HANDLE" |
| 404 |
|
| 405 |
if ! "${TOOLSPATH}/${PREFIX}sequenceupdate" \ |
| 406 |
-hs "${SHA256_SEQUENCE_HANDLE}" \ |
| 407 |
-if "${TMP2FILE}"; |
| 408 |
then |
| 409 |
echo "Error: Could not updated the hash sequence." |
| 410 |
exit 1 |
| 411 |
fi |
| 412 |
echo "Updated sha256 sequence." |
| 413 |
|
| 414 |
echo "Starting a sha384 sequence" |
| 415 |
if ! res="$("${TOOLSPATH}/${PREFIX}hashsequencestart" -halg sha384)"; then |
| 416 |
echo "Error: Could not start sha384 sequence." |
| 417 |
exit 1 |
| 418 |
fi |
| 419 |
SHA384_SEQUENCE_HANDLE="$(echo "$res" | cut -d " " -f3)" |
| 420 |
echo "sha384 sequence handle: $SHA384_SEQUENCE_HANDLE" |
| 421 |
|
| 422 |
if ! "${TOOLSPATH}/${PREFIX}sequenceupdate" \ |
| 423 |
-hs "${SHA384_SEQUENCE_HANDLE}" \ |
| 424 |
-if "${TMP2FILE}"; |
| 425 |
then |
| 426 |
echo "Error: Could not updated the hash sequence." |
| 427 |
exit 1 |
| 428 |
fi |
| 429 |
echo "Updated sha384 sequence." |
| 430 |
fi |
| 431 |
|
| 432 |
if [ "$check" -eq 1 ]; then |
| 433 |
echo -n "456" > "${TMP2FILE}" |
| 434 |
|
| 435 |
echo "Completing previously started sha1 sequence" |
| 436 |
touch "$TPMDIR/h${SHA1_SEQUENCE_HANDLE}.bin" |
| 437 |
res=$("${TOOLSPATH}/${PREFIX}sequencecomplete" \ |
| 438 |
-hs "${SHA1_SEQUENCE_HANDLE}" \ |
| 439 |
-if "${TMP2FILE}" -v | |
| 440 |
tail -n 4 | |
| 441 |
grep " 7c 4a 8d ") |
| 442 |
if [ -z "$res" ]; then |
| 443 |
echo "Error: Did not get expected result from completing sha1 sequence." |
| 444 |
exit 1 |
| 445 |
fi |
| 446 |
|
| 447 |
echo "Completing previously started sha256 sequence" |
| 448 |
touch "$TPMDIR/h${SHA256_SEQUENCE_HANDLE}.bin" |
| 449 |
res=$("${TOOLSPATH}/${PREFIX}sequencecomplete" \ |
| 450 |
-hs "${SHA256_SEQUENCE_HANDLE}" \ |
| 451 |
-if "${TMP2FILE}" -v | |
| 452 |
tail -n 4 | |
| 453 |
grep " 8d 96 9e ") |
| 454 |
if [ -z "$res" ]; then |
| 455 |
echo "Error: Did not get expected result from completing sha256 sequence." |
| 456 |
exit 1 |
| 457 |
fi |
| 458 |
|
| 459 |
echo "Completing previously started sha384 sequence" |
| 460 |
touch "$TPMDIR/h${SHA384_SEQUENCE_HANDLE}.bin" |
| 461 |
res=$("${TOOLSPATH}/${PREFIX}sequencecomplete" \ |
| 462 |
-hs "${SHA384_SEQUENCE_HANDLE}" \ |
| 463 |
-if "${TMP2FILE}" -v | |
| 464 |
tail -n 4 | |
| 465 |
grep " 0a 98 9e ") |
| 466 |
if [ -z "$res" ]; then |
| 467 |
echo "Error: Did not get expected result from completing sha384 sequence." |
| 468 |
exit 1 |
| 469 |
fi |
| 470 |
fi |
| 471 |
} |
| 472 |
|
| 473 |
function test_session() |
| 474 |
{ |
| 475 |
local create="$1" |
| 476 |
local check="$2" |
| 477 |
# whether we are using previous stored stated that had a different |
| 478 |
# key and we have to use the old signature |
| 479 |
local previousstate="$3" |
| 480 |
|
| 481 |
local i res rc |
| 482 |
|
| 483 |
if [ "$create" -eq 1 ]; then |
| 484 |
# Create a permanent primary key that we expect |
| 485 |
# to again see after the TPM has been restarted |
| 486 |
if ! "${TOOLSPATH}/${PREFIX}createprimary" -hi o -st > "$TMPFILE"; then |
| 487 |
echo "Error: createprimary for creating storage key failed." |
| 488 |
exit 1 |
| 489 |
fi |
| 490 |
if ! grep -q 80000000 "$TMPFILE"; then |
| 491 |
echo "Error: createprimary did not result in expected handle 80000000" |
| 492 |
cat "$TMPFILE" |
| 493 |
exit 1 |
| 494 |
fi |
| 495 |
if ! "${TOOLSPATH}/${PREFIX}evictcontrol" -ho 80000000 -hp 81000000 -hi o; then |
| 496 |
echo "Error: evictcontrol did not work" |
| 497 |
exit 1 |
| 498 |
fi |
| 499 |
"${TOOLSPATH}/${PREFIX}flushcontext" -ha 80000000 |
| 500 |
|
| 501 |
if ! "${TOOLSPATH}/${PREFIX}startauthsession" -se h -bi 81000000 > "$TMPFILE"; then |
| 502 |
echo "Error: Could not start an auth session." |
| 503 |
cat "$TMPFILE" |
| 504 |
exit 1 |
| 505 |
fi |
| 506 |
AUTHSESSION_HANDLE=$(sed 's/Handle//' "$TMPFILE") |
| 507 |
if [ -z "${AUTHSESSION_HANDLE}" ]; then |
| 508 |
echo "Error: Could not get auth session handle." |
| 509 |
exit 1 |
| 510 |
fi |
| 511 |
fi |
| 512 |
|
| 513 |
if [ "$check" -eq 1 ]; then |
| 514 |
echo "Using auth session ${AUTHSESSION_HANDLE} to create a key." |
| 515 |
if ! "${TOOLSPATH}/${PREFIX}create" \ |
| 516 |
-hp 81000000 \ |
| 517 |
-st \ |
| 518 |
-se0 "${AUTHSESSION_HANDLE}" 1; then |
| 519 |
echo "Error: Could not create key using authsession" |
| 520 |
exit 1 |
| 521 |
fi |
| 522 |
echo "Successfully created key" |
| 523 |
fi |
| 524 |
} |
| 525 |
|
| 526 |
function test_hmac_context() |
| 527 |
{ |
| 528 |
local create="$1" |
| 529 |
local check="$2" |
| 530 |
# whether we are using previous stored stated that had a different |
| 531 |
# key and we have to use the old signature |
| 532 |
local previousstate="$3" |
| 533 |
|
| 534 |
local i res rc |
| 535 |
|
| 536 |
if [ "$create" -eq 1 ]; then |
| 537 |
if ! "${TOOLSPATH}/${PREFIX}createprimary" -hi o -st > "$TMPFILE"; then |
| 538 |
echo "Error: createprimary failed." |
| 539 |
exit 1 |
| 540 |
fi |
| 541 |
if ! grep -q 80000000 "$TMPFILE"; then |
| 542 |
echo "Error: createprimary did not result in expected handle 80000000" |
| 543 |
exit 1 |
| 544 |
fi |
| 545 |
|
| 546 |
if ! "${TOOLSPATH}/${PREFIX}create" -hp 80000000 -kh \ |
| 547 |
-opr "${HKEYPRIV}" -opu "${HKEYPUB}" > "$TMPFILE"; |
| 548 |
then |
| 549 |
echo "Error: could not create key for HMAC" |
| 550 |
exit 1 |
| 551 |
fi |
| 552 |
|
| 553 |
if ! "${TOOLSPATH}/${PREFIX}load" -hp 80000000 \ |
| 554 |
-ipr "${HKEYPRIV}" -ipu "${HKEYPUB}" -v > "$TMPFILE"; |
| 555 |
then |
| 556 |
echo "Error: could not load key for HMAC" |
| 557 |
cat "$TMPFILE" |
| 558 |
exit 1 |
| 559 |
fi |
| 560 |
if ! grep -q 80000001 "$TMPFILE"; then |
| 561 |
echo "Error: load did not result in expected handle 80000001" |
| 562 |
exit 1 |
| 563 |
fi |
| 564 |
|
| 565 |
if ! "${TOOLSPATH}/${PREFIX}hmacstart" -hk 80000001 > "$TMPFILE"; then |
| 566 |
echo "Error: could not start HMAC sequence" |
| 567 |
exit 1 |
| 568 |
fi |
| 569 |
if ! grep -q 80000002 "$TMPFILE"; then |
| 570 |
echo "Error: load did not result in expected handle 80000002" |
| 571 |
exit 1 |
| 572 |
fi |
| 573 |
|
| 574 |
echo -n "123" > "${TMP2FILE}" |
| 575 |
if ! "${TOOLSPATH}/${PREFIX}sequenceupdate" \ |
| 576 |
-hs 80000002 \ |
| 577 |
-if "${TMP2FILE}"; |
| 578 |
then |
| 579 |
echo "Error: Could not updated the HMAC sequence." |
| 580 |
exit 1 |
| 581 |
fi |
| 582 |
echo "Updated HMAC sequence." |
| 583 |
fi |
| 584 |
|
| 585 |
if [ "$check" -eq 1 ]; then |
| 586 |
echo -n "456" > "${TMP2FILE}" |
| 587 |
|
| 588 |
echo "Completing previously started HMAC sequence" |
| 589 |
touch "$TPMDIR/h80000002.bin" |
| 590 |
"${TOOLSPATH}/${PREFIX}sequencecomplete" \ |
| 591 |
-hs 80000002 \ |
| 592 |
-if "${TMP2FILE}" -v | |
| 593 |
tail -n 4 > "${TMPFILE}" |
| 594 |
if ! grep -q " 6e 40 33 1a " "${TMPFILE}"; then |
| 595 |
echo "Error: Did not get expected result from completing HMAC sequence." |
| 596 |
cat "$TMPFILE" |
| 597 |
exit 1 |
| 598 |
fi |
| 599 |
fi |
| 600 |
} |
| 601 |
|
| 602 |
function test_primary_volatile_load() |
| 603 |
{ |
| 604 |
local create="$1" |
| 605 |
local check="$2" |
| 606 |
# whether we are using previous stored stated that had a different |
| 607 |
# key and we have to use the old signature |
| 608 |
local previousstate="$3" |
| 609 |
|
| 610 |
local i res rc |
| 611 |
|
| 612 |
if [ "$create" -eq 1 ]; then |
| 613 |
# Create a permanent primary key that we expect |
| 614 |
# to again see after the TPM has been restarted |
| 615 |
if ! "${TOOLSPATH}/${PREFIX}createprimary" -hi o -si > "$TMPFILE"; then |
| 616 |
echo "Error: createprimary failed." |
| 617 |
exit 1 |
| 618 |
fi |
| 619 |
if ! grep -q 80000000 "$TMPFILE"; then |
| 620 |
echo "Error: createprimary did not result in expected handle 80000000" |
| 621 |
exit 1 |
| 622 |
fi |
| 623 |
|
| 624 |
echo -n "123" > "$BINFILE" |
| 625 |
if ! "${TOOLSPATH}/${PREFIX}sign" -hk 80000000 -if "${BINFILE}" -os "${SIGFILE}" > "$TMPFILE"; then |
| 626 |
echo "Error: Could not create signature." |
| 627 |
cat "$TMPFILE" |
| 628 |
exit 1 |
| 629 |
fi |
| 630 |
|
| 631 |
printf "Verifying signature with this key (create phase)\n" |
| 632 |
if ! "${TOOLSPATH}/${PREFIX}verifysignature" -hk 80000000 \ |
| 633 |
-is "${SIGFILE}" \ |
| 634 |
-if "${BINFILE}" > "$TMPFILE"; |
| 635 |
then |
| 636 |
echo "Verifying signature failed." |
| 637 |
exit 1 |
| 638 |
fi |
| 639 |
fi |
| 640 |
|
| 641 |
if [ "$check" -eq 1 ]; then |
| 642 |
local sigfile=${SIGFILE} hash1 hash2 |
| 643 |
|
| 644 |
if [ "$previousstate" -ne 0 ]; then |
| 645 |
sigfile=${TESTDIR}/data/tpm2state3d/signature2.bin |
| 646 |
fi |
| 647 |
|
| 648 |
printf "Checking availability of key with handle 0x80000000\n" |
| 649 |
"${TOOLSPATH}/${PREFIX}getcapability" -cap 1 -pr 0x80000000 > "$TMPFILE" |
| 650 |
if ! grep -q 80000000 "$TMPFILE"; then |
| 651 |
echo "Could not find key with handle 0x80000000" |
| 652 |
exit 1 |
| 653 |
fi |
| 654 |
|
| 655 |
printf "Verifying signature with this key (check phase)\n" |
| 656 |
echo -n "123" > "$BINFILE" |
| 657 |
if ! "${TOOLSPATH}/${PREFIX}verifysignature" -hk 80000000 \ |
| 658 |
-is "${sigfile}" \ |
| 659 |
-if "${BINFILE}" > "$TMPFILE"; |
| 660 |
then |
| 661 |
echo "Verifying signature failed." |
| 662 |
exit 1 |
| 663 |
fi |
| 664 |
|
| 665 |
if [ "$previousstate" -eq 0 ]; then |
| 666 |
if ! "${TOOLSPATH}/${PREFIX}sign" -hk 80000000 -if "${BINFILE}" -os "${SIGFILE2}" > "$TMPFILE"; then |
| 667 |
echo "Error: Could not create signature." |
| 668 |
cat "$TMPFILE" |
| 669 |
exit 1 |
| 670 |
fi |
| 671 |
hash1=$(get_sha1_file "${SIGFILE}") |
| 672 |
hash2=$(get_sha1_file "${SIGFILE2}") |
| 673 |
if [ "${hash1}" != "${hash2}" ]; then |
| 674 |
echo "Error: hashes of signatures are different. Loaded key may be different." |
| 675 |
exit 1 |
| 676 |
fi |
| 677 |
fi |
| 678 |
fi |
| 679 |
} |
| 680 |
|
| 681 |
# libtpms issue #195: Create an external key and load it into the TPM 2 |
| 682 |
# and do a context save/load cycle |
| 683 |
function test_external_key() |
| 684 |
{ |
| 685 |
local create="$1" |
| 686 |
local check="$2" |
| 687 |
|
| 688 |
if [ "$create" -eq 1 ]; then |
| 689 |
${CERTTOOL} --generate-privkey --bits 2048 --outfile "${PRIVKEY}" &>/dev/null |
| 690 |
${CERTTOOL} --pubkey-info --load-privkey "${PRIVKEY}" > "${PUBKEY}" |
| 691 |
if ! "$TOOLSPATH/${PREFIX}loadexternal" -hi o -ipem "${PUBKEY}" > "$TMPFILE"; then |
| 692 |
echo "Error: loadexternal failed." |
| 693 |
exit 1 |
| 694 |
fi |
| 695 |
if ! grep -q 80000001 "$TMPFILE"; then |
| 696 |
echo "Error: loadexternal did not result in expected handle 80000001" |
| 697 |
exit 1 |
| 698 |
fi |
| 699 |
fi |
| 700 |
|
| 701 |
if [ "$check" -eq 1 ]; then |
| 702 |
if ! "$TOOLSPATH/${PREFIX}contextsave" -ha 80000001 -of "${PUBKEYCONTEXT}"; then |
| 703 |
echo "Error: contextsave on loaded public key failed." |
| 704 |
exit 1 |
| 705 |
fi |
| 706 |
"$TOOLSPATH/${PREFIX}flushcontext" -ha 80000001 |
| 707 |
if ! "$TOOLSPATH/${PREFIX}contextload" -if "${PUBKEYCONTEXT}" > "$TMPFILE"; then |
| 708 |
echo "Error: contextload on context of public key failed." |
| 709 |
exit 1 |
| 710 |
fi |
| 711 |
if ! grep -q 80000001 "$TMPFILE"; then |
| 712 |
echo "Error: contextload did not result in expected handle 80000001" |
| 713 |
exit 1 |
| 714 |
fi |
| 715 |
fi |
| 716 |
} |
| 717 |
|
| 718 |
export TPM_SERVER_TYPE=raw |
| 719 |
export TPM_SERVER_NAME=127.0.0.1 |
| 720 |
export TPM_INTERFACE_TYPE=socsim |
| 721 |
export TPM_COMMAND_PORT=65533 |
| 722 |
export TPM_DATA_DIR=$TPMDIR |
| 723 |
export TPM_SESSION_ENCKEY="807e2bfe898ddaed8fa6310e716a24dc" # for sessions |
| 724 |
|
| 725 |
$SWTPM_EXE socket \ |
| 726 |
--server port=${TPM_COMMAND_PORT} \ |
| 727 |
--tpmstate "dir=$TPMDIR" \ |
| 728 |
--pid "file=$PID_FILE" \ |
| 729 |
--ctrl "type=unixio,path=$SOCK_PATH" \ |
| 730 |
--log "file=$LOGFILE,level=20" \ |
| 731 |
--tpm2 \ |
| 732 |
${SWTPM_TEST_SECCOMP_OPT:+${SWTPM_TEST_SECCOMP_OPT}} & |
| 733 |
|
| 734 |
if wait_for_file "$PID_FILE" 3; then |
| 735 |
echo "Error: (1) Socket TPM did not write pidfile." |
| 736 |
exit 1 |
| 737 |
fi |
| 738 |
|
| 739 |
PID="$(cat "$PID_FILE")" |
| 740 |
|
| 741 |
# Send TPM_Init |
| 742 |
if ! act=$($SWTPM_IOCTL --unix "$SOCK_PATH" -i 2>&1); then |
| 743 |
echo "Error: $SWTPM_IOCTL CMD_INIT failed: $act" |
| 744 |
exit 1 |
| 745 |
fi |
| 746 |
|
| 747 |
if ! "${TOOLSPATH}/${PREFIX}startup" -c; then |
| 748 |
echo "Error: tpm_startup clear failed." |
| 749 |
exit 1 |
| 750 |
fi |
| 751 |
|
| 752 |
test_nvram_state 1 1 |
| 753 |
test_primary 1 1 0 |
| 754 |
test_pcr_allocation 1 0 # can only check after reboot |
| 755 |
test_hierarchy 1 1 |
| 756 |
|
| 757 |
if ! "${TOOLSPATH}/${PREFIX}shutdown" -c; then |
| 758 |
echo "Error: tpm_shutdown clear failed." |
| 759 |
cat "$LOGFILE" |
| 760 |
exit 1 |
| 761 |
fi |
| 762 |
|
| 763 |
# Send Shutdown |
| 764 |
if ! act=$($SWTPM_IOCTL --unix "$SOCK_PATH" -s 2>&1); then |
| 765 |
echo "Error: $SWTPM_IOCTL CMD_SHUTDOWN failed: $act" |
| 766 |
exit 1 |
| 767 |
fi |
| 768 |
|
| 769 |
echo "============================" >> "$LOGFILE" |
| 770 |
|
| 771 |
echo "TPM was shut down" |
| 772 |
|
| 773 |
# Store this state for later usage |
| 774 |
# cp $TPMDIR/tpm2-00.permall ${TESTDIR}/data/tpm2state3; |
| 775 |
# cp $SIGFILE ${TESTDIR}/data/tpm2state3/signature.bin |
| 776 |
|
| 777 |
################################################################# |
| 778 |
# Run TPM2 with the created state and verify it's the same |
| 779 |
|
| 780 |
$SWTPM_EXE socket \ |
| 781 |
--server port=${TPM_COMMAND_PORT} \ |
| 782 |
--tpmstate "dir=$TPMDIR" \ |
| 783 |
--pid "file=$PID_FILE" \ |
| 784 |
--ctrl "type=unixio,path=$SOCK_PATH" \ |
| 785 |
--log "file=$LOGFILE,level=20" \ |
| 786 |
--tpm2 \ |
| 787 |
${SWTPM_TEST_SECCOMP_OPT:+${SWTPM_TEST_SECCOMP_OPT}} & |
| 788 |
|
| 789 |
if wait_for_file "$PID_FILE" 3; then |
| 790 |
echo "Error: (2) Socket TPM did not write pidfile." |
| 791 |
exit 1 |
| 792 |
fi |
| 793 |
|
| 794 |
echo "TPM re-started" |
| 795 |
|
| 796 |
PID="$(cat "$PID_FILE")" |
| 797 |
|
| 798 |
# Send TPM_Init |
| 799 |
if ! act=$($SWTPM_IOCTL --unix "$SOCK_PATH" -i 2>&1); then |
| 800 |
echo "Error: $SWTPM_IOCTL CMD_INIT failed: $act" |
| 801 |
exit 1 |
| 802 |
fi |
| 803 |
|
| 804 |
if ! "${TOOLSPATH}/${PREFIX}startup" -c; then |
| 805 |
echo "Error: tpm_startup clear failed." |
| 806 |
cat "$LOGFILE" |
| 807 |
exit 1 |
| 808 |
fi |
| 809 |
|
| 810 |
test_nvram_state 0 1 |
| 811 |
test_primary 0 1 0 |
| 812 |
test_pcr_allocation 0 1 |
| 813 |
test_hierarchy 0 1 |
| 814 |
|
| 815 |
if ! "${TOOLSPATH}/${PREFIX}shutdown" -c; then |
| 816 |
echo "Error: tpm_shutdown clear failed." |
| 817 |
cat "$LOGFILE" |
| 818 |
exit 1 |
| 819 |
fi |
| 820 |
|
| 821 |
# Send Shutdown |
| 822 |
if ! act=$($SWTPM_IOCTL --unix "$SOCK_PATH" -s 2>&1); then |
| 823 |
echo "Error: $SWTPM_IOCTL CMD_SHUTDOWN failed: $act" |
| 824 |
exit 1 |
| 825 |
fi |
| 826 |
|
| 827 |
echo "============================" >> "$LOGFILE" |
| 828 |
|
| 829 |
echo "TPM was shut down" |
| 830 |
|
| 831 |
################################################################# |
| 832 |
# Run TPM2 with previously saved state and verify it's the same |
| 833 |
|
| 834 |
rm -f "$TPMDIR/"* |
| 835 |
cp -f "${TESTDIR}/data/tpm2state3/tpm2-00.permall" "$TPMDIR/tpm2-00.permall" |
| 836 |
|
| 837 |
$SWTPM_EXE socket \ |
| 838 |
--server port=${TPM_COMMAND_PORT} \ |
| 839 |
--tpmstate "dir=$TPMDIR" \ |
| 840 |
--pid "file=$PID_FILE" \ |
| 841 |
--ctrl "type=unixio,path=$SOCK_PATH" \ |
| 842 |
--log "file=$LOGFILE,level=20" \ |
| 843 |
--tpm2 \ |
| 844 |
${SWTPM_TEST_SECCOMP_OPT:+${SWTPM_TEST_SECCOMP_OPT}} & |
| 845 |
|
| 846 |
if wait_for_file "$PID_FILE" 3; then |
| 847 |
echo "Error: (3) Socket TPM did not write pidfile." |
| 848 |
exit 1 |
| 849 |
fi |
| 850 |
|
| 851 |
echo "TPM started with previously generated state" |
| 852 |
|
| 853 |
PID="$(cat "$PID_FILE")" |
| 854 |
|
| 855 |
# Send TPM_Init |
| 856 |
if ! act=$($SWTPM_IOCTL --unix "$SOCK_PATH" -i 2>&1); then |
| 857 |
echo "Error: $SWTPM_IOCTL CMD_INIT failed: $act" |
| 858 |
exit 1 |
| 859 |
fi |
| 860 |
|
| 861 |
if ! "${TOOLSPATH}/${PREFIX}startup" -c; then |
| 862 |
echo "Error: tpm_startup clear failed." |
| 863 |
cat "$LOGFILE" |
| 864 |
exit 1 |
| 865 |
fi |
| 866 |
|
| 867 |
test_nvram_state 0 1 |
| 868 |
test_primary 0 1 1 |
| 869 |
test_pcr_allocation 0 1 |
| 870 |
test_hierarchy 0 1 |
| 871 |
|
| 872 |
if ! "${TOOLSPATH}/${PREFIX}shutdown" -c; then |
| 873 |
echo "Error: tpm_shutdown clear failed." |
| 874 |
cat "$LOGFILE" |
| 875 |
exit 1 |
| 876 |
fi |
| 877 |
|
| 878 |
# Send Shutdown |
| 879 |
if ! act=$($SWTPM_IOCTL --unix "$SOCK_PATH" -s 2>&1); then |
| 880 |
echo "Error: $SWTPM_IOCTL CMD_SHUTDOWN failed: $act" |
| 881 |
exit 1 |
| 882 |
fi |
| 883 |
|
| 884 |
echo "Test 1 OK" |
| 885 |
|
| 886 |
# |
| 887 |
# |
| 888 |
# Tests with volatile state |
| 889 |
# |
| 890 |
# |
| 891 |
|
| 892 |
rm -f "${TPMDIR}"/* |
| 893 |
|
| 894 |
$SWTPM_EXE socket \ |
| 895 |
--server port=${TPM_COMMAND_PORT} \ |
| 896 |
--tpmstate "dir=$TPMDIR" \ |
| 897 |
--pid "file=$PID_FILE" \ |
| 898 |
--ctrl "type=unixio,path=$SOCK_PATH" \ |
| 899 |
--log "file=$LOGFILE,level=20" \ |
| 900 |
--tpm2 \ |
| 901 |
${SWTPM_TEST_SECCOMP_OPT:+${SWTPM_TEST_SECCOMP_OPT}} & |
| 902 |
|
| 903 |
if wait_for_file "$PID_FILE" 3; then |
| 904 |
echo "Error: (3) Socket TPM did not write pidfile." |
| 905 |
exit 1 |
| 906 |
fi |
| 907 |
|
| 908 |
PID="$(cat "$PID_FILE")" |
| 909 |
|
| 910 |
# Send TPM_Init |
| 911 |
if ! act=$($SWTPM_IOCTL --unix "$SOCK_PATH" -i 2>&1); then |
| 912 |
echo "Error: $SWTPM_IOCTL CMD_INIT failed: $act" |
| 913 |
exit 1 |
| 914 |
fi |
| 915 |
|
| 916 |
if ! "${TOOLSPATH}/${PREFIX}startup" -c; then |
| 917 |
echo "Error: tpm_startup clear failed." |
| 918 |
cat "$LOGFILE" |
| 919 |
exit 1 |
| 920 |
fi |
| 921 |
|
| 922 |
test_session 1 1 |
| 923 |
test_hash_context 1 0 |
| 924 |
|
| 925 |
if ! act=$($SWTPM_IOCTL --unix "$SOCK_PATH" -v 2>&1); then |
| 926 |
echo "Error: $SWTPM_IOCTL CMD_STORE_VOLATILE failed: $act" |
| 927 |
exit 1 |
| 928 |
fi |
| 929 |
|
| 930 |
if ! "${TOOLSPATH}/${PREFIX}shutdown" -c; then |
| 931 |
echo "Error: tpm_shutdown clear failed." |
| 932 |
cat "$LOGFILE" |
| 933 |
exit 1 |
| 934 |
fi |
| 935 |
|
| 936 |
# Send Shutdown |
| 937 |
if ! act=$($SWTPM_IOCTL --unix "$SOCK_PATH" -s 2>&1); then |
| 938 |
echo "Error: $SWTPM_IOCTL CMD_SHUTDOWN failed: $act" |
| 939 |
exit 1 |
| 940 |
fi |
| 941 |
|
| 942 |
################################################################# |
| 943 |
# Run TPM2 with the saved volatile state |
| 944 |
|
| 945 |
# create a backup for running the next test... |
| 946 |
# cp $TPMDIR/tpm2-00.permall ${TESTDIR}/data/tpm2state3b/tpm2-00.permall |
| 947 |
# cp $TPMDIR/tpm2-00.volatilestate ${TESTDIR}/data/tpm2state3b/tpm2-00.volatilestate |
| 948 |
# cp $TPMDIR/h02000000.bin ${TESTDIR}/data/tpm2state3b/h02000000.bin |
| 949 |
# cp $TPMDIR/h81000000.bin ${TESTDIR}/data/tpm2state3b/h81000000.bin |
| 950 |
|
| 951 |
$SWTPM_EXE socket \ |
| 952 |
--server port=${TPM_COMMAND_PORT} \ |
| 953 |
--tpmstate "dir=$TPMDIR" \ |
| 954 |
--pid "file=$PID_FILE" \ |
| 955 |
--ctrl "type=unixio,path=$SOCK_PATH" \ |
| 956 |
--log "file=$LOGFILE,level=20" \ |
| 957 |
--tpm2 \ |
| 958 |
${SWTPM_TEST_SECCOMP_OPT:+${SWTPM_TEST_SECCOMP_OPT}} & |
| 959 |
|
| 960 |
if wait_for_file "$PID_FILE" 3; then |
| 961 |
echo "Error: (3) Socket TPM did not write pidfile." |
| 962 |
exit 1 |
| 963 |
fi |
| 964 |
|
| 965 |
PID="$(cat "$PID_FILE")" |
| 966 |
|
| 967 |
# Send TPM_Init |
| 968 |
if ! act=$($SWTPM_IOCTL --unix "$SOCK_PATH" -i 2>&1); then |
| 969 |
cat "$LOGFILE" |
| 970 |
echo "Error: $SWTPM_IOCTL CMD_INIT failed: $act" |
| 971 |
exit 1 |
| 972 |
fi |
| 973 |
|
| 974 |
test_hash_context 0 1 |
| 975 |
test_session 0 1 |
| 976 |
|
| 977 |
if ! "${TOOLSPATH}/${PREFIX}shutdown" -c; then |
| 978 |
echo "Error: tpm_shutdown clear failed." |
| 979 |
cat "$LOGFILE" |
| 980 |
exit 1 |
| 981 |
fi |
| 982 |
|
| 983 |
# Send Shutdown |
| 984 |
if ! act=$($SWTPM_IOCTL --unix "$SOCK_PATH" -s 2>&1); then |
| 985 |
echo "Error: $SWTPM_IOCTL CMD_SHUTDOWN failed: $act" |
| 986 |
exit 1 |
| 987 |
fi |
| 988 |
|
| 989 |
##################################################################### |
| 990 |
# Run TPM2 with previously saved (volatile) state and verify it's |
| 991 |
# working as well |
| 992 |
|
| 993 |
cp -f "${TESTDIR}/data/tpm2state3b/tpm2-00.permall" "$TPMDIR/tpm2-00.permall" |
| 994 |
cp -f "${TESTDIR}/data/tpm2state3b/tpm2-00.volatilestate" "$TPMDIR/tpm2-00.volatilestate" |
| 995 |
cp -f "${TESTDIR}/data/tpm2state3b/h02000000.bin" "$TPMDIR/h02000000.bin" |
| 996 |
cp -f "${TESTDIR}/data/tpm2state3b/h81000000.bin" "$TPMDIR/h81000000.bin" |
| 997 |
|
| 998 |
$SWTPM_EXE socket \ |
| 999 |
--server port=${TPM_COMMAND_PORT} \ |
| 1000 |
--tpmstate "dir=$TPMDIR" \ |
| 1001 |
--pid "file=$PID_FILE" \ |
| 1002 |
--ctrl "type=unixio,path=$SOCK_PATH" \ |
| 1003 |
--log "file=$LOGFILE,level=20" \ |
| 1004 |
--tpm2 \ |
| 1005 |
${SWTPM_TEST_SECCOMP_OPT:+${SWTPM_TEST_SECCOMP_OPT}} & |
| 1006 |
|
| 1007 |
if wait_for_file "$PID_FILE" 3; then |
| 1008 |
echo "Error: (3) Socket TPM did not write pidfile." |
| 1009 |
exit 1 |
| 1010 |
fi |
| 1011 |
|
| 1012 |
echo "TPM started with previously generated state" |
| 1013 |
|
| 1014 |
PID="$(cat "$PID_FILE")" |
| 1015 |
|
| 1016 |
# Send TPM_Init |
| 1017 |
if ! act=$($SWTPM_IOCTL --unix "$SOCK_PATH" -i 2>&1); then |
| 1018 |
echo "Error: $SWTPM_IOCTL CMD_INIT failed: $act" |
| 1019 |
tail -n 10 "$LOGFILE" |
| 1020 |
exit 1 |
| 1021 |
fi |
| 1022 |
|
| 1023 |
test_hash_context 0 1 |
| 1024 |
test_session 0 1 |
| 1025 |
|
| 1026 |
# Send Shutdown |
| 1027 |
if ! act=$($SWTPM_IOCTL --unix "$SOCK_PATH" -s 2>&1); then |
| 1028 |
echo "Error: $SWTPM_IOCTL CMD_SHUTDOWN failed: $act" |
| 1029 |
exit 1 |
| 1030 |
fi |
| 1031 |
|
| 1032 |
echo "Test 2 OK" |
| 1033 |
|
| 1034 |
# |
| 1035 |
# |
| 1036 |
# Tests with volatile state -- 2nd test |
| 1037 |
# |
| 1038 |
# |
| 1039 |
|
| 1040 |
rm -f "${TPMDIR}"/* |
| 1041 |
|
| 1042 |
$SWTPM_EXE socket \ |
| 1043 |
--server port=${TPM_COMMAND_PORT} \ |
| 1044 |
--tpmstate "dir=$TPMDIR" \ |
| 1045 |
--pid "file=$PID_FILE" \ |
| 1046 |
--ctrl "type=unixio,path=$SOCK_PATH" \ |
| 1047 |
--log "file=$LOGFILE,level=20" \ |
| 1048 |
--tpm2 \ |
| 1049 |
${SWTPM_TEST_SECCOMP_OPT:+${SWTPM_TEST_SECCOMP_OPT}} & |
| 1050 |
|
| 1051 |
if wait_for_file "$PID_FILE" 3; then |
| 1052 |
echo "Error: (3) Socket TPM did not write pidfile." |
| 1053 |
exit 1 |
| 1054 |
fi |
| 1055 |
|
| 1056 |
PID="$(cat "$PID_FILE")" |
| 1057 |
|
| 1058 |
# Send TPM_Init |
| 1059 |
if ! act=$($SWTPM_IOCTL --unix "$SOCK_PATH" -i 2>&1); then |
| 1060 |
echo "Error: $SWTPM_IOCTL CMD_INIT failed: $act" |
| 1061 |
exit 1 |
| 1062 |
fi |
| 1063 |
|
| 1064 |
if ! "${TOOLSPATH}/${PREFIX}startup" -c; then |
| 1065 |
echo "Error: tpm_startup clear failed." |
| 1066 |
cat "$LOGFILE" |
| 1067 |
exit 1 |
| 1068 |
fi |
| 1069 |
|
| 1070 |
# we only run this to generate the AES key which is different every time... |
| 1071 |
# test_hmac_context 1 0 |
| 1072 |
|
| 1073 |
if ! act=$($SWTPM_IOCTL --unix "$SOCK_PATH" -v 2>&1); then |
| 1074 |
echo "Error: $SWTPM_IOCTL CMD_STORE_VOLATILE failed: $act" |
| 1075 |
exit 1 |
| 1076 |
fi |
| 1077 |
|
| 1078 |
if ! "${TOOLSPATH}/${PREFIX}shutdown" -c; then |
| 1079 |
echo "Error: tpm_shutdown clear failed." |
| 1080 |
cat "$LOGFILE" |
| 1081 |
exit 1 |
| 1082 |
fi |
| 1083 |
|
| 1084 |
# Send Shutdown |
| 1085 |
if ! act=$($SWTPM_IOCTL --unix "$SOCK_PATH" -s 2>&1); then |
| 1086 |
echo "Error: $SWTPM_IOCTL CMD_SHUTDOWN failed: $act" |
| 1087 |
exit 1 |
| 1088 |
fi |
| 1089 |
|
| 1090 |
################################################################# |
| 1091 |
# Run TPM2 with the saved volatile state |
| 1092 |
|
| 1093 |
# create a backup for running the next test... |
| 1094 |
# cp $TPMDIR/tpm2-00.permall ${TESTDIR}/data/tpm2state3c/tpm2-00.permall |
| 1095 |
# cp $TPMDIR/tpm2-00.volatilestate ${TESTDIR}/data/tpm2state3c/tpm2-00.volatilestate |
| 1096 |
|
| 1097 |
#echo $TPMDIR |
| 1098 |
#ls -l $TPMDIR |
| 1099 |
$SWTPM_EXE socket \ |
| 1100 |
--server port=${TPM_COMMAND_PORT} \ |
| 1101 |
--tpmstate "dir=$TPMDIR" \ |
| 1102 |
--pid "file=$PID_FILE" \ |
| 1103 |
--ctrl "type=unixio,path=$SOCK_PATH" \ |
| 1104 |
--log "file=$LOGFILE,level=20" \ |
| 1105 |
--tpm2 \ |
| 1106 |
${SWTPM_TEST_SECCOMP_OPT:+${SWTPM_TEST_SECCOMP_OPT}} & |
| 1107 |
|
| 1108 |
if wait_for_file "$PID_FILE" 3; then |
| 1109 |
echo "Error: (3) Socket TPM did not write pidfile." |
| 1110 |
exit 1 |
| 1111 |
fi |
| 1112 |
|
| 1113 |
PID="$(cat "$PID_FILE")" |
| 1114 |
|
| 1115 |
# Send TPM_Init |
| 1116 |
if ! act=$($SWTPM_IOCTL --unix "$SOCK_PATH" -i 2>&1); then |
| 1117 |
cat "$LOGFILE" |
| 1118 |
echo "Error: $SWTPM_IOCTL CMD_INIT failed: $act" |
| 1119 |
exit 1 |
| 1120 |
fi |
| 1121 |
|
| 1122 |
# since the AES key is different every time, we cannot run |
| 1123 |
# the HMAC function that's using it since the result would |
| 1124 |
# be different every time |
| 1125 |
# test_hmac_context 0 1 |
| 1126 |
|
| 1127 |
if ! "${TOOLSPATH}/${PREFIX}shutdown" -c; then |
| 1128 |
echo "Error: tpm_shutdown clear failed." |
| 1129 |
cat "$LOGFILE" |
| 1130 |
exit 1 |
| 1131 |
fi |
| 1132 |
|
| 1133 |
# Send Shutdown |
| 1134 |
if ! act=$($SWTPM_IOCTL --unix "$SOCK_PATH" -s 2>&1); then |
| 1135 |
echo "Error: $SWTPM_IOCTL CMD_SHUTDOWN failed: $act" |
| 1136 |
exit 1 |
| 1137 |
fi |
| 1138 |
|
| 1139 |
##################################################################### |
| 1140 |
# Run TPM2 with previously saved (volatile) state and verify it's |
| 1141 |
# working as well |
| 1142 |
|
| 1143 |
cp -f "${TESTDIR}/data/tpm2state3c/tpm2-00.volatilestate" "$TPMDIR/tpm2-00.volatilestate" |
| 1144 |
cp -f "${TESTDIR}/data/tpm2state3c/tpm2-00.permall" "$TPMDIR/tpm2-00.permall" |
| 1145 |
|
| 1146 |
$SWTPM_EXE socket \ |
| 1147 |
--server port=${TPM_COMMAND_PORT} \ |
| 1148 |
--tpmstate "dir=$TPMDIR" \ |
| 1149 |
--pid "file=$PID_FILE" \ |
| 1150 |
--ctrl "type=unixio,path=$SOCK_PATH" \ |
| 1151 |
--log "file=$LOGFILE,level=20" \ |
| 1152 |
--tpm2 \ |
| 1153 |
${SWTPM_TEST_SECCOMP_OPT:+${SWTPM_TEST_SECCOMP_OPT}} & |
| 1154 |
|
| 1155 |
if wait_for_file "$PID_FILE" 3; then |
| 1156 |
echo "Error: (3) Socket TPM did not write pidfile." |
| 1157 |
exit 1 |
| 1158 |
fi |
| 1159 |
|
| 1160 |
echo "TPM started with previously generated state" |
| 1161 |
|
| 1162 |
PID="$(cat "$PID_FILE")" |
| 1163 |
|
| 1164 |
# Send TPM_Init |
| 1165 |
if ! act=$($SWTPM_IOCTL --unix "$SOCK_PATH" -i 2>&1); then |
| 1166 |
echo "Error: $SWTPM_IOCTL CMD_INIT failed: $act" |
| 1167 |
exit 1 |
| 1168 |
fi |
| 1169 |
|
| 1170 |
test_hmac_context 0 1 |
| 1171 |
|
| 1172 |
# Send Shutdown |
| 1173 |
if ! act=$($SWTPM_IOCTL --unix "$SOCK_PATH" -s 2>&1); then |
| 1174 |
echo "Error: $SWTPM_IOCTL CMD_SHUTDOWN failed: $act" |
| 1175 |
exit 1 |
| 1176 |
fi |
| 1177 |
|
| 1178 |
echo "Test 3 OK" |
| 1179 |
|
| 1180 |
|
| 1181 |
# |
| 1182 |
# |
| 1183 |
# Tests with volatile state -- 3rd test |
| 1184 |
# |
| 1185 |
# |
| 1186 |
|
| 1187 |
rm -f "${TPMDIR}"/* |
| 1188 |
|
| 1189 |
$SWTPM_EXE socket \ |
| 1190 |
--server port=${TPM_COMMAND_PORT} \ |
| 1191 |
--tpmstate "dir=$TPMDIR" \ |
| 1192 |
--pid "file=$PID_FILE" \ |
| 1193 |
--ctrl "type=unixio,path=$SOCK_PATH" \ |
| 1194 |
--log "file=$LOGFILE,level=20" \ |
| 1195 |
--tpm2 \ |
| 1196 |
${SWTPM_TEST_SECCOMP_OPT:+${SWTPM_TEST_SECCOMP_OPT}} & |
| 1197 |
|
| 1198 |
if wait_for_file "$PID_FILE" 3; then |
| 1199 |
echo "Error: (3) Socket TPM did not write pidfile." |
| 1200 |
exit 1 |
| 1201 |
fi |
| 1202 |
|
| 1203 |
PID="$(cat "$PID_FILE")" |
| 1204 |
|
| 1205 |
# Send TPM_Init |
| 1206 |
if ! act=$($SWTPM_IOCTL --unix "$SOCK_PATH" -i 2>&1); then |
| 1207 |
echo "Error: $SWTPM_IOCTL CMD_INIT failed: $act" |
| 1208 |
exit 1 |
| 1209 |
fi |
| 1210 |
|
| 1211 |
if ! "${TOOLSPATH}/${PREFIX}startup" -c; then |
| 1212 |
echo "Error: tpm_startup clear failed." |
| 1213 |
cat "$LOGFILE" |
| 1214 |
exit 1 |
| 1215 |
fi |
| 1216 |
|
| 1217 |
test_primary_volatile_load 1 0 0 |
| 1218 |
test_external_key 1 1 |
| 1219 |
|
| 1220 |
if ! act=$($SWTPM_IOCTL --unix "$SOCK_PATH" -v 2>&1); then |
| 1221 |
echo "Error: $SWTPM_IOCTL CMD_STORE_VOLATILE failed: $act" |
| 1222 |
exit 1 |
| 1223 |
fi |
| 1224 |
|
| 1225 |
if ! "${TOOLSPATH}/${PREFIX}shutdown" -c; then |
| 1226 |
echo "Error: tpm_shutdown clear failed." |
| 1227 |
cat "$LOGFILE" |
| 1228 |
exit 1 |
| 1229 |
fi |
| 1230 |
|
| 1231 |
# Send Shutdown |
| 1232 |
if ! act=$($SWTPM_IOCTL --unix "$SOCK_PATH" -s 2>&1); then |
| 1233 |
echo "Error: $SWTPM_IOCTL CMD_SHUTDOWN failed: $act" |
| 1234 |
exit 1 |
| 1235 |
fi |
| 1236 |
|
| 1237 |
################################################################# |
| 1238 |
# Run TPM2 with the saved volatile state |
| 1239 |
|
| 1240 |
# create a backup for running the next test... |
| 1241 |
# cp $TPMDIR/tpm2-00.volatilestate ${TESTDIR}/data/tpm2state3d/tpm2-00.volatilestate |
| 1242 |
# cp $TPMDIR/tpm2-00.permall ${TESTDIR}/data/tpm2state3d/tpm2-00.permall |
| 1243 |
# cp $SIGFILE ${TESTDIR}/data/tpm2state3d/signature2.bin |
| 1244 |
|
| 1245 |
#echo $TPMDIR |
| 1246 |
#ls -l $TPMDIR |
| 1247 |
$SWTPM_EXE socket \ |
| 1248 |
--server port=${TPM_COMMAND_PORT} \ |
| 1249 |
--tpmstate "dir=$TPMDIR" \ |
| 1250 |
--pid "file=$PID_FILE" \ |
| 1251 |
--ctrl "type=unixio,path=$SOCK_PATH" \ |
| 1252 |
--log "file=$LOGFILE,level=20" \ |
| 1253 |
--tpm2 \ |
| 1254 |
${SWTPM_TEST_SECCOMP_OPT:+${SWTPM_TEST_SECCOMP_OPT}} & |
| 1255 |
|
| 1256 |
if wait_for_file "$PID_FILE" 3; then |
| 1257 |
echo "Error: (3) Socket TPM did not write pidfile." |
| 1258 |
exit 1 |
| 1259 |
fi |
| 1260 |
|
| 1261 |
PID="$(cat "$PID_FILE")" |
| 1262 |
|
| 1263 |
# Send TPM_Init |
| 1264 |
if ! act=$($SWTPM_IOCTL --unix "$SOCK_PATH" -i 2>&1); then |
| 1265 |
cat "$LOGFILE" |
| 1266 |
echo "Error: $SWTPM_IOCTL CMD_INIT failed: $act" |
| 1267 |
exit 1 |
| 1268 |
fi |
| 1269 |
|
| 1270 |
test_primary_volatile_load 0 1 0 |
| 1271 |
test_external_key 0 1 |
| 1272 |
|
| 1273 |
if ! "${TOOLSPATH}/${PREFIX}shutdown" -c; then |
| 1274 |
echo "Error: tpm_shutdown clear failed." |
| 1275 |
cat "$LOGFILE" |
| 1276 |
exit 1 |
| 1277 |
fi |
| 1278 |
|
| 1279 |
# Send Shutdown |
| 1280 |
if ! act=$($SWTPM_IOCTL --unix "$SOCK_PATH" -s 2>&1); then |
| 1281 |
echo "Error: $SWTPM_IOCTL CMD_SHUTDOWN failed: $act" |
| 1282 |
exit 1 |
| 1283 |
fi |
| 1284 |
|
| 1285 |
##################################################################### |
| 1286 |
# Run TPM2 with previously saved (volatile) state and verify it's |
| 1287 |
# working as well |
| 1288 |
|
| 1289 |
cp -f "${TESTDIR}/data/tpm2state3d/tpm2-00.permall" "$TPMDIR/tpm2-00.permall" |
| 1290 |
cp -f "${TESTDIR}/data/tpm2state3d/tpm2-00.volatilestate" "$TPMDIR/tpm2-00.volatilestate" |
| 1291 |
|
| 1292 |
$SWTPM_EXE socket \ |
| 1293 |
--server port=${TPM_COMMAND_PORT} \ |
| 1294 |
--tpmstate "dir=$TPMDIR" \ |
| 1295 |
--pid "file=$PID_FILE" \ |
| 1296 |
--ctrl "type=unixio,path=$SOCK_PATH" \ |
| 1297 |
--log "file=$LOGFILE,level=20" \ |
| 1298 |
--tpm2 \ |
| 1299 |
${SWTPM_TEST_SECCOMP_OPT:+${SWTPM_TEST_SECCOMP_OPT}} & |
| 1300 |
|
| 1301 |
if wait_for_file "$PID_FILE" 3; then |
| 1302 |
echo "Error: (3) Socket TPM did not write pidfile." |
| 1303 |
exit 1 |
| 1304 |
fi |
| 1305 |
|
| 1306 |
echo "TPM started with previously generated state" |
| 1307 |
|
| 1308 |
PID="$(cat "$PID_FILE")" |
| 1309 |
|
| 1310 |
# Send TPM_Init |
| 1311 |
if ! act=$($SWTPM_IOCTL --unix "$SOCK_PATH" -i 2>&1); then |
| 1312 |
echo "Error: $SWTPM_IOCTL CMD_INIT failed: $act" |
| 1313 |
exit 1 |
| 1314 |
fi |
| 1315 |
|
| 1316 |
test_primary_volatile_load 0 1 1 |
| 1317 |
test_external_key 0 1 |
| 1318 |
# Create the orderly nv indices and have them cleared (for coverage) |
| 1319 |
test_nvram_state 1 1 |
| 1320 |
|
| 1321 |
if ! "${TOOLSPATH}/${PREFIX}clear" -hi p; then |
| 1322 |
echo "Error: clear failed." |
| 1323 |
cat "$LOGFILE" |
| 1324 |
exit 1 |
| 1325 |
fi |
| 1326 |
|
| 1327 |
# Send Shutdown |
| 1328 |
if ! act=$($SWTPM_IOCTL --unix "$SOCK_PATH" -s 2>&1); then |
| 1329 |
echo "Error: $SWTPM_IOCTL CMD_SHUTDOWN failed: $act" |
| 1330 |
exit 1 |
| 1331 |
fi |
| 1332 |
|
| 1333 |
echo "Test 4 OK" |
| 1334 |
|
| 1335 |
exit 0 |