Commit Graph

1258 Commits

Author SHA1 Message Date
Stefan Berger
037eb0e2bb tpm2: Return TPM_RC_VALUE upon decryption failure
When decryption fails then return TPM_RC_VALUE rather than TPM_RC_FAILURE.
The old error code could indicate to an application or driver that
something is wrong with the TPM (has possibly gone into failure mode) even
though only the decryption failed, possibly due to a wrong key.

Signed-off-by: Stefan Berger <stefanb@linux.ibm.com>
2024-08-20 10:46:50 -04:00
Stefan Berger
d3ce650f93 tpm2: Include limits.h for UINT_MAX (BSD)
Signed-off-by: Stefan Berger <stefanb@linux.ibm.com>
2024-08-20 10:16:02 -04:00
Stefan Berger
4b89a028dd rev180: Sync whitespace changes and changes to comments
Signed-off-by: Stefan Berger <stefanb@linux.vnet.ibm.com>
2024-07-29 11:25:31 -04:00
Stefan Berger
ca3d592758 rev180: Add new function to generate RSA primes but do not use it
Some time around rev169 a new function to generate RSA prime numbers was
introduced. Add this function but keep it disabled.

Signed-off-by: Stefan Berger <stefanb@linux.ibm.com>
2024-07-29 08:43:35 -04:00
Stefan Berger
09a67f4588 tpm2: Move code out of RsaAdjustPrimeCandidate
To simplify the code and get rid of the libtpms-specific implementation
of RsaAdjustPrimeCandidate move its code into the case statements in
TpmRsa_GeneratePrimeForRSA.

Signed-off-by: Stefan Berger <stefanb@linux.ibm.com>
2024-07-29 08:43:35 -04:00
Stefan Berger
aa582228d6 tpm2: Move RsaAdjustPrimeCandidate_New
Revision 1.69 received new code for RsaAdjustPrimeCandidate. Before
adding the new function (while keeping it unused since it does not
add much value) move the existing function.

Signed-off-by: Stefan Berger <stefanb@linux.ibm.com>
2024-07-29 08:43:35 -04:00
Stefan Berger
a5affce5cd rev180: Add TpmMath_GetRandomBits but disable it since unused.
Add TPmMath_GetRandomBits but disable it since it will remain unused
for now.

Modify the code to properly mask the bits in the first byte of the byte
stream produced as big endian number.

Signed-off-by: Stefan Berger <stefanb@linux.ibm.com>
2024-07-29 08:43:35 -04:00
Stefan Berger
247a100cd8 tpm2: Add ecc-sm2-p256 to all profiles
Signed-off-by: Stefan Berger <stefanb@linux.ibm.com>
2024-07-26 22:34:38 -04:00
Stefan Berger
ea68ece7a7 tpm2: Disable curves whose keysize is too small and than can be disabled
When ecc-min-size is given use it to disable all curves whose keysize is
too small and that can be disabled.

Signed-off-by: Stefan Berger <stefanb@linux.ibm.com>
2024-07-26 22:34:38 -04:00
Stefan Berger
37c5b7d546 tpm2: Fix display of disabled elliptic curve algorithms
If elliptic curve algorithms were enabled with a shortcut the output
may have shown the individual curves as disabled, even though this is
not the case. Remove the enabledEccCurvesPrint bitfield since it
leads to misleading output.

Signed-off-by: Stefan Berger <stefanb@linux.ibm.com>
2024-07-26 22:34:38 -04:00
Stefan Berger
869bfa53d9 tpm2: Prevent list of disabled algorithms to start with a comma
The list of disabled algorithms may have started with a comma if the first
disabled algorithm was an ec curve. Fix this by passing the 'first'
variable and using it.

Signed-off-by: Stefan Berger <stefanb@linux.ibm.com>
2024-07-26 22:34:38 -04:00
Stefan Berger
18cffed604 tests: Use %zu for size_t
Signed-off-by: Stefan Berger <stefanb@linux.ibm.com>
2024-07-26 22:34:38 -04:00
Stefan Berger
1df35f6c77 tpm2: Change marshalled fields of OBJECT (StateFormatLevel 6)
Bump up the StateFormatLevel to 6 and use it to introduce a new OBJECT
marshalling format version '4' that slighly changes how an OBJECT is
marshalled:

