Commit Graph

1178 Commits

Author SHA1 Message Date
Stefan Berger
5bc860ac37 tpm2: Initialize variable reported by Coverity (false positive)
Initialize a variable in ExecuteCommand following Coverity report
CID 1461252.

Down the callpath as reported in CID 1461252 in
TPMI_ST_COMMAND_TAG_Unmarshal() the passed-in value of
TPMI_ST_COMMAND_TAG *target is stored and possibly restored later on in
case of failure. Coverity complains that the variable is uninitialized.
While this is correct, there's no harm reading the uninitialized value
from the structure and possibly restoring it later on while not doing
anything else with it otherwise. Therefore, it's a false positive.

Signed-off-by: Stefan Berger <stefanb@linux.ibm.com>
2022-03-31 13:56:25 -04:00
Stefan Berger
fbf413ec86 tpm2: Initialize variable reported by Coverity (false positive)
Initialize a variable in TPM2_PolicyAuthorizeNV() following Coverity
report CID 1470811.

Down the callpath as reported in CID 1470811 in TPMI_ALG_HASH_Unmarshal()
the passed-in value of TPMI_ALG_HASH *target is stored and possibly
restored later on in case of failure. Coverity complains that the variable
is uninitialized. While this is correct, there's no harm reading the
uninitialized value from the structure and possibly restoring it later
on while not doing anything else with it otherwise. Therefore, it's a
false positive.

Resolves: https://github.com/stefanberger/libtpms/issues/311
Signed-off-by: Stefan Berger <stefanb@linux.ibm.com>
2022-03-31 10:36:11 -04:00
Stefan Berger
ec873cf8a2 tpm2: Initialize variable reported by Coverity (false positive)
Initialize a variable in USER_NVRAM_Unmarshal() follow Coverity
report CID 1470812.

Down the callpath as reported in CID 1470812 in TPMA_NV_Unmarshal() the
passed-in value of TPMA_NV *target is stored and possibly restored later
on in case of failure. Coverity complains that the variable is
uninitialized. While this is correct, there's no harm reading the
uninitialized value from the structure and possibly restoring it later
on while not doing anything else with it otherwise. Therefore, it's a
false positive.

Resolves: https://github.com/stefanberger/libtpms/issues/310
Signed-off-by: Stefan Berger <stefanb@linux.ibm.com>
2022-03-31 10:34:37 -04:00
Stefan Berger
8590849b65 tpm2: Check return code of BN_div()
Check the return code of BN_div() when calculating Q and Qr of
a private key.

Resolves: https://github.com/stefanberger/libtpms/issues/304
Signed-off-by: Stefan Berger <stefanb@linux.ibm.com>
2022-03-30 10:25:17 -04:00
Steve Langasek
7a64b3e41e tpm2: Initialize variables due to gcc complaint (s390x, false positive)
Detected by gcc on Ubuntu/s390x

Origin: https://git.launchpad.net/ubuntu/+source/libtpms/tree/debian/patches/uninitialized-variable.patch

Signed-off-by: Christian Ehrhardt <christian.ehrhardt@canonical.com>
2022-03-30 08:55:33 -04:00
Steve Langasek
c2f2bc8eaf tpm12: Initialize variables due to gcc complaint (s390x, false positive)
Detected by gcc on Ubuntu/s390x

Origin: https://git.launchpad.net/ubuntu/+source/libtpms/tree/debian/patches/uninitialized-variable.patch

Signed-off-by: Christian Ehrhardt <christian.ehrhardt@canonical.com>
2022-03-30 08:55:33 -04:00
Stefan Berger
bd42b67edb build-sys: Add probing for -fstack-protector
Add probing for -fstack-protector to the existing
-fstack-protector-strong since not all platforms support either one
of them.

Signed-off-by: Stefan Berger <stefanb@linux.ibm.com>
2022-03-02 13:26:04 -05:00
Juergen Repp
e426198437 tpm2: Do not call EVP_PKEY_CTX_set0_rsa_oaep_label() for label of size 0 (OSSL 3)
Openssl 3.0 did return an error if EVP_PKEY_CTX_set0_rsa_oaep_label was called
with label size 0. The function should only be called if the size of the label
is greater 0.
With this fix TPM2_RSA_Encrypt/Decrypt did work with OpenSSL 1.1 and 3.0
for encryption without label.

