| 1 |
# Go bindings to the Microsoft TPM2 Simulator |
| 2 |
|
| 3 |
Microsoft maintains the reference implementation of the TPM2 spec at: |
| 4 |
https://github.com/Microsoft/ms-tpm-20-ref/. |
| 5 |
|
| 6 |
The Microsoft code used here is actually |
| 7 |
[a fork of the upstream source](https://github.com/josephlr/ms-tpm-20-ref/tree/google). |
| 8 |
It is vendored at `simulator/ms-tpm-20-ref` to maintain compatiblity with |
| 9 |
`go get`. Building the simulator requires the OpenSSL headers to be installed. |
| 10 |
This can be done with: |
| 11 |
- Debian based systems (including Ubuntu): `apt install libssl-dev` |
| 12 |
- Red Hat based systems: `yum install openssl-devel` |
| 13 |
- Arch Linux based systems: [`openssl`](https://www.archlinux.org/packages/core/x86_64/openssl/) |
| 14 |
is installed by default (as a dependancy of `base`) and includes the headers. |
| 15 |
|
| 16 |
## Debugging |
| 17 |
|
| 18 |
The simulator provides a useful way to figure out what the TPM is actually doing |
| 19 |
when it executes a command. If you compile a test which runs against the |
| 20 |
simulator, you can step through the simulator C source to see the exact |
| 21 |
operations performed. |
| 22 |
|
| 23 |
To do this: |
| 24 |
1. Compile a test as a standalone binary. For example, if you were using a |
| 25 |
`go-tpm-tools/client` test (which all run against the simulator), compile |
| 26 |
the test binary named `client.test` by running: |
| 27 |
```bash |
| 28 |
go test -c github.com/google/go-tpm-tools/client |
| 29 |
``` |
| 30 |
1. Now you can debug the binary using GDB: |
| 31 |
```bash |
| 32 |
# Load the binary into GDB (fixing any errors/warnings you get) |
| 33 |
gdb ./client.test |
| 34 |
# In GDB, set a breakpoint in the funciton you want to use. |
| 35 |
(gdb) break TPM2_CreatePrimary |
| 36 |
Breakpoint 1 at 0x5d3710: file ./TPMCmd/tpm/src/command/Hierarchy/CreatePrimary.c, line 72. |
| 37 |
# Now you can either run all the tests in the package, or just one. |
| 38 |
# As we want to depug TPM2_CreatePrimary we'll run TestSeal |
| 39 |
(gdb) run -test.run TestSeal |
| 40 |
Starting program: ./client.test -test.run TestSeal |
| 41 |
Thread 1 "client.test" hit Breakpoint 1, TPM2_CreatePrimary |
| 42 |
at ./TPMCmd/tpm/src/command/Hierarchy/CreatePrimary.c:72 |
| 43 |
72 { |
| 44 |
# Go to the next line |
| 45 |
(gdb) n |
| 46 |
81 newObject = FindEmptyObjectSlot(&out->objectHandle); |
| 47 |
# Step into a function |
| 48 |
(gdb) s |
| 49 |
FindEmptyObjectSlot |
| 50 |
at ./TPMCmd/tpm/src/subsystem/Object.c:266 |
| 51 |
266 { |
| 52 |
# Continue until the next breakpoint (or exiting) |
| 53 |
(gdb) c |
| 54 |
Continuing. |
| 55 |
PASS |
| 56 |
[Inferior 1 (process 29395) exited normally] |
| 57 |
``` |
| 58 |
|
| 59 |
## IDE Support |
| 60 |
|
| 61 |
When examining the TPM2 C code, is is often useful to have IDE support for |
| 62 |
things like "Go to Definition". To get this working, all your IDE should need |
| 63 |
is knowing where the headers are and what `#define` statements to use. |
| 64 |
|
| 65 |
For example, when using [VS Code](https://code.visualstudio.com/) with the |
| 66 |
[C/C++ extension](https://marketplace.visualstudio.com/items?itemName=ms-vscode.cpptools), |
| 67 |
add the following file to your workplace root at `.vscode/c_cpp_properties.json`: |
| 68 |
```json |
| 69 |
{ |
| 70 |
"configurations": [ |
| 71 |
{ |
| 72 |
"name": "Linux", |
| 73 |
"includePath": [ |
| 74 |
"${workspaceFolder}/**" |
| 75 |
], |
| 76 |
"defines": [ |
| 77 |
"VTPM=NO", |
| 78 |
"SIMULATION=NO", |
| 79 |
"USE_DA_USED=NO", |
| 80 |
"HASH_LIB=Ossl", |
| 81 |
"SYM_LIB=Ossl", |
| 82 |
"MATH_LIB=Ossl" |
| 83 |
], |
| 84 |
"compilerPath": "/bin/clang", |
| 85 |
"cStandard": "c11", |
| 86 |
"cppStandard": "c++17", |
| 87 |
"intelliSenseMode": "clang-x64" |
| 88 |
} |
| 89 |
], |
| 90 |
"version": 4 |
| 91 |
} |
| 92 |
``` |