Commit Graph

71 Commits

Author SHA1 Message Date
Stefan Berger
27d4c5d83f swtpm_localca: Test for available issuercert before creating CA
Avoid trying to create TPM certificates while the issuer certificate has
not been created, yet (in a 2nd step).

To resolve this do not just test for availability of the signing key, which
is created first, but also test for the issuer certifcate, which is created
in a 2nd step when the local CA is created. If either one is missing,
attempt to create the CA.

Resolves: #644
Signed-off-by: Stefan Berger <stefanb@linux.ibm.com>
2022-02-17 20:29:17 -05:00
Stefan Berger
e621b21d4c samples: Do not follow symlink on logfile (CVE-2020-28407)
Signed-off-by: Stefan Berger <stefanb@linux.ibm.com>
2020-11-17 15:47:43 -05:00
Stefan Berger
5af35971b8 samples: 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
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
48275746e3 samples: Use ${HOME}/.config if XDG_CONFIG_HOME is not set (RHEL)
When creating the user config files, fall back to using ${HOME}/.config
if XDG_CONFIG_HOME is not set on a system.

Signed-off-by: Stefan Berger <stefanb@linux.ibm.com>
2020-09-23 18:14:23 -04:00
Stefan Berger
7b72dfedec samples: swtpm-localca: Pass password via template file when possible
Pass the CA's private key password via the template file. Remove recently
added old GnuTLS support. Extend man page with a paragraph about short-
comings of certtool that doesn't seem to allow private key password being
passed either as environment variable or template file.

Signed-off-by: Stefan Berger <stefanb@linux.ibm.com>
2020-09-21 16:01:20 -04:00
Stefan Berger
c73771b0c5 samples: Fix support for -h and -? options in swtpm-localca
Signed-off-by: Stefan Berger <stefanb@linux.ibm.com>
2020-09-21 13:17:39 -04:00
Stefan Berger
3ed0744de0 samples: Add --password parameter for older GnuTLS versions
Older versions of GnuTLS need --password on the command line while
newer versions require GNUTLS_PIN to be set to pass the password.
So, this patch accomodates older versions of GnuTLS.

Older version of GnuTLS seem to NOT be able to handle a CA private
key needing a password and a signing key needing a password when
creating the intermediate CA. So this case of 2 passwords will
always fail.

The value of the local CA is not so high that passing passwords on
the command line would be an issue. Later on when using the CA the
password are set via environment variables, so not visible to other
users.

Signed-off-by: Stefan Berger <stefanb@linux.ibm.com>
2020-09-18 20:12:01 -04:00
Stefan Berger
14d2d4381d samples: Add support for pkcs11 module environment variables to config file
Add support for pkcs11 module environment variables to the config file.
These variables may have the following format:

   env:VARNAME=VALUE

Signed-off-by: Stefan Berger <stefanb@linux.ibm.com>
2020-09-18 12:09:11 -04:00
Stefan Berger
cc592a0931 samples: Also allow signingkey_password to be used not just 'SWTPM_PKCS11_PIN'
Signed-off-by: Stefan Berger <stefanb@linux.ibm.com>
2020-09-18 12:09:11 -04:00
Stefan Berger
b71da43026 samples: Rewrite swtpm-localca in python
Rewrite swtpm-localca in python and get rid of the bash file and the flock
dependency.

Signed-off-by: Stefan Berger <stefanb@linux.ibm.com>
2020-09-18 12:09:11 -04:00
Stefan Berger
a73e9cb831 samples: Apply password for signing key when creating platform cert (bugfix)
Apply the password for the signing key and parent key when creating a
platform certificate. The bug was introduced in commit 961bb827
"samples: Use new pwd format option for secretly passing passwords"

Extend a test case to ensure that the passwords are always used.

Signed-off-by: Stefan Berger <stefanb@linux.ibm.com>
2020-09-15 21:08:32 -04:00
Stefan Berger
79a0bdb483 samples: Properly apply passwords for localca signing key (bugfix)
When the local CA was create the signing key of the intermediate CA
was not properly protected by the passwords. This patch fixes this.

Signed-off-by: Stefan Berger <stefanb@linux.ibm.com>
2020-09-15 21:08:32 -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
a12b09b1ea samples: Implement script to create user config files and extend man page
Implement a script that creates the user config files in the
${XDG_CONFIG_HOME} directory and sub-directories.

Extend swtpm_setup.pod showing swtpm-create-user-config-files usage.

