Commit Graph

918 Commits

Author SHA1 Message Date
Stefan Berger
ed275d52e0 tpm2: Prepare header for v2 including min. necessary version to parse
Prepare the header we are using for writing out the state blobs
for version 2 where we will including the minimum version necessary
to read a particular structure.

Signed-off-by: Stefan Berger <stefanb@linux.vnet.ibm.com>
2018-06-15 18:07:48 -04:00
Stefan Berger
69966d6d36 tpm2: Throw error on missing data for hash algorithms
Signed-off-by: Stefan Berger <stefanb@linux.vnet.ibm.com>
2018-06-15 18:07:48 -04:00
Stefan Berger
06f8e83e03 tpm2: cast to long int for match to %ld on 32bit platform
Signed-off-by: Stefan Berger <stefanb@linux.vnet.ibm.com>
2018-06-15 18:07:48 -04:00
Stefan Berger
8e97e43514 tpm2: Implement TPM2_PersistentAllStore and use it
Implement TPM2_PersistentAllStore that allocates a buffer big
enough to store all the persistent state and returns that buffer
along with the number of valid bytes.

In this patch we move code from _plat__NvCommit() into this new
function and call this new function now.

Signed-off-by: Stefan Berger <stefanb@linux.vnet.ibm.com>
2018-06-15 18:07:48 -04:00
Stefan Berger
418a10a4cc tpm2: Call _rpc__Signal_PowerOff inside TPM2_Terminate
This way we can later on detect whether the TPM 2 is running
(= powered on).

Signed-off-by: Stefan Berger <stefanb@linux.vnet.ibm.com>
2018-06-15 18:07:48 -04:00
Stefan Berger
f678eab755 tpm2: add missing newline after error message
Signed-off-by: Stefan Berger <stefanb@linux.vnet.ibm.com>
2018-06-15 18:07:48 -04:00
Stefan Berger
88dfea52cf tpm2: Build libtpms with TPM 2 code now
Now that patches for writing the state into files and extensions
to their headers have all been applied we can build the TPM 2
code into the library.

Signed-off-by: Stefan Berger <stefanb@linux.vnet.ibm.com>
2018-06-15 18:07:48 -04:00
Stefan Berger
78a29529b2 tpm2: Write marshalled NvChip into file and read it
When storing, marshalled the NvChip memory's contents and write the byte
stream into the file rather than the NvChip directly. When reading, assume
we get a marshalled NvChip file and we now need to unmarshal the byte
stream and reconstruct the NvChip.

Signed-off-by: Stefan Berger <stefanb@linux.vnet.ibm.com>
2018-06-15 18:07:48 -04:00
Stefan Berger
4c450fe7a4 tpm2: Add test harness for marshalling and reconstructing NVChip 2018-06-15 18:07:48 -04:00
Stefan Berger
ac443b727b tpm2: Enable skipping of compile-time optional variables when unmarshalling
Some variables are compile-time optional and can be skipped if they are
contained in the byte stream but the implementation does not need them.
We enable this with a few simple macros that we use to replace those parts
where the has_block variables are written into the byte stream. On the
unmarshalling side we check whether the block is in the byte stream and
whether the implementation needs the block and react appropriately
including skipping over the block in the byte stream or skipping over the
code unmarshalling the data.

Signed-off-by: Stefan Berger <stefanb@linux.vnet.ibm.com>
2018-06-15 18:07:48 -04:00
Stefan Berger
db573f0e41 tpm2: Remove first parameter from NV_HEADER_Marshal
Remove the NV_HEADER parameter from NV_Header_Marshal() function.
In the single case where it was needed, initialize a UINT32 with
the 'magic'.

Signed-off-by: Stefan Berger <stefanb@linux.vnet.ibm.com>
2018-06-15 18:07:48 -04:00
Stefan Berger
63c4a78edd tpm2: Revert NVChip memory layout to native
Get rid of all the changes to have the NvChip written in big
endian format. Remove test case.

Now the NvChip in memory holds data in native format.

Signed-off-by: Stefan Berger <stefanb@linux.vnet.ibm.com>
2018-06-15 18:07:48 -04:00
Stefan Berger
ab877068ba tpm2: Implement function to marshal and reconstruct the NvChip
Marshal the NvChip memory into a byte stream and reconstruct the
NvChip memory from the byte stream.

