Commit Graph

726 Commits

Author SHA1 Message Date
Marc-André Lureau
155ccdf561 swtpm*: report 'version' in --print-capabilities
The "version" field can be used to decide when capabilities are
supported/reported and give some library build details.

Signed-off-by: Marc-André Lureau <marcandre.lureau@redhat.com>
2021-08-10 07:13:24 -04:00
Marc-André Lureau
da8752b1f7 swtpm: report 'tpm-1.2' & 'tpm-2.0' in --print-capabilities
Signed-off-by: Marc-André Lureau <marcandre.lureau@redhat.com>
2021-08-10 07:13:24 -04:00
Marc-André Lureau
ccaf99f12c swtpm: ignore failure to choose version for --print-capabilities
A version was selected early for TPMLIB_GetInfo() to succeed in
--print-capabilities. TPM 1.2 is the default version, but can now be
disabled in libtpms.

Ignore the error when the version is unsupported by libtpms and skip
reporting the GetInfo related details.

Signed-off-by: Marc-André Lureau <marcandre.lureau@redhat.com>
2021-08-10 07:13:24 -04:00
Nick Chevsky
6961ec4878 swtpm: Preserve mlp->flags in CMD_SET_DATAFD handler
Fix the handler for control channel command CMD_SET_DATAFD so that
it ORs new bits onto mlp->flags instead of overwriting its value.
This was causing loss of flags previously set during command-line
argument parsing, which resulted in user-provided options (e.g.
--terminate) to be permanently ignored if command CMD_SET_DATAFD
was at any point received on the control channel.

Signed-off-by: Nick Chevsky <nchevsky@users.noreply.github.com>
2021-08-02 07:46:00 -04:00
Stefan Berger
6f7a1e2769 swtpm: Use the pollfd array's enums as indices when initializing the array
Signed-off-by: Stefan Berger <stefanb@linux.ibm.com>
2021-08-01 18:49:27 -04:00
Stefan Berger
78559edde9 swtpm_setup: Support --write-ek-cert-files as an option
Write the EK certificate files into the directory specified as parameter
to the --write-ek-cert-files option.

Signed-off-by: Stefan Berger <stefanb@linux.ibm.com>
2021-07-30 16:45:22 -04:00
Stefan Berger
a4cead5a8a swtpm_setup: Get key description from function generating the EK key
Pass an optional key_description parameter through the APIs to be able to
get the a human readable key description, such as 'rsa2048' or 'secp384r1'
of the key that was created.

Signed-off-by: Stefan Berger <stefanb@linux.ibm.com>
2021-07-30 16:45:22 -04:00
Stefan Berger
dbb1bbf87d swtpm_setup: Move functions checking for directory access to library
Move code from setpm_setup into new function check_directory_access
that checks for existence of directory and access to it.

Signed-off-by: Stefan Berger <stefanb@linux.ibm.com>
2021-07-30 16:45:22 -04:00
Stefan Berger
cab1e2186f build-sys: Add MY_CFLAGS, CFLAGS, and MY_LDFLAGS to all Makefile.am's
Add MY_CFLAGS, CFLAGS, and MY_LDFLAGS to all Makefile.am's so that they
can be defined during configure time as well as CFLAGS added during
build time. LDFLAGS were already handled correctly during build-time.

Signed-off-by: Stefan Berger <stefanb@linux.ibm.com>
2021-07-27 15:39:48 -04:00
Stefan Berger
da7338961b build-sys: Rename AM_CFLAGS/LDFLAGS to MY_CFLAGS/LDFLAGS
To avoid the following warning use MY_ as a prefix for the offending
variables that have AM_ as a prefix:

configure.ac:587: warning: macro 'AM_CFLAGS' not found in library
configure.ac:590: warning: macro 'AM_LDFLAGS' not found in library

Signed-off-by: Stefan Berger <stefanb@linux.ibm.com>
2021-07-27 15:39:48 -04:00
Stefan Berger
71a4e33adc Revert "build-sys: Remove using AM_CFLAGS and AM_LDFLAGS"
This reverts commit 7f81d31288.

Signed-off-by: Stefan Berger <stefanb@linux.ibm.com>
2021-07-27 15:39:48 -04:00
Nick Chevsky
ddc7521653 Move swtpm_localca sources from samples/ to src/
- Move swtpm_localca's sources out of samples/ (where they no longer
  belong now that swtpm_localca is a binary) to src/swtpm_localca/.
- Tests now call the swtpm_localca binary directly at the location
  where it was built, as they do with all other compiled programs.
- Simplify samples/swtpm-localca.in and delete swtpm-localca.2inst,
  removing the now-unnecessary logic to selectively call swtpm_localca
  from different locations (samples/ when running tests vs. /usr/bin/
  post-installation).

Signed-off-by: Nick Chevsky <nchevsky@users.noreply.github.com>
2021-07-25 08:52:13 -04:00
Stefan Berger
05f64f5529 swtpm_setup: Fix compiler errors when memcpy is a macro (Cygwin)
Since commit 502cb1129a -D_FORTIFY_SOURCE=2 is passed to the gcc
command line resulting in memcpy() becoming a macro on Cygwin, which
then causes the following compiler errors due to the anonymous arrays
being used:

swtpm.c: In function ‘swtpm_tpm2_createprimary_ek_rsa’:
swtpm.c:686:26: error: macro "memcpy" passed 34 arguments, but takes just 3
  686 |         }, authpolicy_len);
      |                          ^
In file included from /usr/include/string.h:180,
                 from swtpm.c:15:
/usr/include/ssp/string.h:97: note: macro "memcpy" defined here
   97 | #define memcpy(dst, src, len) __ssp_bos_check3(memcpy, dst, src, len)
      |
swtpm.c:682:9: error: statement with no effect [-Werror=unused-value]
  682 |         memcpy(authpolicy, (unsigned char []){
      |         ^~~~~~
swtpm.c:698:26: error: macro "memcpy" passed 50 arguments, but takes just 3
  698 |         }, authpolicy_len);
      |                          ^

The solution is to surround the anonymous array definitions with '( )'.

Signed-off-by: Stefan Berger <stefanb@linux.ibm.com>
2021-07-14 14:16:26 -04:00
Stefan Berger
ecdfc77116 swtpm: Fix issues raised by -Wextra (Cygwin)
Signed-off-by: Stefan Berger <stefanb@linux.ibm.com>
2021-06-24 18:58:22 -04:00
Eiichi Tsukata
a5f4e65749 swtpm: Issue fsync to ensure state data reaches disk
Add fsync(2) before rename(2) for temp file to ensure data reaches disk
and for directory which containing state file to ensure directory entry
also reaches disk.

Signed-off-by: Eiichi Tsukata <eiichi.tsukata@nutanix.com>
2021-06-24 16:19:59 -04:00
Stefan Berger
a69388c0f4 swtpm_setup: Fix issues raised by -Wextra
Signed-off-by: Stefan Berger <stefanb@linux.ibm.com>
2021-06-24 15:05:03 -04:00
Stefan Berger
6a6fd6e811 swtpm_ioctl: Fix issues raised by -Wextra
Signed-off-by: Stefan Berger <stefanb@linu.ibm.com>
2021-06-24 15:05:03 -04:00
Stefan Berger
e7488fd1e5 swtpm_cert: Fix issues raised by -Wextra
Signed-off-by: Stefan Berger <stefanb@linux.ibm.com>
2021-06-24 15:05:03 -04:00
Stefan Berger
5302269acc swtpm_bios: Fix issues raised by -Wextra
Signed-off-by: Stefan Berger <stefanb@linux.ibm.com>
2021-06-24 15:05:03 -04:00
Stefan Berger
fa2fc871d2 swtpm: Fix issues raised by -Wextra
Signed-off-by: Stefan Berger <stefanb@linux.ibm.com>
2021-06-24 15:05:03 -04:00
Tomasz Kłoczko
75c5da2cf1 build-sys: Remove using -L$(PWD)/.libs -lswtpm_libtpms on linking
Such paths should not be used.
Use proper linker libswtpm_libtpms.la scripyt generated by libtool.

