| 1 |
#!/bin/bash -x |
| 2 |
# vim: set tabstop=8 shiftwidth=4 softtabstop=4 expandtab smarttab colorcolumn=80: |
| 3 |
# |
| 4 |
# Copyright (c) 2019 Red Hat, Inc. |
| 5 |
# Author: Sergio Correia <[email protected]> |
| 6 |
# |
| 7 |
# This program is free software: you can redistribute it and/or modify |
| 8 |
# it under the terms of the GNU General Public License as published by |
| 9 |
# the Free Software Foundation, either version 3 of the License, or |
| 10 |
# (at your option) any later version. |
| 11 |
# |
| 12 |
# This program is distributed in the hope that it will be useful, |
| 13 |
# but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 14 |
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 15 |
# GNU General Public License for more details. |
| 16 |
# |
| 17 |
# You should have received a copy of the GNU General Public License |
| 18 |
# along with this program. If not, see <http://www.gnu.org/licenses/>. |
| 19 |
# |
| 20 |
|
| 21 |
. tests-common-functions |
| 22 |
|
| 23 |
SOCAT_BIN="@SOCAT_BIN@" |
| 24 |
SWTPM_BIN="@SWTPM_BIN@" |
| 25 |
SWTPM_SETUP_BIN="@SWTPM_SETUP_BIN@" |
| 26 |
|
| 27 |
SWTPM_SOCKET_PID= |
| 28 |
|
| 29 |
function on_exit() { |
| 30 |
popd || error "Unable to change directory" |
| 31 |
if [ ! -d "$TESTDIR" ] || ! rm -rf "$TESTDIR"; then |
| 32 |
echo "Delete temporary files failed!" >&2 |
| 33 |
echo "You need to clean up: $TESTDIR" >&2 |
| 34 |
exit 1 |
| 35 |
fi |
| 36 |
|
| 37 |
# Cleanup sw emulation |
| 38 |
if [ -n "$SWTPM_SOCKET_PID" ]; then |
| 39 |
kill $SWTPM_SOCKET_PID >/dev/null 2>&1 |
| 40 |
sleep .5 |
| 41 |
# swtpm does not always terminate gracefully, so kill it |
| 42 |
kill -9 $SWTPM_SOCKET_PID >/dev/null 2>&1 |
| 43 |
fi |
| 44 |
} |
| 45 |
if ! TESTDIR="$(mktemp -d)"; then |
| 46 |
echo "Creating a temporary dir for TPM files failed!" >&2 |
| 47 |
exit 1 |
| 48 |
fi |
| 49 |
trap 'on_exit' EXIT |
| 50 |
pushd "$TESTDIR" || error "Unable to change directory" |
| 51 |
|
| 52 |
|
| 53 |
tpm2_hw_available() { |
| 54 |
# Old environment variables for tpm2-tools 3.0 |
| 55 |
export TPM2TOOLS_TCTI_NAME=device |
| 56 |
export TPM2TOOLS_DEVICE_FILE= |
| 57 |
for dev in /dev/tpmrm?; do |
| 58 |
[ -e "${dev}" ] || continue |
| 59 |
TPM2TOOLS_DEVICE_FILE="${dev}" |
| 60 |
break |
| 61 |
done |
| 62 |
|
| 63 |
# New environment variable for tpm2-tools >= 3.1 |
| 64 |
export TPM2TOOLS_TCTI="${TPM2TOOLS_TCTI_NAME}:${TPM2TOOLS_DEVICE_FILE}" |
| 65 |
|
| 66 |
if [ -z "${TPM2TOOLS_DEVICE_FILE}" ]; then |
| 67 |
echo "A TPM2 device with the in-kernel resource manager is needed!" >&2 |
| 68 |
return 1 |
| 69 |
fi |
| 70 |
|
| 71 |
if ! [[ -r "${TPM2TOOLS_DEVICE_FILE}" \ |
| 72 |
&& -w "${TPM2TOOLS_DEVICE_FILE}" ]]; then |
| 73 |
echo "The ${TPM2TOOLS_DEVICE_FILE} device must be readable and writable!" >&2 |
| 74 |
return 1 |
| 75 |
fi |
| 76 |
return 0 |
| 77 |
} |
| 78 |
|
| 79 |
tpm2_version() { |
| 80 |
local _tpm2tools_info |
| 81 |
local _match='version="(.)\.' |
| 82 |
_tpm2tools_info="$(tpm2_createprimary -v)" |
| 83 |
[[ ${_tpm2tools_info} =~ ${_match} ]] && TPM2TOOLS_VERSION="${BASH_REMATCH[1]}" |
| 84 |
if [[ $TPM2TOOLS_VERSION -lt 3 ]] || [[ $TPM2TOOLS_VERSION -gt 5 ]]; then |
| 85 |
echo "The tpm2 pin requires a tpm2-tools version between 3 and 5" >&2 |
| 86 |
return 1 |
| 87 |
fi |
| 88 |
export TPM2TOOLS_VERSION |
| 89 |
} |
| 90 |
|
| 91 |
tpm2_sw_check_preconditions() { |
| 92 |
[ -x "${SOCAT_BIN}" ] || skip_test "Skipping TPM2 test with software emulation, socat not found" |
| 93 |
[ -x "${SWTPM_BIN}" ] || skip_test "Skipping TPM2 test with software emulation, swtpm not found" |
| 94 |
[ -x "${SWTPM_SETUP_BIN}" ] || skip_test "Skipping TPM2 test with software emulation, swtpm_setup not found" |
| 95 |
|
| 96 |
if ! "${SWTPM_BIN}" socket --print-capabilities | jq -e '(.version | test("^0\\.[0-6](\\..*)?$")) or (.features | index("tpm-2.0"))' >/dev/null 2>&1; then |
| 97 |
skip_test "Skipping TPM2 test with software emulation, no support for TPM 2.0 in swtpm" |
| 98 |
fi |
| 99 |
} |
| 100 |
|
| 101 |
tpm2_start_emulation() { |
| 102 |
local socket_wait |
| 103 |
local server_sock |
| 104 |
local control_sock |
| 105 |
|
| 106 |
echo "Starting TPM 2 emulation" >&2 |
| 107 |
|
| 108 |
# Setup TPM 2 data |
| 109 |
"${SWTPM_SETUP_BIN}" --tpm-state "$TESTDIR" --tpm2 --create-platform-cert --lock-nvram --display >&2 || error "Unable to setup TPM 2 emulation" |
| 110 |
|
| 111 |
# Start emulation over socket |
| 112 |
server_sock="$TESTDIR"/swtpm.sock |
| 113 |
control_sock="$TESTDIR"/swtpm.sock.ctrl |
| 114 |
"${SWTPM_BIN}" socket --tpmstate dir="$TESTDIR" --tpm2 --ctrl type=unixio,path="$control_sock" --server type=unixio,path="$server_sock" --flags startup-clear >&2 & |
| 115 |
SWTPM_SOCKET_PID=$! |
| 116 |
|
| 117 |
socket_wait=1 |
| 118 |
while [ $socket_wait -le 100 ]; do |
| 119 |
[ -S "$server_sock" ] && break |
| 120 |
socket_wait=$((socket_wait + 1)) |
| 121 |
sleep 0.1 |
| 122 |
done |
| 123 |
[ "$socket_wait" -gt 100 ] && error "Unable to start TPM 2 emulation" |
| 124 |
|
| 125 |
# Use swtpm in tpm2-tools |
| 126 |
export TPM2TOOLS_TCTI="swtpm:path=$server_sock" |
| 127 |
|
| 128 |
# Test if socket communication is supported |
| 129 |
if ! tpm2_getcap pcrs >/dev/null 2>&1; then |
| 130 |
# Old libtss2 compatibility - use socat |
| 131 |
export TPM2TOOLS_TCTI="cmd:\"$SOCAT_BIN\" - \"UNIX-CONNECT:$server_sock\"" |
| 132 |
fi |
| 133 |
} |