Signed-off-by: Stefan Berger <stefanb@linux.vnet.ibm.com>
2018-06-15 18:07:48 -04:00
Stefan Berger
b8e5802e60 tpm2: Push nvOffset and structure size parameters into functions
Push the nvOffset parameter where either one of the 4 state blobs
found in the NVRAM file can be found. Also push the size parameter
into the functions.

Signed-off-by: Stefan Berger <stefanb@linux.vnet.ibm.com>
2018-06-15 18:07:48 -04:00
Stefan Berger
574426686e tpm2: Major rework of state marshalling breaking compatibility
In one big step, marshal more variables and constants for volatilestate:

- compile-time constants related to data structures marshalled as part
  of the volatile state as well as some other ones

- variables related to time that fix problems with dictionary attack
  related timeouts

- 3 failure related variables

Also introduce magic and version headers when marshalling all the
internal data structures.

Signed-off-by: Stefan Berger <stefanb@linux.vnet.ibm.com>
2018-06-15 18:07:48 -04:00
Stefan Berger
e2248d0eb7 tpm2: Add missing offset checks and comments about gaps to test case
Signed-off-by: Stefan Berger <stefanb@linux.vnet.ibm.com>
2018-06-15 18:07:48 -04:00
Stefan Berger
06f057db49 tpm2: Make tpmclock() a public function
Signed-off-by: Stefan Berger <stefanb@linux.vnet.ibm.com>
2018-06-15 18:07:48 -04:00
Stefan Berger
9a72be9cc7 tpm2: Update comments on variables stored in volatile state
Update the comments on some of the variables we are storing
as part of storing volatile state.

Signed-off-by: Stefan Berger <stefanb@linux.vnet.ibm.com>
2018-06-15 18:07:48 -04:00
Stefan Berger
c0c0515dd8 tpm2: Fix memory leak in VolatileLoad()
Signed-off-by: Stefan Berger <stefanb@linux.vnet.ibm.com>
2018-06-15 18:07:48 -04:00
Stefan Berger
4213dfa2d3 tpm2: Implement TPM2_ValidateState
Signed-off-by: Stefan Berger <stefanb@linux.vnet.ibm.com>
2018-06-15 18:07:48 -04:00
Stefan Berger
218b8a179d tpm2: Have VolatileLoad() return TPM_RC
Have VolatileLoad return a TPM_RC so the caller can see
whether the loading of the volatiles state succeeded.

Signed-off-by: Stefan Berger <stefanb@linux.vnet.ibm.com>
2018-06-15 18:07:48 -04:00
Stefan Berger
1cdb5cf9ff tpm2: Activate failure mode if any blobs could not be handled
When any of the NVRAM blobs could not be handled properly we put
the TPM into failure mode. This should only happen if someone
corrupts the state or if the state blobs are more recent than
what the TPM supports.

Signed-off-by: Stefan Berger <stefanb@linux.vnet.ibm.com>
2018-06-15 18:07:48 -04:00
Stefan Berger
4515414c30 tpm2: extract header init and check from SWAP functions
Extract the initialization of the header fom the SWAP functions
and initialize the header by the appropriat callers of the SWAP
functions.

Version and magic can be 0 when first read after NVRAM was
initialized. So we initialize it then.

Add skeleton code where the upgrade of the data structure would
have to happen later on.

Refuse to accept newer versions of structures than what is supported
at the moment. In particular, return error codes in case the blobs
that were read are not supported.

Signed-off-by: Stefan Berger <stefanb@linux.vnet.ibm.com>
2018-06-15 18:07:48 -04:00
Stefan Berger
f2716accb2 tpm2: Enforce version of volatile state blob
Enforce the version of the volatile state blob. Do not accept a more
recent version than what we support at this point, so downgrading of
state is prevented this way.

