|
| 1 | +#!/bin/bash |
| 2 | +# SPDX-License-Identifier: BSD-3-Clause |
| 3 | + |
| 4 | +SIM_PORT_DATA=2321 |
| 5 | +SIM_PORT_CMD=$((SIM_PORT_DATA+1)) |
| 6 | + |
| 7 | +# Run from top dir of this repository |
| 8 | +SCRIPT_DIR=$( cd -- "$( dirname -- "${BASH_SOURCE[0]}" )" &> /dev/null && pwd ) |
| 9 | +TOP_DIR="$(realpath "$SCRIPT_DIR/..")" |
| 10 | +cd "$TOP_DIR" || { echo "Error: cd to cd $TOP_DIR failed"; exit 1; } |
| 11 | + |
| 12 | + |
| 13 | +verify_simulator_is_running() { |
| 14 | + local pid_tpm=$1 |
| 15 | + |
| 16 | + sleep 1 |
| 17 | + ss -lntp4 2> /dev/null | grep "${pid_tpm}" | grep -q "${SIM_PORT_DATA}" |
| 18 | + ret_data=$? |
| 19 | + ss -lntp4 2> /dev/null | grep "${pid_tpm}" | grep -q "${SIM_PORT_CMD}" |
| 20 | + ret_cmd=$? |
| 21 | + if [ $ret_data -eq 0 ] && [ $ret_cmd -eq 0 ]; then |
| 22 | + echo "Simulator with PID ${pid_tpm} bound to port ${SIM_PORT_DATA} and ${SIM_PORT_CMD} successfully." |
| 23 | + return 0 |
| 24 | + else |
| 25 | + echo "Error: Port conflict? Cleaning up PID: ${pid_tpm}" |
| 26 | + return 1 |
| 27 | + fi |
| 28 | +} |
| 29 | + |
| 30 | +build_tpm2_simulator_ibm() ( |
| 31 | + test -d ibmtpm && return |
| 32 | + echo "---> compiling IBM tpm simulator" |
| 33 | + mkdir ibmtpm |
| 34 | + curl -Ls https://downloads.sourceforge.net/project/ibmswtpm2/ibmtpm1682.tar.gz | tar xz -C ibmtpm |
| 35 | + cd ibmtpm/src && make |
| 36 | +) |
| 37 | + |
| 38 | +start_tpm2_simulator_ibm () { |
| 39 | + build_tpm2_simulator_ibm || return 1 |
| 40 | + |
| 41 | + echo "---> starting IBM tpm simulator" |
| 42 | + ibmtpm/src/tpm_server & |
| 43 | + pid_tpm=$! |
| 44 | + verify_simulator_is_running $pid_tpm |
| 45 | +} |
| 46 | + |
| 47 | +start_tpm2_simulator_swtpm () { |
| 48 | + echo "---> starting swtpm simulator" |
| 49 | + swtpm socket --tpm2 \ |
| 50 | + --server port=$SIM_PORT_DATA \ |
| 51 | + --ctrl type=tcp,port=$SIM_PORT_CMD \ |
| 52 | + --flags not-need-init \ |
| 53 | + --tpmstate dir="$PWD" \ |
| 54 | + --seccomp action=none & |
| 55 | + pid_tpm=$! |
| 56 | + verify_simulator_is_running $pid_tpm |
| 57 | +} |
| 58 | + |
| 59 | +start_dbusd () { |
| 60 | + echo "---> starting dbus daemon" |
| 61 | + dbus-daemon --session --print-address > /tmp/bus-socket-path.txt & |
| 62 | + sleep 1 |
| 63 | + DBUS_SESSION_BUS_ADDRESS="$(tail -n1 /tmp/bus-socket-path.txt)" |
| 64 | + export DBUS_SESSION_BUS_ADDRESS |
| 65 | +} |
| 66 | + |
| 67 | +start_tpm2_abrmd() { |
| 68 | + local tabrmd_tcti=$1 |
| 69 | + |
| 70 | + echo "---> starting abrmd" |
| 71 | + local tabrmd_name="com.intel.tss2.Tabrmd${SIM_PORT_DATA}" |
| 72 | + tpm2-abrmd --session --dbus-name="${tabrmd_name}" --tcti "${tabrmd_tcti}:host=localhost,port=${SIM_PORT_DATA}" & |
| 73 | + TCTI_ADDRESS="tabrmd:bus_name=${tabrmd_name},bus_type=session" |
| 74 | + TPM2TOOLS_TCTI="$TCTI_ADDRESS" |
| 75 | + TPM2OPENSSL_TCTI="$TCTI_ADDRESS" |
| 76 | + export TPM2TOOLS_TCTI |
| 77 | + export TPM2OPENSSL_TCTI |
| 78 | + sleep 1 |
| 79 | + busctl --address="${DBUS_SESSION_BUS_ADDRESS}" list | grep "$tabrmd_name" |
| 80 | +} |
| 81 | + |
| 82 | +start_tpm2_sim_env() { |
| 83 | + local sim_type=$1 |
| 84 | + |
| 85 | + start_dbusd |
| 86 | + |
| 87 | + if [ "$sim_type" = "swtpm" ]; then |
| 88 | + start_tpm2_simulator_swtpm || return 1 |
| 89 | + start_tpm2_abrmd swtpm || return 1 |
| 90 | + elif [ "$sim_type" = "ibm" ]; then |
| 91 | + start_tpm2_simulator_ibm || return 1 |
| 92 | + start_tpm2_abrmd mssim || return 1 |
| 93 | + else |
| 94 | + echo "invalid tpm simulator typ" |
| 95 | + return 1 |
| 96 | + fi |
| 97 | +} |
| 98 | + |
| 99 | +make_check () { |
| 100 | + echo "Running make check" |
| 101 | + openssl version |
| 102 | + tpm2_getcap properties-fixed | head -n 20 |
| 103 | + make check |
| 104 | +} |
| 105 | + |
| 106 | +function cleanup() |
| 107 | +{ |
| 108 | + pkill -P $$ |
| 109 | +} |
| 110 | +trap cleanup EXIT |
| 111 | + |
| 112 | +build_tpm2_openssl() { |
| 113 | + ./bootstrap |
| 114 | + ./configure CC=gcc --enable-op-digest --enable-op-cipher |
| 115 | + make |
| 116 | +} |
| 117 | + |
| 118 | +SIM_TYPE=${1:-swtpm} |
| 119 | +build_tpm2_openssl || { echo "Compiling tpm2-openssl failed"; exit 1; } |
| 120 | +start_tpm2_sim_env "${SIM_TYPE}" || { echo "Starting tpm2 simulator failed ($SIM_TYPE)"; exit 1; } |
| 121 | +make_check || { echo "tpm2-openssl make check failed"; exit 1; } |
0 commit comments