Commit Graph

1178 Commits

Author SHA1 Message Date
Stefan Berger
f9025d9b86 tpm2: Filter bad input values to avoid underflow in FindNthSetBit (Coverity)
Address the following Coverity complaint (1550494) by filtering out bad
input values:

  "Expression i--, which is equal to 65535, where i is known to be equal
   to 0, underflows the type that receives it, an unsigned integer 16 bits
   wide."

aSize is typcially 2048 and n is always >= 1 per the input parameter.
Therefore no side-effects are expected from this filter.

Signed-off-by: Stefan Berger <stefanb@linux.ibm.com>
2024-07-13 20:10:09 -04:00
Stefan Berger
9d3d010951 tpm2: Address a possible unsigned integer underflow (Coverity)
Resolve the following Coverity complaint:

  "Expression command->sessionNum - 1U, which is equal to 4294967295,
   where command->sessionNum is known to be equal to 0, underflows the
   type that receives it, an unsigned integer 32 bits wide."

Signed-off-by: Stefan Berger <stefanb@linux.ibm.com>
2024-07-13 20:10:09 -04:00
Stefan Berger
4945cdcf13 tpm2: Remove assigned to value to offset because it is unused (Coverity)
Resolve the following Coverity complaint by removing assignment to offset:

  "Assigning value from offset + 148UL to offset here, but that stored
   value is overwritten before it can be used."

Signed-off-by: Stefan Berger <stefanb@linux.ibm.com>
2024-07-13 20:10:09 -04:00
Stefan Berger
6228582683 tpm2: Initialize rsa3072_object._pad (Coverity)
Resolve the following Coverity complaint:

 "Using uninitialized value rsa3072_object. Field rsa3072_object._pad is
  uninitialized when calling MemoryCopy."

Signed-off-by: Stefan Berger <stefanb@linux.ibm.com>
2024-07-13 20:10:09 -04:00
Stefan Berger
9cd9fcf7ac tpm2: Initialize eccPublic before passing to TPMS_ECC_POINT_Unmarshal (Coverity)
Resolve the following Coverity complaint:

  "Using uninitialized value eccPublic when calling TPMS_ECC_POINT_Unmarshal."

Signed-off-by: Stefan Berger <stefanb@linux.ibm.com>
2024-07-13 20:10:09 -04:00
Stefan Berger
24d38994b4 tpm2: Preserve more *target and restore them if neede (Coverity)
Signed-off-by: Stefan Berger <stefanb@linux.ibm.com>
2024-07-13 20:10:09 -04:00
Stefan Berger
2bef6b6d64 tpm12: Replace include of engine.h with err.h
Fedora Rawhide and CentOS 10 do not support OpenSSL engine anymore.
Therefore, replace include of engine.h with err.h since the engine is not
needed anyway but we only need the prototype of ERR_get_error_line_data.

Signed-off-by: Stefan Berger <stefanb@linux.ibm.com>
2024-07-08 16:45:33 -04:00
Stefan Berger
07962448cf rev180: Add TPM2_PolicyCapability and dependencies
Signed-off-by: Stefan Berger <stefanb@linux.ibm.com>
2024-07-02 16:49:21 -04:00
Stefan Berger
269a409dad rev180: Sync _plat__NVDisable with upstream
Signed-off-by: Stefan Berger <stefanb@linux.vnet.ibm.com>
2024-07-02 11:20:18 -04:00
Stefan Berger
b2f161e8f7 tpm2: Add missing 'libtpms added/changed' comments
Signed-off-by: Stefan Berger <stefanb@linux.vnet.ibm.com>
2024-07-02 09:41:43 -04:00
Stefan Berger
8cde289747 rev180: Introduce IsCpHashUnionOccupied and use it in simple case
Replace the check for several session attributes flags with a call
to IsCpHashUnionOccupied.

Note that the existing check for

session->u1.cpHash.b.size != 0 || session->attributes.isCpHashDefined

can be replaced with just session->attributes.isCpHashDefined since
isCpHashDefined is always assigned '1' (SET) when session.u1.cpHash
is given a value. isCpHashDefined is reset in SessionResetPolicyData()
as part of resetting all attribute flags and cpHash size is set to 0.