Signed-off-by: Juergen Repp <juergen.repp@sit.fraunhofer.de>
2022-03-02 07:24:21 -05:00
Stefan Berger
967e52e624 tpm2: Only access variable if unmarshalling worked
Only access the variable 'nvi' when the previous unmarshalling worked.
Before this change the undefined value of nvi would have been written
to memory but the error code from the failed marshalling propagated to
the caller so that it was working correctly before as well.

Signed-off-by: Stefan Berger <stefanb@linux.ibm.com>
2022-02-16 13:23:23 -05:00
Stefan Berger
d78a4520ba tpm2: Address Coverity Issue by casting '1' before shift (CID 1470813)
Cast the '1' to UINT64 before shifting it.

Since the shift value is always below 32 it would have never exceeded
the 32bit value it was using before the cast.

Signed-off-by: Stefan Berger <stefanb@linux.ibm.com>
2022-02-16 13:23:23 -05:00
Stefan Berger
26e0eea093 tpm2: Fix typo in __GNUC_MINOR__ preprocessor symbol
Fix typo in the __GNUC_MINOR__ preprocessor symbol.

This change is unlikely to have any impact since it was used for
comparions for gcc version 4.2, which is not in use anymore by now.

Resolves: https://github.com/stefanberger/libtpms/issues/289
Signed-off-by: Stefan Berger <stefanb@linux.ibm.com>
2022-01-12 07:46:20 -05:00
Stefan Berger
aeb7af78a9 tpm2: When writing state initialize s_ContextSlotMask if not set
If s_ContextSlotMask was not set since the TPM 2 was not initialized
by a call to TPM_Manufacture() or the state was not resumed, then
initialize the s_ContextSlotMask to 0xffff.

This situation can occur if a VM with an attached swtpm was started
and the VM's firmware either doesn't support TPM or didn't get to
initialize the vTPM.

The following commands recreated the issue with a SeaBIOS-only VM that
had no attached hard disk but an attached TPM 2:

virsh start BIOS-only-VM ; virsh save BIOS-only-VM save.bin ; \
 virsh restore save.bin

Error: Failed to restore domain from save.bin
error: internal error: qemu unexpectedly closed the monitor: \
2022-01-04T19:26:18.835851Z qemu-system-x86_64: tpm-emulator: Setting the stateblob (type 2) failed with a TPM error 0x3 a parameter is bad
2022-01-04T19:26:18.835899Z qemu-system-x86_64: error while loading state for instance 0x0 of device 'tpm-emulator'
2022-01-04T19:26:18.835929Z qemu-system-x86_64: load of migration failed: Input/output error

Resolves: https://bugzilla.redhat.com/show_bug.cgi?id=2035731
Signed-off-by: Stefan Berger <stefanb@linux.ibm.com>
2022-01-05 08:06:46 -05:00
kpcyrd
cd8025fa6f Fix "maybe-uninitialized" warning
exp_array_size is always initialized if `rc == TPM_RC_SUCCESS` and never used
if `rc != TPM_RC_SUCCESS` but some compilers have trouble noticing this.

Signed-off-by: kpcyrd <git@rxv.cc>
2021-10-29 08:50:36 -04:00
Stefan Berger
b7ba0a69a5 debian: Adjust changelog for 0.10.0~dev1
Signed-off-by: Stefan Berger <stefanb@linux.ibm.com>
2021-10-17 13:07:02 -04:00
Stefan Berger
9b76f24a07 build-sys: Build libtpms v0.10.0
Signed-off-by: Stefan Berger <stefanb@linux.ibm.com>
2021-10-16 09:08:58 -04:00
Stefan Berger
435abf8cda build-sys: Build libtpms v0.9.1
Signed-off-by: Stefan Berger <stefanb@linux.ibm.com>
2021-10-15 15:19:12 -04:00
Stefan Berger
1fb639195b tpm2: Do not write permanent state if only clock changed
To avoid timeouts on short-running commands, such as TPM2_PCR_Extend,
avoid triggering the writing of the permanent state of the TPM 2
if only the clock was updated. So the clock by itself will not cause
the permanent state to be written out anymore but there have to be
other reasons as well.