- only marshal the private exponent for an RSA key
- always marshal the new hierarchy field

The marshalling code can still write previous version '3' when an older
StateFormatLevel is used to support backwards compatibility.

Adjust the test cases marshalling an RSA key OBJECT to check against
expected sizes across a series of StateFormatLevels.

Signed-off-by: Stefan Berger <stefanb@linux.ibm.com>
2024-07-25 11:17:42 -04:00
Stefan Berger
67447f6c88 rev180: Add but not enable new Svn- and FirmwareLimited hierarchies
Introduce new hierarchies SvnLimited and FirmwareLimited but do not enable
them.

Signed-off-by: Stefan Berger <stefanb@linux.ibm.com>
2024-07-25 11:17:42 -04:00
Stefan Berger
a5977d2018 rev180: Add hierarchy field to OBJECT and use it
Add new field hierarchy to OBJECT and assign values to it and access it.

Beyond the sync with upstream we need to also set the hierachy when
- an older OBJECT is converted to the current OBJECT
- an OBJECT is unmarshalled

Remove ObjectGetHierarchy since it is not needed anymore.

Signed-off-by: Stefan Berger <stefanb@linux.ibm.com>
2024-07-25 11:17:42 -04:00
Stefan Berger
ef4af2f0de tpm2: Move code to determine OBJECT hierarchy by its attribute flags
Move code that determines an OBJECT's hierarchy by its attribute flags
into BackwardsCompatibilityObject.c since it will soon only serve the
purpose of backwards compatibility.

Signed-off-by: Stefan Berger <stefanb@linux.ibm.com>
2024-07-25 11:17:42 -04:00
Stefan Berger
fade216e73 tpm2: Add missing argument for string format
Signed-off-by: Stefan Berger <stefanb@linux.ibm.com>
2024-07-25 10:49:09 -04:00
Stefan Berger
0f2506fc41 tpm2: NVMarshal: Add missing case of skipping a block when none is there
Add handling of the case of wanting to skip a block of code when no data
are in the byte stream. This case has not occurred so far where a block
of unmarshalling code needed to be skipped but also no data were there in
the byte stream - it would have otherwise lead to errors while trying to
unmarshal data that were not there. So far there was simply no code there
that should have been skipped.

Signed-off-by: Stefan Berger <stefanb@linux.ibm.com>
2024-07-24 15:51:06 -04:00
Stefan Berger
edd8b65a1d tpm2: Add missing semicolons in BIG_ENDIAN_TPM code
Signed-off-by: Stefan Berger <stefanb@linux.ibm.com>
2024-07-23 15:55:27 -04:00
Stefan Berger
c47b17e209 tpm2: Filter-out unusable and runtime-disabled curves
Allow completely arbitrary TPM_ECC_CURVE's to be passed to
RuntimeAlgorithmKeySizeCheckEnabled by checking that its value lies within
the bitfield and if it doesn't return a FALSE. Out-of-bounds values passed
to TEST_BIT would have caused a pAssert failure.

Call this function now early from CryptCapGetOneECCCurve that now can pass
any value as a TPM_ECC_CRUVE without causing a failure when filtering
out disabled or runtime unusable curves.

Signed-off-by: Stefan Berger <stefanb@linux.ibm.com>
2024-07-22 08:28:07 -04:00
Stefan Berger
3327a145fa tpm2: Filter-out runtime-unsupported algId in AlgorithmCapGetOneImplemented
Allow completely arbitrary TPM_ALG_ID's to be passed to
RuntimeAlgorithmCheckEnabled by checking that its value lies withing the
bitfield and if it doesn't return a FALSE. Out-of-bounds values passed
to TEST_BIT would have caused a pAssert failure.