Signed-off-by: Stefan Berger <stefanb@linux.vnet.ibm.com>
2024-06-28 17:22:06 -04:00
Stefan Berger
9d3496c253 rev180: Rename isTemplateSet to isTemplateHashDefined
Signed-off-by: Stefan Berger <stefanb@linux.vnet.ibm.com>
2024-06-28 16:08:28 -04:00
Stefan Berger
159271c039 rev180: Test KeyUsage flags in X509ProcessExtensions (bugfix?)
Signed-off-by: Stefan Berger <stefanb@linux.vnet.ibm.com>
2024-06-28 13:23:38 -04:00
Stefan Berger
4c289ce47d tpm2: Add missing 'libtpms added' comments
Signed-off-by: Stefan Berger <stefanb@linux.vnet.ibm.com>
2024-06-28 13:23:38 -04:00
Stefan Berger
d1d6f3b3f7 rev180: Rename selftest-related #defines
Signed-off-by: Stefan Berger <stefanb@linux.vnet.ibm.com>
2024-06-28 13:23:38 -04:00
Stefan Berger
fb5d414acb tpm2: Remove trailing space in MANUFACTURER #define
Restore the MANUFACTURER string "IBM" to what it was before commit
6dcb416ddf.

Signed-off-by: Stefan Berger <stefanb@linux.ibm.com>
2024-06-18 14:45:58 -04:00
orbea
93a827aecc tpm12: add missing openssl includes
This fixes the build with LibreSSL 3.9.0 where many implicit
declarations for BN_, EVP_ and RSA_ functions occur which were
implicitly included before.

Signed-off-by: orbea <orbea@riseup.net>
2024-03-13 16:00:55 -04:00
Stefan Berger
87691f7a71 tpm2: Allow 4096 bit RSA key sizes once enabled by RSA_4096
Signed-off-by: Stefan Berger <stefanb@linux.ibm.com>
2024-03-05 21:13:57 -05:00
Stefan Berger
d0f5b67ea0 tpm2: NVMarshal: Use NvObjectToBuffer to prepare an OBJECT for NVRAM
Convert an OBJECT to a byte buffer suitable for writing into NVRAM
by calling NvObjectToBuffer and then copy the buffer into NVRAM.

Signed-off-by: Stefan Berger <stefanb@linux.ibm.com>
2024-03-05 21:13:57 -05:00
Stefan Berger
86dee38f9b tpm2: Use marshalling to write larger OBJECTs into NVRAM
Instead of copying an OBJECT directly from memory into NVRAM memory
determine the type of OBJECT (RSA vs. ECC etc.) and marshal the OBJECT
as ANY_OBJECT into a buffer and copy the buffer into NVRAM. To maintain
backwards compatibility copy RSA keys with size 3072 and smaller
directly into NVRAM (OBJECT is copied as before).

When reading an OBJECT out of NVRAM try to unmarshal it as an
ANY_OBJECT first and if this does not work it is with great likelyhood
an RSA3072_OBJECT that needs to be copied from NVRAM and than
transformed into an OBJECT.

The new marshalling of OBJECTS will only be used once RSA 4096 keys
are enabled or other changes to the code are made.

Signed-off-by: Stefan Berger <stefanb@linux.ibm.com>
2024-03-05 21:13:57 -05:00
Stefan Berger
bb4dafb1e7 tpm2: NVMarshal: Introduce MAX_MARSHALLED_OBJECT_SIZE
Introduce the #define MAX_MARSHALLED_OBJECT_SIZE to be used for a size of
a byte buffer that is supposed to hold a marshalled OBJECT. The number
is not exact but provides for a 'safe' size of a buffer.

Add a test case to check that MAX_MARSHALLED_OBJECT_SIZE is sufficient.
The test case needs access to ANY_OBJECT_Marshal, which is only available
when -static can be used for linking since otherwise the function is
private to the library. Static linking the test case does not work when
test-coverage is enabled, therefore disable statically linked test cases
in when test-coverage is enabled and give control to the user to disable
statically linked test cases in other cases as well.

Signed-off-by: Stefan Berger <stefanb@linux.ibm.com>
2024-03-05 21:13:57 -05:00
Stefan Berger
37f8e2f2c8 tpm2: Introduce RSA3072_OBJECT and implement conversion functions
Take a snapshot of the current OBJECT and call this RSA3072_OBJECT that
gets its own set of dependent datatypes that represent the current
OBJECT's dependent datatypes and only fit an RSA 3072 object (or smaller).

This allows for extension to RSA 4096 or possibly later on to
quantum-safe crypto keys that may also be stored in an OBJECT.

