| 1 |
#!/usr/bin/env bash |
| 2 |
#set -x |
| 3 |
|
| 4 |
# shellcheck disable=SC2090 |
| 5 |
|
| 6 |
# For the license, see the LICENSE file in the root directory. |
| 7 |
|
| 8 |
# This test case builds various versions of libtpms + swtpm and generates |
| 9 |
# TPM 2 state using swtpm_setup with profiles, if supported. After that |
| 10 |
# it starts the built versions of swtpm and tries to use the state that |
| 11 |
# was created possibly by another version of libtpms and runs tests with it. |
| 12 |
# Currently this only works with the null profile whose state can be created |
| 13 |
# and read either with libtpms v0.9 or v0.10. In the future tests can be |
| 14 |
# covering the default-v1 profile as well that must be exchageable with |
| 15 |
# libtpms >v0.10. |
| 16 |
|
| 17 |
if [ "${SWTPM_TEST_EXPENSIVE:-0}" -eq 0 ]; then |
| 18 |
echo "SWTPM_TEST_EXPENSIVE must be set to run this test." |
| 19 |
exit 77 |
| 20 |
fi |
| 21 |
|
| 22 |
if [ "${SWTPM_TEST_IBMTSS2:-0}" -eq 0 ]; then |
| 23 |
echo "SWTPM_TEST_IBMTSS2 must be set to run this test." |
| 24 |
exit 77 |
| 25 |
fi |
| 26 |
|
| 27 |
ROOT=${abs_top_builddir:-$(dirname "$0")/..} |
| 28 |
TESTDIR=${abs_top_testdir:-$(dirname "$0")} |
| 29 |
SRCDIR=${abs_top_srcdir:-$(dirname "$0")/..} |
| 30 |
|
| 31 |
source "${TESTDIR}/common" |
| 32 |
|
| 33 |
workdir="$(mktemp -d)" || exit 1 |
| 34 |
export TPM_PATH=${workdir} |
| 35 |
SWTPM_SERVER_PORT=65478 |
| 36 |
SWTPM_CTRL_PORT=65479 |
| 37 |
SWTPM_SERVER_NAME=127.0.0.1 |
| 38 |
SWTPM_INTERFACE="socket+socket" |
| 39 |
|
| 40 |
LIBTPMS_URL=https://github.com/stefanberger/libtpms |
| 41 |
LIBTPMS_INITIAL_BRANCH=master |
| 42 |
|
| 43 |
SWTPM_URL=https://github.com/stefanberger/swtpm |
| 44 |
SWTPM_DEFAULT_BRANCH=master # during development change to local branch |
| 45 |
|
| 46 |
cat <<_EOF_ > "${workdir}/swtpm-localca.options" |
| 47 |
--tpm-manufacturer IBM |
| 48 |
--tpm-model swtpm-libtpms |
| 49 |
--tpm-version 2 |
| 50 |
--platform-manufacturer "Fedora XYZ" |
| 51 |
--platform-version 2.1 |
| 52 |
--platform-model "QEMU A.B" |
| 53 |
_EOF_ |
| 54 |
|
| 55 |
cat <<_EOF_ > "${workdir}/swtpm_setup.conf" |
| 56 |
create_certs_tool=\${SWTPM_LOCALCA} |
| 57 |
create_certs_tool_config=${workdir}/swtpm-localca.conf |
| 58 |
create_certs_tool_options=${workdir}/swtpm-localca.options |
| 59 |
_EOF_ |
| 60 |
|
| 61 |
# Copy swtpm source tree to workdir |
| 62 |
pushd "${SRCDIR}" &>/dev/null || exit 1 |
| 63 |
|
| 64 |
if [ ! -d ".git" ]; then |
| 65 |
echo "SKIP: This test must be run from a git checkout" |
| 66 |
exit 77 |
| 67 |
fi |
| 68 |
|
| 69 |
mkdir -p "${workdir}/swtpm" |
| 70 |
cp -rp . "${workdir}/swtpm" |
| 71 |
cd "${workdir}/swtpm" || exit 1 |
| 72 |
chmod -R 0755 . # when using 'distcheck' |
| 73 |
|
| 74 |
if [ -n "$(git status --porcelain --untracked-files=no)" ]; then |
| 75 |
# store all current modifications in a temp patch |
| 76 |
git config --local user.name test |
| 77 |
git config --local user.email [email protected] |
| 78 |
git add --all . >/dev/null |
| 79 |
git commit -m "temp" >/dev/null |
| 80 |
fi |
| 81 |
|
| 82 |
git clean -xdf &>/dev/null |
| 83 |
popd &>/dev/null || exit 1 |
| 84 |
|
| 85 |
function cleanup() |
| 86 |
{ |
| 87 |
rm -rf "${workdir}" |
| 88 |
|
| 89 |
if kill_quiet -0 "${SWTPM_PID}"; then |
| 90 |
kill_quiet -9 "${SWTPM_PID}" |
| 91 |
fi |
| 92 |
} |
| 93 |
|
| 94 |
trap "cleanup" SIGTERM EXIT |
| 95 |
|
| 96 |
function nv_storefile() |
| 97 |
{ |
| 98 |
local nvindex="$1" |
| 99 |
local filename="$2" |
| 100 |
|
| 101 |
local sz |
| 102 |
|
| 103 |
if ! sz=$(get_filesize "${filename}"); then |
| 104 |
return 1 |
| 105 |
fi |
| 106 |
|
| 107 |
if ! tssnvdefinespace -ha "${nvindex}" -hi o -pwdn nv -sz "${sz}" 1>/dev/null; then |
| 108 |
return 1 |
| 109 |
fi |
| 110 |
|
| 111 |
if ! tssnvwrite -ha "${nvindex}" -pwdn nv -if "${filename}"; then |
| 112 |
return 1 |
| 113 |
fi |
| 114 |
return 0 |
| 115 |
} |
| 116 |
|
| 117 |
function nv_savetofile() |
| 118 |
{ |
| 119 |
local nvindex="$1" |
| 120 |
local filename="$2" |
| 121 |
|
| 122 |
local sz |
| 123 |
|
| 124 |
if ! tssnvread -ha "${nvindex}" -pwdn nv -of "${filename}" >/dev/null; then |
| 125 |
return 1 |
| 126 |
fi |
| 127 |
sz=$(get_filesize "${filename}") |
| 128 |
if [ "${sz}" -eq 0 ]; then |
| 129 |
# older versions required the -sz parameter |
| 130 |
sz=$(tssnvreadpublic -ha "${nvindex}" | sed -n 's/.*data size //p') |
| 131 |
if ! tssnvread -ha "${nvindex}" -pwdn nv -sz "${sz}" -of "${filename}" >/dev/null; then |
| 132 |
return 1 |
| 133 |
fi |
| 134 |
fi |
| 135 |
|
| 136 |
return 0 |
| 137 |
} |
| 138 |
|
| 139 |
function create_tpm_state() |
| 140 |
{ |
| 141 |
local workdir="$1" |
| 142 |
|
| 143 |
local inputfile="${workdir}/input.txt" |
| 144 |
local encfile="${workdir}/enc" |
| 145 |
local signature="${workdir}/signature.bin" |
| 146 |
local contextfile="${workdir}/context.bin" |
| 147 |
local aespub="${workdir}/aespub.bin" |
| 148 |
local aespriv="${workdir}/aespriv.bin" |
| 149 |
local cc rsasize ecdsaparam |
| 150 |
|
| 151 |
echo "input" > "${inputfile}" |
| 152 |
|
| 153 |
export TPM_COMMAND_PORT=${SWTPM_SERVER_PORT} |
| 154 |
export TPM_PLATFORM_PORT=${SWTPM_CTRL_PORT} |
| 155 |
export TPM_SERVER_NAME=${SWTPM_SERVER_NAME} |
| 156 |
export TPM_INTERFACE_TYPE=socsim |
| 157 |
export TPM_SERVER_TYPE=raw |
| 158 |
|
| 159 |
if tsscreateprimary --help | grep rsa | grep -q keybits; then |
| 160 |
rsasize="3072" |
| 161 |
fi |
| 162 |
|
| 163 |
# Test signing with RSA 3072 key |
| 164 |
if ! tsscreateprimary -rsa ${rsasize:+${rsasize}} -hi o -pwdk ooo -si 1>/dev/null; then |
| 165 |
return 1 |
| 166 |
fi |
| 167 |
|
| 168 |
if ! tsssign -hk 80000000 -pwdk ooo -if "${inputfile}" -os "${signature}"; then |
| 169 |
return 1 |
| 170 |
fi |
| 171 |
|
| 172 |
if ! nv_storefile 01000000 "${signature}"; then |
| 173 |
return 1 |
| 174 |
fi |
| 175 |
|
| 176 |
# Save the key as contextfile |
| 177 |
if ! tsscontextsave -ha 80000000 -of "${contextfile}" 1>/dev/null; then |
| 178 |
return 1 |
| 179 |
fi |
| 180 |
|
| 181 |
if ! nv_storefile 01000001 "${contextfile}"; then |
| 182 |
return 1 |
| 183 |
fi |
| 184 |
|
| 185 |
# Flush all keys |
| 186 |
if ! tssflushcontext -ha 80000000; then |
| 187 |
return 1 |
| 188 |
fi |
| 189 |
|
| 190 |
# Test HMAC |
| 191 |
# tsssign -salg was added in a later version only; Ubuntu Jammy does not have it |
| 192 |
if tsssign -h | grep -q "salg"; then |
| 193 |
if ! tsscreateprimary -rsa ${rsasize:+${rsasize}} -hi o -pwdk ooo 1>/dev/null; then |
| 194 |
return 1 |
| 195 |
fi |
| 196 |
|
| 197 |
if ! tsscreate -hp 80000000 -pwdp ooo -kh -kt f -kt p -opu "${aespub}" -opr "${aespriv}" -halg sha256; then |
| 198 |
return 1 |
| 199 |
fi |
| 200 |
|
| 201 |
if ! nv_storefile 01000002 "${aespub}" || ! nv_storefile 01000003 "${aespriv}" ; then |
| 202 |
return 1 |
| 203 |
fi |
| 204 |
|
| 205 |
if ! tssload -hp 80000000 -pwdp ooo -ipu "${aespub}" -ipr "${aespriv}" 1>/dev/null; then |
| 206 |
return 1 |
| 207 |
fi |
| 208 |
|
| 209 |
if ! tsssign -hk 80000001 -if "${inputfile}" -salg hmac -scheme hmac -os "${signature}"; then |
| 210 |
return 1 |
| 211 |
fi |
| 212 |
|
| 213 |
if ! nv_storefile 01000004 "${signature}"; then |
| 214 |
return 1 |
| 215 |
fi |
| 216 |
|
| 217 |
if ! tsscontextsave -ha 80000000 -of "${contextfile}" 1>/dev/null || \ |
| 218 |
! nv_storefile 01000005 "${contextfile}" || \ |
| 219 |
! tsscontextsave -ha 80000001 -of "${contextfile}" 1>/dev/null || \ |
| 220 |
! nv_storefile 01000006 "${contextfile}"; then |
| 221 |
return 1 |
| 222 |
fi |
| 223 |
|
| 224 |
# Flush all keys |
| 225 |
if ! tssflushcontext -ha 80000000 || ! tssflushcontext -ha 80000001; then |
| 226 |
return 1 |
| 227 |
fi |
| 228 |
fi |
| 229 |
|
| 230 |
# Encryption/Decryption |
| 231 |
if ! tsscreateprimary -hi o -des 1>/dev/null; then |
| 232 |
return 1 |
| 233 |
fi |
| 234 |
|
| 235 |
if ! tssencryptdecrypt -hk 80000000 -if "${inputfile}" -of "${encfile}"; then |
| 236 |
return 1 |
| 237 |
fi |
| 238 |
|
| 239 |
if ! nv_storefile 01000007 "${encfile}"; then |
| 240 |
return 1 |
| 241 |
fi |
| 242 |
|
| 243 |
if ! tsscontextsave -ha 80000000 -of "${contextfile}" 1>/dev/null || \ |
| 244 |
! nv_storefile 01000008 "${contextfile}"; then |
| 245 |
return 1 |
| 246 |
fi |
| 247 |
|
| 248 |
if ! tssflushcontext -ha 80000000; then |
| 249 |
return 1 |
| 250 |
fi |
| 251 |
|
| 252 |
|
| 253 |
# Signing with a NIST P256 key |
| 254 |
if ! tsscreateprimary -ecc nistp256 -hi o -pwdk ooo -si 1>/dev/null; then |
| 255 |
return 1 |
| 256 |
fi |
| 257 |
|
| 258 |
ecdsaparam="-scheme ecdsa" |
| 259 |
# older tools had: [-ecc (ECDSA scheme)] |
| 260 |
if tsssign --help | grep ecc | grep -q scheme; then |
| 261 |
ecdsaparam="-ecc" |
| 262 |
fi |
| 263 |
if ! tsssign ${ecdsaparam:+${ecdsaparam}} -hk 80000000 -pwdk ooo -if "${inputfile}" -os "${signature}"; then |
| 264 |
return 1 |
| 265 |
fi |
| 266 |
|
| 267 |
if ! nv_storefile 01000009 "${signature}"; then |
| 268 |
return 1 |
| 269 |
fi |
| 270 |
|
| 271 |
# Save the key as contextfile |
| 272 |
if ! tsscontextsave -ha 80000000 -of "${contextfile}" 1>/dev/null; then |
| 273 |
return 1 |
| 274 |
fi |
| 275 |
|
| 276 |
if ! nv_storefile 0100000a "${contextfile}"; then |
| 277 |
return 1 |
| 278 |
fi |
| 279 |
|
| 280 |
# Flush all keys |
| 281 |
if ! tssflushcontext -ha 80000000; then |
| 282 |
return 1 |
| 283 |
fi |
| 284 |
|
| 285 |
|
| 286 |
# Test setting command audit if available; Ubuntu Jammy does not have it |
| 287 |
if type -P tsssetcommandcodeauditstatus >/dev/null; then |
| 288 |
# Set a couple of commands to be audited |
| 289 |
for cc in 11f 12a 133 13a 140 147 150 15c 16e 17a 187 190 197; do |
| 290 |
if ! tsssetcommandcodeauditstatus -hi o -set $cc; then |
| 291 |
return 1 |
| 292 |
fi |
| 293 |
done |
| 294 |
fi |
| 295 |
|
| 296 |
return 0 |
| 297 |
} |
| 298 |
|
| 299 |
function check_tpm_state() |
| 300 |
{ |
| 301 |
local workdir="$1" |
| 302 |
local is_fullresume="$2" |
| 303 |
|
| 304 |
local inputfile="${workdir}/input.txt" |
| 305 |
local encfile="${workdir}/enc" |
| 306 |
local decfile="${workdir}/dec" |
| 307 |
local signature="${workdir}/signature.bin" |
| 308 |
local contextfile="${workdir}/context.bin" |
| 309 |
local aespub="${workdir}/aespub.bin" |
| 310 |
local aespriv="${workdir}/aespriv.bin" |
| 311 |
local cc rsasize |
| 312 |
|
| 313 |
echo "input" > "${inputfile}" |
| 314 |
|
| 315 |
export TPM_COMMAND_PORT=${SWTPM_SERVER_PORT} |
| 316 |
export TPM_PLATFORM_PORT=${SWTPM_CTRL_PORT} |
| 317 |
export TPM_SERVER_NAME=${SWTPM_SERVER_NAME} |
| 318 |
export TPM_INTERFACE_TYPE=socsim |
| 319 |
export TPM_SERVER_TYPE=raw |
| 320 |
|
| 321 |
if tsscreateprimary --help | grep rsa | grep -q keybits; then |
| 322 |
rsasize="3072" |
| 323 |
fi |
| 324 |
|
| 325 |
# Test RSA 3072 signing key |
| 326 |
if ! tsscreateprimary -rsa ${rsasize:+${rsasize}} -hi o -pwdk ooo -si 1>/dev/null; then |
| 327 |
return 1 |
| 328 |
fi |
| 329 |
|
| 330 |
if ! nv_savetofile 01000000 "${signature}"; then |
| 331 |
return 1 |
| 332 |
fi |
| 333 |
|
| 334 |
if ! tssverifysignature -hk 80000000 -if "${inputfile}" -is "${signature}"; then |
| 335 |
return 1 |
| 336 |
fi |
| 337 |
echo "INFO: Verified signature with RSA key" |
| 338 |
|
| 339 |
if ! tssflushcontext -ha 80000000; then |
| 340 |
return 1 |
| 341 |
fi |
| 342 |
|
| 343 |
# Test with the key stored in context; this only works with save/restore of all state |
| 344 |
if [ "${is_fullresume}" -ne 0 ]; then |
| 345 |
if ! nv_savetofile 01000001 "${contextfile}"; then |
| 346 |
return 1 |
| 347 |
fi |
| 348 |
|
| 349 |
if ! tsscontextload -if "${contextfile}" 1>/dev/null; then |
| 350 |
return 1 |
| 351 |
fi |
| 352 |
|
| 353 |
if ! tssverifysignature -hk 80000000 -if "${inputfile}" -is "${signature}"; then |
| 354 |
return 1 |
| 355 |
fi |
| 356 |
echo "INFO: Verified signature with RSA key and restored key context" |
| 357 |
|
| 358 |
if ! tssflushcontext -ha 80000000; then |
| 359 |
return 1 |
| 360 |
fi |
| 361 |
fi |
| 362 |
|
| 363 |
# HMAC test |
| 364 |
# tsssign -salg was added in a later version only |
| 365 |
if tsssign -h | grep -q "salg"; then |
| 366 |
if ! tsscreateprimary -rsa ${rsasize:+${rsasize}} -hi o -pwdk ooo 1>/dev/null; then |
| 367 |
return 1 |
| 368 |
fi |
| 369 |
|
| 370 |
if ! nv_savetofile 01000002 "${aespub}" \ |
| 371 |
|| ! nv_savetofile 01000003 "${aespriv}" \ |
| 372 |
|| ! nv_savetofile 01000004 "${signature}"; then |
| 373 |
return 1 |
| 374 |
fi |
| 375 |
|
| 376 |
if ! tssload -hp 80000000 -pwdp ooo -ipu "${aespub}" -ipr "${aespriv}" 1>/dev/null; then |
| 377 |
return 1 |
| 378 |
fi |
| 379 |
|
| 380 |
if ! tssverifysignature -hk 80000001 -if "${inputfile}" -is "${signature}"; then |
| 381 |
return 1 |
| 382 |
fi |
| 383 |
echo "INFO: Verified HMAC" |
| 384 |
|
| 385 |
if ! tssflushcontext -ha 80000000 || ! tssflushcontext -ha 80000001; then |
| 386 |
return 1 |
| 387 |
fi |
| 388 |
|
| 389 |
# Test with the keys stored in contexts; this only works with save/restore of all state |
| 390 |
if [ "${is_fullresume}" -ne 0 ]; then |
| 391 |
if ! nv_savetofile 01000005 "${contextfile}" || \ |
| 392 |
! tsscontextload -if "${contextfile}" 1>/dev/null; then |
| 393 |
return 1 |
| 394 |
fi |
| 395 |
|
| 396 |
if ! nv_savetofile 01000006 "${contextfile}" || \ |
| 397 |
! tsscontextload -if "${contextfile}" 1>/dev/null; then |
| 398 |
return 1 |
| 399 |
fi |
| 400 |
|
| 401 |
if ! tssverifysignature -hk 80000001 -if "${inputfile}" -is "${signature}"; then |
| 402 |
return 1 |
| 403 |
fi |
| 404 |
echo "INFO: Verified HMAC with restored key context" |
| 405 |
|
| 406 |
if ! tssflushcontext -ha 80000000 || ! tssflushcontext -ha 80000001; then |
| 407 |
return 1 |
| 408 |
fi |
| 409 |
fi |
| 410 |
fi |
| 411 |
|
| 412 |
# Encryption/Decryption |
| 413 |
if ! tsscreateprimary -hi o -des 1>/dev/null; then |
| 414 |
return 1 |
| 415 |
fi |
| 416 |
|
| 417 |
if ! nv_savetofile 01000007 "${encfile}"; then |
| 418 |
return 1 |
| 419 |
fi |
| 420 |
|
| 421 |
if ! tssencryptdecrypt -hk 80000000 -d -if "${encfile}" -of "${decfile}"; then |
| 422 |
return 1 |
| 423 |
fi |
| 424 |
|
| 425 |
if [ "$(get_sha1_file "${inputfile}")" != "$(get_sha1_file "${decfile}")" ]; then |
| 426 |
return 1 |
| 427 |
fi |
| 428 |
echo "INFO: Verified decryption" |
| 429 |
|
| 430 |
if ! tssflushcontext -ha 80000000; then |
| 431 |
return 1 |
| 432 |
fi |
| 433 |
|
| 434 |
# Test with the key stored in context; this only works with save/restore of all state |
| 435 |
if [ "${is_fullresume}" -ne 0 ]; then |
| 436 |
if ! nv_savetofile 01000008 "${contextfile}" || \ |
| 437 |
! tsscontextload -if "${contextfile}" 1>/dev/null; then |
| 438 |
return 1 |
| 439 |
fi |
| 440 |
if ! tssencryptdecrypt -hk 80000000 -d -if "${encfile}" -of "${decfile}"; then |
| 441 |
return 1 |
| 442 |
fi |
| 443 |
|
| 444 |
if [ "$(get_sha1_file "${inputfile}")" != "$(get_sha1_file "${decfile}")" ]; then |
| 445 |
return 1 |
| 446 |
fi |
| 447 |
echo "INFO: Verified decryption with restored key context" |
| 448 |
|
| 449 |
if ! tssflushcontext -ha 80000000; then |
| 450 |
return 1 |
| 451 |
fi |
| 452 |
fi |
| 453 |
|
| 454 |
# Test NIST p256 signing key |
| 455 |
if ! tsscreateprimary -ecc nistp256 -hi o -pwdk ooo -si 1>/dev/null; then |
| 456 |
return 1 |
| 457 |
fi |
| 458 |
|
| 459 |
if ! nv_savetofile 01000009 "${signature}"; then |
| 460 |
return 1 |
| 461 |
fi |
| 462 |
|
| 463 |
if ! tssverifysignature -ecc -hk 80000000 -if "${inputfile}" -is "${signature}"; then |
| 464 |
return 1 |
| 465 |
fi |
| 466 |
echo "INFO: Verified signature with RSA key" |
| 467 |
|
| 468 |
if ! tssflushcontext -ha 80000000; then |
| 469 |
return 1 |
| 470 |
fi |
| 471 |
|
| 472 |
# Test with the key stored in context; this only works with save/restore of all state |
| 473 |
if [ "${is_fullresume}" -ne 0 ]; then |
| 474 |
if ! nv_savetofile 0100000a "${contextfile}"; then |
| 475 |
return 1 |
| 476 |
fi |
| 477 |
|
| 478 |
if ! tsscontextload -if "${contextfile}" 1>/dev/null; then |
| 479 |
return 1 |
| 480 |
fi |
| 481 |
|
| 482 |
if ! tssverifysignature -ecc -hk 80000000 -if "${inputfile}" -is "${signature}"; then |
| 483 |
return 1 |
| 484 |
fi |
| 485 |
echo "INFO: Verified signature with RSA key and restored key context" |
| 486 |
|
| 487 |
if ! tssflushcontext -ha 80000000; then |
| 488 |
return 1 |
| 489 |
fi |
| 490 |
fi |
| 491 |
|
| 492 |
|
| 493 |
# Test the audited commands is command was available to set audited commands |
| 494 |
if [ "${is_fullresume}" -ne 0 ]; then |
| 495 |
if type -P tsssetcommandcodeauditstatus >/dev/null; then |
| 496 |
for cc in 11f 12a 133 13a 140 147 150 15c 16e 17a 187 190 197; do |
| 497 |
if ! tssgetcapability -cap 4 | grep -q 00000${cc}; then |
| 498 |
echo "Error: Audit not set for command 0000${cc}" |
| 499 |
tssgetcapability -cap 4 |
| 500 |
return 1 |
| 501 |
fi |
| 502 |
done |
| 503 |
echo "INFO: Verified audited commands" |
| 504 |
fi |
| 505 |
fi |
| 506 |
|
| 507 |
return 0 |
| 508 |
} |
| 509 |
|
| 510 |
function build_swtpm() |
| 511 |
{ |
| 512 |
local srcdir="$1" |
| 513 |
local cflags="$2" |
| 514 |
local destdir="$3" |
| 515 |
local swtpm_git_version="$4" |
| 516 |
|
| 517 |
echo -e "\nBuilding swtpm ${swtpm_git_version} with CFLAGS=${cflags} ..." |
| 518 |
|
| 519 |
pushd "${srcdir}" &>/dev/null || exit 1 |
| 520 |
|
| 521 |
git clean -xdf &>/dev/null |
| 522 |
git reset --hard HEAD |
| 523 |
if ! git checkout "${swtpm_git_version}"; then |
| 524 |
# Travis needs this |
| 525 |
git remote add upstream "${SWTPM_URL}" |
| 526 |
git fetch upstream "${swtpm_git_version}" |
| 527 |
git checkout "upstream/${swtpm_git_version}" |
| 528 |
fi |
| 529 |
PKG_CONFIG_PATH="${destdir}" CFLAGS="${cflags}" \ |
| 530 |
./autogen.sh --without-cuse --without-selinux --without-seccomp |
| 531 |
make -j "$(nproc)" || return 1 |
| 532 |
|
| 533 |
cp \ |
| 534 |
src/swtpm/.libs/swtpm src/swtpm/.libs/libswtpm*.so* \ |
| 535 |
src/swtpm_setup/swtpm_setup \ |
| 536 |
src/swtpm_ioctl/swtpm_ioctl \ |
| 537 |
"${destdir}" |
| 538 |
|
| 539 |
popd &>/dev/null || exit 1 |
| 540 |
} |
| 541 |
|
| 542 |
# Copy a compiled libtpms version to its destination directory; the destination |
| 543 |
# directory will be created |
| 544 |
# @param1: Destination directory's parent |
| 545 |
# @param2: The version of libtpms, e.g. '0.9' or 'master' |
| 546 |
function copy_libtpms() |
| 547 |
{ |
| 548 |
local dest="$1" |
| 549 |
local version="$2" |
| 550 |
|
| 551 |
local destdir="${dest}/libtpms-${version}" |
| 552 |
|
| 553 |
mkdir -p "${destdir}" |
| 554 |
cp libtpms.pc src/.libs/libtpms.so* "${destdir}" |
| 555 |
|
| 556 |
return 0 |
| 557 |
} |
| 558 |
|
| 559 |
# Build various versions of libtpms starting with libtpms 0.9 up to 'master' |
| 560 |
# Build swtpm for each one of these versions as well. |
| 561 |
function build_libtpms_and_swtpm() |
| 562 |
{ |
| 563 |
local workdir="$1" |
| 564 |
|
| 565 |
local tmp major maj min |
| 566 |
local libtpmsdir="${workdir}/libtpms" |
| 567 |
|
| 568 |
pushd "${workdir}" 2>&1 || return 1 |
| 569 |
|
| 570 |
git clone "${LIBTPMS_URL}" libtpms |
| 571 |
|
| 572 |
pushd libtpms 2>&1 || return 1 |
| 573 |
|
| 574 |
git checkout "${LIBTPMS_INITIAL_BRANCH}" |
| 575 |
|
| 576 |
echo -e "\nBuilding libtpms ${INITIAL_BRANCH} ..." |
| 577 |
CFLAGS="-g -O2" ./autogen.sh --with-tpm2 --without-tpm1 |
| 578 |
make -j "$(nproc)" || return 1 |
| 579 |
|
| 580 |
copy_libtpms "${workdir}" "master" |
| 581 |
|
| 582 |
# Determine major version of what master built |
| 583 |
tmp=$(ls src/.libs/libtpms.so.*.*) |
| 584 |
major=$(echo "${tmp}" | sed -n 's/.*.so\.\([^\.]\+\)\.\([^\.]\+\)\..*/\1/p') |
| 585 |
|
| 586 |
if ! build_swtpm \ |
| 587 |
"${workdir}/swtpm" \ |
| 588 |
"-I${libtpmsdir}/include -L${workdir}/libtpms-master" \ |
| 589 |
"${workdir}/libtpms-master" \ |
| 590 |
"${SWTPM_DEFAULT_BRANCH}"; then |
| 591 |
echo "Error: Could not build swtpm" |
| 592 |
return 1 |
| 593 |
fi |
| 594 |
|
| 595 |
for ((maj=0; maj<=major; maj++)) { |
| 596 |
min=0 |
| 597 |
[ "${maj}" = 0 ] && min=9 # start with v0.9 |
| 598 |
|
| 599 |
for ((;; min++)) { |
| 600 |
git checkout "origin/stable-${maj}.${min}" &>/dev/null || break |
| 601 |
|
| 602 |
# clean directory containing libtpms.so.* |
| 603 |
rm -rf src/.libs |
| 604 |
echo -e "\nBuilding libtpms ${maj}.${min}..." |
| 605 |
make -j "$(nproc)" || return 1 |
| 606 |
|
| 607 |
copy_libtpms "${workdir}" "${maj}.${min}" |
| 608 |
|
| 609 |
if ! build_swtpm \ |
| 610 |
"${workdir}/swtpm" \ |
| 611 |
"-I${libtpmsdir}/include -L${workdir}/libtpms-${maj}.${min}" \ |
| 612 |
"${workdir}/libtpms-${maj}.${min}" \ |
| 613 |
stable-0.9; then |
| 614 |
echo "Error: Could not build swtpm" |
| 615 |
return 1 |
| 616 |
fi |
| 617 |
} |
| 618 |
} |
| 619 |
|
| 620 |
popd &>/dev/null || return 1 |
| 621 |
|
| 622 |
#ls -l libtpms-* |
| 623 |
#PATH="${workdir}/libtpms-master" LD_LIBRARY_PATH=${PATH} ./libtpms-master/swtpm_setup --help |
| 624 |
#PATH="${workdir}/libtpms-0.9" LD_LIBRARY_PATH=${PATH} ./libtpms-0.9/swtpm_setup socket --help |
| 625 |
|
| 626 |
popd &>/dev/null || return 1 |
| 627 |
|
| 628 |
return 0 |
| 629 |
} |
| 630 |
|
| 631 |
# Run swtpm_setup on all versions >= v0.10 of libtpms. The state is written |
| 632 |
# into directories with the name patterns of state--${branch}--${profile}, |
| 633 |
# which leads to names like state--0.10--null for null profile and libtpms v0.10 |
| 634 |
function swtpm_setup_create_profile_state() |
| 635 |
{ |
| 636 |
local workdir="$1" |
| 637 |
|
| 638 |
local branch profiles libtpmsdir statedir err=0 has_option |
| 639 |
|
| 640 |
echo -e "INFO: Creating state with various profiles\n" |
| 641 |
|
| 642 |
pushd "${workdir}" &>/dev/null || return 1 |
| 643 |
|
| 644 |
# Setup swtpm-0.10 and later with null profile and check that swtpm-0.9 works with it |
| 645 |
for libtpmsdir in libtpms-master libtpms-*.*; do |
| 646 |
branch=$(echo "${libtpmsdir}" | sed -n 's/.*-//p') |
| 647 |
|
| 648 |
export LD_LIBRARY_PATH="${workdir}/${libtpmsdir}" |
| 649 |
export SWTPM_IOCTL=./${libtpmsdir}/swtpm_ioctl |
| 650 |
|
| 651 |
echo |
| 652 |
has_option=$(PATH="${workdir}/${libtpmsdir}" \ |
| 653 |
swtpm socket --help | grep -E "\-profile") |
| 654 |
|
| 655 |
if [ -n "${has_option}" ]; then |
| 656 |
# get a space-separated list of profile names |
| 657 |
profiles=$(PATH="${workdir}/${libtpmsdir}" \ |
| 658 |
swtpm socket --tpm2 --print-capabilities \ |
| 659 |
| sed -n \ |
| 660 |
-e "s/.* \"profiles\": { \"names\": \[ \(.*\) \].*/\1/" \ |
| 661 |
-e 's/[ "]//g' -e 's/,/ /gp') |
| 662 |
else |
| 663 |
profiles="<none>" |
| 664 |
fi |
| 665 |
|
| 666 |
for profile in ${profiles}; do |
| 667 |
local profileopt="" |
| 668 |
|
| 669 |
if [ "${profile}" = "<none>" ]; then |
| 670 |
profile=null |
| 671 |
else |
| 672 |
# shellcheck disable=SC2089 |
| 673 |
profileopt="--profile {\"Name\":\"${profile}\"}" |
| 674 |
fi |
| 675 |
|
| 676 |
statedir="state--${branch}--${profile}" |
| 677 |
mkdir "${statedir}" |
| 678 |
echo |
| 679 |
PATH="${workdir}/${libtpmsdir}" \ |
| 680 |
swtpm -v |
| 681 |
echo "state dir:$statedir, libtpms dir:$libtpmsdir, profile: $profile" |
| 682 |
|
| 683 |
if ! PATH="${workdir}/${libtpmsdir}" \ |
| 684 |
"./${libtpmsdir}/swtpm_setup" \ |
| 685 |
--tpm2 \ |
| 686 |
--tpmstate "${statedir}" \ |
| 687 |
--config "${workdir}/swtpm_setup.conf" \ |
| 688 |
--log "${statedir}/swtpm_setup.log" \ |
| 689 |
--tpm "${workdir}/${libtpmsdir}/swtpm socket" \ |
| 690 |
${profileopt:+${profileopt}}; then |
| 691 |
echo "Error: Could not run swtpm_setup with libtpms from branch ${branch} with profile ${profile}" |
| 692 |
err=1 |
| 693 |
break |
| 694 |
fi |
| 695 |
|
| 696 |
SWTPM_PID="" |
| 697 |
|
| 698 |
if ! SWTPM_SERVER_NO_DISCONNECT=1 \ |
| 699 |
SWTPM_EXE=./${libtpmsdir}/swtpm \ |
| 700 |
run_swtpm "${SWTPM_INTERFACE}" \ |
| 701 |
--tpm2 \ |
| 702 |
--tpmstate "dir=${workdir}/${statedir}" \ |
| 703 |
--flags not-need-init,startup-clear; then |
| 704 |
echo "Error: Could not start swtpm with null profile state and libtpms version '${libtpmsbranch}'." |
| 705 |
err=1 |
| 706 |
break |
| 707 |
fi |
| 708 |
|
| 709 |
if ! create_tpm_state "${workdir}"; then |
| 710 |
echo "Error: Creating TPM-internal state failed" |
| 711 |
err=1 |
| 712 |
break |
| 713 |
fi |
| 714 |
echo "INFO: Successfully created TPM-internal state" |
| 715 |
|
| 716 |
if ! run_swtpm_ioctl "${SWTPM_INTERFACE}" --save permanent "${workdir}/${statedir}/permanent" \ |
| 717 |
|| ! run_swtpm_ioctl "${SWTPM_INTERFACE}" --save volatile "${workdir}/${statedir}/volatile"; then |
| 718 |
echo "Error: Could not save permanent or volatile state" |
| 719 |
err=1 |
| 720 |
break |
| 721 |
fi |
| 722 |
echo "INFO: Successfully saved permanent and volatile state to ${workdir}/${statedir}/" |
| 723 |
|
| 724 |
if ! check_tpm_state "${workdir}" 1; then |
| 725 |
echo "Error: Checking TPM-internal state failed" |
| 726 |
err=1 |
| 727 |
break |
| 728 |
fi |
| 729 |
echo "INFO: Successfully checked proper functioning of TPM-internal state" |
| 730 |
|
| 731 |
# Use kill here rather than a shutdown to be able to compare the |
| 732 |
# size of the state later on. A shutdown would generate a bigger |
| 733 |
# tpm2-00.permall file that cannot be compare with while the TPM 2 |
| 734 |
# is running. |
| 735 |
kill_quiet -9 "${SWTPM_PID}" |
| 736 |
SWTPM_PID="" |
| 737 |
|
| 738 |
echo "INFO: Sizes of state in ${workdir}/${statedir}" |
| 739 |
ls -l "${workdir}/${statedir}" |
| 740 |
done |
| 741 |
|
| 742 |
if [ -n "${SWTPM_PID}" ]; then |
| 743 |
kill_quiet -9 "${SWTPM_PID}" |
| 744 |
fi |
| 745 |
if [ "${err}" -ne 0 ]; then |
| 746 |
break |
| 747 |
fi |
| 748 |
done |
| 749 |
|
| 750 |
unset LD_LIBRARY_PATH |
| 751 |
|
| 752 |
popd &>/dev/null || return 1 |
| 753 |
|
| 754 |
echo -e "INFO: Done creating state with various profiles\n\n" |
| 755 |
|
| 756 |
return "${err}" |
| 757 |
} |
| 758 |
|
| 759 |
# This function tests that the NULL profile state, that was created by a certain |
| 760 |
# version of libtpms, can be used by swtpm with libtpms v0.9 and others. |
| 761 |
function swtpm_run_with_null_profile_state() |
| 762 |
{ |
| 763 |
local workdir="$1" |
| 764 |
|
| 765 |
local statedir branch libtpmsbranch resp exp fsize err=0 |
| 766 |
|
| 767 |
echo "INFO: Running with NULL profile states using different version of libtpms" |
| 768 |
|
| 769 |
pushd "${workdir}" &>/dev/null || return 1 |
| 770 |
|
| 771 |
for statedir in state--*--null; do |
| 772 |
branch=$(echo "${statedir}" | sed -n 's/.*--\(.*\)--.*/\1/p') |
| 773 |
for libtpmsdir in libtpms-master libtpms-*.*; do |
| 774 |
libtpmsbranch=$(echo "${libtpmsdir}" | sed -n 's/.*-//p') |
| 775 |
fsize=$(get_filesize "${workdir}/${statedir}/tpm2-00.permall") |
| 776 |
|
| 777 |
export LD_LIBRARY_PATH="${workdir}/${libtpmsdir}" |
| 778 |
export SWTPM_IOCTL="./${libtpmsdir}/swtpm_ioctl" |
| 779 |
|
| 780 |
echo -e "\nINFO: Using swtpm in ${libtpmsdir} with TPM state in ${workdir}/${statedir}" |
| 781 |
|
| 782 |
if ! SWTPM_SERVER_NO_DISCONNECT=1 \ |
| 783 |
SWTPM_EXE=./${libtpmsdir}/swtpm \ |
| 784 |
run_swtpm "${SWTPM_INTERFACE}" \ |
| 785 |
--tpm2 \ |
| 786 |
--tpmstate "dir=${workdir}/${statedir}" \ |
| 787 |
--flags not-need-init,startup-clear; then |
| 788 |
echo "Error: Could not start swtpm with null profile state and libtpms version '${libtpmsbranch}'." |
| 789 |
err=1 |
| 790 |
break |
| 791 |
fi |
| 792 |
# Read PCR 10 |
| 793 |
exp=' 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 ' |
| 794 |
resp=$(tsspcrread -ha 10 -halg sha256 | grep " 00" | tr -d "\n" | tr -s " ") |
| 795 |
if [ "${resp}" != "${exp}" ]; then |
| 796 |
echo "Error: Did not get expected result from TPM2_PCRRead(10)" |
| 797 |
echo "expected: ${exp}" |
| 798 |
echo "received: ${resp}" |
| 799 |
err=1 |
| 800 |
break |
| 801 |
fi |
| 802 |
echo " PCR 10 is good: profile: null libtpms: ${libtpmsbranch} state written by: ${branch}" |
| 803 |
|
| 804 |
if [ "$(get_filesize "${workdir}/${statedir}/tpm2-00.permall")" != "${fsize}" ]; then |
| 805 |
echo "Error: Size of tpm2 state file has changed" |
| 806 |
echo "expected: ${fsize}" |
| 807 |
echo "actual : $(get_filesize "${workdir}/${statedir}/tpm2-00.permall")" |
| 808 |
ls -l "${workdir}/${statedir}/" |
| 809 |
err=1 |
| 810 |
break |
| 811 |
fi |
| 812 |
|
| 813 |
if ! check_tpm_state "${workdir}" 0; then |
| 814 |
echo "Error: Checking TPM-internal state failed: profile: null libtpms: ${libtpmsbranch} state written by: ${branch}" |
| 815 |
err=1 |
| 816 |
break |
| 817 |
fi |
| 818 |
echo "INFO: Successfully checked proper functioning of TPM-internal state" |
| 819 |
|
| 820 |
# restore permanent and volatile state and run tests again |
| 821 |
if ! run_swtpm_ioctl "${SWTPM_INTERFACE}" --stop \ |
| 822 |
|| ! run_swtpm_ioctl "${SWTPM_INTERFACE}" --load permanent "${workdir}/${statedir}/permanent" \ |
| 823 |
|| ! run_swtpm_ioctl "${SWTPM_INTERFACE}" --load volatile "${workdir}/${statedir}/volatile" \ |
| 824 |
|| ! run_swtpm_ioctl "${SWTPM_INTERFACE}" -i; then |
| 825 |
echo "Error: Could not load permanent or volatile state" |
| 826 |
err=1 |
| 827 |
break |
| 828 |
fi |
| 829 |
echo "INFO: Successfully restored permanent and volatile state from ${workdir}/${statedir}/" |
| 830 |
|
| 831 |
if ! check_tpm_state "${workdir}" 1; then |
| 832 |
echo "Error: Checking TPM-internal state failed (after resume): profile: null libtpms: ${libtpmsbranch} state written by: ${branch}" |
| 833 |
err=1 |
| 834 |
break |
| 835 |
fi |
| 836 |
echo "INFO: Successfully checked proper functioning of TPM-internal state after state resume" |
| 837 |
|
| 838 |
kill_quiet -9 "${SWTPM_PID}" |
| 839 |
echo " INFO: Passed with libtpms ${libtpmsbranch}" |
| 840 |
|
| 841 |
done |
| 842 |
if [ ${err} -ne 0 ]; then |
| 843 |
kill_quiet -9 "${SWTPM_PID}" |
| 844 |
break |
| 845 |
fi |
| 846 |
done |
| 847 |
|
| 848 |
unset LD_LIBRARY_PATH |
| 849 |
|
| 850 |
popd &>/dev/null || return 1 |
| 851 |
|
| 852 |
echo -e "INFO: Done running with NULL profile states using different version of libtpms\n\n" |
| 853 |
|
| 854 |
return "${err}" |
| 855 |
} |
| 856 |
|
| 857 |
# This function tests that a default profile state with 'stateFormatLevel = 2' can be used |
| 858 |
# and remains at this level. |
| 859 |
function swtpm_run_with_default_profile_state() |
| 860 |
{ |
| 861 |
local workdir="$1" |
| 862 |
local statefile="$2" |
| 863 |
|
| 864 |
local libtpmsdir libtpmsbranch output err=0 exp |
| 865 |
|
| 866 |
mkdir "${workdir}/tmp" |
| 867 |
cp "${statefile}" "${workdir}/tmp/" |
| 868 |
|
| 869 |
echo -e "\n\nINFO: Testing with previously created default profile state" |
| 870 |
|
| 871 |
pushd "${workdir}" &>/dev/null || return 1 |
| 872 |
|
| 873 |
for libtpmsdir in libtpms-master libtpms-*.*; do |
| 874 |
libtpmsbranch=$(echo "${libtpmsdir}" | sed -n 's/.*-//p') |
| 875 |
[ "${libtpmsbranch}" = "0.9" ] && continue |
| 876 |
|
| 877 |
export LD_LIBRARY_PATH="${workdir}/${libtpmsdir}" |
| 878 |
export SWTPM_IOCTL="./${libtpmsdir}/swtpm_ioctl" |
| 879 |
|
| 880 |
if ! SWTPM_EXE="./${libtpmsdir}/swtpm" \ |
| 881 |
run_swtpm "${SWTPM_INTERFACE}" \ |
| 882 |
--tpm2 \ |
| 883 |
--tpmstate "dir=${workdir}/tmp" \ |
| 884 |
--flags not-need-init,startup-clear; then |
| 885 |
echo "Error: Could not start swtpm with 'default' profile state and libtpms version '${libtpmsbranch}'." |
| 886 |
err=1 |
| 887 |
break |
| 888 |
fi |
| 889 |
if ! output=$(run_swtpm_ioctl "${SWTPM_INTERFACE}" --info 0x20); then |
| 890 |
echo "Error: Could not run swtpm_ioctl: ${output}" |
| 891 |
err=1 |
| 892 |
break |
| 893 |
fi |
| 894 |
exp=',"StateFormatLevel":2,' |
| 895 |
if ! echo "${output}" | grep -q "${exp}"; then |
| 896 |
echo "Error: Could not find '${exp}' in swtpm_ioctl output." |
| 897 |
echo " output: ${output}" |
| 898 |
err=1 |
| 899 |
break |
| 900 |
fi |
| 901 |
|
| 902 |
echo " INFO: Found proper default state in libtpms ${libtpmsbranch}" |
| 903 |
|
| 904 |
run_swtpm_ioctl "${SWTPM_INTERFACE}" -s |
| 905 |
done |
| 906 |
|
| 907 |
if [ ${err} -ne 0 ]; then |
| 908 |
run_swtpm_ioctl "${SWTPM_INTERFACE}" -s |
| 909 |
fi |
| 910 |
|
| 911 |
unset LD_LIBRARY_PATH |
| 912 |
|
| 913 |
popd &>/dev/null || return 1 |
| 914 |
|
| 915 |
echo -e "INFO: Done testing with previously created default profile state\n\n" |
| 916 |
|
| 917 |
rm -rf "${workdir}/tmp/" |
| 918 |
|
| 919 |
return "${err}" |
| 920 |
} |
| 921 |
|
| 922 |
if ! build_libtpms_and_swtpm "${workdir}"; then |
| 923 |
echo "Error: Building libtpms and/or swtpm failed" |
| 924 |
exit 1 |
| 925 |
fi |
| 926 |
|
| 927 |
if ! swtpm_setup_create_profile_state "${workdir}"; then |
| 928 |
exit 1 |
| 929 |
fi |
| 930 |
|
| 931 |
if ! swtpm_run_with_null_profile_state "${workdir}"; then |
| 932 |
exit 1 |
| 933 |
fi |
| 934 |
|
| 935 |
if ! swtpm_run_with_default_profile_state "${workdir}" "${TESTDIR}/data/tpm2state7/tpm2-00.permall"; then |
| 936 |
exit 1 |
| 937 |
fi |
| 938 |
|
| 939 |
exit 0 |