The state will still be written out upon a TPM2_Shutdown, which is
supposed to be the last command to be sent to the TPM when shutting
down the VM/vTPM. Also, the permanent state will still carry the
latest clock value if it is retrieved via control channel for
VM/VTPM suspend.

The case that may be affected, but is of lesser importance, is the one
where swtpm's volatile state is written to storage using 'swtpm_ioctl -v'
and then swtpm is terminated and restarted (similar to suspend/resume)
and the permanent state file is read from storage but does not contain
the latest clock value. In this case the go.clock will be updated when
the first command after resume is executed.

This fixes the swtpm issue https://github.com/stefanberger/swtpm/issues/597.

Signed-off-by: Stefan Berger <stefanb@linux.ibm.com>
2021-10-15 11:45:54 -04:00
Stefan Berger
00ae50bdca tpm2: Report SM4 keysizes ing GetInfo's JSON if SM4 is enabled
Signed-off-by: Stefan Berger <stefanb@linux.ibm.com>
2021-10-13 19:37:20 -04:00
Stefan Berger
3cf876ecf8 tpm2: Fix indentations and comments in SM2 related code
Signed-off-by: Stefan Berger <stefanb@linux.ibm.com>
2021-10-13 19:37:20 -04:00
JerryDevis
721f6c2e33 Support SM4
Signed-off-by: JerryDevis <seclab@huawei.com>
2021-10-12 10:57:39 -04:00
Stefan Berger
dc4e3f6313 rpm/debian: Add 0.9.0 entry in changelog
Signed-off-by: Stefan Berger <stefanb@linux.ibm.com>
2021-09-28 16:55:28 -04:00
Stefan Berger
ab25f924d1 CHANGES: Updated CHANGES document for 0.9.0
Signed-off-by: Stefan Berger <stefanb@linux.ibm.com>
2021-09-28 16:55:28 -04:00
Stefan Berger
4bc846f869 build-sys: Use -DOPENSSL_SUPPRESS_DEPRECATED to suppress warning (OSSL 3)
Instead of using -Wno-deprecated-declarations use
-DOPENSSL_SUPPRESS_DEPRECATED to only suppress OpenSSL deprecated
declarations warnings.

Signed-off-by: Stefan Berger <stefanb@linux.ibm.com>
2021-09-21 16:52:22 -04:00
Stefan Berger
f45bafa9bd build-sys: Add -Wno-deprecated-declarations to default CFLAGS (OSSL 3)
To be able to build with OpenSSL 3.0 we need to added
-Wno-deprecated-declarations to the default CFLAGS.

Signed-off-by: Stefan Berger <stefanb@linux.ibm.com>
2021-09-15 13:28:08 -04:00
Stefan Berger
3091ef1184 build-sys: Test for functions if #defines are not found (OSSL 3)
OpenSSL 3.0 has converted several RSA-related #defines to functions, so
that AX_CHECK_DEFINE only works for OpenSSL 1.1.0 but for OpenSSL 3.0.0
we have to also use AC_CHECK_LIB to determine whether the function is
available.

Signed-off-by: Stefan Berger <stefanb@linux.ibm.com>
2021-09-15 13:28:08 -04:00
Stefan Berger
ed94c7fd14 tpm2: Only call EVP_PKEY_CTX_set0_rsa_oaep_label when label != NULL (OSSL 3)
EVP_PKEY_CTX_set0_rsa_oaep_label must only be called with label != NULL.

Signed-off-by: Stefan Berger <stefanb@linux.ibm.com>
2021-09-15 13:28:08 -04:00
Stefan Berger
c8a7074bb6 tpm2: Use EVP_PKEY_get1_RSA() rather than EVP_PKEY_get0_RSA (OSSL 3)
OpenSSL 3.0 has changed the signature of EVP_PKEY_get0_RSA() from

struct rsa_st *EVP_PKEY_get0_RSA(EVP_PKEY *pkey);

to

const struct rsa_st *EVP_PKEY_get0_RSA(const EVP_PKEY *pkey);

We now have to use EVP_PKEY_get1_RSA with this signature so that we can
access the RSA key. The signature of that function hasn't changed between
OpenSSL 1.1.0 and 3.0.0.

