Commit Graph

327 Commits

Author SHA1 Message Date
Stefan Berger
a3a1cd0522 tpm2: Fix public key context save due to ANY_OBJECT_Marshal usage
This patch fixes a VM suspend/resume issue when externally loaded public
keys are involved.

This patch addresses the bug reported in issue #195 where the saving of
an externally loaded public key's context doesn't work due to the usage of
ANY_CONTEXT_SAVE for saving key contexts. This patch fixes the issue by
creating local versions of TPM_SENSITIVE_Marshal/_Unmarshal that deals
with the case where sensitiveType is not a type of private key but a
public key instead that basically doesn't have much information in
TPM_SENSITIVE but is all zeros instead.

Signed-off-by: Stefan Berger <stefanb@linux.ibm.com>
2021-02-26 18:06:33 -05:00
Stefan Berger
f88f3381aa tpm12: Use TPM_PrintFourLimit where <= 4 bytes may exist
Use TPM_PrintFourLimit in those cases where less than 4 bytes of
valid data may exist. Hashes, nonces, encrypted data, and others
typically have more than 4 bytes but data read from NVRAM or
to be encrypted data may have less.

Signed-off-by: Stefan Berger <stefanb@linux.ibm.com>
2021-02-26 18:06:33 -05:00
Stefan Berger
889fb56f5f tpm12: Implement TPM_PrintFourLimit to print 4 or less bytes
Implement TPM_PrintFourLimit to print only up to 4 bytes or less.

Signed-off-by: Stefan Berger <stefanb@linux.ibm.com>
2021-02-26 18:06:33 -05:00
Stefan Berger
3aac390ce1 tpm2: Fix issue with misaligned address when marshalling NVRAM (UBSAN)
UBSAN detects possibly misaligned address when reading out of the
TPM 2's NVRAM and when writing back into it. The NV_RAM_HEADER may
be unaligned like this:

tests/test_tpm2_save_load_state_3.log:tpm2/Marshal.c:117:29: \
  runtime error: load of misaligned address 0x7ffcb53b3bca for type 'UINT32', which requires 4 byte alignment

Signed-off-by: Stefan Berger <stefanb@linux.ibm.com>
2021-02-26 18:06:33 -05:00
Stefan Berger
ad95a32cf2 tpm2: fix PCRBelongsTCBGroup for PCClient (bugfix)
Fix PCRBelongsTCBGroup by adjusting the set of PCRs that belong to the TCB
Group. The effect of this is that PCR changes to PCR 16 (for example) do
not change the pcrUpdateCounter anymore. The effect *should not* have any
negative side effects when using the TPM.

We also need to update the test cases that now show a different
pcrUpdateCounter in the responses. Also 'swtpm' test cases need
to be fixed to expect the changed result.

Signed-off-by: Stefan Berger <stefanb@linux.ibm.com>
2020-06-29 08:48:45 -04:00
Stefan Berger
de7160b127 tpm2: Remove space after 'IBM' in MANUFACTURER
Signed-off-by: Stefan Berger <stefanb@linux.ibm.com>
2020-05-18 12:19:47 -04:00
Stefan Berger
a01de0b2e5 tpm2: Add missing EC Curve cases in Unmarshal function
Add missing EC Curve cases in Unmarshal function.
Also, don't accept curves that are not usable during runtime because OpenSSL
may not support them.

Signed-off-by: Stefan Berger <stefanb@linux.ibm.com>
2020-05-18 12:19:47 -04:00
Stefan Berger
579499bc6a tpm2: Runtime filter useable EC Curves before advertising them
Runtime filter useable EC Curves before advertising them as being
supported.

Signed-off-by: Stefan Berger <stefanb@linux.ibm.com>
2020-05-18 12:19:47 -04:00
Stefan Berger
91e7a1a37d tpm12: Initialize a few variables for x86 gcc -O3
Backported for gcc 10.

gcc (Ubuntu 5.4.0-6ubuntu1~16.04.12) 5.4.0 20160609