Signed-off-by: Stefan Berger <stefanb@linux.vnet.ibm.com>
2018-06-15 18:07:48 -04:00
Stefan Berger
d2fca8e4c9 tpm2: Fix compiler error due to comparison with different signedness
Signed-off-by: Stefan Berger <stefanb@linux.vnet.ibm.com>
2018-06-15 18:07:48 -04:00
Stefan Berger
f5ff8b93f5 build: Build Debian 0.6.0-1-dev1
Signed-off-by: Stefan Berger <stefanb@linux.vnet.ibm.com>
2018-06-15 18:07:48 -04:00
Stefan Berger
9f8884f77b build: Build RPM 0.6.0-1_dev1
Signed-off-by: Stefan Berger <stefanb@linux.vnet.ibm.com>
2018-06-15 18:07:48 -04:00
Stefan Berger
63f70197c8 tpm2: NVRAM file does exist if we get TPM_DECRYPT_ERROR
In case the NVRAM file cannot be decrypted we get a TPM_DECRYPT_ERROR
error which also indicates that the file exists. So do not return FALSE
in this case, which would delete the existing file and start over with
a blank file.

Signed-off-by: Stefan Berger <stefanb@linux.vnet.ibm.com>
2018-06-15 18:07:48 -04:00
Stefan Berger
8fb8816c3c tpm2: add missing header filenames
Signed-off-by: Stefan Berger <stefanb@linux.vnet.ibm.com>
2018-06-15 18:07:48 -04:00
Stefan Berger
d4b9d10ce9 tpm2: Handle failures better
The current TpmFail() implementation invokes longjmp() at the end
and crashes with a segmentation fault if setjmp() wasn't called before.
To avoid this we implement TpmSetFailureMode() that logs the failure and
sets the TPM into failure mode. Since NVRAM may set failure mode before
the CryptInit() is called, we need to make sure we don't reset the failure
mode variable in case CryptInit() succeeds. In this case we now call the
FAIL_NOCMD() macro.

Signed-off-by: Stefan Berger <stefanb@linux.vnet.ibm.com>
2018-06-15 18:07:48 -04:00
Stefan Berger
055f7f313c tpm2: make I/O buffer sizes adjustable
Signed-off-by: Stefan Berger <stefanb@linux.vnet.ibm.com>
2018-06-15 18:07:48 -04:00
Stefan Berger
5a273f8ead tpm2: comment out _COMMAND_FLAGS_ since not needed
Implement bitfield support for _COMMAND_FLAGS_ but comment it
out since the structure is not used.

Signed-off-by: Stefan Berger <stefanb@linux.vnet.ibm.com>
2018-06-15 18:07:48 -04:00
Stefan Berger
1ad2f26704 tpm2: Create real random numbers to get different keys
Do not use rand() for creating random numbers since this only
creates pseudo random numbers and the keys always end up being
the same since it wasn't seeded, either.

Signed-off-by: Stefan Berger <stefanb@linux.vnet.ibm.com>
2018-06-15 18:07:48 -04:00
Stefan Berger
659db6b2b8 tpm2: Make compileable on OpenBSD
Make TPM 2 code compileable on OpenBSD where we have an older version
of gcc with missing builtin swap functions and where endianes #defines
area also different.

Signed-off-by: Stefan Berger <stefanb@linux.vnet.ibm.com>
2018-06-15 18:07:48 -04:00
Stefan Berger
d93761932d Port TPM 2 to OpenSSL 1.1
Port the TPM 2 code to OpenSSL 1.1 by accessing the OpenSSL BIGNUM
only via its public functions. To get there it is necessary to
implement the Bn2bin() function that converts the TPM internal
representation of a bigNum to an array of unsigned chars that can
then be passed to the OpenSSL BN_bin2bn() function.

Signed-off-by: Stefan Berger <stefanb@linux.vnet.ibm.com>
2018-06-15 18:07:48 -04:00
Stefan Berger
c55d458d7c tpm2: add test for checking proper offsets in NVRAM
Add a test for checking for proper offsets in the NVRAM that
must never change.

Signed-off-by: Stefan Berger <stefanb@linux.vnet.ibm.com>
2018-06-15 18:07:48 -04:00
Stefan Berger
3357aa7b0a tpm2: Choose the TPM2 NVRAM memory to be 64kb
For this to work increase the maximum allocatable memory chunk
supported by TPM_Malloc() to 128k.

Signed-off-by: Stefan Berger <stefanb@linux.vnet.ibm.com>
2018-06-15 18:07:48 -04:00
Stefan Berger
a931e31cf9 tpm2: Create functions ANY_OBJECT_Marshal/Unmarshal/SWAP
Create functions ANY_OBJECT_Marshal/Unmarshal/SWAP so that
we can then handle OBJECT and HASH_OBJECT dependening on the
attribute flags.