Call this function now early on from AlgorithmCapGetOneImplemented that
now can pass any value as a TPM_ALG_ID without causing a failure.

Signed-off-by: Stefan Berger <stefanb@linux.ibm.com>
2024-07-22 08:28:07 -04:00
Stefan Berger
a76e8802e8 tests: Add test case when user chooses specific StateFormatLevel
Test that a user is able to choose a specific StateFormatLevel that is not
required by any of the chosen commands but enables a bugfix in the TPM 2
code for example.

Signed-off-by: Stefan Berger <stefanb@linux.ibm.com>
2024-07-22 08:27:49 -04:00
Stefan Berger
d6c1b22f60 tpm2: Rework failure codes retruned by GetStateFormatLevelFromJSON
Replace failure code TPM_RC_FAILURE when an invalid interger is encounterd
while parsing the StateFormatLevel from the json and have it return
TPM_RC_VALUE. Also improve the handling of the different error codes
returned from this function by a caller.

Signed-off-by: Stefan Berger <stefanb@linux.ibm.com>
2024-07-19 16:18:50 -04:00
Stefan Berger
c254804d59 tpm2: Return TPM_RC_VALUE when profile cannot be found by name
Return TPM_RC_VALUE rather than TPM_RC_FAILURE when a profile cannot
be found by its name.

Signed-off-by: Stefan Berger <stefanb@linux.ibm.com>
2024-07-19 16:18:50 -04:00
Stefan Berger
ae970a547a tpm2: Change RuntimeAlgorithm's TPM_RC_FAILUREs to TPM_RC_VALUE
To leave TPM_RC_FAILURE for more severe failures replace it with
TPM_RC_VALUE.

Signed-off-by: Stefan Berger <stefanb@linux.ibm.com>
2024-07-19 16:18:50 -04:00
Stefan Berger
e578709ca1 tpm2: Disable unused code related to TPM2_NV_DefineSpace2
Signed-off-by: Stefan Berger <stefanb@linux.ibm.com>
2024-07-19 16:04:10 -04:00
Stefan Berger
714511aef7 tpm2: Intercept unsupported external and permanent NV indices
Intercept newly added unsupported external and permanent NV indices when
trying to unmarshal and NV index. Add a test case that was using an
unsupported external NV index.

Signed-off-by: Stefan Berger <stefanb@linux.ibm.com>
2024-07-19 16:04:10 -04:00
Stefan Berger
8e27756625 tpm2: Enable TPM2_PolicyParameters in default-v1 profile
Enable TPM2_PolicyParameters in default-v1 profile and bump up the
StateFormatLevel to '5'.

Signed-off-by: Stefan Berger <stefanb@linux.ibm.com>
2024-07-18 16:36:02 -04:00
Stefan Berger
7d31d5ae8b rev180: Add TPM2_SetCapability and dependencies
This command does not do much at the moment and should therefore
also not be enabled.

Signed-off-by: Stefan Berger <stefanb@linux.ibm.com>
2024-07-18 16:36:02 -04:00
Stefan Berger
f1117d4832 rev180: Add TPM2_NV_ReadPublic2 and dependencies
This command would only need to be enabled if TPM2_NV_DefineSpace2 was
enabled.

Signed-off-by: Stefan Berger <stefanb@linux.ibm.com>
2024-07-18 16:36:02 -04:00
Stefan Berger
843fbe0762 rev180: Add TPM2_DefineSpace2 and dependencies
TPM2_DefineSpace2 defines NV areas in the same format as the old
TPM2_DefineSpace function did, so there should not be any compatibility
issues with the NV areas' format. However, it may not currently make
much sense to enable this command since its improvement over the existing
TPM2_DefineSpace is only support for exernal memory, which we don't have.

Signed-off-by: Stefan Berger <stefanb@linux.ibm.com>
2024-07-18 16:36:02 -04:00
Stefan Berger
12d8418c41 rev180: Add TPM2_PolicyParameters and dependencies
Add TPM2_PolicyParameters command but do not enable it.
This patch also introduces the Session attribute isParameterHashDefined
which is only written to in the new function TPM2_PolicyParameters. When
it is read in CheckPolicyAuthSession then it will always be 0 for as long
as TPM2_PolicyParameters is not activated, so it should not influence
older versions of the TPM 2 state (null profile).