Signed-off-by: Stefan Berger <stefanb@linux.ibm.com>
2024-03-05 21:13:57 -05:00
Stefan Berger
eea4fd7bab tpm2: Clean up RSA2048_OBJECT_To_OBJECT
Only convert the private exponent for an RSA key and initialize
the privateExponent with zeros otherwise.

Signed-off-by: Stefan Berger <stefanb@linux.ibm.com>
2024-03-05 21:13:57 -05:00
Stefan Berger
c4390d0d66 build-sys: Display whether test coverage is enabled
Signed-off-by: Stefan Berger <stefanb@linux.ibm.com>
2024-03-05 21:13:57 -05:00
Stefan Berger
5679903b34 rev180: Rearrange order of TPMI_ECC_CURVE_P_UNMARSHAL in unmarshalArray
After adding BOOL allowNull parameter to TPMI_ECC_CURVE_Unmarshal rearrange
the order in which this command is listed in the unmarshalArray so that the
caller passes an addtional parameter to this function now.

Also add a fuzzer test case.

Fixes: 44f0cb0535 ("rev180: Sync TPMI_ECC_CURVE_Unmarshal")
Signed-off-by: Stefan Berger <stefanb@linux.ibm.com>
2024-02-28 11:04:12 -05:00
Stefan Berger
5cefa0a117 tpm2: Sync EntityGetLoadStatus with upstream
Signed-off-by: Stefan Berger <stefanb@linux.ibm.com>
2024-02-27 13:28:24 -05:00
Stefan Berger
f9e52bd044 rev180: Replace handle with HierarchyNormalizeHandle(handle)
Signed-off-by: Stefan Berger <stefanb@linux.ibm.com>
2024-02-27 12:18:03 -05:00
Stefan Berger
44f0cb0535 rev180: Sync TPMI_ECC_CURVE_Unmarshal
Signed-off-by: Stefan Berger <stefanb@linux.ibm.com>
2024-02-27 12:18:03 -05:00
Stefan Berger
5ad0256d1a rev180: Sync comment and whitespace changes
Signed-off-by: Stefan Berger <stefanb@linux.ibm.com>
2024-02-27 12:18:03 -05:00
Stefan Berger
23ab02a1c2 tests: oss-fuzz.sh: Append -fno-sanitize=bounds to CFLAGS
The new TPM 2 code casts bigNum's to Crypt_Int, which has an array 'd' of
type 'crypt_uword_t[2]' which then leads to the following types of errors
that we haven't found another solution for so far.

Running: /mnt/scratch0/clusterfuzz/bot/inputs/fuzzer-testcases/crash-2675de6341d4e056d04ab49179b8e5b8bd456589
	/src/libtpms/src/tpm2/crypto/openssl/CryptPrime.c:377:20: runtime error: index 23 out of bounds for type 'crypt_uword_t[2]' (aka 'unsigned long[2]')
 #0 0x5b4196 in RsaAdjustPrimeCandidate_New libtpms/src/tpm2/crypto/openssl/CryptPrime.c:377:20
 #1 0x5b4196 in RsaAdjustPrimeCandidate libtpms/src/tpm2/crypto/openssl/CryptPrime.c:405:9
 #2 0x5b4366 in TpmRsa_GeneratePrimeForRSA libtpms/src/tpm2/crypto/openssl/CryptPrime.c:454:6
 #3 0x590953 in CryptRsaGenerateKey libtpms/src/tpm2/crypto/openssl/CryptRsa.c:1433:9

Signed-off-by: Stefan Berger <stefanb@linux.ibm.com>
2024-01-15 14:53:19 -05:00
Stefan Berger
a5dfd26d45 tpm2: NVMarshal: To read an OBJECT from NVRAM use NvReadObject not NvRead
NvReadObject knows how to read an OBJECT from NVRAM, so use it instead
of the low-level NvRead(). This prepares the code for possibly larger
OBJECTs in NVRAM in the future that may have been marshalled before
they were written into NVRAM (such as RSA 4096 keys or possibly keys
from QC if they will be stored in OBJECTs).

The call to NvReadObject() requires that the ref in NvReadObject points
to the handle of an NV_ENTRY_HEADER. An NV_ENTRY_HEADER looks like this:

  typedef struct {
      UINT32      size;
      TPM_HANDLE  handle;
  } NV_ENTRY_HEADER;