Signed-off-by: Stefan Berger <stefanb@linux.vnet.ibm.com>
2018-06-15 18:07:48 -04:00
Stefan Berger
41dd3fe4f9 tpm2: Support bitfields for big endian 2018-06-15 18:07:48 -04:00
Stefan Berger
360432f645 tpm2: Add padding to structs for 32bit arch alignments
Some data structures need padding bytes to align the data
structures on 32bit machines to resemble the alignment on
64bit machines. Without it we wouldn't be able to resume
the state on a 32bit machine written by a 64bit machine.

Signed-off-by: Stefan Berger <stefanb@linux.vnet.ibm.com>
2018-06-15 18:07:48 -04:00
Stefan Berger
8822699fb2 tpm2: fix marshalling/unmarshalling of BOOL for big endian
BOOL is an int and therefore we cannot just write out the
single byte at the address of the BOOL. On big endian systems
the BOOL value is at offset 3. So we implement functions for
marshalling and unmarshalling of a BOOL as a single byte and
do the conversion with the 'int' there.

Signed-off-by: Stefan Berger <stefanb@linux.vnet.ibm.com>
2018-06-15 18:07:48 -04:00
Stefan Berger
245dac9b71 tpm2: Prepend header to NVRAM written structs and roundup their sizes
Round up the sizes of the structures written into NVRAM so we
have some space in front of them.

Prepend a heaer in front of the structure written into NVRAM. Initialize
them with a version number and a magic. The version number should
theoretically allow us to read TPM 2 state of different revisions.

Signed-off-by: Stefan Berger <stefanb@linux.vnet.ibm.com>
2018-06-15 18:07:47 -04:00
Stefan Berger
3eaf453125 tpm2: Write TPMEstablished BOOL in volatile state
Write the TPMEstablished BOOL in volatile state.

Signed-off-by: Stefan Berger <stefanb@linux.vnet.ibm.com>
2018-06-15 18:07:47 -04:00
Stefan Berger
fad1e91698 tpm2: Add functions to write state in big endian format
Add functions to marshal the volatile state of the TPM. We write it in
big endian format.

Add functions to write the data structure in big endian format
into NVRAM.

Signed-off-by: Stefan Berger <stefanb@linux.vnet.ibm.com>
2018-06-15 18:07:47 -04:00
Stefan Berger
a6d8f3f3c1 Enable TPM 2 on big endian systems
Enable TPM 2 on big endian systems.

Signed-off-by: Stefan Berger <stefanb@linux.vnet.ibm.com>
2018-06-15 18:07:47 -04:00
Stefan Berger
468f0f3d96 tpm2: Clean up the NV_LIST_TERMINATOR structure
Clean up code around the NV_LIST_TERMINATOR structure. It's not
an array of bytes or UINT32s, but a UINT32 followed by a UINT64.
2018-06-15 18:07:47 -04:00
Stefan Berger
00f85ac180 Support for TPMEstablished flag in TPM2 code
Resolve a few TPMEstablishment bit related FIXMEs by implementing
supporting functions.

Signed-off-by: Stefan Berger <stefanb@linux.vnet.ibm.com>
2018-06-15 18:07:47 -04:00
Stefan Berger
0eaae34cc5 tpm2: Return failure code if TPM was put into failure mode
Return a failure code to the control command initiated
initialization of the TPM 2.

Reset the failure move on every INIT call so that corrections
can be done without having to restart the TPM emulator.

Signed-off-by: Stefan Berger <stefanb@linux.vnet.ibm.com>
2018-06-15 18:07:47 -04:00
Stefan Berger
3cf528aaac Integrate the TPM 2 into the library; do not compile with TPM 2 yet
Introduce --with-tpm2 for ./configure to enable building with
TPM 2 functionality. Delay the building of TPM 2 code until more
patches are applied and the vTPM state that's created has a chance
of being backwards compatible.

Extend the libtpms API to allow user to choose version of TPM.

Missing functionality at this point:
- TPM 2 needs to be extended to serialize and deserialize its volatile state
- Handling of the establishment bit

Signed-off-by: Stefan Berger <stefanb@linux.vnet.ibm.com>
2018-06-15 18:07:47 -04:00
Stefan Berger
7675c9371c Revert "Fix TPM_PROOF to work with older versions"
This reverts commit 986eb59f13f12c0452895c7c0fbbffb338787132.
2018-06-15 18:07:47 -04:00