Signed-off-by: Stefan Berger <stefanb@linux.ibm.com>
2020-08-20 09:49:33 -04:00
Stefan Berger
217c29cb07 Revert "samples: Implement script to create user config files"
This reverts commit d09484c02d.
2020-08-20 09:49:33 -04:00
Stefan Berger
d09484c02d samples: Implement script to create user config files
Implement a script that creates the user config files in the
${XDG_CONFIG_HOME} directory and sub-directories.

Signed-off-by: Stefan Berger <stefanb@linux.ibm.com>
2020-08-19 14:39:31 -04:00
Stefan Berger
d920680776 samples: Get rid of using eval when running swtpm_cert
Get rid of using eval when running swtpm_cert in swtpm-localca.
This is to avoid further evaluation of bash expression that can
spawn subshells ('$(echo foo)') or do other bad things. Bad input
could come from malformed configuration files.

Signed-off-by: Stefan Berger <stefanb@linux.ibm.com>
2020-08-11 15:01:59 -04:00
Stefan Berger
b6aef51ff3 samples: Escape many more characters before calling eval on an entry
Escape many more special shell characters before calling eval on
an entry to convert a variable to its value. Uncareful writing of
a swtpm-local.conf config file could have lead to files being over-
written using '>' for example.

Signed-off-by: Stefan Berger <stefanb@linux.ibm.com>
2020-08-11 15:01:59 -04:00
Stefan Berger
72aac899fa samples: Parse optional id= for ECC curve IDs
Signed-off-by: Stefan Berger <stefanb@linux.ibm.com>
2020-04-29 20:35:04 -04:00
Stefan Berger
f30e202df6 samples: Extend script to create a CA using a TPM 2 for signing
Extend the script that creates a CA that uses a TPM 2 for signing.
For this we have to create tokens using the TPM 2 pkcs11 module's
tpm2_ptool and can then use the p11tool for creating keys.

Add a test case that requires a running tpm2-abrmd and tpm2_ptool.
Eventually the test case should (try to) start its own tpm2-abrmd
and talk to swtpm directly but the tcti module to do that isn't
available as a package, yet.

Signed-off-by: Stefan Berger <stefanb@linux.ibm.com>
2020-04-14 13:09:00 -04:00
Stefan Berger
961bb827a4 samples: Use new pwd format option for secretly passing passwords
Use the swtpm_cert --signkey-pwd and --parentkey-pwd to pass key passwords
using files rather than using the command line options.

Signed-off-by: Stefan Berger <stefanb@linux.ibm.com>
2020-01-30 08:52:58 -05:00
Stefan Berger
b291eb83ed samples: Protect variables in swtpm-create-tpmca
Address several issues reported by shellcheck and protect
variables with quotes so we now can have filenames with spaces.

Signed-off-by: Stefan Berger <stefanb@linux.ibm.com>
2020-01-27 09:16:16 -05:00
Stefan Berger
77819bb2e6 samples: Protect variables with quotes and address shellcheck issues
Protect variables with quotes so that pathnames with spaces are now
supported.

Adjust the accompanying test case to make use of spaces in file paths.

Address several issues found by shellcheck. Some of them are false
positives especially when it comes to protecting variables passed
to a commaned in an 'eval' line. They must not be protected, otherwise
they are not passed correctly.

Signed-off-by: Stefan Berger <stefanb@linux.ibm.com>
2020-01-27 09:16:16 -05:00
Marc-André Lureau
c5a3a4f2f9 swtpm-setup: follow XDG spec more closely for default config
According to the XDG spec,
https://specifications.freedesktop.org/basedir-spec/basedir-spec-latest.html:
"If $XDG_CONFIG_HOME is either not set or empty, a default equal to
$HOME/.config should be used."

This fixes setting up a TPM with libvirt running in a user session.

It works by checking if configuration files are readable in the
directory priority order (XDG_CONFIG_HOME, then HOME, then SYSCONFDIR).

When libvirt is running as a system instance, $HOME isn't set, so it
will fall back on @SYSCONFDIR@ (/etc usually)

Signed-off-by: Marc-André Lureau <marcandre.lureau@redhat.com>
2019-02-26 08:36:10 -05:00
Stefan Berger
7a3d1cde46 samples: Enable swtpm-localca to sign with a pkcs11 URI
Allow the user to specify a pkcs11 URI in the signingkey field
in swtpm-localca.conf.