Signed-off-by: Stefan Berger <stefanb@linux.ibm.com>
2024-07-18 16:36:02 -04:00
Stefan Berger
7b26afa705 rev180: Introduce isNameHashDefined and use it (bugfix?)
The new isNameHashDefined session attribute allows to better
differentiate between cpHash and nameHash being set. Both of these are part
of a union. However, using this new flag would potentially introduce
compatibility issues for older versions that did not have this flag and
if an older session was to be used with this newer version of TPM 2. To
avoid this use the current stateFormatLevel (4) to decide whether to set
and get the isNameHashDefined session attribute that did not exists before
stateFormatLevel 4. Instrument the code accordingly so that expected
behavior of old TPM 2 state (null profile) does not change while new TPM 2
state with the new default-v1 profile may use the new behavor.

Signed-off-by: Stefan Berger <stefanb@linux.ibm.com>
2024-07-18 16:36:02 -04:00
Stefan Berger
d902ce42f8 rev180: Use IsCpHashUnionOccupied to convert non-trivial case
Use IsCpHashUnionOccupied to convert a non-trivial case.

Signed-off-by: Stefan Berger <stefanb@linux.ibm.com>
2024-07-18 16:36:02 -04:00
Stefan Berger
4f1c4f5886 tpm2: Start code block after case statement for variable declaration
Older versions of gcc don't accept variable declations after a case
statement without starting a code block with '{'.

Signed-off-by: Stefan Berger <stefanb@linux.ibm.com>
2024-07-18 00:27:11 -04:00
Stefan Berger
fe69c3d4b1 tpm2: Extend buffer for printing a UINT16 into to 12 bytes (older gcc)
Even though UINT16 when printed as hex number will only use up to 4 bytes
and 3 more bytes for the prefix '0x' and terminating NUL (0xffff), extend
the buffer to 16 bytes to address the following gcc issue:

tpm2/RuntimeCommands.c:450:44: error: ‘__builtin___snprintf_chk’ output
   may be truncated before the last format character
   [-Werror=format-truncation=]

  snprintf(bufferhi, sizeof(bufferhi), "0x%x", commandCodeHi);

/usr/include/x86_64-linux-gnu/bits/stdio2.h:64:10: note:
   ‘__builtin___snprintf_chk’ output between 4 and 11 bytes into a
   destination of size 10

Signed-off-by: Stefan Berger <stefanb@linux.ibm.com>
2024-07-18 00:27:11 -04:00
Stefan Berger
92ab421194 tpm2: Check for out-of-range command code before accessing array
Check for an out-of-range command code before using it to access
an index in an array in a debug statement.

Signed-off-by: Stefan Berger <stefanb@linux.ibm.com>
2024-07-16 13:12:10 -04:00
Stefan Berger
e3520059cf tpm2: Print error message when invalid hash algorithm id appears (Coverity)
In case a hash algorithm id has a value >= 64 print out and error. This
should never occur since any hash algorithm id should have been set through
unmarshalling or by TPM 2-internal code.

Signed-off-by: Stefan Berger <stefanb@linux.ibm.com>
2024-07-16 13:12:10 -04:00
Stefan Berger
ee141c60e0 tpm2: Enable Camellia-192 and AES-192 and bump up stateFormatLevel
Enable Camellia-192 and AES-192 and bump up the stateFormatLevel to '4'.
This now prevents using this state with previous stateFormatLevels (< 4)
because there Camellia-192 or AES-192 was not enabled and the user would
otherwise not be able to decrypt data with either one if it was usable.