struct rsa_st *EVP_PKEY_get1_RSA(EVP_PKEY *pkey);

Free the additional reference held on the RSA key.

Signed-off-by: Stefan Berger <stefanb@linux.ibm.com>
2021-09-15 13:28:08 -04:00
Stefan Berger
9eb9677795 tpm2: Marshal event sequence objects' hash state
Event sequence objects were never properly marshalled and when their state
was saved and later restored their state may have been corrupted. Fix this
now by also marshalling the state of event sequence objects.

Bump up the version of the HASH_OBJECT's header to '3' so that previously
written state can be resumed if an event sequence object is encountered
and we only unmarshal an event sequence object when the version is at least
'3'.

Fixes issue #259.

Signed-off-by: Stefan Berger <stefanb@linux.ibm.com>
2021-09-09 12:28:17 -04:00
Stefan Berger
b06558d32b tpm2: Report supported Camellia keysizes in the JSON
Signed-off-by: Stefan Berger <stefanb@linux.ibm.com>
2021-09-06 13:42:06 -04:00
Stefan Berger
1522d293f5 tpm2: Enable ALG_CAMELLIA in TpmProfile.h
Signed-off-by: Stefan Berger <stefanb@linux.ibm.com>
2021-09-06 13:42:06 -04:00
Stefan Berger
d78e6e3879 tpm2: Add more compile-time constants to array
Signed-off-by: Stefan Berger <stefanb@linux.ibm.com>
2021-09-06 13:42:06 -04:00
Stefan Berger
5e97c2e78d tpm2: Make exp. size of compile-time constants dependent on header version
Make the expected array size of compile-time constants dependent on
the version of the header. This way we can add elements to the array
while bumping up the version of the header.

Signed-off-by: Stefan Berger <stefanb@linux.ibm.com>
2021-09-06 13:42:06 -04:00
Stefan Berger
1d99fa711e git: Fix erroneous gitignore entries
Fix erroneous gitignore entries that previously showed with the
following command line:

git ls-files -i --exclude-standard -c

Resolves #249.

Signed-off-by: Stefan Berger <stefanb@linux.ibm.com>
2021-09-02 13:50:24 -04:00
Stefan Berger
a9657246e2 tpm2: Extend asym key test case data generation script with sm4
Extend the test case data generation script with sm4. Since several
distros' openssl do not support sm4, we need to test for whether sm4
is supported by the installed openssl.

Signed-off-by: Stefan Berger <stefanb@linux.ibm.com>
2021-09-02 12:03:23 -04:00
JerryDevis
4c19adce30 Update SymmetricTestData.h
update the selftest data in dataOut_SM4_CTR

Signed-off-by: JerryDevis <seclab@huawei.com>
2021-09-02 10:55:05 -04:00
Marc-André Lureau
3482da113c build-sys: add with_tpm1/with_tpm2 variables to .pc
Signed-off-by: Marc-André Lureau <marcandre.lureau@redhat.com>
2021-08-06 08:04:37 -04:00
Marc-André Lureau
057242b84d build-sys: add optional --with-tpm1
Signed-off-by: Marc-André Lureau <marcandre.lureau@redhat.com>
2021-08-06 08:04:37 -04:00
Marc-André Lureau
e60c35ecf9 Move common debug, memory & nvfile units to src/
Signed-off-by: Marc-André Lureau <marcandre.lureau@redhat.com>
2021-08-06 08:04:37 -04:00
Stefan Berger
aa579cee5b tpm2: Use EC_POINT_set/get_affine_coordinates on OpenSSL >= 1.1
Use the EC_POINT_set/get_affine_coordinates function on OpenSSL >= 1.1.
These function are a 1:1 replacement for the
EC_POINT_set/get_affine_coordinates_GFp functions and are available
since OpenSSL 1.1 and are deprecated in OpenSSL 3.0.

This patch addresses one aspect of the OpenSSL 3.0 issues raised in
issue #215.

