|
| 1 | +# swtpm |
| 2 | + |
| 3 | +This directory contains a Makefile and a manifest template for running swtpm. |
| 4 | +See https://github.com/stefanberger/swtpm/. |
| 5 | + |
| 6 | +**NOTE**: Currently works only with PR https://github.com/gramineproject/gramine/pull/1210. |
| 7 | +See also https://github.com/stefanberger/swtpm/issues/792. |
| 8 | + |
| 9 | +# Installing |
| 10 | + |
| 11 | +1. Install `libtpms` like this: |
| 12 | + https://github.com/stefanberger/libtpms/wiki#build-a-package-on-ubuntu |
| 13 | + - Version used: `git checkout v0.9.6` |
| 14 | + |
| 15 | +2. Install `swtpm` like this: https://github.com/stefanberger/swtpm/wiki#compile-on-ubuntu-2104 |
| 16 | + - Don't install `libtpms-dev` since we've done it already in step 1 |
| 17 | + - Version used: `git checkout v0.8.0` |
| 18 | + |
| 19 | +Now swtpm tools are installed. We run only `swtpm` executable with Gramine. |
| 20 | + |
| 21 | +# Configuration of `swtpm` |
| 22 | + |
| 23 | +`swtpm` executable can be run in several modes. We hard-code the following configuration |
| 24 | +(command-line options) to run with Gramine: |
| 25 | +```sh |
| 26 | +$ swtpm socket --tpm2 --tpmstate dir=/myvtpm2 --seccomp action=none \ |
| 27 | + --server type=tcp,port=2321,disconnect --ctrl type=tcp,port=2320 \ |
| 28 | + --flags not-need-init,startup-clear |
| 29 | +``` |
| 30 | + |
| 31 | +This configuration means: |
| 32 | +- run `swtpm` in TPM2 mode, |
| 33 | +- save all TPM state under `/myvtpm2/` dir (encrypted under Gramine with SGX), |
| 34 | +- don't use seccomp (Gramine doesn't support it, and it's not needed in Gramine env anyway), |
| 35 | +- listen for client connections on TCP/IP port 2321 (in contrast to CUSE or chardev), |
| 36 | +- create a control channel on TCP/IP port 2320, |
| 37 | +- additional flags for the initial state of TPM. |
| 38 | + |
| 39 | +For more information, see `man swtpm`. |
| 40 | + |
| 41 | +# Building |
| 42 | + |
| 43 | +- `make clean; make` for Gramine without SGX (`gramine-direct`). |
| 44 | +- `make clean; make SGX=1` for Gramine with SGX (`gramine-sgx`). |
| 45 | + |
| 46 | +You can add `DEBUG=1` for verbose Gramine logs. |
| 47 | + |
| 48 | +Notice that `gramine-direct` uses a dummy encryption key for TPM files, hard-coded in the manifest. |
| 49 | +Whereas `gramine-sgx` uses the MRENCLAVE-based sealing encryption key for TPM files (and is |
| 50 | +therefore secure). To make sure the correct key is used, we require a `make clean` step. For details |
| 51 | +on how the key is chosen, see Makefile and manifest template. |
| 52 | + |
| 53 | +# Quick tests of swtpm with Gramine |
| 54 | + |
| 55 | +# 1. Self-test |
| 56 | + |
| 57 | +The test idea is taken from https://github.com/stefanberger/swtpm/wiki/Useful-scripts-for-TPM, |
| 58 | +Section "Trigger a self-test on a TPM 2 listening on command port 2321 with the disconnect flag". |
| 59 | + |
| 60 | +```sh |
| 61 | +# swtpm server in one window |
| 62 | +gramine-sgx swtpm |
| 63 | + |
| 64 | +# client script in another window |
| 65 | +bash -c "exec 100<>/dev/tcp/localhost/2321; \ |
| 66 | + echo -en '\x80\x01\x00\x00\x00\x0b\x00\x00\x01\x43\x01' >&100; \ |
| 67 | + od -tx1 <&100" |
| 68 | + |
| 69 | +## output must be like this: |
| 70 | +## 0000000 80 01 00 00 00 0a 00 00 00 00 |
| 71 | +``` |
| 72 | + |
| 73 | +# 2. Hashing in PCR 17 |
| 74 | + |
| 75 | +The test idea is taken from the unit test: |
| 76 | +https://github.com/stefanberger/swtpm/blob/346b3d62/tests/_test_tpm2_hashing. |
| 77 | + |
| 78 | +```sh |
| 79 | +# swtpm server in one window |
| 80 | +gramine-sgx swtpm |
| 81 | + |
| 82 | +# client scripts in another window |
| 83 | + |
| 84 | +## 1 step: init TPM to known state |
| 85 | +swtpm_ioctl --tcp localhost:2320 -i |
| 86 | + |
| 87 | +## 2 step: startup TPM2 |
| 88 | +bash -c "exec 100<>/dev/tcp/localhost/2321; \ |
| 89 | + echo -en '\x80\x01\x00\x00\x00\x0c\x00\x00\x01\x44\x00\x00' >&100; \ |
| 90 | + od -tx1 <&100" |
| 91 | + |
| 92 | +## output must be like this: |
| 93 | +## 0000000 80 01 00 00 00 0a 00 00 00 00 |
| 94 | + |
| 95 | +## 3 step: ask TPM to hash string "1234" in PCR 17 |
| 96 | +swtpm_ioctl --tcp localhost:2320 -h 1234 |
| 97 | + |
| 98 | +## 4 step: read PCR 17 |
| 99 | +bash -c "exec 100<>/dev/tcp/localhost/2321; \ |
| 100 | + echo -en '\x80\x01\x00\x00\x00\x14\x00\x00\x01\x7e\x00\x00\x00\x01\x00\x0b\x03\x00\x00\x02' >&100; \ |
| 101 | + od -tx1 <&100" |
| 102 | + |
| 103 | +## output must be like this: |
| 104 | +## 0000000 80 01 00 00 00 3e 00 00 00 00 00 00 00 2c 00 00 |
| 105 | +## 0000020 00 01 00 0b 03 00 00 02 00 00 00 01 00 20 fc a5 |
| 106 | +## 0000040 d6 49 bf b0 c9 22 fd 33 0f 79 b2 00 43 28 9d af |
| 107 | +## 0000060 d6 0d 01 a4 c4 37 3c f2 8a db 56 c9 b4 54 |
| 108 | + |
| 109 | +## 5 step: check TPM Established flag (must be 1) |
| 110 | +swtpm_ioctl --tcp localhost:2320 -e |
| 111 | + |
| 112 | +## 6 step: shutdown TPM |
| 113 | +swtpm_ioctl --tcp localhost:2320 -s |
| 114 | +``` |
0 commit comments