Signed-off-by: Stefan Berger <stefanb@linux.ibm.com>
2019-01-23 12:48:43 -05:00
Stefan Berger
d16b86b7b0 build-sys: Adjust localstatedir and files using it
Adjust the localstatedir variable to not have ${prefix} if
prefix is '/usr/', otherwise use ${prefix}.

Also adjust the files that were pointing to directories that
are controlled by $localstatedir.

Signed-off-by: Stefan Berger <stefanb@linux.ibm.com>
2019-01-17 11:28:40 -05:00
Stefan Berger
0432b6535c build-sys: Fix hard coded /etc path and replace with @SYSCONFDIR@
When the project is configured with --prefix=/usr/local several configuration
files are installed to /usr/local/etc but not read by the programs that
need them. This patch fixes this issue by replacing @SYSCONFDIR@ in the source
code of those files with the actual used path.

Signed-off-by: Stefan Berger <stefanb@linux.ibm.com>
2019-01-17 11:28:40 -05:00
Stefan Berger
ccb00a55cd samples: Escape the GnuTLS PKCS11 URL before using in command line
GnuTLS PKCS11 URLs have ';' in the string that we need to preserve for
passing to GnuTLS. So we escape the semicolons before using them on the
command line.

Signed-off-by: Stefan Berger <stefanb@linux.ibm.com>
2018-12-11 13:44:36 -05:00
Stefan Berger
e0c04ff832 samples: Enable support for well known SRK password (TPM 1.2)
If tpmtool supports --srk-well-known we also support the well known
SRK password and allow the user not to provide an SRK password on
the command line.

This patch should have been applied before the previous patch that
tests this. Luckily, only very few systems have tpmtool with
--srk-well-known so that the order would matter there (and cause test
failures).

Signed-off-by: Stefan Berger <stefanb@linux.ibm.com>
2018-11-09 15:07:54 -05:00
Stefan Berger
588c06362b samples: Require an SRK password for TPM 1.2
Signed-off-by: Stefan Berger <stefanb@linux.ibm.com>
2018-11-05 15:23:11 -05:00
Stefan Berger
7be0768859 samples: Return result code from expect script to caller
Return the result code from the expect script to the caller.
Also, display possible log messages in case the private key file
was not written properly.

Signed-off-by: Stefan Berger <stefanb@linux.ibm.com>
2018-11-05 15:23:11 -05:00
Stefan Berger
3327d83878 samples: Fix a typo
Signed-off-by: Stefan Berger <stefanb@linux.ibm.com>
2018-10-25 10:32:36 -04:00
Stefan Berger
15a14c552a samples: Add support for password protected root CA priv. key
Support creation and usage of the root CA with a password protected
private key. The root CA's key password can be set using the environment
variable SWTPM_ROOTCA_PASSWORD.

Signed-off-by: Stefan Berger <stefanb@linux.ibm.com>
2018-10-25 10:32:36 -04:00
Stefan Berger
05f4d55f1f samples: Add a script to create a TPM 1.2 intermediate CA
This (experimental) script uses GnuTLS tpmtool to create a TPM 1.2
intermediate CA that can sign a vTPM's EK and platform certificates.

Add a man page for this tool.

Signed-off-by: Stefan Berger <stefanb@linux.ibm.com>
2018-10-15 20:45:48 -04:00
Stefan Berger
07c786d1a0 samples: Enable support for TPM 1.2 signing keys for cert signing via GnuTLS
GnuTLS can use TPM 1.2 keys for signing. For this we just need to support
key URLs in the GnuTLS format:

tpmkey:uuid=...
tpmkey:file=...

We don't try to read these URLs as files but pass them through as URLs to
the swtpm_cert tool that will then try to sign with the TPM 1.2 key by
passing the URL to GnuTLS API calls.

Signed-off-by: Stefan Berger <stefanb@linux.ibm.com>
2018-10-15 20:45:48 -04:00
Stefan Berger
33e1e50849 samples: Add a usage screen to swtpm-loclca to show upon --help
swtpm-localca was missing a help screen. This patch adds it and
is shown when --help or -? or -h is passed.

Signed-off-by: Stefan Berger <stefanb@linux.ibm.com>
2018-10-12 17:43:19 -04:00
Stefan Berger
8f0f381f95 scripting: Use #!/usr/bin/env bash rather than /bin/bash
On some systems /bin/bash does not exists but the bash is somewhere
else and can be invoked with /usr/bin/env bash.