Signed-off-by: Stefan Berger <stefanb@linux.ibm.com>
2021-08-04 08:57:20 -04:00
Stefan Berger
7899608f2a tpm2: Enable supported AES bit size cases using #if AES_<size>
Signed-off-by: Stefan Berger <stefanb@linux.ibm.com>
2021-07-29 19:15:51 -04:00
Stefan Berger
8b889f50fe tpm2: Enable supported Camellia bit size cases using #if Camellia_<size>
Signed-off-by: Stefan Berger <stefanb@linux.ibm.com>
2021-07-29 19:15:51 -04:00
Stefan Berger
034a5c0248 tpm2: NVMarshal: Handle index orderly RAM without 0-sized terminating node
The NVRAM entries in s_indexOrderlyRam array do not need to contain a
0-sized terminating node. Instead, the entries may fill up this 512
byte array so that no NV_RAM_HEADER structure fits anymore. The fact
that no more NV_RAM_HEADER structure fits is also an indicator for the
last entry. We need to account for this in the code marshalling and
unmarshalling the entries so that we stop marshalling the entries
then and similarly stop unmarshalling.

Signed-off-by: Stefan Berger <stefanb@linux.ibm.com>
2021-07-23 15:54:58 -04:00
Stefan Berger
17255da54c tpm2: Initialize a whole OBJECT before using it
Initialize a while OBJECT before using it. This is necessary since
an OBJECT may also be used as a HASH_OBJECT via the ANY_OBJECT
union and that HASH_OBJECT can leave bad size inidicators in TPM2B
buffer in the OBJECT. To get rid of this problem we reset the whole
OBJECT to 0 before using it. This is as if the memory for the
OBJECT was just initialized.

Signed-off-by: Stefan Berger <stefanb@linux.ibm.com>
2021-07-23 15:54:58 -04:00
Stefan Berger
e56316617c tests: fuzz: Store initialized permall state blob
We have to store the permall state blob once it has been initialized since
otherwise some fields are not having proper values in the internal state.

Signed-off-by: Stefan Berger <stefanb@linux.ibm.com>
2021-07-22 22:23:16 -04:00
Stefan Berger
a6c9836af5 tests: fuzz: Have state suspended and resumed after every fuzz step
Have the TPM 2's state suspended and resumed at every step to
ensure that we can marshal and unmarshal it.

Signed-off-by: Stefan Berger <stefanb@linux.ibm.com>
2021-07-22 17:37:00 -04:00
Stefan Berger
70ebecdf6a tests: fuzz: Register callbacks to avoid creating NVChip file
Register callbacks so that we don't create the NVChip file.

Signed-off-by: Stefan Berger <stefanb@linux.ibm.com>
2021-07-22 17:37:00 -04:00
Stefan Berger
a59db1faea tests: fuzz: Call die() when an error from an API call was returned
Call die() causing as assert() to be triggered if an API call
returned an unexpected failure result.

Signed-off-by: Stefan Berger <stefanb@linux.ibm.com>
2021-07-22 17:37:00 -04:00
Nick Chevsky
d68ef5f0bc Debian packaging improvements
- debian/clean: Clean up files not automatically caught by
  `debian/rules clean`.
- debian/control:
  - Add new recommended/required fields and bump Standards-Version.
  - Remove redundant build dependences that are implied transitively.
  - Move libtpms0's ${misc:Pre-Depends} dependency to `Pre-Depends`.
    Resolves `depends-on-misc-pre-depends` Lintian warning.
  - Sort packages and dependency lists alphabetically as per
    wrap-and-sort(1).
- debian/not-installed: List non-installed files to avoid
  dh_missing(1) warnings.
- debian/rules:
  - Remove unused architecture variable.
  - Remove redundant --parallel, --with autoreconf flags.
  - Remove redundant `dh_update_autotools_config` call.
- debian/*.install:
  - Remove redundant `#! /usr/bin/dh-exec` lines.
  - Sort entries alphabetically as per wrap-and-sort(1).

Signed-off-by: Nick Chevsky <nchevsky@users.noreply.github.com>
2021-07-22 16:48:09 -04:00
Stefan Berger
04f85e2fc1 man3: Fix errors in code example in TPMLIB_RegisterCallbacks
Signed-off-by: Stefan Berger <stefanb@linux.ibm.com>
2021-07-22 13:21:45 -04:00
Stefan Berger
71b33cb9e0 tpm2: Use #if ALG_CAMELLIA rather than #if 0
Signed-off-by: Stefan Berger <stefanb@linux.ibm.com>
2021-07-22 13:21:45 -04:00