Signed-off-by: Tomasz Kłoczko <kloczek@github.com>
2021-06-22 17:03:31 -04:00
Tomasz Kłoczko
7f81d31288 build-sys: Remove using AM_CFLAGS and AM_LDFLAGS
Those automake variables are used by default.

Signed-off-by: Tomasz Kłoczko <kloczek@github.com>
2021-06-22 17:03:31 -04:00
Stefan Berger
a59eead4bc swtpm_setup: Initialize Gerror and free it
Gerror was not initialized to NULL in one place. In the other it wasn't
freed.

Signed-off-by: Stefan Berger <stefanb@linux.ibm.com>
2021-06-16 18:01:35 -04:00
Felipe Franciosi
4a96c7f54a swtpm: Fix SWTPM names in comments and messages
Some of the code comes from libtpms, where various methods are named
"TPM_Something". The swtpm version of these methods are named
"SWTPM_Something". However, certain debug/log messages and comments were
updated accordingly to reflect that.

This is a cosmetic change that fixes that.

Signed-off-by: Felipe Franciosi <felipe@nutanix.com>
2021-06-15 08:35:53 -04:00
Stefan Berger
2bf68cc4be swtpm_setup: Replace '\\s' in regex with [[:space:]] to fix cygwin
Cygwin's regex implementation doesn't seem to understand '\\s'
but needs [[:space:]] instead to properly skip over spaces.

Signed-off-by: Stefan Berger <stefanb@linux.ibm.com>
2021-06-11 07:53:28 -04:00
Stefan Berger
932dc4e270 build-sys: Add HARDENING_CFLAGS and _LDFLAGS to all C programs
Add the HARDENING_CFLAGS and _LDFLAGS to all C programs.

Signed-off-by: Stefan Berger <stefanb@linux.ibm.com>
2021-06-04 16:04:09 -04:00
Stefan Berger
c86be8d960 swtpm: Add 2021 as year in the Copyright notice
Signed-off-by: Stefan Berger <stefanb@linux.ibm.com>
2021-06-04 16:04:09 -04:00
Stefan Berger
80271b5f26 swtpm_setup: Fix the strjoin for the PID file path
The path for the PID file was not joined correctly and was missing
a '/'. Fix this now.

Signed-off-by: Stefan Berger <stefanb@linux.ibm.com>
2021-06-04 16:04:09 -04:00
Stefan Berger
c8c3bb4170 swtpm_setup: Initialize autfreed variables with NULL (gcc-11)
This patch addresses the following gcc-11 compiler issues:

In file included from /usr/include/glib-2.0/glib.h:114,
                 from swtpm_setup_utils.c:14:
swtpm_setup_utils.c: In function 'get_config_value':
/usr/include/glib-2.0/glib/glib-autocleanups.h:28:3: error: 'tmp' may be used uninitialized in this function [-Werror=maybe-uninitialized]
   28 |   g_free (*pp);
      |   ^~~~~~~~~~~~
swtpm_setup_utils.c:36:31: note: 'tmp' was declared here
   36 |             g_autofree gchar *tmp;
      |                               ^~~

swtpm.c: In function 'swtpm_start':
/usr/include/glib-2.0/glib/glib-autocleanups.h:28:3: error: 'pidfile_file' may be used uninitialized in this function [-Werror=maybe-uninitialized]
   28 |   g_free (*pp);
      |   ^~~~~~~~~~~~
swtpm.c:54:23: note: 'pidfile_file' was declared here
   54 |     g_autofree gchar *pidfile_file;
      |                       ^~~~~~~~~~~~