Signed-off-by: Stefan Berger <stefanb@linux.ibm.com>
2024-07-15 14:09:09 -04:00
Stefan Berger
341a278896 tpm2: Enable ECC_Decrypt & ECC_Encrypt in 'default' profile
Enable new commands ECC_Decrypt and ECC_Encrypt in the TPmProfile.h
and also in the 'default' profile. Since the additional commands extend
the ppList and auditCommands array, bump up the version of the stateLevel
to '2' and use the new marshalling functions by using the PERSISTENT_DATA
blob_version '5'.

Signed-off-by: Stefan Berger <stefanb@linux.ibm.com>
2024-07-15 14:09:09 -04:00
Stefan Berger
2803917a09 tpm2: Switch to uncompressed lists and adapt un/marshal functions
Switch the implementation to uncompressed lists (from now on it must
remain uncompressed lists forever) and adapt functions who marshal
and unmarshal arrays that are affected by this switch:

- PERSISTENT_DATA.ppList
- PERSISTENT_DATA.auditCommands

Signed-off-by: Stefan Berger <stefanb@linux.ibm.com>
2024-07-15 14:09:09 -04:00
Stefan Berger
811616f87d tpm2: Implement function to convert from and to compressed lists' bit arrays
Signed-off-by: Stefan Berger <stefanb@linux.ibm.com>
2024-07-15 14:09:09 -04:00
Stefan Berger
a848c37b7b tpm2: Implement RuntimeProfileGetSeedCompatLevel()
The maximum SEED_COMPAT_LEVEL that libtpms may use depends on the earliest
version of libtpms that a profile can run on. Therefore, implement
RuntimeProfileGetSeedCompatLevel() to determine the SEED_COMPAT_LEVEL that
a profile can use, which depends on the profile's stateCompatLevel (which
in turn depends on the version of libtpms)

Signed-off-by: Stefan Berger <stefanb@linux.ibm.com>
2024-07-15 11:51:38 -04:00
Stefan Berger
3e49f54640 tpm2: Write (part) of PERSISTENT_DATA using RuntimeProfile
Resolves: https://github.com/stefanberger/libtpms/issues/284
Resolves: https://github.com/stefanberger/swtpm/issues/710
Signed-off-by: Stefan Berger <stefanb@linux.ibm.com>
2024-07-15 11:51:38 -04:00
Stefan Berger
bda457104e tests: Add test case for TPMLIB_SetProfile()
Signed-off-by: Stefan Berger <stefanb@linux.ibm.com>
2024-07-15 11:51:38 -04:00
Stefan Berger
7e07d15b16 man: Add man page for TPMLIB_SetProfile()
Add a man page describing the details of TPNLIB_SetProfile.

Signed-off-by: Stefan Berger <stefanb@linux.ibm.com>
2024-07-15 11:51:38 -04:00
Stefan Berger
11655415a7 tpm2: Add new API call TPMLIB_SetProfile to enable user to set a profile
Add a new API call TPMLIB_SetProfile that enables a user to set a profile.
The user gets control over the name of the profile to apply and may supply
the algoritms to enable. The user does not get control over the individual
commands to enable.

Signed-off-by: Stefan Berger <stefanb@linux.ibm.com>
2024-07-15 11:51:38 -04:00
Stefan Berger
e02af1e06b tpm2: Use ANY_OBJECT_Marshal for OBJECTs is StateFormatLevel >=2
For profiles that work for libtpms v0.10 or later, so for
StatFormatLevel >=2, use ANY_MARSHAL_Object to write OBJECTS into
the NVRAM. This way OBJECTS are written in a more compact format.

Signed-off-by: Stefan Berger <stefanb@linux.ibm.com>
2024-07-15 11:51:38 -04:00
Stefan Berger
f8d9e6b853 tpm2: Check for unsupported algs in IncrementalSelfTest
Signed-off-by: Stefan Berger <stefanb@linux.ibm.com>
2024-07-15 11:51:38 -04:00
Stefan Berger
4f3def8794 tpm2: Allow to runtime-disable ECC curves by their key sizes
Signed-off-by: Stefan Berger <stefanb@linux.ibm.com>
2024-07-15 11:51:38 -04:00