The above gcc on x86 with -O3 reports the following false positives:

 gcc -DHAVE_CONFIG_H -I. -I.. -include tpm_library_conf.h -I../include/libtpms -I../include/libtpms -fstack-protector-strong -DTPM_V12 -DTPM_PCCLIENT -DTPM_VOLATILE_LOAD -DTPM_ENABLE_ACTIVATE -DTPM_AES -DTPM_LIBTPMS_CALLBACKS -DTPM_NV_DISK -DTPM_POSIX -DTPM_NOMAINTENANCE_COMMANDS -O3 -DUSE_OPENSSL_FUNCTIONS_SYMMETRIC=1 -DUSE_OPENSSL_FUNCTIONS_EC=1 -DUSE_OPENSSL_FUNCTIONS_ECDSA=0 -DUSE_OPENSSL_FUNCTIONS_RSA=0 -Wall -Werror -Wreturn-type -Wsign-compare -Wno-self-assign -MT tpm12/libtpms_tpm12_la-tpm_nvram.lo -MD -MP -MF tpm12/.deps/libtpms_tpm12_la-tpm_nvram.Tpo -c tpm12/tpm_nvram.c -o tpm12/libtpms_tpm12_la-tpm_nvram.o
tpm12/tpm_nvram.c: In function ‘TPM_Process_NVReadValue’:
tpm12/tpm_nvram.c:1539:38: error: ‘isGPIO’ may be used uninitialized in this function [-Werror=maybe-uninitialized]
      if ((returnCode == TPM_SUCCESS) && !isGPIO) {
                                      ^
tpm12/tpm_nvram.c: In function ‘TPM_Process_NVWriteValue’:
tpm12/tpm_nvram.c:2323:6: error: ‘isGPIO’ may be used uninitialized in this function [-Werror=maybe-uninitialized]
   if (!isGPIO) {
      ^

gcc -DHAVE_CONFIG_H -I. -I.. -include tpm_library_conf.h -I../include/libtpms -I../include/libtpms -fstack-protector-strong -DTPM_V12 -DTPM_PCCLIENT -DTPM_VOLATILE_LOAD -DTPM_ENABLE_ACTIVATE -DTPM_AES -DTPM_LIBTPMS_CALLBACKS -DTPM_NV_DISK -DTPM_POSIX -DTPM_NOMAINTENANCE_COMMANDS -O3 -DUSE_OPENSSL_FUNCTIONS_SYMMETRIC=1 -DUSE_OPENSSL_FUNCTIONS_EC=1 -DUSE_OPENSSL_FUNCTIONS_ECDSA=0 -DUSE_OPENSSL_FUNCTIONS_RSA=0 -Wall -Werror -Wreturn-type -Wsign-compare -Wno-self-assign -MT tpm12/libtpms_tpm12_la-tpm_process.lo -MD -MP -MF tpm12/.deps/libtpms_tpm12_la-tpm_process.Tpo -c tpm12/tpm_process.c -o tpm12/libtpms_tpm12_la-tpm_process.o
tpm12/tpm_process.c: In function ‘TPM_Process_GetCapabilitySigned’:
tpm12/tpm_process.c:5089:19: error: ‘transportEncrypt’ may be used uninitialized in this function [-Werror=maybe-uninitialized]
      returnCode = TPM_ProcessAudit(tpm_state,
                   ^
tpm12/tpm_process.c: In function ‘TPM_Process_SetCapability’:
tpm12/tpm_process.c:5309:19: error: ‘transportEncrypt’ may be used uninitialized in this function [-Werror=maybe-uninitialized]
      returnCode = TPM_ProcessAudit(tpm_state,
                   ^
tpm12/tpm_process.c: At top level:

gcc -DHAVE_CONFIG_H -I. -I.. -include tpm_library_conf.h -I../include/libtpms -I../include/libtpms -fstack-protector-strong -DTPM_V12 -DTPM_PCCLIENT -DTPM_VOLATILE_LOAD -DTPM_ENABLE_ACTIVATE -DTPM_AES -DTPM_LIBTPMS_CALLBACKS -DTPM_NV_DISK -DTPM_POSIX -DTPM_NOMAINTENANCE_COMMANDS -O3 -DUSE_OPENSSL_FUNCTIONS_SYMMETRIC=1 -DUSE_OPENSSL_FUNCTIONS_EC=1 -DUSE_OPENSSL_FUNCTIONS_ECDSA=0 -DUSE_OPENSSL_FUNCTIONS_RSA=0 -Wall -Werror -Wreturn-type -Wsign-compare -Wno-self-assign -MT tpm12/libtpms_tpm12_la-tpm_transport.lo -MD -MP -MF tpm12/.deps/libtpms_tpm12_la-tpm_transport.Tpo -c tpm12/tpm_transport.c -o tpm12/libtpms_tpm12_la-tpm_transport.o
tpm12/tpm_transport.c: In function ‘TPM_Process_ReleaseTransportSigned’:
tpm12/tpm_transport.c:2810:42: error: ‘t1TpmTransportInternal’ may be used uninitialized in this function [-Werror=maybe-uninitialized]
  returnCode = TPM_TransportLogOut_Extend(t1TpmTransportInternal->transDigest,
                                          ^

This patch initializes the variables.

Signed-off-by: Stefan Berger <stefanb@linux.ibm.com>
2020-05-12 14:03:40 -04:00
Stefan Berger
b6f135d6a8 tpm12: Initialize some variables for gcc ppc64el compiler
Backported for gcc 10.

gcc (Ubuntu 9.2.1-21ubuntu1) 9.2.1 20191130

The gcc compiler on Ubuntu Focal reports several false positives for
potentially uninitialized variables:

tpm12/tpm_session.c: In function ‘TPM_Process_SaveContext’:
tpm12/tpm_session.c:3229:19: error: ‘tpm_auth_session_data’ may be used uninitialized in this function [-Werror=maybe-uninitialized]
 3229 |      returnCode = TPM_AuthSessionData_Store(&r1ContextSensitive, tpm_auth_session_data);
      |                   ^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

tpm12/tpm_delegate.c: In function ‘TPM_Process_DelegateManage’:
tpm12/tpm_delegate.c:1787:49: error: ‘familyRow’ may be used uninitialized in this function [-Werror=maybe-uninitialized]
 1787 |  if ((opCode != TPM_FAMILY_CREATE) && (familyRow->flags & TPM_DELEGATE_ADMIN_LOCK)) {
      |                                        ~~~~~~~~~^~~~~~~
tpm12/tpm_delegate.c: In function ‘TPM_Process_DelegateUpdateVerification’:
tpm12/tpm_delegate.c:3575:48: error: ‘d1DelegateTableRow’ may be used uninitialized in this function [-Werror=maybe-uninitialized]
 3575 |      d1DelegateTableRow->pub.verificationCount = familyRow->verificationCount;
      |      ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

All of the variables are initialize under the same condition as they are
accessed.

Signed-off-by: Stefan Berger <stefanb@linux.ibm.com>
2020-05-12 14:03:40 -04:00
Stefan Berger
2f0bd3974a tpm2: Fix TDES key creation by adding missing un-/marshalling functions
This is a backport of 2da6f27c33.

Some TDES related marshalling and unmarshalling functions were
missing, so add them. Now TDES keys can be created.

Signed-off-by: Stefan Berger <stefanb@linux.ibm.com>
2019-12-27 13:46:59 -05:00
Stefan Berger
b16f1e5953 tpm2: Fix a bug in CheckAuthSession
This is a backport of e58cbdd4fd.

This patch fixes a bug described in the following TCG Document

ERRATA; Errata Version 1.9, August 23, 2019; For TCG Trusted Platform
Module Library; Family "2.0", Level 00, Revision 1.38, Sept 29, 2016

section 2.34: NV PIN Indices

Signed-off-by: Stefan Berger <stefanb@linux.ibm.com>
2019-12-21 12:09:59 -05:00
Stefan Berger
f01cef4899 tpm12: Fix potential buffer overflow in filename creation
Fix a potential buffer overflow bug in the creation of filenames
that were using sprintf() rather than snprintf(). The buffer overflow
could occurr if the buffer is longer than 4096 bytes. The state path
may alone be 4096 bytes and could possibly trigger the overflow.

Swtpm for example is not affected from this since it uses the callbacks
that are invoked before the faulty function is called.

Signed-off-by: Stefan Berger <stefanb@linux.ibm.com>
2019-10-17 12:59:35 -04:00
Stefan Berger
f74b7104c0 tpm2: Check data size to be at least size of hash
Check the size of the available data before hashing them. A minimum
of 20 bytes needs to be passed into the function so that we can hash
the data 'before' it.

Signed-off-by: Stefan Berger <stefanb@linux.ibm.com>
Reported-by: Yi Ren <yunye.ry@alibaba-inc.com>
2019-08-06 10:30:12 -04:00
Stefan Berger
b244bdf6e2 tpm2: Add CMAC test cases from RFC 4493
Signed-off-by: Stefan Berger <stefanb@linux.ibm.com>
2019-05-10 09:42:35 -04:00
Stefan Berger
94e8f9a744 tpm2: Reset the internal CMAC state when starting a CMAC
Initialize the internal CMAC state to 0s when starting a CMAC.

Signed-off-by: Stefan Berger <stefanb@linux.ibm.com>
2019-05-10 09:42:35 -04:00
Stefan Berger
48d7de2f22 Add missing #define TPM_HAVE_TPM2_DECLARATIONS in tpm2 code (RHEL 6)
Add some missing #define TPM_HAVE_TPM2_DECLARATIONS before the include
of "tpm_library_intern.h" in TPM 2 code so we don't run into compile
errors on RHEL 6 when data types are redefined in TPM 1.2 code.

Previous patch 73cad883ba seems to have missed those.

Signed-off-by: Stefan Berger <stefanb@linux.ibm.com>
2019-04-12 12:48:28 -04:00
Stefan Berger
e4795abfab tpm2: Adapt code for OpenSSL 1.2 deprecated API calls
Some OpenSSL EC API calls are deprecated starting in OpenSSL 1.2
and may fail the build due to the #warning they produce.

The affected functions have been renamed and consolidated with
other functions inside OpenSSL:

- EC_POINT_set_affine_coorindates_GFp
https://github.com/openssl/openssl/blob/OpenSSL_1_1_1-stable/crypto/ec/ec_lib.c#L730

- EC_POINT_get_affine_coordinates_GFp
https://github.com/openssl/openssl/blob/OpenSSL_1_1_1-stable/crypto/ec/ec_lib.c#L768

On both functions the suffix _GFp has been removed by the renaming.
We need to call the new functions if OPENSSL_API_COMPAT is defined
and is >= 0x10200000L.

Signed-off-by: Stefan Berger <stefanb@linux.ibm.com>
2019-03-19 09:37:34 -04:00
Stefan Berger
139925229d build-sys: Add support for --enable-test-coverage to configure
Add support for --enable-test-coverage that sets additional CFLAGS
and LDFLAGS.

gcov creates files with suffixes .gcov, .gcno, and .gcda that we
need clean up in a few directories.

Signed-off-by: Stefan Berger <stefanb@linux.ibm.com>
2019-01-09 11:51:41 -05:00
Stefan Berger
9070c15d72 tpm2: Remove unnecessary LITTLE_ENDIAN_TPM #define for __APPLE__
Remove an unnecessary LITTLE_ENDIAN_TPM #define for the __APPLE__
case.

Signed-off-by: Stefan Berger <stefanb@linux.ibm.com>
2019-01-07 16:40:03 +00:00
Stefan Berger
8cb5f7d4c0 tpm2: Propagate error from CryptRandStartup() and report error info
CryptRandStartup() did not propagate initialization errors to the caller
but always returned TRUE. We now return the initialization errors and
display an error message and some state variables.

This error should almost never occur, but the fuzzer was able to trigger
it with rev 146 when the random number generated returned 32 bit numbers only
and the same 32 bit number was returned as the one before it. With rev 150
the random number generating function now returns as many random numbers as
the caller requested and this error is not getting triggered anymore.

Signed-off-by: Stefan Berger <stefanb@linux.ibm.com>
2019-01-03 13:15:44 -05:00
Stefan Berger
b3509cce84 rev150: Remove unnecessary parameters from unmarshaling functions
The first functions in UnmarshalArray before HANDLE_FIRST_FLAG_TYPE
must not have an additional flag and so we remove it.

Signed-off-by: Stefan Berger <stefanb@linux.ibm.com>
2019-01-03 13:15:44 -05:00
Stefan Berger
1bd13f5bb5 tpm2: fix undefined behavior of macro expansion
Fix the preprocessor warning:

macro expansion producing 'defined' has undefined behavior

Signed-off-by: Stefan Berger <stefanb@linux.ibm.com>
2019-01-03 13:15:44 -05:00
Stefan Berger
a433bf1217 tpm2: Coverity: Deactivate dead functions
CryptHashCopyState is not called from anywhere and the useless
call to CryptGetHashDef() it does obviously doesn't matter. Deactivate
the whole function using #if 0.

NvClearPersistent() and NvUpdatePersistent() are not used, either,
so we can also deactivate them and suppress a pointer related issue
that Coverity is complaining about.

Signed-off-by: Stefan Berger <stefanb@linux.ibm.com>
2019-01-03 13:15:44 -05:00
Stefan Berger
dc55da502e rev150: Update spec defines to revision 150
Signed-off-by: Stefan Berger <stefanb@linux.ibm.com>
2019-01-03 13:15:44 -05:00
Stefan Berger
37196ce15a rev150: Remove obsolete CryptoHashData.h
Signed-off-by: Stefan Berger <stefanb@linux.ibm.com>
2019-01-03 13:15:44 -05:00
Stefan Berger
c8ef72d930 rev150: Replace BnEccData.c with CryptEccData.c 2019-01-03 13:15:44 -05:00
Stefan Berger
db8a15798d rev150: Add #error to prevent users from defining SIMULATION
Signed-off-by: Stefan Berger <stefanb@linux.ibm.com>
2019-01-03 13:15:44 -05:00
Stefan Berger
e327ce3c50 tpm2: Set LIBRARY_COMPATIBILITY_CHECK to YES
Signed-off-by: Stefan Berger <stefanb@linux.ibm.com>
2019-01-03 13:15:44 -05:00
Stefan Berger
509c51e803 rev150: Add LIBRARY_COMPATIBILITY_CHECK build switch
Signed-off-by: Stefan Berger <stefanb@linux.ibm.com>
2019-01-03 13:15:44 -05:00
Stefan Berger
4f9e942d49 rev150: Fix a bug in TPM2_Certify()
Signed-off-by: Stefan Berger <stefanb@linux.ibm.com>
2019-01-03 13:15:44 -05:00
Stefan Berger
e69b7ee294 rev150: Reformatting of code
Reformatting of code and some fixes in code paths not used by libtpms.

Signed-off-by: Stefan Berger <stefanb@linux.ibm.com>
2019-01-03 13:15:44 -05:00
Stefan Berger
380b232ec9 rev150: Code comment changes only
Signed-off-by: Stefan Berger <stefanb@linux.ibm.com>
2019-01-03 13:15:44 -05:00
Stefan Berger
141a71cdbd tpm2: Remove Bn2bin conversion function used during OSSL 1.1 conversion
Bn2bin was used during the conversion of the code for supporting OSSL 1.1
and testing the math results of < OSSL 1.1 against those produced by
the bignum code in the TPM 2. It's not needed anymore since the upstream
code has support for any OSSL version now.

Signed-off-by: Stefan Berger <stefanb@linux.vnet.ibm.com>
2019-01-03 13:15:44 -05:00
Stefan Berger
cd93aacfab tpm2: Remove NVRAM marshalling/unmarshalling test code
The test code for marshalling and unmarshalling the NVRAM hasn't
been used anymore, so remove it.

Signed-off-by: Stefan Berger <stefanb@linux.vnet.ibm.com>
2019-01-03 13:15:44 -05:00
Stefan Berger
ee48021bfd tpm2: Remove code verifying math results
Signed-off-by: Stefan Berger <stefanb@linux.vnet.ibm.com>
2019-01-03 13:15:44 -05:00
Stefan Berger
3cd5a386f5 tpm2: Mark all libtpms changes with comments
Mark all libtpms changes with comments for easy identification
when diff'ing with the original TPM code.

Signed-off-by: Stefan Berger <stefanb@linux.vnet.ibm.com>
2019-01-03 13:15:44 -05:00
Stefan Berger
2bfed46bbc tpm2: Fix an error in BnValidateSignatureEcSm2()
Signed-off-by: Stefan Berger <stefanb@linux.vnet.ibm.com>
2019-01-03 13:15:44 -05:00
Stefan Berger
1fc753b325 rev149: Update spec version to 149
Signed-off-by: Stefan Berger <stefanb@linux.vnet.ibm.com>
2019-01-03 13:15:44 -05:00
Stefan Berger
bb2134a40b rev149: Sync code related to FILE_BACKED_NV #define
Signed-off-by: Stefan Berger <stefanb@linux.vnet.ibm.com>
2019-01-03 13:15:44 -05:00
Stefan Berger
6df36cb56a rev149: Fix Initilazation of EPSeed if USE_PLATFORM_EPS is defined
Signed-off-by: Stefan Berger <stefanb@linux.vnet.ibm.com>
2019-01-03 13:15:44 -05:00
Stefan Berger
24f6798005 rev149: Set retVal to initial value when defining it
Signed-off-by: Stefan Berger <stefanb@linux.vnet.ibm.com>
2019-01-03 13:15:44 -05:00
Stefan Berger
10aa33d97e rev149: Change #if ALG_SM3 to #if ALG_SM3_256
Signed-off-by: Stefan Berger <stefanb@linux.vnet.ibm.com>
2019-01-03 13:15:44 -05:00
Stefan Berger
ec7f2c6d39 rev149: Move #if SIMULATION into function
Signed-off-by: Stefan Berger <stefanb@linux.vnet.ibm.com>
2019-01-03 13:15:44 -05:00
Stefan Berger
ba8f4aeed3 rev149: add pAssert to NvClearPersistent
Signed-off-by: Stefan Berger <stefanb@linux.vnet.ibm.com>
2019-01-03 13:15:44 -05:00
Stefan Berger
5c7f5f49b2 rev149: Use #if define SELF_TEST rather than #if SELF_TEST
Signed-off-nby: Stefan Berger <stefanb@linux.vnet.ibm.com>
2019-01-03 13:15:44 -05:00
Stefan Berger
9485a2fc08 rev149: Fix #define consistency check for big endian
Signed-off-by: Stefan Berger <stefanb@linux.vnet.ibm.com>
2019-01-03 13:15:44 -05:00
Stefan Berger
6b98d9c111 rev149: Remove usage of INLINE_FUNCTIONS #define
Signed-off-by: Stefan Berger <stefanb@linux.vnet.ibm.com>
2019-01-03 13:15:44 -05:00
Stefan Berger
5b0e6d6b7b rev149: Change CryptRandomStir to take UINT32 parameter
Signed-off-by: Stefan Berger <stefanb@linux.vnet.ibm.com>
2019-01-03 13:15:44 -05:00
Stefan Berger
81ee72f482 rev149: Comment and reformatting changes
Signed-off-by: Stefan Berger <stefanb@linux.vnet.ibm.com>
2019-01-03 13:15:44 -05:00