| 1 | #!/usr/bin/env bash |
| 2 | #set -x |
| 3 | |
| 4 | # For the license, see the LICENSE file in the root directory. |
| 5 | |
| 6 | # shellcheck disable=SC1091 |
| 7 | |
| 8 | if [ "$(id -u)" -ne 0 ]; then |
| 9 | echo "Need to be root to run this test." |
| 10 | exit 77 |
| 11 | fi |
| 12 | |
| 13 | # tpmtool is not packaged everywhere ... |
| 14 | if [ -z "$(type -P tpmtool)" ]; then |
| 15 | echo "Could not find tpmtool in PATH" |
| 16 | exit 77 |
| 17 | fi |
| 18 | |
| 19 | ROOT=${abs_top_builddir:-$(dirname "$0")/..} |
| 20 | TESTDIR=${abs_top_testdir:=$(dirname "$0")} |
| 21 | SRCDIR=${abs_top_srcdir:-$(dirname "$0")/..} |
| 22 | |
| 23 | PATH=$ROOT/src/swtpm:$PATH |
| 24 | |
| 25 | source "${TESTDIR}/test_config" |
| 26 | source "${TESTDIR}/common" |
| 27 | |
| 28 | if [ -n "${SWTPM_TEST_UNINSTALLED}" ]; then |
| 29 | SWTPM_CREATE_TPMCA=${SRCDIR}/samples/swtpm-create-tpmca |
| 30 | else |
| 31 | SWTPM_CREATE_TPMCA=/usr/share/swtpm/swtpm-create-tpmca |
| 32 | fi |
| 33 | if [ ! -x "${SWTPM_CREATE_TPMCA}" ]; then |
| 34 | echo "Could not find swtpm-create-tpmca" |
| 35 | exit 77 |
| 36 | fi |
| 37 | |
| 38 | SWTPM_INTERFACE=socket+socket |
| 39 | SWTPM_SERVER_NAME=localhost |
| 40 | SWTPM_SERVER_PORT=65434 |
| 41 | SWTPM_CTRL_PORT=65435 |
| 42 | |
| 43 | TCSD_LISTEN_PORT=65436 |
| 44 | |
| 45 | SRK_PASSWORD=srk |
| 46 | OWNER_PASSWORD=owner |
| 47 | |
| 48 | workdir="$(mktemp -d)" || exit 1 |
| 49 | |
| 50 | TCSD_CONF=${workdir}/tcsd.conf |
| 51 | TCSD_SYSTEM_PS_FILE=${workdir}/system_ps_file |
| 52 | TCSD_PIDFILE=${workdir}/tcsd.pid |
| 53 | SWTPM_LOCALCA_DIR="${workdir}/my localca" |
| 54 | SWTPM_LOCALCA_CONF="${workdir}/my localca/swtpm-localca.conf" |
| 55 | |
| 56 | # Captured TCSD file when using a SRK_PASSWORD=srk |
| 57 | TCSD_FILE="AQEAAAAAAAAAAAAAAAAAAAAAAAABAAAAAAAAAAAAAAAAAAAAAAABLwEAAAAAAwAAAAAAAAAAAAAA |
| 58 | AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA |
| 59 | AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA |
| 60 | AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA |
| 61 | AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA |
| 62 | AAAAAAAAAAAAAAAAAAAAAAAAAQEAAAARAAAAAAEAAAABAAMAAQAAAAwAAAgAAAAAAgAAAAAAAAAA |
| 63 | AAABAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA |
| 64 | AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA |
| 65 | AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA |
| 66 | AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA |
| 67 | AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA" |
| 68 | |
| 69 | function cleanup() |
| 70 | { |
| 71 | if [ -n "${TCSD_PID}" ]; then |
| 72 | kill_quiet -15 "${TCSD_PID}" |
| 73 | fi |
| 74 | if [ -n "${SWTPM_PID}" ]; then |
| 75 | kill_quiet -9 "${SWTPM_PID}" |
| 76 | fi |
| 77 | if [ -n "${BASH_PID}" ]; then |
| 78 | kill_quiet -9 "${BASH_PID}" |
| 79 | fi |
| 80 | rm -rf "${workdir}" |
| 81 | } |
| 82 | |
| 83 | trap "cleanup" SIGTERM EXIT |
| 84 | skip_test_no_tpm12 "${SWTPM_EXE}" |
| 85 | |
| 86 | PATH=${ROOT}/src/swtpm_bios:${ROOT}/src/swtpm_cert:${PATH} |
| 87 | |
| 88 | # run the test with the given owner and SRK passwords |
| 89 | # @param1: owner password; empty means to use well known password |
| 90 | # @param2: SRK password; empty means to use well known password |
| 91 | # @param3: vTPM is a TPM 2.0 |
| 92 | function run_test() { |
| 93 | local owner_password="$1" |
| 94 | local srk_password="$2" |
| 95 | local vtpm_is_tpm2="$3" |
| 96 | |
| 97 | local params certinfo regex regexs fil i skip |
| 98 | |
| 99 | rm -rf "${workdir:?}"/* |
| 100 | |
| 101 | cat <<_EOF_ > "${workdir}/swtpm_setup.conf" |
| 102 | create_certs_tool=${SWTPM_LOCALCA} |
| 103 | create_certs_tool_config=${workdir}/swtpm-localca.conf |
| 104 | create_certs_tool_options=/dev/null |
| 105 | _EOF_ |
| 106 | |
| 107 | params="" |
| 108 | if [ -n "${owner_password}" ]; then |
| 109 | params="${params} --ownerpass ${owner_password}" |
| 110 | else |
| 111 | params="${params} --owner-well-known" |
| 112 | fi |
| 113 | params="${params} --srkpass ${srk_password}" |
| 114 | |
| 115 | # First setup the TPM and take ownership of it and set SRK password |
| 116 | if ! $SWTPM_SETUP \ |
| 117 | --runas root \ |
| 118 | --tpm-state "${workdir}" \ |
| 119 | --logfile "${workdir}/logfile" \ |
| 120 | --config "${workdir}/swtpm_setup.conf" \ |
| 121 | --tpm "${SWTPM_EXE} socket ${SWTPM_TEST_SECCOMP_OPT}" \ |
| 122 | --take-ownership \ |
| 123 | ${params:+${params}} >/dev/null; then |
| 124 | echo "Error: Could not run $SWTPM_SETUP." |
| 125 | echo "Setup Logfile:" |
| 126 | cat "${workdir}/logfile" |
| 127 | exit 1 |
| 128 | fi |
| 129 | |
| 130 | echo "Successfully took ownership of TPM and set owner and SRK passwords." |
| 131 | |
| 132 | run_swtpm "${SWTPM_INTERFACE}" \ |
| 133 | --flags not-need-init \ |
| 134 | --tpmstate "dir=${workdir}" |
| 135 | |
| 136 | # Startup the TPM |
| 137 | res="$(swtpm_cmd_tx "${SWTPM_INTERFACE}" '\x00\xC1\x00\x00\x00\x0C\x00\x00\x00\x99\x00\x01')" |
| 138 | exp=' 00 c4 00 00 00 0a 00 00 00 00' |
| 139 | if [ "$res" != "$exp" ]; then |
| 140 | echo "Error: Did not get expected result from TPM_Startup(ST_Clear)" |
| 141 | echo "expected: $exp" |
| 142 | echo "received: $res" |
| 143 | exit 1 |
| 144 | fi |
| 145 | |
| 146 | echo "$TCSD_FILE" | base64 -d > "${TCSD_SYSTEM_PS_FILE}" |
| 147 | |
| 148 | # Setup the TCSD config file and start TCSD with it |
| 149 | cat <<_EOF_ > "${TCSD_CONF}" |
| 150 | port = ${TCSD_LISTEN_PORT} |
| 151 | system_ps_file = ${TCSD_SYSTEM_PS_FILE} |
| 152 | _EOF_ |
| 153 | |
| 154 | # Due to recent changes in tcsd we have to try with TSS_USER=tss and TSS_USER=root |
| 155 | # Before the following worked: |
| 156 | # - tss:tss 0600 for TSS_USER=tss and TSS_GROUP=tss |
| 157 | # - root:tss 0640 for TSS_USER=root and TSS_GROUP=tss |
| 158 | # After the changes: |
| 159 | # - root:tss 0640 for TSS_USER=tss and TSS_GROUP=tss |
| 160 | while :; do |
| 161 | chown "${TSS_USER}:${TSS_GROUP}" "${TCSD_CONF}" |
| 162 | if [ "${TSS_USER}" == "${TSS_GROUP}" ]; then |
| 163 | chmod 0600 "${TCSD_CONF}" |
| 164 | else |
| 165 | chmod 0640 "${TCSD_CONF}" |
| 166 | fi |
| 167 | |
| 168 | bash -c "TCSD_USE_TCP_DEVICE=1 TCSD_TCP_DEVICE_PORT=${SWTPM_SERVER_PORT} tcsd -c \"${TCSD_CONF}\" -e -f &>/dev/null & echo \$! > \"${TCSD_PIDFILE}\"; wait" & |
| 169 | BASH_PID=$! |
| 170 | |
| 171 | if wait_for_file "${TCSD_PIDFILE}" 3; then |
| 172 | echo "Error: Could not get TCSD's PID file" |
| 173 | exit 1 |
| 174 | fi |
| 175 | |
| 176 | # wait for PID to be written |
| 177 | sleep 0.5 |
| 178 | TCSD_PID=$(cat "${TCSD_PIDFILE}") |
| 179 | if ! kill_quiet -0 "${TCSD_PID}"; then |
| 180 | # Try again with root unless we already tried |
| 181 | if [ "$TSS_USER" != "root" ]; then |
| 182 | TSS_USER="root" |
| 183 | continue |
| 184 | fi |
| 185 | echo "Error: TCSD with pid ${TCSD_PID} must have terminated" |
| 186 | exit 1 |
| 187 | fi |
| 188 | break |
| 189 | done |
| 190 | |
| 191 | if ! ${SWTPM_CREATE_TPMCA} \ |
| 192 | --dir "${SWTPM_LOCALCA_DIR}" \ |
| 193 | --srk-password "${srk_password}" \ |
| 194 | --register \ |
| 195 | --group "${TSS_GROUP}" \ |
| 196 | --tss-tcsd-port "${TCSD_LISTEN_PORT}" \ |
| 197 | --outfile "${SWTPM_LOCALCA_CONF}" &>/dev/null; then |
| 198 | echo "Error: Could not create TPM CA" |
| 199 | exit 1 |
| 200 | fi |
| 201 | |
| 202 | for fil in \ |
| 203 | swtpm-localca-rootca-cert.pem \ |
| 204 | swtpm-localca-rootca-privkey.pem \ |
| 205 | swtpm-localca-tpmca-cert.pem \ |
| 206 | swtpm-localca-tpmca-pubkey.pem; do |
| 207 | if [ ! -r "${SWTPM_LOCALCA_DIR}/${fil}" ]; then |
| 208 | echo "Error: TPM CA tool did not create file ${fil}." |
| 209 | exit 1 |
| 210 | fi |
| 211 | done |
| 212 | |
| 213 | params="" |
| 214 | if [ -n "${srk_password}" ]; then |
| 215 | params="^parentkey_password =" |
| 216 | fi |
| 217 | |
| 218 | for regex in \ |
| 219 | "^statedir = " \ |
| 220 | "^signingkey = " \ |
| 221 | "^issuercert = " \ |
| 222 | "^certserial = " \ |
| 223 | "^TSS_TCSD_HOSTNAME = " \ |
| 224 | "^TSS_TCSD_PORT = " \ |
| 225 | ${params}; do |
| 226 | if [ -n "${regex}" ] && \ |
| 227 | ! grep -q -E "${regex}" "${SWTPM_LOCALCA_CONF}"; then |
| 228 | echo "Error: Could not find regex '${regex}' in CA config file." |
| 229 | cat "${SWTPM_LOCALCA_CONF}" |
| 230 | exit 1 |
| 231 | fi |
| 232 | done |
| 233 | |
| 234 | params="" |
| 235 | if [ "${vtpm_is_tpm2}" -ne 0 ]; then |
| 236 | params="--tpm2" |
| 237 | skip=0 |
| 238 | else |
| 239 | skip=7 # header in cert |
| 240 | fi |
| 241 | |
| 242 | # make sure we can actually sign with this new certificate |
| 243 | if ! ${SWTPM_LOCALCA} \ |
| 244 | --type ek \ |
| 245 | --ek x=739192d8f1004283957a7b1568d610b41c637ccc114aadcac4908c20456468fa,y=59f63ac06f8011f6fdd1460c6bc8e3e0a2d090d4fc188c7e04870e06795ce8ae \ |
| 246 | --dir "${workdir}" --vmid test \ |
| 247 | ${params} \ |
| 248 | --tpm-spec-family 2.0 --tpm-spec-revision 146 --tpm-spec-level 00 \ |
| 249 | --tpm-model swtpm --tpm-version 20170101 --tpm-manufacturer IBM \ |
| 250 | --configfile "${SWTPM_LOCALCA_CONF}" \ |
| 251 | --optsfile /dev/null; then |
| 252 | echo "Error: The CA could not sign with the new certificate" |
| 253 | exit 1 |
| 254 | fi |
| 255 | if [ ! -f "${workdir}/ek.cert" ]; then |
| 256 | echo "Error: The CA did not produce a certificate" |
| 257 | exit 1 |
| 258 | fi |
| 259 | # cert was for example 541 bytes long |
| 260 | if [ "$(get_filesize "${workdir}/ek.cert")" -lt 500 ]; then |
| 261 | echo "Error: The certificate's size is dubious" |
| 262 | ls -l "${workdir}/ek.cert" |
| 263 | exit 1 |
| 264 | fi |
| 265 | |
| 266 | # Check the contents of the certificate |
| 267 | certinfo=$(dd "if=${workdir}/ek.cert" bs=1 "skip=$skip" status=none | \ |
| 268 | "$CERTTOOL" -i --inder) |
| 269 | regexs=('^[[:space:]]+2.23.133.8.1$' |
| 270 | '^[[:space:]]+directoryName:.*(,)?2.23.133.2.3=.*' |
| 271 | '^[[:space:]]+directoryName:.*(,)?2.23.133.2.2=.*' |
| 272 | '^[[:space:]]+directoryName:.*(,)?2.23.133.2.1=.*' |
| 273 | '^[[:space:]]+Certificate Authority \(CA\): FALSE$' |
| 274 | '^[[:space:]]+Unknown extension 2.5.29.9 \(not critical\):$' |
| 275 | '^[[:space:]]+Hexdump: 3019301706056781050210310e300c0c03322e3002010002020092$') |
| 276 | if [ "${vtpm_is_tpm2}" -ne 0 ]; then |
| 277 | # TPM 2.0; due to ecc: Key agreement |
| 278 | regexs+=('^[[:space:]]+Key agreement\.$' |
| 279 | '^[[:space:]]+Signature Algorithm: RSA-SHA256$') |
| 280 | else |
| 281 | regexs+=('^[[:space:]]+Key encipherment\.$' |
| 282 | '^[[:space:]]+Signature Algorithm: RSA-SHA1$') |
| 283 | fi |
| 284 | |
| 285 | for ((i=0; i < ${#regexs}; i++)); do \ |
| 286 | if [ -n "${regexs[$i]}" ] && \ |
| 287 | ! echo "${certinfo}" | grep -q -E "${regexs[$i]}"; then |
| 288 | echo "Error: Could not match regex '${regexs[$i]}' with certificate info:" |
| 289 | echo "${certinfo}" |
| 290 | exit 1 |
| 291 | fi |
| 292 | done |
| 293 | |
| 294 | # Send SIGTERM to TCSD |
| 295 | kill_quiet -15 "${TCSD_PID}" |
| 296 | |
| 297 | # Shut down TPM |
| 298 | if ! run_swtpm_ioctl "${SWTPM_INTERFACE}" -s; then |
| 299 | echo "Error: Could not shut down the ${SWTPM_INTERFACE} TPM." |
| 300 | exit 1 |
| 301 | fi |
| 302 | |
| 303 | if wait_process_gone "${SWTPM_PID}" 4; then |
| 304 | echo "Error: ${SWTPM_INTERFACE} TPM should not be running anymore." |
| 305 | exit 1 |
| 306 | fi |
| 307 | |
| 308 | if wait_process_gone "${SWTPM_PID}" 4; then |
| 309 | echo "Error: tcsd should not be running anymore." |
| 310 | exit 1 |
| 311 | fi |
| 312 | } # run_test |
| 313 | |
| 314 | run_test "${OWNER_PASSWORD}" "${SRK_PASSWORD}" 1 |
| 315 | echo "Test 1: OK" |
| 316 | |
| 317 | run_test "${OWNER_PASSWORD}" "${SRK_PASSWORD}" 0 |
| 318 | echo "Test 2: OK" |
| 319 | |
| 320 | run_test "" "${SRK_PASSWORD}" 1 |
| 321 | echo "Test 3: OK" |
| 322 | |
| 323 | run_test "" "${SRK_PASSWORD}" 0 |
| 324 | echo "Test 4: OK" |
| 325 | |
| 326 | exit 0 |