Commit Graph

1258 Commits

Author SHA1 Message Date
William Roberts
554a13379f MAINTAINERS: add LibreSSL maintainer
Signed-off-by: William Roberts <william.c.roberts@intel.com>
2022-07-12 17:28:40 -04:00
Stefan Berger
f1e8b6c694 tests: Add license identifier to test cases
Signed-off-by: Stefan Berger <stefanb@linux.ibm.com>
2022-07-12 11:30:53 -04:00
Stefan Berger
4e5dffc08a tests: Fix outdated output of expected file size in case of error
Signed-off-by: Stefan Berger <stefanb@linux.ibm.com>
2022-07-12 11:30:53 -04:00
William Roberts
c6c8f63686 Revert "tpm_crypto: support libressl api"
This reverts commit 9afebc712a.

The issue is that opensslv.h is not included and thus the
OPENSSL_VERSION_NUMBER is not getting set.

Signed-off-by: William Roberts <william.c.roberts@intel.com>
2022-07-12 11:28:47 -04:00
William Roberts
27beabc518 tpm_crypto: support libressl api
Signed-off-by: William Roberts <william.c.roberts@intel.com>
2022-07-09 13:30:55 -04:00
William Roberts
4970a8a0c7 tpm_crypto: simplify OSSL version ifdef
Signed-off-by: William Roberts <william.c.roberts@intel.com>
2022-07-09 13:30:55 -04:00
Stefan Berger
f20a7d7d41 Add MAINTAINERS file to project
Signed-off-by: Stefan Berger <stefanb@linux.ibm.com>
2022-06-20 16:15:06 -04:00
Stefan Berger
086e1bcb8b tpm2: Fix a typo in PERSISTENT_ALL_Unmarshal
Signed-off-by: Stefan Berger <stefanb@linux.ibm.com>
2022-06-15 10:35:08 -04:00
Stefan Berger
9c63f076ec tpm2: Implement OpenSSLCryptRsaGenerateKey for OpenSSL 3.0 case
Signed-off-by: Stefan Berger <stefanb@linux.ibm.com>
2022-05-31 20:31:10 -04:00
Stefan Berger
d215cfd612 tpm2: Implement BuildRSAKey for OpenSSL 3.0 case
Signed-off-by: Stefan Berger <stefanb@linux.ibm.com>
2022-05-31 20:31:10 -04:00
Stefan Berger
e998c06694 tpm2: Delay creation of EVP_PKEY in InitOpenSSLRSAPrivateKey
Delay the creation of the EVP_PKEY in InitOpenSSLRSAPrivateKey
so that we can create the key with all the parameters at once.
We have to do this since with the OpenSSL 3.0 API we cannot set
parameters after the initial creation of the key anymore.

Signed-off-by: Stefan Berger <stefanb@linux.ibm.com>
2022-05-31 20:31:10 -04:00
Stefan Berger
c8523779f2 tpm2: Implement DoRSACheckKey with OpenSSL 3.0 API
Signed-off-by: Stefan Berger <stefanb@linux.ibm.com>
2022-05-31 20:31:10 -04:00
Stefan Berger
f7ccda56fb tpm2: Implement ObjectGetPublicParameters to get N and E from OBJECT
Get the BIGNUMs N and E from an RSA key OBJECT. The purpose of
this refactoring is be able to reuse the new function.

Signed-off-by: Stefan Berger <stefanb@linux.ibm.com>
2022-05-31 20:31:10 -04:00
Stefan Berger
3eef1fb035 tpm2: Implement BuildRSAKey for building an RSA EVP_PKEY
Implement BuildRSAKey for building an RSA EVP_PKEY from copies
of the BIGNUMs it gets passed. This way it is clear that the
caller has to free the BIGNUMs it passed itself also in case of
error returned by BuildRSAKey.

Signed-off-by: Stefan Berger <stefanb@linux.ibm.com>
2022-05-31 20:31:10 -04:00
Stefan Berger
6a919f1c6a tpm2: Do not access variable if it could not be read from buffer (Coverity)
Exit the loop when the variable could not be filled with data from the
byte stream. This avoids accessing the variable 'element' in case it
wasn't initialized. The old could would have accessed the possibly
uninitialized variable but exited the loop immediately after.

Signed-off-by: Stefan Berger <stefanb@linux.ibm.com>
2022-05-28 20:07:35 -04:00
Stefan Berger
b6b8506bce tpm2: Access entrysize variable only if it was read from buffer (Coverity)
Only access the entrysize variable if it was read from the buffer. In case
of an error just head towards the exit. Previously, an error would also
have lead the function to do no more useful processing and exited it with
an error code bug Coverity complains that the entrysize variable would be
access while it wasn't initialized.

Signed-off-by: Stefan Berger <stefanb@linux.ibm.com>
2022-05-28 20:07:35 -04:00
Stefan Berger
4923e9d67a man: Update description of tpm_io_getlocality callback function
Update the description of the tpm_io_getlocality() callback function
that the TPM 2 code invokes to get the currently active locality.
It is expected that the called software is trusted and returns a valid
locality number at all times and thus also always return TPM_SUCCESS.