Signed-off-by: Stefan Berger <stefanb@linux.vnet.ibm.com>
2018-09-10 16:49:12 +00:00
Stefan Berger
b63000fa8d samples: set CERTTOOL to gnutls-certtool on OS/X
Signed-off-by: Stefan Berger <stefanb@linux.vnet.ibm.com>
2018-08-30 15:39:00 -04:00
Stefan Berger
f13d2a045c sample: wrap flock in flock_fd for support on OS/X
OS/X has a flock (v0.2.3) implementation using

brew tap discoteq/discoteq
brew install flock

The current version doesn't support -x as a parameter for
an exclusive lock but uses an exclusive lock when no parameter
is passed at all.

Signed-off-by: Stefan Berger <stefanb@linux.vnet.ibm.com>
2018-08-30 15:27:25 -04:00
Stefan Berger
44eac994b6 samples: use flock -x rather than flock -e
flock -e does not work on NetBSD, so replace it with flock -x,
which should work everywhere.

Signed-off-by: Stefan Berger <stefanb@linux.vnet.ibm.com>
2018-08-29 13:01:40 +00:00
Stefan Berger
2266da36d9 samples: create lock with name .lock.swtpm-localca rather than .lock
To avoid clashes with the lock files created by swtpm, have
swtpm-localca creata a lock file with the name .lock.swtpm-localca
so they can share directories if necessary.

This patch makes tests/test_tpm2_swtpm_setup_create_cert run on
OpenBSD since the swtpm and swtpm-localca then do not interfere
with the file locks. This doesn't seem to be an issue on Linux
or Cygwin, though, where probably the file locking is implemented
differently.

Signed-off-by: Stefan Berger <stefanb@linux.vnet.ibm.om>
2018-08-26 16:03:53 -04:00
Stefan Berger
da6ca6fab6 samples: OpenBSD scripting fixes
OpenBSD uses an older sed implementation that doesn't understand the
meaning of the '+' for getting one or multiple occurrences of a letter.
Work around this by not using it and trimming white spaces at the end
of a variable using bash.

Use 100 as file descriptor rather than 200 to accomodate OpenBSD,
which only support file descriptors <= 127.

Signed-off-by: Stefan Berger <stefanb@linux.vnet.ibm.com>
2018-08-26 16:03:52 -04:00
Marc-André Lureau
611a198637 build-sys: fix make distcheck
- fix paths
- ignore chmod issues (they are more package/distro issues)
- scripts are already executable

Signed-off-by: Marc-André Lureau <marcandre.lureau@redhat.com>
2018-08-08 09:58:22 -04:00
Stefan Berger
8a55b8ac08 swtpm-localca: Cleanse variable before running eval on it
Cleanse the tmp variable before running eval on it. This is to prevent
execution of commands that a hidden in variable values read from a config
file. We only need to resolve the values of variables and don't want
the execution of a subshell command initated by either $(...) or `...` .

Signed-off-by: Stefan Berger <stefanb@linux.vnet.ibm.com>
2018-08-07 12:19:47 -04:00
Marc-André Lureau
f9cbc39124 swtpm-localca: create directory for keys if needed
Signed-off-by: Marc-André Lureau <marcandre.lureau@redhat.com>
2018-08-07 12:19:47 -04:00
Marc-André Lureau
a89f460d49 swtpm-localca: evaluate config variables
This will allow to expand environment variables in config files, such as:
statedir = $XDG_RUNTIME_DIR/swtpm-localca

Signed-off-by: Marc-André Lureau <marcandre.lureau@redhat.com>
2018-08-07 12:19:47 -04:00
Marc-André Lureau
331d78dbca swtpm: use XDG_CONFIG_HOME to locate config files
This allows to run swtpm_setup as regular user.

Signed-off-by: Marc-André Lureau <marcandre.lureau@redhat.com>
2018-08-07 12:19:47 -04:00
Stefan Berger
a3b68edd6d samples: add support for signing key password
Signed-off-by: Stefan Berger <stefanb@linux.vnet.ibm.com>
2018-08-07 12:19:47 -04:00
Stefan Berger
aaefebcf96 samples: add support for signing and key encipherment options
To allow the creation of EKs with signing and/or key encipherment
capabilities, add the --allow-signing and --decryption options
to swtpm-localca program.

Signed-off-by: Stefan Berger <stefanb@linux.vnet.ibm.com>
2018-08-07 12:19:46 -04:00