Signed-off-by: Stefan Berger <stefanb@linux.ibm.com>
2024-01-13 14:22:41 -05:00
Stefan Berger
fd7e5d76a2 tpm2: NVMarshal: Indent some break statements
Signed-off-by: Stefan Berger <stefanb@linux.ibm.com>
2024-01-13 14:22:41 -05:00
Stefan Berger
6fb287ce22 tpm2: Rename OLD_OBJECT to RSA2048_OBJECT and rename function
Signed-off-by: Stefan Berger <stefanb@linux.ibm.com>
2024-01-11 13:38:29 -05:00
Stefan Berger
9718874672 tpm2: Move OLD_OBJECT to OBJECT conversion into own function
Signed-off-by: Stefan Berger <stefanb@linux.ibm.com>
2024-01-11 13:38:29 -05:00
Stefan Berger
abcd127b29 tpm2: Leave a FIXME in OBJECT_Marshal
Signed-off-by: Stefan Berger <stefanb@linux.ibm.com>
2024-01-11 13:38:29 -05:00
Stefan Berger
6063c0fbb2 tpm2: Add MUST_BE to ensure unions' largest TPM2Bs are marshalled
Signed-off-by: Stefan Berger <stefanb@linux.ibm.com>
2024-01-11 13:38:29 -05:00
Stefan Berger
1375b8a53b tpm2: Use MUST_BE in BackwardsCompatibilityObject.c
Signed-off-by: Stefan Berger <stefanb@linux.ibm.com>
2024-01-10 16:28:09 -05:00
Stefan Berger
134b33df38 rev180: Enable COMPILER_CHECKS (static asserts)
Signed-off-by: Stefan Berger <stefanb@linux.ibm.com>
2024-01-09 15:23:07 -05:00
Stefan Berger
6af38cf79e tpm2: Re-add include of openssl/ecdsa.h for LibreSSL
Signed-off-by: Stefan Berger <stefanb@linux.ibm.com>
2024-01-09 10:43:49 -05:00
Stefan Berger
e955d926df tpm2: Check libcrypto for EC_POINT_get_affine_coordinates availability
Check -lcrypto for availability of the more recent
EC_POINT_get_affine_coordinates function and use a #define to call it
instead of the older EC_POINT_get_affine_coordinates_GFp. Make the
same change to EC_POINT_set_affine_coordinates_GFp.

Signed-off-by: Stefan Berger <stefanb@linux.ibm.com>
2024-01-09 08:36:35 -05:00
Stefan Berger
4f2a8de359 rev180: Use CRYPT_CURVE_FREE to free CTX and G
Signed-off-by: Stefan Berger <stefanb@linux.ibm.com>
2024-01-04 20:27:25 -05:00
Stefan Berger
31fda0cd51 rev180: Fix a memory leak in CryptEccIsPointOnCurve
Signed-off-by: Stefan Berger <stefanb@linux.ibm.com>
2024-01-04 15:04:05 -05:00
Stefan Berger
db997a843b rev180: Fix possible NULL-pointer derefernces (Coverity)
Signed-off-by: Stefan Berger <stefanb@linux.ibm.com>
2024-01-03 20:23:19 -05:00
Stefan Berger
86faea6504 rev180: Sync header used by TPMCmdp.c
Signed-off-by: Stefan Berger <stefanb@linux.ibm.com>
2024-01-03 20:23:19 -05:00
Stefan Berger
3f106044aa rev180: Append 0-sized padding to state_reset_data
Signed-off-by: Stefan Berger <stefanb@linux.ibm.com>
2024-01-03 20:23:19 -05:00
Stefan Berger
db28927d48 rev180: Sync SM4 related compile-time checks with upstream
Signed-off-by: Stefan Berger <stefanb@linux.ibm.com>
2024-01-03 20:23:19 -05:00
Stefan Berger
6c96a8e0d1 rev180: Sync SM3-related compile-time checks upstream
Signed-off-by: Stefan Berger <stefanb@linux.ibm.com>
2024-01-03 20:23:19 -05:00
Stefan Berger
0ee8b4e34e rev180: Remove unused defines from TpmBuildSwitches.h
Signed-off-by: Stefan Berger <stefanb@linux.ibm.com>
2024-01-03 20:23:19 -05:00
Stefan Berger
f1764ace4c rev180: Sync externaly visible impl. alt. options in TpmBuildSwitches.h
Signed-off-by: Stefan Berger <stefanb@linux.ibm.com>
2024-01-03 20:23:19 -05:00
Stefan Berger
f09c116313 rev180: Sync implementation alternatives options in TpmBuildSwitches.h
Signed-off-by: Stefan Berger <stefanb@linux.ibm.com>
2024-01-03 20:23:19 -05:00