Signed-off-by: Stefan Berger <stefanb@linux.ibm.com>
2022-05-28 20:07:35 -04:00
Stefan Berger
353e835caa tpm2: Expect TPM_SUCCESS from tpm_io_getlocality() callback function
Signed-off-by: Stefan Berger <stefanb@linux.ibm.com>
2022-05-28 20:07:35 -04:00
Stefan Berger
77950dd917 tpm2: Fix a potential overflow expression (coverity)
Signed-off-by: Stefan Berger <stefanb@linux.ibm.com>
2022-05-28 20:07:35 -04:00
Stefan Berger
3b047cb5fe tpm2: Assign result of OsslToTpmBn to OK
Signed-off-by: Stefan Berger <stefanb@linux.ibm.com>
2022-05-28 20:07:35 -04:00
Stefan Berger
0aef1c7471 tpm12: Check returnCode before overwriting it
Signed-off-by: Stefan Berger <stefanb@linux.ibm.com>
2022-05-28 16:40:19 -04:00
Stefan Berger
1555b59304 tpm12: Check returnCode before overwriting it
Signed-off-by: Stefan Berger <stefanb@linux.ibm.com>
2022-05-28 16:40:19 -04:00
Stefan Berger
adce2d6980 tpm12: Assign return value of TPM_DelegatePublic_Copy to rc
Signed-off-by: Stefan Berger <stefanb@linux.ibm.com>
2022-05-28 16:40:19 -04:00
Stefan Berger
2025654843 tpm12: Assign TPM_MGF1 return value to rc
Signed-off-by: Stefan Berger <stefanb@linux.ibm.com>
2022-05-28 16:40:19 -04:00
Stefan Berger
03e90a7dd4 tpm12: Assign rc return value of TPM_Sbuffer_Append
Signed-off-by: Stefan Berger <stefanb@linux.ibm.com>
2022-05-28 16:40:19 -04:00
Ross Lagerwall
3d2bbe2f19 tpm2: Fix size check in CryptSecretDecrypt
Check the secret size against the size of the buffer, not the size
member that has not been set yet.

Reported by Coverity.

Signed-off-by: Ross Lagerwall <ross.lagerwall@citrix.com>
2022-05-24 14:08:56 -04:00
Ross Lagerwall
71d3a27bc7 nvfile: Free allocated memory on failure
In TPM_NVRAM_LoadData(), there is an unlikely path where the function
will return an error code but still expect the caller to free the
allocated data. At least some of the callers don't handle this correctly
so ensure that the caller only needs to free data if the function
returns success.

Reported by Coverity.

Signed-off-by: Ross Lagerwall <ross.lagerwall@citrix.com>
2022-05-20 09:57:09 -04:00
Stefan Berger
f9a6f51cb8 tpm: #undef printf in case it is #define'd (OSS-Fuzz)
The build environment used by OSS-Fuzz reports this error.

 In file included from /src/libtpms/src/tpm_debug.c:42:
 /src/libtpms/src/tpm_debug.h:69:9: error: 'printf' macro redefined [-Werror,-Wmacro-redefined]
 #define printf(...) TPMLIB_LogPrintf(__VA_ARGS__);
 :         ^
 /usr/include/x86_64-linux-gnu/bits/stdio2.h:110:11: note: previous definition is here
 # define printf(...) \
           ^
 1 error generated.

The simple fix is to #undef printf in case it is #define'd.

Signed-off-by: Stefan Berger <stefanb@linux.ibm.com>
2022-04-08 08:35:19 -04:00
Stefan Berger
13dcff6592 Fix configure script to support _FORTIFY_SOURCE=3
gcc 12.1 supports _FORTIFY_SOURCE=3. Modify the existing check for whether
_FORTIFY_SOURCE=2 can be used to test compile with the user provided
CFLAGS and only add _D_FORTIFY_SOURCE=2 to the HARDENING_CFLAGS if the
user doesn't provide anything that's not compatible.

Following an online article _FORTIFY_SOURCE=3 may add more overhead, so
we only go up to level 2 for now and let build servers or user provide
the higher level via the CFLAGS.

https://developers.redhat.com/blog/2021/04/16/broadening-compiler-checks-for-buffer-overflows-in-_fortify_source#what_s_next_for__fortify_source

Signed-off-by: Stefan Berger <stefanb@linux.ibm.com>
2022-04-04 12:40:36 -04:00
Stefan Berger
73fca84031 tpm2: Require TPM_NV_DISK to avoid case of tpm_stata_path being NULL
Address an issue reported by cppcheck that raises the issue that
tpm_state_path could be NULL when the #define TPM_NV_DISK is not set.
Require that the #define TPM_NV_DISK always be set.

Resolves: https://github.com/stefanberger/libtpms/issues/313
Signed-off-by: Stefan Berger <stefanb@linux.ibm.com>
2022-04-01 08:41:35 -04:00
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