Signed-off-by: Stefan Berger <stefanb@linux.ibm.com>
2021-06-04 16:04:09 -04:00
Stefan Berger
4ec17885c4 utils: Have logit and logerr call alog to unify their code
Signed-off-by: Stefan Berger <stefanb@linux.ibm.com>
2021-06-01 09:31:43 -04:00
Stefan Berger
e689684c57 samples: Rewrite of swtpm_localca in 'C' (addresses issue #437)
This is a 1:1 rewrite of swtpm_localca in 'C' as a final step to get
rid of the python dependency. It addresses issue #437.

Tested on: Cygwin, Fedora 33, Ubuntu Xenial & Bionic, FreeBSD,
           DragonFlyBSD, OpenBSD (i386), Mac, Debian (recent), Centos-8,
           CentOS (recent), Alpine (recent), OpenSUSE (recent),
           Fedora 28 PPC Big Endian

Signed-off-by: Stefan Berger <stefanb@linux.ibm.com>
2021-06-01 09:31:43 -04:00
Stefan Berger
b1cde69b09 swtpm_setup: Add proper DEPENDENCY for swtpm_setup on swtpm_utils.la
Signed-off-by: Stefan Berger <stefanb@linux.ibm.com>
2021-06-01 09:31:43 -04:00
Stefan Berger
c125e34bf2 swtpm_setup: Rewrite in 'C' (addresses issue #437)
This is a 1:1 rewrite of swtpm_setup in 'C' as a first step to get
rid of the python dependency. It addresses issue #437.

Tested on: Cygwin, Fedora 33, Ubuntu Xenial & Bionic, FreeBSD,
           DragonFlyBSD, OpenBSD (i386), Mac, Debian (recent), Centos-8,
           CentOS (recent), Alpine (recent), OpenSUSE (recent)

Signed-off-by: Stefan Berger <stefanb@linux.ibm.com>
2021-05-28 13:08:16 -04:00
Stefan Berger
eb178f87c6 swtpm_setup: Fix TPM 1.2 default SRK and owner password and help screen
Some of the wording in the help screen was odd, so rephrase it.

Fix the default owner and SRK passwords for the TPM 1.2 case.

Signed-off-by: Stefan Berger <stefanb@linux.ibm.com>
2021-05-26 19:52:06 -04:00
Stefan Berger
a56a3c027f swtpm_setup: Fix some minor issue
Fix some typos. The value for TPM2_ALG_SHA3_512 was wrong but also is
currently not used since libtpms does not support it yet.

Signed-off-by: Stefan Berger <stefanb@linux.ibm.com>
2021-05-26 19:52:06 -04:00
Stefan Berger
3be2ad2855 swtpm: Initialize key structure before calling OpenSSL function (Coverity)
Initialize the key structure before loading the key into it. This
resolves an issue raised by Coverity, but doesn't seem to be really
necessary.

Signed-off-by: Stefan Berger <stefanb@linux.ibm.com>
2021-05-22 11:28:53 -04:00
Jonas Witschel
4b825a3d94 swtpm_cert: rename deprecated libtasn1 types
These types have been renamed in libtasn1 version 3.0 (released 2012-10-28).
The most recent libtasn1 version 4.17.0 (released 2021-05-13) now prints
deprecation warnings that are made fatal by -Werror:

ek-cert.c:76:13: error: 'ASN1_ARRAY_TYPE' macro is deprecated, use 'asn1_static_node' instead. [-Werror]
   76 | extern const ASN1_ARRAY_TYPE tpm_asn1_tab[];
      |             ^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
[...]

The new types were introduced almost ten years ago, so they should be pretty
universally available by now.

Signed-off-by: Jonas Witschel <diabonas@archlinux.org>
2021-05-21 13:26:10 -04:00
Marc-André Lureau
da2d2f3996 build-sys: leave CFLAGS/LDFLAGS for user to be defined
This allows user to set specific flags during compilation, without
overriding configure-time cflags necessary for compilation.

See also:
https://www.gnu.org/software/automake/manual/html_node/User-Variables.html
https://www.gnu.org/software/automake/manual/html_node/Flag-Variables-Ordering.html

Signed-off-by: Marc-André Lureau <marcandre.lureau@redhat.com>
2021-05-18 18:24:51 -04:00
Stefan Berger
debdb13648 swtpm: Free error that was set by failed options_parse
Signed-off-by: Stefan Berger <stefanb@linux.ibm.com>
2021-03-21 08:18:23 -04:00
Stefan Berger
ef1407f56d swtpm_setup: Write note about non-standard EK when using --allow-signing
Write a note in swtpm_setup's help screen and man page that the usage
of --allow-signing will lead to a non-standard EK. Be more precise in the
man page.

Signed-off-by: Stefan Berger <stefanb@linux.ibm.com>
2021-03-01 07:54:27 -05:00
Stefan Berger
20524cb213 swtpm_ioctl: Use timeouts for communicating with swtpm (Unix socket)
Start using timeouts when communicating with swtpm over Unix sockets so
that swtpm_ioctl does not just hang when it cannot communicate with swtpm
such as establishing a connection or reading results. This is because swtpm
listens to nlye one control channel connections.

This patch addresses an aspect of the problem reported in issue #415
but may also starting hiding bugs if certain operations are done in
the wrong order, as was the case in libvirt (6.10 & 7.0).

Signed-off-by: Stefan Berger <stefanb@linux.ibm.com>
2021-02-25 07:28:47 -05:00
Stefan Berger
90e2be928d swtpm: Fix spelling in comment (SWPTM->SWTPM)
This patch fixes issue #410.

Signed-off-by: Stefan Berger <stefanb@linux.ibm.com>
2021-02-05 16:48:55 -05:00
Stefan Berger
554f52ae0a swtpm_setup: Fix typo in project URL (swptm->swtpm)
This fixes issue #410.

Signed-off-by: Stefan Berger <stefanb@linux.ibm.com>
2021-02-05 16:48:55 -05:00
Stefan Berger
fa1b93904e swtpm_setup: Replace OSError with ValueError (Cygwin bugfix)
A wrong domain to socketpair() causes a ValueError, not an OSError.

Signed-off-by: Stefan Berger <stefanb@linux.ibm.com>
2021-02-02 12:37:00 -05:00
Stefan Berger
224ee05163 swtpm_setup: Probe for supported socketpair domain (cygwin bugfix)
We need to probe for the support socketpair() domain. On Linux only
socket.AF_UNIX is support and on Cygwin AF_INET works.

Signed-off-by: Stefan Berger <stefanb@linux.ibm.com>
2021-02-01 14:48:32 -05:00
Stefan Berger
c396f23f50 swtpm: Fix --print-capabilities for 'swtpm chardev'
This patch fixes the following issue:

$ ./src/swtpm/swtpm chardev --print-capabilities --tpm2
swtpm: Error: Missing character device or file descriptor

Signed-off-by: Stefan Berger <stefanb@linux.ibm.com>
2021-01-13 11:32:16 -05:00
Stefan Berger
024073c5cc swtpm_cuse: Support parial reads (issue #376)
This patch adds support for partial reads to the CUSE swtpm.

We introduce a ptm_read_offset variable that holds the offset where to read
from next. It is reset every time a command has been processed as part of a
write() so that subsequent read()s start reading from offset 0. It is
advanced by the number of bytes that were read.

Signed-off-by: Stefan Berger <stefanb@linux.ibm.com>
2021-01-04 07:51:50 -05:00
Stefan Berger
e753128b27 swtpm: Remove unused file_ops_lock from threadpool.c (asan)
Asan reports this error for the CUSE TPM. The file_ops_lock was accidentally
duplicated in a code move of threadpool related code out of cuse_tpm.c
This patch removes the unused file_ops_lock from threadpool.c to resolve
the ASAN issue.

=================================================================
==545493==ERROR: AddressSanitizer: odr-violation (0x000000419340):
  [1] size=8 'file_ops_lock' cuse_tpm.c:112:8
  [2] size=8 'file_ops_lock' threadpool.c:55:8
These globals were registered at these points:
  [1]:
    #0 0x14f6c27f3cc8  (/lib64/libasan.so.6+0x37cc8)
    #1 0x40c2c3 in _sub_I_00099_1 (/home/stefanb/tmp/swtpm/src/swtpm/.libs/lt-swtpm+0x40c2c3)
    #2 0x40c31c in __libc_csu_init (/home/stefanb/tmp/swtpm/src/swtpm/.libs/lt-swtpm+0x40c31c)

  [2]:
    #0 0x14f6c27f3cc8  (/lib64/libasan.so.6+0x37cc8)
    #1 0x14f6c27aad1a in _sub_I_00099_1 (/home/stefanb/tmp/swtpm/src/swtpm/.libs/libswtpm_libtpms.so.0+0x25d1a)
    #2 0x14f6c31dc7b1 in call_init.part.0 (/lib64/ld-linux-x86-64.so.2+0x117b1)

==545493==HINT: if you don't care about these errors you may set ASAN_OPTIONS=detect_odr_violation=0
SUMMARY: AddressSanitizer: odr-violation: global 'file_ops_lock' at cuse_tpm.c:112:8
==545493==ABORTING

Signed-off-by: Stefan Berger <stefanb@linux.ibm.com>
2021-01-03 18:42:05 -05:00
Stefan Berger
4450856d6d swtpm: Free buffer after failure to realloc (cppcheck)
Free the original buffer after failure to realloc().

Signed-off-by: Stefan Berger <stefanb@linux.ibm.com>
2021-01-02 12:16:39 -05:00
Stefan Berger
6b50772217 swtpm: Remove global unused variable input (issue #395)
Remove the global unused variable with name input. This was detected
by cppcheck.

Signed-off-by: Stefan Berger <stefanb@linux.ibm.com>
2021-01-02 12:16:39 -05:00
Stefan Berger
62b7fcd9f3 swtpm_ioctl: Remove unnecessary if statement (issue #392)
Signed-off-by: Stefan Berger <stefanb@linux.ibm.com)
2020-12-30 08:11:14 -05:00
Stefan Berger
36ee55222f swtpm: Unconditionally close fd if writing of pidfile fails (coverity)
Do not bother trying to keep pidfilefd open in case fd = pidfilefd,
but close it unconditionally. If writing the pidfile fails, the process
terminates anyway, besides that we only ever need to write to the
pidfile once, which is happening in this function.

Signed-off-by: Stefan Berger <stefanb@linux.ibm.com>
2020-12-26 13:32:19 -05:00
Stefan Berger
e1212354c5 swtpm: Adjust size of message buffer to be of size ptm_hdata
This patch fixes the following compilation issue/bug:

ctrlchannel.c: In function ‘ctrlchannel_process_fd’:
ctrlchannel.c:694:13: error: array subscript ‘ptm_hdata[0]’ is partly outside array bounds of ‘struct output[1]’ [-Werror=array-bounds]
  694 |         data->u.resp.tpm_result = htobe32(res);
      |             ^~
ctrlchannel.c:486:7: note: while referencing ‘output’
  486 |     } output;
      |       ^~~~~~

Sending hashes to the TPM is not currently used in any major
application, so this bug should not affect much.

Signed-off-by: Stefan Berger <stefanb@linux.ibm.com>
2020-12-21 18:17:44 -05:00
Stefan Berger
a57c33a1f0 swtpm_setup: Increase timeout from 10s to 30s for slower machines
Older ARM processors need more time to create the initial CA keys
and then the key for the TPM. So we increase the timeout to 30s.

Signed-off-by: Stefan Berger <stefanb@linux.ibm.com>
2020-12-07 14:21:21 -05:00
Stefan Berger
1efe61a69e swtpm_setup: Do not follow symlink on logfile (CVE-2020-28407)
Signed-off-by: Stefan Berger <stefanb@linux.ibm.com>
2020-11-17 15:42:31 -05:00
Stefan Berger
04b099d14b swtpm: Do not follow symlink when accessing log file (CVE-2020-28407)
Signed-off-by: Stefan Berger <stefanb@linux.ibm.com>
2020-11-17 15:42:31 -05:00
Stefan Berger
678d5254de swtpm: Use open() (not fopen()) when accessing statefile (CVE-2020-28407)
This patch addresses CVE-2020-28407.

Use the open() call rather than the fopen() call when accessing
the statefile and make sure we do not follow symlinks using O_NOFOLLOW.

The modification does not allow an attacker to create a symbolic link
with the name of the temporary file (TMP2-00.permall for TPM 2) and
have this point to a valueable file and swtpm ends up overwriting the
file. The success of the attack depends on the attacker having access
to the TPM's state directory (--tpmstate dir=...).

Signed-off-by: Stefan Berger <stefanb@linux.ibm.com>
2020-11-13 09:44:05 -05:00
Stefan Berger
cf3dec63ea swtpm: Switch to open() from fopen() for the pidfile (CVE-2020-28407)
This patch addresses CVE-2020-28407.

Use the open() call rather than the fopen() call when creating a pidfile.
Also prevent us from following symbolic links when opening the pidfile for
writing.

Signed-off-by: Stefan Berger <stefanb@linux.ibm.com>
2020-11-13 09:44:05 -05:00
Stefan Berger
110ab4c908 swtpm: Do not follow symlinks when opening lockfile (CVE-2020-28407)
This patch addresses CVE-2020-28407.

Prevent us from following symliks when we open the lockfile
for writing.

Signed-off-by: Stefan Berger <stefanb@linux.ibm.com>
2020-11-13 09:44:05 -05:00
Stefan Berger
401c9e57a7 swtpm_cert: Switch to open() from fopen() for writing certificate
Switch to open from fopen() and make sure we do not follow symlinks.

Signed-off-by: Stefan Berger <stefanb@linux.ibm.com>
2020-11-13 09:44:05 -05:00
Stefan Berger
22419acee0 swtpm: Move setup of vtpm proxy device into own function
Lift the code that sets up the vtpm proxy device out of the main
function and put it into its own function.

Signed-off-by: Stefan Berger <stefanb@linux.ibm.com>
2020-10-29 18:02:36 -04:00
Stefan Berger
6559a90269 swtpm_cuse: Implement support for --flags startup-xyz options
Signed-off-by: Stefan Berger <stefanb@linux.ibm.com>
2020-10-13 17:08:05 -04:00
Stefan Berger
7eb2c29187 swtpm_cuse: Implement support for --flags not-need-init for CUSE TPM
Signed-off-by: Stefan Berger <stefanb@linux.ibm.com>
2020-10-13 17:08:05 -04:00
Stefan Berger
2ba2248351 swtpm_setup: Do not truncate logfile when testing write-access
Signed-off-by: Stefan Berger <stefanb@linux.ibm.com>
2020-10-06 15:05:08 -04:00
Stefan Berger
1d6a1554a6 swtpm_setup: Remove TPM state file in case error occurred
Remove the TPM state file in case an error occurred.

Signed-off-by: Stefan Berger <stefanb@linux.ibm.com>
2020-10-06 15:05:08 -04:00
Stefan Berger
07ea18bc38 build-sys: Add python-install and python-uninstall targets
To force the installation of the python programs add a python-install
target. This solves an installation issue with non-standard prefix,
like not using /usr or /usr/local, as may be the case on OS X (Apple)
when using homebrew. It leaves the 'make distcheck' working, which also
uses a non-standard prefix and where we don't want to install the tools
due to issues with removing them afterwards where pip3 cannot be used.

Signed-off-by: Stefan Berger <stefanb@linux.ibm.com>
2020-10-05 12:06:05 -04:00
Stefan Berger
3fde803eb6 swtpm: Write state files atomically using file renaming
To support writing state files atomically we first write into
a temporary file and then rename it.

Signed-off-by: Stefan Berger <stefanb@linux.ibm.com>
2020-10-05 08:56:20 -04:00
Stefan Berger
3f2bde0891 swtpm_setup: Add missing --print-capabilities to help screen
Signed-off-by: Stefan Berger <stefanb@linux.ibm.com>
2020-10-02 15:15:04 -04:00
Stefan Berger
d437937f39 swtpm: Prefix debug print function with SWTPM rather than TPM
To avoid clashes with libtpms print functions, prefix the print function
in swtpm with SWTPM_ rather than TPM_. This may matter on older systems
where libtpms could call into swtpm's TPM_PrintAll function due to it
having the same name as libtpms's function.

Signed-off-by: Stefan Berger <stefanb@linux.ibm.com>
2020-09-29 18:12:22 -04:00
Stefan Berger
03c00c0247 swtpm_setup: Do not hardcode '/etc' but use SYSCONFDIR
Do not hardcode the /etc path but use the compile-time generated
SYSCONFDIR instead.

Signed-off-by: Stefan Berger <stefanb@linux.ibm.com>
2020-09-23 09:42:38 -04:00
Stefan Berger
31ac728852 swtpm_setup: Fix support for -h and -? options
Signed-off-by: Stefan Berger <stefanb@linux.ibm.com>
2020-09-21 13:17:39 -04:00
Stefan Berger
ecabc015ae swtpm_setup: Add missing .config path when using ${HOME}
During the rewrite the additonal "/.config/" got dropped from the
path when using ${HOME} for locating he default config file.
Re-add it.

Signed-off-by: Stefan Berger <stefanb@linux.ibm.com>
2020-09-17 13:05:37 -04:00
Jonas Witschel
3dafa38eb7 build-sys: add configure option to disable installation of Python module
Distributions often have their own guidelines regarding installation of Python
modules (e.g. Arch Linux mandates using --optimize=1 for setup.py install,
which the Makefile in this project does not do). Add an option
"--disable-python-installation" (disabled by default) to allow skipping the
Python installation process entirely so that distributions can take care of
this process manually in their preferred way.

Signed-off-by: Jonas Witschel <diabonas@archlinux.org>
2020-09-14 07:40:31 -04:00
Stefan Berger
0586d2f55a build-sys: Use -Wl,-z,relro and -Wl,-z,now only when linking (clang)
Clang complains if eiher one of those linker flags are used during compilation:

clang-9: error: -Wl,-z,relro: 'linker' input unused [-Werror,-Wunused-command-line-argument]
clang-9: error: -Wl,-z,now: 'linker' input unused [-Werror,-Wunused-command-line-argument]

This patch applies those flags only when linking.

Signed-off-by: Stefan Berger <stefanb@linux.ibm.com>
2020-09-12 10:55:32 -04:00
Stefan Berger
3064a72ff0 swtpm_setup: Get rid of 'c' code and support changing user in python
Get rid of the 'c' code that only changed the user and add for support of
the --runas option to change to a different user in the python part.

To get 'make distcheck' to work I needed to name the swtpm_setup python
script with the suffix .in so that it gets copied to the build directory
as swtpm_setup. We need to change execute permissions on this file after
copying.

Signed-off-by: Stefan Berger <stefanb@linux.ibm.com>
2020-09-09 09:53:05 -04:00
Stefan Berger
094dba930f swtpm_setup: Fix wrong variable name detected by pylint 2.4.4
Signed-off-by: Stefan Berger <stefanb@linux.ibm.com>
2020-09-09 09:53:05 -04:00
Stefan Berger
cc410ca91b swtpm_setup: Switch over to new python tool and get rid of the bash script
Switch over to the new python implementation of swtpm_setup. We need to
also adjust test cases that involved the tcsd that otherwise fail for
various reasons. For in-place testing we need to adjust the PYTHONPATH
and PATH so that swtpm_setup.py can be found and so that swtpm_setup.py
then finds swtpm if it is not explicitly passed as parameter.

Adjust the man page for swtpm_setup to reflect the changes.

We now can run swtpm_setup as any user. However, libvirt still runs it
as tss:tss (for example), which is then creating the signing key as tss:tss
as well. Ideally libvirt would run it as tss:root or any other combination
since the tss group may be used for user wanting to access /dev/tpmrm0 for
example. We at least change the directory ownership of /var/lib/swtpm-localca
to tss:root and keep the world out of this directory.

Signed-off-by: Stefan Berger <stefanb@linux.ibm.com>
2020-08-28 16:27:15 -04:00
Stefan Berger
eee8cb5dfb swtpm_setup: Rewrite swtpm_setup.sh in python
Rewrite swtpm_setup.sh in python. Use a permanent connection from the
tool to swtpm via passing file descriptors of a unix socketpair. Implement
all functionality in python so that we don't rely on trousers and tpm-tools
for swtpm_setup. This now allows any user to setup a TPM 1.2 whereas before
it had to be root or the tss user.

Signed-off-by: Stefan Berger <stefanb@linux.ibm.com>
2020-08-28 16:27:15 -04:00
Stefan Berger
3a2f0be373 swtpm_setup: Accomodate for BSD sed that does not print \n as newline
BSD's sed does not print \n as newline, so we have to split the string
into different lines using tr.

Signed-off-by: Stefan Berger <stefanb@linux.ibm.com>
2020-08-19 16:06:10 -04:00
Stefan Berger
3bc10bd2ab build-sys: Label swtpm and swtpm_cuse with SELinux label
When running the selinux-install target also label swptm and swtpm_cuse.

Signed-off-by: Stefan Berger <stefanb@linux.ibm.com>
2020-08-19 14:36:10 -04:00
Stefan Berger
a7320793b8 build-sys: Add build targets selinux-install and selinux-uninstall
Add build targets selinux-install and selinux-uninstall to install
and uninstall the SELinux policy rules at a given priority. The
priority defeaults to 400, which works fine on Fedora.

Signed-off-by: Stefan Berger <stefanb@linux.ibm.com>
2020-08-19 10:50:05 -04:00
Stefan Berger
5a223d2d69 swtpm: Change include of sys/fcntl.h to fcntl.h
Signed-off-by: Stefan Berger <stefanb@linux.ibm.com>
2020-08-16 13:18:16 -04:00
Stefan Berger
88c9deb95d swtpm_setup: Get rid of eval when calling external tool (swtpm-localca)
Get rid of using eval when calling $create_certs_tool and only use
eval for resolving variables from the config file.

We only want variable substitution for entries from configuration
files, so escape all other special shell characters that may be
making it onto the command line so that no subshells are opened
and no redirection to files can occurr.

Signed-off-by: Stefan Berger <stefanb@linux.ibm.com>
2020-08-11 15:01:59 -04:00
Stefan Berger
b71a228538 swtpm_setup: Only change file and directory ownership if needed
Change the file and directory ownership of tcsd related files only if it
is absolutely needed. It is not needed if we are running as user TCSD_USER
in group TCSD_GROUP because then the files were created with the needed
owner and group. This avoids problems when trying to change file ownership
when invoked by libvirt where we do not have the capabilities to change
file ownership even as root.

Signed-off-by: Stefan Berger <stefanb@linux.ibm.com>
2020-07-24 10:50:08 -04:00
Stefan Berger
0d9d33fb05 swtpm_setup: Allow group read-rights on $TCSD_CONFIG file if tss user != tss group
When the TSS_USER != TSS_GROUP, e.g., user 'root' and group 'tss', then
tcsd requires that the access mode bits on the $TCSD_CONFIG file are set
to 0640, otherwise we get this error:

TCSD ERROR: TCSD config file (/tmp/tmp.Yd4LIF7mCE) must be mode 0640

Signed-off-by: Stefan Berger <stefanb@linux.ibm.com>
2020-07-24 10:50:08 -04:00
Stefan Berger
9d8d6d0378 swtpm_setup: log errors from tcsd in logfile
Redirect stdout and stderr from tcsd into a file and if tcsd reported
an error copy the error into the logfile. This makes debugging tcsd
related issues, such as ownership or access mode issues, easier.

Signed-off-by: Stefan Berger <stefanb@linux.ibm.com>
2020-07-24 10:50:08 -04:00
Stefan Berger
0371b63bec build-sys: Explicitly link libswtpm_libtpms with -lcrypto (Gentoo)
This patch fixes the following linker issue reported for Gentoo in
issue #280.

ld.lld: error: /var/tmp/portage/app-crypt/swtpm-0.3.1-r1/work/swtpm-0.3.1/src/swtpm/.libs/libswtpm_libtpms.so: undefined reference to EVP_sha512
ld.lld: error: /var/tmp/portage/app-crypt/swtpm-0.3.1-r1/work/swtpm-0.3.1/src/swtpm/.libs/libswtpm_libtpms.so: undefined reference to PKCS5_PBKDF2_HMAC
ld.lld: error: /var/tmp/portage/app-crypt/swtpm-0.3.1-r1/work/swtpm-0.3.1/src/swtpm/.libs/libswtpm_libtpms.so: undefined reference to SHA512

Signed-off-by: Stefan Berger <stefanb@linux.ibm.com>
2020-07-21 20:52:34 -04:00
Seunghun Han
2bc601bba5 Fix typos in code and man pages
This patch fixes typos of code and man pages reported by lintian.

Signed-off-by: Seunghun Han <kkamagui@gmail.com>
2020-06-15 08:11:34 -04:00
Stefan Berger
b79970f637 swtpm_setup: Create the RSA 3072 EK key with an empty nonce
Create the RSA 3072 EK key with an empty nonce rather than a nonce full
of 0 bytes.

Signed-off-by: Stefan Berger <stefanb@linux.ibm.com>
2020-05-18 09:26:47 -04:00
Stefan Berger
20fd182c24 swtpm_setup: Put RSA 3072 EK Certificate into NVRAM idx 0x01c0001c
Put the RSA 3072 EK Certificate into NVRAM index 0x01c0001c.

Signed-off-by: Stefan Berger stefanb@linux.ibm.com>
2020-05-18 09:26:47 -04:00
Stefan Berger
7dc24c2f8c swtpm_setup: Allow to create largest possible EK
By passing '--rsa-keysize max' allow to create the largest possible RSA
EK key.

Signed-off-by: Stefan Berger <stefanb@linux.ibm.com>
2020-05-05 09:12:21 -04:00
Stefan Berger
6c5b7c2d86 swtpm_setup: Add support for RSA 3072 bit EK keys
Extend the creation of the EK key to support also 3072 bits RSA keys.

Signed-off-by: Stefan Berger <stefanb@linux.ibm.com>
2020-05-05 09:12:21 -04:00
Stefan Berger
80d7bb488c swtpm_setup: Report supported RSA key sizes useful for EK key creation
Extend the --print-capabilities option to also report supported RSA
key sizes. Only the TPM 2 may support anything else than 2048 bit RSA
keys, so we only consult 'swtpm socket --tpm2 --print-capabilities'
and grep for 2048 and 3072 key sizes and report them.
If nothing is found, nothing is reported, as before, and 2048 bit RSA
keys should be assumed.

'swtpm_setup --tpm2 --print-capabilities' may now show the following:
{
  "type": "swtpm_setup",
  "features": [
    "cmdarg-keyfile-fd",
    "cmdarg-pwdfile-fd",
    "tpm2-rsa-keysize-2048",
    "tpm2-rsa-keysize-3072"
  ]
}

Also adjust a test case to use a regular expression for matching
against an expected string that may nor may not have rsa-keysize
verbs.

Signed-off-by: Stefan Berger <stefanb@linux.ibm.com>
2020-05-05 09:12:21 -04:00
Stefan Berger
c6b52d18e0 swtpm: Construct RSA key size capabilities from TPMLIB_GetInfo()
Construct RSA key size capability strings from libtpms TPMLIB_GetInfo()
string so that we can easily show which RSA key sizes are supported by
the TPM 2 implementation. If none are advertised, 1024 & 2048 can be
assumed to be supported.

'swtpm socket --tpm2 --print-capabilities' may now print the following:
{
  "type": "swtpm",
  "features": [
    "tpm-send-command-header",
    "flags-opt-startup",
    "cmdarg-seccomp",
    "cmdarg-key-fd",
    "cmdarg-pwd-fd",
    "no-tpm12-tools",
    "rsa-keysize-1024",
    "rsa-keysize-2048",
    "rsa-keysize-3072"
  ]
}

We need to adapt the related test case to use a regular expression since
the rsa-keysize-xyz strings may or may not be there depending on libtpms
version.

Signed-off-by: Stefan Berger <stefanb@linux.ibm.com>
2020-05-04 18:12:24 -04:00
Stefan Berger
1974f2ce20 swtpm_setup: Report 'no-tpm12-tools' if tcsd or tpm-tools missing
If the host is missing tcsd (trousers) or the tpm-tools, swtpm_setup
will now report the 'no-tpm12-tools' verb like this:

> swtpm_setup --print-capabilities | jq

{
  "type": "swtpm_setup",
  "features": [
    "cmdarg-keyfile-fd",
    "cmdarg-pwdfile-fd",
    "no-tpm12-tools"
  ]
}

The only TPM 1.2 setup parameter that requires interaction with
the TPM 1.2 that can be pass is then '--createek'.

Signed-off-by: Stefan Berger <stefanb@linux.ibm.com>
2020-05-01 17:47:03 -04:00
Stefan Berger
c7c2f90aeb swtpm_setup: bugfix: Create ECC storage primary key in owner hierarchy
The ECC storage primary key was mistakently created in the endorsement
hierarchy but should be in the owner hierarchy. This patch corrects this
to have this key created in the owner hierarchy (like the RSA key),
thus using 0x40 00 00 01.

This only mattered if one used --create-spk and --ecc together.

Signed-off-by: Stefan Berger <stefanb@linux.ibm.com>
2020-05-01 15:27:40 -04:00
Stefan Berger
6d0ef98c50 swtpm_setup: return result of called function rather than 0
Return the result of the called function rather than 0.

Fixes: d65f5ae1 ("swtpm_setup: Create RSA 2048 and ECC NIST P256 keys and certs")
Signed-off-by: Stefan Berger <stefanb@linux.ibm.com>
2020-05-01 11:53:38 -04:00
Stefan Berger
21af92fd04 swtpm_setup: bugfix: remove tpm2_stirrandom and tpm2_changeeps
Remove tpm2_stirrandom, which we should not need to run on a newly
created TPM 2.
Also remove tpm2_changeeps which was called twice when creating two
EKs, thus invalidating a previous EK that may have been created.

Signed-off-by: Stefan Berger <stefanb@linux.ibm.com>
2020-04-30 22:20:08 -04:00
Stefan Berger
f58ead05d8 swtpm_setup: Switch to NIST P384 curves
Switch to NIST P384 curves for the storage primary key as well as the 2nd EK.
The EK part now complies with the following specification:

TCG PC Client Platform TPM Profile Specification for TPM 2.0
Version 1.04
Revision 37
February 3, 2020

Signed-off-by: Stefan Berger <stefanb@linux.ibm.com>
2020-04-30 13:41:50 -04:00
Stefan Berger
d65f5ae13b swtpm_setup: Create RSA 2048 and ECC NIST P256 keys and certs
Following "TCG PC Client Platform TPM Profile Specification for
TPM 2.0, version 1.04, Rev 37" create and RSA and an ECC NIST P256
key now. We will upgrade the ECC NIST key to P384 in the next
step.

Signed-off-by: Stefan Berger <stefanb@linux.ibm.com>
2020-04-30 13:41:50 -04:00
Stefan Berger
8dd0eb5d44 swtpm_setup: Move code into tpm2_create_ek_and_cert
Move a whole bunch of EK and certificate creation code into its
own function.

Signed-off-by: Stefan Berger <stefanb@linux.ibm.com>
2020-04-30 13:41:50 -04:00
Stefan Berger
3d663bacd7 swtpm_setup: Implement function to create ECC NIST P384 EK keys
Signed-off-by: Stefan Berger <stefanb@linux.ibm.com>
2020-04-30 13:41:50 -04:00
Stefan Berger
e8d9126d7b swtpm_setup: Rename NONCE_ECC to NONCE_ECC_256
Signed-off-by: Stefan Berger <stefanb@linux.ibm.com>
2020-04-29 20:35:04 -04:00
Stefan Berger
ee95fd88c0 swtpm_setup: Pass the ECC curve id and hash alg. into functions
Pass the ECC curve id and hash algorithm and the ECC_NONCE to the
function creating the ECC keys rather than hard coding them. Rename
the functions that create the NIST_P256 ECC keys to have _nist_p256
suffix in the name.

Signed-off-by: Stefan Berger <stefanb@linux.ibm.com>
2020-04-29 20:35:04 -04:00
Stefan Berger
8b0367e3b3 swtpm_setup: Calculate offset for 2nd ecc key part based on length
Rather than passing the offset of the 2nd part of an ecc key pass
the length of the key and calculate the 2nd offset using the length.

Signed-off-by: Stefan Berger <stefanb@linux.ibm.com>
2020-04-29 20:35:04 -04:00
Stefan Berger
5f694dbcb7 swtpm_setup: Leave notes about version of templates that were used
Leave a not about what version of template was used for the
RSA and ECC EK keys before updating them now.

Signed-off-by: Stefan Berger <stefanb@linux.ibm.com>
2020-04-29 20:35:04 -04:00
Stefan Berger
fbc42b8d9f swtpm_cert: Support --ecc-curveid option to pass curve id
Implement support for passing the curve id via the --curve-id
option. Default assumes secp256r1. secp384r1 is also supported.

Signed-off-by: Stefan Berger <stefanb@linux.ibm.com>
2020-04-29 20:35:04 -04:00
Stefan Berger
d14934f3a6 swtpm: Make coverity happy by handling default case in case statement
Handle the default cases, which shouldn't ever be reachable, and set
the tocopy to '0' so that no unitialized variable gets copied.

Signed-off-by: Stefan Berger <stefanb@linux.ibm.com>
2020-04-24 16:20:26 -04:00
Stefan Berger
3a3a9f5b5f swtpm: Invoke print capabilites after choosing TPM version
Invoke the printing of the capabilites after choosing the TPM version
in libtpms.

Signed-off-by: Stefan Berger <stefanb@linux.ibm.com>
2020-04-23 16:40:13 -04:00
Stefan Berger
548eb6859b swtpm: Remove unnecessary #include <seccomp.h> (fixes SuSE build)
It's not necessary anymore to #include <seccomp.h> from the main programs.
Once removed, it also fixes the build on SuSE where seccomp.h is in
seccomp/seccomp.h and we didn't use the LIBSECCOMP_CFLAGS for swtpm.c etc.

Signed-off-by: Stefan Berger <stefanb@linux.ibm.com>
2020-04-20 19:31:09 -04:00
Stefan Berger
eb9cfa963a swtpm: Add some recent syscalls to seccomp blacklist
Add some recent syscalls to the seccomp blacklist, particularly
setter type of functions.

Signed-off-by: Stefan Berger <stefanb@linux.ibm.com>
2020-04-13 12:26:11 -04:00
Stefan Berger
dd92f45898 swtpm: Address cygwin compilation warning
Compilation on cygwin reports the following issue:

In file included from key.c:43:
key.c: In function ‘key_stream_to_bin’:
key.c:135:26: error: array subscript has type ‘char’ [-Werror=char-subscripts]
  135 |            !isspace(input[digits]) &&
      |                     ~~~~~^~~~~~~~
key.c:143:40: error: array subscript has type ‘char’ [-Werror=char-subscripts]
  143 |     if (input[digits] && !isspace(input[digits]))
      |                                   ~~~~~^~~~~~~~

Address the issue using an explicit cast of char to int.

Signed-off-by: Stefan Berger <stefanb@linux.ibm.com>
2020-04-12 14:32:38 -04:00
Stefan Berger
bacb56b4ec swtpm_setup: Remove some old cruft
Remove some commented code and some code that isn't executed any
differently for the user id that's running it.

Signed-off-by: Stefan Berger <stefanb@linux.ibm.com>
2020-04-10 09:35:20 -04:00
Stefan Berger
029e9d4638 swtpm_setup: Explicitly set TCSD_TCP_DEVICE_HOSTNAME=127.0.0.1
To make swtpm_setup.sh work on Travis on Bionic we need to
explicitly set TCSD_TCP_DEVICE_HOSTAME=127.0.0.1 since lookup
of localhost (with the API the tcsd is using) does not work.
It doesn't negatively affect any other use case, so no problem
setting it.

Also replace localhost in the bash tcp device path with 127.0.0.1.

Signed-off-by: Stefan Berger <stefanb@linux.ibm.com>
2020-04-09 19:39:38 -04:00
Stefan Berger
cb5ab582d1 swtpm: Fix vtpm proxy case without startup flags
'swtpm chardev --vptm-proxy' currently requires a '--flag startup-xyz'
to be passed since otherwise the need_init_cmd variable would not be
set to false and swtpm would terminate after sending the startup
command. To maintain backwards compatibility we have to always
set the need_init_cmd variable to false for the --vtpm-proxy case
and must not require a startup flag to be passed.

Roll back one of the test case to not use the startup flag.

Fixes: e6bc4bdf0 ('swtpm: Enable sending startup commands ...')
Signed-off-by: Stefan Berger <stefanb@linux.ibm.com>
2020-03-07 09:09:14 -05:00
Stefan Berger
d1083d1164 swtpm: Only call memcpy if tocopy != 0 (coverity)
Signed-off-by: Stefan Berger <stefanb@linux.ibm.com>
2020-02-14 09:53:32 -05:00
Stefan Berger
e6bc4bdf0a swtpm: Enable sending startup commands before processing commands
The vtpm proxy device requires that the TPM be 'Startup' otherwise it
fails sending the first command to it and will send the 'Startup'
(SU_CLEAR/TPM_ST_CLEAR) itself while adding a log entry. We want to
avoid the kernel log entry.

Add options to the existing --flags option that allows one to start
up the TPM 1.2 or TPM 2.0 with the startup types 'clear', 'state'
and 'deactivate' (TPM 1.2 only). Extend the --print-capabilities to
advertise the availability of these options with the string
'flags-opt-startup'.

Signed-off-by: Stefan Berger <stefanb@linux.ibm.com>
2020-02-08 18:19:08 -05:00
Stefan Berger
6eef00e416 swtpm_cert: Accept serial numbers that use up to 64bits
Accept serial number that use up to 64bits.

Signed-off-by: Stefan Berger <stefanb@linux.ibm.com>
2020-01-30 08:52:58 -05:00
Stefan Berger
a718f2a9e7 swtpm_cert: Use getopt_long_only to parse options
Convert the code to use getopt_long_only for parsing the options.

Signed-off-by: Stefan Berger <stefanb@linux.ibm.com>
2020-01-30 08:52:58 -05:00
Stefan Berger
88c7bdc9e3 swtpm_cert: Add support for --print-capabilities option
Add support for the --print-capabilities option to display newly
added capabilities. Adpat the man page and related test case.

Signed-off-by: Stefan Berger <stefanb@linux.ibm.com>
2020-01-30 08:52:58 -05:00
Stefan Berger
b35eb9fcd5 swtpm_cert: Allow passing signing key and parent key via new option
Allow passing signing key and parent key via files and file descriptors
and environment variables. Adapt a test case to exercise this new
functionality.

Signed-off-by: Stefan Berger <stefanb@linux.ibm.com>
2020-01-30 08:52:58 -05:00
Stefan Berger
f9547ddc2c swtpm_setup: Enable spaces in paths and other variables
This patch addresses several issues found with shellcheck. In particular
it now enables variables with spaces in them, such as file paths that
contain spaces.

Adjust one of the accompanying test cases to use spaces in the path.

Signed-off-by: Stefan Berger <stefanb@linux.ibm.com>
2020-01-27 09:16:16 -05:00
Stefan Berger
d66f4d1e03 swtpm_ioctl: Calculate strlen(input) only once
Calculate the strlen(input) only once at the beginning.

Signed-off-by: Stefan Berger <stefanb@linux.ibm.com>
2020-01-22 19:32:24 -05:00
Stefan Berger
3eef9811b9 swtpm_ioctl: Block SIGPIPE so we can get EPIPE on write()
Block the SIGPIPE so that a failing write() can return an EPIPE
rather than killing the process with a SIGPIPE.

Signed-off-by: Stefan Berger <stefanb@linux.ibm.com>
2020-01-22 19:32:24 -05:00
Stefan Berger
f27256f154 swtpm_bios: Block SIGPIPE so we can get EPIPE on write()
Block the SIGPIPE so that a failing write() can return an EPIPE
rather than killing the process with a SIGPIPE.

Signed-off-by: Stefan Berger <stefanb@linux.ibm.com>
2020-01-22 19:32:24 -05:00
Stefan Berger
abadb408a9 swtpm: Only accept() new client ctrl connection if we have none
Only accept new client connection on the control channel if we
currently do not have a client on the control channel.

Signed-off-by: Stefan Berger <stefanb@linux.ibm.com>
2020-01-22 19:32:24 -05:00
Stefan Berger
de10d7cd59 swtpm_setup: Do not fail on future PCR banks' hashes
swtpm_setup will fail once libtpms starts supporting other PCR
hash banks than sha1, sha256, sha384, sha512, and sm3-256. So,
this patch allows to choose active PCR banks of the SHA3 series.
Further, unknown hash banks will not fail the tool anymore when
it tries to determine which hash banks are supported by the TPM
since it will then add the hex number of the hash algorithm to
the collection of supported hashes.

Signed-off-by: Stefan Berger <stefanb@linux.ibm.com>
2020-01-17 20:07:37 -05:00
Stefan Berger
84f350dace swtpm_setup: Use 1st part of SWTPM_EXE/SWTPM_IOCTL to determine executable
Search for the SWTPM_EXE / SWTPM_IOCTL executable using 'type -P' to
determine whether it is an executable rather than assuming a full path
is given on which we can check -x.

Signed-off-by: Stefan Berger <stefanb@linux.ibm.com>
2020-01-13 15:03:01 -05:00
Stefan Berger
fa0ecd9951 swtpm_setup: Keep reserved range of file descriptors for swtpm_setup.sh
swtpm_setup.sh uses file descriptor 100 for 'exec 100 <> ...'.
So we have to make sure that the file descriptor inherited from
the caller of swtpm_setup does not overlap with a reserved range
to be used by swtpm_setup.sh, which we declare to be [100..109].

Signed-off-by: Stefan Berger <stefanb@linux.ibm.com>
2020-01-09 14:22:50 -05:00
Stefan Berger
5196855ad1 swtpm_setup: Log about encryption and fix c&p error in err msg
Fix a cut and paste error in the error message output and be more
verbose in log about encryption when using file descriptors.

Signed-off-by: Stefan Berger <stefanb@linux.ibm.com>
2020-01-07 20:06:03 -05:00
Stefan Berger
a7c4f3734a swtpm: Add --print-capabilities to help screen of 'swtpm chardev'
The --print-capabilities is missing in the 'swtpm chardev' help screen
but the code is there to interpret the command line flag. This patch
adds the missing lines to the help screen.

Signed-off-by: Stefan Berger <stefanb@linux.ibm.com>
2020-01-07 11:18:10 -05:00
Stefan Berger
8330ccd07e swtpm_ioctl: Fix uninitialized variable 'pgi'
Signed-off-by: Stefan Berger <stefanb@linux.ibm.com>
2019-12-23 15:06:33 -05:00
Stefan Berger
132bbf7b7a swtpm_cert: Use gnutls_x509_crt_get_subject_key_id API call for subj keyId
Use the gnutls_x509_crt_get_subject_key_id() API call to get the subject
key ID of the signer's certificate rather than gnutls_x509_crt_get_key_id().
The latter returns the same result most of the time but there are instances
when this is not the case and the returned result is wrong.

Signed-off-by: Stefan Berger <stefanb@linux.ibm.com>
2019-10-22 12:39:29 -04:00
Jeremy Liu
36bfbb7432 Correct parameter name typo
Signed-off-by: Jeremy Liu <liujiong@gohighsec.com>
2019-10-17 12:29:38 -04:00
Stefan Berger
a85b7d2823 swtpm_cert: Fix OIDs for TPM 2 platforms data
The OIDs for the TPM 1.2 and TPM 2 platform data
are different (though have the same name in the spec).
Adapt them for the TPM 2 case.

Signed-off-by: Stefan Berger <stefanb@linux.ibm.com>
2019-10-11 08:34:30 -04:00
Stefan Berger
ab1b346ef0 SELinux: Need more rules for QEMU and KVM type of VMs on F30
More rules are needed for QEMU and KVM type VMs on F30.

Signed-off-by: Stefan Berger <stefanb@linux.ibm.com>
2019-07-26 20:38:46 -04:00
Stefan Berger
bea37691ac swtpm: Fix typo in error report: HMAC instead of hash
Signed-off-by: Stefan Berger <stefanb@linux.ibm.com>
2019-07-25 10:09:25 -04:00
Stefan Berger
5c5d3b793b swtpm: Use writev_full rather than writev; fixes --vtpm-proxy EIO error
We have to use writev_full() when --vtpm-proxy is used since writev()
does not seem to work and we get an EIO error. writev_full() uses write(),
so we can use it.

Signed-off-by: Stefan Berger <stefanb@linux.ibm.com>
2019-07-23 10:06:20 -04:00
Stefan Berger
7491ed0f2a SELinux: A few more rules needed for F30
A few more rules are needed on Fedora 30 to have libvirt start swtpm.

Signed-off-by: Stefan Berger <stefanb@linux.ibm.com>
2019-07-23 10:06:20 -04:00
Stefan Berger
374b66913f swtpm: Address Coverity's dead code complaint
Signed-off-by: stefan Berger <stefanb@linux.ibm.com>
2019-07-13 21:53:13 -04:00
Stefan Berger
ad2fb40077 swtpm: Loop over poll() in case of EINTR
Signed-off-by: Stefan Berger <stefanb@linux.ibm.com>
Reviewed-by: Marc-André Lureau <marcandre.lureau@redhat.com>
2019-07-13 12:48:36 -04:00
Stefan Berger
a442092d8e swtpm: Convert read() to read_eintr() which handles EINTR
Signed-off-by: Stefan Berger <stefanb@linux.ibm.com>
Reviewed-by: Marc-André Lureau <marcandre.lureau@redhat.com>
2019-07-13 12:48:36 -04:00
Stefan Berger
37ac6aec73 swtpm: Implement read_eintr() to read into a buffer and handle EINTR
Implement read_eintr() to read into a buffer and handle EINTR on
the way.

Signed-off-by: Stefan Berger <stefanb@linux.ibm.com>
Reviewed-by: Marc-André Lureau <marcandre.lureau@redhat.com>
2019-07-13 12:48:36 -04:00
Stefan Berger
ec355ee80c swtpm: Use write_full instead of plain write
Signed-off-by: Stefan Berger <stefanb@linux.ibm.com>
Reviewed-by: Marc-André Lureau <marcandre.lureau@redhat.com>
2019-07-13 12:48:36 -04:00
Stefan Berger
2d921e663c swtpm: Implement write_full and writev_full
Implement write_full and writev_full that handle partial writes.

Implement writev_full using write_full since handling partial writes
with iovec's can become quite complicated.

Signed-off-by: Stefan Berger <stefanb@linux.ibm.com>
Reviewed-by: Marc-André Lureau <marcandre.lureau@redhat.com>
2019-07-13 12:48:36 -04:00
Stefan Berger
30e12eae27 SELinux: Add rule for passing pass phrase via file descriptor
Add a rule so we can pass a passphrase from libvirt to swtpm using
a file descriptor.

Signed-off-by: Stefan Berger <stefanb@linux.ibm.com>
2019-07-12 14:18:20 -04:00
Stefan Berger
1edad44acd swtpm_setup: Add --print-capabilities option
Extend swtpm_setup with the --print-capabilities option which
prints a JSON object describing supported features.

Signed-off-by: Stefan Berger <stefanb@linux.ibm.com>
2019-07-12 14:18:20 -04:00
Stefan Berger
c11bf8c5f8 swtpm_setup: Test filenames for empty string before trying to remove
rm on NetBsd prints out an error message when trying to remove a file
with an empty filename. Avoid this situation by checking for non-empty
filenames first.

Signed-off-by: Stefan Berger <stefanb@linux.ibm.com>
2019-07-12 14:18:20 -04:00
Stefan Berger
11114ba745 swtpm: Add --print-capabilities option to command line
Signed-off-by: Stefan Berger <stefanb@linux.ibm.com>
2019-07-12 14:18:20 -04:00
Stefan Berger
1d5e07974b swtpm: Implement support for printing newly added capabilities
Implement support for printing newly added capabilities, such as
being able to read the key and password from a file descriptor.

Signed-off-by: Stefan Berger <stefanb@linux.ibm.com>
2019-07-12 14:18:20 -04:00